[llvm] [dsymutil] Add new argument allow_invalid_macho (PR #173503)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 5 21:24:40 PST 2026
https://github.com/Ch111p updated https://github.com/llvm/llvm-project/pull/173503
>From 9ca91172712ae36878a953452ce1469ecc6eefb5 Mon Sep 17 00:00:00 2001
From: ch1p <hduch1p at gmail.com>
Date: Thu, 25 Dec 2025 01:38:23 +0800
Subject: [PATCH 1/3] [dsymutil] Add new argument allow_invalid_macho
---
llvm/tools/dsymutil/DwarfLinkerForBinary.cpp | 2 +-
llvm/tools/dsymutil/LinkUtils.h | 4 ++++
llvm/tools/dsymutil/MachOUtils.cpp | 17 ++++++++++-------
llvm/tools/dsymutil/MachOUtils.h | 3 ++-
llvm/tools/dsymutil/Options.td | 6 ++++++
llvm/tools/dsymutil/dsymutil.cpp | 1 +
6 files changed, 24 insertions(+), 9 deletions(-)
diff --git a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
index 1fc5bba602d8b..68756a891d90d 100644
--- a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
+++ b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
@@ -883,7 +883,7 @@ bool DwarfLinkerForBinary::linkImpl(
ObjectType == Linker::OutputFileType::Object)
return MachOUtils::generateDsymCompanion(
Options.VFS, Map, *Streamer->getAsmPrinter().OutStreamer, OutFile,
- RelocationsToApply);
+ RelocationsToApply, Options.AllowInvalidMachO);
Streamer->finish();
return true;
diff --git a/llvm/tools/dsymutil/LinkUtils.h b/llvm/tools/dsymutil/LinkUtils.h
index c333a3d4afee0..d950f5f19474c 100644
--- a/llvm/tools/dsymutil/LinkUtils.h
+++ b/llvm/tools/dsymutil/LinkUtils.h
@@ -123,6 +123,10 @@ struct LinkOptions {
bool IncludeSwiftModulesFromInterface = false;
/// @}
+ /// Whether to allow generating Mach-O files that exceed certain format
+ /// limitations, such as sections with file offsets greater than 4GB.
+ bool AllowInvalidMachO = false;
+
LinkOptions() = default;
};
diff --git a/llvm/tools/dsymutil/MachOUtils.cpp b/llvm/tools/dsymutil/MachOUtils.cpp
index fba698896618b..622d1524dac98 100644
--- a/llvm/tools/dsymutil/MachOUtils.cpp
+++ b/llvm/tools/dsymutil/MachOUtils.cpp
@@ -322,9 +322,10 @@ static void transferSegmentAndSections(
}
// Write the __DWARF segment load command to the output file.
-static bool createDwarfSegment(const MCAssembler& Asm,uint64_t VMAddr, uint64_t FileOffset,
- uint64_t FileSize, unsigned NumSections,
- MachObjectWriter &Writer) {
+static bool createDwarfSegment(const MCAssembler &Asm, uint64_t VMAddr,
+ uint64_t FileOffset, uint64_t FileSize,
+ unsigned NumSections, MachObjectWriter &Writer,
+ bool AllowInvalidMachO) {
Writer.writeSegmentLoadCommand("__DWARF", NumSections, VMAddr,
alignTo(FileSize, 0x1000), FileOffset,
FileSize, /* MaxProt */ 7,
@@ -340,7 +341,7 @@ static bool createDwarfSegment(const MCAssembler& Asm,uint64_t VMAddr, uint64_t
VMAddr = alignTo(VMAddr, Alignment);
FileOffset = alignTo(FileOffset, Alignment);
}
- if (FileOffset > UINT32_MAX)
+ if (FileOffset > UINT32_MAX && !AllowInvalidMachO)
return error("section " + Sec->getName() +
"'s file offset exceeds 4GB."
" Refusing to produce an invalid Mach-O file.");
@@ -374,7 +375,8 @@ bool generateDsymCompanion(
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, const DebugMap &DM,
MCStreamer &MS, raw_fd_ostream &OutFile,
const std::vector<MachOUtils::DwarfRelocationApplicationInfo>
- &RelocationsToApply) {
+ &RelocationsToApply,
+ bool AllowInvalidMachO) {
auto &ObjectStreamer = static_cast<MCObjectStreamer &>(MS);
MCAssembler &MCAsm = ObjectStreamer.getAssembler();
auto &Writer = static_cast<MachObjectWriter &>(MCAsm.getWriter());
@@ -585,8 +587,9 @@ bool generateDsymCompanion(
}
// Write the load command for the __DWARF segment.
- if (!createDwarfSegment(MCAsm, DwarfVMAddr, DwarfSegmentStart, DwarfSegmentSize,
- NumDwarfSections, Writer))
+ if (!createDwarfSegment(MCAsm, DwarfVMAddr, DwarfSegmentStart,
+ DwarfSegmentSize, NumDwarfSections, Writer,
+ AllowInvalidMachO))
return false;
assert(OutFile.tell() == LoadCommandSize + HeaderSize);
diff --git a/llvm/tools/dsymutil/MachOUtils.h b/llvm/tools/dsymutil/MachOUtils.h
index 0229647b00f85..ecbb36feab891 100644
--- a/llvm/tools/dsymutil/MachOUtils.h
+++ b/llvm/tools/dsymutil/MachOUtils.h
@@ -59,7 +59,8 @@ bool generateDsymCompanion(
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, const DebugMap &DM,
MCStreamer &MS, raw_fd_ostream &OutFile,
const std::vector<MachOUtils::DwarfRelocationApplicationInfo>
- &RelocationsToApply);
+ &RelocationsToApply,
+ bool AllowInvalidMachO);
std::string getArchName(StringRef Arch);
} // namespace MachOUtils
diff --git a/llvm/tools/dsymutil/Options.td b/llvm/tools/dsymutil/Options.td
index 571f90c1e46f5..036094f5eeca5 100644
--- a/llvm/tools/dsymutil/Options.td
+++ b/llvm/tools/dsymutil/Options.td
@@ -108,6 +108,12 @@ def fat64: F<"fat64">,
def update: F<"update">,
HelpText<"Updates existing dSYM files to contain the latest accelerator tables and other DWARF optimizations.">,
Group<grp_general>;
+
+def allow_invalid_macho: F<"allow-invalid-macho">,
+ HelpText<"Allow generating Mach-O files that exceed certain format limitations, such as sections with file offsets greater than 4GB."
+ "Use with caution, as such files may not be compatible with all tools.">,
+ Group<grp_general>;
+
def: Flag<["-"], "u">,
Alias<update>,
HelpText<"Alias for --update">,
diff --git a/llvm/tools/dsymutil/dsymutil.cpp b/llvm/tools/dsymutil/dsymutil.cpp
index a71c57c60593c..bdf4f1317a3e8 100644
--- a/llvm/tools/dsymutil/dsymutil.cpp
+++ b/llvm/tools/dsymutil/dsymutil.cpp
@@ -315,6 +315,7 @@ static Expected<DsymutilOptions> getOptions(opt::InputArgList &Args) {
Options.LinkOpts.Fat64 = Args.hasArg(OPT_fat64);
Options.LinkOpts.KeepFunctionForStatic =
Args.hasArg(OPT_keep_func_for_static);
+ Options.LinkOpts.AllowInvalidMachO = Args.hasArg(OPT_allow_invalid_macho);
if (opt::Arg *ReproducerPath = Args.getLastArg(OPT_use_reproducer)) {
Options.ReproMode = ReproducerMode::Use;
>From 8e707c1d43226f43fca0849843248c5cb3669773 Mon Sep 17 00:00:00 2001
From: ch1p <hduch1p at gmail.com>
Date: Tue, 6 Jan 2026 02:36:19 +0800
Subject: [PATCH 2/3] [dsymutil] rename flag and variable && update help text
and comment
---
llvm/tools/dsymutil/DwarfLinkerForBinary.cpp | 2 +-
llvm/tools/dsymutil/LinkUtils.h | 6 +++---
llvm/tools/dsymutil/MachOUtils.cpp | 15 +++++++++++----
llvm/tools/dsymutil/MachOUtils.h | 2 +-
llvm/tools/dsymutil/Options.td | 7 ++++---
llvm/tools/dsymutil/dsymutil.cpp | 3 ++-
6 files changed, 22 insertions(+), 13 deletions(-)
diff --git a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
index 68756a891d90d..e3f7557087817 100644
--- a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
+++ b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
@@ -883,7 +883,7 @@ bool DwarfLinkerForBinary::linkImpl(
ObjectType == Linker::OutputFileType::Object)
return MachOUtils::generateDsymCompanion(
Options.VFS, Map, *Streamer->getAsmPrinter().OutStreamer, OutFile,
- RelocationsToApply, Options.AllowInvalidMachO);
+ RelocationsToApply, Options.AllowSliceSectionHeaderOffsetOverflow);
Streamer->finish();
return true;
diff --git a/llvm/tools/dsymutil/LinkUtils.h b/llvm/tools/dsymutil/LinkUtils.h
index d950f5f19474c..4baeee0caeb6d 100644
--- a/llvm/tools/dsymutil/LinkUtils.h
+++ b/llvm/tools/dsymutil/LinkUtils.h
@@ -123,9 +123,9 @@ struct LinkOptions {
bool IncludeSwiftModulesFromInterface = false;
/// @}
- /// Whether to allow generating Mach-O files that exceed certain format
- /// limitations, such as sections with file offsets greater than 4GB.
- bool AllowInvalidMachO = false;
+ /// Whether to allow emitting Mach-O where, within a single slice, section
+ /// header offsets (section.offset, 32-bit) exceed 4GB (non-standard).
+ bool AllowSliceSectionHeaderOffsetOverflow = false;
LinkOptions() = default;
};
diff --git a/llvm/tools/dsymutil/MachOUtils.cpp b/llvm/tools/dsymutil/MachOUtils.cpp
index 622d1524dac98..ee02da70f442c 100644
--- a/llvm/tools/dsymutil/MachOUtils.cpp
+++ b/llvm/tools/dsymutil/MachOUtils.cpp
@@ -325,7 +325,7 @@ static void transferSegmentAndSections(
static bool createDwarfSegment(const MCAssembler &Asm, uint64_t VMAddr,
uint64_t FileOffset, uint64_t FileSize,
unsigned NumSections, MachObjectWriter &Writer,
- bool AllowInvalidMachO) {
+ bool AllowSliceSectionHeaderOffsetOverflow) {
Writer.writeSegmentLoadCommand("__DWARF", NumSections, VMAddr,
alignTo(FileSize, 0x1000), FileOffset,
FileSize, /* MaxProt */ 7,
@@ -341,7 +341,14 @@ static bool createDwarfSegment(const MCAssembler &Asm, uint64_t VMAddr,
VMAddr = alignTo(VMAddr, Alignment);
FileOffset = alignTo(FileOffset, Alignment);
}
- if (FileOffset > UINT32_MAX && !AllowInvalidMachO)
+ // Mach-O section headers store the file offset in a 32-bit field
+ // (section.offset). For large dSYM files, a section can start beyond 4GB
+ // (UINT32_MAX), so the on-disk offset value may wrap/truncate. Within a
+ // single slice, sections are emitted in file order. If we allow emitting
+ // such non-standard Mach-O, compatible readers can reconstruct the true
+ // 64-bit offsets by walking sections in order and accumulating the sizes of
+ // preceding sections.
+ if (FileOffset > UINT32_MAX && !AllowSliceSectionHeaderOffsetOverflow)
return error("section " + Sec->getName() +
"'s file offset exceeds 4GB."
" Refusing to produce an invalid Mach-O file.");
@@ -376,7 +383,7 @@ bool generateDsymCompanion(
MCStreamer &MS, raw_fd_ostream &OutFile,
const std::vector<MachOUtils::DwarfRelocationApplicationInfo>
&RelocationsToApply,
- bool AllowInvalidMachO) {
+ bool AllowSliceSectionHeaderOffsetOverflow) {
auto &ObjectStreamer = static_cast<MCObjectStreamer &>(MS);
MCAssembler &MCAsm = ObjectStreamer.getAssembler();
auto &Writer = static_cast<MachObjectWriter &>(MCAsm.getWriter());
@@ -589,7 +596,7 @@ bool generateDsymCompanion(
// Write the load command for the __DWARF segment.
if (!createDwarfSegment(MCAsm, DwarfVMAddr, DwarfSegmentStart,
DwarfSegmentSize, NumDwarfSections, Writer,
- AllowInvalidMachO))
+ AllowSliceSectionHeaderOffsetOverflow))
return false;
assert(OutFile.tell() == LoadCommandSize + HeaderSize);
diff --git a/llvm/tools/dsymutil/MachOUtils.h b/llvm/tools/dsymutil/MachOUtils.h
index ecbb36feab891..6af702b88813d 100644
--- a/llvm/tools/dsymutil/MachOUtils.h
+++ b/llvm/tools/dsymutil/MachOUtils.h
@@ -60,7 +60,7 @@ bool generateDsymCompanion(
MCStreamer &MS, raw_fd_ostream &OutFile,
const std::vector<MachOUtils::DwarfRelocationApplicationInfo>
&RelocationsToApply,
- bool AllowInvalidMachO);
+ bool AllowSliceSectionHeaderOffsetOverflow);
std::string getArchName(StringRef Arch);
} // namespace MachOUtils
diff --git a/llvm/tools/dsymutil/Options.td b/llvm/tools/dsymutil/Options.td
index 036094f5eeca5..b751d66fd9145 100644
--- a/llvm/tools/dsymutil/Options.td
+++ b/llvm/tools/dsymutil/Options.td
@@ -109,9 +109,10 @@ def update: F<"update">,
HelpText<"Updates existing dSYM files to contain the latest accelerator tables and other DWARF optimizations.">,
Group<grp_general>;
-def allow_invalid_macho: F<"allow-invalid-macho">,
- HelpText<"Allow generating Mach-O files that exceed certain format limitations, such as sections with file offsets greater than 4GB."
- "Use with caution, as such files may not be compatible with all tools.">,
+def allow_slice_section_header_offset_overflow : F<"allow-slice-section-header-offset-overflow">,
+ HelpText<"Allow emitting Mach-O where, within a single slice, section header offsets (section.offset, 32-bit) exceed the 4GB limit (non-standard). "
+ "This option overrides dsymutil's per-slice 4GB check when writing section headers. "
+ "Use with caution: some tools may not be able to read the output.">,
Group<grp_general>;
def: Flag<["-"], "u">,
diff --git a/llvm/tools/dsymutil/dsymutil.cpp b/llvm/tools/dsymutil/dsymutil.cpp
index bdf4f1317a3e8..543ae3c095f45 100644
--- a/llvm/tools/dsymutil/dsymutil.cpp
+++ b/llvm/tools/dsymutil/dsymutil.cpp
@@ -315,7 +315,8 @@ static Expected<DsymutilOptions> getOptions(opt::InputArgList &Args) {
Options.LinkOpts.Fat64 = Args.hasArg(OPT_fat64);
Options.LinkOpts.KeepFunctionForStatic =
Args.hasArg(OPT_keep_func_for_static);
- Options.LinkOpts.AllowInvalidMachO = Args.hasArg(OPT_allow_invalid_macho);
+ Options.LinkOpts.AllowSliceSectionHeaderOffsetOverflow =
+ Args.hasArg(OPT_allow_slice_section_header_offset_overflow);
if (opt::Arg *ReproducerPath = Args.getLastArg(OPT_use_reproducer)) {
Options.ReproMode = ReproducerMode::Use;
>From 4602b7764d49ef46157bed3a53cb652f03874ed0 Mon Sep 17 00:00:00 2001
From: ch1p <hduch1p at gmail.com>
Date: Tue, 6 Jan 2026 13:24:22 +0800
Subject: [PATCH 3/3] [dsymutil] update flag && description
---
llvm/tools/dsymutil/DwarfLinkerForBinary.cpp | 2 +-
llvm/tools/dsymutil/LinkUtils.h | 2 +-
llvm/tools/dsymutil/MachOUtils.cpp | 8 ++++----
llvm/tools/dsymutil/MachOUtils.h | 2 +-
llvm/tools/dsymutil/Options.td | 8 ++++----
llvm/tools/dsymutil/dsymutil.cpp | 4 ++--
6 files changed, 13 insertions(+), 13 deletions(-)
diff --git a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
index e3f7557087817..7114e1924daa7 100644
--- a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
+++ b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
@@ -883,7 +883,7 @@ bool DwarfLinkerForBinary::linkImpl(
ObjectType == Linker::OutputFileType::Object)
return MachOUtils::generateDsymCompanion(
Options.VFS, Map, *Streamer->getAsmPrinter().OutStreamer, OutFile,
- RelocationsToApply, Options.AllowSliceSectionHeaderOffsetOverflow);
+ RelocationsToApply, Options.AllowSectionHeaderOffsetOverflow);
Streamer->finish();
return true;
diff --git a/llvm/tools/dsymutil/LinkUtils.h b/llvm/tools/dsymutil/LinkUtils.h
index 4baeee0caeb6d..27f671152a114 100644
--- a/llvm/tools/dsymutil/LinkUtils.h
+++ b/llvm/tools/dsymutil/LinkUtils.h
@@ -125,7 +125,7 @@ struct LinkOptions {
/// Whether to allow emitting Mach-O where, within a single slice, section
/// header offsets (section.offset, 32-bit) exceed 4GB (non-standard).
- bool AllowSliceSectionHeaderOffsetOverflow = false;
+ bool AllowSectionHeaderOffsetOverflow = false;
LinkOptions() = default;
};
diff --git a/llvm/tools/dsymutil/MachOUtils.cpp b/llvm/tools/dsymutil/MachOUtils.cpp
index ee02da70f442c..9b143157ac773 100644
--- a/llvm/tools/dsymutil/MachOUtils.cpp
+++ b/llvm/tools/dsymutil/MachOUtils.cpp
@@ -325,7 +325,7 @@ static void transferSegmentAndSections(
static bool createDwarfSegment(const MCAssembler &Asm, uint64_t VMAddr,
uint64_t FileOffset, uint64_t FileSize,
unsigned NumSections, MachObjectWriter &Writer,
- bool AllowSliceSectionHeaderOffsetOverflow) {
+ bool AllowSectionHeaderOffsetOverflow) {
Writer.writeSegmentLoadCommand("__DWARF", NumSections, VMAddr,
alignTo(FileSize, 0x1000), FileOffset,
FileSize, /* MaxProt */ 7,
@@ -348,7 +348,7 @@ static bool createDwarfSegment(const MCAssembler &Asm, uint64_t VMAddr,
// such non-standard Mach-O, compatible readers can reconstruct the true
// 64-bit offsets by walking sections in order and accumulating the sizes of
// preceding sections.
- if (FileOffset > UINT32_MAX && !AllowSliceSectionHeaderOffsetOverflow)
+ if (FileOffset > UINT32_MAX && !AllowSectionHeaderOffsetOverflow)
return error("section " + Sec->getName() +
"'s file offset exceeds 4GB."
" Refusing to produce an invalid Mach-O file.");
@@ -383,7 +383,7 @@ bool generateDsymCompanion(
MCStreamer &MS, raw_fd_ostream &OutFile,
const std::vector<MachOUtils::DwarfRelocationApplicationInfo>
&RelocationsToApply,
- bool AllowSliceSectionHeaderOffsetOverflow) {
+ bool AllowSectionHeaderOffsetOverflow) {
auto &ObjectStreamer = static_cast<MCObjectStreamer &>(MS);
MCAssembler &MCAsm = ObjectStreamer.getAssembler();
auto &Writer = static_cast<MachObjectWriter &>(MCAsm.getWriter());
@@ -596,7 +596,7 @@ bool generateDsymCompanion(
// Write the load command for the __DWARF segment.
if (!createDwarfSegment(MCAsm, DwarfVMAddr, DwarfSegmentStart,
DwarfSegmentSize, NumDwarfSections, Writer,
- AllowSliceSectionHeaderOffsetOverflow))
+ AllowSectionHeaderOffsetOverflow))
return false;
assert(OutFile.tell() == LoadCommandSize + HeaderSize);
diff --git a/llvm/tools/dsymutil/MachOUtils.h b/llvm/tools/dsymutil/MachOUtils.h
index 6af702b88813d..6b19b1c5b5d47 100644
--- a/llvm/tools/dsymutil/MachOUtils.h
+++ b/llvm/tools/dsymutil/MachOUtils.h
@@ -60,7 +60,7 @@ bool generateDsymCompanion(
MCStreamer &MS, raw_fd_ostream &OutFile,
const std::vector<MachOUtils::DwarfRelocationApplicationInfo>
&RelocationsToApply,
- bool AllowSliceSectionHeaderOffsetOverflow);
+ bool AllowSectionHeaderOffsetOverflow);
std::string getArchName(StringRef Arch);
} // namespace MachOUtils
diff --git a/llvm/tools/dsymutil/Options.td b/llvm/tools/dsymutil/Options.td
index b751d66fd9145..861fded554b32 100644
--- a/llvm/tools/dsymutil/Options.td
+++ b/llvm/tools/dsymutil/Options.td
@@ -109,10 +109,10 @@ def update: F<"update">,
HelpText<"Updates existing dSYM files to contain the latest accelerator tables and other DWARF optimizations.">,
Group<grp_general>;
-def allow_slice_section_header_offset_overflow : F<"allow-slice-section-header-offset-overflow">,
- HelpText<"Allow emitting Mach-O where, within a single slice, section header offsets (section.offset, 32-bit) exceed the 4GB limit (non-standard). "
- "This option overrides dsymutil's per-slice 4GB check when writing section headers. "
- "Use with caution: some tools may not be able to read the output.">,
+def allow_section_header_offset_overflow : F<"allow-section-header-offset-overflow">,
+ HelpText<"Allow emitting a section offset (32-bit) that, when applied, would exceed the Mach-O 4GB limit. "
+ "This leverages the fact that the offset can be computed as a 64-bit value if the sections are ordered (always the case for dsymutil). "
+ "Note that this generates an invalid Mach-O and requires explicit support by the tools that will need to consume it.">,
Group<grp_general>;
def: Flag<["-"], "u">,
diff --git a/llvm/tools/dsymutil/dsymutil.cpp b/llvm/tools/dsymutil/dsymutil.cpp
index 543ae3c095f45..ce55b7f6183e6 100644
--- a/llvm/tools/dsymutil/dsymutil.cpp
+++ b/llvm/tools/dsymutil/dsymutil.cpp
@@ -315,8 +315,8 @@ static Expected<DsymutilOptions> getOptions(opt::InputArgList &Args) {
Options.LinkOpts.Fat64 = Args.hasArg(OPT_fat64);
Options.LinkOpts.KeepFunctionForStatic =
Args.hasArg(OPT_keep_func_for_static);
- Options.LinkOpts.AllowSliceSectionHeaderOffsetOverflow =
- Args.hasArg(OPT_allow_slice_section_header_offset_overflow);
+ Options.LinkOpts.AllowSectionHeaderOffsetOverflow =
+ Args.hasArg(OPT_allow_section_header_offset_overflow);
if (opt::Arg *ReproducerPath = Args.getLastArg(OPT_use_reproducer)) {
Options.ReproMode = ReproducerMode::Use;
More information about the llvm-commits
mailing list