static bfd * open_bfd(const char *fname) { bfd *abfd; char **matching; abfd = bfd_openr(prg_fname, NULL); if (abfd == NULL) { wpa_printf(MSG_INFO, "bfd_openr failed"); return NULL; } if (bfd_check_format(abfd, bfd_archive)) { wpa_printf(MSG_INFO, "bfd_check_format failed"); bfd_close(abfd); return NULL; } if (!bfd_check_format_matches(abfd, bfd_object, &matching)) { wpa_printf(MSG_INFO, "bfd_check_format_matches failed"); free(matching); bfd_close(abfd); return NULL; } return abfd; }
bool loadModule(void) { char **matches; m_abfd = bfd_openr(m_path.c_str(), NULL); if (!m_abfd) return false; if (bfd_check_format(m_abfd, bfd_archive)) { std::cerr << m_path << ": Failed to read addresses from archive.\n"; unloadModule(); return false; } if (!bfd_check_format_matches(m_abfd, bfd_object, &matches)) { std::cerr << m_path << ": Could not determine the format.\n"; if (bfd_get_error() == bfd_error_file_ambiguously_recognized) { std::cerr << m_path << ": The archive format is ambiguous.\n"; free(matches); } unloadModule(); return false; } return true; }
bfd_boolean ar_emul_default_create (bfd **abfd_out, char *archive_file_name, char *file_name) { char *target = NULL; /* Try to figure out the target to use for the archive from the first object on the list. */ if (file_name != NULL) { bfd *obj; obj = bfd_openr (file_name, NULL); if (obj != NULL) { if (bfd_check_format (obj, bfd_object)) target = bfd_get_target (obj); (void) bfd_close (obj); } } /* Create an empty archive. */ *abfd_out = bfd_openw (archive_file_name, target); if (*abfd_out == NULL || ! bfd_set_format (*abfd_out, bfd_archive) || ! bfd_close (*abfd_out)) bfd_fatal (archive_file_name); return TRUE; }
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; }
SIM_RC sim_load (SIM_DESC sd, char *prog, bfd *abfd, int from_tty) { TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n", prog, from_tty)); ASSERT(prog != NULL); /* create the simulator */ TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n")); simulator = psim_create(prog, root_device); /* bring in all the data section */ psim_init(simulator); /* get the start address */ if (abfd == NULL) { abfd = bfd_openr (prog, 0); if (abfd == NULL) error ("psim: can't open \"%s\": %s\n", prog, bfd_errmsg (bfd_get_error ())); if (!bfd_check_format (abfd, bfd_object)) { const char *errmsg = bfd_errmsg (bfd_get_error ()); bfd_close (abfd); error ("psim: \"%s\" is not an object file: %s\n", prog, errmsg); } bfd_close (abfd); } return SIM_RC_OK; }
static struct a2l_data *addr2line_init(const char *path) { bfd *abfd; struct a2l_data *a2l = NULL; abfd = bfd_openr(path, NULL); if (abfd == NULL) return NULL; if (!bfd_check_format(abfd, bfd_object)) goto out; a2l = zalloc(sizeof(*a2l)); if (a2l == NULL) goto out; a2l->abfd = abfd; a2l->input = strdup(path); if (a2l->input == NULL) goto out; if (slurp_symtab(abfd, a2l)) goto out; return a2l; out: if (a2l) { zfree((char **)&a2l->input); free(a2l); } bfd_close(abfd); return NULL; }
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; }
static void hw_binary_init_data_callback(device *me) { /* get the file name */ const char *file_name = device_find_string_property(me, "file-name"); bfd *image; /* open the file */ image = bfd_openr(file_name, NULL); if (image == NULL) { bfd_perror("binary"); device_error(me, "Failed to open file %s\n", file_name); } /* check it is valid */ if (!bfd_check_format(image, bfd_object)) { bfd_close(image); device_error(me, "The file %s has an invalid binary format\n", file_name); } /* and the data sections */ bfd_map_over_sections(image, update_for_binary_section, (PTR)me); bfd_close(image); }
/* use the bfd for the process exe to initialize disassembler. FIXME: Invetigate removing this dependency. */ static void initdisassembler() { char myexepath[PATH_MAX]; char *target = NULL; if (init_done == 1) return; /* find the victim executable */ int mypid = getpid(); sprintf(myexepath, "/proc/%d/exe", mypid); bfd_init(); abfd = bfd_openr(myexepath,target); if (!abfd) { /*error no bfd to disassemble */ fprintf(stderr, "Could not load bfd for %s\n",myexepath); return; } /* make sure it's an object file */ if (!bfd_check_format (abfd, bfd_object)) { fprintf(stderr, "The bfd %s is not an object file\n",myexepath); return; } init_done = 1; }
void objdump(const char *path) { bfd_init(); bfd *abfd = bfd_openr(path, NULL); if (abfd == NULL) errx(1, bfd_errmsg(bfd_get_error())); if (!bfd_check_format(abfd, bfd_object)) { bfd_close_all_done(abfd); errx(1, "File is not a valid object file."); } printf("%s: file format %s\n", path, bfd_get_target(abfd)); printf("architecture: %s, flags: 0x%08x\n", bfd_printable_arch_mach(bfd_get_arch(abfd), bfd_get_mach(abfd)), abfd->flags); printf("start address 0x%016lx", bfd_get_start_address(abfd)); printf("\n"); printf("Sections:\n"); printf("Idx Name Size VMA LMA File off Algn\n"); printf(" Flags Content\n"); object_stats stats = { FALSE, FALSE }; bfd_map_over_sections(abfd, (void (*)(bfd *, asection *, void *))print_section, &stats); if (stats.contains_hello && stats.contains_world) printf("\nThis file might be a hello world program!\n"); bfd_close(abfd); }
static bfd *try_debug_file(const char *filename, unsigned long crc32) { int fd = open(filename, O_RDONLY); if (fd < 0) return nullptr; unsigned char buf[4*1024]; unsigned long crc = 0; while (1) { ssize_t count = read(fd, buf, sizeof(buf)); if (count <= 0) break; crc = bfd_calc_gnu_debuglink_crc32(crc, buf, count); } close(fd); if (crc != crc32) return nullptr; bfd *object = bfd_openr(filename, nullptr); if (!bfd_check_format(object, bfd_object)) { bfd_close(object); return nullptr; } return object; }
extern bool getResourceFromFile(const char *filename, MemoryBuffer &data, const char * type, unsigned id) { #ifdef _WIN32 HINSTANCE dllHandle = LoadLibraryEx(filename, NULL, LOAD_LIBRARY_AS_DATAFILE|LOAD_LIBRARY_AS_IMAGE_RESOURCE); if (dllHandle == NULL) dllHandle = LoadLibraryEx(filename, NULL, LOAD_LIBRARY_AS_DATAFILE); // the LOAD_LIBRARY_AS_IMAGE_RESOURCE flag is not supported on all versions of Windows if (dllHandle == NULL) { DBGLOG("Failed to load library %s: %d", filename, GetLastError()); return false; } HRSRC hrsrc = FindResource(dllHandle, MAKEINTRESOURCE(id), type); if (!hrsrc) return false; size32_t len = SizeofResource(dllHandle, hrsrc); const void *rdata = (const void *) LoadResource(dllHandle, hrsrc); data.append(len, rdata); FreeLibrary(dllHandle); return true; #else bfd_init (); bfd *file = bfd_openr(filename, NULL); if (file) { StringBuffer sectionName; sectionName.append(type).append("_").append(id).append(".data"); SecScanParam param(data, sectionName.str()); if (bfd_check_format (file, bfd_object)) bfd_map_over_sections (file, secscan, ¶m); bfd_close (file); } return data.length() != 0; #endif }
int BinaryFile::Init(char *filename) { Destroy(); bfd_init(); _abfd = bfd_openr(filename, NULL); if (_abfd == NULL) { perror("bfd_openr"); return -1; } bfd_boolean ret = bfd_check_format(_abfd, bfd_object); if (ret == false) { perror("bfd_check_format_matches"); return -2; } int result = InitSymbols(_symbols, SYM_FUNC); if (result < 0) { fprintf(stderr, "get symbols error!\n"); return -3; } result = InitRelsym(_rels); if (result < 0) { fprintf(stderr, "get rels error!\n"); return -3; } return 0; }
void dump_symbols(const char *filename){ bfd *abfd; asymbol *store; char *p; void *minisyms; int symnum,i; size_t size; int dyn=0; int ret; abfd=bfd_openr(filename,NULL); assert(abfd); ret=bfd_check_format(abfd,bfd_object); assert(ret); if(!(bfd_get_file_flags(abfd)&& HAS_SYMS)){ bfd_close(abfd); return; } store = bfd_make_empty_symbol(abfd); symnum = bfd_read_minisymbols(abfd,dyn,&minisyms,&size); assert(symnum>=0); p=(char *)minisyms; for(i=0;i<symnum;i++){ asymbol *sym = bfd_minisymbol_to_symbol(abfd,dyn,p,store); const char *name = bfd_asymbol_name(sym); int value = bfd_asymbol_value(sym); printf("%08x %s\n",value,name); p+=size; } free(store); free(minisyms); bfd_close(abfd); }
int symbol_init(struct kvm *kvm) { int ret = 0; if (!kvm->vmlinux) return -EINVAL; bfd_init(); abfd = bfd_openr(kvm->vmlinux, NULL); if (abfd == NULL) { bfd_error_type err = bfd_get_error(); switch (err) { case bfd_error_no_memory: ret = -ENOMEM; break; case bfd_error_invalid_target: ret = -EINVAL; break; default: ret = -EFAULT; break; } } return ret; }
extern bool getResourceFromFile(const char *filename, MemoryBuffer &data, const char * type, unsigned id) { #ifdef _WIN32 HINSTANCE dllHandle = LoadLibraryEx(filename, NULL, LOAD_LIBRARY_AS_DATAFILE|LOAD_LIBRARY_AS_IMAGE_RESOURCE); if (dllHandle == NULL) dllHandle = LoadLibraryEx(filename, NULL, LOAD_LIBRARY_AS_DATAFILE); // the LOAD_LIBRARY_AS_IMAGE_RESOURCE flag is not supported on all versions of Windows if (dllHandle == NULL) { DBGLOG("Failed to load library %s: %d", filename, GetLastError()); return false; } HRSRC hrsrc = FindResource(dllHandle, MAKEINTRESOURCE(id), type); if (!hrsrc) return false; size32_t len = SizeofResource(dllHandle, hrsrc); const void *rdata = (const void *) LoadResource(dllHandle, hrsrc); data.append(len, rdata); FreeLibrary(dllHandle); return true; #elif defined (_USE_BINUTILS) CriticalBlock block(bfdCs); bfd_init (); bfd *file = bfd_openr(filename, NULL); if (file) { StringBuffer sectionName; sectionName.append(type).append("_").append(id).append(".data"); SecScanParam param(data, sectionName.str()); if (bfd_check_format (file, bfd_object)) bfd_map_over_sections (file, secscan, ¶m); bfd_close (file); } return data.length() != 0; #else struct stat stat_buf; VStringBuffer sectname("%s_%u", type, id); int fd = open(filename, O_RDONLY); if (fd == -1 || fstat(fd, &stat_buf) == -1) { DBGLOG("Failed to load library %s: %d", filename, errno); return false; } bool ok = false; __uint64 size = stat_buf.st_size; const byte *start_addr = (const byte *) mmap(0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0); if (start_addr == MAP_FAILED) { DBGLOG("Failed to load library %s: %d", filename, errno); } else { ok = getResourceFromMappedFile(filename, start_addr, data, type, id); munmap((void *)start_addr, size); } close(fd); return ok; #endif }
static void add_object_file(const char *FileName) { bfd *Bfd = bfd_openr(FileName, 0); if (Bfd == 0) { printf("%s: error reading file.\n", FileName); return; }; add_bfd(Bfd); };
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(); }
/* Initialize disassembler (libopcodes/libbfd) for given file. * * If the the function returns data with data.bfd_file = NULL, * then the function failed. */ static struct disasm_data disasm_init(const char *filename) { asection *section; struct disasm_data data; static bool initialized = false; if (!initialized) { bfd_init(); initialized = true; } data.bfd_file = bfd_openr(filename, NULL); if (data.bfd_file == NULL) { VERB1 log_bfd_error("bfd_openr", filename); return data; } if (!bfd_check_format(data.bfd_file, bfd_object)) { VERB1 log_bfd_error("bfd_check_format", filename); goto ret_fail; } section = bfd_get_section_by_name(data.bfd_file, ".text"); if (section == NULL) { VERB1 log_bfd_error("bfd_get_section_by_name", filename); goto ret_fail; } data.disassemble = disassembler(data.bfd_file); if (data.disassemble == NULL) { VERB1 log("Unable to find disassembler"); goto ret_fail; } init_disassemble_info(&data.info, NULL, buffer_printf); data.info.arch = bfd_get_arch(data.bfd_file); data.info.mach = bfd_get_mach(data.bfd_file); data.info.buffer_vma = section->vma; data.info.buffer_length = section->size; data.info.section = section; /*TODO: memory error func*/ bfd_malloc_and_get_section(data.bfd_file, section, &data.info.buffer); disassemble_init_for_target(&data.info); return data; ret_fail: bfd_close(data.bfd_file); data.bfd_file = NULL; return data; }
static bfd_boolean ar_emul_aix_internal (bfd ** after_bfd, char * file_name, bfd_boolean verbose, const char * target_name, bfd_boolean is_append, bfd_boolean flatten ATTRIBUTE_UNUSED) { bfd *temp; bfd *try_bfd; temp = *after_bfd; /* Try 64 bit. */ try_bfd = bfd_openr (file_name, target_name); /* Failed or the object is possibly 32 bit. */ if (NULL == try_bfd || ! bfd_check_format (try_bfd, bfd_object)) try_bfd = bfd_openr (file_name, "aixcoff-rs6000"); AR_EMUL_ELEMENT_CHECK (try_bfd, file_name); if (bfd_xcoff_is_xcoff64 (try_bfd) && (! X64)) return FALSE; if (bfd_xcoff_is_xcoff32 (try_bfd) && bfd_check_format (try_bfd, bfd_object) && (! X32)) return FALSE; if (is_append) { AR_EMUL_APPEND_PRINT_VERBOSE (verbose, file_name); } else { AR_EMUL_REPLACE_PRINT_VERBOSE (verbose, file_name); } *after_bfd = try_bfd; (*after_bfd)->archive_next = temp; return TRUE; }
static int init_bfd_ctx (struct bfd_ctx *bc, const char * procname, struct output_buffer *ob) { bfd *b; void *symbol_table; unsigned dummy = 0; char **matching = NULL; bc->handle = NULL; bc->symbol = NULL; b = bfd_openr(procname, 0); if (!b) { output_print(ob, "Failed to open bfd from (%s)\n", procname); return 1; } if (bfd_check_format(b, bfd_archive)) { output_print(ob, "Cannot get addresses from archive (%s)\n", b->filename); bfd_close(b); return -1; } if (!bfd_check_format_matches(b, bfd_object, &matching)) { const char *errmsg = bfd_errmsg(bfd_get_error()); output_print(ob, "%s (%s)\n", errmsg, b->filename); if (bfd_get_error() == bfd_error_file_ambiguously_recognized) { list_matching_formats(ob, b->filename, matching); free(matching); } bfd_close(b); return -1; } if ((bfd_get_file_flags(b) & HAS_SYMS) == 0) { const char *errmsg = bfd_errmsg(bfd_get_error()); output_print(ob, "Failed to get file flags from (%s) %s\n", b->filename, errmsg); bfd_close(b); return 1; } if (bfd_read_minisymbols(b, FALSE, &symbol_table, &dummy) == 0) { if (bfd_read_minisymbols(b, TRUE, &symbol_table, &dummy) < 0) { const char *errmsg = bfd_errmsg(bfd_get_error()); output_print(ob, "Failed to read symbols from (%s): %s\n", b->filename, errmsg); free(symbol_table); bfd_close(b); return 1; } } bc->handle = b; bc->symbol = symbol_table; return 0; }
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; }
static bfd_boolean ar_emul_aix_append (bfd **after_bfd, char *file_name, const char *target, bfd_boolean verbose, bfd_boolean flatten) { bfd *new_bfd; new_bfd = bfd_openr (file_name, target); AR_EMUL_ELEMENT_CHECK (new_bfd, file_name); return do_ar_emul_append (after_bfd, new_bfd, verbose, flatten, check_aix); }
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; } } }
int main(int argc, const char* argv[]) { const char* filename = argv[1]; const char *target = "elf64-x86-64"; bfd *abfd; bfd_init(); abfd = bfd_openr(filename, target); if (abfd == NULL) { fprintf(stderr, "getsyms: '%s' : No such file", filename); return 0; } bfd_check_format(abfd, bfd_object); dump_symbols(abfd); return 0; }
int create_inferior (char *program, char **argv) { bfd *abfd; int pid = 0; char **new_argv; int nargs; abfd = bfd_openr (program, 0); if (!abfd) { fprintf (stderr, "gdbserver: cannot open %s: %s\n", program, bfd_errmsg (bfd_get_error ())); exit (1); } if (!bfd_check_format (abfd, bfd_object)) { fprintf (stderr, "gdbserver: unknown load format for %s: %s\n", program, bfd_errmsg (bfd_get_error ())); exit (1); } /* Add "-E big" or "-E little" to the argument list depending on the endianness of the program to be loaded. */ for (nargs = 0; argv[nargs] != NULL; nargs++) /* count the args */ ; new_argv = alloca (sizeof (char *) * (nargs + 3)); /* allocate new args */ for (nargs = 0; argv[nargs] != NULL; nargs++) /* copy old to new */ new_argv[nargs] = argv[nargs]; new_argv[nargs] = "-E"; new_argv[nargs + 1] = bfd_big_endian (abfd) ? "big" : "little"; new_argv[nargs + 2] = NULL; argv = new_argv; /* Create an instance of the simulator. */ default_callback.init (&default_callback); gdbsim_desc = sim_open (SIM_OPEN_STANDALONE, &default_callback, abfd, argv); if (gdbsim_desc == 0) exit (1); /* Load the program into the simulator. */ if (abfd) if (sim_load (gdbsim_desc, program, NULL, 0) == SIM_RC_FAIL) mygeneric_load (abfd); /* Create an inferior process in the simulator. This initializes SP. */ sim_create_inferior (gdbsim_desc, abfd, argv, /* env */ NULL); sim_resume (gdbsim_desc, 1, 0); /* execute one instr */ return pid; }
static bool fill_bfd_cache(const char *filename, bfd_cache *p) { bfd *abfd = bfd_openr(filename, NULL); // hard to avoid heap here! if (!abfd) return true; p->abfd = abfd; p->syms = NULL; char **match; if (bfd_check_format(abfd, bfd_archive) || !bfd_check_format_matches(abfd, bfd_object, &match) || !slurp_symtab(&p->syms, abfd)) { bfd_close(abfd); return true; } return false; }
int main (int argc, char** argv) { if (argc < 2) exit(1); const char* fn=argv[1]; fprintf(stderr,"file is %s\n",fn); bfd_init(); bfd* bfd = bfd_openr(fn, NULL); if (bfd==NULL) exit(1); if (!bfd_check_format(bfd,bfd_object)) exit(1); asection* s = bfd_get_section_by_name(bfd,".text"); if (s==NULL) exit(1); unsigned char* blob; bfd_size_type sz = s->size; fprintf(stderr,".text is %zu bytes\n",sz); if(!bfd_malloc_and_get_section(bfd,s,&blob)) exit(1); DISASM disObj; DISASM* dis = &disObj; memset(dis,0,sizeof(DISASM)); unsigned int len; unsigned char* blobb = blob; unsigned int invalid = 0; unsigned int n = 0; dis->Archi = 64; do { dis->EIP = (UIntPtr)blobb; dis->SecurityBlock = sz; len = Disasm(dis); if (len==OUT_OF_BLOCK) { fprintf(stderr,"out-of-block-error"); exit(1); } else if (len==UNKNOWN_OPCODE) { invalid++; len = 1; } //else { //puts(dis->CompleteInstr); //} blobb += len; sz-=len; n++; } while(len > 0 && sz > 0); fprintf(stderr,"decoded %u opcode sequences (%u invalid/unknown)\n", n, invalid); return (0); }
bfd_boolean ar_emul_default_replace (bfd **after_bfd, char *file_name, bfd_boolean verbose) { bfd *temp; temp = *after_bfd; *after_bfd = bfd_openr (file_name, NULL); AR_EMUL_ELEMENT_CHECK (*after_bfd, file_name); AR_EMUL_REPLACE_PRINT_VERBOSE (verbose, file_name); (*after_bfd)->archive_next = temp; return TRUE; }