OSDN Git Service

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