2013-06-18 23:03:28 +08: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 19:23:27 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
2013-06-18 23:03:28 +08: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;
|
|
|
|
|
2016-06-28 05:39:39 +08:00
|
|
|
static Error
|
|
|
|
malformedError(Twine Msg) {
|
|
|
|
std::string StringMsg = "truncated or malformed fat file (" + Msg.str() + ")";
|
|
|
|
return make_error<GenericBinaryError>(std::move(StringMsg),
|
|
|
|
object_error::parse_failed);
|
|
|
|
}
|
|
|
|
|
2013-06-18 23:03:28 +08: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)
|
2016-06-23 06:19:08 +08:00
|
|
|
swapStruct(Res);
|
2013-06-18 23:03:28 +08:00
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachOUniversalBinary::ObjectForArch::ObjectForArch(
|
|
|
|
const MachOUniversalBinary *Parent, uint32_t Index)
|
|
|
|
: Parent(Parent), Index(Index) {
|
2014-06-19 06:04:40 +08:00
|
|
|
if (!Parent || Index >= Parent->getNumberOfObjects()) {
|
2013-06-18 23:03:28 +08:00
|
|
|
clear();
|
|
|
|
} else {
|
|
|
|
// Parse object header.
|
|
|
|
StringRef ParentData = Parent->getData();
|
2016-06-21 06:16:18 +08:00
|
|
|
if (Parent->getMagic() == MachO::FAT_MAGIC) {
|
|
|
|
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) {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
} else { // Parent->getMagic() == MachO::FAT_MAGIC_64
|
|
|
|
const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
|
|
|
|
Index * sizeof(MachO::fat_arch_64);
|
|
|
|
Header64 = getUniversalBinaryStruct<MachO::fat_arch_64>(HeaderPos);
|
|
|
|
if (ParentData.size() < Header64.offset + Header64.size) {
|
|
|
|
clear();
|
|
|
|
}
|
2013-06-18 23:03:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 04:35:34 +08:00
|
|
|
Expected<std::unique_ptr<MachOObjectFile>>
|
2014-06-24 04:41:02 +08:00
|
|
|
MachOUniversalBinary::ObjectForArch::getAsObjectFile() const {
|
2015-08-03 08:10:33 +08:00
|
|
|
if (!Parent)
|
2016-06-29 07:16:13 +08:00
|
|
|
report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsObjectFile() "
|
|
|
|
"called when Parent is a nullptr");
|
2015-08-03 08:10:33 +08:00
|
|
|
|
|
|
|
StringRef ParentData = Parent->getData();
|
2016-06-21 06:16:18 +08:00
|
|
|
StringRef ObjectData;
|
2016-10-25 05:15:11 +08:00
|
|
|
uint32_t cputype;
|
|
|
|
if (Parent->getMagic() == MachO::FAT_MAGIC) {
|
2016-06-21 06:16:18 +08:00
|
|
|
ObjectData = ParentData.substr(Header.offset, Header.size);
|
2016-10-25 05:15:11 +08:00
|
|
|
cputype = Header.cputype;
|
|
|
|
} else { // Parent->getMagic() == MachO::FAT_MAGIC_64
|
2016-06-21 06:16:18 +08:00
|
|
|
ObjectData = ParentData.substr(Header64.offset, Header64.size);
|
2016-10-25 05:15:11 +08:00
|
|
|
cputype = Header64.cputype;
|
|
|
|
}
|
2015-08-03 08:10:33 +08:00
|
|
|
StringRef ObjectName = Parent->getFileName();
|
|
|
|
MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
|
2016-10-25 05:15:11 +08:00
|
|
|
return ObjectFile::createMachOObjectFile(ObjBuffer, cputype, Index);
|
2014-05-15 05:18:50 +08:00
|
|
|
}
|
|
|
|
|
2016-06-29 07:16:13 +08:00
|
|
|
Expected<std::unique_ptr<Archive>>
|
2014-12-10 05:05:36 +08:00
|
|
|
MachOUniversalBinary::ObjectForArch::getAsArchive() const {
|
|
|
|
if (!Parent)
|
2016-06-29 07:16:13 +08:00
|
|
|
report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsArchive() "
|
|
|
|
"called when Parent is a nullptr");
|
2014-12-10 05:05:36 +08:00
|
|
|
|
|
|
|
StringRef ParentData = Parent->getData();
|
2016-06-21 06:16:18 +08:00
|
|
|
StringRef ObjectData;
|
|
|
|
if (Parent->getMagic() == MachO::FAT_MAGIC)
|
|
|
|
ObjectData = ParentData.substr(Header.offset, Header.size);
|
|
|
|
else // Parent->getMagic() == MachO::FAT_MAGIC_64
|
|
|
|
ObjectData = ParentData.substr(Header64.offset, Header64.size);
|
2014-12-10 05:05:36 +08:00
|
|
|
StringRef ObjectName = Parent->getFileName();
|
|
|
|
MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
|
2016-06-30 04:35:44 +08:00
|
|
|
return Archive::create(ObjBuffer);
|
2013-06-18 23:03:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachOUniversalBinary::anchor() { }
|
|
|
|
|
2016-06-28 05:39:39 +08:00
|
|
|
Expected<std::unique_ptr<MachOUniversalBinary>>
|
2014-08-20 02:44:46 +08:00
|
|
|
MachOUniversalBinary::create(MemoryBufferRef Source) {
|
2016-06-28 05:39:39 +08:00
|
|
|
Error Err;
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MachOUniversalBinary> Ret(
|
2016-06-28 05:39:39 +08:00
|
|
|
new MachOUniversalBinary(Source, Err));
|
|
|
|
if (Err)
|
|
|
|
return std::move(Err);
|
2014-09-04 01:41:05 +08:00
|
|
|
return std::move(Ret);
|
2014-01-22 07:06:54 +08:00
|
|
|
}
|
|
|
|
|
2016-06-28 05:39:39 +08:00
|
|
|
MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, Error &Err)
|
2016-06-21 06:16:18 +08:00
|
|
|
: Binary(Binary::ID_MachOUniversalBinary, Source), Magic(0),
|
|
|
|
NumberOfObjects(0) {
|
2016-07-23 00:11:25 +08:00
|
|
|
ErrorAsOutParameter ErrAsOutParam(&Err);
|
2014-08-20 02:44:46 +08:00
|
|
|
if (Data.getBufferSize() < sizeof(MachO::fat_header)) {
|
2016-06-28 05:39:39 +08:00
|
|
|
Err = make_error<GenericBinaryError>("File too small to be a Mach-O "
|
|
|
|
"universal file",
|
|
|
|
object_error::invalid_file_type);
|
2013-06-18 23:03:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check for magic value and sufficient header size.
|
|
|
|
StringRef Buf = getData();
|
2016-06-29 08:37:13 +08:00
|
|
|
MachO::fat_header H =
|
|
|
|
getUniversalBinaryStruct<MachO::fat_header>(Buf.begin());
|
2016-06-21 06:16:18 +08:00
|
|
|
Magic = H.magic;
|
2013-09-01 12:28:48 +08:00
|
|
|
NumberOfObjects = H.nfat_arch;
|
2016-06-21 06:16:18 +08:00
|
|
|
uint32_t MinSize = sizeof(MachO::fat_header);
|
|
|
|
if (Magic == MachO::FAT_MAGIC)
|
|
|
|
MinSize += sizeof(MachO::fat_arch) * NumberOfObjects;
|
|
|
|
else if (Magic == MachO::FAT_MAGIC_64)
|
|
|
|
MinSize += sizeof(MachO::fat_arch_64) * NumberOfObjects;
|
|
|
|
else {
|
2016-06-28 05:39:39 +08:00
|
|
|
Err = malformedError("bad magic number");
|
2016-06-21 06:16:18 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Buf.size() < MinSize) {
|
2016-06-28 05:39:39 +08:00
|
|
|
Err = malformedError("fat_arch" +
|
|
|
|
Twine(Magic == MachO::FAT_MAGIC ? "" : "_64") +
|
|
|
|
" structs would extend past the end of the file");
|
2013-06-18 23:03:28 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-06-28 05:39:39 +08:00
|
|
|
Err = Error::success();
|
2013-06-18 23:03:28 +08:00
|
|
|
}
|
|
|
|
|
2016-06-01 04:35:34 +08:00
|
|
|
Expected<std::unique_ptr<MachOObjectFile>>
|
2015-06-23 05:33:24 +08:00
|
|
|
MachOUniversalBinary::getObjectForArch(StringRef ArchName) const {
|
|
|
|
if (Triple(ArchName).getArch() == Triple::ArchType::UnknownArch)
|
2016-06-29 08:37:13 +08:00
|
|
|
return make_error<GenericBinaryError>("Unknown architecture "
|
|
|
|
"named: " +
|
|
|
|
ArchName,
|
2016-06-29 07:16:13 +08:00
|
|
|
object_error::arch_not_found);
|
2015-06-23 05:33:24 +08:00
|
|
|
|
2013-06-18 23:03:28 +08:00
|
|
|
for (object_iterator I = begin_objects(), E = end_objects(); I != E; ++I) {
|
2015-06-23 05:33:24 +08:00
|
|
|
if (I->getArchTypeName() == ArchName)
|
2014-06-24 04:41:02 +08:00
|
|
|
return I->getAsObjectFile();
|
2013-06-18 23:03:28 +08:00
|
|
|
}
|
2016-06-29 08:37:13 +08:00
|
|
|
return make_error<GenericBinaryError>("fat file does not "
|
|
|
|
"contain " +
|
|
|
|
ArchName,
|
2016-06-29 07:16:13 +08:00
|
|
|
object_error::arch_not_found);
|
2013-06-18 23:03:28 +08:00
|
|
|
}
|