📄 objdump.c
字号:
asymbol *sym = *in_ptr++; if (sym->name == NULL || sym->name[0] == '\0') continue; if (sym->flags & (BSF_DEBUGGING)) continue; if (bfd_is_und_section (sym->section) || bfd_is_com_section (sym->section)) continue; *out_ptr++ = sym; } return out_ptr - symbols;}/* Sort symbols into value order. */static int compare_symbols (ap, bp) const PTR ap; const PTR bp;{ const asymbol *a = *(const asymbol **)ap; const asymbol *b = *(const asymbol **)bp; const char *an, *bn; size_t anl, bnl; boolean af, bf; flagword aflags, bflags; if (bfd_asymbol_value (a) > bfd_asymbol_value (b)) return 1; else if (bfd_asymbol_value (a) < bfd_asymbol_value (b)) return -1; if (a->section > b->section) return 1; else if (a->section < b->section) return -1; an = bfd_asymbol_name (a); bn = bfd_asymbol_name (b); anl = strlen (an); bnl = strlen (bn); /* The symbols gnu_compiled and gcc2_compiled convey no real information, so put them after other symbols with the same value. */ af = (strstr (an, "gnu_compiled") != NULL || strstr (an, "gcc2_compiled") != NULL); bf = (strstr (bn, "gnu_compiled") != NULL || strstr (bn, "gcc2_compiled") != NULL); if (af && ! bf) return 1; if (! af && bf) return -1; /* We use a heuristic for the file name, to try to sort it after more useful symbols. It may not work on non Unix systems, but it doesn't really matter; the only difference is precisely which symbol names get printed. */#define file_symbol(s, sn, snl) \ (((s)->flags & BSF_FILE) != 0 \ || ((sn)[(snl) - 2] == '.' \ && ((sn)[(snl) - 1] == 'o' \ || (sn)[(snl) - 1] == 'a'))) af = file_symbol (a, an, anl); bf = file_symbol (b, bn, bnl); if (af && ! bf) return 1; if (! af && bf) return -1; /* Try to sort global symbols before local symbols before function symbols before debugging symbols. */ aflags = a->flags; bflags = b->flags; if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING)) { if ((aflags & BSF_DEBUGGING) != 0) return 1; else return -1; } if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION)) { if ((aflags & BSF_FUNCTION) != 0) return -1; else return 1; } if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL)) { if ((aflags & BSF_LOCAL) != 0) return 1; else return -1; } if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL)) { if ((aflags & BSF_GLOBAL) != 0) return -1; else return 1; } /* Symbols that start with '.' might be section names, so sort them after symbols that don't start with '.'. */ if (an[0] == '.' && bn[0] != '.') return 1; if (an[0] != '.' && bn[0] == '.') return -1; /* Finally, if we can't distinguish them in any other way, try to get consistent results by sorting the symbols by name. */ return strcmp (an, bn);}/* Sort relocs into address order. */static intcompare_relocs (ap, bp) const PTR ap; const PTR bp;{ const arelent *a = *(const arelent **)ap; const arelent *b = *(const arelent **)bp; if (a->address > b->address) return 1; else if (a->address < b->address) return -1; /* So that associated relocations tied to the same address show up in the correct order, we don't do any further sorting. */ if (a > b) return 1; else if (a < b) return -1; else return 0;}/* Print VMA to STREAM. If SKIP_ZEROES is true, omit leading zeroes. */static voidobjdump_print_value (vma, info, skip_zeroes) bfd_vma vma; struct disassemble_info *info; boolean skip_zeroes;{ char buf[30]; char *p; sprintf_vma (buf, vma); if (! skip_zeroes) p = buf; else { for (p = buf; *p == '0'; ++p) ; if (*p == '\0') --p; } (*info->fprintf_func) (info->stream, "%s", p);}/* Print the name of a symbol. */static voidobjdump_print_symname (abfd, info, sym) bfd *abfd; struct disassemble_info *info; asymbol *sym;{ char *alloc; const char *name; const char *print; alloc = NULL; name = bfd_asymbol_name (sym); if (! do_demangle || name[0] == '\0') print = name; else { /* Demangle the name. */ if (bfd_get_symbol_leading_char (abfd) == name[0]) ++name; alloc = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS); if (alloc == NULL) print = name; else print = alloc; } if (info != NULL) (*info->fprintf_func) (info->stream, "%s", print); else printf ("%s", print); if (alloc != NULL) free (alloc);}/* Locate a symbol given a bfd, a section, and a VMA. If REQUIRE_SEC is true, then always require the symbol to be in the section. This returns NULL if there is no suitable symbol. If PLACE is not NULL, then *PLACE is set to the index of the symbol in sorted_syms. */static asymbol *find_symbol_for_address (abfd, sec, vma, require_sec, place) bfd *abfd; asection *sec; bfd_vma vma; boolean require_sec; long *place;{ /* @@ Would it speed things up to cache the last two symbols returned, and maybe their address ranges? For many processors, only one memory operand can be present at a time, so the 2-entry cache wouldn't be constantly churned by code doing heavy memory accesses. */ /* Indices in `sorted_syms'. */ long min = 0; long max = sorted_symcount; long thisplace; unsigned int opb = bfd_octets_per_byte (abfd); if (sorted_symcount < 1) return NULL; /* Perform a binary search looking for the closest symbol to the required value. We are searching the range (min, max]. */ while (min + 1 < max) { asymbol *sym; thisplace = (max + min) / 2; sym = sorted_syms[thisplace]; if (bfd_asymbol_value (sym) > vma) max = thisplace; else if (bfd_asymbol_value (sym) < vma) min = thisplace; else { min = thisplace; break; } } /* The symbol we want is now in min, the low end of the range we were searching. If there are several symbols with the same value, we want the first one. */ thisplace = min; while (thisplace > 0 && (bfd_asymbol_value (sorted_syms[thisplace]) == bfd_asymbol_value (sorted_syms[thisplace - 1]))) --thisplace; /* If the file is relocateable, and the symbol could be from this section, prefer a symbol from this section over symbols from others, even if the other symbol's value might be closer. Note that this may be wrong for some symbol references if the sections have overlapping memory ranges, but in that case there's no way to tell what's desired without looking at the relocation table. */ if (sorted_syms[thisplace]->section != sec && (require_sec || ((abfd->flags & HAS_RELOC) != 0 && vma >= bfd_get_section_vma (abfd, sec) && vma < (bfd_get_section_vma (abfd, sec) + bfd_section_size (abfd, sec) / opb)))) { long i; for (i = thisplace + 1; i < sorted_symcount; i++) { if (bfd_asymbol_value (sorted_syms[i]) != bfd_asymbol_value (sorted_syms[thisplace])) break; } --i; for (; i >= 0; i--) { if (sorted_syms[i]->section == sec && (i == 0 || sorted_syms[i - 1]->section != sec || (bfd_asymbol_value (sorted_syms[i]) != bfd_asymbol_value (sorted_syms[i - 1])))) { thisplace = i; break; } } if (sorted_syms[thisplace]->section != sec) { /* We didn't find a good symbol with a smaller value. Look for one with a larger value. */ for (i = thisplace + 1; i < sorted_symcount; i++) { if (sorted_syms[i]->section == sec) { thisplace = i; break; } } } if (sorted_syms[thisplace]->section != sec && (require_sec || ((abfd->flags & HAS_RELOC) != 0 && vma >= bfd_get_section_vma (abfd, sec) && vma < (bfd_get_section_vma (abfd, sec) + bfd_section_size (abfd, sec))))) { /* There is no suitable symbol. */ return NULL; } } if (place != NULL) *place = thisplace; return sorted_syms[thisplace];}/* Print an address to INFO symbolically. */static voidobjdump_print_addr_with_sym (abfd, sec, sym, vma, info, skip_zeroes) bfd *abfd; asection *sec; asymbol *sym; bfd_vma vma; struct disassemble_info *info; boolean skip_zeroes;{ objdump_print_value (vma, info, skip_zeroes); if (sym == NULL) { bfd_vma secaddr; (*info->fprintf_func) (info->stream, " <%s", bfd_get_section_name (abfd, sec)); secaddr = bfd_get_section_vma (abfd, sec); if (vma < secaddr) { (*info->fprintf_func) (info->stream, "-0x"); objdump_print_value (secaddr - vma, info, true); } else if (vma > secaddr) { (*info->fprintf_func) (info->stream, "+0x"); objdump_print_value (vma - secaddr, info, true); } (*info->fprintf_func) (info->stream, ">"); } else { (*info->fprintf_func) (info->stream, " <"); objdump_print_symname (abfd, info, sym); if (bfd_asymbol_value (sym) > vma) { (*info->fprintf_func) (info->stream, "-0x"); objdump_print_value (bfd_asymbol_value (sym) - vma, info, true); } else if (vma > bfd_asymbol_value (sym)) { (*info->fprintf_func) (info->stream, "+0x"); objdump_print_value (vma - bfd_asymbol_value (sym), info, true); } (*info->fprintf_func) (info->stream, ">"); }}/* Print VMA to INFO, symbolically if possible. If SKIP_ZEROES is true, don't output leading zeroes. */static voidobjdump_print_addr (vma, info, skip_zeroes) bfd_vma vma; struct disassemble_info *info; boolean skip_zeroes;{ struct objdump_disasm_info *aux; asymbol *sym; if (sorted_symcount < 1) { (*info->fprintf_func) (info->stream, "0x"); objdump_print_value (vma, info, skip_zeroes); return; } aux = (struct objdump_disasm_info *) info->application_data; sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec, (long *) NULL); objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, info, skip_zeroes);}/* Print VMA to INFO. This function is passed to the disassembler routine. */static voidobjdump_print_address (vma, info) bfd_vma vma; struct disassemble_info *info;{ objdump_print_addr (vma, info, ! prefix_addresses);}/* Determine of the given address has a symbol associated with it. */static intobjdump_symbol_at_address (vma, info) bfd_vma vma; struct disassemble_info * info;{ struct objdump_disasm_info * aux; asymbol * sym; /* No symbols - do not bother checking. */ if (sorted_symcount < 1) return 0; aux = (struct objdump_disasm_info *) info->application_data; sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec, (long *) NULL); return (sym != NULL && (bfd_asymbol_value (sym) == vma));}/* Hold the last function name and the last line number we displayed in a disassembly. */static char *prev_functionname;static unsigned int prev_line;/* We keep a list of all files that we have seen when doing a dissassembly with source, so that we know how much of the file to display. This can be important for inlined functions. */struct print_file_list{ struct print_file_list *next; char *filename; unsigned int line; FILE *f;};static struct print_file_list *print_files;/* The number of preceding context lines to show when we start displaying a file for the first time. */#define SHOW_PRECEDING_CONTEXT_LINES (5)/* Skip ahead to a given line in a file, optionally printing each line. */static voidskip_to_line PARAMS ((struct print_file_list *, unsigned int, boolean));static voidskip_to_line (p, line, show) struct print_file_list *p; unsigned int line; boolean show;{ while (p->line < line) { char buf[100]; if (fgets (buf, sizeof buf, p->f) == NULL) { fclose (p->f); p->f = NULL; break; } if (show) printf ("%s", buf); if (strchr (buf, '\n') != NULL) ++p->line; }} /* Show the line number, or the source line, in a dissassembly listing. */static voidshow_line (abfd, section, addr_offset) bfd *abfd; asection *section; bfd_vma addr_offset;{ CONST char *filename; CONST char *functionname;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -