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