LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
RelocationEntry.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_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;
32class LIEF_API RelocationEntry : public LIEF::Relocation {
37
38 friend class Parser;
39 friend class Builder;
40 friend class PE::Relocation;
41
42 public:
43 static constexpr auto MAX_ADDR = 1 << 12;
44 enum class BASE_TYPES {
48 UNKNOWN = -1,
49 ABS = 0,
53 HIGH = 1,
65 LOW = 2,
75 HIGHLOW = 3,
87 HIGHADJ = 4,
96
97 MIPS_JMPADDR = 5 | (1 << 8),
98 ARM_MOV32 = 5 | (1 << 9),
99 RISCV_HI20 = 5 | (1 << 10),
100
101 SECTION = 6,
102
103 THUMB_MOV32 = 7 | (1 << 11),
104 RISCV_LOW12I = 7 | (1 << 12),
105
106 RISCV_LOW12S = 8 | (1 << 13),
107 LOONARCH_MARK_LA = 8 | (1 << 14),
108
109 MIPS_JMPADDR16 = 9,
110 DIR64 = 10,
121 HIGH3ADJ = 11,
122 };
123
124 static uint16_t get_position(uint16_t data) {
125 return data & 0xFFF;
126 }
127
128 static uint16_t get_type(uint16_t data) {
129 return (data >> 12) & 0xFF;
130 }
131
132 static BASE_TYPES type_from_data(Header::MACHINE_TYPES arch, uint16_t data);
133
134 RelocationEntry() = default;
135 RelocationEntry(const RelocationEntry& other) :
136 LIEF::Relocation(other),
137 position_(other.position_),
138 type_(other.type_),
139 // Parent relocation is not forwarded during copy
140 relocation_(nullptr)
141 {}
142
143 RelocationEntry& operator=(RelocationEntry other) {
144 swap(other);
145 return *this;
146 }
147
148 RelocationEntry(RelocationEntry&& other) = default;
149 RelocationEntry& operator=(RelocationEntry&& other) = default;
150
151 RelocationEntry(uint16_t position, BASE_TYPES type) :
152 position_(position),
153 type_(type)
154 {
155 assert(position_ < MAX_ADDR);
156 }
157
158 ~RelocationEntry() override = default;
159
160 void swap(RelocationEntry& other) {
162 std::swap(position_, other.position_);
163 std::swap(type_, other.type_);
164 std::swap(relocation_, other.relocation_);
165 }
166 uint64_t address() const override;
169
170 void address(uint64_t address) override;
171 size_t size() const override;
174
175 void size(size_t size) override;
176 uint16_t data() const {
181 return (uint16_t((uint8_t)type_ & 0xFF) << 12) | position_;
182 }
183 uint16_t position() const {
186 return position_;
187 }
188 BASE_TYPES type() const {
191 return type_;
192 }
193
194 void position(uint16_t position) {
195 assert(position < MAX_ADDR);
196 position_ = position;
197 }
198
199 void type(BASE_TYPES type) {
200 type_ = type;
201 }
202
203 void accept(Visitor& visitor) const override;
204
205 LIEF_API friend std::ostream& operator<<(std::ostream& os, const RelocationEntry& entry);
206 LIEF_LOCAL PE::Relocation* parent() {
209 return relocation_;
210 }
211 LIEF_LOCAL const PE::Relocation* parent() const {
214 return relocation_;
215 }
216 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
228LIEF_API const char* to_string(RelocationEntry::BASE_TYPES e);
229
230}
231}
232#endif
Relocation.hpp
Object.hpp
Header.hpp
LIEF::PE::RelocationEntry
Class which represents an entry of the PE relocation table.
Definition RelocationEntry.hpp:36
LIEF::PE::RelocationEntry::address
void address(uint64_t address) override
LIEF::PE::RelocationEntry::operator=
RelocationEntry & operator=(RelocationEntry other)
Definition RelocationEntry.hpp:143
LIEF::PE::RelocationEntry::swap
void swap(RelocationEntry &other)
Definition RelocationEntry.hpp:160
LIEF::PE::RelocationEntry::size
size_t size() const override
The size of the relocatable pointer.
LIEF::PE::RelocationEntry::size
void size(size_t size) override
LIEF::PE::RelocationEntry::position
uint16_t position() const
Offset relative to Relocation::virtual_address where the relocation occurs.
Definition RelocationEntry.hpp:185
LIEF::PE::RelocationEntry::RelocationEntry
RelocationEntry()=default
LIEF::PE::RelocationEntry::data
uint16_t data() const
Raw data of the relocation:
Definition RelocationEntry.hpp:180
LIEF::PE::RelocationEntry::RelocationEntry
RelocationEntry(uint16_t position, BASE_TYPES type)
Definition RelocationEntry.hpp:151
LIEF::PE::RelocationEntry::address
uint64_t address() const override
The address of the relocation.
LIEF::PE::RelocationEntry::operator<<
friend std::ostream & operator<<(std::ostream &os, const RelocationEntry &entry)
LIEF::PE::RelocationEntry::accept
void accept(Visitor &visitor) const override
Method so that the visitor can visit us.
LIEF::PE::RelocationEntry::RelocationEntry
RelocationEntry(const RelocationEntry &other)
Definition RelocationEntry.hpp:135
LIEF::PE::RelocationEntry::position
void position(uint16_t position)
Definition RelocationEntry.hpp:194
LIEF::PE::RelocationEntry::type_from_data
static BASE_TYPES type_from_data(Header::MACHINE_TYPES arch, uint16_t data)
LIEF::PE::RelocationEntry::~RelocationEntry
~RelocationEntry() override=default
LIEF::PE::RelocationEntry::get_type
static uint16_t get_type(uint16_t data)
Definition RelocationEntry.hpp:128
LIEF::PE::RelocationEntry::BASE_TYPES
BASE_TYPES
Relocation type as described in https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#base-...
Definition RelocationEntry.hpp:47
LIEF::PE::RelocationEntry::get_position
static uint16_t get_position(uint16_t data)
Definition RelocationEntry.hpp:124
LIEF::PE::RelocationEntry::RelocationEntry
RelocationEntry(RelocationEntry &&other)=default
LIEF::PE::RelocationEntry::type
void type(BASE_TYPES type)
Definition RelocationEntry.hpp:199
LIEF::PE::RelocationEntry::type
BASE_TYPES type() const
Type of the relocation.
Definition RelocationEntry.hpp:190
LIEF::PE::RelocationEntry::operator=
RelocationEntry & operator=(RelocationEntry &&other)=default
LIEF::PE::Relocation
Class which represents the Base Relocation Block We usually find this structure in the ....
Definition PE/Relocation.hpp:42
LIEF::Relocation::swap
void swap(Relocation &other)
Definition Abstract/Relocation.hpp:42
LIEF::PE
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF::PE::to_string
const char * to_string(AuxiliaryWeakExternal::CHARACTERISTICS e)
LIEF::PE::IMPHASH_MODE::LIEF
@ LIEF
Definition PE/utils.hpp:35
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42