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