1 //===-- llvm/CodeGen/MachineRelocation.h - Target Relocation ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MachineRelocation class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINERELOCATION_H
15 #define LLVM_CODEGEN_MACHINERELOCATION_H
17 #include "llvm/Support/DataTypes.h"
23 /// MachineRelocation - This represents a target-specific relocation value,
24 /// produced by the code emitter. This relocation is resolved after the has
25 /// been emitted, either to an object file or to memory, when the target of the
26 /// relocation can be resolved.
28 /// A relocation is made up of the following logical portions:
29 /// 1. An offset in the machine code buffer, the location to modify.
30 /// 2. A target specific relocation type (a number from 0 to 63).
31 /// 3. A symbol being referenced, either as a GlobalValue* or as a string.
32 /// 4. An optional constant value to be added to the reference.
33 /// 5. A bit, CanRewrite, which indicates to the JIT that a function stub is
34 /// not needed for the relocation.
35 /// 6. An index into the GOT, if the target uses a GOT
37 class MachineRelocation {
39 isResult, // Relocation has be transformed into its result pointer.
40 isGV, // The Target.GV field is valid.
41 isExtSym, // The Target.ExtSym field is valid.
42 isConstPool, // Relocation of constant pool address.
43 isJumpTable, // Relocation of jump table address.
44 isGOTIndex // The Target.GOTIndex field is valid.
47 /// Offset - This is the offset from the start of the code buffer of the
48 /// relocation to perform.
51 /// ConstantVal - A field that may be used by the target relocation type.
55 void *Result; // If this has been resolved to a resolved pointer
56 GlobalValue *GV; // If this is a pointer to an LLVM global
57 const char *ExtSym; // If this is a pointer to a named symbol
58 unsigned Index; // Constant pool / jump table index
59 unsigned GOTIndex; // Index in the GOT of this symbol/global
62 unsigned TargetReloType : 6; // The target relocation ID.
63 AddressType AddrType : 3; // The field of Target to use.
64 bool DoesntNeedFnStub : 1; // True if we don't need a fn stub.
65 bool GOTRelative : 1; // Should this relocation be relative to the GOT?
68 /// MachineRelocation::getGV - Return a relocation entry for a GlobalValue.
70 static MachineRelocation getGV(intptr_t offset, unsigned RelocationType,
71 GlobalValue *GV, intptr_t cst = 0,
72 bool DoesntNeedFunctionStub = 0,
73 bool GOTrelative = 0) {
74 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
75 MachineRelocation Result;
76 Result.Offset = offset;
77 Result.ConstantVal = cst;
78 Result.TargetReloType = RelocationType;
79 Result.AddrType = isGV;
80 Result.DoesntNeedFnStub = DoesntNeedFunctionStub;
81 Result.GOTRelative = GOTrelative;
82 Result.Target.GV = GV;
86 /// MachineRelocation::getExtSym - Return a relocation entry for an external
87 /// symbol, like "free".
89 static MachineRelocation getExtSym(intptr_t offset, unsigned RelocationType,
90 const char *ES, intptr_t cst = 0,
91 bool GOTrelative = 0) {
92 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
93 MachineRelocation Result;
94 Result.Offset = offset;
95 Result.ConstantVal = cst;
96 Result.TargetReloType = RelocationType;
97 Result.AddrType = isExtSym;
98 Result.DoesntNeedFnStub = false;
99 Result.GOTRelative = GOTrelative;
100 Result.Target.ExtSym = ES;
104 /// MachineRelocation::getConstPool - Return a relocation entry for a constant
107 static MachineRelocation getConstPool(intptr_t offset,unsigned RelocationType,
108 unsigned CPI, intptr_t cst = 0) {
109 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
110 MachineRelocation Result;
111 Result.Offset = offset;
112 Result.ConstantVal = cst;
113 Result.TargetReloType = RelocationType;
114 Result.AddrType = isConstPool;
115 Result.DoesntNeedFnStub = false;
116 Result.GOTRelative = false;
117 Result.Target.Index = CPI;
121 /// MachineRelocation::getJumpTable - Return a relocation entry for a jump
124 static MachineRelocation getJumpTable(intptr_t offset,unsigned RelocationType,
125 unsigned JTI, intptr_t cst = 0) {
126 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
127 MachineRelocation Result;
128 Result.Offset = offset;
129 Result.ConstantVal = cst;
130 Result.TargetReloType = RelocationType;
131 Result.AddrType = isJumpTable;
132 Result.DoesntNeedFnStub = false;
133 Result.GOTRelative = false;
134 Result.Target.Index = JTI;
138 /// getMachineCodeOffset - Return the offset into the code buffer that the
139 /// relocation should be performed.
140 intptr_t getMachineCodeOffset() const {
144 /// getRelocationType - Return the target-specific relocation ID for this
146 unsigned getRelocationType() const {
147 return TargetReloType;
150 /// getConstantVal - Get the constant value associated with this relocation.
151 /// This is often an offset from the symbol.
153 intptr_t getConstantVal() const {
157 /// isGlobalValue - Return true if this relocation is a GlobalValue, as
158 /// opposed to a constant string.
159 bool isGlobalValue() const {
160 return AddrType == isGV;
163 /// isString - Return true if this is a constant string.
165 bool isString() const {
166 return AddrType == isExtSym;
169 /// isConstantPoolIndex - Return true if this is a constant pool reference.
171 bool isConstantPoolIndex() const {
172 return AddrType == isConstPool;
175 /// isJumpTableIndex - Return true if this is a jump table reference.
177 bool isJumpTableIndex() const {
178 return AddrType == isJumpTable;
181 /// isGOTRelative - Return true the target wants the index into the GOT of
182 /// the symbol rather than the address of the symbol.
183 bool isGOTRelative() const {
187 /// doesntNeedFunctionStub - This function returns true if the JIT for this
188 /// target is capable of directly handling the relocated instruction without
189 /// using a stub function. It is always conservatively correct for this flag
190 /// to be false, but targets can improve their compilation callback functions
191 /// to handle more general cases if they want improved performance.
192 bool doesntNeedFunctionStub() const {
193 return DoesntNeedFnStub;
196 /// getGlobalValue - If this is a global value reference, return the
197 /// referenced global.
198 GlobalValue *getGlobalValue() const {
199 assert(isGlobalValue() && "This is not a global value reference!");
203 /// getString - If this is a string value, return the string reference.
205 const char *getString() const {
206 assert(isString() && "This is not a string reference!");
207 return Target.ExtSym;
210 /// getConstantPoolIndex - If this is a const pool reference, return
211 /// the index into the constant pool.
212 unsigned getConstantPoolIndex() const {
213 assert(isConstantPoolIndex() && "This is not a constant pool reference!");
217 /// getJumpTableIndex - If this is a jump table reference, return
218 /// the index into the jump table.
219 unsigned getJumpTableIndex() const {
220 assert(isJumpTableIndex() && "This is not a jump table reference!");
224 /// getResultPointer - Once this has been resolved to point to an actual
225 /// address, this returns the pointer.
226 void *getResultPointer() const {
227 assert(AddrType == isResult && "Result pointer isn't set yet!");
228 return Target.Result;
231 /// setResultPointer - Set the result to the specified pointer value.
233 void setResultPointer(void *Ptr) {
238 /// setGOTIndex - Set the GOT index to a specific value.
239 void setGOTIndex(unsigned idx) {
240 AddrType = isGOTIndex;
241 Target.GOTIndex = idx;
244 /// getGOTIndex - Once this has been resolved to an entry in the GOT,
245 /// this returns that index. The index is from the lowest address entry
247 unsigned getGOTIndex() const {
248 assert(AddrType == isGOTIndex);
249 return Target.GOTIndex;