2013-06-18 17:03:28 +02: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"
|
2015-01-14 12:23:27 +01:00
|
|
|
#include "llvm/Object/Archive.h"
|
2013-06-18 17:03:28 +02:00
|
|
|
#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 SwapStruct(T &Value);
|
|
|
|
|
|
|
|
template<>
|
2013-09-01 06:28:48 +02:00
|
|
|
void SwapStruct(MachO::fat_header &H) {
|
2014-06-14 15:49:57 +02:00
|
|
|
sys::swapByteOrder(H.magic);
|
|
|
|
sys::swapByteOrder(H.nfat_arch);
|
2013-06-18 17:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2013-09-01 06:28:48 +02:00
|
|
|
void SwapStruct(MachO::fat_arch &H) {
|
2014-06-14 15:49:57 +02:00
|
|
|
sys::swapByteOrder(H.cputype);
|
|
|
|
sys::swapByteOrder(H.cpusubtype);
|
|
|
|
sys::swapByteOrder(H.offset);
|
|
|
|
sys::swapByteOrder(H.size);
|
|
|
|
sys::swapByteOrder(H.align);
|
2013-06-18 17:03:28 +02: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-06-19 00:04:40 +02:00
|
|
|
if (!Parent || Index >= Parent->getNumberOfObjects()) {
|
2013-06-18 17:03:28 +02:00
|
|
|
clear();
|
|
|
|
} else {
|
|
|
|
// Parse object header.
|
|
|
|
StringRef ParentData = Parent->getData();
|
2013-09-01 06:28:48 +02: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 17:03:28 +02:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 22:30:57 +02:00
|
|
|
ErrorOr<std::unique_ptr<MachOObjectFile>>
|
2014-06-23 22:41:02 +02:00
|
|
|
MachOUniversalBinary::ObjectForArch::getAsObjectFile() const {
|
2013-06-18 17:03:28 +02:00
|
|
|
if (Parent) {
|
|
|
|
StringRef ParentData = Parent->getData();
|
2013-09-01 06:28:48 +02:00
|
|
|
StringRef ObjectData = ParentData.substr(Header.offset, Header.size);
|
2014-08-19 20:59:14 +02:00
|
|
|
StringRef ObjectName = Parent->getFileName();
|
2014-08-19 20:44:46 +02:00
|
|
|
MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
|
2014-06-23 22:41:02 +02:00
|
|
|
return ObjectFile::createMachOObjectFile(ObjBuffer);
|
2013-06-18 17:03:28 +02:00
|
|
|
}
|
|
|
|
return object_error::parse_failed;
|
2014-05-14 23:18:50 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 22:05:36 +01:00
|
|
|
ErrorOr<std::unique_ptr<Archive>>
|
|
|
|
MachOUniversalBinary::ObjectForArch::getAsArchive() const {
|
|
|
|
if (!Parent)
|
|
|
|
return object_error::parse_failed;
|
|
|
|
|
|
|
|
StringRef ParentData = Parent->getData();
|
|
|
|
StringRef ObjectData = ParentData.substr(Header.offset, Header.size);
|
|
|
|
StringRef ObjectName = Parent->getFileName();
|
|
|
|
MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
|
|
|
|
return Archive::create(ObjBuffer);
|
2013-06-18 17:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachOUniversalBinary::anchor() { }
|
|
|
|
|
2014-09-03 19:41:05 +02:00
|
|
|
ErrorOr<std::unique_ptr<MachOUniversalBinary>>
|
2014-08-19 20:44:46 +02:00
|
|
|
MachOUniversalBinary::create(MemoryBufferRef Source) {
|
2014-06-13 04:24:39 +02:00
|
|
|
std::error_code EC;
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<MachOUniversalBinary> Ret(
|
2014-08-19 20:44:46 +02:00
|
|
|
new MachOUniversalBinary(Source, EC));
|
2014-01-22 00:06:54 +01:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2014-09-03 19:41:05 +02:00
|
|
|
return std::move(Ret);
|
2014-01-22 00:06:54 +01:00
|
|
|
}
|
|
|
|
|
2014-08-19 20:44:46 +02:00
|
|
|
MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source,
|
2014-06-13 04:24:39 +02:00
|
|
|
std::error_code &ec)
|
2014-08-19 20:44:46 +02:00
|
|
|
: Binary(Binary::ID_MachOUniversalBinary, Source), NumberOfObjects(0) {
|
|
|
|
if (Data.getBufferSize() < sizeof(MachO::fat_header)) {
|
2013-06-18 17:03:28 +02:00
|
|
|
ec = object_error::invalid_file_type;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check for magic value and sufficient header size.
|
|
|
|
StringRef Buf = getData();
|
2013-09-01 06:28:48 +02: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 17:03:28 +02:00
|
|
|
ec = object_error::parse_failed;
|
|
|
|
return;
|
|
|
|
}
|
2015-06-09 17:20:42 +02:00
|
|
|
ec = std::error_code();
|
2013-06-18 17:03:28 +02:00
|
|
|
}
|
|
|
|
|
2014-10-20 22:30:57 +02:00
|
|
|
ErrorOr<std::unique_ptr<MachOObjectFile>>
|
2015-06-22 23:33:24 +02:00
|
|
|
MachOUniversalBinary::getObjectForArch(StringRef ArchName) const {
|
|
|
|
if (Triple(ArchName).getArch() == Triple::ArchType::UnknownArch)
|
2013-06-18 17:03:28 +02:00
|
|
|
return object_error::arch_not_found;
|
2015-06-22 23:33:24 +02:00
|
|
|
|
2013-06-18 17:03:28 +02:00
|
|
|
for (object_iterator I = begin_objects(), E = end_objects(); I != E; ++I) {
|
2015-06-22 23:33:24 +02:00
|
|
|
if (I->getArchTypeName() == ArchName)
|
2014-06-23 22:41:02 +02:00
|
|
|
return I->getAsObjectFile();
|
2013-06-18 17:03:28 +02:00
|
|
|
}
|
|
|
|
return object_error::arch_not_found;
|
|
|
|
}
|