[llvm] r233107 - [llvm-readobj] add support for macho universal binary.

Rafael Espindola rafael.espindola at gmail.com
Tue Mar 24 13:26:56 PDT 2015


Author: rafael
Date: Tue Mar 24 15:26:55 2015
New Revision: 233107

URL: http://llvm.org/viewvc/llvm-project?rev=233107&view=rev
Log:
[llvm-readobj] add support for macho universal binary.

Patch by Keyue Hu (Chilledheart)!

Added:
    llvm/trunk/test/tools/llvm-readobj/Inputs/macho-universal.x86_64.i386
    llvm/trunk/test/tools/llvm-readobj/macho-universal-x86_64.i386.test
Modified:
    llvm/trunk/include/llvm/Object/MachOUniversal.h
    llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp

Modified: llvm/trunk/include/llvm/Object/MachOUniversal.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/MachOUniversal.h?rev=233107&r1=233106&r2=233107&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/MachOUniversal.h (original)
+++ llvm/trunk/include/llvm/Object/MachOUniversal.h Tue Mar 24 15:26:55 2015
@@ -16,6 +16,7 @@
 
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Triple.h"
+#include "llvm/ADT/iterator_range.h"
 #include "llvm/Object/Archive.h"
 #include "llvm/Object/Binary.h"
 #include "llvm/Object/MachO.h"
@@ -69,9 +70,8 @@ public:
     ObjectForArch Obj;
   public:
     object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
-    const ObjectForArch* operator->() const {
-      return &Obj;
-    }
+    const ObjectForArch *operator->() const { return &Obj; }
+    const ObjectForArch &operator*() const { return Obj; }
 
     bool operator==(const object_iterator &Other) const {
       return Obj == Other.Obj;
@@ -97,6 +97,10 @@ public:
     return ObjectForArch(nullptr, 0);
   }
 
+  iterator_range<object_iterator> objects() const {
+    return make_range(begin_objects(), end_objects());
+  }
+
   uint32_t getNumberOfObjects() const { return NumberOfObjects; }
 
   // Cast methods.

Added: llvm/trunk/test/tools/llvm-readobj/Inputs/macho-universal.x86_64.i386
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/Inputs/macho-universal.x86_64.i386?rev=233107&view=auto
==============================================================================
Binary files llvm/trunk/test/tools/llvm-readobj/Inputs/macho-universal.x86_64.i386 (added) and llvm/trunk/test/tools/llvm-readobj/Inputs/macho-universal.x86_64.i386 Tue Mar 24 15:26:55 2015 differ

