MCFixup.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //===-- llvm/MC/MCFixup.h - Instruction Relocation and Patching -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLVM_MC_MCFIXUP_H
  10. #define LLVM_MC_MCFIXUP_H
  11. #include "llvm/MC/MCExpr.h"
  12. #include "llvm/Support/DataTypes.h"
  13. #include "llvm/Support/ErrorHandling.h"
  14. #include "llvm/Support/SMLoc.h"
  15. #include <cassert>
  16. namespace llvm {
  17. class MCExpr;
  18. /// \brief Extensible enumeration to represent the type of a fixup.
  19. enum MCFixupKind {
  20. FK_Data_1 = 0, ///< A one-byte fixup.
  21. FK_Data_2, ///< A two-byte fixup.
  22. FK_Data_4, ///< A four-byte fixup.
  23. FK_Data_8, ///< A eight-byte fixup.
  24. FK_PCRel_1, ///< A one-byte pc relative fixup.
  25. FK_PCRel_2, ///< A two-byte pc relative fixup.
  26. FK_PCRel_4, ///< A four-byte pc relative fixup.
  27. FK_PCRel_8, ///< A eight-byte pc relative fixup.
  28. FK_GPRel_1, ///< A one-byte gp relative fixup.
  29. FK_GPRel_2, ///< A two-byte gp relative fixup.
  30. FK_GPRel_4, ///< A four-byte gp relative fixup.
  31. FK_GPRel_8, ///< A eight-byte gp relative fixup.
  32. FK_SecRel_1, ///< A one-byte section relative fixup.
  33. FK_SecRel_2, ///< A two-byte section relative fixup.
  34. FK_SecRel_4, ///< A four-byte section relative fixup.
  35. FK_SecRel_8, ///< A eight-byte section relative fixup.
  36. FirstTargetFixupKind = 128,
  37. // Limit range of target fixups, in case we want to pack more efficiently
  38. // later.
  39. MaxTargetFixupKind = (1 << 8)
  40. };
  41. /// \brief Encode information on a single operation to perform on a byte
  42. /// sequence (e.g., an encoded instruction) which requires assemble- or run-
  43. /// time patching.
  44. ///
  45. /// Fixups are used any time the target instruction encoder needs to represent
  46. /// some value in an instruction which is not yet concrete. The encoder will
  47. /// encode the instruction assuming the value is 0, and emit a fixup which
  48. /// communicates to the assembler backend how it should rewrite the encoded
  49. /// value.
  50. ///
  51. /// During the process of relaxation, the assembler will apply fixups as
  52. /// symbolic values become concrete. When relaxation is complete, any remaining
  53. /// fixups become relocations in the object file (or errors, if the fixup cannot
  54. /// be encoded on the target).
  55. class MCFixup {
  56. /// The value to put into the fixup location. The exact interpretation of the
  57. /// expression is target dependent, usually it will be one of the operands to
  58. /// an instruction or an assembler directive.
  59. const MCExpr *Value;
  60. /// The byte index of start of the relocation inside the encoded instruction.
  61. uint32_t Offset;
  62. /// The target dependent kind of fixup item this is. The kind is used to
  63. /// determine how the operand value should be encoded into the instruction.
  64. unsigned Kind;
  65. /// The source location which gave rise to the fixup, if any.
  66. SMLoc Loc;
  67. public:
  68. static MCFixup create(uint32_t Offset, const MCExpr *Value,
  69. MCFixupKind Kind, SMLoc Loc = SMLoc()) {
  70. assert(unsigned(Kind) < MaxTargetFixupKind && "Kind out of range!");
  71. MCFixup FI;
  72. FI.Value = Value;
  73. FI.Offset = Offset;
  74. FI.Kind = unsigned(Kind);
  75. FI.Loc = Loc;
  76. return FI;
  77. }
  78. MCFixupKind getKind() const { return MCFixupKind(Kind); }
  79. uint32_t getOffset() const { return Offset; }
  80. void setOffset(uint32_t Value) { Offset = Value; }
  81. const MCExpr *getValue() const { return Value; }
  82. /// \brief Return the generic fixup kind for a value with the given size. It
  83. /// is an error to pass an unsupported size.
  84. static MCFixupKind getKindForSize(unsigned Size, bool isPCRel) {
  85. switch (Size) {
  86. default: llvm_unreachable("Invalid generic fixup size!");
  87. case 1: return isPCRel ? FK_PCRel_1 : FK_Data_1;
  88. case 2: return isPCRel ? FK_PCRel_2 : FK_Data_2;
  89. case 4: return isPCRel ? FK_PCRel_4 : FK_Data_4;
  90. case 8: return isPCRel ? FK_PCRel_8 : FK_Data_8;
  91. }
  92. }
  93. SMLoc getLoc() const { return Loc; }
  94. };
  95. } // End llvm namespace
  96. #endif