MachO.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //===-------------- MachO.cpp - JIT linker function for MachO -------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // MachO jit-link function.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ExecutionEngine/JITLink/MachO.h"
  13. #include "llvm/BinaryFormat/MachO.h"
  14. #include "llvm/ExecutionEngine/JITLink/MachO_arm64.h"
  15. #include "llvm/ExecutionEngine/JITLink/MachO_x86_64.h"
  16. #include "llvm/Support/Format.h"
  17. #include "llvm/Support/MemoryBuffer.h"
  18. #include "llvm/Support/SwapByteOrder.h"
  19. using namespace llvm;
  20. #define DEBUG_TYPE "jitlink"
  21. namespace llvm {
  22. namespace jitlink {
  23. Expected<std::unique_ptr<LinkGraph>>
  24. createLinkGraphFromMachOObject(MemoryBufferRef ObjectBuffer) {
  25. StringRef Data = ObjectBuffer.getBuffer();
  26. if (Data.size() < 4)
  27. return make_error<JITLinkError>("Truncated MachO buffer \"" +
  28. ObjectBuffer.getBufferIdentifier() + "\"");
  29. uint32_t Magic;
  30. memcpy(&Magic, Data.data(), sizeof(uint32_t));
  31. LLVM_DEBUG({
  32. dbgs() << "jitLink_MachO: magic = " << format("0x%08" PRIx32, Magic)
  33. << ", identifier = \"" << ObjectBuffer.getBufferIdentifier()
  34. << "\"\n";
  35. });
  36. if (Magic == MachO::MH_MAGIC || Magic == MachO::MH_CIGAM)
  37. return make_error<JITLinkError>("MachO 32-bit platforms not supported");
  38. else if (Magic == MachO::MH_MAGIC_64 || Magic == MachO::MH_CIGAM_64) {
  39. if (Data.size() < sizeof(MachO::mach_header_64))
  40. return make_error<JITLinkError>("Truncated MachO buffer \"" +
  41. ObjectBuffer.getBufferIdentifier() +
  42. "\"");
  43. // Read the CPU type from the header.
  44. uint32_t CPUType;
  45. memcpy(&CPUType, Data.data() + 4, sizeof(uint32_t));
  46. if (Magic == MachO::MH_CIGAM_64)
  47. CPUType = ByteSwap_32(CPUType);
  48. LLVM_DEBUG({
  49. dbgs() << "jitLink_MachO: cputype = " << format("0x%08" PRIx32, CPUType)
  50. << "\n";
  51. });
  52. switch (CPUType) {
  53. case MachO::CPU_TYPE_ARM64:
  54. return createLinkGraphFromMachOObject_arm64(ObjectBuffer);
  55. case MachO::CPU_TYPE_X86_64:
  56. return createLinkGraphFromMachOObject_x86_64(ObjectBuffer);
  57. }
  58. return make_error<JITLinkError>("MachO-64 CPU type not valid");
  59. } else
  60. return make_error<JITLinkError>("Unrecognized MachO magic value");
  61. }
  62. void link_MachO(std::unique_ptr<LinkGraph> G,
  63. std::unique_ptr<JITLinkContext> Ctx) {
  64. switch (G->getTargetTriple().getArch()) {
  65. case Triple::aarch64:
  66. return link_MachO_arm64(std::move(G), std::move(Ctx));
  67. case Triple::x86_64:
  68. return link_MachO_x86_64(std::move(G), std::move(Ctx));
  69. default:
  70. Ctx->notifyFailed(make_error<JITLinkError>("MachO-64 CPU type not valid"));
  71. return;
  72. }
  73. }
  74. } // end namespace jitlink
  75. } // end namespace llvm