-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsymbols_linux.cpp
551 lines (482 loc) · 16.1 KB
/
symbols_linux.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
* Copyright 2017 Andrei Pangin
*
* 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.
*/
#ifdef __linux__
#include "symbols_linux.h"
#include "dwarf.h"
#include "log.h"
#include "safeAccess.h"
#include "symbols.h"
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <link.h>
#include <linux/limits.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
ElfSection *ElfParser::findSection(uint32_t type, const char *name) {
const char *strtab = at(section(_header->e_shstrndx));
for (int i = 0; i < _header->e_shnum; i++) {
ElfSection *section = this->section(i);
if (section->sh_type == type && section->sh_name != 0) {
if (strcmp(strtab + section->sh_name, name) == 0) {
return section;
}
}
}
return NULL;
}
ElfProgramHeader *ElfParser::findProgramHeader(uint32_t type) {
const char *pheaders = (const char *)_header + _header->e_phoff;
for (int i = 0; i < _header->e_phnum; i++) {
const char *unvalidated_pheader = pheaders + i * _header->e_phentsize;
// check we can load the pointer
void *checked = SafeAccess::load((void **)unvalidated_pheader);
if (checked == NULL) {
return NULL;
} else {
ElfProgramHeader *pheader = (ElfProgramHeader *)unvalidated_pheader;
if (pheader->p_type == type) {
return pheader;
}
}
}
return NULL;
}
bool ElfParser::parseFile(CodeCache *cc, const char *base,
const char *file_name, bool use_debug) {
int fd = open(file_name, O_RDONLY);
if (fd == -1) {
return false;
}
size_t length = (size_t)lseek64(fd, 0, SEEK_END);
void *addr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (addr == MAP_FAILED) {
Log::warn("Could not parse symbols from %s: %s", file_name,
strerror(errno));
} else {
ElfParser elf(cc, base != nullptr ? base : (const char *)addr, addr,
file_name, length, false);
if (elf.validHeader()) {
elf.loadSymbols(use_debug);
}
munmap(addr, length);
}
return true;
}
void ElfParser::parseProgramHeaders(CodeCache *cc, const char *base,
const char *end, bool relocate_dyn) {
ElfParser elf(cc, base, base, NULL, 0, relocate_dyn);
if (elf.validHeader() && base + elf._header->e_phoff < end) {
cc->setTextBase(base);
elf.calcVirtualLoadAddress();
elf.parseDynamicSection();
elf.parseDwarfInfo();
}
}
void ElfParser::calcVirtualLoadAddress() {
// Find a difference between the virtual load address (often zero) and the
// actual DSO base
const char *pheaders = (const char *)_header + _header->e_phoff;
for (int i = 0; i < _header->e_phnum; i++) {
ElfProgramHeader *pheader =
(ElfProgramHeader *)(pheaders + i * _header->e_phentsize);
if (pheader->p_type == PT_LOAD) {
_vaddr_diff = _base - pheader->p_vaddr;
return;
}
}
_vaddr_diff = _base;
}
void ElfParser::parseDynamicSection() {
ElfProgramHeader *dynamic = findProgramHeader(PT_DYNAMIC);
if (dynamic != NULL) {
const char *symtab = NULL;
const char *strtab = NULL;
char *jmprel = NULL;
char *rel = NULL;
size_t pltrelsz = 0;
size_t relsz = 0;
size_t relent = 0;
size_t relcount = 0;
size_t syment = 0;
uint32_t nsyms = 0;
const char *dyn_start = at(dynamic);
const char *dyn_end = dyn_start + dynamic->p_memsz;
for (ElfDyn *dyn = (ElfDyn *)dyn_start; dyn < (ElfDyn *)dyn_end; dyn++) {
switch (dyn->d_tag) {
case DT_SYMTAB:
symtab = dyn_ptr(dyn);
break;
case DT_STRTAB:
strtab = dyn_ptr(dyn);
break;
case DT_SYMENT:
syment = dyn->d_un.d_val;
break;
case DT_HASH:
nsyms = ((uint32_t *)dyn_ptr(dyn))[1];
break;
case DT_GNU_HASH:
if (nsyms == 0) {
nsyms = getSymbolCount((uint32_t *)dyn_ptr(dyn));
}
break;
case DT_JMPREL:
jmprel = dyn_ptr(dyn);
break;
case DT_PLTRELSZ:
pltrelsz = dyn->d_un.d_val;
break;
case DT_RELA:
case DT_REL:
rel = dyn_ptr(dyn);
break;
case DT_RELASZ:
case DT_RELSZ:
relsz = dyn->d_un.d_val;
break;
case DT_RELAENT:
case DT_RELENT:
relent = dyn->d_un.d_val;
break;
case DT_RELACOUNT:
case DT_RELCOUNT:
relcount = dyn->d_un.d_val;
break;
}
}
if (symtab == NULL || strtab == NULL || syment == 0 || nsyms == 0 ||
relent == 0) {
return;
}
if (!_cc->hasDebugSymbols()) {
loadSymbolTable(symtab, syment * nsyms, syment, strtab);
}
if (jmprel != NULL && pltrelsz != 0) {
// Parse .rela.plt table
for (size_t offs = 0; offs < pltrelsz; offs += relent) {
ElfRelocation *r = (ElfRelocation *)(jmprel + offs);
ElfSymbol *sym = (ElfSymbol *)(symtab + ELF_R_SYM(r->r_info) * syment);
if (sym->st_name != 0) {
_cc->addImport((void **)(_base + r->r_offset), strtab + sym->st_name);
}
}
} else if (rel != NULL && relsz != 0) {
// Shared library was built without PLT (-fno-plt)
// Relocation entries have been moved from .rela.plt to .rela.dyn
for (size_t offs = relcount * relent; offs < relsz; offs += relent) {
ElfRelocation *r = (ElfRelocation *)(rel + offs);
if (ELF_R_TYPE(r->r_info) == R_GLOB_DAT) {
ElfSymbol *sym =
(ElfSymbol *)(symtab + ELF_R_SYM(r->r_info) * syment);
if (sym->st_name != 0) {
_cc->addImport((void **)(_base + r->r_offset),
strtab + sym->st_name);
}
}
}
}
}
}
void ElfParser::parseDwarfInfo() {
if (!DWARF_SUPPORTED)
return;
ElfProgramHeader *eh_frame_hdr = findProgramHeader(PT_GNU_EH_FRAME);
if (eh_frame_hdr != NULL) {
if (eh_frame_hdr->p_vaddr != 0) {
DwarfParser dwarf(_cc->name(), _base, at(eh_frame_hdr));
_cc->setDwarfTable(dwarf.table(), dwarf.count());
} else if (strcmp(_cc->name(), "[vdso]") == 0) {
FrameDesc *table = (FrameDesc *)malloc(sizeof(FrameDesc));
*table = FrameDesc::empty_frame;
_cc->setDwarfTable(table, 1);
}
}
}
uint32_t ElfParser::getSymbolCount(uint32_t *gnu_hash) {
uint32_t nbuckets = gnu_hash[0];
uint32_t *buckets = &gnu_hash[4] + gnu_hash[2] * (sizeof(size_t) / 4);
uint32_t nsyms = 0;
for (uint32_t i = 0; i < nbuckets; i++) {
if (buckets[i] > nsyms)
nsyms = buckets[i];
}
if (nsyms > 0) {
uint32_t *chain = &buckets[nbuckets] - gnu_hash[1];
while (!(chain[nsyms++] & 1))
;
}
return nsyms;
}
void ElfParser::loadSymbols(bool use_debug) {
ElfSection *symtab = findSection(SHT_SYMTAB, ".symtab");
if (symtab != NULL) {
// Parse debug symbols from the original .so
ElfSection *strtab = section(symtab->sh_link);
loadSymbolTable(at(symtab), symtab->sh_size, symtab->sh_entsize,
at(strtab));
_cc->setDebugSymbols(true);
} else if (use_debug) {
// Try to load symbols from an external debuginfo library
loadSymbolsUsingBuildId() || loadSymbolsUsingDebugLink();
}
if (use_debug) {
// Synthesize names for PLT stubs
ElfSection *plt = findSection(SHT_PROGBITS, ".plt");
if (plt != NULL) {
_cc->setPlt(plt->sh_addr, plt->sh_size);
ElfSection *reltab = findSection(SHT_RELA, ".rela.plt");
if (reltab != NULL ||
(reltab = findSection(SHT_REL, ".rel.plt")) != NULL) {
addRelocationSymbols(reltab, _base + plt->sh_addr + PLT_HEADER_SIZE);
}
}
}
}
// Load symbols from /usr/lib/debug/.build-id/ab/cdef1234.debug, where
// abcdef1234 is Build ID
bool ElfParser::loadSymbolsUsingBuildId() {
ElfSection *section = findSection(SHT_NOTE, ".note.gnu.build-id");
if (section == NULL || section->sh_size <= 16) {
return false;
}
ElfNote *note = (ElfNote *)at(section);
if (note->n_namesz != 4 || note->n_descsz < 2 || note->n_descsz > 64) {
return false;
}
const char *build_id = (const char *)note + sizeof(*note) + 4;
int build_id_len = note->n_descsz;
char path[PATH_MAX];
char *p = path + sprintf(path, "/usr/lib/debug/.build-id/%02hhx/",
(unsigned char)build_id[0]);
for (int i = 1; i < build_id_len; i++) {
p += sprintf(p, "%02hhx", (unsigned char)build_id[i]);
}
strcpy(p, ".debug");
return parseFile(_cc, _base, path, false);
}
// Look for debuginfo file specified in .gnu_debuglink section
bool ElfParser::loadSymbolsUsingDebugLink() {
ElfSection *section = findSection(SHT_PROGBITS, ".gnu_debuglink");
if (section == NULL || section->sh_size <= 4) {
return false;
}
const char *basename = strrchr(_file_name, '/');
if (basename == NULL) {
return false;
}
char *dirname = strndup(_file_name, basename - _file_name);
if (dirname == NULL) {
return false;
}
const char *debuglink = at(section);
char path[PATH_MAX];
bool result = false;
// 1. /path/to/libjvm.so.debug
if (strcmp(debuglink, basename + 1) != 0 &&
snprintf(path, PATH_MAX, "%s/%s", dirname, debuglink) < PATH_MAX) {
result = parseFile(_cc, _base, path, false);
}
// 2. /path/to/.debug/libjvm.so.debug
if (!result &&
snprintf(path, PATH_MAX, "%s/.debug/%s", dirname, debuglink) < PATH_MAX) {
result = parseFile(_cc, _base, path, false);
}
// 3. /usr/lib/debug/path/to/libjvm.so.debug
if (!result && snprintf(path, PATH_MAX, "/usr/lib/debug%s/%s", dirname,
debuglink) < PATH_MAX) {
result = parseFile(_cc, _base, path, false);
}
free(dirname);
return result;
}
void ElfParser::loadSymbolTable(const char *symbols, size_t total_size,
size_t ent_size, const char *strings) {
for (const char *symbols_end = symbols + total_size; symbols < symbols_end;
symbols += ent_size) {
ElfSymbol *sym = (ElfSymbol *)symbols;
if (sym->st_name != 0 && sym->st_value != 0) {
// sanity check the offsets not to exceed the file size
if (_length == 0 || (sym->st_name < _length && sym->st_value < _length)) {
// Skip special AArch64 mapping symbols: $x and $d
if (sym->st_size != 0 || sym->st_info != 0 ||
strings[sym->st_name] != '$') {
_cc->add(_base + sym->st_value, (int)sym->st_size,
strings + sym->st_name);
}
}
}
}
}
void ElfParser::addRelocationSymbols(ElfSection *reltab, const char *plt) {
ElfSection *symtab = section(reltab->sh_link);
const char *symbols = at(symtab);
ElfSection *strtab = section(symtab->sh_link);
const char *strings = at(strtab);
const char *relocations = at(reltab);
const char *relocations_end = relocations + reltab->sh_size;
for (; relocations < relocations_end; relocations += reltab->sh_entsize) {
ElfRelocation *r = (ElfRelocation *)relocations;
ElfSymbol *sym =
(ElfSymbol *)(symbols + ELF_R_SYM(r->r_info) * symtab->sh_entsize);
char name[256];
if (sym->st_name == 0) {
strcpy(name, "@plt");
} else {
const char *sym_name = strings + sym->st_name;
snprintf(name, sizeof(name), "%s%cplt", sym_name,
sym_name[0] == '_' && sym_name[1] == 'Z' ? '.' : '@');
name[sizeof(name) - 1] = 0;
}
_cc->add(plt, PLT_ENTRY_SIZE, name);
plt += PLT_ENTRY_SIZE;
}
}
Mutex Symbols::_parse_lock;
bool Symbols::_have_kernel_symbols = false;
static std::set<const void *> _parsed_libraries;
static std::set<u64> _parsed_inodes;
void Symbols::clearParsingCaches() {
_parsed_libraries.clear();
_parsed_inodes.clear();
}
void Symbols::parseKernelSymbols(CodeCache *cc) {
int fd = open("/proc/kallsyms", O_RDONLY);
if (fd == -1) {
Log::warn("open(\"/proc/kallsyms\"): %s", strerror(errno));
return;
}
FILE *f = fdopen(fd, "r");
if (f == NULL) {
Log::warn("fdopen(): %s", strerror(errno));
close(fd);
return;
}
char str[256];
while (fgets(str, sizeof(str) - 8, f) != NULL) {
size_t len = strlen(str) - 1; // trim the '\n'
strcpy(str + len, "_[k]");
SymbolDesc symbol(str);
char type = symbol.type();
if (type == 'T' || type == 't' || type == 'W' || type == 'w') {
const char *addr = symbol.addr();
if (addr != NULL) {
if (!_have_kernel_symbols) {
if (strncmp(symbol.name(), "__LOAD_PHYSICAL_ADDR", 20) == 0 ||
strncmp(symbol.name(), "phys_startup", 12) == 0) {
continue;
}
_have_kernel_symbols = true;
}
cc->add(addr, 0, symbol.name());
}
}
}
fclose(f);
}
static int parseLibrariesCallback(struct dl_phdr_info *info, size_t size,
void *data) {
FILE *f = fopen("/proc/self/maps", "r");
if (f == NULL) {
return 1;
}
CodeCacheArray *array = (CodeCacheArray *)data;
const char *image_base = NULL;
u64 last_inode = 0;
char *str = NULL;
size_t str_size = 0;
ssize_t len;
while ((len = getline(&str, &str_size, f)) > 0) {
str[len - 1] = 0;
MemoryMapDesc map(str);
if (!map.isReadable() || map.file() == NULL || map.file()[0] == 0) {
continue;
}
if (strchr(map.file(), ':') != NULL) {
// Skip pseudofiles like anon_inode:name, /memfd:name
continue;
}
const char *map_start = map.addr();
unsigned long map_offs = map.offs();
if (map_offs == 0) {
image_base = map_start;
last_inode = u64(map.dev()) << 32 | map.inode();
}
if (!map.isExecutable() || !_parsed_libraries.insert(map_start).second) {
// Not an executable segment or it has been already parsed
continue;
}
int count = array->count();
if (count >= MAX_NATIVE_LIBS) {
break;
}
const char *map_end = map.end();
CodeCache *cc = new CodeCache(map.file(), count, false, map_start, map_end);
// Do not try to parse pseudofiles like anon_inode:name, /memfd:name
if (strchr(map.file(), ':') == NULL) {
u64 inode = u64(map.dev()) << 32 | map.inode();
if (inode != 0) {
// Do not parse the same executable twice, e.g. on Alpine Linux
if (_parsed_inodes.insert(inode).second) {
if (inode == last_inode) {
// If last_inode is set, image_base is known to be valid and
// readable
ElfParser::parseFile(cc, image_base, map.file(), true);
// Parse program headers after the file to ensure debug symbols are
// parsed first
ElfParser::parseProgramHeaders(cc, image_base, map_end, MUSL);
} else if ((unsigned long)map_start > map_offs) {
// Unlikely case when image_base has not been found.
// Be careful: executable file is not always ELF, e.g. classes.jsa
ElfParser::parseFile(cc, map_start - map_offs, map.file(), true);
}
}
} else if (strcmp(map.file(), "[vdso]") == 0) {
ElfParser::parseProgramHeaders(cc, map_start, map_end, true);
}
}
cc->sort();
array->add(cc);
}
free(str);
fclose(f);
return 1; // stop at first iteration
}
void Symbols::parseLibraries(CodeCacheArray *array, bool kernel_symbols) {
MutexLocker ml(_parse_lock);
if (kernel_symbols && !haveKernelSymbols()) {
CodeCache *cc = new CodeCache("[kernel]");
parseKernelSymbols(cc);
if (haveKernelSymbols()) {
cc->sort();
array->add(cc);
} else {
delete cc;
}
}
// In glibc, dl_iterate_phdr() holds dl_load_write_lock, therefore preventing
// concurrent loading and unloading of shared libraries.
// Without it, we may access memory of a library that is being unloaded.
dl_iterate_phdr(parseLibrariesCallback, array);
}
#endif // __linux__