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