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(unsigned offset, unsigned RelocationType, GlobalValue *GV,
68 intptr_t cst = 0, bool DoesntNeedFunctionStub = 0,
70 : Offset(offset), ConstantVal(cst), TargetReloType(RelocationType),
71 AddrType(isGV), DoesntNeedFnStub(DoesntNeedFunctionStub),
72 GOTRelative(GOTrelative){
73 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
77 MachineRelocation(unsigned offset, unsigned RelocationType, const char *ES,
78 intptr_t cst = 0, bool GOTrelative = 0)
79 : Offset(offset), ConstantVal(cst), TargetReloType(RelocationType),
80 AddrType(isExtSym), DoesntNeedFnStub(false), GOTRelative(GOTrelative) {
81 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
85 MachineRelocation(unsigned offset, unsigned RelocationType, unsigned CPI,
87 : Offset(offset), ConstantVal(cst), TargetReloType(RelocationType),
88 AddrType(isConstPool), DoesntNeedFnStub(false), GOTRelative(0) {
89 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
90 Target.ConstPool = CPI;
93 /// getMachineCodeOffset - Return the offset into the code buffer that the
94 /// relocation should be performed.
95 unsigned getMachineCodeOffset() const {
99 /// getRelocationType - Return the target-specific relocation ID for this
101 unsigned getRelocationType() const {
102 return TargetReloType;
105 /// getConstantVal - Get the constant value associated with this relocation.
106 /// This is often an offset from the symbol.
108 intptr_t getConstantVal() const {
112 /// isGlobalValue - Return true if this relocation is a GlobalValue, as
113 /// opposed to a constant string.
114 bool isGlobalValue() const {
115 return AddrType == isGV;
118 /// isString - Return true if this is a constant string.
120 bool isString() const {
121 return AddrType == isExtSym;
124 /// isConstantPoolIndex - Return true if this is a constant pool reference.
126 bool isConstantPoolIndex() const {
127 return AddrType == isConstPool;
130 /// isGOTRelative - Return true the target wants the index into the GOT of
131 /// the symbol rather than the address of the symbol.
132 bool isGOTRelative() const {
136 /// doesntNeedFunctionStub - This function returns true if the JIT for this
137 /// target is capable of directly handling the relocated instruction without
138 /// using a stub function. It is always conservatively correct for this flag
139 /// to be false, but targets can improve their compilation callback functions
140 /// to handle more general cases if they want improved performance.
141 bool doesntNeedFunctionStub() const {
142 return DoesntNeedFnStub;
145 /// getGlobalValue - If this is a global value reference, return the
146 /// referenced global.
147 GlobalValue *getGlobalValue() const {
148 assert(isGlobalValue() && "This is not a global value reference!");
152 /// getString - If this is a string value, return the string reference.
154 const char *getString() const {
155 assert(isString() && "This is not a string reference!");
156 return Target.ExtSym;
159 /// getConstantPoolIndex - If this is a const pool reference, return
160 /// the index into the constant pool.
161 unsigned getConstantPoolIndex() const {
162 assert(isConstantPoolIndex() && "This is not a constant pool reference!");
163 return Target.ConstPool;
166 /// getResultPointer - Once this has been resolved to point to an actual
167 /// address, this returns the pointer.
168 void *getResultPointer() const {
169 assert(AddrType == isResult && "Result pointer isn't set yet!");
170 return Target.Result;
173 /// setResultPointer - Set the result to the specified pointer value.
175 void setResultPointer(void *Ptr) {
180 /// setGOTIndex - Set the GOT index to a specific value.
181 void setGOTIndex(unsigned idx) {
182 AddrType = isGOTIndex;
183 Target.GOTIndex = idx;
186 /// getGOTIndex - Once this has been resolved to an entry in the GOT,
187 /// this returns that index. The index is from the lowest address entry
189 unsigned getGOTIndex() const {
190 assert(AddrType == isGOTIndex);
191 return Target.GOTIndex;