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 {
38 /// OffsetTypeExternal - The low 24-bits of this value is the offset from the
39 /// start of the code buffer of the relocation to perform. Bit 24 of this is
40 /// set if Target should use ExtSym instead of GV, Bit 25 is the CanRewrite
41 /// bit, and the high 6 bits hold the relocation type.
42 // FIXME: with the additional types of relocatable things, rearrange the
43 // storage of things to be a bit more effiecient
44 unsigned OffsetTypeExternal;
46 GlobalValue *GV; // If this is a pointer to an LLVM global
47 const char *ExtSym; // If this is a pointer to a named symbol
48 void *Result; // If this has been resolved to a resolved pointer
49 unsigned GOTIndex; // Index in the GOT of this symbol/global
50 unsigned CPool; // Index in the Constant Pool
53 bool GOTRelative; //out of bits in OffsetTypeExternal
57 MachineRelocation(unsigned Offset, unsigned RelocationType, GlobalValue *GV,
58 intptr_t cst = 0, bool DoesntNeedFunctionStub = 0,
60 : OffsetTypeExternal(Offset + (RelocationType << 26)), ConstantVal(cst),
61 GOTRelative(GOTrelative), isConstPool(0) {
62 assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
63 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
65 if (DoesntNeedFunctionStub)
66 OffsetTypeExternal |= 1 << 25;
69 MachineRelocation(unsigned Offset, unsigned RelocationType, const char *ES,
70 intptr_t cst = 0, bool GOTrelative = 0)
71 : OffsetTypeExternal(Offset + (1 << 24) + (RelocationType << 26)),
72 ConstantVal(cst), GOTRelative(GOTrelative), isConstPool(0) {
73 assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
74 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
78 MachineRelocation(unsigned Offset, unsigned RelocationType, unsigned CPI,
80 : OffsetTypeExternal(Offset + (RelocationType << 26)),
81 ConstantVal(cst), GOTRelative(0), isConstPool(1) {
82 assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
83 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
87 /// getMachineCodeOffset - Return the offset into the code buffer that the
88 /// relocation should be performed.
89 unsigned getMachineCodeOffset() const {
90 return OffsetTypeExternal & ((1 << 24)-1);
93 /// getRelocationType - Return the target-specific relocation ID for this
95 unsigned getRelocationType() const {
96 return OffsetTypeExternal >> 26;
99 /// getConstantVal - Get the constant value associated with this relocation.
100 /// This is often an offset from the symbol.
102 intptr_t getConstantVal() const {
106 /// isGlobalValue - Return true if this relocation is a GlobalValue, as
107 /// opposed to a constant string.
108 bool isGlobalValue() const {
109 return (OffsetTypeExternal & (1 << 24)) == 0 && !isConstantPoolIndex();
112 /// isString - Return true if this is a constant string.
114 bool isString() const {
115 return !isGlobalValue() && !isConstantPoolIndex();
118 /// isConstantPoolIndex - Return true if this is a constant pool reference.
120 bool isConstantPoolIndex() const {
124 /// isGOTRelative - Return true the target wants the index into the GOT of
125 /// the symbol rather than the address of the symbol.
126 bool isGOTRelative() const {
130 /// doesntNeedFunctionStub - This function returns true if the JIT for this
131 /// target is capable of directly handling the relocated instruction without
132 /// using a stub function. It is always conservatively correct for this flag
133 /// to be false, but targets can improve their compilation callback functions
134 /// to handle more general cases if they want improved performance.
135 bool doesntNeedFunctionStub() const {
136 return (OffsetTypeExternal & (1 << 25)) != 0;
139 /// getGlobalValue - If this is a global value reference, return the
140 /// referenced global.
141 GlobalValue *getGlobalValue() const {
142 assert(isGlobalValue() && "This is not a global value reference!");
146 /// getString - If this is a string value, return the string reference.
148 const char *getString() const {
149 assert(isString() && "This is not a string reference!");
150 return Target.ExtSym;
153 /// getConstantPoolIndex - If this is a const pool reference, return
154 /// the index into the constant pool.
155 unsigned getConstantPoolIndex() const {
156 assert(isConstantPoolIndex() && "This is not a constant pool reference!");
160 /// getResultPointer - Once this has been resolved to point to an actual
161 /// address, this returns the pointer.
162 void *getResultPointer() const {
163 return Target.Result;
166 /// setResultPointer - Set the result to the specified pointer value.
168 void setResultPointer(void *Ptr) {
172 /// setGOTIndex - Set the GOT index to a specific value.
173 void setGOTIndex(unsigned idx) {
174 Target.GOTIndex = idx;
177 /// getGOTIndex - Once this has been resolved to an entry in the GOT,
178 /// this returns that index. The index is from the lowest address entry
180 unsigned getGOTIndex() const {
181 return Target.GOTIndex;