* objdump.c (dump_section_header): Print the SEC_LINK_ONCE flag
[binutils-gdb.git] / binutils / objdump.c
1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3
4 This file is part of GNU Binutils.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 #include "bfd.h"
21 #include "getopt.h"
22 #include "progress.h"
23 #include "bucomm.h"
24 #include <ctype.h>
25 #include "dis-asm.h"
26 #include "libiberty.h"
27 #include "debug.h"
28 #include "budbg.h"
29
30 #ifdef ANSI_PROTOTYPES
31 #include <stdarg.h>
32 #else
33 #include <varargs.h>
34 #endif
35
36 /* Internal headers for the ELF .stab-dump code - sorry. */
37 #define BYTES_IN_WORD 32
38 #include "aout/aout64.h"
39
40 #ifdef NEED_DECLARATION_FPRINTF
41 /* This is needed by INIT_DISASSEMBLE_INFO. */
42 extern int fprintf PARAMS ((FILE *, const char *, ...));
43 #endif
44
45 char *default_target = NULL; /* default at runtime */
46
47 extern char *program_version;
48
49 int show_version = 0; /* show the version number */
50 int dump_section_contents; /* -s */
51 int dump_section_headers; /* -h */
52 boolean dump_file_header; /* -f */
53 int dump_symtab; /* -t */
54 int dump_dynamic_symtab; /* -T */
55 int dump_reloc_info; /* -r */
56 int dump_dynamic_reloc_info; /* -R */
57 int dump_ar_hdrs; /* -a */
58 int dump_private_headers; /* -p */
59 int with_line_numbers; /* -l */
60 boolean with_source_code; /* -S */
61 int show_raw_insn; /* --show-raw-insn */
62 int dump_stab_section_info; /* --stabs */
63 boolean disassemble; /* -d */
64 boolean disassemble_all; /* -D */
65 boolean formats_info; /* -i */
66 char *only; /* -j secname */
67 int wide_output; /* -w */
68 bfd_vma start_address = (bfd_vma) -1; /* --start-address */
69 bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
70 int dump_debugging; /* --debugging */
71
72 /* Extra info to pass to the disassembler address printing function. */
73 struct objdump_disasm_info {
74 bfd *abfd;
75 asection *sec;
76 boolean require_sec;
77 };
78
79 /* Architecture to disassemble for, or default if NULL. */
80 char *machine = (char *) NULL;
81
82 /* The symbol table. */
83 asymbol **syms;
84
85 /* Number of symbols in `syms'. */
86 long symcount = 0;
87
88 /* The sorted symbol table. */
89 asymbol **sorted_syms;
90
91 /* Number of symbols in `sorted_syms'. */
92 long sorted_symcount = 0;
93
94 /* The dynamic symbol table. */
95 asymbol **dynsyms;
96
97 /* Number of symbols in `dynsyms'. */
98 long dynsymcount = 0;
99
100 /* Forward declarations. */
101
102 static void
103 display_file PARAMS ((char *filename, char *target));
104
105 static void
106 dump_data PARAMS ((bfd *abfd));
107
108 static void
109 dump_relocs PARAMS ((bfd *abfd));
110
111 static void
112 dump_dynamic_relocs PARAMS ((bfd * abfd));
113
114 static void
115 dump_reloc_set PARAMS ((bfd *, arelent **, long));
116
117 static void
118 dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
119
120 static void
121 display_bfd PARAMS ((bfd *abfd));
122
123 static void
124 objdump_print_value PARAMS ((bfd_vma, struct disassemble_info *));
125
126 static void
127 objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *));
128
129 static void
130 show_line PARAMS ((bfd *, asection *, bfd_vma));
131
132 static const char *
133 endian_string PARAMS ((enum bfd_endian));
134 \f
135 void
136 usage (stream, status)
137 FILE *stream;
138 int status;
139 {
140 fprintf (stream, "\
141 Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
142 [--archive-headers] [--target=bfdname] [--debugging] [--disassemble]\n\
143 [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
144 [--info] [--section=section-name] [--line-numbers] [--source]\n",
145 program_name);
146 fprintf (stream, "\
147 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
148 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
149 [--wide] [--version] [--help] [--private-headers]\n\
150 [--start-address=addr] [--stop-address=addr]\n\
151 [--show-raw-insn] objfile...\n\
152 at least one option besides -l (--line-numbers) must be given\n");
153 list_supported_targets (program_name, stream);
154 exit (status);
155 }
156
157 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
158
159 #define OPTION_START_ADDRESS (150)
160 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
161
162 static struct option long_options[]=
163 {
164 {"all-headers", no_argument, NULL, 'x'},
165 {"private-headers", no_argument, NULL, 'p'},
166 {"architecture", required_argument, NULL, 'm'},
167 {"archive-headers", no_argument, NULL, 'a'},
168 {"debugging", no_argument, &dump_debugging, 1},
169 {"disassemble", no_argument, NULL, 'd'},
170 {"disassemble-all", no_argument, NULL, 'D'},
171 {"dynamic-reloc", no_argument, NULL, 'R'},
172 {"dynamic-syms", no_argument, NULL, 'T'},
173 {"file-headers", no_argument, NULL, 'f'},
174 {"full-contents", no_argument, NULL, 's'},
175 {"headers", no_argument, NULL, 'h'},
176 {"help", no_argument, NULL, 'H'},
177 {"info", no_argument, NULL, 'i'},
178 {"line-numbers", no_argument, NULL, 'l'},
179 {"reloc", no_argument, NULL, 'r'},
180 {"section", required_argument, NULL, 'j'},
181 {"section-headers", no_argument, NULL, 'h'},
182 {"show-raw-insn", no_argument, &show_raw_insn, 1},
183 {"source", no_argument, NULL, 'S'},
184 {"stabs", no_argument, &dump_stab_section_info, 1},
185 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
186 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
187 {"syms", no_argument, NULL, 't'},
188 {"target", required_argument, NULL, 'b'},
189 {"version", no_argument, &show_version, 1},
190 {"wide", no_argument, &wide_output, 'w'},
191 {0, no_argument, 0, 0}
192 };
193 \f
194 static void
195 dump_section_header (abfd, section, ignored)
196 bfd *abfd;
197 asection *section;
198 PTR ignored;
199 {
200 char *comma = "";
201
202 printf ("%3d %-13s %08lx ", section->index,
203 bfd_get_section_name (abfd, section),
204 (unsigned long) bfd_section_size (abfd, section));
205 printf_vma (bfd_get_section_vma (abfd, section));
206 printf (" ");
207 printf_vma (section->lma);
208 printf (" %08lx 2**%u", section->filepos,
209 bfd_get_section_alignment (abfd, section));
210 if (! wide_output)
211 printf ("\n ");
212 printf (" ");
213
214 #define PF(x, y) \
215 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
216
217 PF (SEC_HAS_CONTENTS, "CONTENTS");
218 PF (SEC_ALLOC, "ALLOC");
219 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
220 PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
221 PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
222 PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
223 PF (SEC_LOAD, "LOAD");
224 PF (SEC_RELOC, "RELOC");
225 #ifdef SEC_BALIGN
226 PF (SEC_BALIGN, "BALIGN");
227 #endif
228 PF (SEC_READONLY, "READONLY");
229 PF (SEC_CODE, "CODE");
230 PF (SEC_DATA, "DATA");
231 PF (SEC_ROM, "ROM");
232 PF (SEC_DEBUGGING, "DEBUGGING");
233 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
234 PF (SEC_EXCLUDE, "EXCLUDE");
235 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
236
237 if ((section->flags & SEC_LINK_ONCE) != 0)
238 {
239 const char *ls;
240
241 switch (section->flags & SEC_LINK_DUPLICATES)
242 {
243 default:
244 abort ();
245 case SEC_LINK_DUPLICATES_DISCARD:
246 ls = "LINK_ONCE_DISCARD";
247 break;
248 case SEC_LINK_DUPLICATES_ONE_ONLY:
249 ls = "LINK_ONCE_ONE_ONLY";
250 break;
251 case SEC_LINK_DUPLICATES_SAME_SIZE:
252 ls = "LINK_ONCE_SAME_SIZE";
253 break;
254 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
255 ls = "LINK_ONCE_SAME_CONTENTS";
256 break;
257 }
258 printf ("%s%s", comma, ls);
259 comma = ", ";
260 }
261
262 printf ("\n");
263 #undef PF
264 }
265
266 static void
267 dump_headers (abfd)
268 bfd *abfd;
269 {
270 printf ("Sections:\n");
271 #ifndef BFD64
272 printf ("Idx Name Size VMA LMA File off Algn\n");
273 #else
274 printf ("Idx Name Size VMA LMA File off Algn\n");
275 #endif
276 bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
277 }
278 \f
279 static asymbol **
280 slurp_symtab (abfd)
281 bfd *abfd;
282 {
283 asymbol **sy = (asymbol **) NULL;
284 long storage;
285
286 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
287 {
288 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
289 symcount = 0;
290 return NULL;
291 }
292
293 storage = bfd_get_symtab_upper_bound (abfd);
294 if (storage < 0)
295 bfd_fatal (bfd_get_filename (abfd));
296
297 if (storage)
298 {
299 sy = (asymbol **) xmalloc (storage);
300 }
301 symcount = bfd_canonicalize_symtab (abfd, sy);
302 if (symcount < 0)
303 bfd_fatal (bfd_get_filename (abfd));
304 if (symcount == 0)
305 fprintf (stderr, "%s: %s: No symbols\n",
306 program_name, bfd_get_filename (abfd));
307 return sy;
308 }
309
310 /* Read in the dynamic symbols. */
311
312 static asymbol **
313 slurp_dynamic_symtab (abfd)
314 bfd *abfd;
315 {
316 asymbol **sy = (asymbol **) NULL;
317 long storage;
318
319 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
320 if (storage < 0)
321 {
322 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
323 {
324 fprintf (stderr, "%s: %s: not a dynamic object\n",
325 program_name, bfd_get_filename (abfd));
326 dynsymcount = 0;
327 return NULL;
328 }
329
330 bfd_fatal (bfd_get_filename (abfd));
331 }
332
333 if (storage)
334 {
335 sy = (asymbol **) xmalloc (storage);
336 }
337 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
338 if (dynsymcount < 0)
339 bfd_fatal (bfd_get_filename (abfd));
340 if (dynsymcount == 0)
341 fprintf (stderr, "%s: %s: No dynamic symbols\n",
342 program_name, bfd_get_filename (abfd));
343 return sy;
344 }
345
346 /* Filter out (in place) symbols that are useless for disassembly.
347 COUNT is the number of elements in SYMBOLS.
348 Return the number of useful symbols. */
349
350 long
351 remove_useless_symbols (symbols, count)
352 asymbol **symbols;
353 long count;
354 {
355 register asymbol **in_ptr = symbols, **out_ptr = symbols;
356
357 while (--count >= 0)
358 {
359 asymbol *sym = *in_ptr++;
360
361 if (sym->name == NULL || sym->name[0] == '\0')
362 continue;
363 if (sym->flags & (BSF_DEBUGGING))
364 continue;
365 if (bfd_is_und_section (sym->section)
366 || bfd_is_com_section (sym->section))
367 continue;
368
369 *out_ptr++ = sym;
370 }
371 return out_ptr - symbols;
372 }
373
374 /* Sort symbols into value order. */
375
376 static int
377 compare_symbols (ap, bp)
378 const PTR ap;
379 const PTR bp;
380 {
381 const asymbol *a = *(const asymbol **)ap;
382 const asymbol *b = *(const asymbol **)bp;
383 const char *an, *bn;
384 size_t anl, bnl;
385 boolean af, bf;
386 flagword aflags, bflags;
387
388 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
389 return 1;
390 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
391 return -1;
392
393 if (a->section > b->section)
394 return 1;
395 else if (a->section < b->section)
396 return -1;
397
398 an = bfd_asymbol_name (a);
399 bn = bfd_asymbol_name (b);
400 anl = strlen (an);
401 bnl = strlen (bn);
402
403 /* The symbols gnu_compiled and gcc2_compiled convey no real
404 information, so put them after other symbols with the same value. */
405
406 af = (strstr (an, "gnu_compiled") != NULL
407 || strstr (an, "gcc2_compiled") != NULL);
408 bf = (strstr (bn, "gnu_compiled") != NULL
409 || strstr (bn, "gcc2_compiled") != NULL);
410
411 if (af && ! bf)
412 return 1;
413 if (! af && bf)
414 return -1;
415
416 /* We use a heuristic for the file name, to try to sort it after
417 more useful symbols. It may not work on non Unix systems, but it
418 doesn't really matter; the only difference is precisely which
419 symbol names get printed. */
420
421 #define file_symbol(s, sn, snl) \
422 (((s)->flags & BSF_FILE) != 0 \
423 || ((sn)[(snl) - 2] == '.' \
424 && ((sn)[(snl) - 1] == 'o' \
425 || (sn)[(snl) - 1] == 'a')))
426
427 af = file_symbol (a, an, anl);
428 bf = file_symbol (b, bn, bnl);
429
430 if (af && ! bf)
431 return 1;
432 if (! af && bf)
433 return -1;
434
435 /* Finally, try to sort global symbols before local symbols before
436 debugging symbols. */
437
438 aflags = a->flags;
439 bflags = b->flags;
440
441 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
442 {
443 if ((aflags & BSF_DEBUGGING) != 0)
444 return 1;
445 else
446 return -1;
447 }
448 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
449 {
450 if ((aflags & BSF_LOCAL) != 0)
451 return 1;
452 else
453 return -1;
454 }
455
456 return 0;
457 }
458
459 /* Sort relocs into address order. */
460
461 static int
462 compare_relocs (ap, bp)
463 const PTR ap;
464 const PTR bp;
465 {
466 const arelent *a = *(const arelent **)ap;
467 const arelent *b = *(const arelent **)bp;
468
469 if (a->address > b->address)
470 return 1;
471 else if (a->address < b->address)
472 return -1;
473
474 /* So that associated relocations tied to the same address show up
475 in the correct order, we don't do any further sorting. */
476 if (a > b)
477 return 1;
478 else if (a < b)
479 return -1;
480 else
481 return 0;
482 }
483
484 /* Print VMA to STREAM with no leading zeroes. */
485
486 static void
487 objdump_print_value (vma, info)
488 bfd_vma vma;
489 struct disassemble_info *info;
490 {
491 char buf[30];
492 char *p;
493
494 sprintf_vma (buf, vma);
495 for (p = buf; *p == '0'; ++p)
496 ;
497 (*info->fprintf_func) (info->stream, "%s", p);
498 }
499
500 /* Print VMA symbolically to INFO if possible. */
501
502 static void
503 objdump_print_address (vma, info)
504 bfd_vma vma;
505 struct disassemble_info *info;
506 {
507 char buf[30];
508
509 /* @@ Would it speed things up to cache the last two symbols returned,
510 and maybe their address ranges? For many processors, only one memory
511 operand can be present at a time, so the 2-entry cache wouldn't be
512 constantly churned by code doing heavy memory accesses. */
513
514 /* Indices in `sorted_syms'. */
515 long min = 0;
516 long max = sorted_symcount;
517 long thisplace;
518
519 sprintf_vma (buf, vma);
520 (*info->fprintf_func) (info->stream, "%s", buf);
521
522 if (sorted_symcount < 1)
523 return;
524
525 /* Perform a binary search looking for the closest symbol to the
526 required value. We are searching the range (min, max]. */
527 while (min + 1 < max)
528 {
529 asymbol *sym;
530
531 thisplace = (max + min) / 2;
532 sym = sorted_syms[thisplace];
533
534 if (bfd_asymbol_value (sym) > vma)
535 max = thisplace;
536 else if (bfd_asymbol_value (sym) < vma)
537 min = thisplace;
538 else
539 {
540 min = thisplace;
541 break;
542 }
543 }
544
545 /* The symbol we want is now in min, the low end of the range we
546 were searching. If there are several symbols with the same
547 value, we want the first one. */
548 thisplace = min;
549 while (thisplace > 0
550 && (bfd_asymbol_value (sorted_syms[thisplace])
551 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
552 --thisplace;
553
554 {
555 /* If the file is relocateable, and the symbol could be from this
556 section, prefer a symbol from this section over symbols from
557 others, even if the other symbol's value might be closer.
558
559 Note that this may be wrong for some symbol references if the
560 sections have overlapping memory ranges, but in that case there's
561 no way to tell what's desired without looking at the relocation
562 table. */
563 struct objdump_disasm_info *aux;
564 long i;
565
566 aux = (struct objdump_disasm_info *) info->application_data;
567 if (sorted_syms[thisplace]->section != aux->sec
568 && (aux->require_sec
569 || ((aux->abfd->flags & HAS_RELOC) != 0
570 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
571 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
572 + bfd_section_size (aux->abfd, aux->sec)))))
573 {
574 for (i = thisplace + 1; i < sorted_symcount; i++)
575 {
576 if (bfd_asymbol_value (sorted_syms[i])
577 != bfd_asymbol_value (sorted_syms[thisplace]))
578 break;
579 }
580 --i;
581 for (; i >= 0; i--)
582 {
583 if (sorted_syms[i]->section == aux->sec
584 && (i == 0
585 || sorted_syms[i - 1]->section != aux->sec
586 || (bfd_asymbol_value (sorted_syms[i])
587 != bfd_asymbol_value (sorted_syms[i - 1]))))
588 {
589 thisplace = i;
590 break;
591 }
592 }
593
594 if (sorted_syms[thisplace]->section != aux->sec)
595 {
596 /* We didn't find a good symbol with a smaller value.
597 Look for one with a larger value. */
598 for (i = thisplace + 1; i < sorted_symcount; i++)
599 {
600 if (sorted_syms[i]->section == aux->sec)
601 {
602 thisplace = i;
603 break;
604 }
605 }
606 }
607
608 if (sorted_syms[thisplace]->section != aux->sec
609 && (aux->require_sec
610 || ((aux->abfd->flags & HAS_RELOC) != 0
611 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
612 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
613 + bfd_section_size (aux->abfd, aux->sec)))))
614 {
615 bfd_vma secaddr;
616
617 (*info->fprintf_func) (info->stream, " <%s",
618 bfd_get_section_name (aux->abfd, aux->sec));
619 secaddr = bfd_get_section_vma (aux->abfd, aux->sec);
620 if (vma < secaddr)
621 {
622 (*info->fprintf_func) (info->stream, "-");
623 objdump_print_value (secaddr - vma, info);
624 }
625 else if (vma > secaddr)
626 {
627 (*info->fprintf_func) (info->stream, "+");
628 objdump_print_value (vma - secaddr, info);
629 }
630 (*info->fprintf_func) (info->stream, ">");
631 return;
632 }
633 }
634 }
635
636 (*info->fprintf_func) (info->stream, " <%s", sorted_syms[thisplace]->name);
637 if (bfd_asymbol_value (sorted_syms[thisplace]) > vma)
638 {
639 (*info->fprintf_func) (info->stream, "-");
640 objdump_print_value (bfd_asymbol_value (sorted_syms[thisplace]) - vma,
641 info);
642 }
643 else if (vma > bfd_asymbol_value (sorted_syms[thisplace]))
644 {
645 (*info->fprintf_func) (info->stream, "+");
646 objdump_print_value (vma - bfd_asymbol_value (sorted_syms[thisplace]),
647 info);
648 }
649 (*info->fprintf_func) (info->stream, ">");
650 }
651
652 /* Hold the last function name and the last line number we displayed
653 in a disassembly. */
654
655 static char *prev_functionname;
656 static unsigned int prev_line;
657
658 /* We keep a list of all files that we have seen when doing a
659 dissassembly with source, so that we know how much of the file to
660 display. This can be important for inlined functions. */
661
662 struct print_file_list
663 {
664 struct print_file_list *next;
665 char *filename;
666 unsigned int line;
667 FILE *f;
668 };
669
670 static struct print_file_list *print_files;
671
672 /* The number of preceding context lines to show when we start
673 displaying a file for the first time. */
674
675 #define SHOW_PRECEDING_CONTEXT_LINES (5)
676
677 /* Skip ahead to a given line in a file, optionally printing each
678 line. */
679
680 static void
681 skip_to_line PARAMS ((struct print_file_list *, unsigned int, boolean));
682
683 static void
684 skip_to_line (p, line, show)
685 struct print_file_list *p;
686 unsigned int line;
687 boolean show;
688 {
689 while (p->line < line)
690 {
691 char buf[100];
692
693 if (fgets (buf, sizeof buf, p->f) == NULL)
694 {
695 fclose (p->f);
696 p->f = NULL;
697 break;
698 }
699
700 if (show)
701 printf ("%s", buf);
702
703 if (strchr (buf, '\n') != NULL)
704 ++p->line;
705 }
706 }
707
708 /* Show the line number, or the source line, in a dissassembly
709 listing. */
710
711 static void
712 show_line (abfd, section, off)
713 bfd *abfd;
714 asection *section;
715 bfd_vma off;
716 {
717 CONST char *filename;
718 CONST char *functionname;
719 unsigned int line;
720
721 if (! with_line_numbers && ! with_source_code)
722 return;
723
724 if (! bfd_find_nearest_line (abfd, section, syms, off, &filename,
725 &functionname, &line))
726 return;
727
728 if (filename != NULL && *filename == '\0')
729 filename = NULL;
730 if (functionname != NULL && *functionname == '\0')
731 functionname = NULL;
732
733 if (with_line_numbers)
734 {
735 if (functionname != NULL
736 && (prev_functionname == NULL
737 || strcmp (functionname, prev_functionname) != 0))
738 printf ("%s():\n", functionname);
739 if (line > 0 && line != prev_line)
740 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
741 }
742
743 if (with_source_code
744 && filename != NULL
745 && line > 0)
746 {
747 struct print_file_list **pp, *p;
748
749 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
750 if (strcmp ((*pp)->filename, filename) == 0)
751 break;
752 p = *pp;
753
754 if (p != NULL)
755 {
756 if (p != print_files)
757 {
758 int l;
759
760 /* We have reencountered a file name which we saw
761 earlier. This implies that either we are dumping out
762 code from an included file, or the same file was
763 linked in more than once. There are two common cases
764 of an included file: inline functions in a header
765 file, and a bison or flex skeleton file. In the
766 former case we want to just start printing (but we
767 back up a few lines to give context); in the latter
768 case we want to continue from where we left off. I
769 can't think of a good way to distinguish the cases,
770 so I used a heuristic based on the file name. */
771 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
772 l = p->line;
773 else
774 {
775 l = line - SHOW_PRECEDING_CONTEXT_LINES;
776 if (l <= 0)
777 l = 1;
778 }
779
780 if (p->f == NULL)
781 {
782 p->f = fopen (p->filename, "r");
783 p->line = 0;
784 }
785 if (p->f != NULL)
786 skip_to_line (p, l, false);
787
788 if (print_files->f != NULL)
789 {
790 fclose (print_files->f);
791 print_files->f = NULL;
792 }
793 }
794
795 if (p->f != NULL)
796 {
797 skip_to_line (p, line, true);
798 *pp = p->next;
799 p->next = print_files;
800 print_files = p;
801 }
802 }
803 else
804 {
805 FILE *f;
806
807 f = fopen (filename, "r");
808 if (f != NULL)
809 {
810 int l;
811
812 p = ((struct print_file_list *)
813 xmalloc (sizeof (struct print_file_list)));
814 p->filename = xmalloc (strlen (filename) + 1);
815 strcpy (p->filename, filename);
816 p->line = 0;
817 p->f = f;
818
819 if (print_files != NULL && print_files->f != NULL)
820 {
821 fclose (print_files->f);
822 print_files->f = NULL;
823 }
824 p->next = print_files;
825 print_files = p;
826
827 l = line - SHOW_PRECEDING_CONTEXT_LINES;
828 if (l <= 0)
829 l = 1;
830 skip_to_line (p, l, false);
831 if (p->f != NULL)
832 skip_to_line (p, line, true);
833 }
834 }
835 }
836
837 if (functionname != NULL
838 && (prev_functionname == NULL
839 || strcmp (functionname, prev_functionname) != 0))
840 {
841 if (prev_functionname != NULL)
842 free (prev_functionname);
843 prev_functionname = xmalloc (strlen (functionname) + 1);
844 strcpy (prev_functionname, functionname);
845 }
846
847 if (line > 0 && line != prev_line)
848 prev_line = line;
849 }
850
851 /* Pseudo FILE object for strings. */
852 typedef struct {
853 char *buffer;
854 char *current;
855 } SFILE;
856
857 /* sprintf to a "stream" */
858
859 #ifdef ANSI_PROTOTYPES
860 static int
861 objdump_sprintf (SFILE *f, const char *format, ...)
862 {
863 int n;
864 va_list args;
865
866 va_start (args, format);
867 vsprintf (f->current, format, args);
868 f->current += n = strlen (f->current);
869 va_end (args);
870 return n;
871 }
872 #else
873 static int
874 objdump_sprintf (va_alist)
875 va_dcl
876 {
877 int n;
878 SFILE *f;
879 const char *format;
880 va_list args;
881
882 va_start (args);
883 f = va_arg (args, SFILE *);
884 format = va_arg (args, const char *);
885 vsprintf (f->current, format, args);
886 f->current += n = strlen (f->current);
887 va_end (args);
888 return n;
889 }
890 #endif
891
892 void
893 disassemble_data (abfd)
894 bfd *abfd;
895 {
896 long i;
897 disassembler_ftype disassemble_fn = 0; /* New style */
898 struct disassemble_info disasm_info;
899 struct objdump_disasm_info aux;
900 asection *section;
901 boolean done_dot = false;
902 char buf[200];
903 SFILE sfile;
904
905 print_files = NULL;
906 prev_functionname = NULL;
907 prev_line = -1;
908
909 /* We make a copy of syms to sort. We don't want to sort syms
910 because that will screw up the relocs. */
911 sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
912 memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
913
914 sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
915
916 /* Sort the symbols into section and symbol order */
917 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
918
919 INIT_DISASSEMBLE_INFO(disasm_info, stdout, fprintf);
920 disasm_info.application_data = (PTR) &aux;
921 aux.abfd = abfd;
922 disasm_info.print_address_func = objdump_print_address;
923
924 if (machine != (char *) NULL)
925 {
926 const bfd_arch_info_type *info = bfd_scan_arch (machine);
927 if (info == NULL)
928 {
929 fprintf (stderr, "%s: Can't use supplied machine %s\n",
930 program_name,
931 machine);
932 exit (1);
933 }
934 abfd->arch_info = info;
935 }
936
937 disassemble_fn = disassembler (abfd);
938 if (!disassemble_fn)
939 {
940 fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
941 program_name,
942 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
943 exit (1);
944 }
945
946 disasm_info.arch = bfd_get_arch (abfd);
947 disasm_info.mach = bfd_get_mach (abfd);
948 if (bfd_big_endian (abfd))
949 disasm_info.endian = BFD_ENDIAN_BIG;
950 else if (bfd_little_endian (abfd))
951 disasm_info.endian = BFD_ENDIAN_LITTLE;
952 else
953 /* ??? Aborting here seems too drastic. We could default to big or little
954 instead. */
955 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
956
957 for (section = abfd->sections;
958 section != (asection *) NULL;
959 section = section->next)
960 {
961 bfd_byte *data = NULL;
962 bfd_size_type datasize = 0;
963 arelent **relbuf = NULL;
964 arelent **relpp = NULL;
965 arelent **relppend = NULL;
966 long stop;
967
968 if ((section->flags & SEC_LOAD) == 0
969 || (! disassemble_all
970 && only == NULL
971 && (section->flags & SEC_CODE) == 0))
972 continue;
973 if (only != (char *) NULL && strcmp (only, section->name) != 0)
974 continue;
975
976 if (dump_reloc_info
977 && (section->flags & SEC_RELOC) != 0)
978 {
979 long relsize;
980
981 relsize = bfd_get_reloc_upper_bound (abfd, section);
982 if (relsize < 0)
983 bfd_fatal (bfd_get_filename (abfd));
984
985 if (relsize > 0)
986 {
987 long relcount;
988
989 relbuf = (arelent **) xmalloc (relsize);
990 relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
991 if (relcount < 0)
992 bfd_fatal (bfd_get_filename (abfd));
993
994 /* Sort the relocs by address. */
995 qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
996
997 relpp = relbuf;
998 relppend = relpp + relcount;
999 }
1000 }
1001
1002 printf ("Disassembly of section %s:\n", section->name);
1003
1004 datasize = bfd_get_section_size_before_reloc (section);
1005 if (datasize == 0)
1006 continue;
1007
1008 data = (bfd_byte *) xmalloc ((size_t) datasize);
1009
1010 bfd_get_section_contents (abfd, section, data, 0, datasize);
1011
1012 aux.sec = section;
1013 disasm_info.buffer = data;
1014 disasm_info.buffer_vma = section->vma;
1015 disasm_info.buffer_length = datasize;
1016 if (start_address == (bfd_vma) -1
1017 || start_address < disasm_info.buffer_vma)
1018 i = 0;
1019 else
1020 i = start_address - disasm_info.buffer_vma;
1021 if (stop_address == (bfd_vma) -1)
1022 stop = datasize;
1023 else
1024 {
1025 if (stop_address < disasm_info.buffer_vma)
1026 stop = 0;
1027 else
1028 stop = stop_address - disasm_info.buffer_vma;
1029 if (stop > disasm_info.buffer_length)
1030 stop = disasm_info.buffer_length;
1031 }
1032 while (i < stop)
1033 {
1034 int bytes;
1035 boolean need_nl = false;
1036
1037 if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 &&
1038 data[i + 3] == 0)
1039 {
1040 if (done_dot == false)
1041 {
1042 printf ("...\n");
1043 done_dot = true;
1044 }
1045 bytes = 4;
1046 }
1047 else
1048 {
1049 done_dot = false;
1050 if (with_line_numbers || with_source_code)
1051 show_line (abfd, section, i);
1052 aux.require_sec = true;
1053 objdump_print_address (section->vma + i, &disasm_info);
1054 aux.require_sec = false;
1055 putchar (' ');
1056
1057 sfile.buffer = sfile.current = buf;
1058 disasm_info.fprintf_func = (fprintf_ftype) objdump_sprintf;
1059 disasm_info.stream = (FILE *) &sfile;
1060 bytes = (*disassemble_fn) (section->vma + i, &disasm_info);
1061 disasm_info.fprintf_func = (fprintf_ftype) fprintf;
1062 disasm_info.stream = stdout;
1063 if (bytes < 0)
1064 break;
1065
1066 if (show_raw_insn)
1067 {
1068 long j;
1069 for (j = i; j < i + bytes; ++j)
1070 {
1071 printf ("%02x", (unsigned) data[j]);
1072 putchar (' ');
1073 }
1074 /* Separate raw data from instruction by extra space. */
1075 putchar (' ');
1076 }
1077
1078 printf ("%s", sfile.buffer);
1079
1080 if (!wide_output)
1081 putchar ('\n');
1082 else
1083 need_nl = true;
1084 }
1085
1086 if (dump_reloc_info
1087 && (section->flags & SEC_RELOC) != 0)
1088 {
1089 while (relpp < relppend
1090 && ((*relpp)->address >= (bfd_vma) i
1091 && (*relpp)->address < (bfd_vma) i + bytes))
1092 {
1093 arelent *q;
1094 const char *sym_name;
1095
1096 q = *relpp;
1097
1098 printf ("\t\tRELOC: ");
1099
1100 printf_vma (section->vma + q->address);
1101
1102 printf (" %s ", q->howto->name);
1103
1104 if (q->sym_ptr_ptr != NULL
1105 && *q->sym_ptr_ptr != NULL)
1106 {
1107 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1108 if (sym_name == NULL || *sym_name == '\0')
1109 {
1110 asection *sym_sec;
1111
1112 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1113 sym_name = bfd_get_section_name (abfd, sym_sec);
1114 if (sym_name == NULL || *sym_name == '\0')
1115 sym_name = "*unknown*";
1116 }
1117 }
1118 else
1119 sym_name = "*unknown*";
1120
1121 printf ("%s", sym_name);
1122
1123 if (q->addend)
1124 {
1125 printf ("+0x");
1126 printf_vma (q->addend);
1127 }
1128
1129 printf ("\n");
1130 need_nl = false;
1131 ++relpp;
1132 }
1133 }
1134
1135 if (need_nl)
1136 printf ("\n");
1137
1138 i += bytes;
1139 }
1140
1141 free (data);
1142 if (relbuf != NULL)
1143 free (relbuf);
1144 }
1145 free (sorted_syms);
1146 }
1147 \f
1148
1149 /* Define a table of stab values and print-strings. We wish the initializer
1150 could be a direct-mapped table, but instead we build one the first
1151 time we need it. */
1152
1153 void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
1154 char *strsect_name));
1155
1156 /* Dump the stabs sections from an object file that has a section that
1157 uses Sun stabs encoding. It has to use some hooks into BFD because
1158 string table sections are not normally visible to BFD callers. */
1159
1160 void
1161 dump_stabs (abfd)
1162 bfd *abfd;
1163 {
1164 dump_section_stabs (abfd, ".stab", ".stabstr");
1165 dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1166 dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1167 dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1168 }
1169
1170 static struct internal_nlist *stabs;
1171 static bfd_size_type stab_size;
1172
1173 static char *strtab;
1174 static bfd_size_type stabstr_size;
1175
1176 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1177 and string table section STRSECT_NAME into `strtab'.
1178 If the section exists and was read, allocate the space and return true.
1179 Otherwise return false. */
1180
1181 boolean
1182 read_section_stabs (abfd, stabsect_name, strsect_name)
1183 bfd *abfd;
1184 char *stabsect_name;
1185 char *strsect_name;
1186 {
1187 asection *stabsect, *stabstrsect;
1188
1189 stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1190 if (0 == stabsect)
1191 {
1192 printf ("No %s section present\n\n", stabsect_name);
1193 return false;
1194 }
1195
1196 stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1197 if (0 == stabstrsect)
1198 {
1199 fprintf (stderr, "%s: %s has no %s section\n", program_name,
1200 bfd_get_filename (abfd), strsect_name);
1201 return false;
1202 }
1203
1204 stab_size = bfd_section_size (abfd, stabsect);
1205 stabstr_size = bfd_section_size (abfd, stabstrsect);
1206
1207 stabs = (struct internal_nlist *) xmalloc (stab_size);
1208 strtab = (char *) xmalloc (stabstr_size);
1209
1210 if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1211 {
1212 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1213 program_name, stabsect_name, bfd_get_filename (abfd),
1214 bfd_errmsg (bfd_get_error ()));
1215 free (stabs);
1216 free (strtab);
1217 return false;
1218 }
1219
1220 if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1221 stabstr_size))
1222 {
1223 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1224 program_name, strsect_name, bfd_get_filename (abfd),
1225 bfd_errmsg (bfd_get_error ()));
1226 free (stabs);
1227 free (strtab);
1228 return false;
1229 }
1230
1231 return true;
1232 }
1233
1234 #define SWAP_SYMBOL(symp, abfd) \
1235 { \
1236 (symp)->n_strx = bfd_h_get_32(abfd, \
1237 (unsigned char *)&(symp)->n_strx); \
1238 (symp)->n_desc = bfd_h_get_16 (abfd, \
1239 (unsigned char *)&(symp)->n_desc); \
1240 (symp)->n_value = bfd_h_get_32 (abfd, \
1241 (unsigned char *)&(symp)->n_value); \
1242 }
1243
1244 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1245 using string table section STRSECT_NAME (in `strtab'). */
1246
1247 void
1248 print_section_stabs (abfd, stabsect_name, strsect_name)
1249 bfd *abfd;
1250 char *stabsect_name;
1251 char *strsect_name;
1252 {
1253 int i;
1254 unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1255 struct internal_nlist *stabp = stabs,
1256 *stabs_end = (struct internal_nlist *) (stab_size + (char *) stabs);
1257
1258 printf ("Contents of %s section:\n\n", stabsect_name);
1259 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1260
1261 /* Loop through all symbols and print them.
1262
1263 We start the index at -1 because there is a dummy symbol on
1264 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1265
1266 for (i = -1; stabp < stabs_end; stabp++, i++)
1267 {
1268 const char *name;
1269
1270 SWAP_SYMBOL (stabp, abfd);
1271 printf ("\n%-6d ", i);
1272 /* Either print the stab name, or, if unnamed, print its number
1273 again (makes consistent formatting for tools like awk). */
1274 name = bfd_get_stab_name (stabp->n_type);
1275 if (name != NULL)
1276 printf ("%-6s", name);
1277 else if (stabp->n_type == N_UNDF)
1278 printf ("HdrSym");
1279 else
1280 printf ("%-6d", stabp->n_type);
1281 printf (" %-6d %-6d ", stabp->n_other, stabp->n_desc);
1282 printf_vma (stabp->n_value);
1283 printf (" %-6lu", stabp->n_strx);
1284
1285 /* Symbols with type == 0 (N_UNDF) specify the length of the
1286 string table associated with this file. We use that info
1287 to know how to relocate the *next* file's string table indices. */
1288
1289 if (stabp->n_type == N_UNDF)
1290 {
1291 file_string_table_offset = next_file_string_table_offset;
1292 next_file_string_table_offset += stabp->n_value;
1293 }
1294 else
1295 {
1296 /* Using the (possibly updated) string table offset, print the
1297 string (if any) associated with this symbol. */
1298
1299 if ((stabp->n_strx + file_string_table_offset) < stabstr_size)
1300 printf (" %s", &strtab[stabp->n_strx + file_string_table_offset]);
1301 else
1302 printf (" *");
1303 }
1304 }
1305 printf ("\n\n");
1306 }
1307
1308 void
1309 dump_section_stabs (abfd, stabsect_name, strsect_name)
1310 bfd *abfd;
1311 char *stabsect_name;
1312 char *strsect_name;
1313 {
1314 asection *s;
1315
1316 /* Check for section names for which stabsect_name is a prefix, to
1317 handle .stab0, etc. */
1318 for (s = abfd->sections;
1319 s != NULL;
1320 s = s->next)
1321 {
1322 if (strncmp (stabsect_name, s->name, strlen (stabsect_name)) == 0
1323 && strncmp (strsect_name, s->name, strlen (strsect_name)) != 0)
1324 {
1325 if (read_section_stabs (abfd, s->name, strsect_name))
1326 {
1327 print_section_stabs (abfd, s->name, strsect_name);
1328 free (stabs);
1329 free (strtab);
1330 }
1331 }
1332 }
1333 }
1334 \f
1335 static void
1336 dump_bfd_header (abfd)
1337 bfd *abfd;
1338 {
1339 char *comma = "";
1340
1341 printf ("architecture: %s, ",
1342 bfd_printable_arch_mach (bfd_get_arch (abfd),
1343 bfd_get_mach (abfd)));
1344 printf ("flags 0x%08x:\n", abfd->flags);
1345
1346 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1347 PF (HAS_RELOC, "HAS_RELOC");
1348 PF (EXEC_P, "EXEC_P");
1349 PF (HAS_LINENO, "HAS_LINENO");
1350 PF (HAS_DEBUG, "HAS_DEBUG");
1351 PF (HAS_SYMS, "HAS_SYMS");
1352 PF (HAS_LOCALS, "HAS_LOCALS");
1353 PF (DYNAMIC, "DYNAMIC");
1354 PF (WP_TEXT, "WP_TEXT");
1355 PF (D_PAGED, "D_PAGED");
1356 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
1357 printf ("\nstart address 0x");
1358 printf_vma (abfd->start_address);
1359 printf ("\n");
1360 }
1361 \f
1362 static void
1363 dump_bfd_private_header (abfd)
1364 bfd *abfd;
1365 {
1366 bfd_print_private_bfd_data (abfd, stdout);
1367 }
1368 static void
1369 display_bfd (abfd)
1370 bfd *abfd;
1371 {
1372 char **matching;
1373
1374 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
1375 {
1376 bfd_nonfatal (bfd_get_filename (abfd));
1377 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1378 {
1379 list_matching_formats (matching);
1380 free (matching);
1381 }
1382 return;
1383 }
1384
1385 printf ("\n%s: file format %s\n", bfd_get_filename (abfd),
1386 abfd->xvec->name);
1387 if (dump_ar_hdrs)
1388 print_arelt_descr (stdout, abfd, true);
1389 if (dump_file_header)
1390 dump_bfd_header (abfd);
1391 if (dump_private_headers)
1392 dump_bfd_private_header (abfd);
1393 putchar ('\n');
1394 if (dump_section_headers)
1395 dump_headers (abfd);
1396 if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
1397 {
1398 syms = slurp_symtab (abfd);
1399 }
1400 if (dump_dynamic_symtab || dump_dynamic_reloc_info)
1401 {
1402 dynsyms = slurp_dynamic_symtab (abfd);
1403 }
1404 if (dump_symtab)
1405 dump_symbols (abfd, false);
1406 if (dump_dynamic_symtab)
1407 dump_symbols (abfd, true);
1408 if (dump_stab_section_info)
1409 dump_stabs (abfd);
1410 if (dump_reloc_info && ! disassemble)
1411 dump_relocs (abfd);
1412 if (dump_dynamic_reloc_info)
1413 dump_dynamic_relocs (abfd);
1414 if (dump_section_contents)
1415 dump_data (abfd);
1416 if (disassemble)
1417 disassemble_data (abfd);
1418 if (dump_debugging)
1419 {
1420 PTR dhandle;
1421
1422 dhandle = read_debugging_info (abfd, syms, symcount);
1423 if (dhandle != NULL)
1424 {
1425 if (! print_debugging_info (stdout, dhandle))
1426 fprintf (stderr, "%s: printing debugging information failed\n",
1427 bfd_get_filename (abfd));
1428 }
1429 }
1430 if (syms)
1431 {
1432 free (syms);
1433 syms = NULL;
1434 }
1435 if (dynsyms)
1436 {
1437 free (dynsyms);
1438 dynsyms = NULL;
1439 }
1440 }
1441
1442 static void
1443 display_file (filename, target)
1444 char *filename;
1445 char *target;
1446 {
1447 bfd *file, *arfile = (bfd *) NULL;
1448
1449 file = bfd_openr (filename, target);
1450 if (file == NULL)
1451 {
1452 bfd_nonfatal (filename);
1453 return;
1454 }
1455
1456 if (bfd_check_format (file, bfd_archive) == true)
1457 {
1458 bfd *last_arfile = NULL;
1459
1460 printf ("In archive %s:\n", bfd_get_filename (file));
1461 for (;;)
1462 {
1463 bfd_set_error (bfd_error_no_error);
1464
1465 arfile = bfd_openr_next_archived_file (file, arfile);
1466 if (arfile == NULL)
1467 {
1468 if (bfd_get_error () != bfd_error_no_more_archived_files)
1469 {
1470 bfd_nonfatal (bfd_get_filename (file));
1471 }
1472 break;
1473 }
1474
1475 display_bfd (arfile);
1476
1477 if (last_arfile != NULL)
1478 bfd_close (last_arfile);
1479 last_arfile = arfile;
1480 }
1481
1482 if (last_arfile != NULL)
1483 bfd_close (last_arfile);
1484 }
1485 else
1486 display_bfd (file);
1487
1488 bfd_close (file);
1489 }
1490 \f
1491 /* Actually display the various requested regions */
1492
1493 static void
1494 dump_data (abfd)
1495 bfd *abfd;
1496 {
1497 asection *section;
1498 bfd_byte *data = 0;
1499 bfd_size_type datasize = 0;
1500 bfd_size_type i;
1501 bfd_size_type start, stop;
1502
1503 for (section = abfd->sections; section != NULL; section =
1504 section->next)
1505 {
1506 int onaline = 16;
1507
1508 if (only == (char *) NULL ||
1509 strcmp (only, section->name) == 0)
1510 {
1511 if (section->flags & SEC_HAS_CONTENTS)
1512 {
1513 printf ("Contents of section %s:\n", section->name);
1514
1515 if (bfd_section_size (abfd, section) == 0)
1516 continue;
1517 data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
1518 datasize = bfd_section_size (abfd, section);
1519
1520
1521 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
1522
1523 if (start_address == (bfd_vma) -1
1524 || start_address < section->vma)
1525 start = 0;
1526 else
1527 start = start_address - section->vma;
1528 if (stop_address == (bfd_vma) -1)
1529 stop = bfd_section_size (abfd, section);
1530 else
1531 {
1532 if (stop_address < section->vma)
1533 stop = 0;
1534 else
1535 stop = stop_address - section->vma;
1536 if (stop > bfd_section_size (abfd, section))
1537 stop = bfd_section_size (abfd, section);
1538 }
1539 for (i = start; i < stop; i += onaline)
1540 {
1541 bfd_size_type j;
1542
1543 printf (" %04lx ", (unsigned long int) (i + section->vma));
1544 for (j = i; j < i + onaline; j++)
1545 {
1546 if (j < stop)
1547 printf ("%02x", (unsigned) (data[j]));
1548 else
1549 printf (" ");
1550 if ((j & 3) == 3)
1551 printf (" ");
1552 }
1553
1554 printf (" ");
1555 for (j = i; j < i + onaline; j++)
1556 {
1557 if (j >= stop)
1558 printf (" ");
1559 else
1560 printf ("%c", isprint (data[j]) ? data[j] : '.');
1561 }
1562 putchar ('\n');
1563 }
1564 free (data);
1565 }
1566 }
1567 }
1568 }
1569
1570 /* Should perhaps share code and display with nm? */
1571 static void
1572 dump_symbols (abfd, dynamic)
1573 bfd *abfd;
1574 boolean dynamic;
1575 {
1576 asymbol **current;
1577 long max;
1578 long count;
1579
1580 if (dynamic)
1581 {
1582 current = dynsyms;
1583 max = dynsymcount;
1584 if (max == 0)
1585 return;
1586 printf ("DYNAMIC SYMBOL TABLE:\n");
1587 }
1588 else
1589 {
1590 current = syms;
1591 max = symcount;
1592 if (max == 0)
1593 return;
1594 printf ("SYMBOL TABLE:\n");
1595 }
1596
1597 for (count = 0; count < max; count++)
1598 {
1599 if (*current)
1600 {
1601 bfd *cur_bfd = bfd_asymbol_bfd(*current);
1602 if (cur_bfd)
1603 {
1604 bfd_print_symbol (cur_bfd,
1605 stdout,
1606 *current, bfd_print_symbol_all);
1607 printf ("\n");
1608 }
1609 }
1610 current++;
1611 }
1612 printf ("\n");
1613 printf ("\n");
1614 }
1615
1616 static void
1617 dump_relocs (abfd)
1618 bfd *abfd;
1619 {
1620 arelent **relpp;
1621 long relcount;
1622 asection *a;
1623
1624 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
1625 {
1626 long relsize;
1627
1628 if (bfd_is_abs_section (a))
1629 continue;
1630 if (bfd_is_und_section (a))
1631 continue;
1632 if (bfd_is_com_section (a))
1633 continue;
1634
1635 if (only)
1636 {
1637 if (strcmp (only, a->name))
1638 continue;
1639 }
1640 else if ((a->flags & SEC_RELOC) == 0)
1641 continue;
1642
1643 relsize = bfd_get_reloc_upper_bound (abfd, a);
1644 if (relsize < 0)
1645 bfd_fatal (bfd_get_filename (abfd));
1646
1647 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
1648
1649 if (relsize == 0)
1650 {
1651 printf (" (none)\n\n");
1652 }
1653 else
1654 {
1655 relpp = (arelent **) xmalloc (relsize);
1656 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
1657 if (relcount < 0)
1658 bfd_fatal (bfd_get_filename (abfd));
1659 else if (relcount == 0)
1660 {
1661 printf (" (none)\n\n");
1662 }
1663 else
1664 {
1665 printf ("\n");
1666 dump_reloc_set (abfd, relpp, relcount);
1667 printf ("\n\n");
1668 }
1669 free (relpp);
1670 }
1671 }
1672 }
1673
1674 static void
1675 dump_dynamic_relocs (abfd)
1676 bfd *abfd;
1677 {
1678 long relsize;
1679 arelent **relpp;
1680 long relcount;
1681
1682 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1683 if (relsize < 0)
1684 bfd_fatal (bfd_get_filename (abfd));
1685
1686 printf ("DYNAMIC RELOCATION RECORDS");
1687
1688 if (relsize == 0)
1689 {
1690 printf (" (none)\n\n");
1691 }
1692 else
1693 {
1694 relpp = (arelent **) xmalloc (relsize);
1695 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
1696 if (relcount < 0)
1697 bfd_fatal (bfd_get_filename (abfd));
1698 else if (relcount == 0)
1699 {
1700 printf (" (none)\n\n");
1701 }
1702 else
1703 {
1704 printf ("\n");
1705 dump_reloc_set (abfd, relpp, relcount);
1706 printf ("\n\n");
1707 }
1708 free (relpp);
1709 }
1710 }
1711
1712 static void
1713 dump_reloc_set (abfd, relpp, relcount)
1714 bfd *abfd;
1715 arelent **relpp;
1716 long relcount;
1717 {
1718 arelent **p;
1719
1720 /* Get column headers lined up reasonably. */
1721 {
1722 static int width;
1723 if (width == 0)
1724 {
1725 char buf[30];
1726 sprintf_vma (buf, (bfd_vma) -1);
1727 width = strlen (buf) - 7;
1728 }
1729 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
1730 }
1731
1732 for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
1733 {
1734 arelent *q = *p;
1735 CONST char *sym_name;
1736 CONST char *section_name;
1737
1738 if (start_address != (bfd_vma) -1
1739 && q->address < start_address)
1740 continue;
1741 if (stop_address != (bfd_vma) -1
1742 && q->address > stop_address)
1743 continue;
1744
1745 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
1746 {
1747 sym_name = (*(q->sym_ptr_ptr))->name;
1748 section_name = (*(q->sym_ptr_ptr))->section->name;
1749 }
1750 else
1751 {
1752 sym_name = NULL;
1753 section_name = NULL;
1754 }
1755 if (sym_name)
1756 {
1757 printf_vma (q->address);
1758 printf (" %-16s %s",
1759 q->howto->name,
1760 sym_name);
1761 }
1762 else
1763 {
1764 if (section_name == (CONST char *) NULL)
1765 section_name = "*unknown*";
1766 printf_vma (q->address);
1767 printf (" %-16s [%s]",
1768 q->howto->name,
1769 section_name);
1770 }
1771 if (q->addend)
1772 {
1773 printf ("+0x");
1774 printf_vma (q->addend);
1775 }
1776 printf ("\n");
1777 }
1778 }
1779 \f
1780 /* The length of the longest architecture name + 1. */
1781 #define LONGEST_ARCH sizeof("rs6000:6000")
1782
1783 #ifndef L_tmpnam
1784 #define L_tmpnam 25
1785 #endif
1786
1787 static const char *
1788 endian_string (endian)
1789 enum bfd_endian endian;
1790 {
1791 if (endian == BFD_ENDIAN_BIG)
1792 return "big endian";
1793 else if (endian == BFD_ENDIAN_LITTLE)
1794 return "little endian";
1795 else
1796 return "endianness unknown";
1797 }
1798
1799 /* List the targets that BFD is configured to support, each followed
1800 by its endianness and the architectures it supports. */
1801
1802 static void
1803 display_target_list ()
1804 {
1805 extern char *tmpnam ();
1806 extern bfd_target *bfd_target_vector[];
1807 char tmparg[L_tmpnam];
1808 char *dummy_name;
1809 int t;
1810
1811 dummy_name = tmpnam (tmparg);
1812 for (t = 0; bfd_target_vector[t]; t++)
1813 {
1814 bfd_target *p = bfd_target_vector[t];
1815 bfd *abfd = bfd_openw (dummy_name, p->name);
1816 int a;
1817
1818 printf ("%s\n (header %s, data %s)\n", p->name,
1819 endian_string (p->header_byteorder),
1820 endian_string (p->byteorder));
1821
1822 if (abfd == NULL)
1823 {
1824 bfd_nonfatal (dummy_name);
1825 continue;
1826 }
1827
1828 if (! bfd_set_format (abfd, bfd_object))
1829 {
1830 if (bfd_get_error () != bfd_error_invalid_operation)
1831 bfd_nonfatal (p->name);
1832 continue;
1833 }
1834
1835 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1836 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
1837 printf (" %s\n",
1838 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
1839 }
1840 unlink (dummy_name);
1841 }
1842
1843 /* Print a table showing which architectures are supported for entries
1844 FIRST through LAST-1 of bfd_target_vector (targets across,
1845 architectures down). */
1846
1847 static void
1848 display_info_table (first, last)
1849 int first;
1850 int last;
1851 {
1852 extern bfd_target *bfd_target_vector[];
1853 extern char *tmpnam ();
1854 char tmparg[L_tmpnam];
1855 int t, a;
1856 char *dummy_name;
1857
1858 /* Print heading of target names. */
1859 printf ("\n%*s", (int) LONGEST_ARCH, " ");
1860 for (t = first; t < last && bfd_target_vector[t]; t++)
1861 printf ("%s ", bfd_target_vector[t]->name);
1862 putchar ('\n');
1863
1864 dummy_name = tmpnam (tmparg);
1865 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1866 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
1867 {
1868 printf ("%*s ", (int) LONGEST_ARCH - 1,
1869 bfd_printable_arch_mach (a, 0));
1870 for (t = first; t < last && bfd_target_vector[t]; t++)
1871 {
1872 bfd_target *p = bfd_target_vector[t];
1873 boolean ok = true;
1874 bfd *abfd = bfd_openw (dummy_name, p->name);
1875
1876 if (abfd == NULL)
1877 {
1878 bfd_nonfatal (p->name);
1879 ok = false;
1880 }
1881
1882 if (ok)
1883 {
1884 if (! bfd_set_format (abfd, bfd_object))
1885 {
1886 if (bfd_get_error () != bfd_error_invalid_operation)
1887 bfd_nonfatal (p->name);
1888 ok = false;
1889 }
1890 }
1891
1892 if (ok)
1893 {
1894 if (! bfd_set_arch_mach (abfd, a, 0))
1895 ok = false;
1896 }
1897
1898 if (ok)
1899 printf ("%s ", p->name);
1900 else
1901 {
1902 int l = strlen (p->name);
1903 while (l--)
1904 putchar ('-');
1905 putchar (' ');
1906 }
1907 }
1908 putchar ('\n');
1909 }
1910 unlink (dummy_name);
1911 }
1912
1913 /* Print tables of all the target-architecture combinations that
1914 BFD has been configured to support. */
1915
1916 static void
1917 display_target_tables ()
1918 {
1919 int t, columns;
1920 extern bfd_target *bfd_target_vector[];
1921 char *colum;
1922 extern char *getenv ();
1923
1924 columns = 0;
1925 colum = getenv ("COLUMNS");
1926 if (colum != NULL)
1927 columns = atoi (colum);
1928 if (columns == 0)
1929 columns = 80;
1930
1931 t = 0;
1932 while (bfd_target_vector[t] != NULL)
1933 {
1934 int oldt = t, wid;
1935
1936 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
1937 ++t;
1938 while (wid < columns && bfd_target_vector[t] != NULL)
1939 {
1940 int newwid;
1941
1942 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
1943 if (newwid >= columns)
1944 break;
1945 wid = newwid;
1946 ++t;
1947 }
1948 display_info_table (oldt, t);
1949 }
1950 }
1951
1952 static void
1953 display_info ()
1954 {
1955 printf ("BFD header file version %s\n", BFD_VERSION);
1956 display_target_list ();
1957 display_target_tables ();
1958 }
1959
1960 int
1961 main (argc, argv)
1962 int argc;
1963 char **argv;
1964 {
1965 int c;
1966 char *target = default_target;
1967 boolean seenflag = false;
1968
1969 program_name = *argv;
1970 xmalloc_set_program_name (program_name);
1971
1972 START_PROGRESS (program_name, 0);
1973
1974 bfd_init ();
1975
1976 while ((c = getopt_long (argc, argv, "pib:m:VdDlfahrRtTxsSj:w", long_options,
1977 (int *) 0))
1978 != EOF)
1979 {
1980 if (c != 'l' && c != OPTION_START_ADDRESS && c != OPTION_STOP_ADDRESS)
1981 seenflag = true;
1982 switch (c)
1983 {
1984 case 0:
1985 break; /* we've been given a long option */
1986 case 'm':
1987 machine = optarg;
1988 break;
1989 case 'j':
1990 only = optarg;
1991 break;
1992 case 'l':
1993 with_line_numbers = 1;
1994 break;
1995 case 'b':
1996 target = optarg;
1997 break;
1998 case 'f':
1999 dump_file_header = true;
2000 break;
2001 case 'i':
2002 formats_info = true;
2003 break;
2004 case 'p':
2005 dump_private_headers = 1;
2006 break;
2007 case 'x':
2008 dump_private_headers = 1;
2009 dump_symtab = 1;
2010 dump_reloc_info = 1;
2011 dump_file_header = true;
2012 dump_ar_hdrs = 1;
2013 dump_section_headers = 1;
2014 break;
2015 case 't':
2016 dump_symtab = 1;
2017 break;
2018 case 'T':
2019 dump_dynamic_symtab = 1;
2020 break;
2021 case 'd':
2022 disassemble = true;
2023 break;
2024 case 'D':
2025 disassemble = disassemble_all = true;
2026 break;
2027 case 'S':
2028 disassemble = true;
2029 with_source_code = true;
2030 break;
2031 case 's':
2032 dump_section_contents = 1;
2033 break;
2034 case 'r':
2035 dump_reloc_info = 1;
2036 break;
2037 case 'R':
2038 dump_dynamic_reloc_info = 1;
2039 break;
2040 case 'a':
2041 dump_ar_hdrs = 1;
2042 break;
2043 case 'h':
2044 dump_section_headers = 1;
2045 break;
2046 case 'H':
2047 usage (stdout, 0);
2048 case 'V':
2049 show_version = 1;
2050 break;
2051 case 'w':
2052 wide_output = 1;
2053 break;
2054 case OPTION_START_ADDRESS:
2055 start_address = parse_vma (optarg, "--start-address");
2056 break;
2057 case OPTION_STOP_ADDRESS:
2058 stop_address = parse_vma (optarg, "--stop-address");
2059 break;
2060 default:
2061 usage (stderr, 1);
2062 }
2063 }
2064
2065 if (show_version)
2066 {
2067 printf ("GNU %s version %s\n", program_name, program_version);
2068 exit (0);
2069 }
2070
2071 if (seenflag == false)
2072 usage (stderr, 1);
2073
2074 if (formats_info)
2075 {
2076 display_info ();
2077 }
2078 else
2079 {
2080 if (optind == argc)
2081 display_file ("a.out", target);
2082 else
2083 for (; optind < argc;)
2084 display_file (argv[optind++], target);
2085 }
2086
2087 END_PROGRESS (program_name);
2088
2089 return 0;
2090 }