summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAlexey Samsonov <samsonov@google.com>2013-06-18 15:03:28 +0000
committerAlexey Samsonov <samsonov@google.com>2013-06-18 15:03:28 +0000
commit9c22f87b1374b06dc6c07f6e8047890e390bbe2d (patch)
tree4b4111e1ecfdffcfc137e357afdb764d72ea3696 /include
parent79ac9c8402d4113d42ff2d713c7acdfa800d2397 (diff)
downloadllvm-9c22f87b1374b06dc6c07f6e8047890e390bbe2d.tar.gz
llvm-9c22f87b1374b06dc6c07f6e8047890e390bbe2d.tar.bz2
llvm-9c22f87b1374b06dc6c07f6e8047890e390bbe2d.tar.xz
Basic support for parsing Mach-O universal binaries in LLVMObject library
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@184191 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-rw-r--r--include/llvm/Object/Binary.h5
-rw-r--r--include/llvm/Object/Error.h1
-rw-r--r--include/llvm/Object/MachO.h3
-rw-r--r--include/llvm/Object/MachOFormat.h18
-rw-r--r--include/llvm/Object/MachOUniversal.h102
-rw-r--r--include/llvm/Support/FileSystem.h1
6 files changed, 130 insertions, 0 deletions
diff --git a/include/llvm/Object/Binary.h b/include/llvm/Object/Binary.h
index 78fcf6feb8..a3f5625cc9 100644
--- a/include/llvm/Object/Binary.h
+++ b/include/llvm/Object/Binary.h
@@ -38,6 +38,7 @@ protected:
enum {
ID_Archive,
+ ID_MachOUniversalBinary,
// Object and children.
ID_StartObjects,
ID_COFF,
@@ -87,6 +88,10 @@ public:
return TypeID == ID_Archive;
}
+ bool isMachOUniversalBinary() const {
+ return TypeID == ID_MachOUniversalBinary;
+ }
+
bool isELF() const {
return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
}
diff --git a/include/llvm/Object/Error.h b/include/llvm/Object/Error.h
index 32b834f215..8b0570b02f 100644
--- a/include/llvm/Object/Error.h
+++ b/include/llvm/Object/Error.h
@@ -24,6 +24,7 @@ const error_category &object_category();
struct object_error {
enum Impl {
success = 0,
+ arch_not_found,
invalid_file_type,
parse_failed,
unexpected_eof
diff --git a/include/llvm/Object/MachO.h b/include/llvm/Object/MachO.h
index 1b9faaa9fd..50435d6fe2 100644
--- a/include/llvm/Object/MachO.h
+++ b/include/llvm/Object/MachO.h
@@ -17,6 +17,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/Triple.h"
#include "llvm/Object/MachOFormat.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/MachO.h"
@@ -196,6 +197,8 @@ public:
bool is64Bit() const;
void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
+ static Triple::ArchType getArch(uint32_t CPUType);
+
static bool classof(const Binary *v) {
return v->isMachO();
}
diff --git a/include/llvm/Object/MachOFormat.h b/include/llvm/Object/MachOFormat.h
index ffca391ea2..96ee8a766b 100644
--- a/include/llvm/Object/MachOFormat.h
+++ b/include/llvm/Object/MachOFormat.h
@@ -95,6 +95,8 @@ namespace macho {
enum StructureSizes {
Header32Size = 28,
Header64Size = 32,
+ FatHeaderSize = 8,
+ FatArchHeaderSize = 20,
SegmentLoadCommand32Size = 56,
SegmentLoadCommand64Size = 72,
Section32Size = 68,
@@ -130,6 +132,22 @@ namespace macho {
uint32_t Reserved;
};
+ /// \brief Header for universal object files.
+ struct FatHeader {
+ uint32_t Magic;
+ uint32_t NumFatArch;
+ };
+
+ /// \brief Header for a single-architecture object file in a
+ /// universal binary.
+ struct FatArchHeader {
+ uint32_t CPUType;
+ uint32_t CPUSubtype;
+ uint32_t Offset;
+ uint32_t Size;
+ uint32_t Align;
+ };
+
// See <mach-o/loader.h>.
enum HeaderFileType {
HFT_Object = 0x1
diff --git a/include/llvm/Object/MachOUniversal.h b/include/llvm/Object/MachOUniversal.h
new file mode 100644
index 0000000000..57432826b8
--- /dev/null
+++ b/include/llvm/Object/MachOUniversal.h
@@ -0,0 +1,102 @@
+//===- MachOUniversal.h - Mach-O universal binaries -------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file declares Mach-O fat/universal binaries.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_OBJECT_MACHOUNIVERSAL_H
+#define LLVM_OBJECT_MACHOUNIVERSAL_H
+
+#include "llvm/ADT/OwningPtr.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Triple.h"
+#include "llvm/Object/Binary.h"
+#include "llvm/Object/MachOFormat.h"
+
+namespace llvm {
+namespace object {
+
+class ObjectFile;
+
+class MachOUniversalBinary : public Binary {
+ virtual void anchor();
+
+ uint32_t NumberOfObjects;
+public:
+ class ObjectForArch {
+ const MachOUniversalBinary *Parent;
+ /// \brief Index of object in the universal binary.
+ uint32_t Index;
+ /// \brief Descriptor of the object.
+ macho::FatArchHeader Header;
+
+ public:
+ ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index);
+
+ void clear() {
+ Parent = 0;
+ Index = 0;
+ }
+
+ bool operator==(const ObjectForArch &Other) const {
+ return (Parent == Other.Parent) && (Index == Other.Index);
+ }
+
+ ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
+ uint32_t getCPUType() const { return Header.CPUType; }
+
+ error_code getAsObjectFile(OwningPtr<ObjectFile> &Result) const;
+ };
+
+ class object_iterator {
+ ObjectForArch Obj;
+ public:
+ object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
+ const ObjectForArch* operator->() const {
+ return &Obj;
+ }
+
+ bool operator==(const object_iterator &Other) const {
+ return Obj == Other.Obj;
+ }
+ bool operator!=(const object_iterator &Other) const {
+ return !(*this == Other);
+ }
+
+ object_iterator& operator++() { // Preincrement
+ Obj = Obj.getNext();
+ return *this;
+ }
+ };
+
+ MachOUniversalBinary(MemoryBuffer *Source, error_code &ec);
+
+ object_iterator begin_objects() const {
+ return ObjectForArch(this, 0);
+ }
+ object_iterator end_objects() const {
+ return ObjectForArch(0, 0);
+ }
+
+ uint32_t getNumberOfObjects() const { return NumberOfObjects; }
+
+ // Cast methods.
+ static inline bool classof(Binary const *V) {
+ return V->isMachOUniversalBinary();
+ }
+
+ error_code getObjectForArch(Triple::ArchType Arch,
+ OwningPtr<ObjectFile> &Result) const;
+};
+
+}
+}
+
+#endif
diff --git a/include/llvm/Support/FileSystem.h b/include/llvm/Support/FileSystem.h
index c1c1fc6ed6..79f85539c0 100644
--- a/include/llvm/Support/FileSystem.h
+++ b/include/llvm/Support/FileSystem.h
@@ -199,6 +199,7 @@ struct file_magic {
macho_bundle, ///< Mach-O Bundle file
macho_dynamically_linked_shared_lib_stub, ///< Mach-O Shared lib stub
macho_dsym_companion, ///< Mach-O dSYM companion file
+ macho_universal_binary, ///< Mach-O universal binary
coff_object, ///< COFF object file
pecoff_executable ///< PECOFF executable file
};