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