Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -31,25 +31,13 @@ template class InputSectionBase; template class OutputSectionBase; -// This class represents each rule in SECTIONS command. -struct SectionRule { - SectionRule(StringRef D, StringRef S) - : Dest(D), SectionPattern(S) {} - - StringRef Dest; - - StringRef SectionPattern; -}; - -// This enum represents what we can observe in SECTIONS tag of script. -// Each sections-command may of be one of the following: -// (https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS) -// * An ENTRY command. -// * A symbol assignment. -// * An output section description. -// * An overlay description. -// We support only AssignmentKind and OutputSectionKind for now. -enum SectionsCommandKind { AssignmentKind, OutputSectionKind }; +// This enum is used to implement linker script SECTIONS command. +// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS +enum SectionsCommandKind { + AssignmentKind, + OutputSectionKind, + InputSectionKind +}; struct BaseCommand { BaseCommand(int K) : Kind(K) {} @@ -70,10 +58,17 @@ : BaseCommand(OutputSectionKind), Name(Name) {} static bool classof(const BaseCommand *C); StringRef Name; + std::vector> Commands; std::vector Phdrs; std::vector Filler; }; +struct InputSectionDescription : BaseCommand { + InputSectionDescription() : BaseCommand(InputSectionKind) {} + static bool classof(const BaseCommand *C); + std::vector Patterns; +}; + struct PhdrsCommand { StringRef Name; unsigned Type; @@ -83,9 +78,6 @@ // ScriptConfiguration holds linker script parse results. struct ScriptConfiguration { - // SECTIONS commands. - std::vector Sections; - // Used to assign addresses to sections. std::vector> Commands; @@ -113,7 +105,6 @@ std::vector *> createSections(OutputSectionFactory &Factory); - StringRef getOutputSection(InputSectionBase *S); ArrayRef getFiller(StringRef Name); bool isDiscarded(InputSectionBase *S); bool shouldKeep(InputSectionBase *S); Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -47,6 +47,10 @@ return C->Kind == OutputSectionKind; } +bool InputSectionDescription::classof(const BaseCommand *C) { + return C->Kind == InputSectionKind; +} + // This is an operator-precedence parser to parse and evaluate // a linker script expression. For each linker script arithmetic // expression (e.g. ". = . + 0x1000"), a new instance of ExprParser @@ -195,16 +199,8 @@ uint64_t ExprParser::parseExpr() { return parseExpr1(parsePrimary(), 0); } template -StringRef LinkerScript::getOutputSection(InputSectionBase *S) { - for (SectionRule &R : Opt.Sections) - if (globMatch(R.SectionPattern, S->getSectionName())) - return R.Dest; - return ""; -} - -template bool LinkerScript::isDiscarded(InputSectionBase *S) { - return !S || !S->Live || getOutputSection(S) == "/DISCARD/"; + return !S || !S->Live; } template @@ -218,6 +214,7 @@ template std::vector *> LinkerScript::createSections(OutputSectionFactory &Factory) { + typedef const std::unique_ptr> ObjectFile; std::vector *> Result; // Add input section to output section. If there is no output section yet, @@ -234,18 +231,39 @@ // Select input sections matching rule and add them to corresponding // output section. Section rules are processed in order they're listed // in script, so correct input section order is maintained by design. - for (SectionRule &R : Opt.Sections) - for (const std::unique_ptr> &F : - Symtab::X->getObjectFiles()) - for (InputSectionBase *S : F->getSections()) - if (!isDiscarded(S) && !S->OutSec && - globMatch(R.SectionPattern, S->getSectionName())) - // Add single input section to output section. - AddInputSec(S, R.Dest); + for (const std::unique_ptr &Base : Opt.Commands) { + auto *OutCmd = dyn_cast(Base.get()); + if (!OutCmd) + continue; + + for (const std::unique_ptr &Cmd : OutCmd->Commands) { + auto *InCmd = dyn_cast(Cmd.get()); + if (!InCmd) + continue; + + for (ObjectFile &F : Symtab::X->getObjectFiles()) + for (InputSectionBase *S : F->getSections()) { + if (isDiscarded(S) || S->OutSec) + continue; + + for (StringRef P : InCmd->Patterns) { + if (!globMatch(P, S->getSectionName())) + continue; + + if (OutCmd->Name == "/DISCARD/") { + S->Live = false; + break; + } + + AddInputSec(S, OutCmd->Name); + break; + } + } + } + } // Add all other input sections, which are not listed in script. - for (const std::unique_ptr> &F : - Symtab::X->getObjectFiles()) + for (ObjectFile &F : Symtab::X->getObjectFiles()) for (InputSectionBase *S : F->getSections()) if (!isDiscarded(S)) { if (!S->OutSec) @@ -729,17 +747,20 @@ while (!Error && !skip("}")) { StringRef Tok = next(); if (Tok == "*") { + auto *InCmd = new InputSectionDescription(); + Cmd->Commands.emplace_back(InCmd); expect("("); while (!Error && !skip(")")) - Opt.Sections.emplace_back(OutSec, next()); + InCmd->Patterns.push_back(next()); } else if (Tok == "KEEP") { expect("("); expect("*"); expect("("); + auto *InCmd = new InputSectionDescription(); + Cmd->Commands.emplace_back(InCmd); while (!Error && !skip(")")) { - StringRef Sec = next(); - Opt.Sections.emplace_back(OutSec, Sec); - Opt.KeptSections.push_back(Sec); + Opt.KeptSections.push_back(peek()); + InCmd->Patterns.push_back(next()); } expect(")"); } else { Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -90,10 +90,6 @@ template StringRef elf::getOutputSectionName(InputSectionBase *S) { - StringRef Dest = Script::X->getOutputSection(S); - if (!Dest.empty()) - return Dest; - StringRef Name = S->getSectionName(); for (StringRef V : {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.",