[clang] [lldb] [lldb] Support relocatable Mach-O DWARF sections (PR #239801)
Adrian Prantl via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 9 15:49:18 PST 2026
https://github.com/adrian-prantl updated https://github.com/llvm/llvm-project/pull/239801
>From 6cbc57a0adb2b6d5915b528fadf0570421cd6757 Mon Sep 17 00:00:00 2001
From: Adrian Prantl <aprantl at apple.com>
Date: Fri, 6 Nov 2026 10:00:20 -0800
Subject: [PATCH 1/2] [clang] Relocate DWARF across sections on Mach-O firmware
targets
Change the Darwin Toolchain to pass -grelocatable-dwarf for embedded
Mach-O targets when emitting DWARF 5 or later.
Assisted-by: Claude
---
clang/include/clang/Basic/CodeGenOptions.def | 3 ++
clang/include/clang/Driver/CommonArgs.h | 4 ++
clang/include/clang/Options/Options.td | 4 ++
clang/lib/CodeGen/BackendUtil.cpp | 1 +
clang/lib/Driver/ToolChains/Clang.cpp | 4 ++
clang/lib/Driver/ToolChains/CommonArgs.cpp | 7 ++++
clang/lib/Driver/ToolChains/Darwin.cpp | 6 +++
.../DebugInfo/AArch64/relocatable-dwarf.c | 24 ++++++++++++
clang/test/Driver/relocatable-dwarf.c | 39 +++++++++++++++++++
clang/test/Misc/cc1as-relocatable-dwarf.s | 25 ++++++++++++
clang/tools/driver/cc1as_main.cpp | 5 +++
11 files changed, 122 insertions(+)
create mode 100644 clang/test/DebugInfo/AArch64/relocatable-dwarf.c
create mode 100644 clang/test/Driver/relocatable-dwarf.c
create mode 100644 clang/test/Misc/cc1as-relocatable-dwarf.s
diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def
index ff33f5be424ecd..8c7974ae1664a1 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -129,6 +129,9 @@ CODEGENOPT(EmitCompactUnwindNonCanonical, 1, 0, Benign)
CODEGENOPT(EmitSFrameUnwind, 1, 0, Benign) ///< Set when -sframe is enabled.
+/// Set when -grelocatable-dwarf is enabled.
+CODEGENOPT(MachORelocatableDwarf, 1, 0, Benign)
+
///< Set when -fxray-always-emit-customevents is enabled.
CODEGENOPT(XRayAlwaysEmitCustomEvents , 1, 0, Benign)
diff --git a/clang/include/clang/Driver/CommonArgs.h b/clang/include/clang/Driver/CommonArgs.h
index 138e18f8641b90..f97776e622d0bf 100644
--- a/clang/include/clang/Driver/CommonArgs.h
+++ b/clang/include/clang/Driver/CommonArgs.h
@@ -54,6 +54,10 @@ void renderDebugInfoCompressionArgs(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs,
const Driver &D, const ToolChain &TC);
+/// Whether Mach-O objects for \p T with DWARF \p DwarfVersion relocate offsets
+/// between DWARF sections.
+bool useMachORelocatableDwarf(const llvm::Triple &T, unsigned DwarfVersion);
+
void claimNoWarnArgs(const llvm::opt::ArgList &Args);
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args,
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 81f26bb4791416..616630e3d44d78 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -8177,6 +8177,10 @@ def reloc_section_sym : Joined<["--"], "reloc-section-sym=">,
NormalizedValuesScope<"llvm::RelocSectionSymType">,
NormalizedValues<["All", "Internal", "None"]>,
MarshallingInfoEnum<CodeGenOpts<"RelocSectionSym">, "All">;
+def grelocatable_dwarf : Flag<["-"], "grelocatable-dwarf">,
+ HelpText<"Relocate offsets from one DWARF section into another in Mach-O "
+ "object files">,
+ MarshallingInfoFlag<CodeGenOpts<"MachORelocatableDwarf">>;
// The leading 'g' is misleading. This is an unwind tables option, not
// a debug option. But uses this name for gnu compatibility.
def gsframe : Flag<["--"], "gsframe">,
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index f1f03716bdff58..0118cb115af605 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -486,6 +486,7 @@ static bool initTargetOptions(const CompilerInstance &CI,
Options.MCOptions.Crel = CodeGenOpts.Crel;
Options.MCOptions.RelocSectionSym = CodeGenOpts.getRelocSectionSym();
Options.MCOptions.ImplicitMapSyms = CodeGenOpts.ImplicitMapSyms;
+ Options.MCOptions.MachORelocatableDwarf = CodeGenOpts.MachORelocatableDwarf;
Options.MCOptions.X86RelaxRelocations = CodeGenOpts.X86RelaxRelocations;
Options.MCOptions.CompressDebugSections =
CodeGenOpts.getCompressDebugSections();
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index a33b116eda7105..1bd31527c5f0f6 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -5125,6 +5125,8 @@ renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T,
CmdArgs.push_back("-dwarf-explicit-import");
renderDwarfFormat(D, T, Args, CmdArgs, EffectiveDWARFVersion);
+ if (useMachORelocatableDwarf(T, EffectiveDWARFVersion))
+ CmdArgs.push_back("-grelocatable-dwarf");
renderDebugInfoCompressionArgs(Args, CmdArgs, D, TC);
// This controls whether or not we perform JustMyCode instrumentation.
@@ -9457,6 +9459,8 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DwarfVersion,
llvm::DebuggerKind::Default);
renderDwarfFormat(D, Triple, Args, CmdArgs, DwarfVersion);
+ if (useMachORelocatableDwarf(Triple, DwarfVersion))
+ CmdArgs.push_back("-grelocatable-dwarf");
renderDebugInfoCompressionArgs(Args, CmdArgs, D, getToolChain());
// Handle -fPIC et al -- the relocation-model affects the assembler
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index db5250db329a9f..a53059230961e1 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -680,6 +680,13 @@ void tools::addLinkerCompressDebugSectionsOption(
}
}
+bool tools::useMachORelocatableDwarf(const llvm::Triple &T,
+ unsigned DwarfVersion) {
+ return T.isOSBinFormatMachO() && DwarfVersion >= 5 &&
+ !(T.isMacOSX() || T.isiOS() || T.isWatchOS() || T.isXROS() ||
+ T.isDriverKit());
+}
+
void tools::renderDebugInfoCompressionArgs(const ArgList &Args,
ArgStringList &CmdArgs,
const Driver &D,
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index baefa8c8c52015..69ff4188dbaf9c 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -456,6 +456,12 @@ void darwin::Linker::AddLinkArgs(Compilation &C, const ArgList &Args,
CmdArgs.push_back("-disable-atexit-based-global-dtor-lowering");
}
+ if (useMachORelocatableDwarf(getToolChain().getTriple(),
+ getDwarfVersion(getToolChain(), Args))) {
+ CmdArgs.push_back("-mllvm");
+ CmdArgs.push_back("-macho-relocatable-dwarf");
+ }
+
Args.AddLastArg(CmdArgs, options::OPT_prebind);
Args.AddLastArg(CmdArgs, options::OPT_noprebind);
Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
diff --git a/clang/test/DebugInfo/AArch64/relocatable-dwarf.c b/clang/test/DebugInfo/AArch64/relocatable-dwarf.c
new file mode 100644
index 00000000000000..7e61494075ef40
--- /dev/null
+++ b/clang/test/DebugInfo/AArch64/relocatable-dwarf.c
@@ -0,0 +1,24 @@
+// -grelocatable-dwarf relocates offsets between DWARF sections in Mach-O
+// object files.
+
+// REQUIRES: aarch64-registered-target
+
+// RUN: %clang_cc1 -triple arm64-apple-macos15 -debug-info-kind=limited \
+// RUN: -dwarf-version=5 -emit-obj %s -o %t.o
+// RUN: llvm-objdump -r --section=__debug_info %t.o \
+// RUN: | FileCheck %s --check-prefix=PLAIN
+// RUN: %clang_cc1 -triple arm64-apple-macos15 -debug-info-kind=limited \
+// RUN: -dwarf-version=5 -grelocatable-dwarf -emit-obj %s -o %t.reloc.o
+// RUN: llvm-objdump -r --section=__debug_info %t.reloc.o \
+// RUN: | FileCheck %s --check-prefix=RELOC
+
+// PLAIN: file format mach-o arm64
+// PLAIN-NOT: __debug_
+
+// RELOC: RELOCATION RECORDS FOR [__debug_info]:
+// RELOC-DAG: ARM64_RELOC_UNSIGNED __debug_abbrev
+// RELOC-DAG: ARM64_RELOC_UNSIGNED __debug_line
+// RELOC-DAG: ARM64_RELOC_UNSIGNED __debug_str_offs
+// RELOC-DAG: ARM64_RELOC_UNSIGNED __debug_addr
+
+int g;
diff --git a/clang/test/Driver/relocatable-dwarf.c b/clang/test/Driver/relocatable-dwarf.c
new file mode 100644
index 00000000000000..d89fa4f80417dd
--- /dev/null
+++ b/clang/test/Driver/relocatable-dwarf.c
@@ -0,0 +1,39 @@
+// 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.
+
+// RUN: %clang -### --target=arm64-apple-none-macho -g -c %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=CC1
+// RUN: %clang -### --target=arm64-apple-firmware -g -c %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=CC1
+// RUN: %clang -### --target=arm64-apple-bridgeos9.0 -g -c %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=CC1
+// RUN: %clang -### --target=arm64-apple-none-macho -g -c -x assembler %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=CC1AS
+// RUN: %clang -### --target=arm64-apple-none-macho -flto %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=LINK
+
+// RUN: %clang -### --target=arm64-apple-none-macho -g -gdwarf-4 -flto %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=NONE
+// RUN: %clang -### --target=arm64-apple-none-macho -gdwarf-4 -c -x assembler %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=NONE
+// RUN: %clang -### --target=arm64-apple-macos15 -g -flto %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=NONE
+// RUN: %clang -### --target=arm64-apple-ios17 -g -flto %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=NONE
+// RUN: %clang -### --target=arm64-apple-tvos17 -g -flto %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=NONE
+// RUN: %clang -### --target=arm64-apple-watchos10 -g -flto %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=NONE
+// RUN: %clang -### --target=arm64-apple-xros1 -g -flto %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=NONE
+// RUN: %clang -### --target=arm64-apple-driverkit23 -g -flto %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=NONE
+// RUN: %clang -### --target=x86_64-linux-gnu -g -c %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=NONE
+
+// CC1: "-cc1" {{.*}}"-grelocatable-dwarf"
+// CC1AS: "-cc1as" {{.*}}"-grelocatable-dwarf"
+// LINK: {{/|\\\\}}ld{{(.exe)?}}" {{.*}}"-mllvm" "-macho-relocatable-dwarf"
+// NONE-NOT: "-grelocatable-dwarf"
+// NONE-NOT: "-macho-relocatable-dwarf"
diff --git a/clang/test/Misc/cc1as-relocatable-dwarf.s b/clang/test/Misc/cc1as-relocatable-dwarf.s
new file mode 100644
index 00000000000000..a2fcd35df60edb
--- /dev/null
+++ b/clang/test/Misc/cc1as-relocatable-dwarf.s
@@ -0,0 +1,25 @@
+// REQUIRES: aarch64-registered-target
+// RUN: %clang -cc1as -triple arm64-apple-macos14 -filetype obj %s -o %t.o
+// RUN: llvm-objdump -s --section=__debug_info %t.o \
+// RUN: | FileCheck %s --check-prefix=PLAIN
+// RUN: %clang -cc1as -triple arm64-apple-macos14 -filetype obj \
+// RUN: -grelocatable-dwarf %s -o %t.reloc.o
+// RUN: llvm-objdump -s --section=__debug_info %t.reloc.o \
+// RUN: | FileCheck %s --check-prefix=RELOC
+
+// The field holds the address of "second" by default, and its offset in
+// __debug_str with -grelocatable-dwarf.
+// PLAIN: 0011 0a000000
+// RELOC: 0000 06000000
+
+ .text
+_foo:
+ ret
+
+ .section __DWARF,__debug_str,regular,debug
+ .asciz "first"
+Lsecond:
+ .asciz "second"
+
+ .section __DWARF,__debug_info,regular,debug
+ .long Lsecond
diff --git a/clang/tools/driver/cc1as_main.cpp b/clang/tools/driver/cc1as_main.cpp
index 0d7bd90b0354ef..ded9e0d906df01 100644
--- a/clang/tools/driver/cc1as_main.cpp
+++ b/clang/tools/driver/cc1as_main.cpp
@@ -172,6 +172,8 @@ struct AssemblerInvocation {
unsigned Crel : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned ImplicitMapsyms : 1;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned MachORelocatableDwarf : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned X86RelaxRelocations : 1;
@@ -225,6 +227,7 @@ struct AssemblerInvocation {
EmitCompactUnwindNonCanonical = false;
Crel = false;
ImplicitMapsyms = 0;
+ MachORelocatableDwarf = 0;
X86RelaxRelocations = 0;
X86Sse2Avx = 0;
}
@@ -398,6 +401,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
Args.hasArg(OPT_femit_compact_unwind_non_canonical);
Opts.EmitSFrameUnwind = Args.hasArg(OPT_gsframe);
Opts.Crel = Args.hasArg(OPT_crel);
+ Opts.MachORelocatableDwarf = Args.hasArg(OPT_grelocatable_dwarf);
Opts.RelocSectionSym = RelocSectionSymType::All;
if (auto *A = Args.getLastArg(OPT_reloc_section_sym))
Opts.RelocSectionSym = StringSwitch<RelocSectionSymType>(A->getValue())
@@ -475,6 +479,7 @@ executeAssemblerImpl(const cl::LibraryOptionsContext &LibraryOptions,
MCOptions.Crel = Opts.Crel;
MCOptions.RelocSectionSym = Opts.RelocSectionSym;
MCOptions.ImplicitMapSyms = Opts.ImplicitMapsyms;
+ MCOptions.MachORelocatableDwarf = Opts.MachORelocatableDwarf;
MCOptions.X86RelaxRelocations = Opts.X86RelaxRelocations;
MCOptions.X86Sse2Avx = Opts.X86Sse2Avx;
MCOptions.MCNoExecStack = Opts.NoExecStack;
>From 66c3ec12d65707af3d3b135621e88efff2389ed9 Mon Sep 17 00:00:00 2001
From: Adrian Prantl <aprantl at apple.com>
Date: Fri, 6 Nov 2026 10:37:10 -0800
Subject: [PATCH 2/2] [lldb] Support relocatable Mach-O DWARF sections
Symtab::InitAddressIndexes gives symbols without a size the distance
to the next symbol, capped at the end of the section that contains the
symbol's address.
With relocatable DWARF, every section in the __DWARF segment of a
Mach-O object starts at address 0, just like __text. That means the
lookup by address can incorrectly return a DWARF section instead of
__text. The last symbol in __text then gets a size that extends past
the end of __text.
Fix this by using the section the symbol is defined in, and fall back
to the lookup by address for symbols without one.
Assisted-by: Claude
rdar://192083461
---
lldb/source/Symbol/Symtab.cpp | 4 ++-
.../MachO/symbol-size-shared-address.s | 26 +++++++++++++++++++
2 files changed, 29 insertions(+), 1 deletion(-)
create mode 100644 lldb/test/Shell/ObjectFile/MachO/symbol-size-shared-address.s
diff --git a/lldb/source/Symbol/Symtab.cpp b/lldb/source/Symbol/Symtab.cpp
index ce164a38b9c58b..91e0dc3d9ae0cc 100644
--- a/lldb/source/Symbol/Symtab.cpp
+++ b/lldb/source/Symbol/Symtab.cpp
@@ -889,8 +889,10 @@ static void AddSectionsToRangeMap(SectionList *sectlist,
if (child_sectlist.GetNumSections(0) > 0) {
AddSectionsToRangeMap(&child_sectlist, section_ranges);
} else {
+ // Debug info sections can share addresses with the sections that
+ // symbols are defined in.
size_t size = sect_sp->GetByteSize();
- if (size > 0) {
+ if (size > 0 && !sect_sp->ContainsOnlyDebugInfo()) {
addr_t base_addr = sect_sp->GetFileAddress();
RangeVector<addr_t, addr_t>::Entry entry;
entry.SetRangeBase(base_addr);
diff --git a/lldb/test/Shell/ObjectFile/MachO/symbol-size-shared-address.s b/lldb/test/Shell/ObjectFile/MachO/symbol-size-shared-address.s
new file mode 100644
index 00000000000000..13db52a394e3ff
--- /dev/null
+++ b/lldb/test/Shell/ObjectFile/MachO/symbol-size-shared-address.s
@@ -0,0 +1,26 @@
+# REQUIRES: x86
+
+# With relocatable DWARF, every __DWARF section of an object file starts at
+# address 0, like __text. A symbol without a size is still bounded by the end
+# of the section it is defined in.
+
+# RUN: llvm-mc -triple x86_64-apple-macosx -filetype=obj \
+# RUN: -macho-relocatable-dwarf %s -o %t.o
+# RUN: lldb-test symtab --find-symbols-by-regex='.*' %t.o | FileCheck %s
+
+# CHECK-DAG: Code 0x0000000000000000 0x0000000000000003 {{.*}} foo
+# CHECK-DAG: Code 0x0000000000000003 0x0000000000000002 {{.*}} bar
+
+ .text
+ .globl _foo
+_foo:
+ nop
+ nop
+ ret
+ .globl _bar
+_bar:
+ nop
+ ret
+
+ .section __DWARF,__debug_str,regular,debug
+ .space 0x100
More information about the cfe-commits
mailing list