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

Feng Zou via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 20 18:32:39 PST 2024


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

>From e3f67f69be29881b709b44c0220d9ab00a5ad01f Mon Sep 17 00:00:00 2001
From: Feng Zou <feng.zou at intel.com>
Date: Wed, 13 Nov 2024 10:43:33 +0800
Subject: [PATCH 1/3] [X86][MC] Add R_X86_64_CODE_4_GOTTPOFF

For

  mov name at GOTTPOFF(%rip), %reg
  add name at 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 at tpoff, %reg
  add $name at tpoff, %reg

if the first byte of the instruction at the relocation `offset - 4` is `0xd5`
(namely, encoded w/REX2 prefix) when possible.
---
 .../llvm/BinaryFormat/ELFRelocs/x86_64.def    |  1 +
 .../X86/MCTargetDesc/X86ELFObjectWriter.cpp   |  3 ++
 llvm/test/MC/X86/tls.s                        | 40 +++++++++++++++++++
 3 files changed, 44 insertions(+)
 create mode 100644 llvm/test/MC/X86/tls.s

diff --git a/llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def b/llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def
index 43473d47e32819..94b1ad9c1f9464 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_CODE_4_GOTPCRELX,    43)
+ELF_RELOC(R_X86_64_CODE_4_GOTTPOFF,    44)
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
index a57b1335d1437a..aa02934cc99963 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

>From 20319399b4ebc7fabf8aecf4e6977423a24ed336 Mon Sep 17 00:00:00 2001
From: Feng Zou <feng.zou at intel.com>
Date: Tue, 19 Nov 2024 15:14:37 +0800
Subject: [PATCH 2/3] Removed .align and .size in test, and added trailing
 '\n'.

---
 llvm/test/MC/X86/tls.s | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/llvm/test/MC/X86/tls.s b/llvm/test/MC/X86/tls.s
index 418ab692cac576..2e3c16131a9836 100644
--- a/llvm/test/MC/X86/tls.s
+++ b/llvm/test/MC/X86/tls.s
@@ -18,16 +18,12 @@
 .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:
@@ -37,4 +33,5 @@ _start:
     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
+    addq tls1 at GOTTPOFF(%rip), %r16
+

>From d7be008011a19847e397dd017a7ad8a2dba6b86d Mon Sep 17 00:00:00 2001
From: Feng Zou <feng.zou at intel.com>
Date: Thu, 21 Nov 2024 10:31:31 +0800
Subject: [PATCH 3/3] Remove newly added test and add check in relocation.s

---
 llvm/test/MC/ELF/relocation.s | 67 ++++++++++++++++++-----------------
 llvm/test/MC/X86/tls.s        | 37 -------------------
 2 files changed, 35 insertions(+), 69 deletions(-)
 delete mode 100644 llvm/test/MC/X86/tls.s

diff --git a/llvm/test/MC/ELF/relocation.s b/llvm/test/MC/ELF/relocation.s
index 80b671aa2c859e..88301f8447bc2a 100644
--- a/llvm/test/MC/ELF/relocation.s
+++ b/llvm/test/MC/ELF/relocation.s
@@ -19,6 +19,8 @@ bar:
         movq	bar, %rdx         # R_X86_64_32S
 .long bar                         # R_X86_64_32
         leaq	foo at GOTTPOFF(%rip), %rax # R_X86_64_GOTTPOFF
+        movq    foo at GOTTPOFF(%rip), %r31 # R_X86_64_CODE_4_GOTTPOFF
+        addq    foo at GOTTPOFF(%rip), %r31 # R_X86_64_CODE_4_GOTTPOFF
         leaq	foo at TLSGD(%rip), %rax    # R_X86_64_TLSGD
         leaq	foo at TPOFF(%rax), %rax    # R_X86_64_TPOFF32
         leaq	foo at TLSLD(%rip), %rdi    # R_X86_64_TLSLD
@@ -67,7 +69,6 @@ pr24486:
 weak_sym:
         .long  pr23272-weak_sym
 
