diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -489,6 +489,7 @@ case OPT_Z: case OPT_arch: case OPT_syslibroot: + case OPT_sectcreate: // handled elsewhere break; default: @@ -518,6 +519,15 @@ createSyntheticSections(); symtab->addDSOHandle(in.header); + for (opt::Arg *arg : args.filtered(OPT_sectcreate)) { + StringRef segName = arg->getValue(0); + StringRef sectName = arg->getValue(1); + StringRef fileName = arg->getValue(2); + Optional buffer = readFile(fileName); + if (buffer) + inputFiles.push_back(make(*buffer, segName, sectName)); + } + // Initialize InputSections. for (InputFile *file : inputFiles) { for (SubsectionMap &map : file->subsections) { diff --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h --- a/lld/MachO/InputFiles.h +++ b/lld/MachO/InputFiles.h @@ -40,6 +40,7 @@ ObjKind, DylibKind, ArchiveKind, + OpaqueKind, }; virtual ~InputFile() = default; @@ -72,6 +73,14 @@ static bool classof(const InputFile *f) { return f->kind() == ObjKind; } }; +// command-line -sectcreate file +class OpaqueFile : public InputFile { +public: + explicit OpaqueFile(MemoryBufferRef mb, StringRef segName, + StringRef sectName); + static bool classof(const InputFile *f) { return f->kind() == OpaqueKind; } +}; + // .dylib file class DylibFile : public InputFile { public: diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp --- a/lld/MachO/InputFiles.cpp +++ b/lld/MachO/InputFiles.cpp @@ -301,6 +301,18 @@ } } +OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName, + StringRef sectName) + : InputFile(OpaqueKind, mb) { + InputSection *isec = make(); + isec->file = this; + isec->name = sectName.take_front(16); + isec->segname = segName.take_front(16); + auto *buf = reinterpret_cast(mb.getBufferStart()); + isec->data = {buf, mb.getBufferSize()}; + inputSections.push_back(isec); +} + ObjFile::ObjFile(MemoryBufferRef mb) : InputFile(ObjKind, mb) { auto *buf = reinterpret_cast(mb.getBufferStart()); auto *hdr = reinterpret_cast(mb.getBufferStart()); diff --git a/lld/MachO/Options.td b/lld/MachO/Options.td --- a/lld/MachO/Options.td +++ b/lld/MachO/Options.td @@ -156,7 +156,6 @@ def sectcreate : MultiArg<["-"], "sectcreate", 3>, MetaVarName<"
">, HelpText<"Create
in from the contents of ">, - Flags<[HelpHidden]>, Group; def segcreate : MultiArg<["-"], "segcreate", 3>, MetaVarName<"
">, diff --git a/lld/test/MachO/sectcreate.s b/lld/test/MachO/sectcreate.s new file mode 100644 --- /dev/null +++ b/lld/test/MachO/sectcreate.s @@ -0,0 +1,15 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o +# RUN: echo "Contents of -sectcreate option" >%t2 +# RUN: lld -flavor darwinnew -Z -o %t %t.o -sectcreate FOO BARF %t2 +# RUN: llvm-objdump -s %t | FileCheck %s + +# CHECK: Contents of section BARF: +# CHECK: 100001000 436f6e74 656e7473 206f6620 2d736563 Contents of -sec +# CHECK: 100001010 74637265 61746520 6f707469 6f6e0a tcreate option. + +.text +.global _main +_main: + mov $0, %eax + ret