Rizin
unix-like reverse engineering framework and cli tools
elf_symbols.c File Reference
#include "elf.h"
#include <ht_uu.h>

Go to the source code of this file.

Classes

struct  symbols_segment
 
struct  symbol_bind_translation
 
struct  symbol_type_translation
 

Macros

#define HASH_NCHAIN_OFFSET(x)   ((x) + 4)
 

Functions

static struct symbols_segment symbols_segment_init (ut64 offset, ut64 number, ut64 entry_size, bool dynamic, RzBinElfStrtab *strtab)
 
static Elf_ (Word)
 
static bool is_special_arm_symbol (ELFOBJ *bin, Elf_(Sym) *sym, const char *name)
 
static bool is_special_symbol (ELFOBJ *bin, Elf_(Sym) *sym, const char *name)
 
static const char * symbol_type_to_str (ELFOBJ *bin, RzBinElfSymbol *ret, Elf_(Sym) *sym)
 
static const char * symbol_bind_to_str (Elf_(Sym) *sym)
 
static bool get_symbol_entry_aux (ELFOBJ *bin, ut64 offset, Elf_(Sym) *result)
 
static bool get_symbol_entry (ELFOBJ *bin, ut64 offset, Elf_(Sym) *result)
 
static bool is_section_local_symbol (ELFOBJ *bin, Elf_(Sym) *symbol)
 
static bool set_elf_symbol_name (ELFOBJ *bin, struct symbols_segment *segment, RzBinElfSymbol *elf_symbol, Elf_(Sym) *symbol, RzBinElfSection *section)
 
static bool convert_elf_symbol_entry (ELFOBJ *bin, struct symbols_segment *segment, RzBinElfSymbol *elf_symbol, Elf_(Sym) *symbol, size_t ordinal)
 
static bool has_already_been_processed (ELFOBJ *bin, ut64 offset, HtUU *set)
 
static void elf_symbol_fini (void *e, RZ_UNUSED void *user)
 
static bool compute_symbols_from_segment (ELFOBJ *bin, RzVector *result, struct symbols_segment *segment, RzBinElfSymbolFilter filter, HtUU *set)
 
static bool get_dynamic_elf_symbols (ELFOBJ *bin, RzVector *result, RzBinElfSymbolFilter filter, HtUU *set)
 
static bool get_section_elf_symbols (ELFOBJ *bin, RzVector *result, RzBinElfSymbolFilter filter, HtUU *set)
 
static bool get_gnu_debugdata_elf_symbols (ELFOBJ *bin, RzVector *result, RzBinElfSymbolFilter filter, HtUU *set)
 
static bool filter_symbol (RZ_UNUSED ELFOBJ *bin, Elf_(Sym) *symbol, RZ_UNUSED bool is_dynamic)
 
RZ_BORROW RzBinElfSymbol *Elf_() rz_bin_elf_get_symbol (RZ_NONNULL ELFOBJ *bin, ut32 ordinal)
 
RZ_OWN RzVector *Elf_() rz_bin_elf_compute_symbols (ELFOBJ *bin, RzBinElfSymbolFilter filter)
 
RZ_OWN RzVector *Elf_() rz_bin_elf_symbols_new (RZ_NONNULL ELFOBJ *bin)
 
bool Elf_() rz_bin_elf_has_symbols (RZ_NONNULL ELFOBJ *bin)
 

Variables

static struct symbol_bind_translation symbol_bind_translation_table []
 
static const struct symbol_type_translation symbol_type_translation_table []
 

Macro Definition Documentation

◆ HASH_NCHAIN_OFFSET

#define HASH_NCHAIN_OFFSET (   x)    ((x) + 4)

Definition at line 10 of file elf_symbols.c.

Function Documentation

◆ compute_symbols_from_segment()

static bool compute_symbols_from_segment ( ELFOBJ bin,
RzVector result,
struct symbols_segment segment,
RzBinElfSymbolFilter  filter,
HtUU *  set 
)
static

Definition at line 255 of file elf_symbols.c.

