|
23 | 23 | #include <stdbool.h>
|
24 | 24 | #include <stdio.h>
|
25 | 25 | #include <stdlib.h>
|
| 26 | +#include <string.h> |
26 | 27 | #include <elf.h>
|
27 | 28 |
|
28 | 29 | #include "procfs.h"
|
@@ -424,3 +425,85 @@ void free_symbol_table(struct symbol_table *table)
|
424 | 425 | {
|
425 | 426 | free(table);
|
426 | 427 | }
|
| 428 | + |
| 429 | +static Elf64_Word elf_hash(const char *name) |
| 430 | +{ |
| 431 | + Elf64_Word h = 0; |
| 432 | + |
| 433 | + while (*name) { |
| 434 | + h = (h << 4) + *name++; |
| 435 | + |
| 436 | + Elf64_Word g = h & 0xF0000000; |
| 437 | + |
| 438 | + if (g) |
| 439 | + h ^= g >> 24; |
| 440 | + |
| 441 | + h &= ~g; |
| 442 | + } |
| 443 | + |
| 444 | + return h; |
| 445 | +} |
| 446 | + |
| 447 | +static const char* symbol_name(const Elf64_Sym *symbol, |
| 448 | + struct symbol_table *symbols) |
| 449 | +{ |
| 450 | + if (!symbol->st_name) |
| 451 | + return ""; |
| 452 | + |
| 453 | + return &symbols->dynstr.strings[symbol->st_name]; |
| 454 | +} |
| 455 | + |
| 456 | +static unsigned long symbol_address(const char *name, |
| 457 | + const Elf64_Sym *symbol, struct symbol_table *symbols) |
| 458 | +{ |
| 459 | + uint8_t bind = ELF64_ST_BIND(symbol->st_info); |
| 460 | + uint8_t type = ELF64_ST_TYPE(symbol->st_info); |
| 461 | + |
| 462 | + if (symbol->st_shndx == STN_UNDEF) { |
| 463 | + fprintf(stderr, "[!] undefined symbol: %s\n", name); |
| 464 | + return 0; |
| 465 | + } |
| 466 | + |
| 467 | + if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) { |
| 468 | + fprintf(stderr, "[!] local symbol: %s\n", name); |
| 469 | + return 0; |
| 470 | + } |
| 471 | + |
| 472 | + if ((type != STT_FUNC) && (type != STT_OBJECT)) { |
| 473 | + fprintf(stderr, "[!] not a runtime object: %s\n", name); |
| 474 | + return 0; |
| 475 | + } |
| 476 | + |
| 477 | + return symbols->base_vaddr + symbol->st_value; |
| 478 | +} |
| 479 | + |
| 480 | +unsigned long resolve_symbol(const char *name, struct symbol_table *symbols) |
| 481 | +{ |
| 482 | + Elf64_Word nbucket = symbols->hash.table->nbucket; |
| 483 | + const Elf64_Word *buckets = &symbols->hash.table->entries[0]; |
| 484 | + const Elf64_Word *chains = &symbols->hash.table->entries[nbucket]; |
| 485 | + |
| 486 | + Elf64_Word hash = elf_hash(name); |
| 487 | + Elf64_Word bucket = hash % nbucket; |
| 488 | + Elf64_Word index = buckets[bucket]; |
| 489 | + |
| 490 | + for (;;) { |
| 491 | + if (index > symbols->dynsym.count) { |
| 492 | + fprintf(stderr, "[*] invalid hash index: %d (> %ld)\n", |
| 493 | + index, symbols->dynsym.count); |
| 494 | + return 0; |
| 495 | + } |
| 496 | + |
| 497 | + const Elf64_Sym *symbol = &symbols->dynsym.symbols[index]; |
| 498 | + |
| 499 | + if (strcmp(symbol_name(symbol, symbols), name) == 0) |
| 500 | + return symbol_address(name, symbol, symbols); |
| 501 | + |
| 502 | + index = chains[index]; |
| 503 | + |
| 504 | + if (index == STN_UNDEF) { |
| 505 | + fprintf(stderr, "[!] symbol not found: %s\n", name); |
| 506 | + return 0; |
| 507 | + } |
| 508 | + } |
| 509 | +} |
0 commit comments