History

Add function to list all symbols, for purpose of linking separate in-memory compilations

Jan Boon committed 7 years ago

Commit 944c4001 parent c6635503 files · +37 −0Browse files

Unified · Split

3 files
  1. libtcc.h +4 −0
  2. tcc.h +2 −0
  3. tccelf.c +31 −0
libtcc.h+4−0
@@ -93,6 +93,10 @@ LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr); · 92 unmodified lines
9393 /* return symbol value or NULL if not found */
9494 LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name);
9595
96+/* return symbol value or NULL if not found */
97+LIBTCCAPI void tcc_list_symbols(TCCState *s, void *ctx,
98+ void (*symbol_cb)(void *ctx, const char *name, const void *val));
99+
96100 #ifdef __cplusplus
97101 }
98102 #endif
tcc.h+2−0
@@ -1470,6 +1470,8 @@ ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc); · 1469 unmodified lines
14701470 ST_FUNC void squeeze_multi_relocs(Section *sec, size_t oldrelocoffset);
14711471
14721472 ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err);
1473+ST_FUNC void list_elf_symbols(TCCState *s, void *ctx,
1474+ void (*symbol_cb)(void *ctx, const char *name, const void *val));
14731475 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
14741476 ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name);
14751477 #endif
tccelf.c+31−0
@@ -471,12 +471,43 @@ ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err) · 470 unmodified lines
471471 return sym->st_value;
472472 }
473473
474+/* list elf symbol names and values */
475+ST_FUNC void list_elf_symbols(TCCState *s, void *ctx,
476+ void (*symbol_cb)(void *ctx, const char *name, const void *val))
477+{
478+ ElfW(Sym) *sym;
479+ Section *symtab;
480+ int sym_index, end_sym;
481+ const char *name;
482+ unsigned char sym_vis, sym_bind;
483+
484+ symtab = s->symtab;
485+ end_sym = symtab->data_offset / sizeof (ElfSym);
486+ for (sym_index = 0; sym_index < end_sym; ++sym_index) {
487+ sym = &((ElfW(Sym) *)symtab->data)[sym_index];
488+ if (sym->st_value) {
489+ name = (char *) symtab->link->data + sym->st_name;
490+ sym_bind = ELFW(ST_BIND)(sym->st_info);
491+ sym_vis = ELFW(ST_VISIBILITY)(sym->st_other);
492+ if (sym_bind == STB_GLOBAL && sym_vis == STV_DEFAULT)
493+ symbol_cb(ctx, name, (void*)(uintptr_t)sym->st_value);
494+ }
495+ }
496+}
497+
474498 /* return elf symbol value */
475499 LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name)
476500 {
477501 return (void*)(uintptr_t)get_elf_sym_addr(s, name, 0);
478502 }
479503
504+/* list elf symbol names and values */
505+LIBTCCAPI void tcc_list_symbols(TCCState *s, void *ctx,
506+ void (*symbol_cb)(void *ctx, const char *name, const void *val))
507+{
508+ list_elf_symbols(s, ctx, symbol_cb);
509+}
510+
480511 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
481512 /* return elf symbol value or error */
482513 ST_FUNC void* tcc_get_symbol_err(TCCState *s, const char *name)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t