summaryrefslogtreecommitdiff
path: root/ELF/LinkerScript.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ELF/LinkerScript.cpp')
-rw-r--r--ELF/LinkerScript.cpp133
1 files changed, 90 insertions, 43 deletions
diff --git a/ELF/LinkerScript.cpp b/ELF/LinkerScript.cpp
index 63eb90456e17d..22a5b639469b3 100644
--- a/ELF/LinkerScript.cpp
+++ b/ELF/LinkerScript.cpp
@@ -406,8 +406,15 @@ void LinkerScript::processCommands(OutputSectionFactory &Factory) {
}
// Add input sections to an output section.
- for (InputSectionBase *S : V)
- Factory.addInputSec(S, Cmd->Name);
+ unsigned Pos = 0;
+ for (InputSectionBase *S : V) {
+ // The actual offset will be computed during
+ // assignAddresses. For now, use the index as a very crude
+ // approximation so that it is at least easy for other code to
+ // know the section order.
+ cast<InputSection>(S)->OutSecOff = Pos++;
+ Factory.addInputSec(S, Cmd->Name, Cmd->Sec);
+ }
}
}
CurOutSec = nullptr;
@@ -465,9 +472,26 @@ void LinkerScript::fabricateDefaultCommands(bool AllocateHeader) {
// Add sections that didn't match any sections command.
void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) {
- for (InputSectionBase *S : InputSections)
- if (S->Live && !S->OutSec)
- Factory.addInputSec(S, getOutputSectionName(S->Name));
+ for (InputSectionBase *S : InputSections) {
+ if (!S->Live || S->OutSec)
+ continue;
+ StringRef Name = getOutputSectionName(S->Name);
+ auto I = std::find_if(
+ Opt.Commands.begin(), Opt.Commands.end(), [&](BaseCommand *Base) {
+ if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
+ return Cmd->Name == Name;
+ return false;
+ });
+ if (I == Opt.Commands.end()) {
+ Factory.addInputSec(S, Name);
+ } else {
+ auto *Cmd = cast<OutputSectionCommand>(*I);
+ Factory.addInputSec(S, Name, Cmd->Sec);
+ auto *ISD = make<InputSectionDescription>("");
+ ISD->Sections.push_back(S);
+ Cmd->Commands.push_back(ISD);
+ }
+ }
}
static bool isTbss(OutputSection *Sec) {
@@ -475,8 +499,6 @@ static bool isTbss(OutputSection *Sec) {
}
void LinkerScript::output(InputSection *S) {
- if (!AlreadyOutputIS.insert(S).second)
- return;
bool IsTbss = isTbss(CurOutSec);
uint64_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot;
@@ -508,19 +530,9 @@ void LinkerScript::output(InputSection *S) {
Dot = Pos;
}
-void LinkerScript::flush() {
- assert(CurOutSec);
- if (!AlreadyOutputOS.insert(CurOutSec).second)
- return;
- for (InputSection *I : CurOutSec->Sections)
- output(I);
-}
-
void LinkerScript::switchTo(OutputSection *Sec) {
if (CurOutSec == Sec)
return;
- if (AlreadyOutputOS.count(Sec))
- return;
CurOutSec = Sec;
@@ -571,19 +583,11 @@ void LinkerScript::process(BaseCommand &Base) {
if (!Sec->Live)
continue;
- assert(CurOutSec == Sec->OutSec || AlreadyOutputOS.count(Sec->OutSec));
+ assert(CurOutSec == Sec->OutSec);
output(cast<InputSection>(Sec));
}
}
-static OutputSection *
-findSection(StringRef Name, const std::vector<OutputSection *> &Sections) {
- for (OutputSection *Sec : Sections)
- if (Sec->Name == Name)
- return Sec;
- return nullptr;
-}
-
// This function searches for a memory region to place the given output
// section in. If found, a pointer to the appropriate memory region is
// returned. Otherwise, a nullptr is returned.
@@ -638,19 +642,8 @@ void LinkerScript::assignOffsets(OutputSectionCommand *Cmd) {
Dot = CurMemRegion->Offset;
switchTo(Sec);
- // flush() may add orphan sections, so the order of flush() and
- // symbol assignments is important. We want to call flush() first so
- // that symbols pointing the end of the current section points to
- // the location after orphan sections.
- auto Mid =
- std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
- [](BaseCommand *Cmd) { return !isa<SymbolAssignment>(Cmd); })
- .base();
- for (auto I = Cmd->Commands.begin(); I != Mid; ++I)
- process(**I);
- flush();
- for (auto I = Mid, E = Cmd->Commands.end(); I != E; ++I)
- process(**I);
+ for (BaseCommand *C : Cmd->Commands)
+ process(*C);
}
void LinkerScript::removeEmptyCommands() {
@@ -663,7 +656,8 @@ void LinkerScript::removeEmptyCommands() {
auto Pos = std::remove_if(
Opt.Commands.begin(), Opt.Commands.end(), [&](BaseCommand *Base) {
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
- return !Cmd->Sec;
+ return std::find(OutputSections->begin(), OutputSections->end(),
+ Cmd->Sec) == OutputSections->end();
return false;
});
Opt.Commands.erase(Pos, Opt.Commands.end());
@@ -687,8 +681,7 @@ void LinkerScript::adjustSectionsBeforeSorting() {
auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
if (!Cmd)
continue;
- if (OutputSection *Sec = findSection(Cmd->Name, *OutputSections)) {
- Cmd->Sec = Sec;
+ if (OutputSection *Sec = Cmd->Sec) {
Flags = Sec->Flags;
Type = Sec->Type;
continue;
@@ -820,15 +813,24 @@ void LinkerScript::placeOrphanSections() {
++CmdIndex;
}
+ // If there is no command corresponding to this output section,
+ // create one and put a InputSectionDescription in it so that both
+ // representations agree on which input sections to use.
auto Pos = std::find_if(CmdIter, E, [&](BaseCommand *Base) {
auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
return Cmd && Cmd->Name == Name;
});
if (Pos == E) {
auto *Cmd = make<OutputSectionCommand>(Name);
- Cmd->Sec = Sec;
Opt.Commands.insert(CmdIter, Cmd);
++CmdIndex;
+
+ Cmd->Sec = Sec;
+ auto *ISD = make<InputSectionDescription>("");
+ for (InputSection *IS : Sec->Sections)
+ ISD->Sections.push_back(IS);
+ Cmd->Commands.push_back(ISD);
+
continue;
}
@@ -846,6 +848,51 @@ void LinkerScript::processNonSectionCommands() {
}
}
+// Do a last effort at synchronizing the linker script "AST" and the section
+// list. This is needed to account for last minute changes, like adding a
+// .ARM.exidx terminator and sorting SHF_LINK_ORDER sections.
+//
+// FIXME: We should instead create the "AST" earlier and the above changes would
+// be done directly in the "AST".
+//
+// This can only handle new sections being added and sections being reordered.
+void LinkerScript::synchronize() {
+ for (BaseCommand *Base : Opt.Commands) {
+ auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
+ if (!Cmd)
+ continue;
+ ArrayRef<InputSection *> Sections = Cmd->Sec->Sections;
+ std::vector<InputSectionBase **> ScriptSections;
+ DenseSet<InputSectionBase *> ScriptSectionsSet;
+ for (BaseCommand *Base : Cmd->Commands) {
+ auto *ISD = dyn_cast<InputSectionDescription>(Base);
+ if (!ISD)
+ continue;
+ for (InputSectionBase *&IS : ISD->Sections) {
+ if (IS->Live) {
+ ScriptSections.push_back(&IS);
+ ScriptSectionsSet.insert(IS);
+ }
+ }
+ }
+ std::vector<InputSectionBase *> Missing;
+ for (InputSection *IS : Sections)
+ if (!ScriptSectionsSet.count(IS))
+ Missing.push_back(IS);
+ if (!Missing.empty()) {
+ auto ISD = make<InputSectionDescription>("");
+ ISD->Sections = Missing;
+ Cmd->Commands.push_back(ISD);
+ for (InputSectionBase *&IS : ISD->Sections)
+ if (IS->Live)
+ ScriptSections.push_back(&IS);
+ }
+ assert(ScriptSections.size() == Sections.size());
+ for (int I = 0, N = Sections.size(); I < N; ++I)
+ *ScriptSections[I] = Sections[I];
+ }
+}
+
void LinkerScript::assignAddresses(std::vector<PhdrEntry> &Phdrs) {
// Assign addresses as instructed by linker script SECTIONS sub-commands.
Dot = 0;