[llvm] r309658 - [llvm][llvm-objcopy] Added support for outputting to binary in llvm-objcopy

Petr Hosek via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 31 22:18:31 PDT 2017


Author: phosek
Date: Mon Jul 31 22:18:30 2017
New Revision: 309658

URL: http://llvm.org/viewvc/llvm-project?rev=309658&view=rev
Log:
[llvm][llvm-objcopy] Added support for outputting to binary in llvm-objcopy

This change adds the "-O binary" flag which directs llvm-objcopy to
output the object file to the same format as GNU objcopy does when given
the flag "-O binary". This was done by splitting the Object class into
two subclasses ObjectELF and ObjectBianry which each output a different
format but relay on the same code to read in the Object in Object.

Patch by Jake Ehrlich

Differential Revision: https://reviews.llvm.org/D34480

Added:
    llvm/trunk/test/tools/llvm-objcopy/basic-align-copy.test
    llvm/trunk/test/tools/llvm-objcopy/basic-binary-copy.test
Modified:
    llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt
    llvm/trunk/tools/llvm-objcopy/Object.cpp
    llvm/trunk/tools/llvm-objcopy/Object.h
    llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp

Added: llvm/trunk/test/tools/llvm-objcopy/basic-align-copy.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/basic-align-copy.test?rev=309658&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/basic-align-copy.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/basic-align-copy.test Mon Jul 31 22:18:30 2017
@@ -0,0 +1,37 @@
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-objcopy -O binary %t %t2
+# RUN: od -t x2 %t2 | FileCheck %s
+# RUN: wc -c < %t2 | FileCheck %s --check-prefix=SIZE
+
+!ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_EXEC
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000001000
+    Content:         "c3c3c3c3"
+  - Name:            .data
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000001000
+    Content:         "32"
+ProgramHeaders:
+  - Type: PT_LOAD
+    Flags: [ PF_X, PF_R ]
+    Sections:
+      - Section: .text
+  - Type: PT_LOAD
+    Flags: [ PF_R ]
+    Sections:
+      - Section: .data
+
+# CHECK:       0000000 c3c3 c3c3 0000 0000 0000 0000 0000 0000
+# CHECK-NEXT:  0000020 0000 0000 0000 0000 0000 0000 0000 0000
+# CHECK-NEXT:  *
+# CHECK-NEXT:  0010000 0032
+# SIZE:        4097

Added: llvm/trunk/test/tools/llvm-objcopy/basic-binary-copy.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/basic-binary-copy.test?rev=309658&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/basic-binary-copy.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/basic-binary-copy.test Mon Jul 31 22:18:30 2017
@@ -0,0 +1,25 @@
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-objcopy -O binary %t %t2
+# RUN: od -t x2 -v %t2 | FileCheck %s
+# RUN: wc -c < %t2 | FileCheck %s --check-prefix=SIZE
+
+!ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_EXEC
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000001000
+    Content:         "c3c3c3c3"
+ProgramHeaders:
+  - Type: PT_LOAD
+    Flags: [ PF_X, PF_R ]
+    Sections:
+      - Section: .text
+
+# CHECK: 0000000 c3c3 c3c3
+# SIZE:  4

Modified: llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt?rev=309658&r1=309657&r2=309658&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt (original)
+++ llvm/trunk/tools/llvm-objcopy/LLVMBuild.txt Mon Jul 31 22:18:30 2017
@@ -18,4 +18,4 @@
 type = Tool
 name = llvm-objcopy
 parent = Tools
-required_libraries = Object MC
+required_libraries = Object Support MC

Modified: llvm/trunk/tools/llvm-objcopy/Object.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/Object.cpp?rev=309658&r1=309657&r2=309658&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/Object.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/Object.cpp Mon Jul 31 22:18:30 2017
@@ -42,6 +42,13 @@ void Segment::finalize() {
   }
 }
 
+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::finalize() {}
 
 template <class ELFT>
@@ -99,7 +106,8 @@ template <class ELFT>
 void Object<ELFT>::readProgramHeaders(const ELFFile<ELFT> &ElfFile) {
   uint32_t Index = 0;
   for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
-    Segments.emplace_back(llvm::make_unique<Segment>());
+    ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset, Phdr.p_filesz};
+    Segments.emplace_back(llvm::make_unique<Segment>(Data));
     Segment &Seg = *Segments.back();
     Seg.Type = Phdr.p_type;
     Seg.Flags = Phdr.p_flags;
@@ -135,7 +143,7 @@ Object<ELFT>::makeSection(const llvm::ob
   default:
     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
     return llvm::make_unique<Section>(Data);
-  };
+  }
 }
 
 template <class ELFT>
