]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/objdump.c
bfd/
[thirdparty/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
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 = read_section_stabs (abfd, section->name, &stab_size);
2060 if (stabs)
2061 print_section_stabs (abfd, section->name, &sought->string_offset);
2062 }
2063 }
2064 }
2065
2066 static void
2067 dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
2068 {
2069 stab_section_names s;
2070
2071 s.section_name = stabsect_name;
2072 s.string_section_name = strsect_name;
2073 s.string_offset = 0;
2074
2075 bfd_map_over_sections (abfd, find_stabs_section, & s);
2076
2077 free (strtab);
2078 strtab = NULL;
2079 }
2080
2081 /* Dump the any sections containing stabs debugging information. */
2082
2083 static void
2084 dump_stabs (bfd *abfd)
2085 {
2086 dump_stabs_section (abfd, ".stab", ".stabstr");
2087 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
2088 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
2089 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
2090 }
2091 \f
2092 static void
2093 dump_bfd_header (bfd *abfd)
2094 {
2095 char *comma = "";
2096
2097 printf (_("architecture: %s, "),
2098 bfd_printable_arch_mach (bfd_get_arch (abfd),
2099 bfd_get_mach (abfd)));
2100 printf (_("flags 0x%08x:\n"), abfd->flags);
2101
2102 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2103 PF (HAS_RELOC, "HAS_RELOC");
2104 PF (EXEC_P, "EXEC_P");
2105 PF (HAS_LINENO, "HAS_LINENO");
2106 PF (HAS_DEBUG, "HAS_DEBUG");
2107 PF (HAS_SYMS, "HAS_SYMS");
2108 PF (HAS_LOCALS, "HAS_LOCALS");
2109 PF (DYNAMIC, "DYNAMIC");
2110 PF (WP_TEXT, "WP_TEXT");
2111 PF (D_PAGED, "D_PAGED");
2112 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
2113 PF (HAS_LOAD_PAGE, "HAS_LOAD_PAGE");
2114 printf (_("\nstart address 0x"));
2115 bfd_printf_vma (abfd, abfd->start_address);
2116 printf ("\n");
2117 }
2118
2119 \f
2120 static void
2121 dump_bfd_private_header (bfd *abfd)
2122 {
2123 bfd_print_private_bfd_data (abfd, stdout);
2124 }
2125
2126 \f
2127 /* Display a section in hexadecimal format with associated characters.
2128 Each line prefixed by the zero padded address. */
2129
2130 static void
2131 dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
2132 {
2133 bfd_byte *data = 0;
2134 bfd_size_type datasize;
2135 bfd_size_type addr_offset;
2136 bfd_size_type start_offset;
2137 bfd_size_type stop_offset;
2138 unsigned int opb = bfd_octets_per_byte (abfd);
2139 /* Bytes per line. */
2140 const int onaline = 16;
2141 char buf[64];
2142 int count;
2143 int width;
2144
2145 if ((section->flags & SEC_HAS_CONTENTS) == 0)
2146 return;
2147
2148 if (! process_section_p (section))
2149 return;
2150
2151 if ((datasize = bfd_section_size (abfd, section)) == 0)
2152 return;
2153
2154 printf (_("Contents of section %s:\n"), section->name);
2155
2156 data = xmalloc (datasize);
2157
2158 bfd_get_section_contents (abfd, section, data, 0, datasize);
2159
2160 /* Compute the address range to display. */
2161 if (start_address == (bfd_vma) -1
2162 || start_address < section->vma)
2163 start_offset = 0;
2164 else
2165 start_offset = start_address - section->vma;
2166
2167 if (stop_address == (bfd_vma) -1)
2168 stop_offset = datasize / opb;
2169 else
2170 {
2171 if (stop_address < section->vma)
2172 stop_offset = 0;
2173 else
2174 stop_offset = stop_address - section->vma;
2175
2176 if (stop_offset > datasize / opb)
2177 stop_offset = datasize / opb;
2178 }
2179
2180 width = 4;
2181
2182 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
2183 if (strlen (buf) >= sizeof (buf))
2184 abort ();
2185
2186 count = 0;
2187 while (buf[count] == '0' && buf[count+1] != '\0')
2188 count++;
2189 count = strlen (buf) - count;
2190 if (count > width)
2191 width = count;
2192
2193 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
2194 if (strlen (buf) >= sizeof (buf))
2195 abort ();
2196
2197 count = 0;
2198 while (buf[count] == '0' && buf[count+1] != '\0')
2199 count++;
2200 count = strlen (buf) - count;
2201 if (count > width)
2202 width = count;
2203
2204 for (addr_offset = start_offset;
2205 addr_offset < stop_offset; addr_offset += onaline / opb)
2206 {
2207 bfd_size_type j;
2208
2209 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
2210 count = strlen (buf);
2211 if ((size_t) count >= sizeof (buf))
2212 abort ();
2213
2214 putchar (' ');
2215 while (count < width)
2216 {
2217 putchar ('0');
2218 count++;
2219 }
2220 fputs (buf + count - width, stdout);
2221 putchar (' ');
2222
2223 for (j = addr_offset * opb;
2224 j < addr_offset * opb + onaline; j++)
2225 {
2226 if (j < stop_offset * opb)
2227 printf ("%02x", (unsigned) (data[j]));
2228 else
2229 printf (" ");
2230 if ((j & 3) == 3)
2231 printf (" ");
2232 }
2233
2234 printf (" ");
2235 for (j = addr_offset * opb;
2236 j < addr_offset * opb + onaline; j++)
2237 {
2238 if (j >= stop_offset * opb)
2239 printf (" ");
2240 else
2241 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
2242 }
2243 putchar ('\n');
2244 }
2245 free (data);
2246 }
2247
2248 /* Actually display the various requested regions. */
2249
2250 static void
2251 dump_data (bfd *abfd)
2252 {
2253 bfd_map_over_sections (abfd, dump_section, NULL);
2254 }
2255
2256 /* Should perhaps share code and display with nm? */
2257
2258 static void
2259 dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean dynamic)
2260 {
2261 asymbol **current;
2262 long max;
2263 long count;
2264
2265 if (dynamic)
2266 {
2267 current = dynsyms;
2268 max = dynsymcount;
2269 printf ("DYNAMIC SYMBOL TABLE:\n");
2270 }
2271 else
2272 {
2273 current = syms;
2274 max = symcount;
2275 printf ("SYMBOL TABLE:\n");
2276 }
2277
2278 if (max == 0)
2279 printf (_("no symbols\n"));
2280
2281 for (count = 0; count < max; count++)
2282 {
2283 bfd *cur_bfd;
2284
2285 if (*current == NULL)
2286 printf (_("no information for symbol number %ld\n"), count);
2287
2288 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
2289 printf (_("could not determine the type of symbol number %ld\n"),
2290 count);
2291
2292 else if (dump_special_syms
2293 || !bfd_is_target_special_symbol (cur_bfd, *current))
2294 {
2295 const char *name = (*current)->name;
2296
2297 if (do_demangle && name != NULL && *name != '\0')
2298 {
2299 char *alloc;
2300
2301 /* If we want to demangle the name, we demangle it
2302 here, and temporarily clobber it while calling
2303 bfd_print_symbol. FIXME: This is a gross hack. */
2304 alloc = demangle (cur_bfd, name);
2305 (*current)->name = alloc;
2306 bfd_print_symbol (cur_bfd, stdout, *current,
2307 bfd_print_symbol_all);
2308 (*current)->name = name;
2309 free (alloc);
2310 }
2311 else
2312 bfd_print_symbol (cur_bfd, stdout, *current,
2313 bfd_print_symbol_all);
2314 printf ("\n");
2315 }
2316 current++;
2317 }
2318 printf ("\n\n");
2319 }
2320 \f
2321 static void
2322 dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
2323 {
2324 arelent **p;
2325 char *last_filename, *last_functionname;
2326 unsigned int last_line;
2327
2328 /* Get column headers lined up reasonably. */
2329 {
2330 static int width;
2331
2332 if (width == 0)
2333 {
2334 char buf[30];
2335
2336 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
2337 width = strlen (buf) - 7;
2338 }
2339 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
2340 }
2341
2342 last_filename = NULL;
2343 last_functionname = NULL;
2344 last_line = 0;
2345
2346 for (p = relpp; relcount && *p != NULL; p++, relcount--)
2347 {
2348 arelent *q = *p;
2349 const char *filename, *functionname;
2350 unsigned int line;
2351 const char *sym_name;
2352 const char *section_name;
2353
2354 if (start_address != (bfd_vma) -1
2355 && q->address < start_address)
2356 continue;
2357 if (stop_address != (bfd_vma) -1
2358 && q->address > stop_address)
2359 continue;
2360
2361 if (with_line_numbers
2362 && sec != NULL
2363 && bfd_find_nearest_line (abfd, sec, syms, q->address,
2364 &filename, &functionname, &line))
2365 {
2366 if (functionname != NULL
2367 && (last_functionname == NULL
2368 || strcmp (functionname, last_functionname) != 0))
2369 {
2370 printf ("%s():\n", functionname);
2371 if (last_functionname != NULL)
2372 free (last_functionname);
2373 last_functionname = xstrdup (functionname);
2374 }
2375
2376 if (line > 0
2377 && (line != last_line
2378 || (filename != NULL
2379 && last_filename != NULL
2380 && strcmp (filename, last_filename) != 0)))
2381 {
2382 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
2383 last_line = line;
2384 if (last_filename != NULL)
2385 free (last_filename);
2386 if (filename == NULL)
2387 last_filename = NULL;
2388 else
2389 last_filename = xstrdup (filename);
2390 }
2391 }
2392
2393 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
2394 {
2395 sym_name = (*(q->sym_ptr_ptr))->name;
2396 section_name = (*(q->sym_ptr_ptr))->section->name;
2397 }
2398 else
2399 {
2400 sym_name = NULL;
2401 section_name = NULL;
2402 }
2403
2404 if (sym_name)
2405 {
2406 bfd_printf_vma (abfd, q->address);
2407 if (q->howto->name)
2408 printf (" %-16s ", q->howto->name);
2409 else
2410 printf (" %-16d ", q->howto->type);
2411 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
2412 }
2413 else
2414 {
2415 if (section_name == NULL)
2416 section_name = "*unknown*";
2417 bfd_printf_vma (abfd, q->address);
2418 printf (" %-16s [%s]",
2419 q->howto->name,
2420 section_name);
2421 }
2422
2423 if (q->addend)
2424 {
2425 printf ("+0x");
2426 bfd_printf_vma (abfd, q->addend);
2427 }
2428
2429 printf ("\n");
2430 }
2431 }
2432
2433 static void
2434 dump_relocs_in_section (bfd *abfd,
2435 asection *section,
2436 void *dummy ATTRIBUTE_UNUSED)
2437 {
2438 arelent **relpp;
2439 long relcount;
2440 long relsize;
2441
2442 if ( bfd_is_abs_section (section)
2443 || bfd_is_und_section (section)
2444 || bfd_is_com_section (section)
2445 || (! process_section_p (section))
2446 || ((section->flags & SEC_RELOC) == 0))
2447 return;
2448
2449 relsize = bfd_get_reloc_upper_bound (abfd, section);
2450 if (relsize < 0)
2451 bfd_fatal (bfd_get_filename (abfd));
2452
2453 printf ("RELOCATION RECORDS FOR [%s]:", section->name);
2454
2455 if (relsize == 0)
2456 {
2457 printf (" (none)\n\n");
2458 return;
2459 }
2460
2461 relpp = xmalloc (relsize);
2462 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
2463
2464 if (relcount < 0)
2465 bfd_fatal (bfd_get_filename (abfd));
2466 else if (relcount == 0)
2467 printf (" (none)\n\n");
2468 else
2469 {
2470 printf ("\n");
2471 dump_reloc_set (abfd, section, relpp, relcount);
2472 printf ("\n\n");
2473 }
2474 free (relpp);
2475 }
2476
2477 static void
2478 dump_relocs (bfd *abfd)
2479 {
2480 bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
2481 }
2482
2483 static void
2484 dump_dynamic_relocs (bfd *abfd)
2485 {
2486 long relsize;
2487 arelent **relpp;
2488 long relcount;
2489
2490 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2491 if (relsize < 0)
2492 bfd_fatal (bfd_get_filename (abfd));
2493
2494 printf ("DYNAMIC RELOCATION RECORDS");
2495
2496 if (relsize == 0)
2497 printf (" (none)\n\n");
2498 else
2499 {
2500 relpp = xmalloc (relsize);
2501 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
2502
2503 if (relcount < 0)
2504 bfd_fatal (bfd_get_filename (abfd));
2505 else if (relcount == 0)
2506 printf (" (none)\n\n");
2507 else
2508 {
2509 printf ("\n");
2510 dump_reloc_set (abfd, NULL, relpp, relcount);
2511 printf ("\n\n");
2512 }
2513 free (relpp);
2514 }
2515 }
2516
2517 /* Creates a table of paths, to search for source files. */
2518
2519 static void
2520 add_include_path (const char *path)
2521 {
2522 if (path[0] == 0)
2523 return;
2524 include_path_count++;
2525 include_paths = xrealloc (include_paths,
2526 include_path_count * sizeof (*include_paths));
2527 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2528 if (path[1] == ':' && path[2] == 0)
2529 path = concat (path, ".", (const char *) 0);
2530 #endif
2531 include_paths[include_path_count - 1] = path;
2532 }
2533
2534 static void
2535 adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
2536 asection *section,
2537 void *dummy ATTRIBUTE_UNUSED)
2538 {
2539 section->vma += adjust_section_vma;
2540 section->lma += adjust_section_vma;
2541 }
2542
2543 /* Dump selected contents of ABFD. */
2544
2545 static void
2546 dump_bfd (bfd *abfd)
2547 {
2548 /* If we are adjusting section VMA's, change them all now. Changing
2549 the BFD information is a hack. However, we must do it, or
2550 bfd_find_nearest_line will not do the right thing. */
2551 if (adjust_section_vma != 0)
2552 bfd_map_over_sections (abfd, adjust_addresses, NULL);
2553
2554 if (! dump_debugging_tags)
2555 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd),
2556 abfd->xvec->name);
2557 if (dump_ar_hdrs)
2558 print_arelt_descr (stdout, abfd, TRUE);
2559 if (dump_file_header)
2560 dump_bfd_header (abfd);
2561 if (dump_private_headers)
2562 dump_bfd_private_header (abfd);
2563 if (! dump_debugging_tags)
2564 putchar ('\n');
2565 if (dump_section_headers)
2566 dump_headers (abfd);
2567
2568 if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
2569 syms = slurp_symtab (abfd);
2570 if (dump_dynamic_symtab || dump_dynamic_reloc_info
2571 || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
2572 dynsyms = slurp_dynamic_symtab (abfd);
2573 if (disassemble)
2574 {
2575 synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
2576 dynsymcount, dynsyms, &synthsyms);
2577 if (synthcount < 0)
2578 synthcount = 0;
2579 }
2580
2581 if (dump_symtab)
2582 dump_symbols (abfd, FALSE);
2583 if (dump_dynamic_symtab)
2584 dump_symbols (abfd, TRUE);
2585 if (dump_stab_section_info)
2586 dump_stabs (abfd);
2587 if (dump_reloc_info && ! disassemble)
2588 dump_relocs (abfd);
2589 if (dump_dynamic_reloc_info && ! disassemble)
2590 dump_dynamic_relocs (abfd);
2591 if (dump_section_contents)
2592 dump_data (abfd);
2593 if (disassemble)
2594 disassemble_data (abfd);
2595
2596 if (dump_debugging)
2597 {
2598 void *dhandle;
2599
2600 dhandle = read_debugging_info (abfd, syms, symcount);
2601 if (dhandle != NULL)
2602 {
2603 if (! print_debugging_info (stdout, dhandle, abfd, syms, demangle,
2604 dump_debugging_tags ? TRUE : FALSE))
2605 {
2606 non_fatal (_("%s: printing debugging information failed"),
2607 bfd_get_filename (abfd));
2608 exit_status = 1;
2609 }
2610 }
2611 }
2612
2613 if (syms)
2614 {
2615 free (syms);
2616 syms = NULL;
2617 }
2618
2619 if (dynsyms)
2620 {
2621 free (dynsyms);
2622 dynsyms = NULL;
2623 }
2624
2625 if (synthsyms)
2626 {
2627 free (synthsyms);
2628 synthsyms = NULL;
2629 }
2630
2631 symcount = 0;
2632 dynsymcount = 0;
2633 synthcount = 0;
2634 }
2635
2636 static void
2637 display_bfd (bfd *abfd)
2638 {
2639 char **matching;
2640
2641 if (bfd_check_format_matches (abfd, bfd_object, &matching))
2642 {
2643 dump_bfd (abfd);
2644 return;
2645 }
2646
2647 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2648 {
2649 nonfatal (bfd_get_filename (abfd));
2650 list_matching_formats (matching);
2651 free (matching);
2652 return;
2653 }
2654
2655 if (bfd_get_error () != bfd_error_file_not_recognized)
2656 {
2657 nonfatal (bfd_get_filename (abfd));
2658 return;
2659 }
2660
2661 if (bfd_check_format_matches (abfd, bfd_core, &matching))
2662 {
2663 dump_bfd (abfd);
2664 return;
2665 }
2666
2667 nonfatal (bfd_get_filename (abfd));
2668
2669 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2670 {
2671 list_matching_formats (matching);
2672 free (matching);
2673 }
2674 }
2675
2676 static void
2677 display_file (char *filename, char *target)
2678 {
2679 bfd *file;
2680 bfd *arfile = NULL;
2681
2682 if (get_file_size (filename) < 1)
2683 return;
2684
2685 file = bfd_openr (filename, target);
2686 if (file == NULL)
2687 {
2688 nonfatal (filename);
2689 return;
2690 }
2691
2692 /* If the file is an archive, process all of its elements. */
2693 if (bfd_check_format (file, bfd_archive))
2694 {
2695 bfd *last_arfile = NULL;
2696
2697 printf (_("In archive %s:\n"), bfd_get_filename (file));
2698 for (;;)
2699 {
2700 bfd_set_error (bfd_error_no_error);
2701
2702 arfile = bfd_openr_next_archived_file (file, arfile);
2703 if (arfile == NULL)
2704 {
2705 if (bfd_get_error () != bfd_error_no_more_archived_files)
2706 nonfatal (bfd_get_filename (file));
2707 break;
2708 }
2709
2710 display_bfd (arfile);
2711
2712 if (last_arfile != NULL)
2713 bfd_close (last_arfile);
2714 last_arfile = arfile;
2715 }
2716
2717 if (last_arfile != NULL)
2718 bfd_close (last_arfile);
2719 }
2720 else
2721 display_bfd (file);
2722
2723 bfd_close (file);
2724 }
2725 \f
2726 int
2727 main (int argc, char **argv)
2728 {
2729 int c;
2730 char *target = default_target;
2731 bfd_boolean seenflag = FALSE;
2732
2733 #if defined (HAVE_SETLOCALE)
2734 #if defined (HAVE_LC_MESSAGES)
2735 setlocale (LC_MESSAGES, "");
2736 #endif
2737 setlocale (LC_CTYPE, "");
2738 #endif
2739
2740 bindtextdomain (PACKAGE, LOCALEDIR);
2741 textdomain (PACKAGE);
2742
2743 program_name = *argv;
2744 xmalloc_set_program_name (program_name);
2745
2746 START_PROGRESS (program_name, 0);
2747
2748 bfd_init ();
2749 set_default_bfd_target ();
2750
2751 while ((c = getopt_long (argc, argv, "pib:m:M:VvCdDlfaHhrRtTxsSI:j:wE:zgeG",
2752 long_options, (int *) 0))
2753 != EOF)
2754 {
2755 switch (c)
2756 {
2757 case 0:
2758 break; /* We've been given a long option. */
2759 case 'm':
2760 machine = optarg;
2761 break;
2762 case 'M':
2763 if (disassembler_options)
2764 /* Ignore potential memory leak for now. */
2765 disassembler_options = concat (disassembler_options, ",",
2766 optarg, NULL);
2767 else
2768 disassembler_options = optarg;
2769 break;
2770 case 'j':
2771 if (only_used == only_size)
2772 {
2773 only_size += 8;
2774 only = xrealloc (only, only_size * sizeof (char *));
2775 }
2776 only [only_used++] = optarg;
2777 break;
2778 case 'l':
2779 with_line_numbers = TRUE;
2780 break;
2781 case 'b':
2782 target = optarg;
2783 break;
2784 case 'C':
2785 do_demangle = TRUE;
2786 if (optarg != NULL)
2787 {
2788 enum demangling_styles style;
2789
2790 style = cplus_demangle_name_to_style (optarg);
2791 if (style == unknown_demangling)
2792 fatal (_("unknown demangling style `%s'"),
2793 optarg);
2794
2795 cplus_demangle_set_style (style);
2796 }
2797 break;
2798 case 'w':
2799 wide_output = TRUE;
2800 break;
2801 case OPTION_ADJUST_VMA:
2802 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
2803 break;
2804 case OPTION_START_ADDRESS:
2805 start_address = parse_vma (optarg, "--start-address");
2806 break;
2807 case OPTION_STOP_ADDRESS:
2808 stop_address = parse_vma (optarg, "--stop-address");
2809 break;
2810 case 'E':
2811 if (strcmp (optarg, "B") == 0)
2812 endian = BFD_ENDIAN_BIG;
2813 else if (strcmp (optarg, "L") == 0)
2814 endian = BFD_ENDIAN_LITTLE;
2815 else
2816 {
2817 non_fatal (_("unrecognized -E option"));
2818 usage (stderr, 1);
2819 }
2820 break;
2821 case OPTION_ENDIAN:
2822 if (strncmp (optarg, "big", strlen (optarg)) == 0)
2823 endian = BFD_ENDIAN_BIG;
2824 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
2825 endian = BFD_ENDIAN_LITTLE;
2826 else
2827 {
2828 non_fatal (_("unrecognized --endian type `%s'"), optarg);
2829 usage (stderr, 1);
2830 }
2831 break;
2832
2833 case 'f':
2834 dump_file_header = TRUE;
2835 seenflag = TRUE;
2836 break;
2837 case 'i':
2838 formats_info = TRUE;
2839 seenflag = TRUE;
2840 break;
2841 case 'I':
2842 add_include_path (optarg);
2843 break;
2844 case 'p':
2845 dump_private_headers = TRUE;
2846 seenflag = TRUE;
2847 break;
2848 case 'x':
2849 dump_private_headers = TRUE;
2850 dump_symtab = TRUE;
2851 dump_reloc_info = TRUE;
2852 dump_file_header = TRUE;
2853 dump_ar_hdrs = TRUE;
2854 dump_section_headers = TRUE;
2855 seenflag = TRUE;
2856 break;
2857 case 't':
2858 dump_symtab = TRUE;
2859 seenflag = TRUE;
2860 break;
2861 case 'T':
2862 dump_dynamic_symtab = TRUE;
2863 seenflag = TRUE;
2864 break;
2865 case 'd':
2866 disassemble = TRUE;
2867 seenflag = TRUE;
2868 break;
2869 case 'z':
2870 disassemble_zeroes = TRUE;
2871 break;
2872 case 'D':
2873 disassemble = TRUE;
2874 disassemble_all = TRUE;
2875 seenflag = TRUE;
2876 break;
2877 case 'S':
2878 disassemble = TRUE;
2879 with_source_code = TRUE;
2880 seenflag = TRUE;
2881 break;
2882 case 'g':
2883 dump_debugging = 1;
2884 seenflag = TRUE;
2885 break;
2886 case 'e':
2887 dump_debugging = 1;
2888 dump_debugging_tags = 1;
2889 do_demangle = TRUE;
2890 seenflag = TRUE;
2891 break;
2892 case 'G':
2893 dump_stab_section_info = TRUE;
2894 seenflag = TRUE;
2895 break;
2896 case 's':
2897 dump_section_contents = TRUE;
2898 seenflag = TRUE;
2899 break;
2900 case 'r':
2901 dump_reloc_info = TRUE;
2902 seenflag = TRUE;
2903 break;
2904 case 'R':
2905 dump_dynamic_reloc_info = TRUE;
2906 seenflag = TRUE;
2907 break;
2908 case 'a':
2909 dump_ar_hdrs = TRUE;
2910 seenflag = TRUE;
2911 break;
2912 case 'h':
2913 dump_section_headers = TRUE;
2914 seenflag = TRUE;
2915 break;
2916 case 'H':
2917 usage (stdout, 0);
2918 seenflag = TRUE;
2919 case 'v':
2920 case 'V':
2921 show_version = TRUE;
2922 seenflag = TRUE;
2923 break;
2924
2925 default:
2926 usage (stderr, 1);
2927 }
2928 }
2929
2930 if (show_version)
2931 print_version ("objdump");
2932
2933 if (!seenflag)
2934 usage (stderr, 2);
2935
2936 if (formats_info)
2937 exit_status = display_info ();
2938 else
2939 {
2940 if (optind == argc)
2941 display_file ("a.out", target);
2942 else
2943 for (; optind < argc;)
2944 display_file (argv[optind++], target);
2945 }
2946
2947 END_PROGRESS (program_name);
2948
2949 return exit_status;
2950 }