255  {
256  ut64 offset = segment->offset + segment->entry_size;
257 
258  for (size_t i = 1; i < segment->number; i++) {
260  offset += segment->entry_size;
261  continue;
262  }
263 
264  if (!ht_uu_insert(set, offset, offset)) {
265  return false;
266  }
267 
268  Elf_(Sym) entry;
269  if (!get_symbol_entry(bin, offset, &entry)) {
270  return false;
271  }
272 
273  if (!filter(bin, &entry, segment->dynamic)) {
274  offset += segment->entry_size;
275  continue;
276  }
277 
278  RzBinElfSymbol symbol = { 0 };
279 
280  if (!convert_elf_symbol_entry(bin, segment, &symbol, &entry, i)) {
281  return false;
282  }
283 
284  if (!rz_vector_push(result, &symbol)) {
285  elf_symbol_fini(&symbol, NULL);
286  return false;
287  }
288 
289  offset += segment->entry_size;
290  }
291 
292  return true;
293 }
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
static bool convert_elf_symbol_entry(ELFOBJ *bin, struct symbols_segment *segment, RzBinElfSymbol *elf_symbol, Elf_(Sym) *symbol, size_t ordinal)
Definition: elf_symbols.c:215
static Elf_(Word)
Definition: elf_symbols.c:66
static bool get_symbol_entry(ELFOBJ *bin, ut64 offset, Elf_(Sym) *result)
Definition: elf_symbols.c:171
static bool has_already_been_processed(ELFOBJ *bin, ut64 offset, HtUU *set)
Definition: elf_symbols.c:243
static void elf_symbol_fini(void *e, RZ_UNUSED void *user)
Definition: elf_symbols.c:250
voidpf uLong offset
Definition: ioapi.h:144
static bool filter(RzParse *p, ut64 addr, RzFlag *f, RzAnalysisHint *hint, char *data, char *str, int len, bool big_endian)
Definition: filter.c:185
RZ_API void * rz_vector_push(RzVector *vec, void *x)
Definition: vector.c:197
Definition: malloc.c:26
Definition: zipcmp.c:77
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References convert_elf_symbol_entry(), symbols_segment::dynamic, Elf_(), elf_symbol_fini(), symbols_segment::entry_size, filter(), get_symbol_entry(), has_already_been_processed(), i, NULL, symbols_segment::number, symbols_segment::offset, rz_vector_push(), and ut64().

Referenced by get_dynamic_elf_symbols(), and get_section_elf_symbols().

◆ convert_elf_symbol_entry()

static bool convert_elf_symbol_entry ( ELFOBJ bin,
struct symbols_segment segment,
RzBinElfSymbol elf_symbol,
Elf_(Sym) *  symbol,
size_t  ordinal 
)
static

Definition at line 215 of file elf_symbols.c.

215  {
216  RzBinElfSection *section = Elf_(rz_bin_elf_get_section)(bin, symbol->st_shndx);
217 
218  elf_symbol->bind = symbol_bind_to_str(symbol);
219  elf_symbol->ordinal = ordinal;
220  elf_symbol->size = symbol->st_size;
221 
222  if (symbol->st_size == 0 && symbol->st_shndx == SHN_UNDEF && symbol->st_value == 0) {
223  elf_symbol->paddr = elf_symbol->vaddr = UT64_MAX;
224  } else {
226  elf_symbol->paddr = section->offset + symbol->st_value;
227  elf_symbol->vaddr = Elf_(rz_bin_elf_p2v)(bin, elf_symbol->paddr);
228  } else {
229  elf_symbol->vaddr = symbol->st_value;
230  elf_symbol->paddr = Elf_(rz_bin_elf_v2p)(bin, elf_symbol->vaddr);
231  }
232  }
233 
234  if (!set_elf_symbol_name(bin, segment, elf_symbol, symbol, section)) {
235  return false;
236  }
237 
238  elf_symbol->type = symbol_type_to_str(bin, elf_symbol, symbol);
239 
240  return true;
241 }
ut64 Elf_() rz_bin_elf_p2v(RZ_NONNULL ELFOBJ *bin, ut64 paddr)
Convert a physical address to the virtual address.
Definition: elf.c:400
ut64 Elf_() rz_bin_elf_v2p(RZ_NONNULL ELFOBJ *bin, ut64 vaddr)
Convert a virtual address to the physical address.
Definition: elf.c:429
RZ_BORROW RzBinElfSection *Elf_() rz_bin_elf_get_section(RZ_NONNULL ELFOBJ *bin, Elf_(Half) index)
Definition: elf_sections.c:264
bool Elf_() rz_bin_elf_is_relocatable(RZ_NONNULL ELFOBJ *bin)
Check if the elf binary is relocatable.
Definition: elf_info.c:1662
static const char * symbol_bind_to_str(Elf_(Sym) *sym)
Definition: elf_symbols.c:143
static const char * symbol_type_to_str(ELFOBJ *bin, RzBinElfSymbol *ret, Elf_(Sym) *sym)
Definition: elf_symbols.c:129
static bool set_elf_symbol_name(ELFOBJ *bin, struct symbols_segment *segment, RzBinElfSymbol *elf_symbol, Elf_(Sym) *symbol, RzBinElfSection *section)
Definition: elf_symbols.c:197
#define SHN_UNDEF
Definition: common.h:505
#define UT64_MAX
Definition: rz_types_base.h:86
const char * bind
Definition: elf.h:138
const char * type
Definition: elf.h:139
uint32_t offset

References rz_bin_elf_symbol_t::bind, Elf_(), section::offset, rz_bin_elf_symbol_t::ordinal, rz_bin_elf_symbol_t::paddr, rz_bin_elf_get_section(), rz_bin_elf_is_relocatable(), rz_bin_elf_p2v(), rz_bin_elf_v2p(), set_elf_symbol_name(), SHN_UNDEF, rz_bin_elf_symbol_t::size, symbol_bind_to_str(), symbol_type_to_str(), rz_bin_elf_symbol_t::type, UT64_MAX, and rz_bin_elf_symbol_t::vaddr.

