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