-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathelf_loader.cpp
More file actions
324 lines (281 loc) · 11.1 KB
/
Copy pathelf_loader.cpp
File metadata and controls
324 lines (281 loc) · 11.1 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "linx/model/elf_loader.hpp"
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <limits>
#include <stdexcept>
#include <string>
namespace linx::model {
namespace {
constexpr std::array<std::uint8_t, 4> kElfMagic = {0x7f, 'E', 'L', 'F'};
constexpr std::uint8_t kElfClass32 = 1;
constexpr std::uint8_t kElfClass64 = 2;
constexpr std::uint8_t kElfDataLittle = 1;
constexpr std::uint16_t kElfTypeRel = 1;
constexpr std::uint16_t kElfTypeExec = 2;
constexpr std::uint16_t kElfTypeDyn = 3;
constexpr std::uint32_t kPtLoad = 1;
constexpr std::uint32_t kPfX = 1;
constexpr std::uint64_t kShfAlloc = 0x2;
constexpr std::uint64_t kShfExecInstr = 0x4;
constexpr std::uint32_t kShtNoBits = 8;
[[nodiscard]] std::uint16_t ReadLe16(std::span<const std::uint8_t> bytes, std::size_t offset) {
if (offset + 2 > bytes.size()) {
throw std::runtime_error("ELF parse: truncated u16");
}
return static_cast<std::uint16_t>(bytes[offset]) |
(static_cast<std::uint16_t>(bytes[offset + 1]) << 8U);
}
[[nodiscard]] std::uint32_t ReadLe32(std::span<const std::uint8_t> bytes, std::size_t offset) {
if (offset + 4 > bytes.size()) {
throw std::runtime_error("ELF parse: truncated u32");
}
return static_cast<std::uint32_t>(bytes[offset]) |
(static_cast<std::uint32_t>(bytes[offset + 1]) << 8U) |
(static_cast<std::uint32_t>(bytes[offset + 2]) << 16U) |
(static_cast<std::uint32_t>(bytes[offset + 3]) << 24U);
}
[[nodiscard]] std::uint64_t ReadLe64(std::span<const std::uint8_t> bytes, std::size_t offset) {
if (offset + 8 > bytes.size()) {
throw std::runtime_error("ELF parse: truncated u64");
}
std::uint64_t value = 0;
for (std::size_t idx = 0; idx < 8; ++idx) {
value |= static_cast<std::uint64_t>(bytes[offset + idx]) << (idx * 8U);
}
return value;
}
[[nodiscard]] std::string SegmentName(std::size_t index, bool executable) {
return executable ? ".text[" + std::to_string(index) + "]"
: ".load[" + std::to_string(index) + "]";
}
[[nodiscard]] std::string SectionName(std::size_t index, bool executable) {
return executable ? ".text.rel[" + std::to_string(index) + "]"
: ".alloc.rel[" + std::to_string(index) + "]";
}
template <class T> [[nodiscard]] T NarrowCast(std::uint64_t value, const char *context) {
if (value > static_cast<std::uint64_t>(std::numeric_limits<T>::max())) {
throw std::runtime_error(std::string("ELF parse: overflow for ") + context);
}
return static_cast<T>(value);
}
} // namespace
bool IsElfImage(std::span<const std::uint8_t> bytes) noexcept {
return bytes.size() >= kElfMagic.size() &&
std::equal(kElfMagic.begin(), kElfMagic.end(), bytes.begin());
}
std::vector<std::uint8_t> ReadBinaryFile(std::string_view path) {
std::ifstream input(std::string(path), std::ios::binary);
if (!input) {
throw std::runtime_error("failed to open file: " + std::string(path));
}
input.seekg(0, std::ios::end);
const auto end_pos = input.tellg();
if (end_pos < 0) {
throw std::runtime_error("failed to determine file size: " + std::string(path));
}
input.seekg(0, std::ios::beg);
std::vector<std::uint8_t> bytes(static_cast<std::size_t>(end_pos));
if (!bytes.empty()) {
input.read(reinterpret_cast<char *>(bytes.data()), static_cast<std::streamsize>(bytes.size()));
if (!input) {
throw std::runtime_error("failed to read file: " + std::string(path));
}
}
return bytes;
}
ProgramImage LoadRawBinaryImageFromBytes(std::span<const std::uint8_t> bytes,
std::string_view source_path, std::uint64_t base_address,
std::string_view section_name) {
ProgramImage image;
image.source_path = std::string(source_path);
image.entry_point = base_address;
image.sections.push_back(ProgramSection{
.name = std::string(section_name),
.address = base_address,
.bytes = std::vector<std::uint8_t>(bytes.begin(), bytes.end()),
.executable = true,
});
return image;
}
ProgramImage LoadRawBinaryImageFromFile(std::string_view path, std::uint64_t base_address,
std::string_view section_name) {
const auto bytes = ReadBinaryFile(path);
return LoadRawBinaryImageFromBytes(bytes, path, base_address, section_name);
}
ProgramImage LoadElfImageFromBytes(std::span<const std::uint8_t> bytes,
std::string_view source_path) {
if (!IsElfImage(bytes)) {
throw std::runtime_error("not an ELF image: " + std::string(source_path));
}
if (bytes.size() < 16) {
throw std::runtime_error("ELF parse: truncated e_ident");
}
if (bytes[4] != kElfClass32 && bytes[4] != kElfClass64) {
throw std::runtime_error("ELF parse: unsupported class");
}
if (bytes[5] != kElfDataLittle) {
throw std::runtime_error("ELF parse: only little-endian ELF is supported");
}
const bool is_64 = bytes[4] == kElfClass64;
const auto type = ReadLe16(bytes, 16);
if (type != kElfTypeRel && type != kElfTypeExec && type != kElfTypeDyn) {
throw std::runtime_error("ELF parse: unsupported ELF type");
}
ProgramImage image;
image.source_path = std::string(source_path);
std::uint64_t phoff = 0;
std::uint16_t phentsize = 0;
std::uint16_t phnum = 0;
std::uint64_t shoff = 0;
std::uint16_t shentsize = 0;
std::uint16_t shnum = 0;
if (is_64) {
if (bytes.size() < 64) {
throw std::runtime_error("ELF parse: truncated ELF64 header");
}
image.entry_point = ReadLe64(bytes, 24);
phoff = ReadLe64(bytes, 32);
shoff = ReadLe64(bytes, 40);
phentsize = ReadLe16(bytes, 54);
phnum = ReadLe16(bytes, 56);
shentsize = ReadLe16(bytes, 58);
shnum = ReadLe16(bytes, 60);
} else {
if (bytes.size() < 52) {
throw std::runtime_error("ELF parse: truncated ELF32 header");
}
image.entry_point = ReadLe32(bytes, 24);
phoff = ReadLe32(bytes, 28);
shoff = ReadLe32(bytes, 32);
phentsize = ReadLe16(bytes, 42);
phnum = ReadLe16(bytes, 44);
shentsize = ReadLe16(bytes, 46);
shnum = ReadLe16(bytes, 48);
}
if (type == kElfTypeRel) {
std::uint64_t next_address = 0;
if (shentsize == 0 || shnum == 0) {
throw std::runtime_error("ELF parse: relocatable image has no section headers");
}
for (std::uint16_t index = 0; index < shnum; ++index) {
const std::size_t offset = NarrowCast<std::size_t>(
shoff + static_cast<std::uint64_t>(index) * shentsize, "section header offset");
if (offset + shentsize > bytes.size()) {
throw std::runtime_error("ELF parse: truncated section header table");
}
std::uint32_t type_value = 0;
std::uint64_t flags = 0;
std::uint64_t address = 0;
std::uint64_t file_offset = 0;
std::uint64_t file_size = 0;
if (is_64) {
type_value = ReadLe32(bytes, offset + 4);
flags = ReadLe64(bytes, offset + 8);
address = ReadLe64(bytes, offset + 16);
file_offset = ReadLe64(bytes, offset + 24);
file_size = ReadLe64(bytes, offset + 32);
} else {
type_value = ReadLe32(bytes, offset + 4);
flags = ReadLe32(bytes, offset + 8);
address = ReadLe32(bytes, offset + 12);
file_offset = ReadLe32(bytes, offset + 16);
file_size = ReadLe32(bytes, offset + 20);
}
if ((flags & kShfAlloc) == 0U || file_size == 0U) {
continue;
}
if (address == 0U) {
address = next_address;
}
next_address = std::max(next_address, address + file_size + 0x10U);
const bool executable = (flags & kShfExecInstr) != 0U;
std::vector<std::uint8_t> payload;
if (type_value == kShtNoBits) {
payload.resize(NarrowCast<std::size_t>(file_size, "section size"), 0);
} else {
const std::size_t begin = NarrowCast<std::size_t>(file_offset, "section offset");
const std::size_t size = NarrowCast<std::size_t>(file_size, "section size");
if (begin + size > bytes.size()) {
throw std::runtime_error("ELF parse: relocatable section exceeds file size");
}
payload.assign(bytes.begin() + static_cast<std::ptrdiff_t>(begin),
bytes.begin() + static_cast<std::ptrdiff_t>(begin + size));
}
image.sections.push_back(ProgramSection{
.name = SectionName(index, executable),
.address = address,
.bytes = std::move(payload),
.executable = executable,
});
}
if (image.sections.empty()) {
throw std::runtime_error("ELF parse: relocatable image has no alloc sections");
}
image.entry_point = image.sections.front().address;
return image;
}
if (phentsize == 0 || phnum == 0) {
throw std::runtime_error("ELF parse: image has no program headers");
}
for (std::uint16_t index = 0; index < phnum; ++index) {
const std::size_t offset = NarrowCast<std::size_t>(
phoff + static_cast<std::uint64_t>(index) * phentsize, "program header offset");
if (offset + phentsize > bytes.size()) {
throw std::runtime_error("ELF parse: truncated program header table");
}
std::uint32_t type_value = 0;
std::uint32_t flags = 0;
std::uint64_t file_offset = 0;
std::uint64_t virtual_address = 0;
std::uint64_t file_size = 0;
if (is_64) {
type_value = ReadLe32(bytes, offset + 0);
flags = ReadLe32(bytes, offset + 4);
file_offset = ReadLe64(bytes, offset + 8);
virtual_address = ReadLe64(bytes, offset + 16);
file_size = ReadLe64(bytes, offset + 32);
} else {
type_value = ReadLe32(bytes, offset + 0);
file_offset = ReadLe32(bytes, offset + 4);
virtual_address = ReadLe32(bytes, offset + 8);
file_size = ReadLe32(bytes, offset + 16);
flags = ReadLe32(bytes, offset + 24);
}
if (type_value != kPtLoad || file_size == 0) {
continue;
}
const std::size_t begin = NarrowCast<std::size_t>(file_offset, "segment offset");
const std::size_t size = NarrowCast<std::size_t>(file_size, "segment size");
if (begin + size > bytes.size()) {
throw std::runtime_error("ELF parse: segment exceeds file size");
}
const bool executable = (flags & kPfX) != 0U;
image.sections.push_back(ProgramSection{
.name = SegmentName(index, executable),
.address = virtual_address,
.bytes =
std::vector<std::uint8_t>(bytes.begin() + static_cast<std::ptrdiff_t>(begin),
bytes.begin() + static_cast<std::ptrdiff_t>(begin + size)),
.executable = executable,
});
}
if (image.sections.empty()) {
throw std::runtime_error("ELF parse: no PT_LOAD segments found");
}
return image;
}
ProgramImage LoadElfImageFromFile(std::string_view path) {
const auto bytes = ReadBinaryFile(path);
return LoadElfImageFromBytes(bytes, path);
}
ProgramImage LoadProgramImageFromFile(std::string_view path, std::uint64_t raw_base_address) {
const auto bytes = ReadBinaryFile(path);
if (IsElfImage(bytes)) {
return LoadElfImageFromBytes(bytes, path);
}
return LoadRawBinaryImageFromBytes(bytes, path, raw_base_address);
}
} // namespace linx::model