Skip to content

ELF: Have __rela_iplt_{start,end} surround .rela.iplt with --pack-dyn-relocs=android. #118585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions lld/ELF/Relocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,17 @@ template <class ELFT> void elf::scanRelocations(Ctx &ctx) {
outerFn();
}

RelocationBaseSection &elf::getIRelativeSection(Ctx &ctx) {
// Prior to Android V, there was a bug that caused RELR relocations to be
// applied after packed relocations. This meant that resolvers referenced by
// IRELATIVE relocations in the packed relocation section would read
// unrelocated globals with RELR relocations when
// --pack-relative-relocs=android+relr is enabled. Work around this by placing
// IRELATIVE in .rela.plt.
return ctx.arg.androidPackDynRelocs ? *ctx.in.relaPlt
: *ctx.mainPart->relaDyn;
}

static bool handleNonPreemptibleIfunc(Ctx &ctx, Symbol &sym, uint16_t flags) {
// Handle a reference to a non-preemptible ifunc. These are special in a
// few ways:
Expand Down Expand Up @@ -1736,17 +1747,9 @@ static bool handleNonPreemptibleIfunc(Ctx &ctx, Symbol &sym, uint16_t flags) {
// original section/value pairs. For non-GOT non-PLT relocation case below, we
// may alter section/value, so create a copy of the symbol to make
// section/value fixed.
//
// Prior to Android V, there was a bug that caused RELR relocations to be
// applied after packed relocations. This meant that resolvers referenced by
// IRELATIVE relocations in the packed relocation section would read
// unrelocated globals with RELR relocations when
// --pack-relative-relocs=android+relr is enabled. Work around this by placing
// IRELATIVE in .rela.plt.
auto *directSym = makeDefined(cast<Defined>(sym));
directSym->allocateAux(ctx);
auto &dyn =
ctx.arg.androidPackDynRelocs ? *ctx.in.relaPlt : *ctx.mainPart->relaDyn;
auto &dyn = getIRelativeSection(ctx);
addPltEntry(ctx, *ctx.in.iplt, *ctx.in.igotPlt, dyn, ctx.target->iRelativeRel,
*directSym);
sym.allocateAux(ctx);
Expand Down
3 changes: 3 additions & 0 deletions lld/ELF/Relocations.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Symbol;
class InputSection;
class InputSectionBase;
class OutputSection;
class RelocationBaseSection;
class SectionBase;

// Represents a relocation type, such as R_X86_64_PC32 or R_ARM_THM_CALL.
Expand Down Expand Up @@ -356,6 +357,8 @@ sortRels(Relocs<llvm::object::Elf_Crel_Impl<is64>> rels,
return {};
}

RelocationBaseSection &getIRelativeSection(Ctx &ctx);

// Returns true if Expr refers a GOT entry. Note that this function returns
// false for TLS variables even though they need GOT, because TLS variables uses
// GOT differently than the regular variables.
Expand Down
14 changes: 9 additions & 5 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,11 +844,15 @@ template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
ctx.sym.globalOffsetTable->section = sec;
}

// .rela_iplt_{start,end} mark the start and the end of .rel[a].dyn.
if (ctx.sym.relaIpltStart && ctx.mainPart->relaDyn->isNeeded()) {
ctx.sym.relaIpltStart->section = ctx.mainPart->relaDyn.get();
ctx.sym.relaIpltEnd->section = ctx.mainPart->relaDyn.get();
ctx.sym.relaIpltEnd->value = ctx.mainPart->relaDyn->getSize();
// .rela_iplt_{start,end} mark the start and the end of the section containing
// IRELATIVE relocations.
if (ctx.sym.relaIpltStart) {
auto &dyn = getIRelativeSection(ctx);
if (dyn.isNeeded()) {
ctx.sym.relaIpltStart->section = &dyn;
ctx.sym.relaIpltEnd->section = &dyn;
ctx.sym.relaIpltEnd->value = dyn.getSize();
}
}

PhdrEntry *last = nullptr;
Expand Down
25 changes: 25 additions & 0 deletions lld/test/ELF/pack-dyn-relocs-ifunc.s
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,28 @@ _start:
.globl bar
bar:
ret

#--- c.s

# RUN: llvm-mc -filetype=obj -triple=aarch64-linux-android c.s -o c.o
# RUN: ld.lld --pack-dyn-relocs=android c.o -o c
# RUN: llvm-readelf -sS c | FileCheck --check-prefix=STATIC %s

# STATIC: .rela.plt RELA 0000000000200158 000158 000018 18 AI 0 5 8
# STATIC: 0000000000200158 0 NOTYPE LOCAL HIDDEN 1 __rela_iplt_start
# STATIC: 0000000000200170 0 NOTYPE LOCAL HIDDEN 1 __rela_iplt_end

.text
.type foo, %gnu_indirect_function
.globl foo
foo:
ret

.globl _start
_start:
bl foo

.data
.balign 8
.quad __rela_iplt_start
.quad __rela_iplt_end
Loading