Referenced by compute_symbols_from_segment().

◆ Elf_()

static Elf_ ( Word  )
static

Definition at line 66 of file elf_symbols.c.

66  {
67  if (symtab_offset > strtab_offset) {
68  return 0;
69  }
70 
71  ut64 symtab_size = strtab_offset - symtab_offset;
72  return symtab_size / sizeof(Elf_(Sym));
73 }

References ut64().

Referenced by compute_symbols_from_segment(), convert_elf_symbol_entry(), Elf_(), get_dynamic_elf_symbols(), get_gnu_debugdata_elf_symbols(), get_section_elf_symbols(), get_symbol_entry_aux(), rz_bin_elf_symbols_new(), and set_elf_symbol_name().

◆ elf_symbol_fini()

static void elf_symbol_fini ( void *  e,
RZ_UNUSED void *  user 
)
static

Definition at line 250 of file elf_symbols.c.

250  {
251  RzBinElfSymbol *ptr = e;
252  free(ptr->name);
253 }
#define e(frag)
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_OWN char * name
Definition: elf.h:140

References e, free(), and rz_bin_elf_symbol_t::name.

Referenced by compute_symbols_from_segment(), and rz_bin_elf_compute_symbols().

◆ filter_symbol()

static bool filter_symbol ( RZ_UNUSED ELFOBJ bin,
Elf_(Sym) *  symbol,
RZ_UNUSED bool  is_dynamic 
)
static

Definition at line 439 of file elf_symbols.c.

439  {
440  return symbol->st_shndx != SHT_NULL;
441 }
#define SHT_NULL
Definition: common.h:331

References SHT_NULL.

Referenced by rz_bin_elf_symbols_new().

◆ get_dynamic_elf_symbols()

static bool get_dynamic_elf_symbols ( ELFOBJ bin,
RzVector result,
RzBinElfSymbolFilter  filter,
HtUU *  set 
)
static

Definition at line 295 of file elf_symbols.c.

295  {
297  return true;
298  }
299 
300  ut64 addr;
301  ut64 entry_size;
303  return true;
304  }
305 
307  if (offset == UT64_MAX) {
308  return true;
309  }
310 
311  Elf_(Word) number = Elf_(rz_bin_elf_get_number_of_dynamic_symbols)(bin);
312  if (!number) {
313  return true;
314  }
315 
316  struct symbols_segment segment = symbols_segment_init(offset, number, entry_size, true, bin->dynstr);
317 
318  if (!compute_symbols_from_segment(bin, result, &segment, filter, set)) {
319  return false;
320  }
321 
322  return true;
323 }
bool Elf_() rz_bin_elf_is_executable(RZ_NONNULL ELFOBJ *bin)
Check if the elf binary is executable.
Definition: elf_info.c:1648
bool Elf_() rz_bin_elf_get_dt_info(RZ_NONNULL ELFOBJ *bin, ut64 key, RZ_OUT ut64 *info)
Definition: elf_dynamic.c:120
static bool compute_symbols_from_segment(ELFOBJ *bin, RzVector *result, struct symbols_segment *segment, RzBinElfSymbolFilter filter, HtUU *set)
Definition: elf_symbols.c:255
static struct symbols_segment symbols_segment_init(ut64 offset, ut64 number, ut64 entry_size, bool dynamic, RzBinElfStrtab *strtab)
Definition: elf_symbols.c:56
#define DT_SYMTAB
Definition: common.h:543
#define DT_SYMENT
Definition: common.h:548
static int addr
Definition: z80asm.c:58

References addr, compute_symbols_from_segment(), DT_SYMENT, DT_SYMTAB, Elf_(), symbols_segment::entry_size, symbols_segment::number, rz_bin_elf_get_dt_info(), rz_bin_elf_is_executable(), rz_bin_elf_v2p(), symbols_segment_init(), ut64(), and UT64_MAX.

Referenced by rz_bin_elf_compute_symbols().

◆ get_gnu_debugdata_elf_symbols()

static bool get_gnu_debugdata_elf_symbols ( ELFOBJ bin,
RzVector result,
RzBinElfSymbolFilter  filter,
HtUU *  set 
)
static

Definition at line 367 of file elf_symbols.c.

