<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, May 22, 2015 at 4:58 PM, Rafael Espindola <span dir="ltr"><<a href="mailto:rafael.espindola@gmail.com" target="_blank">rafael.espindola@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: rafael<br>
Date: Fri May 22 18:58:30 2015<br>
New Revision: 238073<br>
<br>
URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject-3Frev-3D238073-26view-3Drev&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=V5cnWASoqKXZNbR9ZOLm2WkLb6RdoWWSwzpBqi-os7I&s=A8Ea-hPV-QE_QijFvpIy3uxGKDcYIbRzW0z335kbW1Y&e=" target="_blank">http://llvm.org/viewvc/llvm-project?rev=238073&view=rev</a><br>
Log:<br>
Produce a single string table in a ELF .o<br>
<br>
Normally an ELF .o has two string tables, one for symbols, one for section<br>
names.<br>
<br>
With the scheme of naming sections like ".text.foo" where foo is a symbol,<br>
there is a big potential saving in using a single one.<br>
<br>
Building llvm+clang+lld with master and with this patch the results were:<br>
<br>
master:                          193,267,008 bytes<br>
patch:                           186,107,952 bytes<br>
master non unique section names: 183,260,192 bytes<br>
patch non unique section names:  183,118,632 bytes<br>
<br>
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while<br>
still using distinct names for the sections.<br></blockquote><div><br></div><div>When you show a measurement, it is vital to explain what is being measured.</div><div><br></div><div>-- Sean Silva</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Modified:<br>
    llvm/trunk/lib/MC/ELFObjectWriter.cpp<br>
    llvm/trunk/test/MC/ELF/empty.s<br>
    llvm/trunk/test/MC/ELF/strtab-suffix-opt.s<br>
<br>
Modified: llvm/trunk/lib/MC/ELFObjectWriter.cpp<br>
URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_llvm_trunk_lib_MC_ELFObjectWriter.cpp-3Frev-3D238073-26r1-3D238072-26r2-3D238073-26view-3Ddiff&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=V5cnWASoqKXZNbR9ZOLm2WkLb6RdoWWSwzpBqi-os7I&s=lI-eWzf2B9nA1R7qVPViwfiksxYW1-F999yBezCXu48&e=" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/ELFObjectWriter.cpp?rev=238073&r1=238072&r2=238073&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/MC/ELFObjectWriter.cpp (original)<br>
+++ llvm/trunk/lib/MC/ELFObjectWriter.cpp Fri May 22 18:58:30 2015<br>
@@ -107,7 +107,6 @@ class ELFObjectWriter : public MCObjectW<br>
<br>
     llvm::DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>><br>
         Relocations;<br>
-    StringTableBuilder ShStrTabBuilder;<br>
<br>
     /// @}<br>
     /// @name Symbol Table Data<br>
@@ -129,8 +128,8 @@ class ELFObjectWriter : public MCObjectW<br>
     unsigned StringTableIndex;<br>
     // This holds the .symtab section index.<br>
     unsigned SymbolTableIndex;<br>
-<br>
-    unsigned ShstrtabIndex;<br>
+    // This holds the .symtab_shndx section index.<br>
+    unsigned SymtabShndxSectionIndex = 0;<br>
<br>
     // Sections in the order they are to be output in the section table.<br>
     std::vector<MCSectionELF *> SectionTable;<br>
@@ -157,7 +156,6 @@ class ELFObjectWriter : public MCObjectW<br>
       WeakrefUsedInReloc.clear();<br>
       Renames.clear();<br>
       Relocations.clear();<br>
-      ShStrTabBuilder.clear();<br>
       StrTabBuilder.clear();<br>
       FileSymbolData.clear();<br>
       LocalSymbolData.clear();<br>
@@ -224,7 +222,6 @@ class ELFObjectWriter : public MCObjectW<br>
     MCSectionELF *createRelocationSection(MCContext &Ctx,<br>
                                           const MCSectionELF &Sec);<br>
<br>
-    const MCSectionELF *createSectionHeaderStringTable();<br>
     const MCSectionELF *createStringTable(MCContext &Ctx);<br>
