ELF2: LinkerScript: Implement INCLUDE directive.
llvm-svn: 249960
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index cf1b646..34fda2d 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -39,12 +39,14 @@
void readAsNeeded();
void readEntry();
void readGroup();
+ void readInclude();
void readOutput();
void readOutputFormat();
void readSearchDir();
std::vector<StringRef> Tokens;
size_t Pos = 0;
+ static std::vector<std::unique_ptr<MemoryBuffer>> OwningMBs;
};
}
@@ -55,6 +57,8 @@
readEntry();
} else if (Tok == "GROUP") {
readGroup();
+ } else if (Tok == "INCLUDE") {
+ readInclude();
} else if (Tok == "OUTPUT") {
readOutput();
} else if (Tok == "OUTPUT_FORMAT") {
@@ -160,6 +164,16 @@
}
}
+void LinkerScript::readInclude() {
+ StringRef Tok = next();
+ auto MBOrErr = MemoryBuffer::getFile(Tok);
+ error(MBOrErr, Twine("cannot open ") + Tok);
+ std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
+ std::vector<StringRef> V = tokenize(MB->getMemBufferRef().getBuffer());
+ Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
+ OwningMBs.push_back(std::move(MB)); // keep ownership of MB
+}
+
void LinkerScript::readOutput() {
// -o <file> takes predecence over OUTPUT(<file>).
expect("(");
@@ -182,6 +196,8 @@
expect(")");
}
+std::vector<std::unique_ptr<MemoryBuffer>> LinkerScript::OwningMBs;
+
// Entry point. The other functions or classes are private to this file.
void lld::elf2::readLinkerScript(MemoryBufferRef MB) {
LinkerScript(MB.getBuffer()).run();
diff --git a/lld/test/elf2/linkerscript.s b/lld/test/elf2/linkerscript.s
index d45143f..a280ed9 100644
--- a/lld/test/elf2/linkerscript.s
+++ b/lld/test/elf2/linkerscript.s
@@ -47,6 +47,11 @@
# RUN: ld.lld2 %t.script %t
# RUN: llvm-readobj %t.out > /dev/null
+# RUN: echo "INCLUDE " %t.script2 "OUTPUT(" %t.out ")" > %t.script1
+# RUN: echo "GROUP(" %t ")" > %t.script2
+# RUN: ld.lld2 %t.script1
+# RUN: llvm-readobj %t2 > /dev/null
+
# RUN: echo "FOO(BAR)" > %t.script
# RUN: not ld.lld2 -o foo %t.script > %t.log 2>&1
# RUN: FileCheck -check-prefix=ERR1 %s < %t.log