367  {
368  // Get symbols from .gnu_debugdata according to https://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html
369  bool res = false;
370  const RzBinElfSection *gnu_debugdata = Elf_(rz_bin_elf_get_section_with_name)(bin, ".gnu_debugdata");
371  if (!gnu_debugdata) {
372  return false;
373  }
374 
375  RzBuffer *data_buf = rz_buf_new_slice(bin->b, gnu_debugdata->offset, gnu_debugdata->size);
376  if (!data_buf) {
377  return false;
378  }
379 
380  RzBuffer *dec_buf = rz_buf_new_empty(0);
381  if (!dec_buf) {
382  goto data_buf_err;
383  }
384 
385  if (!rz_lzma_dec_buf(data_buf, dec_buf, 1 << 13, NULL)) {
386  goto dec_buf_err;
387  }
388 
389  RzBinObjectLoadOptions obj_opts = {
390  .baseaddr = UT64_MAX,
391  .loadaddr = 0,
392  .elf_load_sections = true,
393  };
394  ELFOBJ *debug_data_bin = Elf_(rz_bin_elf_new_buf)(dec_buf, &obj_opts);
395  if (!debug_data_bin) {
396  goto dec_buf_err;
397  }
398 
399  RzVector *debug_symbols = Elf_(rz_bin_elf_compute_symbols)(debug_data_bin, filter);
400  if (!debug_symbols) {
401  goto debug_data_err;
402  }
403 
404  HtPP *name_set = ht_pp_new0();
405  if (!name_set) {
406  goto debug_symbols_err;
407  }
408 
409  RzBinElfSymbol *sym;
410  rz_vector_foreach(result, sym) {
411  ht_pp_insert(name_set, sym->name, sym);
412  }
413 
414  rz_vector_foreach(debug_symbols, sym) {
415  bool found;
416  ht_pp_find(name_set, sym->name, &found);
417  if (found) {
418  continue;
419  }
420 
421  rz_vector_push(result, sym);
422  }
423  // The ownership has been moved to `result`, no need to free the elements.
424  debug_symbols->len = 0;
425  res = true;
426 
427  ht_pp_free(name_set);
428 debug_symbols_err:
429  rz_vector_free(debug_symbols);
430 debug_data_err:
431  Elf_(rz_bin_elf_free)(debug_data_bin);
432 dec_buf_err:
433  rz_buf_free(dec_buf);
434 data_buf_err:
435  rz_buf_free(data_buf);
436  return res;
437 }
void Elf_() rz_bin_elf_free(RZ_NONNULL ELFOBJ *bin)
Free the elf binary.
Definition: elf.c:366
RZ_OWN ELFOBJ *Elf_() rz_bin_elf_new_buf(RZ_NONNULL RzBuffer *buf, RZ_NONNULL RzBinObjectLoadOptions *options)
Definition: elf.c:339
#define ELFOBJ
Definition: elf.h:24
RZ_BORROW RzBinElfSection *Elf_() rz_bin_elf_get_section_with_name(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL const char *name)
Definition: elf_sections.c:278
RZ_OWN RzVector *Elf_() rz_bin_elf_compute_symbols(ELFOBJ *bin, RzBinElfSymbolFilter filter)
Definition: elf_symbols.c:486
RZ_API const KEY_TYPE bool * found
Definition: ht_inc.h:130
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RZ_API RZ_OWN RzBuffer * rz_buf_new_empty(ut64 len)
Creates a new empty buffer with a predefined size;.
Definition: buf.c:285
RZ_API RZ_OWN RzBuffer * rz_buf_new_slice(RzBuffer *b, ut64 offset, ut64 size)
Creates a new buffer from a slice of another buffer.
Definition: buf.c:364
RZ_API bool rz_lzma_dec_buf(RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed)
Decompress the src buffer with LZMA algorithm and put the decompressed data in dst.
Definition: compression.c:462
#define rz_vector_foreach(vec, it)
Definition: rz_vector.h:169
RZ_API void rz_vector_free(RzVector *vec)
Definition: vector.c:75
ut64 baseaddr
where the linker maps the binary in memory
Definition: rz_bin.h:248
size_t len
Definition: rz_vector.h:47

References rz_bin_file_load_options_t::baseaddr, Elf_(), ELFOBJ, filter(), found, rz_vector_t::len, rz_bin_elf_symbol_t::name, NULL, rz_bin_elf_section_t::offset, rz_bin_elf_compute_symbols(), rz_bin_elf_free(), rz_bin_elf_get_section_with_name(), rz_bin_elf_new_buf(), rz_buf_free(), rz_buf_new_empty(), rz_buf_new_slice(), rz_lzma_dec_buf(), rz_vector_foreach, rz_vector_free(), rz_vector_push(), rz_bin_elf_section_t::size, and UT64_MAX.

Referenced by rz_bin_elf_compute_symbols().

◆ get_section_elf_symbols()

static bool get_section_elf_symbols ( ELFOBJ bin,
RzVector result,
RzBinElfSymbolFilter  filter,
HtUU *  set 
)
static

Definition at line 325 of file elf_symbols.c.

