diff --git a/lld/MachO/Config.h b/lld/MachO/Config.h --- a/lld/MachO/Config.h +++ b/lld/MachO/Config.h @@ -128,6 +128,7 @@ bool deadStripDylibs = false; bool demangle = false; bool deadStrip = false; + bool keepDwarfUnwind = false; PlatformInfo platformInfo; NamespaceKind namespaceKind = NamespaceKind::twolevel; UndefinedSymbolTreatment undefinedSymbolTreatment = diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -1185,6 +1185,9 @@ (config->arch() == AK_arm64 || config->arch() == AK_arm64e) && config->platform() == PlatformKind::macOS); + config->keepDwarfUnwind = + args.hasFlag(OPT_keep_dwarf_unwind, OPT_no_keep_dwarf_unwind, false); + if (args.hasArg(OPT_v)) { message(getLLDVersion()); message(StringRef("Library search paths:") + diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp --- a/lld/MachO/InputFiles.cpp +++ b/lld/MachO/InputFiles.cpp @@ -265,6 +265,9 @@ auto *buf = reinterpret_cast(mb.getBufferStart()); for (const Section &sec : sections) { + if (!config->keepDwarfUnwind && + strcmp(sec.sectname, section_names::ehFrame) == 0) + continue; if (config->dedupLiterals && sectionType(sec.flags) == S_CSTRING_LITERALS) { if (sec.nreloc) fatal(toString(this) + " contains relocations in " + sec.segname + "," + diff --git a/lld/MachO/Options.td b/lld/MachO/Options.td --- a/lld/MachO/Options.td +++ b/lld/MachO/Options.td @@ -1245,12 +1245,10 @@ Flags<[HelpHidden]>, Group; def keep_dwarf_unwind : Flag<["-"], "keep_dwarf_unwind">, - HelpText<"This option is undocumented in ld64">, - Flags<[HelpHidden]>, + HelpText<"Retain the __TEXT,__eh_frame section">, Group; def no_keep_dwarf_unwind : Flag<["-"], "no_keep_dwarf_unwind">, - HelpText<"This option is undocumented in ld64">, - Flags<[HelpHidden]>, + HelpText<"Omit the __TEXT,__eh_frame section (default)">, Group; def kext : Flag<["-"], "kext">, HelpText<"This option is undocumented in ld64">, diff --git a/lld/test/MachO/eh_frame.s b/lld/test/MachO/eh_frame.s new file mode 100644 --- /dev/null +++ b/lld/test/MachO/eh_frame.s @@ -0,0 +1,28 @@ +# REQUIRES: x86 +# RUN: rm -rf %t; mkdir -p %t + +## Verify that __TEXT,__eh_frame is either retained or omitted from the +## linker output, according to command-line options -(no_)keep_dwarf_unwind + +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/main.o +# RUN: %lld -o %t/keep %t/main.o -lSystem -keep_dwarf_unwind +# RUN: %lld -o %t/toss %t/main.o -lSystem -no_keep_dwarf_unwind +# RUN: %lld -o %t/default %t/main.o -lSystem + +# RUN: llvm-objdump -h %t/keep | FileCheck %s --check-prefixes=KEEP +# RUN: llvm-objdump -h %t/toss | FileCheck %s --check-prefixes=TOSS +# RUN: llvm-objdump -h %t/default | FileCheck %s --check-prefixes=TOSS + +# KEEP-LABEL: {{^}}Sections: +# KEEP: [[#]] __eh_frame {{.*}} DATA + +# TOSS-LABEL: {{^}}Sections: +# TOSS-NOT: [[#]] __eh_frame {{.*}} DATA + +.text +.global _main +_main: + ret + +.section __TEXT,__eh_frame +.fill 8, 8