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