[lld] e11b709 - [ELF][PPC32] Support --emit-relocs link of R_PPC_PLTREL24

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 28 11:04:11 PST 2020


Author: Fangrui Song
Date: 2020-01-28T11:04:04-08:00
New Revision: e11b709b1922ca46b443fcfa5d76b87edca48721

URL: https://github.com/llvm/llvm-project/commit/e11b709b1922ca46b443fcfa5d76b87edca48721
DIFF: https://github.com/llvm/llvm-project/commit/e11b709b1922ca46b443fcfa5d76b87edca48721.diff

LOG: [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 at ha
addi 30, 30, .got2+0x8000-.L1$pb at l
...
bl foo+0x8000 at 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 at ha
addi 30, 30, .got2+0x8000-.L1+ppc32Got2OutSecOff$pb at ha$pb at l
...
bl foo+0x8000+ppc32Got2OutSecOff at 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

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

Modified: 
    lld/ELF/InputSection.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index ea203d1f445d..7c79879c8349 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -485,6 +485,14 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef<RelTy> rels) {
         p->r_addend = sym.getVA(addend) - section->getOutputSection()->addr;
       else if (config->relocatable && type != target->noneRel)
         sec->relocations.push_back({R_ABS, type, rel.r_offset, addend, &sym});
+    } else if (config->emachine == EM_PPC && type == R_PPC_PLTREL24 &&
+               p->r_addend >= 0x8000) {
+      // Similar to R_MIPS_GPREL{16,32}. If the addend of R_PPC_PLTREL24
+      // indicates that r30 is relative to the input section .got2
+      // (r_addend>=0x8000), after linking, r30 should be relative to the output
+      // section .got2 . To compensate for the shift, adjust r_addend by
+      // ppc32Got2OutSecOff.
+      p->r_addend += sec->file->ppc32Got2OutSecOff;
     }
   }
 }

diff  --git a/lld/test/ELF/ppc32-relocatable-got2.s b/lld/test/ELF/ppc32-relocatable-got2.s
new file mode 100644
index 000000000000..06ccb39b6a4a
--- /dev/null
+++ b/lld/test/ELF/ppc32-relocatable-got2.s
@@ -0,0 +1,41 @@
+# REQUIRES: ppc
+## Test addend adjustment of R_PPC_PLTREL24 when copying relocations.
+## If r_addend indicates .got2, adjust it by the local .got2's output section offset.
+
+# RUN: llvm-mc -filetype=obj -triple=powerpc %s -o %t.o
+# RUN: ld.lld -r %t.o %t.o -o %t
+# RUN: llvm-readobj -r %t | FileCheck %s
+
+# RUN: ld.lld -shared --emit-relocs %t.o %t.o -o %t.so
+# RUN: llvm-readobj -r %t.so | FileCheck %s
+
+# CHECK:      .rela.adjust {
+# CHECK-NEXT:   R_PPC_REL16_HA .got2 0x8002
+# CHECK-NEXT:   R_PPC_REL16_LO .got2 0x8006
+# CHECK-NEXT:   R_PPC_PLTREL24 foo 0x8000
+# CHECK-NEXT:   R_PPC_PLTREL24 bar 0x8000
+# CHECK-NEXT:   R_PPC_REL16_HA .got2 0x8006
+# CHECK-NEXT:   R_PPC_REL16_LO .got2 0x800A
+# CHECK-NEXT:   R_PPC_PLTREL24 foo 0x8004
+# CHECK-NEXT:   R_PPC_PLTREL24 bar 0x8004
+# CHECK-NEXT: }
+# CHECK-NEXT: .rela.no_adjust {
+# CHECK-NEXT:   R_PPC_PLTREL24 foo 0x0
+# CHECK-NEXT:   R_PPC_PLTREL24 foo 0x0
+# CHECK-NEXT: }
+.section .got2,"aw"
+.long 0
+
+.section .adjust,"ax"
+bcl 20,30,.L0
+.L0:
+addis 30,30,.got2+0x8000-.L0 at ha
+addi 30,30,.got2+0x8000-.L0 at l
+
+## Refers to .got2+addend, adjust.
+bl foo+0x8000 at plt
+bl bar+0x8000 at plt
+
+.section .no_adjust,"ax"
+## Refers to .got, no adjustment.
+bl foo at plt


        


More information about the llvm-commits mailing list