LLVM API Documentation

MCFixup.h
Go to the documentation of this file.
00001 //===-- llvm/MC/MCFixup.h - Instruction Relocation and Patching -*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_MC_MCFIXUP_H
00011 #define LLVM_MC_MCFIXUP_H
00012 
00013 #include "llvm/MC/MCExpr.h"
00014 #include "llvm/Support/DataTypes.h"
00015 #include "llvm/Support/ErrorHandling.h"
00016 #include "llvm/Support/SMLoc.h"
00017 #include <cassert>
00018 
00019 namespace llvm {
00020 class MCExpr;
00021 
00022 /// MCFixupKind - Extensible enumeration to represent the type of a fixup.
00023 enum MCFixupKind {
00024   FK_Data_1 = 0, ///< A one-byte fixup.
00025   FK_Data_2,     ///< A two-byte fixup.
00026   FK_Data_4,     ///< A four-byte fixup.
00027   FK_Data_8,     ///< A eight-byte fixup.
00028   FK_PCRel_1,    ///< A one-byte pc relative fixup.
00029   FK_PCRel_2,    ///< A two-byte pc relative fixup.
00030   FK_PCRel_4,    ///< A four-byte pc relative fixup.
00031   FK_PCRel_8,    ///< A eight-byte pc relative fixup.
00032   FK_GPRel_1,    ///< A one-byte gp relative fixup.
00033   FK_GPRel_2,    ///< A two-byte gp relative fixup.
00034   FK_GPRel_4,    ///< A four-byte gp relative fixup.
00035   FK_GPRel_8,    ///< A eight-byte gp relative fixup.
00036   FK_SecRel_1,   ///< A one-byte section relative fixup.
00037   FK_SecRel_2,   ///< A two-byte section relative fixup.
00038   FK_SecRel_4,   ///< A four-byte section relative fixup.
00039   FK_SecRel_8,   ///< A eight-byte section relative fixup.
00040 
00041   FirstTargetFixupKind = 128,
00042 
00043   // Limit range of target fixups, in case we want to pack more efficiently
00044   // later.
00045   MaxTargetFixupKind = (1 << 8)
00046 };
00047 
00048 /// MCFixup - Encode information on a single operation to perform on a byte
00049 /// sequence (e.g., an encoded instruction) which requires assemble- or run-
00050 /// time patching.
00051 ///
00052 /// Fixups are used any time the target instruction encoder needs to represent
00053 /// some value in an instruction which is not yet concrete. The encoder will
00054 /// encode the instruction assuming the value is 0, and emit a fixup which
00055 /// communicates to the assembler backend how it should rewrite the encoded
00056 /// value.
00057 ///
00058 /// During the process of relaxation, the assembler will apply fixups as
00059 /// symbolic values become concrete. When relaxation is complete, any remaining
00060 /// fixups become relocations in the object file (or errors, if the fixup cannot
00061 /// be encoded on the target).
00062 class MCFixup {
00063   /// The value to put into the fixup location. The exact interpretation of the
00064   /// expression is target dependent, usually it will be one of the operands to
00065   /// an instruction or an assembler directive.
00066   const MCExpr *Value;
00067 
00068   /// The byte index of start of the relocation inside the encoded instruction.
00069   uint32_t Offset;
00070 
00071   /// The target dependent kind of fixup item this is. The kind is used to
00072   /// determine how the operand value should be encoded into the instruction.
00073   unsigned Kind;
00074 
00075   /// The source location which gave rise to the fixup, if any.
00076   SMLoc Loc;
00077 public:
00078   static MCFixup Create(uint32_t Offset, const MCExpr *Value,
00079                         MCFixupKind Kind, SMLoc Loc = SMLoc()) {
00080     assert(unsigned(Kind) < MaxTargetFixupKind && "Kind out of range!");
00081     MCFixup FI;
00082     FI.Value = Value;
00083     FI.Offset = Offset;
00084     FI.Kind = unsigned(Kind);
00085     FI.Loc = Loc;
00086     return FI;
00087   }
00088 
00089   MCFixupKind getKind() const { return MCFixupKind(Kind); }
00090 
00091   uint32_t getOffset() const { return Offset; }
00092   void setOffset(uint32_t Value) { Offset = Value; }
00093 
00094   const MCExpr *getValue() const { return Value; }
00095 
00096   /// getKindForSize - Return the generic fixup kind for a value with the given
00097   /// size. It is an error to pass an unsupported size.
00098   static MCFixupKind getKindForSize(unsigned Size, bool isPCRel) {
00099     switch (Size) {
00100     default: llvm_unreachable("Invalid generic fixup size!");
00101     case 1: return isPCRel ? FK_PCRel_1 : FK_Data_1;
00102     case 2: return isPCRel ? FK_PCRel_2 : FK_Data_2;
00103     case 4: return isPCRel ? FK_PCRel_4 : FK_Data_4;
00104     case 8: return isPCRel ? FK_PCRel_8 : FK_Data_8;
00105     }
00106   }
00107 
00108   SMLoc getLoc() const { return Loc; }
00109 };
00110 
00111 } // End llvm namespace
00112 
00113 #endif