Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.hotspot.agent/macosx/native/libsaproc/symtab.c
41152 views
1
/*
2
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#include <unistd.h>
26
#include <search.h>
27
#include <stddef.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include <db.h>
31
#include <fcntl.h>
32
33
#include "libproc_impl.h"
34
#include "symtab.h"
35
#ifndef __APPLE__
36
#include "salibelf.h"
37
#endif // __APPLE__
38
39
40
// ----------------------------------------------------
41
// functions for symbol lookups
42
// ----------------------------------------------------
43
44
typedef struct symtab_symbol {
45
char *name; // name like __ZThread_...
46
uintptr_t offset; // to loaded address
47
uintptr_t size; // size strlen
48
} symtab_symbol;
49
50
typedef struct symtab {
51
char *strs; // all symbols "__symbol1__'\0'__symbol2__...."
52
size_t num_symbols;
53
DB* hash_table;
54
symtab_symbol* symbols;
55
} symtab_t;
56
57
#ifdef __APPLE__
58
59
void build_search_table(symtab_t *symtab) {
60
int i;
61
print_debug("build_search_table\n");
62
for (i = 0; i < symtab->num_symbols; i++) {
63
DBT key, value;
64
key.data = symtab->symbols[i].name;
65
key.size = strlen(key.data) + 1;
66
value.data = &(symtab->symbols[i]);
67
value.size = sizeof(symtab_symbol);
68
//print_debug("build_search_table: %d 0x%x %s\n", i, symtab->symbols[i].offset, symtab->symbols[i].name);
69
(*symtab->hash_table->put)(symtab->hash_table, &key, &value, 0);
70
71
// check result
72
if (is_debug()) {
73
DBT rkey, rvalue;
74
char* tmp = (char *)malloc(strlen(symtab->symbols[i].name) + 1);
75
if (tmp == NULL) {
76
print_debug("error allocating array in build_search_table\n");
77
} else {
78
strcpy(tmp, symtab->symbols[i].name);
79
rkey.data = tmp;
80
rkey.size = strlen(tmp) + 1;
81
(*symtab->hash_table->get)(symtab->hash_table, &rkey, &rvalue, 0);
82
// we may get a copy back so compare contents
83
symtab_symbol *res = (symtab_symbol *)rvalue.data;
84
if (strcmp(res->name, symtab->symbols[i].name) ||
85
res->offset != symtab->symbols[i].offset ||
86
res->size != symtab->symbols[i].size) {
87
print_debug("error to get hash_table value!\n");
88
}
89
free(tmp);
90
}
91
}
92
}
93
}
94
95
// read symbol table from given fd.
96
struct symtab* build_symtab(int fd, size_t *p_max_offset) {
97
symtab_t* symtab = NULL;
98
int i, j;
99
mach_header_64 header;
100
off_t image_start;
101
size_t max_offset = 0;
102
103
print_debug("build_symtab\n");
104
if (!get_arch_off(fd, CPU_TYPE_X86_64, &image_start)) {
105
print_debug("failed in get fat header\n");
106
return NULL;
107
}
108
lseek(fd, image_start, SEEK_SET);
109
if (read(fd, (void *)&header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
110
print_debug("reading header failed!\n");
111
return NULL;
112
}
113
// header
114
if (header.magic != MH_MAGIC_64) {
115
print_debug("not a valid .dylib file\n");
116
return NULL;
117
}
118
119
load_command lcmd;
120
symtab_command symtabcmd;
121
nlist_64 lentry;
122
123
bool lcsymtab_exist = false;
124
125
long filepos = ltell(fd);
126
for (i = 0; i < header.ncmds; i++) {
127
lseek(fd, filepos, SEEK_SET);
128
if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
129
print_debug("read load_command failed for file\n");
130
return NULL;
131
}
132
filepos += lcmd.cmdsize; // next command position
133
if (lcmd.cmd == LC_SYMTAB) {
134
lseek(fd, -sizeof(load_command), SEEK_CUR);
135
lcsymtab_exist = true;
136
break;
137
}
138
}
139
if (!lcsymtab_exist) {
140
print_debug("No symtab command found!\n");
141
return NULL;
142
}
143
if (read(fd, (void *)&symtabcmd, sizeof(symtab_command)) != sizeof(symtab_command)) {
144
print_debug("read symtab_command failed for file");
145
return NULL;
146
}
147
symtab = (symtab_t *)malloc(sizeof(symtab_t));
148
if (symtab == NULL) {
149
print_debug("out of memory: allocating symtab\n");
150
return NULL;
151
}
152
153
// create hash table, we use berkeley db to
154
// manipulate the hash table.
155
symtab->hash_table = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL);
156
if (symtab->hash_table == NULL)
157
goto quit;
158
159
// allocate the symtab
160
symtab->num_symbols = symtabcmd.nsyms;
161
symtab->symbols = (symtab_symbol *)malloc(sizeof(symtab_symbol) * symtab->num_symbols);
162
symtab->strs = (char *)malloc(sizeof(char) * symtabcmd.strsize);
163
if (symtab->symbols == NULL || symtab->strs == NULL) {
164
print_debug("out of memory: allocating symtab.symbol or symtab.strs\n");
165
goto quit;
166
}
167
168
// read in the string table
169
lseek(fd, image_start + symtabcmd.stroff, SEEK_SET);
170
int size = read(fd, (void *)(symtab->strs), symtabcmd.strsize * sizeof(char));
171
if (size != symtabcmd.strsize * sizeof(char)) {
172
print_debug("reading string table failed\n");
173
goto quit;
174
}
175
176
// read in each nlist_64 from the symbol table and use to fill in symtab->symbols
177
lseek(fd, image_start + symtabcmd.symoff, SEEK_SET);
178
i = 0;
179
for (j = 0; j < symtab->num_symbols; j++) {
180
if (read(fd, (void *)&lentry, sizeof(nlist_64)) != sizeof(nlist_64)) {
181
print_debug("read nlist_64 failed at %j\n", j);
182
goto quit;
183
}
184
185
uintptr_t offset = lentry.n_value; // offset of the symbol code/data in the file
186
uintptr_t stridx = lentry.n_un.n_strx; // offset of symbol string in the symtabcmd.symoff section
187
188
if (stridx == 0 || offset == 0) {
189
continue; // Skip this entry. It's not a reference to code or data
190
}
191
if (lentry.n_type == N_OSO) {
192
// This is an object file name/path. These entries have something other than
193
// an offset in lentry.n_value, so we need to ignore them.
194
continue;
195
}
196
symtab->symbols[i].offset = offset;
197
symtab->symbols[i].name = symtab->strs + stridx;
198
symtab->symbols[i].size = strlen(symtab->symbols[i].name);
199
200
if (symtab->symbols[i].size == 0) {
201
continue; // Skip this entry. It points to an empty string.
202
}
203
204
// Track the maximum offset we've seen. This is used to determine the address range
205
// that the library covers.
206
if (offset > max_offset) {
207
max_offset = (offset + 4096) & ~0xfff; // Round up to next page boundary
208
}
209
print_debug("symbol read: %d %d n_type=0x%x n_sect=0x%x n_desc=0x%x n_strx=0x%lx offset=0x%lx %s\n",
210
j, i, lentry.n_type, lentry.n_sect, lentry.n_desc, stridx, offset, symtab->symbols[i].name);
211
i++;
212
}
213
214
// Update symtab->num_symbols to be the actual number of symbols we added. Since the symbols
215
// array was allocated larger, reallocate it to the proper size.
216
print_debug("build_symtab: included %d of %d entries.\n", i, symtab->num_symbols);
217
symtab->num_symbols = i;
218
symtab->symbols = (symtab_symbol *)realloc(symtab->symbols, sizeof(symtab_symbol) * symtab->num_symbols);
219
if (symtab->symbols == NULL) {
220
print_debug("out of memory: reallocating symtab.symbol\n");
221
goto quit;
222
}
223
224
// build a hashtable for fast query
225
build_search_table(symtab);
226
*p_max_offset = max_offset;
227
return symtab;
228
quit:
229
if (symtab) destroy_symtab(symtab);
230
return NULL;
231
}
232
233
#else // __APPLE__
234
235
struct elf_section {
236
ELF_SHDR *c_shdr;
237
void *c_data;
238
};
239
240
// read symbol table from given fd.
241
struct symtab* build_symtab(int fd) {
242
ELF_EHDR ehdr;
243
struct symtab* symtab = NULL;
244
245
// Reading of elf header
246
struct elf_section *scn_cache = NULL;
247
int cnt = 0;
248
ELF_SHDR* shbuf = NULL;
249
ELF_SHDR* cursct = NULL;
250
ELF_PHDR* phbuf = NULL;
251
int symtab_found = 0;
252
int dynsym_found = 0;
253
uint32_t symsection = SHT_SYMTAB;
254
255
uintptr_t baseaddr = (uintptr_t)-1;
256
257
lseek(fd, (off_t)0L, SEEK_SET);
258
if (! read_elf_header(fd, &ehdr)) {
259
// not an elf
260
return NULL;
261
}
262
263
// read ELF header
264
if ((shbuf = read_section_header_table(fd, &ehdr)) == NULL) {
265
goto quit;
266
}
267
268
baseaddr = find_base_address(fd, &ehdr);
269
270
scn_cache = calloc(ehdr.e_shnum, sizeof(*scn_cache));
271
if (scn_cache == NULL) {
272
goto quit;
273
}
274
275
for (cursct = shbuf, cnt = 0; cnt < ehdr.e_shnum; cnt++) {
276
scn_cache[cnt].c_shdr = cursct;
277
if (cursct->sh_type == SHT_SYMTAB ||
278
cursct->sh_type == SHT_STRTAB ||
279
cursct->sh_type == SHT_DYNSYM) {
280
if ( (scn_cache[cnt].c_data = read_section_data(fd, &ehdr, cursct)) == NULL) {
281
goto quit;
282
}
283
}
284
285
if (cursct->sh_type == SHT_SYMTAB)
286
symtab_found++;
287
288
if (cursct->sh_type == SHT_DYNSYM)
289
dynsym_found++;
290
291
cursct++;
292
}
293
294
if (!symtab_found && dynsym_found)
295
symsection = SHT_DYNSYM;
296
297
for (cnt = 1; cnt < ehdr.e_shnum; cnt++) {
298
ELF_SHDR *shdr = scn_cache[cnt].c_shdr;
299
300
if (shdr->sh_type == symsection) {
301
ELF_SYM *syms;
302
int j, n;
303
size_t size;
304
305
// FIXME: there could be multiple data buffers associated with the
306
// same ELF section. Here we can handle only one buffer. See man page
307
// for elf_getdata on Solaris.
308
309
// guarantee(symtab == NULL, "multiple symtab");
310
symtab = calloc(1, sizeof(*symtab));
311
if (symtab == NULL) {
312
goto quit;
313
}
314
// the symbol table
315
syms = (ELF_SYM *)scn_cache[cnt].c_data;
316
317
// number of symbols
318
n = shdr->sh_size / shdr->sh_entsize;
319
320
// create hash table, we use berkeley db to
321
// manipulate the hash table.
322
symtab->hash_table = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL);
323
// guarantee(symtab->hash_table, "unexpected failure: dbopen");
324
if (symtab->hash_table == NULL)
325
goto bad;
326
327
// shdr->sh_link points to the section that contains the actual strings
328
// for symbol names. the st_name field in ELF_SYM is just the
329
// string table index. we make a copy of the string table so the
330
// strings will not be destroyed by elf_end.
331
size = scn_cache[shdr->sh_link].c_shdr->sh_size;
332
symtab->strs = malloc(size);
333
if (symtab->strs == NULL)
334
goto bad;
335
memcpy(symtab->strs, scn_cache[shdr->sh_link].c_data, size);
336
337
// allocate memory for storing symbol offset and size;
338
symtab->num_symbols = n;
339
symtab->symbols = calloc(n , sizeof(*symtab->symbols));
340
if (symtab->symbols == NULL)
341
goto bad;
342
343
// copy symbols info our symtab and enter them info the hash table
344
for (j = 0; j < n; j++, syms++) {
345
DBT key, value;
346
char *sym_name = symtab->strs + syms->st_name;
347
348
// skip non-object and non-function symbols
349
int st_type = ELF_ST_TYPE(syms->st_info);
350
if ( st_type != STT_FUNC && st_type != STT_OBJECT)
351
continue;
352
// skip empty strings and undefined symbols
353
if (*sym_name == '\0' || syms->st_shndx == SHN_UNDEF) continue;
354
355
symtab->symbols[j].name = sym_name;
356
symtab->symbols[j].offset = syms->st_value - baseaddr;
357
symtab->symbols[j].size = syms->st_size;
358
359
key.data = sym_name;
360
key.size = strlen(sym_name) + 1;
361
value.data = &(symtab->symbols[j]);
362
value.size = sizeof(symtab_symbol);
363
(*symtab->hash_table->put)(symtab->hash_table, &key, &value, 0);
364
}
365
}
366
}
367
goto quit;
368
369
bad:
370
destroy_symtab(symtab);
371
symtab = NULL;
372
373
quit:
374
if (shbuf) free(shbuf);
375
if (phbuf) free(phbuf);
376
if (scn_cache) {
377
for (cnt = 0; cnt < ehdr.e_shnum; cnt++) {
378
if (scn_cache[cnt].c_data != NULL) {
379
free(scn_cache[cnt].c_data);
380
}
381
}
382
free(scn_cache);
383
}
384
return symtab;
385
}
386
387
#endif // __APPLE__
388
389
void destroy_symtab(symtab_t* symtab) {
390
if (!symtab) return;
391
free(symtab->strs);
392
free(symtab->symbols);
393
free(symtab);
394
}
395
396
uintptr_t search_symbol(struct symtab* symtab, uintptr_t base, const char *sym_name, int *sym_size) {
397
DBT key, value;
398
int ret;
399
400
// library does not have symbol table
401
if (!symtab || !symtab->hash_table) {
402
return 0;
403
}
404
405
key.data = (char*)(uintptr_t)sym_name;
406
key.size = strlen(sym_name) + 1;
407
ret = (*symtab->hash_table->get)(symtab->hash_table, &key, &value, 0);
408
if (ret == 0) {
409
symtab_symbol *sym = value.data;
410
uintptr_t rslt = (uintptr_t) ((char*)base + sym->offset);
411
if (sym_size) *sym_size = sym->size;
412
return rslt;
413
}
414
415
return 0;
416
}
417
418
const char* nearest_symbol(struct symtab* symtab, uintptr_t offset,
419
uintptr_t* poffset) {
420
int n = 0;
421
char* result = NULL;
422
ptrdiff_t lowest_offset_from_sym = -1;
423
if (!symtab) return NULL;
424
// Search the symbol table for the symbol that is closest to the specified offset, but is not under.
425
//
426
// Note we can't just use the first symbol that is >= the offset because the symbols may not be
427
// sorted by offset.
428
//
429
// Note this is a rather slow search that is O(n/2), and libjvm has as many as 250k symbols.
430
// Probably would be good to sort the array and do a binary search, or use a hash table like
431
// we do for name -> address lookups. However, this functionality is not used often and
432
// generally just involves one lookup, such as with the clhsdb "findpc" command.
433
for (; n < symtab->num_symbols; n++) {
434
symtab_symbol* sym = &(symtab->symbols[n]);
435
if (sym->size != 0 && offset >= sym->offset) {
436
ptrdiff_t offset_from_sym = offset - sym->offset;
437
if (offset_from_sym >= 0) { // ignore symbols that come after "offset"
438
if (lowest_offset_from_sym == -1 || offset_from_sym < lowest_offset_from_sym) {
439
lowest_offset_from_sym = offset_from_sym;
440
result = sym->name;
441
//print_debug("nearest_symbol: found %d %s 0x%x 0x%x 0x%x\n",
442
// n, sym->name, offset, sym->offset, lowest_offset_from_sym);
443
}
444
}
445
}
446
}
447
print_debug("nearest_symbol: found symbol %d file_offset=0x%lx sym_offset=0x%lx %s\n",
448
n, offset, lowest_offset_from_sym, result);
449
// Save the offset from the symbol if requested.
450
if (result != NULL && poffset) {
451
*poffset = lowest_offset_from_sym;
452
}
453
return result;
454
}
455
456