llvm-jitlink-elf.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //===---- llvm-jitlink-elf.cpp -- ELF 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. // ELF 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 isELFGOTSection(Section &S) { return S.getName() == "$__GOT"; }
  19. static bool isELFStubsSection(Section &S) { return S.getName() == "$__STUBS"; }
  20. static Expected<Edge &> getFirstRelocationEdge(LinkGraph &G, Block &B) {
  21. auto EItr = std::find_if(B.edges().begin(), B.edges().end(),
  22. [](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 &> getELFGOTTarget(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 &> getELFStubTarget(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() || !isELFGOTSection(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 getELFGOTTarget(G, GOTSym.getBlock());
  56. }
  57. namespace llvm {
  58. Error registerELFGraphInfo(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 ELF file info for \"" << FileName << "\"\n";
  69. });
  70. for (auto &Sec : G.sections()) {
  71. LLVM_DEBUG({
  72. dbgs() << " Section \"" << Sec.getName() << "\": "
  73. << (llvm::empty(Sec.symbols()) ? "empty. skipping."
  74. : "processing...")
  75. << "\n";
  76. });
  77. // Skip empty sections.
  78. if (llvm::empty(Sec.symbols()))
  79. continue;
  80. if (FileInfo.SectionInfos.count(Sec.getName()))
  81. return make_error<StringError>("Encountered duplicate section name \"" +
  82. Sec.getName() + "\" in \"" + FileName +
  83. "\"",
  84. inconvertibleErrorCode());
  85. bool isGOTSection = isELFGOTSection(Sec);
  86. bool isStubsSection = isELFStubsSection(Sec);
  87. bool SectionContainsContent = false;
  88. bool SectionContainsZeroFill = false;
  89. auto *FirstSym = *Sec.symbols().begin();
  90. auto *LastSym = FirstSym;
  91. for (auto *Sym : Sec.symbols()) {
  92. if (Sym->getAddress() < FirstSym->getAddress())
  93. FirstSym = Sym;
  94. if (Sym->getAddress() > LastSym->getAddress())
  95. LastSym = Sym;
  96. if (isGOTSection) {
  97. if (Sym->isSymbolZeroFill())
  98. return make_error<StringError>("zero-fill atom in GOT section",
  99. inconvertibleErrorCode());
  100. // If this is a GOT symbol with size (i.e. not the GOT start symbol)
  101. // then add it to the GOT entry info table.
  102. if (Sym->getSize() != 0) {
  103. if (auto TS = getELFGOTTarget(G, Sym->getBlock()))
  104. FileInfo.GOTEntryInfos[TS->getName()] = {
  105. Sym->getSymbolContent(), Sym->getAddress().getValue()};
  106. else
  107. return TS.takeError();
  108. }
  109. SectionContainsContent = true;
  110. } else if (isStubsSection) {
  111. if (Sym->isSymbolZeroFill())
  112. return make_error<StringError>("zero-fill atom in Stub section",
  113. inconvertibleErrorCode());
  114. if (auto TS = getELFStubTarget(G, Sym->getBlock()))
  115. FileInfo.StubInfos[TS->getName()] = {Sym->getSymbolContent(),
  116. Sym->getAddress().getValue()};
  117. else
  118. return TS.takeError();
  119. SectionContainsContent = true;
  120. }
  121. if (Sym->hasName()) {
  122. if (Sym->isSymbolZeroFill()) {
  123. S.SymbolInfos[Sym->getName()] = {Sym->getSize(),
  124. Sym->getAddress().getValue()};
  125. SectionContainsZeroFill = true;
  126. } else {
  127. S.SymbolInfos[Sym->getName()] = {Sym->getSymbolContent(),
  128. Sym->getAddress().getValue()};
  129. SectionContainsContent = true;
  130. }
  131. }
  132. }
  133. auto SecAddr = FirstSym->getAddress();
  134. auto SecSize =
  135. (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) -
  136. SecAddr;
  137. if (SectionContainsZeroFill && SectionContainsContent)
  138. return make_error<StringError>("Mixed zero-fill and content sections not "
  139. "supported yet",
  140. inconvertibleErrorCode());
  141. if (SectionContainsZeroFill)
  142. FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr.getValue()};
  143. else
  144. FileInfo.SectionInfos[Sec.getName()] = {
  145. ArrayRef<char>(FirstSym->getBlock().getContent().data(), SecSize),
  146. SecAddr.getValue()};
  147. }
  148. return Error::success();
  149. }
  150. } // end namespace llvm