<br>
     void ExecutePostLayoutBinding(MCAssembler &Asm,<br>
@@ -261,7 +258,7 @@ class ELFObjectWriter : public MCObjectW<br>
<br>
 unsigned ELFObjectWriter::addToSectionTable(MCSectionELF *Sec) {<br>
   SectionTable.push_back(Sec);<br>
-  ShStrTabBuilder.add(Sec->getSectionName());<br>
+  StrTabBuilder.add(Sec->getSectionName());<br>
   return SectionTable.size();<br>
 }<br>
<br>
@@ -395,8 +392,8 @@ void ELFObjectWriter::writeHeader(const<br>
   Write16(0);<br>
<br>
   // e_shstrndx  = Section # of '.shstrtab'<br>
-  assert(ShstrtabIndex < ELF::SHN_LORESERVE);<br>
-  Write16(ShstrtabIndex);<br>
+  assert(StringTableIndex < ELF::SHN_LORESERVE);<br>
+  Write16(StringTableIndex);<br>
 }<br>
<br>
 uint64_t ELFObjectWriter::SymbolValue(const MCSymbol &Sym,<br>
@@ -541,13 +538,7 @@ void ELFObjectWriter::WriteSymbol(Symbol<br>
 void ELFObjectWriter::writeSymbolTable(MCContext &Ctx,<br>
                                        const MCAsmLayout &Layout,<br>
                                        SectionOffsetsTy &SectionOffsets) {<br>
-  unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;<br>
-<br>
-  // Symbol table<br>
-  MCSectionELF *SymtabSection =<br>
-      Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize, "");<br>
-  SymtabSection->setAlignment(is64Bit() ? 8 : 4);<br>
-  SymbolTableIndex = addToSectionTable(SymtabSection);<br>
+  MCSectionELF *SymtabSection = SectionTable[SymbolTableIndex - 1];<br>
<br>
   // The string table must be emitted first because we need the index<br>
   // into the string table for all the symbol names.<br>
@@ -599,14 +590,14 @@ void ELFObjectWriter::writeSymbolTable(M<br>
   SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd);<br>
<br>
   ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes();<br>
-  if (ShndxIndexes.empty())<br>
+  if (ShndxIndexes.empty()) {<br>
+    assert(SymtabShndxSectionIndex == 0);<br>
     return;<br>
+  }<br>
+  assert(SymtabShndxSectionIndex != 0);<br>
<br>
   SecStart = OS.tell();<br>
-  MCSectionELF *SymtabShndxSection =<br>
-      Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, "");<br>
-  addToSectionTable(SymtabShndxSection);<br>
-  SymtabShndxSection->setAlignment(4);<br>
+  MCSectionELF *SymtabShndxSection = SectionTable[SymtabShndxSectionIndex - 1];<br>
   for (uint32_t Index : ShndxIndexes)<br>
     write(Index);<br>
   SecEnd = OS.tell();<br>
@@ -917,6 +908,14 @@ void ELFObjectWriter::computeSymbolTable<br>
     MCAssembler &Asm, const MCAsmLayout &Layout,<br>
     const SectionIndexMapTy &SectionIndexMap,<br>
     const RevGroupMapTy &RevGroupMap) {<br>
+  MCContext &Ctx = Asm.getContext();<br>
+  // Symbol table<br>
+  unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;<br>
+  MCSectionELF *SymtabSection =<br>
+      Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize, "");<br>
+  SymtabSection->setAlignment(is64Bit() ? 8 : 4);<br>
+  SymbolTableIndex = addToSectionTable(SymtabSection);<br>
+<br>
   // FIXME: Is this the correct place to do this?<br>
   // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?<br>
   if (NeedsGOT) {<br>
@@ -928,6 +927,7 @@ void ELFObjectWriter::computeSymbolTable<br>
   }<br>
<br>
   // Add the data for the symbols.<br>
+  bool HasLargeSectionIndex = false;<br>
   for (const MCSymbol &Symbol : Asm.symbols()) {<br>
     MCSymbolData &SD = Symbol.getData();<br>
<br>
@@ -959,10 +959,13 @@ void ELFObjectWriter::computeSymbolTable<br>
       assert(!Local);<br>
       MSD.SectionIndex = ELF::SHN_COMMON;<br>
     } else if (BaseSymbol->isUndefined()) {<br>
-      if (isSignature && !Used)<br>
+      if (isSignature && !Used) {<br>
         MSD.SectionIndex = RevGroupMap.lookup(&Symbol);<br>
-      else<br>
+        if (MSD.SectionIndex >= ELF::SHN_LORESERVE)<br>
+          HasLargeSectionIndex = true;<br>
+      } else {<br>
         MSD.SectionIndex = ELF::SHN_UNDEF;<br>
+      }<br>
       if (!Used && WeakrefUsed)<br>
         MCELF::SetBinding(SD, ELF::STB_WEAK);<br>
     } else {<br>
@@ -970,6 +973,8 @@ void ELFObjectWriter::computeSymbolTable<br>
         static_cast<const MCSectionELF&>(BaseSymbol->getSection());<br>
       MSD.SectionIndex = SectionIndexMap.lookup(&Section);<br>
       assert(MSD.SectionIndex && "Invalid section index!");<br>
+      if (MSD.SectionIndex >= ELF::SHN_LORESERVE)<br>
+        HasLargeSectionIndex = true;<br>
     }<br>
<br>
     // The @@@ in symbol version is replaced with @ in undefined symbols and @@<br>
@@ -1023,6 +1028,13 @@ void ELFObjectWriter::computeSymbolTable<br>
       ExternalSymbolData.push_back(MSD);<br>
   }<br>
