LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
COFF/Relocation.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_COFF_RELOCATION_H
17#define LIEF_COFF_RELOCATION_H
18#include <cstdint>
19#include <memory>
20#include <ostream>
21
22#include "LIEF/visibility.h"
23#include "LIEF/COFF/Header.hpp"
25
26namespace LIEF {
27class BinaryStream;
28namespace COFF {
29class Section;
30class Parser;
31class Symbol;
32
34class LIEF_API Relocation : public LIEF::Relocation {
35 public:
36 friend class Parser;
37
38 static constexpr uint32_t I386 = 1 << 17;
39 static constexpr uint32_t X64 = 1 << 18;
40 static constexpr uint32_t ARM = 1 << 19;
41 static constexpr uint32_t ARM64 = 1 << 20;
42 static constexpr uint32_t MIPS = 1 << 21;
43
48 enum class TYPE : uint32_t {
49 UNKNOWN = uint32_t(-1),
50 I386_ABSOLUTE = I386 + 0x0000,
51 I386_DIR16 = I386 + 0x0001,
52 I386_REL16 = I386 + 0x0002,
53 I386_DIR32 = I386 + 0x0006,
54 I386_DIR32NB = I386 + 0x0007,
55 I386_SEG12 = I386 + 0x0009,
56 I386_SECTION = I386 + 0x000A,
57 I386_SECREL = I386 + 0x000B,
58 I386_TOKEN = I386 + 0x000C,
59 I386_SECREL7 = I386 + 0x000D,
60 I386_REL32 = I386 + 0x0014,
61
62 AMD64_ABSOLUTE = X64 + 0x0000,
63 AMD64_ADDR64 = X64 + 0x0001,
64 AMD64_ADDR32 = X64 + 0x0002,
65 AMD64_ADDR32NB = X64 + 0x0003,
66 AMD64_REL32 = X64 + 0x0004,
67 AMD64_REL32_1 = X64 + 0x0005,
68 AMD64_REL32_2 = X64 + 0x0006,
69 AMD64_REL32_3 = X64 + 0x0007,
70 AMD64_REL32_4 = X64 + 0x0008,
71 AMD64_REL32_5 = X64 + 0x0009,
72 AMD64_SECTION = X64 + 0x000A,
73 AMD64_SECREL = X64 + 0x000B,
74 AMD64_SECREL7 = X64 + 0x000C,
75 AMD64_TOKEN = X64 + 0x000D,
76 AMD64_SREL32 = X64 + 0x000E,
77 AMD64_PAIR = X64 + 0x000F,
78 AMD64_SSPAN32 = X64 + 0x0010,
79
80 ARM_ABSOLUTE = ARM + 0x0000,
81 ARM_ADDR32 = ARM + 0x0001,
82 ARM_ADDR32NB = ARM + 0x0002,
83 ARM_BRANCH24 = ARM + 0x0003,
84 ARM_BRANCH11 = ARM + 0x0004,
85 ARM_TOKEN = ARM + 0x0005,
86 ARM_BLX24 = ARM + 0x0008,
87 ARM_BLX11 = ARM + 0x0009,
88 ARM_REL32 = ARM + 0x000A,
89 ARM_SECTION = ARM + 0x000E,
90 ARM_SECREL = ARM + 0x000F,
91 ARM_MOV32A = ARM + 0x0010,
92 ARM_MOV32T = ARM + 0x0011,
93 ARM_BRANCH20T = ARM + 0x0012,
94 ARM_BRANCH24T = ARM + 0x0014,
95 ARM_BLX23T = ARM + 0x0015,
96 ARM_PAIR = ARM + 0x0016,
97
99 ARM64_ADDR32 = ARM64 + 0x0001,
103 ARM64_REL21 = ARM64 + 0x0005,
106 ARM64_SECREL = ARM64 + 0x0008,
110 ARM64_TOKEN = ARM64 + 0x000C,
112 ARM64_ADDR64 = ARM64 + 0x000E,
115 ARM64_REL32 = ARM64 + 0x0011,
116
117 MIPS_ABSOLUTE = MIPS + 0x0000,
118 MIPS_REFHALF = MIPS + 0x0001,
119 MIPS_REFWORD = MIPS + 0x0002,
120 MIPS_JMPADDR = MIPS + 0x0003,
121 MIPS_REFHI = MIPS + 0x0004,
122 MIPS_REFLO = MIPS + 0x0005,
123 MIPS_GPREL = MIPS + 0x0006,
124 MIPS_LITERAL = MIPS + 0x0007,
125 MIPS_SECTION = MIPS + 0x000A,
126 MIPS_SECREL = MIPS + 0x000B,
127 MIPS_SECRELLO = MIPS + 0x000C,
128 MIPS_SECRELHI = MIPS + 0x000D,
131 MIPS_PAIR = MIPS + 0x0025,
132 };
133
135 static uint16_t to_value(TYPE rtype) {
136 return (uint16_t)rtype;
137 }
138
140 static TYPE from_value(uint16_t value, Header::MACHINE_TYPES arch) {
141 switch (arch) {
142 case Header::MACHINE_TYPES::ARM64:
143 return TYPE(value + ARM64);
144
145 case Header::MACHINE_TYPES::AMD64:
146 return TYPE(value + X64);
147
148 case Header::MACHINE_TYPES::I386:
149 return TYPE(value + I386);
150
151 case Header::MACHINE_TYPES::ARM:
152 case Header::MACHINE_TYPES::ARMNT:
153 case Header::MACHINE_TYPES::THUMB:
154 return TYPE(value + ARM);
155
156 case Header::MACHINE_TYPES::R4000:
157 return TYPE(value + MIPS);
158
159 default:
160 return TYPE::UNKNOWN;
161 }
162 return TYPE::UNKNOWN;
163 }
164
166 static std::unique_ptr<Relocation> parse(
167 BinaryStream& stream, Header::MACHINE_TYPES arch);
168
170 uint32_t symbol_idx() const {
171 return symbol_idx_;
172 }
173
176 return symbol_;
177 }
178
179 const Symbol* symbol() const {
180 return symbol_;
181 }
182
184 TYPE type() const {
185 return type_;
186 }
187
190 return section_;
191 }
192
193 const Section* section() const {
194 return section_;
195 }
196
197 std::string to_string() const;
198
199 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Relocation& R) {
200 os << R.to_string();
201 return os;
202 }
203
204 ~Relocation() override = default;
205
206 private:
207 Relocation() = default;
208 uint32_t symbol_idx_ = 0;
209 TYPE type_ = TYPE::UNKNOWN;
210 Section* section_ = nullptr;
211 Symbol* symbol_ = nullptr;
212};
213
215
216}
217}
218#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
LIEF::PE::Header::MACHINE_TYPES MACHINE_TYPES
The different architectures (mirrored from PE).
Definition COFF/Header.hpp:43
Definition COFF/Parser.hpp:34
static constexpr uint32_t MIPS
Definition COFF/Relocation.hpp:42
TYPE type() const
Type of the relocation.
Definition COFF/Relocation.hpp:184
Section * section()
Section in which the relocation takes place.
Definition COFF/Relocation.hpp:189
static constexpr uint32_t X64
Definition COFF/Relocation.hpp:39
~Relocation() override=default
TYPE
The different relocation types.
Definition COFF/Relocation.hpp:48
@ MIPS_LITERAL
Definition COFF/Relocation.hpp:124
@ I386_DIR16
Definition COFF/Relocation.hpp:51
@ I386_REL32
Definition COFF/Relocation.hpp:60
@ ARM_SECTION
Definition COFF/Relocation.hpp:89
@ AMD64_ADDR64
Definition COFF/Relocation.hpp:63
@ I386_SECREL
Definition COFF/Relocation.hpp:57
@ MIPS_REFHALF
Definition COFF/Relocation.hpp:118
@ ARM64_SECREL
Definition COFF/Relocation.hpp:106
@ MIPS_REFWORD
Definition COFF/Relocation.hpp:119
@ AMD64_SSPAN32
Definition COFF/Relocation.hpp:78
@ ARM64_PAGEOFFSET_12L
Definition COFF/Relocation.hpp:105
@ AMD64_REL32_1
Definition COFF/Relocation.hpp:67
@ ARM64_SECREL_HIGH12A
Definition COFF/Relocation.hpp:108
@ ARM_ADDR32
Definition COFF/Relocation.hpp:81
@ AMD64_REL32_3
Definition COFF/Relocation.hpp:69
@ MIPS_REFLO
Definition COFF/Relocation.hpp:122
@ ARM_BLX11
Definition COFF/Relocation.hpp:87
@ AMD64_SREL32
Definition COFF/Relocation.hpp:76
@ MIPS_REFWORDNB
Definition COFF/Relocation.hpp:130
@ MIPS_ABSOLUTE
Definition COFF/Relocation.hpp:117
@ MIPS_SECRELLO
Definition COFF/Relocation.hpp:127
@ ARM64_REL32
Definition COFF/Relocation.hpp:115
@ MIPS_GPREL
Definition COFF/Relocation.hpp:123
@ ARM64_TOKEN
Definition COFF/Relocation.hpp:110
@ I386_SECTION
Definition COFF/Relocation.hpp:56
@ MIPS_SECREL
Definition COFF/Relocation.hpp:126
@ ARM_BRANCH24T
Definition COFF/Relocation.hpp:94
@ AMD64_SECREL
Definition COFF/Relocation.hpp:73
@ ARM64_BRANCH19
Definition COFF/Relocation.hpp:113
@ UNKNOWN
Definition COFF/Relocation.hpp:49
@ AMD64_ADDR32
Definition COFF/Relocation.hpp:64
@ AMD64_PAIR
Definition COFF/Relocation.hpp:77
@ ARM_SECREL
Definition COFF/Relocation.hpp:90
@ ARM64_SECREL_LOW12L
Definition COFF/Relocation.hpp:109
@ ARM_BRANCH11
Definition COFF/Relocation.hpp:84
@ ARM_MOV32A
Definition COFF/Relocation.hpp:91
@ ARM64_BRANCH14
Definition COFF/Relocation.hpp:114
@ ARM_BLX24
Definition COFF/Relocation.hpp:86
@ ARM_BRANCH24
Definition COFF/Relocation.hpp:83
@ ARM64_REL21
Definition COFF/Relocation.hpp:103
@ AMD64_TOKEN
Definition COFF/Relocation.hpp:75
@ MIPS_SECTION
Definition COFF/Relocation.hpp:125
@ ARM64_ADDR32
Definition COFF/Relocation.hpp:99
@ ARM_PAIR
Definition COFF/Relocation.hpp:96
@ AMD64_SECTION
Definition COFF/Relocation.hpp:72
@ AMD64_REL32_5
Definition COFF/Relocation.hpp:71
@ ARM_REL32
Definition COFF/Relocation.hpp:88
@ AMD64_SECREL7
Definition COFF/Relocation.hpp:74
@ AMD64_ADDR32NB
Definition COFF/Relocation.hpp:65
@ MIPS_REFHI
Definition COFF/Relocation.hpp:121
@ ARM64_SECTION
Definition COFF/Relocation.hpp:111
@ I386_ABSOLUTE
Definition COFF/Relocation.hpp:50
@ AMD64_REL32_2
Definition COFF/Relocation.hpp:68
@ ARM64_SECREL_LOW12A
Definition COFF/Relocation.hpp:107
@ ARM64_PAGEOFFSET_12A
Definition COFF/Relocation.hpp:104
@ ARM64_ADDR32NB
Definition COFF/Relocation.hpp:100
@ AMD64_ABSOLUTE
Definition COFF/Relocation.hpp:62
@ ARM_BLX23T
Definition COFF/Relocation.hpp:95
@ MIPS_JMPADDR16
Definition COFF/Relocation.hpp:129
@ I386_REL16
Definition COFF/Relocation.hpp:52
@ MIPS_JMPADDR
Definition COFF/Relocation.hpp:120
@ ARM64_BRANCH26
Definition COFF/Relocation.hpp:101
@ I386_DIR32
Definition COFF/Relocation.hpp:53
@ ARM64_ADDR64
Definition COFF/Relocation.hpp:112
@ ARM_ADDR32NB
Definition COFF/Relocation.hpp:82
@ MIPS_SECRELHI
Definition COFF/Relocation.hpp:128
@ I386_DIR32NB
Definition COFF/Relocation.hpp:54
@ I386_TOKEN
Definition COFF/Relocation.hpp:58
@ AMD64_REL32_4
Definition COFF/Relocation.hpp:70
@ ARM_MOV32T
Definition COFF/Relocation.hpp:92
@ AMD64_REL32
Definition COFF/Relocation.hpp:66
@ ARM_ABSOLUTE
Definition COFF/Relocation.hpp:80
@ I386_SEG12
Definition COFF/Relocation.hpp:55
@ ARM64_PAGEBASE_REL21
Definition COFF/Relocation.hpp:102
@ MIPS_PAIR
Definition COFF/Relocation.hpp:131
@ ARM64_ABSOLUTE
Definition COFF/Relocation.hpp:98
@ I386_SECREL7
Definition COFF/Relocation.hpp:59
@ ARM_TOKEN
Definition COFF/Relocation.hpp:85
@ ARM_BRANCH20T
Definition COFF/Relocation.hpp:93
static constexpr uint32_t ARM
Definition COFF/Relocation.hpp:40
static constexpr uint32_t I386
Definition COFF/Relocation.hpp:38
static constexpr uint32_t ARM64
Definition COFF/Relocation.hpp:41
static std::unique_ptr< Relocation > parse(BinaryStream &stream, Header::MACHINE_TYPES arch)
Create a relocation from the given stream.
static TYPE from_value(uint16_t value, Header::MACHINE_TYPES arch)
Create a relocation type from its raw value and the architecture.
Definition COFF/Relocation.hpp:140
friend std::ostream & operator<<(std::ostream &os, const Relocation &R)
Definition COFF/Relocation.hpp:199
uint32_t symbol_idx() const
Symbol index associated with this relocation.
Definition COFF/Relocation.hpp:170
const Symbol * symbol() const
Definition COFF/Relocation.hpp:179
std::string to_string() const
friend class Parser
Definition COFF/Relocation.hpp:36
Symbol * symbol()
Symbol associated with the relocation (if any).
Definition COFF/Relocation.hpp:175
static uint16_t to_value(TYPE rtype)
Convert a relocation enum type into a 16-bits value.
Definition COFF/Relocation.hpp:135
const Section * section() const
Definition COFF/Relocation.hpp:193
This class represents a COFF section.
Definition COFF/Section.hpp:39
This class represents a COFF symbol.
Definition COFF/Symbol.hpp:35
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:27
Definition AuxiliarySymbol.hpp:29
const char * to_string(AuxiliarySectionDefinition::COMDAT_SELECTION e)
LIEF namespace.
Definition Abstract/Binary.hpp:40
#define LIEF_API
Definition visibility.h:41