Added: llvm/trunk/test/tools/llvm-readobj/macho-universal-x86_64.i386.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/macho-universal-x86_64.i386.test?rev=233107&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-readobj/macho-universal-x86_64.i386.test (added)
+++ llvm/trunk/test/tools/llvm-readobj/macho-universal-x86_64.i386.test Tue Mar 24 15:26:55 2015
@@ -0,0 +1,141 @@
+RUN: llvm-readobj -h %p/Inputs/macho-universal.x86_64.i386 \
+RUN: | FileCheck %s -check-prefix MULTIHEADER
+
+RUN: llvm-readobj -sections %p/Inputs/macho-universal.x86_64.i386 \
+RUN: | FileCheck %s -check-prefix MULTISECTIONS
+
+MULTIHEADER: Format: Mach-O 64-bit x86-64
+MULTIHEADER: Arch: x86_64
+MULTIHEADER: AddressSize: 64bit
+MULTIHEADER: MachHeader {
+MULTIHEADER:   Magic: Magic64 (0xFEEDFACF)
+MULTIHEADER:   CpuType: X86-64 (0x1000007)
+MULTIHEADER:   CpuSubType: CPU_SUBTYPE_X86_64_ALL (0x3)
+MULTIHEADER:   FileType: Executable (0x2)
+MULTIHEADER:   NumOfLoadCommands: 16
+MULTIHEADER:   SizeOfLoadCommands: 880
+MULTIHEADER:   Flags [ (0x200085)
+MULTIHEADER:     MH_DYLDLINK (0x4)
+MULTIHEADER:     MH_NOUNDEFS (0x1)
+MULTIHEADER:     MH_PIE (0x200000)
+MULTIHEADER:     MH_TWOLEVEL (0x80)
+MULTIHEADER:   ]
+MULTIHEADER:   Reserved: 0x0
+MULTIHEADER: }
+
+MULTIHEADER: Format: Mach-O 32-bit i386
+MULTIHEADER: Arch: i386
+MULTIHEADER: AddressSize: 32bit
+MULTIHEADER: MachHeader {
+MULTIHEADER:   Magic: Magic (0xFEEDFACE)
+MULTIHEADER:   CpuType: X86 (0x7)
+MULTIHEADER:   CpuSubType: CPU_SUBTYPE_I386_ALL (0x3)
+MULTIHEADER:   FileType: Executable (0x2)
+MULTIHEADER:   NumOfLoadCommands: 16
+MULTIHEADER:   SizeOfLoadCommands: 716
+MULTIHEADER:   Flags [ (0x1200085)
+MULTIHEADER:     MH_DYLDLINK (0x4)
+MULTIHEADER:     MH_NOUNDEFS (0x1)
+MULTIHEADER:     MH_NO_HEAP_EXECUTION (0x1000000)
+MULTIHEADER:     MH_PIE (0x200000)
+MULTIHEADER:     MH_TWOLEVEL (0x80)
+MULTIHEADER:   ]
+MULTIHEADER: }
+
+
+
+MULTISECTIONS: Format: Mach-O 64-bit x86-64
+MULTISECTIONS: Arch: x86_64
+MULTISECTIONS: AddressSize: 64bit
+MULTISECTIONS: Sections [
+MULTISECTIONS:   Section {
+MULTISECTIONS:     Index: 0
+MULTISECTIONS:     Name: __text (5F 5F 74 65 78 74 00 00 00 00 00 00 00 00 00 00)
+MULTISECTIONS:     Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00)
+MULTISECTIONS:     Address: 0x100000F60
+MULTISECTIONS:     Size: 0x12
+MULTISECTIONS:     Offset: 3936
+MULTISECTIONS:     Alignment: 4
+MULTISECTIONS:     RelocationOffset: 0x0
+MULTISECTIONS:     RelocationCount: 0
+MULTISECTIONS:     Type: 0x0
+MULTISECTIONS:     Attributes [ (0x800004)
+MULTISECTIONS:       PureInstructions (0x800000)
+MULTISECTIONS:       SomeInstructions (0x4)
+MULTISECTIONS:     ]
+MULTISECTIONS:     Reserved1: 0x0
+MULTISECTIONS:     Reserved2: 0x0
+MULTISECTIONS:   }
+MULTISECTIONS:   Section {
+MULTISECTIONS:     Index: 1
+MULTISECTIONS:     Name: __unwind_info (5F 5F 75 6E 77 69 6E 64 5F 69 6E 66 6F 00 00 00)
+MULTISECTIONS:     Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00)
+MULTISECTIONS:     Address: 0x100000F72
+MULTISECTIONS:     Size: 0x48
+MULTISECTIONS:     Offset: 3954
+MULTISECTIONS:     Alignment: 0
+MULTISECTIONS:     RelocationOffset: 0x0
+MULTISECTIONS:     RelocationCount: 0
+MULTISECTIONS:     Type: 0x0
+MULTISECTIONS:     Attributes [ (0x0)
+MULTISECTIONS:     ]
+MULTISECTIONS:     Reserved1: 0x0
+MULTISECTIONS:     Reserved2: 0x0
+MULTISECTIONS:   }
+MULTISECTIONS:   Section {
+MULTISECTIONS:     Index: 2
+MULTISECTIONS:     Name: __eh_frame (5F 5F 65 68 5F 66 72 61 6D 65 00 00 00 00 00 00)
+MULTISECTIONS:     Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00)
+MULTISECTIONS:     Address: 0x100000FC0
+MULTISECTIONS:     Size: 0x40
+MULTISECTIONS:     Offset: 4032
+MULTISECTIONS:     Alignment: 3
+MULTISECTIONS:     RelocationOffset: 0x0
+MULTISECTIONS:     RelocationCount: 0
+MULTISECTIONS:     Type: 0x0
+MULTISECTIONS:     Attributes [ (0x0)
+MULTISECTIONS:     ]
+MULTISECTIONS:     Reserved1: 0x0
+MULTISECTIONS:     Reserved2: 0x0
+MULTISECTIONS:   }
+MULTISECTIONS: ]
+
+MULTISECTIONS: Format: Mach-O 32-bit i386
+MULTISECTIONS: Arch: i386
+MULTISECTIONS: AddressSize: 32bit
+MULTISECTIONS: Sections [
+MULTISECTIONS:   Section {
+MULTISECTIONS:     Index: 0
+MULTISECTIONS:     Name: __text (5F 5F 74 65 78 74 00 00 00 00 00 00 00 00 00 00)
+MULTISECTIONS:     Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00)
+MULTISECTIONS:     Address: 0x1FA0
+MULTISECTIONS:     Size: 0x15
+MULTISECTIONS:     Offset: 4000
+MULTISECTIONS:     Alignment: 4
+MULTISECTIONS:     RelocationOffset: 0x0
+MULTISECTIONS:     RelocationCount: 0
+MULTISECTIONS:     Type: 0x0
+MULTISECTIONS:     Attributes [ (0x800004)
+MULTISECTIONS:       PureInstructions (0x800000)
+MULTISECTIONS:       SomeInstructions (0x4)
+MULTISECTIONS:     ]
+MULTISECTIONS:     Reserved1: 0x0
+MULTISECTIONS:     Reserved2: 0x0
+MULTISECTIONS:   }
+MULTISECTIONS:   Section {
+MULTISECTIONS:     Index: 1
+MULTISECTIONS:     Name: __unwind_info (5F 5F 75 6E 77 69 6E 64 5F 69 6E 66 6F 00 00 00)
+MULTISECTIONS:     Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00)
+MULTISECTIONS:     Address: 0x1FB5
+MULTISECTIONS:     Size: 0x48
+MULTISECTIONS:     Offset: 4021
+MULTISECTIONS:     Alignment: 0
+MULTISECTIONS:     RelocationOffset: 0x0
+MULTISECTIONS:     RelocationCount: 0
+MULTISECTIONS:     Type: 0x0
+MULTISECTIONS:     Attributes [ (0x0)
+MULTISECTIONS:     ]
+MULTISECTIONS:     Reserved1: 0x0
+MULTISECTIONS:     Reserved2: 0x0
+MULTISECTIONS:   }
+MULTISECTIONS: ]