325  {
326  size_t i;
329  if (!section->is_valid) {
330  continue;
331  }
332 
333  if (section->type != SHT_SYMTAB && section->type != SHT_DYNSYM) {
334  continue;
335  }
336 
337  if (!section->link) {
338  RZ_LOG_WARN("The section %zu has a null link.\n", i);
339  continue;
340  }
341 
342  RzBinElfSection *strtab_section = Elf_(rz_bin_elf_get_section)(bin, section->link);
343  if (!strtab_section) {
344  continue;
345  }
346 
347  RzBinElfStrtab *strtab = Elf_(rz_bin_elf_strtab_new)(bin, strtab_section->offset, strtab_section->size);
348  if (!strtab) {
349  continue;
350  }
351 
352  ut64 number = (section->size / sizeof(Elf_(Sym)));
353 
354  struct symbols_segment segment = symbols_segment_init(section->offset, number, sizeof(Elf_(Sym)), false, strtab);
355 
356  if (!compute_symbols_from_segment(bin, result, &segment, filter, set)) {
358  return false;
359  }
360 
362  }
363 
364  return true;
365 }
#define rz_bin_elf_enumerate_sections(bin, section, i)
Definition: elf.h:34
RZ_OWN RzBinElfStrtab *Elf_() rz_bin_elf_strtab_new(RZ_NONNULL ELFOBJ *bin, ut64 offset, ut64 size)
Definition: elf_strtab.c:18
void Elf_() rz_bin_elf_strtab_free(RzBinElfStrtab *ptr)
Definition: elf_strtab.c:72
#define SHT_SYMTAB
Definition: common.h:333
#define SHT_DYNSYM
Definition: common.h:342
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56
uint32_t size
RZ_BORROW RzBinElfStrtab * strtab
Definition: elf_symbols.c:17

References compute_symbols_from_segment(), Elf_(), i, symbols_segment::number, rz_bin_elf_section_t::offset, section::offset, rz_bin_elf_enumerate_sections, rz_bin_elf_get_section(), rz_bin_elf_strtab_free(), rz_bin_elf_strtab_new(), RZ_LOG_WARN, SHT_DYNSYM, SHT_SYMTAB, rz_bin_elf_section_t::size, section::size, symbols_segment::strtab, symbols_segment_init(), and ut64().

Referenced by rz_bin_elf_compute_symbols().

◆ get_symbol_entry()

static bool get_symbol_entry ( ELFOBJ bin,
ut64  offset,
Elf_(Sym) *  result 
)
static

Definition at line 171 of file elf_symbols.c.

171  {
172  if (!get_symbol_entry_aux(bin, offset, result)) {
173  RZ_LOG_WARN("Failed to read symbol entry at 0x%" PFMT64x ".\n", offset);
174  return false;
175  }
176 
177  return true;
178 }
static bool get_symbol_entry_aux(ELFOBJ *bin, ut64 offset, Elf_(Sym) *result)
Definition: elf_symbols.c:153
#define PFMT64x
Definition: rz_types.h:393

References get_symbol_entry_aux(), PFMT64x, and RZ_LOG_WARN.

Referenced by compute_symbols_from_segment().

◆ get_symbol_entry_aux()

static bool get_symbol_entry_aux ( ELFOBJ bin,
ut64  offset,
Elf_(Sym) *  result 
)
static

Definition at line 153 of file elf_symbols.c.

153  {
154 #if RZ_BIN_ELF64
155  return Elf_(rz_bin_elf_read_word)(bin, &offset, &result->st_name) &&
156  Elf_(rz_bin_elf_read_char)(bin, &offset, &result->st_info) &&
157  Elf_(rz_bin_elf_read_char)(bin, &offset, &result->st_other) &&
158  Elf_(rz_bin_elf_read_section)(bin, &offset, &result->st_shndx) &&
159  Elf_(rz_bin_elf_read_addr)(bin, &offset, &result->st_value) &&
160  Elf_(rz_bin_elf_read_xword)(bin, &offset, &result->st_size);
161 #else
162  return Elf_(rz_bin_elf_read_word)(bin, &offset, &result->st_name) &&
163  Elf_(rz_bin_elf_read_addr)(bin, &offset, &result->st_value) &&
164  Elf_(rz_bin_elf_read_word)(bin, &offset, &result->st_size) &&
165  Elf_(rz_bin_elf_read_char)(bin, &offset, &result->st_info) &&
166  Elf_(rz_bin_elf_read_char)(bin, &offset, &result->st_other) &&
167  Elf_(rz_bin_elf_read_section)(bin, &offset, &result->st_shndx);
168 #endif
169 }
bool Elf_() rz_bin_elf_read_addr(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Addr) *result)
Definition: elf_misc.c:82
bool Elf_() rz_bin_elf_read_char(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT ut8 *result)
Definition: elf_misc.c:52
bool Elf_() rz_bin_elf_read_word(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Word) *result)
Definition: elf_misc.c:62
bool Elf_() rz_bin_elf_read_xword(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Xword) *result)
Definition: elf_misc.c:72
bool Elf_() rz_bin_elf_read_section(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Section) *result)
Definition: elf_misc.c:100

References Elf_(), rz_bin_elf_read_addr(), rz_bin_elf_read_char(), rz_bin_elf_read_section(), rz_bin_elf_read_word(), and rz_bin_elf_read_xword().

Referenced by get_symbol_entry().

◆ has_already_been_processed()

static bool has_already_been_processed ( ELFOBJ bin,
ut64  offset,
HtUU *  set 
)
static

Definition at line 243 of file elf_symbols.c.

243  {
244  bool found;
245  ht_uu_find(set, offset, &found);
246 
247  return found;
248 }

