// binary search function for get_aon_index_binary() static int find_aon_idx_bin( vmi_instance_t vmi, const char *symbol, addr_t aon_base_va, int low, int high, addr_t base_addr, vmi_pid_t pid) { int mid, cmp; addr_t str_rva_loc; // location of curr name's RVA uint32_t str_rva; // RVA of curr name char *name = 0; // curr name if (high < low) goto not_found; // calc the current index ("mid") mid = (low + high) / 2; str_rva_loc = aon_base_va + mid * sizeof(uint32_t); vmi_read_32_va(vmi, str_rva_loc, pid, &str_rva); if (!str_rva) goto not_found; // get the curr string & compare to symbol name = rva_to_string(vmi, (addr_t) str_rva, base_addr, pid); if(!name) goto not_found; cmp = strcmp(symbol, name); free(name); if (cmp < 0) { // symbol < name ==> try lower region return find_aon_idx_bin(vmi, symbol, aon_base_va, low, mid - 1, base_addr, pid); } else if (cmp > 0) { // symbol > name ==> try higher region return find_aon_idx_bin(vmi, symbol, aon_base_va, mid + 1, high, base_addr, pid); } else { // symbol == name return mid; // found } not_found: return -1; }
// binary search function for get_aon_index_binary() static int find_aon_idx_bin( vmi_instance_t vmi, const char *symbol, addr_t aon_base_va, int low, int high, const access_context_t *ctx) { access_context_t _ctx = *ctx; int mid, cmp; uint32_t str_rva = 0; // RVA of curr name char *name = 0; // curr name if (high < low) goto not_found; // calc the current index ("mid") mid = (low + high) / 2; _ctx.addr = aon_base_va + mid * sizeof(uint32_t); if (VMI_FAILURE == vmi_read_32(vmi, &_ctx, &str_rva) || !str_rva) goto not_found; // get the curr string & compare to symbol _ctx.addr = ctx->addr + str_rva; name = vmi_read_str(vmi, &_ctx); if (!name) goto not_found; cmp = strcmp(symbol, name); free(name); if (cmp < 0) { // symbol < name ==> try lower region return find_aon_idx_bin(vmi, symbol, aon_base_va, low, mid - 1, ctx); } else if (cmp > 0) { // symbol > name ==> try higher region return find_aon_idx_bin(vmi, symbol, aon_base_va, mid + 1, high, ctx); } else { // symbol == name return mid; // found } not_found: return -1; }
// Finds the index of the exported symbol specified - binary search int get_aon_index_binary( vmi_instance_t vmi, const char *symbol, struct export_table *et, const access_context_t *ctx) { addr_t aon_base_addr = ctx->addr + et->address_of_names; int name_ct = et->number_of_names; return find_aon_idx_bin(vmi, symbol, aon_base_addr, 0, name_ct - 1, ctx); }
// Finds the index of the exported symbol specified - binary search int get_aon_index_binary( vmi_instance_t vmi, const char *symbol, struct export_table *et, addr_t base_addr, vmi_pid_t pid) { addr_t aon_base_addr = base_addr + et->address_of_names; int name_ct = et->number_of_names; return find_aon_idx_bin(vmi, symbol, aon_base_addr, 0, name_ct - 1, base_addr, pid); }