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); }
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 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; }
static void exec_close(quitting) { register struct vmap *vp, *nxt; struct objfile *obj; for (nxt = vmap; vp = nxt; ) { nxt = vp->nxt; /* if there is an objfile associated with this bfd, free_objfile() will do proper cleanup of objfile *and* bfd. */ if (obj = lookup_objfile_bfd (vp->bfd)) free_objfile (obj); else bfd_close(vp->bfd); free_named_symtabs(vp->name); free(vp); } vmap = 0; if (exec_bfd) { bfd_close (exec_bfd); exec_bfd = NULL; } if (exec_ops.to_sections) { free (exec_ops.to_sections); exec_ops.to_sections = NULL; exec_ops.to_sections_end = NULL; } }
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); }
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 void exec_close (int quitting) { int need_symtab_cleanup = 0; struct vmap *vp, *nxt; for (nxt = vmap; nxt != NULL;) { vp = nxt; nxt = vp->nxt; /* if there is an objfile associated with this bfd, free_objfile() will do proper cleanup of objfile *and* bfd. */ if (vp->objfile) { free_objfile (vp->objfile); need_symtab_cleanup = 1; } else if (vp->bfd != exec_bfd) /* FIXME-leak: We should be freeing vp->name too, I think. */ if (!bfd_close (vp->bfd)) warning (_("cannot close \"%s\": %s"), vp->name, bfd_errmsg (bfd_get_error ())); /* FIXME: This routine is #if 0'd in symfile.c. What should we be doing here? Should we just free everything in vp->objfile->symtabs? Should free_objfile do that? FIXME-as-well: free_objfile already free'd vp->name, so it isn't valid here. */ free_named_symtabs (vp->name); xfree (vp); } vmap = NULL; if (exec_bfd) { char *name = bfd_get_filename (exec_bfd); if (!bfd_close (exec_bfd)) warning (_("cannot close \"%s\": %s"), name, bfd_errmsg (bfd_get_error ())); xfree (name); exec_bfd = NULL; exec_bfd_mtime = 0; } if (exec_ops.to_sections) { xfree (exec_ops.to_sections); exec_ops.to_sections = NULL; exec_ops.to_sections_end = NULL; } }
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 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 }
void close_bfd_ctx(bfd_ctx * bc) { if (bc) { free(bc->symbol); bfd_close(bc->handle); } }
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; }
/* ------------------------------------------------------------------------- */ SIM_RC sim_load (SIM_DESC sd, char *prog, struct bfd *abfd, int from_tty) { bfd *prog_bfd; #ifdef OR32_SIM_DEBUG printf ("sim_load called\n"); #endif /* Use the built in loader, which will in turn use our write function. */ prog_bfd = sim_load_file (sd, sd->myname, sd->callback, prog, abfd, sd->is_debug, 0, sim_write); if (NULL == prog_bfd) { return SIM_RC_FAIL; } /* If the BFD was not already open, then close the loaded program. */ if (NULL == abfd) { bfd_close (prog_bfd); } return SIM_RC_OK; } /* sim_load () */
/* Read resource file */ rc_res_directory * read_res_file (const char *fn) { rc_uint_type off, flen; windres_bfd wrbfd; bfd *abfd; asection *sec; filename = fn; flen = (rc_uint_type) get_file_size (filename); if (! flen) fatal ("can't open '%s' for input.", filename); abfd = windres_open_as_binary (filename, 1); sec = bfd_get_section_by_name (abfd, ".data"); if (sec == NULL) bfd_fatal ("bfd_get_section_by_name"); set_windres_bfd (&wrbfd, abfd, sec, (target_is_bigendian ? WR_KIND_BFD_BIN_B : WR_KIND_BFD_BIN_L)); off = 0; if (! probe_binary (&wrbfd, flen)) set_windres_bfd_endianess (&wrbfd, ! target_is_bigendian); skip_null_resource (&wrbfd, &off, flen); while (read_resource_entry (&wrbfd, &off, flen)) ; bfd_close (abfd); return resources; }
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; }
void unloadModule(void) { if (m_abfd) bfd_close(m_abfd); m_abfd = 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; }
void target_bfd_xclose (struct target_ops *t, int quitting) { bfd_close (t->to_data); xfree (t->to_sections); xfree (t); }
void plugin_maybe_claim (struct ld_plugin_input_file *file, lang_input_statement_type *entry) { int claimed = 0; /* We create a dummy BFD, initially empty, to house whatever symbols the plugin may want to add. */ file->handle = plugin_get_ir_dummy_bfd (entry->the_bfd->filename, entry->the_bfd); if (plugin_call_claim_file (file, &claimed)) einfo (_("%P%F: %s: plugin reported error claiming file\n"), plugin_error_plugin ()); /* fd belongs to us, not the plugin; but we don't need it. */ close (file->fd); if (claimed) { /* Discard the real file's BFD and substitute the dummy one. */ /* BFD archive handling caches elements so we can't call bfd_close for archives. */ if (entry->the_bfd->my_archive == NULL) bfd_close (entry->the_bfd); entry->the_bfd = file->handle; entry->flags.claimed = TRUE; bfd_make_readable (entry->the_bfd); } else { /* If plugin didn't claim the file, we don't need the dummy bfd. Can't avoid speculatively creating it, alas. */ bfd_close_all_done (file->handle); entry->flags.claimed = FALSE; } }
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 void disasm_close(struct disasm_data data) { if (data.bfd_file) { bfd_close(data.bfd_file); } }
static void close_bfd_ctx (struct bfd_ctx* bc) { free(bc->symbol); if (bc->handle) { bfd_close(bc->handle); } }
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 }
ELFFile::~ELFFile() { free (this->syms); free (this->dynsyms); free (this->sorted_syms); free (this->synthsyms); bfd_close (this->abfd); }
UVDBFDObject::~UVDBFDObject() { if( m_bfd ) { bfd_close(m_bfd); m_bfd = NULL; } }
void BinaryFile::Destroy() { if (_abfd) { bfd_close(_abfd); _abfd = NULL; } _symbols.clear(); }
static void _symtab_fini (struct symtab *symtab) { if (symtab->syms != NULL) free (symtab->syms); if (symtab->bfd != NULL) bfd_close (symtab->bfd); }
static void addr2line_cleanup(struct a2l_data *a2l) { if (a2l->abfd) bfd_close(a2l->abfd); zfree((char **)&a2l->input); zfree(&a2l->syms); free(a2l); }
traceback::Bfd::Context::~Context() { if (image) LogTraceObj(L"[%s]", image); HostFree(memory::heap::DefaultStat, symbol); if (handle) bfd_close(handle); HostFree(memory::heap::DefaultStat, image); }
static void arch_bfdDestroy(bfd_t * bfdParams) { if (bfdParams->syms) { free(bfdParams->syms); } if (bfdParams->bfdh) { bfd_close(bfdParams->bfdh); } }
/* 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; }