1 //===-- llvm/MC/MCFixup.h - Instruction Relocation and Patching -*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLVM_MC_MCFIXUP_H 11 #define LLVM_MC_MCFIXUP_H 12 13 #include "llvm/MC/MCExpr.h" 14 #include "llvm/Support/DataTypes.h" 15 #include "llvm/Support/ErrorHandling.h" 16 #include "llvm/Support/SMLoc.h" 17 #include <cassert> 18 19 namespace llvm { 20 class MCExpr; 21 22 /// \brief Extensible enumeration to represent the type of a fixup. 23 enum MCFixupKind { 24 FK_Data_1 = 0, ///< A one-byte fixup. 25 FK_Data_2, ///< A two-byte fixup. 26 FK_Data_4, ///< A four-byte fixup. 27 FK_Data_8, ///< A eight-byte fixup. 28 FK_PCRel_1, ///< A one-byte pc relative fixup. 29 FK_PCRel_2, ///< A two-byte pc relative fixup. 30 FK_PCRel_4, ///< A four-byte pc relative fixup. 31 FK_PCRel_8, ///< A eight-byte pc relative fixup. 32 FK_GPRel_1, ///< A one-byte gp relative fixup. 33 FK_GPRel_2, ///< A two-byte gp relative fixup. 34 FK_GPRel_4, ///< A four-byte gp relative fixup. 35 FK_GPRel_8, ///< A eight-byte gp relative fixup. 36 FK_SecRel_1, ///< A one-byte section relative fixup. 37 FK_SecRel_2, ///< A two-byte section relative fixup. 38 FK_SecRel_4, ///< A four-byte section relative fixup. 39 FK_SecRel_8, ///< A eight-byte section relative fixup. 40 41 FirstTargetFixupKind = 128, 42 43 // Limit range of target fixups, in case we want to pack more efficiently 44 // later. 45 MaxTargetFixupKind = (1 << 8) 46 }; 47 48 /// \brief Encode information on a single operation to perform on a byte 49 /// sequence (e.g., an encoded instruction) which requires assemble- or run- 50 /// time patching. 51 /// 52 /// Fixups are used any time the target instruction encoder needs to represent 53 /// some value in an instruction which is not yet concrete. The encoder will 54 /// encode the instruction assuming the value is 0, and emit a fixup which 55 /// communicates to the assembler backend how it should rewrite the encoded 56 /// value. 57 /// 58 /// During the process of relaxation, the assembler will apply fixups as 59 /// symbolic values become concrete. When relaxation is complete, any remaining 60 /// fixups become relocations in the object file (or errors, if the fixup cannot 61 /// be encoded on the target). 62 class MCFixup { 63 /// The value to put into the fixup location. The exact interpretation of the 64 /// expression is target dependent, usually it will be one of the operands to 65 /// an instruction or an assembler directive. 66 const MCExpr *Value; 67 68 /// The byte index of start of the relocation inside the encoded instruction. 69 uint32_t Offset; 70 71 /// The target dependent kind of fixup item this is. The kind is used to 72 /// determine how the operand value should be encoded into the instruction. 73 unsigned Kind; 74 75 /// The source location which gave rise to the fixup, if any. 76 SMLoc Loc; 77 public: 78 static MCFixup create(uint32_t Offset, const MCExpr *Value, 79 MCFixupKind Kind, SMLoc Loc = SMLoc()) { 80 assert(unsigned(Kind) < MaxTargetFixupKind && "Kind out of range!"); 81 MCFixup FI; 82 FI.Value = Value; 83 FI.Offset = Offset; 84 FI.Kind = unsigned(Kind); 85 FI.Loc = Loc; 86 return FI; 87 } 88 getKind()89 MCFixupKind getKind() const { return MCFixupKind(Kind); } 90 getOffset()91 uint32_t getOffset() const { return Offset; } setOffset(uint32_t Value)92 void setOffset(uint32_t Value) { Offset = Value; } 93 getValue()94 const MCExpr *getValue() const { return Value; } 95 96 /// \brief Return the generic fixup kind for a value with the given size. It 97 /// is an error to pass an unsupported size. getKindForSize(unsigned Size,bool isPCRel)98 static MCFixupKind getKindForSize(unsigned Size, bool isPCRel) { 99 switch (Size) { 100 default: llvm_unreachable("Invalid generic fixup size!"); 101 case 1: return isPCRel ? FK_PCRel_1 : FK_Data_1; 102 case 2: return isPCRel ? FK_PCRel_2 : FK_Data_2; 103 case 4: return isPCRel ? FK_PCRel_4 : FK_Data_4; 104 case 8: return isPCRel ? FK_PCRel_8 : FK_Data_8; 105 } 106 } 107 getLoc()108 SMLoc getLoc() const { return Loc; } 109 }; 110 111 } // End llvm namespace 112 113 #endif 114