Coverage Report

Created: 2019-02-20 07:29

/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_Data_1 = 0, ///< A one-byte fixup.
24
  FK_Data_2,     ///< A two-byte fixup.
25
  FK_Data_4,     ///< A four-byte fixup.
26
  FK_Data_8,     ///< A eight-byte fixup.
27
  FK_PCRel_1,    ///< A one-byte pc relative fixup.
28
  FK_PCRel_2,    ///< A two-byte pc relative fixup.
29
  FK_PCRel_4,    ///< A four-byte pc relative fixup.
30
  FK_PCRel_8,    ///< A eight-byte pc relative fixup.
31
  FK_GPRel_1,    ///< A one-byte gp relative fixup.
32
  FK_GPRel_2,    ///< A two-byte gp relative fixup.
33
  FK_GPRel_4,    ///< A four-byte gp relative fixup.
34
  FK_GPRel_8,    ///< A eight-byte gp relative fixup.
35
  FK_DTPRel_4,   ///< A four-byte dtp relative fixup.
36
  FK_DTPRel_8,   ///< A eight-byte dtp relative fixup.
37
  FK_TPRel_4,    ///< A four-byte tp relative fixup.
38
  FK_TPRel_8,    ///< A eight-byte tp relative fixup.
39
  FK_SecRel_1,   ///< A one-byte section relative fixup.
40
  FK_SecRel_2,   ///< A two-byte section relative fixup.
41
  FK_SecRel_4,   ///< A four-byte section relative fixup.
42
  FK_SecRel_8,   ///< A eight-byte section relative fixup.
43
  FK_Data_Add_1, ///< A one-byte add fixup.
44
  FK_Data_Add_2, ///< A two-byte add fixup.
45
  FK_Data_Add_4, ///< A four-byte add fixup.
46
  FK_Data_Add_8, ///< A eight-byte add fixup.
47
  FK_Data_Sub_1, ///< A one-byte sub fixup.
48
  FK_Data_Sub_2, ///< A two-byte sub fixup.
49
  FK_Data_Sub_4, ///< A four-byte sub fixup.
50
  FK_Data_Sub_8, ///< A eight-byte sub fixup.
51
52
  FirstTargetFixupKind = 128,
53
54
  // Limit range of target fixups, in case we want to pack more efficiently
55
  // later.
56
  MaxTargetFixupKind = (1 << 8)
