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