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