57
};
58
59
/// Encode information on a single operation to perform on a byte
60
/// sequence (e.g., an encoded instruction) which requires assemble- or run-
61
/// time patching.
62
///
63
/// Fixups are used any time the target instruction encoder needs to represent
64
/// some value in an instruction which is not yet concrete. The encoder will
65
/// encode the instruction assuming the value is 0, and emit a fixup which
66
/// communicates to the assembler backend how it should rewrite the encoded
67
/// value.
68
///
69
/// During the process of relaxation, the assembler will apply fixups as
70
/// symbolic values become concrete. When relaxation is complete, any remaining
71
/// fixups become relocations in the object file (or errors, if the fixup cannot
72
/// be encoded on the target).
73
class MCFixup {
74
  /// The value to put into the fixup location. The exact interpretation of the
75
  /// expression is target dependent, usually it will be one of the operands to
76
  /// an instruction or an assembler directive.
77
  const MCExpr *Value;
78
79
  /// The byte index of start of the relocation inside the MCFragment.
80
  uint32_t Offset;
81
82
  /// The target dependent kind of fixup item this is. The kind is used to
83
  /// determine how the operand value should be encoded into the instruction.
84
  unsigned Kind;
85
86
  /// The source location which gave rise to the fixup, if any.
87
  SMLoc Loc;
88
public:
89
  static MCFixup create(uint32_t Offset, const MCExpr *Value,
90
6.00M
                        MCFixupKind Kind, SMLoc Loc = SMLoc()) {
91
6.00M
    assert(unsigned(Kind) < MaxTargetFixupKind && "Kind out of range!");
92
6.00M
    MCFixup FI;
93
6.00M
    FI.Value = Value;
94
6.00M
    FI.Offset = Offset;
95
6.00M
    FI.Kind = unsigned(Kind);
96
6.00M
    FI.Loc = Loc;
97
6.00M
    return FI;
98
6.00M
  }
99
100
  /// Return a fixup corresponding to the add half of a add/sub fixup pair for
101
  /// the given Fixup.
102
0
  static MCFixup createAddFor(const MCFixup &Fixup) {
103
0
    MCFixup FI;
104
0
    FI.Value = Fixup.getValue();
105
0
    FI.Offset = Fixup.getOffset();
106
0
    FI.Kind = (unsigned)getAddKindForKind(Fixup.getKind());
107
0
    FI.Loc = Fixup.getLoc();
108
0
    return FI;
109
0
  }
110
111
  /// Return a fixup corresponding to the sub half of a add/sub fixup pair for
112
  /// the given Fixup.
113
0
  static MCFixup createSubFor(const MCFixup &Fixup) {
114
0
    MCFixup FI;
115
0
    FI.Value = Fixup.getValue();
116
0
    FI.Offset = Fixup.getOffset();
117
0
    FI.Kind = (unsigned)getSubKindForKind(Fixup.getKind());
118
0
    FI.Loc = Fixup.getLoc();
119
0
    return FI;
120
0
  }
121
122
39.7M
  MCFixupKind getKind() const { return MCFixupKind(Kind); }
123
124
21.6M
  uint32_t getOffset() const { return Offset; }
125
4.26M
  void setOffset(uint32_t Value) { Offset = Value; }
126
127
6.61M
  const MCExpr *getValue() const { return Value; }
128
129
  /// Return the generic fixup kind for a value with the given size. It
130
  /// is an error to pass an unsupported size.
131
2.10M
  static MCFixupKind getKindForSize(unsigned Size, bool isPCRel) {
132
2.10M
    switch (Size) {
133
2.10M
    
default: 0
llvm_unreachable0
("Invalid generic fixup size!");
134
2.10M
    
case 1: return isPCRel 417k
?
FK_PCRel_1191k
:
FK_Data_1226k
;
135
2.10M
    
case 2: return isPCRel 8.36k
?
FK_PCRel_26
:
FK_Data_28.35k
;
136
2.10M
    
case 4: return isPCRel 806k
?
FK_PCRel_4204k
:
FK_Data_4602k
;
137
2.10M
    
case 8: return isPCRel 868k
?
FK_PCRel_80
:
FK_Data_8868k
;
138
2.10M
    }
139
2.10M
  }
140
141
  /// Return the generic fixup kind for an addition with a given size. It
142
  /// is an error to pass an unsupported size.
143
0
  static MCFixupKind getAddKindForKind(unsigned Kind) {
144
0
    switch (Kind) {
145
0
    default: llvm_unreachable("Unknown type to convert!");
146
0
    case FK_Data_1: return FK_Data_Add_1;
147
0
    case FK_Data_2: return FK_Data_Add_2;
148
0
    case FK_Data_4: return FK_Data_Add_4;
149
0
    case FK_Data_8: return FK_Data_Add_8;
150
0
    }
151
0
  }
152
153
  /// Return the generic fixup kind for an subtraction with a given size. It
154
  /// is an error to pass an unsupported size.
155
0
  static MCFixupKind getSubKindForKind(unsigned Kind) {
156
0
    switch (Kind) {
157
0
    default: llvm_unreachable("Unknown type to convert!");
158
0
    case FK_Data_1: return FK_Data_Sub_1;
159
0
    case FK_Data_2: return FK_Data_Sub_2;
160
0
    case FK_Data_4: return FK_Data_Sub_4;
161
0
    case FK_Data_8: return FK_Data_Sub_8;
162
0
    }
163
0
  }
164
165
30.7k
  SMLoc getLoc() const { return Loc; }
166
};
167
168
} // End llvm namespace
169
170
#endif