References found.

Referenced by compute_symbols_from_segment().

◆ is_section_local_symbol()

static bool is_section_local_symbol ( ELFOBJ bin,
Elf_(Sym) *  symbol 
)
static

Definition at line 180 of file elf_symbols.c.

180  {
181  if (symbol->st_name != 0) {
182  return false;
183  }
184  if (ELF_ST_TYPE(symbol->st_info) != STT_SECTION) {
185  return false;
186  }
187  if (ELF_ST_BIND(symbol->st_info) != STB_LOCAL) {
188  return false;
189  }
190  if (symbol->st_shndx >= bin->ehdr.e_shnum) {
191  return false;
192  }
193 
194  return true;
195 }
#define STB_LOCAL
Definition: common.h:480
#define STT_SECTION
Definition: common.h:491
#define ELF_ST_BIND(val)
Definition: common.h:455
#define ELF_ST_TYPE(val)
Definition: common.h:456

References ELF_ST_BIND, ELF_ST_TYPE, STB_LOCAL, and STT_SECTION.

Referenced by set_elf_symbol_name().

◆ is_special_arm_symbol()

static bool is_special_arm_symbol ( ELFOBJ bin,
Elf_(Sym) *  sym,
const char *  name 
)
static

Definition at line 100 of file elf_symbols.c.

100  {
101  if (!name) {
102  return false;
103  }
104 
105  if (name[0] != '$') {
106  return false;
107  }
108 
109  if (name[1] == 'a' || name[1] == 't' || name[1] == 'd' || name[1] == 'x') {
110  return (name[2] == '\0' || name[2] == '.') &&
111  ELF_ST_TYPE(sym->st_info) == STT_NOTYPE &&
112  ELF_ST_BIND(sym->st_info) == STB_LOCAL &&
113  ELF_ST_VISIBILITY(sym->st_info) == STV_DEFAULT;
114  }
115 
116  return false;
117 }
#define STV_DEFAULT
Definition: common.h:520
#define STT_NOTYPE
Definition: common.h:488
#define ELF_ST_VISIBILITY(v)
Definition: common.h:471
Definition: z80asm.h:102

References ELF_ST_BIND, ELF_ST_TYPE, ELF_ST_VISIBILITY, STB_LOCAL, STT_NOTYPE, and STV_DEFAULT.

Referenced by is_special_symbol().

◆ is_special_symbol()

static bool is_special_symbol ( ELFOBJ bin,
Elf_(Sym) *  sym,
const char *  name 
)
static

Definition at line 119 of file elf_symbols.c.

119  {
120  switch (bin->ehdr.e_machine) {
121  case EM_ARM:
122  case EM_AARCH64:
123  return is_special_arm_symbol(bin, sym, name);
124  default:
125  return false;
126  }
127 }
#define EM_AARCH64
Definition: elf_specs.h:161
static bool is_special_arm_symbol(ELFOBJ *bin, Elf_(Sym) *sym, const char *name)
Definition: elf_symbols.c:100
#define EM_ARM
Definition: common.h:129

References EM_AARCH64, EM_ARM, and is_special_arm_symbol().

Referenced by symbol_type_to_str().

◆ rz_bin_elf_compute_symbols()

RZ_OWN RzVector* Elf_() rz_bin_elf_compute_symbols ( ELFOBJ bin,
RzBinElfSymbolFilter  filter 
)

Definition at line 486 of file elf_symbols.c.

486  {
488  if (!result) {
489  return NULL;
490  }
491 
492  HtUU *set = ht_uu_new0();
493  if (!set) {
494  rz_vector_free(result);
495  return NULL;
496  }
497 
498  if (!get_dynamic_elf_symbols(bin, result, filter, set)) {
499  rz_vector_free(result);
500  ht_uu_free(set);
501  return NULL;
502  }
503 
504  if (!get_section_elf_symbols(bin, result, filter, set)) {
505  rz_vector_free(result);
506  ht_uu_free(set);
507  return NULL;
508  }
509 
510  // Parsing .gnu_debugdata is completely optional, ignore errors if any and just continue
511  (void)get_gnu_debugdata_elf_symbols(bin, result, filter, set);
512 
513  ht_uu_free(set);
514 
515  if (!rz_vector_len(result)) {
516  rz_vector_free(result);
517  return NULL;
518  }
519 
520  return result;
521 }
static bool get_section_elf_symbols(ELFOBJ *bin, RzVector *result, RzBinElfSymbolFilter filter, HtUU *set)
Definition: elf_symbols.c:325
static bool get_gnu_debugdata_elf_symbols(ELFOBJ *bin, RzVector *result, RzBinElfSymbolFilter filter, HtUU *set)
Definition: elf_symbols.c:367
static bool get_dynamic_elf_symbols(ELFOBJ *bin, RzVector *result, RzBinElfSymbolFilter filter, HtUU *set)
Definition: elf_symbols.c:295
RZ_API RzVector * rz_vector_new(size_t elem_size, RzVectorFree free, void *free_user)
Definition: vector.c:42
static size_t rz_vector_len(const RzVector *vec)
Definition: rz_vector.h:82

