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