f63e298152b971dfde341b0aa5e1a017764d3b49
[deliverable/binutils-gdb.git] / binutils / objdump.c
1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
5
6 This file is part of GNU Binutils.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 /* Objdump overview.
23
24 Objdump displays information about one or more object files, either on
25 their own, or inside libraries. It is commonly used as a disassembler,
26 but it can also display information about file headers, symbol tables,
27 relocations, debugging directives and more.
28
29 The flow of execution is as follows:
30
31 1. Command line arguments are checked for control switches and the
32 information to be displayed is selected.
33
34 2. Any remaining arguments are assumed to be object files, and they are
35 processed in order by display_bfd(). If the file is an archive each
36 of its elements is processed in turn.
37
38 3. The file's target architecture and binary file format are determined
39 by bfd_check_format(). If they are recognised, then dump_bfd() is
40 called.
41
42 4. dump_bfd() in turn calls separate functions to display the requested
43 item(s) of information(s). For example disassemble_data() is called if
44 a disassembly has been requested.
45
46 When disassembling the code loops through blocks of instructions bounded
47 by symbols, calling disassemble_bytes() on each block. The actual
48 disassembling is done by the libopcodes library, via a function pointer
49 supplied by the disassembler() function. */
50
51 #include "bfd.h"
52 #include "bfdver.h"
53 #include "progress.h"
54 #include "bucomm.h"
55 #include "budemang.h"
56 #include "getopt.h"
57 #include "safe-ctype.h"
58 #include "dis-asm.h"
59 #include "libiberty.h"
60 #include "demangle.h"
61 #include "debug.h"
62 #include "budbg.h"
63
64 /* Internal headers for the ELF .stab-dump code - sorry. */
65 #define BYTES_IN_WORD 32
66 #include "aout/aout64.h"
67
68 #ifdef NEED_DECLARATION_FPRINTF
69 /* This is needed by init_disassemble_info(). */
70 extern int fprintf (FILE *, const char *, ...);
71 #endif
72
73 /* Exit status. */
74 static int exit_status = 0;
75
76 static char *default_target = NULL; /* Default at runtime. */
77
78 /* The following variables are set based on arguments passed on the
79 command line. */
80 static int show_version = 0; /* Show the version number. */
81 static int dump_section_contents; /* -s */
82 static int dump_section_headers; /* -h */
83 static bfd_boolean dump_file_header; /* -f */
84 static int dump_symtab; /* -t */
85 static int dump_dynamic_symtab; /* -T */
86 static int dump_reloc_info; /* -r */
87 static int dump_dynamic_reloc_info; /* -R */
88 static int dump_ar_hdrs; /* -a */
89 static int dump_private_headers; /* -p */
90 static int prefix_addresses; /* --prefix-addresses */
91 static int with_line_numbers; /* -l */
92 static bfd_boolean with_source_code; /* -S */
93 static int show_raw_insn; /* --show-raw-insn */
94 static int dump_stab_section_info; /* --stabs */
95 static int do_demangle; /* -C, --demangle */
96 static bfd_boolean disassemble; /* -d */
97 static bfd_boolean disassemble_all; /* -D */
98 static int disassemble_zeroes; /* --disassemble-zeroes */
99 static bfd_boolean formats_info; /* -i */
100 static int wide_output; /* -w */
101 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
102 static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
103 static int dump_debugging; /* --debugging */
104 static int dump_debugging_tags; /* --debugging-tags */
105 static int dump_special_syms = 0; /* --special-syms */
106 static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
107 static int file_start_context = 0; /* --file-start-context */
108
109 /* Pointer to an array of section names provided by
110 one or more "-j secname" command line options. */
111 static char **only;
112 /* The total number of slots in the only[] array. */
113 static size_t only_size = 0;
114 /* The number of occupied slots in the only[] array. */
115 static size_t only_used = 0;
116
117 /* Variables for handling include file path table. */
118 static const char **include_paths;
119 static int include_path_count;
120
121 /* Extra info to pass to the section disassembler and address printing
122 function. */
123 struct objdump_disasm_info
124 {
125 bfd * abfd;
126 asection * sec;
127 bfd_boolean require_sec;
128 arelent ** dynrelbuf;
129 long dynrelcount;
130 disassembler_ftype disassemble_fn;
131 };
132
133 /* Architecture to disassemble for, or default if NULL. */
134 static char *machine = NULL;
135
136 /* Target specific options to the disassembler. */
137 static char *disassembler_options = NULL;
138
139 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
140 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
141
142 /* The symbol table. */
143 static asymbol **syms;
144
145 /* Number of symbols in `syms'. */
146 static long symcount = 0;
147
148 /* The sorted symbol table. */
149 static asymbol **sorted_syms;
150
151 /* Number of symbols in `sorted_syms'. */
152 static long sorted_symcount = 0;
153
154 /* The dynamic symbol table. */
155 static asymbol **dynsyms;
156
157 /* The synthetic symbol table. */
158 static asymbol *synthsyms;
159 static long synthcount = 0;
160
161 /* Number of symbols in `dynsyms'. */
162 static long dynsymcount = 0;
163
164 static bfd_byte *stabs;
165 static bfd_size_type stab_size;
166
167 static char *strtab;
168 static bfd_size_type stabstr_size;
169 \f
170 static void
171 usage (FILE *stream, int status)
172 {
173 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
174 fprintf (stream, _(" Display information from object <file(s)>.\n"));
175 fprintf (stream, _(" At least one of the following switches must be given:\n"));
176 fprintf (stream, _("\
177 -a, --archive-headers Display archive header information\n\
178 -f, --file-headers Display the contents of the overall file header\n\
179 -p, --private-headers Display object format specific file header contents\n\
180 -h, --[section-]headers Display the contents of the section headers\n\
181 -x, --all-headers Display the contents of all headers\n\
182 -d, --disassemble Display assembler contents of executable sections\n\
183 -D, --disassemble-all Display assembler contents of all sections\n\
184 -S, --source Intermix source code with disassembly\n\
185 -s, --full-contents Display the full contents of all sections requested\n\
186 -g, --debugging Display debug information in object file\n\
187 -e, --debugging-tags Display debug information using ctags style\n\
188 -G, --stabs Display (in raw form) any STABS info in the file\n\
189 -t, --syms Display the contents of the symbol table(s)\n\
190 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
191 -r, --reloc Display the relocation entries in the file\n\
192 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
193 -v, --version Display this program's version number\n\
194 -i, --info List object formats and architectures supported\n\
195 -H, --help Display this information\n\
196 "));
197 if (status != 2)
198 {
199 fprintf (stream, _("\n The following switches are optional:\n"));
200 fprintf (stream, _("\
201 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
202 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
203 -j, --section=NAME Only display information for section NAME\n\
204 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
205 -EB --endian=big Assume big endian format when disassembling\n\
206 -EL --endian=little Assume little endian format when disassembling\n\
207 --file-start-context Include context from start of file (with -S)\n\
208 -I, --include=DIR Add DIR to search list for source files\n\
209 -l, --line-numbers Include line numbers and filenames in output\n\
210 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
211 The STYLE, if specified, can be `auto', `gnu',\n\
212 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
213 or `gnat'\n\
214 -w, --wide Format output for more than 80 columns\n\
215 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
216 --start-address=ADDR Only process data whose address is >= ADDR\n\
217 --stop-address=ADDR Only process data whose address is <= ADDR\n\
218 --prefix-addresses Print complete address alongside disassembly\n\
219 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
220 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
221 --special-syms Include special symbols in symbol dumps\n\
222 \n"));
223 list_supported_targets (program_name, stream);
224 list_supported_architectures (program_name, stream);
225
226 disassembler_usage (stream);
227 }
228 if (status == 0)
229 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
230 exit (status);
231 }
232
233 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
234 enum option_values
235 {
236 OPTION_ENDIAN=150,
237 OPTION_START_ADDRESS,
238 OPTION_STOP_ADDRESS,
239 OPTION_ADJUST_VMA
240 };
241
242 static struct option long_options[]=
243 {
244 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
245 {"all-headers", no_argument, NULL, 'x'},
246 {"private-headers", no_argument, NULL, 'p'},
247 {"architecture", required_argument, NULL, 'm'},
248 {"archive-headers", no_argument, NULL, 'a'},
249 {"debugging", no_argument, NULL, 'g'},
250 {"debugging-tags", no_argument, NULL, 'e'},
251 {"demangle", optional_argument, NULL, 'C'},
252 {"disassemble", no_argument, NULL, 'd'},
253 {"disassemble-all", no_argument, NULL, 'D'},
254 {"disassembler-options", required_argument, NULL, 'M'},
255 {"disassemble-zeroes", no_argument, NULL, 'z'},
256 {"dynamic-reloc", no_argument, NULL, 'R'},
257 {"dynamic-syms", no_argument, NULL, 'T'},
258 {"endian", required_argument, NULL, OPTION_ENDIAN},
259 {"file-headers", no_argument, NULL, 'f'},
260 {"file-start-context", no_argument, &file_start_context, 1},
261 {"full-contents", no_argument, NULL, 's'},
262 {"headers", no_argument, NULL, 'h'},
263 {"help", no_argument, NULL, 'H'},
264 {"info", no_argument, NULL, 'i'},
265 {"line-numbers", no_argument, NULL, 'l'},
266 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
267 {"prefix-addresses", no_argument, &prefix_addresses, 1},
268 {"reloc", no_argument, NULL, 'r'},
269 {"section", required_argument, NULL, 'j'},
270 {"section-headers", no_argument, NULL, 'h'},
271 {"show-raw-insn", no_argument, &show_raw_insn, 1},
272 {"source", no_argument, NULL, 'S'},
273 {"special-syms", no_argument, &dump_special_syms, 1},
274 {"include", required_argument, NULL, 'I'},
275 {"stabs", no_argument, NULL, 'G'},
276 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
277 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
278 {"syms", no_argument, NULL, 't'},
279 {"target", required_argument, NULL, 'b'},
280 {"version", no_argument, NULL, 'V'},
281 {"wide", no_argument, NULL, 'w'},
282 {0, no_argument, 0, 0}
283 };
284 \f
285 static void
286 nonfatal (const char *msg)
287 {
288 bfd_nonfatal (msg);
289 exit_status = 1;
290 }
291 \f
292 static void
293 dump_section_header (bfd *abfd, asection *section,
294 void *ignored ATTRIBUTE_UNUSED)
295 {
296 char *comma = "";
297 unsigned int opb = bfd_octets_per_byte (abfd);
298
299 printf ("%3d %-13s %08lx ", section->index,
300 bfd_get_section_name (abfd, section),
301 (unsigned long) bfd_section_size (abfd, section) / opb);
302 bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
303 printf (" ");
304 bfd_printf_vma (abfd, section->lma);
305 printf (" %08lx 2**%u", (unsigned long) section->filepos,
306 bfd_get_section_alignment (abfd, section));
307 if (! wide_output)
308 printf ("\n ");
309 printf (" ");
310
311 #define PF(x, y) \
312 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
313
314 PF (SEC_HAS_CONTENTS, "CONTENTS");
315 PF (SEC_ALLOC, "ALLOC");
316 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
317 PF (SEC_LOAD, "LOAD");
318 PF (SEC_RELOC, "RELOC");
319 PF (SEC_READONLY, "READONLY");
320 PF (SEC_CODE, "CODE");
321 PF (SEC_DATA, "DATA");
322 PF (SEC_ROM, "ROM");
323 PF (SEC_DEBUGGING, "DEBUGGING");
324 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
325 PF (SEC_EXCLUDE, "EXCLUDE");
326 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
327 if (bfd_get_arch (abfd) == bfd_arch_tic54x)
328 {
329 PF (SEC_TIC54X_BLOCK, "BLOCK");
330 PF (SEC_TIC54X_CLINK, "CLINK");
331 }
332 PF (SEC_SMALL_DATA, "SMALL_DATA");
333 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
334 PF (SEC_COFF_SHARED, "SHARED");
335 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
336
337 if ((section->flags & SEC_LINK_ONCE) != 0)
338 {
339 const char *ls;
340 struct coff_comdat_info *comdat;
341
342 switch (section->flags & SEC_LINK_DUPLICATES)
343 {
344 default:
345 abort ();
346 case SEC_LINK_DUPLICATES_DISCARD:
347 ls = "LINK_ONCE_DISCARD";
348 break;
349 case SEC_LINK_DUPLICATES_ONE_ONLY:
350 ls = "LINK_ONCE_ONE_ONLY";
351 break;
352 case SEC_LINK_DUPLICATES_SAME_SIZE:
353 ls = "LINK_ONCE_SAME_SIZE";
354 break;
355 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
356 ls = "LINK_ONCE_SAME_CONTENTS";
357 break;
358 }
359 printf ("%s%s", comma, ls);
360
361 comdat = bfd_coff_get_comdat_section (abfd, section);
362 if (comdat != NULL)
363 printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
364
365 comma = ", ";
366 }
367
368 printf ("\n");
369 #undef PF
370 }
371
372 static void
373 dump_headers (bfd *abfd)
374 {
375 printf (_("Sections:\n"));
376
377 #ifndef BFD64
378 printf (_("Idx Name Size VMA LMA File off Algn"));
379 #else
380 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
381 if (bfd_get_arch_size (abfd) == 32)
382 printf (_("Idx Name Size VMA LMA File off Algn"));
383 else
384 printf (_("Idx Name Size VMA LMA File off Algn"));
385 #endif
386
387 if (wide_output)
388 printf (_(" Flags"));
389 if (abfd->flags & HAS_LOAD_PAGE)
390 printf (_(" Pg"));
391 printf ("\n");
392
393 bfd_map_over_sections (abfd, dump_section_header, NULL);
394 }
395 \f
396 static asymbol **
397 slurp_symtab (bfd *abfd)
398 {
399 asymbol **sy = NULL;
400 long storage;
401
402 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
403 {
404 symcount = 0;
405 return NULL;
406 }
407
408 storage = bfd_get_symtab_upper_bound (abfd);
409 if (storage < 0)
410 bfd_fatal (bfd_get_filename (abfd));
411 if (storage)
412 sy = xmalloc (storage);
413
414 symcount = bfd_canonicalize_symtab (abfd, sy);
415 if (symcount < 0)
416 bfd_fatal (bfd_get_filename (abfd));
417 return sy;
418 }
419
420 /* Read in the dynamic symbols. */
421
422 static asymbol **
423 slurp_dynamic_symtab (bfd *abfd)
424 {
425 asymbol **sy = NULL;
426 long storage;
427
428 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
429 if (storage < 0)
430 {
431 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
432 {
433 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
434 dynsymcount = 0;
435 return NULL;
436 }
437
438 bfd_fatal (bfd_get_filename (abfd));
439 }
440 if (storage)
441 sy = xmalloc (storage);
442
443 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
444 if (dynsymcount < 0)
445 bfd_fatal (bfd_get_filename (abfd));
446 return sy;
447 }
448
449 /* Filter out (in place) symbols that are useless for disassembly.
450 COUNT is the number of elements in SYMBOLS.
451 Return the number of useful symbols. */
452
453 static long
454 remove_useless_symbols (asymbol **symbols, long count)
455 {
456 asymbol **in_ptr = symbols, **out_ptr = symbols;
457
458 while (--count >= 0)
459 {
460 asymbol *sym = *in_ptr++;
461
462 if (sym->name == NULL || sym->name[0] == '\0')
463 continue;
464 if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
465 continue;
466 if (bfd_is_und_section (sym->section)
467 || bfd_is_com_section (sym->section))
468 continue;
469
470 *out_ptr++ = sym;
471 }
472 return out_ptr - symbols;
473 }
474
475 /* Sort symbols into value order. */
476
477 static int
478 compare_symbols (const void *ap, const void *bp)
479 {
480 const asymbol *a = * (const asymbol **) ap;
481 const asymbol *b = * (const asymbol **) bp;
482 const char *an;
483 const char *bn;
484 size_t anl;
485 size_t bnl;
486 bfd_boolean af;
487 bfd_boolean bf;
488 flagword aflags;
489 flagword bflags;
490
491 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
492 return 1;
493 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
494 return -1;
495
496 if (a->section > b->section)
497 return 1;
498 else if (a->section < b->section)
499 return -1;
500
501 an = bfd_asymbol_name (a);
502 bn = bfd_asymbol_name (b);
503 anl = strlen (an);
504 bnl = strlen (bn);
505
506 /* The symbols gnu_compiled and gcc2_compiled convey no real
507 information, so put them after other symbols with the same value. */
508 af = (strstr (an, "gnu_compiled") != NULL
509 || strstr (an, "gcc2_compiled") != NULL);
510 bf = (strstr (bn, "gnu_compiled") != NULL
511 || strstr (bn, "gcc2_compiled") != NULL);
512
513 if (af && ! bf)
514 return 1;
515 if (! af && bf)
516 return -1;
517
518 /* We use a heuristic for the file name, to try to sort it after
519 more useful symbols. It may not work on non Unix systems, but it
520 doesn't really matter; the only difference is precisely which
521 symbol names get printed. */
522
523 #define file_symbol(s, sn, snl) \
524 (((s)->flags & BSF_FILE) != 0 \
525 || ((sn)[(snl) - 2] == '.' \
526 && ((sn)[(snl) - 1] == 'o' \
527 || (sn)[(snl) - 1] == 'a')))
528
529 af = file_symbol (a, an, anl);
530 bf = file_symbol (b, bn, bnl);
531
532 if (af && ! bf)
533 return 1;
534 if (! af && bf)
535 return -1;
536
537 /* Try to sort global symbols before local symbols before function
538 symbols before debugging symbols. */
539
540 aflags = a->flags;
541 bflags = b->flags;
542
543 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
544 {
545 if ((aflags & BSF_DEBUGGING) != 0)
546 return 1;
547 else
548 return -1;
549 }
550 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
551 {
552 if ((aflags & BSF_FUNCTION) != 0)
553 return -1;
554 else
555 return 1;
556 }
557 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
558 {
559 if ((aflags & BSF_LOCAL) != 0)
560 return 1;
561 else
562 return -1;
563 }
564 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
565 {
566 if ((aflags & BSF_GLOBAL) != 0)
567 return -1;
568 else
569 return 1;
570 }
571
572 /* Symbols that start with '.' might be section names, so sort them
573 after symbols that don't start with '.'. */
574 if (an[0] == '.' && bn[0] != '.')
575 return 1;
576 if (an[0] != '.' && bn[0] == '.')
577 return -1;
578
579 /* Finally, if we can't distinguish them in any other way, try to
580 get consistent results by sorting the symbols by name. */
581 return strcmp (an, bn);
582 }
583
584 /* Sort relocs into address order. */
585
586 static int
587 compare_relocs (const void *ap, const void *bp)
588 {
589 const arelent *a = * (const arelent **) ap;
590 const arelent *b = * (const arelent **) bp;
591
592 if (a->address > b->address)
593 return 1;
594 else if (a->address < b->address)
595 return -1;
596
597 /* So that associated relocations tied to the same address show up
598 in the correct order, we don't do any further sorting. */
599 if (a > b)
600 return 1;
601 else if (a < b)
602 return -1;
603 else
604 return 0;
605 }
606
607 /* Print an address (VMA) to the output stream in INFO.
608 If SKIP_ZEROES is TRUE, omit leading zeroes. */
609
610 static void
611 objdump_print_value (bfd_vma vma, struct disassemble_info *info,
612 bfd_boolean skip_zeroes)
613 {
614 char buf[30];
615 char *p;
616 struct objdump_disasm_info *aux;
617
618 aux = (struct objdump_disasm_info *) info->application_data;
619 bfd_sprintf_vma (aux->abfd, buf, vma);
620 if (! skip_zeroes)
621 p = buf;
622 else
623 {
624 for (p = buf; *p == '0'; ++p)
625 ;
626 if (*p == '\0')
627 --p;
628 }
629 (*info->fprintf_func) (info->stream, "%s", p);
630 }
631
632 /* Print the name of a symbol. */
633
634 static void
635 objdump_print_symname (bfd *abfd, struct disassemble_info *info,
636 asymbol *sym)
637 {
638 char *alloc;
639 const char *name;
640
641 alloc = NULL;
642 name = bfd_asymbol_name (sym);
643 if (do_demangle && name[0] != '\0')
644 {
645 /* Demangle the name. */
646 alloc = demangle (abfd, name);
647 name = alloc;
648 }
649
650 if (info != NULL)
651 (*info->fprintf_func) (info->stream, "%s", name);
652 else
653 printf ("%s", name);
654
655 if (alloc != NULL)
656 free (alloc);
657 }
658
659 /* Locate a symbol given a bfd and a section (from INFO->application_data),
660 and a VMA. If INFO->application_data->require_sec is TRUE, then always
661 require the symbol to be in the section. Returns NULL if there is no
662 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
663 of the symbol in sorted_syms. */
664
665 static asymbol *
666 find_symbol_for_address (bfd_vma vma,
667 struct disassemble_info *info,
668 long *place)
669 {
670 /* @@ Would it speed things up to cache the last two symbols returned,
671 and maybe their address ranges? For many processors, only one memory
672 operand can be present at a time, so the 2-entry cache wouldn't be
673 constantly churned by code doing heavy memory accesses. */
674
675 /* Indices in `sorted_syms'. */
676 long min = 0;
677 long max = sorted_symcount;
678 long thisplace;
679 struct objdump_disasm_info *aux;
680 bfd *abfd;
681 asection *sec;
682 unsigned int opb;
683
684 if (sorted_symcount < 1)
685 return NULL;
686
687 aux = (struct objdump_disasm_info *) info->application_data;
688 abfd = aux->abfd;
689 sec = aux->sec;
690 opb = bfd_octets_per_byte (abfd);
691
692 /* Perform a binary search looking for the closest symbol to the
693 required value. We are searching the range (min, max]. */
694 while (min + 1 < max)
695 {
696 asymbol *sym;
697
698 thisplace = (max + min) / 2;
699 sym = sorted_syms[thisplace];
700
701 if (bfd_asymbol_value (sym) > vma)
702 max = thisplace;
703 else if (bfd_asymbol_value (sym) < vma)
704 min = thisplace;
705 else
706 {
707 min = thisplace;
708 break;
709 }
710 }
711
712 /* The symbol we want is now in min, the low end of the range we
713 were searching. If there are several symbols with the same
714 value, we want the first one. */
715 thisplace = min;
716 while (thisplace > 0
717 && (bfd_asymbol_value (sorted_syms[thisplace])
718 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
719 --thisplace;
720
721 /* If the file is relocatable, and the symbol could be from this
722 section, prefer a symbol from this section over symbols from
723 others, even if the other symbol's value might be closer.
724
725 Note that this may be wrong for some symbol references if the
726 sections have overlapping memory ranges, but in that case there's
727 no way to tell what's desired without looking at the relocation
728 table. */
729 if (sorted_syms[thisplace]->section != sec
730 && (aux->require_sec
731 || ((abfd->flags & HAS_RELOC) != 0
732 && vma >= bfd_get_section_vma (abfd, sec)
733 && vma < (bfd_get_section_vma (abfd, sec)
734 + bfd_section_size (abfd, sec) / opb))))
735 {
736 long i;
737
738 for (i = thisplace + 1; i < sorted_symcount; i++)
739 {
740 if (bfd_asymbol_value (sorted_syms[i])
741 != bfd_asymbol_value (sorted_syms[thisplace]))
742 break;
743 }
744
745 --i;
746
747 for (; i >= 0; i--)
748 {
749 if (sorted_syms[i]->section == sec
750 && (i == 0
751 || sorted_syms[i - 1]->section != sec
752 || (bfd_asymbol_value (sorted_syms[i])
753 != bfd_asymbol_value (sorted_syms[i - 1]))))
754 {
755 thisplace = i;
756 break;
757 }
758 }
759
760 if (sorted_syms[thisplace]->section != sec)
761 {
762 /* We didn't find a good symbol with a smaller value.
763 Look for one with a larger value. */
764 for (i = thisplace + 1; i < sorted_symcount; i++)
765 {
766 if (sorted_syms[i]->section == sec)
767 {
768 thisplace = i;
769 break;
770 }
771 }
772 }
773
774 if (sorted_syms[thisplace]->section != sec
775 && (aux->require_sec
776 || ((abfd->flags & HAS_RELOC) != 0
777 && vma >= bfd_get_section_vma (abfd, sec)
778 && vma < (bfd_get_section_vma (abfd, sec)
779 + bfd_section_size (abfd, sec)))))
780 /* There is no suitable symbol. */
781 return NULL;
782 }
783
784 /* Give the target a chance to reject the symbol. */
785 while (! info->symbol_is_valid (sorted_syms [thisplace], info))
786 {
787 ++ thisplace;
788 if (thisplace >= sorted_symcount
789 || bfd_asymbol_value (sorted_syms [thisplace]) > vma)
790 return NULL;
791 }
792
793 if (place != NULL)
794 *place = thisplace;
795
796 return sorted_syms[thisplace];
797 }
798
799 /* Print an address and the offset to the nearest symbol. */
800
801 static void
802 objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
803 bfd_vma vma, struct disassemble_info *info,
804 bfd_boolean skip_zeroes)
805 {
806 objdump_print_value (vma, info, skip_zeroes);
807
808 if (sym == NULL)
809 {
810 bfd_vma secaddr;
811
812 (*info->fprintf_func) (info->stream, " <%s",
813 bfd_get_section_name (abfd, sec));
814 secaddr = bfd_get_section_vma (abfd, sec);
815 if (vma < secaddr)
816 {
817 (*info->fprintf_func) (info->stream, "-0x");
818 objdump_print_value (secaddr - vma, info, TRUE);
819 }
820 else if (vma > secaddr)
821 {
822 (*info->fprintf_func) (info->stream, "+0x");
823 objdump_print_value (vma - secaddr, info, TRUE);
824 }
825 (*info->fprintf_func) (info->stream, ">");
826 }
827 else
828 {
829 (*info->fprintf_func) (info->stream, " <");
830 objdump_print_symname (abfd, info, sym);
831 if (bfd_asymbol_value (sym) > vma)
832 {
833 (*info->fprintf_func) (info->stream, "-0x");
834 objdump_print_value (bfd_asymbol_value (sym) - vma, info, TRUE);
835 }
836 else if (vma > bfd_asymbol_value (sym))
837 {
838 (*info->fprintf_func) (info->stream, "+0x");
839 objdump_print_value (vma - bfd_asymbol_value (sym), info, TRUE);
840 }
841 (*info->fprintf_func) (info->stream, ">");
842 }
843 }
844
845 /* Print an address (VMA), symbolically if possible.
846 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
847
848 static void
849 objdump_print_addr (bfd_vma vma,
850 struct disassemble_info *info,
851 bfd_boolean skip_zeroes)
852 {
853 struct objdump_disasm_info *aux;
854 asymbol *sym;
855
856 if (sorted_symcount < 1)
857 {
858 (*info->fprintf_func) (info->stream, "0x");
859 objdump_print_value (vma, info, skip_zeroes);
860 return;
861 }
862
863 aux = (struct objdump_disasm_info *) info->application_data;
864 sym = find_symbol_for_address (vma, info, NULL);
865 objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, info,
866 skip_zeroes);
867 }
868
869 /* Print VMA to INFO. This function is passed to the disassembler
870 routine. */
871
872 static void
873 objdump_print_address (bfd_vma vma, struct disassemble_info *info)
874 {
875 objdump_print_addr (vma, info, ! prefix_addresses);
876 }
877
878 /* Determine of the given address has a symbol associated with it. */
879
880 static int
881 objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * info)
882 {
883 asymbol * sym;
884
885 sym = find_symbol_for_address (vma, info, NULL);
886
887 return (sym != NULL && (bfd_asymbol_value (sym) == vma));
888 }
889
890 /* Hold the last function name and the last line number we displayed
891 in a disassembly. */
892
893 static char *prev_functionname;
894 static unsigned int prev_line;
895
896 /* We keep a list of all files that we have seen when doing a
897 disassembly with source, so that we know how much of the file to
898 display. This can be important for inlined functions. */
899
900 struct print_file_list
901 {
902 struct print_file_list *next;
903 const char *filename;
904 const char *modname;
905 unsigned int line;
906 FILE *f;
907 };
908
909 static struct print_file_list *print_files;
910
911 /* The number of preceding context lines to show when we start
912 displaying a file for the first time. */
913
914 #define SHOW_PRECEDING_CONTEXT_LINES (5)
915
916 /* Tries to open MODNAME, and if successful adds a node to print_files
917 linked list and returns that node. Returns NULL on failure. */
918
919 static struct print_file_list *
920 try_print_file_open (const char *origname, const char *modname)
921 {
922 struct print_file_list *p;
923 FILE *f;
924
925 f = fopen (modname, "r");
926 if (f == NULL)
927 return NULL;
928
929 if (print_files != NULL && print_files->f != NULL)
930 {
931 fclose (print_files->f);
932 print_files->f = NULL;
933 }
934
935 p = xmalloc (sizeof (struct print_file_list));
936 p->filename = origname;
937 p->modname = modname;
938 p->line = 0;
939 p->f = f;
940 p->next = print_files;
941 print_files = p;
942 return p;
943 }
944
945 /* If the the source file, as described in the symtab, is not found
946 try to locate it in one of the paths specified with -I
947 If found, add location to print_files linked list. */
948
949 static struct print_file_list *
950 update_source_path (const char *filename)
951 {
952 struct print_file_list *p;
953 const char *fname;
954 int i;
955
956 if (filename == NULL)
957 return NULL;
958
959 p = try_print_file_open (filename, filename);
960 if (p != NULL)
961 return p;
962
963 if (include_path_count == 0)
964 return NULL;
965
966 /* Get the name of the file. */
967 fname = strrchr (filename, '/');
968 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
969 {
970 /* We could have a mixed forward/back slash case. */
971 char *backslash = strrchr (filename, '\\');
972 if (fname == NULL || (backslash != NULL && backslash > fname))
973 fname = backslash;
974 if (fname == NULL && filename[0] != '\0' && filename[1] == ':')
975 fname = filename + 1;
976 }
977 #endif
978 if (fname == NULL)
979 fname = filename;
980 else
981 ++fname;
982
983 /* If file exists under a new path, we need to add it to the list
984 so that show_line knows about it. */
985 for (i = 0; i < include_path_count; i++)
986 {
987 char *modname = concat (include_paths[i], "/", fname, (const char *) 0);
988
989 p = try_print_file_open (filename, modname);
990 if (p)
991 return p;
992
993 free (modname);
994 }
995
996 return NULL;
997 }
998
999 /* Skip ahead to a given line in a file, optionally printing each
1000 line. */
1001
1002 static void
1003 skip_to_line (struct print_file_list *p, unsigned int line,
1004 bfd_boolean show)
1005 {
1006 while (p->line < line)
1007 {
1008 char buf[100];
1009
1010 if (fgets (buf, sizeof buf, p->f) == NULL)
1011 {
1012 fclose (p->f);
1013 p->f = NULL;
1014 break;
1015 }
1016
1017 if (show)
1018 printf ("%s", buf);
1019
1020 if (strchr (buf, '\n') != NULL)
1021 ++p->line;
1022 }
1023 }
1024
1025 /* Show the line number, or the source line, in a disassembly
1026 listing. */
1027
1028 static void
1029 show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
1030 {
1031 const char *filename;
1032 const char *functionname;
1033 unsigned int line;
1034
1035 if (! with_line_numbers && ! with_source_code)
1036 return;
1037
1038 if (! bfd_find_nearest_line (abfd, section, syms, addr_offset, &filename,
1039 &functionname, &line))
1040 return;
1041
1042 if (filename != NULL && *filename == '\0')
1043 filename = NULL;
1044 if (functionname != NULL && *functionname == '\0')
1045 functionname = NULL;
1046
1047 if (with_line_numbers)
1048 {
1049 if (functionname != NULL
1050 && (prev_functionname == NULL
1051 || strcmp (functionname, prev_functionname) != 0))
1052 printf ("%s():\n", functionname);
1053 if (line > 0 && line != prev_line)
1054 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
1055 }
1056
1057 if (with_source_code
1058 && filename != NULL
1059 && line > 0)
1060 {
1061 struct print_file_list **pp, *p;
1062
1063 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
1064 if (strcmp ((*pp)->filename, filename) == 0)
1065 break;
1066 p = *pp;
1067
1068 if (p != NULL)
1069 {
1070 if (p != print_files)
1071 {
1072 int l;
1073
1074 /* We have reencountered a file name which we saw
1075 earlier. This implies that either we are dumping out
1076 code from an included file, or the same file was
1077 linked in more than once. There are two common cases
1078 of an included file: inline functions in a header
1079 file, and a bison or flex skeleton file. In the
1080 former case we want to just start printing (but we
1081 back up a few lines to give context); in the latter
1082 case we want to continue from where we left off. I
1083 can't think of a good way to distinguish the cases,
1084 so I used a heuristic based on the file name. */
1085 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
1086 l = p->line;
1087 else
1088 {
1089 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1090 if (l < 0)
1091 l = 0;
1092 }
1093
1094 if (p->f == NULL)
1095 {
1096 p->f = fopen (p->modname, "r");
1097 p->line = 0;
1098 }
1099 if (p->f != NULL)
1100 skip_to_line (p, l, FALSE);
1101
1102 if (print_files->f != NULL)
1103 {
1104 fclose (print_files->f);
1105 print_files->f = NULL;
1106 }
1107 }
1108
1109 if (p->f != NULL)
1110 {
1111 skip_to_line (p, line, TRUE);
1112 *pp = p->next;
1113 p->next = print_files;
1114 print_files = p;
1115 }
1116 }
1117 else
1118 {
1119 p = update_source_path (filename);
1120
1121 if (p != NULL)
1122 {
1123 int l;
1124
1125 if (file_start_context)
1126 l = 0;
1127 else
1128 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1129 if (l < 0)
1130 l = 0;
1131 skip_to_line (p, l, FALSE);
1132 if (p->f != NULL)
1133 skip_to_line (p, line, TRUE);
1134 }
1135 }
1136 }
1137
1138 if (functionname != NULL
1139 && (prev_functionname == NULL
1140 || strcmp (functionname, prev_functionname) != 0))
1141 {
1142 if (prev_functionname != NULL)
1143 free (prev_functionname);
1144 prev_functionname = xmalloc (strlen (functionname) + 1);
1145 strcpy (prev_functionname, functionname);
1146 }
1147
1148 if (line > 0 && line != prev_line)
1149 prev_line = line;
1150 }
1151
1152 /* Pseudo FILE object for strings. */
1153 typedef struct
1154 {
1155 char *buffer;
1156 size_t pos;
1157 size_t alloc;
1158 } SFILE;
1159
1160 /* sprintf to a "stream". */
1161
1162 static int
1163 objdump_sprintf (SFILE *f, const char *format, ...)
1164 {
1165 size_t n;
1166 va_list args;
1167
1168 while (1)
1169 {
1170 size_t space = f->alloc - f->pos;
1171
1172 va_start (args, format);
1173 n = vsnprintf (f->buffer + f->pos, space, format, args);
1174 va_end (args);
1175
1176 if (space > n)
1177 break;
1178
1179 f->alloc = (f->alloc + n) * 2;
1180 f->buffer = xrealloc (f->buffer, f->alloc);
1181 }
1182 f->pos += n;
1183
1184 return n;
1185 }
1186
1187 /* Returns TRUE if the specified section should be dumped. */
1188
1189 static bfd_boolean
1190 process_section_p (asection * section)
1191 {
1192 size_t i;
1193
1194 if (only == NULL)
1195 return TRUE;
1196
1197 for (i = 0; i < only_used; i++)
1198 if (strcmp (only [i], section->name) == 0)
1199 return TRUE;
1200
1201 return FALSE;
1202 }
1203
1204
1205 /* The number of zeroes we want to see before we start skipping them.
1206 The number is arbitrarily chosen. */
1207
1208 #define DEFAULT_SKIP_ZEROES 8
1209
1210 /* The number of zeroes to skip at the end of a section. If the
1211 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1212 SKIP_ZEROES, they will be disassembled. If there are fewer than
1213 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1214 attempt to avoid disassembling zeroes inserted by section
1215 alignment. */
1216
1217 #define DEFAULT_SKIP_ZEROES_AT_END 3
1218
1219 /* Disassemble some data in memory between given values. */
1220
1221 static void
1222 disassemble_bytes (struct disassemble_info * info,
1223 disassembler_ftype disassemble_fn,
1224 bfd_boolean insns,
1225 bfd_byte * data,
1226 bfd_vma start_offset,
1227 bfd_vma stop_offset,
1228 bfd_vma rel_offset,
1229 arelent *** relppp,
1230 arelent ** relppend)
1231 {
1232 struct objdump_disasm_info *aux;
1233 asection *section;
1234 int octets_per_line;
1235 bfd_boolean done_dot;
1236 int skip_addr_chars;
1237 bfd_vma addr_offset;
1238 unsigned int opb = info->octets_per_byte;
1239 unsigned int skip_zeroes = info->skip_zeroes;
1240 unsigned int skip_zeroes_at_end = info->skip_zeroes_at_end;
1241 SFILE sfile;
1242
1243 aux = (struct objdump_disasm_info *) info->application_data;
1244 section = aux->sec;
1245
1246 sfile.alloc = 120;
1247 sfile.buffer = xmalloc (sfile.alloc);
1248 sfile.pos = 0;
1249
1250 if (insns)
1251 octets_per_line = 4;
1252 else
1253 octets_per_line = 16;
1254
1255 /* Figure out how many characters to skip at the start of an
1256 address, to make the disassembly look nicer. We discard leading
1257 zeroes in chunks of 4, ensuring that there is always a leading
1258 zero remaining. */
1259 skip_addr_chars = 0;
1260 if (! prefix_addresses)
1261 {
1262 char buf[30];
1263 char *s;
1264
1265 bfd_sprintf_vma
1266 (aux->abfd, buf,
1267 (section->vma
1268 + bfd_section_size (section->owner, section) / opb));
1269 s = buf;
1270 while (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0'
1271 && s[4] == '0')
1272 {
1273 skip_addr_chars += 4;
1274 s += 4;
1275 }
1276 }
1277
1278 info->insn_info_valid = 0;
1279
1280 done_dot = FALSE;
1281 addr_offset = start_offset;
1282 while (addr_offset < stop_offset)
1283 {
1284 bfd_vma z;
1285 int octets = 0;
1286 bfd_boolean need_nl = FALSE;
1287
1288 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1289 print `...'. */
1290 for (z = addr_offset * opb; z < stop_offset * opb; z++)
1291 if (data[z] != 0)
1292 break;
1293 if (! disassemble_zeroes
1294 && (info->insn_info_valid == 0
1295 || info->branch_delay_insns == 0)
1296 && (z - addr_offset * opb >= skip_zeroes
1297 || (z == stop_offset * opb &&
1298 z - addr_offset * opb < skip_zeroes_at_end)))
1299 {
1300 printf ("\t...\n");
1301
1302 /* If there are more nonzero octets to follow, we only skip
1303 zeroes in multiples of 4, to try to avoid running over
1304 the start of an instruction which happens to start with
1305 zero. */
1306 if (z != stop_offset * opb)
1307 z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
1308
1309 octets = z - addr_offset * opb;
1310 }
1311 else
1312 {
1313 char buf[50];
1314 int bpc = 0;
1315 int pb = 0;
1316
1317 done_dot = FALSE;
1318
1319 if (with_line_numbers || with_source_code)
1320 /* The line number tables will refer to unadjusted
1321 section VMAs, so we must undo any VMA modifications
1322 when calling show_line. */
1323 show_line (aux->abfd, section, addr_offset - adjust_section_vma);
1324
1325 if (! prefix_addresses)
1326 {
1327 char *s;
1328
1329 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
1330 for (s = buf + skip_addr_chars; *s == '0'; s++)
1331 *s = ' ';
1332 if (*s == '\0')
1333 *--s = '0';
1334 printf ("%s:\t", buf + skip_addr_chars);
1335 }
1336 else
1337 {
1338 aux->require_sec = TRUE;
1339 objdump_print_address (section->vma + addr_offset, info);
1340 aux->require_sec = FALSE;
1341 putchar (' ');
1342 }
1343
1344 if (insns)
1345 {
1346 sfile.pos = 0;
1347 info->fprintf_func = (fprintf_ftype) objdump_sprintf;
1348 info->stream = (FILE *) &sfile;
1349 info->bytes_per_line = 0;
1350 info->bytes_per_chunk = 0;
1351
1352 #ifdef DISASSEMBLER_NEEDS_RELOCS
1353 /* FIXME: This is wrong. It tests the number of octets
1354 in the last instruction, not the current one. */
1355 if (*relppp < relppend
1356 && (**relppp)->address >= rel_offset + addr_offset
1357 && ((**relppp)->address
1358 < rel_offset + addr_offset + octets / opb))
1359 info->flags = INSN_HAS_RELOC;
1360 else
1361 #endif
1362 info->flags = 0;
1363
1364 octets = (*disassemble_fn) (section->vma + addr_offset, info);
1365 info->fprintf_func = (fprintf_ftype) fprintf;
1366 info->stream = stdout;
1367 if (info->bytes_per_line != 0)
1368 octets_per_line = info->bytes_per_line;
1369 if (octets < 0)
1370 {
1371 if (sfile.pos)
1372 printf ("%s\n", sfile.buffer);
1373 break;
1374 }
1375 }
1376 else
1377 {
1378 bfd_vma j;
1379
1380 octets = octets_per_line;
1381 if (addr_offset + octets / opb > stop_offset)
1382 octets = (stop_offset - addr_offset) * opb;
1383
1384 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
1385 {
1386 if (ISPRINT (data[j]))
1387 buf[j - addr_offset * opb] = data[j];
1388 else
1389 buf[j - addr_offset * opb] = '.';
1390 }
1391 buf[j - addr_offset * opb] = '\0';
1392 }
1393
1394 if (prefix_addresses
1395 ? show_raw_insn > 0
1396 : show_raw_insn >= 0)
1397 {
1398 bfd_vma j;
1399
1400 /* If ! prefix_addresses and ! wide_output, we print
1401 octets_per_line octets per line. */
1402 pb = octets;
1403 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
1404 pb = octets_per_line;
1405
1406 if (info->bytes_per_chunk)
1407 bpc = info->bytes_per_chunk;
1408 else
1409 bpc = 1;
1410
1411 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
1412 {
1413 int k;
1414
1415 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1416 {
1417 for (k = bpc - 1; k >= 0; k--)
1418 printf ("%02x", (unsigned) data[j + k]);
1419 putchar (' ');
1420 }
1421 else
1422 {
1423 for (k = 0; k < bpc; k++)
1424 printf ("%02x", (unsigned) data[j + k]);
1425 putchar (' ');
1426 }
1427 }
1428
1429 for (; pb < octets_per_line; pb += bpc)
1430 {
1431 int k;
1432
1433 for (k = 0; k < bpc; k++)
1434 printf (" ");
1435 putchar (' ');
1436 }
1437
1438 /* Separate raw data from instruction by extra space. */
1439 if (insns)
1440 putchar ('\t');
1441 else
1442 printf (" ");
1443 }
1444
1445 if (! insns)
1446 printf ("%s", buf);
1447 else if (sfile.pos)
1448 printf ("%s", sfile.buffer);
1449
1450 if (prefix_addresses
1451 ? show_raw_insn > 0
1452 : show_raw_insn >= 0)
1453 {
1454 while (pb < octets)
1455 {
1456 bfd_vma j;
1457 char *s;
1458
1459 putchar ('\n');
1460 j = addr_offset * opb + pb;
1461
1462 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
1463 for (s = buf + skip_addr_chars; *s == '0'; s++)
1464 *s = ' ';
1465 if (*s == '\0')
1466 *--s = '0';
1467 printf ("%s:\t", buf + skip_addr_chars);
1468
1469 pb += octets_per_line;
1470 if (pb > octets)
1471 pb = octets;
1472 for (; j < addr_offset * opb + pb; j += bpc)
1473 {
1474 int k;
1475
1476 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1477 {
1478 for (k = bpc - 1; k >= 0; k--)
1479 printf ("%02x", (unsigned) data[j + k]);
1480 putchar (' ');
1481 }
1482 else
1483 {
1484 for (k = 0; k < bpc; k++)
1485 printf ("%02x", (unsigned) data[j + k]);
1486 putchar (' ');
1487 }
1488 }
1489 }
1490 }
1491
1492 if (!wide_output)
1493 putchar ('\n');
1494 else
1495 need_nl = TRUE;
1496 }
1497
1498 while ((*relppp) < relppend
1499 && (**relppp)->address < rel_offset + addr_offset + octets / opb)
1500 {
1501 if (dump_reloc_info || dump_dynamic_reloc_info)
1502 {
1503 arelent *q;
1504
1505 q = **relppp;
1506
1507 if (wide_output)
1508 putchar ('\t');
1509 else
1510 printf ("\t\t\t");
1511
1512 objdump_print_value (section->vma - rel_offset + q->address,
1513 info, TRUE);
1514
1515 printf (": %s\t", q->howto->name);
1516
1517 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
1518 printf ("*unknown*");
1519 else
1520 {
1521 const char *sym_name;
1522
1523 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1524 if (sym_name != NULL && *sym_name != '\0')
1525 objdump_print_symname (aux->abfd, info, *q->sym_ptr_ptr);
1526 else
1527 {
1528 asection *sym_sec;
1529
1530 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1531 sym_name = bfd_get_section_name (aux->abfd, sym_sec);
1532 if (sym_name == NULL || *sym_name == '\0')
1533 sym_name = "*unknown*";
1534 printf ("%s", sym_name);
1535 }
1536 }
1537
1538 if (q->addend)
1539 {
1540 printf ("+0x");
1541 objdump_print_value (q->addend, info, TRUE);
1542 }
1543
1544 printf ("\n");
1545 need_nl = FALSE;
1546 }
1547 ++(*relppp);
1548 }
1549
1550 if (need_nl)
1551 printf ("\n");
1552
1553 addr_offset += octets / opb;
1554 }
1555
1556 free (sfile.buffer);
1557 }
1558
1559 static void
1560 disassemble_section (bfd *abfd, asection *section, void *info)
1561 {
1562 struct disassemble_info * pinfo = (struct disassemble_info *) info;
1563 struct objdump_disasm_info * paux;
1564 unsigned int opb = pinfo->octets_per_byte;
1565 bfd_byte * data = NULL;
1566 bfd_size_type datasize = 0;
1567 arelent ** rel_pp = NULL;
1568 arelent ** rel_ppstart = NULL;
1569 arelent ** rel_ppend;
1570 unsigned long stop_offset;
1571 asymbol * sym = NULL;
1572 long place = 0;
1573 long rel_count;
1574 bfd_vma rel_offset;
1575 unsigned long addr_offset;
1576
1577 /* Sections that do not contain machine
1578 code are not normally disassembled. */
1579 if (! disassemble_all
1580 && only == NULL
1581 && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
1582 != (SEC_CODE | SEC_HAS_CONTENTS)))
1583 return;
1584
1585 if (! process_section_p (section))
1586 return;
1587
1588 datasize = bfd_get_section_size (section);
1589 if (datasize == 0)
1590 return;
1591
1592 /* Decide which set of relocs to use. Load them if necessary. */
1593 paux = (struct objdump_disasm_info *) pinfo->application_data;
1594 if (paux->dynrelbuf)
1595 {
1596 rel_pp = paux->dynrelbuf;
1597 rel_count = paux->dynrelcount;
1598 /* Dynamic reloc addresses are absolute, non-dynamic are section
1599 relative. REL_OFFSET specifies the reloc address corresponding
1600 to the start of this section. */
1601 rel_offset = section->vma;
1602 }
1603 else
1604 {
1605 rel_count = 0;
1606 rel_pp = NULL;
1607 rel_offset = 0;
1608
1609 if ((section->flags & SEC_RELOC) != 0
1610 #ifndef DISASSEMBLER_NEEDS_RELOCS
1611 && dump_reloc_info
1612 #endif
1613 )
1614 {
1615 long relsize;
1616
1617 relsize = bfd_get_reloc_upper_bound (abfd, section);
1618 if (relsize < 0)
1619 bfd_fatal (bfd_get_filename (abfd));
1620
1621 if (relsize > 0)
1622 {
1623 rel_ppstart = rel_pp = xmalloc (relsize);
1624 rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
1625 if (rel_count < 0)
1626 bfd_fatal (bfd_get_filename (abfd));
1627
1628 /* Sort the relocs by address. */
1629 qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
1630 }
1631 }
1632
1633 }
1634 rel_ppend = rel_pp + rel_count;
1635
1636 data = xmalloc (datasize);
1637
1638 bfd_get_section_contents (abfd, section, data, 0, datasize);
1639
1640 paux->sec = section;
1641 pinfo->buffer = data;
1642 pinfo->buffer_vma = section->vma;
1643 pinfo->buffer_length = datasize;
1644 pinfo->section = section;
1645
1646 if (start_address == (bfd_vma) -1
1647 || start_address < pinfo->buffer_vma)
1648 addr_offset = 0;
1649 else
1650 addr_offset = start_address - pinfo->buffer_vma;
1651
1652 if (stop_address == (bfd_vma) -1)
1653 stop_offset = datasize / opb;
1654 else
1655 {
1656 if (stop_address < pinfo->buffer_vma)
1657 stop_offset = 0;
1658 else
1659 stop_offset = stop_address - pinfo->buffer_vma;
1660 if (stop_offset > pinfo->buffer_length / opb)
1661 stop_offset = pinfo->buffer_length / opb;
1662 }
1663
1664 /* Skip over the relocs belonging to addresses below the
1665 start address. */
1666 while (rel_pp < rel_ppend
1667 && (*rel_pp)->address < rel_offset + addr_offset)
1668 ++rel_pp;
1669
1670 printf (_("Disassembly of section %s:\n"), section->name);
1671
1672 /* Find the nearest symbol forwards from our current position. */
1673 paux->require_sec = TRUE;
1674 sym = find_symbol_for_address (section->vma + addr_offset, info, &place);
1675 paux->require_sec = FALSE;
1676
1677 /* Disassemble a block of instructions up to the address associated with
1678 the symbol we have just found. Then print the symbol and find the
1679 next symbol on. Repeat until we have disassembled the entire section
1680 or we have reached the end of the address range we are interested in. */
1681 while (addr_offset < stop_offset)
1682 {
1683 bfd_vma addr;
1684 asymbol *nextsym;
1685 unsigned long nextstop_offset;
1686 bfd_boolean insns;
1687
1688 addr = section->vma + addr_offset;
1689
1690 if (sym != NULL && bfd_asymbol_value (sym) <= addr)
1691 {
1692 int x;
1693
1694 for (x = place;
1695 (x < sorted_symcount
1696 && (bfd_asymbol_value (sorted_syms[x]) <= addr));
1697 ++x)
1698 continue;
1699
1700 pinfo->symbols = sorted_syms + place;
1701 pinfo->num_symbols = x - place;
1702 }
1703 else
1704 {
1705 pinfo->symbols = NULL;
1706 pinfo->num_symbols = 0;
1707 }
1708
1709 if (! prefix_addresses)
1710 {
1711 pinfo->fprintf_func (pinfo->stream, "\n");
1712 objdump_print_addr_with_sym (abfd, section, sym, addr,
1713 pinfo, FALSE);
1714 pinfo->fprintf_func (pinfo->stream, ":\n");
1715 }
1716
1717 if (sym != NULL && bfd_asymbol_value (sym) > addr)
1718 nextsym = sym;
1719 else if (sym == NULL)
1720 nextsym = NULL;
1721 else
1722 {
1723 #define is_valid_next_sym(SYM) \
1724 ((SYM)->section == section \
1725 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
1726 && pinfo->symbol_is_valid (SYM, pinfo))
1727
1728 /* Search forward for the next appropriate symbol in
1729 SECTION. Note that all the symbols are sorted
1730 together into one big array, and that some sections
1731 may have overlapping addresses. */
1732 while (place < sorted_symcount
1733 && ! is_valid_next_sym (sorted_syms [place]))
1734 ++place;
1735
1736 if (place >= sorted_symcount)
1737 nextsym = NULL;
1738 else
1739 nextsym = sorted_syms[place];
1740 }
1741
1742 if (sym != NULL && bfd_asymbol_value (sym) > addr)
1743 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
1744 else if (nextsym == NULL)
1745 nextstop_offset = stop_offset;
1746 else
1747 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
1748
1749 if (nextstop_offset > stop_offset)
1750 nextstop_offset = stop_offset;
1751
1752 /* If a symbol is explicitly marked as being an object
1753 rather than a function, just dump the bytes without
1754 disassembling them. */
1755 if (disassemble_all
1756 || sym == NULL
1757 || bfd_asymbol_value (sym) > addr
1758 || ((sym->flags & BSF_OBJECT) == 0
1759 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
1760 == NULL)
1761 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
1762 == NULL))
1763 || (sym->flags & BSF_FUNCTION) != 0)
1764 insns = TRUE;
1765 else
1766 insns = FALSE;
1767
1768 disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
1769 addr_offset, nextstop_offset,
1770 rel_offset, &rel_pp, rel_ppend);
1771
1772 addr_offset = nextstop_offset;
1773 sym = nextsym;
1774 }
1775
1776 free (data);
1777
1778 if (rel_ppstart != NULL)
1779 free (rel_ppstart);
1780 }
1781
1782 /* Disassemble the contents of an object file. */
1783
1784 static void
1785 disassemble_data (bfd *abfd)
1786 {
1787 struct disassemble_info disasm_info;
1788 struct objdump_disasm_info aux;
1789 long i;
1790
1791 print_files = NULL;
1792 prev_functionname = NULL;
1793 prev_line = -1;
1794
1795 /* We make a copy of syms to sort. We don't want to sort syms
1796 because that will screw up the relocs. */
1797 sorted_symcount = symcount ? symcount : dynsymcount;
1798 sorted_syms = xmalloc ((sorted_symcount + synthcount) * sizeof (asymbol *));
1799 memcpy (sorted_syms, symcount ? syms : dynsyms,
1800 sorted_symcount * sizeof (asymbol *));
1801
1802 sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
1803
1804 for (i = 0; i < synthcount; ++i)
1805 {
1806 sorted_syms[sorted_symcount] = synthsyms + i;
1807 ++sorted_symcount;
1808 }
1809
1810 /* Sort the symbols into section and symbol order. */
1811 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
1812
1813 init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
1814
1815 disasm_info.application_data = (void *) &aux;
1816 aux.abfd = abfd;
1817 aux.require_sec = FALSE;
1818 aux.dynrelbuf = NULL;
1819 aux.dynrelcount = 0;
1820
1821 disasm_info.print_address_func = objdump_print_address;
1822 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
1823
1824 if (machine != NULL)
1825 {
1826 const bfd_arch_info_type *info = bfd_scan_arch (machine);
1827
1828 if (info == NULL)
1829 fatal (_("Can't use supplied machine %s"), machine);
1830
1831 abfd->arch_info = info;
1832 }
1833
1834 if (endian != BFD_ENDIAN_UNKNOWN)
1835 {
1836 struct bfd_target *xvec;
1837
1838 xvec = xmalloc (sizeof (struct bfd_target));
1839 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
1840 xvec->byteorder = endian;
1841 abfd->xvec = xvec;
1842 }
1843
1844 /* Use libopcodes to locate a suitable disassembler. */
1845 aux.disassemble_fn = disassembler (abfd);
1846 if (!aux.disassemble_fn)
1847 {
1848 non_fatal (_("Can't disassemble for architecture %s\n"),
1849 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
1850 exit_status = 1;
1851 return;
1852 }
1853
1854 disasm_info.flavour = bfd_get_flavour (abfd);
1855 disasm_info.arch = bfd_get_arch (abfd);
1856 disasm_info.mach = bfd_get_mach (abfd);
1857 disasm_info.disassembler_options = disassembler_options;
1858 disasm_info.octets_per_byte = bfd_octets_per_byte (abfd);
1859 disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
1860 disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
1861
1862 if (bfd_big_endian (abfd))
1863 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
1864 else if (bfd_little_endian (abfd))
1865 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
1866 else
1867 /* ??? Aborting here seems too drastic. We could default to big or little
1868 instead. */
1869 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
1870
1871 /* Allow the target to customize the info structure. */
1872 disassemble_init_for_target (& disasm_info);
1873
1874 /* Pre-load the dynamic relocs if we are going
1875 to be dumping them along with the disassembly. */
1876 if (dump_dynamic_reloc_info)
1877 {
1878 long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1879
1880 if (relsize < 0)
1881 bfd_fatal (bfd_get_filename (abfd));
1882
1883 if (relsize > 0)
1884 {
1885 aux.dynrelbuf = xmalloc (relsize);
1886 aux.dynrelcount = bfd_canonicalize_dynamic_reloc (abfd,
1887 aux.dynrelbuf,
1888 dynsyms);
1889 if (aux.dynrelcount < 0)
1890 bfd_fatal (bfd_get_filename (abfd));
1891
1892 /* Sort the relocs by address. */
1893 qsort (aux.dynrelbuf, aux.dynrelcount, sizeof (arelent *),
1894 compare_relocs);
1895 }
1896 }
1897
1898 bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
1899
1900 if (aux.dynrelbuf != NULL)
1901 free (aux.dynrelbuf);
1902 free (sorted_syms);
1903 }
1904 \f
1905 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
1906 it. Return NULL on failure. */
1907
1908 static char *
1909 read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr)
1910 {
1911 asection *stabsect;
1912 bfd_size_type size;
1913 char *contents;
1914
1915 stabsect = bfd_get_section_by_name (abfd, sect_name);
1916 if (stabsect == NULL)
1917 {
1918 printf (_("No %s section present\n\n"), sect_name);
1919 return FALSE;
1920 }
1921
1922 size = bfd_section_size (abfd, stabsect);
1923 contents = xmalloc (size);
1924
1925 if (! bfd_get_section_contents (abfd, stabsect, contents, 0, size))
1926 {
1927 non_fatal (_("Reading %s section of %s failed: %s"),
1928 sect_name, bfd_get_filename (abfd),
1929 bfd_errmsg (bfd_get_error ()));
1930 free (contents);
1931 exit_status = 1;
1932 return NULL;
1933 }
1934
1935 *size_ptr = size;
1936
1937 return contents;
1938 }
1939
1940 /* Stabs entries use a 12 byte format:
1941 4 byte string table index
1942 1 byte stab type
1943 1 byte stab other field
1944 2 byte stab desc field
1945 4 byte stab value
1946 FIXME: This will have to change for a 64 bit object format. */
1947
1948 #define STRDXOFF (0)
1949 #define TYPEOFF (4)
1950 #define OTHEROFF (5)
1951 #define DESCOFF (6)
1952 #define VALOFF (8)
1953 #define STABSIZE (12)
1954
1955 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1956 using string table section STRSECT_NAME (in `strtab'). */
1957
1958 static void
1959 print_section_stabs (bfd *abfd,
1960 const char *stabsect_name,
1961 unsigned *string_offset_ptr)
1962 {
1963 int i;
1964 unsigned file_string_table_offset = 0;
1965 unsigned next_file_string_table_offset = *string_offset_ptr;
1966 bfd_byte *stabp, *stabs_end;
1967
1968 stabp = stabs;
1969 stabs_end = stabp + stab_size;
1970
1971 printf (_("Contents of %s section:\n\n"), stabsect_name);
1972 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1973
1974 /* Loop through all symbols and print them.
1975
1976 We start the index at -1 because there is a dummy symbol on
1977 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1978 for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++)
1979 {
1980 const char *name;
1981 unsigned long strx;
1982 unsigned char type, other;
1983 unsigned short desc;
1984 bfd_vma value;
1985
1986 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1987 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1988 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1989 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1990 value = bfd_h_get_32 (abfd, stabp + VALOFF);
1991
1992 printf ("\n%-6d ", i);
1993 /* Either print the stab name, or, if unnamed, print its number
1994 again (makes consistent formatting for tools like awk). */
1995 name = bfd_get_stab_name (type);
1996 if (name != NULL)
1997 printf ("%-6s", name);
1998 else if (type == N_UNDF)
1999 printf ("HdrSym");
2000 else
2001 printf ("%-6d", type);
2002 printf (" %-6d %-6d ", other, desc);
2003 bfd_printf_vma (abfd, value);
2004 printf (" %-6lu", strx);
2005
2006 /* Symbols with type == 0 (N_UNDF) specify the length of the
2007 string table associated with this file. We use that info
2008 to know how to relocate the *next* file's string table indices. */
2009 if (type == N_UNDF)
2010 {
2011 file_string_table_offset = next_file_string_table_offset;
2012 next_file_string_table_offset += value;
2013 }
2014 else
2015 {
2016 /* Using the (possibly updated) string table offset, print the
2017 string (if any) associated with this symbol. */
2018 if ((strx + file_string_table_offset) < stabstr_size)
2019 printf (" %s", &strtab[strx + file_string_table_offset]);
2020 else
2021 printf (" *");
2022 }
2023 }
2024 printf ("\n\n");
2025 *string_offset_ptr = next_file_string_table_offset;
2026 }
2027
2028 typedef struct
2029 {
2030 const char * section_name;
2031 const char * string_section_name;
2032 unsigned string_offset;
2033 }
2034 stab_section_names;
2035
2036 static void
2037 find_stabs_section (bfd *abfd, asection *section, void *names)
2038 {
2039 int len;
2040 stab_section_names * sought = (stab_section_names *) names;
2041
2042 /* Check for section names for which stabsect_name is a prefix, to
2043 handle .stab.N, etc. */
2044 len = strlen (sought->section_name);
2045
2046 /* If the prefix matches, and the files section name ends with a
2047 nul or a digit, then we match. I.e., we want either an exact
2048 match or a section followed by a number. */
2049 if (strncmp (sought->section_name, section->name, len) == 0
2050 && (section->name[len] == 0
2051 || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
2052 {
2053 if (strtab == NULL)
2054 strtab = read_section_stabs (abfd, sought->string_section_name,
2055 &stabstr_size);
2056
2057 if (strtab)
2058 {
2059 stabs = (bfd_byte *) read_section_stabs (abfd, section->name,
2060 &stab_size);
2061 if (stabs)
2062 print_section_stabs (abfd, section->name, &sought->string_offset);
2063 }
2064 }
2065 }
2066
2067 static void
2068 dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
2069 {
2070 stab_section_names s;
2071
2072 s.section_name = stabsect_name;
2073 s.string_section_name = strsect_name;
2074 s.string_offset = 0;
2075
2076 bfd_map_over_sections (abfd, find_stabs_section, & s);
2077
2078 free (strtab);
2079 strtab = NULL;
2080 }
2081
2082 /* Dump the any sections containing stabs debugging information. */
2083
2084 static void
2085 dump_stabs (bfd *abfd)
2086 {
2087 dump_stabs_section (abfd, ".stab", ".stabstr");
2088 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
2089 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
2090 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
2091 }
2092 \f
2093 static void
2094 dump_bfd_header (bfd *abfd)
2095 {
2096 char *comma = "";
2097
2098 printf (_("architecture: %s, "),
2099 bfd_printable_arch_mach (bfd_get_arch (abfd),
2100 bfd_get_mach (abfd)));
2101 printf (_("flags 0x%08x:\n"), abfd->flags);
2102
2103 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2104 PF (HAS_RELOC, "HAS_RELOC");
2105 PF (EXEC_P, "EXEC_P");
2106 PF (HAS_LINENO, "HAS_LINENO");
2107 PF (HAS_DEBUG, "HAS_DEBUG");
2108 PF (HAS_SYMS, "HAS_SYMS");
2109 PF (HAS_LOCALS, "HAS_LOCALS");
2110 PF (DYNAMIC, "DYNAMIC");
2111 PF (WP_TEXT, "WP_TEXT");
2112 PF (D_PAGED, "D_PAGED");
2113 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
2114 PF (HAS_LOAD_PAGE, "HAS_LOAD_PAGE");
2115 printf (_("\nstart address 0x"));
2116 bfd_printf_vma (abfd, abfd->start_address);
2117 printf ("\n");
2118 }
2119
2120 \f
2121 static void
2122 dump_bfd_private_header (bfd *abfd)
2123 {
2124 bfd_print_private_bfd_data (abfd, stdout);
2125 }
2126
2127 \f
2128 /* Display a section in hexadecimal format with associated characters.
2129 Each line prefixed by the zero padded address. */
2130
2131 static void
2132 dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
2133 {
2134 bfd_byte *data = 0;
2135 bfd_size_type datasize;
2136 bfd_size_type addr_offset;
2137 bfd_size_type start_offset;
2138 bfd_size_type stop_offset;
2139 unsigned int opb = bfd_octets_per_byte (abfd);
2140 /* Bytes per line. */
2141 const int onaline = 16;
2142 char buf[64];
2143 int count;
2144 int width;
2145
2146 if ((section->flags & SEC_HAS_CONTENTS) == 0)
2147 return;
2148
2149 if (! process_section_p (section))
2150 return;
2151
2152 if ((datasize = bfd_section_size (abfd, section)) == 0)
2153 return;
2154
2155 printf (_("Contents of section %s:\n"), section->name);
2156
2157 data = xmalloc (datasize);
2158
2159 bfd_get_section_contents (abfd, section, data, 0, datasize);
2160
2161 /* Compute the address range to display. */
2162 if (start_address == (bfd_vma) -1
2163 || start_address < section->vma)
2164 start_offset = 0;
2165 else
2166 start_offset = start_address - section->vma;
2167
2168 if (stop_address == (bfd_vma) -1)
2169 stop_offset = datasize / opb;
2170 else
2171 {
2172 if (stop_address < section->vma)
2173 stop_offset = 0;
2174 else
2175 stop_offset = stop_address - section->vma;
2176
2177 if (stop_offset > datasize / opb)
2178 stop_offset = datasize / opb;
2179 }
2180
2181 width = 4;
2182
2183 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
2184 if (strlen (buf) >= sizeof (buf))
2185 abort ();
2186
2187 count = 0;
2188 while (buf[count] == '0' && buf[count+1] != '\0')
2189 count++;
2190 count = strlen (buf) - count;
2191 if (count > width)
2192 width = count;
2193
2194 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
2195 if (strlen (buf) >= sizeof (buf))
2196 abort ();
2197
2198 count = 0;
2199 while (buf[count] == '0' && buf[count+1] != '\0')
2200 count++;
2201 count = strlen (buf) - count;
2202 if (count > width)
2203 width = count;
2204
2205 for (addr_offset = start_offset;
2206 addr_offset < stop_offset; addr_offset += onaline / opb)
2207 {
2208 bfd_size_type j;
2209
2210 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
2211 count = strlen (buf);
2212 if ((size_t) count >= sizeof (buf))
2213 abort ();
2214
2215 putchar (' ');
2216 while (count < width)
2217 {
2218 putchar ('0');
2219 count++;
2220 }
2221 fputs (buf + count - width, stdout);
2222 putchar (' ');
2223
2224 for (j = addr_offset * opb;
2225 j < addr_offset * opb + onaline; j++)
2226 {
2227 if (j < stop_offset * opb)
2228 printf ("%02x", (unsigned) (data[j]));
2229 else
2230 printf (" ");
2231 if ((j & 3) == 3)
2232 printf (" ");
2233 }
2234
2235 printf (" ");
2236 for (j = addr_offset * opb;
2237 j < addr_offset * opb + onaline; j++)
2238 {
2239 if (j >= stop_offset * opb)
2240 printf (" ");
2241 else
2242 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
2243 }
2244 putchar ('\n');
2245 }
2246 free (data);
2247 }
2248
2249 /* Actually display the various requested regions. */
2250
2251 static void
2252 dump_data (bfd *abfd)
2253 {
2254 bfd_map_over_sections (abfd, dump_section, NULL);
2255 }
2256
2257 /* Should perhaps share code and display with nm? */
2258
2259 static void
2260 dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean dynamic)
2261 {
2262 asymbol **current;
2263 long max;
2264 long count;
2265
2266 if (dynamic)
2267 {
2268 current = dynsyms;
2269 max = dynsymcount;
2270 printf ("DYNAMIC SYMBOL TABLE:\n");
2271 }
2272 else
2273 {
2274 current = syms;
2275 max = symcount;
2276 printf ("SYMBOL TABLE:\n");
2277 }
2278
2279 if (max == 0)
2280 printf (_("no symbols\n"));
2281
2282 for (count = 0; count < max; count++)
2283 {
2284 bfd *cur_bfd;
2285
2286 if (*current == NULL)
2287 printf (_("no information for symbol number %ld\n"), count);
2288
2289 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
2290 printf (_("could not determine the type of symbol number %ld\n"),
2291 count);
2292
2293 else if (dump_special_syms
2294 || !bfd_is_target_special_symbol (cur_bfd, *current))
2295 {
2296 const char *name = (*current)->name;
2297
2298 if (do_demangle && name != NULL && *name != '\0')
2299 {
2300 char *alloc;
2301
2302 /* If we want to demangle the name, we demangle it
2303 here, and temporarily clobber it while calling
2304 bfd_print_symbol. FIXME: This is a gross hack. */
2305 alloc = demangle (cur_bfd, name);
2306 (*current)->name = alloc;
2307 bfd_print_symbol (cur_bfd, stdout, *current,
2308 bfd_print_symbol_all);
2309 (*current)->name = name;
2310 free (alloc);
2311 }
2312 else
2313 bfd_print_symbol (cur_bfd, stdout, *current,
2314 bfd_print_symbol_all);
2315 printf ("\n");
2316 }
2317 current++;
2318 }
2319 printf ("\n\n");
2320 }
2321 \f
2322 static void
2323 dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
2324 {
2325 arelent **p;
2326 char *last_filename, *last_functionname;
2327 unsigned int last_line;
2328
2329 /* Get column headers lined up reasonably. */
2330 {
2331 static int width;
2332
2333 if (width == 0)
2334 {
2335 char buf[30];
2336
2337 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
2338 width = strlen (buf) - 7;
2339 }
2340 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
2341 }
2342
2343 last_filename = NULL;
2344 last_functionname = NULL;
2345 last_line = 0;
2346
2347 for (p = relpp; relcount && *p != NULL; p++, relcount--)
2348 {
2349 arelent *q = *p;
2350 const char *filename, *functionname;
2351 unsigned int line;
2352 const char *sym_name;
2353 const char *section_name;
2354
2355 if (start_address != (bfd_vma) -1
2356 && q->address < start_address)
2357 continue;
2358 if (stop_address != (bfd_vma) -1
2359 && q->address > stop_address)
2360 continue;
2361
2362 if (with_line_numbers
2363 && sec != NULL
2364 && bfd_find_nearest_line (abfd, sec, syms, q->address,
2365 &filename, &functionname, &line))
2366 {
2367 if (functionname != NULL
2368 && (last_functionname == NULL
2369 || strcmp (functionname, last_functionname) != 0))
2370 {
2371 printf ("%s():\n", functionname);
2372 if (last_functionname != NULL)
2373 free (last_functionname);
2374 last_functionname = xstrdup (functionname);
2375 }
2376
2377 if (line > 0
2378 && (line != last_line
2379 || (filename != NULL
2380 && last_filename != NULL
2381 && strcmp (filename, last_filename) != 0)))
2382 {
2383 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
2384 last_line = line;
2385 if (last_filename != NULL)
2386 free (last_filename);
2387 if (filename == NULL)
2388 last_filename = NULL;
2389 else
2390 last_filename = xstrdup (filename);
2391 }
2392 }
2393
2394 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
2395 {
2396 sym_name = (*(q->sym_ptr_ptr))->name;
2397 section_name = (*(q->sym_ptr_ptr))->section->name;
2398 }
2399 else
2400 {
2401 sym_name = NULL;
2402 section_name = NULL;
2403 }
2404
2405 if (sym_name)
2406 {
2407 bfd_printf_vma (abfd, q->address);
2408 if (q->howto->name)
2409 printf (" %-16s ", q->howto->name);
2410 else
2411 printf (" %-16d ", q->howto->type);
2412 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
2413 }
2414 else
2415 {
2416 if (section_name == NULL)
2417 section_name = "*unknown*";
2418 bfd_printf_vma (abfd, q->address);
2419 printf (" %-16s [%s]",
2420 q->howto->name,
2421 section_name);
2422 }
2423
2424 if (q->addend)
2425 {
2426 printf ("+0x");
2427 bfd_printf_vma (abfd, q->addend);
2428 }
2429
2430 printf ("\n");
2431 }
2432 }
2433
2434 static void
2435 dump_relocs_in_section (bfd *abfd,
2436 asection *section,
2437 void *dummy ATTRIBUTE_UNUSED)
2438 {
2439 arelent **relpp;
2440 long relcount;
2441 long relsize;
2442
2443 if ( bfd_is_abs_section (section)
2444 || bfd_is_und_section (section)
2445 || bfd_is_com_section (section)
2446 || (! process_section_p (section))
2447 || ((section->flags & SEC_RELOC) == 0))
2448 return;
2449
2450 relsize = bfd_get_reloc_upper_bound (abfd, section);
2451 if (relsize < 0)
2452 bfd_fatal (bfd_get_filename (abfd));
2453
2454 printf ("RELOCATION RECORDS FOR [%s]:", section->name);
2455
2456 if (relsize == 0)
2457 {
2458 printf (" (none)\n\n");
2459 return;
2460 }
2461
2462 relpp = xmalloc (relsize);
2463 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
2464
2465 if (relcount < 0)
2466 bfd_fatal (bfd_get_filename (abfd));
2467 else if (relcount == 0)
2468 printf (" (none)\n\n");
2469 else
2470 {
2471 printf ("\n");
2472 dump_reloc_set (abfd, section, relpp, relcount);
2473 printf ("\n\n");
2474 }
2475 free (relpp);
2476 }
2477
2478 static void
2479 dump_relocs (bfd *abfd)
2480 {
2481 bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
2482 }
2483
2484 static void
2485 dump_dynamic_relocs (bfd *abfd)
2486 {
2487 long relsize;
2488 arelent **relpp;
2489 long relcount;
2490
2491 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2492 if (relsize < 0)
2493 bfd_fatal (bfd_get_filename (abfd));
2494
2495 printf ("DYNAMIC RELOCATION RECORDS");
2496
2497 if (relsize == 0)
2498 printf (" (none)\n\n");
2499 else
2500 {
2501 relpp = xmalloc (relsize);
2502 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
2503
2504 if (relcount < 0)
2505 bfd_fatal (bfd_get_filename (abfd));
2506 else if (relcount == 0)
2507 printf (" (none)\n\n");
2508 else
2509 {
2510 printf ("\n");
2511 dump_reloc_set (abfd, NULL, relpp, relcount);
2512 printf ("\n\n");
2513 }
2514 free (relpp);
2515 }
2516 }
2517
2518 /* Creates a table of paths, to search for source files. */
2519
2520 static void
2521 add_include_path (const char *path)
2522 {
2523 if (path[0] == 0)
2524 return;
2525 include_path_count++;
2526 include_paths = xrealloc (include_paths,
2527 include_path_count * sizeof (*include_paths));
2528 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2529 if (path[1] == ':' && path[2] == 0)
2530 path = concat (path, ".", (const char *) 0);
2531 #endif
2532 include_paths[include_path_count - 1] = path;
2533 }
2534
2535 static void
2536 adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
2537 asection *section,
2538 void *dummy ATTRIBUTE_UNUSED)
2539 {
2540 section->vma += adjust_section_vma;
2541 section->lma += adjust_section_vma;
2542 }
2543
2544 /* Dump selected contents of ABFD. */
2545
2546 static void
2547 dump_bfd (bfd *abfd)
2548 {
2549 /* If we are adjusting section VMA's, change them all now. Changing
2550 the BFD information is a hack. However, we must do it, or
2551 bfd_find_nearest_line will not do the right thing. */
2552 if (adjust_section_vma != 0)
2553 bfd_map_over_sections (abfd, adjust_addresses, NULL);
2554
2555 if (! dump_debugging_tags)
2556 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd),
2557 abfd->xvec->name);
2558 if (dump_ar_hdrs)
2559 print_arelt_descr (stdout, abfd, TRUE);
2560 if (dump_file_header)
2561 dump_bfd_header (abfd);
2562 if (dump_private_headers)
2563 dump_bfd_private_header (abfd);
2564 if (! dump_debugging_tags)
2565 putchar ('\n');
2566 if (dump_section_headers)
2567 dump_headers (abfd);
2568
2569 if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
2570 syms = slurp_symtab (abfd);
2571 if (dump_dynamic_symtab || dump_dynamic_reloc_info
2572 || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
2573 dynsyms = slurp_dynamic_symtab (abfd);
2574 if (disassemble)
2575 {
2576 synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
2577 dynsymcount, dynsyms, &synthsyms);
2578 if (synthcount < 0)
2579 synthcount = 0;
2580 }
2581
2582 if (dump_symtab)
2583 dump_symbols (abfd, FALSE);
2584 if (dump_dynamic_symtab)
2585 dump_symbols (abfd, TRUE);
2586 if (dump_stab_section_info)
2587 dump_stabs (abfd);
2588 if (dump_reloc_info && ! disassemble)
2589 dump_relocs (abfd);
2590 if (dump_dynamic_reloc_info && ! disassemble)
2591 dump_dynamic_relocs (abfd);
2592 if (dump_section_contents)
2593 dump_data (abfd);
2594 if (disassemble)
2595 disassemble_data (abfd);
2596
2597 if (dump_debugging)
2598 {
2599 void *dhandle;
2600
2601 dhandle = read_debugging_info (abfd, syms, symcount);
2602 if (dhandle != NULL)
2603 {
2604 if (! print_debugging_info (stdout, dhandle, abfd, syms, demangle,
2605 dump_debugging_tags ? TRUE : FALSE))
2606 {
2607 non_fatal (_("%s: printing debugging information failed"),
2608 bfd_get_filename (abfd));
2609 exit_status = 1;
2610 }
2611 }
2612 }
2613
2614 if (syms)
2615 {
2616 free (syms);
2617 syms = NULL;
2618 }
2619
2620 if (dynsyms)
2621 {
2622 free (dynsyms);
2623 dynsyms = NULL;
2624 }
2625
2626 if (synthsyms)
2627 {
2628 free (synthsyms);
2629 synthsyms = NULL;
2630 }
2631
2632 symcount = 0;
2633 dynsymcount = 0;
2634 synthcount = 0;
2635 }
2636
2637 static void
2638 display_bfd (bfd *abfd)
2639 {
2640 char **matching;
2641
2642 if (bfd_check_format_matches (abfd, bfd_object, &matching))
2643 {
2644 dump_bfd (abfd);
2645 return;
2646 }
2647
2648 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2649 {
2650 nonfatal (bfd_get_filename (abfd));
2651 list_matching_formats (matching);
2652 free (matching);
2653 return;
2654 }
2655
2656 if (bfd_get_error () != bfd_error_file_not_recognized)
2657 {
2658 nonfatal (bfd_get_filename (abfd));
2659 return;
2660 }
2661
2662 if (bfd_check_format_matches (abfd, bfd_core, &matching))
2663 {
2664 dump_bfd (abfd);
2665 return;
2666 }
2667
2668 nonfatal (bfd_get_filename (abfd));
2669
2670 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2671 {
2672 list_matching_formats (matching);
2673 free (matching);
2674 }
2675 }
2676
2677 static void
2678 display_file (char *filename, char *target)
2679 {
2680 bfd *file;
2681 bfd *arfile = NULL;
2682
2683 if (get_file_size (filename) < 1)
2684 return;
2685
2686 file = bfd_openr (filename, target);
2687 if (file == NULL)
2688 {
2689 nonfatal (filename);
2690 return;
2691 }
2692
2693 /* If the file is an archive, process all of its elements. */
2694 if (bfd_check_format (file, bfd_archive))
2695 {
2696 bfd *last_arfile = NULL;
2697
2698 printf (_("In archive %s:\n"), bfd_get_filename (file));
2699 for (;;)
2700 {
2701 bfd_set_error (bfd_error_no_error);
2702
2703 arfile = bfd_openr_next_archived_file (file, arfile);
2704 if (arfile == NULL)
2705 {
2706 if (bfd_get_error () != bfd_error_no_more_archived_files)
2707 nonfatal (bfd_get_filename (file));
2708 break;
2709 }
2710
2711 display_bfd (arfile);
2712
2713 if (last_arfile != NULL)
2714 bfd_close (last_arfile);
2715 last_arfile = arfile;
2716 }
2717
2718 if (last_arfile != NULL)
2719 bfd_close (last_arfile);
2720 }
2721 else
2722 display_bfd (file);
2723
2724 bfd_close (file);
2725 }
2726 \f
2727 int
2728 main (int argc, char **argv)
2729 {
2730 int c;
2731 char *target = default_target;
2732 bfd_boolean seenflag = FALSE;
2733
2734 #if defined (HAVE_SETLOCALE)
2735 #if defined (HAVE_LC_MESSAGES)
2736 setlocale (LC_MESSAGES, "");
2737 #endif
2738 setlocale (LC_CTYPE, "");
2739 #endif
2740
2741 bindtextdomain (PACKAGE, LOCALEDIR);
2742 textdomain (PACKAGE);
2743
2744 program_name = *argv;
2745 xmalloc_set_program_name (program_name);
2746
2747 START_PROGRESS (program_name, 0);
2748
2749 bfd_init ();
2750 set_default_bfd_target ();
2751
2752 while ((c = getopt_long (argc, argv, "pib:m:M:VvCdDlfaHhrRtTxsSI:j:wE:zgeG",
2753 long_options, (int *) 0))
2754 != EOF)
2755 {
2756 switch (c)
2757 {
2758 case 0:
2759 break; /* We've been given a long option. */
2760 case 'm':
2761 machine = optarg;
2762 break;
2763 case 'M':
2764 if (disassembler_options)
2765 /* Ignore potential memory leak for now. */
2766 disassembler_options = concat (disassembler_options, ",",
2767 optarg, NULL);
2768 else
2769 disassembler_options = optarg;
2770 break;
2771 case 'j':
2772 if (only_used == only_size)
2773 {
2774 only_size += 8;
2775 only = xrealloc (only, only_size * sizeof (char *));
2776 }
2777 only [only_used++] = optarg;
2778 break;
2779 case 'l':
2780 with_line_numbers = TRUE;
2781 break;
2782 case 'b':
2783 target = optarg;
2784 break;
2785 case 'C':
2786 do_demangle = TRUE;
2787 if (optarg != NULL)
2788 {
2789 enum demangling_styles style;
2790
2791 style = cplus_demangle_name_to_style (optarg);
2792 if (style == unknown_demangling)
2793 fatal (_("unknown demangling style `%s'"),
2794 optarg);
2795
2796 cplus_demangle_set_style (style);
2797 }
2798 break;
2799 case 'w':
2800 wide_output = TRUE;
2801 break;
2802 case OPTION_ADJUST_VMA:
2803 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
2804 break;
2805 case OPTION_START_ADDRESS:
2806 start_address = parse_vma (optarg, "--start-address");
2807 break;
2808 case OPTION_STOP_ADDRESS:
2809 stop_address = parse_vma (optarg, "--stop-address");
2810 break;
2811 case 'E':
2812 if (strcmp (optarg, "B") == 0)
2813 endian = BFD_ENDIAN_BIG;
2814 else if (strcmp (optarg, "L") == 0)
2815 endian = BFD_ENDIAN_LITTLE;
2816 else
2817 {
2818 non_fatal (_("unrecognized -E option"));
2819 usage (stderr, 1);
2820 }
2821 break;
2822 case OPTION_ENDIAN:
2823 if (strncmp (optarg, "big", strlen (optarg)) == 0)
2824 endian = BFD_ENDIAN_BIG;
2825 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
2826 endian = BFD_ENDIAN_LITTLE;
2827 else
2828 {
2829 non_fatal (_("unrecognized --endian type `%s'"), optarg);
2830 usage (stderr, 1);
2831 }
2832 break;
2833
2834 case 'f':
2835 dump_file_header = TRUE;
2836 seenflag = TRUE;
2837 break;
2838 case 'i':
2839 formats_info = TRUE;
2840 seenflag = TRUE;
2841 break;
2842 case 'I':
2843 add_include_path (optarg);
2844 break;
2845 case 'p':
2846 dump_private_headers = TRUE;
2847 seenflag = TRUE;
2848 break;
2849 case 'x':
2850 dump_private_headers = TRUE;
2851 dump_symtab = TRUE;
2852 dump_reloc_info = TRUE;
2853 dump_file_header = TRUE;
2854 dump_ar_hdrs = TRUE;
2855 dump_section_headers = TRUE;
2856 seenflag = TRUE;
2857 break;
2858 case 't':
2859 dump_symtab = TRUE;
2860 seenflag = TRUE;
2861 break;
2862 case 'T':
2863 dump_dynamic_symtab = TRUE;
2864 seenflag = TRUE;
2865 break;
2866 case 'd':
2867 disassemble = TRUE;
2868 seenflag = TRUE;
2869 break;
2870 case 'z':
2871 disassemble_zeroes = TRUE;
2872 break;
2873 case 'D':
2874 disassemble = TRUE;
2875 disassemble_all = TRUE;
2876 seenflag = TRUE;
2877 break;
2878 case 'S':
2879 disassemble = TRUE;
2880 with_source_code = TRUE;
2881 seenflag = TRUE;
2882 break;
2883 case 'g':
2884 dump_debugging = 1;
2885 seenflag = TRUE;
2886 break;
2887 case 'e':
2888 dump_debugging = 1;
2889 dump_debugging_tags = 1;
2890 do_demangle = TRUE;
2891 seenflag = TRUE;
2892 break;
2893 case 'G':
2894 dump_stab_section_info = TRUE;
2895 seenflag = TRUE;
2896 break;
2897 case 's':
2898 dump_section_contents = TRUE;
2899 seenflag = TRUE;
2900 break;
2901 case 'r':
2902 dump_reloc_info = TRUE;
2903 seenflag = TRUE;
2904 break;
2905 case 'R':
2906 dump_dynamic_reloc_info = TRUE;
2907 seenflag = TRUE;
2908 break;
2909 case 'a':
2910 dump_ar_hdrs = TRUE;
2911 seenflag = TRUE;
2912 break;
2913 case 'h':
2914 dump_section_headers = TRUE;
2915 seenflag = TRUE;
2916 break;
2917 case 'H':
2918 usage (stdout, 0);
2919 seenflag = TRUE;
2920 case 'v':
2921 case 'V':
2922 show_version = TRUE;
2923 seenflag = TRUE;
2924 break;
2925
2926 default:
2927 usage (stderr, 1);
2928 }
2929 }
2930
2931 if (show_version)
2932 print_version ("objdump");
2933
2934 if (!seenflag)
2935 usage (stderr, 2);
2936
2937 if (formats_info)
2938 exit_status = display_info ();
2939 else
2940 {
2941 if (optind == argc)
2942 display_file ("a.out", target);
2943 else
2944 for (; optind < argc;)
2945 display_file (argv[optind++], target);
2946 }
2947
2948 END_PROGRESS (program_name);
2949
2950 return exit_status;
2951 }
This page took 0.089794 seconds and 4 git commands to generate.