* gas/lib/gas-defs.exp (gas_finish): Call "close" and "wait"
[deliverable/binutils-gdb.git] / binutils / objdump.c
CommitLineData
d20f480f 1/* objdump.c -- dump information about an object file.
37853673 2 Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
2fa0b342 3
b3a2b497 4This file is part of GNU Binutils.
2fa0b342 5
b3a2b497 6This program is free software; you can redistribute it and/or modify
2fa0b342 7it under the terms of the GNU General Public License as published by
d20f480f 8the Free Software Foundation; either version 2, or (at your option)
2fa0b342
DHW
9any later version.
10
b3a2b497 11This program is distributed in the hope that it will be useful,
2fa0b342
DHW
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
b3a2b497
ILT
17along with this program; if not, write to the Free Software
18Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
2fa0b342 19
2fa0b342 20#include "bfd.h"
d20f480f 21#include "sysdep.h"
2fa0b342 22#include "getopt.h"
e1ec9f07 23#include "bucomm.h"
2fa0b342
DHW
24#include <stdio.h>
25#include <ctype.h>
2e8adbd7 26#include "dis-asm.h"
105da05c 27#include "libiberty.h"
2fa0b342 28
73b8f102
JG
29/* Internal headers for the ELF .stab-dump code - sorry. */
30#define BYTES_IN_WORD 32
31#include "aout/aout64.h"
bf661056 32
80d19ec1 33#ifndef FPRINTF_ALREADY_DECLARED
6f575704 34extern int fprintf PARAMS ((FILE *, CONST char *, ...));
80d19ec1 35#endif
2fa0b342
DHW
36
37char *default_target = NULL; /* default at runtime */
38
e1ec9f07 39extern char *program_version;
2fa0b342 40
249c6fc0 41int show_version = 0; /* show the version number */
2fa0b342
DHW
42int dump_section_contents; /* -s */
43int dump_section_headers; /* -h */
44boolean dump_file_header; /* -f */
45int dump_symtab; /* -t */
de3b08ac 46int dump_dynamic_symtab; /* -T */
2fa0b342 47int dump_reloc_info; /* -r */
de3b08ac 48int dump_dynamic_reloc_info; /* -R */
2fa0b342 49int dump_ar_hdrs; /* -a */
aa0a709a 50int with_line_numbers; /* -l */
9b018ecd 51int dump_stab_section_info; /* --stabs */
aa0a709a 52boolean disassemble; /* -d */
e1ec9f07 53boolean formats_info; /* -i */
195d1adf
KR
54char *only; /* -j secname */
55
cef35d48 56/* Extra info to pass to the disassembler address printing function. */
195d1adf
KR
57struct objdump_disasm_info {
58 bfd *abfd;
59 asection *sec;
60};
2fa0b342 61
cef35d48 62/* Architecture to disassemble for, or default if NULL. */
aa0a709a 63char *machine = (char *) NULL;
f7b839f7
DM
64
65/* The symbol table. */
aa0a709a 66asymbol **syms;
2fa0b342 67
f7b839f7 68/* Number of symbols in `syms'. */
ae5d2ff5 69long symcount = 0;
2fa0b342 70
de3b08ac
ILT
71/* The dynamic symbol table. */
72asymbol **dynsyms;
73
74/* Number of symbols in `dynsyms'. */
75long dynsymcount = 0;
76
d9971b83
KR
77/* Forward declarations. */
78
79static void
80display_file PARAMS ((char *filename, char *target));
81
82static void
83dump_data PARAMS ((bfd *abfd));
84
85static void
86dump_relocs PARAMS ((bfd *abfd));
87
88static void
de3b08ac
ILT
89dump_dynamic_relocs PARAMS ((bfd * abfd));
90
91static void
92dump_reloc_set PARAMS ((bfd *, arelent **, long));
93
94static void
95dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
02a68547
ILT
96
97static void
98display_bfd PARAMS ((bfd *abfd));
8f197c94
ILT
99
100static void
101objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *));
d9971b83 102\f
2fa0b342 103void
b3a2b497
ILT
104usage (stream, status)
105 FILE *stream;
106 int status;
2fa0b342 107{
b3a2b497 108 fprintf (stream, "\
de3b08ac 109Usage: %s [-ahifdrRtTxsl] [-b bfdname] [-m machine] [-j section-name]\n\
02a68547
ILT
110 [--archive-headers] [--target=bfdname] [--disassemble] [--file-headers]\n\
111 [--section-headers] [--headers] [--info] [--section=section-name]\n\
112 [--line-numbers] [--architecture=machine] [--reloc] [--full-contents]\n\
de3b08ac
ILT
113 [--stabs] [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
114 [--version] [--help] objfile...\n\
02a68547 115at least one option besides -l (--line-numbers) must be given\n",
b3a2b497
ILT
116 program_name);
117 exit (status);
2fa0b342
DHW
118}
119
aa0a709a
SC
120static struct option long_options[]=
121{
02a68547
ILT
122 {"all-headers", no_argument, NULL, 'x'},
123 {"architecture", required_argument, NULL, 'm'},
124 {"archive-headers", no_argument, NULL, 'a'},
125 {"disassemble", no_argument, NULL, 'd'},
de3b08ac
ILT
126 {"dynamic-reloc", no_argument, NULL, 'R'},
127 {"dynamic-syms", no_argument, NULL, 'T'},
02a68547
ILT
128 {"file-headers", no_argument, NULL, 'f'},
129 {"full-contents", no_argument, NULL, 's'},
130 {"headers", no_argument, NULL, 'h'},
131 {"help", no_argument, NULL, 'H'},
132 {"info", no_argument, NULL, 'i'},
133 {"line-numbers", no_argument, NULL, 'l'},
134 {"reloc", no_argument, NULL, 'r'},
135 {"section", required_argument, NULL, 'j'},
136 {"section-headers", no_argument, NULL, 'h'},
73b8f102 137 {"stabs", no_argument, &dump_stab_section_info, 1},
02a68547
ILT
138 {"syms", no_argument, NULL, 't'},
139 {"target", required_argument, NULL, 'b'},
140 {"version", no_argument, &show_version, 1},
d2442698
DM
141 {0, no_argument, 0, 0}
142};
f7b839f7 143\f
2fa0b342 144static void
f7b839f7 145dump_section_header (abfd, section, ignored)
aa0a709a 146 bfd *abfd;
f7b839f7
DM
147 asection *section;
148 PTR ignored;
2fa0b342 149{
f7b839f7 150 char *comma = "";
aa0a709a 151
2fa0b342 152#define PF(x,y) \
f7b839f7
DM
153 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
154
155
156 printf ("SECTION %d [%s]\t: size %08x",
157 section->index,
158 section->name,
159 (unsigned) bfd_get_section_size_before_reloc (section));
160 printf (" vma ");
161 printf_vma (section->vma);
162 printf (" align 2**%u\n ",
163 section->alignment_power);
164 PF (SEC_ALLOC, "ALLOC");
165 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
166 PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
167 PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
168 PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
169 PF (SEC_LOAD, "LOAD");
170 PF (SEC_RELOC, "RELOC");
195d1adf 171#ifdef SEC_BALIGN
f7b839f7 172 PF (SEC_BALIGN, "BALIGN");
195d1adf 173#endif
f7b839f7
DM
174 PF (SEC_READONLY, "READONLY");
175 PF (SEC_CODE, "CODE");
176 PF (SEC_DATA, "DATA");
177 PF (SEC_ROM, "ROM");
178 PF (SEC_DEBUGGING, "DEBUGGING");
179 printf ("\n");
2fa0b342 180#undef PF
2fa0b342
DHW
181}
182
f7b839f7
DM
183static void
184dump_headers (abfd)
185 bfd *abfd;
186{
187 bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
188}
189\f
2fa0b342 190static asymbol **
abdcac0f
DM
191slurp_symtab (abfd)
192 bfd *abfd;
2fa0b342 193{
aa0a709a 194 asymbol **sy = (asymbol **) NULL;
ae5d2ff5 195 long storage;
2fa0b342 196
aa0a709a
SC
197 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
198 {
f7b839f7
DM
199 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
200 return NULL;
aa0a709a
SC
201 }
202
ae5d2ff5
ILT
203 storage = bfd_get_symtab_upper_bound (abfd);
204 if (storage < 0)
205 bfd_fatal (bfd_get_filename (abfd));
206
aa0a709a
SC
207 if (storage)
208 {
02a68547 209 sy = (asymbol **) xmalloc (storage);
aa0a709a
SC
210 }
211 symcount = bfd_canonicalize_symtab (abfd, sy);
ae5d2ff5
ILT
212 if (symcount < 0)
213 bfd_fatal (bfd_get_filename (abfd));
214 if (symcount == 0)
de3b08ac
ILT
215 fprintf (stderr, "%s: %s: No symbols\n",
216 program_name, bfd_get_filename (abfd));
217 return sy;
218}
219
220/* Read in the dynamic symbols. */
221
222static asymbol **
223slurp_dynamic_symtab (abfd)
224 bfd *abfd;
225{
226 asymbol **sy = (asymbol **) NULL;
227 long storage;
228
229 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
f50af42b 230 {
de3b08ac 231 fprintf (stderr, "%s: %s: not a dynamic object\n",
f50af42b 232 program_name, bfd_get_filename (abfd));
de3b08ac 233 return NULL;
f50af42b 234 }
de3b08ac
ILT
235
236 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
237 if (storage < 0)
238 bfd_fatal (bfd_get_filename (abfd));
239
240 if (storage)
241 {
242 sy = (asymbol **) xmalloc (storage);
243 }
244 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
245 if (dynsymcount < 0)
246 bfd_fatal (bfd_get_filename (abfd));
247 if (dynsymcount == 0)
248 fprintf (stderr, "%s: %s: No dynamic symbols\n",
249 program_name, bfd_get_filename (abfd));
aa0a709a 250 return sy;
2fa0b342 251}
aa0a709a 252
f7b839f7
DM
253/* Filter out (in place) symbols that are useless for disassembly.
254 COUNT is the number of elements in SYMBOLS.
255 Return the number of useful symbols. */
3ae36cb6 256
ae5d2ff5 257long
f7b839f7
DM
258remove_useless_symbols (symbols, count)
259 asymbol **symbols;
ae5d2ff5 260 long count;
3ae36cb6 261{
f7b839f7 262 register asymbol **in_ptr = symbols, **out_ptr = symbols;
3ae36cb6 263
f7b839f7 264 while (--count >= 0)
3ae36cb6
PB
265 {
266 asymbol *sym = *in_ptr++;
267
268 if (sym->name == NULL || sym->name[0] == '\0')
269 continue;
270 if (sym->flags & (BSF_DEBUGGING))
271 continue;
272 if (sym->section == &bfd_und_section
273 || bfd_is_com_section (sym->section))
274 continue;
275
276 *out_ptr++ = sym;
277 }
f7b839f7 278 return out_ptr - symbols;
3ae36cb6
PB
279}
280
37853673
SS
281/* Sort symbols into value order. */
282
aa0a709a 283static int
37853673 284compare_symbols (ap, bp)
770cde30
JG
285 PTR ap;
286 PTR bp;
2fa0b342 287{
770cde30
JG
288 asymbol *a = *(asymbol **)ap;
289 asymbol *b = *(asymbol **)bp;
2fa0b342 290
3ae36cb6
PB
291 if (a->value > b->value)
292 return 1;
293 else if (a->value < b->value)
294 return -1;
2fa0b342 295
3ae36cb6
PB
296 if (a->section > b->section)
297 return 1;
298 else if (a->section < b->section)
299 return -1;
300 return 0;
2fa0b342
DHW
301}
302
f7b839f7
DM
303/* Print VMA symbolically to INFO if possible. */
304
8f197c94 305static void
545a2768 306objdump_print_address (vma, info)
aa0a709a 307 bfd_vma vma;
545a2768 308 struct disassemble_info *info;
2fa0b342 309{
195d1adf
KR
310 /* @@ For relocateable files, should filter out symbols belonging to
311 the wrong section. Unfortunately, not enough information is supplied
312 to this routine to determine the correct section in all cases. */
313 /* @@ Would it speed things up to cache the last two symbols returned,
314 and maybe their address ranges? For many processors, only one memory
315 operand can be present at a time, so the 2-entry cache wouldn't be
316 constantly churned by code doing heavy memory accesses. */
2fa0b342 317
f7b839f7 318 /* Indices in `syms'. */
ae5d2ff5
ILT
319 long min = 0;
320 long max = symcount;
321 long thisplace;
2fa0b342 322
8f197c94 323 bfd_signed_vma vardiff;
2fa0b342 324
3ae36cb6
PB
325 fprintf_vma (info->stream, vma);
326
f7b839f7
DM
327 if (symcount < 1)
328 return;
329
8f197c94
ILT
330 /* Perform a binary search looking for the closest symbol to the
331 required value. We are searching the range (min, max]. */
332 while (min + 1 < max)
aa0a709a 333 {
f7b839f7 334 asymbol *sym;
8f197c94 335
f7b839f7 336 thisplace = (max + min) / 2;
f7b839f7 337 sym = syms[thisplace];
8f197c94 338
f7b839f7 339 vardiff = sym->value - vma;
f7b839f7
DM
340
341 if (vardiff > 0)
342 max = thisplace;
343 else if (vardiff < 0)
344 min = thisplace;
345 else
aa0a709a 346 {
8f197c94
ILT
347 min = thisplace;
348 break;
aa0a709a 349 }
f7b839f7 350 }
fc5d6074 351
8f197c94
ILT
352 /* The symbol we want is now in min, the low end of the range we
353 were searching. */
354 thisplace = min;
355
f7b839f7
DM
356 {
357 /* If this symbol isn't global, search for one with the same value
358 that is. */
359 bfd_vma val = syms[thisplace]->value;
ae5d2ff5 360 long i;
f7b839f7
DM
361 if (syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING))
362 for (i = thisplace - 1; i >= 0; i--)
aa0a709a 363 {
f7b839f7
DM
364 if (syms[i]->value == val
365 && (!(syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING))
366 || ((syms[thisplace]->flags & BSF_DEBUGGING)
367 && !(syms[i]->flags & BSF_DEBUGGING))))
aa0a709a 368 {
f7b839f7
DM
369 thisplace = i;
370 break;
aa0a709a
SC
371 }
372 }
f7b839f7
DM
373 if (syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING))
374 for (i = thisplace + 1; i < symcount; i++)
375 {
376 if (syms[i]->value == val
377 && (!(syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING))
378 || ((syms[thisplace]->flags & BSF_DEBUGGING)
379 && !(syms[i]->flags & BSF_DEBUGGING))))
195d1adf 380 {
f7b839f7
DM
381 thisplace = i;
382 break;
195d1adf 383 }
f7b839f7
DM
384 }
385 }
386 {
387 /* If the file is relocateable, and the symbol could be from this
388 section, prefer a symbol from this section over symbols from
389 others, even if the other symbol's value might be closer.
390
391 Note that this may be wrong for some symbol references if the
392 sections have overlapping memory ranges, but in that case there's
393 no way to tell what's desired without looking at the relocation
394 table. */
395 struct objdump_disasm_info *aux;
ae5d2ff5 396 long i;
f7b839f7
DM
397
398 aux = (struct objdump_disasm_info *) info->application_data;
399 if ((aux->abfd->flags & HAS_RELOC)
400 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
401 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
402 + bfd_get_section_size_before_reloc (aux->sec))
403 && syms[thisplace]->section != aux->sec)
404 {
405 for (i = thisplace + 1; i < symcount; i++)
8f197c94
ILT
406 {
407 if (syms[i]->value != syms[thisplace]->value)
408 {
409 i--;
410 break;
411 }
412 }
413 for (; i >= 0; i--)
414 {
415 if (syms[i]->section == aux->sec)
416 {
417 thisplace = i;
418 break;
419 }
420 }
195d1adf 421 }
f7b839f7
DM
422 }
423 fprintf (info->stream, " <%s", syms[thisplace]->name);
424 if (syms[thisplace]->value > vma)
425 {
426 char buf[30], *p = buf;
427 sprintf_vma (buf, syms[thisplace]->value - vma);
428 while (*p == '0')
429 p++;
430 fprintf (info->stream, "-%s", p);
431 }
432 else if (vma > syms[thisplace]->value)
433 {
434 char buf[30], *p = buf;
435 sprintf_vma (buf, vma - syms[thisplace]->value);
436 while (*p == '0')
437 p++;
438 fprintf (info->stream, "+%s", p);
2fa0b342 439 }
f7b839f7 440 fprintf (info->stream, ">");
2fa0b342
DHW
441}
442
443void
aa0a709a
SC
444disassemble_data (abfd)
445 bfd *abfd;
2fa0b342 446{
ae5d2ff5 447 long i;
cef35d48 448 unsigned int (*print) () = 0; /* Old style */
2e8adbd7 449 disassembler_ftype disassemble = 0; /* New style */
2e8adbd7 450 struct disassemble_info disasm_info;
195d1adf 451 struct objdump_disasm_info aux;
2e8adbd7
PB
452
453 int prevline;
454 CONST char *prev_function = "";
d20f480f 455
2fa0b342 456 asection *section;
aa0a709a 457
96d7950b 458 boolean done_dot = false;
aa0a709a 459
cef35d48 460 /* Replace symbol section relative values with abs values. */
aa0a709a
SC
461 for (i = 0; i < symcount; i++)
462 {
2fa0b342 463 syms[i]->value += syms[i]->section->vma;
aa0a709a 464 }
2fa0b342 465
3ae36cb6 466 symcount = remove_useless_symbols (syms, symcount);
2fa0b342
DHW
467
468 /* Sort the symbols into section and symbol order */
f7b839f7 469 qsort (syms, symcount, sizeof (asymbol *), compare_symbols);
2fa0b342 470
cef35d48
DM
471 INIT_DISASSEMBLE_INFO(disasm_info, stdout);
472 disasm_info.application_data = (PTR) &aux;
473 aux.abfd = abfd;
474 disasm_info.print_address_func = objdump_print_address;
475
aa0a709a
SC
476 if (machine != (char *) NULL)
477 {
cef35d48
DM
478 bfd_arch_info_type *info = bfd_scan_arch (machine);
479 if (info == NULL)
aa0a709a
SC
480 {
481 fprintf (stderr, "%s: Can't use supplied machine %s\n",
482 program_name,
483 machine);
484 exit (1);
485 }
486 abfd->arch_info = info;
2fa0b342 487 }
e779a58c 488
cef35d48 489 /* See if we can disassemble using bfd. */
e779a58c 490
aa0a709a
SC
491 if (abfd->arch_info->disassemble)
492 {
493 print = abfd->arch_info->disassemble;
e779a58c 494 }
aa0a709a
SC
495 else
496 {
105da05c
ILT
497 disassemble = disassembler (abfd);
498 if (!disassemble)
aa0a709a 499 {
aa0a709a
SC
500 fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
501 program_name,
105da05c 502 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
aa0a709a
SC
503 exit (1);
504 }
aa0a709a 505 }
2fa0b342
DHW
506
507 for (section = abfd->sections;
aa0a709a
SC
508 section != (asection *) NULL;
509 section = section->next)
65cceb78 510 {
cef35d48
DM
511 bfd_byte *data = NULL;
512 bfd_size_type datasize = 0;
2fa0b342 513
cef35d48
DM
514 if (!(section->flags & SEC_LOAD))
515 continue;
516 if (only != (char *) NULL && strcmp (only, section->name) != 0)
517 continue;
2fa0b342 518
cef35d48 519 printf ("Disassembly of section %s:\n", section->name);
2fa0b342 520
cef35d48
DM
521 datasize = bfd_get_section_size_before_reloc (section);
522 if (datasize == 0)
523 continue;
2fa0b342 524
cef35d48 525 data = (bfd_byte *) xmalloc ((size_t) datasize);
2fa0b342 526
cef35d48 527 bfd_get_section_contents (abfd, section, data, 0, datasize);
2fa0b342 528
cef35d48
DM
529 aux.sec = section;
530 disasm_info.buffer = data;
531 disasm_info.buffer_vma = section->vma;
532 disasm_info.buffer_length = datasize;
533 i = 0;
534 while (i < disasm_info.buffer_length)
535 {
536 if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 &&
537 data[i + 3] == 0)
aa0a709a 538 {
cef35d48 539 if (done_dot == false)
aa0a709a 540 {
cef35d48
DM
541 printf ("...\n");
542 done_dot = true;
65cceb78 543 }
cef35d48
DM
544 i += 4;
545 }
546 else
547 {
548 done_dot = false;
549 if (with_line_numbers)
aa0a709a 550 {
cef35d48
DM
551 CONST char *filename;
552 CONST char *functionname;
553 unsigned int line;
554
555 if (bfd_find_nearest_line (abfd,
556 section,
557 syms,
558 section->vma + i,
559 &filename,
560 &functionname,
561 &line))
aa0a709a 562 {
cef35d48
DM
563 if (functionname && *functionname
564 && strcmp(functionname, prev_function))
565 {
566 printf ("%s():\n", functionname);
567 prev_function = functionname;
568 }
569 if (!filename)
570 filename = "???";
571 if (line && line != prevline)
aa0a709a 572 {
cef35d48
DM
573 printf ("%s:%u\n", filename, line);
574 prevline = line;
aa0a709a
SC
575 }
576 }
cef35d48
DM
577 }
578 objdump_print_address (section->vma + i, &disasm_info);
579 putchar (' ');
65cceb78 580
cef35d48
DM
581 if (disassemble) /* New style */
582 {
583 int bytes = (*disassemble)(section->vma + i,
584 &disasm_info);
585 if (bytes < 0)
586 break;
587 i += bytes;
aa0a709a 588 }
cef35d48
DM
589 else /* Old style */
590 i += print (section->vma + i,
591 data + i,
592 stdout);
593 putchar ('\n');
96d7950b 594 }
96d7950b 595 }
cef35d48 596 free (data);
2fa0b342 597 }
2fa0b342 598}
73b8f102 599\f
73b8f102
JG
600
601/* Define a table of stab values and print-strings. We wish the initializer
602 could be a direct-mapped table, but instead we build one the first
603 time we need it. */
604
fe2750e1 605char **stab_name;
73b8f102
JG
606
607struct stab_print {
608 int value;
fe2750e1 609 char *string;
73b8f102
JG
610};
611
612struct stab_print stab_print[] = {
613#define __define_stab(NAME, CODE, STRING) {CODE, STRING},
614#include "aout/stab.def"
615#undef __define_stab
02a68547 616 {0, ""}
73b8f102
JG
617};
618
250e36fe
DM
619void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
620 char *strsect_name));
249c6fc0 621
250e36fe 622/* Dump the stabs sections from an object file that has a section that
73b8f102
JG
623 uses Sun stabs encoding. It has to use some hooks into BFD because
624 string table sections are not normally visible to BFD callers. */
625
626void
9b018ecd 627dump_stabs (abfd)
73b8f102
JG
628 bfd *abfd;
629{
fe2750e1
SS
630 /* Allocate and initialize stab name array if first time. */
631 if (stab_name == NULL)
73b8f102 632 {
250e36fe
DM
633 int i;
634
fe2750e1
SS
635 stab_name = (char **) xmalloc (256 * sizeof(char *));
636 /* Clear the array. */
73b8f102 637 for (i = 0; i < 256; i++)
fe2750e1
SS
638 stab_name[i] = NULL;
639 /* Fill in the defined stabs. */
640 for (i = 0; *stab_print[i].string; i++)
641 stab_name[stab_print[i].value] = stab_print[i].string;
73b8f102
JG
642 }
643
250e36fe
DM
644 dump_section_stabs (abfd, ".stab", ".stabstr");
645 dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
646 dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
647 dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
249c6fc0
RS
648}
649
250e36fe
DM
650static struct internal_nlist *stabs;
651static bfd_size_type stab_size;
652
653static char *strtab;
654static bfd_size_type stabstr_size;
655
656/* Read ABFD's stabs section STABSECT_NAME into `stabs'
657 and string table section STRSECT_NAME into `strtab'.
658 If the section exists and was read, allocate the space and return true.
659 Otherwise return false. */
660
661boolean
662read_section_stabs (abfd, stabsect_name, strsect_name)
249c6fc0 663 bfd *abfd;
250e36fe
DM
664 char *stabsect_name;
665 char *strsect_name;
249c6fc0 666{
9b018ecd 667 asection *stabsect, *stabstrsect;
9b018ecd 668
d5671c53
ILT
669 stabsect = bfd_get_section_by_name (abfd, stabsect_name);
670 if (0 == stabsect)
73b8f102 671 {
250e36fe
DM
672 printf ("No %s section present\n\n", stabsect_name);
673 return false;
73b8f102
JG
674 }
675
d5671c53
ILT
676 stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
677 if (0 == stabstrsect)
73b8f102 678 {
eae82145 679 fprintf (stderr, "%s: %s has no %s section\n", program_name,
250e36fe
DM
680 bfd_get_filename (abfd), strsect_name);
681 return false;
73b8f102 682 }
9b018ecd 683
d5671c53
ILT
684 stab_size = bfd_section_size (abfd, stabsect);
685 stabstr_size = bfd_section_size (abfd, stabstrsect);
73b8f102 686
9b018ecd
ILT
687 stabs = (struct internal_nlist *) xmalloc (stab_size);
688 strtab = (char *) xmalloc (stabstr_size);
73b8f102 689
d5671c53 690 if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
9b018ecd 691 {
d5671c53
ILT
692 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
693 program_name, stabsect_name, bfd_get_filename (abfd),
694 bfd_errmsg (bfd_get_error ()));
695 free (stabs);
696 free (strtab);
697 return false;
73b8f102 698 }
2fa0b342 699
d5671c53
ILT
700 if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
701 stabstr_size))
9b018ecd 702 {
d5671c53
ILT
703 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
704 program_name, strsect_name, bfd_get_filename (abfd),
705 bfd_errmsg (bfd_get_error ()));
706 free (stabs);
707 free (strtab);
708 return false;
73b8f102 709 }
d5671c53 710
250e36fe
DM
711 return true;
712}
73b8f102
JG
713
714#define SWAP_SYMBOL(symp, abfd) \
250e36fe 715{ \
73b8f102 716 (symp)->n_strx = bfd_h_get_32(abfd, \
250e36fe 717 (unsigned char *)&(symp)->n_strx); \
73b8f102 718 (symp)->n_desc = bfd_h_get_16 (abfd, \
250e36fe 719 (unsigned char *)&(symp)->n_desc); \
73b8f102 720 (symp)->n_value = bfd_h_get_32 (abfd, \
250e36fe
DM
721 (unsigned char *)&(symp)->n_value); \
722}
73b8f102 723
250e36fe
DM
724/* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
725 using string table section STRSECT_NAME (in `strtab'). */
726
727void
728print_section_stabs (abfd, stabsect_name, strsect_name)
729 bfd *abfd;
730 char *stabsect_name;
731 char *strsect_name;
732{
733 int i;
734 unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
735 struct internal_nlist *stabp = stabs,
736 *stabs_end = (struct internal_nlist *) (stab_size + (char *) stabs);
73b8f102 737
250e36fe
DM
738 printf ("Contents of %s section:\n\n", stabsect_name);
739 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
249c6fc0
RS
740
741 /* Loop through all symbols and print them.
742
743 We start the index at -1 because there is a dummy symbol on
250e36fe 744 the front of stabs-in-{coff,elf} sections that supplies sizes. */
249c6fc0 745
250e36fe 746 for (i = -1; stabp < stabs_end; stabp++, i++)
73b8f102 747 {
250e36fe 748 SWAP_SYMBOL (stabp, abfd);
fe2750e1 749 printf ("\n%-6d ", i);
250e36fe 750 /* Either print the stab name, or, if unnamed, print its number
fe2750e1 751 again (makes consistent formatting for tools like awk). */
250e36fe
DM
752 if (stab_name[stabp->n_type])
753 printf ("%-6s", stab_name[stabp->n_type]);
105da05c
ILT
754 else if (stabp->n_type == N_UNDF)
755 printf ("HdrSym");
fe2750e1 756 else
105da05c 757 printf ("%-6d", stabp->n_type);
250e36fe
DM
758 printf (" %-6d %-6d ", stabp->n_other, stabp->n_desc);
759 printf_vma (stabp->n_value);
760 printf (" %-6lu", stabp->n_strx);
249c6fc0
RS
761
762 /* Symbols with type == 0 (N_UNDF) specify the length of the
763 string table associated with this file. We use that info
764 to know how to relocate the *next* file's string table indices. */
765
250e36fe 766 if (stabp->n_type == N_UNDF)
249c6fc0
RS
767 {
768 file_string_table_offset = next_file_string_table_offset;
250e36fe 769 next_file_string_table_offset += stabp->n_value;
249c6fc0 770 }
249c6fc0 771 else
e1ec9f07 772 {
250e36fe 773 /* Using the (possibly updated) string table offset, print the
e1ec9f07
SS
774 string (if any) associated with this symbol. */
775
250e36fe
DM
776 if ((stabp->n_strx + file_string_table_offset) < stabstr_size)
777 printf (" %s", &strtab[stabp->n_strx + file_string_table_offset]);
e1ec9f07
SS
778 else
779 printf (" *");
780 }
73b8f102
JG
781 }
782 printf ("\n\n");
783}
249c6fc0 784
250e36fe
DM
785void
786dump_section_stabs (abfd, stabsect_name, strsect_name)
787 bfd *abfd;
788 char *stabsect_name;
789 char *strsect_name;
790{
791 if (read_section_stabs (abfd, stabsect_name, strsect_name))
792 {
793 print_section_stabs (abfd, stabsect_name, strsect_name);
794 free (stabs);
795 free (strtab);
796 }
797}
798\f
eae82145 799static void
f7b839f7
DM
800dump_bfd_header (abfd)
801 bfd *abfd;
802{
803 char *comma = "";
804
805 printf ("architecture: %s, ",
806 bfd_printable_arch_mach (bfd_get_arch (abfd),
807 bfd_get_mach (abfd)));
808 printf ("flags 0x%08x:\n", abfd->flags);
809
810#define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
811 PF (HAS_RELOC, "HAS_RELOC");
812 PF (EXEC_P, "EXEC_P");
813 PF (HAS_LINENO, "HAS_LINENO");
814 PF (HAS_DEBUG, "HAS_DEBUG");
815 PF (HAS_SYMS, "HAS_SYMS");
816 PF (HAS_LOCALS, "HAS_LOCALS");
817 PF (DYNAMIC, "DYNAMIC");
818 PF (WP_TEXT, "WP_TEXT");
819 PF (D_PAGED, "D_PAGED");
820 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
821 printf ("\nstart address 0x");
822 printf_vma (abfd->start_address);
823}
824
02a68547 825static void
2fa0b342
DHW
826display_bfd (abfd)
827 bfd *abfd;
828{
209e5610
DM
829 char **matching;
830
831 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
aa0a709a 832 {
cef35d48 833 bfd_nonfatal (bfd_get_filename (abfd));
8f197c94 834 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
209e5610
DM
835 {
836 list_matching_formats (matching);
837 free (matching);
838 }
aa0a709a
SC
839 return;
840 }
f7b839f7 841
cef35d48
DM
842 printf ("\n%s: file format %s\n", bfd_get_filename (abfd),
843 abfd->xvec->name);
aa0a709a
SC
844 if (dump_ar_hdrs)
845 print_arelt_descr (stdout, abfd, true);
aa0a709a 846 if (dump_file_header)
f7b839f7
DM
847 dump_bfd_header (abfd);
848 putchar ('\n');
2fa0b342 849 if (dump_section_headers)
aa0a709a
SC
850 dump_headers (abfd);
851 if (dump_symtab || dump_reloc_info || disassemble)
852 {
853 syms = slurp_symtab (abfd);
854 }
de3b08ac
ILT
855 if (dump_dynamic_symtab || dump_dynamic_reloc_info)
856 {
857 dynsyms = slurp_dynamic_symtab (abfd);
858 }
aa0a709a 859 if (dump_symtab)
de3b08ac
ILT
860 dump_symbols (abfd, false);
861 if (dump_dynamic_symtab)
862 dump_symbols (abfd, true);
73b8f102 863 if (dump_stab_section_info)
9b018ecd 864 dump_stabs (abfd);
aa0a709a
SC
865 if (dump_reloc_info)
866 dump_relocs (abfd);
de3b08ac
ILT
867 if (dump_dynamic_reloc_info)
868 dump_dynamic_relocs (abfd);
aa0a709a
SC
869 if (dump_section_contents)
870 dump_data (abfd);
3ae36cb6
PB
871 /* Note that disassemble_data re-orders the syms table, but that is
872 safe - as long as it is done last! */
aa0a709a
SC
873 if (disassemble)
874 disassemble_data (abfd);
2fa0b342
DHW
875}
876
d9971b83 877static void
2fa0b342
DHW
878display_file (filename, target)
879 char *filename;
880 char *target;
881{
882 bfd *file, *arfile = (bfd *) NULL;
883
884 file = bfd_openr (filename, target);
aa0a709a
SC
885 if (file == NULL)
886 {
cef35d48 887 bfd_nonfatal (filename);
aa0a709a
SC
888 return;
889 }
2fa0b342 890
aa0a709a
SC
891 if (bfd_check_format (file, bfd_archive) == true)
892 {
8f197c94
ILT
893 bfd *last_arfile = NULL;
894
aa0a709a
SC
895 printf ("In archive %s:\n", bfd_get_filename (file));
896 for (;;)
897 {
8f197c94 898 bfd_set_error (bfd_error_no_error);
aa0a709a
SC
899
900 arfile = bfd_openr_next_archived_file (file, arfile);
901 if (arfile == NULL)
902 {
8f197c94 903 if (bfd_get_error () != bfd_error_no_more_archived_files)
d2442698 904 {
cef35d48 905 bfd_nonfatal (bfd_get_filename (file));
d2442698 906 }
8f197c94 907 break;
aa0a709a 908 }
2fa0b342 909
aa0a709a 910 display_bfd (arfile);
8f197c94
ILT
911
912 if (last_arfile != NULL)
913 bfd_close (last_arfile);
914 last_arfile = arfile;
aa0a709a 915 }
8f197c94
ILT
916
917 if (last_arfile != NULL)
918 bfd_close (last_arfile);
2fa0b342 919 }
2fa0b342 920 else
aa0a709a 921 display_bfd (file);
2fa0b342 922
aa0a709a 923 bfd_close (file);
2fa0b342
DHW
924}
925\f
926/* Actually display the various requested regions */
927
d9971b83 928static void
2fa0b342
DHW
929dump_data (abfd)
930 bfd *abfd;
931{
932 asection *section;
aa0a709a 933 bfd_byte *data = 0;
fc5d6074
SC
934 bfd_size_type datasize = 0;
935 bfd_size_type i;
2fa0b342
DHW
936
937 for (section = abfd->sections; section != NULL; section =
aa0a709a
SC
938 section->next)
939 {
940 int onaline = 16;
2fa0b342 941
aa0a709a
SC
942 if (only == (char *) NULL ||
943 strcmp (only, section->name) == 0)
60c80016 944 {
aa0a709a
SC
945 if (section->flags & SEC_HAS_CONTENTS)
946 {
947 printf ("Contents of section %s:\n", section->name);
948
9b018ecd 949 if (bfd_section_size (abfd, section) == 0)
aa0a709a 950 continue;
02a68547 951 data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
9b018ecd 952 datasize = bfd_section_size (abfd, section);
2fa0b342 953
2fa0b342 954
9b018ecd 955 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
2fa0b342 956
9b018ecd 957 for (i = 0; i < bfd_section_size (abfd, section); i += onaline)
aa0a709a
SC
958 {
959 bfd_size_type j;
960
961 printf (" %04lx ", (unsigned long int) (i + section->vma));
962 for (j = i; j < i + onaline; j++)
963 {
9b018ecd 964 if (j < bfd_section_size (abfd, section))
aa0a709a
SC
965 printf ("%02x", (unsigned) (data[j]));
966 else
967 printf (" ");
968 if ((j & 3) == 3)
969 printf (" ");
970 }
2fa0b342 971
aa0a709a
SC
972 printf (" ");
973 for (j = i; j < i + onaline; j++)
974 {
9b018ecd 975 if (j >= bfd_section_size (abfd, section))
aa0a709a
SC
976 printf (" ");
977 else
978 printf ("%c", isprint (data[j]) ? data[j] : '.');
979 }
980 putchar ('\n');
981 }
d9971b83 982 free (data);
60c80016 983 }
2fa0b342 984 }
2fa0b342 985 }
2fa0b342
DHW
986}
987
2fa0b342 988/* Should perhaps share code and display with nm? */
d9971b83 989static void
de3b08ac 990dump_symbols (abfd, dynamic)
2fa0b342 991 bfd *abfd;
de3b08ac 992 boolean dynamic;
2fa0b342 993{
de3b08ac
ILT
994 asymbol **current;
995 long max;
ae5d2ff5 996 long count;
2fa0b342 997
de3b08ac 998 if (dynamic)
aa0a709a 999 {
de3b08ac
ILT
1000 current = dynsyms;
1001 max = dynsymcount;
1002 if (max == 0)
1003 return;
1004 printf ("DYNAMIC SYMBOL TABLE:\n");
1005 }
1006 else
1007 {
1008 current = syms;
1009 max = symcount;
1010 if (max == 0)
1011 return;
1012 printf ("SYMBOL TABLE:\n");
1013 }
2fa0b342 1014
de3b08ac
ILT
1015 for (count = 0; count < max; count++)
1016 {
d9971b83 1017 if (*current)
aa0a709a 1018 {
d9971b83
KR
1019 bfd *cur_bfd = bfd_asymbol_bfd(*current);
1020 if (cur_bfd)
1021 {
1022 bfd_print_symbol (cur_bfd,
1023 stdout,
1024 *current, bfd_print_symbol_all);
1025 printf ("\n");
1026 }
aa0a709a
SC
1027 }
1028 current++;
2fa0b342 1029 }
aa0a709a
SC
1030 printf ("\n");
1031 printf ("\n");
2fa0b342
DHW
1032}
1033
d9971b83 1034static void
aa0a709a
SC
1035dump_relocs (abfd)
1036 bfd *abfd;
2fa0b342
DHW
1037{
1038 arelent **relpp;
ae5d2ff5 1039 long relcount;
2fa0b342 1040 asection *a;
aa0a709a
SC
1041
1042 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
1043 {
ae5d2ff5
ILT
1044 long relsize;
1045
aa0a709a
SC
1046 if (a == &bfd_abs_section)
1047 continue;
1048 if (a == &bfd_und_section)
1049 continue;
d9971b83 1050 if (bfd_is_com_section (a))
aa0a709a
SC
1051 continue;
1052
195d1adf
KR
1053 if (only)
1054 {
1055 if (strcmp (only, a->name))
1056 continue;
1057 }
1058 else if ((a->flags & SEC_RELOC) == 0)
1059 continue;
1060
aa0a709a
SC
1061 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
1062
ae5d2ff5
ILT
1063 relsize = bfd_get_reloc_upper_bound (abfd, a);
1064 if (relsize < 0)
1065 bfd_fatal (bfd_get_filename (abfd));
1066
1067 if (relsize == 0)
aa0a709a
SC
1068 {
1069 printf (" (none)\n\n");
d20f480f 1070 }
aa0a709a
SC
1071 else
1072 {
ae5d2ff5 1073 relpp = (arelent **) xmalloc (relsize);
3ae36cb6 1074 /* Note that this must be done *before* we sort the syms table. */
aa0a709a 1075 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
ae5d2ff5
ILT
1076 if (relcount < 0)
1077 bfd_fatal (bfd_get_filename (abfd));
1078 else if (relcount == 0)
aa0a709a
SC
1079 {
1080 printf (" (none)\n\n");
d20f480f 1081 }
aa0a709a
SC
1082 else
1083 {
1084 printf ("\n");
de3b08ac 1085 dump_reloc_set (abfd, relpp, relcount);
aa0a709a 1086 printf ("\n\n");
d20f480f 1087 }
de3b08ac 1088 free (relpp);
2fa0b342 1089 }
de3b08ac
ILT
1090 }
1091}
2fa0b342 1092
de3b08ac
ILT
1093static void
1094dump_dynamic_relocs (abfd)
1095 bfd *abfd;
1096{
1097 long relsize;
1098 arelent **relpp;
1099 long relcount;
1100
1101 printf ("DYNAMIC RELOCATION RECORDS");
1102
1103 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1104 if (relsize < 0)
1105 bfd_fatal (bfd_get_filename (abfd));
1106
1107 if (relsize == 0)
1108 {
1109 printf (" (none)\n\n");
1110 }
1111 else
1112 {
1113 relpp = (arelent **) xmalloc (relsize);
1114 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
1115 if (relcount < 0)
1116 bfd_fatal (bfd_get_filename (abfd));
1117 else if (relcount == 0)
1118 {
1119 printf (" (none)\n\n");
1120 }
1121 else
1122 {
1123 printf ("\n");
1124 dump_reloc_set (abfd, relpp, relcount);
1125 printf ("\n\n");
1126 }
1127 free (relpp);
1128 }
1129}
1130
1131static void
1132dump_reloc_set (abfd, relpp, relcount)
1133 bfd *abfd;
1134 arelent **relpp;
1135 long relcount;
1136{
1137 arelent **p;
1138
1139 /* Get column headers lined up reasonably. */
1140 {
1141 static int width;
1142 if (width == 0)
1143 {
1144 char buf[30];
1145 sprintf_vma (buf, (bfd_vma) -1);
1146 width = strlen (buf) - 7;
1147 }
1148 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
1149 }
1150
1151 for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
1152 {
1153 arelent *q = *p;
1154 CONST char *sym_name;
1155 CONST char *section_name;
1156
1157 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
1158 {
1159 sym_name = (*(q->sym_ptr_ptr))->name;
1160 section_name = (*(q->sym_ptr_ptr))->section->name;
1161 }
1162 else
1163 {
1164 sym_name = NULL;
1165 section_name = NULL;
1166 }
1167 if (sym_name)
1168 {
1169 printf_vma (q->address);
1170 printf (" %-16s %s",
1171 q->howto->name,
1172 sym_name);
1173 }
1174 else
1175 {
1176 if (section_name == (CONST char *) NULL)
1177 section_name = "*unknown*";
1178 printf_vma (q->address);
1179 printf (" %-16s [%s]",
1180 q->howto->name,
1181 section_name);
1182 }
1183 if (q->addend)
1184 {
1185 printf ("+0x");
1186 printf_vma (q->addend);
1187 }
1188 printf ("\n");
d20f480f 1189 }
2fa0b342 1190}
f7b839f7 1191\f
f7b839f7
DM
1192/* The length of the longest architecture name + 1. */
1193#define LONGEST_ARCH sizeof("rs6000:6000")
1194
1195/* List the targets that BFD is configured to support, each followed
1196 by its endianness and the architectures it supports. */
1197
1198static void
1199display_target_list ()
1200{
de04bceb 1201 extern char *tmpnam ();
f7b839f7 1202 extern bfd_target *bfd_target_vector[];
de04bceb 1203 char *dummy_name;
f7b839f7
DM
1204 int t;
1205
de04bceb 1206 dummy_name = tmpnam ((char *) NULL);
f7b839f7
DM
1207 for (t = 0; bfd_target_vector[t]; t++)
1208 {
f7b839f7 1209 bfd_target *p = bfd_target_vector[t];
de04bceb 1210 bfd *abfd = bfd_openw (dummy_name, p->name);
105da05c
ILT
1211 int a;
1212
1213 printf ("%s\n (header %s, data %s)\n", p->name,
1214 p->header_byteorder_big_p ? "big endian" : "little endian",
1215 p->byteorder_big_p ? "big endian" : "little endian");
f7b839f7 1216
334d6e76
SS
1217 if (abfd == NULL)
1218 {
de04bceb 1219 bfd_nonfatal (dummy_name);
105da05c 1220 continue;
334d6e76 1221 }
105da05c
ILT
1222
1223 if (! bfd_set_format (abfd, bfd_object))
1224 {
1225 if (bfd_get_error () != bfd_error_invalid_operation)
1226 bfd_nonfatal (p->name);
1227 continue;
1228 }
1229
f7b839f7
DM
1230 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1231 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
1232 printf (" %s\n",
1233 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
1234 }
de04bceb 1235 unlink (dummy_name);
f7b839f7
DM
1236}
1237
1238/* Print a table showing which architectures are supported for entries
1239 FIRST through LAST-1 of bfd_target_vector (targets across,
1240 architectures down). */
1241
9872a49c 1242static void
abdcac0f
DM
1243display_info_table (first, last)
1244 int first;
1245 int last;
9872a49c 1246{
abdcac0f 1247 extern bfd_target *bfd_target_vector[];
de04bceb
ILT
1248 extern char *tmpnam ();
1249 int t, a;
1250 char *dummy_name;
9872a49c 1251
f7b839f7 1252 /* Print heading of target names. */
ae5d2ff5 1253 printf ("\n%*s", (int) LONGEST_ARCH, " ");
105da05c 1254 for (t = first; t < last && bfd_target_vector[t]; t++)
f7b839f7
DM
1255 printf ("%s ", bfd_target_vector[t]->name);
1256 putchar ('\n');
9872a49c 1257
de04bceb 1258 dummy_name = tmpnam ((char *) NULL);
f7b839f7
DM
1259 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1260 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
e779a58c 1261 {
ae5d2ff5
ILT
1262 printf ("%*s ", (int) LONGEST_ARCH - 1,
1263 bfd_printable_arch_mach (a, 0));
105da05c 1264 for (t = first; t < last && bfd_target_vector[t]; t++)
aa0a709a 1265 {
f7b839f7 1266 bfd_target *p = bfd_target_vector[t];
105da05c 1267 boolean ok = true;
de04bceb 1268 bfd *abfd = bfd_openw (dummy_name, p->name);
aa0a709a 1269
334d6e76
SS
1270 if (abfd == NULL)
1271 {
105da05c
ILT
1272 bfd_nonfatal (p->name);
1273 ok = false;
1274 }
1275
1276 if (ok)
1277 {
1278 if (! bfd_set_format (abfd, bfd_object))
1279 {
1280 if (bfd_get_error () != bfd_error_invalid_operation)
1281 bfd_nonfatal (p->name);
1282 ok = false;
1283 }
334d6e76 1284 }
105da05c
ILT
1285
1286 if (ok)
1287 {
1288 if (! bfd_set_arch_mach (abfd, a, 0))
1289 ok = false;
1290 }
1291
1292 if (ok)
aa0a709a
SC
1293 printf ("%s ", p->name);
1294 else
e779a58c 1295 {
f7b839f7 1296 int l = strlen (p->name);
aa0a709a 1297 while (l--)
f7b839f7
DM
1298 putchar ('-');
1299 putchar (' ');
e779a58c 1300 }
e779a58c 1301 }
f7b839f7 1302 putchar ('\n');
e779a58c 1303 }
de04bceb 1304 unlink (dummy_name);
9872a49c 1305}
aa0a709a 1306
f7b839f7
DM
1307/* Print tables of all the target-architecture combinations that
1308 BFD has been configured to support. */
1309
aa0a709a 1310static void
f7b839f7 1311display_target_tables ()
aa0a709a 1312{
f7b839f7 1313 int t, columns;
abdcac0f 1314 extern bfd_target *bfd_target_vector[];
f7b839f7 1315 char *colum;
aa0a709a
SC
1316 extern char *getenv ();
1317
aa0a709a 1318 columns = 0;
f7b839f7
DM
1319 colum = getenv ("COLUMNS");
1320 if (colum != NULL)
aa0a709a 1321 columns = atoi (colum);
f7b839f7 1322 if (columns == 0)
aa0a709a 1323 columns = 80;
f7b839f7 1324
105da05c
ILT
1325 t = 0;
1326 while (bfd_target_vector[t] != NULL)
aa0a709a 1327 {
f7b839f7
DM
1328 int oldt = t, wid;
1329
105da05c
ILT
1330 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
1331 ++t;
1332 while (wid < columns && bfd_target_vector[t] != NULL)
1333 {
1334 int newwid;
1335
1336 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
1337 if (newwid >= columns)
1338 break;
1339 wid = newwid;
1340 ++t;
1341 }
f7b839f7 1342 display_info_table (oldt, t);
aa0a709a
SC
1343 }
1344}
1345
f7b839f7
DM
1346static void
1347display_info ()
1348{
1349 printf ("BFD header file version %s\n", BFD_VERSION);
1350 display_target_list ();
1351 display_target_tables ();
1352}
1353
2fa0b342
DHW
1354int
1355main (argc, argv)
1356 int argc;
1357 char **argv;
1358{
1359 int c;
2fa0b342
DHW
1360 char *target = default_target;
1361 boolean seenflag = false;
2fa0b342
DHW
1362
1363 program_name = *argv;
8f197c94
ILT
1364 xmalloc_set_program_name (program_name);
1365
1366 bfd_init ();
2fa0b342 1367
de3b08ac 1368 while ((c = getopt_long (argc, argv, "ib:m:VdlfahrRtTxsj:", long_options,
d2442698 1369 (int *) 0))
aa0a709a
SC
1370 != EOF)
1371 {
1372 seenflag = true;
1373 switch (c)
1374 {
b3a2b497
ILT
1375 case 0:
1376 break; /* we've been given a long option */
aa0a709a
SC
1377 case 'm':
1378 machine = optarg;
1379 break;
1380 case 'j':
1381 only = optarg;
1382 break;
1383 case 'l':
1384 with_line_numbers = 1;
1385 break;
1386 case 'b':
1387 target = optarg;
1388 break;
1389 case 'f':
1390 dump_file_header = true;
1391 break;
1392 case 'i':
e1ec9f07 1393 formats_info = true;
aa0a709a
SC
1394 break;
1395 case 'x':
1396 dump_symtab = 1;
1397 dump_reloc_info = 1;
1398 dump_file_header = true;
1399 dump_ar_hdrs = 1;
1400 dump_section_headers = 1;
1401 break;
aa0a709a
SC
1402 case 't':
1403 dump_symtab = 1;
1404 break;
de3b08ac
ILT
1405 case 'T':
1406 dump_dynamic_symtab = 1;
1407 break;
aa0a709a
SC
1408 case 'd':
1409 disassemble = true;
1410 break;
1411 case 's':
1412 dump_section_contents = 1;
1413 break;
1414 case 'r':
1415 dump_reloc_info = 1;
1416 break;
de3b08ac
ILT
1417 case 'R':
1418 dump_dynamic_reloc_info = 1;
1419 break;
aa0a709a
SC
1420 case 'a':
1421 dump_ar_hdrs = 1;
1422 break;
1423 case 'h':
1424 dump_section_headers = 1;
1425 break;
b3a2b497
ILT
1426 case 'H':
1427 usage (stdout, 0);
249c6fc0
RS
1428 case 'V':
1429 show_version = 1;
1430 break;
aa0a709a 1431 default:
b3a2b497 1432 usage (stderr, 1);
aa0a709a 1433 }
2fa0b342 1434 }
2fa0b342 1435
249c6fc0 1436 if (show_version)
b3a2b497
ILT
1437 {
1438 printf ("GNU %s version %s\n", program_name, program_version);
1439 exit (0);
1440 }
249c6fc0 1441
2fa0b342 1442 if (seenflag == false)
b3a2b497 1443 usage (stderr, 1);
2fa0b342 1444
e1ec9f07 1445 if (formats_info)
aa0a709a
SC
1446 {
1447 display_info ();
1448 }
1449 else
1450 {
1451 if (optind == argc)
1452 display_file ("a.out", target);
1453 else
1454 for (; optind < argc;)
1455 display_file (argv[optind++], target);
1456 }
2fa0b342
DHW
1457 return 0;
1458}
This page took 0.249466 seconds and 4 git commands to generate.