Rizin
unix-like reverse engineering framework and cli tools
demangler.c File Reference
#include <rz_demangler.h>
#include <rz_util.h>
#include <rz_libdemangle.h>
#include <config.h>

Go to the source code of this file.

Macros

#define DEFINE_DEMANGLER_PLUGIN(name, lang, lic, auth, handler)
 

Functions

 DEFINE_DEMANGLER_PLUGIN (java, "java", "LGPL3", "deroad", libdemangle_handler_java)
 
 DEFINE_DEMANGLER_PLUGIN (msvc, "msvc", "LGPL3", "inisider", libdemangle_handler_msvc)
 
 DEFINE_DEMANGLER_PLUGIN (objc, "objc", "LGPL3", "pancake", libdemangle_handler_objc)
 
 RZ_LIB_VERSION (rz_demangler)
 
RZ_API RZ_OWN char * rz_demangler_java (RZ_NULLABLE const char *symbol)
 Demangles java symbols. More...
 
RZ_API RZ_OWN char * rz_demangler_cxx (RZ_NONNULL const char *symbol)
 Demangles c++ symbols. More...
 
RZ_API RZ_OWN char * rz_demangler_objc (RZ_NONNULL const char *symbol)
 Demangles objc symbols. More...
 
RZ_API RZ_OWN char * rz_demangler_rust (RZ_NONNULL const char *symbol)
 Demangles rust symbols. More...
 
RZ_API RZ_OWN char * rz_demangler_msvc (RZ_NONNULL const char *symbol)
 Demangles microsft vc symbols. More...
 
RZ_API RZ_OWN RzDemanglerrz_demangler_new (void)
 Initializes the plugin list and returns a RzDemangler struct. More...
 
RZ_API void rz_demangler_free (RZ_NULLABLE RzDemangler *dem)
 Frees the RzDemangler struct. More...
 
RZ_API void rz_demangler_plugin_iterate (RZ_NONNULL RzDemangler *dem, RZ_NONNULL RzDemanglerIter iter, RZ_NULLABLE void *data)
 Iterates over the plugin list. More...
 
RZ_API bool rz_demangler_plugin_add (RZ_NONNULL RzDemangler *dem, RZ_NONNULL RzDemanglerPlugin *plugin)
 Adds a new demangler plugin to the plugin list. More...
 
RZ_API RZ_BORROW const RzDemanglerPluginrz_demangler_plugin_get (RZ_NONNULL RzDemangler *dem, RZ_NONNULL const char *language)
 Returns a demangler plugin pointer based on the language that is found. More...
 
RZ_API bool rz_demangler_resolve (RZ_NONNULL RzDemangler *dem, RZ_NULLABLE const char *symbol, RZ_NONNULL const char *language, RZ_NONNULL RZ_OWN char **output)
 Resolves a symbol based on its language and return an output that needs to be freed. More...
 

Variables

static RzDemanglerPlugindemangler_static_plugins [] = { RZ_DEMANGLER_STATIC_PLUGINS }
 

Macro Definition Documentation

◆ DEFINE_DEMANGLER_PLUGIN

#define DEFINE_DEMANGLER_PLUGIN (   name,
  lang,
  lic,
  auth,
  handler 
)
Value:
RZ_API RzDemanglerPlugin rz_demangler_plugin_##name = { \
.language = lang, \
.license = lic, \
.author = auth, \
.demangle = &handler, \
}
#define RZ_API
const ut8 lic[]
Definition: nin.h:62
Definition: z80asm.h:102

Definition at line 10 of file demangler.c.

Function Documentation

◆ DEFINE_DEMANGLER_PLUGIN() [1/3]

DEFINE_DEMANGLER_PLUGIN ( java  ,
"java"  ,
"LGPL3"  ,
"deroad"  ,
libdemangle_handler_java   
)

◆ DEFINE_DEMANGLER_PLUGIN() [2/3]

DEFINE_DEMANGLER_PLUGIN ( msvc  ,
"msvc"  ,
"LGPL3"  ,
"inisider"  ,
libdemangle_handler_msvc   
)

◆ DEFINE_DEMANGLER_PLUGIN() [3/3]

DEFINE_DEMANGLER_PLUGIN ( objc  ,
"objc"  ,
"LGPL3"  ,
"pancake"  ,
libdemangle_handler_objc   
)

◆ rz_demangler_cxx()

RZ_API RZ_OWN char* rz_demangler_cxx ( RZ_NONNULL const char *  symbol)

Demangles c++ symbols.

Definition at line 45 of file demangler.c.

45  {
46 #if WITH_GPL
47  return libdemangle_handler_cxx(symbol);
48 #else
49  return NULL;
50 #endif
51 }
#define NULL
Definition: cris-opc.c:27

References NULL.

