LLVM API Documentation

MachineRelocation.h
Go to the documentation of this file.
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 an 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 MayNeedFarStub     : 1; // True if this relocation may require a far-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 MayNeedFarStub = 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.MayNeedFarStub = MayNeedFarStub;
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 MayNeedFarStub = 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.MayNeedFarStub = MayNeedFarStub;
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.MayNeedFarStub = 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                                      bool NeedStub = true) {
00143     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
00144     MachineRelocation Result;
00145     Result.Offset = offset;
00146     Result.ConstantVal = cst;
00147     Result.TargetReloType = RelocationType;
00148     Result.AddrType = isExtSym;
00149     Result.MayNeedFarStub = NeedStub;
00150     Result.GOTRelative = GOTrelative;
00151     Result.TargetResolve = false;
00152     Result.Target.ExtSym = ES;
00153     return Result;
00154   }
00155 
00156   /// MachineRelocation::getConstPool - Return a relocation entry for a constant
00157   /// pool entry.
00158   ///
00159   static MachineRelocation getConstPool(uintptr_t offset,unsigned RelocationType,
00160                                         unsigned CPI, intptr_t cst = 0,
00161                                         bool letTargetResolve = false) {
00162     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
00163     MachineRelocation Result;
00164     Result.Offset = offset;
00165     Result.ConstantVal = cst;
00166     Result.TargetReloType = RelocationType;
00167     Result.AddrType = isConstPool;
00168     Result.MayNeedFarStub = false;
00169     Result.GOTRelative = false;
00170     Result.TargetResolve = letTargetResolve;
00171     Result.Target.Index = CPI;
00172     return Result;
00173   }
00174 
00175   /// MachineRelocation::getJumpTable - Return a relocation entry for a jump
00176   /// table entry.
00177   ///
00178   static MachineRelocation getJumpTable(uintptr_t offset,unsigned RelocationType,
00179                                         unsigned JTI, intptr_t cst = 0,
00180                                         bool letTargetResolve = false) {
00181     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
00182     MachineRelocation Result;
00183     Result.Offset = offset;
00184     Result.ConstantVal = cst;
00185     Result.TargetReloType = RelocationType;
00186     Result.AddrType = isJumpTable;
00187     Result.MayNeedFarStub = false;
00188     Result.GOTRelative = false;
00189     Result.TargetResolve = letTargetResolve;
00190     Result.Target.Index = JTI;
00191     return Result;
00192   }
00193 
00194   /// getMachineCodeOffset - Return the offset into the code buffer that the
00195   /// relocation should be performed.
00196   intptr_t getMachineCodeOffset() const {
00197     return Offset;
00198   }
00199 
00200   /// getRelocationType - Return the target-specific relocation ID for this
00201   /// relocation.
00202   unsigned getRelocationType() const {
00203     return TargetReloType;
00204   }
00205 
00206   /// getConstantVal - Get the constant value associated with this relocation.
00207   /// This is often an offset from the symbol.
00208   ///
00209   intptr_t getConstantVal() const {
00210     return ConstantVal;
00211   }
00212 
00213   /// setConstantVal - Set the constant value associated with this relocation.
00214   /// This is often an offset from the symbol.
00215   ///
00216   void setConstantVal(intptr_t val) {
00217     ConstantVal = val;
00218   }
00219 
00220   /// isGlobalValue - Return true if this relocation is a GlobalValue, as
00221   /// opposed to a constant string.
00222   bool isGlobalValue() const {
00223     return AddrType == isGV;
00224   }
00225 
00226   /// isIndirectSymbol - Return true if this relocation is the address an
00227   /// indirect symbol
00228   bool isIndirectSymbol() const {
00229     return AddrType == isIndirectSym;
00230   }
00231 
00232   /// isBasicBlock - Return true if this relocation is a basic block reference.
00233   ///
00234   bool isBasicBlock() const {
00235     return AddrType == isBB;
00236   }
00237 
00238   /// isExternalSymbol - Return true if this is a constant string.
00239   ///
00240   bool isExternalSymbol() const {
00241     return AddrType == isExtSym;
00242   }
00243 
00244   /// isConstantPoolIndex - Return true if this is a constant pool reference.
00245   ///
00246   bool isConstantPoolIndex() const {
00247     return AddrType == isConstPool;
00248   }
00249 
00250   /// isJumpTableIndex - Return true if this is a jump table reference.
00251   ///
00252   bool isJumpTableIndex() const {
00253     return AddrType == isJumpTable;
00254   }
00255 
00256   /// isGOTRelative - Return true the target wants the index into the GOT of
00257   /// the symbol rather than the address of the symbol.
00258   bool isGOTRelative() const {
00259     return GOTRelative;
00260   }
00261 
00262   /// mayNeedFarStub - This function returns true if the JIT for this target may
00263   /// need either a stub function or an indirect global-variable load to handle
00264   /// the relocated GlobalValue reference.  For example, the x86-64 call
00265   /// instruction can only call functions within +/-2GB of the call site.
00266   /// Anything farther away needs a longer mov+call sequence, which can't just
00267   /// be written on top of the existing call.
00268   bool mayNeedFarStub() const {
00269     return MayNeedFarStub;
00270   }
00271 
00272   /// letTargetResolve - Return true if the target JITInfo is usually
00273   /// responsible for resolving the address of this relocation.
00274   bool letTargetResolve() const {
00275     return TargetResolve;
00276   }
00277 
00278   /// getGlobalValue - If this is a global value reference, return the
00279   /// referenced global.
00280   GlobalValue *getGlobalValue() const {
00281     assert((isGlobalValue() || isIndirectSymbol()) &&
00282            "This is not a global value reference!");
00283     return Target.GV;
00284   }
00285 
00286   MachineBasicBlock *getBasicBlock() const {
00287     assert(isBasicBlock() && "This is not a basic block reference!");
00288     return Target.MBB;
00289   }
00290 
00291   /// getString - If this is a string value, return the string reference.
00292   ///
00293   const char *getExternalSymbol() const {
00294     assert(isExternalSymbol() && "This is not an external symbol reference!");
00295     return Target.ExtSym;
00296   }
00297 
00298   /// getConstantPoolIndex - If this is a const pool reference, return
00299   /// the index into the constant pool.
00300   unsigned getConstantPoolIndex() const {
00301     assert(isConstantPoolIndex() && "This is not a constant pool reference!");
00302     return Target.Index;
00303   }
00304 
00305   /// getJumpTableIndex - If this is a jump table reference, return
00306   /// the index into the jump table.
00307   unsigned getJumpTableIndex() const {
00308     assert(isJumpTableIndex() && "This is not a jump table reference!");
00309     return Target.Index;
00310   }
00311 
00312   /// getResultPointer - Once this has been resolved to point to an actual
00313   /// address, this returns the pointer.
00314   void *getResultPointer() const {
00315     assert(AddrType == isResult && "Result pointer isn't set yet!");
00316     return Target.Result;
00317   }
00318 
00319   /// setResultPointer - Set the result to the specified pointer value.
00320   ///
00321   void setResultPointer(void *Ptr) {
00322     Target.Result = Ptr;
00323     AddrType = isResult;
00324   }
00325 
00326   /// setGOTIndex - Set the GOT index to a specific value.
00327   void setGOTIndex(unsigned idx) {
00328     AddrType = isGOTIndex;
00329     Target.GOTIndex = idx;
00330   }
00331 
00332   /// getGOTIndex - Once this has been resolved to an entry in the GOT,
00333   /// this returns that index.  The index is from the lowest address entry
00334   /// in the GOT.
00335   unsigned getGOTIndex() const {
00336     assert(AddrType == isGOTIndex);
00337     return Target.GOTIndex;
00338   }
00339 };
00340 }
00341 
00342 #endif