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, // The Target.ConstPool field is valid.
43 isGOTIndex // The Target.GOTIndex field is valid.
46 /// Offset - This is the offset from the start of the code buffer of the
47 /// relocation to perform.
50 /// ConstantVal - A field that may be used by the target relocation type.
54 void *Result; // If this has been resolved to a resolved pointer
55 GlobalValue *GV; // If this is a pointer to an LLVM global
56 const char *ExtSym; // If this is a pointer to a named symbol
57 unsigned ConstPool; // In this is a pointer to a constant pool entry
58 unsigned GOTIndex; // Index in the GOT of this symbol/global
61 unsigned TargetReloType : 6; // The target relocation ID.
62 AddressType AddrType : 3; // The field of Target to use.
63 bool DoesntNeedFnStub : 1; // True if we don't need a fn stub.
64 bool GOTRelative : 1; // Should this relocation be relative to the GOT?
67 /// MachineRelocation::getGV - Return a relocation entry for a GlobalValue.
69 static MachineRelocation getGV(intptr_t offset, unsigned RelocationType,
70 GlobalValue *GV, intptr_t cst = 0,
71 bool DoesntNeedFunctionStub = 0,
72 bool GOTrelative = 0) {
73 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
74 MachineRelocation Result;
75 Result.Offset = offset;
76 Result.ConstantVal = cst;
77 Result.TargetReloType = RelocationType;
78 Result.AddrType = isGV;
79 Result.DoesntNeedFnStub = DoesntNeedFunctionStub;
80 Result.GOTRelative = GOTrelative;
81 Result.Target.GV = GV;
85 /// MachineRelocation::getExtSym - Return a relocation entry for an external
86 /// symbol, like "free".
88 static MachineRelocation getExtSym(intptr_t offset, unsigned RelocationType,
89 const char *ES, intptr_t cst = 0,
90 bool GOTrelative = 0) {
91 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
92 MachineRelocation Result;
93 Result.Offset = offset;
94 Result.ConstantVal = cst;
95 Result.TargetReloType = RelocationType;
96 Result.AddrType = isExtSym;
97 Result.DoesntNeedFnStub = false;
98 Result.GOTRelative = GOTrelative;
99 Result.Target.ExtSym = ES;
103 /// MachineRelocation::getConstPool - Return a relocation entry for a constant
106 static MachineRelocation getConstPool(intptr_t offset,unsigned RelocationType,
107 unsigned CPI, intptr_t cst = 0) {
108 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
109 MachineRelocation Result;
110 Result.Offset = offset;
111 Result.ConstantVal = cst;
112 Result.TargetReloType = RelocationType;
113 Result.AddrType = isConstPool;
114 Result.DoesntNeedFnStub = false;
115 Result.GOTRelative = false;
116 Result.Target.ConstPool = CPI;
120 /// getMachineCodeOffset - Return the offset into the code buffer that the
121 /// relocation should be performed.
122 intptr_t getMachineCodeOffset() const {
126 /// getRelocationType - Return the target-specific relocation ID for this
128 unsigned getRelocationType() const {
129 return TargetReloType;
132 /// getConstantVal - Get the constant value associated with this relocation.
133 /// This is often an offset from the symbol.
135 intptr_t getConstantVal() const {
139 /// isGlobalValue - Return true if this relocation is a GlobalValue, as
140 /// opposed to a constant string.
141 bool isGlobalValue() const {
142 return AddrType == isGV;
145 /// isString - Return true if this is a constant string.
147 bool isString() const {
148 return AddrType == isExtSym;
151 /// isConstantPoolIndex - Return true if this is a constant pool reference.
153 bool isConstantPoolIndex() const {
154 return AddrType == isConstPool;
157 /// isGOTRelative - Return true the target wants the index into the GOT of
158 /// the symbol rather than the address of the symbol.
159 bool isGOTRelative() const {
163 /// doesntNeedFunctionStub - This function returns true if the JIT for this
164 /// target is capable of directly handling the relocated instruction without
165 /// using a stub function. It is always conservatively correct for this flag
166 /// to be false, but targets can improve their compilation callback functions
167 /// to handle more general cases if they want improved performance.
168 bool doesntNeedFunctionStub() const {
169 return DoesntNeedFnStub;
172 /// getGlobalValue - If this is a global value reference, return the
173 /// referenced global.
174 GlobalValue *getGlobalValue() const {
175 assert(isGlobalValue() && "This is not a global value reference!");
179 /// getString - If this is a string value, return the string reference.
181 const char *getString() const {
182 assert(isString() && "This is not a string reference!");
183 return Target.ExtSym;
186 /// getConstantPoolIndex - If this is a const pool reference, return
187 /// the index into the constant pool.
188 unsigned getConstantPoolIndex() const {
189 assert(isConstantPoolIndex() && "This is not a constant pool reference!");
190 return Target.ConstPool;
193 /// getResultPointer - Once this has been resolved to point to an actual
194 /// address, this returns the pointer.
195 void *getResultPointer() const {
196 assert(AddrType == isResult && "Result pointer isn't set yet!");
197 return Target.Result;
200 /// setResultPointer - Set the result to the specified pointer value.
202 void setResultPointer(void *Ptr) {
207 /// setGOTIndex - Set the GOT index to a specific value.
208 void setGOTIndex(unsigned idx) {
209 AddrType = isGOTIndex;
210 Target.GOTIndex = idx;
213 /// getGOTIndex - Once this has been resolved to an entry in the GOT,
214 /// this returns that index. The index is from the lowest address entry
216 unsigned getGOTIndex() const {
217 assert(AddrType == isGOTIndex);
218 return Target.GOTIndex;