static int slurp_symtab(bfd *abfd, struct a2l_data *a2l) { long storage; long symcount; asymbol **syms; bfd_boolean dynamic = FALSE; if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0) return bfd_error(bfd_get_filename(abfd)); storage = bfd_get_symtab_upper_bound(abfd); if (storage == 0L) { storage = bfd_get_dynamic_symtab_upper_bound(abfd); dynamic = TRUE; } if (storage < 0L) return bfd_error(bfd_get_filename(abfd)); syms = malloc(storage); if (dynamic) symcount = bfd_canonicalize_dynamic_symtab(abfd, syms); else symcount = bfd_canonicalize_symtab(abfd, syms); if (symcount < 0) { free(syms); return bfd_error(bfd_get_filename(abfd)); } a2l->syms = syms; return 0; }
static CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname) { long storage_needed; asymbol *sym; asymbol **symbol_table; unsigned int number_of_symbols; unsigned int i; struct cleanup *back_to; CORE_ADDR symaddr = 0; storage_needed = bfd_get_symtab_upper_bound (abfd); if (storage_needed > 0) { symbol_table = (asymbol **) xmalloc (storage_needed); back_to = make_cleanup (xfree, symbol_table); number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); for (i = 0; i < number_of_symbols; i++) { sym = *symbol_table++; if (strcmp (sym->name, symname) == 0) { /* Bfd symbols are section relative. */ symaddr = sym->value + sym->section->vma; break; } } do_cleanups (back_to); } if (symaddr) return symaddr; /* Look for the symbol in the dynamic string table too. */ storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd); if (storage_needed > 0) { symbol_table = (asymbol **) xmalloc (storage_needed); back_to = make_cleanup (xfree, symbol_table); number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table); for (i = 0; i < number_of_symbols; i++) { sym = *symbol_table++; if (strcmp (sym->name, symname) == 0) { /* Bfd symbols are section relative. */ symaddr = sym->value + sym->section->vma; break; } } do_cleanups (back_to); } return symaddr; }
static bool arch_bfdInit(pid_t pid, bfd_t * bfdParams) { char fname[PATH_MAX]; snprintf(fname, sizeof(fname), "/proc/%d/exe", pid); if ((bfdParams->bfdh = bfd_openr(fname, 0)) == NULL) { LOG_E("bfd_openr(%s) failed", fname); return false; } if (!bfd_check_format(bfdParams->bfdh, bfd_object)) { LOG_E("bfd_check_format() failed"); return false; } int storage_needed = bfd_get_symtab_upper_bound(bfdParams->bfdh); if (storage_needed <= 0) { LOG_E("bfd_get_symtab_upper_bound() returned '%d'", storage_needed); return false; } if ((bfdParams->syms = (asymbol **) malloc(storage_needed)) == NULL) { PLOG_E("malloc(%d) failed", storage_needed); return false; } bfd_canonicalize_symtab(bfdParams->bfdh, bfdParams->syms); if ((bfdParams->section = bfd_get_section_by_name(bfdParams->bfdh, ".text")) == NULL) { LOG_E("bfd_get_section_by_name('.text') failed"); return false; } return true; }
static CORE_ADDR lookup_symbol_from_bfd (bfd *abfd, char *symname) { long storage_needed; asymbol **symbol_table; unsigned int number_of_symbols; unsigned int i; CORE_ADDR symaddr = 0; storage_needed = bfd_get_symtab_upper_bound (abfd); if (storage_needed <= 0) return 0; symbol_table = (asymbol **) xmalloc (storage_needed); number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); for (i = 0; i < number_of_symbols; i++) { asymbol *sym = symbol_table[i]; if (strcmp (sym->name, symname) == 0 && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0) { /* BFD symbols are section relative. */ symaddr = sym->value + sym->section->vma; break; } } xfree (symbol_table); return symaddr; }
unsigned long getsym(bfd *obj, char* symbol) { long storage_needed; asymbol **symbol_table; long number_of_symbols; int i; storage_needed = bfd_get_symtab_upper_bound (obj); if (storage_needed <= 0){ printl(PRINT_HIGH, "no symbols in object file\n"); exit(-1); } symbol_table = (asymbol **) malloc (storage_needed); number_of_symbols = bfd_canonicalize_symtab(obj, symbol_table); //notes: symbol_table[i]->flags & (BSF_FUNCTION | BSF_GLOBAL) for (i = 0; i < number_of_symbols; i++) { if(!strcmp(symbol, symbol_table[i]->name)){ return symbol_table[i]->section->vma + symbol_table[i]->value; } } printl(PRINT_DEBUG, "Unable to find symbol named %s\n", symbol); return 0; }
static long get_sym_offset(char *filename, char *sym) { bfd *objfile; size_t symtabsize, numsyms, i; long offset = -1; asymbol **symtab = 0; objfile = bfd_openr(filename, 0); if (!objfile) goto bail; if (!bfd_check_format(objfile, bfd_object)) goto bail; /* Get the symbol table */ symtabsize = bfd_get_symtab_upper_bound(objfile); symtab = (asymbol **) malloc (symtabsize); if (!symtab) { fprintf(stderr, "Out of memory.\n"); exit(1); } numsyms = bfd_canonicalize_symtab(objfile, symtab); /* Walk the symbol table */ for (i=0; i < numsyms; i++) if (strcmp(bfd_asymbol_name(symtab[i]),sym)==0) { offset = symtab[i]->value + symtab[i]->section->filepos; break; } bail: if (objfile) bfd_close(objfile); if (symtab) free(symtab); return offset; }
int msp430_get_current_source_location (int mypc, const char ** pfilename, const char ** pfunctionname, unsigned int * plineno) { static int initted = 0; if (current_bfd == NULL) { printf("no bfd\n"); return 0; } if (!initted) { int storage; asection * s; initted = 1; memset (& info, 0, sizeof (info)); INIT_DISASSEMBLE_INFO (info, stdout, op_printf); info.read_memory_func = sim_dis_read; info.arch = bfd_get_arch (current_bfd); info.mach = bfd_get_mach (current_bfd); if (info.mach == 0) info.arch = bfd_arch_msp430; disassemble_init_for_target (& info); storage = bfd_get_symtab_upper_bound (current_bfd); if (storage > 0) { symtab = (asymbol **) xmalloc (storage); symcount = bfd_canonicalize_symtab (current_bfd, symtab); symcount = remove_useless_symbols (symtab, symcount); qsort (symtab, symcount, sizeof (asymbol *), compare_symbols); } for (s = current_bfd->sections; s; s = s->next) { if (s->flags & SEC_CODE || code_section == 0) { code_section = s; code_base = bfd_section_lma (current_bfd, s); break; } } } *pfilename = *pfunctionname = NULL; *plineno = 0; bfd_find_nearest_line (current_bfd, code_section, symtab, mypc - code_base, pfilename, pfunctionname, plineno); return 1; }
static void check_local_sym_xref (lang_input_statement_type *statement) { bfd *abfd; lang_input_statement_type *li; asymbol **asymbols, **syms; abfd = statement->the_bfd; if (abfd == NULL) return; li = abfd->usrdata; if (li != NULL && li->asymbols != NULL) asymbols = li->asymbols; else { long symsize; long symbol_count; symsize = bfd_get_symtab_upper_bound (abfd); if (symsize < 0) einfo (_("%B%F: could not read symbols; %E\n"), abfd); asymbols = xmalloc (symsize); symbol_count = bfd_canonicalize_symtab (abfd, asymbols); if (symbol_count < 0) einfo (_("%B%F: could not read symbols: %E\n"), abfd); if (li != NULL) { li->asymbols = asymbols; li->symbol_count = symbol_count; } } for (syms = asymbols; *syms; ++syms) { asymbol *sym = *syms; if (sym->flags & (BSF_GLOBAL | BSF_WARNING | BSF_INDIRECT | BSF_FILE)) continue; if ((sym->flags & (BSF_LOCAL | BSF_SECTION_SYM)) != 0 && sym->section->output_section != NULL) { const char *outsecname, *symname; struct lang_nocrossrefs *ncrs; struct lang_nocrossref *ncr; outsecname = sym->section->output_section->name; symname = NULL; if ((sym->flags & BSF_SECTION_SYM) == 0) symname = sym->name; for (ncrs = nocrossref_list; ncrs != NULL; ncrs = ncrs->next) for (ncr = ncrs->list; ncr != NULL; ncr = ncr->next) if (strcmp (ncr->name, outsecname) == 0) check_refs (symname, FALSE, sym->section, abfd, ncrs); } } if (li == NULL) free (asymbols); }
int main(int argc, char *argv[]) { bfd *abfd; asection *text; long storage_needed; asymbol **symbol_table; long number_of_symbols; long i; char **matching; sec_ptr section; char *symbol_name; long symbol_offset, section_vma, symbol_address; if (argc < 2) return 0; printf("Open %s\n", argv[1]); bfd_init(); abfd = bfd_openr(argv[1],NULL); if (abfd == (bfd *) 0) { bfd_perror("bfd_openr"); return -1; } if (!bfd_check_format_matches(abfd, bfd_object, &matching)) { return -1; } if (!(bfd_get_file_flags (abfd) & HAS_SYMS)) { printf("ERROR flag!\n"); return -1; } if ((storage_needed = bfd_get_symtab_upper_bound(abfd)) < 0) return -1; symbol_table = (asymbol **) xmalloc(storage_needed); number_of_symbols = bfd_canonicalize_symtab(abfd, symbol_table); if (number_of_symbols < 0) return -1; fun_table = (FUN_TABLE **)malloc(sizeof(FUN_TABLE)*number_of_symbols); bzero(fun_table, sizeof(FUN_TABLE)*number_of_symbols); for (i = 0; i < number_of_symbols; i++) { if (symbol_table[i]->flags & (BSF_FUNCTION|BSF_GLOBAL)) { section = symbol_table[i]->section; section_vma = bfd_get_section_vma(abfd, section); symbol_name = symbol_table[i]->name; symbol_offset = symbol_table[i]->value; symbol_address = section_vma + symbol_offset; if (section->flags & SEC_CODE) { add_function_table(symbol_name, symbol_address); } } } bfd_close(abfd); /* 將函式對照表作排序 */ qsort(fun_table, table_count, sizeof(FUN_TABLE), compare_function); dump_function_table(); }
extern void DEBUG_LoadSymbols( const char *name ) { bfd* abfd; char **matching; bfd_init(); abfd = bfd_openr(name, "default"); if (abfd == NULL) { barf("can't open executable %s to get symbol table", name); } if (!bfd_check_format_matches (abfd, bfd_object, &matching)) { barf("mismatch"); } { long storage_needed; asymbol **symbol_table; long number_of_symbols; long num_real_syms = 0; long i; storage_needed = bfd_get_symtab_upper_bound (abfd); if (storage_needed < 0) { barf("can't read symbol table"); } symbol_table = (asymbol **) stgMallocBytes(storage_needed,"DEBUG_LoadSymbols"); number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); if (number_of_symbols < 0) { barf("can't canonicalise symbol table"); } if (add_to_fname_table == NULL) add_to_fname_table = allocHashTable(); for( i = 0; i != number_of_symbols; ++i ) { symbol_info info; bfd_get_symbol_info(abfd,symbol_table[i],&info); if (isReal(info.type, info.name)) { insertHashTable(add_to_fname_table, info.value, (void*)info.name); num_real_syms += 1; } } IF_DEBUG(interpreter, debugBelch("Loaded %ld symbols. Of which %ld are real symbols\n", number_of_symbols, num_real_syms) ); stgFree(symbol_table); } }
static int elf_read_symbols(elf_reader_t *reader, const char *path, elfread_src_t srcsec) { int i, nsymbols, nfiltered, iflt; size_t storage_needed; reader->__abfd = bfd_openr(path, NULL); if (!reader->__abfd) return -1; bfd_check_format(reader->__abfd, bfd_object); storage_needed = (srcsec == READSYMBOLS_TEXT) ? bfd_get_symtab_upper_bound(reader->__abfd) : bfd_get_dynamic_symtab_upper_bound(reader->__abfd); if (storage_needed <= 0) { return -1; } reader->__symbol_table = (asymbol**)malloc(storage_needed); if (!reader->__symbol_table) return -1; nsymbols = (srcsec == READSYMBOLS_TEXT) ? bfd_canonicalize_symtab(reader->__abfd, reader->__symbol_table) : bfd_canonicalize_dynamic_symtab(reader->__abfd, reader->__symbol_table); if (nsymbols < 0) return -1; nfiltered = 0; for (i = 0; i < nsymbols; i++) { if (reader->__symbol_table[i]->flags & (BSF_FUNCTION | BSF_GLOBAL)) nfiltered++; } reader->symbols = (elf_symbol_t *)malloc(nfiltered * sizeof(elf_symbol_t)); if (!reader->symbols) return -1; for (i = 0, iflt = 0; i < nsymbols && iflt < nfiltered; i++) { asymbol *is = reader->__symbol_table[i]; if (is->flags & (BSF_FUNCTION | BSF_GLOBAL)) { elf_symbol_t *os = reader->symbols + iflt++; os->symbol_name = (const char *)bfd_asymbol_name(is); os->symbol_value = bfd_asymbol_value(is); os->symbol_size = BFD_GET_SYMBOL_SIZE(is); os->symbol_class = (char)bfd_decode_symclass(is); } } assert(iflt == nfiltered); return iflt; }
void get_symbols(asymbol*** psyms, int* psymnum, bfd* abfd) { long storage; storage = bfd_get_symtab_upper_bound(abfd); assert(storage >= 0); if (storage) *psyms = (asymbol**)malloc(storage); *psymnum = bfd_canonicalize_symtab(abfd, *psyms); assert(*psymnum >= 0); return; }
static void resolve(unw_word_t addr, const char** file, unsigned int* line, const char** func) { static bool can_give_lineinfo = true; if (!can_give_lineinfo) { return; } if (!current_executable) { static char exec_name[PATH_MAX]; int len = readlink("/proc/self/exe", exec_name, PATH_MAX); if (len < 0) { std::cerr << "Unable to find the current executable, so backtrace won't display line information." << std::endl; can_give_lineinfo = false; return; } bfd_init(); current_executable = bfd_openr(exec_name, 0); if (!current_executable) { std::cerr << "Unable to initialize libbfd, so backtrace won't display line information." << std::endl; can_give_lineinfo = false; return; } /* this is a required check. no idea why. */ bfd_check_format(current_executable, bfd_object); unsigned storage_needed = bfd_get_symtab_upper_bound(current_executable); syms = (asymbol**) malloc(storage_needed); unsigned cSymbols = bfd_canonicalize_symtab(current_executable, syms); /* Get the text section so we can 'un-relocate' symbols. */ text = bfd_get_section_by_name(current_executable, ".text"); } unsigned offset = addr - text->vma; if (offset > 0) { /* Address is in the current executable! Great! */ int success = bfd_find_nearest_line(current_executable, text, syms, offset, file, func, line); if (*file && success) { return; } } }
static void nlm_symtab_read (bfd *abfd, CORE_ADDR addr, struct objfile *objfile) { long storage_needed; asymbol *sym; asymbol **symbol_table; long number_of_symbols; long i; struct cleanup *back_to; CORE_ADDR symaddr; enum minimal_symbol_type ms_type; storage_needed = bfd_get_symtab_upper_bound (abfd); if (storage_needed < 0) error ("Can't read symbols from %s: %s", bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ())); if (storage_needed > 0) { symbol_table = (asymbol **) xmalloc (storage_needed); back_to = make_cleanup (xfree, symbol_table); number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); if (number_of_symbols < 0) error ("Can't read symbols from %s: %s", bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ())); for (i = 0; i < number_of_symbols; i++) { sym = symbol_table[i]; if ( /*sym -> flags & BSF_GLOBAL */ 1) { /* Bfd symbols are section relative. */ symaddr = sym->value + sym->section->vma; /* Relocate all non-absolute symbols by base address. */ if (sym->section != &bfd_abs_section) symaddr += addr; /* For non-absolute symbols, use the type of the section they are relative to, to intuit text/data. BFD provides no way of figuring this out for absolute symbols. */ if (sym->section->flags & SEC_CODE) ms_type = mst_text; else if (sym->section->flags & SEC_DATA) ms_type = mst_data; else ms_type = mst_unknown; prim_record_minimal_symbol (sym->name, symaddr, ms_type, objfile); } } do_cleanups (back_to); } }
/* Get the memory bank parameters by looking at the global symbols defined by the linker. */ static int sim_get_bank_parameters (SIM_DESC sd, bfd* abfd) { sim_cpu *cpu; long symsize; long symbol_count, i; unsigned size; asymbol** asymbols; asymbol** current; cpu = STATE_CPU (sd, 0); symsize = bfd_get_symtab_upper_bound (abfd); if (symsize < 0) { sim_io_eprintf (sd, "Cannot read symbols of program"); return 0; } asymbols = (asymbol **) xmalloc (symsize); symbol_count = bfd_canonicalize_symtab (abfd, asymbols); if (symbol_count < 0) { sim_io_eprintf (sd, "Cannot read symbols of program"); return 0; } size = 0; for (i = 0, current = asymbols; i < symbol_count; i++, current++) { const char* name = bfd_asymbol_name (*current); if (strcmp (name, BFD_M68HC11_BANK_START_NAME) == 0) { cpu->bank_start = bfd_asymbol_value (*current); } else if (strcmp (name, BFD_M68HC11_BANK_SIZE_NAME) == 0) { size = bfd_asymbol_value (*current); } else if (strcmp (name, BFD_M68HC11_BANK_VIRTUAL_NAME) == 0) { cpu->bank_virtual = bfd_asymbol_value (*current); } } free (asymbols); cpu->bank_end = cpu->bank_start + size; cpu->bank_shift = 0; for (; size > 1; size >>= 1) cpu->bank_shift++; return 0; }
// Symbol extraction std::vector<asymbol*> slurpSymtab(bfd* abfd) { long storage_needed = bfd_get_symtab_upper_bound (abfd); if (storage_needed <= 0) return std::vector<asymbol*>(); std::vector<asymbol*> res(storage_needed); long symcount = bfd_canonicalize_symtab (abfd, &res[0]); res.resize(symcount); return std::move(res); }
/** * Obtain the information of dynamic symbol (library call) */ dyn_functions_t *get_synthetic_symbols(const char *filename) { asymbol** syms = NULL; long symcount = 0; asymbol** dynsyms = NULL; long dynsymcount = 0; asymbol* synthsyms = NULL; long synthcount = 0; long storage; vector<dyn_function_t *> *ret = new vector<dyn_function_t *>(); asm_program_t *prog = new asm_program_t; bfd *abfd = initialize_bfd(filename); initialize_sections(abfd, prog); if (bfd_get_file_flags (abfd) & HAS_SYMS) { storage = bfd_get_symtab_upper_bound (abfd); assert (storage >= 0); if (storage > 0) syms = (asymbol**) xmalloc(storage); symcount = bfd_canonicalize_symtab (abfd, syms); assert (symcount >= 0); } storage = bfd_get_dynamic_symtab_upper_bound (abfd); if (storage > 0) { dynsyms = (asymbol**) xmalloc(storage); dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, dynsyms); } synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms, dynsymcount, dynsyms, &synthsyms); if (synthcount < 0) synthcount = 0; for (int i=0; i<synthcount; ++i) { dyn_function_t *temp = new dyn_function_t; temp->name = string(bfd_asymbol_name(&(synthsyms[i]))); temp->addr = bfd_asymbol_value(&(synthsyms[i])); ret->push_back(temp); } if (synthsyms) free(synthsyms); if (dynsyms) free(dynsyms); if (syms) free(syms); bfd_close(abfd); return ret; }
static void add_bfd(bfd *Bfd) { if (bfd_check_format(Bfd, bfd_object)) { bfd_info_t *BfdInfo = new(bfd_info_t); BfdInfo->FileName = Bfd->filename; memset(BfdInfo->LocalTable, 0, sizeof(BfdInfo->LocalTable)); BfdInfo->Symbols = (asymbol **)malloc(bfd_get_symtab_upper_bound(Bfd)); int NoOfSymbols = bfd_canonicalize_symtab(Bfd, BfdInfo->Symbols); bfd_map_over_sections(Bfd, (bfd_map)add_bfd_section, BfdInfo); for (int I = NoOfSymbols - 1; I >= 0; --I) { asymbol *Sym = BfdInfo->Symbols[I]; if (Sym->flags & BSF_GLOBAL) { const char *Name = strdup(Sym->name); symbol_t *Symbol = new_symbol(Name, (section_t *)Sym->section->userdata, Sym->value); stringtable_put(BfdInfo->LocalTable, Name, Symbol); stringtable_put(GlobalTable, Name, Symbol); } else if (Sym->section == bfd_com_section_ptr) { symbol_t *Symbol = (symbol_t *)stringtable_get(GlobalTable, Sym->name); bss_section_t *Section; if (!Symbol) { const char *Name = strdup(Sym->name); Section = new_bss_section(0); Symbol = new_symbol(Name, (section_t *)Section, 0); stringtable_put(GlobalTable, Name, Symbol); stringtable_put(BfdInfo->LocalTable, Name, Symbol); } else { Section = (bss_section_t *)Symbol->Section; }; if (Sym->value > Section->Size) Section->Size = Sym->value; } else if (Sym->flags & BSF_LOCAL) { const char *Name = strdup(Sym->name); symbol_t *Symbol = new_symbol(Name, (section_t *)Sym->section->userdata, Sym->value); stringtable_put(BfdInfo->LocalTable, Name, Symbol); } else if (Sym->flags & BSF_WEAK) { const char *Name = strdup(Sym->name); symbol_t *Symbol = new_symbol(Name, (section_t *)Sym->section->userdata, Sym->value); stringtable_put(WeakTable, Name, Symbol); } else if (Sym->section == bfd_und_section_ptr) { } else if (Sym->flags & BSF_DEBUGGING) { // This may be supported later } else { printf("%s: unknown symbol type: %8x.\n", Bfd->filename, Sym->flags); exit(1); }; }; } else if (bfd_check_format(Bfd, bfd_archive)) { bfd *Bfd2 = 0; while ((Bfd2 = bfd_openr_next_archived_file(Bfd, Bfd2))) add_bfd(Bfd2); }; };
long _bfd_generic_read_minisymbols (bfd *abfd, bfd_boolean dynamic, void **minisymsp, unsigned int *sizep) { long storage; asymbol **syms = NULL; long symcount; if (dynamic) storage = bfd_get_dynamic_symtab_upper_bound (abfd); else storage = bfd_get_symtab_upper_bound (abfd); if (storage < 0) goto error_return; if (storage == 0) return 0; syms = (asymbol **) bfd_malloc (storage); if (syms == NULL) goto error_return; if (dynamic) symcount = bfd_canonicalize_dynamic_symtab (abfd, syms); else symcount = bfd_canonicalize_symtab (abfd, syms); if (symcount < 0) goto error_return; *minisymsp = syms; *sizep = sizeof (asymbol *); return symcount; error_return: bfd_set_error (bfd_error_no_symbols); if (syms != NULL) free (syms); return -1; }
long locate_dv_func(void) { asymbol **symbol_table; bfd *b = bfd_openr(oracle, NULL); if (b == NULL) { perror("bfd_openr"); exit(-1); } bfd_check_format(b, bfd_object); long storage_needed = bfd_get_symtab_upper_bound(b); if(storage_needed < 0) { fprintf(stderr, "wtf?!\n"); exit(-1); } if((symbol_table = (asymbol**)malloc(storage_needed)) == 0) { perror("malloc"); exit(-1); } int num_symbols; if((num_symbols = bfd_canonicalize_symtab(b, symbol_table)) <= 0) { fprintf(stderr, "no symbols info\n"); exit(-1); } int i; for(i = 0; i < num_symbols; i++) { char *symname = bfd_asymbol_name(symbol_table[i]); void *symaddr = bfd_asymbol_value(symbol_table[i]); /* don't even ask why this funciton, for real hardcore: gdb -p <oraclePIDs> */ if(!strcmp(symname, "kzvtins")) { fprintf(stderr, "[%d] symbol \"kzvtins\" at 0x%lx\n", getpid(), (long) symaddr); return (long) symaddr; } } return 0; }
static void read_syms(bfd *abfd) { long storage, symcount; bfd_boolean dynamic = FALSE; if (syms) return; if (!(bfd_get_file_flags(abfd) & HAS_SYMS)) { wpa_printf(MSG_INFO, "No symbols"); return; } storage = bfd_get_symtab_upper_bound(abfd); if (storage == 0) { storage = bfd_get_dynamic_symtab_upper_bound(abfd); dynamic = TRUE; } if (storage < 0) { wpa_printf(MSG_INFO, "Unknown symtab upper bound"); return; } syms = malloc(storage); if (syms == NULL) { wpa_printf(MSG_INFO, "Failed to allocate memory for symtab " "(%ld bytes)", storage); return; } if (dynamic) symcount = bfd_canonicalize_dynamic_symtab(abfd, syms); else symcount = bfd_canonicalize_symtab(abfd, syms); if (symcount < 0) { wpa_printf(MSG_INFO, "Failed to canonicalize %ssymtab", dynamic ? "dynamic " : ""); free(syms); syms = NULL; return; } }
// Read in the symbol table. static bfd_boolean slurp_symtab (bfd *abfd, asymbol ***syms, long *symcount) { long storage; if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0) return FALSE; storage = bfd_get_symtab_upper_bound (abfd); if (storage < 0) return FALSE; *syms = (asymbol **) GlobalAlloc(GMEM_FIXED, storage); if (*syms == NULL) return FALSE; if((*symcount = bfd_canonicalize_symtab (abfd, *syms)) < 0) return FALSE; return TRUE; }
static unsigned long find_limit (bfd *prog_bfd) { struct bfd_symbol **asymbols; long symsize; long symbol_count; long s; symsize = bfd_get_symtab_upper_bound (prog_bfd); if (symsize < 0) return 0; asymbols = (asymbol **) xmalloc (symsize); symbol_count = bfd_canonicalize_symtab (prog_bfd, asymbols); if (symbol_count < 0) return 0; for (s = 0; s < symbol_count; s++) { if (!strcmp (asymbols[s]->name, "_fstack")) return (asymbols[s]->value + 65536) & ~(0xffffUL); } return 0; }
bool ElfFile::readExports() { long storage_needed; asymbol **symbol_table; long number_of_symbols; long i; m_symbols.clear(); bfd_set_default_target("bfd_target_elf_flavour"); m_bfd = bfd_openr(m_name.toStdString().c_str(),NULL); if(!bfd_check_format(m_bfd,bfd_object)) { return false; } storage_needed = bfd_get_symtab_upper_bound(m_bfd); symbol_table = (asymbol **)malloc(storage_needed); number_of_symbols = bfd_canonicalize_symtab(m_bfd, symbol_table); qDebug() << "Number of symbols " << number_of_symbols; for(i = 0; i < number_of_symbols; i++) { m_symbols << SymbolDescription( symbol_table[i]->section->name, symbol_table[i]->name, symbol_table[i]->flags, symbol_table[i]->value); qDebug() << "found " << symbol_table[i]->name; } free(symbol_table); symbol_table = NULL; return true; }
int dicos_load_module_p (bfd *abfd, int header_size) { long storage_needed; int ret = 0; asymbol **symbol_table = NULL; const char *symname = "Dicos_loadModuleInfo"; asection *section; /* DICOS files don't have a .note.ABI-tag marker or something similar. We do know there's always a "header" section of HEADER_SIZE bytes (size depends on architecture), and there's always a "Dicos_loadModuleInfo" symbol defined. Look for the section first, as that should be cheaper. */ section = bfd_get_section_by_name (abfd, "header"); if (!section) return 0; if (bfd_section_size (abfd, section) != header_size) return 0; /* Dicos LMs always have a "Dicos_loadModuleInfo" symbol defined. Look for it. */ storage_needed = bfd_get_symtab_upper_bound (abfd); if (storage_needed < 0) { warning (_("Can't read elf symbols from %s: %s"), bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ())); return 0; } if (storage_needed > 0) { long i, symcount; symbol_table = xmalloc (storage_needed); symcount = bfd_canonicalize_symtab (abfd, symbol_table); if (symcount < 0) warning (_("Can't read elf symbols from %s: %s"), bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ())); else { for (i = 0; i < symcount; i++) { asymbol *sym = symbol_table[i]; if (sym->name != NULL && symname[0] == sym->name[0] && strcmp (symname + 1, sym->name + 1) == 0) { ret = 1; break; } } } } xfree (symbol_table); return ret; }
void core_init (const char * aout_name) { int core_sym_bytes; asymbol *synthsyms; long synth_count; core_bfd = bfd_openr (aout_name, 0); if (!core_bfd) { perror (aout_name); done (1); } if (!bfd_check_format (core_bfd, bfd_object)) { fprintf (stderr, _("%s: %s: not in executable format\n"), whoami, aout_name); done (1); } /* Get core's text section. */ core_text_sect = bfd_get_section_by_name (core_bfd, ".text"); if (!core_text_sect) { core_text_sect = bfd_get_section_by_name (core_bfd, "$CODE$"); if (!core_text_sect) { fprintf (stderr, _("%s: can't find .text section in %s\n"), whoami, aout_name); done (1); } } /* Read core's symbol table. */ /* This will probably give us more than we need, but that's ok. */ core_sym_bytes = bfd_get_symtab_upper_bound (core_bfd); if (core_sym_bytes < 0) { fprintf (stderr, "%s: %s: %s\n", whoami, aout_name, bfd_errmsg (bfd_get_error ())); done (1); } core_syms = (asymbol **) xmalloc (core_sym_bytes); core_num_syms = bfd_canonicalize_symtab (core_bfd, core_syms); if (core_num_syms < 0) { fprintf (stderr, "%s: %s: %s\n", whoami, aout_name, bfd_errmsg (bfd_get_error ())); done (1); } synth_count = bfd_get_synthetic_symtab (core_bfd, core_num_syms, core_syms, 0, NULL, &synthsyms); if (synth_count > 0) { asymbol **symp; long new_size; long i; new_size = (core_num_syms + synth_count + 1) * sizeof (*core_syms); core_syms = (asymbol **) xrealloc (core_syms, new_size); symp = core_syms + core_num_syms; core_num_syms += synth_count; for (i = 0; i < synth_count; i++) *symp++ = synthsyms + i; *symp = 0; } min_insn_size = 1; offset_to_code = 0; switch (bfd_get_arch (core_bfd)) { case bfd_arch_vax: case bfd_arch_tahoe: offset_to_code = 2; break; case bfd_arch_alpha: min_insn_size = 4; break; default: break; } if (function_mapping_file) read_function_mappings (function_mapping_file); }
void sim_disasm_one (void) { static int initted = 0; static asymbol **symtab = 0; static int symcount = 0; static int last_sym = -1; static struct disassemble_info info; int storage, sym, bestaddr; int min, max, i; static asection *code_section = 0; static bfd_vma code_base = 0; asection *s; int save_trace = trace; static const char *prev_filename = ""; static int prev_lineno = 0; const char *filename; const char *functionname; unsigned int lineno; int mypc = get_reg (pc); if (current_bfd == 0) return; trace = 0; if (!initted) { initted = 1; memset (&info, 0, sizeof (info)); INIT_DISASSEMBLE_INFO (info, stdout, op_printf); info.read_memory_func = sim_dis_read; info.arch = bfd_get_arch (current_bfd); info.mach = bfd_get_mach (current_bfd); if (info.mach == 0) { info.arch = bfd_arch_m32c; info.mach = default_machine; } disassemble_init_for_target (&info); storage = bfd_get_symtab_upper_bound (current_bfd); if (storage > 0) { symtab = (asymbol **) malloc (storage); symcount = bfd_canonicalize_symtab (current_bfd, symtab); symcount = remove_useless_symbols (symtab, symcount); qsort (symtab, symcount, sizeof (asymbol *), compare_symbols); } for (s = current_bfd->sections; s; s = s->next) { if (s->flags & SEC_CODE || code_section == 0) { code_section = s; code_base = bfd_section_lma (current_bfd, s); break; } } } filename = functionname = 0; lineno = 0; if (bfd_find_nearest_line (current_bfd, code_section, symtab, mypc - code_base, &filename, &functionname, &lineno)) { if (filename && functionname && lineno) { if (lineno != prev_lineno || strcmp (prev_filename, filename)) { char *the_line = load_file_and_line (filename, lineno); const char *slash = strrchr (filename, '/'); if (!slash) slash = filename; else slash++; printf ("========================================" "=====================================\n"); printf ("\033[37;41m %s:%d: \033[33;40m %s\033[K\033[0m\n", slash, lineno, the_line); } prev_lineno = lineno; prev_filename = filename; } } { min = -1; max = symcount; while (min < max - 1) { bfd_vma sa; sym = (min + max) / 2; sa = bfd_asymbol_value (symtab[sym]); /*printf("checking %4d %08x %s\n", sym, sa, bfd_asymbol_name (symtab[sym])); */ if (sa > mypc) max = sym; else if (sa < mypc) min = sym; else { min = sym; break; } } if (min != -1 && min != last_sym) { bestaddr = bfd_asymbol_value (symtab[min]); printf ("\033[43;30m%s", bfd_asymbol_name (symtab[min])); if (bestaddr != mypc) printf ("+%d", mypc - bestaddr); printf (":\t\t\t\033[0m\n"); last_sym = min; #if 0 if (trace == 1) if (strcmp (bfd_asymbol_name (symtab[min]), "abort") == 0 || strcmp (bfd_asymbol_name (symtab[min]), "exit") == 0) trace = 0; #endif } } opbuf[0] = 0; printf ("\033[33m%06x: ", mypc); max = print_insn_m32c (mypc, &info); for (i = 0; i < max; i++) printf ("%02x", mem_get_qi (mypc + i)); for (; i < 6; i++) printf (" "); printf ("%-16s ", opbuf); printf ("\033[0m\n"); trace = save_trace; }
int main(int argc, char *argv[]) { bfd *abfd; bfd_init(); abfd = bfd_openr(argv[1], NULL); if (abfd == NULL) { bfd_perror("bfd_openr"); exit(1); } if (! bfd_check_format(abfd, bfd_object)) { bfd_perror("bfd_check_format"); } printf("SYMBOL TABLE:\n"); { long storage_needed; asymbol **symbol_table; long number_of_symbols; long i; storage_needed = bfd_get_symtab_upper_bound (abfd); printf("storage_need=%d\n", storage_needed); if (storage_needed < 0) { bfd_perror("bfd_get_symtab_upper_bound"); exit(1); } if (storage_needed == 0) { printf("no symbols\n"); exit(0); } symbol_table = (asymbol **)malloc (storage_needed); number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); if (number_of_symbols < 0) { bfd_perror("bfd_canonicalize_symtab"); exit(1); } for (i = 0; i < number_of_symbols; i++) { asymbol *asym = symbol_table[i]; int symclass = bfd_decode_symclass(asym); symbol_info syminfo; bfd_symbol_info(asym, &syminfo); bfd_print_symbol_vandf(abfd, stdout, asym); printf(" 0x%x %s ", symclass, bfd_is_undefined_symclass(symclass) ? "?" : " "); printf(" %s ", bfd_asymbol_name(asym)); printf("%p ", bfd_asymbol_value(asym)); // printf(" %d ", syminfo.value); /* asymbol_value */ // printf(" %d ", syminfo.type); /* symclass */ // printf(" %s ", syminfo.name); /* asymbol_name */ printf(" %d ", syminfo.stab_type); printf(" %d ", syminfo.stab_other); printf(" %d ", syminfo.stab_desc); // printf(" %s ", syminfo.stab_name); printf("\n"); } } printf("DYNAMIC SYMBOL TABLE:\n"); { long storage_needed; asymbol **symbol_table; long number_of_symbols; long i; storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd); printf("storage_need=%d\n", storage_needed); if (storage_needed < 0) { bfd_perror("bfd_get_symtab_upper_bound"); exit(1); } if (storage_needed == 0) { printf("no symbols\n"); exit(0); } symbol_table = (asymbol **)malloc (storage_needed); number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table); if (number_of_symbols < 0) { bfd_perror("bfd_canonicalize_symtab"); exit(1); } for (i = 0; i < number_of_symbols; i++) { asymbol *asym = symbol_table[i]; int symclass = bfd_decode_symclass(asym); symbol_info syminfo; bfd_symbol_info(asym, &syminfo); bfd_print_symbol_vandf(abfd, stdout, asym); printf(" 0x%x %s ", symclass, bfd_is_undefined_symclass(symclass) ? "?" : " "); printf(" %s ", bfd_asymbol_name(asym)); printf("%p ", bfd_asymbol_value(asym)); // printf(" %d ", syminfo.value); /* asymbol_value */ // printf(" %d ", syminfo.type); /* symclass */ // printf(" %s ", syminfo.name); /* asymbol_name */ printf(" %d ", syminfo.stab_type); printf(" %d ", syminfo.stab_other); printf(" %d ", syminfo.stab_desc); // printf(" %s ", syminfo.stab_name); printf("\n"); } } exit(0); }
/** * @brief initialization of a symbol table * * @param filename * @param arch_name */ void init_symbol_table(char* filename, char* arch_name) { long i,j, digit; ENTRY newentry, *oldentry; SYM_FUNC *symp; asymbol *symptr; int key; bfd *abfd; if(!filename){ skyeye_info("Can not get correct kernel filename!Maybe your skyeye.conf have something wrong!\n"); return; } abfd = bfd_openr (filename, get_bfd_target(arch_name)); if (!bfd_check_format(abfd, bfd_object)) { bfd_close(abfd); skyeye_log(Debug_log, __FUNCTION__, "wrong bfd format\n"); return; //exit(0); } storage_needed = bfd_get_symtab_upper_bound(abfd); if (storage_needed < 0){ printf("FAIL\n"); exit(0); } symbol_table = (asymbol **) skyeye_mm_zero (storage_needed); if(symbol_table == NULL){ fprintf(stderr, "Can not alloc memory for symbol table.\n"); exit(0); } number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); kernel_number = number_of_symbols; /* <tktan> BUG200106022219 */ if (number_of_symbols < 0){ printf("FAIL\n"); exit(0); } if (!hcreate(number_of_symbols << 1)) { printf("Not enough memory for hash table\n"); exit(0); } for (i = 0; i < number_of_symbols; i++) { symptr = symbol_table[i] ; key = symptr->value + symptr->section->vma; // adjust for section address if (((i<kernel_number) && (symbol_table[i]->flags == 0x01)) || // <tktan> BUG200105172154, BUG200106022219 ((i<kernel_number) && (symbol_table[i]->flags == 0x02)) || // <tktan> BUG200204051654 (symbol_table[i]->flags & 0x10)) { // Is a function symbol // printf("%x %8x %s\n", symbol_table[i]->flags, key, symbol_table[i]->name); // *********************************************************** // This is converting the function symbol value to char string // and use it as a key in the GNU hash table // ******************************************************** newentry.key = (char *) skyeye_mm_zero(9); for (j=0;j<8;j++) { newentry.key[j] = itoa_tab[((key) >> (j << 2)) & 0xf] ; } newentry.key[8] = 0 ; // ************************************************* // This is allocating memory for a struct funcsym // ************************************************* symp = (SYM_FUNC *) skyeye_mm_zero(sizeof(SYM_FUNC)); newentry.data = (char *) symp; symp->name = (char *) symbol_table[i]->name ; symp->total_cycle = 0; symp->total_energy = 0; symp->instances = 0; // *********************************************** // Insert into hash table // ******************************************* /* <tktan> BUG200106022219 */ oldentry = hsearch(newentry, FIND) ; if (oldentry) { // was entered // printf("Duplicate Symbol: %x %s\n", key, symp->name); oldentry->data = (char *) symp ; } else if (!hsearch(newentry, ENTER)) { printf("Insufficient memory\n"); exit(0) ; } } }
static void BFDmanager_loadBFDdata (char *file, bfd **image, asymbol ***symbols, unsigned *nDataSymbols, data_symbol_t **DataSymbols) { bfd *bfdImage = NULL; asymbol **bfdSymbols = NULL; if (nDataSymbols) *nDataSymbols = 0; if (DataSymbols) *DataSymbols = NULL; /* Open the binary file in read-only mode */ bfdImage = bfd_openr (file, NULL); if (bfdImage == NULL) { const char *errmsg = bfd_errmsg (bfd_get_error()); fprintf (stderr, "mpi2prv: WARNING! Cannot open binary file '%s': %s.\n" " Addresses will not be translated into source code references\n", file, errmsg); return; } /* Check the binary file format */ if (!bfd_check_format (bfdImage, bfd_object)) { const char *errmsg = bfd_errmsg( bfd_get_error() ); fprintf (stderr, "mpi2prv: WARNING! Binary file format does not match for file '%s' : %s\n" " Addresses will not be translated into source code references\n", file, errmsg); } /* Load the mini-Symbol Table */ if (bfd_get_file_flags (bfdImage) & HAS_SYMS) { long symcount; size_t size = bfd_get_symtab_upper_bound (bfdImage); if (size > 0) { #if defined(BFD_MANAGER_GENERATE_ADDRESSES) long s; unsigned nDataSyms = 0; data_symbol_t *DataSyms = NULL; #endif bfdSymbols = (asymbol**) malloc (size); if (bfdSymbols == NULL) FATAL_ERROR ("Cannot allocate memory to translate addresses into source code references\n"); #if 0 /* HSG This is supposed to be space-efficient, but showed some errors .... :( */ symcount = bfd_read_minisymbols (bfdImage, FALSE, (PTR) bfdSymbols, &usize); if (symcount == 0) symcount = bfd_read_minisymbols (bfdImage, TRUE, (PTR) bfdSymbols, &usize); #else symcount = bfd_canonicalize_symtab (bfdImage, bfdSymbols); # if defined(BFD_MANAGER_GENERATE_ADDRESSES) if (nDataSymbols && DataSymbols) { for (s = 0; s < symcount; s++) { symbol_info syminfo; bfd_get_symbol_info (bfdImage, bfdSymbols[s], &syminfo); if (((bfdSymbols[s]->flags & BSF_DEBUGGING) == 0) && (syminfo.type == 'R' || syminfo.type == 'r' || /* read-only data */ syminfo.type == 'B' || syminfo.type == 'b' || /* uninited data */ syminfo.type == 'G' || syminfo.type == 'g' || /* inited data */ syminfo.type == 'C')) /* common data*/ { unsigned long long sz = 0; if (bfd_get_flavour(bfdImage) == bfd_target_elf_flavour) sz = ((elf_symbol_type*) bfdSymbols[s])->internal_elf_sym.st_size; DataSyms = (data_symbol_t*) realloc (DataSyms, sizeof(data_symbol_t)*(nDataSyms+1)); if (DataSyms == NULL) FATAL_ERROR ("Cannot allocate memory to allocate data symbols\n"); DataSyms[nDataSyms].name = strdup (syminfo.name); DataSyms[nDataSyms].address = (void*) syminfo.value; DataSyms[nDataSyms].size = sz; nDataSyms++; } } *nDataSymbols = nDataSyms; *DataSymbols = DataSyms; } # endif #endif if (symcount < 0) { /* There aren't symbols! */ const char *errmsg = bfd_errmsg( bfd_get_error() ); fprintf(stderr, "mpi2prv: WARNING! Cannot read symbol table for file '%s' : %s\n" " Addresses will not be translated into source code references\n", file, errmsg); } } } *image = bfdImage; *symbols = bfdSymbols; #if defined(DEBUG) printf ("BFD file=%s bfdImage = %p bfdSymbols = %p\n", file, bfdImage, bfdSymbols); #endif }