llvm-jitlink-coff.cpp 6.2 KB

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