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