[llvm] [X86][MC] Add R_X86_64_CODE_4_GOTTPOFF (PR #116633)

via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 18 07:02:50 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mc

@llvm/pr-subscribers-llvm-binary-utilities

Author: Feng Zou (fzou1)

<details>
<summary>Changes</summary>

For

  mov name@<!-- -->GOTTPOFF(%rip), %reg
  add name@<!-- -->GOTTPOFF(%rip), %reg

add

  `R_X86_64_CODE_4_GOTTPOFF` = 44

if the instruction starts at 4 bytes before the relocation offset. It's similar to R_X86_64_GOTTPOFF.

Linker can treat `R_X86_64_CODE_4_GOTTPOFF` as `R_X86_64_GOTTPOFF` or convert the instructions above to

  mov $name@<!-- -->tpoff, %reg
  add $name@<!-- -->tpoff, %reg

if the first byte of the instruction at the relocation `offset - 4` is `0xd5` (namely, encoded w/REX2 prefix) when possible.

---
Full diff: https://github.com/llvm/llvm-project/pull/116633.diff


3 Files Affected:

- (modified) llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def (+1) 
- (modified) llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp (+3) 
- (added) llvm/test/MC/X86/tls.s (+40) 


``````````diff
diff --git a/llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def b/llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def
index 161b1969abfeb4..ff4516d1edf507 100644
--- a/llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def
+++ b/llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def
@@ -44,3 +44,4 @@ ELF_RELOC(R_X86_64_IRELATIVE,   37)
 ELF_RELOC(R_X86_64_GOTPCRELX,   41)
 ELF_RELOC(R_X86_64_REX_GOTPCRELX,    42)
 ELF_RELOC(R_X86_64_REX2_GOTPCRELX,    43)
+ELF_RELOC(R_X86_64_CODE_4_GOTTPOFF,    44)
\ No newline at end of file
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
index 90222278d1ad6f..ab363e4eb8dce3 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
@@ -197,6 +197,9 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
     return ELF::R_X86_64_TLSGD;
   case MCSymbolRefExpr::VK_GOTTPOFF:
     checkIs32(Ctx, Loc, Type);
+    if ((unsigned)Kind == X86::reloc_riprel_4byte_movq_load_rex2 ||
+        (unsigned)Kind == X86::reloc_riprel_4byte_relax_rex2)
+      return ELF::R_X86_64_CODE_4_GOTTPOFF;
     return ELF::R_X86_64_GOTTPOFF;
   case MCSymbolRefExpr::VK_TLSLD:
     checkIs32(Ctx, Loc, Type);
diff --git a/llvm/test/MC/X86/tls.s b/llvm/test/MC/X86/tls.s
new file mode 100644
index 00000000000000..418ab692cac576
--- /dev/null
+++ b/llvm/test/MC/X86/tls.s
@@ -0,0 +1,40 @@
+// RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t
+// RUN: llvm-objdump -dr --no-print-imm-hex %t | FileCheck %s
+
+// CHECK:      <_start>:
+// CHECK-NEXT:   movq (%rip), %r16
+// CHECK-NEXT:   R_X86_64_CODE_4_GOTTPOFF    tls0-0x4
+// CHECK-NEXT:   movq (%rip), %r20
+// CHECK-NEXT:   R_X86_64_CODE_4_GOTTPOFF    tls0-0x4
+// CHECK-NEXT:   movq (%rip), %r16
+// CHECK-NEXT:   R_X86_64_CODE_4_GOTTPOFF    tls1-0x4
+// CHECK-NEXT:   addq (%rip), %r16
+// CHECK-NEXT:   R_X86_64_CODE_4_GOTTPOFF    tls0-0x4
+// CHECK-NEXT:   addq (%rip), %r28
+// CHECK-NEXT:   R_X86_64_CODE_4_GOTTPOFF    tls0-0x4
+// CHECK-NEXT:   addq (%rip), %r16
+// CHECK-NEXT:   R_X86_64_CODE_4_GOTTPOFF    tls1-0x4
+
+.type tls0, at object
+.section .tbss,"awT", at nobits
+.globl tls0
+.align 4
+tls0:
+.long 0
+.size tls0, 4
+.type  tls1, at object
+.globl tls1
+.align 4
+tls1:
+.long 0
+.size tls1, 4
+.section .text
+.globl _start
+_start:
+    # EGPR
+    movq tls0 at GOTTPOFF(%rip), %r16
+    movq tls0 at GOTTPOFF(%rip), %r20
+    movq tls1 at GOTTPOFF(%rip), %r16
+    addq tls0 at GOTTPOFF(%rip), %r16
+    addq tls0 at GOTTPOFF(%rip), %r28
+    addq tls1 at GOTTPOFF(%rip), %r16
\ No newline at end of file

``````````

</details>


https://github.com/llvm/llvm-project/pull/116633


More information about the llvm-commits mailing list