LLVM API Documentation
00001 //===-- llvm/CodeGen/MachineRelocation.h - Target Relocation ----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the MachineRelocation class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CODEGEN_MACHINERELOCATION_H 00015 #define LLVM_CODEGEN_MACHINERELOCATION_H 00016 00017 #include "llvm/Support/DataTypes.h" 00018 #include <cassert> 00019 00020 namespace llvm { 00021 class GlobalValue; 00022 class MachineBasicBlock; 00023 00024 /// MachineRelocation - This represents a target-specific relocation value, 00025 /// produced by the code emitter. This relocation is resolved after the has 00026 /// been emitted, either to an object file or to memory, when the target of the 00027 /// relocation can be resolved. 00028 /// 00029 /// A relocation is made up of the following logical portions: 00030 /// 1. An offset in the machine code buffer, the location to modify. 00031 /// 2. A target specific relocation type (a number from 0 to 63). 00032 /// 3. A symbol being referenced, either as a GlobalValue* or as a string. 00033 /// 4. An optional constant value to be added to the reference. 00034 /// 5. A bit, CanRewrite, which indicates to the JIT that a function stub is 00035 /// not needed for the relocation. 00036 /// 6. An index into the GOT, if the target uses a GOT 00037 /// 00038 class MachineRelocation { 00039 enum AddressType { 00040 isResult, // Relocation has be transformed into its result pointer. 00041 isGV, // The Target.GV field is valid. 00042 isIndirectSym, // Relocation of an indirect symbol. 00043 isBB, // Relocation of BB address. 00044 isExtSym, // The Target.ExtSym field is valid. 00045 isConstPool, // Relocation of constant pool address. 00046 isJumpTable, // Relocation of jump table address. 00047 isGOTIndex // The Target.GOTIndex field is valid. 00048 }; 00049 00050 /// Offset - This is the offset from the start of the code buffer of the 00051 /// relocation to perform. 00052 uintptr_t Offset; 00053 00054 /// ConstantVal - A field that may be used by the target relocation type. 00055 intptr_t ConstantVal; 00056 00057 union { 00058 void *Result; // If this has been resolved to a resolved pointer 00059 GlobalValue *GV; // If this is a pointer to a GV or an indirect ref. 00060 MachineBasicBlock *MBB; // If this is a pointer to a LLVM BB 00061 const char *ExtSym; // If this is a pointer to a named symbol 00062 unsigned Index; // Constant pool / jump table index 00063 unsigned GOTIndex; // Index in the GOT of this symbol/global 00064 } Target; 00065 00066 unsigned TargetReloType : 6; // The target relocation ID 00067 AddressType AddrType : 4; // The field of Target to use 00068 bool NeedStub : 1; // True if this relocation requires a stub 00069 bool GOTRelative : 1; // Should this relocation be relative to the GOT? 00070 bool TargetResolve : 1; // True if target should resolve the address 00071 00072 public: 00073 // Relocation types used in a generic implementation. Currently, relocation 00074 // entries for all things use the generic VANILLA type until they are refined 00075 // into target relocation types. 00076 enum RelocationType { 00077 VANILLA 00078 }; 00079 00080 /// MachineRelocation::getGV - Return a relocation entry for a GlobalValue. 00081 /// 00082 static MachineRelocation getGV(uintptr_t offset, unsigned RelocationType, 00083 GlobalValue *GV, intptr_t cst = 0, 00084 bool NeedStub = 0, 00085 bool GOTrelative = 0) { 00086 assert((RelocationType & ~63) == 0 && "Relocation type too large!"); 00087 MachineRelocation Result; 00088 Result.Offset = offset; 00089 Result.ConstantVal = cst; 00090 Result.TargetReloType = RelocationType; 00091 Result.AddrType = isGV; 00092 Result.NeedStub = NeedStub; 00093 Result.GOTRelative = GOTrelative; 00094 Result.TargetResolve = false; 00095 Result.Target.GV = GV; 00096 return Result; 00097 } 00098 00099 /// MachineRelocation::getIndirectSymbol - Return a relocation entry for an 00100 /// indirect symbol. 00101 static MachineRelocation getIndirectSymbol(uintptr_t offset, 00102 unsigned RelocationType, 00103 GlobalValue *GV, intptr_t cst = 0, 00104 bool NeedStub = 0, 00105 bool GOTrelative = 0) { 00106 assert((RelocationType & ~63) == 0 && "Relocation type too large!"); 00107 MachineRelocation Result; 00108 Result.Offset = offset; 00109 Result.ConstantVal = cst; 00110 Result.TargetReloType = RelocationType; 00111 Result.AddrType = isIndirectSym; 00112 Result.NeedStub = NeedStub; 00113 Result.GOTRelative = GOTrelative; 00114 Result.TargetResolve = false; 00115 Result.Target.GV = GV; 00116 return Result; 00117 } 00118 00119 /// MachineRelocation::getBB - Return a relocation entry for a BB. 00120 /// 00121 static MachineRelocation getBB(uintptr_t offset,unsigned RelocationType, 00122 MachineBasicBlock *MBB, intptr_t cst = 0) { 00123 assert((RelocationType & ~63) == 0 && "Relocation type too large!"); 00124 MachineRelocation Result; 00125 Result.Offset = offset; 00126 Result.ConstantVal = cst; 00127 Result.TargetReloType = RelocationType; 00128 Result.AddrType = isBB; 00129 Result.NeedStub = false; 00130 Result.GOTRelative = false; 00131 Result.TargetResolve = false; 00132 Result.Target.MBB = MBB; 00133 return Result; 00134 } 00135 00136 /// MachineRelocation::getExtSym - Return a relocation entry for an external 00137 /// symbol, like "free". 00138 /// 00139 static MachineRelocation getExtSym(uintptr_t offset, unsigned RelocationType, 00140 const char *ES, intptr_t cst = 0, 00141 bool GOTrelative = 0) { 00142 assert((RelocationType & ~63) == 0 && "Relocation type too large!"); 00143 MachineRelocation Result; 00144 Result.Offset = offset; 00145 Result.ConstantVal = cst; 00146 Result.TargetReloType = RelocationType; 00147 Result.AddrType = isExtSym; 00148 Result.NeedStub = true; 00149 Result.GOTRelative = GOTrelative; 00150 Result.TargetResolve = false; 00151 Result.Target.ExtSym = ES; 00152 return Result; 00153 } 00154 00155 /// MachineRelocation::getConstPool - Return a relocation entry for a constant 00156 /// pool entry. 00157 /// 00158 static MachineRelocation getConstPool(uintptr_t offset,unsigned RelocationType, 00159 unsigned CPI, intptr_t cst = 0, 00160 bool letTargetResolve = false) { 00161 assert((RelocationType & ~63) == 0 && "Relocation type too large!"); 00162 MachineRelocation Result; 00163 Result.Offset = offset; 00164 Result.ConstantVal = cst; 00165 Result.TargetReloType = RelocationType; 00166 Result.AddrType = isConstPool; 00167 Result.NeedStub = false; 00168 Result.GOTRelative = false; 00169 Result.TargetResolve = letTargetResolve; 00170 Result.Target.Index = CPI; 00171 return Result; 00172 } 00173 00174 /// MachineRelocation::getJumpTable - Return a relocation entry for a jump 00175 /// table entry. 00176 /// 00177 static MachineRelocation getJumpTable(uintptr_t offset,unsigned RelocationType, 00178 unsigned JTI, intptr_t cst = 0, 00179 bool letTargetResolve = false) { 00180 assert((RelocationType & ~63) == 0 && "Relocation type too large!"); 00181 MachineRelocation Result; 00182 Result.Offset = offset; 00183 Result.ConstantVal = cst; 00184 Result.TargetReloType = RelocationType; 00185 Result.AddrType = isJumpTable; 00186 Result.NeedStub = false; 00187 Result.GOTRelative = false; 00188 Result.TargetResolve = letTargetResolve; 00189 Result.Target.Index = JTI; 00190 return Result; 00191 } 00192 00193 /// getMachineCodeOffset - Return the offset into the code buffer that the 00194 /// relocation should be performed. 00195 intptr_t getMachineCodeOffset() const { 00196 return Offset; 00197 } 00198 00199 /// getRelocationType - Return the target-specific relocation ID for this 00200 /// relocation. 00201 unsigned getRelocationType() const { 00202 return TargetReloType; 00203 } 00204 00205 /// getConstantVal - Get the constant value associated with this relocation. 00206 /// This is often an offset from the symbol. 00207 /// 00208 intptr_t getConstantVal() const { 00209 return ConstantVal; 00210 } 00211 00212 /// setConstantVal - Set the constant value associated with this relocation. 00213 /// This is often an offset from the symbol. 00214 /// 00215 void setConstantVal(intptr_t val) { 00216 ConstantVal = val; 00217 } 00218 00219 /// isGlobalValue - Return true if this relocation is a GlobalValue, as 00220 /// opposed to a constant string. 00221 bool isGlobalValue() const { 00222 return AddrType == isGV; 00223 } 00224 00225 /// isIndirectSymbol - Return true if this relocation is the address an 00226 /// indirect symbol 00227 bool isIndirectSymbol() const { 00228 return AddrType == isIndirectSym; 00229 } 00230 00231 /// isBasicBlock - Return true if this relocation is a basic block reference. 00232 /// 00233 bool isBasicBlock() const { 00234 return AddrType == isBB; 00235 } 00236 00237 /// isExternalSymbol - Return true if this is a constant string. 00238 /// 00239 bool isExternalSymbol() const { 00240 return AddrType == isExtSym; 00241 } 00242 00243 /// isConstantPoolIndex - Return true if this is a constant pool reference. 00244 /// 00245 bool isConstantPoolIndex() const { 00246 return AddrType == isConstPool; 00247 } 00248 00249 /// isJumpTableIndex - Return true if this is a jump table reference. 00250 /// 00251 bool isJumpTableIndex() const { 00252 return AddrType == isJumpTable; 00253 } 00254 00255 /// isGOTRelative - Return true the target wants the index into the GOT of 00256 /// the symbol rather than the address of the symbol. 00257 bool isGOTRelative() const { 00258 return GOTRelative; 00259 } 00260 00261 /// doesntNeedStub - This function returns true if the JIT for this target 00262 /// target is capable of directly handling the relocated GlobalValue reference 00263 /// without using either a stub function or issuing an extra load to get the 00264 /// GV address. 00265 bool doesntNeedStub() const { 00266 return !NeedStub; 00267 } 00268 00269 /// letTargetResolve - Return true if the target JITInfo is usually 00270 /// responsible for resolving the address of this relocation. 00271 bool letTargetResolve() const { 00272 return TargetResolve; 00273 } 00274 00275 /// getGlobalValue - If this is a global value reference, return the 00276 /// referenced global. 00277 GlobalValue *getGlobalValue() const { 00278 assert((isGlobalValue() || isIndirectSymbol()) && 00279 "This is not a global value reference!"); 00280 return Target.GV; 00281 } 00282 00283 MachineBasicBlock *getBasicBlock() const { 00284 assert(isBasicBlock() && "This is not a basic block reference!"); 00285 return Target.MBB; 00286 } 00287 00288 /// getString - If this is a string value, return the string reference. 00289 /// 00290 const char *getExternalSymbol() const { 00291 assert(isExternalSymbol() && "This is not an external symbol reference!"); 00292 return Target.ExtSym; 00293 } 00294 00295 /// getConstantPoolIndex - If this is a const pool reference, return 00296 /// the index into the constant pool. 00297 unsigned getConstantPoolIndex() const { 00298 assert(isConstantPoolIndex() && "This is not a constant pool reference!"); 00299 return Target.Index; 00300 } 00301 00302 /// getJumpTableIndex - If this is a jump table reference, return 00303 /// the index into the jump table. 00304 unsigned getJumpTableIndex() const { 00305 assert(isJumpTableIndex() && "This is not a jump table reference!"); 00306 return Target.Index; 00307 } 00308 00309 /// getResultPointer - Once this has been resolved to point to an actual 00310 /// address, this returns the pointer. 00311 void *getResultPointer() const { 00312 assert(AddrType == isResult && "Result pointer isn't set yet!"); 00313 return Target.Result; 00314 } 00315 00316 /// setResultPointer - Set the result to the specified pointer value. 00317 /// 00318 void setResultPointer(void *Ptr) { 00319 Target.Result = Ptr; 00320 AddrType = isResult; 00321 } 00322 00323 /// setGOTIndex - Set the GOT index to a specific value. 00324 void setGOTIndex(unsigned idx) { 00325 AddrType = isGOTIndex; 00326 Target.GOTIndex = idx; 00327 } 00328 00329 /// getGOTIndex - Once this has been resolved to an entry in the GOT, 00330 /// this returns that index. The index is from the lowest address entry 00331 /// in the GOT. 00332 unsigned getGOTIndex() const { 00333 assert(AddrType == isGOTIndex); 00334 return Target.GOTIndex; 00335 } 00336 }; 00337 } 00338 00339 #endif