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