Skip to content

Commit bc45ea2

Browse files
authored
[ELF] Fix IRELATIVE addend if the resolver address is updated by linker relaxation (llvm#179063)
For a non-preemptible ifunc, `handleNonPreemptibleIfunc` creates a cloned symbol (`directSym`) to compute the addend of the IRELATIVE dynamic relocation. This cloned symbol wasn't tracked by `initSymbolAnchors`, so its value wasn't adjusted during RISC-V/LoongArch linker relaxation. This caused IRELATIVE addends to point to pre-relaxation addresses. Fix this by: - Tracking cloned IRELATIVE symbols in `ctx.irelativeSyms` - Adding these symbols to `relaxAux->anchors` in `initSymbolAnchors`
1 parent d43e735 commit bc45ea2

5 files changed

Lines changed: 204 additions & 92 deletions

File tree

lld/ELF/Arch/RISCV.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -714,28 +714,33 @@ void elf::initSymbolAnchors(Ctx &ctx) {
714714
}
715715
}
716716
}
717-
// Store anchors (st_value and st_value+st_size) for symbols relative to text
718-
// sections.
717+
// Store symbol anchors for adjusting st_value/st_size during relaxation.
718+
// We include symbols where d->file == file for the prevailing copies.
719719
//
720720
// For a defined symbol foo, we may have `d->file != file` with --wrap=foo.
721721
// We should process foo, as the defining object file's symbol table may not
722-
// contain foo after redirectSymbols changed the foo entry to __wrap_foo. To
723-
// avoid adding a Defined that is undefined in one object file, use
724-
// `!d->scriptDefined` to exclude symbols that are definitely not wrapped.
722+
// contain foo after redirectSymbols changed the foo entry to __wrap_foo. Use
723+
// `d->scriptDefined` to include such symbols.
725724
//
726725
// `relaxAux->anchors` may contain duplicate symbols, but that is fine.
726+
auto addAnchor = [](Defined *d) {
727+
if (auto *sec = dyn_cast_or_null<InputSection>(d->section))
728+
if (sec->flags & SHF_EXECINSTR && sec->relaxAux) {
729+
// If sec is discarded, relaxAux will be nullptr.
730+
sec->relaxAux->anchors.push_back({d->value, d, false});
731+
sec->relaxAux->anchors.push_back({d->value + d->size, d, true});
732+
}
733+
};
727734
for (InputFile *file : ctx.objectFiles)
728735
for (Symbol *sym : file->getSymbols()) {
729736
auto *d = dyn_cast<Defined>(sym);
730-
if (!d || (d->file != file && !d->scriptDefined))
731-
continue;
732-
if (auto *sec = dyn_cast_or_null<InputSection>(d->section))
733-
if (sec->flags & SHF_EXECINSTR && sec->relaxAux) {
734-
// If sec is discarded, relaxAux will be nullptr.
735-
sec->relaxAux->anchors.push_back({d->value, d, false});
736-
sec->relaxAux->anchors.push_back({d->value + d->size, d, true});
737-
}
737+
if (d && (d->file == file || d->scriptDefined))
738+
addAnchor(d);
738739
}
740+
// Add anchors for IRELATIVE symbols (see `handleNonPreemptibleIfunc`).
741+
// Their values must be adjusted so IRELATIVE addends remain correct.
742+
for (Defined *d : ctx.irelativeSyms)
743+
addAnchor(d);
739744
// Sort anchors by offset so that we can find the closest relocation
740745
// efficiently. For a zero size symbol, ensure that its start anchor precedes
741746
// its end anchor. For two symbols with anchors at the same offset, their

