llvm-jitlink-macho.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 =
  24. llvm::find_if(B.edges(), [](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. << (Sec.symbols().empty() ? "empty. skipping." : "processing...")
  76. << "\n";
  77. });
  78. // Skip empty sections.
  79. if (Sec.symbols().empty())
  80. continue;
  81. if (FileInfo.SectionInfos.count(Sec.getName()))
  82. return make_error<StringError>("Encountered duplicate section name \"" +
  83. Sec.getName() + "\" in \"" + FileName +
  84. "\"",
  85. inconvertibleErrorCode());
  86. bool isGOTSection = isMachOGOTSection(Sec);
  87. bool isStubsSection = isMachOStubsSection(Sec);
  88. bool SectionContainsContent = false;
  89. bool SectionContainsZeroFill = false;
  90. auto *FirstSym = *Sec.symbols().begin();
  91. auto *LastSym = FirstSym;
  92. for (auto *Sym : Sec.symbols()) {
  93. if (Sym->getAddress() < FirstSym->getAddress())
  94. FirstSym = Sym;
  95. if (Sym->getAddress() > LastSym->getAddress())
  96. LastSym = Sym;
  97. if (isGOTSection) {
  98. if (Sym->isSymbolZeroFill())
  99. return make_error<StringError>("zero-fill atom in GOT section",
  100. inconvertibleErrorCode());
  101. if (auto TS = getMachOGOTTarget(G, Sym->getBlock()))
  102. FileInfo.GOTEntryInfos[TS->getName()] = {
  103. Sym->getSymbolContent(), Sym->getAddress().getValue()};
  104. else
  105. return TS.takeError();
  106. SectionContainsContent = true;
  107. } else if (isStubsSection) {
  108. if (Sym->isSymbolZeroFill())
  109. return make_error<StringError>("zero-fill atom in Stub section",
  110. inconvertibleErrorCode());
  111. if (auto TS = getMachOStubTarget(G, Sym->getBlock()))
  112. FileInfo.StubInfos[TS->getName()] = {Sym->getSymbolContent(),
  113. Sym->getAddress().getValue()};
  114. else
  115. return TS.takeError();
  116. SectionContainsContent = true;
  117. } else if (Sym->hasName()) {
  118. if (Sym->isSymbolZeroFill()) {
  119. S.SymbolInfos[Sym->getName()] = {Sym->getSize(),
  120. Sym->getAddress().getValue()};
  121. SectionContainsZeroFill = true;
  122. } else {
  123. S.SymbolInfos[Sym->getName()] = {Sym->getSymbolContent(),
  124. Sym->getAddress().getValue()};
  125. SectionContainsContent = true;
  126. }
  127. }
  128. }
  129. auto SecAddr = FirstSym->getAddress();
  130. auto SecSize =
  131. (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) -
  132. SecAddr;
  133. if (SectionContainsZeroFill && SectionContainsContent)
  134. return make_error<StringError>("Mixed zero-fill and content sections not "
  135. "supported yet",
  136. inconvertibleErrorCode());
  137. if (SectionContainsZeroFill)
  138. FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr.getValue()};
  139. else
  140. FileInfo.SectionInfos[Sec.getName()] = {
  141. ArrayRef<char>(FirstSym->getBlock().getContent().data(), SecSize),
  142. SecAddr.getValue()};
  143. }
  144. return Error::success();
  145. }
  146. } // end namespace llvm