-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathelf_reader.cpp
252 lines (213 loc) · 9.1 KB
/
elf_reader.cpp
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
/*******************************************************************\
Module: Read ELF
Author:
\*******************************************************************/
/// \file
/// Read ELF
#include "elf_reader.h"
#include <util/exception_utils.h>
#include <cstdint>
#include <istream>
static void u16_to_native_endian_inplace(bool le_input, uint16_t &input)
{
const uint8_t *input_as_bytes = reinterpret_cast<uint8_t *>(&input);
input = (((uint16_t)input_as_bytes[0]) << (le_input ? 0 : 8)) |
(((uint16_t)input_as_bytes[1]) << (le_input ? 8 : 0));
}
static void u32_to_native_endian_inplace(bool le_input, uint32_t &input)
{
const uint8_t *input_as_bytes = reinterpret_cast<uint8_t *>(&input);
input = (((uint32_t)input_as_bytes[0]) << (le_input ? 0 : 24)) |
(((uint32_t)input_as_bytes[1]) << (le_input ? 8 : 16)) |
(((uint32_t)input_as_bytes[2]) << (le_input ? 16 : 8)) |
(((uint32_t)input_as_bytes[3]) << (le_input ? 24 : 0));
}
static void
u64_to_native_endian_inplace(bool le_input, unsigned long long &input)
{
static_assert(
sizeof(unsigned long long) == 8,
"unsigned long long expected to be 8 bytes");
const uint8_t *input_as_bytes = reinterpret_cast<uint8_t *>(&input);
input = (((unsigned long long)input_as_bytes[0]) << (le_input ? 0 : 56)) |
(((unsigned long long)input_as_bytes[1]) << (le_input ? 8 : 48)) |
(((unsigned long long)input_as_bytes[2]) << (le_input ? 16 : 40)) |
(((unsigned long long)input_as_bytes[3]) << (le_input ? 24 : 32)) |
(((unsigned long long)input_as_bytes[4]) << (le_input ? 32 : 24)) |
(((unsigned long long)input_as_bytes[5]) << (le_input ? 40 : 16)) |
(((unsigned long long)input_as_bytes[6]) << (le_input ? 48 : 8)) |
(((unsigned long long)input_as_bytes[7]) << (le_input ? 56 : 0));
}
elf_readert::elf_readert(std::istream &_in):in(_in)
{
// read 32-bit header
in.read(
reinterpret_cast<char*>(&elf32_header),
sizeof(elf32_header));
if(!in)
throw deserialization_exceptiont("failed to read ELF header");
if(elf32_header.e_ident[0]!=0x7f ||
elf32_header.e_ident[1]!='E' ||
elf32_header.e_ident[2]!='L' ||
elf32_header.e_ident[3]!='F')
throw deserialization_exceptiont("ELF header malformed (magic)");
elf_class=(elf_classt)elf32_header.e_ident[4];
if(elf_class==ELF32)
{
const auto ei_data = elf32_header.e_ident[5];
if(ei_data==1)
little_endian=true;
else if(ei_data==2)
little_endian=false;
else
throw deserialization_exceptiont("ELF32 header malformed (EI_DATA)");
u16_to_native_endian_inplace(little_endian, elf32_header.e_type);
u16_to_native_endian_inplace(little_endian, elf32_header.e_machine);
u32_to_native_endian_inplace(little_endian, elf32_header.e_version);
u32_to_native_endian_inplace(little_endian, elf32_header.e_entry);
u32_to_native_endian_inplace(little_endian, elf32_header.e_phoff);
u32_to_native_endian_inplace(little_endian, elf32_header.e_shoff);
u32_to_native_endian_inplace(little_endian, elf32_header.e_flags);
u16_to_native_endian_inplace(little_endian, elf32_header.e_ehsize);
u16_to_native_endian_inplace(little_endian, elf32_header.e_phentsize);
u16_to_native_endian_inplace(little_endian, elf32_header.e_phnum);
u16_to_native_endian_inplace(little_endian, elf32_header.e_shentsize);
u16_to_native_endian_inplace(little_endian, elf32_header.e_shnum);
u16_to_native_endian_inplace(little_endian, elf32_header.e_shstrndx);
if(elf32_header.e_version!=1)
throw deserialization_exceptiont("unknown ELF32 version");
// get offset for section header
if(elf32_header.e_shoff==0 ||
elf32_header.e_shnum==0)
throw deserialization_exceptiont("ELF32 without section header");
elf32_section_header_table.resize(elf32_header.e_shnum);
number_of_sections=elf32_header.e_shnum;
// iterate over these
for(std::size_t i=0; i<elf32_section_header_table.size(); i++)
{
// go to right place
in.seekg(elf32_header.e_shoff+i*elf32_header.e_shentsize);
// read section header
in.read(
reinterpret_cast<char*>(&elf32_section_header_table[i]),
sizeof(Elf32_Shdr));
u32_to_native_endian_inplace(
little_endian, elf32_section_header_table[i].sh_name);
u32_to_native_endian_inplace(
little_endian, elf32_section_header_table[i].sh_type);
u32_to_native_endian_inplace(
little_endian, elf32_section_header_table[i].sh_flags);
u32_to_native_endian_inplace(
little_endian, elf32_section_header_table[i].sh_addr);
u32_to_native_endian_inplace(
little_endian, elf32_section_header_table[i].sh_offset);
u32_to_native_endian_inplace(
little_endian, elf32_section_header_table[i].sh_size);
u32_to_native_endian_inplace(
little_endian, elf32_section_header_table[i].sh_link);
u32_to_native_endian_inplace(
little_endian, elf32_section_header_table[i].sh_info);
u32_to_native_endian_inplace(
little_endian, elf32_section_header_table[i].sh_addralign);
u32_to_native_endian_inplace(
little_endian, elf32_section_header_table[i].sh_entsize);
}
// string table
unsigned string_table_nr=elf32_header.e_shstrndx;
if(string_table_nr>=elf32_section_header_table.size())
throw deserialization_exceptiont("ELF32 without string table");
string_table_offset=section_offset(string_table_nr);
}
else if(elf_class==ELF64)
{
// read 64-bit header
in.seekg(0);
in.read(
reinterpret_cast<char*>(&elf64_header),
sizeof(elf64_header));
const auto ei_data = elf64_header.e_ident[5];
if(ei_data==1)
little_endian=true;
else if(ei_data==2)
little_endian=false;
else
throw deserialization_exceptiont("ELF64 header malformed (EI_DATA)");
u16_to_native_endian_inplace(little_endian, elf64_header.e_type);
u16_to_native_endian_inplace(little_endian, elf64_header.e_machine);
u32_to_native_endian_inplace(little_endian, elf64_header.e_version);
u64_to_native_endian_inplace(little_endian, elf64_header.e_entry);
u64_to_native_endian_inplace(little_endian, elf64_header.e_phoff);
u64_to_native_endian_inplace(little_endian, elf64_header.e_shoff);
u32_to_native_endian_inplace(little_endian, elf64_header.e_flags);
u16_to_native_endian_inplace(little_endian, elf64_header.e_ehsize);
u16_to_native_endian_inplace(little_endian, elf64_header.e_phentsize);
u16_to_native_endian_inplace(little_endian, elf64_header.e_phnum);
u16_to_native_endian_inplace(little_endian, elf64_header.e_shentsize);
u16_to_native_endian_inplace(little_endian, elf64_header.e_shnum);
u16_to_native_endian_inplace(little_endian, elf64_header.e_shstrndx);
if(elf64_header.e_version!=1)
throw deserialization_exceptiont("unknown ELF64 version");
// get offset for section header
if(elf64_header.e_shoff==0 ||
elf64_header.e_shnum==0)
throw deserialization_exceptiont("ELF64 without section header");
elf64_section_header_table.resize(elf64_header.e_shnum);
number_of_sections=elf64_header.e_shnum;
// iterate over these
for(std::size_t i=0; i<elf64_section_header_table.size(); i++)
{
// go to right place
in.seekg(elf64_header.e_shoff+i*elf64_header.e_shentsize);
// read section header
in.read(
reinterpret_cast<char*>(&elf64_section_header_table[i]),
sizeof(Elf64_Shdr));
u32_to_native_endian_inplace(
little_endian, elf64_section_header_table[i].sh_name);
u32_to_native_endian_inplace(
little_endian, elf64_section_header_table[i].sh_type);
u64_to_native_endian_inplace(
little_endian, elf64_section_header_table[i].sh_flags);
u64_to_native_endian_inplace(
little_endian, elf64_section_header_table[i].sh_addr);
u64_to_native_endian_inplace(
little_endian, elf64_section_header_table[i].sh_offset);
u64_to_native_endian_inplace(
little_endian, elf64_section_header_table[i].sh_size);
u32_to_native_endian_inplace(
little_endian, elf64_section_header_table[i].sh_link);
u32_to_native_endian_inplace(
little_endian, elf64_section_header_table[i].sh_info);
u64_to_native_endian_inplace(
little_endian, elf64_section_header_table[i].sh_addralign);
u64_to_native_endian_inplace(
little_endian, elf64_section_header_table[i].sh_entsize);
}
// string table
unsigned string_table_nr=elf64_header.e_shstrndx;
if(string_table_nr>=elf64_section_header_table.size())
throw deserialization_exceptiont("ELF64 without string table");
string_table_offset=section_offset(string_table_nr);
}
}
std::string elf_readert::get_string(std::streampos index) const
{
in.seekg(string_table_offset+index);
std::string result;
while(in)
{
char ch;
in.read(&ch, 1);
if(ch==0)
break;
result+=ch;
}
return result;
}
bool elf_readert::has_section(const std::string &name) const
{
for(std::size_t i=0; i<number_of_sections; i++)
if(section_name(i)==name)
return true;
return false;
}