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