@@ -163,12 +171,6 @@ void Object<ELFT>::readSectionHeaders(co
   }
 }
 
-template <class ELFT> size_t Object<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.
-  return SHOffset + Sections.size() * sizeof(Elf_Shdr) + sizeof(Elf_Shdr);
-}
-
 template <class ELFT> Object<ELFT>::Object(const ELFObjectFile<ELFT> &Obj) {
   const auto &ElfFile = *Obj.getELFFile();
   const auto &Ehdr = *ElfFile.getHeader();
@@ -187,22 +189,76 @@ template <class ELFT> Object<ELFT>::Obje
       dyn_cast<StringTableSection>(Sections[Ehdr.e_shstrndx - 1].get());
 }
 
-template <class ELFT> void Object<ELFT>::sortSections() {
+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(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_shoff = SHOffset;
+  Ehdr.e_flags = Flags;
+  Ehdr.e_ehsize = sizeof(Elf_Ehdr);
+  Ehdr.e_phentsize = sizeof(Elf_Phdr);
+  Ehdr.e_phnum = Segments.size();
+  Ehdr.e_shentsize = sizeof(Elf_Shdr);
+  Ehdr.e_shnum = Sections.size() + 1;
+  Ehdr.e_shstrndx = SectionNames->Index;
+}
+
+template <class ELFT>
+void Object<ELFT>::writeProgramHeaders(FileOutputBuffer &Out) const {
+  for (auto &Phdr : Segments)
+    Phdr->template writeHeader<ELFT>(Out);
+}
+
+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.
+  Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(Buf);
+  Shdr.sh_name = 0;
+  Shdr.sh_type = SHT_NULL;
+  Shdr.sh_flags = 0;
+  Shdr.sh_addr = 0;
+  Shdr.sh_offset = 0;
+  Shdr.sh_size = 0;
+  Shdr.sh_link = 0;
+  Shdr.sh_info = 0;
+  Shdr.sh_addralign = 0;
+  Shdr.sh_entsize = 0;
+
+  for (auto &Section : Sections)
+    Section->template writeHeader<ELFT>(Out);
+}
+
+template <class ELFT>
+void Object<ELFT>::writeSectionData(FileOutputBuffer &Out) const {
+  for (auto &Section : Sections)
+    Section->writeSection(Out);
+}
+
+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) {
     return A->OriginalOffset < B->OriginalOffset;
   };
-  std::stable_sort(std::begin(Sections), std::end(Sections), CompareSections);
+  std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
+                   CompareSections);
 }
 
