[llvm-branch-commits] [lld] [lld][LoongArch] Handle DTPREL relocations in debug sections (PR #199327)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri May 22 23:12:29 PDT 2026


https://github.com/heiher created https://github.com/llvm/llvm-project/pull/199327

Teach the LoongArch lld backend to classify R_LARCH_TLS_DTPREL32 and R_LARCH_TLS_DTPREL64 as R_DTPREL.

This allows linker processing of TLS debug info references emitted into .debug_info via .dtprelword/.dtpreldword. Add 32-bit and 64-bit tests that assemble objects with DTPREL relocations in debug sections and verify the linked .debug_info contents.

>From 971da154361c839c850e41165e11b55774b4b067 Mon Sep 17 00:00:00 2001
From: WANG Rui <r at hev.cc>
Date: Sat, 23 May 2026 11:13:40 +0800
Subject: [PATCH] [lld][LoongArch] Handle DTPREL relocations in debug sections

Teach the LoongArch lld backend to classify R_LARCH_TLS_DTPREL32
and R_LARCH_TLS_DTPREL64 as R_DTPREL.

This allows linker processing of TLS debug info references emitted
into .debug_info via .dtprelword/.dtpreldword. Add 32-bit and 64-bit
tests that assemble objects with DTPREL relocations in debug sections
and verify the linked .debug_info contents.
---
 lld/ELF/Arch/LoongArch.cpp                 |  3 +++
 lld/test/ELF/loongarch-debug-info-tls-32.s | 31 ++++++++++++++++++++++
 lld/test/ELF/loongarch-debug-info-tls-64.s | 31 ++++++++++++++++++++++
 3 files changed, 65 insertions(+)
 create mode 100644 lld/test/ELF/loongarch-debug-info-tls-32.s
 create mode 100644 lld/test/ELF/loongarch-debug-info-tls-64.s

diff --git a/lld/ELF/Arch/LoongArch.cpp b/lld/ELF/Arch/LoongArch.cpp
index 7de21a56cf297..39cb50cf5dad2 100644
--- a/lld/ELF/Arch/LoongArch.cpp
+++ b/lld/ELF/Arch/LoongArch.cpp
@@ -454,6 +454,9 @@ RelExpr LoongArch::getRelExpr(const RelType type, const Symbol &s,
   case R_LARCH_PCREL20_S2:
   case R_LARCH_PCADD_HI20:
     return R_PC;
+  case R_LARCH_TLS_DTPREL32:
+  case R_LARCH_TLS_DTPREL64:
+    return R_DTPREL;
   default:
     Err(ctx) << getErrorLoc(ctx, loc) << "unknown relocation (" << type.v
              << ") against symbol " << &s;
diff --git a/lld/test/ELF/loongarch-debug-info-tls-32.s b/lld/test/ELF/loongarch-debug-info-tls-32.s
new file mode 100644
index 0000000000000..0d6a0aa32a929
--- /dev/null
+++ b/lld/test/ELF/loongarch-debug-info-tls-32.s
@@ -0,0 +1,31 @@
+# REQUIRES: loongarch
+# Check handling of TLS related relocations in a reduced .debug_info section.
+
+# RUN: llvm-mc -filetype=obj -triple=loongarch32-unknown-elf %s -o %t.o
+# RUN: ld.lld %t.o -o %t
+# RUN: llvm-objdump -s -t %t | FileCheck %s
+
+# CHECK:      Contents of section .debug_info:
+# CHECK-NEXT:  {{.*}} 08000000 05000104 00000000
+
+	.text
+	.globl	_Z8get_tvarv
+	.p2align	2
+	.type	_Z8get_tvarv, at function
+_Z8get_tvarv:
+	ret
+
+	.type	_ZZ8get_tvarvE4tvar, at object
+	.section	.tbss,"awT", at nobits
+	.p2align	2, 0x0
+_ZZ8get_tvarvE4tvar:
+	.word	0
+
+	.section	.debug_info,"", at progbits
+	.word	.Ldebug_info_end0-.Ldebug_info_start0 # Length of Unit
+.Ldebug_info_start0:
+	.half	5                               # DWARF version number
+	.byte	1                               # DWARF Unit Type
+	.byte	4                               # Address Size (in bytes)
+	.dtprelword	_ZZ8get_tvarvE4tvar
+.Ldebug_info_end0:
diff --git a/lld/test/ELF/loongarch-debug-info-tls-64.s b/lld/test/ELF/loongarch-debug-info-tls-64.s
new file mode 100644
index 0000000000000..6d00aca8aaf2e
--- /dev/null
+++ b/lld/test/ELF/loongarch-debug-info-tls-64.s
@@ -0,0 +1,31 @@
+# REQUIRES: loongarch
+# Check handling of TLS related relocations in a reduced .debug_info section.
+
+# RUN: llvm-mc -filetype=obj -triple=loongarch64-unknown-elf %s -o %t.o
+# RUN: ld.lld %t.o -o %t
+# RUN: llvm-objdump -s -t %t | FileCheck %s
+
+# CHECK:      Contents of section .debug_info:
+# CHECK-NEXT:  {{.*}} 0c000000 05000108 00000000 00000000
+
+	.text
+	.globl	_Z8get_tvarv
+	.p2align	2
+	.type	_Z8get_tvarv, at function
+_Z8get_tvarv:
+	ret
+
+	.type	_ZZ8get_tvarvE4tvar, at object
+	.section	.tbss,"awT", at nobits
+	.p2align	2, 0x0
+_ZZ8get_tvarvE4tvar:
+	.word	0
+
+	.section	.debug_info,"", at progbits
+	.word	.Ldebug_info_end0-.Ldebug_info_start0 # Length of Unit
+.Ldebug_info_start0:
+	.half	5                               # DWARF version number
+	.byte	1                               # DWARF Unit Type
+	.byte	8                               # Address Size (in bytes)
+	.dtpreldword	_ZZ8get_tvarvE4tvar
+.Ldebug_info_end0:



More information about the llvm-branch-commits mailing list