Revert "[llvm-objcopy] Add support for large indexes"

Not all build bots have unzip which I used in a test.

This reverts commit 0b1f26d39ea42dd3716b525fbc8c78d8c7bb4479.

llvm-svn: 326941
diff --git a/llvm/tools/llvm-objcopy/Object.cpp b/llvm/tools/llvm-objcopy/Object.cpp
index dc77fed..b3ad732 100644
--- a/llvm/tools/llvm-objcopy/Object.cpp
+++ b/llvm/tools/llvm-objcopy/Object.cpp
@@ -68,10 +68,6 @@
 
 SectionVisitor::~SectionVisitor() {}
 
-void BinarySectionWriter::visit(const SectionIndexSection &Sec) {
-  error("Cannot write symbol section index table '" + Sec.Name + "' ");
-}
-
 void BinarySectionWriter::visit(const SymbolTableSection &Sec) {
   error("Cannot write symbol table '" + Sec.Name + "' out to binary");
 }
@@ -121,29 +117,6 @@
   Visitor.visit(*this);
 }
 
-template <class ELFT>
-void ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) {
-  uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
-  auto *Indexes = reinterpret_cast<typename ELFT::Word *>(Buf);
-  std::copy(std::begin(Sec.Indexes), std::end(Sec.Indexes), Indexes);
-}
-
-void SectionIndexSection::initialize(SectionTableRef SecTable) {
-  Size = 0;
-  setSymTab(SecTable.getSectionOfType<SymbolTableSection>(
-      Link,
-      "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
-      "Link field value " + Twine(Link) + " in section " + Name +
-          " is not a symbol table"));
-  Symbols->setShndxTable(this);
-}
-
-void SectionIndexSection::finalize() { Link = Symbols->Index; }
-
-void SectionIndexSection::accept(SectionVisitor &Visitor) const {
-  Visitor.visit(*this);
-}
-
 static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
   switch (Index) {
   case SHN_ABS:
@@ -162,12 +135,8 @@
   return false;
 }
 
