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