[lld] r290151 - Simplify type of Config->SymbolOrderingFile.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 19 17:51:09 PST 2016


Author: ruiu
Date: Mon Dec 19 19:51:08 2016
New Revision: 290151

URL: http://llvm.org/viewvc/llvm-project?rev=290151&view=rev
Log:
Simplify type of Config->SymbolOrderingFile.

That variable was of type DenseMap<StringRef, unsigned>, but the
unsigned numbers needed to be monotonicly increasing numbers because
the implementation that used the variable depended on that fact.
That was an implementation detail and shouldn't have leaked into Config.

This patch simplifies its type to std::vector<StringRef>.

Modified:
    lld/trunk/ELF/Config.h
    lld/trunk/ELF/Driver.cpp
    lld/trunk/ELF/OutputSections.cpp
    lld/trunk/ELF/OutputSections.h
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Config.h?rev=290151&r1=290150&r2=290151&view=diff
==============================================================================
--- lld/trunk/ELF/Config.h (original)
+++ lld/trunk/ELF/Config.h Mon Dec 19 19:51:08 2016
@@ -72,7 +72,6 @@ struct VersionDefinition {
 struct Configuration {
   InputFile *FirstElf = nullptr;
   uint8_t OSABI = 0;
-  llvm::DenseMap<llvm::StringRef, unsigned> SymbolOrderingFile;
   llvm::StringMap<uint64_t> SectionStartMap;
   llvm::StringRef DynamicLinker;
   llvm::StringRef Entry;
@@ -89,6 +88,7 @@ struct Configuration {
   std::vector<VersionDefinition> VersionDefinitions;
   std::vector<llvm::StringRef> AuxiliaryList;
   std::vector<llvm::StringRef> SearchPaths;
+  std::vector<llvm::StringRef> SymbolOrderingFile;
   std::vector<llvm::StringRef> Undefined;
   std::vector<SymbolVersion> VersionScriptGlobals;
   std::vector<SymbolVersion> VersionScriptLocals;

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=290151&r1=290150&r2=290151&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Mon Dec 19 19:51:08 2016
@@ -491,16 +491,6 @@ static std::vector<StringRef> getLines(M
   return Ret;
 }
 
-// Parse the --symbol-ordering-file argument. File has form:
-// symbolName1
-// [...]
-// symbolNameN
-static void parseSymbolOrderingList(MemoryBufferRef MB) {
-  unsigned I = 0;
-  for (StringRef S : getLines(MB))
-    Config->SymbolOrderingFile.insert({S, I++});
-}
-
 // Initializes Config members by the command line options.
 void LinkerDriver::readConfigs(opt::InputArgList &Args) {
   for (auto *Arg : Args.filtered(OPT_L))
@@ -645,7 +635,7 @@ void LinkerDriver::readConfigs(opt::Inpu
 
   if (auto *Arg = Args.getLastArg(OPT_symbol_ordering_file))
     if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
-      parseSymbolOrderingList(*Buffer);
+      Config->SymbolOrderingFile = getLines(*Buffer);
 
   // If --retain-symbol-file is used, we'll retail only the symbols listed in
   // the file and discard all others.

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=290151&r1=290150&r2=290151&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Mon Dec 19 19:51:08 2016
@@ -153,7 +153,7 @@ template <class ELFT> void OutputSection
 
 template <class ELFT>
 void OutputSection<ELFT>::sort(
-    std::function<unsigned(InputSection<ELFT> *S)> Order) {
+    std::function<int(InputSection<ELFT> *S)> Order) {
   typedef std::pair<unsigned, InputSection<ELFT> *> Pair;
   auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
 

Modified: lld/trunk/ELF/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=290151&r1=290150&r2=290151&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.h (original)
+++ lld/trunk/ELF/OutputSections.h Mon Dec 19 19:51:08 2016
@@ -111,7 +111,7 @@ public:
   typedef typename ELFT::uint uintX_t;
   OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
   void addSection(InputSectionData *C) override;
-  void sort(std::function<unsigned(InputSection<ELFT> *S)> Order);
+  void sort(std::function<int(InputSection<ELFT> *S)> Order);
   void sortInitFini();
   void sortCtorsDtors();
   void writeTo(uint8_t *Buf) override;

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=290151&r1=290150&r2=290151&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Mon Dec 19 19:51:08 2016
@@ -765,33 +765,34 @@ template <class ELFT> static void sortCt
 
 // Sort input sections using the list provided by --symbol-ordering-file.
 template <class ELFT>
-static void sortBySymbolsOrder(ArrayRef<OutputSectionBase *> V) {
+static void sortBySymbolsOrder(ArrayRef<OutputSectionBase *> OutputSections) {
   if (Config->SymbolOrderingFile.empty())
     return;
 
-  // Build sections order map from symbols list.
-  DenseMap<InputSectionBase<ELFT> *, unsigned> SectionsOrder;
+  // Build a map from symbols to their priorities. Symbols that didn't
+  // appear in the symbol ordering file have the lowest priority 0.
+  // All explicitly mentioned symbols have negative (higher) priorities.
+  DenseMap<StringRef, int> SymbolOrder;
+  int Priority = -Config->SymbolOrderingFile.size();
+  for (StringRef S : Config->SymbolOrderingFile)
+    SymbolOrder.insert({S, Priority++});
+
+  // Build a map from sections to their priorities.
+  DenseMap<InputSectionBase<ELFT> *, int> SectionOrder;
   for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) {
     for (SymbolBody *Body : File->getSymbols()) {
       auto *D = dyn_cast<DefinedRegular<ELFT>>(Body);
       if (!D || !D->Section)
         continue;
-      auto It = Config->SymbolOrderingFile.find(Body->getName());
-      if (It == Config->SymbolOrderingFile.end())
-        continue;
-
-      auto It2 = SectionsOrder.insert({D->Section, It->second});
-      if (!It2.second)
-        It2.first->second = std::min(It->second, It2.first->second);
+      int &Priority = SectionOrder[D->Section];
+      Priority = std::min(Priority, SymbolOrder.lookup(D->getName()));
     }
   }
 
-  for (OutputSectionBase *Base : V)
+  // Sort sections by priority.
+  for (OutputSectionBase *Base : OutputSections)
     if (auto *Sec = dyn_cast<OutputSection<ELFT>>(Base))
-      Sec->sort([&](InputSection<ELFT> *S) {
-        auto It = SectionsOrder.find(S);
-        return It == SectionsOrder.end() ? UINT32_MAX : It->second;
-      });
+      Sec->sort([&](InputSection<ELFT> *S) { return SectionOrder.lookup(S); });
 }
 
 template <class ELFT>




More information about the llvm-commits mailing list