Teach llvm-nm to know about fat archives (aka MachOUniversal files
authorKevin Enderby <enderby@apple.com>
Wed, 14 May 2014 21:18:50 +0000 (21:18 +0000)
committerKevin Enderby <enderby@apple.com>
Wed, 14 May 2014 21:18:50 +0000 (21:18 +0000)
containing archives).  First step as other tools will be updated next.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208812 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Object/MachOUniversal.h
lib/Object/MachOUniversal.cpp
test/Object/Inputs/macho-universal-archive.x86_64.i386 [new file with mode: 0644]
test/Object/nm-universal-binary.test
tools/llvm-nm/llvm-nm.cpp

index da1445fd70a476c6b5f0e931156fe98c37c7b07a..d27c824a87a0b7b0a97e72572d0066cd72471d37 100644 (file)
@@ -17,6 +17,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Triple.h"
 #include "llvm/Object/Binary.h"
+#include "llvm/Object/Archive.h"
 #include "llvm/Support/ErrorOr.h"
 #include "llvm/Support/MachO.h"
 
@@ -53,6 +54,8 @@ public:
     uint32_t getCPUType() const { return Header.cputype; }
 
     error_code getAsObjectFile(std::unique_ptr<ObjectFile> &Result) const;
+
+    error_code getAsArchive(std::unique_ptr<Archive> &Result) const;
   };
 
   class object_iterator {
index 3f3731f22d85d795e114c37598e381d04a9ea40d..5085efde5ecfbeece709c9122bc355b32ae549d4 100644 (file)
@@ -14,6 +14,7 @@
 #include "llvm/Object/MachOUniversal.h"
 #include "llvm/Object/MachO.h"
 #include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/Archive.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/Host.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -90,6 +91,25 @@ error_code MachOUniversalBinary::ObjectForArch::getAsObjectFile(
   return object_error::parse_failed;
 }
 
+error_code MachOUniversalBinary::ObjectForArch::getAsArchive(
+    std::unique_ptr<Archive> &Result) const {
+  if (Parent) {
+    StringRef ParentData = Parent->getData();
+    StringRef ObjectData = ParentData.substr(Header.offset, Header.size);
+    std::string ObjectName =
+        Parent->getFileName().str() + ":" +
+        Triple::getArchTypeName(MachOObjectFile::getArch(Header.cputype));
+    MemoryBuffer *ObjBuffer = MemoryBuffer::getMemBuffer(
+        ObjectData, ObjectName, false);
+    ErrorOr<Archive *> Obj = Archive::create(ObjBuffer);
+    if (error_code EC = Obj.getError())
+      return EC;
+    Result.reset(Obj.get());
+    return object_error::success;
+  }
+  return object_error::parse_failed;
+}
+
 void MachOUniversalBinary::anchor() { }
 
 ErrorOr<MachOUniversalBinary *>
diff --git a/test/Object/Inputs/macho-universal-archive.x86_64.i386 b/test/Object/Inputs/macho-universal-archive.x86_64.i386
new file mode 100644 (file)
index 0000000..1660714
Binary files /dev/null and b/test/Object/Inputs/macho-universal-archive.x86_64.i386 differ
index 07a97f7aff99aad29613c67af12944b98ffd5d7b..faf4812e5378b20f276dece5cee9a19040bb7d33 100644 (file)
@@ -1,6 +1,19 @@
-RUN: llvm-nm %p/Inputs/macho-universal.x86_64.i386 | FileCheck %s
+RUN: llvm-nm %p/Inputs/macho-universal.x86_64.i386 \
+RUN:         | FileCheck %s -check-prefix CHECK-OBJ
+RUN: llvm-nm %p/Inputs/macho-universal-archive.x86_64.i386 \
+RUN:         | FileCheck %s -check-prefix CHECK-AR
 
-CHECK: macho-universal.x86_64.i386:x86_64
-CHECK: 0000000100000f60 T _main
-CHECK: macho-universal.x86_64.i386:i386
-CHECK: 00001fa0 T _main
+CHECK-OBJ: macho-universal.x86_64.i386:x86_64
+CHECK-OBJ: 0000000100000f60 T _main
+CHECK-OBJ: macho-universal.x86_64.i386:i386
+CHECK-OBJ: 00001fa0 T _main
+
+CHECK-AR: macho-universal-archive.x86_64.i386:x86_64:hello.o:
+CHECK-AR: 0000000000000068 s EH_frame0
+CHECK-AR: 000000000000003b s L_.str
+CHECK-AR: 0000000000000000 T _main
+CHECK-AR: 0000000000000080 S _main.eh
+CHECK-AR: 0000000000000000 U _printf
+CHECK-AR: macho-universal-archive.x86_64.i386:i386:foo.o:
+CHECK-AR: 00000008 S _bar
+CHECK-AR: 00000000 T _foo
index 0a1491b846755f59ce73ef9dfb12d59667e8e850..57e9d55c866e2b7c89557835229455db58d3f9be 100644 (file)
@@ -595,10 +595,24 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
                                                E = UB->end_objects();
          I != E; ++I) {
       std::unique_ptr<ObjectFile> Obj;
+      std::unique_ptr<Archive> A;
       if (!I->getAsObjectFile(Obj)) {
         outs() << Obj->getFileName() << ":\n";
         dumpSymbolNamesFromObject(Obj.get());
       }
+      else if (!I->getAsArchive(A)) {
+        for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
+             AI != AE; ++AI) {
+          std::unique_ptr<Binary> Child;
+          if (AI->getAsBinary(Child, &Context))
+            continue;
+          if (SymbolicFile *O = dyn_cast<SymbolicFile>(Child.get())) {
+            outs() << A->getFileName() << ":";
+            outs() << O->getFileName() << ":\n";
+            dumpSymbolNamesFromObject(O);
+          }
+        }
+      }
     }
     return;
   }