1 //===-- llvm/MC/MCFixup.h - Instruction Relocation and Patching -*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_MC_MCFIXUP_H 10 #define LLVM_MC_MCFIXUP_H 11 12 #include "llvm/MC/MCExpr.h" 13 #include "llvm/Support/DataTypes.h" 14 #include "llvm/Support/ErrorHandling.h" 15 #include "llvm/Support/SMLoc.h" 16 #include <cassert> 17 18 namespace llvm { 19 class MCExpr; 20 21 /// Extensible enumeration to represent the type of a fixup. 22 enum MCFixupKind { 23 FK_NONE = 0, ///< A no-op fixup. 24 FK_Data_1, ///< 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_Data_6b, ///< A six-bits fixup. 29 FK_PCRel_1, ///< A one-byte pc relative fixup. 30 FK_PCRel_2, ///< A two-byte pc relative fixup. 31 FK_PCRel_4, ///< A four-byte pc relative fixup. 32 FK_PCRel_8, ///< A eight-byte pc relative fixup. 33 FK_GPRel_1, ///< A one-byte gp relative fixup. 34 FK_GPRel_2, ///< A two-byte gp relative fixup. 35 FK_GPRel_4, ///< A four-byte gp relative fixup. 36 FK_GPRel_8, ///< A eight-byte gp relative fixup. 37 FK_DTPRel_4, ///< A four-byte dtp relative fixup. 38 FK_DTPRel_8, ///< A eight-byte dtp relative fixup. 39 FK_TPRel_4, ///< A four-byte tp relative fixup. 40 FK_TPRel_8, ///< A eight-byte tp relative fixup. 41 FK_SecRel_1, ///< A one-byte section relative fixup. 42 FK_SecRel_2, ///< A two-byte section relative fixup. 43 FK_SecRel_4, ///< A four-byte section relative fixup. 44 FK_SecRel_8, ///< A eight-byte section relative fixup. 45 FK_Data_Add_1, ///< A one-byte add fixup. 46 FK_Data_Add_2, ///< A two-byte add fixup. 47 FK_Data_Add_4, ///< A four-byte add fixup. 48 FK_Data_Add_8, ///< A eight-byte add fixup. 49 FK_Data_Add_6b, ///< A six-bits add fixup. 50 FK_Data_Sub_1, ///< A one-byte sub fixup. 51 FK_Data_Sub_2, ///< A two-byte sub fixup. 52 FK_Data_Sub_4, ///< A four-byte sub fixup. 53 FK_Data_Sub_8, ///< A eight-byte sub fixup. 54 FK_Data_Sub_6b, ///< A six-bits sub fixup. 55 56 FirstTargetFixupKind = 128, 57 58 /// The range [FirstLiteralRelocationKind, MaxTargetFixupKind) is used for 59 /// relocations coming from .reloc directive. Fixup kind 60 /// FirstLiteralRelocationKind+V represents the relocation type with number V. 61 FirstLiteralRelocationKind = 256, 62 63 /// Set limit to accommodate the highest reloc type in use for all Targets, 64 /// currently R_AARCH64_IRELATIVE at 1032, including room for expansion. 65 MaxFixupKind = FirstLiteralRelocationKind + 1032 + 32, 66 }; 67 68 /// Encode information on a single operation to perform on a byte 69 /// sequence (e.g., an encoded instruction) which requires assemble- or run- 70 /// time patching. 71 /// 72 /// Fixups are used any time the target instruction encoder needs to represent 73 /// some value in an instruction which is not yet concrete. The encoder will 74 /// encode the instruction assuming the value is 0, and emit a fixup which 75 /// communicates to the assembler backend how it should rewrite the encoded 76 /// value. 77 /// 78 /// During the process of relaxation, the assembler will apply fixups as 79 /// symbolic values become concrete. When relaxation is complete, any remaining 80 /// fixups become relocations in the object file (or errors, if the fixup cannot 81 /// be encoded on the target). 82 class MCFixup { 83 /// The value to put into the fixup location. The exact interpretation of the 84 /// expression is target dependent, usually it will be one of the operands to 85 /// an instruction or an assembler directive. 86 const MCExpr *Value = nullptr; 87 88 /// The byte index of start of the relocation inside the MCFragment. 89 uint32_t Offset = 0; 90 91 /// The target dependent kind of fixup item this is. The kind is used to 92 /// determine how the operand value should be encoded into the instruction. 93 MCFixupKind Kind = FK_NONE; 94 95 /// The source location which gave rise to the fixup, if any. 96 SMLoc Loc; 97 public: 98 static MCFixup create(uint32_t Offset, const MCExpr *Value, 99 MCFixupKind Kind, SMLoc Loc = SMLoc()) { 100 assert(Kind <= MaxFixupKind && "Kind out of range!"); 101 MCFixup FI; 102 FI.Value = Value; 103 FI.Offset = Offset; 104 FI.Kind = Kind; 105 FI.Loc = Loc; 106 return FI; 107 } 108 109 /// Return a fixup corresponding to the add half of a add/sub fixup pair for 110 /// the given Fixup. createAddFor(const MCFixup & Fixup)111 static MCFixup createAddFor(const MCFixup &Fixup) { 112 MCFixup FI; 113 FI.Value = Fixup.getValue(); 114 FI.Offset = Fixup.getOffset(); 115 FI.Kind = getAddKindForKind(Fixup.getKind()); 116 FI.Loc = Fixup.getLoc(); 117 return FI; 118 } 119 120 /// Return a fixup corresponding to the sub half of a add/sub fixup pair for 121 /// the given Fixup. createSubFor(const MCFixup & Fixup)122 static MCFixup createSubFor(const MCFixup &Fixup) { 123 MCFixup FI; 124 FI.Value = Fixup.getValue(); 125 FI.Offset = Fixup.getOffset(); 126 FI.Kind = getSubKindForKind(Fixup.getKind()); 127 FI.Loc = Fixup.getLoc(); 128 return FI; 129 } 130 getKind()131 MCFixupKind getKind() const { return Kind; } 132 getTargetKind()133 unsigned getTargetKind() const { return Kind; } 134 getOffset()135 uint32_t getOffset() const { return Offset; } setOffset(uint32_t Value)136 void setOffset(uint32_t Value) { Offset = Value; } 137 getValue()138 const MCExpr *getValue() const { return Value; } 139 140 /// Return the generic fixup kind for a value with the given size. It 141 /// is an error to pass an unsupported size. getKindForSize(unsigned Size,bool IsPCRel)142 static MCFixupKind getKindForSize(unsigned Size, bool IsPCRel) { 143 switch (Size) { 144 default: llvm_unreachable("Invalid generic fixup size!"); 145 case 1: 146 return IsPCRel ? FK_PCRel_1 : FK_Data_1; 147 case 2: 148 return IsPCRel ? FK_PCRel_2 : FK_Data_2; 149 case 4: 150 return IsPCRel ? FK_PCRel_4 : FK_Data_4; 151 case 8: 152 return IsPCRel ? FK_PCRel_8 : FK_Data_8; 153 } 154 } 155 156 /// Return the generic fixup kind for a value with the given size in bits. 157 /// It is an error to pass an unsupported size. getKindForSizeInBits(unsigned Size,bool IsPCRel)158 static MCFixupKind getKindForSizeInBits(unsigned Size, bool IsPCRel) { 159 switch (Size) { 160 default: 161 llvm_unreachable("Invalid generic fixup size!"); 162 case 6: 163 assert(!IsPCRel && "Invalid pc-relative fixup size!"); 164 return FK_Data_6b; 165 case 8: 166 return IsPCRel ? FK_PCRel_1 : FK_Data_1; 167 case 16: 168 return IsPCRel ? FK_PCRel_2 : FK_Data_2; 169 case 32: 170 return IsPCRel ? FK_PCRel_4 : FK_Data_4; 171 case 64: 172 return IsPCRel ? FK_PCRel_8 : FK_Data_8; 173 } 174 } 175 176 /// Return the generic fixup kind for an addition with a given size. It 177 /// is an error to pass an unsupported size. getAddKindForKind(MCFixupKind Kind)178 static MCFixupKind getAddKindForKind(MCFixupKind Kind) { 179 switch (Kind) { 180 default: llvm_unreachable("Unknown type to convert!"); 181 case FK_Data_1: return FK_Data_Add_1; 182 case FK_Data_2: return FK_Data_Add_2; 183 case FK_Data_4: return FK_Data_Add_4; 184 case FK_Data_8: return FK_Data_Add_8; 185 case FK_Data_6b: return FK_Data_Add_6b; 186 } 187 } 188 189 /// Return the generic fixup kind for an subtraction with a given size. It 190 /// is an error to pass an unsupported size. getSubKindForKind(MCFixupKind Kind)191 static MCFixupKind getSubKindForKind(MCFixupKind Kind) { 192 switch (Kind) { 193 default: llvm_unreachable("Unknown type to convert!"); 194 case FK_Data_1: return FK_Data_Sub_1; 195 case FK_Data_2: return FK_Data_Sub_2; 196 case FK_Data_4: return FK_Data_Sub_4; 197 case FK_Data_8: return FK_Data_Sub_8; 198 case FK_Data_6b: return FK_Data_Sub_6b; 199 } 200 } 201 getLoc()202 SMLoc getLoc() const { return Loc; } 203 }; 204 205 } // End llvm namespace 206 207 #endif 208