dwarfsym.c (1638B)
1 #include "dwarfsym.h" 2 3 #include <string.h> 4 5 #include "driver.h" 6 7 int driver_dwarfsym_open(DriverDwarfSym* s, DriverEnv* env, const char* tool, 8 const char* path) { 9 KitSlice input; 10 11 memset(s, 0, sizeof *s); 12 s->env = env; 13 s->ctx = driver_env_to_context(env); 14 15 if (driver_load_bytes(&env->file_io, tool, path, &s->ld, &input) != 0) 16 return 1; 17 18 { 19 KitSlice name_slice = kit_slice_cstr(path); 20 if (kit_obj_open(&s->ctx, name_slice, &input, &s->of) != KIT_OK) { 21 driver_errf(tool, "%s: not a recognized object file", path); 22 return 1; 23 } 24 } 25 26 if (kit_dwarf_open(&s->ctx, s->of, &s->dwarf) != KIT_OK) { 27 driver_errf(tool, "%s: no debug info available", path); 28 return 1; 29 } 30 31 return 0; 32 } 33 34 void driver_dwarfsym_close(DriverDwarfSym* s) { 35 if (!s) return; 36 if (s->dwarf) { 37 kit_dwarf_free(s->dwarf); 38 s->dwarf = NULL; 39 } 40 if (s->of) { 41 kit_obj_free(s->of); 42 s->of = NULL; 43 } 44 if (s->env && s->ld.loaded) driver_release_bytes(&s->env->file_io, &s->ld); 45 } 46 47 void driver_dwarfsym_lookup(DriverDwarfSym* s, uint64_t addr, int want_func, 48 DriverSymLoc* out) { 49 KitSlice file; 50 uint32_t line = 0, col = 0; 51 52 memset(out, 0, sizeof *out); 53 54 if (kit_dwarf_addr_to_line(s->dwarf, addr, &file, &line, &col) == KIT_OK) { 55 out->have_line = 1; 56 out->file = file; 57 out->line = line; 58 out->col = col; 59 } 60 61 if (want_func) { 62 KitSlice func; 63 uint64_t func_lo = 0, func_hi = 0; 64 if (kit_dwarf_func_at(s->dwarf, addr, &func, &func_lo, &func_hi) == 65 KIT_OK) { 66 out->have_func = 1; 67 out->func = func; 68 } 69 } 70 }