[PATCH] D91993: [ELF] Don't relax R_X86_64_GOTPCRELX if addend != -4
Fangrui Song via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 25 00:29:29 PST 2020
MaskRay updated this revision to Diff 307530.
MaskRay marked an inline comment as done.
MaskRay edited the summary of this revision.
MaskRay added a comment.
Extract refactoring part
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D91993/new/
https://reviews.llvm.org/D91993
Files:
lld/ELF/Arch/X86_64.cpp
lld/test/ELF/x86-64-gotpc-offset.s
Index: lld/test/ELF/x86-64-gotpc-offset.s
===================================================================
--- /dev/null
+++ lld/test/ELF/x86-64-gotpc-offset.s
@@ -0,0 +1,18 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+# RUN: ld.lld %t.o -o %t
+# RUN: llvm-objdump -s -d %t | FileCheck %s
+
+# CHECK: Contents of section .got:
+# CHECK-NEXT: 2021a0 9d112000 00000000
+
+## The second instruction has an offset (addend!=-4). It is incorrect to relax
+## movl to leal.
+# CHECK: leaq 6(%rip), %rax # {{.*}} <foo>
+# CHECK-NEXT: movl {{.*}}(%rip), %eax # 2021a4
+ movq foo at GOTPCREL(%rip), %rax
+ movl foo at GOTPCREL+4(%rip), %eax
+
+.globl foo
+foo:
+ nop
Index: lld/ELF/Arch/X86_64.cpp
===================================================================
--- lld/ELF/Arch/X86_64.cpp
+++ lld/ELF/Arch/X86_64.cpp
@@ -730,7 +730,12 @@
RelExpr X86_64::adjustGotPcExpr(RelType type, int64_t addend,
const uint8_t *loc) const {
- if (type != R_X86_64_GOTPCRELX && type != R_X86_64_REX_GOTPCRELX)
+ // Only R_X86_64_[REX_]GOTPCRELX can be relaxed. addend != -4 means the
+ // instruction does not load the full GOT entry, we cannot relax the
+ // relocation. E.g. movl x at GOTPCREL+4(%rip), %rax (addend=0) loads the high 32
+ // bits of the GOT entry.
+ if ((type != R_X86_64_GOTPCRELX && type != R_X86_64_REX_GOTPCRELX) ||
+ addend != -4)
return R_GOT_PC;
const uint8_t op = loc[-2];
const uint8_t modRm = loc[-1];
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91993.307530.patch
Type: text/x-patch
Size: 1512 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201125/bc60b378/attachment.bin>
More information about the llvm-commits
mailing list