[lld] [llvm] [lld][ELF] Support the 32-bit GOTTPOFF forms used by ILP32 code (PR #221557)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 6 05:05:28 PDT 2026


https://github.com/HomuHomu833 updated https://github.com/llvm/llvm-project/pull/221557

>From 5f3872104bd800e2d3e76a6557694fa2d5027100 Mon Sep 17 00:00:00 2001
From: Homura <egzozu.be.bas at gmail.com>
Date: Sun, 6 Sep 2026 14:23:31 +0300
Subject: [PATCH 1/3] [lld][ELF] Support the 32-bit GOTTPOFF forms used by
 ILP32 code

relaxTlsIeToLe only matched the REX.W movq/addq encodings. ILP32 code
materializes the TLS offset with a 32-bit operand, so the relocation lands on
movl/addl and linking failed with "R_X86_64_GOTTPOFF must be used in MOVQ or
ADDQ instructions only".

Handle movl and addl, with and without a REX.R prefix. Every rewrite keeps the
original instruction length: movl becomes "movl $foo", addl becomes
"leal foo(%reg)", and addl with ESP or R12D becomes "addl $foo" because LEA
with those registers would need a SIB byte and no longer fit.

Fixes #161059
---
 lld/ELF/Arch/X86_64.cpp          | 33 +++++++++++++++++++++++
 lld/test/ELF/x86-64-tls-ie-x32.s | 45 ++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)
 create mode 100644 lld/test/ELF/x86-64-tls-ie-x32.s

diff --git a/lld/ELF/Arch/X86_64.cpp b/lld/ELF/Arch/X86_64.cpp
index f172080dc2b6a..96e2422874fc3 100644
--- a/lld/ELF/Arch/X86_64.cpp
+++ b/lld/ELF/Arch/X86_64.cpp
@@ -939,6 +939,39 @@ void X86_64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
       // "movq foo at gottpoff(%rip),%reg" -> "movq $foo,%reg"
       memcpy(inst, "\x48\xc7", 2);
       *regSlot = 0xc0 | reg;
