MCFixup.h revision 82f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3df
173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar//===-- llvm/MC/MCFixup.h - Instruction Relocation and Patching -*- C++ -*-===// 273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar// 373c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar// The LLVM Compiler Infrastructure 473c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar// 573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar// This file is distributed under the University of Illinois Open Source 673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar// License. See LICENSE.TXT for details. 773c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar// 873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar//===----------------------------------------------------------------------===// 973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar 1073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar#ifndef LLVM_MC_MCFIXUP_H 1173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar#define LLVM_MC_MCFIXUP_H 1273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar 131f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/DataTypes.h" 1482f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3dfJim Grosbach#include "llvm/Support/SMLoc.h" 1573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar#include <cassert> 1673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar 1773c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbarnamespace llvm { 185d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbarclass MCExpr; 1973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar 2073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// MCFixupKind - Extensible enumeration to represent the type of a fixup. 2173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbarenum MCFixupKind { 2273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar FK_Data_1 = 0, ///< A one-byte fixup. 2373c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar FK_Data_2, ///< A two-byte fixup. 2473c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar FK_Data_4, ///< A four-byte fixup. 2573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar FK_Data_8, ///< A eight-byte fixup. 26e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola FK_PCRel_1, ///< A one-byte pc relative fixup. 27e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola FK_PCRel_2, ///< A two-byte pc relative fixup. 28e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola FK_PCRel_4, ///< A four-byte pc relative fixup. 293a83c40ab61d5ca624f2bbadd70237c6adbdb304Rafael Espindola FK_PCRel_8, ///< A eight-byte pc relative fixup. 3084bfc2f090639f933df06cc675c4385511516befAkira Hatanaka FK_GPRel_1, ///< A one-byte gp relative fixup. 3184bfc2f090639f933df06cc675c4385511516befAkira Hatanaka FK_GPRel_2, ///< A two-byte gp relative fixup. 3284bfc2f090639f933df06cc675c4385511516befAkira Hatanaka FK_GPRel_4, ///< A four-byte gp relative fixup. 3384bfc2f090639f933df06cc675c4385511516befAkira Hatanaka FK_GPRel_8, ///< A eight-byte gp relative fixup. 34ce618af3e880ae0ec0ddd81aca8ed5bbd4096ae4Rafael Espindola FK_SecRel_1, ///< A one-byte section relative fixup. 35ce618af3e880ae0ec0ddd81aca8ed5bbd4096ae4Rafael Espindola FK_SecRel_2, ///< A two-byte section relative fixup. 36ce618af3e880ae0ec0ddd81aca8ed5bbd4096ae4Rafael Espindola FK_SecRel_4, ///< A four-byte section relative fixup. 37ce618af3e880ae0ec0ddd81aca8ed5bbd4096ae4Rafael Espindola FK_SecRel_8, ///< A eight-byte section relative fixup. 3873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar 390dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar FirstTargetFixupKind = 128, 4073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar 410dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar // Limit range of target fixups, in case we want to pack more efficiently 420dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar // later. 430dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar MaxTargetFixupKind = (1 << 8) 4473c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar}; 4573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar 460dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar/// MCFixup - Encode information on a single operation to perform on a byte 4773c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// sequence (e.g., an encoded instruction) which requires assemble- or run- 4873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// time patching. 4973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// 5073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// Fixups are used any time the target instruction encoder needs to represent 5173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// some value in an instruction which is not yet concrete. The encoder will 5273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// encode the instruction assuming the value is 0, and emit a fixup which 5373c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// communicates to the assembler backend how it should rewrite the encoded 5473c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// value. 5573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// 5673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// During the process of relaxation, the assembler will apply fixups as 5773c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// symbolic values become concrete. When relaxation is complete, any remaining 5873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// fixups become relocations in the object file (or errors, if the fixup cannot 5973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar/// be encoded on the target). 6073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbarclass MCFixup { 615d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar /// The value to put into the fixup location. The exact interpretation of the 620dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar /// expression is target dependent, usually it will be one of the operands to 630dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar /// an instruction or an assembler directive. 645d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar const MCExpr *Value; 655d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar 6673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar /// The byte index of start of the relocation inside the encoded instruction. 670dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar uint32_t Offset; 6873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar 6973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar /// The target dependent kind of fixup item this is. The kind is used to 7073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar /// determine how the operand value should be encoded into the instruction. 710dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar unsigned Kind; 7273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar 7382f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3dfJim Grosbach /// The source location which gave rise to the fixup, if any. 7482f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3dfJim Grosbach SMLoc Loc; 7573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbarpublic: 760dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar static MCFixup Create(uint32_t Offset, const MCExpr *Value, 7782f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3dfJim Grosbach MCFixupKind Kind, SMLoc Loc = SMLoc()) { 780dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar assert(unsigned(Kind) < MaxTargetFixupKind && "Kind out of range!"); 7973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar MCFixup FI; 805d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar FI.Value = Value; 8173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar FI.Offset = Offset; 8273c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar FI.Kind = unsigned(Kind); 8382f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3dfJim Grosbach FI.Loc = Loc; 8473c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar return FI; 8573c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar } 8673c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar 875d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar MCFixupKind getKind() const { return MCFixupKind(Kind); } 8873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar 890dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar uint32_t getOffset() const { return Offset; } 900dd0c941c9eb4adc13319ed7adcaffe58a68d294Daniel Dunbar void setOffset(uint32_t Value) { Offset = Value; } 9173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar 925d5a1e13a129e18ee6031fe6354acd2ab4d39f37Daniel Dunbar const MCExpr *getValue() const { return Value; } 932be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar 942be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar /// getKindForSize - Return the generic fixup kind for a value with the given 952be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar /// size. It is an error to pass an unsupported size. 96e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola static MCFixupKind getKindForSize(unsigned Size, bool isPCRel) { 972be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar switch (Size) { 982be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar default: assert(0 && "Invalid generic fixup size!"); 99e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola case 1: return isPCRel ? FK_PCRel_1 : FK_Data_1; 100e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola case 2: return isPCRel ? FK_PCRel_2 : FK_Data_2; 101e04ed7e45f194f14a7b28bbf3f55694d8e2bcf80Rafael Espindola case 4: return isPCRel ? FK_PCRel_4 : FK_Data_4; 1023a83c40ab61d5ca624f2bbadd70237c6adbdb304Rafael Espindola case 8: return isPCRel ? FK_PCRel_8 : FK_Data_8; 1032be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar } 1042be2fd073003c0988723d2894dfb117ad90be11bDaniel Dunbar } 10582f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3dfJim Grosbach 10682f4ce5081fc9cfbf34bbe61eb0412e7ca4dc3dfJim Grosbach SMLoc getLoc() const { return Loc; } 10773c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar}; 10873c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar 10973c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar} // End llvm namespace 11073c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar 11173c557458c0e28899f37c557bcaf36c2b6701260Daniel Dunbar#endif 112