i386.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //=== i386.h - Generic JITLink i386 edge kinds, utilities -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Generic utilities for graphs representing i386 objects.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_EXECUTIONENGINE_JITLINK_I386_H
  18. #define LLVM_EXECUTIONENGINE_JITLINK_I386_H
  19. #include "llvm/ExecutionEngine/JITLink/JITLink.h"
  20. #include "llvm/ExecutionEngine/JITLink/TableManager.h"
  21. namespace llvm::jitlink::i386 {
  22. /// Represets i386 fixups
  23. enum EdgeKind_i386 : Edge::Kind {
  24. /// None
  25. None = Edge::FirstRelocation,
  26. /// A plain 32-bit pointer value relocation.
  27. ///
  28. /// Fixup expression:
  29. /// Fixup <- Target + Addend : uint32
  30. ///
  31. /// Errors:
  32. /// - The target must reside in the low 32-bits of the address space,
  33. /// otherwise an out-of-range error will be returned.
  34. ///
  35. Pointer32,
  36. /// A 32-bit PC-relative relocation.
  37. ///
  38. /// Represents a data/control flow instruction using PC-relative addressing
  39. /// to a target.
  40. ///
  41. /// The fixup expression for this kind includes an implicit offset to account
  42. /// for the PC (unlike the Delta edges) so that a PCRel32 with a target
  43. /// T and addend zero is a call/branch to the start (offset zero) of T.
  44. ///
  45. /// Fixup expression:
  46. /// Fixup <- Target - (Fixup + 4) + Addend : int32
  47. ///
  48. /// Errors:
  49. /// - The result of the fixup expression must fit into an int32, otherwise
  50. /// an out-of-range error will be returned.
  51. ///
  52. PCRel32,
  53. /// A plain 16-bit pointer value relocation.
  54. ///
  55. /// Fixup expression:
  56. /// Fixup <- Target + Addend : uint16
  57. ///
  58. /// Errors:
  59. /// - The target must reside in the low 16-bits of the address space,
  60. /// otherwise an out-of-range error will be returned.
  61. ///
  62. Pointer16,
  63. /// A 16-bit PC-relative relocation.
  64. ///
  65. /// Represents a data/control flow instruction using PC-relative addressing
  66. /// to a target.
  67. ///
  68. /// The fixup expression for this kind includes an implicit offset to account
  69. /// for the PC (unlike the Delta edges) so that a PCRel16 with a target
  70. /// T and addend zero is a call/branch to the start (offset zero) of T.
  71. ///
  72. /// Fixup expression:
  73. /// Fixup <- Target - (Fixup + 4) + Addend : int16
  74. ///
  75. /// Errors:
  76. /// - The result of the fixup expression must fit into an int16, otherwise
  77. /// an out-of-range error will be returned.
  78. ///
  79. PCRel16,
  80. /// A 32-bit delta.
  81. ///
  82. /// Delta from the fixup to the target.
  83. ///
  84. /// Fixup expression:
  85. /// Fixup <- Target - Fixup + Addend : int64
  86. ///
  87. /// Errors:
  88. /// - The result of the fixup expression must fit into an int32, otherwise
  89. /// an out-of-range error will be returned.
  90. Delta32,
  91. /// A 32-bit GOT delta.
  92. ///
  93. /// Delta from the global offset table to the target.
  94. ///
  95. /// Fixup expression:
  96. /// Fixup <- Target - GOTSymbol + Addend : int32
  97. ///
  98. /// Errors:
  99. /// - *ASSERTION* Failure to a null pointer GOTSymbol, which the GOT section
  100. /// symbol was not been defined.
  101. Delta32FromGOT,
  102. /// A GOT entry offset within GOT getter/constructor, transformed to
  103. /// Delta32FromGOT pointing at the GOT entry for the original target.
  104. ///
  105. /// Indicates that this edge should be transformed into a Delta32FromGOT
  106. /// targeting the GOT entry for the edge's current target, maintaining the
  107. /// same addend.
  108. /// A GOT entry for the target should be created if one does not already
  109. /// exist.
  110. ///
  111. /// Edges of this kind are usually handled by a GOT builder pass inserted by
  112. /// default
  113. ///
  114. /// Fixup expression:
  115. /// NONE
  116. ///
  117. /// Errors:
  118. /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
  119. /// phase will result in an assert/unreachable during the fixup phase
  120. RequestGOTAndTransformToDelta32FromGOT,
  121. };
  122. /// Returns a string name for the given i386 edge. For debugging purposes
  123. /// only
  124. const char *getEdgeKindName(Edge::Kind K);
  125. /// Returns true if the given uint32_t value is in range for a uint16_t.
  126. inline bool isInRangeForImmU16(uint32_t Value) {
  127. return Value <= std::numeric_limits<uint16_t>::max();
  128. }
  129. /// Returns true if the given int32_t value is in range for an int16_t.
  130. inline bool isInRangeForImmS16(int32_t Value) {
  131. return (Value >= std::numeric_limits<int16_t>::min() &&
  132. Value <= std::numeric_limits<int16_t>::max());
  133. }
  134. /// Apply fixup expression for edge to block content.
  135. inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E,
  136. const Symbol *GOTSymbol) {
  137. using namespace i386;
  138. using namespace llvm::support;
  139. char *BlockWorkingMem = B.getAlreadyMutableContent().data();
  140. char *FixupPtr = BlockWorkingMem + E.getOffset();
  141. auto FixupAddress = B.getAddress() + E.getOffset();
  142. switch (E.getKind()) {
  143. case i386::None: {
  144. break;
  145. }
  146. case i386::Pointer32: {
  147. uint32_t Value = E.getTarget().getAddress().getValue() + E.getAddend();
  148. *(ulittle32_t *)FixupPtr = Value;
  149. break;
  150. }
  151. case i386::PCRel32: {
  152. int32_t Value =
  153. E.getTarget().getAddress() - (FixupAddress + 4) + E.getAddend();
  154. *(little32_t *)FixupPtr = Value;
  155. break;
  156. }
  157. case i386::Pointer16: {
  158. uint32_t Value = E.getTarget().getAddress().getValue() + E.getAddend();
  159. if (LLVM_LIKELY(isInRangeForImmU16(Value)))
  160. *(ulittle16_t *)FixupPtr = Value;
  161. else
  162. return makeTargetOutOfRangeError(G, B, E);
  163. break;
  164. }
  165. case i386::PCRel16: {
  166. int32_t Value =
  167. E.getTarget().getAddress() - (FixupAddress + 4) + E.getAddend();
  168. if (LLVM_LIKELY(isInRangeForImmS16(Value)))
  169. *(little16_t *)FixupPtr = Value;
  170. else
  171. return makeTargetOutOfRangeError(G, B, E);
  172. break;
  173. }
  174. case i386::Delta32: {
  175. int32_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
  176. *(little32_t *)FixupPtr = Value;
  177. break;
  178. }
  179. case i386::Delta32FromGOT: {
  180. assert(GOTSymbol && "No GOT section symbol");
  181. int32_t Value =
  182. E.getTarget().getAddress() - GOTSymbol->getAddress() + E.getAddend();
  183. *(little32_t *)FixupPtr = Value;
  184. break;
  185. }
  186. default:
  187. return make_error<JITLinkError>(
  188. "In graph " + G.getName() + ", section " + B.getSection().getName() +
  189. "unsupported edge kind" + getEdgeKindName(E.getKind()));
  190. }
  191. return Error::success();
  192. }
  193. /// i386 pointer size.
  194. constexpr uint32_t PointerSize = 4;
  195. /// i386 null pointer content.
  196. extern const char NullPointerContent[PointerSize];
  197. /// Creates a new pointer block in the given section and returns an anonymous
  198. /// symbol pointing to it.
  199. ///
  200. /// If InitialTarget is given then an Pointer32 relocation will be added to the
  201. /// block pointing at InitialTarget.
  202. ///
  203. /// The pointer block will have the following default values:
  204. /// alignment: 32-bit
  205. /// alignment-offset: 0
  206. /// address: highest allowable (~7U)
  207. inline Symbol &createAnonymousPointer(LinkGraph &G, Section &PointerSection,
  208. Symbol *InitialTarget = nullptr,
  209. uint64_t InitialAddend = 0) {
  210. auto &B = G.createContentBlock(PointerSection, NullPointerContent,
  211. orc::ExecutorAddr(), 8, 0);
  212. if (InitialTarget)
  213. B.addEdge(Pointer32, 0, *InitialTarget, InitialAddend);
  214. return G.addAnonymousSymbol(B, 0, PointerSize, false, false);
  215. }
  216. /// Global Offset Table Builder.
  217. class GOTTableManager : public TableManager<GOTTableManager> {
  218. public:
  219. static StringRef getSectionName() { return "$__GOT"; }
  220. bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
  221. Edge::Kind KindToSet = Edge::Invalid;
  222. switch (E.getKind()) {
  223. case i386::Delta32FromGOT: {
  224. // we need to make sure that the GOT section exists, but don't otherwise
  225. // need to fix up this edge
  226. getGOTSection(G);
  227. return false;
  228. }
  229. case i386::RequestGOTAndTransformToDelta32FromGOT:
  230. KindToSet = i386::Delta32FromGOT;
  231. break;
  232. default:
  233. return false;
  234. }
  235. assert(KindToSet != Edge::Invalid &&
  236. "Fell through switch, but no new kind to set");
  237. DEBUG_WITH_TYPE("jitlink", {
  238. dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
  239. << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
  240. << formatv("{0:x}", E.getOffset()) << ")\n";
  241. });
  242. E.setKind(KindToSet);
  243. E.setTarget(getEntryForTarget(G, E.getTarget()));
  244. return true;
  245. }
  246. Symbol &createEntry(LinkGraph &G, Symbol &Target) {
  247. return createAnonymousPointer(G, getGOTSection(G), &Target);
  248. }
  249. private:
  250. Section &getGOTSection(LinkGraph &G) {
  251. if (!GOTSection)
  252. GOTSection = &G.createSection(getSectionName(), orc::MemProt::Read);
  253. return *GOTSection;
  254. }
  255. Section *GOTSection = nullptr;
  256. };
  257. } // namespace llvm::jitlink::i386
  258. #endif // LLVM_EXECUTIONENGINE_JITLINK_I386_H
  259. #ifdef __GNUC__
  260. #pragma GCC diagnostic pop
  261. #endif