diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2018-01-30 16:20:08 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2018-01-30 16:20:08 +0000 |
commit | 22d533568bf70d533d67dfcddb6951732c9c8671 (patch) | |
tree | 4f9360ccba8c09f980b8377a091551f7a09768d4 | |
parent | 1f59ae311bc234f718624d72483152e9b1e160b3 (diff) | |
download | bcm5719-llvm-22d533568bf70d533d67dfcddb6951732c9c8671.tar.gz bcm5719-llvm-22d533568bf70d533d67dfcddb6951732c9c8671.zip |
Sort orphan section if --symbol-ordering-file is given.
Before this patch orphan sections were not sorted.
llvm-svn: 323779
-rw-r--r-- | lld/ELF/LinkerScript.cpp | 30 | ||||
-rw-r--r-- | lld/ELF/LinkerScript.h | 7 | ||||
-rw-r--r-- | lld/ELF/OutputSections.cpp | 4 | ||||
-rw-r--r-- | lld/ELF/OutputSections.h | 2 | ||||
-rw-r--r-- | lld/ELF/Writer.cpp | 11 | ||||
-rw-r--r-- | lld/test/ELF/linkerscript/symbol-ordering-file.s | 4 |
6 files changed, 22 insertions, 36 deletions
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index e6d0e455dd9..24885e04fe1 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -285,23 +285,11 @@ static void sortSections(MutableArrayRef<InputSection *> Vec, // --sort-section is handled as an inner SORT command. // 3. If one SORT command is given, and if it is SORT_NONE, don't sort. // 4. If no SORT command is given, sort according to --sort-section. -// 5. If no SORT commands are given and --sort-section is not specified, -// apply sorting provided by --symbol-ordering-file if any exist. -static void sortInputSections( - MutableArrayRef<InputSection *> Vec, const SectionPattern &Pat, - const DenseMap<SectionBase *, int> &Order) { +static void sortInputSections(MutableArrayRef<InputSection *> Vec, + const SectionPattern &Pat) { if (Pat.SortOuter == SortSectionPolicy::None) return; - if (Pat.SortOuter == SortSectionPolicy::Default && - Config->SortSection == SortSectionPolicy::Default) { - // If -symbol-ordering-file was given, sort accordingly. - // Usually, Order is empty. - if (!Order.empty()) - sortByOrder(Vec, [&](InputSectionBase *S) { return Order.lookup(S); }); - return; - } - if (Pat.SortInner == SortSectionPolicy::Default) sortSections(Vec, Config->SortSection); else @@ -311,8 +299,7 @@ static void sortInputSections( // Compute and remember which sections the InputSectionDescription matches. std::vector<InputSection *> -LinkerScript::computeInputSections(const InputSectionDescription *Cmd, - const DenseMap<SectionBase *, int> &Order) { +LinkerScript::computeInputSections(const InputSectionDescription *Cmd) { std::vector<InputSection *> Ret; // Collects all sections that satisfy constraints of Cmd. @@ -343,7 +330,7 @@ LinkerScript::computeInputSections(const InputSectionDescription *Cmd, } sortInputSections(MutableArrayRef<InputSection *>(Ret).slice(SizeBefore), - Pat, Order); + Pat); } return Ret; } @@ -360,13 +347,13 @@ void LinkerScript::discard(ArrayRef<InputSection *> V) { } } -std::vector<InputSection *> LinkerScript::createInputSectionList( - OutputSection &OutCmd, const DenseMap<SectionBase *, int> &Order) { +std::vector<InputSection *> +LinkerScript::createInputSectionList(OutputSection &OutCmd) { std::vector<InputSection *> Ret; for (BaseCommand *Base : OutCmd.SectionCommands) { if (auto *Cmd = dyn_cast<InputSectionDescription>(Base)) { - Cmd->Sections = computeInputSections(Cmd, Order); + Cmd->Sections = computeInputSections(Cmd); Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end()); } } @@ -395,7 +382,6 @@ void LinkerScript::processSectionCommands() { Ctx->OutSec = Aether; size_t I = 0; - DenseMap<SectionBase *, int> Order = buildSectionOrder(); // Add input sections to output sections. for (BaseCommand *Base : SectionCommands) { // Handle symbol assignments outside of any output section. @@ -405,7 +391,7 @@ void LinkerScript::processSectionCommands() { } if (auto *Sec = dyn_cast<OutputSection>(Base)) { - std::vector<InputSection *> V = createInputSectionList(*Sec, Order); + std::vector<InputSection *> V = createInputSectionList(*Sec); // The output section name `/DISCARD/' is special. // Any input section assigned to it is discarded. diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h index 53868600417..c16388d90e5 100644 --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -217,12 +217,9 @@ class LinkerScript final { void setDot(Expr E, const Twine &Loc, bool InSec); std::vector<InputSection *> - computeInputSections(const InputSectionDescription *, - const llvm::DenseMap<SectionBase *, int> &Order); + computeInputSections(const InputSectionDescription *); - std::vector<InputSection *> - createInputSectionList(OutputSection &Cmd, - const llvm::DenseMap<SectionBase *, int> &Order); + std::vector<InputSection *> createInputSectionList(OutputSection &Cmd); std::vector<size_t> getPhdrIndices(OutputSection *Sec); diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index 94c98284196..75df1c254f9 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -134,8 +134,8 @@ void OutputSection::addSection(InputSection *IS) { } } -void elf::sortByOrder(MutableArrayRef<InputSection *> In, - std::function<int(InputSectionBase *S)> Order) { +static void sortByOrder(MutableArrayRef<InputSection *> In, + std::function<int(InputSectionBase *S)> Order) { typedef std::pair<int, InputSection *> Pair; auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; }; diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h index c006eae0c04..810ad25c48d 100644 --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -143,8 +143,6 @@ namespace lld { namespace elf { uint64_t getHeaderSize(); -void sortByOrder(llvm::MutableArrayRef<InputSection *> In, - std::function<int(InputSectionBase *S)> Order); extern std::vector<OutputSection *> OutputSections; } // namespace elf diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index de6631cb410..d8cfd9d10da 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1018,10 +1018,8 @@ findOrphanPos(std::vector<BaseCommand *>::iterator B, } // If no layout was provided by linker script, we want to apply default -// sorting for special input sections and handle --symbol-ordering-file. +// sorting for special input sections. This also handles --symbol-ordering-file. template <class ELFT> void Writer<ELFT>::sortInputSections() { - assert(!Script->HasSectionsCommand); - // Sort input sections by priority using the list provided // by --symbol-ordering-file. DenseMap<SectionBase *, int> Order = buildSectionOrder(); @@ -1031,6 +1029,9 @@ template <class ELFT> void Writer<ELFT>::sortInputSections() { if (Sec->Live) Sec->sort([&](InputSectionBase *S) { return Order.lookup(S); }); + if (Script->HasSectionsCommand) + return; + // Sort input sections by section name suffixes for // __attribute__((init_priority(N))). if (OutputSection *Sec = findSection(".init_array")) @@ -1057,9 +1058,9 @@ template <class ELFT> void Writer<ELFT>::sortSections() { if (auto *Sec = dyn_cast<OutputSection>(Base)) Sec->SortRank = getSectionRank(Sec); - if (!Script->HasSectionsCommand) { - sortInputSections(); + sortInputSections(); + if (!Script->HasSectionsCommand) { // We know that all the OutputSections are contiguous in this case. auto E = Script->SectionCommands.end(); auto I = Script->SectionCommands.begin(); diff --git a/lld/test/ELF/linkerscript/symbol-ordering-file.s b/lld/test/ELF/linkerscript/symbol-ordering-file.s index be686c42088..b458cde2613 100644 --- a/lld/test/ELF/linkerscript/symbol-ordering-file.s +++ b/lld/test/ELF/linkerscript/symbol-ordering-file.s @@ -14,6 +14,10 @@ # AFTER: Contents of section .foo: # AFTER-NEXT: 2211 +# RUN: echo "SECTIONS { .text : { *(.text) } }" > %t2.script +# RUN: ld.lld --symbol-ordering-file %t.ord %t.o --script %t2.script -o %t3.out +# RUN: llvm-objdump -s %t3.out| FileCheck %s --check-prefix=AFTER + .section .foo,"ax",@progbits,unique,1 _foo1: .byte 0x11 |