#include "llvm/BinaryFormat/MachO.h"
#include "llvm/ExecutionEngine/JITLink/MachO_arm64.h"
#include "llvm/ExecutionEngine/JITLink/MachO_x86_64.h"
-#include "llvm/Support/Endian.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/SwapByteOrder.h"
using namespace llvm;
StringRef Data = Ctx->getObjectBuffer().getBuffer();
if (Data.size() < 4) {
- Ctx->notifyFailed(make_error<JITLinkError>("Truncated MachO buffer"));
+ StringRef BufferName = Ctx->getObjectBuffer().getBufferIdentifier();
+ Ctx->notifyFailed(make_error<JITLinkError>("Truncated MachO buffer \"" +
+ BufferName + "\""));
return;
}
make_error<JITLinkError>("MachO 32-bit platforms not supported"));
return;
} else if (Magic == MachO::MH_MAGIC_64 || Magic == MachO::MH_CIGAM_64) {
- MachO::mach_header_64 Header;
- memcpy(&Header, Data.data(), sizeof(MachO::mach_header_64));
+ if (Data.size() < sizeof(MachO::mach_header_64)) {
+ StringRef BufferName = Ctx->getObjectBuffer().getBufferIdentifier();
+ Ctx->notifyFailed(make_error<JITLinkError>("Truncated MachO buffer \"" +
+ BufferName + "\""));
+ return;
+ }
+
+ // Read the CPU type from the header.
+ uint32_t CPUType;
+ memcpy(&CPUType, Data.data() + 4, sizeof(uint32_t));
if (Magic == MachO::MH_CIGAM_64)
- swapStruct(Header);
+ ByteSwap_32(CPUType);
LLVM_DEBUG({
- dbgs() << "jitLink_MachO: cputype = "
- << format("0x%08" PRIx32, Header.cputype)
- << ", cpusubtype = " << format("0x%08" PRIx32, Header.cpusubtype)
+ dbgs() << "jitLink_MachO: cputype = " << format("0x%08" PRIx32, CPUType)
<< "\n";
});
- switch (Header.cputype) {
+ switch (CPUType) {
case MachO::CPU_TYPE_ARM64:
return jitLink_MachO_arm64(std::move(Ctx));
case MachO::CPU_TYPE_X86_64: