[PATCH] D128492: ELF: Do not relax ADRP/LDR -> ADRP/ADD for absolute symbols in PIC.

Peter Collingbourne via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 23 22:23:17 PDT 2022


pcc created this revision.
pcc added reviewers: MaskRay, peter.smith.
Herald added subscribers: StephenFan, arichardson, emaste.
Herald added a project: All.
pcc requested review of this revision.
Herald added a project: LLVM.

GOT references to absolute symbols can't be relaxed to use ADRP/ADD in
position-independent code because these instructions produce a relative
address.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128492

Files:
  lld/ELF/Arch/AArch64.cpp
  lld/test/ELF/aarch64-adrp-ldr-got-symbols.s


Index: lld/test/ELF/aarch64-adrp-ldr-got-symbols.s
===================================================================
--- lld/test/ELF/aarch64-adrp-ldr-got-symbols.s
+++ lld/test/ELF/aarch64-adrp-ldr-got-symbols.s
@@ -5,8 +5,9 @@
 # RUN: rm -rf %t && split-file %s %t
 
 # RUN: llvm-mc -filetype=obj -triple=aarch64 %t/symbols.s -o %t/symbols.o
+# RUN: llvm-mc -filetype=obj -triple=aarch64 %t/abs.s -o %t/abs.o
 
-# RUN: ld.lld -shared -T %t/linker.t %t/symbols.o -o %t/symbols.so
+# RUN: ld.lld -shared -T %t/linker.t %t/symbols.o %t/abs.o -o %t/symbols.so
 # RUN: llvm-objdump --no-show-raw-insn -d %t/symbols.so | \
 # RUN:   FileCheck --check-prefix=LIB %s
 
@@ -26,7 +27,11 @@
 LIB-NEXT: adrp   x3
 LIB-NEXT: ldr    x3
 
-# RUN: ld.lld -T %t/linker.t -z undefs %t/symbols.o -o %t/symbols
+## Symbol 'abs_sym' is absolute, no relaxations should be applied.
+LIB-NEXT: adrp   x4
+LIB-NEXT: ldr    x4
+
+# RUN: ld.lld -T %t/linker.t -z undefs %t/symbols.o %t/abs.o -o %t/symbols
 # RUN: llvm-objdump --no-show-raw-insn -d %t/symbols | \
 # RUN:   FileCheck --check-prefix=EXE %s
 
@@ -34,6 +39,10 @@
 EXE:      adrp   x1
 EXE-NEXT: add    x1
 
+## Symbol 'abs_sym' is absolute, relaxations should be applied.
+EXE:      adrp   x4
+EXE-NEXT: add    x4
+
 ## The linker script ensures that .rodata and .text are sufficiently (>1MB)
 ## far apart so that the adrp + ldr pair cannot be relaxed to adr + nop.
 #--- linker.t
@@ -42,6 +51,13 @@
  .text   0x300100: { *(.text) }
 }
 
+# This symbol is defined in a separate file to prevent the definition from
+# being folded into the instructions that reference it.
+#--- abs.s
+.global abs_sym
+.hidden abs_sym
+abs_sym = 0x1000
+
 #--- symbols.s
 .rodata
 .hidden hidden_sym
@@ -68,3 +84,5 @@
   ldr     x2, [x2, #:got_lo12:undefined_sym]
   adrp    x3, :got:ifunc_sym
   ldr     x3, [x3, #:got_lo12:ifunc_sym]
+  adrp    x4, :got:abs_sym
+  ldr     x4, [x4, #:got_lo12:abs_sym]
Index: lld/ELF/Arch/AArch64.cpp
===================================================================
--- lld/ELF/Arch/AArch64.cpp
+++ lld/ELF/Arch/AArch64.cpp
@@ -693,6 +693,11 @@
     return false;
 
   Symbol &sym = *adrpRel.sym;
+  // GOT references to absolute symbols can't be relaxed to use ADRP/ADD in
+  // position-independent code because these instructions produce a relative
+  // address.
+  if (config->isPic && !cast<Defined>(sym).section)
+    return false;
   // Check if the address difference is within 4GB range.
   int64_t val =
       getAArch64Page(sym.getVA()) - getAArch64Page(secAddr + adrpRel.offset);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128492.439620.patch
Type: text/x-patch
Size: 2556 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220624/5ec7504a/attachment.bin>


More information about the llvm-commits mailing list