LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
RelocationEntry.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 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_PE_RELOCATION_ENTRY_H
17#define LIEF_PE_RELOCATION_ENTRY_H
18
19#include <ostream>
20#include <cassert>
21
23
24#include "LIEF/Object.hpp"
25#include "LIEF/visibility.h"
26#include "LIEF/PE/Header.hpp"
27
28namespace LIEF {
29namespace PE {
30
31class Relocation;
32
38
39 friend class Parser;
40 friend class Builder;
41 friend class PE::Relocation;
42
43 public:
44 static constexpr auto MAX_ADDR = 1 << 12;
45
48 enum class BASE_TYPES {
49 UNKNOWN = -1,
50
53 ABS = 0,
54
65 HIGH = 1,
66
75 LOW = 2,
76
87 HIGHLOW = 3,
88
96 HIGHADJ = 4,
97
98 MIPS_JMPADDR = 5 | (1 << 8),
99 ARM_MOV32 = 5 | (1 << 9),
100 RISCV_HI20 = 5 | (1 << 10),
101
102 SECTION = 6,
103
104 THUMB_MOV32 = 7 | (1 << 11),
105 RISCV_LOW12I = 7 | (1 << 12),
106
107 RISCV_LOW12S = 8 | (1 << 13),
108 LOONARCH_MARK_LA = 8 | (1 << 14),
109
110 MIPS_JMPADDR16 = 9,
111
121 DIR64 = 10,
122 HIGH3ADJ = 11,
123 };
124
125 static uint16_t get_position(uint16_t data) {
126 return data & 0xFFF;
127 }
128
129 static uint16_t get_type(uint16_t data) {
130 return (data >> 12) & 0xFF;
131 }
132
134
135 RelocationEntry() = default;
137 LIEF::Relocation(other),
138 position_(other.position_),
139 type_(other.type_),
140 // Parent relocation is not forwarded during copy
141 relocation_(nullptr) {}
142
144 swap(other);
145 return *this;
146 }
147
150
152 position_(position),
153 type_(type) {
154 assert(position_ < MAX_ADDR);
155 }
156
157 ~RelocationEntry() override = default;
158
159 void swap(RelocationEntry& other) {
161 std::swap(position_, other.position_);
162 std::swap(type_, other.type_);
163 std::swap(relocation_, other.relocation_);
164 }
165
167 uint64_t address() const override;
168
169 void address(uint64_t address) override;
170
172 size_t size() const override;
173
174 void size(size_t size) override;
175
179 uint16_t data() const {
180 return (uint16_t((uint8_t)type_ & 0xFF) << 12) | position_;
181 }
182
184 uint16_t position() const {
185 return position_;
186 }
187
189 BASE_TYPES type() const {
190 return type_;
191 }
192
193 void position(uint16_t position) {
194 assert(position < MAX_ADDR);
195 position_ = position;
196 }
197
199 type_ = type;
200 }
201
202 void accept(Visitor& visitor) const override;
203
204 LIEF_API friend std::ostream& operator<<(std::ostream& os,
205 const RelocationEntry& entry);
206
208 LIEF_LOCAL PE::Relocation* parent() {
209 return relocation_;
210 }
211
213 LIEF_LOCAL const PE::Relocation* parent() const {
214 return relocation_;
215 }
216
218 LIEF_LOCAL void parent(PE::Relocation& R) {
219 relocation_ = &R;
220 }
221
222 private:
223 uint16_t position_ = 0;
224 BASE_TYPES type_ = BASE_TYPES::ABS;
225 PE::Relocation* relocation_ = nullptr; // Used to compute some information
226};
227
229
230}
231}
232#endif
MACHINE_TYPES
Definition PE/Header.hpp:40
void address(uint64_t address) override
RelocationEntry & operator=(RelocationEntry other)
Definition RelocationEntry.hpp:143
void swap(RelocationEntry &other)
Definition RelocationEntry.hpp:159
size_t size() const override
The size of the relocatable pointer.
void size(size_t size) override
uint16_t position() const
Offset relative to Relocation::virtual_address where the relocation occurs.
Definition RelocationEntry.hpp:184
uint16_t data() const
Raw data of the relocation:
Definition RelocationEntry.hpp:179
RelocationEntry(uint16_t position, BASE_TYPES type)
Definition RelocationEntry.hpp:151
uint64_t address() const override
The address of the relocation.
friend std::ostream & operator<<(std::ostream &os, const RelocationEntry &entry)
friend class PE::Relocation
Definition RelocationEntry.hpp:41
void accept(Visitor &visitor) const override
Method so that the visitor can visit us.
RelocationEntry(const RelocationEntry &other)
Definition RelocationEntry.hpp:136
void position(uint16_t position)
Definition RelocationEntry.hpp:193
static BASE_TYPES type_from_data(Header::MACHINE_TYPES arch, uint16_t data)
friend class Builder
Definition RelocationEntry.hpp:40
~RelocationEntry() override=default
static uint16_t get_type(uint16_t data)
Definition RelocationEntry.hpp:129
BASE_TYPES
Relocation type as described in https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#base-...
Definition RelocationEntry.hpp:48
static uint16_t get_position(uint16_t data)
Definition RelocationEntry.hpp:125
friend class Parser
Definition RelocationEntry.hpp:39
RelocationEntry(RelocationEntry &&other)=default
static constexpr auto MAX_ADDR
Definition RelocationEntry.hpp:44
void type(BASE_TYPES type)
Definition RelocationEntry.hpp:198
BASE_TYPES type() const
Type of the relocation.
Definition RelocationEntry.hpp:189
RelocationEntry & operator=(RelocationEntry &&other)=default
Class which represents the Base Relocation Block We usually find this structure in the ....
Definition PE/Relocation.hpp:43
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:27
void swap(Relocation &other)
Definition Abstract/Relocation.hpp:41
Definition Visitor.hpp:212
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
const char * to_string(CODE_PAGES e)
LIEF namespace.
Definition Abstract/Binary.hpp:40
#define LIEF_API
Definition visibility.h:43
#define LIEF_LOCAL
Definition visibility.h:44