/* do we need this? --aij raw_insts_t * raw_insts_of_file(const char *filename) { //segs = NULL; raw_insts_t *ret = new raw_insts_t; asm_program_t *prog = new asm_program_t; bfd *abfd = initialize_bfd(filename); initialize_sections(abfd, prog); prog->functions = identify_functions(prog->sections, abfd); for(map<address_t, asm_function_t *>::const_iterator it = prog->functions.begin(); it != prog->functions.end(); it++){ asm_function_t *f = it->second; bfd_vma pc = f->start_addr; // Disassemble the function while(pc <= f->end_addr){ bfd_byte *ptr = get_ptr_to_instr(prog, pc); raw_inst_t *temp = new raw_inst_t; temp->length = instr->length; temp->bytes = ptr; temp->address = instr->address; ret->push_back(temp); pc += instr->length; } } return ret; } */ asm_program_t * disassemble_program(const char *filename) { asm_program_t *prog = new asm_program_t; //segs = NULL; bfd *abfd = initialize_bfd(filename); prog->abfd = abfd; initialize_sections(abfd, prog); prog->functions = identify_functions(prog->sections, abfd); unsigned long total_instrs =0; set<asm_function_t *> functions; for(map<address_t, asm_function_t *>::const_iterator it = prog->functions.begin(); it != prog->functions.end(); it++){ // Give each function a unique ID (for BGL) // and fill in control flow graph, etc for function asm_function_t *f = it->second; //f->cg_num = cg_num++; disassemble_function(f, prog); functions.insert(f); if(is_debug_on("stats")){ for(map<address_t, Instruction *>::const_iterator iit = f->instmap.begin(); iit != f->instmap.end(); iit++){ total_instrs++; } } } print_debug("stats", "Size of program %u (instrs)\n", total_instrs); //create_callgraph(prog); return prog; }
/** * 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; }
asm_program_t * asmir_open_file(const char *filename, bfd_vma base) { asm_program_t *prog = malloc(sizeof(asm_program_t)); if (!prog) return NULL; bfd *abfd = initialize_bfd(filename); if (!abfd) { free(prog); return NULL; } prog->abfd = abfd; initialize_sections(prog, base); return prog; }
address_t get_last_segment_address(const char *filename, address_t addr) { bfd *abfd = initialize_bfd(filename); unsigned int opb = bfd_octets_per_byte(abfd); address_t ret = addr; for(asection *section = abfd->sections; section != (asection *) NULL; section = section->next){ bfd_vma datasize = bfd_get_section_size_before_reloc(section); if(datasize == 0) continue; address_t start = section->vma; address_t end = section->vma + datasize/opb; if(addr >= start && addr <= end) return end; } return ret; }
vine_symbols_t * get_symbols_of_file(const char *filename) { asymbol** syms = NULL; long symcount = 0; long sorted_symcount = 0; asymbol** dynsyms = NULL; long dynsymcount = 0; asymbol* synthsyms = NULL; long synthcount = 0; long storage; vector<vine_symbol_t *> *ret = new vector<vine_symbol_t *>(); // asm_program_t *prog = new asm_program_t; // printf("initializing bfd\n\n"); bfd *abfd = initialize_bfd(filename); // printf("done\n\n"); // printf("initializing sections...\n\n"); // initialize_sections(abfd, prog); // printf("done\n\n"); asymbol *sym; 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); sorted_symcount = remove_useless_symbols(syms, symcount); qsort(syms, sorted_symcount, sizeof(asymbol *), compare_symbols); for(int i= 0; i < sorted_symcount; i++){ vine_symbol_t *temp = new vine_symbol_t; temp->name = string(bfd_asymbol_name((syms[i]))); temp->addr = bfd_asymbol_value((syms[i])); sym = syms[i]; temp->is_function = (sym->flags & BSF_FUNCTION) != 0; temp->is_dynamic = 0; ret->push_back(temp); } } 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++) { vine_symbol_t *temp = new vine_symbol_t; temp->name = string(bfd_asymbol_name(&(synthsyms[i]))); temp->addr = bfd_asymbol_value(&(synthsyms[i])); temp->is_function = (synthsyms[i].flags & BSF_FUNCTION) != 0; temp->is_dynamic = 1; ret->push_back(temp); } if (synthsyms) free(synthsyms); if (dynsyms) free(dynsyms); if (syms) free(syms); bfd_close(abfd); return ret; }