lld/ELF/Config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,9 @@ struct Ctx : CommonLinkerContext {
668668
ElfSym sym{};
669669
std::unique_ptr<SymbolTable> symtab;
670670
SmallVector<Symbol *, 0> synthesizedSymbols;
671+
// ifunc resolver symbol clones for IRELATIVE. Linker relaxation adjusts
672+
// these.
673+
SmallVector<Defined *, 0> irelativeSyms;
671674

672675
SmallVector<std::unique_ptr<MemoryBuffer>> memoryBuffers;
673676
SmallVector<ELFFileBase *, 0> objectFiles;

lld/ELF/Relocations.cpp

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,42 +1454,25 @@ RelocationBaseSection &elf::getIRelativeSection(Ctx &ctx) {
14541454
}
14551455

14561456
static bool handleNonPreemptibleIfunc(Ctx &ctx, Symbol &sym, uint16_t flags) {
1457-
// Handle a reference to a non-preemptible ifunc. These are special in a
1458-
// few ways:
1457+
// Non-preemptible ifuncs are called via a PLT entry that resolves the actual
1458+
// address at runtime. We create an IPLT entry and an IGOTPLT slot. The
1459+
// IGOTPLT slot is relocated by an IRELATIVE relocation, whose addend encodes
1460+
// the resolver address. At startup, the runtime calls the resolver and
1461+
// fills the IGOTPLT slot.
14591462
//
1460-
// - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have
1461-
// a fixed value. But assuming that all references to the ifunc are
1462-
// GOT-generating or PLT-generating, the handling of an ifunc is
1463-
// relatively straightforward. We create a PLT entry in Iplt, which is
1464-
// usually at the end of .plt, which makes an indirect call using a
1465-
// matching GOT entry in igotPlt, which is usually at the end of .got.plt.
1466-
// The GOT entry is relocated using an IRELATIVE relocation in relaDyn,
1467-
// which is usually at the end of .rela.dyn.
1463+
// For direct (non-GOT/PLT) relocations, the symbol must have a constant
1464+
// address. We achieve this by redirecting the symbol to its IPLT entry
1465+
// ("canonicalizing" it), so all references see the same address, and the
1466+
// resolver is called exactly once. This may result in two GOT entries: one
1467+
// in .got.plt for the IRELATIVE, and one in .got pointing to the canonical
1468+
// IPLT entry (for GOT-generating relocations).
14681469
//
1469-
// - Despite the fact that an ifunc does not have a fixed value, compilers
1470-
// that are not passed -fPIC will assume that they do, and will emit
1471-
// direct (non-GOT-generating, non-PLT-generating) relocations to the
1472-
// symbol. This means that if a direct relocation to the symbol is
1473-
// seen, the linker must set a value for the symbol, and this value must
1474-
// be consistent no matter what type of reference is made to the symbol.
1475-
// This can be done by creating a PLT entry for the symbol in the way
1476-
// described above and making it canonical, that is, making all references
1477-
// point to the PLT entry instead of the resolver. In lld we also store
1478-
// the address of the PLT entry in the dynamic symbol table, which means
1479-
// that the symbol will also have the same value in other modules.
1480-
// Because the value loaded from the GOT needs to be consistent with
1481-
// the value computed using a direct relocation, a non-preemptible ifunc
1482-
// may end up with two GOT entries, one in .got.plt that points to the
1483-
// address returned by the resolver and is used only by the PLT entry,
1484-
// and another in .got that points to the PLT entry and is used by
1485-
// GOT-generating relocations.
1470+
// We clone the symbol to preserve the original resolver address for the
1471+
// IRELATIVE addend. The clone is tracked in ctx.irelativeSyms so that linker
1472+
// relaxation can adjust its value when the resolver address changes.
14861473
//
1487-
// - The fact that these symbols do not have a fixed value makes them an
1488-
// exception to the general rule that a statically linked executable does
1489-
// not require any form of dynamic relocation. To handle these relocations
1490-
// correctly, the IRELATIVE relocations are stored in an array which a
1491-
// statically linked executable's startup code must enumerate using the
1492-
// linker-defined symbols __rela?_iplt_{start,end}.
1474+
// Note: IRELATIVE relocations are needed even in static executables; see
1475+
// `addRelIpltSymbols`.
14931476
if (!sym.isGnuIFunc() || sym.isPreemptible || ctx.arg.zIfuncNoplt)
14941477
return false;
14951478
// Skip unreferenced non-preemptible ifunc.
@@ -1498,17 +1481,14 @@ static bool handleNonPreemptibleIfunc(Ctx &ctx, Symbol &sym, uint16_t flags) {
14981481

14991482
sym.isInIplt = true;
15001483

1501-
// Create an Iplt and the associated IRELATIVE relocation pointing to the
1502-
// original section/value pairs. For non-GOT non-PLT relocation case below, we
1503-
// may alter section/value, so create a copy of the symbol to make
1504-
// section/value fixed.
1505-
auto *directSym = makeDefined(cast<Defined>(sym));
1506-
directSym->allocateAux(ctx);
1484+
auto *irelativeSym = makeDefined(cast<Defined>(sym));
1485+
irelativeSym->allocateAux(ctx);
1486+
ctx.irelativeSyms.push_back(irelativeSym);
15071487
auto &dyn = getIRelativeSection(ctx);
15081488
addPltEntry(ctx, *ctx.in.iplt, *ctx.in.igotPlt, dyn, ctx.target->iRelativeRel,
1509-
*directSym);
1489+
*irelativeSym);
15101490
sym.allocateAux(ctx);
1511-
ctx.symAux.back().pltIdx = ctx.symAux[directSym->auxIdx].pltIdx;
1491+
ctx.symAux.back().pltIdx = ctx.symAux[irelativeSym->auxIdx].pltIdx;
15121492

15131493
if (flags & HAS_DIRECT_RELOC) {
15141494
// Change the value to the IPLT and redirect all references to it.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# REQUIRES: loongarch
2+
# RUN: llvm-mc -filetype=obj -triple=loongarch64 -mattr=+relax %s -o %t.o
3+
# RUN: ld.lld -pie %t.o -o %t
4+
# RUN: llvm-readobj -r %t | FileCheck --check-prefix=RELOC %s
5+
# RUN: llvm-readelf -s %t | FileCheck --check-prefix=SYM %s
6+
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=DIS %s
7+
8+
## ifunc0 has a direct relocation, so it gets canonicalized to the IPLT entry.
9+
## ifunc1 has only a GOT relocation, so its symbol remains in the original section.
10+
## ifunc2 has both direct and GOT relocations, so it gets canonicalized to the IPLT entry.
11+
## All IRELATIVE addends must be correctly adjusted after relaxation.
12+
13+
# RELOC: .rela.dyn {
14+
# RELOC-NEXT: 0x203E0 R_LARCH_RELATIVE - 0x10300
15+
# RELOC-NEXT: 0x303E8 R_LARCH_IRELATIVE - 0x102D0
16+
# RELOC-NEXT: 0x303F0 R_LARCH_IRELATIVE - 0x102D4
17+
# RELOC-NEXT: 0x303F8 R_LARCH_IRELATIVE - 0x102D8
18+
# RELOC-NEXT: }
19+
20+
# SYM: {{0*}}102e0 0 FUNC GLOBAL DEFAULT {{.*}} ifunc0
21+
# SYM-NEXT: {{0*}}102d4 0 IFUNC GLOBAL DEFAULT {{.*}} ifunc1
22+
# SYM-NEXT: {{0*}}10300 0 FUNC GLOBAL DEFAULT {{.*}} ifunc2
23+
24+
# DIS: <_start>:
25+
# DIS-NEXT: 102a8: bl 36 <func>
26+
# DIS-NEXT: pcalau12i $a0, 0
27+
# DIS-NEXT: addi.d $a0, $a0, 736
28+
# DIS-NEXT: pcalau12i $a1, 32
29+
# DIS-NEXT: ld.d $a1, $a1, 1008
30+
# DIS-NEXT: pcalau12i $a2, 0
31+
# DIS-NEXT: addi.d $a2, $a2, 768
32+
# DIS-NEXT: pcalau12i $a3, 0
33+
# DIS-NEXT: addi.d $a3, $a3, 768
34+
# DIS: Disassembly of section .iplt:
35+
# DIS: <ifunc0>:
36+
# DIS-NEXT: 102e0: pcaddu12i $t3, 32
37+
38+
.text
39+
.globl _start
40+
_start:
41+
call36 func
42+
.L0:
43+
pcalau12i $a0, %pc_hi20(ifunc0)
44+
addi.d $a0, $a0, %pc_lo12(ifunc0)
45+
.L1:
46+
pcalau12i $a1, %got_pc_hi20(ifunc1)
47+
ld.d $a1, $a1, %got_pc_lo12(ifunc1)
48+
.L2:
49+
pcalau12i $a2, %pc_hi20(ifunc2)
50+
addi.d $a2, $a2, %pc_lo12(ifunc2)
51+
.L3:
52+
pcalau12i $a3, %got_pc_hi20(ifunc2)
53+
ld.d $a3, $a3, %got_pc_lo12(ifunc2)
54+
55+
.globl func
56+
func:
57+
ret
58+
59+
## Resolvers are after relaxed code, so their addresses shift due to relaxation.
60+
## The IRELATIVE addends must be adjusted accordingly.
61+
.globl ifunc0, ifunc1, ifunc2
62+
.type ifunc0, @gnu_indirect_function
63+
.type ifunc1, @gnu_indirect_function
64+
.type ifunc2, @gnu_indirect_function
65+
ifunc0:
66+
ret
67+
ifunc1:
68+
ret
69+
ifunc2:
70+
ret
Lines changed: 92 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,124 @@
11
# REQUIRES: riscv
2-
# RUN: llvm-mc -filetype=obj -triple=riscv32 %s -o %t.32.o
3-
# RUN: ld.lld -pie %t.32.o -o %t.32
4-
# RUN: ld.lld -pie %t.32.o -o %t.32-apply --apply-dynamic-relocs
2+
# RUN: llvm-mc -filetype=obj -triple=riscv32 %s -mattr=+relax -o %t.32.o
3+
# DEFINE: %{layout} = --section-start .rela.dyn=0x1000 -Ttext=0x2000 --section-start=.iplt=0x3000
4+
# RUN: ld.lld -pie %{layout} %t.32.o -o %t.32
5+
# RUN: ld.lld -pie %{layout} %t.32.o -o %t.32-apply --apply-dynamic-relocs
56
# RUN: llvm-readobj -r -x .got.plt %t.32 | FileCheck --check-prefixes=RELOC32,NO-APPLY-RELOC32 %s
67
# RUN: llvm-readobj -r -x .got.plt %t.32-apply | FileCheck --check-prefixes=RELOC32,APPLY-RELOC32 %s
78
# RUN: llvm-readelf -s %t.32 | FileCheck --check-prefix=SYM32 %s
89
# RUN: llvm-objdump -d --no-show-raw-insn %t.32 | FileCheck --check-prefix=DIS32 %s
910

10-
# RUN: llvm-mc -filetype=obj -triple=riscv64 %s -o %t.64.o
11-
# RUN: ld.lld -pie %t.64.o -o %t.64
12-
# RUN: ld.lld -pie %t.64.o -o %t.64-apply --apply-dynamic-relocs
11+
# RUN: llvm-mc -filetype=obj -triple=riscv64 %s -mattr=+relax -o %t.64.o
12+
# RUN: ld.lld -pie %{layout} %t.64.o -o %t.64
13+
# RUN: ld.lld -pie %{layout} %t.64.o -o %t.64-apply --apply-dynamic-relocs
1314
# RUN: llvm-readobj -r -x .got.plt %t.64 | FileCheck --check-prefixes=RELOC64,NO-APPLY-RELOC64 %s
1415
# RUN: llvm-readobj -r -x .got.plt %t.64-apply | FileCheck --check-prefixes=RELOC64,APPLY-RELOC64 %s
1516
# RUN: llvm-readelf -s %t.64 | FileCheck --check-prefix=SYM64 %s
1617
# RUN: llvm-objdump -d --no-show-raw-insn %t.64 | FileCheck --check-prefix=DIS64 %s
1718

19+
## ifunc0 has a direct relocation, so it gets canonicalized to the IPLT entry.
20+
## ifunc1 has only a GOT relocation, so its symbol remains in the original section.
21+
## ifunc2 has both direct and GOT relocations, so it gets canonicalized to the IPLT entry.
22+
## All IRELATIVE addends must be correctly adjusted after relaxation.
23+
1824
# RELOC32: .rela.dyn {
19-
# RELOC32-NEXT: 0x3200 R_RISCV_IRELATIVE - 0x117C
25+
# RELOC32-NEXT: 0x50D8 R_RISCV_RELATIVE - 0x3020
26+
# RELOC32-NEXT: 0x60DC R_RISCV_IRELATIVE - 0x2028
27+
# RELOC32-NEXT: 0x60E0 R_RISCV_IRELATIVE - 0x202C
28+
# RELOC32-NEXT: 0x60E4 R_RISCV_IRELATIVE - 0x2030
2029
# RELOC32-NEXT: }
2130
# RELOC32-LABEL: Hex dump of section '.got.plt':
22-
# NO-APPLY-RELOC32: 0x00003200 00000000
23-
# APPLY-RELOC32: 0x00003200 7c110000
24-
# RELOC32-EMPTY:
31+
# NO-APPLY-RELOC32: 0x000060dc 00000000 00000000 00000000
32+
# APPLY-RELOC32: 0x000060dc 28200000 2c200000 30200000
2533

26-
# SYM32: 0001190 0 FUNC GLOBAL DEFAULT {{.*}} func
34+
# SYM32: {{0*}}3000 0 FUNC GLOBAL DEFAULT {{.*}} ifunc0
35+
# SYM32-NEXT: {{0*}}202c 0 IFUNC GLOBAL DEFAULT {{.*}} ifunc1
36+
# SYM32-NEXT: {{0*}}3020 0 FUNC GLOBAL DEFAULT {{.*}} ifunc2
2737

2838
# DIS32: <_start>:
29-
# DIS32-NEXT: 1180: auipc a0, 0x0
30-
# DIS32-NEXT: addi a0, a0, 0x10
39+
# DIS32-NEXT: 2000: jal 0x2024 <func>
40+
# DIS32: <.L0>:
41+
# DIS32-NEXT: 2004: auipc a0, 0x1
42+
# DIS32-NEXT: addi a0, a0, -0x4
43+
# DIS32: <.L1>:
44+
# DIS32-NEXT: 200c: auipc a1, 0x4
45+
# DIS32-NEXT: addi a1, a1, 0xd4
46+
# DIS32: <.L2>:
47+
# DIS32-NEXT: 2014: auipc a2, 0x1
48+
# DIS32-NEXT: addi a2, a2, 0xc
49+
# DIS32: <.L3>:
50+
# DIS32-NEXT: 201c: auipc a3, 0x3
51+
# DIS32-NEXT: addi a3, a3, 0xbc
3152
# DIS32: Disassembly of section .iplt:
32-
# DIS32: <func>:
33-
## 32-bit: &.got.plt[func]-. = 0x3200-0x1190 = 4096*2+0x70
34-
# DIS32-NEXT: 1190: auipc t3, 0x2
35-
# DIS32-NEXT: lw t3, 0x70(t3)
36-
# DIS32-NEXT: jalr t1, t3
37-
# DIS32-NEXT: nop
53+
# DIS32: <ifunc0>:
54+
## 32-bit: &.got.plt[ifunc0]-. = 0x60dc-0x3000 = 4096*3+0xdc
55+
# DIS32-NEXT: 3000: auipc t3, 0x3
56+
# DIS32-NEXT: lw t3, 0xdc(t3)
3857

3958
# RELOC64: .rela.dyn {
40-
# RELOC64-NEXT: 0x3340 R_RISCV_IRELATIVE - 0x1260
59+
# RELOC64-NEXT: 0x5150 R_RISCV_RELATIVE - 0x3020
60+
# RELOC64-NEXT: 0x6158 R_RISCV_IRELATIVE - 0x2028
61+
# RELOC64-NEXT: 0x6160 R_RISCV_IRELATIVE - 0x202C
62+
# RELOC64-NEXT: 0x6168 R_RISCV_IRELATIVE - 0x2030
4163
# RELOC64-NEXT: }
4264
# RELOC64-LABEL: Hex dump of section '.got.plt':
43-
# NO-APPLY-RELOC64: 0x00003340 00000000 00000000
44-
# APPLY-RELOC64: 0x00003340 60120000 00000000
45-
# RELOC64-EMPTY:
65+
# NO-APPLY-RELOC64: 0x00006158 00000000 00000000 00000000 00000000
66+
# APPLY-RELOC64: 0x00006158 28200000 00000000 2c200000 00000000
4667

47-
# SYM64: 000000000001270 0 FUNC GLOBAL DEFAULT {{.*}} func
68+
# SYM64: {{0*}}3000 0 FUNC GLOBAL DEFAULT {{.*}} ifunc0
69+
# SYM64-NEXT: {{0*}}202c 0 IFUNC GLOBAL DEFAULT {{.*}} ifunc1
70+
# SYM64-NEXT: {{0*}}3020 0 FUNC GLOBAL DEFAULT {{.*}} ifunc2
4871

4972
# DIS64: <_start>:
50-
# DIS64-NEXT: 1264: auipc a0, 0x0
51-
# DIS64-NEXT: addi a0, a0, 0xc
73+
# DIS64-NEXT: 2000: jal 0x2024 <func>
74+
# DIS64: <.L0>:
75+
# DIS64-NEXT: 2004: auipc a0, 0x1
76+
# DIS64-NEXT: addi a0, a0, -0x4
77+
# DIS64: <.L1>:
78+
# DIS64-NEXT: 200c: auipc a1, 0x4
79+
# DIS64-NEXT: addi a1, a1, 0x154
80+
# DIS64: <.L2>:
81+
# DIS64-NEXT: 2014: auipc a2, 0x1
82+
# DIS64-NEXT: addi a2, a2, 0xc
83+
# DIS64: <.L3>:
84+
# DIS64-NEXT: 201c: auipc a3, 0x3
85+
# DIS64-NEXT: addi a3, a3, 0x134
5286
# DIS64: Disassembly of section .iplt:
53-
# DIS64: <func>:
54-
## 64-bit: &.got.plt[func]-. = 0x3340-0x1270 = 4096*2+0xd0
55-
# DIS64-NEXT: 1270: auipc t3, 0x2
56-
# DIS64-NEXT: ld t3, 0xd0(t3)
57-
# DIS64-NEXT: jalr t1, t3
58-
# DIS64-NEXT: nop
87+
# DIS64: <ifunc0>:
88+
## 64-bit: &.got.plt[ifunc0]-. = 0x6158-0x3000 = 4096*3+0x158
89+
# DIS64-NEXT: 3000: auipc t3, 0x3
90+
# DIS64-NEXT: ld t3, 0x158(t3)
5991

6092
.text
93+
.globl _start
94+
_start:
95+
call func
96+
.L0:
97+
auipc a0, %pcrel_hi(ifunc0)
98+
addi a0, a0, %pcrel_lo(.L0)
99+
.L1:
100+
auipc a1, %got_pcrel_hi(ifunc1)
101+
addi a1, a1, %pcrel_lo(.L1)
102+
.L2:
103+
auipc a2, %pcrel_hi(ifunc2)
104+
addi a2, a2, %pcrel_lo(.L2)
105+
.L3:
106+
auipc a3, %got_pcrel_hi(ifunc2)
107+
addi a3, a3, %pcrel_lo(.L3)
108+
61109
.globl func
62-
.type func, @gnu_indirect_function
63110
func:
64111
ret
65112

66-
.globl _start
67-
_start:
68-
.L:
69-
auipc a0, %pcrel_hi(func)
70-
addi a0, a0, %pcrel_lo(.L)
113+
## Resolvers are after relaxed code, so their addresses shift due to relaxation.
114+
## The IRELATIVE addends must be adjusted accordingly.
115+
.globl ifunc0, ifunc1, ifunc2
116+
.type ifunc0, @gnu_indirect_function
117+
.type ifunc1, @gnu_indirect_function
118+
.type ifunc2, @gnu_indirect_function
119+
ifunc0:
120+
ret
121+
ifunc1:
122+
ret
123+
ifunc2:
124+
ret

0 commit comments

Comments
 (0)