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