(O_SYS_CMDLINE): New pseudo opcode for command line processing.
[deliverable/binutils-gdb.git] / binutils / objdump.c
CommitLineData
252b5132 1/* objdump.c -- dump information about an object file.
8c2bc687 2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
f0c8c24a 3 2000, 2001, 2002, 2003
252b5132
RH
4 Free Software Foundation, Inc.
5
b5e2a4f3 6 This file is part of GNU Binutils.
252b5132 7
b5e2a4f3
NC
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
252b5132 12
b5e2a4f3
NC
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
252b5132 17
b5e2a4f3
NC
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
252b5132
RH
21
22#include "bfd.h"
6e1a7e9a 23#include "bfdver.h"
252b5132
RH
24#include "progress.h"
25#include "bucomm.h"
a6637ec0 26#include "budemang.h"
d7a283d4 27#include "getopt.h"
3882b010 28#include "safe-ctype.h"
252b5132
RH
29#include "dis-asm.h"
30#include "libiberty.h"
31#include "demangle.h"
32#include "debug.h"
33#include "budbg.h"
34
252b5132
RH
35/* Internal headers for the ELF .stab-dump code - sorry. */
36#define BYTES_IN_WORD 32
37#include "aout/aout64.h"
38
39#ifdef NEED_DECLARATION_FPRINTF
40/* This is needed by INIT_DISASSEMBLE_INFO. */
b34976b6
AM
41extern int fprintf
42 PARAMS ((FILE *, const char *, ...));
252b5132
RH
43#endif
44
75cd796a
ILT
45/* Exit status. */
46static int exit_status = 0;
47
252b5132
RH
48static char *default_target = NULL; /* default at runtime */
49
50static int show_version = 0; /* show the version number */
51static int dump_section_contents; /* -s */
52static int dump_section_headers; /* -h */
b34976b6 53static bfd_boolean dump_file_header; /* -f */
252b5132
RH
54static int dump_symtab; /* -t */
55static int dump_dynamic_symtab; /* -T */
56static int dump_reloc_info; /* -r */
57static int dump_dynamic_reloc_info; /* -R */
58static int dump_ar_hdrs; /* -a */
59static int dump_private_headers; /* -p */
60static int prefix_addresses; /* --prefix-addresses */
61static int with_line_numbers; /* -l */
b34976b6 62static bfd_boolean with_source_code; /* -S */
252b5132
RH
63static int show_raw_insn; /* --show-raw-insn */
64static int dump_stab_section_info; /* --stabs */
65static int do_demangle; /* -C, --demangle */
b34976b6
AM
66static bfd_boolean disassemble; /* -d */
67static bfd_boolean disassemble_all; /* -D */
252b5132 68static int disassemble_zeroes; /* --disassemble-zeroes */
b34976b6 69static bfd_boolean formats_info; /* -i */
252b5132
RH
70static char *only; /* -j secname */
71static int wide_output; /* -w */
72static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
73static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
74static int dump_debugging; /* --debugging */
75static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
f1563258 76static int file_start_context = 0; /* --file-start-context */
252b5132
RH
77
78/* Extra info to pass to the disassembler address printing function. */
026df7c5
NC
79struct objdump_disasm_info
80{
252b5132
RH
81 bfd *abfd;
82 asection *sec;
b34976b6 83 bfd_boolean require_sec;
252b5132
RH
84};
85
86/* Architecture to disassemble for, or default if NULL. */
87static char *machine = (char *) NULL;
88
dd92f639
NC
89/* Target specific options to the disassembler. */
90static char *disassembler_options = (char *) NULL;
91
252b5132
RH
92/* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
93static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
94
95/* The symbol table. */
96static asymbol **syms;
97
98/* Number of symbols in `syms'. */
99static long symcount = 0;
100
101/* The sorted symbol table. */
102static asymbol **sorted_syms;
103
104/* Number of symbols in `sorted_syms'. */
105static long sorted_symcount = 0;
106
107/* The dynamic symbol table. */
108static asymbol **dynsyms;
109
110/* Number of symbols in `dynsyms'. */
111static long dynsymcount = 0;
112
113/* Static declarations. */
114
b34976b6
AM
115static void usage
116 PARAMS ((FILE *, int));
117static void nonfatal
118 PARAMS ((const char *));
119static void display_file
120 PARAMS ((char *filename, char *target));
121static void dump_section_header
122 PARAMS ((bfd *, asection *, PTR));
123static void dump_headers
124 PARAMS ((bfd *));
125static void dump_data
126 PARAMS ((bfd *abfd));
127static void dump_relocs
128 PARAMS ((bfd *abfd));
129static void dump_dynamic_relocs
130 PARAMS ((bfd * abfd));
131static void dump_reloc_set
132 PARAMS ((bfd *, asection *, arelent **, long));
133static void dump_symbols
134 PARAMS ((bfd *abfd, bfd_boolean dynamic));
135static void dump_bfd_header
136 PARAMS ((bfd *));
137static void dump_bfd_private_header
138 PARAMS ((bfd *));
139static void dump_bfd
140 PARAMS ((bfd *));
141static void display_bfd
142 PARAMS ((bfd *abfd));
143static void display_target_list
144 PARAMS ((void));
145static void display_info_table
146 PARAMS ((int, int));
147static void display_target_tables
148 PARAMS ((void));
149static void display_info
150 PARAMS ((void));
2863d58a 151static void objdump_print_value
b34976b6 152 PARAMS ((bfd_vma, struct disassemble_info *, bfd_boolean));
2863d58a
AM
153static void objdump_print_symname
154 PARAMS ((bfd *, struct disassemble_info *, asymbol *));
155static asymbol *find_symbol_for_address
b34976b6 156 PARAMS ((bfd *, asection *, bfd_vma, bfd_boolean, long *));
2863d58a
AM
157static void objdump_print_addr_with_sym
158 PARAMS ((bfd *, asection *, asymbol *, bfd_vma,
b34976b6 159 struct disassemble_info *, bfd_boolean));
2863d58a 160static void objdump_print_addr
b34976b6 161 PARAMS ((bfd_vma, struct disassemble_info *, bfd_boolean));
2863d58a
AM
162static void objdump_print_address
163 PARAMS ((bfd_vma, struct disassemble_info *));
164static int objdump_symbol_at_address
165 PARAMS ((bfd_vma, struct disassemble_info *));
b34976b6
AM
166static void show_line
167 PARAMS ((bfd *, asection *, bfd_vma));
2863d58a 168static void disassemble_bytes
b34976b6 169 PARAMS ((struct disassemble_info *, disassembler_ftype, bfd_boolean,
2863d58a 170 bfd_byte *, bfd_vma, bfd_vma, arelent ***, arelent **));
b34976b6
AM
171static void disassemble_data
172 PARAMS ((bfd *));
173static const char *endian_string
174 PARAMS ((enum bfd_endian));
175static asymbol ** slurp_symtab
176 PARAMS ((bfd *));
177static asymbol ** slurp_dynamic_symtab
178 PARAMS ((bfd *));
179static long remove_useless_symbols
180 PARAMS ((asymbol **, long));
181static int compare_symbols
182 PARAMS ((const PTR, const PTR));
183static int compare_relocs
184 PARAMS ((const PTR, const PTR));
185static void dump_stabs
186 PARAMS ((bfd *));
187static bfd_boolean read_section_stabs
188 PARAMS ((bfd *, const char *, const char *));
189static void print_section_stabs
190 PARAMS ((bfd *, const char *, const char *));
252b5132
RH
191\f
192static void
193usage (stream, status)
194 FILE *stream;
195 int status;
196{
8b53311e
NC
197 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
198 fprintf (stream, _(" Display information from object <file(s)>.\n"));
199 fprintf (stream, _(" At least one of the following switches must be given:\n"));
252b5132 200 fprintf (stream, _("\
86d65c94
MK
201 -a, --archive-headers Display archive header information\n\
202 -f, --file-headers Display the contents of the overall file header\n\
203 -p, --private-headers Display object format specific file header contents\n\
204 -h, --[section-]headers Display the contents of the section headers\n\
205 -x, --all-headers Display the contents of all headers\n\
206 -d, --disassemble Display assembler contents of executable sections\n\
207 -D, --disassemble-all Display assembler contents of all sections\n\
208 -S, --source Intermix source code with disassembly\n\
209 -s, --full-contents Display the full contents of all sections requested\n\
210 -g, --debugging Display debug information in object file\n\
211 -G, --stabs Display (in raw form) any STABS info in the file\n\
212 -t, --syms Display the contents of the symbol table(s)\n\
213 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
214 -r, --reloc Display the relocation entries in the file\n\
215 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
8b53311e 216 -v, --version Display this program's version number\n\
86d65c94
MK
217 -i, --info List object formats and architectures supported\n\
218 -H, --help Display this information\n\
1dada9c5
NC
219"));
220 if (status != 2)
221 {
222 fprintf (stream, _("\n The following switches are optional:\n"));
223 fprintf (stream, _("\
86d65c94
MK
224 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
225 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
226 -j, --section=NAME Only display information for section NAME\n\
227 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
1dada9c5
NC
228 -EB --endian=big Assume big endian format when disassembling\n\
229 -EL --endian=little Assume little endian format when disassembling\n\
f1563258 230 --file-start-context Include context from start of file (with -S)\n\
86d65c94 231 -l, --line-numbers Include line numbers and filenames in output\n\
28c309a2 232 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
f0c8c24a
NC
233 The STYLE, if specified, can be `auto', `gnu',\n\
234 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
235 or `gnat'\n\
86d65c94
MK
236 -w, --wide Format output for more than 80 columns\n\
237 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
f0c8c24a
NC
238 --start-address=ADDR Only process data whose address is >= ADDR\n\
239 --stop-address=ADDR Only process data whose address is <= ADDR\n\
1dada9c5
NC
240 --prefix-addresses Print complete address alongside disassembly\n\
241 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
86d65c94 242 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
1dada9c5
NC
243\n"));
244 list_supported_targets (program_name, stream);
2f83960e 245 list_supported_architectures (program_name, stream);
86d65c94 246
94470b23 247 disassembler_usage (stream);
1dada9c5 248 }
252b5132 249 if (status == 0)
86d65c94 250 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
252b5132
RH
251 exit (status);
252}
253
254/* 150 isn't special; it's just an arbitrary non-ASCII char value. */
255
256#define OPTION_ENDIAN (150)
257#define OPTION_START_ADDRESS (OPTION_ENDIAN + 1)
258#define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
259#define OPTION_ADJUST_VMA (OPTION_STOP_ADDRESS + 1)
260
261static struct option long_options[]=
262{
263 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
264 {"all-headers", no_argument, NULL, 'x'},
265 {"private-headers", no_argument, NULL, 'p'},
266 {"architecture", required_argument, NULL, 'm'},
267 {"archive-headers", no_argument, NULL, 'a'},
1dada9c5 268 {"debugging", no_argument, NULL, 'g'},
28c309a2 269 {"demangle", optional_argument, NULL, 'C'},
252b5132
RH
270 {"disassemble", no_argument, NULL, 'd'},
271 {"disassemble-all", no_argument, NULL, 'D'},
dd92f639 272 {"disassembler-options", required_argument, NULL, 'M'},
1dada9c5 273 {"disassemble-zeroes", no_argument, NULL, 'z'},
252b5132
RH
274 {"dynamic-reloc", no_argument, NULL, 'R'},
275 {"dynamic-syms", no_argument, NULL, 'T'},
276 {"endian", required_argument, NULL, OPTION_ENDIAN},
277 {"file-headers", no_argument, NULL, 'f'},
f1563258 278 {"file-start-context", no_argument, &file_start_context, 1},
252b5132
RH
279 {"full-contents", no_argument, NULL, 's'},
280 {"headers", no_argument, NULL, 'h'},
281 {"help", no_argument, NULL, 'H'},
282 {"info", no_argument, NULL, 'i'},
283 {"line-numbers", no_argument, NULL, 'l'},
284 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
285 {"prefix-addresses", no_argument, &prefix_addresses, 1},
286 {"reloc", no_argument, NULL, 'r'},
287 {"section", required_argument, NULL, 'j'},
288 {"section-headers", no_argument, NULL, 'h'},
289 {"show-raw-insn", no_argument, &show_raw_insn, 1},
290 {"source", no_argument, NULL, 'S'},
1dada9c5 291 {"stabs", no_argument, NULL, 'G'},
252b5132
RH
292 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
293 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
294 {"syms", no_argument, NULL, 't'},
295 {"target", required_argument, NULL, 'b'},
1dada9c5
NC
296 {"version", no_argument, NULL, 'V'},
297 {"wide", no_argument, NULL, 'w'},
252b5132
RH
298 {0, no_argument, 0, 0}
299};
300\f
301static void
75cd796a
ILT
302nonfatal (msg)
303 const char *msg;
304{
305 bfd_nonfatal (msg);
306 exit_status = 1;
307}
308\f
309static void
252b5132 310dump_section_header (abfd, section, ignored)
b4c96d0d 311 bfd *abfd ATTRIBUTE_UNUSED;
252b5132 312 asection *section;
b4c96d0d 313 PTR ignored ATTRIBUTE_UNUSED;
252b5132
RH
314{
315 char *comma = "";
f6af82bd 316 unsigned int opb = bfd_octets_per_byte (abfd);
252b5132
RH
317
318 printf ("%3d %-13s %08lx ", section->index,
319 bfd_get_section_name (abfd, section),
940b2b78 320 (unsigned long) bfd_section_size (abfd, section) / opb);
d8180c76 321 bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
252b5132 322 printf (" ");
d8180c76 323 bfd_printf_vma (abfd, section->lma);
e59b4dfb 324 printf (" %08lx 2**%u", (unsigned long) section->filepos,
252b5132
RH
325 bfd_get_section_alignment (abfd, section));
326 if (! wide_output)
327 printf ("\n ");
328 printf (" ");
329
330#define PF(x, y) \
331 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
332
333 PF (SEC_HAS_CONTENTS, "CONTENTS");
334 PF (SEC_ALLOC, "ALLOC");
335 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
252b5132
RH
336 PF (SEC_LOAD, "LOAD");
337 PF (SEC_RELOC, "RELOC");
252b5132
RH
338 PF (SEC_READONLY, "READONLY");
339 PF (SEC_CODE, "CODE");
340 PF (SEC_DATA, "DATA");
341 PF (SEC_ROM, "ROM");
342 PF (SEC_DEBUGGING, "DEBUGGING");
343 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
344 PF (SEC_EXCLUDE, "EXCLUDE");
345 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
34cbe64e
TW
346 PF (SEC_BLOCK, "BLOCK");
347 PF (SEC_CLINK, "CLINK");
24c411ed
ILT
348 PF (SEC_SMALL_DATA, "SMALL_DATA");
349 PF (SEC_SHARED, "SHARED");
9e85c2a0 350 PF (SEC_ARCH_BIT_0, "ARCH_BIT_0");
13ae64f3 351 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
252b5132
RH
352
353 if ((section->flags & SEC_LINK_ONCE) != 0)
354 {
355 const char *ls;
356
357 switch (section->flags & SEC_LINK_DUPLICATES)
358 {
359 default:
360 abort ();
361 case SEC_LINK_DUPLICATES_DISCARD:
362 ls = "LINK_ONCE_DISCARD";
363 break;
364 case SEC_LINK_DUPLICATES_ONE_ONLY:
365 ls = "LINK_ONCE_ONE_ONLY";
366 break;
367 case SEC_LINK_DUPLICATES_SAME_SIZE:
368 ls = "LINK_ONCE_SAME_SIZE";
369 break;
370 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
371 ls = "LINK_ONCE_SAME_CONTENTS";
372 break;
373 }
374 printf ("%s%s", comma, ls);
deecf979
ILT
375
376 if (section->comdat != NULL)
377 printf (" (COMDAT %s %ld)", section->comdat->name,
378 section->comdat->symbol);
379
252b5132
RH
380 comma = ", ";
381 }
382
383 printf ("\n");
384#undef PF
385}
386
387static void
388dump_headers (abfd)
389 bfd *abfd;
390{
391 printf (_("Sections:\n"));
8bea4d5c 392
252b5132 393#ifndef BFD64
8bea4d5c 394 printf (_("Idx Name Size VMA LMA File off Algn"));
252b5132 395#else
21611032
TS
396 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
397 if (bfd_get_arch_size (abfd) == 32)
398 printf (_("Idx Name Size VMA LMA File off Algn"));
399 else
400 printf (_("Idx Name Size VMA LMA File off Algn"));
252b5132 401#endif
8bea4d5c
ILT
402
403 if (wide_output)
404 printf (_(" Flags"));
026df7c5
NC
405 if (abfd->flags & HAS_LOAD_PAGE)
406 printf (_(" Pg"));
8bea4d5c
ILT
407 printf ("\n");
408
252b5132
RH
409 bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
410}
411\f
412static asymbol **
413slurp_symtab (abfd)
414 bfd *abfd;
415{
416 asymbol **sy = (asymbol **) NULL;
417 long storage;
418
419 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
420 {
252b5132
RH
421 symcount = 0;
422 return NULL;
423 }
424
425 storage = bfd_get_symtab_upper_bound (abfd);
426 if (storage < 0)
427 bfd_fatal (bfd_get_filename (abfd));
252b5132 428 if (storage)
28b18af1
AM
429 sy = (asymbol **) xmalloc (storage);
430
252b5132
RH
431 symcount = bfd_canonicalize_symtab (abfd, sy);
432 if (symcount < 0)
433 bfd_fatal (bfd_get_filename (abfd));
252b5132
RH
434 return sy;
435}
436
437/* Read in the dynamic symbols. */
438
439static asymbol **
440slurp_dynamic_symtab (abfd)
441 bfd *abfd;
442{
443 asymbol **sy = (asymbol **) NULL;
444 long storage;
445
446 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
447 if (storage < 0)
448 {
449 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
450 {
37cc8ec1 451 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
252b5132
RH
452 dynsymcount = 0;
453 return NULL;
454 }
455
456 bfd_fatal (bfd_get_filename (abfd));
457 }
252b5132 458 if (storage)
28b18af1
AM
459 sy = (asymbol **) xmalloc (storage);
460
252b5132
RH
461 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
462 if (dynsymcount < 0)
463 bfd_fatal (bfd_get_filename (abfd));
252b5132
RH
464 return sy;
465}
466
467/* Filter out (in place) symbols that are useless for disassembly.
468 COUNT is the number of elements in SYMBOLS.
0af11b59 469 Return the number of useful symbols. */
252b5132
RH
470
471static long
472remove_useless_symbols (symbols, count)
473 asymbol **symbols;
474 long count;
475{
476 register asymbol **in_ptr = symbols, **out_ptr = symbols;
477
478 while (--count >= 0)
479 {
480 asymbol *sym = *in_ptr++;
481
482 if (sym->name == NULL || sym->name[0] == '\0')
483 continue;
484 if (sym->flags & (BSF_DEBUGGING))
485 continue;
486 if (bfd_is_und_section (sym->section)
487 || bfd_is_com_section (sym->section))
488 continue;
489
490 *out_ptr++ = sym;
491 }
492 return out_ptr - symbols;
493}
494
495/* Sort symbols into value order. */
496
0af11b59 497static int
252b5132
RH
498compare_symbols (ap, bp)
499 const PTR ap;
500 const PTR bp;
501{
502 const asymbol *a = *(const asymbol **)ap;
503 const asymbol *b = *(const asymbol **)bp;
504 const char *an, *bn;
505 size_t anl, bnl;
b34976b6 506 bfd_boolean af, bf;
252b5132
RH
507 flagword aflags, bflags;
508
509 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
510 return 1;
511 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
512 return -1;
513
514 if (a->section > b->section)
515 return 1;
516 else if (a->section < b->section)
517 return -1;
518
519 an = bfd_asymbol_name (a);
520 bn = bfd_asymbol_name (b);
521 anl = strlen (an);
522 bnl = strlen (bn);
523
524 /* The symbols gnu_compiled and gcc2_compiled convey no real
525 information, so put them after other symbols with the same value. */
526
527 af = (strstr (an, "gnu_compiled") != NULL
528 || strstr (an, "gcc2_compiled") != NULL);
529 bf = (strstr (bn, "gnu_compiled") != NULL
530 || strstr (bn, "gcc2_compiled") != NULL);
531
532 if (af && ! bf)
533 return 1;
534 if (! af && bf)
535 return -1;
536
537 /* We use a heuristic for the file name, to try to sort it after
538 more useful symbols. It may not work on non Unix systems, but it
539 doesn't really matter; the only difference is precisely which
540 symbol names get printed. */
541
542#define file_symbol(s, sn, snl) \
543 (((s)->flags & BSF_FILE) != 0 \
544 || ((sn)[(snl) - 2] == '.' \
545 && ((sn)[(snl) - 1] == 'o' \
546 || (sn)[(snl) - 1] == 'a')))
547
548 af = file_symbol (a, an, anl);
549 bf = file_symbol (b, bn, bnl);
550
551 if (af && ! bf)
552 return 1;
553 if (! af && bf)
554 return -1;
555
556 /* Try to sort global symbols before local symbols before function
557 symbols before debugging symbols. */
558
559 aflags = a->flags;
560 bflags = b->flags;
561
562 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
563 {
564 if ((aflags & BSF_DEBUGGING) != 0)
565 return 1;
566 else
567 return -1;
568 }
569 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
570 {
571 if ((aflags & BSF_FUNCTION) != 0)
572 return -1;
573 else
574 return 1;
575 }
576 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
577 {
578 if ((aflags & BSF_LOCAL) != 0)
579 return 1;
580 else
581 return -1;
582 }
583 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
584 {
585 if ((aflags & BSF_GLOBAL) != 0)
586 return -1;
587 else
588 return 1;
589 }
590
591 /* Symbols that start with '.' might be section names, so sort them
592 after symbols that don't start with '.'. */
593 if (an[0] == '.' && bn[0] != '.')
594 return 1;
595 if (an[0] != '.' && bn[0] == '.')
596 return -1;
597
598 /* Finally, if we can't distinguish them in any other way, try to
599 get consistent results by sorting the symbols by name. */
600 return strcmp (an, bn);
601}
602
603/* Sort relocs into address order. */
604
605static int
606compare_relocs (ap, bp)
607 const PTR ap;
608 const PTR bp;
609{
610 const arelent *a = *(const arelent **)ap;
611 const arelent *b = *(const arelent **)bp;
612
613 if (a->address > b->address)
614 return 1;
615 else if (a->address < b->address)
616 return -1;
617
618 /* So that associated relocations tied to the same address show up
619 in the correct order, we don't do any further sorting. */
620 if (a > b)
621 return 1;
622 else if (a < b)
623 return -1;
624 else
625 return 0;
626}
627
b34976b6 628/* Print VMA to STREAM. If SKIP_ZEROES is TRUE, omit leading zeroes. */
252b5132
RH
629
630static void
631objdump_print_value (vma, info, skip_zeroes)
632 bfd_vma vma;
633 struct disassemble_info *info;
b34976b6 634 bfd_boolean skip_zeroes;
252b5132
RH
635{
636 char buf[30];
637 char *p;
d8180c76
L
638 struct objdump_disasm_info *aux
639 = (struct objdump_disasm_info *) info->application_data;
252b5132 640
d8180c76 641 bfd_sprintf_vma (aux->abfd, buf, vma);
252b5132
RH
642 if (! skip_zeroes)
643 p = buf;
644 else
645 {
646 for (p = buf; *p == '0'; ++p)
647 ;
648 if (*p == '\0')
649 --p;
650 }
651 (*info->fprintf_func) (info->stream, "%s", p);
652}
653
654/* Print the name of a symbol. */
655
656static void
657objdump_print_symname (abfd, info, sym)
658 bfd *abfd;
659 struct disassemble_info *info;
660 asymbol *sym;
661{
662 char *alloc;
663 const char *name;
252b5132
RH
664
665 alloc = NULL;
666 name = bfd_asymbol_name (sym);
a6637ec0 667 if (do_demangle && name[0] != '\0')
252b5132
RH
668 {
669 /* Demangle the name. */
a6637ec0
AM
670 alloc = demangle (abfd, name);
671 name = alloc;
252b5132
RH
672 }
673
674 if (info != NULL)
a6637ec0 675 (*info->fprintf_func) (info->stream, "%s", name);
252b5132 676 else
a6637ec0 677 printf ("%s", name);
252b5132
RH
678
679 if (alloc != NULL)
680 free (alloc);
681}
682
683/* Locate a symbol given a bfd, a section, and a VMA. If REQUIRE_SEC
b34976b6 684 is TRUE, then always require the symbol to be in the section. This
252b5132
RH
685 returns NULL if there is no suitable symbol. If PLACE is not NULL,
686 then *PLACE is set to the index of the symbol in sorted_syms. */
687
688static asymbol *
689find_symbol_for_address (abfd, sec, vma, require_sec, place)
690 bfd *abfd;
691 asection *sec;
692 bfd_vma vma;
b34976b6 693 bfd_boolean require_sec;
252b5132
RH
694 long *place;
695{
696 /* @@ Would it speed things up to cache the last two symbols returned,
697 and maybe their address ranges? For many processors, only one memory
698 operand can be present at a time, so the 2-entry cache wouldn't be
699 constantly churned by code doing heavy memory accesses. */
700
701 /* Indices in `sorted_syms'. */
702 long min = 0;
703 long max = sorted_symcount;
704 long thisplace;
0af11b59 705 unsigned int opb = bfd_octets_per_byte (abfd);
252b5132
RH
706
707 if (sorted_symcount < 1)
708 return NULL;
709
710 /* Perform a binary search looking for the closest symbol to the
711 required value. We are searching the range (min, max]. */
712 while (min + 1 < max)
713 {
714 asymbol *sym;
715
716 thisplace = (max + min) / 2;
717 sym = sorted_syms[thisplace];
718
719 if (bfd_asymbol_value (sym) > vma)
720 max = thisplace;
721 else if (bfd_asymbol_value (sym) < vma)
722 min = thisplace;
723 else
724 {
725 min = thisplace;
726 break;
727 }
728 }
729
730 /* The symbol we want is now in min, the low end of the range we
731 were searching. If there are several symbols with the same
732 value, we want the first one. */
733 thisplace = min;
734 while (thisplace > 0
735 && (bfd_asymbol_value (sorted_syms[thisplace])
736 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
737 --thisplace;
738
739 /* If the file is relocateable, and the symbol could be from this
740 section, prefer a symbol from this section over symbols from
741 others, even if the other symbol's value might be closer.
0af11b59 742
252b5132
RH
743 Note that this may be wrong for some symbol references if the
744 sections have overlapping memory ranges, but in that case there's
745 no way to tell what's desired without looking at the relocation
746 table. */
747
748 if (sorted_syms[thisplace]->section != sec
749 && (require_sec
750 || ((abfd->flags & HAS_RELOC) != 0
751 && vma >= bfd_get_section_vma (abfd, sec)
752 && vma < (bfd_get_section_vma (abfd, sec)
940b2b78 753 + bfd_section_size (abfd, sec) / opb))))
252b5132
RH
754 {
755 long i;
756
757 for (i = thisplace + 1; i < sorted_symcount; i++)
758 {
759 if (bfd_asymbol_value (sorted_syms[i])
760 != bfd_asymbol_value (sorted_syms[thisplace]))
761 break;
762 }
763 --i;
764 for (; i >= 0; i--)
765 {
766 if (sorted_syms[i]->section == sec
767 && (i == 0
768 || sorted_syms[i - 1]->section != sec
769 || (bfd_asymbol_value (sorted_syms[i])
770 != bfd_asymbol_value (sorted_syms[i - 1]))))
771 {
772 thisplace = i;
773 break;
774 }
775 }
776
777 if (sorted_syms[thisplace]->section != sec)
778 {
779 /* We didn't find a good symbol with a smaller value.
780 Look for one with a larger value. */
781 for (i = thisplace + 1; i < sorted_symcount; i++)
782 {
783 if (sorted_syms[i]->section == sec)
784 {
785 thisplace = i;
786 break;
787 }
788 }
789 }
790
791 if (sorted_syms[thisplace]->section != sec
792 && (require_sec
793 || ((abfd->flags & HAS_RELOC) != 0
794 && vma >= bfd_get_section_vma (abfd, sec)
795 && vma < (bfd_get_section_vma (abfd, sec)
796 + bfd_section_size (abfd, sec)))))
797 {
798 /* There is no suitable symbol. */
799 return NULL;
800 }
801 }
802
803 if (place != NULL)
804 *place = thisplace;
805
806 return sorted_syms[thisplace];
807}
808
809/* Print an address to INFO symbolically. */
810
811static void
812objdump_print_addr_with_sym (abfd, sec, sym, vma, info, skip_zeroes)
813 bfd *abfd;
814 asection *sec;
815 asymbol *sym;
816 bfd_vma vma;
817 struct disassemble_info *info;
b34976b6 818 bfd_boolean skip_zeroes;
252b5132
RH
819{
820 objdump_print_value (vma, info, skip_zeroes);
821
822 if (sym == NULL)
823 {
824 bfd_vma secaddr;
825
826 (*info->fprintf_func) (info->stream, " <%s",
827 bfd_get_section_name (abfd, sec));
828 secaddr = bfd_get_section_vma (abfd, sec);
829 if (vma < secaddr)
830 {
831 (*info->fprintf_func) (info->stream, "-0x");
b34976b6 832 objdump_print_value (secaddr - vma, info, TRUE);
252b5132
RH
833 }
834 else if (vma > secaddr)
835 {
836 (*info->fprintf_func) (info->stream, "+0x");
b34976b6 837 objdump_print_value (vma - secaddr, info, TRUE);
252b5132
RH
838 }
839 (*info->fprintf_func) (info->stream, ">");
840 }
841 else
842 {
843 (*info->fprintf_func) (info->stream, " <");
844 objdump_print_symname (abfd, info, sym);
845 if (bfd_asymbol_value (sym) > vma)
846 {
847 (*info->fprintf_func) (info->stream, "-0x");
b34976b6 848 objdump_print_value (bfd_asymbol_value (sym) - vma, info, TRUE);
252b5132
RH
849 }
850 else if (vma > bfd_asymbol_value (sym))
851 {
852 (*info->fprintf_func) (info->stream, "+0x");
b34976b6 853 objdump_print_value (vma - bfd_asymbol_value (sym), info, TRUE);
252b5132
RH
854 }
855 (*info->fprintf_func) (info->stream, ">");
856 }
857}
858
859/* Print VMA to INFO, symbolically if possible. If SKIP_ZEROES is
b34976b6 860 TRUE, don't output leading zeroes. */
252b5132
RH
861
862static void
863objdump_print_addr (vma, info, skip_zeroes)
864 bfd_vma vma;
865 struct disassemble_info *info;
b34976b6 866 bfd_boolean skip_zeroes;
252b5132
RH
867{
868 struct objdump_disasm_info *aux;
869 asymbol *sym;
870
871 if (sorted_symcount < 1)
872 {
873 (*info->fprintf_func) (info->stream, "0x");
874 objdump_print_value (vma, info, skip_zeroes);
875 return;
876 }
877
878 aux = (struct objdump_disasm_info *) info->application_data;
879 sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec,
880 (long *) NULL);
881 objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, info,
882 skip_zeroes);
883}
884
885/* Print VMA to INFO. This function is passed to the disassembler
886 routine. */
887
888static void
889objdump_print_address (vma, info)
890 bfd_vma vma;
891 struct disassemble_info *info;
892{
893 objdump_print_addr (vma, info, ! prefix_addresses);
894}
895
896/* Determine of the given address has a symbol associated with it. */
897
898static int
899objdump_symbol_at_address (vma, info)
900 bfd_vma vma;
901 struct disassemble_info * info;
902{
903 struct objdump_disasm_info * aux;
904 asymbol * sym;
905
906 /* No symbols - do not bother checking. */
907 if (sorted_symcount < 1)
908 return 0;
909
910 aux = (struct objdump_disasm_info *) info->application_data;
911 sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec,
912 (long *) NULL);
913
914 return (sym != NULL && (bfd_asymbol_value (sym) == vma));
915}
916
917/* Hold the last function name and the last line number we displayed
918 in a disassembly. */
919
920static char *prev_functionname;
921static unsigned int prev_line;
922
923/* We keep a list of all files that we have seen when doing a
924 dissassembly with source, so that we know how much of the file to
925 display. This can be important for inlined functions. */
926
927struct print_file_list
928{
929 struct print_file_list *next;
930 char *filename;
931 unsigned int line;
932 FILE *f;
933};
934
935static struct print_file_list *print_files;
936
937/* The number of preceding context lines to show when we start
938 displaying a file for the first time. */
939
940#define SHOW_PRECEDING_CONTEXT_LINES (5)
941
942/* Skip ahead to a given line in a file, optionally printing each
943 line. */
944
b34976b6
AM
945static void skip_to_line
946 PARAMS ((struct print_file_list *, unsigned int, bfd_boolean));
252b5132
RH
947
948static void
949skip_to_line (p, line, show)
950 struct print_file_list *p;
951 unsigned int line;
b34976b6 952 bfd_boolean show;
252b5132
RH
953{
954 while (p->line < line)
955 {
956 char buf[100];
957
958 if (fgets (buf, sizeof buf, p->f) == NULL)
959 {
960 fclose (p->f);
961 p->f = NULL;
962 break;
963 }
964
965 if (show)
966 printf ("%s", buf);
967
968 if (strchr (buf, '\n') != NULL)
969 ++p->line;
970 }
0af11b59 971}
252b5132
RH
972
973/* Show the line number, or the source line, in a dissassembly
974 listing. */
975
976static void
940b2b78 977show_line (abfd, section, addr_offset)
252b5132
RH
978 bfd *abfd;
979 asection *section;
940b2b78 980 bfd_vma addr_offset;
252b5132 981{
b1f88ebe
AM
982 const char *filename;
983 const char *functionname;
252b5132
RH
984 unsigned int line;
985
986 if (! with_line_numbers && ! with_source_code)
987 return;
988
940b2b78 989 if (! bfd_find_nearest_line (abfd, section, syms, addr_offset, &filename,
252b5132
RH
990 &functionname, &line))
991 return;
992
993 if (filename != NULL && *filename == '\0')
994 filename = NULL;
995 if (functionname != NULL && *functionname == '\0')
996 functionname = NULL;
997
998 if (with_line_numbers)
999 {
1000 if (functionname != NULL
1001 && (prev_functionname == NULL
1002 || strcmp (functionname, prev_functionname) != 0))
1003 printf ("%s():\n", functionname);
1004 if (line > 0 && line != prev_line)
1005 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
1006 }
1007
1008 if (with_source_code
1009 && filename != NULL
1010 && line > 0)
1011 {
1012 struct print_file_list **pp, *p;
1013
1014 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
1015 if (strcmp ((*pp)->filename, filename) == 0)
1016 break;
1017 p = *pp;
1018
1019 if (p != NULL)
1020 {
1021 if (p != print_files)
1022 {
1023 int l;
1024
1025 /* We have reencountered a file name which we saw
1026 earlier. This implies that either we are dumping out
1027 code from an included file, or the same file was
1028 linked in more than once. There are two common cases
1029 of an included file: inline functions in a header
1030 file, and a bison or flex skeleton file. In the
1031 former case we want to just start printing (but we
1032 back up a few lines to give context); in the latter
1033 case we want to continue from where we left off. I
1034 can't think of a good way to distinguish the cases,
1035 so I used a heuristic based on the file name. */
1036 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
1037 l = p->line;
1038 else
1039 {
1040 l = line - SHOW_PRECEDING_CONTEXT_LINES;
f1563258
TW
1041 if (l < 0)
1042 l = 0;
252b5132
RH
1043 }
1044
1045 if (p->f == NULL)
1046 {
1047 p->f = fopen (p->filename, "r");
1048 p->line = 0;
1049 }
1050 if (p->f != NULL)
b34976b6 1051 skip_to_line (p, l, FALSE);
252b5132
RH
1052
1053 if (print_files->f != NULL)
1054 {
1055 fclose (print_files->f);
1056 print_files->f = NULL;
1057 }
1058 }
1059
1060 if (p->f != NULL)
1061 {
b34976b6 1062 skip_to_line (p, line, TRUE);
252b5132
RH
1063 *pp = p->next;
1064 p->next = print_files;
1065 print_files = p;
1066 }
1067 }
1068 else
1069 {
1070 FILE *f;
1071
1072 f = fopen (filename, "r");
1073 if (f != NULL)
1074 {
1075 int l;
1076
1077 p = ((struct print_file_list *)
1078 xmalloc (sizeof (struct print_file_list)));
1079 p->filename = xmalloc (strlen (filename) + 1);
1080 strcpy (p->filename, filename);
1081 p->line = 0;
1082 p->f = f;
1083
1084 if (print_files != NULL && print_files->f != NULL)
1085 {
1086 fclose (print_files->f);
1087 print_files->f = NULL;
1088 }
1089 p->next = print_files;
1090 print_files = p;
1091
0af11b59
KH
1092 if (file_start_context)
1093 l = 0;
1094 else
1095 l = line - SHOW_PRECEDING_CONTEXT_LINES;
f1563258
TW
1096 if (l < 0)
1097 l = 0;
b34976b6 1098 skip_to_line (p, l, FALSE);
252b5132 1099 if (p->f != NULL)
b34976b6 1100 skip_to_line (p, line, TRUE);
252b5132
RH
1101 }
1102 }
1103 }
1104
1105 if (functionname != NULL
1106 && (prev_functionname == NULL
1107 || strcmp (functionname, prev_functionname) != 0))
1108 {
1109 if (prev_functionname != NULL)
1110 free (prev_functionname);
1111 prev_functionname = xmalloc (strlen (functionname) + 1);
1112 strcpy (prev_functionname, functionname);
1113 }
1114
1115 if (line > 0 && line != prev_line)
1116 prev_line = line;
1117}
1118
1119/* Pseudo FILE object for strings. */
1120typedef struct
1121{
1122 char *buffer;
1123 size_t size;
1124 char *current;
1125} SFILE;
1126
1127/* sprintf to a "stream" */
1128
1129static int
451dad9c 1130objdump_sprintf VPARAMS ((SFILE *f, const char *format, ...))
252b5132 1131{
252b5132 1132 char *buf;
252b5132
RH
1133 size_t n;
1134
451dad9c
AM
1135 VA_OPEN (args, format);
1136 VA_FIXEDARG (args, SFILE *, f);
1137 VA_FIXEDARG (args, const char *, format);
252b5132
RH
1138
1139 vasprintf (&buf, format, args);
1140
252b5132
RH
1141 if (buf == NULL)
1142 {
451dad9c 1143 va_end (args);
37cc8ec1 1144 fatal (_("Out of virtual memory"));
252b5132
RH
1145 }
1146
1147 n = strlen (buf);
1148
b4c96d0d 1149 while ((size_t) ((f->buffer + f->size) - f->current) < n + 1)
252b5132
RH
1150 {
1151 size_t curroff;
1152
1153 curroff = f->current - f->buffer;
1154 f->size *= 2;
1155 f->buffer = xrealloc (f->buffer, f->size);
1156 f->current = f->buffer + curroff;
1157 }
1158
1159 memcpy (f->current, buf, n);
1160 f->current += n;
1161 f->current[0] = '\0';
1162
1163 free (buf);
1164
451dad9c 1165 VA_CLOSE (args);
252b5132
RH
1166 return n;
1167}
1168
1169/* The number of zeroes we want to see before we start skipping them.
1170 The number is arbitrarily chosen. */
1171
20fe0649 1172#ifndef SKIP_ZEROES
252b5132 1173#define SKIP_ZEROES (8)
20fe0649 1174#endif
252b5132
RH
1175
1176/* The number of zeroes to skip at the end of a section. If the
1177 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1178 SKIP_ZEROES, they will be disassembled. If there are fewer than
1179 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1180 attempt to avoid disassembling zeroes inserted by section
1181 alignment. */
1182
20fe0649 1183#ifndef SKIP_ZEROES_AT_END
252b5132 1184#define SKIP_ZEROES_AT_END (3)
20fe0649 1185#endif
252b5132
RH
1186
1187/* Disassemble some data in memory between given values. */
1188
1189static void
0af11b59
KH
1190disassemble_bytes (info, disassemble_fn, insns, data,
1191 start_offset, stop_offset, relppp,
252b5132
RH
1192 relppend)
1193 struct disassemble_info *info;
1194 disassembler_ftype disassemble_fn;
b34976b6 1195 bfd_boolean insns;
252b5132 1196 bfd_byte *data;
940b2b78
TW
1197 bfd_vma start_offset;
1198 bfd_vma stop_offset;
252b5132
RH
1199 arelent ***relppp;
1200 arelent **relppend;
1201{
1202 struct objdump_disasm_info *aux;
1203 asection *section;
940b2b78 1204 int octets_per_line;
b34976b6 1205 bfd_boolean done_dot;
252b5132 1206 int skip_addr_chars;
940b2b78
TW
1207 bfd_vma addr_offset;
1208 int opb = info->octets_per_byte;
252b5132
RH
1209
1210 aux = (struct objdump_disasm_info *) info->application_data;
1211 section = aux->sec;
1212
1213 if (insns)
940b2b78 1214 octets_per_line = 4;
252b5132 1215 else
940b2b78 1216 octets_per_line = 16;
252b5132
RH
1217
1218 /* Figure out how many characters to skip at the start of an
1219 address, to make the disassembly look nicer. We discard leading
1220 zeroes in chunks of 4, ensuring that there is always a leading
1221 zero remaining. */
1222 skip_addr_chars = 0;
1223 if (! prefix_addresses)
1224 {
1225 char buf[30];
1226 char *s;
1227
d8180c76
L
1228 bfd_sprintf_vma
1229 (aux->abfd, buf,
1230 (section->vma
1231 + bfd_section_size (section->owner, section) / opb));
252b5132
RH
1232 s = buf;
1233 while (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0'
1234 && s[4] == '0')
1235 {
1236 skip_addr_chars += 4;
1237 s += 4;
1238 }
1239 }
1240
1241 info->insn_info_valid = 0;
1242
b34976b6 1243 done_dot = FALSE;
940b2b78
TW
1244 addr_offset = start_offset;
1245 while (addr_offset < stop_offset)
252b5132
RH
1246 {
1247 bfd_vma z;
940b2b78 1248 int octets = 0;
b34976b6 1249 bfd_boolean need_nl = FALSE;
252b5132 1250
940b2b78 1251 /* If we see more than SKIP_ZEROES octets of zeroes, we just
252b5132 1252 print `...'. */
940b2b78 1253 for (z = addr_offset * opb; z < stop_offset * opb; z++)
252b5132
RH
1254 if (data[z] != 0)
1255 break;
1256 if (! disassemble_zeroes
1257 && (info->insn_info_valid == 0
1258 || info->branch_delay_insns == 0)
940b2b78 1259 && (z - addr_offset * opb >= SKIP_ZEROES
0af11b59
KH
1260 || (z == stop_offset * opb &&
1261 z - addr_offset * opb < SKIP_ZEROES_AT_END)))
252b5132
RH
1262 {
1263 printf ("\t...\n");
1264
940b2b78 1265 /* If there are more nonzero octets to follow, we only skip
252b5132
RH
1266 zeroes in multiples of 4, to try to avoid running over
1267 the start of an instruction which happens to start with
1268 zero. */
940b2b78
TW
1269 if (z != stop_offset * opb)
1270 z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
252b5132 1271
940b2b78 1272 octets = z - addr_offset * opb;
252b5132
RH
1273 }
1274 else
1275 {
1276 char buf[50];
1277 SFILE sfile;
1278 int bpc = 0;
1279 int pb = 0;
1280
b34976b6 1281 done_dot = FALSE;
252b5132
RH
1282
1283 if (with_line_numbers || with_source_code)
76a406e5
NC
1284 /* The line number tables will refer to unadjusted
1285 section VMAs, so we must undo any VMA modifications
1286 when calling show_line. */
1287 show_line (aux->abfd, section, addr_offset - adjust_section_vma);
252b5132
RH
1288
1289 if (! prefix_addresses)
1290 {
1291 char *s;
1292
d8180c76 1293 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
252b5132
RH
1294 for (s = buf + skip_addr_chars; *s == '0'; s++)
1295 *s = ' ';
1296 if (*s == '\0')
1297 *--s = '0';
1298 printf ("%s:\t", buf + skip_addr_chars);
1299 }
1300 else
1301 {
b34976b6 1302 aux->require_sec = TRUE;
940b2b78 1303 objdump_print_address (section->vma + addr_offset, info);
b34976b6 1304 aux->require_sec = FALSE;
252b5132
RH
1305 putchar (' ');
1306 }
1307
1308 if (insns)
1309 {
1310 sfile.size = 120;
1311 sfile.buffer = xmalloc (sfile.size);
1312 sfile.current = sfile.buffer;
1313 info->fprintf_func = (fprintf_ftype) objdump_sprintf;
1314 info->stream = (FILE *) &sfile;
1315 info->bytes_per_line = 0;
1316 info->bytes_per_chunk = 0;
1317
8b1e6df3 1318#ifdef DISASSEMBLER_NEEDS_RELOCS
940b2b78 1319 /* FIXME: This is wrong. It tests the number of octets
252b5132
RH
1320 in the last instruction, not the current one. */
1321 if (*relppp < relppend
940b2b78 1322 && (**relppp)->address >= addr_offset
8b1e6df3 1323 && (**relppp)->address <= addr_offset + octets / opb)
252b5132
RH
1324 info->flags = INSN_HAS_RELOC;
1325 else
8b1e6df3 1326#endif
252b5132
RH
1327 info->flags = 0;
1328
940b2b78 1329 octets = (*disassemble_fn) (section->vma + addr_offset, info);
252b5132
RH
1330 info->fprintf_func = (fprintf_ftype) fprintf;
1331 info->stream = stdout;
1332 if (info->bytes_per_line != 0)
940b2b78
TW
1333 octets_per_line = info->bytes_per_line;
1334 if (octets < 0)
e07bf1ac
ILT
1335 {
1336 if (sfile.current != sfile.buffer)
1337 printf ("%s\n", sfile.buffer);
1338 free (sfile.buffer);
1339 break;
1340 }
252b5132
RH
1341 }
1342 else
1343 {
b4c96d0d 1344 bfd_vma j;
252b5132 1345
940b2b78
TW
1346 octets = octets_per_line;
1347 if (addr_offset + octets / opb > stop_offset)
1348 octets = (stop_offset - addr_offset) * opb;
252b5132 1349
940b2b78 1350 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
252b5132 1351 {
3882b010 1352 if (ISPRINT (data[j]))
940b2b78 1353 buf[j - addr_offset * opb] = data[j];
252b5132 1354 else
940b2b78 1355 buf[j - addr_offset * opb] = '.';
252b5132 1356 }
940b2b78 1357 buf[j - addr_offset * opb] = '\0';
252b5132
RH
1358 }
1359
1360 if (prefix_addresses
1361 ? show_raw_insn > 0
1362 : show_raw_insn >= 0)
1363 {
b4c96d0d 1364 bfd_vma j;
252b5132
RH
1365
1366 /* If ! prefix_addresses and ! wide_output, we print
940b2b78
TW
1367 octets_per_line octets per line. */
1368 pb = octets;
1369 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
1370 pb = octets_per_line;
252b5132
RH
1371
1372 if (info->bytes_per_chunk)
1373 bpc = info->bytes_per_chunk;
1374 else
1375 bpc = 1;
1376
940b2b78 1377 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
252b5132
RH
1378 {
1379 int k;
1380 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1381 {
1382 for (k = bpc - 1; k >= 0; k--)
1383 printf ("%02x", (unsigned) data[j + k]);
1384 putchar (' ');
1385 }
1386 else
1387 {
1388 for (k = 0; k < bpc; k++)
1389 printf ("%02x", (unsigned) data[j + k]);
1390 putchar (' ');
1391 }
1392 }
1393
940b2b78 1394 for (; pb < octets_per_line; pb += bpc)
252b5132
RH
1395 {
1396 int k;
1397
1398 for (k = 0; k < bpc; k++)
1399 printf (" ");
1400 putchar (' ');
1401 }
1402
1403 /* Separate raw data from instruction by extra space. */
1404 if (insns)
1405 putchar ('\t');
1406 else
1407 printf (" ");
1408 }
1409
1410 if (! insns)
1411 printf ("%s", buf);
1412 else
1413 {
1414 printf ("%s", sfile.buffer);
1415 free (sfile.buffer);
1416 }
1417
1418 if (prefix_addresses
1419 ? show_raw_insn > 0
1420 : show_raw_insn >= 0)
1421 {
940b2b78 1422 while (pb < octets)
252b5132 1423 {
b4c96d0d 1424 bfd_vma j;
252b5132
RH
1425 char *s;
1426
1427 putchar ('\n');
940b2b78 1428 j = addr_offset * opb + pb;
252b5132 1429
d8180c76 1430 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
252b5132
RH
1431 for (s = buf + skip_addr_chars; *s == '0'; s++)
1432 *s = ' ';
1433 if (*s == '\0')
1434 *--s = '0';
1435 printf ("%s:\t", buf + skip_addr_chars);
1436
940b2b78
TW
1437 pb += octets_per_line;
1438 if (pb > octets)
1439 pb = octets;
1440 for (; j < addr_offset * opb + pb; j += bpc)
252b5132
RH
1441 {
1442 int k;
1443
1444 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1445 {
1446 for (k = bpc - 1; k >= 0; k--)
1447 printf ("%02x", (unsigned) data[j + k]);
1448 putchar (' ');
1449 }
1450 else
1451 {
1452 for (k = 0; k < bpc; k++)
1453 printf ("%02x", (unsigned) data[j + k]);
1454 putchar (' ');
1455 }
1456 }
1457 }
1458 }
1459
1460 if (!wide_output)
1461 putchar ('\n');
1462 else
b34976b6 1463 need_nl = TRUE;
252b5132
RH
1464 }
1465
8b1e6df3 1466 if ((section->flags & SEC_RELOC) != 0
0af11b59
KH
1467#ifndef DISASSEMBLER_NEEDS_RELOCS
1468 && dump_reloc_info
8b1e6df3
NC
1469#endif
1470 )
252b5132
RH
1471 {
1472 while ((*relppp) < relppend
940b2b78
TW
1473 && ((**relppp)->address >= (bfd_vma) addr_offset
1474 && (**relppp)->address < (bfd_vma) addr_offset + octets / opb))
8b1e6df3
NC
1475#ifdef DISASSEMBLER_NEEDS_RELOCS
1476 if (! dump_reloc_info)
1477 ++(*relppp);
1478 else
1479#endif
252b5132
RH
1480 {
1481 arelent *q;
1482
1483 q = **relppp;
1484
1485 if (wide_output)
1486 putchar ('\t');
1487 else
1488 printf ("\t\t\t");
1489
b34976b6 1490 objdump_print_value (section->vma + q->address, info, TRUE);
252b5132
RH
1491
1492 printf (": %s\t", q->howto->name);
1493
1494 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
1495 printf ("*unknown*");
1496 else
1497 {
1498 const char *sym_name;
1499
1500 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1501 if (sym_name != NULL && *sym_name != '\0')
1502 objdump_print_symname (aux->abfd, info, *q->sym_ptr_ptr);
1503 else
1504 {
1505 asection *sym_sec;
1506
1507 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1508 sym_name = bfd_get_section_name (aux->abfd, sym_sec);
1509 if (sym_name == NULL || *sym_name == '\0')
1510 sym_name = "*unknown*";
1511 printf ("%s", sym_name);
1512 }
1513 }
1514
1515 if (q->addend)
1516 {
1517 printf ("+0x");
b34976b6 1518 objdump_print_value (q->addend, info, TRUE);
252b5132
RH
1519 }
1520
1521 printf ("\n");
b34976b6 1522 need_nl = FALSE;
252b5132
RH
1523 ++(*relppp);
1524 }
1525 }
1526
1527 if (need_nl)
1528 printf ("\n");
1529
940b2b78 1530 addr_offset += octets / opb;
252b5132
RH
1531 }
1532}
1533
1534/* Disassemble the contents of an object file. */
1535
1536static void
1537disassemble_data (abfd)
1538 bfd *abfd;
1539{
f6af82bd 1540 unsigned long addr_offset;
252b5132
RH
1541 disassembler_ftype disassemble_fn;
1542 struct disassemble_info disasm_info;
1543 struct objdump_disasm_info aux;
1544 asection *section;
ea584125 1545 unsigned int opb;
252b5132
RH
1546
1547 print_files = NULL;
1548 prev_functionname = NULL;
1549 prev_line = -1;
1550
1551 /* We make a copy of syms to sort. We don't want to sort syms
1552 because that will screw up the relocs. */
1553 sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
1554 memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
1555
1556 sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
1557
1558 /* Sort the symbols into section and symbol order */
1559 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
1560
1561 INIT_DISASSEMBLE_INFO(disasm_info, stdout, fprintf);
1562 disasm_info.application_data = (PTR) &aux;
1563 aux.abfd = abfd;
b34976b6 1564 aux.require_sec = FALSE;
252b5132
RH
1565 disasm_info.print_address_func = objdump_print_address;
1566 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
1567
1568 if (machine != (char *) NULL)
1569 {
1570 const bfd_arch_info_type *info = bfd_scan_arch (machine);
1571 if (info == NULL)
1572 {
37cc8ec1 1573 fatal (_("Can't use supplied machine %s"), machine);
252b5132
RH
1574 }
1575 abfd->arch_info = info;
1576 }
1577
1578 if (endian != BFD_ENDIAN_UNKNOWN)
1579 {
1580 struct bfd_target *xvec;
1581
1582 xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
1583 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
1584 xvec->byteorder = endian;
1585 abfd->xvec = xvec;
1586 }
1587
1588 disassemble_fn = disassembler (abfd);
1589 if (!disassemble_fn)
1590 {
37cc8ec1
AM
1591 non_fatal (_("Can't disassemble for architecture %s\n"),
1592 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
75cd796a 1593 exit_status = 1;
252b5132
RH
1594 return;
1595 }
1596
ea584125
TW
1597 opb = bfd_octets_per_byte (abfd);
1598
252b5132
RH
1599 disasm_info.flavour = bfd_get_flavour (abfd);
1600 disasm_info.arch = bfd_get_arch (abfd);
1601 disasm_info.mach = bfd_get_mach (abfd);
dd92f639 1602 disasm_info.disassembler_options = disassembler_options;
ea584125 1603 disasm_info.octets_per_byte = opb;
0af11b59 1604
252b5132 1605 if (bfd_big_endian (abfd))
a8a9050d 1606 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
252b5132 1607 else if (bfd_little_endian (abfd))
a8a9050d 1608 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
252b5132
RH
1609 else
1610 /* ??? Aborting here seems too drastic. We could default to big or little
1611 instead. */
1612 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
1613
1614 for (section = abfd->sections;
1615 section != (asection *) NULL;
1616 section = section->next)
1617 {
1618 bfd_byte *data = NULL;
1619 bfd_size_type datasize = 0;
1620 arelent **relbuf = NULL;
1621 arelent **relpp = NULL;
1622 arelent **relppend = NULL;
f6af82bd 1623 unsigned long stop_offset;
252b5132
RH
1624 asymbol *sym = NULL;
1625 long place = 0;
1626
1627 if ((section->flags & SEC_LOAD) == 0
1628 || (! disassemble_all
1629 && only == NULL
1630 && (section->flags & SEC_CODE) == 0))
1631 continue;
1632 if (only != (char *) NULL && strcmp (only, section->name) != 0)
1633 continue;
1634
8b1e6df3 1635 if ((section->flags & SEC_RELOC) != 0
0af11b59 1636#ifndef DISASSEMBLER_NEEDS_RELOCS
8b1e6df3
NC
1637 && dump_reloc_info
1638#endif
0af11b59 1639 )
252b5132
RH
1640 {
1641 long relsize;
1642
1643 relsize = bfd_get_reloc_upper_bound (abfd, section);
1644 if (relsize < 0)
1645 bfd_fatal (bfd_get_filename (abfd));
1646
1647 if (relsize > 0)
1648 {
1649 long relcount;
1650
1651 relbuf = (arelent **) xmalloc (relsize);
1652 relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
1653 if (relcount < 0)
1654 bfd_fatal (bfd_get_filename (abfd));
1655
1656 /* Sort the relocs by address. */
1657 qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
1658
1659 relpp = relbuf;
1660 relppend = relpp + relcount;
1661
1662 /* Skip over the relocs belonging to addresses below the
1663 start address. */
1664 if (start_address != (bfd_vma) -1)
1665 {
1666 while (relpp < relppend
1667 && (*relpp)->address < start_address)
1668 ++relpp;
1669 }
1670 }
1671 }
1672
1673 printf (_("Disassembly of section %s:\n"), section->name);
1674
1675 datasize = bfd_get_section_size_before_reloc (section);
1676 if (datasize == 0)
1677 continue;
1678
1679 data = (bfd_byte *) xmalloc ((size_t) datasize);
1680
1681 bfd_get_section_contents (abfd, section, data, 0, datasize);
1682
1683 aux.sec = section;
1684 disasm_info.buffer = data;
1685 disasm_info.buffer_vma = section->vma;
1686 disasm_info.buffer_length = datasize;
8f9d9bd9 1687 disasm_info.section = section;
252b5132
RH
1688 if (start_address == (bfd_vma) -1
1689 || start_address < disasm_info.buffer_vma)
940b2b78 1690 addr_offset = 0;
252b5132 1691 else
940b2b78 1692 addr_offset = start_address - disasm_info.buffer_vma;
252b5132 1693 if (stop_address == (bfd_vma) -1)
940b2b78 1694 stop_offset = datasize / opb;
252b5132
RH
1695 else
1696 {
1697 if (stop_address < disasm_info.buffer_vma)
940b2b78 1698 stop_offset = 0;
252b5132 1699 else
940b2b78
TW
1700 stop_offset = stop_address - disasm_info.buffer_vma;
1701 if (stop_offset > disasm_info.buffer_length / opb)
1702 stop_offset = disasm_info.buffer_length / opb;
252b5132
RH
1703 }
1704
940b2b78 1705 sym = find_symbol_for_address (abfd, section, section->vma + addr_offset,
b34976b6 1706 TRUE, &place);
252b5132 1707
940b2b78 1708 while (addr_offset < stop_offset)
252b5132
RH
1709 {
1710 asymbol *nextsym;
f6af82bd 1711 unsigned long nextstop_offset;
b34976b6 1712 bfd_boolean insns;
0af11b59 1713
940b2b78 1714 if (sym != NULL && bfd_asymbol_value (sym) <= section->vma + addr_offset)
252b5132
RH
1715 {
1716 int x;
1717
1718 for (x = place;
1719 (x < sorted_symcount
940b2b78 1720 && bfd_asymbol_value (sorted_syms[x]) <= section->vma + addr_offset);
252b5132
RH
1721 ++x)
1722 continue;
1723 disasm_info.symbols = & sorted_syms[place];
1724 disasm_info.num_symbols = x - place;
1725 }
1726 else
1727 disasm_info.symbols = NULL;
1728
1729 if (! prefix_addresses)
1730 {
1731 printf ("\n");
1732 objdump_print_addr_with_sym (abfd, section, sym,
940b2b78 1733 section->vma + addr_offset,
252b5132 1734 &disasm_info,
b34976b6 1735 FALSE);
252b5132
RH
1736 printf (":\n");
1737 }
0af11b59 1738
940b2b78 1739 if (sym != NULL && bfd_asymbol_value (sym) > section->vma + addr_offset)
252b5132
RH
1740 nextsym = sym;
1741 else if (sym == NULL)
1742 nextsym = NULL;
1743 else
1744 {
f1f8ce07
ILT
1745 /* Search forward for the next appropriate symbol in
1746 SECTION. Note that all the symbols are sorted
1747 together into one big array, and that some sections
1748 may have overlapping addresses. */
252b5132 1749 while (place < sorted_symcount
252b5132
RH
1750 && (sorted_syms[place]->section != section
1751 || (bfd_asymbol_value (sorted_syms[place])
1752 <= bfd_asymbol_value (sym))))
1753 ++place;
1754 if (place >= sorted_symcount)
1755 nextsym = NULL;
1756 else
1757 nextsym = sorted_syms[place];
1758 }
0af11b59 1759
940b2b78 1760 if (sym != NULL && bfd_asymbol_value (sym) > section->vma + addr_offset)
252b5132 1761 {
940b2b78
TW
1762 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
1763 if (nextstop_offset > stop_offset)
1764 nextstop_offset = stop_offset;
252b5132
RH
1765 }
1766 else if (nextsym == NULL)
940b2b78 1767 nextstop_offset = stop_offset;
252b5132
RH
1768 else
1769 {
940b2b78
TW
1770 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
1771 if (nextstop_offset > stop_offset)
1772 nextstop_offset = stop_offset;
252b5132 1773 }
0af11b59 1774
252b5132
RH
1775 /* If a symbol is explicitly marked as being an object
1776 rather than a function, just dump the bytes without
1777 disassembling them. */
1778 if (disassemble_all
1779 || sym == NULL
940b2b78 1780 || bfd_asymbol_value (sym) > section->vma + addr_offset
252b5132
RH
1781 || ((sym->flags & BSF_OBJECT) == 0
1782 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
1783 == NULL)
1784 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
1785 == NULL))
1786 || (sym->flags & BSF_FUNCTION) != 0)
b34976b6 1787 insns = TRUE;
252b5132 1788 else
b34976b6 1789 insns = FALSE;
0af11b59
KH
1790
1791 disassemble_bytes (&disasm_info, disassemble_fn, insns, data,
1792 addr_offset, nextstop_offset, &relpp, relppend);
1793
940b2b78 1794 addr_offset = nextstop_offset;
252b5132
RH
1795 sym = nextsym;
1796 }
0af11b59 1797
252b5132
RH
1798 free (data);
1799 if (relbuf != NULL)
1800 free (relbuf);
1801 }
1802 free (sorted_syms);
1803}
1804\f
1805
1806/* Define a table of stab values and print-strings. We wish the initializer
1807 could be a direct-mapped table, but instead we build one the first
1808 time we need it. */
1809
b34976b6
AM
1810static void dump_section_stabs
1811 PARAMS ((bfd *abfd, char *stabsect_name, char *strsect_name));
252b5132
RH
1812
1813/* Dump the stabs sections from an object file that has a section that
1814 uses Sun stabs encoding. */
1815
1816static void
1817dump_stabs (abfd)
1818 bfd *abfd;
1819{
1820 dump_section_stabs (abfd, ".stab", ".stabstr");
1821 dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1822 dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1823 dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1824}
1825
1826static bfd_byte *stabs;
1827static bfd_size_type stab_size;
1828
1829static char *strtab;
1830static bfd_size_type stabstr_size;
1831
1832/* Read ABFD's stabs section STABSECT_NAME into `stabs'
1833 and string table section STRSECT_NAME into `strtab'.
b34976b6
AM
1834 If the section exists and was read, allocate the space and return TRUE.
1835 Otherwise return FALSE. */
252b5132 1836
b34976b6 1837static bfd_boolean
252b5132
RH
1838read_section_stabs (abfd, stabsect_name, strsect_name)
1839 bfd *abfd;
1840 const char *stabsect_name;
1841 const char *strsect_name;
1842{
1843 asection *stabsect, *stabstrsect;
1844
1845 stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1846 if (0 == stabsect)
1847 {
1848 printf (_("No %s section present\n\n"), stabsect_name);
b34976b6 1849 return FALSE;
252b5132
RH
1850 }
1851
1852 stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1853 if (0 == stabstrsect)
1854 {
37cc8ec1
AM
1855 non_fatal (_("%s has no %s section"),
1856 bfd_get_filename (abfd), strsect_name);
75cd796a 1857 exit_status = 1;
b34976b6 1858 return FALSE;
252b5132 1859 }
0af11b59 1860
252b5132
RH
1861 stab_size = bfd_section_size (abfd, stabsect);
1862 stabstr_size = bfd_section_size (abfd, stabstrsect);
1863
1864 stabs = (bfd_byte *) xmalloc (stab_size);
1865 strtab = (char *) xmalloc (stabstr_size);
0af11b59 1866
252b5132
RH
1867 if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1868 {
37cc8ec1
AM
1869 non_fatal (_("Reading %s section of %s failed: %s"),
1870 stabsect_name, bfd_get_filename (abfd),
1871 bfd_errmsg (bfd_get_error ()));
252b5132
RH
1872 free (stabs);
1873 free (strtab);
75cd796a 1874 exit_status = 1;
b34976b6 1875 return FALSE;
252b5132
RH
1876 }
1877
1878 if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1879 stabstr_size))
1880 {
37cc8ec1
AM
1881 non_fatal (_("Reading %s section of %s failed: %s\n"),
1882 strsect_name, bfd_get_filename (abfd),
1883 bfd_errmsg (bfd_get_error ()));
252b5132
RH
1884 free (stabs);
1885 free (strtab);
75cd796a 1886 exit_status = 1;
b34976b6 1887 return FALSE;
252b5132
RH
1888 }
1889
b34976b6 1890 return TRUE;
252b5132
RH
1891}
1892
1893/* Stabs entries use a 12 byte format:
1894 4 byte string table index
1895 1 byte stab type
1896 1 byte stab other field
1897 2 byte stab desc field
1898 4 byte stab value
1899 FIXME: This will have to change for a 64 bit object format. */
1900
1901#define STRDXOFF (0)
1902#define TYPEOFF (4)
1903#define OTHEROFF (5)
1904#define DESCOFF (6)
1905#define VALOFF (8)
1906#define STABSIZE (12)
1907
1908/* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1909 using string table section STRSECT_NAME (in `strtab'). */
1910
1911static void
1912print_section_stabs (abfd, stabsect_name, strsect_name)
1913 bfd *abfd;
1914 const char *stabsect_name;
b4c96d0d 1915 const char *strsect_name ATTRIBUTE_UNUSED;
252b5132
RH
1916{
1917 int i;
1918 unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1919 bfd_byte *stabp, *stabs_end;
1920
1921 stabp = stabs;
1922 stabs_end = stabp + stab_size;
1923
1924 printf (_("Contents of %s section:\n\n"), stabsect_name);
1925 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1926
1927 /* Loop through all symbols and print them.
1928
1929 We start the index at -1 because there is a dummy symbol on
1930 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1931
1932 for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++)
1933 {
1934 const char *name;
1935 unsigned long strx;
1936 unsigned char type, other;
1937 unsigned short desc;
1938 bfd_vma value;
1939
1940 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1941 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1942 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1943 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1944 value = bfd_h_get_32 (abfd, stabp + VALOFF);
1945
1946 printf ("\n%-6d ", i);
1947 /* Either print the stab name, or, if unnamed, print its number
0af11b59 1948 again (makes consistent formatting for tools like awk). */
252b5132
RH
1949 name = bfd_get_stab_name (type);
1950 if (name != NULL)
1951 printf ("%-6s", name);
1952 else if (type == N_UNDF)
1953 printf ("HdrSym");
1954 else
1955 printf ("%-6d", type);
1956 printf (" %-6d %-6d ", other, desc);
d8180c76 1957 bfd_printf_vma (abfd, value);
252b5132
RH
1958 printf (" %-6lu", strx);
1959
1960 /* Symbols with type == 0 (N_UNDF) specify the length of the
1961 string table associated with this file. We use that info
1962 to know how to relocate the *next* file's string table indices. */
1963
1964 if (type == N_UNDF)
1965 {
1966 file_string_table_offset = next_file_string_table_offset;
1967 next_file_string_table_offset += value;
1968 }
1969 else
1970 {
1971 /* Using the (possibly updated) string table offset, print the
1972 string (if any) associated with this symbol. */
1973
1974 if ((strx + file_string_table_offset) < stabstr_size)
1975 printf (" %s", &strtab[strx + file_string_table_offset]);
1976 else
1977 printf (" *");
1978 }
1979 }
1980 printf ("\n\n");
1981}
1982
1983static void
1984dump_section_stabs (abfd, stabsect_name, strsect_name)
1985 bfd *abfd;
1986 char *stabsect_name;
1987 char *strsect_name;
1988{
1989 asection *s;
1990
1991 /* Check for section names for which stabsect_name is a prefix, to
1992 handle .stab0, etc. */
1993 for (s = abfd->sections;
1994 s != NULL;
1995 s = s->next)
1996 {
1997 int len;
1998
1999 len = strlen (stabsect_name);
2000
2001 /* If the prefix matches, and the files section name ends with a
2002 nul or a digit, then we match. I.e., we want either an exact
2003 match or a section followed by a number. */
2004 if (strncmp (stabsect_name, s->name, len) == 0
2005 && (s->name[len] == '\000'
3882b010 2006 || ISDIGIT (s->name[len])))
252b5132
RH
2007 {
2008 if (read_section_stabs (abfd, s->name, strsect_name))
2009 {
2010 print_section_stabs (abfd, s->name, strsect_name);
2011 free (stabs);
2012 free (strtab);
2013 }
2014 }
2015 }
2016}
2017\f
2018static void
2019dump_bfd_header (abfd)
2020 bfd *abfd;
2021{
2022 char *comma = "";
2023
2024 printf (_("architecture: %s, "),
2025 bfd_printable_arch_mach (bfd_get_arch (abfd),
2026 bfd_get_mach (abfd)));
2027 printf (_("flags 0x%08x:\n"), abfd->flags);
2028
2029#define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2030 PF (HAS_RELOC, "HAS_RELOC");
2031 PF (EXEC_P, "EXEC_P");
2032 PF (HAS_LINENO, "HAS_LINENO");
2033 PF (HAS_DEBUG, "HAS_DEBUG");
2034 PF (HAS_SYMS, "HAS_SYMS");
2035 PF (HAS_LOCALS, "HAS_LOCALS");
2036 PF (DYNAMIC, "DYNAMIC");
2037 PF (WP_TEXT, "WP_TEXT");
2038 PF (D_PAGED, "D_PAGED");
2039 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
026df7c5 2040 PF (HAS_LOAD_PAGE, "HAS_LOAD_PAGE");
252b5132 2041 printf (_("\nstart address 0x"));
d8180c76 2042 bfd_printf_vma (abfd, abfd->start_address);
252b5132
RH
2043 printf ("\n");
2044}
2045\f
2046static void
2047dump_bfd_private_header (abfd)
2048bfd *abfd;
2049{
2050 bfd_print_private_bfd_data (abfd, stdout);
2051}
2052
d24de309
ILT
2053/* Dump selected contents of ABFD */
2054
252b5132 2055static void
d24de309 2056dump_bfd (abfd)
252b5132
RH
2057 bfd *abfd;
2058{
252b5132
RH
2059 /* If we are adjusting section VMA's, change them all now. Changing
2060 the BFD information is a hack. However, we must do it, or
2061 bfd_find_nearest_line will not do the right thing. */
2062 if (adjust_section_vma != 0)
2063 {
2064 asection *s;
2065
2066 for (s = abfd->sections; s != NULL; s = s->next)
2067 {
2068 s->vma += adjust_section_vma;
2069 s->lma += adjust_section_vma;
2070 }
2071 }
2072
fe0507eb
AM
2073 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd),
2074 abfd->xvec->name);
252b5132 2075 if (dump_ar_hdrs)
b34976b6 2076 print_arelt_descr (stdout, abfd, TRUE);
252b5132
RH
2077 if (dump_file_header)
2078 dump_bfd_header (abfd);
2079 if (dump_private_headers)
2080 dump_bfd_private_header (abfd);
2081 putchar ('\n');
2082 if (dump_section_headers)
2083 dump_headers (abfd);
026df7c5 2084
252b5132 2085 if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
026df7c5 2086 syms = slurp_symtab (abfd);
252b5132 2087 if (dump_dynamic_symtab || dump_dynamic_reloc_info)
026df7c5
NC
2088 dynsyms = slurp_dynamic_symtab (abfd);
2089
252b5132 2090 if (dump_symtab)
b34976b6 2091 dump_symbols (abfd, FALSE);
252b5132 2092 if (dump_dynamic_symtab)
b34976b6 2093 dump_symbols (abfd, TRUE);
252b5132
RH
2094 if (dump_stab_section_info)
2095 dump_stabs (abfd);
2096 if (dump_reloc_info && ! disassemble)
2097 dump_relocs (abfd);
2098 if (dump_dynamic_reloc_info)
2099 dump_dynamic_relocs (abfd);
2100 if (dump_section_contents)
2101 dump_data (abfd);
2102 if (disassemble)
2103 disassemble_data (abfd);
2104 if (dump_debugging)
2105 {
2106 PTR dhandle;
2107
2108 dhandle = read_debugging_info (abfd, syms, symcount);
2109 if (dhandle != NULL)
2110 {
2111 if (! print_debugging_info (stdout, dhandle))
75cd796a 2112 {
37cc8ec1
AM
2113 non_fatal (_("%s: printing debugging information failed"),
2114 bfd_get_filename (abfd));
75cd796a
ILT
2115 exit_status = 1;
2116 }
252b5132
RH
2117 }
2118 }
026df7c5 2119
252b5132
RH
2120 if (syms)
2121 {
2122 free (syms);
2123 syms = NULL;
2124 }
026df7c5 2125
252b5132
RH
2126 if (dynsyms)
2127 {
2128 free (dynsyms);
2129 dynsyms = NULL;
2130 }
2131}
2132
d24de309
ILT
2133static void
2134display_bfd (abfd)
2135 bfd *abfd;
2136{
2137 char **matching;
2138
2139 if (bfd_check_format_matches (abfd, bfd_object, &matching))
2140 {
2141 dump_bfd (abfd);
2142 return;
2143 }
2144
2145 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2146 {
2147 nonfatal (bfd_get_filename (abfd));
2148 list_matching_formats (matching);
2149 free (matching);
2150 return;
2151 }
2152
2153 if (bfd_get_error () != bfd_error_file_not_recognized)
2154 {
2155 nonfatal (bfd_get_filename (abfd));
2156 return;
2157 }
2158
2159 if (bfd_check_format_matches (abfd, bfd_core, &matching))
2160 {
2161 dump_bfd (abfd);
2162 return;
2163 }
2164
2165 nonfatal (bfd_get_filename (abfd));
2166
2167 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2168 {
2169 list_matching_formats (matching);
2170 free (matching);
2171 }
2172}
2173
252b5132
RH
2174static void
2175display_file (filename, target)
2176 char *filename;
2177 char *target;
2178{
2179 bfd *file, *arfile = (bfd *) NULL;
2180
2181 file = bfd_openr (filename, target);
2182 if (file == NULL)
2183 {
75cd796a 2184 nonfatal (filename);
252b5132
RH
2185 return;
2186 }
2187
b34976b6 2188 if (bfd_check_format (file, bfd_archive))
252b5132
RH
2189 {
2190 bfd *last_arfile = NULL;
2191
2192 printf (_("In archive %s:\n"), bfd_get_filename (file));
2193 for (;;)
2194 {
2195 bfd_set_error (bfd_error_no_error);
2196
2197 arfile = bfd_openr_next_archived_file (file, arfile);
2198 if (arfile == NULL)
2199 {
2200 if (bfd_get_error () != bfd_error_no_more_archived_files)
75cd796a 2201 nonfatal (bfd_get_filename (file));
252b5132
RH
2202 break;
2203 }
2204
2205 display_bfd (arfile);
2206
2207 if (last_arfile != NULL)
2208 bfd_close (last_arfile);
2209 last_arfile = arfile;
2210 }
2211
2212 if (last_arfile != NULL)
2213 bfd_close (last_arfile);
2214 }
2215 else
2216 display_bfd (file);
2217
2218 bfd_close (file);
2219}
2220\f
2221/* Actually display the various requested regions */
2222
2223static void
2224dump_data (abfd)
2225 bfd *abfd;
2226{
2227 asection *section;
2228 bfd_byte *data = 0;
2229 bfd_size_type datasize = 0;
940b2b78
TW
2230 bfd_size_type addr_offset;
2231 bfd_size_type start_offset, stop_offset;
f6af82bd 2232 unsigned int opb = bfd_octets_per_byte (abfd);
252b5132
RH
2233
2234 for (section = abfd->sections; section != NULL; section =
2235 section->next)
2236 {
2237 int onaline = 16;
2238
2239 if (only == (char *) NULL ||
2240 strcmp (only, section->name) == 0)
2241 {
2242 if (section->flags & SEC_HAS_CONTENTS)
2243 {
2244 printf (_("Contents of section %s:\n"), section->name);
2245
2246 if (bfd_section_size (abfd, section) == 0)
2247 continue;
2248 data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
2249 datasize = bfd_section_size (abfd, section);
2250
2251
2252 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
2253
2254 if (start_address == (bfd_vma) -1
2255 || start_address < section->vma)
940b2b78 2256 start_offset = 0;
252b5132 2257 else
940b2b78 2258 start_offset = start_address - section->vma;
252b5132 2259 if (stop_address == (bfd_vma) -1)
940b2b78 2260 stop_offset = bfd_section_size (abfd, section) / opb;
252b5132
RH
2261 else
2262 {
2263 if (stop_address < section->vma)
940b2b78 2264 stop_offset = 0;
252b5132 2265 else
940b2b78
TW
2266 stop_offset = stop_address - section->vma;
2267 if (stop_offset > bfd_section_size (abfd, section) / opb)
2268 stop_offset = bfd_section_size (abfd, section) / opb;
252b5132 2269 }
0af11b59 2270 for (addr_offset = start_offset;
8ab56b6f 2271 addr_offset < stop_offset; addr_offset += onaline / opb)
252b5132
RH
2272 {
2273 bfd_size_type j;
2274
0af11b59
KH
2275 printf (" %04lx ", (unsigned long int)
2276 (addr_offset + section->vma));
2277 for (j = addr_offset * opb;
2278 j < addr_offset * opb + onaline; j++)
252b5132 2279 {
940b2b78 2280 if (j < stop_offset * opb)
252b5132
RH
2281 printf ("%02x", (unsigned) (data[j]));
2282 else
2283 printf (" ");
2284 if ((j & 3) == 3)
2285 printf (" ");
2286 }
2287
2288 printf (" ");
8ab56b6f
AM
2289 for (j = addr_offset * opb;
2290 j < addr_offset * opb + onaline; j++)
252b5132 2291 {
940b2b78 2292 if (j >= stop_offset * opb)
252b5132
RH
2293 printf (" ");
2294 else
3882b010 2295 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
252b5132
RH
2296 }
2297 putchar ('\n');
2298 }
2299 free (data);
2300 }
2301 }
2302 }
2303}
2304
2305/* Should perhaps share code and display with nm? */
2306static void
2307dump_symbols (abfd, dynamic)
b4c96d0d 2308 bfd *abfd ATTRIBUTE_UNUSED;
b34976b6 2309 bfd_boolean dynamic;
252b5132
RH
2310{
2311 asymbol **current;
2312 long max;
2313 long count;
2314
2315 if (dynamic)
2316 {
2317 current = dynsyms;
2318 max = dynsymcount;
252b5132
RH
2319 printf ("DYNAMIC SYMBOL TABLE:\n");
2320 }
2321 else
2322 {
2323 current = syms;
2324 max = symcount;
252b5132
RH
2325 printf ("SYMBOL TABLE:\n");
2326 }
2327
a1df01d1
AM
2328 if (max == 0)
2329 printf (_("no symbols\n"));
2330
252b5132
RH
2331 for (count = 0; count < max; count++)
2332 {
2333 if (*current)
2334 {
2335 bfd *cur_bfd = bfd_asymbol_bfd (*current);
2336
2337 if (cur_bfd != NULL)
2338 {
2339 const char *name;
2340 char *alloc;
2341
a6637ec0 2342 name = (*current)->name;
252b5132
RH
2343 alloc = NULL;
2344 if (do_demangle && name != NULL && *name != '\0')
2345 {
252b5132
RH
2346 /* If we want to demangle the name, we demangle it
2347 here, and temporarily clobber it while calling
2348 bfd_print_symbol. FIXME: This is a gross hack. */
2349
a6637ec0
AM
2350 alloc = demangle (cur_bfd, name);
2351 (*current)->name = alloc;
252b5132
RH
2352 }
2353
2354 bfd_print_symbol (cur_bfd, stdout, *current,
2355 bfd_print_symbol_all);
2356
2357 (*current)->name = name;
2358 if (alloc != NULL)
2359 free (alloc);
2360
2361 printf ("\n");
2362 }
2363 }
2364 current++;
2365 }
2366 printf ("\n");
2367 printf ("\n");
2368}
2369
2370static void
2371dump_relocs (abfd)
2372 bfd *abfd;
2373{
2374 arelent **relpp;
2375 long relcount;
2376 asection *a;
2377
2378 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
2379 {
2380 long relsize;
2381
2382 if (bfd_is_abs_section (a))
2383 continue;
2384 if (bfd_is_und_section (a))
2385 continue;
2386 if (bfd_is_com_section (a))
2387 continue;
2388
2389 if (only)
2390 {
2391 if (strcmp (only, a->name))
2392 continue;
2393 }
2394 else if ((a->flags & SEC_RELOC) == 0)
2395 continue;
2396
2397 relsize = bfd_get_reloc_upper_bound (abfd, a);
2398 if (relsize < 0)
2399 bfd_fatal (bfd_get_filename (abfd));
2400
2401 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
2402
2403 if (relsize == 0)
2404 {
2405 printf (" (none)\n\n");
2406 }
2407 else
2408 {
2409 relpp = (arelent **) xmalloc (relsize);
2410 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
2411 if (relcount < 0)
2412 bfd_fatal (bfd_get_filename (abfd));
2413 else if (relcount == 0)
2414 {
2415 printf (" (none)\n\n");
2416 }
2417 else
2418 {
2419 printf ("\n");
2420 dump_reloc_set (abfd, a, relpp, relcount);
2421 printf ("\n\n");
2422 }
2423 free (relpp);
2424 }
2425 }
2426}
2427
2428static void
2429dump_dynamic_relocs (abfd)
2430 bfd *abfd;
2431{
2432 long relsize;
2433 arelent **relpp;
2434 long relcount;
2435
2436 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2437 if (relsize < 0)
2438 bfd_fatal (bfd_get_filename (abfd));
2439
2440 printf ("DYNAMIC RELOCATION RECORDS");
2441
2442 if (relsize == 0)
2443 {
2444 printf (" (none)\n\n");
2445 }
2446 else
2447 {
2448 relpp = (arelent **) xmalloc (relsize);
2449 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
2450 if (relcount < 0)
2451 bfd_fatal (bfd_get_filename (abfd));
2452 else if (relcount == 0)
2453 {
2454 printf (" (none)\n\n");
2455 }
2456 else
2457 {
2458 printf ("\n");
2459 dump_reloc_set (abfd, (asection *) NULL, relpp, relcount);
2460 printf ("\n\n");
2461 }
2462 free (relpp);
2463 }
2464}
2465
2466static void
2467dump_reloc_set (abfd, sec, relpp, relcount)
2468 bfd *abfd;
2469 asection *sec;
2470 arelent **relpp;
2471 long relcount;
2472{
2473 arelent **p;
2474 char *last_filename, *last_functionname;
2475 unsigned int last_line;
2476
2477 /* Get column headers lined up reasonably. */
2478 {
2479 static int width;
2480 if (width == 0)
2481 {
2482 char buf[30];
d8180c76 2483 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
252b5132
RH
2484 width = strlen (buf) - 7;
2485 }
2486 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
2487 }
2488
2489 last_filename = NULL;
2490 last_functionname = NULL;
2491 last_line = 0;
2492
2493 for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
2494 {
2495 arelent *q = *p;
2496 const char *filename, *functionname;
2497 unsigned int line;
2498 const char *sym_name;
2499 const char *section_name;
2500
2501 if (start_address != (bfd_vma) -1
2502 && q->address < start_address)
2503 continue;
2504 if (stop_address != (bfd_vma) -1
2505 && q->address > stop_address)
2506 continue;
2507
2508 if (with_line_numbers
2509 && sec != NULL
2510 && bfd_find_nearest_line (abfd, sec, syms, q->address,
2511 &filename, &functionname, &line))
2512 {
2513 if (functionname != NULL
2514 && (last_functionname == NULL
2515 || strcmp (functionname, last_functionname) != 0))
2516 {
2517 printf ("%s():\n", functionname);
2518 if (last_functionname != NULL)
2519 free (last_functionname);
2520 last_functionname = xstrdup (functionname);
2521 }
2522 if (line > 0
2523 && (line != last_line
2524 || (filename != NULL
2525 && last_filename != NULL
2526 && strcmp (filename, last_filename) != 0)))
2527 {
2528 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
2529 last_line = line;
2530 if (last_filename != NULL)
2531 free (last_filename);
2532 if (filename == NULL)
2533 last_filename = NULL;
2534 else
2535 last_filename = xstrdup (filename);
2536 }
2537 }
2538
2539 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
2540 {
2541 sym_name = (*(q->sym_ptr_ptr))->name;
2542 section_name = (*(q->sym_ptr_ptr))->section->name;
2543 }
2544 else
2545 {
2546 sym_name = NULL;
2547 section_name = NULL;
2548 }
2549 if (sym_name)
2550 {
d8180c76 2551 bfd_printf_vma (abfd, q->address);
09cda596
DD
2552 if (q->howto->name)
2553 printf (" %-16s ", q->howto->name);
2554 else
2555 printf (" %-16d ", q->howto->type);
252b5132
RH
2556 objdump_print_symname (abfd, (struct disassemble_info *) NULL,
2557 *q->sym_ptr_ptr);
2558 }
2559 else
2560 {
b1f88ebe 2561 if (section_name == (const char *) NULL)
252b5132 2562 section_name = "*unknown*";
d8180c76 2563 bfd_printf_vma (abfd, q->address);
252b5132
RH
2564 printf (" %-16s [%s]",
2565 q->howto->name,
2566 section_name);
2567 }
2568 if (q->addend)
2569 {
2570 printf ("+0x");
d8180c76 2571 bfd_printf_vma (abfd, q->addend);
252b5132
RH
2572 }
2573 printf ("\n");
2574 }
2575}
2576\f
2577/* The length of the longest architecture name + 1. */
2ac374c4 2578#define LONGEST_ARCH sizeof("powerpc:common")
252b5132
RH
2579
2580static const char *
2581endian_string (endian)
2582 enum bfd_endian endian;
2583{
2584 if (endian == BFD_ENDIAN_BIG)
2585 return "big endian";
2586 else if (endian == BFD_ENDIAN_LITTLE)
2587 return "little endian";
2588 else
2589 return "endianness unknown";
2590}
2591
2592/* List the targets that BFD is configured to support, each followed
2593 by its endianness and the architectures it supports. */
2594
2595static void
2596display_target_list ()
2597{
7340082d 2598 extern const bfd_target *const *bfd_target_vector;
252b5132
RH
2599 char *dummy_name;
2600 int t;
2601
5ee1baa2 2602 dummy_name = make_temp_file (NULL);
252b5132
RH
2603 for (t = 0; bfd_target_vector[t]; t++)
2604 {
7340082d 2605 const bfd_target *p = bfd_target_vector[t];
252b5132
RH
2606 bfd *abfd = bfd_openw (dummy_name, p->name);
2607 int a;
2608
2609 printf ("%s\n (header %s, data %s)\n", p->name,
2610 endian_string (p->header_byteorder),
2611 endian_string (p->byteorder));
2612
2613 if (abfd == NULL)
2614 {
75cd796a 2615 nonfatal (dummy_name);
252b5132
RH
2616 continue;
2617 }
2618
2619 if (! bfd_set_format (abfd, bfd_object))
2620 {
2621 if (bfd_get_error () != bfd_error_invalid_operation)
75cd796a 2622 nonfatal (p->name);
fb101ead 2623 bfd_close_all_done (abfd);
252b5132
RH
2624 continue;
2625 }
2626
2627 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
2628 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
2629 printf (" %s\n",
2630 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
fb101ead 2631 bfd_close_all_done (abfd);
252b5132
RH
2632 }
2633 unlink (dummy_name);
2634 free (dummy_name);
2635}
2636
2637/* Print a table showing which architectures are supported for entries
2638 FIRST through LAST-1 of bfd_target_vector (targets across,
2639 architectures down). */
2640
2641static void
2642display_info_table (first, last)
2643 int first;
2644 int last;
2645{
7340082d 2646 extern const bfd_target *const *bfd_target_vector;
252b5132
RH
2647 int t, a;
2648 char *dummy_name;
2649
2650 /* Print heading of target names. */
2651 printf ("\n%*s", (int) LONGEST_ARCH, " ");
2652 for (t = first; t < last && bfd_target_vector[t]; t++)
2653 printf ("%s ", bfd_target_vector[t]->name);
2654 putchar ('\n');
2655
5ee1baa2 2656 dummy_name = make_temp_file (NULL);
252b5132
RH
2657 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
2658 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
2659 {
2660 printf ("%*s ", (int) LONGEST_ARCH - 1,
2661 bfd_printable_arch_mach (a, 0));
2662 for (t = first; t < last && bfd_target_vector[t]; t++)
2663 {
7340082d 2664 const bfd_target *p = bfd_target_vector[t];
b34976b6 2665 bfd_boolean ok = TRUE;
252b5132
RH
2666 bfd *abfd = bfd_openw (dummy_name, p->name);
2667
2668 if (abfd == NULL)
2669 {
75cd796a 2670 nonfatal (p->name);
b34976b6 2671 ok = FALSE;
252b5132
RH
2672 }
2673
2674 if (ok)
2675 {
2676 if (! bfd_set_format (abfd, bfd_object))
2677 {
2678 if (bfd_get_error () != bfd_error_invalid_operation)
75cd796a 2679 nonfatal (p->name);
b34976b6 2680 ok = FALSE;
252b5132
RH
2681 }
2682 }
2683
2684 if (ok)
2685 {
2686 if (! bfd_set_arch_mach (abfd, a, 0))
b34976b6 2687 ok = FALSE;
252b5132
RH
2688 }
2689
2690 if (ok)
2691 printf ("%s ", p->name);
2692 else
2693 {
2694 int l = strlen (p->name);
2695 while (l--)
2696 putchar ('-');
2697 putchar (' ');
2698 }
f190c5c4 2699 if (abfd != NULL)
fb101ead 2700 bfd_close_all_done (abfd);
252b5132
RH
2701 }
2702 putchar ('\n');
2703 }
2704 unlink (dummy_name);
2705 free (dummy_name);
2706}
2707
2708/* Print tables of all the target-architecture combinations that
2709 BFD has been configured to support. */
2710
2711static void
2712display_target_tables ()
2713{
2714 int t, columns;
7340082d 2715 extern const bfd_target *const *bfd_target_vector;
252b5132
RH
2716 char *colum;
2717
2718 columns = 0;
2719 colum = getenv ("COLUMNS");
2720 if (colum != NULL)
2721 columns = atoi (colum);
2722 if (columns == 0)
2723 columns = 80;
2724
2725 t = 0;
2726 while (bfd_target_vector[t] != NULL)
2727 {
2728 int oldt = t, wid;
2729
2730 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
2731 ++t;
2732 while (wid < columns && bfd_target_vector[t] != NULL)
2733 {
2734 int newwid;
2735
2736 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
2737 if (newwid >= columns)
2738 break;
2739 wid = newwid;
2740 ++t;
2741 }
2742 display_info_table (oldt, t);
2743 }
2744}
2745
2746static void
2747display_info ()
2748{
6a8c2b0d 2749 printf (_("BFD header file version %s\n"), BFD_VERSION_STRING);
252b5132
RH
2750 display_target_list ();
2751 display_target_tables ();
2752}
2753
65de42c0
TS
2754int main PARAMS ((int, char **));
2755
252b5132
RH
2756int
2757main (argc, argv)
2758 int argc;
2759 char **argv;
2760{
2761 int c;
2762 char *target = default_target;
b34976b6 2763 bfd_boolean seenflag = FALSE;
252b5132
RH
2764
2765#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2766 setlocale (LC_MESSAGES, "");
3882b010
L
2767#endif
2768#if defined (HAVE_SETLOCALE)
2769 setlocale (LC_CTYPE, "");
252b5132
RH
2770#endif
2771 bindtextdomain (PACKAGE, LOCALEDIR);
2772 textdomain (PACKAGE);
2773
2774 program_name = *argv;
2775 xmalloc_set_program_name (program_name);
2776
2777 START_PROGRESS (program_name, 0);
2778
2779 bfd_init ();
2780 set_default_bfd_target ();
2781
8b53311e 2782 while ((c = getopt_long (argc, argv, "pib:m:M:VvCdDlfaHhrRtTxsSj:wE:zgG",
252b5132
RH
2783 long_options, (int *) 0))
2784 != EOF)
2785 {
252b5132
RH
2786 switch (c)
2787 {
2788 case 0:
8b53311e 2789 break; /* We've been given a long option. */
252b5132
RH
2790 case 'm':
2791 machine = optarg;
2792 break;
dd92f639
NC
2793 case 'M':
2794 disassembler_options = optarg;
2795 break;
252b5132
RH
2796 case 'j':
2797 only = optarg;
2798 break;
2799 case 'l':
b34976b6 2800 with_line_numbers = TRUE;
252b5132
RH
2801 break;
2802 case 'b':
2803 target = optarg;
2804 break;
1dada9c5 2805 case 'C':
b34976b6 2806 do_demangle = TRUE;
28c309a2
NC
2807 if (optarg != NULL)
2808 {
2809 enum demangling_styles style;
8b53311e 2810
28c309a2 2811 style = cplus_demangle_name_to_style (optarg);
0af11b59 2812 if (style == unknown_demangling)
28c309a2
NC
2813 fatal (_("unknown demangling style `%s'"),
2814 optarg);
8b53311e 2815
28c309a2 2816 cplus_demangle_set_style (style);
0af11b59 2817 }
1dada9c5
NC
2818 break;
2819 case 'w':
b34976b6 2820 wide_output = TRUE;
1dada9c5
NC
2821 break;
2822 case OPTION_ADJUST_VMA:
2823 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
2824 break;
2825 case OPTION_START_ADDRESS:
2826 start_address = parse_vma (optarg, "--start-address");
2827 break;
2828 case OPTION_STOP_ADDRESS:
2829 stop_address = parse_vma (optarg, "--stop-address");
2830 break;
2831 case 'E':
2832 if (strcmp (optarg, "B") == 0)
2833 endian = BFD_ENDIAN_BIG;
2834 else if (strcmp (optarg, "L") == 0)
2835 endian = BFD_ENDIAN_LITTLE;
2836 else
2837 {
37cc8ec1 2838 non_fatal (_("unrecognized -E option"));
1dada9c5
NC
2839 usage (stderr, 1);
2840 }
2841 break;
2842 case OPTION_ENDIAN:
2843 if (strncmp (optarg, "big", strlen (optarg)) == 0)
2844 endian = BFD_ENDIAN_BIG;
2845 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
2846 endian = BFD_ENDIAN_LITTLE;
2847 else
2848 {
37cc8ec1 2849 non_fatal (_("unrecognized --endian type `%s'"), optarg);
1dada9c5
NC
2850 usage (stderr, 1);
2851 }
2852 break;
8b53311e 2853
252b5132 2854 case 'f':
b34976b6
AM
2855 dump_file_header = TRUE;
2856 seenflag = TRUE;
252b5132
RH
2857 break;
2858 case 'i':
b34976b6
AM
2859 formats_info = TRUE;
2860 seenflag = TRUE;
252b5132
RH
2861 break;
2862 case 'p':
b34976b6
AM
2863 dump_private_headers = TRUE;
2864 seenflag = TRUE;
252b5132
RH
2865 break;
2866 case 'x':
b34976b6
AM
2867 dump_private_headers = TRUE;
2868 dump_symtab = TRUE;
2869 dump_reloc_info = TRUE;
2870 dump_file_header = TRUE;
2871 dump_ar_hdrs = TRUE;
2872 dump_section_headers = TRUE;
2873 seenflag = TRUE;
252b5132
RH
2874 break;
2875 case 't':
b34976b6
AM
2876 dump_symtab = TRUE;
2877 seenflag = TRUE;
252b5132
RH
2878 break;
2879 case 'T':
b34976b6
AM
2880 dump_dynamic_symtab = TRUE;
2881 seenflag = TRUE;
252b5132
RH
2882 break;
2883 case 'd':
b34976b6
AM
2884 disassemble = TRUE;
2885 seenflag = TRUE;
1dada9c5
NC
2886 break;
2887 case 'z':
b34976b6 2888 disassemble_zeroes = TRUE;
252b5132
RH
2889 break;
2890 case 'D':
b34976b6
AM
2891 disassemble = TRUE;
2892 disassemble_all = TRUE;
2893 seenflag = TRUE;
252b5132
RH
2894 break;
2895 case 'S':
b34976b6
AM
2896 disassemble = TRUE;
2897 with_source_code = TRUE;
2898 seenflag = TRUE;
1dada9c5
NC
2899 break;
2900 case 'g':
2901 dump_debugging = 1;
b34976b6 2902 seenflag = TRUE;
1dada9c5
NC
2903 break;
2904 case 'G':
b34976b6
AM
2905 dump_stab_section_info = TRUE;
2906 seenflag = TRUE;
252b5132
RH
2907 break;
2908 case 's':
b34976b6
AM
2909 dump_section_contents = TRUE;
2910 seenflag = TRUE;
252b5132
RH
2911 break;
2912 case 'r':
b34976b6
AM
2913 dump_reloc_info = TRUE;
2914 seenflag = TRUE;
252b5132
RH
2915 break;
2916 case 'R':
b34976b6
AM
2917 dump_dynamic_reloc_info = TRUE;
2918 seenflag = TRUE;
252b5132
RH
2919 break;
2920 case 'a':
b34976b6
AM
2921 dump_ar_hdrs = TRUE;
2922 seenflag = TRUE;
252b5132
RH
2923 break;
2924 case 'h':
b34976b6
AM
2925 dump_section_headers = TRUE;
2926 seenflag = TRUE;
252b5132
RH
2927 break;
2928 case 'H':
2929 usage (stdout, 0);
b34976b6 2930 seenflag = TRUE;
8b53311e 2931 case 'v':
252b5132 2932 case 'V':
b34976b6
AM
2933 show_version = TRUE;
2934 seenflag = TRUE;
252b5132 2935 break;
0af11b59 2936
252b5132
RH
2937 default:
2938 usage (stderr, 1);
2939 }
2940 }
2941
2942 if (show_version)
2943 print_version ("objdump");
2944
b34976b6 2945 if (!seenflag)
1dada9c5 2946 usage (stderr, 2);
252b5132
RH
2947
2948 if (formats_info)
1dada9c5 2949 display_info ();
252b5132
RH
2950 else
2951 {
2952 if (optind == argc)
2953 display_file ("a.out", target);
2954 else
2955 for (; optind < argc;)
2956 display_file (argv[optind++], target);
2957 }
2958
2959 END_PROGRESS (program_name);
2960
75cd796a 2961 return exit_status;
252b5132 2962}
This page took 0.287904 seconds and 4 git commands to generate.