Add analysis pass to make avgl command show global variables (#6306)

This commit is contained in:
bubblepipe 2026-05-09 13:29:46 +07:00 committed by GitHub
parent bc2ee4c1f8
commit a8429853d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 71 additions and 2 deletions

View file

@ -3942,6 +3942,53 @@ static void core_analysis_analyze_local_var_and_arg(RzCore *core) {
}
}
static void analysis_global_vars_from_symbols(RzCore *core) {
// DWARF/PDB already enumerate globals with accurate types; only fall back
// to symbol-table inference when no debug info is present.
RzAnalysisDebugInfo *dbg_info = rz_analysis_get_debug_info(core->analysis);
if (dbg_info && dbg_info->dw) {
return;
}
RzBinObject *obj = rz_bin_cur_object(core->bin);
if (!obj) {
return;
}
RzTypeDB *typedb = rz_analysis_get_type_db(core->analysis);
RzPVector *symbols = (RzPVector *)rz_bin_object_get_symbols(obj);
if (!symbols) {
return;
}
bool virt_addr = rz_config_get_b(core->config, "io.va");
void **it;
rz_pvector_foreach (symbols, it) {
RzBinSymbol *sym = *it;
if (!sym->name || sym->is_imported) {
continue;
}
if (!sym->type || strcmp(sym->type, RZ_BIN_TYPE_OBJECT_STR)) {
continue;
}
if (!sym->size) {
continue;
}
ut64 addr = virt_addr ? rz_bin_object_get_vaddr(obj, sym->paddr, sym->vaddr) : sym->paddr;
if (addr == UT64_MAX || addr == 0) {
continue;
}
if (rz_analysis_var_global_get_byaddr_in(core->analysis, addr)) {
continue;
}
RzType *type = sym->size == 1
? rz_type_identifier_of_base_type_str(typedb, "uint8_t")
: rz_type_array_of_base_type_str(typedb, "uint8_t", sym->size);
if (!type) {
continue;
}
// rz_analysis_var_global_create takes effective ownership of type
rz_analysis_var_global_create(core->analysis, sym->name, type, addr);
}
}
/**
* Runs all the steps of the deep analysis.
*
@ -4114,6 +4161,15 @@ RZ_API bool rz_core_analysis_everything(RzCore *core, bool experimental, char *d
rz_core_notify_done(core, "%s", notify);
}
notify = "Recover global variables from symbols";
rz_core_notify_begin(core, "%s", notify);
analysis_global_vars_from_symbols(core);
rz_core_notify_done(core, "%s", notify);
rz_core_task_yield(&core->tasks);
if (rz_cons_is_breaked()) {
return false;
}
if (rz_config_get_b(core->config, "analysis.resolve.pointers")) {
notify = "Resolve pointers to data sections";
rz_core_notify_begin(core, "%s", notify);

View file

@ -4419,4 +4419,16 @@ EXPECT=<<EOF
| 0x001b9b30 push r15
| 0x001b9b32 push r14
EOF
RUN
RUN
NAME=global variables populated from ELF OBJ symbols without DWARF
FILE=bins/elf/dectest64
CMDS=<<EOF
aaa
avgl~global_var
avgl~global_array
EOF
EXPECT=<<EOF
global uint8_t [4] global_var @ 0x404050
global uint8_t [8] global_array @ 0x404058
EOF
RUN

View file

@ -207,7 +207,8 @@ NAME=flj shows demangled symbols
FILE=bins/elf/demangle-test-cpp
CMDS=<<EOF
aaa
flj~{214}
s reloc.operator_delete_void
fl.j~{0}
EOF
EXPECT=<<EOF
{"name":"reloc.operator_delete_void","realname":"operator delete(void*)","size":8,"offset":16432}