RuntimeDyldMachOX86_64.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //===-- RuntimeDyldMachOX86_64.h ---- MachO/X86_64 specific code. -*- C++ -*-=//
  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. #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H
  10. #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H
  11. #include "../RuntimeDyldMachO.h"
  12. #define DEBUG_TYPE "dyld"
  13. namespace llvm {
  14. class RuntimeDyldMachOX86_64
  15. : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOX86_64> {
  16. public:
  17. typedef uint64_t TargetPtrT;
  18. RuntimeDyldMachOX86_64(RuntimeDyld::MemoryManager &MM,
  19. RuntimeDyld::SymbolResolver &Resolver)
  20. : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
  21. unsigned getMaxStubSize() override { return 8; }
  22. unsigned getStubAlignment() override { return 1; }
  23. relocation_iterator
  24. processRelocationRef(unsigned SectionID, relocation_iterator RelI,
  25. const ObjectFile &BaseObjT,
  26. ObjSectionToIDMap &ObjSectionToID,
  27. StubMap &Stubs) override {
  28. const MachOObjectFile &Obj =
  29. static_cast<const MachOObjectFile &>(BaseObjT);
  30. MachO::any_relocation_info RelInfo =
  31. Obj.getRelocation(RelI->getRawDataRefImpl());
  32. assert(!Obj.isRelocationScattered(RelInfo) &&
  33. "Scattered relocations not supported on X86_64");
  34. RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
  35. RE.Addend = memcpyAddend(RE);
  36. RelocationValueRef Value(
  37. getRelocationValueRef(Obj, RelI, RE, ObjSectionToID));
  38. bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
  39. if (!IsExtern && RE.IsPCRel)
  40. makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
  41. if (RE.RelType == MachO::X86_64_RELOC_GOT ||
  42. RE.RelType == MachO::X86_64_RELOC_GOT_LOAD)
  43. processGOTRelocation(RE, Value, Stubs);
  44. else {
  45. RE.Addend = Value.Offset;
  46. if (Value.SymbolName)
  47. addRelocationForSymbol(RE, Value.SymbolName);
  48. else
  49. addRelocationForSection(RE, Value.SectionID);
  50. }
  51. return ++RelI;
  52. }
  53. void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
  54. DEBUG(dumpRelocationToResolve(RE, Value));
  55. const SectionEntry &Section = Sections[RE.SectionID];
  56. uint8_t *LocalAddress = Section.Address + RE.Offset;
  57. // If the relocation is PC-relative, the value to be encoded is the
  58. // pointer difference.
  59. if (RE.IsPCRel) {
  60. // FIXME: It seems this value needs to be adjusted by 4 for an effective
  61. // PC address. Is that expected? Only for branches, perhaps?
  62. uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
  63. Value -= FinalAddress + 4;
  64. }
  65. switch (RE.RelType) {
  66. default:
  67. llvm_unreachable("Invalid relocation type!");
  68. case MachO::X86_64_RELOC_SIGNED_1:
  69. case MachO::X86_64_RELOC_SIGNED_2:
  70. case MachO::X86_64_RELOC_SIGNED_4:
  71. case MachO::X86_64_RELOC_SIGNED:
  72. case MachO::X86_64_RELOC_UNSIGNED:
  73. case MachO::X86_64_RELOC_BRANCH:
  74. writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
  75. break;
  76. case MachO::X86_64_RELOC_GOT_LOAD:
  77. case MachO::X86_64_RELOC_GOT:
  78. case MachO::X86_64_RELOC_SUBTRACTOR:
  79. case MachO::X86_64_RELOC_TLV:
  80. Error("Relocation type not implemented yet!");
  81. }
  82. }
  83. void finalizeSection(const ObjectFile &Obj, unsigned SectionID,
  84. const SectionRef &Section) {}
  85. private:
  86. void processGOTRelocation(const RelocationEntry &RE,
  87. RelocationValueRef &Value, StubMap &Stubs) {
  88. SectionEntry &Section = Sections[RE.SectionID];
  89. assert(RE.IsPCRel);
  90. assert(RE.Size == 2);
  91. Value.Offset -= RE.Addend;
  92. RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
  93. uint8_t *Addr;
  94. if (i != Stubs.end()) {
  95. Addr = Section.Address + i->second;
  96. } else {
  97. Stubs[Value] = Section.StubOffset;
  98. uint8_t *GOTEntry = Section.Address + Section.StubOffset;
  99. RelocationEntry GOTRE(RE.SectionID, Section.StubOffset,
  100. MachO::X86_64_RELOC_UNSIGNED, Value.Offset, false,
  101. 3);
  102. if (Value.SymbolName)
  103. addRelocationForSymbol(GOTRE, Value.SymbolName);
  104. else
  105. addRelocationForSection(GOTRE, Value.SectionID);
  106. Section.StubOffset += 8;
  107. Addr = GOTEntry;
  108. }
  109. RelocationEntry TargetRE(RE.SectionID, RE.Offset,
  110. MachO::X86_64_RELOC_UNSIGNED, RE.Addend, true, 2);
  111. resolveRelocation(TargetRE, (uint64_t)Addr);
  112. }
  113. };
  114. }
  115. #undef DEBUG_TYPE
  116. #endif