https://github.com/adrian-prantl created 
https://github.com/llvm/llvm-project/pull/244069

On 32-bit ARM and x86, Mach-O uses scattered relocations, which identify their 
target by address. Relocatable DWARF puts every __DWARF section at address 0, 
so these relocations become ambiguous: a line table's reference to a file name 
in __debug_line_str resolves to the first function in __text instead.

Don't enable relocatable DWARF for these architectures.

Assisted-by: Claude

>From 16433c86bd3f107ac526c382a543f71fe3a205eb Mon Sep 17 00:00:00 2001
From: Adrian Prantl <[email protected]>
Date: Wed, 18 Nov 2026 16:25:53 -0800
Subject: [PATCH] [clang] Don't relocate DWARF on 32-bit ARM and x86 Mach-O

On 32-bit ARM and x86, Mach-O uses scattered relocations, which
identify their target by address. Relocatable DWARF puts every
__DWARF section at address 0, so these relocations become ambiguous:
a line table's reference to a file name in __debug_line_str resolves
to the first function in __text instead.

Don't enable relocatable DWARF for these architectures.

Assisted-by: Claude
---
 clang/lib/Driver/ToolChains/CommonArgs.cpp |  5 +++++
 clang/test/Driver/relocatable-dwarf.c      | 17 +++++++++++++++--
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index b7a263c18b1465..cec0ebc2564a82 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -682,6 +682,11 @@ void tools::addLinkerCompressDebugSectionsOption(
 
 bool tools::useMachORelocatableDwarf(const llvm::Triple &T,
                                      unsigned DwarfVersion) {
+  // 32-bit ARM and x86 Mach-O refer to a section plus an offset with a
+  // scattered relocation, which identifies its target by address. With every
+  // __DWARF section at address 0, that address is ambiguous.
+  if (T.isARM() || T.isThumb() || T.getArch() == llvm::Triple::x86)
+    return false;
   return T.isOSBinFormatMachO() && DwarfVersion >= 5 &&
          !(T.isMacOSX() || T.isiOS() || T.isWatchOS() || T.isXROS() ||
            T.isDriverKit());
diff --git a/clang/test/Driver/relocatable-dwarf.c 
b/clang/test/Driver/relocatable-dwarf.c
index c4830801ebf7a3..2ba1c8685a1dc8 100644
--- a/clang/test/Driver/relocatable-dwarf.c
+++ b/clang/test/Driver/relocatable-dwarf.c
@@ -1,6 +1,7 @@
 // Mach-O objects with DWARF 5 or later relocate offsets between DWARF 
sections,
-// except for the Apple operating systems below. This applies to code
-// generation, the integrated assembler, and LTO code generation in the linker.
+// except for the Apple operating systems and 32-bit ARM and x86 below. This
+// applies to code generation, the integrated assembler, and LTO code 
generation
+// in the linker.
 
 // RUN: %clang -### --target=arm64-apple-none-macho -g -c %s 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=CC1
@@ -31,6 +32,18 @@
 // RUN:   | FileCheck %s --check-prefix=NONE
 // RUN: %clang -### --target=x86_64-linux-gnu -g -c %s 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=NONE
+// RUN: %clang -### --target=armv7em-apple-none-macho -g -c %s 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=NONE
+// RUN: %clang -### --target=armv7em-apple-none-macho -g -c -x assembler %s 
2>&1 \
+// RUN:   | FileCheck %s --check-prefix=NONE
+// RUN: %clang -### --target=armv7em-apple-none-macho -g -flto %s 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=NONE
+// RUN: %clang -### --target=i386-apple-none-macho -g -c %s 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=NONE
+// RUN: %clang -### --target=i386-apple-none-macho -g -c -x assembler %s 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=NONE
+// RUN: %clang -### --target=i386-apple-none-macho -g -flto %s 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=NONE
 
 // CC1:   "-cc1" {{.*}}"-grelocatable-dwarf"
 // CC1AS: "-cc1as" {{.*}}"-grelocatable-dwarf"

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to