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