2013-06-18 23:03:28 +08:00
|
|
|
//===- MachOUniversal.cpp - Mach-O universal binary -------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-06-18 23:03:28 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the MachOUniversalBinary class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Object/MachOUniversal.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
2020-08-26 09:55:50 +08:00
|
|
|
#include "llvm/Object/IRObjectFile.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) {
|
2016-11-29 06:40:50 +08:00
|
|
|
// The iterators use Parent as a nullptr and an Index+1 == NumberOfObjects.
|
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);
|
|
|
|
} 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);
|
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
|
|
|
}
|
|
|
|
|
2020-08-26 09:55:50 +08:00
|
|
|
Expected<std::unique_ptr<IRObjectFile>>
|
|
|
|
MachOUniversalBinary::ObjectForArch::getAsIRObject(LLVMContext &Ctx) const {
|
|
|
|
if (!Parent)
|
|
|
|
report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsIRObject() "
|
|
|
|
"called when Parent is a nullptr");
|
|
|
|
|
|
|
|
StringRef ParentData = Parent->getData();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
StringRef ObjectName = Parent->getFileName();
|
|
|
|
MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
|
|
|
|
|
|
|
|
return IRObjectFile::create(ObjBuffer, Ctx);
|
|
|
|
}
|
|
|
|
|
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-11-11 12:28:40 +08:00
|
|
|
Error Err = Error::success();
|
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)
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(Err);
|
|
|
|
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-11-29 06:40:50 +08:00
|
|
|
if (NumberOfObjects == 0) {
|
|
|
|
Err = malformedError("contains zero architecture types");
|
|
|
|
return;
|
|
|
|
}
|
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-11-29 06:40:50 +08:00
|
|
|
for (uint32_t i = 0; i < NumberOfObjects; i++) {
|
|
|
|
ObjectForArch A(this, i);
|
|
|
|
uint64_t bigSize = A.getOffset();
|
|
|
|
bigSize += A.getSize();
|
|
|
|
if (bigSize > Buf.size()) {
|
|
|
|
Err = malformedError("offset plus size of cputype (" +
|
|
|
|
Twine(A.getCPUType()) + ") cpusubtype (" +
|
|
|
|
Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
|
|
|
|
") extends past the end of the file");
|
|
|
|
return;
|
|
|
|
}
|
2019-07-25 08:29:13 +08:00
|
|
|
|
|
|
|
if (A.getAlign() > MaxSectionAlignment) {
|
|
|
|
Err = malformedError("align (2^" + Twine(A.getAlign()) +
|
|
|
|
") too large for cputype (" + Twine(A.getCPUType()) +
|
|
|
|
") cpusubtype (" +
|
|
|
|
Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
|
|
|
|
") (maximum 2^" + Twine(MaxSectionAlignment) + ")");
|
2016-11-29 06:40:50 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-10-18 17:59:31 +08:00
|
|
|
if(A.getOffset() % (1ull << A.getAlign()) != 0){
|
2016-11-29 06:40:50 +08:00
|
|
|
Err = malformedError("offset: " + Twine(A.getOffset()) +
|
|
|
|
" for cputype (" + Twine(A.getCPUType()) + ") cpusubtype (" +
|
|
|
|
Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
|
|
|
|
") not aligned on it's alignment (2^" + Twine(A.getAlign()) + ")");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (A.getOffset() < MinSize) {
|
|
|
|
Err = malformedError("cputype (" + Twine(A.getCPUType()) + ") "
|
|
|
|
"cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
|
|
|
|
") offset " + Twine(A.getOffset()) + " overlaps universal headers");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < NumberOfObjects; i++) {
|
|
|
|
ObjectForArch A(this, i);
|
|
|
|
for (uint32_t j = i + 1; j < NumberOfObjects; j++) {
|
|
|
|
ObjectForArch B(this, j);
|
|
|
|
if (A.getCPUType() == B.getCPUType() &&
|
|
|
|
(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) ==
|
|
|
|
(B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)) {
|
|
|
|
Err = malformedError("contains two of the same architecture (cputype "
|
|
|
|
"(" + Twine(A.getCPUType()) + ") cpusubtype (" +
|
|
|
|
Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) + "))");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((A.getOffset() >= B.getOffset() &&
|
|
|
|
A.getOffset() < B.getOffset() + B.getSize()) ||
|
|
|
|
(A.getOffset() + A.getSize() > B.getOffset() &&
|
|
|
|
A.getOffset() + A.getSize() < B.getOffset() + B.getSize()) ||
|
|
|
|
(A.getOffset() <= B.getOffset() &&
|
|
|
|
A.getOffset() + A.getSize() >= B.getOffset() + B.getSize())) {
|
|
|
|
Err = malformedError("cputype (" + Twine(A.getCPUType()) + ") "
|
|
|
|
"cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
|
|
|
|
") at offset " + Twine(A.getOffset()) + " with a size of " +
|
|
|
|
Twine(A.getSize()) + ", overlaps cputype (" + Twine(B.getCPUType()) +
|
|
|
|
") cpusubtype (" + Twine(B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)
|
|
|
|
+ ") at offset " + Twine(B.getOffset()) + " with a size of "
|
|
|
|
+ Twine(B.getSize()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-28 05:39:39 +08:00
|
|
|
Err = Error::success();
|
2013-06-18 23:03:28 +08:00
|
|
|
}
|
|
|
|
|
2019-09-19 08:02:12 +08:00
|
|
|
Expected<MachOUniversalBinary::ObjectForArch>
|
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);
|
2019-09-19 08:02:12 +08:00
|
|
|
for (const auto &Obj : objects())
|
2016-12-17 06:54:02 +08:00
|
|
|
if (Obj.getArchFlagName() == ArchName)
|
2019-09-19 08:02:12 +08:00
|
|
|
return Obj;
|
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
|
|
|
}
|
2019-09-19 08:02:12 +08:00
|
|
|
|
|
|
|
Expected<std::unique_ptr<MachOObjectFile>>
|
|
|
|
MachOUniversalBinary::getMachOObjectForArch(StringRef ArchName) const {
|
|
|
|
Expected<ObjectForArch> O = getObjectForArch(ArchName);
|
|
|
|
if (!O)
|
|
|
|
return O.takeError();
|
|
|
|
return O->getAsObjectFile();
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:55:50 +08:00
|
|
|
Expected<std::unique_ptr<IRObjectFile>>
|
|
|
|
MachOUniversalBinary::getIRObjectForArch(StringRef ArchName,
|
|
|
|
LLVMContext &Ctx) const {
|
|
|
|
Expected<ObjectForArch> O = getObjectForArch(ArchName);
|
|
|
|
if (!O)
|
|
|
|
return O.takeError();
|
|
|
|
return O->getAsIRObject(Ctx);
|
|
|
|
}
|
|
|
|
|
2019-09-19 08:02:12 +08:00
|
|
|
Expected<std::unique_ptr<Archive>>
|
|
|
|
MachOUniversalBinary::getArchiveForArch(StringRef ArchName) const {
|
|
|
|
Expected<ObjectForArch> O = getObjectForArch(ArchName);
|
|
|
|
if (!O)
|
|
|
|
return O.takeError();
|
|
|
|
return O->getAsArchive();
|
|
|
|
}
|