LLVM 20.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
14#include "llvm/Support/SMLoc.h"
15#include <cassert>
16
17namespace llvm {
18class 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_leb128, ///< A leb128 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
46
47 /// The range [FirstLiteralRelocationKind, MaxTargetFixupKind) is used for
48 /// relocations coming from .reloc directive. Fixup kind
49 /// FirstLiteralRelocationKind+V represents the relocation type with number V.
51
52 /// Set limit to accommodate the highest reloc type in use for all Targets,
53 /// currently R_AARCH64_IRELATIVE at 1032, including room for expansion.
55};
56
57/// Encode information on a single operation to perform on a byte
58/// sequence (e.g., an encoded instruction) which requires assemble- or run-
59/// time patching.
60///
61/// Fixups are used any time the target instruction encoder needs to represent
62/// some value in an instruction which is not yet concrete. The encoder will
63/// encode the instruction assuming the value is 0, and emit a fixup which
64/// communicates to the assembler backend how it should rewrite the encoded
65/// value.
66///
67/// During the process of relaxation, the assembler will apply fixups as
68/// symbolic values become concrete. When relaxation is complete, any remaining
69/// fixups become relocations in the object file (or errors, if the fixup cannot
70/// be encoded on the target).
71class MCFixup {
72 /// The value to put into the fixup location. The exact interpretation of the
73 /// expression is target dependent, usually it will be one of the operands to
74 /// an instruction or an assembler directive.
75 const MCExpr *Value = nullptr;
76
77 /// The byte index of start of the relocation inside the MCFragment.
78 uint32_t Offset = 0;
79
80 /// The target dependent kind of fixup item this is. The kind is used to
81 /// determine how the operand value should be encoded into the instruction.
82 MCFixupKind Kind = FK_NONE;
83
84 /// The source location which gave rise to the fixup, if any.
85 SMLoc Loc;
86public:
87 static MCFixup create(uint32_t Offset, const MCExpr *Value,
88 MCFixupKind Kind, SMLoc Loc = SMLoc()) {
89 assert(Kind <= MaxFixupKind && "Kind out of range!");
90 MCFixup FI;
91 FI.Value = Value;
92 FI.Offset = Offset;
93 FI.Kind = Kind;
94 FI.Loc = Loc;
95 return FI;
96 }
97
98 MCFixupKind getKind() const { return Kind; }
99
100 unsigned getTargetKind() const { return Kind; }
101
102 uint32_t getOffset() const { return Offset; }
103 void setOffset(uint32_t Value) { Offset = Value; }
104
105 const MCExpr *getValue() const { return Value; }
106
107 /// Return the generic fixup kind for a value with the given size. It
108 /// is an error to pass an unsupported size.
109 static MCFixupKind getKindForSize(unsigned Size, bool IsPCRel) {
110 switch (Size) {
111 default: llvm_unreachable("Invalid generic fixup size!");
112 case 1:
113 return IsPCRel ? FK_PCRel_1 : FK_Data_1;
114 case 2:
115 return IsPCRel ? FK_PCRel_2 : FK_Data_2;
116 case 4:
117 return IsPCRel ? FK_PCRel_4 : FK_Data_4;
118 case 8:
119 return IsPCRel ? FK_PCRel_8 : FK_Data_8;
120 }
121 }
122
123 SMLoc getLoc() const { return Loc; }
124};
125
126} // End llvm namespace
127
128#endif
uint64_t Size
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
static MCFixupKind getKindForSize(unsigned Size, bool IsPCRel)
Return the generic fixup kind for a value with the given size.
Definition: MCFixup.h:109
const MCExpr * getValue() const
Definition: MCFixup.h:105
uint32_t getOffset() const
Definition: MCFixup.h:102
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:87
unsigned getTargetKind() const
Definition: MCFixup.h:100
void setOffset(uint32_t Value)
Definition: MCFixup.h:103
SMLoc getLoc() const
Definition: MCFixup.h:123
MCFixupKind getKind() const
Definition: MCFixup.h:98
Represents a location in source code.
Definition: SMLoc.h:23
LLVM Value Representation.
Definition: Value.h:74
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
@ FirstTargetFixupKind
Definition: MCFixup.h:45
@ FK_PCRel_4
A four-byte pc relative fixup.
Definition: MCFixup.h:30
@ FK_PCRel_2
A two-byte pc relative fixup.
Definition: MCFixup.h:29
@ FK_SecRel_2
A two-byte section relative fixup.
Definition: MCFixup.h:41
@ FirstLiteralRelocationKind
The range [FirstLiteralRelocationKind, MaxTargetFixupKind) is used for relocations coming from ....
Definition: MCFixup.h:50
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:23
@ FK_GPRel_8
A eight-byte gp relative fixup.
Definition: MCFixup.h:35
@ MaxFixupKind
Set limit to accommodate the highest reloc type in use for all Targets, currently R_AARCH64_IRELATIVE...
Definition: MCFixup.h:54
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
@ FK_DTPRel_4
A four-byte dtp relative fixup.
Definition: MCFixup.h:36
@ FK_DTPRel_8
A eight-byte dtp relative fixup.
Definition: MCFixup.h:37
@ FK_SecRel_8
A eight-byte section relative fixup.
Definition: MCFixup.h:43
@ FK_PCRel_8
A eight-byte pc relative fixup.
Definition: MCFixup.h:31
@ FK_NONE
A no-op fixup.
Definition: MCFixup.h:22
@ FK_SecRel_4
A four-byte section relative fixup.
Definition: MCFixup.h:42
@ FK_GPRel_1
A one-byte gp relative fixup.
Definition: MCFixup.h:32
@ FK_TPRel_4
A four-byte tp relative fixup.
Definition: MCFixup.h:38
@ FK_GPRel_4
A four-byte gp relative fixup.
Definition: MCFixup.h:34
@ FK_PCRel_1
A one-byte pc relative fixup.
Definition: MCFixup.h:28
@ FK_TPRel_8
A eight-byte tp relative fixup.
Definition: MCFixup.h:39
@ FK_SecRel_1
A one-byte section relative fixup.
Definition: MCFixup.h:40
@ FK_Data_leb128
A leb128 fixup.
Definition: MCFixup.h:27
@ FK_GPRel_2
A two-byte gp relative fixup.
Definition: MCFixup.h:33
@ FK_Data_2
A two-byte fixup.
Definition: MCFixup.h:24