llvm-jitlink-macho.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //===-- llvm-jitlink-macho.cpp -- MachO parsing support for llvm-jitlink --===//
  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 parsing support for llvm-jitlink.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm-jitlink.h"
  13. #include "llvm/Support/Error.h"
  14. #include "llvm/Support/Path.h"
  15. #define DEBUG_TYPE "llvm_jitlink"
  16. using namespace llvm;
  17. using namespace llvm::jitlink;
  18. static bool isMachOGOTSection(Section &S) { return S.getName() == "$__GOT"; }
  19. static bool isMachOStubsSection(Section &S) {
  20. return S.getName() == "$__STUBS";
  21. }
  22. static Expected<Edge &> getFirstRelocationEdge(LinkGraph &G, Block &B) {
  23. auto EItr = std::find_if(B.edges().begin(), B.edges().end(),
  24. [](Edge &E) { return E.isRelocation(); });
  25. if (EItr == B.edges().end())
  26. return make_error<StringError>("GOT entry in " + G.getName() + ", \"" +
  27. B.getSection().getName() +
  28. "\" has no relocations",
  29. inconvertibleErrorCode());
  30. return *EItr;
  31. }
  32. static Expected<Symbol &> getMachOGOTTarget(LinkGraph &G, Block &B) {
  33. auto E = getFirstRelocationEdge(G, B);
  34. if (!E)
  35. return E.takeError();
  36. auto &TargetSym = E->getTarget();
  37. if (!TargetSym.hasName())
  38. return make_error<StringError>(
  39. "GOT entry in " + G.getName() + ", \"" +
  40. TargetSym.getBlock().getSection().getName() +
  41. "\" points to anonymous "
  42. "symbol",
  43. inconvertibleErrorCode());
  44. return TargetSym;
  45. }
  46. static Expected<Symbol &> getMachOStubTarget(LinkGraph &G, Block &B) {
  47. auto E = getFirstRelocationEdge(G, B);
  48. if (!E)
  49. return E.takeError();
  50. auto &GOTSym = E->getTarget();
  51. if (!GOTSym.isDefined() || !isMachOGOTSection(GOTSym.getBlock().getSection()))
  52. return make_error<StringError>(
  53. "Stubs entry in " + G.getName() + ", \"" +
  54. GOTSym.getBlock().getSection().getName() +
  55. "\" does not point to GOT entry",
  56. inconvertibleErrorCode());
  57. return getMachOGOTTarget(G, GOTSym.getBlock());
  58. }
  59. namespace llvm {
  60. Error registerMachOGraphInfo(Session &S, LinkGraph &G) {
  61. auto FileName = sys::path::filename(G.getName());
  62. if (S.FileInfos.count(FileName)) {
  63. return make_error<StringError>("When -check is passed, file names must be "
  64. "distinct (duplicate: \"" +
  65. FileName + "\")",
  66. inconvertibleErrorCode());
  67. }
  68. auto &FileInfo = S.FileInfos[FileName];
  69. LLVM_DEBUG({
  70. dbgs() << "Registering MachO file info for \"" << FileName << "\"\n";
  71. });
  72. for (auto &Sec : G.sections()) {
  73. LLVM_DEBUG({
  74. dbgs() << " Section \"" << Sec.getName() << "\": "
  75. << (llvm::empty(Sec.symbols()) ? "empty. skipping."
  76. : "processing...")
  77. << "\n";
  78. });
  79. // Skip empty sections.
  80. if (llvm::empty(Sec.symbols()))
  81. continue;
  82. if (FileInfo.SectionInfos.count(Sec.getName()))
  83. return make_error<StringError>("Encountered duplicate section name \"" +
  84. Sec.getName() + "\" in \"" + FileName +
  85. "\"",
  86. inconvertibleErrorCode());
  87. bool isGOTSection = isMachOGOTSection(Sec);
  88. bool isStubsSection = isMachOStubsSection(Sec);
  89. bool SectionContainsContent = false;
  90. bool SectionContainsZeroFill = false;
  91. auto *FirstSym = *Sec.symbols().begin();
  92. auto *LastSym = FirstSym;
  93. for (auto *Sym : Sec.symbols()) {
  94. if (Sym->getAddress() < FirstSym->getAddress())
  95. FirstSym = Sym;
  96. if (Sym->getAddress() > LastSym->getAddress())
  97. LastSym = Sym;
  98. if (isGOTSection) {
  99. if (Sym->isSymbolZeroFill())
  100. return make_error<StringError>("zero-fill atom in GOT section",
  101. inconvertibleErrorCode());
  102. if (auto TS = getMachOGOTTarget(G, Sym->getBlock()))
  103. FileInfo.GOTEntryInfos[TS->getName()] = {
  104. Sym->getSymbolContent(), Sym->getAddress().getValue()};
  105. else
  106. return TS.takeError();
  107. SectionContainsContent = true;
  108. } else if (isStubsSection) {
  109. if (Sym->isSymbolZeroFill())
  110. return make_error<StringError>("zero-fill atom in Stub section",
  111. inconvertibleErrorCode());
  112. if (auto TS = getMachOStubTarget(G, Sym->getBlock()))
  113. FileInfo.StubInfos[TS->getName()] = {Sym->getSymbolContent(),
  114. Sym->getAddress().getValue()};
  115. else
  116. return TS.takeError();
  117. SectionContainsContent = true;
  118. } else if (Sym->hasName()) {
  119. if (Sym->isSymbolZeroFill()) {
  120. S.SymbolInfos[Sym->getName()] = {Sym->getSize(),
  121. Sym->getAddress().getValue()};
  122. SectionContainsZeroFill = true;
  123. } else {
  124. S.SymbolInfos[Sym->getName()] = {Sym->getSymbolContent(),
  125. Sym->getAddress().getValue()};
  126. SectionContainsContent = true;
  127. }
  128. }
  129. }
  130. auto SecAddr = FirstSym->getAddress();
  131. auto SecSize =
  132. (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) -
  133. SecAddr;
  134. if (SectionContainsZeroFill && SectionContainsContent)
  135. return make_error<StringError>("Mixed zero-fill and content sections not "
  136. "supported yet",
  137. inconvertibleErrorCode());
  138. if (SectionContainsZeroFill)
  139. FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr.getValue()};
  140. else
  141. FileInfo.SectionInfos[Sec.getName()] = {
  142. ArrayRef<char>(FirstSym->getBlock().getContent().data(), SecSize),
  143. SecAddr.getValue()};
  144. }
  145. return Error::success();
  146. }
  147. } // end namespace llvm