LLVM 24.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
15#include "llvm/Support/SMLoc.h"
16#include <cassert>
17
18namespace llvm {
19class MCExpr;
20
21/// Extensible enumeration to represent the type of a fixup.
23
24// [0, FirstLiteralRelocationKind) encodes raw relocation types.
25//
26// [FirstLiteralRelocationKind, FK_NONE) encodes raw relocation types coming
27// from .reloc directives. Fixup kind FirstLiteralRelocationKind+t encodes
28// relocation type t.
29//
30// The range boundaries are not enumerators, so that arithmetic with ELF::R_*
31// or target fixup enumerators does not mix enumeration types.
33
34enum {
35 // Other kinds indicate the fixup may resolve to a constant, allowing the
36 // assembler to update the instruction or data directly without a relocation.
37 FK_NONE = 4000, ///< A no-op fixup.
38 FK_Data_1, ///< A one-byte fixup.
39 FK_Data_2, ///< A two-byte fixup.
40 FK_Data_4, ///< A four-byte fixup.
41 FK_Data_8, ///< A eight-byte fixup.
42 FK_Data_leb128, ///< A leb128 fixup.
43 FK_SecRel_1, ///< A one-byte section relative fixup.
44 FK_SecRel_2, ///< A two-byte section relative fixup.
45 FK_SecRel_4, ///< A four-byte section relative fixup.
46 FK_SecRel_8, ///< A eight-byte section relative fixup.
47};
48
50
51/// Encode information on a single operation to perform on a byte
52/// sequence (e.g., an encoded instruction) which requires assemble- or run-
53/// time patching.
54///
55/// Fixups are used any time the target instruction encoder needs to represent
56/// some value in an instruction which is not yet concrete. The encoder will
57/// encode the instruction assuming the value is 0, and emit a fixup which
58/// communicates to the assembler backend how it should rewrite the encoded
59/// value.
60///
61/// During the process of relaxation, the assembler will apply fixups as
62/// symbolic values become concrete. When relaxation is complete, any remaining
63/// fixups become relocations in the object file (or errors, if the fixup cannot
64/// be encoded on the target).
65class MCFixup {
66 /// The value to put into the fixup location. The exact interpretation of the
67 /// expression is target dependent, usually it will be one of the operands to
68 /// an instruction or an assembler directive.
69 const MCExpr *Value = nullptr;
70
71 /// The byte index of start of the relocation inside the MCFragment.
72 uint32_t Offset = 0;
73
74 /// The target dependent kind of fixup item this is. The kind is used to
75 /// determine how the operand value should be encoded into the instruction.
76 MCFixupKind Kind = FK_NONE;
77
78 /// True if this is a PC-relative fixup. The relocatable expression is
79 /// typically resolved When SymB is nullptr and SymA is a local symbol defined
80 /// within the current section.
81 bool PCRel = false;
82
83 /// Used by RISC-V style linker relaxation. Whether the fixup is
84 /// linker-relaxable.
85 bool LinkerRelaxable = false;
86
87 /// Consider bit fields if we need more flags.
88
89public:
90 static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind,
91 bool PCRel = false) {
92 MCFixup FI;
93 FI.Value = Value;
94 FI.Offset = Offset;
95 FI.Kind = Kind;
96 FI.PCRel = PCRel;
97 return FI;
98 }
99
100 MCFixupKind getKind() 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 bool isPCRel() const { return PCRel; }
108 void setPCRel() { PCRel = true; }
109 bool isLinkerRelaxable() const { return LinkerRelaxable; }
110 void setLinkerRelaxable() { LinkerRelaxable = true; }
111
112 /// Return the generic fixup kind for a value with the given size. It
113 /// is an error to pass an unsupported size.
115 switch (Size) {
116 default: llvm_unreachable("Invalid generic fixup size!");
117 case 1:
118 return FK_Data_1;
119 case 2:
120 return FK_Data_2;
121 case 4:
122 return FK_Data_4;
123 case 8:
124 return FK_Data_8;
125 }
126 }
127
128 LLVM_ABI SMLoc getLoc() const;
129};
130
131namespace mc {
132// Check if the fixup kind is a relocation type. Return false if the fixup can
133// be resolved without a relocation.
135
136// Check if the fixup kind represents a relocation type from a .reloc directive.
137// In ELF, this skips STT_SECTION adjustment and STT_TLS symbol type setting for
138// TLS relocations.
142} // namespace mc
143
144} // End llvm namespace
145
146#endif
#define LLVM_ABI
Definition Compiler.h:215
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:65
static MCFixupKind getDataKindForSize(unsigned Size)
Return the generic fixup kind for a value with the given size.
Definition MCFixup.h:114
void setPCRel()
Definition MCFixup.h:108
const MCExpr * getValue() const
Definition MCFixup.h:105
void setLinkerRelaxable()
Definition MCFixup.h:110
LLVM_ABI SMLoc getLoc() const
uint32_t getOffset() const
Definition MCFixup.h:102
bool isPCRel() const
Definition MCFixup.h:107
bool isLinkerRelaxable() const
Definition MCFixup.h:109
void setOffset(uint32_t Value)
Definition MCFixup.h:103
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
Definition MCFixup.h:90
MCFixupKind getKind() const
Definition MCFixup.h:100
Represents a location in source code.
Definition SMLoc.h:22
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isRelocRelocation(MCFixupKind FixupKind)
Definition MCFixup.h:139
bool isRelocation(MCFixupKind FixupKind)
Definition MCFixup.h:134
This is an optimization pass for GlobalISel generic memory operations.
constexpr MCFixupKind FirstTargetFixupKind
Definition MCFixup.h:49
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
static Lanai::Fixups FixupKind(const MCExpr *Expr)
@ FK_SecRel_2
A two-byte section relative fixup.
Definition MCFixup.h:44
@ FK_Data_8
A eight-byte fixup.
Definition MCFixup.h:41
@ FK_Data_1
A one-byte fixup.
Definition MCFixup.h:38
@ FK_Data_4
A four-byte fixup.
Definition MCFixup.h:40
@ FK_SecRel_8
A eight-byte section relative fixup.
Definition MCFixup.h:46
@ FK_NONE
A no-op fixup.
Definition MCFixup.h:37
@ FK_SecRel_4
A four-byte section relative fixup.
Definition MCFixup.h:45
@ FK_SecRel_1
A one-byte section relative fixup.
Definition MCFixup.h:43
@ FK_Data_leb128
A leb128 fixup.
Definition MCFixup.h:42
@ FK_Data_2
A two-byte fixup.
Definition MCFixup.h:39
constexpr MCFixupKind FirstLiteralRelocationKind
Definition MCFixup.h:32