Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:13

0001 //===-- llvm/MC/MCFixup.h - Instruction Relocation and Patching -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_MC_MCFIXUP_H
0010 #define LLVM_MC_MCFIXUP_H
0011 
0012 #include "llvm/Support/DataTypes.h"
0013 #include "llvm/Support/ErrorHandling.h"
0014 #include "llvm/Support/SMLoc.h"
0015 #include <cassert>
0016 
0017 namespace llvm {
0018 class MCExpr;
0019 
0020 /// Extensible enumeration to represent the type of a fixup.
0021 enum MCFixupKind {
0022   FK_NONE = 0,    ///< A no-op fixup.
0023   FK_Data_1,      ///< A one-byte fixup.
0024   FK_Data_2,      ///< A two-byte fixup.
0025   FK_Data_4,      ///< A four-byte fixup.
0026   FK_Data_8,      ///< A eight-byte fixup.
0027   FK_Data_leb128, ///< A leb128 fixup.
0028   FK_PCRel_1,     ///< A one-byte pc relative fixup.
0029   FK_PCRel_2,     ///< A two-byte pc relative fixup.
0030   FK_PCRel_4,     ///< A four-byte pc relative fixup.
0031   FK_PCRel_8,     ///< A eight-byte pc relative fixup.
0032   FK_GPRel_1,     ///< A one-byte gp relative fixup.
0033   FK_GPRel_2,     ///< A two-byte gp relative fixup.
0034   FK_GPRel_4,     ///< A four-byte gp relative fixup.
0035   FK_GPRel_8,     ///< A eight-byte gp relative fixup.
0036   FK_DTPRel_4,    ///< A four-byte dtp relative fixup.
0037   FK_DTPRel_8,    ///< A eight-byte dtp relative fixup.
0038   FK_TPRel_4,     ///< A four-byte tp relative fixup.
0039   FK_TPRel_8,     ///< A eight-byte tp relative fixup.
0040   FK_SecRel_1,    ///< A one-byte section relative fixup.
0041   FK_SecRel_2,    ///< A two-byte section relative fixup.
0042   FK_SecRel_4,    ///< A four-byte section relative fixup.
0043   FK_SecRel_8,    ///< A eight-byte section relative fixup.
0044 
0045   FirstTargetFixupKind = 128,
0046 
0047   /// The range [FirstLiteralRelocationKind, MaxTargetFixupKind) is used for
0048   /// relocations coming from .reloc directive. Fixup kind
0049   /// FirstLiteralRelocationKind+V represents the relocation type with number V.
0050   FirstLiteralRelocationKind = 256,
0051 
0052   /// Set limit to accommodate the highest reloc type in use for all Targets,
0053   /// currently R_AARCH64_IRELATIVE at 1032, including room for expansion.
0054   MaxFixupKind = FirstLiteralRelocationKind + 1032 + 32,
0055 };
0056 
0057 /// Encode information on a single operation to perform on a byte
0058 /// sequence (e.g., an encoded instruction) which requires assemble- or run-
0059 /// time patching.
0060 ///
0061 /// Fixups are used any time the target instruction encoder needs to represent
0062 /// some value in an instruction which is not yet concrete. The encoder will
0063 /// encode the instruction assuming the value is 0, and emit a fixup which
0064 /// communicates to the assembler backend how it should rewrite the encoded
0065 /// value.
0066 ///
0067 /// During the process of relaxation, the assembler will apply fixups as
0068 /// symbolic values become concrete. When relaxation is complete, any remaining
0069 /// fixups become relocations in the object file (or errors, if the fixup cannot
0070 /// be encoded on the target).
0071 class MCFixup {
0072   /// The value to put into the fixup location. The exact interpretation of the
0073   /// expression is target dependent, usually it will be one of the operands to
0074   /// an instruction or an assembler directive.
0075   const MCExpr *Value = nullptr;
0076 
0077   /// The byte index of start of the relocation inside the MCFragment.
0078   uint32_t Offset = 0;
0079 
0080   /// The target dependent kind of fixup item this is. The kind is used to
0081   /// determine how the operand value should be encoded into the instruction.
0082   MCFixupKind Kind = FK_NONE;
0083 
0084   /// The source location which gave rise to the fixup, if any.
0085   SMLoc Loc;
0086 public:
0087   static MCFixup create(uint32_t Offset, const MCExpr *Value,
0088                         MCFixupKind Kind, SMLoc Loc = SMLoc()) {
0089     assert(Kind <= MaxFixupKind && "Kind out of range!");
0090     MCFixup FI;
0091     FI.Value = Value;
0092     FI.Offset = Offset;
0093     FI.Kind = Kind;
0094     FI.Loc = Loc;
0095     return FI;
0096   }
0097 
0098   MCFixupKind getKind() const { return Kind; }
0099 
0100   unsigned getTargetKind() const { return Kind; }
0101 
0102   uint32_t getOffset() const { return Offset; }
0103   void setOffset(uint32_t Value) { Offset = Value; }
0104 
0105   const MCExpr *getValue() const { return Value; }
0106 
0107   /// Return the generic fixup kind for a value with the given size. It
0108   /// is an error to pass an unsupported size.
0109   static MCFixupKind getKindForSize(unsigned Size, bool IsPCRel) {
0110     switch (Size) {
0111     default: llvm_unreachable("Invalid generic fixup size!");
0112     case 1:
0113       return IsPCRel ? FK_PCRel_1 : FK_Data_1;
0114     case 2:
0115       return IsPCRel ? FK_PCRel_2 : FK_Data_2;
0116     case 4:
0117       return IsPCRel ? FK_PCRel_4 : FK_Data_4;
0118     case 8:
0119       return IsPCRel ? FK_PCRel_8 : FK_Data_8;
0120     }
0121   }
0122 
0123   SMLoc getLoc() const { return Loc; }
0124 };
0125 
0126 } // End llvm namespace
0127 
0128 #endif