References elf_symbol_fini(), get_dynamic_elf_symbols(), get_gnu_debugdata_elf_symbols(), get_section_elf_symbols(), NULL, rz_vector_free(), rz_vector_len(), and rz_vector_new().

Referenced by get_gnu_debugdata_elf_symbols(), rz_bin_elf_analyse_imports(), and rz_bin_elf_symbols_new().

◆ rz_bin_elf_get_symbol()

RZ_BORROW RzBinElfSymbol* Elf_() rz_bin_elf_get_symbol ( RZ_NONNULL ELFOBJ bin,
ut32  ordinal 
)

Definition at line 473 of file elf_symbols.c.

473  {
475 
476  RzBinElfSymbol *symbol;
478  if (symbol->ordinal == ordinal) {
479  return symbol;
480  }
481  }
482 
483  return NULL;
484 }
#define rz_bin_elf_foreach_symbols(bin, symbol)
Definition: elf.h:46
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108

References NULL, rz_bin_elf_symbol_t::ordinal, rz_bin_elf_foreach_symbols, and rz_return_val_if_fail.

◆ rz_bin_elf_has_symbols()

bool Elf_() rz_bin_elf_has_symbols ( RZ_NONNULL ELFOBJ bin)

Definition at line 534 of file elf_symbols.c.

534  {
535  rz_return_val_if_fail(bin, false);
536  return bin->symbols;
537 }

References rz_return_val_if_fail.

◆ rz_bin_elf_symbols_new()

RZ_OWN RzVector* Elf_() rz_bin_elf_symbols_new ( RZ_NONNULL ELFOBJ bin)

Definition at line 523 of file elf_symbols.c.

523  {
525 
527  if (!result) {
528  return NULL;
529  }
530 
531  return result;
532 }
static bool filter_symbol(RZ_UNUSED ELFOBJ *bin, Elf_(Sym) *symbol, RZ_UNUSED bool is_dynamic)
Definition: elf_symbols.c:439

References Elf_(), filter_symbol(), NULL, rz_bin_elf_compute_symbols(), and rz_return_val_if_fail.

Referenced by init().

◆ set_elf_symbol_name()

static bool set_elf_symbol_name ( ELFOBJ bin,
struct symbols_segment segment,
RzBinElfSymbol elf_symbol,
Elf_(Sym) *  symbol,
RzBinElfSection section 
)
static

Definition at line 197 of file elf_symbols.c.

197  {
198  if (section && is_section_local_symbol(bin, symbol)) {
199  elf_symbol->name = rz_str_new(section->name);
200  return elf_symbol->name;
201  }
202 
203  if (!segment->strtab) {
204  return false;
205  }
206 
207  elf_symbol->name = Elf_(rz_bin_elf_strtab_get_dup)(segment->strtab, symbol->st_name);
208  if (!elf_symbol->name) {
209  return false;
210  }
211 
212  return true;
213 }
RZ_OWN char *Elf_() rz_bin_elf_strtab_get_dup(RZ_NONNULL RzBinElfStrtab *strtab, ut64 index)
Definition: elf_strtab.c:52
static bool is_section_local_symbol(ELFOBJ *bin, Elf_(Sym) *symbol)
Definition: elf_symbols.c:180
RZ_API char * rz_str_new(const char *str)
Definition: str.c:865

References Elf_(), is_section_local_symbol(), rz_bin_elf_symbol_t::name, rz_bin_elf_strtab_get_dup(), rz_str_new(), and symbols_segment::strtab.

Referenced by convert_elf_symbol_entry().

◆ symbol_bind_to_str()

static const char* symbol_bind_to_str ( Elf_(Sym) *  sym)
static

Definition at line 143 of file elf_symbols.c.

143  {
144  for (size_t i = 0; i < RZ_ARRAY_SIZE(symbol_bind_translation_table); i++) {
145  if (ELF_ST_BIND(sym->st_info) == symbol_bind_translation_table[i].bind) {
147  }
148  }
149 
151 }
static struct symbol_bind_translation symbol_bind_translation_table[]
Definition: elf_symbols.c:30
#define RZ_BIN_BIND_UNKNOWN_STR
Definition: rz_bin.h:115
#define RZ_ARRAY_SIZE(x)
Definition: rz_types.h:300
unsigned char bind
Definition: elf_symbols.c:21

References symbol_bind_translation::bind, ELF_ST_BIND, i, symbol_bind_translation::name, RZ_ARRAY_SIZE, RZ_BIN_BIND_UNKNOWN_STR, and symbol_bind_translation_table.

Referenced by convert_elf_symbol_entry().

◆ symbol_type_to_str()

static const char* symbol_type_to_str ( ELFOBJ bin,
RzBinElfSymbol ret,
Elf_(Sym) *  sym 
)
static

Definition at line 129 of file elf_symbols.c.

