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