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