MCFixup.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/MC/MCFixup.h - Instruction Relocation and Patching -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MC_MCFIXUP_H
  14. #define LLVM_MC_MCFIXUP_H
  15. #include "llvm/Support/DataTypes.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. #include "llvm/Support/SMLoc.h"
  18. #include <cassert>
  19. namespace llvm {
  20. class MCExpr;
  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. FirstTargetFixupKind = 128,
  46. /// The range [FirstLiteralRelocationKind, MaxTargetFixupKind) is used for
  47. /// relocations coming from .reloc directive. Fixup kind
  48. /// FirstLiteralRelocationKind+V represents the relocation type with number V.
  49. FirstLiteralRelocationKind = 256,
  50. /// Set limit to accommodate the highest reloc type in use for all Targets,
  51. /// currently R_AARCH64_IRELATIVE at 1032, including room for expansion.
  52. MaxFixupKind = FirstLiteralRelocationKind + 1032 + 32,
  53. };
  54. /// Encode information on a single operation to perform on a byte
  55. /// sequence (e.g., an encoded instruction) which requires assemble- or run-
  56. /// time patching.
  57. ///
  58. /// Fixups are used any time the target instruction encoder needs to represent
  59. /// some value in an instruction which is not yet concrete. The encoder will
  60. /// encode the instruction assuming the value is 0, and emit a fixup which
  61. /// communicates to the assembler backend how it should rewrite the encoded
  62. /// value.
  63. ///
  64. /// During the process of relaxation, the assembler will apply fixups as
  65. /// symbolic values become concrete. When relaxation is complete, any remaining
  66. /// fixups become relocations in the object file (or errors, if the fixup cannot
  67. /// be encoded on the target).
  68. class MCFixup {
  69. /// The value to put into the fixup location. The exact interpretation of the
  70. /// expression is target dependent, usually it will be one of the operands to
  71. /// an instruction or an assembler directive.
  72. const MCExpr *Value = nullptr;
  73. /// The byte index of start of the relocation inside the MCFragment.
  74. uint32_t Offset = 0;
  75. /// The target dependent kind of fixup item this is. The kind is used to
  76. /// determine how the operand value should be encoded into the instruction.
  77. MCFixupKind Kind = FK_NONE;
  78. /// The source location which gave rise to the fixup, if any.
  79. SMLoc Loc;
  80. public:
  81. static MCFixup create(uint32_t Offset, const MCExpr *Value,
  82. MCFixupKind Kind, SMLoc Loc = SMLoc()) {
  83. assert(Kind <= MaxFixupKind && "Kind out of range!");
  84. MCFixup FI;
  85. FI.Value = Value;
  86. FI.Offset = Offset;
  87. FI.Kind = Kind;
  88. FI.Loc = Loc;
  89. return FI;
  90. }
  91. MCFixupKind getKind() const { return Kind; }
  92. unsigned getTargetKind() const { return Kind; }
  93. uint32_t getOffset() const { return Offset; }
  94. void setOffset(uint32_t Value) { Offset = Value; }
  95. const MCExpr *getValue() const { return Value; }
  96. /// Return the generic fixup kind for a value with the given size. It
  97. /// is an error to pass an unsupported size.
  98. static MCFixupKind getKindForSize(unsigned Size, bool IsPCRel) {
  99. switch (Size) {
  100. default: llvm_unreachable("Invalid generic fixup size!");
  101. case 1:
  102. return IsPCRel ? FK_PCRel_1 : FK_Data_1;
  103. case 2:
  104. return IsPCRel ? FK_PCRel_2 : FK_Data_2;
  105. case 4:
  106. return IsPCRel ? FK_PCRel_4 : FK_Data_4;
  107. case 8:
  108. return IsPCRel ? FK_PCRel_8 : FK_Data_8;
  109. }
  110. }
  111. /// Return the generic fixup kind for a value with the given size in bits.
  112. /// It is an error to pass an unsupported size.
  113. static MCFixupKind getKindForSizeInBits(unsigned Size, bool IsPCRel) {
  114. switch (Size) {
  115. default:
  116. llvm_unreachable("Invalid generic fixup size!");
  117. case 6:
  118. assert(!IsPCRel && "Invalid pc-relative fixup size!");
  119. return FK_Data_6b;
  120. case 8:
  121. return IsPCRel ? FK_PCRel_1 : FK_Data_1;
  122. case 16:
  123. return IsPCRel ? FK_PCRel_2 : FK_Data_2;
  124. case 32:
  125. return IsPCRel ? FK_PCRel_4 : FK_Data_4;
  126. case 64:
  127. return IsPCRel ? FK_PCRel_8 : FK_Data_8;
  128. }
  129. }
  130. SMLoc getLoc() const { return Loc; }
  131. };
  132. } // End llvm namespace
  133. #endif
  134. #ifdef __GNUC__
  135. #pragma GCC diagnostic pop
  136. #endif