📄 load.c
字号:
while (relpp < relppend && (*relpp)->address < start_address) ++relpp; } } printf ("Disassembly of section %s:\n", section->name); datasize = bfd_get_section_size_before_reloc (section); if (datasize == 0) continue; data = (bfd_byte *) xmalloc ((size_t) datasize); bfd_get_section_contents (abfd, section, data, 0, datasize); aux.sec = section; disasm_info.buffer = data; disasm_info.buffer_vma = section->vma; disasm_info.buffer_length = datasize; disasm_info.section = section; if (start_address == (bfd_vma) -1 || start_address < disasm_info.buffer_vma) addr_offset = 0; else addr_offset = start_address - disasm_info.buffer_vma; if (stop_address == (bfd_vma) -1) stop_offset = datasize / opb; else { if (stop_address < disasm_info.buffer_vma) stop_offset = 0; else stop_offset = stop_address - disasm_info.buffer_vma; if (stop_offset > disasm_info.buffer_length / opb) stop_offset = disasm_info.buffer_length / opb; } sym = find_symbol_for_address (abfd, section, section->vma + addr_offset, TRUE, &place); while (addr_offset < stop_offset) { asymbol *nextsym; unsigned long nextstop_offset; bfd_boolean insns; if (sym != NULL && bfd_asymbol_value (sym) <= section->vma + addr_offset) { int x; for (x = place; (x < sorted_symcount && bfd_asymbol_value (sorted_syms[x]) <= section->vma + addr_offset); ++x) continue; disasm_info.symbols = & sorted_syms[place]; disasm_info.num_symbols = x - place; } else disasm_info.symbols = NULL; if (! prefix_addresses) { (* disasm_info.fprintf_func) (disasm_info.stream, "\n"); objdump_print_addr_with_sym (abfd, section, sym, section->vma + addr_offset, &disasm_info, FALSE); (* disasm_info.fprintf_func) (disasm_info.stream, ":\n"); } if (sym != NULL && bfd_asymbol_value (sym) > section->vma + addr_offset) nextsym = sym; else if (sym == NULL) nextsym = NULL; else { /* Search forward for the next appropriate symbol in SECTION. Note that all the symbols are sorted together into one big array, and that some sections may have overlapping addresses. */ while (place < sorted_symcount && (sorted_syms[place]->section != section || (bfd_asymbol_value (sorted_syms[place]) <= bfd_asymbol_value (sym)))) ++place; if (place >= sorted_symcount) nextsym = NULL; else nextsym = sorted_syms[place]; } if (sym != NULL && bfd_asymbol_value (sym) > section->vma + addr_offset) { nextstop_offset = bfd_asymbol_value (sym) - section->vma; if (nextstop_offset > stop_offset) nextstop_offset = stop_offset; } else if (nextsym == NULL) nextstop_offset = stop_offset; else { nextstop_offset = bfd_asymbol_value (nextsym) - section->vma; if (nextstop_offset > stop_offset) nextstop_offset = stop_offset; } /* If a symbol is explicitly marked as being an object rather than a function, just dump the bytes without disassembling them. */ if (disassemble_all || sym == NULL || bfd_asymbol_value (sym) > section->vma + addr_offset || ((sym->flags & BSF_OBJECT) == 0 && (strstr (bfd_asymbol_name (sym), "gnu_compiled") == NULL) && (strstr (bfd_asymbol_name (sym), "gcc2_compiled") == NULL)) || (sym->flags & BSF_FUNCTION) != 0) insns = TRUE; else insns = FALSE; disassemble_bytes (&disasm_info, disassemble_fn, insns, data, addr_offset, nextstop_offset, &relpp, relppend); addr_offset = nextstop_offset; sym = nextsym; } free (data); if (relbuf != NULL) free (relbuf); } free (sorted_syms);}static asymbol **slurp_symtab (abfd) bfd *abfd;{ asymbol **sy = (asymbol **) NULL; long storage; if (!(bfd_get_file_flags (abfd) & HAS_SYMS)) { symcount = 0; return NULL; } storage = bfd_get_symtab_upper_bound (abfd); if (storage < 0) bfd_fatal (bfd_get_filename (abfd)); if (storage) sy = (asymbol **) xmalloc (storage); symcount = bfd_canonicalize_symtab (abfd, sy); if (symcount < 0) bfd_fatal (bfd_get_filename (abfd)); return sy;}/* Read in the dynamic symbols. */static asymbol **slurp_dynamic_symtab (abfd) bfd *abfd;{ asymbol **sy = (asymbol **) NULL; long storage; storage = bfd_get_dynamic_symtab_upper_bound (abfd); if (storage < 0) { if (!(bfd_get_file_flags (abfd) & DYNAMIC)) { non_fatal ("%s: not a dynamic object", bfd_get_filename (abfd)); dynsymcount = 0; return NULL; } bfd_fatal (bfd_get_filename (abfd)); } if (storage) sy = (asymbol **) xmalloc (storage); dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy); if (dynsymcount < 0) bfd_fatal (bfd_get_filename (abfd)); return sy;}static voiddump_symbols (abfd, dynamic) bfd *abfd ATTRIBUTE_UNUSED; bfd_boolean dynamic;{ asymbol **current; long max; long count; if (dynamic) { current = dynsyms; max = dynsymcount; printf ("DYNAMIC SYMBOL TABLE:\n"); } else { current = syms; max = symcount; printf ("SYMBOL TABLE:\n"); } if (max == 0) printf ("no symbols\n"); for (count = 0; count < max; count++) { if (*current) { bfd *cur_bfd = bfd_asymbol_bfd (*current); if (cur_bfd != NULL) { const char *name; char *alloc; name = (*current)->name; alloc = NULL; //if (do_demangle && name != NULL && *name != '\0') // { /* If we want to demangle the name, we demangle it here, and temporarily clobber it while calling bfd_print_symbol. FIXME: This is a gross hack. */ //alloc = demangle (cur_bfd, name); //(*current)->name = alloc; //} bfd_print_symbol (cur_bfd, stdout, *current, bfd_print_symbol_all); (*current)->name = name; if (alloc != NULL) free (alloc); printf ("\n"); } } current++; } printf ("\n"); printf ("\n");}static voiddump_section_header (abfd, section, ignored) bfd *abfd ATTRIBUTE_UNUSED; asection *section; PTR ignored ATTRIBUTE_UNUSED;{ char *comma = ""; unsigned int opb = bfd_octets_per_byte (abfd); printf ("%3d %-13s %08lx ", section->index, bfd_get_section_name (abfd, section), (unsigned long) bfd_section_size (abfd, section) / opb); bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section)); printf (" "); bfd_printf_vma (abfd, section->lma); printf (" %08lx 2**%u", (unsigned long) section->filepos, bfd_get_section_alignment (abfd, section)); if (! wide_output) printf ("\n "); printf (" ");#define PF(x, y) \ if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; } PF (SEC_HAS_CONTENTS, "CONTENTS"); PF (SEC_ALLOC, "ALLOC"); PF (SEC_CONSTRUCTOR, "CONSTRUCTOR"); PF (SEC_LOAD, "LOAD"); PF (SEC_RELOC, "RELOC"); PF (SEC_READONLY, "READONLY"); PF (SEC_CODE, "CODE"); PF (SEC_DATA, "DATA"); PF (SEC_ROM, "ROM"); PF (SEC_DEBUGGING, "DEBUGGING"); PF (SEC_NEVER_LOAD, "NEVER_LOAD"); PF (SEC_EXCLUDE, "EXCLUDE"); PF (SEC_SORT_ENTRIES, "SORT_ENTRIES"); PF (SEC_BLOCK, "BLOCK"); PF (SEC_CLINK, "CLINK"); PF (SEC_SMALL_DATA, "SMALL_DATA"); PF (SEC_SHARED, "SHARED"); PF (SEC_ARCH_BIT_0, "ARCH_BIT_0"); PF (SEC_THREAD_LOCAL, "THREAD_LOCAL"); if ((section->flags & SEC_LINK_ONCE) != 0) { const char *ls; switch (section->flags & SEC_LINK_DUPLICATES) { default: abort (); case SEC_LINK_DUPLICATES_DISCARD: ls = "LINK_ONCE_DISCARD"; break; case SEC_LINK_DUPLICATES_ONE_ONLY: ls = "LINK_ONCE_ONE_ONLY"; break; case SEC_LINK_DUPLICATES_SAME_SIZE: ls = "LINK_ONCE_SAME_SIZE"; break; case SEC_LINK_DUPLICATES_SAME_CONTENTS: ls = "LINK_ONCE_SAME_CONTENTS"; break; } printf ("%s%s", comma, ls); if (section->comdat != NULL) printf (" (COMDAT %s %ld)", section->comdat->name, section->comdat->symbol); comma = ", "; } printf ("\n");#undef PF}static voiddump_headers (abfd) bfd *abfd;{ printf ("Sections:\n");#ifndef BFD64 printf ("Idx Name Size VMA LMA File off Algn");#else /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */ if (bfd_get_arch_size (abfd) == 32) printf ("Idx Name Size VMA LMA File off Algn"); else printf ("Idx Name Size VMA LMA File off Algn");#endif if (wide_output) printf (" Flags"); if (abfd->flags & HAS_LOAD_PAGE) printf (" Pg"); printf ("\n"); bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);}static voiddump_bfd_header (abfd) bfd *abfd;{ char *comma = ""; printf ("architecture: %s, ", bfd_printable_arch_mach (bfd_get_arch (abfd), bfd_get_mach (abfd))); printf ("flags 0x%08x:\n", abfd->flags);#define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";} PF (HAS_RELOC, "HAS_RELOC"); PF (EXEC_P, "EXEC_P"); PF (HAS_LINENO, "HAS_LINENO"); PF (HAS_DEBUG, "HAS_DEBUG"); PF (HAS_SYMS, "HAS_SYMS"); PF (HAS_LOCALS, "HAS_LOCALS"); PF (DYNAMIC, "DYNAMIC"); PF (WP_TEXT, "WP_TEXT"); PF (D_PAGED, "D_PAGED"); PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE"); PF (HAS_LOAD_PAGE, "HAS_LOAD_PAGE"); printf ("\nstart address 0x"); bfd_printf_vma (abfd, abfd->start_address); printf ("\n");}static voidl1_read_data (abfd) bfd *abfd;{ asection *section; bfd_byte *data = 0; bfd_size_type datasize = 0; bfd_size_type addr_offset; bfd_size_type start_offset, stop_offset; unsigned int opb = bfd_octets_per_byte (abfd); dump_bfd_header (abfd); dump_headers (abfd); syms = slurp_symtab (abfd); dynsyms = slurp_dynamic_symtab (abfd); dump_symbols (abfd, 0); disassemble_data (abfd); for (section = abfd->sections; section != NULL; section = section->next) { int onaline = 16; if (only == (char *) NULL || strcmp (only, section->name) == 0) { if (section->flags & SEC_HAS_CONTENTS) { char buf[64]; int count, width; printf ("Contents of section %s:\n", section->name); if (bfd_section_size (abfd, section) == 0) continue; data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section)); datasize = bfd_section_size (abfd, section); bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section)); if (start_address == (bfd_vma) -1 || start_address < section->vma) start_offset = 0; else start_offset = start_address - section->vma; if (stop_address == (bfd_vma) -1) stop_offset = bfd_section_size (abfd, section) / opb; else { if (stop_address < section->vma) stop_offset = 0; else stop_offset = stop_address - section->vma; if (stop_offset > bfd_section_size (abfd, section) / opb) stop_offset = bfd_section_size (abfd, section) / opb; } width = 4; bfd_sprintf_vma (abfd, buf, start_offset + section->vma); if (strlen (buf) >= sizeof (buf)) abort (); count = 0; while (buf[count] == '0' && buf[count+1] != '\0') count++; count = strlen (buf) - count; if (count > width) width = count; bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1); if (strlen (buf) >= sizeof (buf)) abort (); count = 0; while (buf[count] == '0' && buf[count+1] != '\0') count++; count = strlen (buf) - count; if (count > width) width = count; for (addr_offset = start_offset; addr_offset < stop_offset; addr_offset += onaline / opb) { bfd_size_type j; bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma)); count = strlen (buf); if (count >= sizeof (buf)) abort (); putchar (' '); while (count < width) { putchar ('0'); count++; } fputs (buf + count - width, stdout); putchar (' '); for (j = addr_offset * opb; j < addr_offset * opb + onaline; j++) { if (j < stop_offset * opb) printf ("%02x", (unsigned) (data[j])); else printf (" "); if ((j & 3) == 3) printf (" "); } printf (" "); for (j = addr_offset * opb; j < addr_offset * opb + onaline; j++) { if (j >= stop_offset * opb) printf (" "); else printf ("%c", isprint (data[j]) ? data[j] : '.'); } putchar ('\n'); } free (data); } } }}static voidl1_read_bfd (abfd) bfd *abfd;{ char **matching; printf ("\n%s: file format %s\n", bfd_get_filename (abfd), abfd->xvec->name); if (bfd_check_format_matches (abfd, bfd_object, &matching)) { l1_read_data (abfd); return; } if (bfd_get_error () == bfd_error_file_ambiguously_recognized) { nonfatal (bfd_get_filename (abfd)); list_matching_formats (matching); free (matching); //read_exe (char *filename, char *target) { return; } if (bfd_get_error () != bfd_error_file_not_recognized) { nonfatal (bfd_get_filename (abfd)); return; } if (bfd_check_format_matches (abfd, bfd_core, &matching)) { l1_read_data (abfd); return; } nonfatal (bfd_get_filename (abfd)); if (bfd_get_error () == bfd_error_file_ambiguously_recognized) { list_matching_formats (matching); free (matching); }}void read_exe (char *filename, char *target) { bfd *file, *arfile = (bfd *) NULL; file = bfd_openr (filename, target); if (file == NULL) { nonfatal (filename); return; } if (bfd_check_format (file, bfd_archive)) { bfd *last_arfile = NULL; printf ("In archive %s:\n", bfd_get_filename (file)); for (;;) { bfd_set_error (bfd_error_no_error); arfile = bfd_openr_next_archived_file (file, arfile); if (arfile == NULL) { if (bfd_get_error () != bfd_error_no_more_archived_files) nonfatal (bfd_get_filename (file)); break; } l1_read_bfd (arfile); if (last_arfile != NULL) bfd_close (last_arfile); last_arfile = arfile; } if (last_arfile != NULL) bfd_close (last_arfile); } else l1_read_bfd (file); bfd_close (file);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -