LLVM  10.0.0svn
MachO.cpp
Go to the documentation of this file.
1 //===-------------- MachO.cpp - JIT linker function for MachO -------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // MachO jit-link function.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 
19 #include "llvm/Support/Endian.h"
20 #include "llvm/Support/Format.h"
22 
23 using namespace llvm;
24 
25 #define DEBUG_TYPE "jitlink"
26 
27 namespace llvm {
28 namespace jitlink {
29 
30 void jitLink_MachO(std::unique_ptr<JITLinkContext> Ctx) {
31 
32  // We don't want to do full MachO validation here. Just parse enough of the
33  // header to find out what MachO linker to use.
34 
35  StringRef Data = Ctx->getObjectBuffer().getBuffer();
36  if (Data.size() < 4) {
37  Ctx->notifyFailed(make_error<JITLinkError>("Truncated MachO buffer"));
38  return;
39  }
40 
42  memcpy(&Magic, Data.data(), sizeof(uint32_t));
43  LLVM_DEBUG({
44  dbgs() << "jitLink_MachO: magic = " << format("0x%08" PRIx32, Magic)
45  << ", identifier = \""
46  << Ctx->getObjectBuffer().getBufferIdentifier() << "\"\n";
47  });
48 
49  if (Magic == MachO::MH_MAGIC || Magic == MachO::MH_CIGAM) {
50  Ctx->notifyFailed(
51  make_error<JITLinkError>("MachO 32-bit platforms not supported"));
52  return;
53  } else if (Magic == MachO::MH_MAGIC_64 || Magic == MachO::MH_CIGAM_64) {
54  MachO::mach_header_64 Header;
55 
56  memcpy(&Header, Data.data(), sizeof(MachO::mach_header_64));
57  if (Magic == MachO::MH_CIGAM_64)
58  swapStruct(Header);
59 
60  LLVM_DEBUG({
61  dbgs() << "jitLink_MachO: cputype = "
62  << format("0x%08" PRIx32, Header.cputype)
63  << ", cpusubtype = " << format("0x%08" PRIx32, Header.cpusubtype)
64  << "\n";
65  });
66 
67  switch (Header.cputype) {
69  return jitLink_MachO_arm64(std::move(Ctx));
71  return jitLink_MachO_x86_64(std::move(Ctx));
72  }
73  Ctx->notifyFailed(make_error<JITLinkError>("MachO-64 CPU type not valid"));
74  return;
75  }
76 
77  Ctx->notifyFailed(make_error<JITLinkError>("MachO magic not valid"));
78 }
79 
80 } // end namespace jitlink
81 } // end namespace llvm
void swapStruct(fat_header &mh)
Definition: MachO.h:1002
This class represents lattice values for constants.
Definition: AllocatorList.h:23
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:144
static const char *const Magic
Definition: Archive.cpp:41
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:136
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
#define LLVM_DEBUG(X)
Definition: Debug.h:122