[lld] Enough LLD changes to bootstrap OpenBSD on sparc64 (PR #207609)
Kirill A. Korinsky via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 6 02:55:05 PDT 2026
https://github.com/catap updated https://github.com/llvm/llvm-project/pull/207609
>From 4c5cf3e6dca6429384d3fba6a5f79e0e02c7ff3e Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:47:31 +0200
Subject: [PATCH 01/18] [lld][ELF][SPARC] Use RELA relocations for SPARCV9
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Driver.cpp | 7 ++++---
lld/test/ELF/sparcv9-rela.s | 14 ++++++++++++++
2 files changed, 18 insertions(+), 3 deletions(-)
create mode 100644 lld/test/ELF/sparcv9-rela.s
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 7ec7dfcae6bca..a2552a1ae1ab7 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -1318,9 +1318,10 @@ static SmallVector<StringRef, 0> getSymbolOrderingFile(Ctx &ctx,
static bool getIsRela(Ctx &ctx, opt::InputArgList &args) {
// The psABI specifies the default relocation entry format.
- bool rela = is_contained({EM_AARCH64, EM_AMDGPU, EM_HEXAGON, EM_LOONGARCH,
- EM_PPC, EM_PPC64, EM_RISCV, EM_S390, EM_X86_64},
- ctx.arg.emachine);
+ bool rela =
+ is_contained({EM_AARCH64, EM_AMDGPU, EM_HEXAGON, EM_LOONGARCH, EM_PPC,
+ EM_PPC64, EM_RISCV, EM_S390, EM_SPARCV9, EM_X86_64},
+ ctx.arg.emachine);
// If -z rel or -z rela is specified, use the last option.
for (auto *arg : args.filtered(OPT_z)) {
StringRef s(arg->getValue());
diff --git a/lld/test/ELF/sparcv9-rela.s b/lld/test/ELF/sparcv9-rela.s
new file mode 100644
index 0000000000000..2cebe2acffbed
--- /dev/null
+++ b/lld/test/ELF/sparcv9-rela.s
@@ -0,0 +1,14 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-readelf -r %t.so | FileCheck %s
+
+# CHECK: Relocation section '.rela.dyn'
+# CHECK: R_SPARC_RELATIVE
+
+.data
+.quad local
+
+.hidden local
+local:
+ .quad 0
>From 65666116742e9f17ecf2282309e081107d3e7e31 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:47:47 +0200
Subject: [PATCH 02/18] [lld][ELF][SPARC] Support 8 and 16-bit absolute
relocations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 14 ++++++++++++++
lld/test/ELF/sparcv9-abs-8-16.s | 12 ++++++++++++
2 files changed, 26 insertions(+)
create mode 100644 lld/test/ELF/sparcv9-abs-8-16.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index 405be69a8f572..aba2148656ad2 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -92,10 +92,13 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
continue;
// Absolute relocations:
+ case R_SPARC_8:
+ case R_SPARC_16:
case R_SPARC_32:
case R_SPARC_UA32:
case R_SPARC_64:
case R_SPARC_UA64:
+ case R_SPARC_UA16:
case R_SPARC_H44:
case R_SPARC_M44:
case R_SPARC_L44:
@@ -147,6 +150,17 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
uint64_t val) const {
switch (rel.type) {
+ case R_SPARC_8:
+ // V-byte8
+ checkUInt(ctx, loc, val, 8, rel);
+ *loc = val;
+ break;
+ case R_SPARC_16:
+ case R_SPARC_UA16:
+ // V-half16
+ checkUInt(ctx, loc, val, 16, rel);
+ write16be(loc, val);
+ break;
case R_SPARC_32:
case R_SPARC_UA32:
// V-word32
diff --git a/lld/test/ELF/sparcv9-abs-8-16.s b/lld/test/ELF/sparcv9-abs-8-16.s
new file mode 100644
index 0000000000000..9c2e19a4d4803
--- /dev/null
+++ b/lld/test/ELF/sparcv9-abs-8-16.s
@@ -0,0 +1,12 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld %t.o --defsym=abs8=0x7f --defsym=abs16=0x1234 -o %t
+# RUN: llvm-objdump -s --section=.data %t | FileCheck %s
+
+# CHECK: Contents of section .data:
+# CHECK-NEXT: {{[0-9a-f]+}} 7f123412 34
+
+.data
+ .byte abs8
+ .half abs16
+ .uahalf abs16
>From 8380540a2c1a1b51afc6b794d2113decd54b0468 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:48:03 +0200
Subject: [PATCH 03/18] [lld][ELF][SPARC] Support R_SPARC_13 relocations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 6 ++++++
lld/test/ELF/sparcv9-abs-13.s | 12 ++++++++++++
2 files changed, 18 insertions(+)
create mode 100644 lld/test/ELF/sparcv9-abs-13.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index aba2148656ad2..6eb622e791683 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -106,6 +106,7 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
case R_SPARC_HM10:
case R_SPARC_LM22:
case R_SPARC_HI22:
+ case R_SPARC_13:
case R_SPARC_LO10:
expr = R_ABS;
break;
@@ -183,6 +184,11 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
checkUInt(ctx, loc, val, 22, rel);
write32be(loc, (read32be(loc) & ~0x003fffff) | (val & 0x003fffff));
break;
+ case R_SPARC_13:
+ // V-simm13
+ checkIntUInt(ctx, loc, val, 13, rel);
+ write32be(loc, (read32be(loc) & ~0x00001fff) | (val & 0x00001fff));
+ break;
case R_SPARC_GOT22:
case R_SPARC_PC22:
case R_SPARC_LM22:
diff --git a/lld/test/ELF/sparcv9-abs-13.s b/lld/test/ELF/sparcv9-abs-13.s
new file mode 100644
index 0000000000000..0d793df052e6d
--- /dev/null
+++ b/lld/test/ELF/sparcv9-abs-13.s
@@ -0,0 +1,12 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld %t.o --defsym=small=0x123 -o %t
+# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
+
+# CHECK-LABEL: <_start>:
+# CHECK-NEXT: mov 0x123, %g1
+
+.text
+.globl _start
+_start:
+ or %g0, small, %g1
>From 5031c101bb4cc14f80ff6340469a25ec1c158e61 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:48:14 +0200
Subject: [PATCH 04/18] [lld][ELF][SPARC] Support HIX22 and LOX10 relocations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 8 ++++++++
lld/test/ELF/sparcv9-abs-hix-lox.s | 14 ++++++++++++++
2 files changed, 22 insertions(+)
create mode 100644 lld/test/ELF/sparcv9-abs-hix-lox.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index 6eb622e791683..e4a2a983d9837 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -108,6 +108,8 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
case R_SPARC_HI22:
case R_SPARC_13:
case R_SPARC_LO10:
+ case R_SPARC_HIX22:
+ case R_SPARC_LOX10:
expr = R_ABS;
break;
@@ -228,6 +230,11 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
// T-simm13
write32be(loc, (read32be(loc) & ~0x00001fff) | ((val >> 32) & 0x000003ff));
break;
+ case R_SPARC_HIX22:
+ // V-imm22
+ checkUInt(ctx, loc, ~val, 32, rel);
+ write32be(loc, (read32be(loc) & ~0x003fffff) | ((~val >> 10) & 0x003fffff));
+ break;
case R_SPARC_H44:
// V-imm22
checkUInt(ctx, loc, val >> 22, 22, rel);
@@ -245,6 +252,7 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
// T-imm22
write32be(loc, (read32be(loc) & ~0x003fffff) | ((~val >> 10) & 0x003fffff));
break;
+ case R_SPARC_LOX10:
case R_SPARC_TLS_LE_LOX10:
// T-simm13
write32be(loc, (read32be(loc) & ~0x00001fff) | (val & 0x000003ff) | 0x1C00);
diff --git a/lld/test/ELF/sparcv9-abs-hix-lox.s b/lld/test/ELF/sparcv9-abs-hix-lox.s
new file mode 100644
index 0000000000000..c26df2d1ca68e
--- /dev/null
+++ b/lld/test/ELF/sparcv9-abs-hix-lox.s
@@ -0,0 +1,14 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld %t.o --defsym=neg1=0xffffffffffffffff -o %t
+# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
+
+# CHECK-LABEL: <_start>:
+# CHECK-NEXT: sethi 0x0, %g2
+# CHECK-NEXT: xor %g2, -0x1, %g2
+
+.text
+.globl _start
+_start:
+ sethi %hix(neg1), %g2
+ xor %g2, %lox(neg1), %g2
>From 4c79a096010a50546199fc21ce362d5bdf682c43 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:48:27 +0200
Subject: [PATCH 05/18] [lld][ELF][SPARC] Correct HI22 and LO10 relocations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 4 ++--
lld/test/ELF/sparcv9-abs-hi-lo.s | 14 ++++++++++++++
2 files changed, 16 insertions(+), 2 deletions(-)
create mode 100644 lld/test/ELF/sparcv9-abs-hi-lo.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index e4a2a983d9837..9a3d43a91e11d 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -199,7 +199,7 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
break;
case R_SPARC_HI22:
// V-imm22
- checkUInt(ctx, loc, val >> 10, 22, rel);
+ checkUInt(ctx, loc, val, 32, rel);
write32be(loc, (read32be(loc) & ~0x003fffff) | ((val >> 10) & 0x003fffff));
break;
case R_SPARC_WDISP19:
@@ -214,7 +214,7 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
break;
case R_SPARC_LO10:
// T-simm13
- write32be(loc, (read32be(loc) & ~0x00001fff) | (val & 0x000003ff));
+ write32be(loc, (read32be(loc) & ~0x000003ff) | (val & 0x000003ff));
break;
case R_SPARC_64:
case R_SPARC_UA64:
diff --git a/lld/test/ELF/sparcv9-abs-hi-lo.s b/lld/test/ELF/sparcv9-abs-hi-lo.s
new file mode 100644
index 0000000000000..7bc33770b3959
--- /dev/null
+++ b/lld/test/ELF/sparcv9-abs-hi-lo.s
@@ -0,0 +1,14 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld %t.o --defsym=sym=0x12345678 -o %t
+# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
+
+# CHECK-LABEL: <_start>:
+# CHECK-NEXT: sethi 0x48d15, %g1
+# CHECK-NEXT: or %g1, 0x278, %g1
+
+.text
+.globl _start
+_start:
+ sethi %hi(sym), %g1
+ or %g1, %lo(sym), %g1
>From 8ed9bd81fbc4ea5cc33697ab2faaf455d36e6a64 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:48:39 +0200
Subject: [PATCH 06/18] [lld][ELF][SPARC] Correct high-part absolute
relocations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 7 +++----
lld/test/ELF/sparcv9-abs-high.s | 23 +++++++++++++++++++++++
2 files changed, 26 insertions(+), 4 deletions(-)
create mode 100644 lld/test/ELF/sparcv9-abs-high.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index 9a3d43a91e11d..03f6fdf46514c 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -223,12 +223,11 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
break;
case R_SPARC_HH22:
// V-imm22
- checkUInt(ctx, loc, val >> 42, 22, rel);
write32be(loc, (read32be(loc) & ~0x003fffff) | ((val >> 42) & 0x003fffff));
break;
case R_SPARC_HM10:
// T-simm13
- write32be(loc, (read32be(loc) & ~0x00001fff) | ((val >> 32) & 0x000003ff));
+ write32be(loc, (read32be(loc) & ~0x000003ff) | ((val >> 32) & 0x000003ff));
break;
case R_SPARC_HIX22:
// V-imm22
@@ -237,7 +236,7 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
break;
case R_SPARC_H44:
// V-imm22
- checkUInt(ctx, loc, val >> 22, 22, rel);
+ checkUInt(ctx, loc, val, 44, rel);
write32be(loc, (read32be(loc) & ~0x003fffff) | ((val >> 22) & 0x003fffff));
break;
case R_SPARC_M44:
@@ -246,7 +245,7 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
break;
case R_SPARC_L44:
// T-imm13
- write32be(loc, (read32be(loc) & ~0x00001fff) | (val & 0x00000fff));
+ write32be(loc, (read32be(loc) & ~0x00000fff) | (val & 0x00000fff));
break;
case R_SPARC_TLS_LE_HIX22:
// T-imm22
diff --git a/lld/test/ELF/sparcv9-abs-high.s b/lld/test/ELF/sparcv9-abs-high.s
new file mode 100644
index 0000000000000..fc39d14402f88
--- /dev/null
+++ b/lld/test/ELF/sparcv9-abs-high.s
@@ -0,0 +1,23 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld %t.o --defsym=sym64=0x123456789abcdef \
+# RUN: --defsym=sym44=0x123456789ab -o %t
+# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
+
+# CHECK-LABEL: <_start>:
+# CHECK-NEXT: sethi 0x48d1, %g1
+# CHECK-NEXT: or %g1, 0x167, %g1
+# CHECK-NEXT: sethi 0x226af3, %g2
+# CHECK-NEXT: sethi 0x48d15, %g3
+# CHECK-NEXT: or %g3, 0x278, %g3
+# CHECK-NEXT: or %g3, 0x9ab, %g3
+
+.text
+.globl _start
+_start:
+ sethi %hh(sym64), %g1
+ or %g1, %hm(sym64), %g1
+ sethi %lm(sym64), %g2
+ sethi %h44(sym44), %g3
+ or %g3, %m44(sym44), %g3
+ or %g3, %l44(sym44), %g3
>From 224d77f9e043b112909b01a8396b03f8124f4c78 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:48:52 +0200
Subject: [PATCH 07/18] [lld][ELF][SPARC] Support additional PC-relative
relocations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 40 ++++++++++++++++++++++++++++----
lld/test/ELF/sparcv9-pc-relocs.s | 17 ++++++++++++++
2 files changed, 53 insertions(+), 4 deletions(-)
create mode 100644 lld/test/ELF/sparcv9-pc-relocs.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index 03f6fdf46514c..6cf9c853073f1 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -121,8 +121,14 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
// PC-relative relocations:
case R_SPARC_PC10:
case R_SPARC_PC22:
+ case R_SPARC_DISP8:
+ case R_SPARC_DISP16:
case R_SPARC_DISP32:
case R_SPARC_WDISP30:
+ case R_SPARC_WDISP22:
+ case R_SPARC_WDISP16:
+ case R_SPARC_WDISP19:
+ case R_SPARC_DISP64:
rs.processR_PC(type, offset, addend, sym);
continue;
@@ -172,15 +178,30 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
break;
case R_SPARC_DISP32:
// V-disp32
- checkInt(ctx, loc, val, 32, rel);
+ checkIntUInt(ctx, loc, val, 32, rel);
write32be(loc, val);
break;
case R_SPARC_WDISP30:
case R_SPARC_WPLT30:
// V-disp30
- checkInt(ctx, loc, val, 32, rel);
+ checkIntUInt(ctx, loc, val, 32, rel);
write32be(loc, (read32be(loc) & ~0x3fffffff) | ((val >> 2) & 0x3fffffff));
break;
+ case R_SPARC_DISP8:
+ // V-byte8
+ checkIntUInt(ctx, loc, val, 8, rel);
+ *loc = val;
+ break;
+ case R_SPARC_DISP16:
+ // V-half16
+ checkIntUInt(ctx, loc, val, 16, rel);
+ write16be(loc, val);
+ break;
+ case R_SPARC_WDISP22:
+ // V-disp22
+ checkIntUInt(ctx, loc, val, 24, rel);
+ write32be(loc, (read32be(loc) & ~0x003fffff) | ((val >> 2) & 0x003fffff));
+ break;
case R_SPARC_22:
// V-imm22
checkUInt(ctx, loc, val, 22, rel);
@@ -192,11 +213,15 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
write32be(loc, (read32be(loc) & ~0x00001fff) | (val & 0x00001fff));
break;
case R_SPARC_GOT22:
- case R_SPARC_PC22:
case R_SPARC_LM22:
// T-imm22
write32be(loc, (read32be(loc) & ~0x003fffff) | ((val >> 10) & 0x003fffff));
break;
+ case R_SPARC_PC22:
+ // V-disp22
+ checkIntUInt(ctx, loc, val, 32, rel);
+ write32be(loc, (read32be(loc) & ~0x003fffff) | ((val >> 10) & 0x003fffff));
+ break;
case R_SPARC_HI22:
// V-imm22
checkUInt(ctx, loc, val, 32, rel);
@@ -204,9 +229,15 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
break;
case R_SPARC_WDISP19:
// V-disp19
- checkInt(ctx, loc, val, 21, rel);
+ checkIntUInt(ctx, loc, val, 21, rel);
write32be(loc, (read32be(loc) & ~0x0007ffff) | ((val >> 2) & 0x0007ffff));
break;
+ case R_SPARC_WDISP16:
+ // V-d2/disp14
+ checkIntUInt(ctx, loc, val, 18, rel);
+ write32be(loc, (read32be(loc) & ~0x0303fff) | (((val >> 2) & 0xc000) << 6) |
+ ((val >> 2) & 0x00003fff));
+ break;
case R_SPARC_GOT10:
case R_SPARC_PC10:
// T-simm10
@@ -217,6 +248,7 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
write32be(loc, (read32be(loc) & ~0x000003ff) | (val & 0x000003ff));
break;
case R_SPARC_64:
+ case R_SPARC_DISP64:
case R_SPARC_UA64:
// V-xword64
write64be(loc, val);
diff --git a/lld/test/ELF/sparcv9-pc-relocs.s b/lld/test/ELF/sparcv9-pc-relocs.s
new file mode 100644
index 0000000000000..d878d52f3c1ef
--- /dev/null
+++ b/lld/test/ELF/sparcv9-pc-relocs.s
@@ -0,0 +1,17 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %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>:
+# CHECK: ba,a
+# CHECK: brnz
+
+.text
+.globl _start
+_start:
+ ba,a target
+ brnz %g1, target
+ .xword target - .
+target:
+ nop
>From a8ebfe23a67525a6312e6cb9f45b565d0038af24 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:49:03 +0200
Subject: [PATCH 08/18] [lld][ELF][SPARC] Support GOT13 relocations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 2 ++
lld/test/ELF/sparcv9-got13.s | 11 +++++++++++
2 files changed, 13 insertions(+)
create mode 100644 lld/test/ELF/sparcv9-got13.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index 6cf9c853073f1..4fe3f24883bd8 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -134,6 +134,7 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
// GOT relocations:
case R_SPARC_GOT10:
+ case R_SPARC_GOT13:
case R_SPARC_GOT22:
expr = R_GOT_OFF;
break;
@@ -208,6 +209,7 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
write32be(loc, (read32be(loc) & ~0x003fffff) | (val & 0x003fffff));
break;
case R_SPARC_13:
+ case R_SPARC_GOT13:
// V-simm13
checkIntUInt(ctx, loc, val, 13, rel);
write32be(loc, (read32be(loc) & ~0x00001fff) | (val & 0x00001fff));
diff --git a/lld/test/ELF/sparcv9-got13.s b/lld/test/ELF/sparcv9-got13.s
new file mode 100644
index 0000000000000..a1de7513ac9ff
--- /dev/null
+++ b/lld/test/ELF/sparcv9-got13.s
@@ -0,0 +1,11 @@
+# REQUIRES: sparc
+# RUN: llvm-mc --position-independent -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-readelf -r %t.so | FileCheck %s
+
+# CHECK: R_SPARC_GLOB_DAT {{.*}} value
+
+.text
+.globl _start
+_start:
+ ldx [%i0 + value], %i0
>From de425672bdf57a23a73dba611cc2038d0aa00f65 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:49:18 +0200
Subject: [PATCH 09/18] [lld][ELF][SPARC] Add SPARC GOT header entry
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 6 ++++++
lld/test/ELF/sparcv9-got-header.s | 11 +++++++++++
2 files changed, 17 insertions(+)
create mode 100644 lld/test/ELF/sparcv9-got-header.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index 4fe3f24883bd8..130d01e4d34fb 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -25,6 +25,7 @@ class SPARCV9 final : public TargetInfo {
SPARCV9(Ctx &);
RelExpr getRelExpr(RelType type, const Symbol &s,
const uint8_t *loc) const override;
+ void writeGotHeader(uint8_t *buf) const override;
void writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const override;
template <class ELFT, class RelTy>
@@ -43,6 +44,7 @@ SPARCV9::SPARCV9(Ctx &ctx) : TargetInfo(ctx) {
pltRel = R_SPARC_JMP_SLOT;
relativeRel = R_SPARC_RELATIVE;
symbolicRel = R_SPARC_64;
+ gotHeaderEntriesNum = 1;
pltEntrySize = 32;
pltHeaderSize = 4 * pltEntrySize;
@@ -295,6 +297,10 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
}
}
+void SPARCV9::writeGotHeader(uint8_t *buf) const {
+ write64be(buf, ctx.in.dynamic->getVA());
+}
+
void SPARCV9::writePlt(uint8_t *buf, const Symbol & /*sym*/,
uint64_t pltEntryAddr) const {
const uint8_t pltData[] = {
diff --git a/lld/test/ELF/sparcv9-got-header.s b/lld/test/ELF/sparcv9-got-header.s
new file mode 100644
index 0000000000000..d4630104e5fba
--- /dev/null
+++ b/lld/test/ELF/sparcv9-got-header.s
@@ -0,0 +1,11 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-readelf -S -x .got %t.so | FileCheck %s
+
+# CHECK: .got PROGBITS {{[0-9a-f]+}} {{[0-9a-f]+}} 000008
+# CHECK-LABEL: Hex dump of section '.got':
+# CHECK-NEXT: 0x{{[0-9a-f]+}} {{[0-9a-f ]*[1-9a-f][0-9a-f ]*}}
+
+.data
+.xword _GLOBAL_OFFSET_TABLE_
>From bdb0275875841f04cfa354b748c1f339edc44d53 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:49:31 +0200
Subject: [PATCH 10/18] [lld][ELF][SPARC] Support TLS GD relocations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 47 +++++++++++++++++++++++++++++++++++
lld/test/ELF/sparcv9-tls-gd.s | 22 ++++++++++++++++
2 files changed, 69 insertions(+)
create mode 100644 lld/test/ELF/sparcv9-tls-gd.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index 130d01e4d34fb..55d1dc5ff8e4d 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -6,7 +6,9 @@
//
//===----------------------------------------------------------------------===//
+#include "InputFiles.h"
#include "RelocScan.h"
+#include "SymbolTable.h"
#include "Symbols.h"
#include "SyntheticSections.h"
#include "Target.h"
@@ -35,6 +37,7 @@ class SPARCV9 final : public TargetInfo {
}
void relocate(uint8_t *loc, const Relocation &rel,
uint64_t val) const override;
+ void finalizeRelocScan() override;
};
} // namespace
@@ -44,6 +47,9 @@ SPARCV9::SPARCV9(Ctx &ctx) : TargetInfo(ctx) {
pltRel = R_SPARC_JMP_SLOT;
relativeRel = R_SPARC_RELATIVE;
symbolicRel = R_SPARC_64;
+ tlsGotRel = R_SPARC_TLS_TPOFF64;
+ tlsModuleIndexRel = R_SPARC_TLS_DTPMOD64;
+ tlsOffsetRel = R_SPARC_TLS_DTPOFF64;
gotHeaderEntriesNum = 1;
pltEntrySize = 32;
pltHeaderSize = 4 * pltEntrySize;
@@ -91,6 +97,7 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
RelExpr expr;
switch (type) {
case R_SPARC_NONE:
+ case R_SPARC_TLS_GD_ADD:
continue;
// Absolute relocations:
@@ -119,6 +126,9 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
case R_SPARC_WPLT30:
rs.processR_PLT_PC(type, offset, addend, sym);
continue;
+ case R_SPARC_TLS_GD_CALL:
+ sec.addReloc({R_PLT_PC, type, offset, addend, &sym});
+ continue;
// PC-relative relocations:
case R_SPARC_PC10:
@@ -148,6 +158,11 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
continue;
expr = R_TPREL;
break;
+ case R_SPARC_TLS_GD_HI22:
+ case R_SPARC_TLS_GD_LO10:
+ sym.setFlags(NEEDS_TLSGD);
+ sec.addReloc({R_TLSGD_GOT, type, offset, addend, &sym});
+ continue;
default:
Err(ctx) << getErrorLoc(ctx, sec.content().data() + offset)
@@ -186,6 +201,7 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
break;
case R_SPARC_WDISP30:
case R_SPARC_WPLT30:
+ case R_SPARC_TLS_GD_CALL:
// V-disp30
checkIntUInt(ctx, loc, val, 32, rel);
write32be(loc, (read32be(loc) & ~0x3fffffff) | ((val >> 2) & 0x3fffffff));
@@ -218,6 +234,7 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
break;
case R_SPARC_GOT22:
case R_SPARC_LM22:
+ case R_SPARC_TLS_GD_HI22:
// T-imm22
write32be(loc, (read32be(loc) & ~0x003fffff) | ((val >> 10) & 0x003fffff));
break;
@@ -244,6 +261,7 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
break;
case R_SPARC_GOT10:
case R_SPARC_PC10:
+ case R_SPARC_TLS_GD_LO10:
// T-simm10
write32be(loc, (read32be(loc) & ~0x000003ff) | (val & 0x000003ff));
break;
@@ -283,6 +301,8 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
// T-imm13
write32be(loc, (read32be(loc) & ~0x00000fff) | (val & 0x00000fff));
break;
+ case R_SPARC_TLS_GD_ADD:
+ break;
case R_SPARC_TLS_LE_HIX22:
// T-imm22
write32be(loc, (read32be(loc) & ~0x003fffff) | ((~val >> 10) & 0x003fffff));
@@ -297,6 +317,33 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
}
}
+static bool isTlsCall(RelType type) { return type == R_SPARC_TLS_GD_CALL; }
+
+void SPARCV9::finalizeRelocScan() {
+ Symbol *tga = nullptr;
+
+ for (ELFFileBase *file : ctx.objectFiles) {
+ for (InputSectionBase *s : file->getSections()) {
+ auto *isec = dyn_cast_or_null<InputSection>(s);
+ if (!isec || !isec->isLive())
+ continue;
+ for (Relocation &rel : isec->relocs()) {
+ if (rel.expr != R_PLT_PC || !isTlsCall(rel.type))
+ continue;
+ if (!tga) {
+ tga = ctx.symtab->addSymbol(Undefined{ctx.internalFile,
+ "__tls_get_addr", STB_GLOBAL,
+ STV_DEFAULT, STT_FUNC});
+ tga->isUsedInRegularObj = true;
+ tga->isPreemptible = true;
+ tga->setFlags(NEEDS_PLT | USED);
+ }
+ rel.sym = tga;
+ }
+ }
+ }
+}
+
void SPARCV9::writeGotHeader(uint8_t *buf) const {
write64be(buf, ctx.in.dynamic->getVA());
}
diff --git a/lld/test/ELF/sparcv9-tls-gd.s b/lld/test/ELF/sparcv9-tls-gd.s
new file mode 100644
index 0000000000000..e6427cf41301e
--- /dev/null
+++ b/lld/test/ELF/sparcv9-tls-gd.s
@@ -0,0 +1,22 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-readelf -r %t.so | FileCheck %s
+
+# CHECK: R_SPARC_TLS_DTPMOD64 {{.*}} a
+# CHECK: R_SPARC_TLS_DTPOFF64 {{.*}} a
+# CHECK: R_SPARC_JMP_SLOT {{.*}} __tls_get_addr
+
+.text
+.globl _start
+_start:
+ sethi %tgd_hi22(a), %i1
+ add %i1, %tgd_lo10(a), %i1
+ add %i0, %i1, %o0, %tgd_add(a)
+ call __tls_get_addr, %tgd_call(a)
+ nop
+
+.section .tbss,"awT", at nobits
+.globl a
+a:
+ .zero 8
>From 13fdb5ecf812b2a6c304b1b0d3d7d08356620594 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:49:43 +0200
Subject: [PATCH 11/18] [lld][ELF][SPARC] Support TLS LD relocations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 26 +++++++++++++++++++++++++-
lld/test/ELF/sparcv9-tls-ld.s | 25 +++++++++++++++++++++++++
2 files changed, 50 insertions(+), 1 deletion(-)
create mode 100644 lld/test/ELF/sparcv9-tls-ld.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index 55d1dc5ff8e4d..6b8670736d469 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -98,6 +98,8 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
switch (type) {
case R_SPARC_NONE:
case R_SPARC_TLS_GD_ADD:
+ case R_SPARC_TLS_LDM_ADD:
+ case R_SPARC_TLS_LDO_ADD:
continue;
// Absolute relocations:
@@ -127,6 +129,7 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
rs.processR_PLT_PC(type, offset, addend, sym);
continue;
case R_SPARC_TLS_GD_CALL:
+ case R_SPARC_TLS_LDM_CALL:
sec.addReloc({R_PLT_PC, type, offset, addend, &sym});
continue;
@@ -163,6 +166,15 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
sym.setFlags(NEEDS_TLSGD);
sec.addReloc({R_TLSGD_GOT, type, offset, addend, &sym});
continue;
+ case R_SPARC_TLS_LDM_HI22:
+ case R_SPARC_TLS_LDM_LO10:
+ ctx.needsTlsLd.store(true, std::memory_order_relaxed);
+ sec.addReloc({R_TLSLD_GOT, type, offset, addend, &sym});
+ continue;
+ case R_SPARC_TLS_LDO_HIX22:
+ case R_SPARC_TLS_LDO_LOX10:
+ expr = R_DTPREL;
+ break;
default:
Err(ctx) << getErrorLoc(ctx, sec.content().data() + offset)
@@ -202,6 +214,7 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
case R_SPARC_WDISP30:
case R_SPARC_WPLT30:
case R_SPARC_TLS_GD_CALL:
+ case R_SPARC_TLS_LDM_CALL:
// V-disp30
checkIntUInt(ctx, loc, val, 32, rel);
write32be(loc, (read32be(loc) & ~0x3fffffff) | ((val >> 2) & 0x3fffffff));
@@ -235,6 +248,8 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
case R_SPARC_GOT22:
case R_SPARC_LM22:
case R_SPARC_TLS_GD_HI22:
+ case R_SPARC_TLS_LDM_HI22:
+ case R_SPARC_TLS_LDO_HIX22:
// T-imm22
write32be(loc, (read32be(loc) & ~0x003fffff) | ((val >> 10) & 0x003fffff));
break;
@@ -262,6 +277,7 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
case R_SPARC_GOT10:
case R_SPARC_PC10:
case R_SPARC_TLS_GD_LO10:
+ case R_SPARC_TLS_LDM_LO10:
// T-simm10
write32be(loc, (read32be(loc) & ~0x000003ff) | (val & 0x000003ff));
break;
@@ -302,6 +318,8 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
write32be(loc, (read32be(loc) & ~0x00000fff) | (val & 0x00000fff));
break;
case R_SPARC_TLS_GD_ADD:
+ case R_SPARC_TLS_LDM_ADD:
+ case R_SPARC_TLS_LDO_ADD:
break;
case R_SPARC_TLS_LE_HIX22:
// T-imm22
@@ -312,12 +330,18 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
// T-simm13
write32be(loc, (read32be(loc) & ~0x00001fff) | (val & 0x000003ff) | 0x1C00);
break;
+ case R_SPARC_TLS_LDO_LOX10:
+ // T-simm13
+ write32be(loc, (read32be(loc) & ~0x00001fff) | (val & 0x000003ff));
+ break;
default:
llvm_unreachable("unknown relocation");
}
}
-static bool isTlsCall(RelType type) { return type == R_SPARC_TLS_GD_CALL; }
+static bool isTlsCall(RelType type) {
+ return type == R_SPARC_TLS_GD_CALL || type == R_SPARC_TLS_LDM_CALL;
+}
void SPARCV9::finalizeRelocScan() {
Symbol *tga = nullptr;
diff --git a/lld/test/ELF/sparcv9-tls-ld.s b/lld/test/ELF/sparcv9-tls-ld.s
new file mode 100644
index 0000000000000..db0be3cd8bc19
--- /dev/null
+++ b/lld/test/ELF/sparcv9-tls-ld.s
@@ -0,0 +1,25 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-readelf -r %t.so | FileCheck %s
+
+# CHECK: R_SPARC_TLS_DTPMOD64 0
+# CHECK: R_SPARC_JMP_SLOT {{.*}} __tls_get_addr
+
+.text
+.globl _start
+_start:
+ sethi %tldo_hix22(a), %i1
+ sethi %tldm_hi22(a), %i2
+ add %i2, %tldm_lo10(a), %i2
+ add %i0, %i2, %o0, %tldm_add(a)
+ xor %i1, %tldo_lox10(a), %i0
+ call __tls_get_addr, %tldm_call(a)
+ nop
+ add %o0, %i0, %o0, %tldo_add(a)
+
+.section .tbss,"awT", at nobits
+.globl a
+.hidden a
+a:
+ .zero 8
>From 82a90b6bf394799b1d1e2e9e9500909058ea567d Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:49:56 +0200
Subject: [PATCH 12/18] [lld][ELF][SPARC] Support TLS IE relocations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 13 +++++++++++++
lld/test/ELF/sparcv9-tls-ie.s | 19 +++++++++++++++++++
2 files changed, 32 insertions(+)
create mode 100644 lld/test/ELF/sparcv9-tls-ie.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index 6b8670736d469..d50172a922c4c 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -100,6 +100,9 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
case R_SPARC_TLS_GD_ADD:
case R_SPARC_TLS_LDM_ADD:
case R_SPARC_TLS_LDO_ADD:
+ case R_SPARC_TLS_IE_LD:
+ case R_SPARC_TLS_IE_LDX:
+ case R_SPARC_TLS_IE_ADD:
continue;
// Absolute relocations:
@@ -175,6 +178,11 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
case R_SPARC_TLS_LDO_LOX10:
expr = R_DTPREL;
break;
+ case R_SPARC_TLS_IE_HI22:
+ case R_SPARC_TLS_IE_LO10:
+ sym.setFlags(NEEDS_TLSIE);
+ sec.addReloc({R_GOT, type, offset, addend, &sym});
+ continue;
default:
Err(ctx) << getErrorLoc(ctx, sec.content().data() + offset)
@@ -250,6 +258,7 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
case R_SPARC_TLS_GD_HI22:
case R_SPARC_TLS_LDM_HI22:
case R_SPARC_TLS_LDO_HIX22:
+ case R_SPARC_TLS_IE_HI22:
// T-imm22
write32be(loc, (read32be(loc) & ~0x003fffff) | ((val >> 10) & 0x003fffff));
break;
@@ -278,6 +287,7 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
case R_SPARC_PC10:
case R_SPARC_TLS_GD_LO10:
case R_SPARC_TLS_LDM_LO10:
+ case R_SPARC_TLS_IE_LO10:
// T-simm10
write32be(loc, (read32be(loc) & ~0x000003ff) | (val & 0x000003ff));
break;
@@ -320,6 +330,9 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
case R_SPARC_TLS_GD_ADD:
case R_SPARC_TLS_LDM_ADD:
case R_SPARC_TLS_LDO_ADD:
+ case R_SPARC_TLS_IE_LD:
+ case R_SPARC_TLS_IE_LDX:
+ case R_SPARC_TLS_IE_ADD:
break;
case R_SPARC_TLS_LE_HIX22:
// T-imm22
diff --git a/lld/test/ELF/sparcv9-tls-ie.s b/lld/test/ELF/sparcv9-tls-ie.s
new file mode 100644
index 0000000000000..b814c9ab07be4
--- /dev/null
+++ b/lld/test/ELF/sparcv9-tls-ie.s
@@ -0,0 +1,19 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-readelf -r %t.so | FileCheck %s
+
+# CHECK: R_SPARC_TLS_TPOFF64 {{.*}} b
+
+.text
+.globl _start
+_start:
+ sethi %tie_hi22(b), %i3
+ add %i3, %tie_lo10(b), %i3
+ ldx [%i0 + %i3], %i3, %tie_ldx(b)
+ add %i3, %g7, %i3, %tie_add(b)
+
+.section .tbss,"awT", at nobits
+.globl b
+b:
+ .zero 8
>From be309c5182a01c2fe23b85fa2f48cfd0ac933f3a Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:50:07 +0200
Subject: [PATCH 13/18] [lld][ELF][SPARC] Apply PLT relocations to .plt
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 1 +
lld/ELF/Relocations.cpp | 14 ++++++++------
lld/ELF/Symbols.cpp | 6 ++++++
lld/ELF/Symbols.h | 1 +
lld/ELF/SyntheticSections.cpp | 17 ++++++++++++-----
lld/ELF/Target.h | 5 ++++-
lld/ELF/Writer.cpp | 10 +++++++---
lld/test/ELF/sparcv9-plt.s | 23 +++++++++++++++++++++++
8 files changed, 62 insertions(+), 15 deletions(-)
create mode 100644 lld/test/ELF/sparcv9-plt.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index d50172a922c4c..8203e8d5d5ba8 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -53,6 +53,7 @@ SPARCV9::SPARCV9(Ctx &ctx) : TargetInfo(ctx) {
gotHeaderEntriesNum = 1;
pltEntrySize = 32;
pltHeaderSize = 4 * pltEntrySize;
+ usesGotPlt = false;
defaultCommonPageSize = 8192;
defaultMaxPageSize = 0x100000;
diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 0d45236e6d11e..3ff302042eb5f 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -747,14 +747,16 @@ static void addRelativeReloc(Ctx &ctx, InputSectionBase &isec,
template <class PltSection, class GotPltSection>
static void addPltEntry(Ctx &ctx, PltSection &plt, GotPltSection &gotPlt,
RelocationBaseSection &rel, RelType type, Symbol &sym) {
+ RelExpr expr = sym.isPreemptible ? R_ADDEND : R_ABS;
plt.addEntry(sym);
- gotPlt.addEntry(sym);
- if (sym.isPreemptible)
- rel.addReloc(
- {type, &gotPlt, sym.getGotPltOffset(ctx), true, sym, 0, R_ADDEND});
- else
+ if (ctx.target->usesGotPlt) {
+ gotPlt.addEntry(sym);
+ rel.addReloc({type, &gotPlt, sym.getGotPltOffset(ctx), sym.isPreemptible,
+ sym, 0, expr});
+ } else {
rel.addReloc(
- {type, &gotPlt, sym.getGotPltOffset(ctx), false, sym, 0, R_ABS});
+ {type, &plt, sym.getPltOffset(ctx), sym.isPreemptible, sym, 0, expr});
+ }
}
void elf::addGotEntry(Ctx &ctx, Symbol &sym) {
diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index 7bad4ceccec33..67e07b840cb82 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -181,6 +181,12 @@ uint64_t Symbol::getGotPltOffset(Ctx &ctx) const {
ctx.target->gotEntrySize;
}
+uint64_t Symbol::getPltOffset(Ctx &ctx) const {
+ if (isInIplt)
+ return getPltIdx(ctx) * ctx.target->ipltEntrySize;
+ return ctx.in.plt->headerSize + getPltIdx(ctx) * ctx.target->pltEntrySize;
+}
+
uint64_t Symbol::getPltVA(Ctx &ctx) const {
uint64_t outVA = isInIplt ? ctx.in.iplt->getVA() +
getPltIdx(ctx) * ctx.target->ipltEntrySize
diff --git a/lld/ELF/Symbols.h b/lld/ELF/Symbols.h
index b7d0b13ae476c..0893776882dc6 100644
--- a/lld/ELF/Symbols.h
+++ b/lld/ELF/Symbols.h
@@ -198,6 +198,7 @@ class Symbol {
uint64_t getGotVA(Ctx &) const;
uint64_t getGotPltOffset(Ctx &) const;
uint64_t getGotPltVA(Ctx &) const;
+ uint64_t getPltOffset(Ctx &) const;
uint64_t getPltVA(Ctx &) const;
uint64_t getSize() const;
OutputSection *getOutputSection() const;
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index b9a42590dc8e7..1fd1923372a2d 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -1523,9 +1523,14 @@ void RelocationBaseSection::finalizeContents() {
else
getParent()->link = 0;
- if (ctx.in.relaPlt.get() == this && ctx.in.gotPlt->getParent()) {
- getParent()->flags |= ELF::SHF_INFO_LINK;
- getParent()->info = ctx.in.gotPlt->getParent()->sectionIndex;
+ if (ctx.in.relaPlt.get() == this) {
+ if (ctx.target->usesGotPlt && ctx.in.gotPlt->getParent()) {
+ getParent()->flags |= ELF::SHF_INFO_LINK;
+ getParent()->info = ctx.in.gotPlt->getParent()->sectionIndex;
+ } else if (ctx.in.plt->getParent()) {
+ getParent()->flags |= ELF::SHF_INFO_LINK;
+ getParent()->info = ctx.in.plt->getParent()->sectionIndex;
+ }
}
}
@@ -4529,10 +4534,12 @@ template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
// _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
// it as a relocation and ensure the referenced section is created.
if (ctx.sym.globalOffsetTable && ctx.arg.emachine != EM_MIPS) {
- if (ctx.target->gotBaseSymInGotPlt)
+ if (ctx.target->gotBaseSymInGotPlt) {
+ assert(ctx.target->usesGotPlt);
ctx.in.gotPlt->hasGotPltOffRel = true;
- else
+ } else {
ctx.in.got->hasGotOffRel = true;
+ }
}
// We always need to add rel[a].plt to output if it has entries.
diff --git a/lld/ELF/Target.h b/lld/ELF/Target.h
index 199f76e3fe851..9cf91485fa700 100644
--- a/lld/ELF/Target.h
+++ b/lld/ELF/Target.h
@@ -144,7 +144,8 @@ class TargetInfo {
uint64_t getImageBase() const;
- // True if _GLOBAL_OFFSET_TABLE_ is relative to .got.plt, false if .got.
+ // True if _GLOBAL_OFFSET_TABLE_ is relative to .got.plt, false if .got. If
+ // true, usesGotPlt must also be true.
bool gotBaseSymInGotPlt = false;
static constexpr RelType noneRel = 0;
@@ -171,6 +172,8 @@ class TargetInfo {
// On PPC ELF V2 abi, the first entry in the .got is the .TOC.
unsigned gotHeaderEntriesNum = 0;
+ bool usesGotPlt = true;
+
// On PPC ELF V2 abi, the dynamic section needs DT_PPC64_OPT (DT_LOPROC + 3)
// to be set to 0x2 if there can be multiple TOC's. Although we do not emit
// multiple TOC's, there can be a mix of TOC and NOTOC addressing which
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 49d3f903b3743..37338f53cf9b6 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -609,7 +609,7 @@ static bool isRelroSection(Ctx &ctx, const OutputSection *sec) {
// by default resolved lazily, so we usually cannot put it into RELRO.
// However, if "-z now" is given, the lazy symbol resolution is
// disabled, which enables us to put it into RELRO.
- if (sec == ctx.in.gotPlt->getParent())
+ if (ctx.target->usesGotPlt && sec == ctx.in.gotPlt->getParent())
return ctx.arg.zNow;
if (ctx.in.relroPadding && sec == ctx.in.relroPadding->getParent())
@@ -835,10 +835,14 @@ template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
if (ctx.sym.globalOffsetTable) {
// The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
// to the start of the .got or .got.plt section.
- InputSection *sec = ctx.in.gotPlt.get();
- if (!ctx.target->gotBaseSymInGotPlt)
+ InputSection *sec;
+ if (ctx.target->gotBaseSymInGotPlt) {
+ assert(ctx.target->usesGotPlt);
+ sec = ctx.in.gotPlt.get();
+ } else {
sec = ctx.in.mipsGot ? cast<InputSection>(ctx.in.mipsGot.get())
: cast<InputSection>(ctx.in.got.get());
+ }
ctx.sym.globalOffsetTable->section = sec;
}
diff --git a/lld/test/ELF/sparcv9-plt.s b/lld/test/ELF/sparcv9-plt.s
new file mode 100644
index 0000000000000..a45369a21a8b2
--- /dev/null
+++ b/lld/test/ELF/sparcv9-plt.s
@@ -0,0 +1,23 @@
+# REQUIRES: sparc
+# RUN: llvm-mc --position-independent -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-readelf -SW -r %t.so | FileCheck --check-prefix=READELF %s
+# RUN: llvm-readobj --section-headers %t.so | FileCheck --check-prefix=SEC %s
+
+# READELF-NOT: .got.plt
+# READELF: .plt PROGBITS
+# READELF-NOT: .got.plt
+# READELF: Relocation section '.rela.plt'
+# READELF: R_SPARC_JMP_SLOT {{.*}} foo
+
+# SEC: Name: .rela.plt
+# SEC: Flags [
+# SEC-NEXT: SHF_ALLOC
+# SEC-NEXT: SHF_INFO_LINK
+# SEC-NEXT: ]
+
+.text
+.globl _start
+_start:
+ call foo
+ nop
>From 531bf34a44771fbf863054fffdd3a91c0c0ffbd1 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:50:17 +0200
Subject: [PATCH 14/18] [lld][ELF][SPARC] Align PLT to 256 bytes
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/SyntheticSections.cpp | 4 +++-
lld/test/ELF/sparcv9-plt-align.s | 12 ++++++++++++
2 files changed, 15 insertions(+), 1 deletion(-)
create mode 100644 lld/test/ELF/sparcv9-plt-align.s
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 1fd1923372a2d..bc1c2c042618a 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -2407,8 +2407,10 @@ PltSection::PltSection(Ctx &ctx)
// The PLT needs to be writable on SPARC as the dynamic linker will
// modify the instructions in the PLT entries.
- if (ctx.arg.emachine == EM_SPARCV9)
+ if (ctx.arg.emachine == EM_SPARCV9) {
this->flags |= SHF_WRITE;
+ addralign = 256;
+ }
}
void PltSection::writeTo(uint8_t *buf) {
diff --git a/lld/test/ELF/sparcv9-plt-align.s b/lld/test/ELF/sparcv9-plt-align.s
new file mode 100644
index 0000000000000..d0dad3095823b
--- /dev/null
+++ b/lld/test/ELF/sparcv9-plt-align.s
@@ -0,0 +1,12 @@
+# REQUIRES: sparc
+# RUN: llvm-mc --position-independent -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-readelf -SW %t.so | FileCheck %s
+
+# CHECK: .plt PROGBITS {{[0-9a-f]+}} {{[0-9a-f]+}} {{[0-9a-f]+}} 00 WAX 0 0 256
+
+.text
+.globl _start
+_start:
+ call foo
+ nop
>From 39a68a27a3d791050120d248f46639dd4096bcc9 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:50:28 +0200
Subject: [PATCH 15/18] [lld][ELF][SPARC] Set the IRELATIVE relocation
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 6 ++++--
lld/test/ELF/sparcv9-ifunc.s | 21 +++++++++++++++++++++
2 files changed, 25 insertions(+), 2 deletions(-)
create mode 100644 lld/test/ELF/sparcv9-ifunc.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index 8203e8d5d5ba8..17a26eb50d2d5 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -46,6 +46,7 @@ SPARCV9::SPARCV9(Ctx &ctx) : TargetInfo(ctx) {
gotRel = R_SPARC_GLOB_DAT;
pltRel = R_SPARC_JMP_SLOT;
relativeRel = R_SPARC_RELATIVE;
+ iRelativeRel = R_SPARC_IRELATIVE;
symbolicRel = R_SPARC_64;
tlsGotRel = R_SPARC_TLS_TPOFF64;
tlsModuleIndexRel = R_SPARC_TLS_DTPMOD64;
@@ -386,7 +387,7 @@ void SPARCV9::writeGotHeader(uint8_t *buf) const {
write64be(buf, ctx.in.dynamic->getVA());
}
-void SPARCV9::writePlt(uint8_t *buf, const Symbol & /*sym*/,
+void SPARCV9::writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const {
const uint8_t pltData[] = {
0x03, 0x00, 0x00, 0x00, // sethi (. - .PLT0), %g1
@@ -400,7 +401,8 @@ void SPARCV9::writePlt(uint8_t *buf, const Symbol & /*sym*/,
};
memcpy(buf, pltData, sizeof(pltData));
- uint64_t off = pltEntryAddr - ctx.in.plt->getVA();
+ uint64_t pltBase = sym.isInIplt ? ctx.in.iplt->getVA() : ctx.in.plt->getVA();
+ uint64_t off = pltEntryAddr - pltBase;
relocateNoSym(buf, R_SPARC_22, off);
relocateNoSym(buf + 4, R_SPARC_WDISP19, -(off + 4 - pltEntrySize));
}
diff --git a/lld/test/ELF/sparcv9-ifunc.s b/lld/test/ELF/sparcv9-ifunc.s
new file mode 100644
index 0000000000000..34ef9bd627f03
--- /dev/null
+++ b/lld/test/ELF/sparcv9-ifunc.s
@@ -0,0 +1,21 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-readelf -r %t.so | FileCheck %s
+
+# CHECK: R_SPARC_IRELATIVE
+
+.text
+.globl _start
+_start:
+ nop
+
+.type ifunc, at gnu_indirect_function
+.globl ifunc
+.hidden ifunc
+ifunc:
+ retl
+ nop
+
+.data
+.xword ifunc
>From 3261219751db7c53341fc73226a28a9578d68489 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:50:39 +0200
Subject: [PATCH 16/18] [lld][ELF][SPARC] Convert aligned UA64 dynamic relocs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 7 +++++++
lld/ELF/SyntheticSections.cpp | 12 ++++++++++++
lld/test/ELF/sparcv9-ua64-dynrel.s | 29 +++++++++++++++++++++++++++++
3 files changed, 48 insertions(+)
create mode 100644 lld/test/ELF/sparcv9-ua64-dynrel.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index 17a26eb50d2d5..e45bf9cefacba 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -27,6 +27,7 @@ class SPARCV9 final : public TargetInfo {
SPARCV9(Ctx &);
RelExpr getRelExpr(RelType type, const Symbol &s,
const uint8_t *loc) const override;
+ RelType getDynRel(RelType type) const override;
void writeGotHeader(uint8_t *buf) const override;
void writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const override;
@@ -82,6 +83,12 @@ RelExpr SPARCV9::getRelExpr(RelType type, const Symbol &s,
}
}
+RelType SPARCV9::getDynRel(RelType type) const {
+ if (type == symbolicRel || type == R_SPARC_UA64)
+ return type;
+ return R_SPARC_NONE;
+}
+
template <class ELFT, class RelTy>
void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
RelocScan rs(ctx, &sec);
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index bc1c2c042618a..aa5fabe9f9cbc 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -1536,6 +1536,18 @@ void RelocationBaseSection::finalizeContents() {
void DynamicReloc::finalize(Ctx &ctx, SymbolTableBaseSection *symt) {
r_offset = getOffset();
+ if (ctx.arg.emachine == EM_SPARCV9 && type == R_SPARC_UA64 &&
+ needsDynSymIndex() && !sym->isPreemptible) {
+ if (r_offset % 8 != 0) {
+ Err(ctx) << "R_SPARC_UA64 relocation at offset " << r_offset
+ << " against non-preemptible symbol " << sym
+ << " is not 8-byte aligned";
+ } else {
+ type = ctx.target->relativeRel;
+ isAgainstSymbol = false;
+ expr = R_ABS;
+ }
+ }
r_sym = getSymIndex(symt);
addend = computeAddend(ctx);
isFinal = true; // Catch errors
diff --git a/lld/test/ELF/sparcv9-ua64-dynrel.s b/lld/test/ELF/sparcv9-ua64-dynrel.s
new file mode 100644
index 0000000000000..9fcab99365bcc
--- /dev/null
+++ b/lld/test/ELF/sparcv9-ua64-dynrel.s
@@ -0,0 +1,29 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-readelf -r %t.so | FileCheck %s
+
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 --defsym ERR=1 %s -o %t.err.o
+# RUN: not ld.lld -shared %t.err.o -o /dev/null 2>&1 | FileCheck --check-prefix=ERR %s
+
+# CHECK: R_SPARC_RELATIVE
+# CHECK-NOT: R_SPARC_UA64
+
+# ERR: R_SPARC_UA64 relocation at offset {{[0-9]+}} against non-preemptible symbol local is not 8-byte aligned
+
+.data
+.p2align 3
+aligned:
+ .xword 0
+ .reloc aligned, R_SPARC_UA64, local
+
+.ifdef ERR
+ .byte 0
+unaligned:
+ .xword 0
+ .reloc unaligned, R_SPARC_UA64, local
+.endif
+
+.hidden local
+local:
+ .xword 0
>From a592994668fdb1e319519b4bc61a729cc03afc63 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:50:49 +0200
Subject: [PATCH 17/18] [lld][ELF][SPARC] Handle GOTDATA relocations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 22 ++++++++++++++++++++++
lld/test/ELF/sparcv9-gotdata.s | 20 ++++++++++++++++++++
2 files changed, 42 insertions(+)
create mode 100644 lld/test/ELF/sparcv9-gotdata.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index e45bf9cefacba..52038e4e2f6f0 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -163,8 +163,15 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
case R_SPARC_GOT10:
case R_SPARC_GOT13:
case R_SPARC_GOT22:
+ case R_SPARC_GOTDATA_OP_HIX22:
+ case R_SPARC_GOTDATA_OP_LOX10:
+ case R_SPARC_GOTDATA_OP:
expr = R_GOT_OFF;
break;
+ case R_SPARC_GOTDATA_HIX22:
+ case R_SPARC_GOTDATA_LOX10:
+ expr = R_GOTREL;
+ break;
// TLS LE relocations:
case R_SPARC_TLS_LE_HIX22:
@@ -349,9 +356,24 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
break;
case R_SPARC_LOX10:
case R_SPARC_TLS_LE_LOX10:
+ case R_SPARC_GOTDATA_LOX10:
+ case R_SPARC_GOTDATA_OP_LOX10:
// T-simm13
write32be(loc, (read32be(loc) & ~0x00001fff) | (val & 0x000003ff) | 0x1C00);
break;
+ case R_SPARC_GOTDATA_HIX22:
+ // V-imm22
+ checkUInt(ctx, loc, ((int64_t)val < 0 ? ~val : val), 32, rel);
+ write32be(loc, (read32be(loc) & ~0x003fffff) |
+ ((((int64_t)val < 0 ? ~val : val) >> 10) & 0x003fffff));
+ break;
+ case R_SPARC_GOTDATA_OP_HIX22:
+ // T-imm22
+ write32be(loc, (read32be(loc) & ~0x003fffff) |
+ ((((int64_t)val < 0 ? ~val : val) >> 10) & 0x003fffff));
+ break;
+ case R_SPARC_GOTDATA_OP:
+ break;
case R_SPARC_TLS_LDO_LOX10:
// T-simm13
write32be(loc, (read32be(loc) & ~0x00001fff) | (val & 0x000003ff));
diff --git a/lld/test/ELF/sparcv9-gotdata.s b/lld/test/ELF/sparcv9-gotdata.s
new file mode 100644
index 0000000000000..e8cdb3be20670
--- /dev/null
+++ b/lld/test/ELF/sparcv9-gotdata.s
@@ -0,0 +1,20 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-objdump -d --no-show-raw-insn %t.so | FileCheck %s
+# RUN: llvm-readelf -r %t.so | FileCheck --check-prefix=REL %s
+
+# CHECK-LABEL: <extern_ref>:
+# CHECK: ldx [%l7+%l1], %l2
+
+# REL: R_SPARC_GLOB_DAT {{.*}} extern
+
+.text
+.globl extern_ref
+.type extern_ref, at function
+extern_ref:
+ sethi %gdop_hix22(extern), %l1
+ or %l1, %gdop_lox10(extern), %l1
+ ldx [%l7 + %l1], %l2, %gdop(extern)
+ retl
+ nop
>From d6d422db84c91474a9350db4c557c2674caa30f8 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Mon, 6 Jul 2026 11:50:59 +0200
Subject: [PATCH 18/18] [lld][ELF][SPARC] Relax local GOTDATA_OP relocations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: LemonBoy <thatlemon at gmail.com>
Co-authored-by: Alex Rønne Petersen <alex at alexrp.com>
---
lld/ELF/Arch/SPARCV9.cpp | 48 ++++++++++++++++++++++++++++
lld/ELF/InputSection.cpp | 1 +
lld/ELF/Relocations.cpp | 17 ++++++----
lld/ELF/Relocations.h | 1 +
lld/ELF/Target.cpp | 6 ++++
lld/ELF/Target.h | 2 ++
lld/test/ELF/sparcv9-gotdata-relax.s | 25 +++++++++++++++
7 files changed, 94 insertions(+), 6 deletions(-)
create mode 100644 lld/test/ELF/sparcv9-gotdata-relax.s
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index 52038e4e2f6f0..f7aa24629781e 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -39,6 +39,11 @@ class SPARCV9 final : public TargetInfo {
void relocate(uint8_t *loc, const Relocation &rel,
uint64_t val) const override;
void finalizeRelocScan() override;
+ RelExpr adjustGotOffExpr(RelType type, const Symbol &sym, int64_t addend,
+ const uint8_t *loc) const override;
+
+private:
+ void relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) const;
};
} // namespace
@@ -212,6 +217,13 @@ void SPARCV9::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
uint64_t val) const {
+ switch (rel.expr) {
+ case R_RELAX_GOT_OFF:
+ return relaxGot(loc, rel, val);
+ default:
+ break;
+ }
+
switch (rel.type) {
case R_SPARC_8:
// V-byte8
@@ -383,6 +395,42 @@ void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
}
}
+RelExpr SPARCV9::adjustGotOffExpr(RelType type, const Symbol &sym,
+ int64_t addend, const uint8_t *loc) const {
+ switch (type) {
+ case R_SPARC_GOTDATA_OP_HIX22:
+ case R_SPARC_GOTDATA_OP_LOX10:
+ case R_SPARC_GOTDATA_OP:
+ if (sym.isLocal())
+ return R_RELAX_GOT_OFF;
+ [[fallthrough]];
+ default:
+ return R_GOT_OFF;
+ }
+}
+
+void SPARCV9::relaxGot(uint8_t *loc, const Relocation &rel,
+ uint64_t val) const {
+ switch (rel.type) {
+ case R_SPARC_GOTDATA_OP_HIX22:
+ // T-imm22
+ write32be(loc, (read32be(loc) & ~0x003fffff) |
+ ((((int64_t)val < 0 ? ~val : val) >> 10) & 0x003fffff));
+ break;
+ case R_SPARC_GOTDATA_OP_LOX10:
+ // T-imm13
+ write32be(loc, (read32be(loc) & ~0x00001fff) | (val & 0x000003ff) |
+ ((int64_t)val < 0 ? 0x1c00 : 0));
+ break;
+ case R_SPARC_GOTDATA_OP:
+ // ldx [%rs1 + %rs2], %rd -> add %rs1, %rs2, %rd
+ write32be(loc, (read32be(loc) & 0x3e07c01f) | 0x80000000);
+ break;
+ default:
+ llvm_unreachable("unknown relocation");
+ }
+}
+
static bool isTlsCall(RelType type) {
return type == R_SPARC_TLS_GD_CALL || type == R_SPARC_TLS_LDM_CALL;
}
diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index c78800787d27e..c26425bbc4a7b 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -833,6 +833,7 @@ uint64_t InputSectionBase::getRelocTargetVA(Ctx &ctx, const Relocation &r,
case R_GOTPLTONLY_PC:
return ctx.in.gotPlt->getVA() + a - p;
case R_GOTREL:
+ case R_RELAX_GOT_OFF:
return r.sym->getVA(ctx, a) - ctx.in.got->getVA();
case R_GOTPLTREL:
return r.sym->getVA(ctx, a) - ctx.in.gotPlt->getVA();
diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 3ff302042eb5f..c0c11b0b81fb4 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -145,7 +145,7 @@ bool lld::elf::needsGot(RelExpr expr) {
static bool isRelExpr(RelExpr expr) {
return oneof<R_PC, R_GOTREL, R_GOTPLTREL, RE_ARM_PCA, RE_MIPS_GOTREL,
RE_PPC64_CALL, RE_AARCH64_PAGE_PC, R_RELAX_GOT_PC,
- RE_RISCV_PC_INDIRECT, RE_LOONGARCH_PAGE_PC,
+ R_RELAX_GOT_OFF, RE_RISCV_PC_INDIRECT, RE_LOONGARCH_PAGE_PC,
RE_LOONGARCH_PC_INDIRECT>(expr);
}
@@ -922,14 +922,19 @@ void RelocScan::process(RelExpr expr, RelType type, uint64_t offset,
// indirection.
const bool isIfunc = sym.isGnuIFunc();
if (!sym.isPreemptible && !isIfunc) {
- if (expr != R_GOT_PC) {
+ if (expr != R_GOT_PC && expr != R_GOT_OFF) {
expr = fromPlt(expr);
} else if (!isAbsoluteOrTls(sym)) {
- expr = ctx.target->adjustGotPcExpr(type, addend,
- sec->content().data() + offset);
- // If the target adjusted the expression to R_RELAX_GOT_PC, we may end up
+ if (expr == R_GOT_PC)
+ expr = ctx.target->adjustGotPcExpr(type, addend,
+ sec->content().data() + offset);
+ else
+ expr = ctx.target->adjustGotOffExpr(type, sym, addend,
+ sec->content().data() + offset);
+
+ // If the target adjusted the expression to R_RELAX_GOT_*, we may end up
// needing the GOT if we can't relax everything.
- if (expr == R_RELAX_GOT_PC)
+ if (expr == R_RELAX_GOT_PC || expr == R_RELAX_GOT_OFF)
ctx.in.got->hasGotOffRel.store(true, std::memory_order_relaxed);
}
}
diff --git a/lld/ELF/Relocations.h b/lld/ELF/Relocations.h
index abc62bf01a421..210ef5efb4a07 100644
--- a/lld/ELF/Relocations.h
+++ b/lld/ELF/Relocations.h
@@ -61,6 +61,7 @@ enum RelExpr {
R_PLT_GOTPLT,
R_PLT_GOTREL,
R_RELAX_HINT,
+ R_RELAX_GOT_OFF,
R_RELAX_GOT_PC,
R_RELAX_GOT_PC_NOPIC,
R_RELAX_TLS_GD_TO_IE,
diff --git a/lld/ELF/Target.cpp b/lld/ELF/Target.cpp
index 89e4dbeed3109..6ad4904d2546a 100644
--- a/lld/ELF/Target.cpp
+++ b/lld/ELF/Target.cpp
@@ -149,6 +149,12 @@ RelExpr TargetInfo::adjustGotPcExpr(RelType type, int64_t addend,
return R_GOT_PC;
}
+RelExpr TargetInfo::adjustGotOffExpr(RelType type, const Symbol &sym,
+ int64_t addend,
+ const uint8_t *data) const {
+ return R_GOT_OFF;
+}
+
static void relocateImpl(const TargetInfo &target, InputSectionBase &sec,
uint64_t secAddr, uint8_t *buf) {
auto &ctx = target.ctx;
diff --git a/lld/ELF/Target.h b/lld/ELF/Target.h
index 9cf91485fa700..73ab71abbdca3 100644
--- a/lld/ELF/Target.h
+++ b/lld/ELF/Target.h
@@ -198,6 +198,8 @@ class TargetInfo {
virtual RelExpr adjustTlsExpr(RelType type, RelExpr expr) const;
virtual RelExpr adjustGotPcExpr(RelType type, int64_t addend,
const uint8_t *loc) const;
+ virtual RelExpr adjustGotOffExpr(RelType type, const Symbol &sym,
+ int64_t addend, const uint8_t *loc) const;
protected:
// On FreeBSD x86_64 the first page cannot be mmaped.
diff --git a/lld/test/ELF/sparcv9-gotdata-relax.s b/lld/test/ELF/sparcv9-gotdata-relax.s
new file mode 100644
index 0000000000000..b94ea94528c21
--- /dev/null
+++ b/lld/test/ELF/sparcv9-gotdata-relax.s
@@ -0,0 +1,25 @@
+# REQUIRES: sparc
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s -o %t.o
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-objdump -d --no-show-raw-insn %t.so | FileCheck %s
+# RUN: llvm-readelf -r %t.so | FileCheck --check-prefix=REL %s
+
+# CHECK-LABEL: <local_ref>:
+# CHECK: add %l7, %l1, %l2
+
+# REL-NOT: local
+
+.text
+.globl local_ref
+.type local_ref, at function
+local_ref:
+ sethi %gdop_hix22(local), %l1
+ or %l1, %gdop_lox10(local), %l1
+ ldx [%l7 + %l1], %l2, %gdop(local)
+ retl
+ nop
+
+.data
+.hidden local
+local:
+ .xword 0
More information about the llvm-commits
mailing list