mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-26 12:50:30 +00:00
Basic support for parsing Mach-O universal binaries in LLVMObject library
llvm-svn: 184191
This commit is contained in:
parent
0d881ffff7
commit
50c414e3d0
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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
|
||||
|
102
include/llvm/Object/MachOUniversal.h
Normal file
102
include/llvm/Object/MachOUniversal.h
Normal file
@ -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
|
@ -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
|
||||
};
|
||||
|
@ -527,6 +527,7 @@ ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
|
||||
case sys::fs::file_magic::archive:
|
||||
case sys::fs::file_magic::coff_object:
|
||||
case sys::fs::file_magic::pecoff_executable:
|
||||
case sys::fs::file_magic::macho_universal_binary:
|
||||
report_fatal_error("Incompatible object format!");
|
||||
}
|
||||
} else {
|
||||
|
@ -20,6 +20,7 @@
|
||||
// Include headers for createBinary.
|
||||
#include "llvm/Object/Archive.h"
|
||||
#include "llvm/Object/COFF.h"
|
||||
#include "llvm/Object/MachOUniversal.h"
|
||||
#include "llvm/Object/ObjectFile.h"
|
||||
|
||||
using namespace llvm;
|
||||
@ -82,6 +83,12 @@ error_code object::createBinary(MemoryBuffer *Source,
|
||||
Result.swap(ret);
|
||||
return object_error::success;
|
||||
}
|
||||
case sys::fs::file_magic::macho_universal_binary: {
|
||||
OwningPtr<Binary> ret(new MachOUniversalBinary(scopedSource.take(), ec));
|
||||
if (ec) return ec;
|
||||
Result.swap(ret);
|
||||
return object_error::success;
|
||||
}
|
||||
case sys::fs::file_magic::coff_object:
|
||||
case sys::fs::file_magic::pecoff_executable: {
|
||||
OwningPtr<Binary> ret(
|
||||
|
@ -7,6 +7,7 @@ add_llvm_library(LLVMObject
|
||||
ELFYAML.cpp
|
||||
Error.cpp
|
||||
MachOObjectFile.cpp
|
||||
MachOUniversal.cpp
|
||||
Object.cpp
|
||||
ObjectFile.cpp
|
||||
YAML.cpp
|
||||
|
@ -34,6 +34,8 @@ std::string _object_error_category::message(int ev) const {
|
||||
object_error::Impl E = static_cast<object_error::Impl>(ev);
|
||||
switch (E) {
|
||||
case object_error::success: return "Success";
|
||||
case object_error::arch_not_found:
|
||||
return "No object file for requested architecture";
|
||||
case object_error::invalid_file_type:
|
||||
return "The file was not recognized as a valid object file";
|
||||
case object_error::parse_failed:
|
||||
|
@ -1297,8 +1297,8 @@ StringRef MachOObjectFile::getFileFormatName() const {
|
||||
}
|
||||
}
|
||||
|
||||
unsigned MachOObjectFile::getArch() const {
|
||||
switch (getCPUType(this)) {
|
||||
Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
|
||||
switch (CPUType) {
|
||||
case llvm::MachO::CPUTypeI386:
|
||||
return Triple::x86;
|
||||
case llvm::MachO::CPUTypeX86_64:
|
||||
@ -1314,6 +1314,10 @@ unsigned MachOObjectFile::getArch() const {
|
||||
}
|
||||
}
|
||||
|
||||
unsigned MachOObjectFile::getArch() const {
|
||||
return getArch(getCPUType(this));
|
||||
}
|
||||
|
||||
StringRef MachOObjectFile::getLoadName() const {
|
||||
// TODO: Implement
|
||||
report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
|
||||
|
139
lib/Object/MachOUniversal.cpp
Normal file
139
lib/Object/MachOUniversal.cpp
Normal file
@ -0,0 +1,139 @@
|
||||
//===- MachOUniversal.cpp - Mach-O universal binary -------------*- C++ -*-===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file defines the MachOUniversalBinary class.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/Object/MachOUniversal.h"
|
||||
|
||||
#include "llvm/Object/MachO.h"
|
||||
#include "llvm/Object/ObjectFile.h"
|
||||
#include "llvm/Support/Casting.h"
|
||||
#include "llvm/Support/Host.h"
|
||||
#include "llvm/Support/MemoryBuffer.h"
|
||||
|
||||
using namespace llvm;
|
||||
using namespace object;
|
||||
|
||||
template<typename T>
|
||||
static void SwapValue(T &Value) {
|
||||
Value = sys::SwapByteOrder(Value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static void SwapStruct(T &Value);
|
||||
|
||||
template<>
|
||||
void SwapStruct(macho::FatHeader &H) {
|
||||
SwapValue(H.Magic);
|
||||
SwapValue(H.NumFatArch);
|
||||
}
|
||||
|
||||
template<>
|
||||
void SwapStruct(macho::FatArchHeader &H) {
|
||||
SwapValue(H.CPUType);
|
||||
SwapValue(H.CPUSubtype);
|
||||
SwapValue(H.Offset);
|
||||
SwapValue(H.Size);
|
||||
SwapValue(H.Align);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static T getUniversalBinaryStruct(const char *Ptr) {
|
||||
T Res;
|
||||
memcpy(&Res, Ptr, sizeof(T));
|
||||
// Universal binary headers have big-endian byte order.
|
||||
if (sys::IsLittleEndianHost)
|
||||
SwapStruct(Res);
|
||||
return Res;
|
||||
}
|
||||
|
||||
MachOUniversalBinary::ObjectForArch::ObjectForArch(
|
||||
const MachOUniversalBinary *Parent, uint32_t Index)
|
||||
: Parent(Parent), Index(Index) {
|
||||
if (Parent == 0 || Index > Parent->getNumberOfObjects()) {
|
||||
clear();
|
||||
} else {
|
||||
// Parse object header.
|
||||
StringRef ParentData = Parent->getData();
|
||||
const char *HeaderPos = ParentData.begin() + macho::FatHeaderSize +
|
||||
Index * macho::FatArchHeaderSize;
|
||||
Header = getUniversalBinaryStruct<macho::FatArchHeader>(HeaderPos);
|
||||
if (ParentData.size() < Header.Offset + Header.Size) {
|
||||
clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
error_code MachOUniversalBinary::ObjectForArch::getAsObjectFile(
|
||||
OwningPtr<ObjectFile> &Result) const {
|
||||
if (Parent) {
|
||||
StringRef ParentData = Parent->getData();
|
||||
StringRef ObjectData = ParentData.substr(Header.Offset, Header.Size);
|
||||
Twine ObjectName =
|
||||
Twine(Parent->getFileName()) + ":" +
|
||||
Triple::getArchTypeName(MachOObjectFile::getArch(Header.CPUType));
|
||||
MemoryBuffer *ObjBuffer = MemoryBuffer::getMemBuffer(
|
||||
ObjectData, ObjectName.str(), false);
|
||||
if (ObjectFile *Obj = ObjectFile::createMachOObjectFile(ObjBuffer)) {
|
||||
Result.reset(Obj);
|
||||
return object_error::success;
|
||||
}
|
||||
}
|
||||
return object_error::parse_failed;
|
||||
}
|
||||
|
||||
void MachOUniversalBinary::anchor() { }
|
||||
|
||||
MachOUniversalBinary::MachOUniversalBinary(MemoryBuffer *Source,
|
||||
error_code &ec)
|
||||
: Binary(Binary::ID_MachOUniversalBinary, Source),
|
||||
NumberOfObjects(0) {
|
||||
if (Source->getBufferSize() < macho::FatHeaderSize) {
|
||||
ec = object_error::invalid_file_type;
|
||||
return;
|
||||
}
|
||||
// Check for magic value and sufficient header size.
|
||||
StringRef Buf = getData();
|
||||
macho::FatHeader H = getUniversalBinaryStruct<macho::FatHeader>(Buf.begin());
|
||||
NumberOfObjects = H.NumFatArch;
|
||||
uint32_t MinSize = macho::FatHeaderSize +
|
||||
macho::FatArchHeaderSize * NumberOfObjects;
|
||||
if (H.Magic != macho::HM_Universal || Buf.size() < MinSize) {
|
||||
ec = object_error::parse_failed;
|
||||
return;
|
||||
}
|
||||
ec = object_error::success;
|
||||
}
|
||||
|
||||
static bool getCTMForArch(Triple::ArchType Arch, mach::CPUTypeMachine &CTM) {
|
||||
switch (Arch) {
|
||||
case Triple::x86: CTM = mach::CTM_i386; return true;
|
||||
case Triple::x86_64: CTM = mach::CTM_x86_64; return true;
|
||||
case Triple::arm: CTM = mach::CTM_ARM; return true;
|
||||
case Triple::sparc: CTM = mach::CTM_SPARC; return true;
|
||||
case Triple::ppc: CTM = mach::CTM_PowerPC; return true;
|
||||
case Triple::ppc64: CTM = mach::CTM_PowerPC64; return true;
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
error_code
|
||||
MachOUniversalBinary::getObjectForArch(Triple::ArchType Arch,
|
||||
OwningPtr<ObjectFile> &Result) const {
|
||||
mach::CPUTypeMachine CTM;
|
||||
if (!getCTMForArch(Arch, CTM))
|
||||
return object_error::arch_not_found;
|
||||
for (object_iterator I = begin_objects(), E = end_objects(); I != E; ++I) {
|
||||
if (I->getCPUType() == static_cast<uint32_t>(CTM))
|
||||
return I->getAsObjectFile(Result);
|
||||
}
|
||||
return object_error::arch_not_found;
|
||||
}
|
@ -46,6 +46,7 @@ ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) {
|
||||
case sys::fs::file_magic::unknown:
|
||||
case sys::fs::file_magic::bitcode:
|
||||
case sys::fs::file_magic::archive:
|
||||
case sys::fs::file_magic::macho_universal_binary:
|
||||
return 0;
|
||||
case sys::fs::file_magic::elf_relocatable:
|
||||
case sys::fs::file_magic::elf_executable:
|
||||
|
@ -810,8 +810,7 @@ error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
|
||||
// This is complicated by an overlap with Java class files.
|
||||
// See the Mach-O section in /usr/share/file/magic for details.
|
||||
if (Magic.size() >= 8 && Magic[7] < 43)
|
||||
// FIXME: Universal Binary of any type.
|
||||
return file_magic::macho_dynamically_linked_shared_lib;
|
||||
return file_magic::macho_universal_binary;
|
||||
}
|
||||
break;
|
||||
|
||||
|
BIN
test/Object/Inputs/macho-universal.x86_64.i386
Executable file
BIN
test/Object/Inputs/macho-universal.x86_64.i386
Executable file
Binary file not shown.
6
test/Object/nm-universal-binary.test
Normal file
6
test/Object/nm-universal-binary.test
Normal file
@ -0,0 +1,6 @@
|
||||
RUN: llvm-nm %p/Inputs/macho-universal.x86_64.i386 | FileCheck %s
|
||||
|
||||
CHECK: macho-universal.x86_64.i386:x86_64
|
||||
CHECK: main
|
||||
CHECK: macho-universal.x86_64.i386:i386
|
||||
CHECK: main
|
@ -20,6 +20,7 @@
|
||||
#include "llvm/Bitcode/ReaderWriter.h"
|
||||
#include "llvm/IR/Module.h"
|
||||
#include "llvm/Object/Archive.h"
|
||||
#include "llvm/Object/MachOUniversal.h"
|
||||
#include "llvm/Object/ObjectFile.h"
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
#include "llvm/Support/FileSystem.h"
|
||||
@ -402,6 +403,23 @@ static void DumpSymbolNamesFromFile(std::string &Filename) {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (magic == sys::fs::file_magic::macho_universal_binary) {
|
||||
OwningPtr<Binary> Bin;
|
||||
if (error(object::createBinary(Buffer.take(), Bin), Filename))
|
||||
return;
|
||||
|
||||
object::MachOUniversalBinary *UB =
|
||||
cast<object::MachOUniversalBinary>(Bin.get());
|
||||
for (object::MachOUniversalBinary::object_iterator
|
||||
I = UB->begin_objects(),
|
||||
E = UB->end_objects();
|
||||
I != E; ++I) {
|
||||
OwningPtr<ObjectFile> Obj;
|
||||
if (!I->getAsObjectFile(Obj)) {
|
||||
outs() << Obj->getFileName() << ":\n";
|
||||
DumpSymbolNamesFromObject(Obj.get());
|
||||
}
|
||||
}
|
||||
} else if (magic.is_object()) {
|
||||
OwningPtr<Binary> obj;
|
||||
if (error(object::createBinary(Buffer.take(), obj), Filename))
|
||||
|
Loading…
Reference in New Issue
Block a user