-
Notifications
You must be signed in to change notification settings - Fork 62
/
kallsyms_lookup_name.cpp
363 lines (322 loc) · 10.8 KB
/
kallsyms_lookup_name.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#pragma once
#include "kallsyms_lookup_name.h"
#include "base_func.h"
#ifndef MIN
#define MIN(x, y)(x < y) ? (x) : (y)
#endif // !MIN
#define MAX_FIND_RANGE 0x1000
namespace {
const int KSYM_NAME_LEN = 128;
}
KallsymsLookupName::KallsymsLookupName(const std::vector<char>& file_buf) : m_file_buf(file_buf)
{
}
KallsymsLookupName::~KallsymsLookupName()
{
}
bool KallsymsLookupName::init() {
size_t addresses_list_start = 0, addresses_list_end = 0;
if (!find_kallsyms_addresses_list(addresses_list_start, addresses_list_end)) {
std::cout << "Unable to find the list of kallsyms addresses" << std::endl;
return false;
}
size_t kallsyms_num_offset = 0;
m_kallsyms_num = find_kallsyms_num(addresses_list_start, addresses_list_end, kallsyms_num_offset);
if (!m_kallsyms_num) {
std::cout << "Unable to find the num of kallsyms addresses list" << std::endl;
return false;
}
std::cout << std::hex << "kallsyms_num: 0x" << m_kallsyms_num << std::endl;
addresses_list_start = addresses_list_end - m_kallsyms_num * sizeof(uint64_t);
std::cout << std::hex << "kallsyms_addresses_start: 0x" << addresses_list_start << std::endl;
std::cout << std::hex << "kallsyms_addresses_end: 0x" << addresses_list_end << std::endl;
m_kallsyms_addresses.offset = addresses_list_start;
size_t kallsyms_num_end_offset = kallsyms_num_offset + sizeof(m_kallsyms_num);
size_t name_list_start = 0, name_list_end = 0;
if (!find_kallsyms_names_list(m_kallsyms_num, kallsyms_num_end_offset, name_list_start, name_list_end)) {
std::cout << "Unable to find the list of kallsyms names list" << std::endl;
return false;
}
std::cout << std::hex << "kallsyms_names_start: 0x" << name_list_start << std::endl;
std::cout << std::hex << "kallsyms_names_end: 0x" << name_list_end << std::endl;
m_kallsyms_names.offset = name_list_start;
size_t markers_list_start = 0;
size_t markers_list_end = 0;
if (!find_kallsyms_markers_list(m_kallsyms_num, name_list_end, markers_list_start, markers_list_end)) {
std::cout << "Unable to find the list of kallsyms markers list" << std::endl;
return false;
}
std::cout << std::hex << "kallsyms_markers_start: 0x" << markers_list_start << std::endl;
std::cout << std::hex << "kallsyms_markers_end: 0x" << markers_list_end << std::endl;
m_kallsyms_markers.offset = markers_list_start;
size_t token_table_start = 0;
size_t token_table_end = 0;
if (!find_kallsyms_token_table(markers_list_end, token_table_start, token_table_end)) {
std::cout << "Unable to find the list of kallsyms token table" << std::endl;
return false;
}
std::cout << std::hex << "kallsyms_token_table_start: 0x" << token_table_start << std::endl;
std::cout << std::hex << "kallsyms_token_table_end: 0x" << token_table_end << std::endl;
m_kallsyms_token_table.offset = token_table_start;
size_t token_index_start = 0;
if (!find_kallsyms_token_index(token_table_end, token_index_start)) {
std::cout << "Unable to find the list of kallsyms token table" << std::endl;
return false;
}
std::cout << std::hex << "kallsyms_token_index_start: 0x" << token_index_start << std::endl;
m_kallsyms_token_index.offset = token_index_start;
size_t kallsyms_sym_func_entry_offset = 0;
if (!find_kallsyms_sym_func_entry_offset(kallsyms_sym_func_entry_offset)) {
std::cout << "Unable to find the list of kallsyms sym function entry offset" << std::endl;
return false;
}
std::cout << std::hex << "kallsyms_sym_func_entry_offset: 0x" << kallsyms_sym_func_entry_offset << std::endl;
m_kallsyms_sym_func_entry_offset = kallsyms_sym_func_entry_offset;
m_inited = true;
return true;
}
bool KallsymsLookupName::is_inited() {
return m_inited;
}
int KallsymsLookupName::get_kallsyms_num() {
return m_kallsyms_num;
}
bool KallsymsLookupName::find_kallsyms_addresses_list(size_t& start, size_t& end) {
const int var_len = sizeof(uint64_t);
for (auto x = 0; x + var_len < m_file_buf.size(); x += var_len) {
uint64_t val1 = *(uint64_t*)&m_file_buf[x];
uint64_t val2 = *(uint64_t*)&m_file_buf[x + var_len];
if (val1 != 0 || val1 >= val2) {
continue;
}
int cnt = 0;
auto j = x + var_len;
for (; j + var_len < m_file_buf.size(); j += var_len) {
val1 = *(uint64_t*)&m_file_buf[j];
val2 = *(uint64_t*)&m_file_buf[j + var_len];
if (val1 > val2 || val2 == 0 || val2 == 0x1000000000000000) {
j += var_len;
break;
}
cnt++;
}
if (cnt >= 0x10000) {
start = x;
end = j;
return true;
}
}
return false;
}
int KallsymsLookupName::find_kallsyms_num(size_t addresses_list_start, size_t addresses_list_end, size_t& kallsyms_num_offset) {
size_t size = (addresses_list_end - addresses_list_start) / sizeof(uint64_t);
size_t allow_min_size = size - 10;
size_t allow_max_size = size + 10;
auto _min = MIN(m_file_buf.size(), MAX_FIND_RANGE);
int cnt = 10;
for (size_t x = 0; (x + sizeof(int)) < _min; x++) {
auto pos = addresses_list_end + x * sizeof(int);
int val = *(int*)&m_file_buf[pos];
if (val == 0) {
continue;
}
if (val >= allow_min_size && val < allow_max_size) {
kallsyms_num_offset = pos;
return val;
}
if (--cnt == 0) {
break;
}
}
return 0;
}
bool KallsymsLookupName::find_kallsyms_names_list(int kallsyms_num, size_t kallsyms_num_end_offset, size_t& name_list_start, size_t& name_list_end) {
name_list_start = 0;
name_list_end = 0;
size_t x = kallsyms_num_end_offset;
auto _min = MIN(m_file_buf.size(), x + MAX_FIND_RANGE);
for (; (x + sizeof(char)) < _min; x++) {
char val = *(char*)&m_file_buf[x];
if (val == '\0') {
continue;
}
name_list_start = x;
break;
}
size_t off = name_list_start;
for (int i = 0; i < kallsyms_num; i++) {
unsigned char ch = (unsigned char)m_file_buf[off++];
off += ch;
}
name_list_end = off;
return true;
}
bool KallsymsLookupName::find_kallsyms_markers_list(int kallsyms_num, size_t name_list_end_offset, size_t& markers_list_start, size_t& markers_list_end) {
size_t start = align8(name_list_end_offset);
const int var_len = sizeof(long);
for (auto x = start; x + var_len < m_file_buf.size(); x += var_len) {
long val1 = *(long*)&m_file_buf[x];
long val2 = *(long*)&m_file_buf[x + var_len];
if (val1 == 0 && val2 > 0) {
markers_list_start = x;
break;
}
else if (val1 == 0 && val2 == 0) {
continue;
}
return false;
}
auto exist_val_start = markers_list_start + var_len;
bool is_align8 = false;
int cnt = 5;
long last_second_var_val = 0;
for (auto y = markers_list_start + var_len; y + var_len < m_file_buf.size(); y += var_len * 2) {
long val1 = *(long*)&m_file_buf[y];
long val2 = *(long*)&m_file_buf[y + var_len];
if (val2 != last_second_var_val) {
break;
}
last_second_var_val = val2;
cnt--;
if (cnt == 0) {
is_align8 = true;
break;
}
}
if (is_align8) {
size_t back_val = align8(markers_list_start) - markers_list_start;
if (back_val == 0) {
markers_list_start -= 8;
}
else {
markers_list_start -= back_val; // 4
}
markers_list_end = markers_list_start + ((kallsyms_num + 255) >> 8) * sizeof(long) * 2;
}
else {
markers_list_end = markers_list_start + ((kallsyms_num + 255) >> 8) * sizeof(long);
}
return true;
}
bool KallsymsLookupName::find_kallsyms_token_table(size_t markers_list_end_offset, size_t& kallsyms_token_table_start, size_t& kallsyms_token_table_end) {
size_t start = align8(markers_list_end_offset);
const int var_len = sizeof(long);
for (auto x = start; x + var_len < m_file_buf.size(); x += var_len) {
long val1 = *(long*)&m_file_buf[x];
if (val1 == 0) {
continue;
}
size_t off = x;
for (unsigned int i = 0; i < 256; i++) {
const char* str = (const char*)&m_file_buf[off];
off += strlen(str) + 1;
}
kallsyms_token_table_start = x;
kallsyms_token_table_end = off;
return true;
}
return false;
}
bool KallsymsLookupName::find_kallsyms_token_index(size_t kallsyms_token_table_end, size_t& kallsyms_token_index_start) {
size_t start = align8(kallsyms_token_table_end);
const int var_len = sizeof(short);
for (auto x = start; x + var_len < m_file_buf.size(); x += var_len) {
short val1 = *(short*)&m_file_buf[x];
short val2 = *(short*)&m_file_buf[x + var_len];
if (val1 == 0 && val2 > 0) {
kallsyms_token_index_start = x;
break;
}
else if (val1 == 0 && val2 == 0) {
continue;
}
return false;
}
return true;
}
bool KallsymsLookupName::find_kallsyms_sym_func_entry_offset(size_t& kallsyms_sym_func_entry_offset) {
size_t _text_offset = __kallsyms_lookup_name("_text");
if (_text_offset == 0) {
return false;
}
m_text_offset = _text_offset;
size_t _stext_offset = __kallsyms_lookup_name("_stext");
const int var_len = sizeof(int);
for (auto x = _stext_offset; x + var_len < m_file_buf.size(); x += var_len) {
int val1 = *(int*)&m_file_buf[x];
if (val1 == 0) {
continue;
}
kallsyms_sym_func_entry_offset = x - _stext_offset;
break;
}
return true;
}
/*
* Expand a compressed symbol data into the resulting uncompressed string,
* if uncompressed string is too long (>= maxlen), it will be truncated,
* given the offset to where the symbol is in the compressed stream.
*/
unsigned int KallsymsLookupName::kallsyms_expand_symbol(unsigned int off, char* result, size_t maxlen)
{
int len, skipped_first = 0;
const char* tptr;
const uint8_t* data;
/* Get the compressed symbol length from the first symbol byte. */
data = (uint8_t*)&m_file_buf[m_kallsyms_names.offset + off * sizeof(uint8_t)];
len = *data;
data++;
/*
* Update the offset to return the offset for the next symbol on
* the compressed stream.
*/
off += len + 1;
/*
* For every byte on the compressed symbol data, copy the table
* entry for that byte.
*/
while (len) {
uint8_t x = *data;
short y = *(short*)&m_file_buf[m_kallsyms_token_index.offset + x * sizeof(uint16_t)];
tptr = &m_file_buf[m_kallsyms_token_table.offset + y * sizeof(unsigned char)];
data++;
len--;
while (*tptr) {
if (skipped_first) {
if (maxlen <= 1)
goto tail;
*result = *tptr;
result++;
maxlen--;
}
else
skipped_first = 1;
tptr++;
}
}
tail:
if (maxlen)
*result = '\0';
/* Return to offset to the next symbol. */
return off;
}
/* Lookup the address for this symbol. Returns 0 if not found. */
uint64_t KallsymsLookupName::__kallsyms_lookup_name(const char* name, bool include_str_mode) {
for (auto i = 0, off = 0; i < m_kallsyms_num; i++) {
char namebuf[KSYM_NAME_LEN] = { 0 };
off = kallsyms_expand_symbol(off, namebuf, sizeof(namebuf));
//std::cout << namebuf << std::endl;
if (strcmp(namebuf, name) == 0 || (include_str_mode && strstr(namebuf, name))) {
auto pos = m_kallsyms_addresses.offset + i * sizeof(uint64_t);
uint64_t offset = *(uint64_t*)&m_file_buf[pos];
offset -= m_text_offset;
offset += m_kallsyms_sym_func_entry_offset;
return offset;
}
}
return 0;
}
uint64_t KallsymsLookupName::kallsyms_lookup_name(const char* name, bool include_str_mode) {
if (!m_inited) { return 0; }
return __kallsyms_lookup_name(name, include_str_mode);
}