Modified: llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp?rev=233107&r1=233106&r2=233107&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp Tue Mar 24 15:26:55 2015
@@ -25,6 +25,7 @@
 #include "StreamWriter.h"
 #include "llvm/Object/Archive.h"
 #include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Object/MachOUniversal.h"
 #include "llvm/Object/ObjectFile.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/CommandLine.h"
@@ -320,6 +321,19 @@ static void dumpArchive(const Archive *A
   }
 }
 
+/// @brief Dumps each object file in \a MachO Universal Binary;
+static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) {
+  for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
+    ErrorOr<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
+    if (std::error_code EC = ObjOrErr.getError()) {
+      reportError(UBinary->getFileName(), EC.message());
+      continue;
+    }
+
+    if (MachOObjectFile *MachOObj = ObjOrErr.get().get())
+      dumpObject(MachOObj);
+  }
+}
 
 /// @brief Opens \a File and dumps it.
 static void dumpInput(StringRef File) {
@@ -339,6 +353,9 @@ static void dumpInput(StringRef File) {
 
   if (Archive *Arc = dyn_cast<Archive>(&Binary))
     dumpArchive(Arc);
+  else if (MachOUniversalBinary *UBinary =
+               dyn_cast<MachOUniversalBinary>(&Binary))
+    dumpMachOUniversalBinary(UBinary);
   else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
     dumpObject(Obj);
   else





More information about the llvm-commits mailing list