rizin/librz/bin/bobj_process_section.c
wargio c85b1afa4c Rewrite how bins are loaded into RzBinObject
This commit rewrites completely how RzBinPlugin are used to load info
into the RzBinObject structure.

Main changes are:
- Avoid looping multiple times to set the same data:
  now the data is cycled only once to rebase addresses, demangle strings
  and to make each loaded structure similar to each other.

- rz_bin_demangle is now demangling only strings:
  the old implementation was a hack over a hack to add classes & methods
  into the RzBinObject while demangling a string.
  This allowed methods and classes to be added to RzBinObject when for
  example invoking `is` or other printing commands.
  This was meant to be an "optimization" to avoid looping and doing
  things on-the-fly.

- Now the demangling is done after guessing the language used in the bin.
  This is done to avoid looping to various demanglers hoping that a
  string is going to be correctly demangled.
  After each string is demangled, the string is given to the language
  related function which adds classes, methods and fields into the
  RzBinObject fields.

- Swift handling is a special case, since the demangler can be disabled
  via -Duse_swift_demangler.
  The issue with the swift demangler is that is not correctly demangling
  strings and outputting swift code, but instead is creating something
  that sometimes works, sometimes doesn't.
  Also the old code only added fields and never methods for "reasons",
  and the new code just uses what the old code was doing as is.

- The new code also takes advantage of the language detection, which now
  happens before the demangling of symbols, imports and relocs.
2023-06-01 23:16:09 +08:00

45 lines
1.2 KiB
C

// SPDX-FileCopyrightText: 2023 RizinOrg <info@rizin.re>
// SPDX-FileCopyrightText: 2023 deroad <wargio@libero.it>
// SPDX-License-Identifier: LGPL-3.0-only
#include <rz_bin.h>
#include "i/private.h"
static void process_handle_section(RzBinSection *section, RzBinObject *o, HtPP *filter_db) {
// rebase physical address
section->paddr += o->opts.loadaddr;
if (!filter_db) {
// we do not have to filter the names.
return;
}
// check if section name was already found, then rename it.
if (!ht_pp_find(filter_db, section->name, NULL)) {
ht_pp_insert(filter_db, section->name, section);
return;
}
char *name = rz_str_newf("%s_0x%" PFMT64x, section->name, section->paddr);
free(section->name);
section->name = name;
}
RZ_IPI void rz_bin_set_and_process_sections(RzBinFile *bf, RzBinObject *o) {
RzBin *bin = bf->rbin;
RzBinPlugin *plugin = o->plugin;
rz_list_free(o->sections);
if (!plugin->sections || !(o->sections = plugin->sections(bf))) {
o->sections = rz_list_newf((RzListFree)rz_bin_section_free);
}
HtPP *filter_db = bin->filter ? ht_pp_new0() : NULL;
RzListIter *it;
RzBinSection *element;
rz_list_foreach (o->sections, it, element) {
process_handle_section(element, o, filter_db);
}
ht_pp_free(filter_db);
}