[lld] r248864 - ELF2: Split SymbolTableSection<ELFT>::writeTo into two smaller functions.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 29 17:32:11 PDT 2015


Author: ruiu
Date: Tue Sep 29 19:32:10 2015
New Revision: 248864

URL: http://llvm.org/viewvc/llvm-project?rev=248864&view=rev
Log:
ELF2: Split SymbolTableSection<ELFT>::writeTo into two smaller functions.

Also added brief comments.

Modified:
    lld/trunk/ELF/OutputSections.cpp
    lld/trunk/ELF/OutputSections.h

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=248864&r1=248863&r2=248864&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Tue Sep 29 19:32:10 2015
@@ -396,40 +396,52 @@ template <class ELFT> void SymbolTableSe
 
   // All symbols with STB_LOCAL binding precede the weak and global symbols.
   // .dynsym only contains global symbols.
-  if (!Config->DiscardAll && !StrTabSec.isDynamic()) {
-    for (const std::unique_ptr<ObjectFileBase> &FileB :
-         Table.getObjectFiles()) {
-      auto &File = cast<ObjectFile<ELFT>>(*FileB);
-      Elf_Sym_Range Syms = File.getLocalSymbols();
-      for (const Elf_Sym &Sym : Syms) {
-        auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
-        ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
-        if (SymName && !shouldKeepInSymtab(*SymName))
-          continue;
-        ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0;
-        ESym->st_size = Sym.st_size;
-        ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
-        uint32_t SecIndex = Sym.st_shndx;
-        uintX_t VA = Sym.st_value;
-        if (SecIndex == SHN_ABS) {
-          ESym->st_shndx = SHN_ABS;
-        } else {
-          if (SecIndex == SHN_XINDEX)
-            SecIndex = File.getObj().getExtendedSymbolTableIndex(
-                &Sym, File.getSymbolTable(), File.getSymbolTableShndx());
-          ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
-          const InputSection<ELFT> *Section = Sections[SecIndex];
-          const OutputSection<ELFT> *Out = Section->getOutputSection();
-          ESym->st_shndx = Out->getSectionIndex();
-          VA += Out->getVA() + Section->getOutputSectionOff();
-        }
-        ESym->st_value = VA;
-        Buf += sizeof(Elf_Sym);
+  if (!Config->DiscardAll && !StrTabSec.isDynamic())
+    writeLocalSymbols(Buf);
+
+  writeGlobalSymbols(Buf);
+}
+
+template <class ELFT>
+void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
+  // Iterate over all input object files to copy their local symbols
+  // to the output symbol table pointed by Buf.
+  for (const std::unique_ptr<ObjectFileBase> &FileB : Table.getObjectFiles()) {
+    auto &File = cast<ObjectFile<ELFT>>(*FileB);
+    Elf_Sym_Range Syms = File.getLocalSymbols();
+    for (const Elf_Sym &Sym : Syms) {
+      auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
+      ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
+      if (SymName && !shouldKeepInSymtab(*SymName))
+        continue;
+      ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0;
+      ESym->st_size = Sym.st_size;
+      ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
+      uint32_t SecIndex = Sym.st_shndx;
+      uintX_t VA = Sym.st_value;
+      if (SecIndex == SHN_ABS) {
+        ESym->st_shndx = SHN_ABS;
+      } else {
+        if (SecIndex == SHN_XINDEX)
+          SecIndex = File.getObj().getExtendedSymbolTableIndex(
+              &Sym, File.getSymbolTable(), File.getSymbolTableShndx());
+        ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
+        const InputSection<ELFT> *Section = Sections[SecIndex];
+        const OutputSection<ELFT> *Out = Section->getOutputSection();
+        ESym->st_shndx = Out->getSectionIndex();
+        VA += Out->getVA() + Section->getOutputSectionOff();
       }
+      ESym->st_value = VA;
+      Buf += sizeof(Elf_Sym);
     }
   }
+}
 
-  for (auto &P : Table.getSymbols()) {
+template <class ELFT>
+void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *&Buf) {
+  // Write the internal symbol table contents to the output symbol table
+  // pointed by Buf.
+  for (const std::pair<StringRef, Symbol *> &P : Table.getSymbols()) {
     StringRef Name = P.first;
     Symbol *Sym = P.second;
     SymbolBody *Body = Sym->Body;

Modified: lld/trunk/ELF/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=248864&r1=248863&r2=248864&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.h (original)
+++ lld/trunk/ELF/OutputSections.h Tue Sep 29 19:32:10 2015
@@ -171,6 +171,9 @@ public:
   unsigned getNumSymbols() const { return NumVisible + 1; }
 
 private:
+  void writeLocalSymbols(uint8_t *&Buf);
+  void writeGlobalSymbols(uint8_t *&Buf);
+
   SymbolTable &Table;
   StringTableSection<ELFT::Is64Bits> &StrTabSec;
   unsigned NumVisible = 0;




More information about the llvm-commits mailing list