<br>
+  if (HasLargeSectionIndex) {<br>
+    MCSectionELF *SymtabShndxSection =<br>
+        Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, "");<br>
+    SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection);<br>
+    SymtabShndxSection->setAlignment(4);<br>
+  }<br>
+<br>
   for (auto i = Asm.file_names_begin(), e = Asm.file_names_end(); i != e; ++i)<br>
     StrTabBuilder.add(*i);<br>
<br>
@@ -1227,17 +1239,8 @@ void ELFObjectWriter::writeRelocations(c<br>
   }<br>
 }<br>
<br>
-const MCSectionELF *ELFObjectWriter::createSectionHeaderStringTable() {<br>
-  const MCSectionELF *ShstrtabSection = SectionTable[ShstrtabIndex - 1];<br>
-  ShStrTabBuilder.finalize(StringTableBuilder::ELF);<br>
-  OS << ShStrTabBuilder.data();<br>
-  return ShstrtabSection;<br>
-}<br>
-<br>
 const MCSectionELF *ELFObjectWriter::createStringTable(MCContext &Ctx) {<br>
-  MCSectionELF *StrtabSection =<br>
-      Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);<br>
-  StringTableIndex = addToSectionTable(StrtabSection);<br>
+  MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1];<br>
   OS << StrTabBuilder.data();<br>
   return StrtabSection;<br>
 }<br>
@@ -1285,7 +1288,7 @@ void ELFObjectWriter::writeSection(const<br>
       Section.getType() == ELF::SHT_ARM_EXIDX)<br>
     sh_link = SectionIndexMap.lookup(Section.getAssociatedSection());<br>
<br>
-  WriteSecHdrEntry(ShStrTabBuilder.getOffset(Section.getSectionName()),<br>
+  WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getSectionName()),<br>
                    Section.getType(), Section.getFlags(), 0, Offset, Size,<br>
                    sh_link, sh_info, Section.getAlignment(),<br>
                    Section.getEntrySize());<br>
@@ -1328,9 +1331,9 @@ void ELFObjectWriter::writeSectionHeader<br>
 void ELFObjectWriter::WriteObject(MCAssembler &Asm,<br>
                                   const MCAsmLayout &Layout) {<br>
   MCContext &Ctx = Asm.getContext();<br>
-  MCSectionELF *ShstrtabSection =<br>
-      Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0);<br>
-  ShstrtabIndex = addToSectionTable(ShstrtabSection);<br>
+  MCSectionELF *StrtabSection =<br>
+      Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);<br>
+  StringTableIndex = addToSectionTable(StrtabSection);<br>
<br>
   RevGroupMapTy RevGroupMap;<br>
   SectionIndexMapTy SectionIndexMap;<br>
@@ -1425,13 +1428,6 @@ void ELFObjectWriter::WriteObject(MCAsse<br>
     uint64_t SecEnd = OS.tell();<br>
     SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd);<br>
   }<br>
