2013-06-18 15:03:28 +00:00
|
|
|
//===- 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<>
|
2013-08-27 05:38:30 +00:00
|
|
|
void SwapStruct(macho::FatHeader &H) {
|
|
|
|
SwapValue(H.Magic);
|
|
|
|
SwapValue(H.NumFatArch);
|
2013-06-18 15:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2013-08-27 05:38:30 +00:00
|
|
|
void SwapStruct(macho::FatArchHeader &H) {
|
|
|
|
SwapValue(H.CPUType);
|
|
|
|
SwapValue(H.CPUSubtype);
|
|
|
|
SwapValue(H.Offset);
|
|
|
|
SwapValue(H.Size);
|
|
|
|
SwapValue(H.Align);
|
2013-06-18 15:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2013-08-27 05:38:30 +00:00
|
|
|
const char *HeaderPos = ParentData.begin() + macho::FatHeaderSize +
|
|
|
|
Index * macho::FatArchHeaderSize;
|
|
|
|
Header = getUniversalBinaryStruct<macho::FatArchHeader>(HeaderPos);
|
|
|
|
if (ParentData.size() < Header.Offset + Header.Size) {
|
2013-06-18 15:03:28 +00:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error_code MachOUniversalBinary::ObjectForArch::getAsObjectFile(
|
|
|
|
OwningPtr<ObjectFile> &Result) const {
|
|
|
|
if (Parent) {
|
|
|
|
StringRef ParentData = Parent->getData();
|
2013-08-27 05:38:30 +00:00
|
|
|
StringRef ObjectData = ParentData.substr(Header.Offset, Header.Size);
|
2013-06-19 09:55:05 +00:00
|
|
|
std::string ObjectName =
|
|
|
|
Parent->getFileName().str() + ":" +
|
2013-08-27 05:38:30 +00:00
|
|
|
Triple::getArchTypeName(MachOObjectFile::getArch(Header.CPUType));
|
2013-06-18 15:03:28 +00:00
|
|
|
MemoryBuffer *ObjBuffer = MemoryBuffer::getMemBuffer(
|
2013-06-19 09:55:05 +00:00
|
|
|
ObjectData, ObjectName, false);
|
2013-06-18 15:03:28 +00:00
|
|
|
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) {
|
2013-08-27 05:38:30 +00:00
|
|
|
if (Source->getBufferSize() < macho::FatHeaderSize) {
|
2013-06-18 15:03:28 +00:00
|
|
|
ec = object_error::invalid_file_type;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check for magic value and sufficient header size.
|
|
|
|
StringRef Buf = getData();
|
2013-08-27 05:38:30 +00:00
|
|
|
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) {
|
2013-06-18 15:03:28 +00:00
|
|
|
ec = object_error::parse_failed;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ec = object_error::success;
|
|
|
|
}
|
|
|
|
|
2013-08-27 05:38:30 +00:00
|
|
|
static bool getCTMForArch(Triple::ArchType Arch, mach::CPUTypeMachine &CTM) {
|
2013-06-18 15:03:28 +00:00
|
|
|
switch (Arch) {
|
2013-08-27 05:38:30 +00:00
|
|
|
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;
|
2013-06-18 15:03:28 +00:00
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error_code
|
|
|
|
MachOUniversalBinary::getObjectForArch(Triple::ArchType Arch,
|
|
|
|
OwningPtr<ObjectFile> &Result) const {
|
2013-08-27 05:38:30 +00:00
|
|
|
mach::CPUTypeMachine CTM;
|
2013-06-18 15:03:28 +00:00
|
|
|
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;
|
|
|
|
}
|