List symbols from symtab

This commit is contained in:
2025-03-10 12:36:21 +01:00
parent 31f6fa1cc1
commit 6687646a53
3 changed files with 112 additions and 2 deletions

View File

@@ -6,6 +6,9 @@
#include <inttypes.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <libelf.h>
#include <gelf.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
@@ -16,6 +19,7 @@
#include "mujs.h"
#include "hash.h"
#include "pmparser.h"
#include "da.h"
#define LOG_ERR(fmt, ...) fprintf(stderr, "Error: " fmt, ##__VA_ARGS__)
#define LOG_INF(fmt, ...) fprintf(stdout, "Info: " fmt, ##__VA_ARGS__)
@@ -139,12 +143,23 @@ void brk_disable(Brk *brk)
brk->enabled = false;
}
typedef struct {
const char *name;
uintptr_t addr;
} Symbol;
typedef struct {
Symbol *items;
size_t count, capacity;
} Symbols;
typedef struct {
const char *file;
pid_t pid;
js_State *js;
HashTable brks;
uintptr_t program_load_offset;
Symbols symbols;
} Dbg;
void dbg_wait(Dbg *dbg)
@@ -395,6 +410,16 @@ void dbg_js_mem_write(js_State *js)
js_pushundefined(js);
}
void dbg_js_list_syms(js_State *js)
{
Dbg *dbg = getdbg();
for (int i = 0; i < dbg->symbols.count; i++) {
Symbol *sym = &dbg->symbols.items[i];
LOG_INF("Symbol %s at %"PRIxPTR"\n", sym->name, sym->addr);
}
js_pushundefined(js);
}
void dbg_init_js(Dbg *dbg)
{
dbg->js = js_newstate(NULL, NULL, JS_STRICT);
@@ -420,6 +445,7 @@ void dbg_init_js(Dbg *dbg)
make_js_func(set_reg, 2 /* reg name, value*/);
make_js_func(mem_read, 1 /*addr*/);
make_js_func(mem_write, 2 /*addr, value*/);
make_js_func(list_syms, 0);
#undef make_js_func
}
@@ -459,9 +485,65 @@ void dbg_load_script(Dbg *dbg, const char *script_path)
js_dostring(dbg->js, script_buf);
free(script_buf);
fclose(script);
}
void dbg_load_symbols(Dbg *dbg)
{
FILE *bin = fopen(dbg->file, "rb");
if (bin == NULL) {
LOG_ERR("Cannot load symbols from file\n");
return;
}
Elf *elf;
Elf_Scn *scn = NULL;
GElf_Shdr shdr;
Elf_Data *data;
GElf_Sym sym;
elf_version(EV_CURRENT);
elf = elf_begin(fileno(bin), ELF_C_READ, NULL);
if (elf == NULL) {
LOG_ERR("Could not parse elf\n");
fclose(bin);
return;
}
while ((scn = elf_nextscn(elf, scn)) != NULL) {
gelf_getshdr(scn, &shdr);
if (shdr.sh_type == SHT_SYMTAB) {
break;
}
}
data = elf_getdata(scn, NULL);
int count = shdr.sh_size / shdr.sh_entsize;
for (int i = 0; i < count; i++) {
gelf_getsym(data, i, &sym);
if (sym.st_info != 2 /*func*/) {
char *name = elf_strptr(elf, shdr.sh_link, sym.st_name);
size_t len = strlen(name);
if (len == 0 || sym.st_value == 0x0) {
continue;
}
char *n = malloc(len+1);
strcpy(n, name);
n[len] = '\0';
Symbol symbol = { .name = name, .addr = dbg->program_load_offset + (uintptr_t)sym.st_value };
da_append(&dbg->symbols, symbol);
}
}
elf_end(elf);
fclose(bin);
}
void dbg_init(Dbg *dbg, const char *file, pid_t pid)
{
memset(dbg, 0, sizeof(*dbg));
@@ -470,7 +552,7 @@ void dbg_init(Dbg *dbg, const char *file, pid_t pid)
dbg_init_js(dbg);
dbg_init_load_offset(dbg);
hashtable_init(&dbg->brks, MAX_BRKS);
dbg_load_symbols(dbg);
dbg_load_script(dbg, INIT_SCRIPT);
}
@@ -478,6 +560,7 @@ void dbg_deinit(Dbg *dbg)
{
js_freestate(dbg->js);
hashtable_deinit(&dbg->brks);
da_deinit(&dbg->symbols);
}
void dbg_loop(Dbg *dbg)