2004-11-20 04:05:50 +01:00
|
|
|
//===-- llvm/CodeGen/MachineRelocation.h - Target Relocation ----*- C++ -*-===//
|
2005-04-21 22:39:54 +02:00
|
|
|
//
|
2004-11-20 04:05:50 +01:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:39:54 +02:00
|
|
|
//
|
2004-11-20 04:05:50 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the MachineRelocation class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CODEGEN_MACHINERELOCATION_H
|
|
|
|
#define LLVM_CODEGEN_MACHINERELOCATION_H
|
|
|
|
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2004-11-21 00:40:54 +01:00
|
|
|
#include <cassert>
|
2004-11-20 04:05:50 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class GlobalValue;
|
2006-07-27 20:18:13 +02:00
|
|
|
class MachineBasicBlock;
|
2004-11-20 04:05:50 +01:00
|
|
|
|
|
|
|
/// MachineRelocation - This represents a target-specific relocation value,
|
|
|
|
/// produced by the code emitter. This relocation is resolved after the has
|
|
|
|
/// been emitted, either to an object file or to memory, when the target of the
|
|
|
|
/// relocation can be resolved.
|
|
|
|
///
|
|
|
|
/// A relocation is made up of the following logical portions:
|
|
|
|
/// 1. An offset in the machine code buffer, the location to modify.
|
2004-11-21 04:27:13 +01:00
|
|
|
/// 2. A target specific relocation type (a number from 0 to 63).
|
2004-11-20 04:05:50 +01:00
|
|
|
/// 3. A symbol being referenced, either as a GlobalValue* or as a string.
|
|
|
|
/// 4. An optional constant value to be added to the reference.
|
2004-11-21 04:27:13 +01:00
|
|
|
/// 5. A bit, CanRewrite, which indicates to the JIT that a function stub is
|
|
|
|
/// not needed for the relocation.
|
2005-07-22 22:46:42 +02:00
|
|
|
/// 6. An index into the GOT, if the target uses a GOT
|
2004-11-20 04:05:50 +01:00
|
|
|
///
|
|
|
|
class MachineRelocation {
|
2006-05-03 20:52:31 +02:00
|
|
|
enum AddressType {
|
|
|
|
isResult, // Relocation has be transformed into its result pointer.
|
|
|
|
isGV, // The Target.GV field is valid.
|
2008-11-10 02:08:07 +01:00
|
|
|
isIndirectSym, // Relocation of an indirect symbol.
|
2006-07-27 20:18:13 +02:00
|
|
|
isBB, // Relocation of BB address.
|
2006-05-03 20:52:31 +02:00
|
|
|
isExtSym, // The Target.ExtSym field is valid.
|
2006-06-23 03:02:37 +02:00
|
|
|
isConstPool, // Relocation of constant pool address.
|
|
|
|
isJumpTable, // Relocation of jump table address.
|
2006-05-03 20:52:31 +02:00
|
|
|
isGOTIndex // The Target.GOTIndex field is valid.
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Offset - This is the offset from the start of the code buffer of the
|
|
|
|
/// relocation to perform.
|
2008-12-10 03:32:19 +01:00
|
|
|
uintptr_t Offset;
|
2006-05-03 20:52:31 +02:00
|
|
|
|
|
|
|
/// ConstantVal - A field that may be used by the target relocation type.
|
|
|
|
intptr_t ConstantVal;
|
|
|
|
|
2004-11-20 04:05:50 +01:00
|
|
|
union {
|
2006-07-27 20:18:13 +02:00
|
|
|
void *Result; // If this has been resolved to a resolved pointer
|
2008-11-10 02:08:07 +01:00
|
|
|
GlobalValue *GV; // If this is a pointer to a GV or an indirect ref.
|
2006-07-27 20:18:13 +02:00
|
|
|
MachineBasicBlock *MBB; // If this is a pointer to a LLVM BB
|
|
|
|
const char *ExtSym; // If this is a pointer to a named symbol
|
|
|
|
unsigned Index; // Constant pool / jump table index
|
|
|
|
unsigned GOTIndex; // Index in the GOT of this symbol/global
|
2004-11-20 04:05:50 +01:00
|
|
|
} Target;
|
2006-05-03 20:52:31 +02:00
|
|
|
|
2008-10-30 00:53:42 +01:00
|
|
|
unsigned TargetReloType : 6; // The target relocation ID
|
|
|
|
AddressType AddrType : 4; // The field of Target to use
|
2009-11-07 09:51:52 +01:00
|
|
|
bool MayNeedFarStub : 1; // True if this relocation may require a far-stub
|
2006-05-03 20:52:31 +02:00
|
|
|
bool GOTRelative : 1; // Should this relocation be relative to the GOT?
|
2008-10-30 00:53:42 +01:00
|
|
|
bool TargetResolve : 1; // True if target should resolve the address
|
2005-07-22 22:46:42 +02:00
|
|
|
|
2004-11-20 04:05:50 +01:00
|
|
|
public:
|
2006-12-11 03:19:29 +01:00
|
|
|
// Relocation types used in a generic implementation. Currently, relocation
|
|
|
|
// entries for all things use the generic VANILLA type until they are refined
|
|
|
|
// into target relocation types.
|
|
|
|
enum RelocationType {
|
|
|
|
VANILLA
|
|
|
|
};
|
|
|
|
|
2006-05-03 22:30:20 +02:00
|
|
|
/// MachineRelocation::getGV - Return a relocation entry for a GlobalValue.
|
|
|
|
///
|
2008-12-10 03:32:19 +01:00
|
|
|
static MachineRelocation getGV(uintptr_t offset, unsigned RelocationType,
|
2006-05-03 22:30:20 +02:00
|
|
|
GlobalValue *GV, intptr_t cst = 0,
|
2009-11-07 09:51:52 +01:00
|
|
|
bool MayNeedFarStub = 0,
|
2006-05-03 22:30:20 +02:00
|
|
|
bool GOTrelative = 0) {
|
2004-11-21 04:27:13 +01:00
|
|
|
assert((RelocationType & ~63) == 0 && "Relocation type too large!");
|
2006-05-03 22:30:20 +02:00
|
|
|
MachineRelocation Result;
|
|
|
|
Result.Offset = offset;
|
|
|
|
Result.ConstantVal = cst;
|
|
|
|
Result.TargetReloType = RelocationType;
|
|
|
|
Result.AddrType = isGV;
|
2009-11-07 09:51:52 +01:00
|
|
|
Result.MayNeedFarStub = MayNeedFarStub;
|
2006-05-03 22:30:20 +02:00
|
|
|
Result.GOTRelative = GOTrelative;
|
2008-10-30 00:53:42 +01:00
|
|
|
Result.TargetResolve = false;
|
2006-05-03 22:30:20 +02:00
|
|
|
Result.Target.GV = GV;
|
|
|
|
return Result;
|
2004-11-20 04:05:50 +01:00
|
|
|
}
|
|
|
|
|
2008-11-10 02:08:07 +01:00
|
|
|
/// MachineRelocation::getIndirectSymbol - Return a relocation entry for an
|
|
|
|
/// indirect symbol.
|
2008-12-10 03:32:19 +01:00
|
|
|
static MachineRelocation getIndirectSymbol(uintptr_t offset,
|
2008-11-10 02:08:07 +01:00
|
|
|
unsigned RelocationType,
|
|
|
|
GlobalValue *GV, intptr_t cst = 0,
|
2009-11-07 09:51:52 +01:00
|
|
|
bool MayNeedFarStub = 0,
|
2008-11-10 02:08:07 +01:00
|
|
|
bool GOTrelative = 0) {
|
2008-01-04 11:46:51 +01:00
|
|
|
assert((RelocationType & ~63) == 0 && "Relocation type too large!");
|
|
|
|
MachineRelocation Result;
|
|
|
|
Result.Offset = offset;
|
|
|
|
Result.ConstantVal = cst;
|
|
|
|
Result.TargetReloType = RelocationType;
|
2008-11-10 02:08:07 +01:00
|
|
|
Result.AddrType = isIndirectSym;
|
2009-11-07 09:51:52 +01:00
|
|
|
Result.MayNeedFarStub = MayNeedFarStub;
|
2008-01-04 11:46:51 +01:00
|
|
|
Result.GOTRelative = GOTrelative;
|
2008-10-30 00:53:42 +01:00
|
|
|
Result.TargetResolve = false;
|
2008-01-04 11:46:51 +01:00
|
|
|
Result.Target.GV = GV;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2006-07-27 20:18:13 +02:00
|
|
|
/// MachineRelocation::getBB - Return a relocation entry for a BB.
|
|
|
|
///
|
2008-12-10 03:32:19 +01:00
|
|
|
static MachineRelocation getBB(uintptr_t offset,unsigned RelocationType,
|
2006-07-27 20:18:13 +02:00
|
|
|
MachineBasicBlock *MBB, intptr_t cst = 0) {
|
|
|
|
assert((RelocationType & ~63) == 0 && "Relocation type too large!");
|
|
|
|
MachineRelocation Result;
|
|
|
|
Result.Offset = offset;
|
|
|
|
Result.ConstantVal = cst;
|
|
|
|
Result.TargetReloType = RelocationType;
|
|
|
|
Result.AddrType = isBB;
|
2009-11-07 09:51:52 +01:00
|
|
|
Result.MayNeedFarStub = false;
|
2006-07-27 20:18:13 +02:00
|
|
|
Result.GOTRelative = false;
|
2008-10-30 00:53:42 +01:00
|
|
|
Result.TargetResolve = false;
|
2006-07-27 20:18:13 +02:00
|
|
|
Result.Target.MBB = MBB;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2006-05-03 22:30:20 +02:00
|
|
|
/// MachineRelocation::getExtSym - Return a relocation entry for an external
|
|
|
|
/// symbol, like "free".
|
|
|
|
///
|
2008-12-10 03:32:19 +01:00
|
|
|
static MachineRelocation getExtSym(uintptr_t offset, unsigned RelocationType,
|
2008-05-31 00:47:19 +02:00
|
|
|
const char *ES, intptr_t cst = 0,
|
2010-02-04 20:56:59 +01:00
|
|
|
bool GOTrelative = 0,
|
|
|
|
bool NeedStub = true) {
|
2004-11-21 04:27:13 +01:00
|
|
|
assert((RelocationType & ~63) == 0 && "Relocation type too large!");
|
2006-05-03 22:30:20 +02:00
|
|
|
MachineRelocation Result;
|
|
|
|
Result.Offset = offset;
|
|
|
|
Result.ConstantVal = cst;
|
|
|
|
Result.TargetReloType = RelocationType;
|
|
|
|
Result.AddrType = isExtSym;
|
2010-02-04 20:56:59 +01:00
|
|
|
Result.MayNeedFarStub = NeedStub;
|
2006-05-03 22:30:20 +02:00
|
|
|
Result.GOTRelative = GOTrelative;
|
2008-10-30 00:53:42 +01:00
|
|
|
Result.TargetResolve = false;
|
2008-05-31 00:47:19 +02:00
|
|
|
Result.Target.ExtSym = ES;
|
2006-05-03 22:30:20 +02:00
|
|
|
return Result;
|
2004-11-20 04:05:50 +01:00
|
|
|
}
|
|
|
|
|
2006-05-03 22:30:20 +02:00
|
|
|
/// MachineRelocation::getConstPool - Return a relocation entry for a constant
|
|
|
|
/// pool entry.
|
|
|
|
///
|
2008-12-10 03:32:19 +01:00
|
|
|
static MachineRelocation getConstPool(uintptr_t offset,unsigned RelocationType,
|
2008-10-30 00:53:42 +01:00
|
|
|
unsigned CPI, intptr_t cst = 0,
|
|
|
|
bool letTargetResolve = false) {
|
2005-07-22 22:46:42 +02:00
|
|
|
assert((RelocationType & ~63) == 0 && "Relocation type too large!");
|
2006-05-03 22:30:20 +02:00
|
|
|
MachineRelocation Result;
|
|
|
|
Result.Offset = offset;
|
|
|
|
Result.ConstantVal = cst;
|
|
|
|
Result.TargetReloType = RelocationType;
|
|
|
|
Result.AddrType = isConstPool;
|
2009-11-07 09:51:52 +01:00
|
|
|
Result.MayNeedFarStub = false;
|
2006-05-03 22:30:20 +02:00
|
|
|
Result.GOTRelative = false;
|
2008-10-30 00:53:42 +01:00
|
|
|
Result.TargetResolve = letTargetResolve;
|
2006-06-23 03:02:37 +02:00
|
|
|
Result.Target.Index = CPI;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// MachineRelocation::getJumpTable - Return a relocation entry for a jump
|
|
|
|
/// table entry.
|
|
|
|
///
|
2008-12-10 03:32:19 +01:00
|
|
|
static MachineRelocation getJumpTable(uintptr_t offset,unsigned RelocationType,
|
2008-11-07 10:01:15 +01:00
|
|
|
unsigned JTI, intptr_t cst = 0,
|
|
|
|
bool letTargetResolve = false) {
|
2006-06-23 03:02:37 +02:00
|
|
|
assert((RelocationType & ~63) == 0 && "Relocation type too large!");
|
|
|
|
MachineRelocation Result;
|
|
|
|
Result.Offset = offset;
|
|
|
|
Result.ConstantVal = cst;
|
|
|
|
Result.TargetReloType = RelocationType;
|
|
|
|
Result.AddrType = isJumpTable;
|
2009-11-07 09:51:52 +01:00
|
|
|
Result.MayNeedFarStub = false;
|
2006-06-23 03:02:37 +02:00
|
|
|
Result.GOTRelative = false;
|
2008-11-07 10:01:15 +01:00
|
|
|
Result.TargetResolve = letTargetResolve;
|
2006-06-23 03:02:37 +02:00
|
|
|
Result.Target.Index = JTI;
|
2006-05-03 22:30:20 +02:00
|
|
|
return Result;
|
2005-07-22 22:46:42 +02:00
|
|
|
}
|
|
|
|
|
2004-11-20 04:05:50 +01:00
|
|
|
/// getMachineCodeOffset - Return the offset into the code buffer that the
|
|
|
|
/// relocation should be performed.
|
2006-05-03 22:30:20 +02:00
|
|
|
intptr_t getMachineCodeOffset() const {
|
2006-05-03 20:52:31 +02:00
|
|
|
return Offset;
|
2004-11-20 04:05:50 +01:00
|
|
|
}
|
|
|
|
|
2004-11-20 04:43:50 +01:00
|
|
|
/// getRelocationType - Return the target-specific relocation ID for this
|
|
|
|
/// relocation.
|
|
|
|
unsigned getRelocationType() const {
|
2006-05-03 20:52:31 +02:00
|
|
|
return TargetReloType;
|
2004-11-20 04:43:50 +01:00
|
|
|
}
|
|
|
|
|
2004-11-20 04:05:50 +01:00
|
|
|
/// getConstantVal - Get the constant value associated with this relocation.
|
|
|
|
/// This is often an offset from the symbol.
|
|
|
|
///
|
|
|
|
intptr_t getConstantVal() const {
|
|
|
|
return ConstantVal;
|
|
|
|
}
|
|
|
|
|
2006-12-11 03:19:29 +01:00
|
|
|
/// setConstantVal - Set the constant value associated with this relocation.
|
|
|
|
/// This is often an offset from the symbol.
|
|
|
|
///
|
|
|
|
void setConstantVal(intptr_t val) {
|
|
|
|
ConstantVal = val;
|
|
|
|
}
|
|
|
|
|
2004-11-20 04:05:50 +01:00
|
|
|
/// isGlobalValue - Return true if this relocation is a GlobalValue, as
|
|
|
|
/// opposed to a constant string.
|
|
|
|
bool isGlobalValue() const {
|
2006-05-03 20:52:31 +02:00
|
|
|
return AddrType == isGV;
|
2004-11-20 04:05:50 +01:00
|
|
|
}
|
|
|
|
|
2008-11-10 02:08:07 +01:00
|
|
|
/// isIndirectSymbol - Return true if this relocation is the address an
|
|
|
|
/// indirect symbol
|
|
|
|
bool isIndirectSymbol() const {
|
|
|
|
return AddrType == isIndirectSym;
|
2008-01-04 11:46:51 +01:00
|
|
|
}
|
|
|
|
|
2006-07-27 20:18:13 +02:00
|
|
|
/// isBasicBlock - Return true if this relocation is a basic block reference.
|
|
|
|
///
|
|
|
|
bool isBasicBlock() const {
|
|
|
|
return AddrType == isBB;
|
|
|
|
}
|
|
|
|
|
2008-11-08 08:37:34 +01:00
|
|
|
/// isExternalSymbol - Return true if this is a constant string.
|
2004-11-20 04:05:50 +01:00
|
|
|
///
|
2008-11-08 08:37:34 +01:00
|
|
|
bool isExternalSymbol() const {
|
2006-05-03 20:52:31 +02:00
|
|
|
return AddrType == isExtSym;
|
2005-07-22 22:46:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isConstantPoolIndex - Return true if this is a constant pool reference.
|
|
|
|
///
|
|
|
|
bool isConstantPoolIndex() const {
|
2006-05-03 20:52:31 +02:00
|
|
|
return AddrType == isConstPool;
|
2005-07-22 22:46:42 +02:00
|
|
|
}
|
|
|
|
|
2006-06-23 03:02:37 +02:00
|
|
|
/// isJumpTableIndex - Return true if this is a jump table reference.
|
|
|
|
///
|
|
|
|
bool isJumpTableIndex() const {
|
|
|
|
return AddrType == isJumpTable;
|
|
|
|
}
|
|
|
|
|
2005-07-22 22:46:42 +02:00
|
|
|
/// isGOTRelative - Return true the target wants the index into the GOT of
|
|
|
|
/// the symbol rather than the address of the symbol.
|
|
|
|
bool isGOTRelative() const {
|
|
|
|
return GOTRelative;
|
2004-11-20 04:05:50 +01:00
|
|
|
}
|
|
|
|
|
2009-11-07 09:51:52 +01:00
|
|
|
/// mayNeedFarStub - This function returns true if the JIT for this target may
|
|
|
|
/// need either a stub function or an indirect global-variable load to handle
|
|
|
|
/// the relocated GlobalValue reference. For example, the x86-64 call
|
|
|
|
/// instruction can only call functions within +/-2GB of the call site.
|
|
|
|
/// Anything farther away needs a longer mov+call sequence, which can't just
|
|
|
|
/// be written on top of the existing call.
|
|
|
|
bool mayNeedFarStub() const {
|
|
|
|
return MayNeedFarStub;
|
2004-11-21 04:27:13 +01:00
|
|
|
}
|
|
|
|
|
2008-10-30 00:53:42 +01:00
|
|
|
/// letTargetResolve - Return true if the target JITInfo is usually
|
|
|
|
/// responsible for resolving the address of this relocation.
|
|
|
|
bool letTargetResolve() const {
|
|
|
|
return TargetResolve;
|
|
|
|
}
|
|
|
|
|
2004-11-20 04:05:50 +01:00
|
|
|
/// getGlobalValue - If this is a global value reference, return the
|
|
|
|
/// referenced global.
|
|
|
|
GlobalValue *getGlobalValue() const {
|
2008-11-10 02:08:07 +01:00
|
|
|
assert((isGlobalValue() || isIndirectSymbol()) &&
|
2008-01-04 11:46:51 +01:00
|
|
|
"This is not a global value reference!");
|
2004-11-20 04:05:50 +01:00
|
|
|
return Target.GV;
|
|
|
|
}
|
|
|
|
|
2006-07-27 20:18:13 +02:00
|
|
|
MachineBasicBlock *getBasicBlock() const {
|
|
|
|
assert(isBasicBlock() && "This is not a basic block reference!");
|
|
|
|
return Target.MBB;
|
|
|
|
}
|
|
|
|
|
2004-11-20 04:05:50 +01:00
|
|
|
/// getString - If this is a string value, return the string reference.
|
|
|
|
///
|
2008-11-08 08:37:34 +01:00
|
|
|
const char *getExternalSymbol() const {
|
|
|
|
assert(isExternalSymbol() && "This is not an external symbol reference!");
|
2004-11-20 04:05:50 +01:00
|
|
|
return Target.ExtSym;
|
|
|
|
}
|
|
|
|
|
2005-07-22 22:46:42 +02:00
|
|
|
/// getConstantPoolIndex - If this is a const pool reference, return
|
|
|
|
/// the index into the constant pool.
|
|
|
|
unsigned getConstantPoolIndex() const {
|
|
|
|
assert(isConstantPoolIndex() && "This is not a constant pool reference!");
|
2006-06-23 03:02:37 +02:00
|
|
|
return Target.Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getJumpTableIndex - If this is a jump table reference, return
|
|
|
|
/// the index into the jump table.
|
|
|
|
unsigned getJumpTableIndex() const {
|
|
|
|
assert(isJumpTableIndex() && "This is not a jump table reference!");
|
|
|
|
return Target.Index;
|
2005-07-22 22:46:42 +02:00
|
|
|
}
|
|
|
|
|
2004-11-20 04:05:50 +01:00
|
|
|
/// getResultPointer - Once this has been resolved to point to an actual
|
|
|
|
/// address, this returns the pointer.
|
|
|
|
void *getResultPointer() const {
|
2006-05-03 20:52:31 +02:00
|
|
|
assert(AddrType == isResult && "Result pointer isn't set yet!");
|
2004-11-20 04:05:50 +01:00
|
|
|
return Target.Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// setResultPointer - Set the result to the specified pointer value.
|
|
|
|
///
|
|
|
|
void setResultPointer(void *Ptr) {
|
|
|
|
Target.Result = Ptr;
|
2006-05-03 20:52:31 +02:00
|
|
|
AddrType = isResult;
|
2004-11-20 04:05:50 +01:00
|
|
|
}
|
2005-07-22 22:46:42 +02:00
|
|
|
|
|
|
|
/// setGOTIndex - Set the GOT index to a specific value.
|
|
|
|
void setGOTIndex(unsigned idx) {
|
2006-05-03 20:52:31 +02:00
|
|
|
AddrType = isGOTIndex;
|
2005-07-22 22:46:42 +02:00
|
|
|
Target.GOTIndex = idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getGOTIndex - Once this has been resolved to an entry in the GOT,
|
2005-07-27 07:53:44 +02:00
|
|
|
/// this returns that index. The index is from the lowest address entry
|
2005-07-22 22:46:42 +02:00
|
|
|
/// in the GOT.
|
|
|
|
unsigned getGOTIndex() const {
|
2006-05-03 20:52:31 +02:00
|
|
|
assert(AddrType == isGOTIndex);
|
2005-07-22 22:46:42 +02:00
|
|
|
return Target.GOTIndex;
|
|
|
|
}
|
2004-11-20 04:05:50 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|