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"
|
2014-05-14 21:18:50 +00:00
|
|
|
#include "llvm/Object/Archive.h"
|
2013-06-18 15:03:28 +00:00
|
|
|
#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-09-01 04:28:48 +00:00
|
|
|
void SwapStruct(MachO::fat_header &H) {
|
|
|
|
SwapValue(H.magic);
|
|
|
|
SwapValue(H.nfat_arch);
|
2013-06-18 15:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2013-09-01 04:28:48 +00:00
|
|
|
void SwapStruct(MachO::fat_arch &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) {
|
2014-04-15 06:32:26 +00:00
|
|
|
if (!Parent || Index > Parent->getNumberOfObjects()) {
|
2013-06-18 15:03:28 +00:00
|
|
|
clear();
|
|
|
|
} else {
|
|
|
|
// Parse object header.
|
|
|
|
StringRef ParentData = Parent->getData();
|
2013-09-01 04:28:48 +00:00
|
|
|
const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
|
|
|
|
Index * sizeof(MachO::fat_arch);
|
|
|
|
Header = getUniversalBinaryStruct<MachO::fat_arch>(HeaderPos);
|
|
|
|
if (ParentData.size() < Header.offset + Header.size) {
|
2013-06-18 15:03:28 +00:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error_code MachOUniversalBinary::ObjectForArch::getAsObjectFile(
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<ObjectFile> &Result) const {
|
2013-06-18 15:03:28 +00:00
|
|
|
if (Parent) {
|
|
|
|
StringRef ParentData = Parent->getData();
|
2013-09-01 04:28:48 +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-09-01 04:28:48 +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);
|
2014-01-21 23:06:54 +00:00
|
|
|
ErrorOr<ObjectFile *> Obj = ObjectFile::createMachOObjectFile(ObjBuffer);
|
|
|
|
if (error_code EC = Obj.getError())
|
|
|
|
return EC;
|
|
|
|
Result.reset(Obj.get());
|
|
|
|
return object_error::success;
|
2013-06-18 15:03:28 +00:00
|
|
|
}
|
|
|
|
return object_error::parse_failed;
|
2014-05-14 21:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-06-18 15:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachOUniversalBinary::anchor() { }
|
|
|
|
|
2014-01-21 23:06:54 +00:00
|
|
|
ErrorOr<MachOUniversalBinary *>
|
|
|
|
MachOUniversalBinary::create(MemoryBuffer *Source) {
|
|
|
|
error_code EC;
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<MachOUniversalBinary> Ret(
|
|
|
|
new MachOUniversalBinary(Source, EC));
|
2014-01-21 23:06:54 +00:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2014-03-05 10:19:29 +00:00
|
|
|
return Ret.release();
|
2014-01-21 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
2013-06-18 15:03:28 +00:00
|
|
|
MachOUniversalBinary::MachOUniversalBinary(MemoryBuffer *Source,
|
|
|
|
error_code &ec)
|
|
|
|
: Binary(Binary::ID_MachOUniversalBinary, Source),
|
|
|
|
NumberOfObjects(0) {
|
2013-09-01 04:28:48 +00:00
|
|
|
if (Source->getBufferSize() < sizeof(MachO::fat_header)) {
|
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-09-01 04:28:48 +00:00
|
|
|
MachO::fat_header H= getUniversalBinaryStruct<MachO::fat_header>(Buf.begin());
|
|
|
|
NumberOfObjects = H.nfat_arch;
|
|
|
|
uint32_t MinSize = sizeof(MachO::fat_header) +
|
|
|
|
sizeof(MachO::fat_arch) * NumberOfObjects;
|
|
|
|
if (H.magic != MachO::FAT_MAGIC || Buf.size() < MinSize) {
|
2013-06-18 15:03:28 +00:00
|
|
|
ec = object_error::parse_failed;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ec = object_error::success;
|
|
|
|
}
|
|
|
|
|
2013-09-01 04:28:48 +00:00
|
|
|
static bool getCTMForArch(Triple::ArchType Arch, MachO::CPUType &CTM) {
|
2013-06-18 15:03:28 +00:00
|
|
|
switch (Arch) {
|
2013-09-01 04:28:48 +00:00
|
|
|
case Triple::x86: CTM = MachO::CPU_TYPE_I386; return true;
|
|
|
|
case Triple::x86_64: CTM = MachO::CPU_TYPE_X86_64; return true;
|
|
|
|
case Triple::arm: CTM = MachO::CPU_TYPE_ARM; return true;
|
|
|
|
case Triple::sparc: CTM = MachO::CPU_TYPE_SPARC; return true;
|
|
|
|
case Triple::ppc: CTM = MachO::CPU_TYPE_POWERPC; return true;
|
|
|
|
case Triple::ppc64: CTM = MachO::CPU_TYPE_POWERPC64; return true;
|
2013-06-18 15:03:28 +00:00
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 05:51:42 +00:00
|
|
|
error_code MachOUniversalBinary::getObjectForArch(
|
|
|
|
Triple::ArchType Arch, std::unique_ptr<ObjectFile> &Result) const {
|
2013-09-01 04:28:48 +00:00
|
|
|
MachO::CPUType 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;
|
|
|
|
}
|