-// Large indexes force us to clarify exactly what this function should do. This
-// function should return the proper value of st_shndx.
 uint16_t Symbol::getShndx() const {
   if (DefinedIn != nullptr) {
-    if (DefinedIn->Index >= SHN_LORESERVE)
-      return SHN_XINDEX;
     return DefinedIn->Index;
   }
   switch (ShndxType) {
@@ -181,7 +150,6 @@
   case SYMBOL_HEXAGON_SCOMMON_2:
   case SYMBOL_HEXAGON_SCOMMON_4:
   case SYMBOL_HEXAGON_SCOMMON_8:
-  case SYMBOL_XINDEX:
     return static_cast<uint16_t>(ShndxType);
   }
   llvm_unreachable("Symbol with invalid ShndxType encountered");
@@ -196,12 +164,12 @@
   Sym.Binding = Bind;
   Sym.Type = Type;
   Sym.DefinedIn = DefinedIn;
-  if (DefinedIn != nullptr)
-    DefinedIn->HasSymbol = true;
-  if (Shndx >= SHN_LORESERVE)
-    Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
-  else
-    Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
+  if (DefinedIn == nullptr) {
+    if (Shndx >= SHN_LORESERVE)
+      Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
+    else
+      Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
+  }
   Sym.Value = Value;
   Sym.Visibility = Visibility;
   Sym.Size = Sz;
@@ -211,9 +179,6 @@
 }
 
 void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) {
-  if (SectionIndexTable == Sec)
-    SectionIndexTable = nullptr;
-
   if (SymbolNames == Sec) {
     error("String table " + SymbolNames->Name +
           " cannot be removed because it is referenced by the symbol table " +
@@ -247,7 +212,6 @@
 
 void SymbolTableSection::initialize(SectionTableRef SecTable) {
   Size = 0;
-
   setStrTab(SecTable.getSectionOfType<StringTableSection>(
       Link,
       "Symbol table has link index of " + Twine(Link) +
@@ -271,17 +235,7 @@
   Info = MaxLocalIndex + 1;
 }
 
-void SymbolTableSection::prepareForLayout() {
-  // Add all potential section indexes before file layout so that the section
-  // index section has the approprite size.
-  if (SectionIndexTable != nullptr) {
-    for (const auto &Sym : Symbols) {
-      if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE)
-        SectionIndexTable->addIndex(Sym->DefinedIn->Index);
-      else
-        SectionIndexTable->addIndex(SHN_UNDEF);
-    }
-  }
+void SymbolTableSection::addSymbolNames() {
   // Add all of our strings to SymbolNames so that SymbolNames has the right
   // size before layout is decided.
   for (auto &Sym : Symbols)
@@ -584,32 +538,12 @@
 void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) {
   const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
   StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
-  ArrayRef<Elf_Word> ShndxData;
 
-  auto Symbols = unwrapOrError(ElfFile.symbols(&Shdr));
-  for (const auto &Sym : Symbols) {
+  for (const auto &Sym : unwrapOrError(ElfFile.symbols(&Shdr))) {
     SectionBase *DefSection = nullptr;
     StringRef Name = unwrapOrError(Sym.getName(StrTabData));
 
-    if (Sym.st_shndx == SHN_XINDEX) {
-      if (SymTab->getShndxTable() == nullptr)
-        error("Symbol '" + Name +
-              "' has index SHN_XINDEX but no SHT_SYMTAB_SHNDX section exists.");
-      if (ShndxData.data() == nullptr) {
-        const Elf_Shdr &ShndxSec =
-            *unwrapOrError(ElfFile.getSection(SymTab->getShndxTable()->Index));
-        ShndxData = unwrapOrError(
-            ElfFile.template getSectionContentsAsArray<Elf_Word>(&ShndxSec));
-        if (ShndxData.size() != Symbols.size())
-          error("Symbol section index table does not have the same number of "
-                "entries as the symbol table.");
-      }
-      auto Index = ShndxData[&Sym - Symbols.begin()];
-      DefSection = Obj.sections().getSection(
-          Index,
-          "Symbol '" + Name + "' is defined in invalid section with index " +
-              Twine(Index));
-    } else if (Sym.st_shndx >= SHN_LORESERVE) {
+    if (Sym.st_shndx >= SHN_LORESERVE) {
       if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) {
         error(
             "Symbol '" + Name +
@@ -649,14 +583,14 @@
   }
 }
 
-SectionBase *SectionTableRef::getSection(uint32_t Index, Twine ErrMsg) {
+SectionBase *SectionTableRef::getSection(uint16_t Index, Twine ErrMsg) {
   if (Index == SHN_UNDEF || Index > Sections.size())
     error(ErrMsg);
   return Sections[Index - 1].get();
 }
 
 template <class T>
-T *SectionTableRef::getSectionOfType(uint32_t Index, Twine IndexErrMsg,
+T *SectionTableRef::getSectionOfType(uint16_t Index, Twine IndexErrMsg,
                                      Twine TypeErrMsg) {
   if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg)))
     return Sec;
@@ -700,11 +634,6 @@
     Obj.SymbolTable = &SymTab;
     return SymTab;
   }
-  case SHT_SYMTAB_SHNDX: {
-    auto &ShndxSection = Obj.addSection<SectionIndexSection>();
-    Obj.SectionIndexTable = &ShndxSection;
-    return ShndxSection;
-  }
   case SHT_NOBITS:
     return Obj.addSection<Section>(Data);
   default:
@@ -735,10 +664,6 @@
     Sec.Index = Index++;
   }
 
-  // If we have a SectionIndexTable we need to initialize it before the symbol
-  // table because the symbol table will need it to properly read in symbols.
-  if (Obj.SectionIndexTable)
-    Obj.SectionIndexTable->initialize(Obj.sections());
   // Now that all of the sections have been added we can fill out some extra
   // details about symbol tables. We need the symbol table filled out before
   // any relocations.
@@ -779,16 +704,12 @@
   readSectionHeaders();
   readProgramHeaders();
 
-  uint32_t ShstrIndex = Ehdr.e_shstrndx;
-  if (ShstrIndex == SHN_XINDEX)
-    ShstrIndex = unwrapOrError(ElfFile.getSection(0))->sh_link;
-
   Obj.SectionNames =
       Obj.sections().template getSectionOfType<StringTableSection>(
-          ShstrIndex,
-          "e_shstrndx field value " + Twine(ShstrIndex) +
+          Ehdr.e_shstrndx,
+          "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
               " in elf header " + " is invalid",
-          "e_shstrndx field value " + Twine(ShstrIndex) +
+          "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
               " in elf header " + " is not a string table");
 }
 
@@ -859,27 +780,8 @@
   Ehdr.e_shentsize = sizeof(Elf_Shdr);
   if (WriteSectionHeaders) {
     Ehdr.e_shoff = Obj.SHOffset;
-    // """
-    // If the number of sections is greater than or equal to
-    // SHN_LORESERVE (0xff00), this member has the value zero and the actual
-    // number of section header table entries is contained in the sh_size field
-    // of the section header at index 0.
-    // """
-    auto Shnum = size(Obj.sections()) + 1;
-    if (Shnum >= SHN_LORESERVE)
-      Ehdr.e_shnum = 0;
-    else
-      Ehdr.e_shnum = Shnum;
-    // """
-    // If the section name string table section index is greater than or equal
-    // to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff)
-    // and the actual index of the section name string table section is
-    // contained in the sh_link field of the section header at index 0.
-    // """
-    if (Obj.SectionNames->Index >= SHN_LORESERVE)
-      Ehdr.e_shstrndx = SHN_XINDEX;
-    else
-      Ehdr.e_shstrndx = Obj.SectionNames->Index;
+    Ehdr.e_shnum = size(Obj.sections()) + 1;
+    Ehdr.e_shstrndx = Obj.SectionNames->Index;
   } else {
     Ehdr.e_shoff = 0;
     Ehdr.e_shnum = 0;
@@ -902,17 +804,8 @@
   Shdr.sh_flags = 0;
   Shdr.sh_addr = 0;
   Shdr.sh_offset = 0;
-  // See writeEhdr for why we do this.
-  auto Shnum = size(Obj.sections()) + 1;
-  if (Shnum >= SHN_LORESERVE) {
-    Shdr.sh_size = Shnum;
-  } else
-    Shdr.sh_size = 0;
-  // See writeEhdr for why we do this.
-  if (Obj.SectionNames != nullptr && Obj.SectionNames->Index >= SHN_LORESERVE) {
-    Shdr.sh_link = Obj.SectionNames->Index;
-  } else
-    Shdr.sh_link = 0;
+  Shdr.sh_size = 0;
+  Shdr.sh_link = 0;
   Shdr.sh_info = 0;
   Shdr.sh_addralign = 0;
   Shdr.sh_entsize = 0;
@@ -940,10 +833,9 @@
       });
   if (SymbolTable != nullptr && ToRemove(*SymbolTable))
     SymbolTable = nullptr;
-  if (SectionNames != nullptr && ToRemove(*SectionNames))
+  if (SectionNames != nullptr && ToRemove(*SectionNames)) {
     SectionNames = nullptr;
-  if (SectionIndexTable != nullptr && ToRemove(*SectionIndexTable))
-    SectionIndexTable = nullptr;
+  }
   // Now make sure there are no remaining references to the sections that will
   // be removed. Sometimes it is impossible to remove a reference so we emit
   // an error here instead.
@@ -1105,57 +997,16 @@
     error("Cannot write section header table because section header string "
           "table was removed.");
 
-  Obj.sortSections();
-  // We need to assign indexes before we perform layout because we need to know
-  // if we need large indexes or not. We can assign indexes first and check as
-  // we go to see if we will actully need large indexes.
-  bool NeedsLargeIndexes = false;
-  if (size(Obj.sections()) >= SHN_LORESERVE) {
-    uint64_t Index = SHN_LORESERVE;
-    auto Sections = Obj.sections();
-    auto LargeIndexSections =
-        make_range(Sections.begin() + SHN_LORESERVE, Sections.end());
-    for (auto &Sec : LargeIndexSections) {
-      Sec.Index = Index++;
-      if (Sec.HasSymbol) {
-        NeedsLargeIndexes = true;
-        break;
-      }
-    }
-    // TODO: handle case where only one section needs the large index table but
-    // only needs it because the large index table hasn't been removed yet.
-  }
-
-  if (NeedsLargeIndexes) {
-    // This means we definitely need to have a section index table but if
-    // already have one then we should use it instead of making a new one.
-    if (Obj.SymbolTable != nullptr && Obj.SectionIndexTable == nullptr) {
-      auto &Shndx = Obj.addSection<SectionIndexSection>();
-      Obj.SymbolTable->setShndxTable(&Shndx);
-      Shndx.setSymTab(Obj.SymbolTable);
-    }
-  } else {
-    // Since we don't need SectionIndexTable we should remove it and all
-    // references to it.
-    if (Obj.SectionIndexTable != nullptr) {
-      Obj.removeSections(
-          [](const SectionBase &Sec) { return Sec.Type == SHT_SYMTAB_SHNDX; });
-    }
-  }
-
-  // Make sure we add the names of all the sections. Importantly this must be
-  // done after we decide to add or remove SectionIndexes.
+  // Make sure we add the names of all the sections.
   if (Obj.SectionNames != nullptr)
     for (const auto &Section : Obj.sections()) {
       Obj.SectionNames->addString(Section.Name);
     }
-
-  // The symbol table does not update all other sections on update. For
-  // instance symbol names are not added as new symbols are added. This means
-  // that some sections, like .strtab, don't yet have their final size.
+  // Make sure we add the names of all the symbols.
   if (Obj.SymbolTable != nullptr)
-    Obj.SymbolTable->prepareForLayout();
+    Obj.SymbolTable->addSymbolNames();
 
+  Obj.sortSections();
   assignOffsets();
 
   // Finalize SectionNames first so that we can assign name indexes.