+    } else if ((loc[-2] == 0x8b || loc[-2] == 0x03) &&
+               (loc[-1] & 0xc7) == 0x05) {
+      // ILP32 code loads the offset with a 32-bit operand, so the instruction
+      // has no REX.W and, for %eax-%edi, no REX prefix at all. The memory
+      // operand is RIP-relative, so REX.B is unused and REX.R is the only bit
+      // that can appear here.
+      //
+      // Note that ADD with ESP or R12D is converted to ADD instead of LEA for
+      // the same reason as above.
+      const bool hasRex = loc[-3] == 0x40 || loc[-3] == 0x44;
+      if (loc[-2] == 0x8b) {
+        // "movl foo at gottpoff(%rip),%reg" -> "movl $foo,%reg"
+        // "movl foo at gottpoff(%rip),%r[8-15]d" -> "movl $foo,%r[8-15]d"
+        loc[-2] = 0xc7;
+        *regSlot = 0xc0 | reg;
+        // Move the R bit to the B bit.
+        if (hasRex)
+          loc[-3] = 0x41;
+      } else if (reg == 4) {
+        // "addl foo at gottpoff(%rip),%esp" -> "addl $foo,%esp"
+        // "addl foo at gottpoff(%rip),%r12d" -> "addl $foo,%r12d"
+        loc[-2] = 0x81;
+        *regSlot = 0xc0 | reg;
+        if (hasRex)
+          loc[-3] = 0x41;
+      } else {
+        // "addl foo at gottpoff(%rip),%reg" -> "leal foo(%reg),%reg"
+        // "addl foo at gottpoff(%rip),%r[8-15]d" -> "leal foo(%r[8-15]),%r[8-15]d"
+        loc[-2] = 0x8d;
+        *regSlot = 0x80 | (reg << 3) | reg;
+        if (hasRex)
+          loc[-3] = 0x45;
+      }
     } else {
       Err(ctx)
           << getErrorLoc(ctx, loc - 3)
diff --git a/lld/test/ELF/x86-64-tls-ie-x32.s b/lld/test/ELF/x86-64-tls-ie-x32.s
new file mode 100644
index 0000000000000..c62c3bc019050
--- /dev/null
+++ b/lld/test/ELF/x86-64-tls-ie-x32.s
@@ -0,0 +1,45 @@
+# REQUIRES: x86
+## ILP32 (x32) code materializes the TLS offset with 32-bit operands, so the
+## GOTTPOFF relocation lands on movl/addl rather than the REX.W movq/addq
+## forms. Check that IE->LE relaxation handles them, with and without REX.R.
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: ld.lld %t.o -o %t
+# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
+
+# CHECK-LABEL: <_start>:
+## "movl foo at gottpoff(%rip),%eax" -> "movl $foo,%eax"
+# CHECK-NEXT:    movl ${{.*}}, %eax
+## "addl foo at gottpoff(%rip),%ecx" -> "leal foo(%ecx),%ecx"
+# CHECK-NEXT:    leal {{.*}}(%rcx), %ecx
+## "addl foo at gottpoff(%rip),%esp" -> "addl $foo,%esp" (LEA would need a SIB)
+# CHECK-NEXT:    addl ${{.*}}, %esp
+## REX.R forms select %r8d-%r15d and must become REX.B in the rewritten form.
+# CHECK-NEXT:    movl ${{.*}}, %r8d
+# CHECK-NEXT:    leal {{.*}}(%r9), %r9d
+
+.type tls0, at object
+.section .tbss,"awT", at nobits
+.globl tls0
+.align 4
+tls0:
+ .long 0
+ .size tls0, 4
+
+.text
+.globl _start
+_start:
+  movl 0(%rip), %eax
+  .reloc .-4, R_X86_64_GOTTPOFF, tls0-4
+
+  addl 0(%rip), %ecx
+  .reloc .-4, R_X86_64_GOTTPOFF, tls0-4
+
+  addl 0(%rip), %esp
+  .reloc .-4, R_X86_64_GOTTPOFF, tls0-4
+
+  movl 0(%rip), %r8d
+  .reloc .-4, R_X86_64_GOTTPOFF, tls0-4
+
+  addl 0(%rip), %r9d
+  .reloc .-4, R_X86_64_GOTTPOFF, tls0-4

>From 4a4bfc248f084ea83fde4a7e86e684786499e242 Mon Sep 17 00:00:00 2001
From: Homura <egzozu.be.bas at gmail.com>
Date: Sun, 6 Sep 2026 15:05:18 +0300
Subject: [PATCH 2/3] [lld][ELF] Gate the GOTTPOFF forms on the ELF class

---
 lld/ELF/Arch/X86_64.cpp | 56 ++++++++++++++++++++++-------------------
 1 file changed, 30 insertions(+), 26 deletions(-)

diff --git a/lld/ELF/Arch/X86_64.cpp b/lld/ELF/Arch/X86_64.cpp
index 96e2422874fc3..675c3beda30d4 100644
--- a/lld/ELF/Arch/X86_64.cpp
+++ b/lld/ELF/Arch/X86_64.cpp
@@ -917,30 +917,7 @@ void X86_64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
     // because LEA with these registers needs 4 bytes to encode and thus
     // wouldn't fit the space.
 
-    if (memcmp(inst, "\x48\x03\x25", 3) == 0) {
-      // "addq foo at gottpoff(%rip),%rsp" -> "addq $foo,%rsp"
-      memcpy(inst, "\x48\x81\xc4", 3);
-    } else if (memcmp(inst, "\x4c\x03\x25", 3) == 0) {
-      // "addq foo at gottpoff(%rip),%r12" -> "addq $foo,%r12"
-      memcpy(inst, "\x49\x81\xc4", 3);
-    } else if (memcmp(inst, "\x4c\x03", 2) == 0) {
-      // "addq foo at gottpoff(%rip),%r[8-15]" -> "leaq foo(%r[8-15]),%r[8-15]"
-      memcpy(inst, "\x4d\x8d", 2);
-      *regSlot = 0x80 | (reg << 3) | reg;
-    } else if (memcmp(inst, "\x48\x03", 2) == 0) {
-      // "addq foo at gottpoff(%rip),%reg -> "leaq foo(%reg),%reg"
-      memcpy(inst, "\x48\x8d", 2);
-      *regSlot = 0x80 | (reg << 3) | reg;
-    } else if (memcmp(inst, "\x4c\x8b", 2) == 0) {
-      // "movq foo at gottpoff(%rip),%r[8-15]" -> "movq $foo,%r[8-15]"
-      memcpy(inst, "\x49\xc7", 2);
-      *regSlot = 0xc0 | reg;
-    } else if (memcmp(inst, "\x48\x8b", 2) == 0) {
-      // "movq foo at gottpoff(%rip),%reg" -> "movq $foo,%reg"
-      memcpy(inst, "\x48\xc7", 2);
-      *regSlot = 0xc0 | reg;
-    } else if ((loc[-2] == 0x8b || loc[-2] == 0x03) &&
-               (loc[-1] & 0xc7) == 0x05) {
+    if (!ctx.arg.is64) {
       // ILP32 code loads the offset with a 32-bit operand, so the instruction
       // has no REX.W and, for %eax-%edi, no REX prefix at all. The memory
       // operand is RIP-relative, so REX.B is unused and REX.R is the only bit
@@ -957,21 +934,48 @@ void X86_64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
         // Move the R bit to the B bit.
         if (hasRex)
           loc[-3] = 0x41;
-      } else if (reg == 4) {
+      } else if (loc[-2] == 0x03 && reg == 4) {
         // "addl foo at gottpoff(%rip),%esp" -> "addl $foo,%esp"
         // "addl foo at gottpoff(%rip),%r12d" -> "addl $foo,%r12d"
         loc[-2] = 0x81;
         *regSlot = 0xc0 | reg;
         if (hasRex)
           loc[-3] = 0x41;
-      } else {
+      } else if (loc[-2] == 0x03) {
         // "addl foo at gottpoff(%rip),%reg" -> "leal foo(%reg),%reg"
         // "addl foo at gottpoff(%rip),%r[8-15]d" -> "leal foo(%r[8-15]),%r[8-15]d"
         loc[-2] = 0x8d;
         *regSlot = 0x80 | (reg << 3) | reg;
         if (hasRex)
           loc[-3] = 0x45;
+      } else {
+        Err(ctx)
+            << getErrorLoc(ctx, loc - 2)
+            << "R_X86_64_GOTTPOFF must be used in MOVL or ADDL instructions "
+               "only";
       }
+    } else if (memcmp(inst, "\x48\x03\x25", 3) == 0) {
+      // "addq foo at gottpoff(%rip),%rsp" -> "addq $foo,%rsp"
+      memcpy(inst, "\x48\x81\xc4", 3);
+    } else if (memcmp(inst, "\x4c\x03\x25", 3) == 0) {
+      // "addq foo at gottpoff(%rip),%r12" -> "addq $foo,%r12"
+      memcpy(inst, "\x49\x81\xc4", 3);
+    } else if (memcmp(inst, "\x4c\x03", 2) == 0) {
+      // "addq foo at gottpoff(%rip),%r[8-15]" -> "leaq foo(%r[8-15]),%r[8-15]"
+      memcpy(inst, "\x4d\x8d", 2);
+      *regSlot = 0x80 | (reg << 3) | reg;
+    } else if (memcmp(inst, "\x48\x03", 2) == 0) {
+      // "addq foo at gottpoff(%rip),%reg -> "leaq foo(%reg),%reg"
+      memcpy(inst, "\x48\x8d", 2);
+      *regSlot = 0x80 | (reg << 3) | reg;
+    } else if (memcmp(inst, "\x4c\x8b", 2) == 0) {
+      // "movq foo at gottpoff(%rip),%r[8-15]" -> "movq $foo,%r[8-15]"
+      memcpy(inst, "\x49\xc7", 2);
+      *regSlot = 0xc0 | reg;
+    } else if (memcmp(inst, "\x48\x8b", 2) == 0) {
+      // "movq foo at gottpoff(%rip),%reg" -> "movq $foo,%reg"
+      memcpy(inst, "\x48\xc7", 2);
+      *regSlot = 0xc0 | reg;
     } else {
       Err(ctx)
           << getErrorLoc(ctx, loc - 3)

>From bdc40affc8322ee2518bdc2da22ae89b6ef9dadc Mon Sep 17 00:00:00 2001
From: Homura <egzozu.be.bas at gmail.com>
Date: Sun, 6 Sep 2026 15:05:20 +0300
Subject: [PATCH 3/3] [lld][ELF] Build the test as a real x32 object

---
 lld/test/ELF/x86-64-tls-ie-x32.s | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lld/test/ELF/x86-64-tls-ie-x32.s b/lld/test/ELF/x86-64-tls-ie-x32.s
index c62c3bc019050..88bcd666b934f 100644
--- a/lld/test/ELF/x86-64-tls-ie-x32.s
+++ b/lld/test/ELF/x86-64-tls-ie-x32.s
@@ -1,10 +1,10 @@
 # REQUIRES: x86
-## ILP32 (x32) code materializes the TLS offset with 32-bit operands, so the
+## ILP32 code materializes the TLS offset with a 32-bit operand, so the
 ## GOTTPOFF relocation lands on movl/addl rather than the REX.W movq/addq
 ## forms. Check that IE->LE relaxation handles them, with and without REX.R.
 
-# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
-# RUN: ld.lld %t.o -o %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux-gnux32 %s -o %t.o
+# RUN: ld.lld -m elf32_x86_64 %t.o -o %t
 # RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
 
 # CHECK-LABEL: <_start>:
@@ -12,9 +12,9 @@
 # CHECK-NEXT:    movl ${{.*}}, %eax
 ## "addl foo at gottpoff(%rip),%ecx" -> "leal foo(%ecx),%ecx"
 # CHECK-NEXT:    leal {{.*}}(%rcx), %ecx
-## "addl foo at gottpoff(%rip),%esp" -> "addl $foo,%esp" (LEA would need a SIB)
+## LEA with %esp would need a SIB byte, so ADD is used instead.
 # CHECK-NEXT:    addl ${{.*}}, %esp
-## REX.R forms select %r8d-%r15d and must become REX.B in the rewritten form.
+## REX.R selects %r8d-%r15d and has to become REX.B in the rewritten form.
 # CHECK-NEXT:    movl ${{.*}}, %r8d
 # CHECK-NEXT:    leal {{.*}}(%r9), %r9d
 



More information about the llvm-commits mailing list