diff options
| author | George Rimar <grimar@accesssoftek.com> | 2017-10-30 10:12:49 +0000 |
|---|---|---|
| committer | George Rimar <grimar@accesssoftek.com> | 2017-10-30 10:12:49 +0000 |
| commit | a6b1fbece11f2cece7f3682f9e9e1b7146235f49 (patch) | |
| tree | 50d7a28004257ccdaee9f7bb561dcacc50a4516c | |
| parent | 9a8c8bf60d16682193bb7edda7f30798ce99d298 (diff) | |
| download | bcm5719-llvm-a6b1fbece11f2cece7f3682f9e9e1b7146235f49.tar.gz bcm5719-llvm-a6b1fbece11f2cece7f3682f9e9e1b7146235f49.zip | |
[ELF] - Stop sorting input sections in createSections().
It does not seem that createSections() is a good place for
applying sorting. Patch changes code to do that inside
sortSections(), which looks more appropriate place.
Differential revision: https://reviews.llvm.org/D39371
llvm-svn: 316893
| -rw-r--r-- | lld/ELF/OutputSections.cpp | 1 | ||||
| -rw-r--r-- | lld/ELF/Writer.cpp | 64 |
2 files changed, 33 insertions, 32 deletions
diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index 72bb75cbbaf..10eebb4028f 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -296,6 +296,7 @@ bool OutputSection::classof(const BaseCommand *C) { } void OutputSection::sort(std::function<int(InputSectionBase *S)> Order) { + assert(Live); assert(SectionCommands.size() == 1); sortByOrder(cast<InputSectionDescription>(SectionCommands[0])->Sections, Order); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index b9ac65d32d9..497b7573214 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -52,6 +52,7 @@ private: void createSections(); void forEachRelSec(std::function<void(InputSectionBase &)> Fn); void sortSections(); + void sortInputSections(); void finalizeSections(); void addPredefinedSections(); void setReservedSymbolSections(); @@ -837,31 +838,6 @@ template <class ELFT> void Writer<ELFT>::addReservedSymbols() { ElfSym::Edata2 = Add("_edata", -1); } -// Sort input sections by section name suffixes for -// __attribute__((init_priority(N))). -static void sortInitFini(OutputSection *Cmd) { - if (Cmd) - Cmd->sortInitFini(); -} - -// Sort input sections by the special rule for .ctors and .dtors. -static void sortCtorsDtors(OutputSection *Cmd) { - if (Cmd) - Cmd->sortCtorsDtors(); -} - -// Sort input sections using the list provided by --symbol-ordering-file. -static void sortBySymbolsOrder() { - if (Config->SymbolOrderingFile.empty()) - return; - - // Sort sections by priority. - DenseMap<SectionBase *, int> SectionOrder = buildSectionOrder(); - for (BaseCommand *Base : Script->SectionCommands) - if (auto *Sec = dyn_cast<OutputSection>(Base)) - Sec->sort([&](InputSectionBase *S) { return SectionOrder.lookup(S); }); -} - template <class ELFT> void Writer<ELFT>::forEachRelSec(std::function<void(InputSectionBase &)> Fn) { // Scan all relocations. Each relocation goes through a series @@ -888,11 +864,6 @@ template <class ELFT> void Writer<ELFT>::createSections() { Vec.end()); Script->fabricateDefaultCommands(); - sortBySymbolsOrder(); - sortInitFini(findSection(".init_array")); - sortInitFini(findSection(".fini_array")); - sortCtorsDtors(findSection(".ctors")); - sortCtorsDtors(findSection(".dtors")); } // This function generates assignments for predefined symbols (e.g. _end or @@ -1050,6 +1021,34 @@ findOrphanPos(std::vector<BaseCommand *>::iterator B, return I; } +// If no layout was provided by linker script, we want to apply default +// sorting for special input sections and handle --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(); + if (!Order.empty()) + for (BaseCommand *Base : Script->SectionCommands) + if (auto *Sec = dyn_cast<OutputSection>(Base)) + if (Sec->Live) + Sec->sort([&](InputSectionBase *S) { return Order.lookup(S); }); + + // Sort input sections by section name suffixes for + // __attribute__((init_priority(N))). + if (OutputSection *Sec = findSection(".init_array")) + Sec->sortInitFini(); + if (OutputSection *Sec = findSection(".fini_array")) + Sec->sortInitFini(); + + // Sort input sections by the special rule for .ctors and .dtors. + if (OutputSection *Sec = findSection(".ctors")) + Sec->sortCtorsDtors(); + if (OutputSection *Sec = findSection(".dtors")) + Sec->sortCtorsDtors(); +} + template <class ELFT> void Writer<ELFT>::sortSections() { Script->adjustSectionsBeforeSorting(); @@ -1063,8 +1062,9 @@ template <class ELFT> void Writer<ELFT>::sortSections() { Sec->SortRank = getSectionRank(Sec); if (!Script->HasSectionsCommand) { - // We know that all the OutputSections are contiguous in - // this case. + sortInputSections(); + + // We know that all the OutputSections are contiguous in this case. auto E = Script->SectionCommands.end(); auto I = Script->SectionCommands.begin(); auto IsSection = [](BaseCommand *Base) { return isa<OutputSection>(Base); }; |