129  {
130  if (bin && ret && is_special_symbol(bin, sym, ret->name)) {
132  }
133 
134  for (size_t i = 0; i < RZ_ARRAY_SIZE(symbol_type_translation_table); i++) {
135  if (ELF_ST_TYPE(sym->st_info) == symbol_type_translation_table[i].type) {
137  }
138  }
139 
141 }
static const struct symbol_type_translation symbol_type_translation_table[]
Definition: elf_symbols.c:41
static bool is_special_symbol(ELFOBJ *bin, Elf_(Sym) *sym, const char *name)
Definition: elf_symbols.c:119
#define RZ_BIN_TYPE_UNKNOWN_STR
Definition: rz_bin.h:134
#define RZ_BIN_TYPE_SPECIAL_SYM_STR
Definition: rz_bin.h:133
unsigned char type
Definition: elf_symbols.c:26

References ELF_ST_TYPE, i, is_special_symbol(), rz_bin_elf_symbol_t::name, symbol_type_translation::name, RZ_ARRAY_SIZE, RZ_BIN_TYPE_SPECIAL_SYM_STR, RZ_BIN_TYPE_UNKNOWN_STR, symbol_type_translation_table, and symbol_type_translation::type.

Referenced by convert_elf_symbol_entry().

◆ symbols_segment_init()

static struct symbols_segment symbols_segment_init ( ut64  offset,
ut64  number,
ut64  entry_size,
bool  dynamic,
RzBinElfStrtab strtab 
)
static

Definition at line 1 of file elf_symbols.c.

56  {
57  return (struct symbols_segment){
58  .offset = offset,
59  .number = number,
60  .entry_size = entry_size,
61  .dynamic = dynamic,
62  .strtab = strtab
63  };
64 }
int dynamic(struct state *s)
Definition: puff.c:665

Referenced by get_dynamic_elf_symbols(), and get_section_elf_symbols().

Variable Documentation

◆ symbol_bind_translation_table

struct symbol_bind_translation symbol_bind_translation_table[]
static
Initial value:
= {
}
#define STB_NUM
Definition: glibc_elf.h:581
#define STB_LOPROC
Definition: common.h:485
#define STB_HIPROC
Definition: common.h:486
#define STB_GLOBAL
Definition: common.h:481
#define STB_HIOS
Definition: common.h:484
#define STB_LOOS
Definition: common.h:483
#define STB_WEAK
Definition: common.h:482
#define RZ_BIN_BIND_WEAK_STR
Definition: rz_bin.h:108
#define RZ_BIN_BIND_HIPROC_STR
Definition: rz_bin.h:113
#define RZ_BIN_BIND_LOPROC_STR
Definition: rz_bin.h:112
#define RZ_BIN_BIND_HIOS_STR
Definition: rz_bin.h:111
#define RZ_BIN_BIND_NUM_STR
Definition: rz_bin.h:109
#define RZ_BIN_BIND_LOCAL_STR
Definition: rz_bin.h:106
#define RZ_BIN_BIND_LOOS_STR
Definition: rz_bin.h:110
#define RZ_BIN_BIND_GLOBAL_STR
Definition: rz_bin.h:107

Definition at line 1 of file elf_symbols.c.

Referenced by symbol_bind_to_str().

◆ symbol_type_translation_table

const struct symbol_type_translation symbol_type_translation_table[]
static
Initial value:
= {
}
#define STT_NUM
Definition: glibc_elf.h:597
#define STT_COMMON
Definition: common.h:493
#define STT_HIPROC
Definition: common.h:500
#define STT_HIOS
Definition: common.h:498
#define STT_FILE
Definition: common.h:492
#define STT_FUNC
Definition: common.h:490
#define STT_OBJECT
Definition: common.h:489
#define STT_TLS
Definition: common.h:494
#define STT_LOPROC
Definition: common.h:499
#define STT_LOOS
Definition: common.h:497
#define RZ_BIN_TYPE_HIOS_STR
Definition: rz_bin.h:130
#define RZ_BIN_TYPE_COMMON_STR
Definition: rz_bin.h:126
#define RZ_BIN_TYPE_OBJECT_STR
Definition: rz_bin.h:118
#define RZ_BIN_TYPE_TLS_STR
Definition: rz_bin.h:127
#define RZ_BIN_TYPE_NOTYPE_STR
Definition: rz_bin.h:117
#define RZ_BIN_TYPE_NUM_STR
Definition: rz_bin.h:128
#define RZ_BIN_TYPE_LOOS_STR
Definition: rz_bin.h:129
#define RZ_BIN_TYPE_LOPROC_STR
Definition: rz_bin.h:131
#define RZ_BIN_TYPE_FILE_STR
Definition: rz_bin.h:125
#define RZ_BIN_TYPE_FUNC_STR
Definition: rz_bin.h:119
#define RZ_BIN_TYPE_HIPROC_STR
Definition: rz_bin.h:132
#define RZ_BIN_TYPE_SECTION_STR
Definition: rz_bin.h:124

Definition at line 1 of file elf_symbols.c.

Referenced by symbol_type_to_str().