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