[ELF] - Support "INSERT AFTER" statement.

This implements INSERT AFTER in a following way:

During reading scripts it collects all insert statements.
After we done and read all files it inserts statements into script commands list.

With that:
* Rest of code does know nothing about INSERT.
* Approach is straightforward and have no visible limitations.
* It is also easy to support INSERT BEFORE (was seen in clang code once).
* Should work for PR35877 and similar cases.

Cons:
* It assumes we have "main" scripts that describes sections.

Differential revision: https://reviews.llvm.org/D43468

llvm-svn: 327003
This commit is contained in:
George Rimar
2018-03-08 14:54:38 +00:00
parent 9003b3b76d
commit 9e2c8a9db1
7 changed files with 87 additions and 1 deletions

View File

@@ -203,6 +203,26 @@ static void declareSymbol(SymbolAssignment *Cmd) {
Cmd->Provide = false;
}
// This method is used to handle INSERT AFTER statement. Here we rebuild
// the list of script commands to mix sections inserted into.
void LinkerScript::processInsertCommands() {
std::vector<BaseCommand *> V;
for (BaseCommand *Base : SectionCommands) {
V.push_back(Base);
if (auto *Cmd = dyn_cast<OutputSection>(Base)) {
std::vector<BaseCommand *> &W = InsertAfterCommands[Cmd->Name];
V.insert(V.end(), W.begin(), W.end());
W.clear();
}
}
for (std::pair<StringRef, std::vector<BaseCommand *>> &P :
InsertAfterCommands)
if (!P.second.empty())
error("unable to INSERT AFTER " + P.first + ": section not defined");
SectionCommands = std::move(V);
}
// Symbols defined in script should not be inlined by LTO. At the same time
// we don't know their final values until late stages of link. Here we scan
// over symbol assignment commands and create placeholder symbols if needed.