Skip to content

Commit 970d6d2

Browse files
authored
ELF: Have __rela_iplt_{start,end} surround .rela.iplt with --pack-dyn-relocs=android.
In #86751 we moved the IRELATIVE relocations to .rela.plt when --pack-dyn-relocs=android was enabled but we neglected to also move the __rela_iplt_{start,end} symbols. As a result, static binaries linked with this flag were unable to find their IRELATIVE relocations. Fix it by having the symbols surround the correct section. Reviewers: MaskRay, smithp35 Reviewed By: MaskRay Pull Request: #118585
1 parent af4ae12 commit 970d6d2

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

lld/ELF/Relocations.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,17 @@ template <class ELFT> void elf::scanRelocations(Ctx &ctx) {
16871687
outerFn();
16881688
}
16891689

1690+
RelocationBaseSection &elf::getIRelativeSection(Ctx &ctx) {
1691+
// Prior to Android V, there was a bug that caused RELR relocations to be
1692+
// applied after packed relocations. This meant that resolvers referenced by
1693+
// IRELATIVE relocations in the packed relocation section would read
1694+
// unrelocated globals with RELR relocations when
1695+
// --pack-relative-relocs=android+relr is enabled. Work around this by placing
1696+
// IRELATIVE in .rela.plt.
1697+
return ctx.arg.androidPackDynRelocs ? *ctx.in.relaPlt
1698+
: *ctx.mainPart->relaDyn;
1699+
}
1700+
16901701
static bool handleNonPreemptibleIfunc(Ctx &ctx, Symbol &sym, uint16_t flags) {
16911702
// Handle a reference to a non-preemptible ifunc. These are special in a
16921703
// few ways:
@@ -1736,17 +1747,9 @@ static bool handleNonPreemptibleIfunc(Ctx &ctx, Symbol &sym, uint16_t flags) {
17361747
// original section/value pairs. For non-GOT non-PLT relocation case below, we
17371748
// may alter section/value, so create a copy of the symbol to make
17381749
// section/value fixed.
1739-
//
1740-
// Prior to Android V, there was a bug that caused RELR relocations to be
1741-
// applied after packed relocations. This meant that resolvers referenced by
1742-
// IRELATIVE relocations in the packed relocation section would read
1743-
// unrelocated globals with RELR relocations when
1744-
// --pack-relative-relocs=android+relr is enabled. Work around this by placing
1745-
// IRELATIVE in .rela.plt.
17461750
auto *directSym = makeDefined(cast<Defined>(sym));
17471751
directSym->allocateAux(ctx);
1748-
auto &dyn =
1749-
ctx.arg.androidPackDynRelocs ? *ctx.in.relaPlt : *ctx.mainPart->relaDyn;
1752+
auto &dyn = getIRelativeSection(ctx);
17501753
addPltEntry(ctx, *ctx.in.iplt, *ctx.in.igotPlt, dyn, ctx.target->iRelativeRel,
17511754
*directSym);
17521755
sym.allocateAux(ctx);

lld/ELF/Relocations.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Symbol;
2222
class InputSection;
2323
class InputSectionBase;
2424
class OutputSection;
25+
class RelocationBaseSection;
2526
class SectionBase;
2627

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

360+
RelocationBaseSection &getIRelativeSection(Ctx &ctx);
361+
359362
// Returns true if Expr refers a GOT entry. Note that this function returns
360363
// false for TLS variables even though they need GOT, because TLS variables uses
361364
// GOT differently than the regular variables.

lld/ELF/Writer.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -844,11 +844,15 @@ template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
844844
ctx.sym.globalOffsetTable->section = sec;
845845
}
846846

847-
// .rela_iplt_{start,end} mark the start and the end of .rel[a].dyn.
848-
if (ctx.sym.relaIpltStart && ctx.mainPart->relaDyn->isNeeded()) {
849-
ctx.sym.relaIpltStart->section = ctx.mainPart->relaDyn.get();
850-
ctx.sym.relaIpltEnd->section = ctx.mainPart->relaDyn.get();
851-
ctx.sym.relaIpltEnd->value = ctx.mainPart->relaDyn->getSize();
847+
// .rela_iplt_{start,end} mark the start and the end of the section containing
848+
// IRELATIVE relocations.
849+
if (ctx.sym.relaIpltStart) {
850+
auto &dyn = getIRelativeSection(ctx);
851+
if (dyn.isNeeded()) {
852+
ctx.sym.relaIpltStart->section = &dyn;
853+
ctx.sym.relaIpltEnd->section = &dyn;
854+
ctx.sym.relaIpltEnd->value = dyn.getSize();
855+
}
852856
}
853857

854858
PhdrEntry *last = nullptr;

lld/test/ELF/pack-dyn-relocs-ifunc.s

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,28 @@ _start:
4747
.globl bar
4848
bar:
4949
ret
50+
51+
#--- c.s
52+
53+
# RUN: llvm-mc -filetype=obj -triple=aarch64-linux-android c.s -o c.o
54+
# RUN: ld.lld --pack-dyn-relocs=android c.o -o c
55+
# RUN: llvm-readelf -sS c | FileCheck --check-prefix=STATIC %s
56+
57+
# STATIC: .rela.plt RELA 0000000000200158 000158 000018 18 AI 0 5 8
58+
# STATIC: 0000000000200158 0 NOTYPE LOCAL HIDDEN 1 __rela_iplt_start
59+
# STATIC: 0000000000200170 0 NOTYPE LOCAL HIDDEN 1 __rela_iplt_end
60+
61+
.text
62+
.type foo, %gnu_indirect_function
63+
.globl foo
64+
foo:
65+
ret
66+
67+
.globl _start
68+
_start:
69+
bl foo
70+
71+
.data
72+
.balign 8
73+
.quad __rela_iplt_start
74+
.quad __rela_iplt_end

0 commit comments

Comments
 (0)