[lld] a379f2c - [lld-macho] Handle command-line option -sectcreate SEG SECT FILE
Jez Ng via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 10 18:47:44 PDT 2020
Author: Greg McGary
Date: 2020-08-10T18:47:13-07:00
New Revision: a379f2c251d0dbe3b191888fcd9c3d1eb42a980b
URL: https://github.com/llvm/llvm-project/commit/a379f2c251d0dbe3b191888fcd9c3d1eb42a980b
DIFF: https://github.com/llvm/llvm-project/commit/a379f2c251d0dbe3b191888fcd9c3d1eb42a980b.diff
LOG: [lld-macho] Handle command-line option -sectcreate SEG SECT FILE
Handle command-line option `-sectcreate SEG SECT FILE`, which inputs a binary blob from `FILE` into `SEG,SECT`
Reviewed By: int3
Differential Revision: https://reviews.llvm.org/D85501
Added:
lld/test/MachO/sectcreate.s
Modified:
lld/MachO/Driver.cpp
lld/MachO/InputFiles.cpp
lld/MachO/InputFiles.h
lld/MachO/Options.td
Removed:
################################################################################
diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index d332acd398a1..93b61fc823b2 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -495,6 +495,7 @@ bool macho::link(llvm::ArrayRef<const char *> argsArr, bool canExitEarly,
case OPT_Z:
case OPT_arch:
case OPT_syslibroot:
+ case OPT_sectcreate:
// handled elsewhere
break;
default:
@@ -524,6 +525,15 @@ bool macho::link(llvm::ArrayRef<const char *> argsArr, bool canExitEarly,
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<MemoryBufferRef> buffer = readFile(fileName);
+ if (buffer)
+ inputFiles.push_back(make<OpaqueFile>(*buffer, segName, sectName));
+ }
+
// Initialize InputSections.
for (InputFile *file : inputFiles) {
for (SubsectionMap &map : file->subsections) {
diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index f1afc187aca2..28e138a84522 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -301,6 +301,18 @@ void InputFile::parseSymbols(ArrayRef<structs::nlist_64> nList,
}
}
+OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName,
+ StringRef sectName)
+ : InputFile(OpaqueKind, mb) {
+ InputSection *isec = make<InputSection>();
+ isec->file = this;
+ isec->name = sectName.take_front(16);
+ isec->segname = segName.take_front(16);
+ const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
+ isec->data = {buf, mb.getBufferSize()};
+ subsections.push_back({{0, isec}});
+}
+
ObjFile::ObjFile(MemoryBufferRef mb) : InputFile(ObjKind, mb) {
auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
auto *hdr = reinterpret_cast<const mach_header_64 *>(mb.getBufferStart());
diff --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h
index bc5ad86ccaa9..2aa03014cd50 100644
--- a/lld/MachO/InputFiles.h
+++ b/lld/MachO/InputFiles.h
@@ -40,6 +40,7 @@ class InputFile {
ObjKind,
DylibKind,
ArchiveKind,
+ OpaqueKind,
};
virtual ~InputFile() = default;
@@ -72,6 +73,14 @@ class ObjFile : public InputFile {
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/Options.td b/lld/MachO/Options.td
index c27ca0d0195f..b606ff5d8821 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -156,7 +156,6 @@ def grp_content : OptionGroup<"content">, HelpText<"ADDITIONAL CONTENT">;
def sectcreate : MultiArg<["-"], "sectcreate", 3>,
MetaVarName<"<segment> <section> <file>">,
HelpText<"Create <section> in <segment> from the contents of <file>">,
- Flags<[HelpHidden]>,
Group<grp_content>;
def segcreate : MultiArg<["-"], "segcreate", 3>,
MetaVarName<"<segment> <section> <file>">,
diff --git a/lld/test/MachO/sectcreate.s b/lld/test/MachO/sectcreate.s
new file mode 100644
index 000000000000..ac561d88110b
--- /dev/null
+++ b/lld/test/MachO/sectcreate.s
@@ -0,0 +1,31 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
+# RUN: echo "-sectcreate 1.1" >%t1
+# RUN: echo "-sectcreate 1.2" >%t2
+# RUN: echo "-sectcreate 2" >%t3
+# RUN: lld -flavor darwinnew -Z \
+# RUN: -sectcreate SEG SEC1 %t1 \
+# RUN: -sectcreate SEG SEC2 %t3 \
+# RUN: -sectcreate SEG SEC1 %t2 \
+# RUN: -o %t %t.o
+# RUN: llvm-objdump -s %t | FileCheck %s
+
+# CHECK: Contents of section __text:
+# CHECK: Contents of section __data:
+# CHECK: my string!.
+# CHECK: Contents of section SEC1:
+# CHECK: -sectcreate 1.1.
+# CHECK: -sectcreate 1.2.
+# CHECK: Contents of section SEC2:
+# CHECK: -sectcreate 2.
+
+.text
+.global _main
+_main:
+ mov $0, %eax
+ ret
+
+.data
+.global my_string
+my_string:
+ .string "my string!"
More information about the llvm-commits
mailing list