Add comments.
This patch rearranges code a bit to make it easy to explain.
llvm-svn: 287515
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 5c56388..ff58e44 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -8,12 +8,6 @@
//===----------------------------------------------------------------------===//
//
// This file contains the parser/evaluator of the linker script.
-// It parses a linker script and write the result to Config or ScriptConfig
-// objects.
-//
-// If SECTIONS command is used, a ScriptConfig contains an AST
-// of the command which will later be consumed by createSections() and
-// assignAddresses().
//
//===----------------------------------------------------------------------===//
@@ -308,11 +302,14 @@
for (unsigned I = 0; I < Opt.Commands.size(); ++I) {
auto Iter = Opt.Commands.begin() + I;
const std::unique_ptr<BaseCommand> &Base1 = *Iter;
+
+ // Handle symbol assignments outside of any output section.
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
if (shouldDefine<ELFT>(Cmd))
addSymbol<ELFT>(Cmd);
continue;
}
+
if (auto *Cmd = dyn_cast<AssertCommand>(Base1.get())) {
// If we don't have SECTIONS then output sections have already been
// created by Writer<ELFT>. The LinkerScript<ELFT>::assignAddresses
@@ -325,11 +322,20 @@
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
+ // The output section name `/DISCARD/' is special.
+ // Any input section assigned to it is discarded.
if (Cmd->Name == "/DISCARD/") {
discard(V);
continue;
}
+ // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
+ // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
+ // sections satisfy a given constraint. If not, a directive is handled
+ // as if it wasn't present from the beginning.
+ //
+ // Because we'll iterate over Commands many more times, the easiest
+ // way to "make it as if it wasn't present" is to just remove it.
if (!matchConstraints<ELFT>(V, Cmd->Constraint)) {
for (InputSectionBase<ELFT> *S : V)
S->Assigned = false;
@@ -338,25 +344,32 @@
continue;
}
+ // A directive may contain symbol definitions like this:
+ // ".foo : { ...; bar = .; }". Handle them.
for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get()))
if (shouldDefine<ELFT>(OutCmd))
addSymbol<ELFT>(OutCmd);
- for (InputSectionBase<ELFT> *Sec : V) {
- addSection(Factory, Sec, Cmd->Name);
- if (uint32_t Subalign = Cmd->SubalignExpr ? Cmd->SubalignExpr(0) : 0)
- Sec->Alignment = Subalign;
+ // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
+ // is given, input sections are aligned to that value, whether the
+ // given value is larger or smaller than the original section alignment.
+ if (Cmd->SubalignExpr) {
+ uint32_t Subalign = Cmd->SubalignExpr(0);
+ for (InputSectionBase<ELFT> *S : V)
+ S->Alignment = Subalign;
}
+
+ // Add input sections to an output section.
+ for (InputSectionBase<ELFT> *S : V)
+ addSection(Factory, S, Cmd->Name);
}
}
}
+// Add sections that didn't match any sections command.
template <class ELFT>
-void LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
- processCommands(Factory);
-
- // Add orphan sections.
+void LinkerScript<ELFT>::addOrphanSections(OutputSectionFactory<ELFT> &Factory) {
for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections)
if (S->Live && !S->OutSec)
addSection(Factory, S, getOutputSectionName(S->Name));
@@ -482,6 +495,8 @@
return Ret;
}
+// This function assigns offsets to input sections and an output section
+// for a single sections command (e.g. ".text { *(.text); }").
template <class ELFT>
void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) {
if (Cmd->LMAExpr)
@@ -491,6 +506,7 @@
if (Sections.empty())
return;
switchTo(Sections[0]);
+
// Find the last section output location. We will output orphan sections
// there so that end symbols point to the correct location.
auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
@@ -517,10 +533,9 @@
auto Pos = std::remove_if(
Opt.Commands.begin(), Opt.Commands.end(),
[&](const std::unique_ptr<BaseCommand> &Base) {
- auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
- if (!Cmd)
- return false;
- return findSections<ELFT>(Cmd->Name, *OutputSections).empty();
+ if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
+ return findSections<ELFT>(Cmd->Name, *OutputSections).empty();
+ return false;
});
Opt.Commands.erase(Pos, Opt.Commands.end());
}
@@ -672,10 +687,8 @@
}
auto *Cmd = cast<OutputSectionCommand>(Base.get());
-
if (Cmd->AddrExpr)
Dot = Cmd->AddrExpr(Dot);
-
assignOffsets(Cmd);
}