blob: c5397819aee9606e48e79e5c9ac09cf74e71e40b [file] [log] [blame]
Chris Lattnerb89df9c2004-11-20 03:05:50 +00001//===-- llvm/CodeGen/MachineRelocation.h - Target Relocation ----*- C++ -*-===//
Misha Brukmanea61c352005-04-21 20:39:54 +00002//
Chris Lattnerb89df9c2004-11-20 03:05:50 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner7ed47a12007-12-29 19:59:42 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanea61c352005-04-21 20:39:54 +00007//
Chris Lattnerb89df9c2004-11-20 03:05:50 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines the MachineRelocation class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_MACHINERELOCATION_H
15#define LLVM_CODEGEN_MACHINERELOCATION_H
16
17#include "llvm/Support/DataTypes.h"
Chris Lattner4e2239d2004-11-20 23:40:54 +000018#include <cassert>
Chris Lattnerb89df9c2004-11-20 03:05:50 +000019
20namespace llvm {
21class GlobalValue;
Evan Chengb4e80f82006-07-27 18:18:13 +000022class MachineBasicBlock;
Chris Lattnerb89df9c2004-11-20 03:05:50 +000023
24/// MachineRelocation - This represents a target-specific relocation value,
25/// produced by the code emitter. This relocation is resolved after the has
26/// been emitted, either to an object file or to memory, when the target of the
27/// relocation can be resolved.
28///
29/// A relocation is made up of the following logical portions:
30/// 1. An offset in the machine code buffer, the location to modify.
Chris Lattner765da912004-11-21 03:27:13 +000031/// 2. A target specific relocation type (a number from 0 to 63).
Chris Lattnerb89df9c2004-11-20 03:05:50 +000032/// 3. A symbol being referenced, either as a GlobalValue* or as a string.
33/// 4. An optional constant value to be added to the reference.
Chris Lattner765da912004-11-21 03:27:13 +000034/// 5. A bit, CanRewrite, which indicates to the JIT that a function stub is
35/// not needed for the relocation.
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +000036/// 6. An index into the GOT, if the target uses a GOT
Chris Lattnerb89df9c2004-11-20 03:05:50 +000037///
38class MachineRelocation {
Chris Lattner1e3822c2006-05-03 18:52:31 +000039 enum AddressType {
40 isResult, // Relocation has be transformed into its result pointer.
41 isGV, // The Target.GV field is valid.
Evan Cheng9ed2f802008-11-10 01:08:07 +000042 isIndirectSym, // Relocation of an indirect symbol.
Evan Chengb4e80f82006-07-27 18:18:13 +000043 isBB, // Relocation of BB address.
Chris Lattner1e3822c2006-05-03 18:52:31 +000044 isExtSym, // The Target.ExtSym field is valid.
Evan Cheng52b510b2006-06-23 01:02:37 +000045 isConstPool, // Relocation of constant pool address.
46 isJumpTable, // Relocation of jump table address.
Chris Lattner1e3822c2006-05-03 18:52:31 +000047 isGOTIndex // The Target.GOTIndex field is valid.
48 };
49
50 /// Offset - This is the offset from the start of the code buffer of the
51 /// relocation to perform.
Evan Cheng5788d1a2008-12-10 02:32:19 +000052 uintptr_t Offset;
Chris Lattner1e3822c2006-05-03 18:52:31 +000053
54 /// ConstantVal - A field that may be used by the target relocation type.
55 intptr_t ConstantVal;
56
Chris Lattnerb89df9c2004-11-20 03:05:50 +000057 union {
Evan Chengb4e80f82006-07-27 18:18:13 +000058 void *Result; // If this has been resolved to a resolved pointer
Evan Cheng9ed2f802008-11-10 01:08:07 +000059 GlobalValue *GV; // If this is a pointer to a GV or an indirect ref.
Evan Chengb4e80f82006-07-27 18:18:13 +000060 MachineBasicBlock *MBB; // If this is a pointer to a LLVM BB
61 const char *ExtSym; // If this is a pointer to a named symbol
62 unsigned Index; // Constant pool / jump table index
63 unsigned GOTIndex; // Index in the GOT of this symbol/global
Chris Lattnerb89df9c2004-11-20 03:05:50 +000064 } Target;
Chris Lattner1e3822c2006-05-03 18:52:31 +000065
Evan Cheng70ba70f2008-10-29 23:53:42 +000066 unsigned TargetReloType : 6; // The target relocation ID
67 AddressType AddrType : 4; // The field of Target to use
68 bool NeedStub : 1; // True if this relocation requires a stub
Chris Lattner1e3822c2006-05-03 18:52:31 +000069 bool GOTRelative : 1; // Should this relocation be relative to the GOT?
Evan Cheng70ba70f2008-10-29 23:53:42 +000070 bool TargetResolve : 1; // True if target should resolve the address
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +000071
Chris Lattnerb89df9c2004-11-20 03:05:50 +000072public:
Nate Begeman5381baa2006-12-11 02:19:29 +000073 // Relocation types used in a generic implementation. Currently, relocation
74 // entries for all things use the generic VANILLA type until they are refined
75 // into target relocation types.
76 enum RelocationType {
77 VANILLA
78 };
79
Chris Lattner5a032de2006-05-03 20:30:20 +000080 /// MachineRelocation::getGV - Return a relocation entry for a GlobalValue.
81 ///
Evan Cheng5788d1a2008-12-10 02:32:19 +000082 static MachineRelocation getGV(uintptr_t offset, unsigned RelocationType,
Chris Lattner5a032de2006-05-03 20:30:20 +000083 GlobalValue *GV, intptr_t cst = 0,
Evan Cheng02aabbf2008-01-03 02:56:28 +000084 bool NeedStub = 0,
Chris Lattner5a032de2006-05-03 20:30:20 +000085 bool GOTrelative = 0) {
Chris Lattner765da912004-11-21 03:27:13 +000086 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
Chris Lattner5a032de2006-05-03 20:30:20 +000087 MachineRelocation Result;
88 Result.Offset = offset;
89 Result.ConstantVal = cst;
90 Result.TargetReloType = RelocationType;
91 Result.AddrType = isGV;
Evan Cheng02aabbf2008-01-03 02:56:28 +000092 Result.NeedStub = NeedStub;
Chris Lattner5a032de2006-05-03 20:30:20 +000093 Result.GOTRelative = GOTrelative;
Evan Cheng70ba70f2008-10-29 23:53:42 +000094 Result.TargetResolve = false;
Chris Lattner5a032de2006-05-03 20:30:20 +000095 Result.Target.GV = GV;
96 return Result;
Chris Lattnerb89df9c2004-11-20 03:05:50 +000097 }
98
Evan Cheng9ed2f802008-11-10 01:08:07 +000099 /// MachineRelocation::getIndirectSymbol - Return a relocation entry for an
100 /// indirect symbol.
Evan Cheng5788d1a2008-12-10 02:32:19 +0000101 static MachineRelocation getIndirectSymbol(uintptr_t offset,
Evan Cheng9ed2f802008-11-10 01:08:07 +0000102 unsigned RelocationType,
103 GlobalValue *GV, intptr_t cst = 0,
104 bool NeedStub = 0,
105 bool GOTrelative = 0) {
Evan Chengbe8c03f2008-01-04 10:46:51 +0000106 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
107 MachineRelocation Result;
108 Result.Offset = offset;
109 Result.ConstantVal = cst;
110 Result.TargetReloType = RelocationType;
Evan Cheng9ed2f802008-11-10 01:08:07 +0000111 Result.AddrType = isIndirectSym;
Evan Chengbe8c03f2008-01-04 10:46:51 +0000112 Result.NeedStub = NeedStub;
113 Result.GOTRelative = GOTrelative;
Evan Cheng70ba70f2008-10-29 23:53:42 +0000114 Result.TargetResolve = false;
Evan Chengbe8c03f2008-01-04 10:46:51 +0000115 Result.Target.GV = GV;
116 return Result;
117 }
118
Evan Chengb4e80f82006-07-27 18:18:13 +0000119 /// MachineRelocation::getBB - Return a relocation entry for a BB.
120 ///
Evan Cheng5788d1a2008-12-10 02:32:19 +0000121 static MachineRelocation getBB(uintptr_t offset,unsigned RelocationType,
Evan Chengb4e80f82006-07-27 18:18:13 +0000122 MachineBasicBlock *MBB, intptr_t cst = 0) {
123 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
124 MachineRelocation Result;
125 Result.Offset = offset;
126 Result.ConstantVal = cst;
127 Result.TargetReloType = RelocationType;
128 Result.AddrType = isBB;
Evan Cheng02aabbf2008-01-03 02:56:28 +0000129 Result.NeedStub = false;
Evan Chengb4e80f82006-07-27 18:18:13 +0000130 Result.GOTRelative = false;
Evan Cheng70ba70f2008-10-29 23:53:42 +0000131 Result.TargetResolve = false;
Evan Chengb4e80f82006-07-27 18:18:13 +0000132 Result.Target.MBB = MBB;
133 return Result;
134 }
135
Chris Lattner5a032de2006-05-03 20:30:20 +0000136 /// MachineRelocation::getExtSym - Return a relocation entry for an external
137 /// symbol, like "free".
138 ///
Evan Cheng5788d1a2008-12-10 02:32:19 +0000139 static MachineRelocation getExtSym(uintptr_t offset, unsigned RelocationType,
Evan Cheng652f7ea2008-05-30 22:47:19 +0000140 const char *ES, intptr_t cst = 0,
Chris Lattner5a032de2006-05-03 20:30:20 +0000141 bool GOTrelative = 0) {
Chris Lattner765da912004-11-21 03:27:13 +0000142 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
Chris Lattner5a032de2006-05-03 20:30:20 +0000143 MachineRelocation Result;
144 Result.Offset = offset;
145 Result.ConstantVal = cst;
146 Result.TargetReloType = RelocationType;
147 Result.AddrType = isExtSym;
Chris Lattnerc2f191c2008-01-21 22:27:27 +0000148 Result.NeedStub = true;
Chris Lattner5a032de2006-05-03 20:30:20 +0000149 Result.GOTRelative = GOTrelative;
Evan Cheng70ba70f2008-10-29 23:53:42 +0000150 Result.TargetResolve = false;
Evan Cheng652f7ea2008-05-30 22:47:19 +0000151 Result.Target.ExtSym = ES;
Chris Lattner5a032de2006-05-03 20:30:20 +0000152 return Result;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000153 }
154
Chris Lattner5a032de2006-05-03 20:30:20 +0000155 /// MachineRelocation::getConstPool - Return a relocation entry for a constant
156 /// pool entry.
157 ///
Evan Cheng5788d1a2008-12-10 02:32:19 +0000158 static MachineRelocation getConstPool(uintptr_t offset,unsigned RelocationType,
Evan Cheng70ba70f2008-10-29 23:53:42 +0000159 unsigned CPI, intptr_t cst = 0,
160 bool letTargetResolve = false) {
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000161 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
Chris Lattner5a032de2006-05-03 20:30:20 +0000162 MachineRelocation Result;
163 Result.Offset = offset;
164 Result.ConstantVal = cst;
165 Result.TargetReloType = RelocationType;
166 Result.AddrType = isConstPool;
Evan Cheng02aabbf2008-01-03 02:56:28 +0000167 Result.NeedStub = false;
Chris Lattner5a032de2006-05-03 20:30:20 +0000168 Result.GOTRelative = false;
Evan Cheng70ba70f2008-10-29 23:53:42 +0000169 Result.TargetResolve = letTargetResolve;
Evan Cheng52b510b2006-06-23 01:02:37 +0000170 Result.Target.Index = CPI;
171 return Result;
172 }
173
174 /// MachineRelocation::getJumpTable - Return a relocation entry for a jump
175 /// table entry.
176 ///
Evan Cheng5788d1a2008-12-10 02:32:19 +0000177 static MachineRelocation getJumpTable(uintptr_t offset,unsigned RelocationType,
Evan Chenga27b3532008-11-07 09:01:15 +0000178 unsigned JTI, intptr_t cst = 0,
179 bool letTargetResolve = false) {
Evan Cheng52b510b2006-06-23 01:02:37 +0000180 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
181 MachineRelocation Result;
182 Result.Offset = offset;
183 Result.ConstantVal = cst;
184 Result.TargetReloType = RelocationType;
185 Result.AddrType = isJumpTable;
Evan Cheng02aabbf2008-01-03 02:56:28 +0000186 Result.NeedStub = false;
Evan Cheng52b510b2006-06-23 01:02:37 +0000187 Result.GOTRelative = false;
Evan Chenga27b3532008-11-07 09:01:15 +0000188 Result.TargetResolve = letTargetResolve;
Evan Cheng52b510b2006-06-23 01:02:37 +0000189 Result.Target.Index = JTI;
Chris Lattner5a032de2006-05-03 20:30:20 +0000190 return Result;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000191 }
192
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000193 /// getMachineCodeOffset - Return the offset into the code buffer that the
194 /// relocation should be performed.
Chris Lattner5a032de2006-05-03 20:30:20 +0000195 intptr_t getMachineCodeOffset() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000196 return Offset;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000197 }
198
Chris Lattnerfab11a72004-11-20 03:43:50 +0000199 /// getRelocationType - Return the target-specific relocation ID for this
200 /// relocation.
201 unsigned getRelocationType() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000202 return TargetReloType;
Chris Lattnerfab11a72004-11-20 03:43:50 +0000203 }
204
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000205 /// getConstantVal - Get the constant value associated with this relocation.
206 /// This is often an offset from the symbol.
207 ///
208 intptr_t getConstantVal() const {
209 return ConstantVal;
210 }
211
Nate Begeman5381baa2006-12-11 02:19:29 +0000212 /// setConstantVal - Set the constant value associated with this relocation.
213 /// This is often an offset from the symbol.
214 ///
215 void setConstantVal(intptr_t val) {
216 ConstantVal = val;
217 }
218
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000219 /// isGlobalValue - Return true if this relocation is a GlobalValue, as
220 /// opposed to a constant string.
221 bool isGlobalValue() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000222 return AddrType == isGV;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000223 }
224
Evan Cheng9ed2f802008-11-10 01:08:07 +0000225 /// isIndirectSymbol - Return true if this relocation is the address an
226 /// indirect symbol
227 bool isIndirectSymbol() const {
228 return AddrType == isIndirectSym;
Evan Chengbe8c03f2008-01-04 10:46:51 +0000229 }
230
Evan Chengb4e80f82006-07-27 18:18:13 +0000231 /// isBasicBlock - Return true if this relocation is a basic block reference.
232 ///
233 bool isBasicBlock() const {
234 return AddrType == isBB;
235 }
236
Evan Chengd7398c92008-11-08 07:37:34 +0000237 /// isExternalSymbol - Return true if this is a constant string.
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000238 ///
Evan Chengd7398c92008-11-08 07:37:34 +0000239 bool isExternalSymbol() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000240 return AddrType == isExtSym;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000241 }
242
243 /// isConstantPoolIndex - Return true if this is a constant pool reference.
244 ///
245 bool isConstantPoolIndex() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000246 return AddrType == isConstPool;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000247 }
248
Evan Cheng52b510b2006-06-23 01:02:37 +0000249 /// isJumpTableIndex - Return true if this is a jump table reference.
250 ///
251 bool isJumpTableIndex() const {
252 return AddrType == isJumpTable;
253 }
254
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000255 /// isGOTRelative - Return true the target wants the index into the GOT of
256 /// the symbol rather than the address of the symbol.
257 bool isGOTRelative() const {
258 return GOTRelative;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000259 }
260
Evan Cheng02aabbf2008-01-03 02:56:28 +0000261 /// doesntNeedStub - This function returns true if the JIT for this target
262 /// target is capable of directly handling the relocated GlobalValue reference
263 /// without using either a stub function or issuing an extra load to get the
264 /// GV address.
265 bool doesntNeedStub() const {
266 return !NeedStub;
Chris Lattner765da912004-11-21 03:27:13 +0000267 }
268
Evan Cheng70ba70f2008-10-29 23:53:42 +0000269 /// letTargetResolve - Return true if the target JITInfo is usually
270 /// responsible for resolving the address of this relocation.
271 bool letTargetResolve() const {
272 return TargetResolve;
273 }
274
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000275 /// getGlobalValue - If this is a global value reference, return the
276 /// referenced global.
277 GlobalValue *getGlobalValue() const {
Evan Cheng9ed2f802008-11-10 01:08:07 +0000278 assert((isGlobalValue() || isIndirectSymbol()) &&
Evan Chengbe8c03f2008-01-04 10:46:51 +0000279 "This is not a global value reference!");
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000280 return Target.GV;
281 }
282
Evan Chengb4e80f82006-07-27 18:18:13 +0000283 MachineBasicBlock *getBasicBlock() const {
284 assert(isBasicBlock() && "This is not a basic block reference!");
285 return Target.MBB;
286 }
287
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000288 /// getString - If this is a string value, return the string reference.
289 ///
Evan Chengd7398c92008-11-08 07:37:34 +0000290 const char *getExternalSymbol() const {
291 assert(isExternalSymbol() && "This is not an external symbol reference!");
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000292 return Target.ExtSym;
293 }
294
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000295 /// getConstantPoolIndex - If this is a const pool reference, return
296 /// the index into the constant pool.
297 unsigned getConstantPoolIndex() const {
298 assert(isConstantPoolIndex() && "This is not a constant pool reference!");
Evan Cheng52b510b2006-06-23 01:02:37 +0000299 return Target.Index;
300 }
301
302 /// getJumpTableIndex - If this is a jump table reference, return
303 /// the index into the jump table.
304 unsigned getJumpTableIndex() const {
305 assert(isJumpTableIndex() && "This is not a jump table reference!");
306 return Target.Index;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000307 }
308
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000309 /// getResultPointer - Once this has been resolved to point to an actual
310 /// address, this returns the pointer.
311 void *getResultPointer() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000312 assert(AddrType == isResult && "Result pointer isn't set yet!");
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000313 return Target.Result;
314 }
315
316 /// setResultPointer - Set the result to the specified pointer value.
317 ///
318 void setResultPointer(void *Ptr) {
319 Target.Result = Ptr;
Chris Lattner1e3822c2006-05-03 18:52:31 +0000320 AddrType = isResult;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000321 }
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000322
323 /// setGOTIndex - Set the GOT index to a specific value.
324 void setGOTIndex(unsigned idx) {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000325 AddrType = isGOTIndex;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000326 Target.GOTIndex = idx;
327 }
328
329 /// getGOTIndex - Once this has been resolved to an entry in the GOT,
Jeff Cohen9eb59ec2005-07-27 05:53:44 +0000330 /// this returns that index. The index is from the lowest address entry
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000331 /// in the GOT.
332 unsigned getGOTIndex() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000333 assert(AddrType == isGOTIndex);
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000334 return Target.GOTIndex;
335 }
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000336};
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000337}
338
339#endif