forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelocation.cpp
More file actions
155 lines (141 loc) · 3.88 KB
/
Copy pathRelocation.cpp
File metadata and controls
155 lines (141 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* Copyright 2017 - 2025 R. Thomas
* Copyright 2017 - 2025 Quarkslab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sstream>
#include "LIEF/COFF/Relocation.hpp"
#include "LIEF/COFF/Symbol.hpp"
#include "LIEF/COFF/Section.hpp"
#include "LIEF/BinaryStream/BinaryStream.hpp"
#include <spdlog/fmt/fmt.h>
#include "COFF/structures.hpp"
#include "frozen.hpp"
namespace LIEF::COFF {
std::unique_ptr<Relocation> Relocation::parse(
BinaryStream& stream, Header::MACHINE_TYPES arch)
{
auto raw_reloc = stream.read<details::relocation>();
if (!raw_reloc) {
return nullptr;
}
auto reloc = std::unique_ptr<Relocation>(new Relocation());
reloc->address_ = raw_reloc->VirtualAddress;
reloc->symbol_idx_ = raw_reloc->SymbolTableIndex;
reloc->type_ = from_value(raw_reloc->Type, arch);
return reloc;
}
std::string Relocation::to_string() const {
using fmt::format;
std::ostringstream oss;
oss << format("0x{:08x} {:>26} 0x{:08x}", address(),
COFF::to_string(type()), symbol_idx()
);
if (const Symbol* sym = symbol()) {
oss << " symbol=" << sym->name();
}
if (const Section* sec = section()) {
oss << " section=" << sec->name();
}
return oss.str();
}
const char* to_string(Relocation::TYPE e) {
#define ENTRY(X) std::pair(Relocation::TYPE::X, #X)
STRING_MAP enums2str {
ENTRY(UNKNOWN),
ENTRY(AMD64_ABSOLUTE),
ENTRY(AMD64_ADDR32),
ENTRY(AMD64_ADDR32NB),
ENTRY(AMD64_ADDR64),
ENTRY(AMD64_PAIR),
ENTRY(AMD64_REL32),
ENTRY(AMD64_REL32_1),
ENTRY(AMD64_REL32_2),
ENTRY(AMD64_REL32_3),
ENTRY(AMD64_REL32_4),
ENTRY(AMD64_REL32_5),
ENTRY(AMD64_SECREL),
ENTRY(AMD64_SECREL7),
ENTRY(AMD64_SECTION),
ENTRY(AMD64_SREL32),
ENTRY(AMD64_SSPAN32),
ENTRY(AMD64_TOKEN),
ENTRY(ARM64_ABSOLUTE),
ENTRY(ARM64_ADDR32),
ENTRY(ARM64_ADDR32NB),
ENTRY(ARM64_ADDR64),
ENTRY(ARM64_BRANCH14),
ENTRY(ARM64_BRANCH19),
ENTRY(ARM64_BRANCH26),
ENTRY(ARM64_PAGEBASE_REL21),
ENTRY(ARM64_PAGEOFFSET_12A),
ENTRY(ARM64_PAGEOFFSET_12L),
ENTRY(ARM64_REL21),
ENTRY(ARM64_REL32),
ENTRY(ARM64_SECREL),
ENTRY(ARM64_SECREL_HIGH12A),
ENTRY(ARM64_SECREL_LOW12A),
ENTRY(ARM64_SECREL_LOW12L),
ENTRY(ARM64_SECTION),
ENTRY(ARM64_TOKEN),
ENTRY(ARM_ABSOLUTE),
ENTRY(ARM_ADDR32),
ENTRY(ARM_ADDR32NB),
ENTRY(ARM_BLX11),
ENTRY(ARM_BLX23T),
ENTRY(ARM_BLX24),
ENTRY(ARM_BRANCH11),
ENTRY(ARM_BRANCH20T),
ENTRY(ARM_BRANCH24),
ENTRY(ARM_BRANCH24T),
ENTRY(ARM_MOV32A),
ENTRY(ARM_MOV32T),
ENTRY(ARM_PAIR),
ENTRY(ARM_REL32),
ENTRY(ARM_SECREL),
ENTRY(ARM_SECTION),
ENTRY(ARM_TOKEN),
ENTRY(I386_ABSOLUTE),
ENTRY(I386_DIR16),
ENTRY(I386_DIR32),
ENTRY(I386_DIR32NB),
ENTRY(I386_REL16),
ENTRY(I386_REL32),
ENTRY(I386_SECREL),
ENTRY(I386_SECREL7),
ENTRY(I386_SECTION),
ENTRY(I386_SEG12),
ENTRY(I386_TOKEN),
ENTRY(MIPS_ABSOLUTE),
ENTRY(MIPS_GPREL),
ENTRY(MIPS_JMPADDR),
ENTRY(MIPS_JMPADDR16),
ENTRY(MIPS_LITERAL),
ENTRY(MIPS_PAIR),
ENTRY(MIPS_REFHALF),
ENTRY(MIPS_REFHI),
ENTRY(MIPS_REFLO),
ENTRY(MIPS_REFWORD),
ENTRY(MIPS_REFWORDNB),
ENTRY(MIPS_SECREL),
ENTRY(MIPS_SECRELHI),
ENTRY(MIPS_SECRELLO),
ENTRY(MIPS_SECTION),
};
#undef ENTRY
if (auto it = enums2str.find(e); it != enums2str.end()) {
return it->second;
}
return "UNKNOWN";
}
}