diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -1558,6 +1558,12 @@ inputFiles.insert(make(*buffer, segName, sectName)); } + for (const Arg *arg : args.filtered(OPT_add_empty_section)) { + StringRef segName = arg->getValue(0); + StringRef sectName = arg->getValue(1); + inputFiles.insert(make(MemoryBufferRef(), segName, sectName)); + } + gatherInputSections(); if (config->callGraphProfileSort) extractCallGraphProfile(); diff --git a/lld/MachO/Options.td b/lld/MachO/Options.td --- a/lld/MachO/Options.td +++ b/lld/MachO/Options.td @@ -252,6 +252,10 @@ Alias, HelpText<"Alias for -sectcreate">, Group; +def add_empty_section : MultiArg<["-"], "add_empty_section", 2>, + MetaVarName<"
">, + HelpText<"Create an empty
in ">, + Group; def filelist : Separate<["-"], "filelist">, MetaVarName<"">, HelpText<"Read names of files to link from ">, diff --git a/lld/test/MachO/sectcreate.s b/lld/test/MachO/sectcreate.s --- a/lld/test/MachO/sectcreate.s +++ b/lld/test/MachO/sectcreate.s @@ -20,6 +20,9 @@ # RUN: llvm-objdump -s %t | FileCheck --check-prefix=STRIPPED %s # RUN: llvm-readobj --sections %t | FileCheck --check-prefix=STRIPPEDSEC %s +# RUN: %lld -add_empty_section __foo __bar -o %t %t.o +# RUN: llvm-readobj --sections %t | FileCheck --check-prefix=EMPTYSECTION %s + # CHECK: Contents of section __TEXT,__text: # CHECK: Contents of section __DATA,__data: # CHECK: my string!. @@ -40,6 +43,11 @@ # STRIPPEDSEC-NOT: NoDeadStrip +# EMPTYSECTION: Name: __bar +# EMPTYSECTION: Segment: __foo +# EMPTYSECTION: Size: 0x0 +# EMPTYSECTION-NOT: Name: + .text .global _main _main: