llvm-jitlink-elf.cpp 6.2 KB

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