-template <class ELFT> void Object<ELFT>::assignOffsets() {
+template <class ELFT> void ELFObject<ELFT>::assignOffsets() {
   // Decide file offsets and indexes.
-  size_t PhdrSize = Segments.size() * sizeof(Elf_Phdr);
+  size_t PhdrSize = this->Segments.size() * sizeof(Elf_Phdr);
   // We can put section data after the ELF header and the program headers.
   uint64_t Offset = sizeof(Elf_Ehdr) + PhdrSize;
   uint64_t Index = 1;
-  for (auto &Section : Sections) {
+  for (auto &Section : this->Sections) {
     // The segment can have a different alignment than the section. In the case
     // that there is a parent segment then as long as we satisfy the alignment
     // of the segment it should follow that that the section is aligned.
@@ -249,93 +305,88 @@ template <class ELFT> void Object<ELFT>:
   // this needs to be 4-byte aligned and on 64-bit it needs to be 8-byte aligned
   // so the size of ELFT::Addr is used to ensure this.
   Offset = alignTo(Offset, sizeof(typename ELFT::Addr));
-  SHOffset = Offset;
+  this->SHOffset = Offset;
 }
 
-template <class ELFT> void Object<ELFT>::finalize() {
-  for (auto &Section : Sections)
-    SectionNames->addString(Section->Name);
+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.
+  return this->SHOffset + this->Sections.size() * sizeof(Elf_Shdr) +
+         sizeof(Elf_Shdr);
+}
+
+template <class ELFT> void ELFObject<ELFT>::write(FileOutputBuffer &Out) const {
+  this->writeHeader(Out);
+  this->writeProgramHeaders(Out);
+  this->writeSectionData(Out);
+  this->writeSectionHeaders(Out);
+}
+
+template <class ELFT> void ELFObject<ELFT>::finalize() {
+  for (const auto &Section : this->Sections) {
+    this->SectionNames->addString(Section->Name);
+  }
 
   sortSections();
   assignOffsets();
 
   // Finalize SectionNames first so that we can assign name indexes.
-  SectionNames->finalize();
+  this->SectionNames->finalize();
   // Finally now that all offsets and indexes have been set we can finalize any
   // remaining issues.
-  uint64_t Offset = SHOffset + sizeof(Elf_Shdr);
-  for (auto &Section : Sections) {
+  uint64_t Offset = this->SHOffset + sizeof(Elf_Shdr);
+  for (auto &Section : this->Sections) {
     Section->HeaderOffset = Offset;
     Offset += sizeof(Elf_Shdr);
-    Section->NameIndex = SectionNames->findIndex(Section->Name);
+    Section->NameIndex = this->SectionNames->findIndex(Section->Name);
     Section->finalize();
   }
 
-  for (auto &Segment : Segments)
+  for (auto &Segment : this->Segments)
     Segment->finalize();
 }
 
-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(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_shoff = SHOffset;
-  Ehdr.e_flags = Flags;
-  Ehdr.e_ehsize = sizeof(Elf_Ehdr);
-  Ehdr.e_phentsize = sizeof(Elf_Phdr);
-  Ehdr.e_phnum = Segments.size();
-  Ehdr.e_shentsize = sizeof(Elf_Shdr);
-  Ehdr.e_shnum = Sections.size() + 1;
-  Ehdr.e_shstrndx = SectionNames->Index;
+template <class ELFT> size_t BinaryObject<ELFT>::totalSize() const {
+  return TotalSize;
 }
 
 template <class ELFT>
-void Object<ELFT>::writeProgramHeaders(FileOutputBuffer &Out) const {
-  for (auto &Phdr : Segments)
-    Phdr->template writeHeader<ELFT>(Out);
+void BinaryObject<ELFT>::write(FileOutputBuffer &Out) const {
+  for (auto &Segment : this->Segments) {
+    if (Segment->Type == llvm::ELF::PT_LOAD) {
+      Segment->writeSegment(Out);
+    }
+  }
 }
 
-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.
-  Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(Buf);
-  Shdr.sh_name = 0;
-  Shdr.sh_type = SHT_NULL;
-  Shdr.sh_flags = 0;
-  Shdr.sh_addr = 0;
-  Shdr.sh_offset = 0;
-  Shdr.sh_size = 0;
-  Shdr.sh_link = 0;
-  Shdr.sh_info = 0;
-  Shdr.sh_addralign = 0;
-  Shdr.sh_entsize = 0;
-
-  for (auto &Section : Sections)
-    Section->template writeHeader<ELFT>(Out);
-}
+template <class ELFT> void BinaryObject<ELFT>::finalize() {
+  for (auto &Segment : this->Segments)
+    Segment->finalize();
 
-template <class ELFT>
-void Object<ELFT>::writeSectionData(FileOutputBuffer &Out) const {
-  for (auto &Section : Sections)
-    Section->writeSection(Out);
-}
+  // Put all segments in offset order.
+  auto CompareSegments = [](const SegPtr &A, const SegPtr &B) {
+    return A->Offset < B->Offset;
+  };
+  std::sort(std::begin(this->Segments), std::end(this->Segments),
+            CompareSegments);
 
-template <class ELFT> void Object<ELFT>::write(FileOutputBuffer &Out) {
-  writeHeader(Out);
-  writeProgramHeaders(Out);
-  writeSectionData(Out);
-  writeSectionHeaders(Out);
+  uint64_t Offset = 0;
+  for (auto &Segment : this->Segments) {
+    if (Segment->Type == llvm::ELF::PT_LOAD) {
+      Offset = alignTo(Offset, Segment->Align);
+      Segment->Offset = Offset;
+      Offset += Segment->FileSize;
+    }
+  }
+  TotalSize = Offset;
 }
 
-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>;

Modified: llvm/trunk/tools/llvm-objcopy/Object.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/Object.h?rev=309658&r1=309657&r2=309658&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/Object.h (original)
+++ llvm/trunk/tools/llvm-objcopy/Object.h Mon Jul 31 22:18:30 2017
@@ -58,6 +58,7 @@ private:
   };
 
   std::set<const SectionBase *, SectionCompare> Sections;
+  llvm::ArrayRef<uint8_t> Contents;
 
 public:
   uint64_t Align;
@@ -70,6 +71,7 @@ public:
   uint64_t Type;
   uint64_t VAddr;
 
+  Segment(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
   void finalize();
   const SectionBase *firstSection() const {
     if (!Sections.empty())
@@ -78,6 +80,7 @@ public:
   }
   void addSection(const SectionBase *sec) { Sections.insert(sec); }
   template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const;
+  void writeSegment(llvm::FileOutputBuffer &Out) const;
 };
 
 class Section : public SectionBase {
@@ -117,16 +120,16 @@ private:
   typedef typename ELFT::Ehdr Elf_Ehdr;
   typedef typename ELFT::Phdr Elf_Phdr;
 
-  StringTableSection *SectionNames;
-  std::vector<SecPtr> Sections;
-  std::vector<SegPtr> Segments;
-
-  void sortSections();
-  void assignOffsets();
   SecPtr makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
                      const Elf_Shdr &Shdr);
   void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
   void readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
+
+protected:
+  StringTableSection *SectionNames;
+  std::vector<SecPtr> Sections;
+  std::vector<SegPtr> Segments;
+
   void writeHeader(llvm::FileOutputBuffer &Out) const;
   void writeProgramHeaders(llvm::FileOutputBuffer &Out) const;
   void writeSectionData(llvm::FileOutputBuffer &Out) const;
@@ -142,9 +145,43 @@ public:
   uint32_t Flags;
 
   Object(const llvm::object::ELFObjectFile<ELFT> &Obj);
-  size_t totalSize() const;
-  void finalize();
-  void write(llvm::FileOutputBuffer &Out);
+  virtual size_t totalSize() const = 0;
+  virtual void finalize() = 0;
+  virtual void write(llvm::FileOutputBuffer &Out) const = 0;
+  virtual ~Object() = default;
+};
+
+template <class ELFT> class ELFObject : public Object<ELFT> {
+private:
+  typedef std::unique_ptr<SectionBase> SecPtr;
+  typedef std::unique_ptr<Segment> SegPtr;
+
+  typedef typename ELFT::Shdr Elf_Shdr;
+  typedef typename ELFT::Ehdr Elf_Ehdr;
+  typedef typename ELFT::Phdr Elf_Phdr;
+
+  void sortSections();
+  void assignOffsets();
+
+public:
+  ELFObject(const llvm::object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
+  void finalize() override;
+  size_t totalSize() const override;
+  void write(llvm::FileOutputBuffer &Out) const override;
 };
 
+template <class ELFT> class BinaryObject : public Object<ELFT> {
+private:
+  typedef std::unique_ptr<SectionBase> SecPtr;
+  typedef std::unique_ptr<Segment> SegPtr;
+
+  uint64_t TotalSize;
+
+public:
+  BinaryObject(const llvm::object::ELFObjectFile<ELFT> &Obj)
+      : Object<ELFT>(Obj) {}
+  void finalize() override;
+  size_t totalSize() const override;
+  void write(llvm::FileOutputBuffer &Out) const override;
+};
 #endif

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=309658&r1=309657&r2=309658&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp Mon Jul 31 22:18:30 2017
@@ -53,13 +53,23 @@ LLVM_ATTRIBUTE_NORETURN void reportError
 cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input>"));
 cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("<output>"),
                                     cl::init("-"));
+cl::opt<std::string>
+    OutputFormat("O", cl::desc("set output format to one of the following:"
+                               "\n\tbinary"));
 
 void CopyBinary(const ELFObjectFile<ELF64LE> &ObjFile) {
   std::unique_ptr<FileOutputBuffer> Buffer;
-  Object<ELF64LE> Obj{ObjFile};
-  Obj.finalize();
+  std::unique_ptr<Object<ELF64LE>> Obj;
+  if (!OutputFormat.empty() && OutputFormat != "binary")
+    error("invalid output format '" + OutputFormat + "'");
+
+  if (!OutputFormat.empty() && OutputFormat == "binary")
+    Obj = llvm::make_unique<BinaryObject<ELF64LE>>(ObjFile);
+  else
+    Obj = llvm::make_unique<ELFObject<ELF64LE>>(ObjFile);
+  Obj->finalize();
   ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
-      FileOutputBuffer::create(OutputFilename, Obj.totalSize(),
+      FileOutputBuffer::create(OutputFilename, Obj->totalSize(),
                                FileOutputBuffer::F_executable);
   if (BufferOrErr.getError())
     error("failed to open " + OutputFilename);
@@ -68,7 +78,7 @@ void CopyBinary(const ELFObjectFile<ELF6
   std::error_code EC;
   if (EC)
     report_fatal_error(EC.message());
-  Obj.write(*Buffer);
+  Obj->write(*Buffer);
   if (auto EC = Buffer->commit())
     reportError(OutputFilename, EC);
 }




More information about the llvm-commits mailing list