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 
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/Format.h"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "jitlink"
25 
26 namespace llvm {
27 namespace jitlink {
28 
29 void jitLink_MachO(std::unique_ptr<JITLinkContext> Ctx) {
30 
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.
33 
34  StringRef Data = Ctx->getObjectBuffer().getBuffer();
35  if (Data.size() < 4) {
36  Ctx->notifyFailed(make_error<JITLinkError>("Truncated MachO buffer"));
37  return;
38  }
39 
41  memcpy(&Magic, Data.data(), sizeof(uint32_t));
42  LLVM_DEBUG({
43  dbgs() << "jitLink_MachO: magic = " << format("0x%08" PRIx32, Magic)
44  << ", identifier = \""
45  << Ctx->getObjectBuffer().getBufferIdentifier() << "\"\n";
46  });
47 
48  if (Magic == MachO::MH_MAGIC || Magic == MachO::MH_CIGAM) {
49  Ctx->notifyFailed(
50  make_error<JITLinkError>("MachO 32-bit platforms not supported"));
51  return;
52  } else if (Magic == MachO::MH_MAGIC_64 || Magic == MachO::MH_CIGAM_64) {
53  MachO::mach_header_64 Header;
54 
55  memcpy(&Header, Data.data(), sizeof(MachO::mach_header_64));
56  if (Magic == MachO::MH_CIGAM_64)
57  swapStruct(Header);
58 
59  LLVM_DEBUG({
60  dbgs() << "jitLink_MachO: cputype = "
61  << format("0x%08" PRIx32, Header.cputype)
62  << ", cpusubtype = " << format("0x%08" PRIx32, Header.cpusubtype)
63  << "\n";
64  });
65 
66  switch (Header.cputype) {
68  return jitLink_MachO_x86_64(std::move(Ctx));
69  }
70  Ctx->notifyFailed(make_error<JITLinkError>("MachO-64 CPU type not valid"));
71  return;
72  }
73 
74  Ctx->notifyFailed(make_error<JITLinkError>("MachO magic not valid"));
75 }
76 
77 } // end namespace jitlink
78 } // 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