[llvm] r323465 - Reverting r323463 as it appears to be an accidental commit. Regardless, it broke a lot of build bots, so reverting back to green.

Aaron Ballman via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 25 13:03:39 PST 2018


Author: aaronballman
Date: Thu Jan 25 13:03:38 2018
New Revision: 323465

URL: http://llvm.org/viewvc/llvm-project?rev=323465&view=rev
Log:
Reverting r323463 as it appears to be an accidental commit. Regardless, it broke a lot of build bots, so reverting back to green.

http://lab.llvm.org:8011/builders/lldb-amd64-ninja-netbsd8/builds/9294
http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/builds/24084
http://lab.llvm.org:8011/builders/clang-ppc64le-linux-lnt/builds/9567

Removed:
    llvm/trunk/test/tools/llvm-objcopy/Inputs/alloc-symtab.o
    llvm/trunk/test/tools/llvm-objcopy/binary-out-error.test
Modified:
    llvm/trunk/test/tools/llvm-objcopy/remove-shstrtab-error.test
    llvm/trunk/tools/llvm-objcopy/Object.cpp
    llvm/trunk/tools/llvm-objcopy/Object.h
    llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp
    llvm/trunk/tools/llvm-objcopy/llvm-objcopy.h

Removed: llvm/trunk/test/tools/llvm-objcopy/Inputs/alloc-symtab.o
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/Inputs/alloc-symtab.o?rev=323464&view=auto
==============================================================================
Binary files llvm/trunk/test/tools/llvm-objcopy/Inputs/alloc-symtab.o (original) and llvm/trunk/test/tools/llvm-objcopy/Inputs/alloc-symtab.o (removed) differ

Removed: llvm/trunk/test/tools/llvm-objcopy/binary-out-error.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/binary-out-error.test?rev=323464&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/binary-out-error.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/binary-out-error.test (removed)
@@ -1,2 +0,0 @@
-# RUN: not llvm-objcopy -O binary %p/Inputs/alloc-symtab.o %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=SYMTAB
-# SYMTAB: Cannot write symbol table '.symtab' out to binary

Modified: llvm/trunk/test/tools/llvm-objcopy/remove-shstrtab-error.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/remove-shstrtab-error.test?rev=323465&r1=323464&r2=323465&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/remove-shstrtab-error.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/remove-shstrtab-error.test Thu Jan 25 13:03:38 2018
@@ -8,4 +8,4 @@ FileHeader:
   Type:            ET_REL
   Machine:         EM_X86_64
 
-# CHECK: Cannot write section header table because section header string table was removed.
+# CHECK: Cannot remove .shstrtab because it is the section header string table.

Modified: llvm/trunk/tools/llvm-objcopy/Object.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/Object.cpp?rev=323465&r1=323464&r2=323465&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/Object.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/Object.cpp Thu Jan 25 13:03:38 2018
@@ -30,73 +30,61 @@ using namespace llvm;
 using namespace object;
 using namespace ELF;
 