-<br>
-  {<br>
-    uint64_t SecStart = OS.tell();<br>
-    const MCSectionELF *Sec = createSectionHeaderStringTable();<br>
-    uint64_t SecEnd = OS.tell();<br>
-    SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd);<br>
-  }<br>
<br>
   uint64_t NaturalAlignment = is64Bit() ? 8 : 4;<br>
   uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment);<br>
<br>
Modified: llvm/trunk/test/MC/ELF/empty.s<br>
URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_llvm_trunk_test_MC_ELF_empty.s-3Frev-3D238073-26r1-3D238072-26r2-3D238073-26view-3Ddiff&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=V5cnWASoqKXZNbR9ZOLm2WkLb6RdoWWSwzpBqi-os7I&s=ZfrpX6hrI9X2vXf_7ZTZuVMHJhjCX5DSf_oDHHcnVEU&e=" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/empty.s?rev=238073&r1=238072&r2=238073&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/MC/ELF/empty.s (original)<br>
+++ llvm/trunk/test/MC/ELF/empty.s Fri May 22 18:58:30 2015<br>
@@ -11,16 +11,16 @@<br>
 // WINDOWS-NEXT: Arch: x86_64<br>
<br>
 // Test that like gnu as we create text, data and bss by default. Also test<br>
-// that shstrtab, symtab and strtab are listed.<br>
+// that symtab and strtab are listed.<br>
<br>
 // CHECK:        Section {<br>
-// CHECK:          Name: .shstrtab<br>
+// CHECK:          Name: .strtab<br>
 // CHECK-NEXT:     Type: SHT_STRTAB<br>
 // CHECK-NEXT:     Flags [<br>
 // CHECK-NEXT:     ]<br>
 // CHECK-NEXT:     Address: 0x0<br>
 // CHECK-NEXT:     Offset:<br>
-// CHECK-NEXT:     Size: 44<br>
+// CHECK-NEXT:     Size: 34<br>
 // CHECK-NEXT:     Link: 0<br>
 // CHECK-NEXT:     Info: 0<br>
 // CHECK-NEXT:     AddressAlignment: 1<br>
@@ -84,16 +84,3 @@<br>
 // CHECK-NEXT:     AddressAlignment: 8<br>
 // CHECK-NEXT:     EntrySize: 24<br>
 // CHECK-NEXT:   }<br>
-// CHECK:        Section {<br>
-// CHECK:          Name: .strtab<br>
-// CHECK-NEXT:     Type: SHT_STRTAB<br>
-// CHECK-NEXT:     Flags [<br>
-// CHECK-NEXT:     ]<br>
-// CHECK-NEXT:     Address: 0x0<br>
-// CHECK-NEXT:     Offset:<br>
-// CHECK-NEXT:     Size: 1<br>
-// CHECK-NEXT:     Link: 0<br>
-// CHECK-NEXT:     Info: 0<br>
-// CHECK-NEXT:     AddressAlignment: 1<br>
-// CHECK-NEXT:     EntrySize: 0<br>
-// CHECK-NEXT:   }<br>
<br>
Modified: llvm/trunk/test/MC/ELF/strtab-suffix-opt.s<br>
URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_llvm_trunk_test_MC_ELF_strtab-2Dsuffix-2Dopt.s-3Frev-3D238073-26r1-3D238072-26r2-3D238073-26view-3Ddiff&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=V5cnWASoqKXZNbR9ZOLm2WkLb6RdoWWSwzpBqi-os7I&s=dDa4Dlg5DhFUiWueGbYpXSB9w_kUcRuGQqgqSHd0b90&e=" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/strtab-suffix-opt.s?rev=238073&r1=238072&r2=238073&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/MC/ELF/strtab-suffix-opt.s (original)<br>
+++ llvm/trunk/test/MC/ELF/strtab-suffix-opt.s Fri May 22 18:58:30 2015<br>
@@ -16,6 +16,6 @@ foobar:<br>
 .Ltmp3:<br>
        .size   foobar, .Ltmp3-foobar<br>
<br>
-// CHECK:     Name: foobar (1)<br>
-// CHECK:     Name: bar (4)<br>
-// CHECK:     Name: foo (8)<br>
+// CHECK:     Name: foobar (16)<br>
+// CHECK:     Name: bar (19)<br>
+// CHECK:     Name: foo (23)<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>