1 //===-------------- MachO.cpp - JIT linker function for MachO -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // MachO jit-link function.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ExecutionEngine/JITLink/MachO.h"
16 #include "llvm/BinaryFormat/MachO.h"
17 #include "llvm/ExecutionEngine/JITLink/MachO_x86_64.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/Format.h"
20 #include "llvm/Support/MemoryBuffer.h"
24 #define DEBUG_TYPE "jitlink"
29 void jitLink_MachO(std::unique_ptr
<JITLinkContext
> Ctx
) {
31 // We don't want to do full MachO validation here. Just parse enough of the
32 // header to find out what MachO linker to use.
34 StringRef Data
= Ctx
->getObjectBuffer().getBuffer();
35 if (Data
.size() < 4) {
36 Ctx
->notifyFailed(make_error
<JITLinkError
>("Truncated MachO buffer"));
41 memcpy(&Magic
, Data
.data(), sizeof(uint32_t));
43 dbgs() << "jitLink_MachO: magic = " << format("0x%08" PRIx32
, Magic
)
44 << ", identifier = \""
45 << Ctx
->getObjectBuffer().getBufferIdentifier() << "\"\n";
48 if (Magic
== MachO::MH_MAGIC
|| Magic
== MachO::MH_CIGAM
) {
50 make_error
<JITLinkError
>("MachO 32-bit platforms not supported"));
52 } else if (Magic
== MachO::MH_MAGIC_64
|| Magic
== MachO::MH_CIGAM_64
) {
53 MachO::mach_header_64 Header
;
55 memcpy(&Header
, Data
.data(), sizeof(MachO::mach_header_64
));
56 if (Magic
== MachO::MH_CIGAM_64
)
60 dbgs() << "jitLink_MachO: cputype = "
61 << format("0x%08" PRIx32
, Header
.cputype
)
62 << ", cpusubtype = " << format("0x%08" PRIx32
, Header
.cpusubtype
)
66 switch (Header
.cputype
) {
67 case MachO::CPU_TYPE_X86_64
:
68 return jitLink_MachO_x86_64(std::move(Ctx
));
70 Ctx
->notifyFailed(make_error
<JITLinkError
>("MachO-64 CPU type not valid"));
74 Ctx
->notifyFailed(make_error
<JITLinkError
>("MachO magic not valid"));
77 } // end namespace jitlink
78 } // end namespace llvm