Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
12 KiB

  1. //===-- llvm/CodeGen/MachineRelocation.h - Target Relocation ----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the MachineRelocation class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_MACHINERELOCATION_H
  14. #define LLVM_CODEGEN_MACHINERELOCATION_H
  15. #include "llvm/Support/DataTypes.h"
  16. #include <cassert>
  17. namespace llvm {
  18. class GlobalValue;
  19. class MachineBasicBlock;
  20. /// MachineRelocation - This represents a target-specific relocation value,
  21. /// produced by the code emitter. This relocation is resolved after the has
  22. /// been emitted, either to an object file or to memory, when the target of the
  23. /// relocation can be resolved.
  24. ///
  25. /// A relocation is made up of the following logical portions:
  26. /// 1. An offset in the machine code buffer, the location to modify.
  27. /// 2. A target specific relocation type (a number from 0 to 63).
  28. /// 3. A symbol being referenced, either as a GlobalValue* or as a string.
  29. /// 4. An optional constant value to be added to the reference.
  30. /// 5. A bit, CanRewrite, which indicates to the JIT that a function stub is
  31. /// not needed for the relocation.
  32. /// 6. An index into the GOT, if the target uses a GOT
  33. ///
  34. class MachineRelocation {
  35. enum AddressType {
  36. isResult, // Relocation has be transformed into its result pointer.
  37. isGV, // The Target.GV field is valid.
  38. isIndirectSym, // Relocation of an indirect symbol.
  39. isBB, // Relocation of BB address.
  40. isExtSym, // The Target.ExtSym field is valid.
  41. isConstPool, // Relocation of constant pool address.
  42. isJumpTable, // Relocation of jump table address.
  43. isGOTIndex // The Target.GOTIndex field is valid.
  44. };
  45. /// Offset - This is the offset from the start of the code buffer of the
  46. /// relocation to perform.
  47. uintptr_t Offset;
  48. /// ConstantVal - A field that may be used by the target relocation type.
  49. intptr_t ConstantVal;
  50. union {
  51. void *Result; // If this has been resolved to a resolved pointer
  52. GlobalValue *GV; // If this is a pointer to a GV or an indirect ref.
  53. MachineBasicBlock *MBB; // If this is a pointer to a LLVM BB
  54. const char *ExtSym; // If this is a pointer to a named symbol
  55. unsigned Index; // Constant pool / jump table index
  56. unsigned GOTIndex; // Index in the GOT of this symbol/global
  57. } Target;
  58. unsigned TargetReloType : 6; // The target relocation ID
  59. AddressType AddrType : 4; // The field of Target to use
  60. bool MayNeedFarStub : 1; // True if this relocation may require a far-stub
  61. bool GOTRelative : 1; // Should this relocation be relative to the GOT?
  62. bool TargetResolve : 1; // True if target should resolve the address
  63. public:
  64. // Relocation types used in a generic implementation. Currently, relocation
  65. // entries for all things use the generic VANILLA type until they are refined
  66. // into target relocation types.
  67. enum RelocationType {
  68. VANILLA
  69. };
  70. /// MachineRelocation::getGV - Return a relocation entry for a GlobalValue.
  71. ///
  72. static MachineRelocation getGV(uintptr_t offset, unsigned RelocationType,
  73. GlobalValue *GV, intptr_t cst = 0,
  74. bool MayNeedFarStub = 0,
  75. bool GOTrelative = 0) {
  76. assert((RelocationType & ~63) == 0 && "Relocation type too large!");
  77. MachineRelocation Result;
  78. Result.Offset = offset;
  79. Result.ConstantVal = cst;
  80. Result.TargetReloType = RelocationType;
  81. Result.AddrType = isGV;
  82. Result.MayNeedFarStub = MayNeedFarStub;
  83. Result.GOTRelative = GOTrelative;
  84. Result.TargetResolve = false;
  85. Result.Target.GV = GV;
  86. return Result;
  87. }
  88. /// MachineRelocation::getIndirectSymbol - Return a relocation entry for an
  89. /// indirect symbol.
  90. static MachineRelocation getIndirectSymbol(uintptr_t offset,
  91. unsigned RelocationType,
  92. GlobalValue *GV, intptr_t cst = 0,
  93. bool MayNeedFarStub = 0,
  94. bool GOTrelative = 0) {
  95. assert((RelocationType & ~63) == 0 && "Relocation type too large!");
  96. MachineRelocation Result;
  97. Result.Offset = offset;
  98. Result.ConstantVal = cst;
  99. Result.TargetReloType = RelocationType;
  100. Result.AddrType = isIndirectSym;
  101. Result.MayNeedFarStub = MayNeedFarStub;
  102. Result.GOTRelative = GOTrelative;
  103. Result.TargetResolve = false;
  104. Result.Target.GV = GV;
  105. return Result;
  106. }
  107. /// MachineRelocation::getBB - Return a relocation entry for a BB.
  108. ///
  109. static MachineRelocation getBB(uintptr_t offset,unsigned RelocationType,
  110. MachineBasicBlock *MBB, intptr_t cst = 0) {
  111. assert((RelocationType & ~63) == 0 && "Relocation type too large!");
  112. MachineRelocation Result;
  113. Result.Offset = offset;
  114. Result.ConstantVal = cst;
  115. Result.TargetReloType = RelocationType;
  116. Result.AddrType = isBB;
  117. Result.MayNeedFarStub = false;
  118. Result.GOTRelative = false;
  119. Result.TargetResolve = false;
  120. Result.Target.MBB = MBB;
  121. return Result;
  122. }
  123. /// MachineRelocation::getExtSym - Return a relocation entry for an external
  124. /// symbol, like "free".
  125. ///
  126. static MachineRelocation getExtSym(uintptr_t offset, unsigned RelocationType,
  127. const char *ES, intptr_t cst = 0,
  128. bool GOTrelative = 0,
  129. bool NeedStub = true) {
  130. assert((RelocationType & ~63) == 0 && "Relocation type too large!");
  131. MachineRelocation Result;
  132. Result.Offset = offset;
  133. Result.ConstantVal = cst;
  134. Result.TargetReloType = RelocationType;
  135. Result.AddrType = isExtSym;
  136. Result.MayNeedFarStub = NeedStub;
  137. Result.GOTRelative = GOTrelative;
  138. Result.TargetResolve = false;
  139. Result.Target.ExtSym = ES;
  140. return Result;
  141. }
  142. /// MachineRelocation::getConstPool - Return a relocation entry for a constant
  143. /// pool entry.
  144. ///
  145. static MachineRelocation getConstPool(uintptr_t offset,unsigned RelocationType,
  146. unsigned CPI, intptr_t cst = 0,
  147. bool letTargetResolve = false) {
  148. assert((RelocationType & ~63) == 0 && "Relocation type too large!");
  149. MachineRelocation Result;
  150. Result.Offset = offset;
  151. Result.ConstantVal = cst;
  152. Result.TargetReloType = RelocationType;
  153. Result.AddrType = isConstPool;
  154. Result.MayNeedFarStub = false;
  155. Result.GOTRelative = false;
  156. Result.TargetResolve = letTargetResolve;
  157. Result.Target.Index = CPI;
  158. return Result;
  159. }
  160. /// MachineRelocation::getJumpTable - Return a relocation entry for a jump
  161. /// table entry.
  162. ///
  163. static MachineRelocation getJumpTable(uintptr_t offset,unsigned RelocationType,
  164. unsigned JTI, intptr_t cst = 0,
  165. bool letTargetResolve = false) {
  166. assert((RelocationType & ~63) == 0 && "Relocation type too large!");
  167. MachineRelocation Result;
  168. Result.Offset = offset;
  169. Result.ConstantVal = cst;
  170. Result.TargetReloType = RelocationType;
  171. Result.AddrType = isJumpTable;
  172. Result.MayNeedFarStub = false;
  173. Result.GOTRelative = false;
  174. Result.TargetResolve = letTargetResolve;
  175. Result.Target.Index = JTI;
  176. return Result;
  177. }
  178. /// getMachineCodeOffset - Return the offset into the code buffer that the
  179. /// relocation should be performed.
  180. intptr_t getMachineCodeOffset() const {
  181. return Offset;
  182. }
  183. /// getRelocationType - Return the target-specific relocation ID for this
  184. /// relocation.
  185. unsigned getRelocationType() const {
  186. return TargetReloType;
  187. }
  188. /// getConstantVal - Get the constant value associated with this relocation.
  189. /// This is often an offset from the symbol.
  190. ///
  191. intptr_t getConstantVal() const {
  192. return ConstantVal;
  193. }
  194. /// setConstantVal - Set the constant value associated with this relocation.
  195. /// This is often an offset from the symbol.
  196. ///
  197. void setConstantVal(intptr_t val) {
  198. ConstantVal = val;
  199. }
  200. /// isGlobalValue - Return true if this relocation is a GlobalValue, as
  201. /// opposed to a constant string.
  202. bool isGlobalValue() const {
  203. return AddrType == isGV;
  204. }
  205. /// isIndirectSymbol - Return true if this relocation is the address an
  206. /// indirect symbol
  207. bool isIndirectSymbol() const {
  208. return AddrType == isIndirectSym;
  209. }
  210. /// isBasicBlock - Return true if this relocation is a basic block reference.
  211. ///
  212. bool isBasicBlock() const {
  213. return AddrType == isBB;
  214. }
  215. /// isExternalSymbol - Return true if this is a constant string.
  216. ///
  217. bool isExternalSymbol() const {
  218. return AddrType == isExtSym;
  219. }
  220. /// isConstantPoolIndex - Return true if this is a constant pool reference.
  221. ///
  222. bool isConstantPoolIndex() const {
  223. return AddrType == isConstPool;
  224. }
  225. /// isJumpTableIndex - Return true if this is a jump table reference.
  226. ///
  227. bool isJumpTableIndex() const {
  228. return AddrType == isJumpTable;
  229. }
  230. /// isGOTRelative - Return true the target wants the index into the GOT of
  231. /// the symbol rather than the address of the symbol.
  232. bool isGOTRelative() const {
  233. return GOTRelative;
  234. }
  235. /// mayNeedFarStub - This function returns true if the JIT for this target may
  236. /// need either a stub function or an indirect global-variable load to handle
  237. /// the relocated GlobalValue reference. For example, the x86-64 call
  238. /// instruction can only call functions within +/-2GB of the call site.
  239. /// Anything farther away needs a longer mov+call sequence, which can't just
  240. /// be written on top of the existing call.
  241. bool mayNeedFarStub() const {
  242. return MayNeedFarStub;
  243. }
  244. /// letTargetResolve - Return true if the target JITInfo is usually
  245. /// responsible for resolving the address of this relocation.
  246. bool letTargetResolve() const {
  247. return TargetResolve;
  248. }
  249. /// getGlobalValue - If this is a global value reference, return the
  250. /// referenced global.
  251. GlobalValue *getGlobalValue() const {
  252. assert((isGlobalValue() || isIndirectSymbol()) &&
  253. "This is not a global value reference!");
  254. return Target.GV;
  255. }
  256. MachineBasicBlock *getBasicBlock() const {
  257. assert(isBasicBlock() && "This is not a basic block reference!");
  258. return Target.MBB;
  259. }
  260. /// getString - If this is a string value, return the string reference.
  261. ///
  262. const char *getExternalSymbol() const {
  263. assert(isExternalSymbol() && "This is not an external symbol reference!");
  264. return Target.ExtSym;
  265. }
  266. /// getConstantPoolIndex - If this is a const pool reference, return
  267. /// the index into the constant pool.
  268. unsigned getConstantPoolIndex() const {
  269. assert(isConstantPoolIndex() && "This is not a constant pool reference!");
  270. return Target.Index;
  271. }
  272. /// getJumpTableIndex - If this is a jump table reference, return
  273. /// the index into the jump table.
  274. unsigned getJumpTableIndex() const {
  275. assert(isJumpTableIndex() && "This is not a jump table reference!");
  276. return Target.Index;
  277. }
  278. /// getResultPointer - Once this has been resolved to point to an actual
  279. /// address, this returns the pointer.
  280. void *getResultPointer() const {
  281. assert(AddrType == isResult && "Result pointer isn't set yet!");
  282. return Target.Result;
  283. }
  284. /// setResultPointer - Set the result to the specified pointer value.
  285. ///
  286. void setResultPointer(void *Ptr) {
  287. Target.Result = Ptr;
  288. AddrType = isResult;
  289. }
  290. /// setGOTIndex - Set the GOT index to a specific value.
  291. void setGOTIndex(unsigned idx) {
  292. AddrType = isGOTIndex;
  293. Target.GOTIndex = idx;
  294. }
  295. /// getGOTIndex - Once this has been resolved to an entry in the GOT,
  296. /// this returns that index. The index is from the lowest address entry
  297. /// in the GOT.
  298. unsigned getGOTIndex() const {
  299. assert(AddrType == isGOTIndex);
  300. return Target.GOTIndex;
  301. }
  302. };
  303. }
  304. #endif