Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/MC/MCFixup.h
Line
Count
Source (jump to first uncovered line)
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
12
#include "llvm/MC/MCExpr.h"
13
#include "llvm/Support/DataTypes.h"
14
#include "llvm/Support/ErrorHandling.h"
15
#include "llvm/Support/SMLoc.h"
16
#include <cassert>
17
18
namespace llvm {
19
class MCExpr;
20
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
  FK_Data_Add_1,  ///< A one-byte add fixup.
46
  FK_Data_Add_2,  ///< A two-byte add fixup.
47
  FK_Data_Add_4,  ///< A four-byte add fixup.
48
  FK_Data_Add_8,  ///< A eight-byte add fixup.
49
  FK_Data_Add_6b, ///< A six-bits add fixup.
50
  FK_Data_Sub_1,  ///< A one-byte sub fixup.
51
  FK_Data_Sub_2,  ///< A two-byte sub fixup.
52
  FK_Data_Sub_4,  ///< A four-byte sub fixup.
53
  FK_Data_Sub_8,  ///< A eight-byte sub fixup.
54
  FK_Data_Sub_6b, ///< A six-bits sub fixup.
55
56
  FirstTargetFixupKind = 128,
57
58
  // Limit range of target fixups, in case we want to pack more efficiently
59
  // later.
60
  MaxTargetFixupKind = (1 << 8)
61
};
62
63
/// Encode information on a single operation to perform on a byte
64
/// sequence (e.g., an encoded instruction) which requires assemble- or run-
65
/// time patching.
66
///
67
/// Fixups are used any time the target instruction encoder needs to represent
68
/// some value in an instruction which is not yet concrete. The encoder will
69
/// encode the instruction assuming the value is 0, and emit a fixup which
70
/// communicates to the assembler backend how it should rewrite the encoded
71
/// value.
72
///
73
/// During the process of relaxation, the assembler will apply fixups as
74
/// symbolic values become concrete. When relaxation is complete, any remaining
75
/// fixups become relocations in the object file (or errors, if the fixup cannot
76
/// be encoded on the target).
77
class MCFixup {
78
  /// The value to put into the fixup location. The exact interpretation of the
79
  /// expression is target dependent, usually it will be one of the operands to
80
  /// an instruction or an assembler directive.
81
  const MCExpr *Value;
82
83
  /// The byte index of start of the relocation inside the MCFragment.
84
  uint32_t Offset;
85
86
  /// The target dependent kind of fixup item this is. The kind is used to
87
  /// determine how the operand value should be encoded into the instruction.
88
  unsigned Kind;
89
90
  /// The source location which gave rise to the fixup, if any.
91
  SMLoc Loc;
92
public:
93
  static MCFixup create(uint32_t Offset, const MCExpr *Value,
94
5.86M
                        MCFixupKind Kind, SMLoc Loc = SMLoc()) {
95
5.86M
    assert(unsigned(Kind) < MaxTargetFixupKind && "Kind out of range!");
96
5.86M
    MCFixup FI;
97
5.86M
    FI.Value = Value;
98
5.86M
    FI.Offset = Offset;
99
5.86M
    FI.Kind = unsigned(Kind);
100
5.86M
    FI.Loc = Loc;
101
5.86M
    return FI;
102
5.86M
  }
103
104
  /// Return a fixup corresponding to the add half of a add/sub fixup pair for
105
  /// the given Fixup.
106
105
  static MCFixup createAddFor(const MCFixup &Fixup) {
107
105
    MCFixup FI;
108
105
    FI.Value = Fixup.getValue();
109
105
    FI.Offset = Fixup.getOffset();
110
105
    FI.Kind = (unsigned)getAddKindForKind(Fixup.getKind());
111
105
    FI.Loc = Fixup.getLoc();
112
105
    return FI;
113
105
  }
114
115
  /// Return a fixup corresponding to the sub half of a add/sub fixup pair for
116
  /// the given Fixup.
117
105
  static MCFixup createSubFor(const MCFixup &Fixup) {
118
105
    MCFixup FI;
119
105
    FI.Value = Fixup.getValue();
120
105
    FI.Offset = Fixup.getOffset();
121
105
    FI.Kind = (unsigned)getSubKindForKind(Fixup.getKind());
122
105
    FI.Loc = Fixup.getLoc();
123
105
    return FI;
124
105
  }
125
126
37.9M
  MCFixupKind getKind() const { return MCFixupKind(Kind); }
127
128
21.1M
  uint32_t getOffset() const { return Offset; }
129
4.18M
  void setOffset(uint32_t Value) { Offset = Value; }
130
131
6.43M
  const MCExpr *getValue() const { return Value; }
132
133
  /// Return the generic fixup kind for a value with the given size. It
134
  /// is an error to pass an unsupported size.
135
2.00M
  static MCFixupKind getKindForSize(unsigned Size, bool IsPCRel) {
136
2.00M
    switch (Size) {
137
2.00M
    
default: 0
llvm_unreachable0
("Invalid generic fixup size!");
138
2.00M
    case 1:
139
405k
      return IsPCRel ? 
FK_PCRel_1185k
:
FK_Data_1219k
;
140
2.00M
    case 2:
141
15.3k
      return IsPCRel ? 
FK_PCRel_26
:
FK_Data_215.3k
;
142
2.00M
    case 4:
143
725k
      return IsPCRel ? 
FK_PCRel_4191k
:
FK_Data_4534k
;
144
2.00M
    case 8:
145
860k
      return IsPCRel ? 
FK_PCRel_80
: FK_Data_8;
146
2.00M
    }
147
2.00M
  }
148
149
  /// Return the generic fixup kind for a value with the given size in bits.
150
  /// It is an error to pass an unsupported size.
151
87
  static MCFixupKind getKindForSizeInBits(unsigned Size, bool IsPCRel) {
152
87
    switch (Size) {
153
87
    default:
154
0
      llvm_unreachable("Invalid generic fixup size!");
155
87
    case 6:
156
87
      assert(!IsPCRel && "Invalid pc-relative fixup size!");
157
87
      return FK_Data_6b;
158
87
    case 8:
159
0
      return IsPCRel ? FK_PCRel_1 : FK_Data_1;
160
87
    case 16:
161
0
      return IsPCRel ? FK_PCRel_2 : FK_Data_2;
162
87
    case 32:
163
0
      return IsPCRel ? FK_PCRel_4 : FK_Data_4;
164
87
    case 64:
165
0
      return IsPCRel ? FK_PCRel_8 : FK_Data_8;
166
87
    }
167
87
  }
168
169
  /// Return the generic fixup kind for an addition with a given size. It
170
  /// is an error to pass an unsupported size.
171
105
  static MCFixupKind getAddKindForKind(unsigned Kind) {
172
105
    switch (Kind) {
173
105
    
default: 0
llvm_unreachable0
("Unknown type to convert!");
174
105
    
case FK_Data_1: return FK_Data_Add_16
;
175
105
    
case FK_Data_2: return FK_Data_Add_218
;
176
105
    
case FK_Data_4: return FK_Data_Add_437
;
177
105
    
case FK_Data_8: return FK_Data_Add_815
;
178
105
    
case FK_Data_6b: return FK_Data_Add_6b29
;
179
105
    }
180
105
  }
181
182
  /// Return the generic fixup kind for an subtraction with a given size. It
183
  /// is an error to pass an unsupported size.
184
105
  static MCFixupKind getSubKindForKind(unsigned Kind) {
185
105
    switch (Kind) {
186
105
    
default: 0
llvm_unreachable0
("Unknown type to convert!");
187
105
    
case FK_Data_1: return FK_Data_Sub_16
;
188
105
    
case FK_Data_2: return FK_Data_Sub_218
;
189
105
    
case FK_Data_4: return FK_Data_Sub_437
;
190
105
    
case FK_Data_8: return FK_Data_Sub_815
;
191
105
    
case FK_Data_6b: return FK_Data_Sub_6b29
;
192
105
    }
193
105
  }
194
195
34.5k
  SMLoc getLoc() const { return Loc; }
196
};
197
198
} // End llvm namespace
199
200
#endif