Use make to create linker script command objects.

It simplifies variable types.

llvm-svn: 299505
This commit is contained in:
Rui Ueyama 2017-04-05 03:20:42 +00:00
parent d379f7357d
commit 8f99f73c8f
2 changed files with 61 additions and 65 deletions

View File

@ -395,8 +395,8 @@ std::vector<InputSectionBase *>
LinkerScript::createInputSectionList(OutputSectionCommand &OutCmd) { LinkerScript::createInputSectionList(OutputSectionCommand &OutCmd) {
std::vector<InputSectionBase *> Ret; std::vector<InputSectionBase *> Ret;
for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) { for (BaseCommand *Base : OutCmd.Commands) {
auto *Cmd = dyn_cast<InputSectionDescription>(Base.get()); auto *Cmd = dyn_cast<InputSectionDescription>(Base);
if (!Cmd) if (!Cmd)
continue; continue;
@ -425,15 +425,15 @@ void LinkerScript::processCommands(OutputSectionFactory &Factory) {
for (unsigned I = 0; I < Opt.Commands.size(); ++I) { for (unsigned I = 0; I < Opt.Commands.size(); ++I) {
auto Iter = Opt.Commands.begin() + I; auto Iter = Opt.Commands.begin() + I;
const std::unique_ptr<BaseCommand> &Base1 = *Iter; BaseCommand *Base1 = *Iter;
// Handle symbol assignments outside of any output section. // Handle symbol assignments outside of any output section.
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) { if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1)) {
addSymbol(Cmd); addSymbol(Cmd);
continue; continue;
} }
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) { if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1)) {
std::vector<InputSectionBase *> V = createInputSectionList(*Cmd); std::vector<InputSectionBase *> V = createInputSectionList(*Cmd);
// The output section name `/DISCARD/' is special. // The output section name `/DISCARD/' is special.
@ -460,8 +460,8 @@ void LinkerScript::processCommands(OutputSectionFactory &Factory) {
// A directive may contain symbol definitions like this: // A directive may contain symbol definitions like this:
// ".foo : { ...; bar = .; }". Handle them. // ".foo : { ...; bar = .; }". Handle them.
for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands) for (BaseCommand *Base : Cmd->Commands)
if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get())) if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
addSymbol(OutCmd); addSymbol(OutCmd);
// Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
@ -663,16 +663,15 @@ void LinkerScript::assignOffsets(OutputSectionCommand *Cmd) {
// Find the last section output location. We will output orphan sections // Find the last section output location. We will output orphan sections
// there so that end symbols point to the correct location. // there so that end symbols point to the correct location.
auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(), auto E =
[](const std::unique_ptr<BaseCommand> &Cmd) { std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
return !isa<SymbolAssignment>(*Cmd); [](BaseCommand *Cmd) { return !isa<SymbolAssignment>(Cmd); })
})
.base(); .base();
for (auto I = Cmd->Commands.begin(); I != E; ++I) for (auto I = Cmd->Commands.begin(); I != E; ++I)
process(**I); process(**I);
flush(); flush();
std::for_each(E, Cmd->Commands.end(), std::for_each(E, Cmd->Commands.end(),
[this](std::unique_ptr<BaseCommand> &B) { process(*B.get()); }); [this](BaseCommand *B) { process(*B); });
} }
void LinkerScript::removeEmptyCommands() { void LinkerScript::removeEmptyCommands() {
@ -683,9 +682,8 @@ void LinkerScript::removeEmptyCommands() {
// We instead remove trivially empty sections. The bfd linker seems even // We instead remove trivially empty sections. The bfd linker seems even
// more aggressive at removing them. // more aggressive at removing them.
auto Pos = std::remove_if( auto Pos = std::remove_if(
Opt.Commands.begin(), Opt.Commands.end(), Opt.Commands.begin(), Opt.Commands.end(), [&](BaseCommand *Base) {
[&](const std::unique_ptr<BaseCommand> &Base) { if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
return !findSection(Cmd->Name, *OutputSections); return !findSection(Cmd->Name, *OutputSections);
return false; return false;
}); });
@ -693,8 +691,8 @@ void LinkerScript::removeEmptyCommands() {
} }
static bool isAllSectionDescription(const OutputSectionCommand &Cmd) { static bool isAllSectionDescription(const OutputSectionCommand &Cmd) {
for (const std::unique_ptr<BaseCommand> &I : Cmd.Commands) for (BaseCommand *Base : Cmd.Commands)
if (!isa<InputSectionDescription>(*I)) if (!isa<InputSectionDescription>(*Base))
return false; return false;
return true; return true;
} }
@ -706,8 +704,8 @@ void LinkerScript::adjustSectionsBeforeSorting() {
// consequeces and gives us a section to put the symbol in. // consequeces and gives us a section to put the symbol in.
uint64_t Flags = SHF_ALLOC; uint64_t Flags = SHF_ALLOC;
uint32_t Type = SHT_NOBITS; uint32_t Type = SHT_NOBITS;
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { for (BaseCommand *Base : Opt.Commands) {
auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
if (!Cmd) if (!Cmd)
continue; continue;
if (OutputSection *Sec = findSection(Cmd->Name, *OutputSections)) { if (OutputSection *Sec = findSection(Cmd->Name, *OutputSections)) {
@ -742,10 +740,11 @@ void LinkerScript::adjustSectionsAfterSorting() {
// Walk the commands and propagate the program headers to commands that don't // Walk the commands and propagate the program headers to commands that don't
// explicitly specify them. // explicitly specify them.
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { for (BaseCommand *Base : Opt.Commands) {
auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
if (!Cmd) if (!Cmd)
continue; continue;
if (Cmd->Phdrs.empty()) if (Cmd->Phdrs.empty())
Cmd->Phdrs = DefPhdrs; Cmd->Phdrs = DefPhdrs;
else else
@ -810,15 +809,13 @@ void LinkerScript::placeOrphanSections() {
// section. We do this because it is common to set a load address by starting // section. We do this because it is common to set a load address by starting
// the script with ". = 0xabcd" and the expectation is that every section is // the script with ". = 0xabcd" and the expectation is that every section is
// after that. // after that.
auto FirstSectionOrDotAssignment = auto FirstSectionOrDotAssignment = std::find_if(
std::find_if(Opt.Commands.begin(), Opt.Commands.end(), Opt.Commands.begin(), Opt.Commands.end(), [](BaseCommand *Cmd) {
[](const std::unique_ptr<BaseCommand> &Cmd) { if (isa<OutputSectionCommand>(Cmd))
if (isa<OutputSectionCommand>(*Cmd))
return true; return true;
const auto *Assign = dyn_cast<SymbolAssignment>(Cmd.get()); if (auto *Assign = dyn_cast<SymbolAssignment>(Cmd))
if (!Assign)
return false;
return Assign->Name == "."; return Assign->Name == ".";
return false;
}); });
if (FirstSectionOrDotAssignment != Opt.Commands.end()) { if (FirstSectionOrDotAssignment != Opt.Commands.end()) {
CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin(); CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin();
@ -838,14 +835,12 @@ void LinkerScript::placeOrphanSections() {
++CmdIndex; ++CmdIndex;
} }
auto Pos = auto Pos = std::find_if(CmdIter, E, [&](BaseCommand *Base) {
std::find_if(CmdIter, E, [&](const std::unique_ptr<BaseCommand> &Base) { auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
return Cmd && Cmd->Name == Name; return Cmd && Cmd->Name == Name;
}); });
if (Pos == E) { if (Pos == E) {
Opt.Commands.insert(CmdIter, Opt.Commands.insert(CmdIter, make<OutputSectionCommand>(Name));
llvm::make_unique<OutputSectionCommand>(Name));
++CmdIndex; ++CmdIndex;
continue; continue;
} }
@ -856,10 +851,10 @@ void LinkerScript::placeOrphanSections() {
} }
void LinkerScript::processNonSectionCommands() { void LinkerScript::processNonSectionCommands() {
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { for (BaseCommand *Base : Opt.Commands) {
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) if (auto *Cmd = dyn_cast<SymbolAssignment>(Base))
assignSymbol(Cmd, false); assignSymbol(Cmd, false);
else if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) else if (auto *Cmd = dyn_cast<AssertCommand>(Base))
Cmd->Expression(); Cmd->Expression();
} }
} }
@ -870,18 +865,18 @@ void LinkerScript::assignAddresses(std::vector<PhdrEntry> &Phdrs) {
ErrorOnMissingSection = true; ErrorOnMissingSection = true;
switchTo(Aether); switchTo(Aether);
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { for (BaseCommand *Base : Opt.Commands) {
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) { if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
assignSymbol(Cmd, false); assignSymbol(Cmd, false);
continue; continue;
} }
if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) { if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
Cmd->Expression(); Cmd->Expression();
continue; continue;
} }
auto *Cmd = cast<OutputSectionCommand>(Base.get()); auto *Cmd = cast<OutputSectionCommand>(Base);
assignOffsets(Cmd); assignOffsets(Cmd);
} }
@ -942,8 +937,8 @@ bool LinkerScript::ignoreInterpSection() {
} }
uint32_t LinkerScript::getFiller(StringRef Name) { uint32_t LinkerScript::getFiller(StringRef Name) {
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) for (BaseCommand *Base : Opt.Commands)
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
if (Cmd->Name == Name) if (Cmd->Name == Name)
return Cmd->Filler; return Cmd->Filler;
return 0; return 0;
@ -973,15 +968,15 @@ void LinkerScript::writeDataBytes(StringRef Name, uint8_t *Buf) {
if (I == INT_MAX) if (I == INT_MAX)
return; return;
auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get()); auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I]);
for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands) for (BaseCommand *Base : Cmd->Commands)
if (auto *Data = dyn_cast<BytesDataCommand>(Base.get())) if (auto *Data = dyn_cast<BytesDataCommand>(Base))
writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size); writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
} }
bool LinkerScript::hasLMA(StringRef Name) { bool LinkerScript::hasLMA(StringRef Name) {
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) for (BaseCommand *Base : Opt.Commands)
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
if (Cmd->LMAExpr && Cmd->Name == Name) if (Cmd->LMAExpr && Cmd->Name == Name)
return true; return true;
return false; return false;
@ -993,7 +988,7 @@ bool LinkerScript::hasLMA(StringRef Name) {
// it returns INT_MAX, so that it will be laid out at end of file. // it returns INT_MAX, so that it will be laid out at end of file.
int LinkerScript::getSectionIndex(StringRef Name) { int LinkerScript::getSectionIndex(StringRef Name) {
for (int I = 0, E = Opt.Commands.size(); I != E; ++I) for (int I = 0, E = Opt.Commands.size(); I != E; ++I)
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get())) if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I]))
if (Cmd->Name == Name) if (Cmd->Name == Name)
return I; return I;
return INT_MAX; return INT_MAX;
@ -1018,8 +1013,8 @@ bool LinkerScript::isDefined(StringRef S) { return findSymbol(S) != nullptr; }
// by Name. Each index is a zero based number of ELF header listed within // by Name. Each index is a zero based number of ELF header listed within
// PHDRS {} script block. // PHDRS {} script block.
std::vector<size_t> LinkerScript::getPhdrIndices(StringRef SectionName) { std::vector<size_t> LinkerScript::getPhdrIndices(StringRef SectionName) {
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { for (BaseCommand *Base : Opt.Commands) {
auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
if (!Cmd || Cmd->Name != SectionName) if (!Cmd || Cmd->Name != SectionName)
continue; continue;
@ -1154,7 +1149,7 @@ void ScriptParser::readLinkerScript() {
continue; continue;
if (Tok == "ASSERT") { if (Tok == "ASSERT") {
Script->Opt.Commands.emplace_back(new AssertCommand(readAssert())); Script->Opt.Commands.push_back(make<AssertCommand>(readAssert()));
} else if (Tok == "ENTRY") { } else if (Tok == "ENTRY") {
readEntry(); readEntry();
} else if (Tok == "EXTERN") { } else if (Tok == "EXTERN") {
@ -1355,11 +1350,11 @@ void ScriptParser::readSections() {
BaseCommand *Cmd = readProvideOrAssignment(Tok); BaseCommand *Cmd = readProvideOrAssignment(Tok);
if (!Cmd) { if (!Cmd) {
if (Tok == "ASSERT") if (Tok == "ASSERT")
Cmd = new AssertCommand(readAssert()); Cmd = make<AssertCommand>(readAssert());
else else
Cmd = readOutputSectionDescription(Tok); Cmd = readOutputSectionDescription(Tok);
} }
Script->Opt.Commands.emplace_back(Cmd); Script->Opt.Commands.push_back(Cmd);
} }
} }
@ -1433,8 +1428,9 @@ std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
// <section-list> is parsed by readInputSectionsList(). // <section-list> is parsed by readInputSectionsList().
InputSectionDescription * InputSectionDescription *
ScriptParser::readInputSectionRules(StringRef FilePattern) { ScriptParser::readInputSectionRules(StringRef FilePattern) {
auto *Cmd = new InputSectionDescription(FilePattern); auto *Cmd = make<InputSectionDescription>(FilePattern);
expect("("); expect("(");
while (!Error && !consume(")")) { while (!Error && !consume(")")) {
SortSectionPolicy Outer = readSortKind(); SortSectionPolicy Outer = readSortKind();
SortSectionPolicy Inner = SortSectionPolicy::Default; SortSectionPolicy Inner = SortSectionPolicy::Default;
@ -1512,7 +1508,7 @@ uint32_t ScriptParser::readFill() {
OutputSectionCommand * OutputSectionCommand *
ScriptParser::readOutputSectionDescription(StringRef OutSec) { ScriptParser::readOutputSectionDescription(StringRef OutSec) {
OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec); OutputSectionCommand *Cmd = make<OutputSectionCommand>(OutSec);
Cmd->Location = getCurrentLocation(); Cmd->Location = getCurrentLocation();
// Read an address expression. // Read an address expression.
@ -1626,7 +1622,7 @@ SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
std::string Loc = getCurrentLocation(); std::string Loc = getCurrentLocation();
E = [=] { return add(Script->getSymbolValue(Loc, Name), E()); }; E = [=] { return add(Script->getSymbolValue(Loc, Name), E()); };
} }
return new SymbolAssignment(Name, E, getCurrentLocation()); return make<SymbolAssignment>(Name, E, getCurrentLocation());
} }
// This is an operator-precedence parser to parse a linker // This is an operator-precedence parser to parse a linker
@ -1756,7 +1752,7 @@ BytesDataCommand *ScriptParser::readBytesDataCommand(StringRef Tok) {
if (Size == -1) if (Size == -1)
return nullptr; return nullptr;
return new BytesDataCommand(readParenExpr(), Size); return make<BytesDataCommand>(readParenExpr(), Size);
} }
StringRef ScriptParser::readParenLiteral() { StringRef ScriptParser::readParenLiteral() {

View File

@ -123,7 +123,7 @@ struct OutputSectionCommand : BaseCommand {
Expr AlignExpr; Expr AlignExpr;
Expr LMAExpr; Expr LMAExpr;
Expr SubalignExpr; Expr SubalignExpr;
std::vector<std::unique_ptr<BaseCommand>> Commands; std::vector<BaseCommand *> Commands;
std::vector<StringRef> Phdrs; std::vector<StringRef> Phdrs;
uint32_t Filler = 0; uint32_t Filler = 0;
ConstraintKind Constraint = ConstraintKind::NoConstraint; ConstraintKind Constraint = ConstraintKind::NoConstraint;
@ -204,7 +204,7 @@ struct MemoryRegion {
// ScriptConfiguration holds linker script parse results. // ScriptConfiguration holds linker script parse results.
struct ScriptConfiguration { struct ScriptConfiguration {
// Used to assign addresses to sections. // Used to assign addresses to sections.
std::vector<std::unique_ptr<BaseCommand>> Commands; std::vector<BaseCommand *> Commands;
// Used to assign sections to headers. // Used to assign sections to headers.
std::vector<PhdrsCommand> PhdrsCommands; std::vector<PhdrsCommand> PhdrsCommands;