[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:38:29 PDT 2022
pcc updated this revision to Diff 439625.
pcc added a comment.
Comment
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D128492/new/
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 may be applied in -no-pie mode.
+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.439625.patch
Type: text/x-patch
Size: 2569 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220624/9cbf0126/attachment.bin>
More information about the llvm-commits
mailing list