-
 // CHECK:        Section {
 // CHECK:          Name: .rela.text
 // CHECK:          Relocations [
@@ -78,37 +79,39 @@ weak_sym:
 // CHECK-NEXT:       0x22 R_X86_64_32S      .text
 // CHECK-NEXT:       0x26 R_X86_64_32       .text
 // CHECK-NEXT:       0x2D R_X86_64_GOTTPOFF foo 0xFFFFFFFFFFFFFFFC
-// CHECK-NEXT:       0x34 R_X86_64_TLSGD    foo 0xFFFFFFFFFFFFFFFC
-// CHECK-NEXT:       0x3B R_X86_64_TPOFF32  foo 0x0
-// CHECK-NEXT:       0x42 R_X86_64_TLSLD    foo 0xFFFFFFFFFFFFFFFC
-// CHECK-NEXT:       0x49 R_X86_64_DTPOFF32 foo 0x0
-// CHECK-NEXT:       0x4F R_X86_64_GOT64 foo 0x0
-// CHECK-NEXT:       0x59 R_X86_64_GOTOFF64 foo 0x0
-// CHECK-NEXT:       0x62 R_X86_64_32S .text 0x0
-// CHECK-NEXT:       0x69 R_X86_64_PC32 foo 0xFFFFFFFFFFFFFFFC
-// CHECK-NEXT:       0x70 R_X86_64_PC32 foo 0x70
-// CHECK-NEXT:       0x77 R_X86_64_32S .text 0x0
-// CHECK-NEXT:       0x7B R_X86_64_DTPOFF64 foo 0x0
-// CHECK-NEXT:       0x85 R_X86_64_TPOFF64 baz 0x0
-// CHECK-NEXT:       0x8D R_X86_64_PC16 foo 0x8D
-// CHECK-NEXT:       0x8F R_X86_64_PC8 foo 0x8F
-// CHECK-NEXT:       0x91 R_X86_64_PLT32 foo 0xFFFFFFFFFFFFFFFC
-// CHECK-NEXT:       0x98 R_X86_64_PC32 foo 0xFFFFFFFFFFFFFFFB
-// CHECK-NEXT:       0x9F R_X86_64_GOTPC32 _GLOBAL_OFFSET_TABLE_ 0x3
-// CHECK-NEXT:       0xA6 R_X86_64_GOTPC32 _GLOBAL_OFFSET_TABLE_ 0xFFFFFFFFFFFFFFFC
-// CHECK-NEXT:       0xAB R_X86_64_GOTPC32 _GLOBAL_OFFSET_TABLE_ 0x1
-// CHECK-NEXT:       0xB1 R_X86_64_GOTPC64 _GLOBAL_OFFSET_TABLE_ 0x2
-// CHECK-NEXT:       0xB9 R_X86_64_SIZE64 blah 0x0
-// CHECK-NEXT:       0xC1 R_X86_64_SIZE64 blah 0x20
-// CHECK-NEXT:       0xC9 R_X86_64_SIZE64 blah 0xFFFFFFFFFFFFFFE0
-// CHECK-NEXT:       0xD4 R_X86_64_SIZE32 blah 0x0
-// CHECK-NEXT:       0xDB R_X86_64_SIZE32 blah 0x20
-// CHECK-NEXT:       0xE2 R_X86_64_SIZE32 blah 0xFFFFFFFFFFFFFFE0
-// CHECK-NEXT:       0xE6 R_X86_64_GOTPCREL foo 0x0
-// CHECK-NEXT:       0xEA R_X86_64_PLT32 foo 0x0
-// CHECK-NEXT:       0xFE R_X86_64_32 .text 0xFE
-// CHECK-NEXT:       0x103 R_X86_64_PC16 pr23771 0xFFFFFFFFFFFFFFFE
-// CHECK-NEXT:       0x105 R_X86_64_PC32 pr23272 0x0
+// CHECK-NEXT:       0x35 R_X86_64_CODE_4_GOTTPOFF foo 0xFFFFFFFFFFFFFFFC
+// CHECK-NEXT:       0x3D R_X86_64_CODE_4_GOTTPOFF foo 0xFFFFFFFFFFFFFFFC
+// CHECK-NEXT:       0x44 R_X86_64_TLSGD    foo 0xFFFFFFFFFFFFFFFC
+// CHECK-NEXT:       0x4B R_X86_64_TPOFF32  foo 0x0
+// CHECK-NEXT:       0x52 R_X86_64_TLSLD    foo 0xFFFFFFFFFFFFFFFC
+// CHECK-NEXT:       0x59 R_X86_64_DTPOFF32 foo 0x0
+// CHECK-NEXT:       0x5F R_X86_64_GOT64 foo 0x0
+// CHECK-NEXT:       0x69 R_X86_64_GOTOFF64 foo 0x0
+// CHECK-NEXT:       0x72 R_X86_64_32S .text 0x0
+// CHECK-NEXT:       0x79 R_X86_64_PC32 foo 0xFFFFFFFFFFFFFFFC
+// CHECK-NEXT:       0x80 R_X86_64_PC32 foo 0x80
+// CHECK-NEXT:       0x87 R_X86_64_32S .text 0x0
+// CHECK-NEXT:       0x8B R_X86_64_DTPOFF64 foo 0x0
+// CHECK-NEXT:       0x95 R_X86_64_TPOFF64 baz 0x0
+// CHECK-NEXT:       0x9D R_X86_64_PC16 foo 0x9D
+// CHECK-NEXT:       0x9F R_X86_64_PC8 foo 0x9F
+// CHECK-NEXT:       0xA1 R_X86_64_PLT32 foo 0xFFFFFFFFFFFFFFFC
+// CHECK-NEXT:       0xA8 R_X86_64_PC32 foo 0xFFFFFFFFFFFFFFFB
+// CHECK-NEXT:       0xAF R_X86_64_GOTPC32 _GLOBAL_OFFSET_TABLE_ 0x3
+// CHECK-NEXT:       0xB6 R_X86_64_GOTPC32 _GLOBAL_OFFSET_TABLE_ 0xFFFFFFFFFFFFFFFC
+// CHECK-NEXT:       0xBB R_X86_64_GOTPC32 _GLOBAL_OFFSET_TABLE_ 0x1
+// CHECK-NEXT:       0xC1 R_X86_64_GOTPC64 _GLOBAL_OFFSET_TABLE_ 0x2
+// CHECK-NEXT:       0xC9 R_X86_64_SIZE64 blah 0x0
+// CHECK-NEXT:       0xD1 R_X86_64_SIZE64 blah 0x20
+// CHECK-NEXT:       0xD9 R_X86_64_SIZE64 blah 0xFFFFFFFFFFFFFFE0
+// CHECK-NEXT:       0xE4 R_X86_64_SIZE32 blah 0x0
+// CHECK-NEXT:       0xEB R_X86_64_SIZE32 blah 0x20
+// CHECK-NEXT:       0xF2 R_X86_64_SIZE32 blah 0xFFFFFFFFFFFFFFE0
+// CHECK-NEXT:       0xF6 R_X86_64_GOTPCREL foo 0x0
+// CHECK-NEXT:       0xFA R_X86_64_PLT32 foo 0x0
+// CHECK-NEXT:       0x10E R_X86_64_32 .text 0x10E
+// CHECK-NEXT:       0x113 R_X86_64_PC16 pr23771 0xFFFFFFFFFFFFFFFE
+// CHECK-NEXT:       0x115 R_X86_64_PC32 pr23272 0x0
 // CHECK-NEXT:     ]
 // CHECK-NEXT:   }
 
diff --git a/llvm/test/MC/X86/tls.s b/llvm/test/MC/X86/tls.s
deleted file mode 100644
index 2e3c16131a9836..00000000000000
--- a/llvm/test/MC/X86/tls.s
+++ /dev/null
@@ -1,37 +0,0 @@
-// 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
-tls0:
-.long 0
-.type  tls1, at object
-.globl tls1
-tls1:
-.long 0
-.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
-



More information about the llvm-commits mailing list