blob: 244b466e172826a465e2ed6143cd04cba3aa7b97 [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
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000017#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
Jeffrey Yasskin2d274412009-11-07 08:51:52 +000068 bool MayNeedFarStub : 1; // True if this relocation may require a far-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,
Jeffrey Yasskin2d274412009-11-07 08:51:52 +000084 bool MayNeedFarStub = 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;
Jeffrey Yasskin2d274412009-11-07 08:51:52 +000092 Result.MayNeedFarStub = MayNeedFarStub;
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,
Jeffrey Yasskin2d274412009-11-07 08:51:52 +0000104 bool MayNeedFarStub = 0,
Evan Cheng9ed2f802008-11-10 01:08:07 +0000105 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;
Jeffrey Yasskin2d274412009-11-07 08:51:52 +0000112 Result.MayNeedFarStub = MayNeedFarStub;
Evan Chengbe8c03f2008-01-04 10:46:51 +0000113 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;
Jeffrey Yasskin2d274412009-11-07 08:51:52 +0000129 Result.MayNeedFarStub = 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,
Evan Phoenix85bb54f2010-02-04 19:56:59 +0000141 bool GOTrelative = 0,
142 bool NeedStub = true) {
Chris Lattner765da912004-11-21 03:27:13 +0000143 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
Chris Lattner5a032de2006-05-03 20:30:20 +0000144 MachineRelocation Result;
145 Result.Offset = offset;
146 Result.ConstantVal = cst;
147 Result.TargetReloType = RelocationType;
148 Result.AddrType = isExtSym;
Evan Phoenix85bb54f2010-02-04 19:56:59 +0000149 Result.MayNeedFarStub = NeedStub;
Chris Lattner5a032de2006-05-03 20:30:20 +0000150 Result.GOTRelative = GOTrelative;
Evan Cheng70ba70f2008-10-29 23:53:42 +0000151 Result.TargetResolve = false;
Evan Cheng652f7ea2008-05-30 22:47:19 +0000152 Result.Target.ExtSym = ES;
Chris Lattner5a032de2006-05-03 20:30:20 +0000153 return Result;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000154 }
155
Chris Lattner5a032de2006-05-03 20:30:20 +0000156 /// MachineRelocation::getConstPool - Return a relocation entry for a constant
157 /// pool entry.
158 ///
Evan Cheng5788d1a2008-12-10 02:32:19 +0000159 static MachineRelocation getConstPool(uintptr_t offset,unsigned RelocationType,
Evan Cheng70ba70f2008-10-29 23:53:42 +0000160 unsigned CPI, intptr_t cst = 0,
161 bool letTargetResolve = false) {
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000162 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
Chris Lattner5a032de2006-05-03 20:30:20 +0000163 MachineRelocation Result;
164 Result.Offset = offset;
165 Result.ConstantVal = cst;
166 Result.TargetReloType = RelocationType;
167 Result.AddrType = isConstPool;
Jeffrey Yasskin2d274412009-11-07 08:51:52 +0000168 Result.MayNeedFarStub = false;
Chris Lattner5a032de2006-05-03 20:30:20 +0000169 Result.GOTRelative = false;
Evan Cheng70ba70f2008-10-29 23:53:42 +0000170 Result.TargetResolve = letTargetResolve;
Evan Cheng52b510b2006-06-23 01:02:37 +0000171 Result.Target.Index = CPI;
172 return Result;
173 }
174
175 /// MachineRelocation::getJumpTable - Return a relocation entry for a jump
176 /// table entry.
177 ///
Evan Cheng5788d1a2008-12-10 02:32:19 +0000178 static MachineRelocation getJumpTable(uintptr_t offset,unsigned RelocationType,
Evan Chenga27b3532008-11-07 09:01:15 +0000179 unsigned JTI, intptr_t cst = 0,
180 bool letTargetResolve = false) {
Evan Cheng52b510b2006-06-23 01:02:37 +0000181 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
182 MachineRelocation Result;
183 Result.Offset = offset;
184 Result.ConstantVal = cst;
185 Result.TargetReloType = RelocationType;
186 Result.AddrType = isJumpTable;
Jeffrey Yasskin2d274412009-11-07 08:51:52 +0000187 Result.MayNeedFarStub = false;
Evan Cheng52b510b2006-06-23 01:02:37 +0000188 Result.GOTRelative = false;
Evan Chenga27b3532008-11-07 09:01:15 +0000189 Result.TargetResolve = letTargetResolve;
Evan Cheng52b510b2006-06-23 01:02:37 +0000190 Result.Target.Index = JTI;
Chris Lattner5a032de2006-05-03 20:30:20 +0000191 return Result;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000192 }
193
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000194 /// getMachineCodeOffset - Return the offset into the code buffer that the
195 /// relocation should be performed.
Chris Lattner5a032de2006-05-03 20:30:20 +0000196 intptr_t getMachineCodeOffset() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000197 return Offset;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000198 }
199
Chris Lattnerfab11a72004-11-20 03:43:50 +0000200 /// getRelocationType - Return the target-specific relocation ID for this
201 /// relocation.
202 unsigned getRelocationType() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000203 return TargetReloType;
Chris Lattnerfab11a72004-11-20 03:43:50 +0000204 }
205
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000206 /// getConstantVal - Get the constant value associated with this relocation.
207 /// This is often an offset from the symbol.
208 ///
209 intptr_t getConstantVal() const {
210 return ConstantVal;
211 }
212
Nate Begeman5381baa2006-12-11 02:19:29 +0000213 /// setConstantVal - Set the constant value associated with this relocation.
214 /// This is often an offset from the symbol.
215 ///
216 void setConstantVal(intptr_t val) {
217 ConstantVal = val;
218 }
219
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000220 /// isGlobalValue - Return true if this relocation is a GlobalValue, as
221 /// opposed to a constant string.
222 bool isGlobalValue() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000223 return AddrType == isGV;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000224 }
225
Evan Cheng9ed2f802008-11-10 01:08:07 +0000226 /// isIndirectSymbol - Return true if this relocation is the address an
227 /// indirect symbol
228 bool isIndirectSymbol() const {
229 return AddrType == isIndirectSym;
Evan Chengbe8c03f2008-01-04 10:46:51 +0000230 }
231
Evan Chengb4e80f82006-07-27 18:18:13 +0000232 /// isBasicBlock - Return true if this relocation is a basic block reference.
233 ///
234 bool isBasicBlock() const {
235 return AddrType == isBB;
236 }
237
Evan Chengd7398c92008-11-08 07:37:34 +0000238 /// isExternalSymbol - Return true if this is a constant string.
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000239 ///
Evan Chengd7398c92008-11-08 07:37:34 +0000240 bool isExternalSymbol() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000241 return AddrType == isExtSym;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000242 }
243
244 /// isConstantPoolIndex - Return true if this is a constant pool reference.
245 ///
246 bool isConstantPoolIndex() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000247 return AddrType == isConstPool;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000248 }
249
Evan Cheng52b510b2006-06-23 01:02:37 +0000250 /// isJumpTableIndex - Return true if this is a jump table reference.
251 ///
252 bool isJumpTableIndex() const {
253 return AddrType == isJumpTable;
254 }
255
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000256 /// isGOTRelative - Return true the target wants the index into the GOT of
257 /// the symbol rather than the address of the symbol.
258 bool isGOTRelative() const {
259 return GOTRelative;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000260 }
261
Jeffrey Yasskin2d274412009-11-07 08:51:52 +0000262 /// mayNeedFarStub - This function returns true if the JIT for this target may
263 /// need either a stub function or an indirect global-variable load to handle
264 /// the relocated GlobalValue reference. For example, the x86-64 call
265 /// instruction can only call functions within +/-2GB of the call site.
266 /// Anything farther away needs a longer mov+call sequence, which can't just
267 /// be written on top of the existing call.
268 bool mayNeedFarStub() const {
269 return MayNeedFarStub;
Chris Lattner765da912004-11-21 03:27:13 +0000270 }
271
Evan Cheng70ba70f2008-10-29 23:53:42 +0000272 /// letTargetResolve - Return true if the target JITInfo is usually
273 /// responsible for resolving the address of this relocation.
274 bool letTargetResolve() const {
275 return TargetResolve;
276 }
277
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000278 /// getGlobalValue - If this is a global value reference, return the
279 /// referenced global.
280 GlobalValue *getGlobalValue() const {
Evan Cheng9ed2f802008-11-10 01:08:07 +0000281 assert((isGlobalValue() || isIndirectSymbol()) &&
Evan Chengbe8c03f2008-01-04 10:46:51 +0000282 "This is not a global value reference!");
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000283 return Target.GV;
284 }
285
Evan Chengb4e80f82006-07-27 18:18:13 +0000286 MachineBasicBlock *getBasicBlock() const {
287 assert(isBasicBlock() && "This is not a basic block reference!");
288 return Target.MBB;
289 }
290
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000291 /// getString - If this is a string value, return the string reference.
292 ///
Evan Chengd7398c92008-11-08 07:37:34 +0000293 const char *getExternalSymbol() const {
294 assert(isExternalSymbol() && "This is not an external symbol reference!");
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000295 return Target.ExtSym;
296 }
297
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000298 /// getConstantPoolIndex - If this is a const pool reference, return
299 /// the index into the constant pool.
300 unsigned getConstantPoolIndex() const {
301 assert(isConstantPoolIndex() && "This is not a constant pool reference!");
Evan Cheng52b510b2006-06-23 01:02:37 +0000302 return Target.Index;
303 }
304
305 /// getJumpTableIndex - If this is a jump table reference, return
306 /// the index into the jump table.
307 unsigned getJumpTableIndex() const {
308 assert(isJumpTableIndex() && "This is not a jump table reference!");
309 return Target.Index;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000310 }
311
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000312 /// getResultPointer - Once this has been resolved to point to an actual
313 /// address, this returns the pointer.
314 void *getResultPointer() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000315 assert(AddrType == isResult && "Result pointer isn't set yet!");
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000316 return Target.Result;
317 }
318
319 /// setResultPointer - Set the result to the specified pointer value.
320 ///
321 void setResultPointer(void *Ptr) {
322 Target.Result = Ptr;
Chris Lattner1e3822c2006-05-03 18:52:31 +0000323 AddrType = isResult;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000324 }
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000325
326 /// setGOTIndex - Set the GOT index to a specific value.
327 void setGOTIndex(unsigned idx) {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000328 AddrType = isGOTIndex;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000329 Target.GOTIndex = idx;
330 }
331
332 /// getGOTIndex - Once this has been resolved to an entry in the GOT,
Jeff Cohen9eb59ec2005-07-27 05:53:44 +0000333 /// this returns that index. The index is from the lowest address entry
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000334 /// in the GOT.
335 unsigned getGOTIndex() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000336 assert(AddrType == isGOTIndex);
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000337 return Target.GOTIndex;
338 }
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000339};
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000340}
341
342#endif