[PATCH] D44702: [llvm-objcopy] Keep the information about the object elf type

Alexander Shaposhnikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 20 14:18:09 PDT 2018


alexshap created this revision.
alexshap added reviewers: jakehehrlich, jhenderson, compnerd.
alexshap created this object with visibility "All Users".

This diff adds ELFType field to the class Object. 
This enables us to get rid of the global variable OutputElfType, 
kill the helper CreateReader and, furthermore, 
this will enable us later to propagate this information further to allow Section* types to parse their content
properly (to handle big-endian/little-endian cases correctly).

Test plan: make check-all


Repository:
  rL LLVM

https://reviews.llvm.org/D44702

Files:
  Object.cpp
  Object.h
  llvm-objcopy.cpp


Index: llvm-objcopy.cpp
===================================================================
--- llvm-objcopy.cpp
+++ llvm-objcopy.cpp
@@ -139,14 +139,12 @@
   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) {
+  switch (Obj.ELFType) {
   case ELFT_ELF32LE:
     return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj, !StripSections);
   case ELFT_ELF64LE:
@@ -318,14 +316,6 @@
   }
 }
 
-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);
-}
-
 int main(int argc, char **argv) {
   // Print a stack trace if we signal out.
   sys::PrintStackTraceOnErrorSignal(argv[0]);
@@ -338,7 +328,7 @@
     return 2;
   }
 
-  auto Reader = CreateReader();
+  auto Reader = llvm::make_unique<ELFReader>(InputFilename);
   auto Obj = Reader->create();
   StringRef Output =
       OutputFilename.getNumOccurrences() ? OutputFilename : InputFilename;
Index: Object.h
===================================================================
--- Object.h
+++ Object.h
@@ -545,7 +545,6 @@
 };
 
 class Object {
-private:
   using SecPtr = std::unique_ptr<SectionBase>;
   using SegPtr = std::unique_ptr<Segment>;
 
@@ -562,6 +561,8 @@
   using ConstRange = iterator_range<pointee_iterator<
       typename std::vector<std::unique_ptr<T>>::const_iterator>>;
 
+  ElfType ELFType;
+
   // It is often the case that the ELF header and the program header table are
   // not present in any segment. This could be a problem during file layout,
   // because other segments may get assigned an offset where either of the
Index: Object.cpp
===================================================================
--- Object.cpp
+++ Object.cpp
@@ -744,6 +744,7 @@
 
 std::unique_ptr<Object> ELFReader::create() const {
   auto Obj = llvm::make_unique<Object>(Data);
+  Obj->ELFType = getElfType();
   if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(Bin.get())) {
     ELFBuilder<ELF32LE> Builder(*o, *Obj);
     Builder.build();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44702.139193.patch
Type: text/x-patch
Size: 2441 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180320/a2879152/attachment.bin>


More information about the llvm-commits mailing list