Skip to content

Commit e11b709

Browse files
committed
[ELF][PPC32] Support --emit-relocs link of R_PPC_PLTREL24
Similar to R_MIPS_GPREL16 and R_MIPS_GPREL32 (D45972). If the addend of an R_PPC_PLTREL24 is >= 0x8000, it indicates that r30 is relative to the input section .got2. ``` addis 30, 30, .got2+0x8000-.L1$pb@ha addi 30, 30, .got2+0x8000-.L1$pb@l ... bl foo+0x8000@PLT ``` After linking, the relocation will be relative to the output section .got2. To compensate for the shift `address(input section .got2) - address(output section .got2) = ppc32Got2OutSecOff`, adjust by `ppc32Got2OutSecOff`: ``` addis 30, 30, .got2+0x8000-.L1+ppc32Got2OutSecOff$pb@ha addi 30, 30, .got2+0x8000-.L1+ppc32Got2OutSecOff$pb@ha$pb@l ... bl foo+0x8000+ppc32Got2OutSecOff@PLT ``` This rule applys to a relocatable link or a non-relocatable link with --emit-relocs. Reviewed By: Bdragon28 Differential Revision: https://reviews.llvm.org/D73532
1 parent a1f6ff2 commit e11b709

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

lld/ELF/InputSection.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,14 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef<RelTy> rels) {
485485
p->r_addend = sym.getVA(addend) - section->getOutputSection()->addr;
486486
else if (config->relocatable && type != target->noneRel)
487487
sec->relocations.push_back({R_ABS, type, rel.r_offset, addend, &sym});
488+
} else if (config->emachine == EM_PPC && type == R_PPC_PLTREL24 &&
489+
p->r_addend >= 0x8000) {
490+
// Similar to R_MIPS_GPREL{16,32}. If the addend of R_PPC_PLTREL24
491+
// indicates that r30 is relative to the input section .got2
492+
// (r_addend>=0x8000), after linking, r30 should be relative to the output
493+
// section .got2 . To compensate for the shift, adjust r_addend by
494+
// ppc32Got2OutSecOff.
495+
p->r_addend += sec->file->ppc32Got2OutSecOff;
488496
}
489497
}
490498
}

lld/test/ELF/ppc32-relocatable-got2.s

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# REQUIRES: ppc
2+
## Test addend adjustment of R_PPC_PLTREL24 when copying relocations.
3+
## If r_addend indicates .got2, adjust it by the local .got2's output section offset.
4+
5+
# RUN: llvm-mc -filetype=obj -triple=powerpc %s -o %t.o
6+
# RUN: ld.lld -r %t.o %t.o -o %t
7+
# RUN: llvm-readobj -r %t | FileCheck %s
8+
9+
# RUN: ld.lld -shared --emit-relocs %t.o %t.o -o %t.so
10+
# RUN: llvm-readobj -r %t.so | FileCheck %s
11+
12+
# CHECK: .rela.adjust {
13+
# CHECK-NEXT: R_PPC_REL16_HA .got2 0x8002
14+
# CHECK-NEXT: R_PPC_REL16_LO .got2 0x8006
15+
# CHECK-NEXT: R_PPC_PLTREL24 foo 0x8000
16+
# CHECK-NEXT: R_PPC_PLTREL24 bar 0x8000
17+
# CHECK-NEXT: R_PPC_REL16_HA .got2 0x8006
18+
# CHECK-NEXT: R_PPC_REL16_LO .got2 0x800A
19+
# CHECK-NEXT: R_PPC_PLTREL24 foo 0x8004
20+
# CHECK-NEXT: R_PPC_PLTREL24 bar 0x8004
21+
# CHECK-NEXT: }
22+
# CHECK-NEXT: .rela.no_adjust {
23+
# CHECK-NEXT: R_PPC_PLTREL24 foo 0x0
24+
# CHECK-NEXT: R_PPC_PLTREL24 foo 0x0
25+
# CHECK-NEXT: }
26+
.section .got2,"aw"
27+
.long 0
28+
29+
.section .adjust,"ax"
30+
bcl 20,30,.L0
31+
.L0:
32+
addis 30,30,.got2+0x8000-.L0@ha
33+
addi 30,30,.got2+0x8000-.L0@l
34+
35+
## Refers to .got2+addend, adjust.
36+
bl foo+0x8000@plt
37+
bl bar+0x8000@plt
38+
39+
.section .no_adjust,"ax"
40+
## Refers to .got, no adjustment.
41+
bl foo@plt

0 commit comments

Comments
 (0)