-template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) {
+template <class ELFT> void Segment::writeHeader(FileOutputBuffer &Out) const {
   using Elf_Ehdr = typename ELFT::Ehdr;
   using Elf_Phdr = typename ELFT::Phdr;
 
-  uint8_t *Buf = BufPtr->getBufferStart();
-  Buf += sizeof(Elf_Ehdr) + Seg.Index * sizeof(Elf_Phdr);
+  uint8_t *Buf = Out.getBufferStart();
+  Buf += sizeof(Elf_Ehdr) + Index * sizeof(Elf_Phdr);
   Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(Buf);
-  Phdr.p_type = Seg.Type;
-  Phdr.p_flags = Seg.Flags;
-  Phdr.p_offset = Seg.Offset;
-  Phdr.p_vaddr = Seg.VAddr;
-  Phdr.p_paddr = Seg.PAddr;
-  Phdr.p_filesz = Seg.FileSize;
-  Phdr.p_memsz = Seg.MemSize;
-  Phdr.p_align = Seg.Align;
+  Phdr.p_type = Type;
+  Phdr.p_flags = Flags;
+  Phdr.p_offset = Offset;
+  Phdr.p_vaddr = VAddr;
+  Phdr.p_paddr = PAddr;
+  Phdr.p_filesz = FileSize;
+  Phdr.p_memsz = MemSize;
+  Phdr.p_align = Align;
+}
+
+void Segment::writeSegment(FileOutputBuffer &Out) const {
+  uint8_t *Buf = Out.getBufferStart() + Offset;
+  // We want to maintain segments' interstitial data and contents exactly.
+  // This lets us just copy segments directly.
+  std::copy(std::begin(Contents), std::end(Contents), Buf);
 }
 
 void SectionBase::removeSectionReferences(const SectionBase *Sec) {}
 void SectionBase::initialize(SectionTableRef SecTable) {}
 void SectionBase::finalize() {}
 
-template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) {
-  uint8_t *Buf = BufPtr->getBufferStart();
-  Buf += Sec.HeaderOffset;
+template <class ELFT>
+void SectionBase::writeHeader(FileOutputBuffer &Out) const {
+  uint8_t *Buf = Out.getBufferStart();
+  Buf += HeaderOffset;
   typename ELFT::Shdr &Shdr = *reinterpret_cast<typename ELFT::Shdr *>(Buf);
-  Shdr.sh_name = Sec.NameIndex;
-  Shdr.sh_type = Sec.Type;
-  Shdr.sh_flags = Sec.Flags;
-  Shdr.sh_addr = Sec.Addr;
-  Shdr.sh_offset = Sec.Offset;
-  Shdr.sh_size = Sec.Size;
-  Shdr.sh_link = Sec.Link;
-  Shdr.sh_info = Sec.Info;
-  Shdr.sh_addralign = Sec.Align;
-  Shdr.sh_entsize = Sec.EntrySize;
-}
-
-SectionVisitor::~SectionVisitor() {}
-
-void BinarySectionWriter::visit(const SymbolTableSection &Sec) {
-  error("Cannot write symbol table '" + Sec.Name + "' out to binary");
-}
-
-void BinarySectionWriter::visit(const RelocationSection &Sec) {
-  error("Cannot write relocation section '" + Sec.Name + "' out to binary");
+  Shdr.sh_name = NameIndex;
+  Shdr.sh_type = Type;
+  Shdr.sh_flags = Flags;
+  Shdr.sh_addr = Addr;
+  Shdr.sh_offset = Offset;
+  Shdr.sh_size = Size;
+  Shdr.sh_link = Link;
+  Shdr.sh_info = Info;
+  Shdr.sh_addralign = Align;
+  Shdr.sh_entsize = EntrySize;
 }
 
-void BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) {
-  error("Cannot write '.gnu_debuglink' out to binary");
-}
-
-void SectionWriter::visit(const Section &Sec) {
-  if (Sec.Type == SHT_NOBITS)
+void Section::writeSection(FileOutputBuffer &Out) const {
+  if (Type == SHT_NOBITS)
     return;
-  uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
-  std::copy(std::begin(Sec.Contents), std::end(Sec.Contents), Buf);
+  uint8_t *Buf = Out.getBufferStart() + Offset;
+  std::copy(std::begin(Contents), std::end(Contents), Buf);
 }
 
-void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); }
-
-void SectionWriter::visit(const OwnedDataSection &Sec) {
-  uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
-  std::copy(std::begin(Sec.Data), std::end(Sec.Data), Buf);
-}
-
-void OwnedDataSection::accept(SectionVisitor &Visitor) const {
-  Visitor.visit(*this);
+void OwnedDataSection::writeSection(FileOutputBuffer &Out) const {
+  uint8_t *Buf = Out.getBufferStart() + Offset;
+  std::copy(std::begin(Data), std::end(Data), Buf);
 }
 
 void StringTableSection::addString(StringRef Name) {
@@ -110,12 +98,8 @@ uint32_t StringTableSection::findIndex(S
 
 void StringTableSection::finalize() { StrTabBuilder.finalize(); }
 
-void SectionWriter::visit(const StringTableSection &Sec) {
-  Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset);
-}
-
-void StringTableSection::accept(SectionVisitor &Visitor) const {
-  Visitor.visit(*this);
+void StringTableSection::writeSection(FileOutputBuffer &Out) const {
+  StrTabBuilder.write(Out.getBufferStart() + Offset);
 }
 
 static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
@@ -250,12 +234,12 @@ const Symbol *SymbolTableSection::getSym
 }
 
 template <class ELFT>
-void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
+void SymbolTableSectionImpl<ELFT>::writeSection(FileOutputBuffer &Out) const {
   uint8_t *Buf = Out.getBufferStart();
-  Buf += Sec.Offset;
+  Buf += Offset;
   typename ELFT::Sym *Sym = reinterpret_cast<typename ELFT::Sym *>(Buf);
   // Loop though symbols setting each entry of the symbol table.
-  for (auto &Symbol : Sec.Symbols) {
+  for (auto &Symbol : Symbols) {
     Sym->st_name = Symbol->NameIndex;
     Sym->st_value = Symbol->Value;
     Sym->st_size = Symbol->Size;
@@ -267,10 +251,6 @@ void ELFSectionWriter<ELFT>::visit(const
   }
 }
 
-void SymbolTableSection::accept(SectionVisitor &Visitor) const {
-  Visitor.visit(*this);
-}
-
 template <class SymTabType>
 void RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences(
     const SectionBase *Sec) {
@@ -314,8 +294,9 @@ void setAddend(Elf_Rel_Impl<ELFT, true>
   Rela.r_addend = Addend;
 }
 
-template <class RelRange, class T>
-void writeRel(const RelRange &Relocations, T *Buf) {
+template <class ELFT>
+template <class T>
+void RelocationSection<ELFT>::writeRel(T *Buf) const {
   for (const auto &Reloc : Relocations) {
     Buf->r_offset = Reloc.Offset;
     setAddend(*Buf, Reloc.Addend);
@@ -325,25 +306,17 @@ void writeRel(const RelRange &Relocation
 }
 
 template <class ELFT>
-void ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) {
-  uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
-  if (Sec.Type == SHT_REL)
-    writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf));
+void RelocationSection<ELFT>::writeSection(FileOutputBuffer &Out) const {
+  uint8_t *Buf = Out.getBufferStart() + Offset;
+  if (Type == SHT_REL)
+    writeRel(reinterpret_cast<Elf_Rel *>(Buf));
   else
-    writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf));
+    writeRel(reinterpret_cast<Elf_Rela *>(Buf));
 }
 
-void RelocationSection::accept(SectionVisitor &Visitor) const {
-  Visitor.visit(*this);
-}
-
-void SectionWriter::visit(const DynamicRelocationSection &Sec) {
-  std::copy(std::begin(Sec.Contents), std::end(Sec.Contents),
-            Out.getBufferStart() + Sec.Offset);
-}
-
-void DynamicRelocationSection::accept(SectionVisitor &Visitor) const {
-  Visitor.visit(*this);
+void DynamicRelocationSection::writeSection(FileOutputBuffer &Out) const {
+  std::copy(std::begin(Contents), std::end(Contents),
+            Out.getBufferStart() + Offset);
 }
 
 void SectionWithStrTab::removeSectionReferences(const SectionBase *Sec) {
@@ -371,7 +344,8 @@ void SectionWithStrTab::initialize(Secti
 
 void SectionWithStrTab::finalize() { this->Link = StrTab->Index; }
 
-void GnuDebugLinkSection::init(StringRef File, StringRef Data) {
+template <class ELFT>
+void GnuDebugLinkSection<ELFT>::init(StringRef File, StringRef Data) {
   FileName = sys::path::stem(File);
   // The format for the .gnu_debuglink starts with the stemmed file name and is
   // followed by a null terminator and then the CRC32 of the file. The CRC32
@@ -394,7 +368,9 @@ void GnuDebugLinkSection::init(StringRef
   CRC32 = ~crc.getCRC();
 }
 
-GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) : FileName(File) {
+template <class ELFT>
+GnuDebugLinkSection<ELFT>::GnuDebugLinkSection(StringRef File)
+    : FileName(File) {
   // Read in the file to compute the CRC of it.
   auto DebugOrErr = MemoryBuffer::getFile(File);
   if (!DebugOrErr)
@@ -404,17 +380,12 @@ GnuDebugLinkSection::GnuDebugLinkSection
 }
 
 template <class ELFT>
-void ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) {
-  auto Buf = Out.getBufferStart() + Sec.Offset;
+void GnuDebugLinkSection<ELFT>::writeSection(FileOutputBuffer &Out) const {
+  auto Buf = Out.getBufferStart() + Offset;
   char *File = reinterpret_cast<char *>(Buf);
-  Elf_Word *CRC =
-      reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word));
-  *CRC = Sec.CRC32;
-  std::copy(std::begin(Sec.FileName), std::end(Sec.FileName), File);
-}
-
-void GnuDebugLinkSection::accept(SectionVisitor &Visitor) const {
-  Visitor.visit(*this);
+  Elf_Word *CRC = reinterpret_cast<Elf_Word *>(Buf + Size - sizeof(Elf_Word));
+  *CRC = CRC32;
+  std::copy(std::begin(FileName), std::end(FileName), File);
 }
 
 // Returns true IFF a section is wholly inside the range of a segment
@@ -456,12 +427,14 @@ static bool compareSegmentsByPAddr(const
   return A->Index < B->Index;
 }
 
-template <class ELFT> void ELFBuilder<ELFT>::readProgramHeaders() {
+template <class ELFT>
+void Object<ELFT>::readProgramHeaders(const ELFFile<ELFT> &ElfFile) {
   uint32_t Index = 0;
   for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
     ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset,
                            (size_t)Phdr.p_filesz};
-    Segment &Seg = Obj.addSegment(Data);
+    Segments.emplace_back(llvm::make_unique<Segment>(Data));
+    Segment &Seg = *Segments.back();
     Seg.Type = Phdr.p_type;
     Seg.Flags = Phdr.p_flags;
     Seg.OriginalOffset = Phdr.p_offset;
@@ -472,29 +445,29 @@ template <class ELFT> void ELFBuilder<EL
     Seg.MemSize = Phdr.p_memsz;
     Seg.Align = Phdr.p_align;
     Seg.Index = Index++;
-    for (auto &Section : Obj.sections()) {
-      if (sectionWithinSegment(Section, Seg)) {
-        Seg.addSection(&Section);
-        if (!Section.ParentSegment ||
-            Section.ParentSegment->Offset > Seg.Offset) {
-          Section.ParentSegment = &Seg;
+    for (auto &Section : Sections) {
+      if (sectionWithinSegment(*Section, Seg)) {
+        Seg.addSection(&*Section);
+        if (!Section->ParentSegment ||
+            Section->ParentSegment->Offset > Seg.Offset) {
+          Section->ParentSegment = &Seg;
         }
       }
     }
   }
   // Now we do an O(n^2) loop through the segments in order to match up
   // segments.
-  for (auto &Child : Obj.segments()) {
-    for (auto &Parent : Obj.segments()) {
+  for (auto &Child : Segments) {
+    for (auto &Parent : Segments) {
       // Every segment will overlap with itself but we don't want a segment to
       // be it's own parent so we avoid that situation.
-      if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) {
+      if (&Child != &Parent && segmentOverlapsSegment(*Child, *Parent)) {
         // We want a canonical "most parental" segment but this requires
         // inspecting the ParentSegment.
-        if (compareSegmentsByOffset(&Parent, &Child))
-          if (Child.ParentSegment == nullptr ||
-              compareSegmentsByOffset(&Parent, Child.ParentSegment)) {
-            Child.ParentSegment = &Parent;
+        if (compareSegmentsByOffset(Parent.get(), Child.get()))
+          if (Child->ParentSegment == nullptr ||
+              compareSegmentsByOffset(Parent.get(), Child->ParentSegment)) {
+            Child->ParentSegment = Parent.get();
           }
       }
     }
@@ -502,7 +475,9 @@ template <class ELFT> void ELFBuilder<EL
 }
 
 template <class ELFT>
-void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) {
+void Object<ELFT>::initSymbolTable(const object::ELFFile<ELFT> &ElfFile,
+                                   SymbolTableSection *SymTab,
+                                   SectionTableRef SecTable) {
   const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
   StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
 
@@ -511,14 +486,14 @@ void ELFBuilder<ELFT>::initSymbolTable(S
     StringRef Name = unwrapOrError(Sym.getName(StrTabData));
 
     if (Sym.st_shndx >= SHN_LORESERVE) {
-      if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) {
+      if (!isValidReservedSectionIndex(Sym.st_shndx, Machine)) {
         error(
             "Symbol '" + Name +
             "' has unsupported value greater than or equal to SHN_LORESERVE: " +
             Twine(Sym.st_shndx));
       }
     } else if (Sym.st_shndx != SHN_UNDEF) {
-      DefSection = Obj.sections().getSection(
+      DefSection = SecTable.getSection(
           Sym.st_shndx,
           "Symbol '" + Name + "' is defined in invalid section with index " +
               Twine(Sym.st_shndx));
@@ -537,9 +512,9 @@ static void getAddend(uint64_t &ToSet, c
   ToSet = Rela.r_addend;
 }
 
-template <class T>
-void initRelocations(RelocationSection *Relocs, SymbolTableSection *SymbolTable,
-                     T RelRange) {
+template <class ELFT, class T>
+void initRelocations(RelocationSection<ELFT> *Relocs,
+                     SymbolTableSection *SymbolTable, T RelRange) {
   for (const auto &Rel : RelRange) {
     Relocation ToAdd;
     ToAdd.Offset = Rel.r_offset;
@@ -565,190 +540,147 @@ T *SectionTableRef::getSectionOfType(uin
 }
 
 template <class ELFT>
-SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
+std::unique_ptr<SectionBase>
+Object<ELFT>::makeSection(const object::ELFFile<ELFT> &ElfFile,
+                          const Elf_Shdr &Shdr) {
   ArrayRef<uint8_t> Data;
   switch (Shdr.sh_type) {
   case SHT_REL:
   case SHT_RELA:
     if (Shdr.sh_flags & SHF_ALLOC) {
       Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
-      return Obj.addSection<DynamicRelocationSection>(Data);
+      return llvm::make_unique<DynamicRelocationSection>(Data);
     }
-    return Obj.addSection<RelocationSection>();
+    return llvm::make_unique<RelocationSection<ELFT>>();
   case SHT_STRTAB:
     // If a string table is allocated we don't want to mess with it. That would
     // mean altering the memory image. There are no special link types or
     // anything so we can just use a Section.
     if (Shdr.sh_flags & SHF_ALLOC) {
       Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
-      return Obj.addSection<Section>(Data);
+      return llvm::make_unique<Section>(Data);
     }
-    return Obj.addSection<StringTableSection>();
+    return llvm::make_unique<StringTableSection>();
   case SHT_HASH:
   case SHT_GNU_HASH:
     // Hash tables should refer to SHT_DYNSYM which we're not going to change.
     // Because of this we don't need to mess with the hash tables either.
     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
-    return Obj.addSection<Section>(Data);
+    return llvm::make_unique<Section>(Data);
   case SHT_DYNSYM:
     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
-    return Obj.addSection<DynamicSymbolTableSection>(Data);
+    return llvm::make_unique<DynamicSymbolTableSection>(Data);
   case SHT_DYNAMIC:
     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
-    return Obj.addSection<DynamicSection>(Data);
+    return llvm::make_unique<DynamicSection>(Data);
   case SHT_SYMTAB: {
-    auto &SymTab = Obj.addSection<SymbolTableSection>();
-    Obj.SymbolTable = &SymTab;
-    return SymTab;
+    auto SymTab = llvm::make_unique<SymbolTableSectionImpl<ELFT>>();
+    SymbolTable = SymTab.get();
+    return std::move(SymTab);
   }
   case SHT_NOBITS:
-    return Obj.addSection<Section>(Data);
+    return llvm::make_unique<Section>(Data);
   default:
     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
-    return Obj.addSection<Section>(Data);
+    return llvm::make_unique<Section>(Data);
   }
 }
 
-template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
+template <class ELFT>
+SectionTableRef Object<ELFT>::readSectionHeaders(const ELFFile<ELFT> &ElfFile) {
   uint32_t Index = 0;
   for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
     if (Index == 0) {
       ++Index;
       continue;
     }
-    auto &Sec = makeSection(Shdr);
-    Sec.Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
-    Sec.Type = Shdr.sh_type;
-    Sec.Flags = Shdr.sh_flags;
-    Sec.Addr = Shdr.sh_addr;
-    Sec.Offset = Shdr.sh_offset;
-    Sec.OriginalOffset = Shdr.sh_offset;
-    Sec.Size = Shdr.sh_size;
-    Sec.Link = Shdr.sh_link;
-    Sec.Info = Shdr.sh_info;
-    Sec.Align = Shdr.sh_addralign;
-    Sec.EntrySize = Shdr.sh_entsize;
-    Sec.Index = Index++;
+    SecPtr Sec = makeSection(ElfFile, Shdr);
+    Sec->Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
+    Sec->Type = Shdr.sh_type;
+    Sec->Flags = Shdr.sh_flags;
+    Sec->Addr = Shdr.sh_addr;
+    Sec->Offset = Shdr.sh_offset;
+    Sec->OriginalOffset = Shdr.sh_offset;
+    Sec->Size = Shdr.sh_size;
+    Sec->Link = Shdr.sh_link;
+    Sec->Info = Shdr.sh_info;
+    Sec->Align = Shdr.sh_addralign;
+    Sec->EntrySize = Shdr.sh_entsize;
+    Sec->Index = Index++;
+    Sections.push_back(std::move(Sec));
   }
 
+  SectionTableRef SecTable(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.
-  if (Obj.SymbolTable) {
-    Obj.SymbolTable->initialize(Obj.sections());
-    initSymbolTable(Obj.SymbolTable);
+  if (SymbolTable) {
+    SymbolTable->initialize(SecTable);
+    initSymbolTable(ElfFile, SymbolTable, SecTable);
   }
 
   // Now that all sections and symbols have been added we can add
   // relocations that reference symbols and set the link and info fields for
   // relocation sections.
-  for (auto &Section : Obj.sections()) {
-    if (&Section == Obj.SymbolTable)
+  for (auto &Section : Sections) {
+    if (Section.get() == SymbolTable)
       continue;
-    Section.initialize(Obj.sections());
-    if (auto RelSec = dyn_cast<RelocationSection>(&Section)) {
+    Section->initialize(SecTable);
+    if (auto RelSec = dyn_cast<RelocationSection<ELFT>>(Section.get())) {
       auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
       if (RelSec->Type == SHT_REL)
-        initRelocations(RelSec, Obj.SymbolTable,
-                        unwrapOrError(ElfFile.rels(Shdr)));
+        initRelocations(RelSec, SymbolTable, unwrapOrError(ElfFile.rels(Shdr)));
       else
-        initRelocations(RelSec, Obj.SymbolTable,
+        initRelocations(RelSec, SymbolTable,
                         unwrapOrError(ElfFile.relas(Shdr)));
     }
   }
+
+  return SecTable;
 }
 
-template <class ELFT> void ELFBuilder<ELFT>::build() {
+template <class ELFT> Object<ELFT>::Object(const ELFObjectFile<ELFT> &Obj) {
+  const auto &ElfFile = *Obj.getELFFile();
   const auto &Ehdr = *ElfFile.getHeader();
 
-  std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Obj.Ident);
-  Obj.Type = Ehdr.e_type;
-  Obj.Machine = Ehdr.e_machine;
-  Obj.Version = Ehdr.e_version;
-  Obj.Entry = Ehdr.e_entry;
-  Obj.Flags = Ehdr.e_flags;
-
-  readSectionHeaders();
-  readProgramHeaders();
-
-  Obj.SectionNames =
-      Obj.sections().template getSectionOfType<StringTableSection>(
-          Ehdr.e_shstrndx,
-          "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
-              " in elf header " + " is invalid",
-          "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
-              " in elf header " + " is not a string table");
-}
-
-// A generic size function which computes sizes of any random access range.
-template <class R> size_t size(R &&Range) {
-  return static_cast<size_t>(std::end(Range) - std::begin(Range));
-}
-
-Writer::~Writer() {}
-
-Reader::~Reader() {}
-
-ELFReader::ELFReader(StringRef File) {
-  auto BinaryOrErr = createBinary(File);
-  if (!BinaryOrErr)
-    reportError(File, BinaryOrErr.takeError());
-  auto Bin = std::move(BinaryOrErr.get());
-  std::tie(Binary, Data) = Bin.takeBinary();
-}
-
-ElfType ELFReader::getElfType() const {
-  if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(Binary.get()))
-    return ELFT_ELF32LE;
-  if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(Binary.get()))
-    return ELFT_ELF64LE;
-  if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(Binary.get()))
-    return ELFT_ELF32BE;
-  if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(Binary.get()))
-    return ELFT_ELF64BE;
-  llvm_unreachable("Invalid ELFType");
-}
-
-std::unique_ptr<Object> ELFReader::create() const {
-  auto Obj = llvm::make_unique<Object>(Data);
-  if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(Binary.get())) {
-    ELFBuilder<ELF32LE> Builder(*o, *Obj);
-    Builder.build();
-    return Obj;
-  } else if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(Binary.get())) {
-    ELFBuilder<ELF64LE> Builder(*o, *Obj);
-    Builder.build();
-    return Obj;
-  } else if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(Binary.get())) {
-    ELFBuilder<ELF32BE> Builder(*o, *Obj);
-    Builder.build();
-    return Obj;
-  } else if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(Binary.get())) {
-    ELFBuilder<ELF64BE> Builder(*o, *Obj);
-    Builder.build();
-    return Obj;
-  }
-  error("Invalid file type");
+  std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Ident);
+  Type = Ehdr.e_type;
+  Machine = Ehdr.e_machine;
+  Version = Ehdr.e_version;
+  Entry = Ehdr.e_entry;
+  Flags = Ehdr.e_flags;
+
+  SectionTableRef SecTable = readSectionHeaders(ElfFile);
+  readProgramHeaders(ElfFile);
+
+  SectionNames = SecTable.getSectionOfType<StringTableSection>(
+      Ehdr.e_shstrndx,
+      "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + " in elf header " +
+          " is invalid",
+      "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + " in elf header " +
+          " is not a string table");
 }
 
-template <class ELFT> void ELFWriter<ELFT>::writeEhdr() {
-  uint8_t *Buf = BufPtr->getBufferStart();
+template <class ELFT>
+void Object<ELFT>::writeHeader(FileOutputBuffer &Out) const {
+  uint8_t *Buf = Out.getBufferStart();
   Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(Buf);
-  std::copy(Obj.Ident, Obj.Ident + 16, Ehdr.e_ident);
-  Ehdr.e_type = Obj.Type;
-  Ehdr.e_machine = Obj.Machine;
-  Ehdr.e_version = Obj.Version;
-  Ehdr.e_entry = Obj.Entry;
+  std::copy(Ident, Ident + 16, Ehdr.e_ident);
+  Ehdr.e_type = Type;
+  Ehdr.e_machine = Machine;
+  Ehdr.e_version = Version;
+  Ehdr.e_entry = Entry;
   Ehdr.e_phoff = sizeof(Elf_Ehdr);
-  Ehdr.e_flags = Obj.Flags;
+  Ehdr.e_flags = Flags;
   Ehdr.e_ehsize = sizeof(Elf_Ehdr);
   Ehdr.e_phentsize = sizeof(Elf_Phdr);
-  Ehdr.e_phnum = size(Obj.segments());
+  Ehdr.e_phnum = Segments.size();
   Ehdr.e_shentsize = sizeof(Elf_Shdr);
   if (WriteSectionHeaders) {
-    Ehdr.e_shoff = Obj.SHOffset;
-    Ehdr.e_shnum = size(Obj.sections()) + 1;
-    Ehdr.e_shstrndx = Obj.SectionNames->Index;
+    Ehdr.e_shoff = SHOffset;
+    Ehdr.e_shnum = Sections.size() + 1;
+    Ehdr.e_shstrndx = SectionNames->Index;
   } else {
     Ehdr.e_shoff = 0;
     Ehdr.e_shnum = 0;
@@ -756,13 +688,15 @@ template <class ELFT> void ELFWriter<ELF
   }
 }
 
-template <class ELFT> void ELFWriter<ELFT>::writePhdrs() {
-  for (auto &Seg : Obj.segments())
-    writePhdr(Seg);
+template <class ELFT>
+void Object<ELFT>::writeProgramHeaders(FileOutputBuffer &Out) const {
+  for (auto &Phdr : Segments)
+    Phdr->template writeHeader<ELFT>(Out);
 }
 
-template <class ELFT> void ELFWriter<ELFT>::writeShdrs() {
-  uint8_t *Buf = BufPtr->getBufferStart() + Obj.SHOffset;
+template <class ELFT>
+void Object<ELFT>::writeSectionHeaders(FileOutputBuffer &Out) const {
+  uint8_t *Buf = Out.getBufferStart() + SHOffset;
   // This reference serves to write the dummy section header at the begining
   // of the file. It is not used for anything else
   Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(Buf);
@@ -777,16 +711,19 @@ template <class ELFT> void ELFWriter<ELF
   Shdr.sh_addralign = 0;
   Shdr.sh_entsize = 0;
 
-  for (auto &Sec : Obj.sections())
-    writeShdr(Sec);
+  for (auto &Section : Sections)
+    Section->template writeHeader<ELFT>(Out);
 }
 
-template <class ELFT> void ELFWriter<ELFT>::writeSectionData() {
-  for (auto &Sec : Obj.sections())
-    Sec.accept(*SecWriter);
+template <class ELFT>
+void Object<ELFT>::writeSectionData(FileOutputBuffer &Out) const {
+  for (auto &Section : Sections)
+    Section->writeSection(Out);
 }
 
-void Object::removeSections(std::function<bool(const SectionBase &)> ToRemove) {
+template <class ELFT>
+void Object<ELFT>::removeSections(
+    std::function<bool(const SectionBase &)> ToRemove) {
 
   auto Iter = std::stable_partition(
       std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
@@ -800,7 +737,10 @@ void Object::removeSections(std::functio
       });
   if (SymbolTable != nullptr && ToRemove(*SymbolTable))
     SymbolTable = nullptr;
-  if (SectionNames != nullptr && ToRemove(*SectionNames)) {
+  if (ToRemove(*SectionNames)) {
+    if (WriteSectionHeaders)
+      error("Cannot remove " + SectionNames->Name +
+            " because it is the section header string table.");
     SectionNames = nullptr;
   }
   // Now make sure there are no remaining references to the sections that will
@@ -816,7 +756,18 @@ void Object::removeSections(std::functio
   Sections.erase(Iter, std::end(Sections));
 }
 
-void Object::sortSections() {
+template <class ELFT>
+void Object<ELFT>::addSection(StringRef SecName, ArrayRef<uint8_t> Data) {
+  auto Sec = llvm::make_unique<OwnedDataSection>(SecName, Data);
+  Sec->OriginalOffset = ~0ULL;
+  Sections.push_back(std::move(Sec));
+}
+
+template <class ELFT> void Object<ELFT>::addGnuDebugLink(StringRef File) {
+  Sections.emplace_back(llvm::make_unique<GnuDebugLinkSection<ELFT>>(File));
+}
+
+template <class ELFT> void ELFObject<ELFT>::sortSections() {
   // Put all sections in offset order. Maintain the ordering as closely as
   // possible while meeting that demand however.
   auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
@@ -881,8 +832,8 @@ static uint64_t LayoutSegments(std::vect
 // does not have a ParentSegment. It returns either the offset given if all
 // sections had a ParentSegment or an offset one past the last section if there
 // was a section that didn't have a ParentSegment.
-template <class Range>
-static uint64_t LayoutSections(Range Sections, uint64_t Offset) {
+template <class SecPtr>
+static uint64_t LayoutSections(std::vector<SecPtr> &Sections, uint64_t Offset) {
   // Now the offset of every segment has been set we can assign the offsets
   // of each section. For sections that are covered by a segment we should use
   // the segment's original offset and the section's original offset to compute
@@ -891,28 +842,28 @@ static uint64_t LayoutSections(Range Sec
   // covered by segments we can just bump Offset to the next valid location.
   uint32_t Index = 1;
   for (auto &Section : Sections) {
-    Section.Index = Index++;
-    if (Section.ParentSegment != nullptr) {
-      auto Segment = *Section.ParentSegment;
-      Section.Offset =
-          Segment.Offset + (Section.OriginalOffset - Segment.OriginalOffset);
+    Section->Index = Index++;
+    if (Section->ParentSegment != nullptr) {
+      auto Segment = Section->ParentSegment;
+      Section->Offset =
+          Segment->Offset + (Section->OriginalOffset - Segment->OriginalOffset);
     } else {
-      Offset = alignTo(Offset, Section.Align == 0 ? 1 : Section.Align);
-      Section.Offset = Offset;
-      if (Section.Type != SHT_NOBITS)
-        Offset += Section.Size;
+      Offset = alignTo(Offset, Section->Align == 0 ? 1 : Section->Align);
+      Section->Offset = Offset;
+      if (Section->Type != SHT_NOBITS)
+        Offset += Section->Size;
     }
   }
   return Offset;
 }
 
-template <class ELFT> void ELFWriter<ELFT>::assignOffsets() {
+template <class ELFT> void ELFObject<ELFT>::assignOffsets() {
   // We need a temporary list of segments that has a special order to it
   // so that we know that anytime ->ParentSegment is set that segment has
   // already had its offset properly set.
   std::vector<Segment *> OrderedSegments;
-  for (auto &Segment : Obj.segments())
-    OrderedSegments.push_back(&Segment);
+  for (auto &Segment : this->Segments)
+    OrderedSegments.push_back(Segment.get());
   OrderSegments(OrderedSegments);
   // The size of ELF + program headers will not change so it is ok to assume
   // that the first offset of the first segment is a good place to start
@@ -925,88 +876,72 @@ template <class ELFT> void ELFWriter<ELF
     Offset = sizeof(Elf_Ehdr);
   }
   Offset = LayoutSegments(OrderedSegments, Offset);
-  Offset = LayoutSections(Obj.sections(), Offset);
+  Offset = LayoutSections(this->Sections, Offset);
   // If we need to write the section header table out then we need to align the
   // Offset so that SHOffset is valid.
-  if (WriteSectionHeaders)
+  if (this->WriteSectionHeaders)
     Offset = alignTo(Offset, sizeof(typename ELFT::Addr));
-  Obj.SHOffset = Offset;
+  this->SHOffset = Offset;
 }
 
-template <class ELFT> size_t ELFWriter<ELFT>::totalSize() const {
+template <class ELFT> size_t ELFObject<ELFT>::totalSize() const {
   // We already have the section header offset so we can calculate the total
   // size by just adding up the size of each section header.
-  auto NullSectionSize = WriteSectionHeaders ? sizeof(Elf_Shdr) : 0;
-  return Obj.SHOffset + size(Obj.sections()) * sizeof(Elf_Shdr) +
+  auto NullSectionSize = this->WriteSectionHeaders ? sizeof(Elf_Shdr) : 0;
+  return this->SHOffset + this->Sections.size() * sizeof(Elf_Shdr) +
          NullSectionSize;
 }
 
-template <class ELFT> void ELFWriter<ELFT>::write() {
-  writeEhdr();
-  writePhdrs();
-  writeSectionData();
-  if (WriteSectionHeaders)
-    writeShdrs();
-  if (auto E = BufPtr->commit())
-    reportError(File, errorToErrorCode(std::move(E)));
-}
-
-void Writer::createBuffer(uint64_t Size) {
-  auto BufferOrErr =
-      FileOutputBuffer::create(File, Size, FileOutputBuffer::F_executable);
-  handleAllErrors(BufferOrErr.takeError(), [this](const ErrorInfoBase &) {
-    error("failed to open " + File);
-  });
-  BufPtr = std::move(*BufferOrErr);
-}
-
-template <class ELFT> void ELFWriter<ELFT>::finalize() {
-  // It could happen that SectionNames has been removed and yet the user wants
-  // a section header table output. We need to throw an error if a user tries
-  // to do that.
-  if (Obj.SectionNames == nullptr && WriteSectionHeaders)
-    error("Cannot write section header table because section header string "
-          "table was removed.");
+template <class ELFT> void ELFObject<ELFT>::write(FileOutputBuffer &Out) const {
+  this->writeHeader(Out);
+  this->writeProgramHeaders(Out);
+  this->writeSectionData(Out);
+  if (this->WriteSectionHeaders)
+    this->writeSectionHeaders(Out);
+}
 
+template <class ELFT> void ELFObject<ELFT>::finalize() {
   // 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);
+  if (this->SectionNames != nullptr)
+    for (const auto &Section : this->Sections) {
+      this->SectionNames->addString(Section->Name);
     }
   // Make sure we add the names of all the symbols.
-  if (Obj.SymbolTable != nullptr)
-    Obj.SymbolTable->addSymbolNames();
+  if (this->SymbolTable != nullptr)
+    this->SymbolTable->addSymbolNames();
 
-  Obj.sortSections();
+  sortSections();
   assignOffsets();
 
   // Finalize SectionNames first so that we can assign name indexes.
-  if (Obj.SectionNames != nullptr)
-    Obj.SectionNames->finalize();
+  if (this->SectionNames != nullptr)
+    this->SectionNames->finalize();
   // Finally now that all offsets and indexes have been set we can finalize any
   // remaining issues.
-  uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr);
-  for (auto &Section : Obj.sections()) {
-    Section.HeaderOffset = Offset;
+  uint64_t Offset = this->SHOffset + sizeof(Elf_Shdr);
+  for (auto &Section : this->Sections) {
+    Section->HeaderOffset = Offset;
     Offset += sizeof(Elf_Shdr);
-    if (WriteSectionHeaders)
-      Section.NameIndex = Obj.SectionNames->findIndex(Section.Name);
-    Section.finalize();
+    if (this->WriteSectionHeaders)
+      Section->NameIndex = this->SectionNames->findIndex(Section->Name);
+    Section->finalize();
   }
+}
 
-  createBuffer(totalSize());
-  SecWriter = llvm::make_unique<ELFSectionWriter<ELFT>>(*BufPtr);
+template <class ELFT> size_t BinaryObject<ELFT>::totalSize() const {
+  return TotalSize;
 }
 
-void BinaryWriter::write() {
-  for (auto &Section : Obj.sections()) {
-    if ((Section.Flags & SHF_ALLOC) == 0)
+template <class ELFT>
+void BinaryObject<ELFT>::write(FileOutputBuffer &Out) const {
+  for (auto &Section : this->Sections) {
+    if ((Section->Flags & SHF_ALLOC) == 0)
       continue;
-    Section.accept(*SecWriter);
+    Section->writeSection(Out);
   }
 }
 
-void BinaryWriter::finalize() {
+template <class ELFT> void BinaryObject<ELFT>::finalize() {
   // TODO: Create a filter range to construct OrderedSegments from so that this
   // code can be deduped with assignOffsets above. This should also solve the
   // todo below for LayoutSections.
@@ -1015,9 +950,10 @@ void BinaryWriter::finalize() {
   // already had it's offset properly set. We only want to consider the segments
   // that will affect layout of allocated sections so we only add those.
   std::vector<Segment *> OrderedSegments;
-  for (auto &Section : Obj.sections()) {
-    if ((Section.Flags & SHF_ALLOC) != 0 && Section.ParentSegment != nullptr) {
-      OrderedSegments.push_back(Section.ParentSegment);
+  for (auto &Section : this->Sections) {
+    if ((Section->Flags & SHF_ALLOC) != 0 &&
+        Section->ParentSegment != nullptr) {
+      OrderedSegments.push_back(Section->ParentSegment);
     }
   }
 
@@ -1068,12 +1004,12 @@ void BinaryWriter::finalize() {
   // not hold. Then pass such a range to LayoutSections instead of constructing
   // AllocatedSections here.
   std::vector<SectionBase *> AllocatedSections;
-  for (auto &Section : Obj.sections()) {
-    if ((Section.Flags & SHF_ALLOC) == 0)
+  for (auto &Section : this->Sections) {
+    if ((Section->Flags & SHF_ALLOC) == 0)
       continue;
-    AllocatedSections.push_back(&Section);
+    AllocatedSections.push_back(Section.get());
   }
-  LayoutSections(make_pointee_range(AllocatedSections), Offset);
+  LayoutSections(AllocatedSections, Offset);
 
   // Now that every section has been laid out we just need to compute the total
   // file size. This might not be the same as the offset returned by
@@ -1084,21 +1020,23 @@ void BinaryWriter::finalize() {
     if (Section->Type != SHT_NOBITS)
       TotalSize = std::max(TotalSize, Section->Offset + Section->Size);
   }
-
-  createBuffer(TotalSize);
-  SecWriter = llvm::make_unique<BinarySectionWriter>(*BufPtr);
 }
 
 namespace llvm {
 
-template class ELFBuilder<ELF64LE>;
-template class ELFBuilder<ELF64BE>;
-template class ELFBuilder<ELF32LE>;
-template class ELFBuilder<ELF32BE>;
-
-template class ELFWriter<ELF64LE>;
-template class ELFWriter<ELF64BE>;
-template class ELFWriter<ELF32LE>;
-template class ELFWriter<ELF32BE>;
+template class Object<ELF64LE>;
+template class Object<ELF64BE>;
+template class Object<ELF32LE>;
+template class Object<ELF32BE>;
+
+template class ELFObject<ELF64LE>;
+template class ELFObject<ELF64BE>;
+template class ELFObject<ELF32LE>;
+template class ELFObject<ELF32BE>;
+
+template class BinaryObject<ELF64LE>;
+template class BinaryObject<ELF64BE>;
+template class BinaryObject<ELF32LE>;
+template class BinaryObject<ELF32BE>;
 
 } // end namespace llvm

Modified: llvm/trunk/tools/llvm-objcopy/Object.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/Object.h?rev=323465&r1=323464&r2=323465&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/Object.h (original)
+++ llvm/trunk/tools/llvm-objcopy/Object.h Thu Jan 25 13:03:38 2018
@@ -16,7 +16,6 @@
 #include "llvm/BinaryFormat/ELF.h"
 #include "llvm/MC/StringTableBuilder.h"
 #include "llvm/Object/ELFObjectFile.h"
-#include "llvm/Support/FileOutputBuffer.h"
 #include "llvm/Support/JamCRC.h"
 #include <cstddef>
 #include <cstdint>
@@ -27,159 +26,25 @@
 
 namespace llvm {
 
+class FileOutputBuffer;
 class SectionBase;
-class Section;
-class OwnedDataSection;
-class StringTableSection;
-class SymbolTableSection;
-class RelocationSection;
-class DynamicRelocationSection;
-class GnuDebugLinkSection;
 class Segment;
-class Object;
 
 class SectionTableRef {
 private:
-  MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
+  ArrayRef<std::unique_ptr<SectionBase>> Sections;
 
 public:
-  using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
-
-  SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
+  SectionTableRef(ArrayRef<std::unique_ptr<SectionBase>> Secs)
       : Sections(Secs) {}
   SectionTableRef(const SectionTableRef &) = default;
 
-  iterator begin() { return iterator(Sections.data()); }
-  iterator end() { return iterator(Sections.data() + Sections.size()); }
-
   SectionBase *getSection(uint16_t Index, Twine ErrMsg);
 
   template <class T>
   T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
 };
 
-enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
-
-class SectionVisitor {
-public:
-  virtual ~SectionVisitor();
-
-  virtual void visit(const Section &Sec) = 0;
-  virtual void visit(const OwnedDataSection &Sec) = 0;
-  virtual void visit(const StringTableSection &Sec) = 0;
-  virtual void visit(const SymbolTableSection &Sec) = 0;
-  virtual void visit(const RelocationSection &Sec) = 0;
-  virtual void visit(const DynamicRelocationSection &Sec) = 0;
-  virtual void visit(const GnuDebugLinkSection &Sec) = 0;
-};
-
-class SectionWriter : public SectionVisitor {
-protected:
-  FileOutputBuffer &Out;
-
-public:
-  virtual ~SectionWriter(){};
-
-  void visit(const Section &Sec) override;
-  void visit(const OwnedDataSection &Sec) override;
-  void visit(const StringTableSection &Sec) override;
-  void visit(const DynamicRelocationSection &Sec) override;
-  virtual void visit(const SymbolTableSection &Sec) override = 0;
-  virtual void visit(const RelocationSection &Sec) override = 0;
-  virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
-
-  SectionWriter(FileOutputBuffer &Buf) : Out(Buf) {}
-};
-
-template <class ELFT> class ELFSectionWriter : public SectionWriter {
-private:
-  using Elf_Word = typename ELFT::Word;
-  using Elf_Rel = typename ELFT::Rel;
-  using Elf_Rela = typename ELFT::Rela;
-
-public:
-  virtual ~ELFSectionWriter() {}
-  void visit(const SymbolTableSection &Sec) override;
-  void visit(const RelocationSection &Sec) override;
-  void visit(const GnuDebugLinkSection &Sec) override;
-
-  ELFSectionWriter(FileOutputBuffer &Buf) : SectionWriter(Buf) {}
-};
-
-#define MAKE_SEC_WRITER_FRIEND                                                 \
-  template friend class SectionWriter;                                         \
-  template <class ELFT> friend class ELFSectionWriter;
-
-class BinarySectionWriter : public SectionWriter {
-public:
-  virtual ~BinarySectionWriter() {}
-
-  void visit(const SymbolTableSection &Sec) override;
-  void visit(const RelocationSection &Sec) override;
-  void visit(const GnuDebugLinkSection &Sec) override;
-  BinarySectionWriter(FileOutputBuffer &Buf) : SectionWriter(Buf) {}
-};
-
-class Writer {
-protected:
-  StringRef File;
-  Object &Obj;
-  std::unique_ptr<FileOutputBuffer> BufPtr;
-
-  void createBuffer(uint64_t Size);
-
-public:
-  virtual ~Writer();
-
-  virtual void finalize() = 0;
-  virtual void write() = 0;
-
-  Writer(StringRef File, Object &Obj) : File(File), Obj(Obj) {}
-};
-
-template <class ELFT> class ELFWriter : public Writer {
-private:
-  using Elf_Shdr = typename ELFT::Shdr;
-  using Elf_Phdr = typename ELFT::Phdr;
-  using Elf_Ehdr = typename ELFT::Ehdr;
-
-  void writeEhdr();
-  void writePhdr(const Segment &Seg);
-  void writeShdr(const SectionBase &Sec);
-
-  void writePhdrs();
-  void writeShdrs();
-  void writeSectionData();
-
-  void assignOffsets();
-
-  std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
-
-  size_t totalSize() const;
-
-public:
-  virtual ~ELFWriter() {}
-  bool WriteSectionHeaders = true;
-
-  void finalize() override;
-  void write() override;
-  ELFWriter(StringRef File, Object &Obj, bool WSH)
-      : Writer(File, Obj), WriteSectionHeaders(WSH) {}
-};
-
-class BinaryWriter : public Writer {
-private:
-  std::unique_ptr<BinarySectionWriter> SecWriter;
-
-  uint64_t TotalSize;
-
-public:
-  ~BinaryWriter() {}
-  void finalize() override;
-  void write() override;
-  BinaryWriter(StringRef File, Object &Obj) : Writer(File, Obj) {}
-};
-
 class SectionBase {
 public:
   StringRef Name;
@@ -204,7 +69,8 @@ public:
   virtual void initialize(SectionTableRef SecTable);
   virtual void finalize();
   virtual void removeSectionReferences(const SectionBase *Sec);
-  virtual void accept(SectionVisitor &Visitor) const = 0;
+  template <class ELFT> void writeHeader(FileOutputBuffer &Out) const;
+  virtual void writeSection(FileOutputBuffer &Out) const = 0;
 };
 
 class Segment {
@@ -247,23 +113,21 @@ public:
 
   void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
   void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
+  template <class ELFT> void writeHeader(FileOutputBuffer &Out) const;
+  void writeSegment(FileOutputBuffer &Out) const;
 };
 
 class Section : public SectionBase {
-  MAKE_SEC_WRITER_FRIEND
-
 private:
   ArrayRef<uint8_t> Contents;
 
 public:
   Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
 
-  void accept(SectionVisitor &Visitor) const override;
+  void writeSection(FileOutputBuffer &Out) const override;
 };
 
 class OwnedDataSection : public SectionBase {
-  MAKE_SEC_WRITER_FRIEND
-
 private:
   std::vector<uint8_t> Data;
 
@@ -273,10 +137,8 @@ public:
     Name = SecName;
     Type = ELF::SHT_PROGBITS;
     Size = Data.size();
-    OriginalOffset = std::numeric_limits<uint64_t>::max();
   }
-
-  void accept(SectionVisitor &Sec) const override;
+  void writeSection(FileOutputBuffer &Out) const override;
 };
 
 // There are two types of string tables that can exist, dynamic and not dynamic.
@@ -288,8 +150,6 @@ public:
 // classof method checks that the particular instance is not allocated. This
 // then agrees with the makeSection method used to construct most sections.
 class StringTableSection : public SectionBase {
-  MAKE_SEC_WRITER_FRIEND
-
 private:
   StringTableBuilder StrTabBuilder;
 
@@ -301,7 +161,7 @@ public:
   void addString(StringRef Name);
   uint32_t findIndex(StringRef Name) const;
   void finalize() override;
-  void accept(SectionVisitor &Visitor) const override;
+  void writeSection(FileOutputBuffer &Out) const override;
 
   static bool classof(const SectionBase *S) {
     if (S->Flags & ELF::SHF_ALLOC)
@@ -340,8 +200,6 @@ struct Symbol {
 };
 
 class SymbolTableSection : public SectionBase {
-  MAKE_SEC_WRITER_FRIEND
-
 protected:
   std::vector<std::unique_ptr<Symbol>> Symbols;
   StringTableSection *SymbolNames = nullptr;
@@ -360,13 +218,17 @@ public:
   void localize(std::function<bool(const Symbol &)> ToLocalize);
   void initialize(SectionTableRef SecTable) override;
   void finalize() override;
-  void accept(SectionVisitor &Visitor) const override;
 
   static bool classof(const SectionBase *S) {
     return S->Type == ELF::SHT_SYMTAB;
   }
 };
 
+// Only writeSection depends on the ELF type so we implement it in a subclass.
+template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection {
+  void writeSection(FileOutputBuffer &Out) const override;
+};
+
 struct Relocation {
   const Symbol *RelocSymbol = nullptr;
   uint64_t Offset;
@@ -413,16 +275,20 @@ public:
   void finalize() override;
 };
 
+template <class ELFT>
 class RelocationSection
     : public RelocSectionWithSymtabBase<SymbolTableSection> {
-  MAKE_SEC_WRITER_FRIEND
-
 private:
+  using Elf_Rel = typename ELFT::Rel;
+  using Elf_Rela = typename ELFT::Rela;
+
   std::vector<Relocation> Relocations;
 
+  template <class T> void writeRel(T *Buf) const;
+
 public:
   void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
-  void accept(SectionVisitor &Visitor) const override;
+  void writeSection(FileOutputBuffer &Out) const override;
 
   static bool classof(const SectionBase *S) {
     if (S->Flags & ELF::SHF_ALLOC)
@@ -465,15 +331,13 @@ public:
 
 class DynamicRelocationSection
     : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
-  MAKE_SEC_WRITER_FRIEND
-
 private:
   ArrayRef<uint8_t> Contents;
 
 public:
   DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
 
-  void accept(SectionVisitor &) const override;
+  void writeSection(FileOutputBuffer &Out) const override;
 
   static bool classof(const SectionBase *S) {
     if (!(S->Flags & ELF::SHF_ALLOC))
@@ -482,10 +346,12 @@ public:
   }
 };
 
-class GnuDebugLinkSection : public SectionBase {
-  MAKE_SEC_WRITER_FRIEND
-
+template <class ELFT> class GnuDebugLinkSection : public SectionBase {
 private:
+  // Elf_Word is 4-bytes on every format but has the same endianess as the elf
+  // type ELFT. We'll need to write the CRC32 out in the proper endianess so
+  // we'll make sure to use this type.
+  using Elf_Word = typename ELFT::Word;
 
   StringRef FileName;
   uint32_t CRC32;
@@ -495,101 +361,92 @@ private:
 public:
   // If we add this section from an external source we can use this ctor.
   GnuDebugLinkSection(StringRef File);
-  void accept(SectionVisitor &Visitor) const override;
-};
-
-class Reader {
-public:
-  virtual ~Reader();
-  virtual std::unique_ptr<Object> create() const = 0;
+  void writeSection(FileOutputBuffer &Out) const override;
 };
 
-using object::OwningBinary;
-using object::Binary;
-using object::ELFFile;
-using object::ELFObjectFile;
-
-template <class ELFT> class ELFBuilder {
+template <class ELFT> class Object {
 private:
+  using SecPtr = std::unique_ptr<SectionBase>;
+  using SegPtr = std::unique_ptr<Segment>;
+
   using Elf_Shdr = typename ELFT::Shdr;
+  using Elf_Ehdr = typename ELFT::Ehdr;
+  using Elf_Phdr = typename ELFT::Phdr;
 
-  const ELFFile<ELFT> &ElfFile;
-  Object &Obj;
+  void initSymbolTable(const object::ELFFile<ELFT> &ElfFile,
+                       SymbolTableSection *SymTab, SectionTableRef SecTable);
+  SecPtr makeSection(const object::ELFFile<ELFT> &ElfFile,
+                     const Elf_Shdr &Shdr);
+  void readProgramHeaders(const object::ELFFile<ELFT> &ElfFile);
+  SectionTableRef readSectionHeaders(const object::ELFFile<ELFT> &ElfFile);
 
-  void readProgramHeaders();
-  void initSymbolTable(SymbolTableSection *SymTab);
-  void readSectionHeaders();
-  SectionBase &makeSection(const Elf_Shdr &Shdr);
+protected:
+  StringTableSection *SectionNames = nullptr;
+  SymbolTableSection *SymbolTable = nullptr;
+  std::vector<SecPtr> Sections;
+  std::vector<SegPtr> Segments;
 
-public:
-  ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
-      : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
+  void writeHeader(FileOutputBuffer &Out) const;
+  void writeProgramHeaders(FileOutputBuffer &Out) const;
+  void writeSectionData(FileOutputBuffer &Out) const;
+  void writeSectionHeaders(FileOutputBuffer &Out) const;
 
-  void build();
-};
+public:
+  uint8_t Ident[16];
+  uint64_t Entry;
+  uint64_t SHOffset;
+  uint32_t Type;
+  uint32_t Machine;
+  uint32_t Version;
+  uint32_t Flags;
+  bool WriteSectionHeaders = true;
 
-class ELFReader : public Reader {
-private:
-  std::unique_ptr<Binary> Binary;
-  std::shared_ptr<MemoryBuffer> Data;
+  Object(const object::ELFObjectFile<ELFT> &Obj);
+  virtual ~Object() = default;
 
-public:
-  ElfType getElfType() const;
-  std::unique_ptr<Object> create() const override;
-  ELFReader(StringRef File);
+  SymbolTableSection *getSymTab() const { return SymbolTable; }
+  const SectionBase *getSectionHeaderStrTab() const { return SectionNames; }
+  void removeSections(std::function<bool(const SectionBase &)> ToRemove);
+  void addSection(StringRef SecName, ArrayRef<uint8_t> Data);
+  void addGnuDebugLink(StringRef File);
+  virtual size_t totalSize() const = 0;
+  virtual void finalize() = 0;
+  virtual void write(FileOutputBuffer &Out) const = 0;
 };
 
-class Object {
+template <class ELFT> class ELFObject : public Object<ELFT> {
 private:
   using SecPtr = std::unique_ptr<SectionBase>;
   using SegPtr = std::unique_ptr<Segment>;
 
-  std::shared_ptr<MemoryBuffer> OwnedData;
-  std::vector<SecPtr> Sections;
-  std::vector<SegPtr> Segments;
+  using Elf_Shdr = typename ELFT::Shdr;
+  using Elf_Ehdr = typename ELFT::Ehdr;
+  using Elf_Phdr = typename ELFT::Phdr;
 
-public:
-  template <class T>
-  using Range = iterator_range<
-      pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
+  void sortSections();
+  void assignOffsets();
 
-  template <class T>
-  using ConstRange = iterator_range<pointee_iterator<
-      typename std::vector<std::unique_ptr<T>>::const_iterator>>;
+public:
+  ELFObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
 
-  uint8_t Ident[16];
-  uint64_t Entry;
-  uint64_t SHOffset;
-  uint32_t Type;
-  uint32_t Machine;
-  uint32_t Version;
-  uint32_t Flags;
+  void finalize() override;
+  size_t totalSize() const override;
+  void write(FileOutputBuffer &Out) const override;
+};
 
-  StringTableSection *SectionNames = nullptr;
-  SymbolTableSection *SymbolTable = nullptr;
+template <class ELFT> class BinaryObject : public Object<ELFT> {
+private:
+  using SecPtr = std::unique_ptr<SectionBase>;
+  using SegPtr = std::unique_ptr<Segment>;
 
-  Object(std::shared_ptr<MemoryBuffer> Data) : OwnedData(Data) {}
-  virtual ~Object() = default;
+  uint64_t TotalSize;
 
-  void sortSections();
-  SectionTableRef sections() { return SectionTableRef(Sections); }
-  ConstRange<SectionBase> sections() const {
-    return make_pointee_range(Sections);
-  }
-  Range<Segment> segments() { return make_pointee_range(Segments); }
-  ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
+public:
+  BinaryObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
 
-  void removeSections(std::function<bool(const SectionBase &)> ToRemove);
-  template <class T, class... Ts> T &addSection(Ts &&... Args) {
-    auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
-    auto Ptr = Sec.get();
-    Sections.emplace_back(std::move(Sec));
-    return *Ptr;
-  }
-  Segment &addSegment(ArrayRef<uint8_t> Data) {
-    Segments.emplace_back(llvm::make_unique<Segment>(Data));
-    return *Segments.back();
-  }
+  void finalize() override;
+  size_t totalSize() const override;
+  void write(FileOutputBuffer &Out) const override;
 };
 
 } // end namespace llvm

Modified: llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp?rev=323465&r1=323464&r2=323465&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp Thu Jan 25 13:03:38 2018
@@ -130,42 +130,42 @@ using SectionPred = std::function<bool(c
 
 bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); }
 
-bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
+template <class ELFT>
+bool OnlyKeepDWOPred(const Object<ELFT> &Obj, const SectionBase &Sec) {
   // We can't remove the section header string table.
-  if (&Sec == Obj.SectionNames)
+  if (&Sec == Obj.getSectionHeaderStrTab())
     return false;
   // Short of keeping the string table we want to keep everything that is a DWO
   // section and remove everything else.
   return !IsDWOSection(Sec);
 }
 
-static ElfType OutputElfType;
-
-std::unique_ptr<Writer> CreateWriter(Object &Obj, StringRef File) {
-  if (OutputFormat == "binary") {
-    return llvm::make_unique<BinaryWriter>(OutputFilename, Obj);
-  }
-  // Depending on the initial ELFT and OutputFormat we need a different Writer.
-  switch (OutputElfType) {
-  case ELFT_ELF32LE:
-    return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj, !StripSections);
-  case ELFT_ELF64LE:
-    return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj, !StripSections);
-  case ELFT_ELF32BE:
-    return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj, !StripSections);
-  case ELFT_ELF64BE:
-    return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj, !StripSections);
-  }
-  llvm_unreachable("Invalid output format");
-}
-
-void SplitDWOToFile(const Reader &Reader, StringRef File) {
-  auto DWOFile = Reader.create();
-  DWOFile->removeSections(
-      [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });
-  auto Writer = CreateWriter(*DWOFile, File);
-  Writer->finalize();
-  Writer->write();
+template <class ELFT>
+void WriteObjectFile(const Object<ELFT> &Obj, StringRef File) {
+  std::unique_ptr<FileOutputBuffer> Buffer;
+  Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
+      FileOutputBuffer::create(File, Obj.totalSize(),
+                               FileOutputBuffer::F_executable);
+  handleAllErrors(BufferOrErr.takeError(), [](const ErrorInfoBase &) {
+    error("failed to open " + OutputFilename);
+  });
+  Buffer = std::move(*BufferOrErr);
+
+  Obj.write(*Buffer);
+  if (auto E = Buffer->commit())
+    reportError(File, errorToErrorCode(std::move(E)));
+}
+
+template <class ELFT>
+void SplitDWOToFile(const ELFObjectFile<ELFT> &ObjFile, StringRef File) {
+  // Construct a second output file for the DWO sections.
+  ELFObject<ELFT> DWOFile(ObjFile);
+
+  DWOFile.removeSections([&](const SectionBase &Sec) {
+    return OnlyKeepDWOPred<ELFT>(DWOFile, Sec);
+  });
+  DWOFile.finalize();
+  WriteObjectFile(DWOFile, File);
 }
 
 // This function handles the high level operations of GNU objcopy including
@@ -175,16 +175,23 @@ void SplitDWOToFile(const Reader &Reader
 // any previous removals. Lastly whether or not something is removed shouldn't
 // depend a) on the order the options occur in or b) on some opaque priority
 // system. The only priority is that keeps/copies overrule removes.
-void HandleArgs(Object &Obj, const Reader &Reader) {
+template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) {
+  std::unique_ptr<Object<ELFT>> Obj;
 
-  if (!SplitDWO.empty()) {
-    SplitDWOToFile(Reader, SplitDWO);
-  }
+  if (!OutputFormat.empty() && OutputFormat != "binary")
+    error("invalid output format '" + OutputFormat + "'");
+  if (!OutputFormat.empty() && OutputFormat == "binary")
+    Obj = llvm::make_unique<BinaryObject<ELFT>>(ObjFile);
+  else
+    Obj = llvm::make_unique<ELFObject<ELFT>>(ObjFile);
+
+  if (!SplitDWO.empty())
+    SplitDWOToFile<ELFT>(ObjFile, SplitDWO.getValue());
 
   // Localize:
 
   if (LocalizeHidden) {
-    Obj.SymbolTable->localize([](const Symbol &Sym) {
+    Obj->getSymTab()->localize([](const Symbol &Sym) {
       return Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL;
     });
   }
@@ -207,7 +214,7 @@ void HandleArgs(Object &Obj, const Reade
 
   if (ExtractDWO)
     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
-      return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
+      return OnlyKeepDWOPred(*Obj, Sec) || RemovePred(Sec);
     };
 
   if (StripAllGNU)
@@ -216,7 +223,7 @@ void HandleArgs(Object &Obj, const Reade
         return true;
       if ((Sec.Flags & SHF_ALLOC) != 0)
         return false;
-      if (&Sec == Obj.SectionNames)
+      if (&Sec == Obj->getSectionHeaderStrTab())
         return false;
       switch (Sec.Type) {
       case SHT_SYMTAB:
@@ -232,6 +239,7 @@ void HandleArgs(Object &Obj, const Reade
     RemovePred = [RemovePred](const SectionBase &Sec) {
       return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
     };
+    Obj->WriteSectionHeaders = false;
   }
 
   if (StripDebug) {
@@ -244,7 +252,7 @@ void HandleArgs(Object &Obj, const Reade
     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
       if (RemovePred(Sec))
         return true;
-      if (&Sec == Obj.SectionNames)
+      if (&Sec == Obj->getSectionHeaderStrTab())
         return false;
       return (Sec.Flags & SHF_ALLOC) == 0;
     };
@@ -253,7 +261,7 @@ void HandleArgs(Object &Obj, const Reade
     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
       if (RemovePred(Sec))
         return true;
-      if (&Sec == Obj.SectionNames)
+      if (&Sec == Obj->getSectionHeaderStrTab())
         return false;
       if (Sec.Name.startswith(".gnu.warning"))
         return false;
@@ -270,15 +278,17 @@ void HandleArgs(Object &Obj, const Reade
         return false;
 
       // Allow all implicit removes.
-      if (RemovePred(Sec))
+      if (RemovePred(Sec)) {
         return true;
+      }
 
       // Keep special sections.
-      if (Obj.SectionNames == &Sec)
+      if (Obj->getSectionHeaderStrTab() == &Sec) {
         return false;
-      if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec)
+      }
+      if (Obj->getSymTab() == &Sec || Obj->getSymTab()->getStrTab() == &Sec) {
         return false;
-
+      }
       // Remove everything else.
       return true;
     };
@@ -295,7 +305,7 @@ void HandleArgs(Object &Obj, const Reade
     };
   }
 
-  Obj.removeSections(RemovePred);
+  Obj->removeSections(RemovePred);
 
   if (!AddSection.empty()) {
     for (const auto &Flag : AddSection) {
@@ -308,22 +318,16 @@ void HandleArgs(Object &Obj, const Reade
       auto Buf = std::move(*BufOrErr);
       auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
       auto BufSize = Buf->getBufferSize();
-      Obj.addSection<OwnedDataSection>(SecName,
-                                       ArrayRef<uint8_t>(BufPtr, BufSize));
+      Obj->addSection(SecName, ArrayRef<uint8_t>(BufPtr, BufSize));
     }
   }
 
   if (!AddGnuDebugLink.empty()) {
-    Obj.addSection<GnuDebugLinkSection>(StringRef(AddGnuDebugLink));
+    Obj->addGnuDebugLink(AddGnuDebugLink);
   }
-}
 
-std::unique_ptr<Reader> CreateReader() {
-  // Right now we can only read ELF files so there's only one reader;
-  auto Out = llvm::make_unique<ELFReader>(StringRef(InputFilename));
-  // We need to set the default ElfType for output.
-  OutputElfType = Out->getElfType();
-  return std::move(Out);
+  Obj->finalize();
+  WriteObjectFile(*Obj, OutputFilename.getValue());
 }
 
 int main(int argc, char **argv) {
@@ -337,11 +341,25 @@ int main(int argc, char **argv) {
     cl::PrintHelpMessage();
     return 2;
   }
-
-  auto Reader = CreateReader();
-  auto Obj = Reader->create();
-  auto Writer = CreateWriter(*Obj, OutputFilename);
-  HandleArgs(*Obj, *Reader);
-  Writer->finalize();
-  Writer->write();
+  Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(InputFilename);
+  if (!BinaryOrErr)
+    reportError(InputFilename, BinaryOrErr.takeError());
+  Binary &Binary = *BinaryOrErr.get().getBinary();
+  if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(&Binary)) {
+    CopyBinary(*o);
+    return 0;
+  }
+  if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(&Binary)) {
+    CopyBinary(*o);
+    return 0;
+  }
+  if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(&Binary)) {
+    CopyBinary(*o);
+    return 0;
+  }
+  if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(&Binary)) {
+    CopyBinary(*o);
+    return 0;
+  }
+  reportError(InputFilename, object_error::invalid_file_type);
 }

Modified: llvm/trunk/tools/llvm-objcopy/llvm-objcopy.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/llvm-objcopy.h?rev=323465&r1=323464&r2=323465&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/llvm-objcopy.h (original)
+++ llvm/trunk/tools/llvm-objcopy/llvm-objcopy.h Thu Jan 25 13:03:38 2018
@@ -19,9 +19,6 @@
 namespace llvm {
 
 LLVM_ATTRIBUTE_NORETURN extern void error(Twine Message);
-LLVM_ATTRIBUTE_NORETURN extern void reportError(StringRef File, Error E);
-LLVM_ATTRIBUTE_NORETURN extern void reportError(StringRef File,
-                                                std::error_code EC);
 
 // This is taken from llvm-readobj.
 // [see here](llvm/tools/llvm-readobj/llvm-readobj.h:38)




More information about the llvm-commits mailing list