◆ rz_demangler_free()

RZ_API void rz_demangler_free ( RZ_NULLABLE RzDemangler dem)

Frees the RzDemangler struct.

Definition at line 112 of file demangler.c.

112  {
113  if (!dem) {
114  return;
115  }
116  rz_list_free(dem->plugins);
117  free(dem);
118 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137

References free(), and rz_list_free().

Referenced by rz_bin_free(), and rz_bin_new().

◆ rz_demangler_java()

RZ_API RZ_OWN char* rz_demangler_java ( RZ_NULLABLE const char *  symbol)

Demangles java symbols.

Definition at line 38 of file demangler.c.

38  {
39  return libdemangle_handler_java(symbol);
40 }
RZ_API char * libdemangle_handler_java(const char *symbol)
Demangles java classes/methods/fields.
Definition: java.c:260

References libdemangle_handler_java().

Referenced by classdump_java(), demangle_type(), rz_bin_demangle(), rz_bin_java_class_as_source_code(), rz_bin_java_class_const_pool_as_text(), rz_bin_java_class_methods_as_symbols(), and rz_cmd_javar_handler().

◆ rz_demangler_msvc()

RZ_API RZ_OWN char* rz_demangler_msvc ( RZ_NONNULL const char *  symbol)

Demangles microsft vc symbols.

Definition at line 74 of file demangler.c.

74  {
75  return libdemangle_handler_msvc(symbol);
76 }
RZ_API char * libdemangle_handler_msvc(const char *symbol)
Definition: msvc.c:6

References libdemangle_handler_msvc().

Referenced by pdb_set_symbols(), rz_bin_demangle(), and rz_core_bin_pdb_gvars_as_string().

◆ rz_demangler_new()

RZ_API RZ_OWN RzDemangler* rz_demangler_new ( void  )

Initializes the plugin list and returns a RzDemangler struct.

Definition at line 81 of file demangler.c.

81  {
83  if (!dem) {
84  return NULL;
85  }
86 
87  RzList *plugins = rz_list_new();
88  if (!plugins) {
89  free(dem);
90  return NULL;
91  }
92 
93  for (ut32 i = 0; i < RZ_ARRAY_SIZE(demangler_static_plugins); ++i) {
95  rz_warn_if_fail(p->language);
96  rz_warn_if_fail(p->license);
97  rz_warn_if_fail(p->author);
98  rz_warn_if_fail(p->demangle);
99  if (!p->demangle || !rz_list_append(plugins, p)) {
100  const char *lang = p->language ? p->language : "";
101  RZ_LOG_WARN("rz_demangler: failed to add '%s' plugin at index %u", lang, i);
102  }
103  }
104 
105  dem->plugins = plugins;
106  return dem;
107 }
lzma_index ** i
Definition: index.h:629
uint32_t ut32
void * p
Definition: libc.cpp:67
static RzDemanglerPlugin * demangler_static_plugins[]
Definition: demangler.c:31
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
#define rz_warn_if_fail(expr)
Definition: rz_assert.h:35
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_ARRAY_SIZE(x)
Definition: rz_types.h:300
RzList * plugins
Definition: rz_demangler.h:24

References demangler_static_plugins, free(), i, NULL, p, rz_demangler_t::plugins, RZ_ARRAY_SIZE, rz_list_append(), rz_list_new(), RZ_LOG_WARN, RZ_NEW0, and rz_warn_if_fail.

Referenced by rz_bin_new().

◆ rz_demangler_objc()

RZ_API RZ_OWN char* rz_demangler_objc ( RZ_NONNULL const char *  symbol)

Demangles objc symbols.

Definition at line 56 of file demangler.c.

56  {
57  return libdemangle_handler_objc(symbol);
58 }
RZ_API char * libdemangle_handler_objc(const char *symbol)
Definition: objc.c:6

References libdemangle_handler_objc().

Referenced by rz_bin_demangle().

◆ rz_demangler_plugin_add()

RZ_API bool rz_demangler_plugin_add ( RZ_NONNULL RzDemangler dem,
RZ_NONNULL RzDemanglerPlugin plugin 
)

Adds a new demangler plugin to the plugin list.

If two plugins handles the same language, then the old plugin is removed.

Definition at line 144 of file demangler.c.

144  {
145  rz_return_val_if_fail(dem && dem->plugins && plugin && plugin->language, false);
146  rz_warn_if_fail(plugin->author);
147  rz_warn_if_fail(plugin->license);
148 
149  const RzDemanglerPlugin *cp;
150  RzListIter *it = NULL;
151 
152  rz_list_foreach (dem->plugins, it, cp) {
153  if (!strcmp(cp->language, plugin->language)) {
154  // avoids to have duplicates
155  rz_list_delete(dem->plugins, it);
156  break;
157  }
158  }
159 
160  return rz_list_append(dem->plugins, plugin);
161 }
RZ_API void rz_list_delete(RZ_NONNULL RzList *list, RZ_NONNULL RzListIter *iter)
Removes an entry in the list by using the RzListIter pointer.
Definition: list.c:162
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
const char * language
demangler language
Definition: rz_demangler.h:17

References rz_demangler_plugin_t::language, NULL, rz_list_append(), rz_list_delete(), rz_return_val_if_fail, and rz_warn_if_fail.

Referenced by __lib_demangler_cb().

◆ rz_demangler_plugin_get()

RZ_API RZ_BORROW const RzDemanglerPlugin* rz_demangler_plugin_get ( RZ_NONNULL RzDemangler dem,
RZ_NONNULL const char *  language 
)

Returns a demangler plugin pointer based on the language that is found.

This function returns NULL only when the requested language is not available.

Definition at line 168 of file demangler.c.

168  {
169  rz_return_val_if_fail(RZ_STR_ISNOTEMPTY(language) && dem && dem->plugins, NULL);
170 
171  RzListIter *it;
172  const RzDemanglerPlugin *plugin;
173  rz_list_foreach (dem->plugins, it, plugin) {
174  if (!strcmp(language, plugin->language)) {
175  return plugin;
176  }
177  }
178 
179  return NULL;
180 }
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68

References rz_demangler_plugin_t::language, NULL, rz_return_val_if_fail, and RZ_STR_ISNOTEMPTY.

Referenced by rz_main_rz_bin().

◆ rz_demangler_plugin_iterate()

RZ_API void rz_demangler_plugin_iterate ( RZ_NONNULL RzDemangler dem,
RZ_NONNULL RzDemanglerIter  iter,
RZ_NULLABLE void *  data 
)

Iterates over the plugin list.

Iterates over the plugin list and passes a RzDemanglerPlugin pointer to the iter function; if the iter function returns false, then the iteration is halted.

Definition at line 127 of file demangler.c.

127  {
128  rz_return_if_fail(dem && dem->plugins && iter);
129  const RzDemanglerPlugin *plugin;
130  RzListIter *it;
131 
132  rz_list_foreach (dem->plugins, it, plugin) {
133  if (!iter(plugin, data)) {
134  break;
135  }
136  }
137 }
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100

References rz_return_if_fail.

Referenced by rz_cmd_info_demangle_handler(), rz_cmd_info_demangle_list_handler(), and rz_main_rz_bin().

◆ rz_demangler_resolve()

RZ_API bool rz_demangler_resolve ( RZ_NONNULL RzDemangler dem,
RZ_NULLABLE const char *  symbol,
RZ_NONNULL const char *  language,
RZ_NONNULL RZ_OWN char **  output 
)

Resolves a symbol based on its language and return an output that needs to be freed.

This function fails only when the requested language is not available.

Definition at line 187 of file demangler.c.

187  {
188  rz_return_val_if_fail(language && dem->plugins && output, false);
189 
190  if (RZ_STR_ISEMPTY(symbol)) {
191  *output = NULL;
192  return true;
193  }
194 
195  const RzDemanglerPlugin *plugin;
196  RzListIter *it;
197 
198  rz_list_foreach (dem->plugins, it, plugin) {
199  if (!strcmp(plugin->language, language)) {
200  *output = plugin->demangle(symbol);
201  return true;
202  }
203  }
204 
205  return false;
206 }
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_OWN char *(* demangle)(RZ_NONNULL const char *symbol)
demangler method to resolve the mangled symbol
Definition: rz_demangler.h:20
diff_output_t output
Definition: zipcmp.c:237

References rz_demangler_plugin_t::demangle, rz_demangler_plugin_t::language, NULL, output, rz_return_val_if_fail, and RZ_STR_ISEMPTY.

Referenced by rz_bin_demangle(), and rz_cmd_info_demangle_handler().

◆ rz_demangler_rust()

RZ_API RZ_OWN char* rz_demangler_rust ( RZ_NONNULL const char *  symbol)

Demangles rust symbols.

Definition at line 63 of file demangler.c.

63  {
64 #if WITH_GPL
65  return libdemangle_handler_rust(symbol);
66 #else
67  return NULL;
68 #endif
69 }
char * libdemangle_handler_rust(const char *sym)
Definition: rust.c:25

References libdemangle_handler_rust(), and NULL.

◆ RZ_LIB_VERSION()

RZ_LIB_VERSION ( rz_demangler  )

Variable Documentation

◆ demangler_static_plugins

RzDemanglerPlugin* demangler_static_plugins[] = { RZ_DEMANGLER_STATIC_PLUGINS }
static

Definition at line 31 of file demangler.c.

Referenced by rz_demangler_new().