Fix objdump's display of indirect strings in object files, which was broken by the...
[deliverable/binutils-gdb.git] / binutils / objdump.c
1 /* objdump.c -- dump information about an object file.
2 Copyright (C) 1990-2019 Free Software Foundation, Inc.
3
4 This file is part of GNU Binutils.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21
22 /* Objdump overview.
23
24 Objdump displays information about one or more object files, either on
25 their own, or inside libraries. It is commonly used as a disassembler,
26 but it can also display information about file headers, symbol tables,
27 relocations, debugging directives and more.
28
29 The flow of execution is as follows:
30
31 1. Command line arguments are checked for control switches and the
32 information to be displayed is selected.
33
34 2. Any remaining arguments are assumed to be object files, and they are
35 processed in order by display_bfd(). If the file is an archive each
36 of its elements is processed in turn.
37
38 3. The file's target architecture and binary file format are determined
39 by bfd_check_format(). If they are recognised, then dump_bfd() is
40 called.
41
42 4. dump_bfd() in turn calls separate functions to display the requested
43 item(s) of information(s). For example disassemble_data() is called if
44 a disassembly has been requested.
45
46 When disassembling the code loops through blocks of instructions bounded
47 by symbols, calling disassemble_bytes() on each block. The actual
48 disassembling is done by the libopcodes library, via a function pointer
49 supplied by the disassembler() function. */
50
51 #include "sysdep.h"
52 #include "bfd.h"
53 #include "elf-bfd.h"
54 #include "coff-bfd.h"
55 #include "progress.h"
56 #include "bucomm.h"
57 #include "elfcomm.h"
58 #include "dwarf.h"
59 #include "ctf-api.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 #include "objdump.h"
69
70 #ifdef HAVE_MMAP
71 #include <sys/mman.h>
72 #endif
73
74 /* Internal headers for the ELF .stab-dump code - sorry. */
75 #define BYTES_IN_WORD 32
76 #include "aout/aout64.h"
77
78 /* Exit status. */
79 static int exit_status = 0;
80
81 static char *default_target = NULL; /* Default at runtime. */
82
83 /* The following variables are set based on arguments passed on the
84 command line. */
85 static int show_version = 0; /* Show the version number. */
86 static int dump_section_contents; /* -s */
87 static int dump_section_headers; /* -h */
88 static bfd_boolean dump_file_header; /* -f */
89 static int dump_symtab; /* -t */
90 static int dump_dynamic_symtab; /* -T */
91 static int dump_reloc_info; /* -r */
92 static int dump_dynamic_reloc_info; /* -R */
93 static int dump_ar_hdrs; /* -a */
94 static int dump_private_headers; /* -p */
95 static char *dump_private_options; /* -P */
96 static int prefix_addresses; /* --prefix-addresses */
97 static int with_line_numbers; /* -l */
98 static bfd_boolean with_source_code; /* -S */
99 static int show_raw_insn; /* --show-raw-insn */
100 static int dump_dwarf_section_info; /* --dwarf */
101 static int dump_stab_section_info; /* --stabs */
102 static int dump_ctf_section_info; /* --ctf */
103 static char *dump_ctf_section_name;
104 static char *dump_ctf_parent_name; /* --ctf-parent */
105 static int do_demangle; /* -C, --demangle */
106 static bfd_boolean disassemble; /* -d */
107 static bfd_boolean disassemble_all; /* -D */
108 static int disassemble_zeroes; /* --disassemble-zeroes */
109 static bfd_boolean formats_info; /* -i */
110 static int wide_output; /* -w */
111 static int insn_width; /* --insn-width */
112 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
113 static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
114 static int dump_debugging; /* --debugging */
115 static int dump_debugging_tags; /* --debugging-tags */
116 static int suppress_bfd_header;
117 static int dump_special_syms = 0; /* --special-syms */
118 static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
119 static int file_start_context = 0; /* --file-start-context */
120 static bfd_boolean display_file_offsets;/* -F */
121 static const char *prefix; /* --prefix */
122 static int prefix_strip; /* --prefix-strip */
123 static size_t prefix_length;
124 static bfd_boolean unwind_inlines; /* --inlines. */
125 static const char * disasm_sym; /* Disassembly start symbol. */
126
127 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
128
129 /* A structure to record the sections mentioned in -j switches. */
130 struct only
131 {
132 const char * name; /* The name of the section. */
133 bfd_boolean seen; /* A flag to indicate that the section has been found in one or more input files. */
134 struct only * next; /* Pointer to the next structure in the list. */
135 };
136 /* Pointer to an array of 'only' structures.
137 This pointer is NULL if the -j switch has not been used. */
138 static struct only * only_list = NULL;
139
140 /* Variables for handling include file path table. */
141 static const char **include_paths;
142 static int include_path_count;
143
144 /* Extra info to pass to the section disassembler and address printing
145 function. */
146 struct objdump_disasm_info
147 {
148 bfd * abfd;
149 asection * sec;
150 bfd_boolean require_sec;
151 arelent ** dynrelbuf;
152 long dynrelcount;
153 disassembler_ftype disassemble_fn;
154 arelent * reloc;
155 const char * symbol;
156 };
157
158 /* Architecture to disassemble for, or default if NULL. */
159 static char *machine = NULL;
160
161 /* Target specific options to the disassembler. */
162 static char *disassembler_options = NULL;
163
164 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
165 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
166
167 /* The symbol table. */
168 static asymbol **syms;
169
170 /* Number of symbols in `syms'. */
171 static long symcount = 0;
172
173 /* The sorted symbol table. */
174 static asymbol **sorted_syms;
175
176 /* Number of symbols in `sorted_syms'. */
177 static long sorted_symcount = 0;
178
179 /* The dynamic symbol table. */
180 static asymbol **dynsyms;
181
182 /* The synthetic symbol table. */
183 static asymbol *synthsyms;
184 static long synthcount = 0;
185
186 /* Number of symbols in `dynsyms'. */
187 static long dynsymcount = 0;
188
189 static bfd_byte *stabs;
190 static bfd_size_type stab_size;
191
192 static bfd_byte *strtab;
193 static bfd_size_type stabstr_size;
194
195 /* Handlers for -P/--private. */
196 static const struct objdump_private_desc * const objdump_private_vectors[] =
197 {
198 OBJDUMP_PRIVATE_VECTORS
199 NULL
200 };
201 \f
202 static void usage (FILE *, int) ATTRIBUTE_NORETURN;
203 static void
204 usage (FILE *stream, int status)
205 {
206 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
207 fprintf (stream, _(" Display information from object <file(s)>.\n"));
208 fprintf (stream, _(" At least one of the following switches must be given:\n"));
209 fprintf (stream, _("\
210 -a, --archive-headers Display archive header information\n\
211 -f, --file-headers Display the contents of the overall file header\n\
212 -p, --private-headers Display object format specific file header contents\n\
213 -P, --private=OPT,OPT... Display object format specific contents\n\
214 -h, --[section-]headers Display the contents of the section headers\n\
215 -x, --all-headers Display the contents of all headers\n\
216 -d, --disassemble Display assembler contents of executable sections\n\
217 -D, --disassemble-all Display assembler contents of all sections\n\
218 --disassemble=<sym> Display assembler contents from <sym>\n\
219 -S, --source Intermix source code with disassembly\n\
220 -s, --full-contents Display the full contents of all sections requested\n\
221 -g, --debugging Display debug information in object file\n\
222 -e, --debugging-tags Display debug information using ctags style\n\
223 -G, --stabs Display (in raw form) any STABS info in the file\n\
224 -W[lLiaprmfFsoRtUuTgAckK] or\n\
225 --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
226 =frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
227 =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
228 =addr,=cu_index,=links,=follow-links]\n\
229 Display DWARF info in the file\n\
230 --ctf=SECTION Display CTF info from SECTION\n\
231 -t, --syms Display the contents of the symbol table(s)\n\
232 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
233 -r, --reloc Display the relocation entries in the file\n\
234 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
235 @<file> Read options from <file>\n\
236 -v, --version Display this program's version number\n\
237 -i, --info List object formats and architectures supported\n\
238 -H, --help Display this information\n\
239 "));
240 if (status != 2)
241 {
242 const struct objdump_private_desc * const *desc;
243
244 fprintf (stream, _("\n The following switches are optional:\n"));
245 fprintf (stream, _("\
246 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
247 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
248 -j, --section=NAME Only display information for section NAME\n\
249 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
250 -EB --endian=big Assume big endian format when disassembling\n\
251 -EL --endian=little Assume little endian format when disassembling\n\
252 --file-start-context Include context from start of file (with -S)\n\
253 -I, --include=DIR Add DIR to search list for source files\n\
254 -l, --line-numbers Include line numbers and filenames in output\n\
255 -F, --file-offsets Include file offsets when displaying information\n\
256 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
257 The STYLE, if specified, can be `auto', `gnu',\n\
258 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
259 or `gnat'\n\
260 --recurse-limit Enable a limit on recursion whilst demangling. [Default]\n\
261 --no-recurse-limit Disable a limit on recursion whilst demangling\n\
262 -w, --wide Format output for more than 80 columns\n\
263 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
264 --start-address=ADDR Only process data whose address is >= ADDR\n\
265 --stop-address=ADDR Only process data whose address is <= ADDR\n\
266 --prefix-addresses Print complete address alongside disassembly\n\
267 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
268 --insn-width=WIDTH Display WIDTH bytes on a single line for -d\n\
269 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
270 --special-syms Include special symbols in symbol dumps\n\
271 --inlines Print all inlines for source line (with -l)\n\
272 --prefix=PREFIX Add PREFIX to absolute paths for -S\n\
273 --prefix-strip=LEVEL Strip initial directory names for -S\n"));
274 fprintf (stream, _("\
275 --dwarf-depth=N Do not display DIEs at depth N or greater\n\
276 --dwarf-start=N Display DIEs starting with N, at the same depth\n\
277 or deeper\n\
278 --dwarf-check Make additional dwarf internal consistency checks.\
279 \n\
280 --ctf-parent=SECTION Use SECTION as the CTF parent\n\n"));
281 list_supported_targets (program_name, stream);
282 list_supported_architectures (program_name, stream);
283
284 disassembler_usage (stream);
285
286 if (objdump_private_vectors[0] != NULL)
287 {
288 fprintf (stream,
289 _("\nOptions supported for -P/--private switch:\n"));
290 for (desc = objdump_private_vectors; *desc != NULL; desc++)
291 (*desc)->help (stream);
292 }
293 }
294 if (REPORT_BUGS_TO[0] && status == 0)
295 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
296 exit (status);
297 }
298
299 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
300 enum option_values
301 {
302 OPTION_ENDIAN=150,
303 OPTION_START_ADDRESS,
304 OPTION_STOP_ADDRESS,
305 OPTION_DWARF,
306 OPTION_PREFIX,
307 OPTION_PREFIX_STRIP,
308 OPTION_INSN_WIDTH,
309 OPTION_ADJUST_VMA,
310 OPTION_DWARF_DEPTH,
311 OPTION_DWARF_CHECK,
312 OPTION_DWARF_START,
313 OPTION_RECURSE_LIMIT,
314 OPTION_NO_RECURSE_LIMIT,
315 OPTION_INLINES,
316 OPTION_CTF,
317 OPTION_CTF_PARENT
318 };
319
320 static struct option long_options[]=
321 {
322 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
323 {"all-headers", no_argument, NULL, 'x'},
324 {"private-headers", no_argument, NULL, 'p'},
325 {"private", required_argument, NULL, 'P'},
326 {"architecture", required_argument, NULL, 'm'},
327 {"archive-headers", no_argument, NULL, 'a'},
328 {"debugging", no_argument, NULL, 'g'},
329 {"debugging-tags", no_argument, NULL, 'e'},
330 {"demangle", optional_argument, NULL, 'C'},
331 {"disassemble", optional_argument, NULL, 'd'},
332 {"disassemble-all", no_argument, NULL, 'D'},
333 {"disassembler-options", required_argument, NULL, 'M'},
334 {"disassemble-zeroes", no_argument, NULL, 'z'},
335 {"dynamic-reloc", no_argument, NULL, 'R'},
336 {"dynamic-syms", no_argument, NULL, 'T'},
337 {"endian", required_argument, NULL, OPTION_ENDIAN},
338 {"file-headers", no_argument, NULL, 'f'},
339 {"file-offsets", no_argument, NULL, 'F'},
340 {"file-start-context", no_argument, &file_start_context, 1},
341 {"full-contents", no_argument, NULL, 's'},
342 {"headers", no_argument, NULL, 'h'},
343 {"help", no_argument, NULL, 'H'},
344 {"info", no_argument, NULL, 'i'},
345 {"line-numbers", no_argument, NULL, 'l'},
346 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
347 {"prefix-addresses", no_argument, &prefix_addresses, 1},
348 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
349 {"recursion-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
350 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
351 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
352 {"reloc", no_argument, NULL, 'r'},
353 {"section", required_argument, NULL, 'j'},
354 {"section-headers", no_argument, NULL, 'h'},
355 {"show-raw-insn", no_argument, &show_raw_insn, 1},
356 {"source", no_argument, NULL, 'S'},
357 {"special-syms", no_argument, &dump_special_syms, 1},
358 {"include", required_argument, NULL, 'I'},
359 {"dwarf", optional_argument, NULL, OPTION_DWARF},
360 {"ctf", required_argument, NULL, OPTION_CTF},
361 {"ctf-parent", required_argument, NULL, OPTION_CTF_PARENT},
362 {"stabs", no_argument, NULL, 'G'},
363 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
364 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
365 {"syms", no_argument, NULL, 't'},
366 {"target", required_argument, NULL, 'b'},
367 {"version", no_argument, NULL, 'V'},
368 {"wide", no_argument, NULL, 'w'},
369 {"prefix", required_argument, NULL, OPTION_PREFIX},
370 {"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
371 {"insn-width", required_argument, NULL, OPTION_INSN_WIDTH},
372 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
373 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
374 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
375 {"inlines", no_argument, 0, OPTION_INLINES},
376 {0, no_argument, 0, 0}
377 };
378 \f
379 static void
380 nonfatal (const char *msg)
381 {
382 bfd_nonfatal (msg);
383 exit_status = 1;
384 }
385
386 /* Returns a version of IN with any control characters
387 replaced by escape sequences. Uses a static buffer
388 if necessary. */
389
390 static const char *
391 sanitize_string (const char * in)
392 {
393 static char * buffer = NULL;
394 static size_t buffer_len = 0;
395 const char * original = in;
396 char * out;
397
398 /* Paranoia. */
399 if (in == NULL)
400 return "";
401
402 /* See if any conversion is necessary. In the majority
403 of cases it will not be needed. */
404 do
405 {
406 char c = *in++;
407
408 if (c == 0)
409 return original;
410
411 if (ISCNTRL (c))
412 break;
413 }
414 while (1);
415
416 /* Copy the input, translating as needed. */
417 in = original;
418 if (buffer_len < (strlen (in) * 2))
419 {
420 free ((void *) buffer);
421 buffer_len = strlen (in) * 2;
422 buffer = xmalloc (buffer_len + 1);
423 }
424
425 out = buffer;
426 do
427 {
428 char c = *in++;
429
430 if (c == 0)
431 break;
432
433 if (!ISCNTRL (c))
434 *out++ = c;
435 else
436 {
437 *out++ = '^';
438 *out++ = c + 0x40;
439 }
440 }
441 while (1);
442
443 *out = 0;
444 return buffer;
445 }
446
447 \f
448 /* Returns TRUE if the specified section should be dumped. */
449
450 static bfd_boolean
451 process_section_p (asection * section)
452 {
453 struct only * only;
454
455 if (only_list == NULL)
456 return TRUE;
457
458 for (only = only_list; only; only = only->next)
459 if (strcmp (only->name, section->name) == 0)
460 {
461 only->seen = TRUE;
462 return TRUE;
463 }
464
465 return FALSE;
466 }
467
468 /* Add an entry to the 'only' list. */
469
470 static void
471 add_only (char * name)
472 {
473 struct only * only;
474
475 /* First check to make sure that we do not
476 already have an entry for this name. */
477 for (only = only_list; only; only = only->next)
478 if (strcmp (only->name, name) == 0)
479 return;
480
481 only = xmalloc (sizeof * only);
482 only->name = name;
483 only->seen = FALSE;
484 only->next = only_list;
485 only_list = only;
486 }
487
488 /* Release the memory used by the 'only' list.
489 PR 11225: Issue a warning message for unseen sections.
490 Only do this if none of the sections were seen. This is mainly to support
491 tools like the GAS testsuite where an object file is dumped with a list of
492 generic section names known to be present in a range of different file
493 formats. */
494
495 static void
496 free_only_list (void)
497 {
498 bfd_boolean at_least_one_seen = FALSE;
499 struct only * only;
500 struct only * next;
501
502 if (only_list == NULL)
503 return;
504
505 for (only = only_list; only; only = only->next)
506 if (only->seen)
507 {
508 at_least_one_seen = TRUE;
509 break;
510 }
511
512 for (only = only_list; only; only = next)
513 {
514 if (! at_least_one_seen)
515 {
516 non_fatal (_("section '%s' mentioned in a -j option, "
517 "but not found in any input file"),
518 only->name);
519 exit_status = 1;
520 }
521 next = only->next;
522 free (only);
523 }
524 }
525
526 \f
527 static void
528 dump_section_header (bfd *abfd, asection *section, void *data)
529 {
530 char *comma = "";
531 unsigned int opb = bfd_octets_per_byte (abfd);
532 int longest_section_name = *((int *) data);
533
534 /* Ignore linker created section. See elfNN_ia64_object_p in
535 bfd/elfxx-ia64.c. */
536 if (section->flags & SEC_LINKER_CREATED)
537 return;
538
539 /* PR 10413: Skip sections that we are ignoring. */
540 if (! process_section_p (section))
541 return;
542
543 printf ("%3d %-*s %08lx ", section->index, longest_section_name,
544 sanitize_string (bfd_get_section_name (abfd, section)),
545 (unsigned long) bfd_section_size (abfd, section) / opb);
546 bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
547 printf (" ");
548 bfd_printf_vma (abfd, section->lma);
549 printf (" %08lx 2**%u", (unsigned long) section->filepos,
550 bfd_get_section_alignment (abfd, section));
551 if (! wide_output)
552 printf ("\n ");
553 printf (" ");
554
555 #define PF(x, y) \
556 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
557
558 PF (SEC_HAS_CONTENTS, "CONTENTS");
559 PF (SEC_ALLOC, "ALLOC");
560 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
561 PF (SEC_LOAD, "LOAD");
562 PF (SEC_RELOC, "RELOC");
563 PF (SEC_READONLY, "READONLY");
564 PF (SEC_CODE, "CODE");
565 PF (SEC_DATA, "DATA");
566 PF (SEC_ROM, "ROM");
567 PF (SEC_DEBUGGING, "DEBUGGING");
568 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
569 PF (SEC_EXCLUDE, "EXCLUDE");
570 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
571 if (bfd_get_arch (abfd) == bfd_arch_tic54x)
572 {
573 PF (SEC_TIC54X_BLOCK, "BLOCK");
574 PF (SEC_TIC54X_CLINK, "CLINK");
575 }
576 PF (SEC_SMALL_DATA, "SMALL_DATA");
577 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
578 {
579 PF (SEC_COFF_SHARED, "SHARED");
580 PF (SEC_COFF_NOREAD, "NOREAD");
581 }
582 else if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
583 PF (SEC_ELF_PURECODE, "PURECODE");
584 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
585 PF (SEC_GROUP, "GROUP");
586 if (bfd_get_arch (abfd) == bfd_arch_mep)
587 {
588 PF (SEC_MEP_VLIW, "VLIW");
589 }
590
591 if ((section->flags & SEC_LINK_ONCE) != 0)
592 {
593 const char *ls;
594 struct coff_comdat_info *comdat;
595
596 switch (section->flags & SEC_LINK_DUPLICATES)
597 {
598 default:
599 abort ();
600 case SEC_LINK_DUPLICATES_DISCARD:
601 ls = "LINK_ONCE_DISCARD";
602 break;
603 case SEC_LINK_DUPLICATES_ONE_ONLY:
604 ls = "LINK_ONCE_ONE_ONLY";
605 break;
606 case SEC_LINK_DUPLICATES_SAME_SIZE:
607 ls = "LINK_ONCE_SAME_SIZE";
608 break;
609 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
610 ls = "LINK_ONCE_SAME_CONTENTS";
611 break;
612 }
613 printf ("%s%s", comma, ls);
614
615 comdat = bfd_coff_get_comdat_section (abfd, section);
616 if (comdat != NULL)
617 printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
618
619 comma = ", ";
620 }
621
622 printf ("\n");
623 #undef PF
624 }
625
626 /* Called on each SECTION in ABFD, update the int variable pointed to by
627 DATA which contains the string length of the longest section name. */
628
629 static void
630 find_longest_section_name (bfd *abfd, asection *section, void *data)
631 {
632 int *longest_so_far = (int *) data;
633 const char *name;
634 int len;
635
636 /* Ignore linker created section. */
637 if (section->flags & SEC_LINKER_CREATED)
638 return;
639
640 /* Skip sections that we are ignoring. */
641 if (! process_section_p (section))
642 return;
643
644 name = bfd_get_section_name (abfd, section);
645 len = (int) strlen (name);
646 if (len > *longest_so_far)
647 *longest_so_far = len;
648 }
649
650 static void
651 dump_headers (bfd *abfd)
652 {
653 /* The default width of 13 is just an arbitrary choice. */
654 int max_section_name_length = 13;
655 int bfd_vma_width;
656
657 #ifndef BFD64
658 bfd_vma_width = 10;
659 #else
660 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
661 if (bfd_get_arch_size (abfd) == 32)
662 bfd_vma_width = 10;
663 else
664 bfd_vma_width = 18;
665 #endif
666
667 printf (_("Sections:\n"));
668
669 if (wide_output)
670 bfd_map_over_sections (abfd, find_longest_section_name,
671 &max_section_name_length);
672
673 printf (_("Idx %-*s Size %-*s%-*sFile off Algn"),
674 max_section_name_length, "Name",
675 bfd_vma_width, "VMA",
676 bfd_vma_width, "LMA");
677
678 if (wide_output)
679 printf (_(" Flags"));
680 printf ("\n");
681
682 bfd_map_over_sections (abfd, dump_section_header,
683 &max_section_name_length);
684 }
685 \f
686 static asymbol **
687 slurp_symtab (bfd *abfd)
688 {
689 asymbol **sy = NULL;
690 long storage;
691
692 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
693 {
694 symcount = 0;
695 return NULL;
696 }
697
698 storage = bfd_get_symtab_upper_bound (abfd);
699 if (storage < 0)
700 {
701 non_fatal (_("failed to read symbol table from: %s"), bfd_get_filename (abfd));
702 bfd_fatal (_("error message was"));
703 }
704 if (storage)
705 {
706 off_t filesize = bfd_get_file_size (abfd);
707
708 /* qv PR 24707. */
709 if (filesize > 0
710 && filesize < storage
711 /* The MMO file format supports its own special compression
712 technique, so its sections can be larger than the file size. */
713 && bfd_get_flavour (abfd) != bfd_target_mmo_flavour)
714 {
715 bfd_nonfatal_message (bfd_get_filename (abfd), abfd, NULL,
716 _("error: symbol table size (%#lx) is larger than filesize (%#lx)"),
717 storage, (long) filesize);
718 exit_status = 1;
719 symcount = 0;
720 return NULL;
721 }
722
723 sy = (asymbol **) xmalloc (storage);
724 }
725
726 symcount = bfd_canonicalize_symtab (abfd, sy);
727 if (symcount < 0)
728 bfd_fatal (bfd_get_filename (abfd));
729 return sy;
730 }
731
732 /* Read in the dynamic symbols. */
733
734 static asymbol **
735 slurp_dynamic_symtab (bfd *abfd)
736 {
737 asymbol **sy = NULL;
738 long storage;
739
740 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
741 if (storage < 0)
742 {
743 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
744 {
745 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
746 exit_status = 1;
747 dynsymcount = 0;
748 return NULL;
749 }
750
751 bfd_fatal (bfd_get_filename (abfd));
752 }
753 if (storage)
754 sy = (asymbol **) xmalloc (storage);
755
756 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
757 if (dynsymcount < 0)
758 bfd_fatal (bfd_get_filename (abfd));
759 return sy;
760 }
761
762 /* Some symbol names are significant and should be kept in the
763 table of sorted symbol names, even if they are marked as
764 debugging/section symbols. */
765
766 static bfd_boolean
767 is_significant_symbol_name (const char * name)
768 {
769 return strncmp (name, ".plt", 4) == 0 || strcmp (name, ".got") == 0;
770 }
771
772 /* Filter out (in place) symbols that are useless for disassembly.
773 COUNT is the number of elements in SYMBOLS.
774 Return the number of useful symbols. */
775
776 static long
777 remove_useless_symbols (asymbol **symbols, long count)
778 {
779 asymbol **in_ptr = symbols, **out_ptr = symbols;
780
781 while (--count >= 0)
782 {
783 asymbol *sym = *in_ptr++;
784
785 if (sym->name == NULL || sym->name[0] == '\0')
786 continue;
787 if ((sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
788 && ! is_significant_symbol_name (sym->name))
789 continue;
790 if (bfd_is_und_section (sym->section)
791 || bfd_is_com_section (sym->section))
792 continue;
793
794 *out_ptr++ = sym;
795 }
796 return out_ptr - symbols;
797 }
798
799 /* Sort symbols into value order. */
800
801 static int
802 compare_symbols (const void *ap, const void *bp)
803 {
804 const asymbol *a = * (const asymbol **) ap;
805 const asymbol *b = * (const asymbol **) bp;
806 const char *an;
807 const char *bn;
808 size_t anl;
809 size_t bnl;
810 bfd_boolean af;
811 bfd_boolean bf;
812 flagword aflags;
813 flagword bflags;
814
815 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
816 return 1;
817 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
818 return -1;
819
820 if (a->section > b->section)
821 return 1;
822 else if (a->section < b->section)
823 return -1;
824
825 an = bfd_asymbol_name (a);
826 bn = bfd_asymbol_name (b);
827 anl = strlen (an);
828 bnl = strlen (bn);
829
830 /* The symbols gnu_compiled and gcc2_compiled convey no real
831 information, so put them after other symbols with the same value. */
832 af = (strstr (an, "gnu_compiled") != NULL
833 || strstr (an, "gcc2_compiled") != NULL);
834 bf = (strstr (bn, "gnu_compiled") != NULL
835 || strstr (bn, "gcc2_compiled") != NULL);
836
837 if (af && ! bf)
838 return 1;
839 if (! af && bf)
840 return -1;
841
842 /* We use a heuristic for the file name, to try to sort it after
843 more useful symbols. It may not work on non Unix systems, but it
844 doesn't really matter; the only difference is precisely which
845 symbol names get printed. */
846
847 #define file_symbol(s, sn, snl) \
848 (((s)->flags & BSF_FILE) != 0 \
849 || ((sn)[(snl) - 2] == '.' \
850 && ((sn)[(snl) - 1] == 'o' \
851 || (sn)[(snl) - 1] == 'a')))
852
853 af = file_symbol (a, an, anl);
854 bf = file_symbol (b, bn, bnl);
855
856 if (af && ! bf)
857 return 1;
858 if (! af && bf)
859 return -1;
860
861 /* Try to sort global symbols before local symbols before function
862 symbols before debugging symbols. */
863
864 aflags = a->flags;
865 bflags = b->flags;
866
867 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
868 {
869 if ((aflags & BSF_DEBUGGING) != 0)
870 return 1;
871 else
872 return -1;
873 }
874 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
875 {
876 if ((aflags & BSF_FUNCTION) != 0)
877 return -1;
878 else
879 return 1;
880 }
881 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
882 {
883 if ((aflags & BSF_LOCAL) != 0)
884 return 1;
885 else
886 return -1;
887 }
888 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
889 {
890 if ((aflags & BSF_GLOBAL) != 0)
891 return -1;
892 else
893 return 1;
894 }
895
896 if (bfd_get_flavour (bfd_asymbol_bfd (a)) == bfd_target_elf_flavour
897 && bfd_get_flavour (bfd_asymbol_bfd (b)) == bfd_target_elf_flavour)
898 {
899 bfd_vma asz, bsz;
900
901 asz = 0;
902 if ((a->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
903 asz = ((elf_symbol_type *) a)->internal_elf_sym.st_size;
904 bsz = 0;
905 if ((b->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
906 bsz = ((elf_symbol_type *) b)->internal_elf_sym.st_size;
907 if (asz != bsz)
908 return asz > bsz ? -1 : 1;
909 }
910
911 /* Symbols that start with '.' might be section names, so sort them
912 after symbols that don't start with '.'. */
913 if (an[0] == '.' && bn[0] != '.')
914 return 1;
915 if (an[0] != '.' && bn[0] == '.')
916 return -1;
917
918 /* Finally, if we can't distinguish them in any other way, try to
919 get consistent results by sorting the symbols by name. */
920 return strcmp (an, bn);
921 }
922
923 /* Sort relocs into address order. */
924
925 static int
926 compare_relocs (const void *ap, const void *bp)
927 {
928 const arelent *a = * (const arelent **) ap;
929 const arelent *b = * (const arelent **) bp;
930
931 if (a->address > b->address)
932 return 1;
933 else if (a->address < b->address)
934 return -1;
935
936 /* So that associated relocations tied to the same address show up
937 in the correct order, we don't do any further sorting. */
938 if (a > b)
939 return 1;
940 else if (a < b)
941 return -1;
942 else
943 return 0;
944 }
945
946 /* Print an address (VMA) to the output stream in INFO.
947 If SKIP_ZEROES is TRUE, omit leading zeroes. */
948
949 static void
950 objdump_print_value (bfd_vma vma, struct disassemble_info *inf,
951 bfd_boolean skip_zeroes)
952 {
953 char buf[30];
954 char *p;
955 struct objdump_disasm_info *aux;
956
957 aux = (struct objdump_disasm_info *) inf->application_data;
958 bfd_sprintf_vma (aux->abfd, buf, vma);
959 if (! skip_zeroes)
960 p = buf;
961 else
962 {
963 for (p = buf; *p == '0'; ++p)
964 ;
965 if (*p == '\0')
966 --p;
967 }
968 (*inf->fprintf_func) (inf->stream, "%s", p);
969 }
970
971 /* Print the name of a symbol. */
972
973 static void
974 objdump_print_symname (bfd *abfd, struct disassemble_info *inf,
975 asymbol *sym)
976 {
977 char *alloc;
978 const char *name, *version_string = NULL;
979 bfd_boolean hidden = FALSE;
980
981 alloc = NULL;
982 name = bfd_asymbol_name (sym);
983 if (do_demangle && name[0] != '\0')
984 {
985 /* Demangle the name. */
986 alloc = bfd_demangle (abfd, name, demangle_flags);
987 if (alloc != NULL)
988 name = alloc;
989 }
990
991 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
992 version_string = bfd_get_symbol_version_string (abfd, sym, &hidden);
993
994 if (bfd_is_und_section (bfd_get_section (sym)))
995 hidden = TRUE;
996
997 name = sanitize_string (name);
998
999 if (inf != NULL)
1000 {
1001 (*inf->fprintf_func) (inf->stream, "%s", name);
1002 if (version_string && *version_string != '\0')
1003 (*inf->fprintf_func) (inf->stream, hidden ? "@%s" : "@@%s",
1004 version_string);
1005 }
1006 else
1007 {
1008 printf ("%s", name);
1009 if (version_string && *version_string != '\0')
1010 printf (hidden ? "@%s" : "@@%s", version_string);
1011 }
1012
1013 if (alloc != NULL)
1014 free (alloc);
1015 }
1016
1017 static inline bfd_boolean
1018 sym_ok (bfd_boolean want_section,
1019 bfd * abfd ATTRIBUTE_UNUSED,
1020 long place,
1021 asection * sec,
1022 struct disassemble_info * inf)
1023 {
1024 if (want_section)
1025 {
1026 /* Note - we cannot just compare section pointers because they could
1027 be different, but the same... Ie the symbol that we are trying to
1028 find could have come from a separate debug info file. Under such
1029 circumstances the symbol will be associated with a section in the
1030 debug info file, whilst the section we want is in a normal file.
1031 So the section pointers will be different, but the section names
1032 will be the same. */
1033 if (strcmp (bfd_section_name (abfd, sorted_syms[place]->section),
1034 bfd_section_name (abfd, sec)) != 0)
1035 return FALSE;
1036 }
1037
1038 return inf->symbol_is_valid (sorted_syms[place], inf);
1039 }
1040
1041 /* Locate a symbol given a bfd and a section (from INFO->application_data),
1042 and a VMA. If INFO->application_data->require_sec is TRUE, then always
1043 require the symbol to be in the section. Returns NULL if there is no
1044 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
1045 of the symbol in sorted_syms. */
1046
1047 static asymbol *
1048 find_symbol_for_address (bfd_vma vma,
1049 struct disassemble_info *inf,
1050 long *place)
1051 {
1052 /* @@ Would it speed things up to cache the last two symbols returned,
1053 and maybe their address ranges? For many processors, only one memory
1054 operand can be present at a time, so the 2-entry cache wouldn't be
1055 constantly churned by code doing heavy memory accesses. */
1056
1057 /* Indices in `sorted_syms'. */
1058 long min = 0;
1059 long max_count = sorted_symcount;
1060 long thisplace;
1061 struct objdump_disasm_info *aux;
1062 bfd *abfd;
1063 asection *sec;
1064 unsigned int opb;
1065 bfd_boolean want_section;
1066 long rel_count;
1067
1068 if (sorted_symcount < 1)
1069 return NULL;
1070
1071 aux = (struct objdump_disasm_info *) inf->application_data;
1072 abfd = aux->abfd;
1073 sec = aux->sec;
1074 opb = inf->octets_per_byte;
1075
1076 /* Perform a binary search looking for the closest symbol to the
1077 required value. We are searching the range (min, max_count]. */
1078 while (min + 1 < max_count)
1079 {
1080 asymbol *sym;
1081
1082 thisplace = (max_count + min) / 2;
1083 sym = sorted_syms[thisplace];
1084
1085 if (bfd_asymbol_value (sym) > vma)
1086 max_count = thisplace;
1087 else if (bfd_asymbol_value (sym) < vma)
1088 min = thisplace;
1089 else
1090 {
1091 min = thisplace;
1092 break;
1093 }
1094 }
1095
1096 /* The symbol we want is now in min, the low end of the range we
1097 were searching. If there are several symbols with the same
1098 value, we want the first (non-section/non-debugging) one. */
1099 thisplace = min;
1100 while (thisplace > 0
1101 && (bfd_asymbol_value (sorted_syms[thisplace])
1102 == bfd_asymbol_value (sorted_syms[thisplace - 1]))
1103 && ((sorted_syms[thisplace - 1]->flags
1104 & (BSF_SECTION_SYM | BSF_DEBUGGING)) == 0)
1105 )
1106 --thisplace;
1107
1108 /* Prefer a symbol in the current section if we have multple symbols
1109 with the same value, as can occur with overlays or zero size
1110 sections. */
1111 min = thisplace;
1112 while (min < max_count
1113 && (bfd_asymbol_value (sorted_syms[min])
1114 == bfd_asymbol_value (sorted_syms[thisplace])))
1115 {
1116 if (sym_ok (TRUE, abfd, min, sec, inf))
1117 {
1118 thisplace = min;
1119
1120 if (place != NULL)
1121 *place = thisplace;
1122
1123 return sorted_syms[thisplace];
1124 }
1125 ++min;
1126 }
1127
1128 /* If the file is relocatable, and the symbol could be from this
1129 section, prefer a symbol from this section over symbols from
1130 others, even if the other symbol's value might be closer.
1131
1132 Note that this may be wrong for some symbol references if the
1133 sections have overlapping memory ranges, but in that case there's
1134 no way to tell what's desired without looking at the relocation
1135 table.
1136
1137 Also give the target a chance to reject symbols. */
1138 want_section = (aux->require_sec
1139 || ((abfd->flags & HAS_RELOC) != 0
1140 && vma >= bfd_get_section_vma (abfd, sec)
1141 && vma < (bfd_get_section_vma (abfd, sec)
1142 + bfd_section_size (abfd, sec) / opb)));
1143
1144 if (! sym_ok (want_section, abfd, thisplace, sec, inf))
1145 {
1146 long i;
1147 long newplace = sorted_symcount;
1148
1149 for (i = min - 1; i >= 0; i--)
1150 {
1151 if (sym_ok (want_section, abfd, i, sec, inf))
1152 {
1153 if (newplace == sorted_symcount)
1154 newplace = i;
1155
1156 if (bfd_asymbol_value (sorted_syms[i])
1157 != bfd_asymbol_value (sorted_syms[newplace]))
1158 break;
1159
1160 /* Remember this symbol and keep searching until we reach
1161 an earlier address. */
1162 newplace = i;
1163 }
1164 }
1165
1166 if (newplace != sorted_symcount)
1167 thisplace = newplace;
1168 else
1169 {
1170 /* We didn't find a good symbol with a smaller value.
1171 Look for one with a larger value. */
1172 for (i = thisplace + 1; i < sorted_symcount; i++)
1173 {
1174 if (sym_ok (want_section, abfd, i, sec, inf))
1175 {
1176 thisplace = i;
1177 break;
1178 }
1179 }
1180 }
1181
1182 if (! sym_ok (want_section, abfd, thisplace, sec, inf))
1183 /* There is no suitable symbol. */
1184 return NULL;
1185 }
1186
1187 /* If we have not found an exact match for the specified address
1188 and we have dynamic relocations available, then we can produce
1189 a better result by matching a relocation to the address and
1190 using the symbol associated with that relocation. */
1191 rel_count = aux->dynrelcount;
1192 if (!want_section
1193 && sorted_syms[thisplace]->value != vma
1194 && rel_count > 0
1195 && aux->dynrelbuf != NULL
1196 && aux->dynrelbuf[0]->address <= vma
1197 && aux->dynrelbuf[rel_count - 1]->address >= vma
1198 /* If we have matched a synthetic symbol, then stick with that. */
1199 && (sorted_syms[thisplace]->flags & BSF_SYNTHETIC) == 0)
1200 {
1201 arelent ** rel_low;
1202 arelent ** rel_high;
1203
1204 rel_low = aux->dynrelbuf;
1205 rel_high = rel_low + rel_count - 1;
1206 while (rel_low <= rel_high)
1207 {
1208 arelent **rel_mid = &rel_low[(rel_high - rel_low) / 2];
1209 arelent * rel = *rel_mid;
1210
1211 if (rel->address == vma)
1212 {
1213 /* Absolute relocations do not provide a more helpful
1214 symbolic address. Find a non-absolute relocation
1215 with the same address. */
1216 arelent **rel_vma = rel_mid;
1217 for (rel_mid--;
1218 rel_mid >= rel_low && rel_mid[0]->address == vma;
1219 rel_mid--)
1220 rel_vma = rel_mid;
1221
1222 for (; rel_vma <= rel_high && rel_vma[0]->address == vma;
1223 rel_vma++)
1224 {
1225 rel = *rel_vma;
1226 if (rel->sym_ptr_ptr != NULL
1227 && ! bfd_is_abs_section ((* rel->sym_ptr_ptr)->section))
1228 {
1229 if (place != NULL)
1230 * place = thisplace;
1231 return * rel->sym_ptr_ptr;
1232 }
1233 }
1234 break;
1235 }
1236
1237 if (vma < rel->address)
1238 rel_high = rel_mid;
1239 else if (vma >= rel_mid[1]->address)
1240 rel_low = rel_mid + 1;
1241 else
1242 break;
1243 }
1244 }
1245
1246 if (place != NULL)
1247 *place = thisplace;
1248
1249 return sorted_syms[thisplace];
1250 }
1251
1252 /* Print an address and the offset to the nearest symbol. */
1253
1254 static void
1255 objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
1256 bfd_vma vma, struct disassemble_info *inf,
1257 bfd_boolean skip_zeroes)
1258 {
1259 objdump_print_value (vma, inf, skip_zeroes);
1260
1261 if (sym == NULL)
1262 {
1263 bfd_vma secaddr;
1264
1265 (*inf->fprintf_func) (inf->stream, " <%s",
1266 sanitize_string (bfd_get_section_name (abfd, sec)));
1267 secaddr = bfd_get_section_vma (abfd, sec);
1268 if (vma < secaddr)
1269 {
1270 (*inf->fprintf_func) (inf->stream, "-0x");
1271 objdump_print_value (secaddr - vma, inf, TRUE);
1272 }
1273 else if (vma > secaddr)
1274 {
1275 (*inf->fprintf_func) (inf->stream, "+0x");
1276 objdump_print_value (vma - secaddr, inf, TRUE);
1277 }
1278 (*inf->fprintf_func) (inf->stream, ">");
1279 }
1280 else
1281 {
1282 (*inf->fprintf_func) (inf->stream, " <");
1283
1284 objdump_print_symname (abfd, inf, sym);
1285
1286 if (bfd_asymbol_value (sym) == vma)
1287 ;
1288 /* Undefined symbols in an executables and dynamic objects do not have
1289 a value associated with them, so it does not make sense to display
1290 an offset relative to them. Normally we would not be provided with
1291 this kind of symbol, but the target backend might choose to do so,
1292 and the code in find_symbol_for_address might return an as yet
1293 unresolved symbol associated with a dynamic reloc. */
1294 else if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
1295 && bfd_is_und_section (sym->section))
1296 ;
1297 else if (bfd_asymbol_value (sym) > vma)
1298 {
1299 (*inf->fprintf_func) (inf->stream, "-0x");
1300 objdump_print_value (bfd_asymbol_value (sym) - vma, inf, TRUE);
1301 }
1302 else if (vma > bfd_asymbol_value (sym))
1303 {
1304 (*inf->fprintf_func) (inf->stream, "+0x");
1305 objdump_print_value (vma - bfd_asymbol_value (sym), inf, TRUE);
1306 }
1307
1308 (*inf->fprintf_func) (inf->stream, ">");
1309 }
1310
1311 if (display_file_offsets)
1312 inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
1313 (long int)(sec->filepos + (vma - sec->vma)));
1314 }
1315
1316 /* Print an address (VMA), symbolically if possible.
1317 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
1318
1319 static void
1320 objdump_print_addr (bfd_vma vma,
1321 struct disassemble_info *inf,
1322 bfd_boolean skip_zeroes)
1323 {
1324 struct objdump_disasm_info *aux;
1325 asymbol *sym = NULL;
1326 bfd_boolean skip_find = FALSE;
1327
1328 aux = (struct objdump_disasm_info *) inf->application_data;
1329
1330 if (sorted_symcount < 1)
1331 {
1332 (*inf->fprintf_func) (inf->stream, "0x");
1333 objdump_print_value (vma, inf, skip_zeroes);
1334
1335 if (display_file_offsets)
1336 inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
1337 (long int)(aux->sec->filepos + (vma - aux->sec->vma)));
1338 return;
1339 }
1340
1341 if (aux->reloc != NULL
1342 && aux->reloc->sym_ptr_ptr != NULL
1343 && * aux->reloc->sym_ptr_ptr != NULL)
1344 {
1345 sym = * aux->reloc->sym_ptr_ptr;
1346
1347 /* Adjust the vma to the reloc. */
1348 vma += bfd_asymbol_value (sym);
1349
1350 if (bfd_is_und_section (bfd_get_section (sym)))
1351 skip_find = TRUE;
1352 }
1353
1354 if (!skip_find)
1355 sym = find_symbol_for_address (vma, inf, NULL);
1356
1357 objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, inf,
1358 skip_zeroes);
1359 }
1360
1361 /* Print VMA to INFO. This function is passed to the disassembler
1362 routine. */
1363
1364 static void
1365 objdump_print_address (bfd_vma vma, struct disassemble_info *inf)
1366 {
1367 objdump_print_addr (vma, inf, ! prefix_addresses);
1368 }
1369
1370 /* Determine if the given address has a symbol associated with it. */
1371
1372 static int
1373 objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * inf)
1374 {
1375 asymbol * sym;
1376
1377 sym = find_symbol_for_address (vma, inf, NULL);
1378
1379 return (sym != NULL && (bfd_asymbol_value (sym) == vma));
1380 }
1381
1382 /* Hold the last function name and the last line number we displayed
1383 in a disassembly. */
1384
1385 static char *prev_functionname;
1386 static unsigned int prev_line;
1387 static unsigned int prev_discriminator;
1388
1389 /* We keep a list of all files that we have seen when doing a
1390 disassembly with source, so that we know how much of the file to
1391 display. This can be important for inlined functions. */
1392
1393 struct print_file_list
1394 {
1395 struct print_file_list *next;
1396 const char *filename;
1397 const char *modname;
1398 const char *map;
1399 size_t mapsize;
1400 const char **linemap;
1401 unsigned maxline;
1402 unsigned last_line;
1403 unsigned max_printed;
1404 int first;
1405 };
1406
1407 static struct print_file_list *print_files;
1408
1409 /* The number of preceding context lines to show when we start
1410 displaying a file for the first time. */
1411
1412 #define SHOW_PRECEDING_CONTEXT_LINES (5)
1413
1414 /* Read a complete file into memory. */
1415
1416 static const char *
1417 slurp_file (const char *fn, size_t *size, struct stat *fst)
1418 {
1419 #ifdef HAVE_MMAP
1420 int ps = getpagesize ();
1421 size_t msize;
1422 #endif
1423 const char *map;
1424 int fd = open (fn, O_RDONLY | O_BINARY);
1425
1426 if (fd < 0)
1427 return NULL;
1428 if (fstat (fd, fst) < 0)
1429 {
1430 close (fd);
1431 return NULL;
1432 }
1433 *size = fst->st_size;
1434 #ifdef HAVE_MMAP
1435 msize = (*size + ps - 1) & ~(ps - 1);
1436 map = mmap (NULL, msize, PROT_READ, MAP_SHARED, fd, 0);
1437 if (map != (char *) -1L)
1438 {
1439 close (fd);
1440 return map;
1441 }
1442 #endif
1443 map = (const char *) malloc (*size);
1444 if (!map || (size_t) read (fd, (char *) map, *size) != *size)
1445 {
1446 free ((void *) map);
1447 map = NULL;
1448 }
1449 close (fd);
1450 return map;
1451 }
1452
1453 #define line_map_decrease 5
1454
1455 /* Precompute array of lines for a mapped file. */
1456
1457 static const char **
1458 index_file (const char *map, size_t size, unsigned int *maxline)
1459 {
1460 const char *p, *lstart, *end;
1461 int chars_per_line = 45; /* First iteration will use 40. */
1462 unsigned int lineno;
1463 const char **linemap = NULL;
1464 unsigned long line_map_size = 0;
1465
1466 lineno = 0;
1467 lstart = map;
1468 end = map + size;
1469
1470 for (p = map; p < end; p++)
1471 {
1472 if (*p == '\n')
1473 {
1474 if (p + 1 < end && p[1] == '\r')
1475 p++;
1476 }
1477 else if (*p == '\r')
1478 {
1479 if (p + 1 < end && p[1] == '\n')
1480 p++;
1481 }
1482 else
1483 continue;
1484
1485 /* End of line found. */
1486
1487 if (linemap == NULL || line_map_size < lineno + 1)
1488 {
1489 unsigned long newsize;
1490
1491 chars_per_line -= line_map_decrease;
1492 if (chars_per_line <= 1)
1493 chars_per_line = 1;
1494 line_map_size = size / chars_per_line + 1;
1495 if (line_map_size < lineno + 1)
1496 line_map_size = lineno + 1;
1497 newsize = line_map_size * sizeof (char *);
1498 linemap = (const char **) xrealloc (linemap, newsize);
1499 }
1500
1501 linemap[lineno++] = lstart;
1502 lstart = p + 1;
1503 }
1504
1505 *maxline = lineno;
1506 return linemap;
1507 }
1508
1509 /* Tries to open MODNAME, and if successful adds a node to print_files
1510 linked list and returns that node. Returns NULL on failure. */
1511
1512 static struct print_file_list *
1513 try_print_file_open (const char *origname, const char *modname, struct stat *fst)
1514 {
1515 struct print_file_list *p;
1516
1517 p = (struct print_file_list *) xmalloc (sizeof (struct print_file_list));
1518
1519 p->map = slurp_file (modname, &p->mapsize, fst);
1520 if (p->map == NULL)
1521 {
1522 free (p);
1523 return NULL;
1524 }
1525
1526 p->linemap = index_file (p->map, p->mapsize, &p->maxline);
1527 p->last_line = 0;
1528 p->max_printed = 0;
1529 p->filename = origname;
1530 p->modname = modname;
1531 p->next = print_files;
1532 p->first = 1;
1533 print_files = p;
1534 return p;
1535 }
1536
1537 /* If the source file, as described in the symtab, is not found
1538 try to locate it in one of the paths specified with -I
1539 If found, add location to print_files linked list. */
1540
1541 static struct print_file_list *
1542 update_source_path (const char *filename, bfd *abfd)
1543 {
1544 struct print_file_list *p;
1545 const char *fname;
1546 struct stat fst;
1547 int i;
1548
1549 p = try_print_file_open (filename, filename, &fst);
1550 if (p == NULL)
1551 {
1552 if (include_path_count == 0)
1553 return NULL;
1554
1555 /* Get the name of the file. */
1556 fname = lbasename (filename);
1557
1558 /* If file exists under a new path, we need to add it to the list
1559 so that show_line knows about it. */
1560 for (i = 0; i < include_path_count; i++)
1561 {
1562 char *modname = concat (include_paths[i], "/", fname,
1563 (const char *) 0);
1564
1565 p = try_print_file_open (filename, modname, &fst);
1566 if (p)
1567 break;
1568
1569 free (modname);
1570 }
1571 }
1572
1573 if (p != NULL)
1574 {
1575 long mtime = bfd_get_mtime (abfd);
1576
1577 if (fst.st_mtime > mtime)
1578 warn (_("source file %s is more recent than object file\n"),
1579 filename);
1580 }
1581
1582 return p;
1583 }
1584
1585 /* Print a source file line. */
1586
1587 static void
1588 print_line (struct print_file_list *p, unsigned int linenum)
1589 {
1590 const char *l;
1591 size_t len;
1592
1593 --linenum;
1594 if (linenum >= p->maxline)
1595 return;
1596 l = p->linemap [linenum];
1597 /* Test fwrite return value to quiet glibc warning. */
1598 len = strcspn (l, "\n\r");
1599 if (len == 0 || fwrite (l, len, 1, stdout) == 1)
1600 putchar ('\n');
1601 }
1602
1603 /* Print a range of source code lines. */
1604
1605 static void
1606 dump_lines (struct print_file_list *p, unsigned int start, unsigned int end)
1607 {
1608 if (p->map == NULL)
1609 return;
1610 while (start <= end)
1611 {
1612 print_line (p, start);
1613 start++;
1614 }
1615 }
1616
1617 /* Show the line number, or the source line, in a disassembly
1618 listing. */
1619
1620 static void
1621 show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
1622 {
1623 const char *filename;
1624 const char *functionname;
1625 unsigned int linenumber;
1626 unsigned int discriminator;
1627 bfd_boolean reloc;
1628 char *path = NULL;
1629
1630 if (! with_line_numbers && ! with_source_code)
1631 return;
1632
1633 if (! bfd_find_nearest_line_discriminator (abfd, section, syms, addr_offset,
1634 &filename, &functionname,
1635 &linenumber, &discriminator))
1636 return;
1637
1638 if (filename != NULL && *filename == '\0')
1639 filename = NULL;
1640 if (functionname != NULL && *functionname == '\0')
1641 functionname = NULL;
1642
1643 if (filename
1644 && IS_ABSOLUTE_PATH (filename)
1645 && prefix)
1646 {
1647 char *path_up;
1648 const char *fname = filename;
1649
1650 path = xmalloc (prefix_length + PATH_MAX + 1);
1651
1652 if (prefix_length)
1653 memcpy (path, prefix, prefix_length);
1654 path_up = path + prefix_length;
1655
1656 /* Build relocated filename, stripping off leading directories
1657 from the initial filename if requested. */
1658 if (prefix_strip > 0)
1659 {
1660 int level = 0;
1661 const char *s;
1662
1663 /* Skip selected directory levels. */
1664 for (s = fname + 1; *s != '\0' && level < prefix_strip; s++)
1665 if (IS_DIR_SEPARATOR(*s))
1666 {
1667 fname = s;
1668 level++;
1669 }
1670 }
1671
1672 /* Update complete filename. */
1673 strncpy (path_up, fname, PATH_MAX);
1674 path_up[PATH_MAX] = '\0';
1675
1676 filename = path;
1677 reloc = TRUE;
1678 }
1679 else
1680 reloc = FALSE;
1681
1682 if (with_line_numbers)
1683 {
1684 if (functionname != NULL
1685 && (prev_functionname == NULL
1686 || strcmp (functionname, prev_functionname) != 0))
1687 {
1688 printf ("%s():\n", sanitize_string (functionname));
1689 prev_line = -1;
1690 }
1691 if (linenumber > 0
1692 && (linenumber != prev_line
1693 || discriminator != prev_discriminator))
1694 {
1695 if (discriminator > 0)
1696 printf ("%s:%u (discriminator %u)\n",
1697 filename == NULL ? "???" : sanitize_string (filename),
1698 linenumber, discriminator);
1699 else
1700 printf ("%s:%u\n", filename == NULL
1701 ? "???" : sanitize_string (filename),
1702 linenumber);
1703 }
1704 if (unwind_inlines)
1705 {
1706 const char *filename2;
1707 const char *functionname2;
1708 unsigned line2;
1709
1710 while (bfd_find_inliner_info (abfd, &filename2, &functionname2,
1711 &line2))
1712 {
1713 printf ("inlined by %s:%u",
1714 sanitize_string (filename2), line2);
1715 printf (" (%s)\n", sanitize_string (functionname2));
1716 }
1717 }
1718 }
1719
1720 if (with_source_code
1721 && filename != NULL
1722 && linenumber > 0)
1723 {
1724 struct print_file_list **pp, *p;
1725 unsigned l;
1726
1727 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
1728 if (filename_cmp ((*pp)->filename, filename) == 0)
1729 break;
1730 p = *pp;
1731
1732 if (p == NULL)
1733 {
1734 if (reloc)
1735 filename = xstrdup (filename);
1736 p = update_source_path (filename, abfd);
1737 }
1738
1739 if (p != NULL && linenumber != p->last_line)
1740 {
1741 if (file_start_context && p->first)
1742 l = 1;
1743 else
1744 {
1745 l = linenumber - SHOW_PRECEDING_CONTEXT_LINES;
1746 if (l >= linenumber)
1747 l = 1;
1748 if (p->max_printed >= l)
1749 {
1750 if (p->max_printed < linenumber)
1751 l = p->max_printed + 1;
1752 else
1753 l = linenumber;
1754 }
1755 }
1756 dump_lines (p, l, linenumber);
1757 if (p->max_printed < linenumber)
1758 p->max_printed = linenumber;
1759 p->last_line = linenumber;
1760 p->first = 0;
1761 }
1762 }
1763
1764 if (functionname != NULL
1765 && (prev_functionname == NULL
1766 || strcmp (functionname, prev_functionname) != 0))
1767 {
1768 if (prev_functionname != NULL)
1769 free (prev_functionname);
1770 prev_functionname = (char *) xmalloc (strlen (functionname) + 1);
1771 strcpy (prev_functionname, functionname);
1772 }
1773
1774 if (linenumber > 0 && linenumber != prev_line)
1775 prev_line = linenumber;
1776
1777 if (discriminator != prev_discriminator)
1778 prev_discriminator = discriminator;
1779
1780 if (path)
1781 free (path);
1782 }
1783
1784 /* Pseudo FILE object for strings. */
1785 typedef struct
1786 {
1787 char *buffer;
1788 size_t pos;
1789 size_t alloc;
1790 } SFILE;
1791
1792 /* sprintf to a "stream". */
1793
1794 static int ATTRIBUTE_PRINTF_2
1795 objdump_sprintf (SFILE *f, const char *format, ...)
1796 {
1797 size_t n;
1798 va_list args;
1799
1800 while (1)
1801 {
1802 size_t space = f->alloc - f->pos;
1803
1804 va_start (args, format);
1805 n = vsnprintf (f->buffer + f->pos, space, format, args);
1806 va_end (args);
1807
1808 if (space > n)
1809 break;
1810
1811 f->alloc = (f->alloc + n) * 2;
1812 f->buffer = (char *) xrealloc (f->buffer, f->alloc);
1813 }
1814 f->pos += n;
1815
1816 return n;
1817 }
1818
1819 /* The number of zeroes we want to see before we start skipping them.
1820 The number is arbitrarily chosen. */
1821
1822 #define DEFAULT_SKIP_ZEROES 8
1823
1824 /* The number of zeroes to skip at the end of a section. If the
1825 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1826 SKIP_ZEROES, they will be disassembled. If there are fewer than
1827 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1828 attempt to avoid disassembling zeroes inserted by section
1829 alignment. */
1830
1831 #define DEFAULT_SKIP_ZEROES_AT_END 3
1832
1833 /* Disassemble some data in memory between given values. */
1834
1835 static void
1836 disassemble_bytes (struct disassemble_info * inf,
1837 disassembler_ftype disassemble_fn,
1838 bfd_boolean insns,
1839 bfd_byte * data,
1840 bfd_vma start_offset,
1841 bfd_vma stop_offset,
1842 bfd_vma rel_offset,
1843 arelent *** relppp,
1844 arelent ** relppend)
1845 {
1846 struct objdump_disasm_info *aux;
1847 asection *section;
1848 int octets_per_line;
1849 int skip_addr_chars;
1850 bfd_vma addr_offset;
1851 unsigned int opb = inf->octets_per_byte;
1852 unsigned int skip_zeroes = inf->skip_zeroes;
1853 unsigned int skip_zeroes_at_end = inf->skip_zeroes_at_end;
1854 int octets = opb;
1855 SFILE sfile;
1856
1857 aux = (struct objdump_disasm_info *) inf->application_data;
1858 section = aux->sec;
1859
1860 sfile.alloc = 120;
1861 sfile.buffer = (char *) xmalloc (sfile.alloc);
1862 sfile.pos = 0;
1863
1864 if (insn_width)
1865 octets_per_line = insn_width;
1866 else if (insns)
1867 octets_per_line = 4;
1868 else
1869 octets_per_line = 16;
1870
1871 /* Figure out how many characters to skip at the start of an
1872 address, to make the disassembly look nicer. We discard leading
1873 zeroes in chunks of 4, ensuring that there is always a leading
1874 zero remaining. */
1875 skip_addr_chars = 0;
1876 if (! prefix_addresses)
1877 {
1878 char buf[30];
1879
1880 bfd_sprintf_vma (aux->abfd, buf, section->vma + section->size / opb);
1881
1882 while (buf[skip_addr_chars] == '0')
1883 ++skip_addr_chars;
1884
1885 /* Don't discard zeros on overflow. */
1886 if (buf[skip_addr_chars] == '\0' && section->vma != 0)
1887 skip_addr_chars = 0;
1888
1889 if (skip_addr_chars != 0)
1890 skip_addr_chars = (skip_addr_chars - 1) & -4;
1891 }
1892
1893 inf->insn_info_valid = 0;
1894
1895 addr_offset = start_offset;
1896 while (addr_offset < stop_offset)
1897 {
1898 bfd_vma z;
1899 bfd_boolean need_nl = FALSE;
1900 int previous_octets;
1901
1902 /* Remember the length of the previous instruction. */
1903 previous_octets = octets;
1904 octets = 0;
1905
1906 /* Make sure we don't use relocs from previous instructions. */
1907 aux->reloc = NULL;
1908
1909 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1910 print `...'. */
1911 for (z = addr_offset * opb; z < stop_offset * opb; z++)
1912 if (data[z] != 0)
1913 break;
1914 if (! disassemble_zeroes
1915 && (inf->insn_info_valid == 0
1916 || inf->branch_delay_insns == 0)
1917 && (z - addr_offset * opb >= skip_zeroes
1918 || (z == stop_offset * opb &&
1919 z - addr_offset * opb < skip_zeroes_at_end)))
1920 {
1921 /* If there are more nonzero octets to follow, we only skip
1922 zeroes in multiples of 4, to try to avoid running over
1923 the start of an instruction which happens to start with
1924 zero. */
1925 if (z != stop_offset * opb)
1926 z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
1927
1928 octets = z - addr_offset * opb;
1929
1930 /* If we are going to display more data, and we are displaying
1931 file offsets, then tell the user how many zeroes we skip
1932 and the file offset from where we resume dumping. */
1933 if (display_file_offsets && ((addr_offset + (octets / opb)) < stop_offset))
1934 printf ("\t... (skipping %d zeroes, resuming at file offset: 0x%lx)\n",
1935 octets / opb,
1936 (unsigned long) (section->filepos
1937 + (addr_offset + (octets / opb))));
1938 else
1939 printf ("\t...\n");
1940 }
1941 else
1942 {
1943 char buf[50];
1944 int bpc = 0;
1945 int pb = 0;
1946
1947 if (with_line_numbers || with_source_code)
1948 show_line (aux->abfd, section, addr_offset);
1949
1950 if (! prefix_addresses)
1951 {
1952 char *s;
1953
1954 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
1955 for (s = buf + skip_addr_chars; *s == '0'; s++)
1956 *s = ' ';
1957 if (*s == '\0')
1958 *--s = '0';
1959 printf ("%s:\t", buf + skip_addr_chars);
1960 }
1961 else
1962 {
1963 aux->require_sec = TRUE;
1964 objdump_print_address (section->vma + addr_offset, inf);
1965 aux->require_sec = FALSE;
1966 putchar (' ');
1967 }
1968
1969 if (insns)
1970 {
1971 sfile.pos = 0;
1972 inf->fprintf_func = (fprintf_ftype) objdump_sprintf;
1973 inf->stream = &sfile;
1974 inf->bytes_per_line = 0;
1975 inf->bytes_per_chunk = 0;
1976 inf->flags = ((disassemble_all ? DISASSEMBLE_DATA : 0)
1977 | (wide_output ? WIDE_OUTPUT : 0));
1978 if (machine)
1979 inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
1980
1981 if (inf->disassembler_needs_relocs
1982 && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
1983 && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
1984 && *relppp < relppend)
1985 {
1986 bfd_signed_vma distance_to_rel;
1987
1988 distance_to_rel = (**relppp)->address
1989 - (rel_offset + addr_offset);
1990
1991 /* Check to see if the current reloc is associated with
1992 the instruction that we are about to disassemble. */
1993 if (distance_to_rel == 0
1994 /* FIXME: This is wrong. We are trying to catch
1995 relocs that are addressed part way through the
1996 current instruction, as might happen with a packed
1997 VLIW instruction. Unfortunately we do not know the
1998 length of the current instruction since we have not
1999 disassembled it yet. Instead we take a guess based
2000 upon the length of the previous instruction. The
2001 proper solution is to have a new target-specific
2002 disassembler function which just returns the length
2003 of an instruction at a given address without trying
2004 to display its disassembly. */
2005 || (distance_to_rel > 0
2006 && distance_to_rel < (bfd_signed_vma) (previous_octets/ opb)))
2007 {
2008 inf->flags |= INSN_HAS_RELOC;
2009 aux->reloc = **relppp;
2010 }
2011 }
2012
2013 if (! disassemble_all
2014 && (section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
2015 == (SEC_CODE | SEC_HAS_CONTENTS))
2016 /* Set a stop_vma so that the disassembler will not read
2017 beyond the next symbol. We assume that symbols appear on
2018 the boundaries between instructions. We only do this when
2019 disassembling code of course, and when -D is in effect. */
2020 inf->stop_vma = section->vma + stop_offset;
2021
2022 inf->stop_offset = stop_offset;
2023 octets = (*disassemble_fn) (section->vma + addr_offset, inf);
2024
2025 inf->stop_vma = 0;
2026 inf->fprintf_func = (fprintf_ftype) fprintf;
2027 inf->stream = stdout;
2028 if (insn_width == 0 && inf->bytes_per_line != 0)
2029 octets_per_line = inf->bytes_per_line;
2030 if (octets < (int) opb)
2031 {
2032 if (sfile.pos)
2033 printf ("%s\n", sfile.buffer);
2034 if (octets >= 0)
2035 {
2036 non_fatal (_("disassemble_fn returned length %d"),
2037 octets);
2038 exit_status = 1;
2039 }
2040 break;
2041 }
2042 }
2043 else
2044 {
2045 bfd_vma j;
2046
2047 octets = octets_per_line;
2048 if (addr_offset + octets / opb > stop_offset)
2049 octets = (stop_offset - addr_offset) * opb;
2050
2051 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
2052 {
2053 if (ISPRINT (data[j]))
2054 buf[j - addr_offset * opb] = data[j];
2055 else
2056 buf[j - addr_offset * opb] = '.';
2057 }
2058 buf[j - addr_offset * opb] = '\0';
2059 }
2060
2061 if (prefix_addresses
2062 ? show_raw_insn > 0
2063 : show_raw_insn >= 0)
2064 {
2065 bfd_vma j;
2066
2067 /* If ! prefix_addresses and ! wide_output, we print
2068 octets_per_line octets per line. */
2069 pb = octets;
2070 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
2071 pb = octets_per_line;
2072
2073 if (inf->bytes_per_chunk)
2074 bpc = inf->bytes_per_chunk;
2075 else
2076 bpc = 1;
2077
2078 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
2079 {
2080 /* PR 21580: Check for a buffer ending early. */
2081 if (j + bpc <= stop_offset * opb)
2082 {
2083 int k;
2084
2085 if (inf->display_endian == BFD_ENDIAN_LITTLE)
2086 {
2087 for (k = bpc - 1; k >= 0; k--)
2088 printf ("%02x", (unsigned) data[j + k]);
2089 }
2090 else
2091 {
2092 for (k = 0; k < bpc; k++)
2093 printf ("%02x", (unsigned) data[j + k]);
2094 }
2095 }
2096 putchar (' ');
2097 }
2098
2099 for (; pb < octets_per_line; pb += bpc)
2100 {
2101 int k;
2102
2103 for (k = 0; k < bpc; k++)
2104 printf (" ");
2105 putchar (' ');
2106 }
2107
2108 /* Separate raw data from instruction by extra space. */
2109 if (insns)
2110 putchar ('\t');
2111 else
2112 printf (" ");
2113 }
2114
2115 if (! insns)
2116 printf ("%s", buf);
2117 else if (sfile.pos)
2118 printf ("%s", sfile.buffer);
2119
2120 if (prefix_addresses
2121 ? show_raw_insn > 0
2122 : show_raw_insn >= 0)
2123 {
2124 while (pb < octets)
2125 {
2126 bfd_vma j;
2127 char *s;
2128
2129 putchar ('\n');
2130 j = addr_offset * opb + pb;
2131
2132 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
2133 for (s = buf + skip_addr_chars; *s == '0'; s++)
2134 *s = ' ';
2135 if (*s == '\0')
2136 *--s = '0';
2137 printf ("%s:\t", buf + skip_addr_chars);
2138
2139 pb += octets_per_line;
2140 if (pb > octets)
2141 pb = octets;
2142 for (; j < addr_offset * opb + pb; j += bpc)
2143 {
2144 /* PR 21619: Check for a buffer ending early. */
2145 if (j + bpc <= stop_offset * opb)
2146 {
2147 int k;
2148
2149 if (inf->display_endian == BFD_ENDIAN_LITTLE)
2150 {
2151 for (k = bpc - 1; k >= 0; k--)
2152 printf ("%02x", (unsigned) data[j + k]);
2153 }
2154 else
2155 {
2156 for (k = 0; k < bpc; k++)
2157 printf ("%02x", (unsigned) data[j + k]);
2158 }
2159 }
2160 putchar (' ');
2161 }
2162 }
2163 }
2164
2165 if (!wide_output)
2166 putchar ('\n');
2167 else
2168 need_nl = TRUE;
2169 }
2170
2171 while ((*relppp) < relppend
2172 && (**relppp)->address < rel_offset + addr_offset + octets / opb)
2173 {
2174 if (dump_reloc_info || dump_dynamic_reloc_info)
2175 {
2176 arelent *q;
2177
2178 q = **relppp;
2179
2180 if (wide_output)
2181 putchar ('\t');
2182 else
2183 printf ("\t\t\t");
2184
2185 objdump_print_value (section->vma - rel_offset + q->address,
2186 inf, TRUE);
2187
2188 if (q->howto == NULL)
2189 printf (": *unknown*\t");
2190 else if (q->howto->name)
2191 printf (": %s\t", q->howto->name);
2192 else
2193 printf (": %d\t", q->howto->type);
2194
2195 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
2196 printf ("*unknown*");
2197 else
2198 {
2199 const char *sym_name;
2200
2201 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
2202 if (sym_name != NULL && *sym_name != '\0')
2203 objdump_print_symname (aux->abfd, inf, *q->sym_ptr_ptr);
2204 else
2205 {
2206 asection *sym_sec;
2207
2208 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
2209 sym_name = bfd_get_section_name (aux->abfd, sym_sec);
2210 if (sym_name == NULL || *sym_name == '\0')
2211 sym_name = "*unknown*";
2212 printf ("%s", sanitize_string (sym_name));
2213 }
2214 }
2215
2216 if (q->addend)
2217 {
2218 bfd_signed_vma addend = q->addend;
2219 if (addend < 0)
2220 {
2221 printf ("-0x");
2222 addend = -addend;
2223 }
2224 else
2225 printf ("+0x");
2226 objdump_print_value (addend, inf, TRUE);
2227 }
2228
2229 printf ("\n");
2230 need_nl = FALSE;
2231 }
2232 ++(*relppp);
2233 }
2234
2235 if (need_nl)
2236 printf ("\n");
2237
2238 addr_offset += octets / opb;
2239 }
2240
2241 free (sfile.buffer);
2242 }
2243
2244 static void
2245 disassemble_section (bfd *abfd, asection *section, void *inf)
2246 {
2247 const struct elf_backend_data * bed;
2248 bfd_vma sign_adjust = 0;
2249 struct disassemble_info * pinfo = (struct disassemble_info *) inf;
2250 struct objdump_disasm_info * paux;
2251 unsigned int opb = pinfo->octets_per_byte;
2252 bfd_byte * data = NULL;
2253 bfd_size_type datasize = 0;
2254 arelent ** rel_pp = NULL;
2255 arelent ** rel_ppstart = NULL;
2256 arelent ** rel_ppend;
2257 bfd_vma stop_offset;
2258 asymbol * sym = NULL;
2259 long place = 0;
2260 long rel_count;
2261 bfd_vma rel_offset;
2262 unsigned long addr_offset;
2263 bfd_boolean do_print;
2264 enum loop_control
2265 {
2266 stop_offset_reached,
2267 function_sym,
2268 next_sym
2269 } loop_until;
2270
2271 /* Sections that do not contain machine
2272 code are not normally disassembled. */
2273 if (! disassemble_all
2274 && only_list == NULL
2275 && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
2276 != (SEC_CODE | SEC_HAS_CONTENTS)))
2277 return;
2278
2279 if (! process_section_p (section))
2280 return;
2281
2282 datasize = bfd_get_section_size (section);
2283 if (datasize == 0)
2284 return;
2285
2286 if (start_address == (bfd_vma) -1
2287 || start_address < section->vma)
2288 addr_offset = 0;
2289 else
2290 addr_offset = start_address - section->vma;
2291
2292 if (stop_address == (bfd_vma) -1)
2293 stop_offset = datasize / opb;
2294 else
2295 {
2296 if (stop_address < section->vma)
2297 stop_offset = 0;
2298 else
2299 stop_offset = stop_address - section->vma;
2300 if (stop_offset > datasize / opb)
2301 stop_offset = datasize / opb;
2302 }
2303
2304 if (addr_offset >= stop_offset)
2305 return;
2306
2307 /* Decide which set of relocs to use. Load them if necessary. */
2308 paux = (struct objdump_disasm_info *) pinfo->application_data;
2309 if (paux->dynrelbuf && dump_dynamic_reloc_info)
2310 {
2311 rel_pp = paux->dynrelbuf;
2312 rel_count = paux->dynrelcount;
2313 /* Dynamic reloc addresses are absolute, non-dynamic are section
2314 relative. REL_OFFSET specifies the reloc address corresponding
2315 to the start of this section. */
2316 rel_offset = section->vma;
2317 }
2318 else
2319 {
2320 rel_count = 0;
2321 rel_pp = NULL;
2322 rel_offset = 0;
2323
2324 if ((section->flags & SEC_RELOC) != 0
2325 && (dump_reloc_info || pinfo->disassembler_needs_relocs))
2326 {
2327 long relsize;
2328
2329 relsize = bfd_get_reloc_upper_bound (abfd, section);
2330 if (relsize < 0)
2331 bfd_fatal (bfd_get_filename (abfd));
2332
2333 if (relsize > 0)
2334 {
2335 rel_ppstart = rel_pp = (arelent **) xmalloc (relsize);
2336 rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
2337 if (rel_count < 0)
2338 bfd_fatal (bfd_get_filename (abfd));
2339
2340 /* Sort the relocs by address. */
2341 qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
2342 }
2343 }
2344 }
2345 rel_ppend = rel_pp + rel_count;
2346
2347 if (!bfd_malloc_and_get_section (abfd, section, &data))
2348 {
2349 non_fatal (_("Reading section %s failed because: %s"),
2350 section->name, bfd_errmsg (bfd_get_error ()));
2351 return;
2352 }
2353
2354 paux->sec = section;
2355 pinfo->buffer = data;
2356 pinfo->buffer_vma = section->vma;
2357 pinfo->buffer_length = datasize;
2358 pinfo->section = section;
2359
2360 /* Skip over the relocs belonging to addresses below the
2361 start address. */
2362 while (rel_pp < rel_ppend
2363 && (*rel_pp)->address < rel_offset + addr_offset)
2364 ++rel_pp;
2365
2366 printf (_("\nDisassembly of section %s:\n"), sanitize_string (section->name));
2367
2368 /* Find the nearest symbol forwards from our current position. */
2369 paux->require_sec = TRUE;
2370 sym = (asymbol *) find_symbol_for_address (section->vma + addr_offset,
2371 (struct disassemble_info *) inf,
2372 &place);
2373 paux->require_sec = FALSE;
2374
2375 /* PR 9774: If the target used signed addresses then we must make
2376 sure that we sign extend the value that we calculate for 'addr'
2377 in the loop below. */
2378 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2379 && (bed = get_elf_backend_data (abfd)) != NULL
2380 && bed->sign_extend_vma)
2381 sign_adjust = (bfd_vma) 1 << (bed->s->arch_size - 1);
2382
2383 /* Disassemble a block of instructions up to the address associated with
2384 the symbol we have just found. Then print the symbol and find the
2385 next symbol on. Repeat until we have disassembled the entire section
2386 or we have reached the end of the address range we are interested in. */
2387 do_print = paux->symbol == NULL;
2388 loop_until = stop_offset_reached;
2389
2390 while (addr_offset < stop_offset)
2391 {
2392 bfd_vma addr;
2393 asymbol *nextsym;
2394 bfd_vma nextstop_offset;
2395 bfd_boolean insns;
2396
2397 addr = section->vma + addr_offset;
2398 addr = ((addr & ((sign_adjust << 1) - 1)) ^ sign_adjust) - sign_adjust;
2399
2400 if (sym != NULL && bfd_asymbol_value (sym) <= addr)
2401 {
2402 int x;
2403
2404 for (x = place;
2405 (x < sorted_symcount
2406 && (bfd_asymbol_value (sorted_syms[x]) <= addr));
2407 ++x)
2408 continue;
2409
2410 pinfo->symbols = sorted_syms + place;
2411 pinfo->num_symbols = x - place;
2412 pinfo->symtab_pos = place;
2413 }
2414 else
2415 {
2416 pinfo->symbols = NULL;
2417 pinfo->num_symbols = 0;
2418 pinfo->symtab_pos = -1;
2419 }
2420
2421 /* If we are only disassembling from a specific symbol,
2422 check to see if we should start or stop displaying. */
2423 if (sym && paux->symbol)
2424 {
2425 if (do_print)
2426 {
2427 /* See if we should stop printing. */
2428 switch (loop_until)
2429 {
2430 case function_sym:
2431 if (sym->flags & BSF_FUNCTION)
2432 do_print = FALSE;
2433 break;
2434
2435 case stop_offset_reached:
2436 /* Handled by the while loop. */
2437 break;
2438
2439 case next_sym:
2440 /* FIXME: There is an implicit assumption here
2441 that the name of sym is different from
2442 paux->symbol. */
2443 if (! bfd_is_local_label (abfd, sym))
2444 do_print = FALSE;
2445 break;
2446 }
2447 }
2448 else
2449 {
2450 const char * name = bfd_asymbol_name (sym);
2451 char * alloc = NULL;
2452
2453 if (do_demangle && name[0] != '\0')
2454 {
2455 /* Demangle the name. */
2456 alloc = bfd_demangle (abfd, name, demangle_flags);
2457 if (alloc != NULL)
2458 name = alloc;
2459 }
2460
2461 /* We are not currently printing. Check to see
2462 if the current symbol matches the requested symbol. */
2463 if (streq (name, paux->symbol))
2464 {
2465 do_print = TRUE;
2466
2467 if (sym->flags & BSF_FUNCTION)
2468 {
2469 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2470 && ((elf_symbol_type *) sym)->internal_elf_sym.st_size > 0)
2471 {
2472 /* Sym is a function symbol with a size associated
2473 with it. Turn on automatic disassembly for the
2474 next VALUE bytes. */
2475 stop_offset = addr_offset
2476 + ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
2477 loop_until = stop_offset_reached;
2478 }
2479 else
2480 {
2481 /* Otherwise we need to tell the loop heuristic to
2482 loop until the next function symbol is encountered. */
2483 loop_until = function_sym;
2484 }
2485 }
2486 else
2487 {
2488 /* Otherwise loop until the next symbol is encountered. */
2489 loop_until = next_sym;
2490 }
2491 }
2492
2493 free (alloc);
2494 }
2495 }
2496
2497 if (! prefix_addresses && do_print)
2498 {
2499 pinfo->fprintf_func (pinfo->stream, "\n");
2500 objdump_print_addr_with_sym (abfd, section, sym, addr,
2501 pinfo, FALSE);
2502 pinfo->fprintf_func (pinfo->stream, ":\n");
2503 }
2504
2505 if (sym != NULL && bfd_asymbol_value (sym) > addr)
2506 nextsym = sym;
2507 else if (sym == NULL)
2508 nextsym = NULL;
2509 else
2510 {
2511 #define is_valid_next_sym(SYM) \
2512 (strcmp (bfd_section_name (abfd, (SYM)->section), bfd_section_name (abfd, section)) == 0 \
2513 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
2514 && pinfo->symbol_is_valid (SYM, pinfo))
2515
2516 /* Search forward for the next appropriate symbol in
2517 SECTION. Note that all the symbols are sorted
2518 together into one big array, and that some sections
2519 may have overlapping addresses. */
2520 while (place < sorted_symcount
2521 && ! is_valid_next_sym (sorted_syms [place]))
2522 ++place;
2523
2524 if (place >= sorted_symcount)
2525 nextsym = NULL;
2526 else
2527 nextsym = sorted_syms[place];
2528 }
2529
2530 if (sym != NULL && bfd_asymbol_value (sym) > addr)
2531 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
2532 else if (nextsym == NULL)
2533 nextstop_offset = stop_offset;
2534 else
2535 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
2536
2537 if (nextstop_offset > stop_offset
2538 || nextstop_offset <= addr_offset)
2539 nextstop_offset = stop_offset;
2540
2541 /* If a symbol is explicitly marked as being an object
2542 rather than a function, just dump the bytes without
2543 disassembling them. */
2544 if (disassemble_all
2545 || sym == NULL
2546 || sym->section != section
2547 || bfd_asymbol_value (sym) > addr
2548 || ((sym->flags & BSF_OBJECT) == 0
2549 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
2550 == NULL)
2551 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
2552 == NULL))
2553 || (sym->flags & BSF_FUNCTION) != 0)
2554 insns = TRUE;
2555 else
2556 insns = FALSE;
2557
2558 if (do_print)
2559 disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
2560 addr_offset, nextstop_offset,
2561 rel_offset, &rel_pp, rel_ppend);
2562
2563 addr_offset = nextstop_offset;
2564 sym = nextsym;
2565 }
2566
2567 free (data);
2568
2569 if (rel_ppstart != NULL)
2570 free (rel_ppstart);
2571 }
2572
2573 /* Disassemble the contents of an object file. */
2574
2575 static void
2576 disassemble_data (bfd *abfd)
2577 {
2578 struct disassemble_info disasm_info;
2579 struct objdump_disasm_info aux;
2580 long i;
2581
2582 print_files = NULL;
2583 prev_functionname = NULL;
2584 prev_line = -1;
2585 prev_discriminator = 0;
2586
2587 /* We make a copy of syms to sort. We don't want to sort syms
2588 because that will screw up the relocs. */
2589 sorted_symcount = symcount ? symcount : dynsymcount;
2590 sorted_syms = (asymbol **) xmalloc ((sorted_symcount + synthcount)
2591 * sizeof (asymbol *));
2592 memcpy (sorted_syms, symcount ? syms : dynsyms,
2593 sorted_symcount * sizeof (asymbol *));
2594
2595 sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
2596
2597 for (i = 0; i < synthcount; ++i)
2598 {
2599 sorted_syms[sorted_symcount] = synthsyms + i;
2600 ++sorted_symcount;
2601 }
2602
2603 /* Sort the symbols into section and symbol order. */
2604 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
2605
2606 init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
2607
2608 disasm_info.application_data = (void *) &aux;
2609 aux.abfd = abfd;
2610 aux.require_sec = FALSE;
2611 aux.dynrelbuf = NULL;
2612 aux.dynrelcount = 0;
2613 aux.reloc = NULL;
2614 aux.symbol = disasm_sym;
2615
2616 disasm_info.print_address_func = objdump_print_address;
2617 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
2618
2619 if (machine != NULL)
2620 {
2621 const bfd_arch_info_type *inf = bfd_scan_arch (machine);
2622
2623 if (inf == NULL)
2624 fatal (_("can't use supplied machine %s"), machine);
2625
2626 abfd->arch_info = inf;
2627 }
2628
2629 if (endian != BFD_ENDIAN_UNKNOWN)
2630 {
2631 struct bfd_target *xvec;
2632
2633 xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
2634 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
2635 xvec->byteorder = endian;
2636 abfd->xvec = xvec;
2637 }
2638
2639 /* Use libopcodes to locate a suitable disassembler. */
2640 aux.disassemble_fn = disassembler (bfd_get_arch (abfd),
2641 bfd_big_endian (abfd),
2642 bfd_get_mach (abfd), abfd);
2643 if (!aux.disassemble_fn)
2644 {
2645 non_fatal (_("can't disassemble for architecture %s\n"),
2646 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
2647 exit_status = 1;
2648 return;
2649 }
2650
2651 disasm_info.flavour = bfd_get_flavour (abfd);
2652 disasm_info.arch = bfd_get_arch (abfd);
2653 disasm_info.mach = bfd_get_mach (abfd);
2654 disasm_info.disassembler_options = disassembler_options;
2655 disasm_info.octets_per_byte = bfd_octets_per_byte (abfd);
2656 disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
2657 disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
2658 disasm_info.disassembler_needs_relocs = FALSE;
2659
2660 if (bfd_big_endian (abfd))
2661 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
2662 else if (bfd_little_endian (abfd))
2663 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
2664 else
2665 /* ??? Aborting here seems too drastic. We could default to big or little
2666 instead. */
2667 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
2668
2669 /* Allow the target to customize the info structure. */
2670 disassemble_init_for_target (& disasm_info);
2671
2672 /* Pre-load the dynamic relocs as we may need them during the disassembly. */
2673 {
2674 long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2675
2676 if (relsize < 0 && dump_dynamic_reloc_info)
2677 bfd_fatal (bfd_get_filename (abfd));
2678
2679 if (relsize > 0)
2680 {
2681 aux.dynrelbuf = (arelent **) xmalloc (relsize);
2682 aux.dynrelcount = bfd_canonicalize_dynamic_reloc (abfd,
2683 aux.dynrelbuf,
2684 dynsyms);
2685 if (aux.dynrelcount < 0)
2686 bfd_fatal (bfd_get_filename (abfd));
2687
2688 /* Sort the relocs by address. */
2689 qsort (aux.dynrelbuf, aux.dynrelcount, sizeof (arelent *),
2690 compare_relocs);
2691 }
2692 }
2693 disasm_info.symtab = sorted_syms;
2694 disasm_info.symtab_size = sorted_symcount;
2695
2696 bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
2697
2698 if (aux.dynrelbuf != NULL)
2699 free (aux.dynrelbuf);
2700 free (sorted_syms);
2701 }
2702 \f
2703 static bfd_boolean
2704 load_specific_debug_section (enum dwarf_section_display_enum debug,
2705 asection *sec, void *file)
2706 {
2707 struct dwarf_section *section = &debug_displays [debug].section;
2708 bfd *abfd = (bfd *) file;
2709 bfd_byte *contents;
2710 bfd_size_type amt;
2711 size_t alloced;
2712
2713 if (section->start != NULL)
2714 {
2715 /* If it is already loaded, do nothing. */
2716 if (streq (section->filename, bfd_get_filename (abfd)))
2717 return TRUE;
2718 free (section->start);
2719 }
2720
2721 section->filename = bfd_get_filename (abfd);
2722 section->reloc_info = NULL;
2723 section->num_relocs = 0;
2724 section->address = bfd_get_section_vma (abfd, sec);
2725 section->user_data = sec;
2726 section->size = bfd_get_section_size (sec);
2727 /* PR 24360: On 32-bit hosts sizeof (size_t) < sizeof (bfd_size_type). */
2728 alloced = amt = section->size + 1;
2729 if (alloced != amt || alloced == 0)
2730 {
2731 section->start = NULL;
2732 free_debug_section (debug);
2733 printf (_("\nSection '%s' has an invalid size: %#llx.\n"),
2734 sanitize_string (section->name),
2735 (unsigned long long) section->size);
2736 return FALSE;
2737 }
2738 section->start = contents = malloc (alloced);
2739 if (section->start == NULL
2740 || !bfd_get_full_section_contents (abfd, sec, &contents))
2741 {
2742 free_debug_section (debug);
2743 printf (_("\nCan't get contents for section '%s'.\n"),
2744 sanitize_string (section->name));
2745 return FALSE;
2746 }
2747 /* Ensure any string section has a terminating NUL. */
2748 section->start[section->size] = 0;
2749
2750 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
2751 && debug_displays [debug].relocate)
2752 {
2753 long reloc_size;
2754 bfd_boolean ret;
2755
2756 bfd_cache_section_contents (sec, section->start);
2757
2758 ret = bfd_simple_get_relocated_section_contents (abfd,
2759 sec,
2760 section->start,
2761 syms) != NULL;
2762
2763 if (! ret)
2764 {
2765 free_debug_section (debug);
2766 printf (_("\nCan't get contents for section '%s'.\n"),
2767 sanitize_string (section->name));
2768 return FALSE;
2769 }
2770
2771 reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
2772 if (reloc_size > 0)
2773 {
2774 unsigned long reloc_count;
2775 arelent **relocs;
2776
2777 relocs = (arelent **) xmalloc (reloc_size);
2778
2779 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, NULL);
2780 if (reloc_count == 0)
2781 free (relocs);
2782 else
2783 {
2784 section->reloc_info = relocs;
2785 section->num_relocs = reloc_count;
2786 }
2787 }
2788 }
2789
2790 return TRUE;
2791 }
2792
2793 bfd_boolean
2794 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
2795 {
2796 arelent ** relocs;
2797 arelent * rp;
2798
2799 if (dsec == NULL || dsec->reloc_info == NULL)
2800 return FALSE;
2801
2802 relocs = (arelent **) dsec->reloc_info;
2803
2804 for (; (rp = * relocs) != NULL; ++ relocs)
2805 if (rp->address == offset)
2806 return TRUE;
2807
2808 return FALSE;
2809 }
2810
2811 bfd_boolean
2812 load_debug_section (enum dwarf_section_display_enum debug, void *file)
2813 {
2814 struct dwarf_section *section = &debug_displays [debug].section;
2815 bfd *abfd = (bfd *) file;
2816 asection *sec;
2817
2818 /* If it is already loaded, do nothing. */
2819 if (section->start != NULL)
2820 {
2821 if (streq (section->filename, bfd_get_filename (abfd)))
2822 return TRUE;
2823 }
2824
2825 /* Locate the debug section. */
2826 sec = bfd_get_section_by_name (abfd, section->uncompressed_name);
2827 if (sec != NULL)
2828 section->name = section->uncompressed_name;
2829 else
2830 {
2831 sec = bfd_get_section_by_name (abfd, section->compressed_name);
2832 if (sec != NULL)
2833 section->name = section->compressed_name;
2834 }
2835 if (sec == NULL)
2836 return FALSE;
2837
2838 return load_specific_debug_section (debug, sec, file);
2839 }
2840
2841 void
2842 free_debug_section (enum dwarf_section_display_enum debug)
2843 {
2844 struct dwarf_section *section = &debug_displays [debug].section;
2845
2846 if (section->start == NULL)
2847 return;
2848
2849 /* PR 17512: file: 0f67f69d. */
2850 if (section->user_data != NULL)
2851 {
2852 asection * sec = (asection *) section->user_data;
2853
2854 /* If we are freeing contents that are also pointed to by the BFD
2855 library's section structure then make sure to update those pointers
2856 too. Otherwise, the next time we try to load data for this section
2857 we can end up using a stale pointer. */
2858 if (section->start == sec->contents)
2859 {
2860 sec->contents = NULL;
2861 sec->flags &= ~ SEC_IN_MEMORY;
2862 sec->compress_status = COMPRESS_SECTION_NONE;
2863 }
2864 }
2865
2866 free ((char *) section->start);
2867 section->start = NULL;
2868 section->address = 0;
2869 section->size = 0;
2870 }
2871
2872 void
2873 close_debug_file (void * file)
2874 {
2875 bfd * abfd = (bfd *) file;
2876
2877 bfd_close (abfd);
2878 }
2879
2880 void *
2881 open_debug_file (const char * pathname)
2882 {
2883 bfd * data;
2884
2885 data = bfd_openr (pathname, NULL);
2886 if (data == NULL)
2887 return NULL;
2888
2889 if (! bfd_check_format (data, bfd_object))
2890 return NULL;
2891
2892 return data;
2893 }
2894
2895 static void
2896 dump_dwarf_section (bfd *abfd, asection *section,
2897 void *arg ATTRIBUTE_UNUSED)
2898 {
2899 const char *name = bfd_get_section_name (abfd, section);
2900 const char *match;
2901 int i;
2902
2903 if (CONST_STRNEQ (name, ".gnu.linkonce.wi."))
2904 match = ".debug_info";
2905 else
2906 match = name;
2907
2908 for (i = 0; i < max; i++)
2909 if ((strcmp (debug_displays [i].section.uncompressed_name, match) == 0
2910 || strcmp (debug_displays [i].section.compressed_name, match) == 0)
2911 && debug_displays [i].enabled != NULL
2912 && *debug_displays [i].enabled)
2913 {
2914 struct dwarf_section *sec = &debug_displays [i].section;
2915
2916 if (strcmp (sec->uncompressed_name, match) == 0)
2917 sec->name = sec->uncompressed_name;
2918 else
2919 sec->name = sec->compressed_name;
2920 if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
2921 section, abfd))
2922 {
2923 debug_displays [i].display (sec, abfd);
2924
2925 if (i != info && i != abbrev)
2926 free_debug_section ((enum dwarf_section_display_enum) i);
2927 }
2928 break;
2929 }
2930 }
2931
2932 /* Dump the dwarf debugging information. */
2933
2934 static void
2935 dump_dwarf (bfd *abfd)
2936 {
2937 /* The byte_get pointer should have been set at the start of dump_bfd(). */
2938 if (byte_get == NULL)
2939 {
2940 warn (_("File %s does not contain any dwarf debug information\n"),
2941 bfd_get_filename (abfd));
2942 return;
2943 }
2944
2945 eh_addr_size = bfd_arch_bits_per_address (abfd) / 8;
2946
2947 switch (bfd_get_arch (abfd))
2948 {
2949 case bfd_arch_i386:
2950 switch (bfd_get_mach (abfd))
2951 {
2952 case bfd_mach_x86_64:
2953 case bfd_mach_x86_64_intel_syntax:
2954 case bfd_mach_x86_64_nacl:
2955 case bfd_mach_x64_32:
2956 case bfd_mach_x64_32_intel_syntax:
2957 case bfd_mach_x64_32_nacl:
2958 init_dwarf_regnames_x86_64 ();
2959 break;
2960
2961 default:
2962 init_dwarf_regnames_i386 ();
2963 break;
2964 }
2965 break;
2966
2967 case bfd_arch_iamcu:
2968 init_dwarf_regnames_iamcu ();
2969 break;
2970
2971 case bfd_arch_aarch64:
2972 init_dwarf_regnames_aarch64();
2973 break;
2974
2975 case bfd_arch_s390:
2976 init_dwarf_regnames_s390 ();
2977 break;
2978
2979 case bfd_arch_riscv:
2980 init_dwarf_regnames_riscv ();
2981 break;
2982
2983 case bfd_arch_s12z:
2984 /* S12Z has a 24 bit address space. But the only known
2985 producer of dwarf_info encodes addresses into 32 bits. */
2986 eh_addr_size = 4;
2987 break;
2988
2989 default:
2990 break;
2991 }
2992
2993 bfd_map_over_sections (abfd, dump_dwarf_section, NULL);
2994 }
2995 \f
2996 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
2997 it. Return NULL on failure. */
2998
2999 static bfd_byte *
3000 read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr,
3001 bfd_size_type *entsize_ptr)
3002 {
3003 asection *stabsect;
3004 bfd_byte *contents;
3005
3006 stabsect = bfd_get_section_by_name (abfd, sect_name);
3007 if (stabsect == NULL)
3008 {
3009 printf (_("No %s section present\n\n"),
3010 sanitize_string (sect_name));
3011 return FALSE;
3012 }
3013
3014 if (!bfd_malloc_and_get_section (abfd, stabsect, &contents))
3015 {
3016 non_fatal (_("reading %s section of %s failed: %s"),
3017 sect_name, bfd_get_filename (abfd),
3018 bfd_errmsg (bfd_get_error ()));
3019 exit_status = 1;
3020 free (contents);
3021 return NULL;
3022 }
3023
3024 *size_ptr = bfd_section_size (abfd, stabsect);
3025 if (entsize_ptr)
3026 *entsize_ptr = stabsect->entsize;
3027
3028 return contents;
3029 }
3030
3031 /* Stabs entries use a 12 byte format:
3032 4 byte string table index
3033 1 byte stab type
3034 1 byte stab other field
3035 2 byte stab desc field
3036 4 byte stab value
3037 FIXME: This will have to change for a 64 bit object format. */
3038
3039 #define STRDXOFF (0)
3040 #define TYPEOFF (4)
3041 #define OTHEROFF (5)
3042 #define DESCOFF (6)
3043 #define VALOFF (8)
3044 #define STABSIZE (12)
3045
3046 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
3047 using string table section STRSECT_NAME (in `strtab'). */
3048
3049 static void
3050 print_section_stabs (bfd *abfd,
3051 const char *stabsect_name,
3052 unsigned *string_offset_ptr)
3053 {
3054 int i;
3055 unsigned file_string_table_offset = 0;
3056 unsigned next_file_string_table_offset = *string_offset_ptr;
3057 bfd_byte *stabp, *stabs_end;
3058
3059 stabp = stabs;
3060 stabs_end = stabp + stab_size;
3061
3062 printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name));
3063 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
3064
3065 /* Loop through all symbols and print them.
3066
3067 We start the index at -1 because there is a dummy symbol on
3068 the front of stabs-in-{coff,elf} sections that supplies sizes. */
3069 for (i = -1; stabp <= stabs_end - STABSIZE; stabp += STABSIZE, i++)
3070 {
3071 const char *name;
3072 unsigned long strx;
3073 unsigned char type, other;
3074 unsigned short desc;
3075 bfd_vma value;
3076
3077 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
3078 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
3079 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
3080 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
3081 value = bfd_h_get_32 (abfd, stabp + VALOFF);
3082
3083 printf ("\n%-6d ", i);
3084 /* Either print the stab name, or, if unnamed, print its number
3085 again (makes consistent formatting for tools like awk). */
3086 name = bfd_get_stab_name (type);
3087 if (name != NULL)
3088 printf ("%-6s", sanitize_string (name));
3089 else if (type == N_UNDF)
3090 printf ("HdrSym");
3091 else
3092 printf ("%-6d", type);
3093 printf (" %-6d %-6d ", other, desc);
3094 bfd_printf_vma (abfd, value);
3095 printf (" %-6lu", strx);
3096
3097 /* Symbols with type == 0 (N_UNDF) specify the length of the
3098 string table associated with this file. We use that info
3099 to know how to relocate the *next* file's string table indices. */
3100 if (type == N_UNDF)
3101 {
3102 file_string_table_offset = next_file_string_table_offset;
3103 next_file_string_table_offset += value;
3104 }
3105 else
3106 {
3107 bfd_size_type amt = strx + file_string_table_offset;
3108
3109 /* Using the (possibly updated) string table offset, print the
3110 string (if any) associated with this symbol. */
3111 if (amt < stabstr_size)
3112 /* PR 17512: file: 079-79389-0.001:0.1.
3113 FIXME: May need to sanitize this string before displaying. */
3114 printf (" %.*s", (int)(stabstr_size - amt), strtab + amt);
3115 else
3116 printf (" *");
3117 }
3118 }
3119 printf ("\n\n");
3120 *string_offset_ptr = next_file_string_table_offset;
3121 }
3122
3123 typedef struct
3124 {
3125 const char * section_name;
3126 const char * string_section_name;
3127 unsigned string_offset;
3128 }
3129 stab_section_names;
3130
3131 static void
3132 find_stabs_section (bfd *abfd, asection *section, void *names)
3133 {
3134 int len;
3135 stab_section_names * sought = (stab_section_names *) names;
3136
3137 /* Check for section names for which stabsect_name is a prefix, to
3138 handle .stab.N, etc. */
3139 len = strlen (sought->section_name);
3140
3141 /* If the prefix matches, and the files section name ends with a
3142 nul or a digit, then we match. I.e., we want either an exact
3143 match or a section followed by a number. */
3144 if (strncmp (sought->section_name, section->name, len) == 0
3145 && (section->name[len] == 0
3146 || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
3147 {
3148 if (strtab == NULL)
3149 strtab = read_section_stabs (abfd, sought->string_section_name,
3150 &stabstr_size, NULL);
3151
3152 if (strtab)
3153 {
3154 stabs = read_section_stabs (abfd, section->name, &stab_size, NULL);
3155 if (stabs)
3156 print_section_stabs (abfd, section->name, &sought->string_offset);
3157 }
3158 }
3159 }
3160
3161 static void
3162 dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
3163 {
3164 stab_section_names s;
3165
3166 s.section_name = stabsect_name;
3167 s.string_section_name = strsect_name;
3168 s.string_offset = 0;
3169
3170 bfd_map_over_sections (abfd, find_stabs_section, & s);
3171
3172 free (strtab);
3173 strtab = NULL;
3174 }
3175
3176 /* Dump the any sections containing stabs debugging information. */
3177
3178 static void
3179 dump_stabs (bfd *abfd)
3180 {
3181 dump_stabs_section (abfd, ".stab", ".stabstr");
3182 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
3183 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
3184
3185 /* For Darwin. */
3186 dump_stabs_section (abfd, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
3187
3188 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
3189 }
3190 \f
3191 static void
3192 dump_bfd_header (bfd *abfd)
3193 {
3194 char *comma = "";
3195
3196 printf (_("architecture: %s, "),
3197 bfd_printable_arch_mach (bfd_get_arch (abfd),
3198 bfd_get_mach (abfd)));
3199 printf (_("flags 0x%08x:\n"), abfd->flags & ~BFD_FLAGS_FOR_BFD_USE_MASK);
3200
3201 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
3202 PF (HAS_RELOC, "HAS_RELOC");
3203 PF (EXEC_P, "EXEC_P");
3204 PF (HAS_LINENO, "HAS_LINENO");
3205 PF (HAS_DEBUG, "HAS_DEBUG");
3206 PF (HAS_SYMS, "HAS_SYMS");
3207 PF (HAS_LOCALS, "HAS_LOCALS");
3208 PF (DYNAMIC, "DYNAMIC");
3209 PF (WP_TEXT, "WP_TEXT");
3210 PF (D_PAGED, "D_PAGED");
3211 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
3212 printf (_("\nstart address 0x"));
3213 bfd_printf_vma (abfd, abfd->start_address);
3214 printf ("\n");
3215 }
3216 \f
3217
3218 /* Formatting callback function passed to ctf_dump. Returns either the pointer
3219 it is passed, or a pointer to newly-allocated storage, in which case
3220 dump_ctf() will free it when it no longer needs it. */
3221
3222 static char *
3223 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
3224 char *s, void *arg)
3225 {
3226 const char *blanks = arg;
3227 char *new_s;
3228
3229 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
3230 return s;
3231 return new_s;
3232 }
3233
3234 /* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */
3235 static ctf_sect_t
3236 make_ctfsect (const char *name, bfd_byte *data,
3237 bfd_size_type size)
3238 {
3239 ctf_sect_t ctfsect;
3240
3241 ctfsect.cts_name = name;
3242 ctfsect.cts_entsize = 1;
3243 ctfsect.cts_size = size;
3244 ctfsect.cts_data = data;
3245
3246 return ctfsect;
3247 }
3248
3249 /* Dump one CTF archive member. */
3250
3251 static int
3252 dump_ctf_archive_member (ctf_file_t *ctf, const char *name, void *arg)
3253 {
3254 ctf_file_t *parent = (ctf_file_t *) arg;
3255 const char *things[] = {"Labels", "Data objects", "Function objects",
3256 "Variables", "Types", "Strings", ""};
3257 const char **thing;
3258 size_t i;
3259
3260 /* Only print out the name of non-default-named archive members.
3261 The name .ctf appears everywhere, even for things that aren't
3262 really archives, so printing it out is liable to be confusing. */
3263 if (strcmp (name, ".ctf") != 0)
3264 printf (_("\nCTF archive member: %s:\n"), sanitize_string (name));
3265
3266 ctf_import (ctf, parent);
3267 for (i = 1, thing = things; *thing[0]; thing++, i++)
3268 {
3269 ctf_dump_state_t *s = NULL;
3270 char *item;
3271
3272 printf ("\n %s:\n", *thing);
3273 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
3274 (void *) " ")) != NULL)
3275 {
3276 printf ("%s\n", item);
3277 free (item);
3278 }
3279
3280 if (ctf_errno (ctf))
3281 {
3282 non_fatal (_("Iteration failed: %s, %s\n"), *thing,
3283 ctf_errmsg (ctf_errno (ctf)));
3284 break;
3285 }
3286 }
3287 return 0;
3288 }
3289
3290 /* Dump the CTF debugging information. */
3291
3292 static void
3293 dump_ctf (bfd *abfd, const char *sect_name, const char *parent_name)
3294 {
3295 ctf_archive_t *ctfa, *parenta = NULL;
3296 bfd_byte *ctfdata, *parentdata = NULL;
3297 bfd_size_type ctfsize, parentsize;
3298 ctf_sect_t ctfsect;
3299 ctf_file_t *parent = NULL;
3300 int err;
3301
3302 if ((ctfdata = read_section_stabs (abfd, sect_name, &ctfsize, NULL)) == NULL)
3303 bfd_fatal (bfd_get_filename (abfd));
3304
3305 if (parent_name
3306 && (parentdata = read_section_stabs (abfd, parent_name, &parentsize,
3307 NULL)) == NULL)
3308 bfd_fatal (bfd_get_filename (abfd));
3309
3310 /* Load the CTF file and dump it. */
3311
3312 ctfsect = make_ctfsect (sect_name, ctfdata, ctfsize);
3313 if ((ctfa = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
3314 {
3315 non_fatal (_("CTF open failure: %s\n"), ctf_errmsg (err));
3316 bfd_fatal (bfd_get_filename (abfd));
3317 }
3318
3319 if (parentdata)
3320 {
3321 ctfsect = make_ctfsect (parent_name, parentdata, parentsize);
3322 if ((parenta = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
3323 {
3324 non_fatal (_("CTF open failure: %s\n"), ctf_errmsg (err));
3325 bfd_fatal (bfd_get_filename (abfd));
3326 }
3327
3328 /* Assume that the applicable parent archive member is the default one.
3329 (This is what all known implementations are expected to do, if they
3330 put CTFs and their parents in archives together.) */
3331 if ((parent = ctf_arc_open_by_name (parenta, NULL, &err)) == NULL)
3332 {
3333 non_fatal (_("CTF open failure: %s\n"), ctf_errmsg (err));
3334 bfd_fatal (bfd_get_filename (abfd));
3335 }
3336 }
3337
3338 printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name));
3339
3340 ctf_archive_iter (ctfa, dump_ctf_archive_member, parent);
3341 ctf_file_close (parent);
3342 ctf_close (ctfa);
3343 ctf_close (parenta);
3344 free (parentdata);
3345 free (ctfdata);
3346 }
3347
3348 \f
3349 static void
3350 dump_bfd_private_header (bfd *abfd)
3351 {
3352 if (!bfd_print_private_bfd_data (abfd, stdout))
3353 non_fatal (_("warning: private headers incomplete: %s"),
3354 bfd_errmsg (bfd_get_error ()));
3355 }
3356
3357 static void
3358 dump_target_specific (bfd *abfd)
3359 {
3360 const struct objdump_private_desc * const *desc;
3361 struct objdump_private_option *opt;
3362 char *e, *b;
3363
3364 /* Find the desc. */
3365 for (desc = objdump_private_vectors; *desc != NULL; desc++)
3366 if ((*desc)->filter (abfd))
3367 break;
3368
3369 if (*desc == NULL)
3370 {
3371 non_fatal (_("option -P/--private not supported by this file"));
3372 return;
3373 }
3374
3375 /* Clear all options. */
3376 for (opt = (*desc)->options; opt->name; opt++)
3377 opt->selected = FALSE;
3378
3379 /* Decode options. */
3380 b = dump_private_options;
3381 do
3382 {
3383 e = strchr (b, ',');
3384
3385 if (e)
3386 *e = 0;
3387
3388 for (opt = (*desc)->options; opt->name; opt++)
3389 if (strcmp (opt->name, b) == 0)
3390 {
3391 opt->selected = TRUE;
3392 break;
3393 }
3394 if (opt->name == NULL)
3395 non_fatal (_("target specific dump '%s' not supported"), b);
3396
3397 if (e)
3398 {
3399 *e = ',';
3400 b = e + 1;
3401 }
3402 }
3403 while (e != NULL);
3404
3405 /* Dump. */
3406 (*desc)->dump (abfd);
3407 }
3408 \f
3409 /* Display a section in hexadecimal format with associated characters.
3410 Each line prefixed by the zero padded address. */
3411
3412 static void
3413 dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
3414 {
3415 bfd_byte *data = NULL;
3416 bfd_size_type datasize;
3417 bfd_vma addr_offset;
3418 bfd_vma start_offset;
3419 bfd_vma stop_offset;
3420 unsigned int opb = bfd_octets_per_byte (abfd);
3421 /* Bytes per line. */
3422 const int onaline = 16;
3423 char buf[64];
3424 int count;
3425 int width;
3426
3427 if ((section->flags & SEC_HAS_CONTENTS) == 0)
3428 return;
3429
3430 if (! process_section_p (section))
3431 return;
3432
3433 if ((datasize = bfd_section_size (abfd, section)) == 0)
3434 return;
3435
3436 /* Compute the address range to display. */
3437 if (start_address == (bfd_vma) -1
3438 || start_address < section->vma)
3439 start_offset = 0;
3440 else
3441 start_offset = start_address - section->vma;
3442
3443 if (stop_address == (bfd_vma) -1)
3444 stop_offset = datasize / opb;
3445 else
3446 {
3447 if (stop_address < section->vma)
3448 stop_offset = 0;
3449 else
3450 stop_offset = stop_address - section->vma;
3451
3452 if (stop_offset > datasize / opb)
3453 stop_offset = datasize / opb;
3454 }
3455
3456 if (start_offset >= stop_offset)
3457 return;
3458
3459 printf (_("Contents of section %s:"), sanitize_string (section->name));
3460 if (display_file_offsets)
3461 printf (_(" (Starting at file offset: 0x%lx)"),
3462 (unsigned long) (section->filepos + start_offset));
3463 printf ("\n");
3464
3465 if (!bfd_get_full_section_contents (abfd, section, &data))
3466 {
3467 non_fatal (_("Reading section %s failed because: %s"),
3468 section->name, bfd_errmsg (bfd_get_error ()));
3469 return;
3470 }
3471
3472 width = 4;
3473
3474 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
3475 if (strlen (buf) >= sizeof (buf))
3476 abort ();
3477
3478 count = 0;
3479 while (buf[count] == '0' && buf[count+1] != '\0')
3480 count++;
3481 count = strlen (buf) - count;
3482 if (count > width)
3483 width = count;
3484
3485 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
3486 if (strlen (buf) >= sizeof (buf))
3487 abort ();
3488
3489 count = 0;
3490 while (buf[count] == '0' && buf[count+1] != '\0')
3491 count++;
3492 count = strlen (buf) - count;
3493 if (count > width)
3494 width = count;
3495
3496 for (addr_offset = start_offset;
3497 addr_offset < stop_offset; addr_offset += onaline / opb)
3498 {
3499 bfd_size_type j;
3500
3501 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
3502 count = strlen (buf);
3503 if ((size_t) count >= sizeof (buf))
3504 abort ();
3505
3506 putchar (' ');
3507 while (count < width)
3508 {
3509 putchar ('0');
3510 count++;
3511 }
3512 fputs (buf + count - width, stdout);
3513 putchar (' ');
3514
3515 for (j = addr_offset * opb;
3516 j < addr_offset * opb + onaline; j++)
3517 {
3518 if (j < stop_offset * opb)
3519 printf ("%02x", (unsigned) (data[j]));
3520 else
3521 printf (" ");
3522 if ((j & 3) == 3)
3523 printf (" ");
3524 }
3525
3526 printf (" ");
3527 for (j = addr_offset * opb;
3528 j < addr_offset * opb + onaline; j++)
3529 {
3530 if (j >= stop_offset * opb)
3531 printf (" ");
3532 else
3533 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
3534 }
3535 putchar ('\n');
3536 }
3537 free (data);
3538 }
3539
3540 /* Actually display the various requested regions. */
3541
3542 static void
3543 dump_data (bfd *abfd)
3544 {
3545 bfd_map_over_sections (abfd, dump_section, NULL);
3546 }
3547
3548 /* Should perhaps share code and display with nm? */
3549
3550 static void
3551 dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean dynamic)
3552 {
3553 asymbol **current;
3554 long max_count;
3555 long count;
3556
3557 if (dynamic)
3558 {
3559 current = dynsyms;
3560 max_count = dynsymcount;
3561 printf ("DYNAMIC SYMBOL TABLE:\n");
3562 }
3563 else
3564 {
3565 current = syms;
3566 max_count = symcount;
3567 printf ("SYMBOL TABLE:\n");
3568 }
3569
3570 if (max_count == 0)
3571 printf (_("no symbols\n"));
3572
3573 for (count = 0; count < max_count; count++)
3574 {
3575 bfd *cur_bfd;
3576
3577 if (*current == NULL)
3578 printf (_("no information for symbol number %ld\n"), count);
3579
3580 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
3581 printf (_("could not determine the type of symbol number %ld\n"),
3582 count);
3583
3584 else if (process_section_p ((* current)->section)
3585 && (dump_special_syms
3586 || !bfd_is_target_special_symbol (cur_bfd, *current)))
3587 {
3588 const char *name = (*current)->name;
3589
3590 if (do_demangle && name != NULL && *name != '\0')
3591 {
3592 char *alloc;
3593
3594 /* If we want to demangle the name, we demangle it
3595 here, and temporarily clobber it while calling
3596 bfd_print_symbol. FIXME: This is a gross hack. */
3597 alloc = bfd_demangle (cur_bfd, name, demangle_flags);
3598 if (alloc != NULL)
3599 (*current)->name = alloc;
3600 bfd_print_symbol (cur_bfd, stdout, *current,
3601 bfd_print_symbol_all);
3602 if (alloc != NULL)
3603 {
3604 (*current)->name = name;
3605 free (alloc);
3606 }
3607 }
3608 else
3609 bfd_print_symbol (cur_bfd, stdout, *current,
3610 bfd_print_symbol_all);
3611 printf ("\n");
3612 }
3613
3614 current++;
3615 }
3616 printf ("\n\n");
3617 }
3618 \f
3619 static void
3620 dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
3621 {
3622 arelent **p;
3623 char *last_filename, *last_functionname;
3624 unsigned int last_line;
3625 unsigned int last_discriminator;
3626
3627 /* Get column headers lined up reasonably. */
3628 {
3629 static int width;
3630
3631 if (width == 0)
3632 {
3633 char buf[30];
3634
3635 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
3636 width = strlen (buf) - 7;
3637 }
3638 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
3639 }
3640
3641 last_filename = NULL;
3642 last_functionname = NULL;
3643 last_line = 0;
3644 last_discriminator = 0;
3645
3646 for (p = relpp; relcount && *p != NULL; p++, relcount--)
3647 {
3648 arelent *q = *p;
3649 const char *filename, *functionname;
3650 unsigned int linenumber;
3651 unsigned int discriminator;
3652 const char *sym_name;
3653 const char *section_name;
3654 bfd_vma addend2 = 0;
3655
3656 if (start_address != (bfd_vma) -1
3657 && q->address < start_address)
3658 continue;
3659 if (stop_address != (bfd_vma) -1
3660 && q->address > stop_address)
3661 continue;
3662
3663 if (with_line_numbers
3664 && sec != NULL
3665 && bfd_find_nearest_line_discriminator (abfd, sec, syms, q->address,
3666 &filename, &functionname,
3667 &linenumber, &discriminator))
3668 {
3669 if (functionname != NULL
3670 && (last_functionname == NULL
3671 || strcmp (functionname, last_functionname) != 0))
3672 {
3673 printf ("%s():\n", sanitize_string (functionname));
3674 if (last_functionname != NULL)
3675 free (last_functionname);
3676 last_functionname = xstrdup (functionname);
3677 }
3678
3679 if (linenumber > 0
3680 && (linenumber != last_line
3681 || (filename != NULL
3682 && last_filename != NULL
3683 && filename_cmp (filename, last_filename) != 0)
3684 || (discriminator != last_discriminator)))
3685 {
3686 if (discriminator > 0)
3687 printf ("%s:%u\n", filename == NULL ? "???" :
3688 sanitize_string (filename), linenumber);
3689 else
3690 printf ("%s:%u (discriminator %u)\n",
3691 filename == NULL ? "???" : sanitize_string (filename),
3692 linenumber, discriminator);
3693 last_line = linenumber;
3694 last_discriminator = discriminator;
3695 if (last_filename != NULL)
3696 free (last_filename);
3697 if (filename == NULL)
3698 last_filename = NULL;
3699 else
3700 last_filename = xstrdup (filename);
3701 }
3702 }
3703
3704 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
3705 {
3706 sym_name = (*(q->sym_ptr_ptr))->name;
3707 section_name = (*(q->sym_ptr_ptr))->section->name;
3708 }
3709 else
3710 {
3711 sym_name = NULL;
3712 section_name = NULL;
3713 }
3714
3715 bfd_printf_vma (abfd, q->address);
3716 if (q->howto == NULL)
3717 printf (" *unknown* ");
3718 else if (q->howto->name)
3719 {
3720 const char *name = q->howto->name;
3721
3722 /* R_SPARC_OLO10 relocations contain two addends.
3723 But because 'arelent' lacks enough storage to
3724 store them both, the 64-bit ELF Sparc backend
3725 records this as two relocations. One R_SPARC_LO10
3726 and one R_SPARC_13, both pointing to the same
3727 address. This is merely so that we have some
3728 place to store both addend fields.
3729
3730 Undo this transformation, otherwise the output
3731 will be confusing. */
3732 if (abfd->xvec->flavour == bfd_target_elf_flavour
3733 && elf_tdata(abfd)->elf_header->e_machine == EM_SPARCV9
3734 && relcount > 1
3735 && !strcmp (q->howto->name, "R_SPARC_LO10"))
3736 {
3737 arelent *q2 = *(p + 1);
3738 if (q2 != NULL
3739 && q2->howto
3740 && q->address == q2->address
3741 && !strcmp (q2->howto->name, "R_SPARC_13"))
3742 {
3743 name = "R_SPARC_OLO10";
3744 addend2 = q2->addend;
3745 p++;
3746 }
3747 }
3748 printf (" %-16s ", name);
3749 }
3750 else
3751 printf (" %-16d ", q->howto->type);
3752
3753 if (sym_name)
3754 {
3755 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
3756 }
3757 else
3758 {
3759 if (section_name == NULL)
3760 section_name = "*unknown*";
3761 printf ("[%s]", sanitize_string (section_name));
3762 }
3763
3764 if (q->addend)
3765 {
3766 bfd_signed_vma addend = q->addend;
3767 if (addend < 0)
3768 {
3769 printf ("-0x");
3770 addend = -addend;
3771 }
3772 else
3773 printf ("+0x");
3774 bfd_printf_vma (abfd, addend);
3775 }
3776 if (addend2)
3777 {
3778 printf ("+0x");
3779 bfd_printf_vma (abfd, addend2);
3780 }
3781
3782 printf ("\n");
3783 }
3784
3785 if (last_filename != NULL)
3786 free (last_filename);
3787 if (last_functionname != NULL)
3788 free (last_functionname);
3789 }
3790
3791 static void
3792 dump_relocs_in_section (bfd *abfd,
3793 asection *section,
3794 void *dummy ATTRIBUTE_UNUSED)
3795 {
3796 arelent **relpp = NULL;
3797 long relcount;
3798 long relsize;
3799
3800 if ( bfd_is_abs_section (section)
3801 || bfd_is_und_section (section)
3802 || bfd_is_com_section (section)
3803 || (! process_section_p (section))
3804 || ((section->flags & SEC_RELOC) == 0))
3805 return;
3806
3807 printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section->name));
3808
3809 relsize = bfd_get_reloc_upper_bound (abfd, section);
3810 if (relsize == 0)
3811 {
3812 printf (" (none)\n\n");
3813 return;
3814 }
3815
3816 if (relsize < 0)
3817 relcount = relsize;
3818 else
3819 {
3820 relpp = (arelent **) xmalloc (relsize);
3821 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
3822 }
3823
3824 if (relcount < 0)
3825 {
3826 printf ("\n");
3827 non_fatal (_("failed to read relocs in: %s"),
3828 sanitize_string (bfd_get_filename (abfd)));
3829 bfd_fatal (_("error message was"));
3830 }
3831 else if (relcount == 0)
3832 printf (" (none)\n\n");
3833 else
3834 {
3835 printf ("\n");
3836 dump_reloc_set (abfd, section, relpp, relcount);
3837 printf ("\n\n");
3838 }
3839 free (relpp);
3840 }
3841
3842 static void
3843 dump_relocs (bfd *abfd)
3844 {
3845 bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
3846 }
3847
3848 static void
3849 dump_dynamic_relocs (bfd *abfd)
3850 {
3851 long relsize;
3852 arelent **relpp;
3853 long relcount;
3854
3855 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
3856 if (relsize < 0)
3857 bfd_fatal (bfd_get_filename (abfd));
3858
3859 printf ("DYNAMIC RELOCATION RECORDS");
3860
3861 if (relsize == 0)
3862 printf (" (none)\n\n");
3863 else
3864 {
3865 relpp = (arelent **) xmalloc (relsize);
3866 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
3867
3868 if (relcount < 0)
3869 bfd_fatal (bfd_get_filename (abfd));
3870 else if (relcount == 0)
3871 printf (" (none)\n\n");
3872 else
3873 {
3874 printf ("\n");
3875 dump_reloc_set (abfd, NULL, relpp, relcount);
3876 printf ("\n\n");
3877 }
3878 free (relpp);
3879 }
3880 }
3881
3882 /* Creates a table of paths, to search for source files. */
3883
3884 static void
3885 add_include_path (const char *path)
3886 {
3887 if (path[0] == 0)
3888 return;
3889 include_path_count++;
3890 include_paths = (const char **)
3891 xrealloc (include_paths, include_path_count * sizeof (*include_paths));
3892 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3893 if (path[1] == ':' && path[2] == 0)
3894 path = concat (path, ".", (const char *) 0);
3895 #endif
3896 include_paths[include_path_count - 1] = path;
3897 }
3898
3899 static void
3900 adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
3901 asection *section,
3902 void *arg)
3903 {
3904 if ((section->flags & SEC_DEBUGGING) == 0)
3905 {
3906 bfd_boolean *has_reloc_p = (bfd_boolean *) arg;
3907 section->vma += adjust_section_vma;
3908 if (*has_reloc_p)
3909 section->lma += adjust_section_vma;
3910 }
3911 }
3912
3913 /* Return the sign-extended form of an ARCH_SIZE sized VMA. */
3914
3915 static bfd_vma
3916 sign_extend_address (bfd *abfd ATTRIBUTE_UNUSED,
3917 bfd_vma vma,
3918 unsigned arch_size)
3919 {
3920 bfd_vma mask;
3921 mask = (bfd_vma) 1 << (arch_size - 1);
3922 return (((vma & ((mask << 1) - 1)) ^ mask) - mask);
3923 }
3924
3925 /* Dump selected contents of ABFD. */
3926
3927 static void
3928 dump_bfd (bfd *abfd, bfd_boolean is_mainfile)
3929 {
3930 const struct elf_backend_data * bed;
3931
3932 if (bfd_big_endian (abfd))
3933 byte_get = byte_get_big_endian;
3934 else if (bfd_little_endian (abfd))
3935 byte_get = byte_get_little_endian;
3936 else
3937 byte_get = NULL;
3938
3939 /* Load any separate debug information files.
3940 We do this now and without checking do_follow_links because separate
3941 debug info files may contain symbol tables that we will need when
3942 displaying information about the main file. Any memory allocated by
3943 load_separate_debug_files will be released when we call
3944 free_debug_memory below.
3945
3946 The test on is_mainfile is there because the chain of separate debug
3947 info files is a global variable shared by all invocations of dump_bfd. */
3948 if (is_mainfile)
3949 {
3950 load_separate_debug_files (abfd, bfd_get_filename (abfd));
3951
3952 /* If asked to do so, recursively dump the separate files. */
3953 if (do_follow_links)
3954 {
3955 separate_info * i;
3956
3957 for (i = first_separate_info; i != NULL; i = i->next)
3958 dump_bfd (i->handle, FALSE);
3959 }
3960 }
3961
3962 /* Adjust user-specified start and stop limits for targets that use
3963 signed addresses. */
3964 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
3965 && (bed = get_elf_backend_data (abfd)) != NULL
3966 && bed->sign_extend_vma)
3967 {
3968 start_address = sign_extend_address (abfd, start_address,
3969 bed->s->arch_size);
3970 stop_address = sign_extend_address (abfd, stop_address,
3971 bed->s->arch_size);
3972 }
3973
3974 /* If we are adjusting section VMA's, change them all now. Changing
3975 the BFD information is a hack. However, we must do it, or
3976 bfd_find_nearest_line will not do the right thing. */
3977 if (adjust_section_vma != 0)
3978 {
3979 bfd_boolean has_reloc = (abfd->flags & HAS_RELOC);
3980 bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
3981 }
3982
3983 if (! dump_debugging_tags && ! suppress_bfd_header)
3984 printf (_("\n%s: file format %s\n"),
3985 sanitize_string (bfd_get_filename (abfd)),
3986 abfd->xvec->name);
3987 if (dump_ar_hdrs)
3988 print_arelt_descr (stdout, abfd, TRUE, FALSE);
3989 if (dump_file_header)
3990 dump_bfd_header (abfd);
3991 if (dump_private_headers)
3992 dump_bfd_private_header (abfd);
3993 if (dump_private_options != NULL)
3994 dump_target_specific (abfd);
3995 if (! dump_debugging_tags && ! suppress_bfd_header)
3996 putchar ('\n');
3997
3998 if (dump_symtab
3999 || dump_reloc_info
4000 || disassemble
4001 || dump_debugging
4002 || dump_dwarf_section_info)
4003 {
4004 syms = slurp_symtab (abfd);
4005
4006 /* If following links, load any symbol tables from the linked files as well. */
4007 if (do_follow_links && is_mainfile)
4008 {
4009 separate_info * i;
4010
4011 for (i = first_separate_info; i != NULL; i = i->next)
4012 {
4013 asymbol ** extra_syms;
4014 long old_symcount = symcount;
4015
4016 extra_syms = slurp_symtab (i->handle);
4017
4018 if (extra_syms)
4019 {
4020 if (old_symcount == 0)
4021 {
4022 syms = extra_syms;
4023 }
4024 else
4025 {
4026 syms = xrealloc (syms, (symcount + old_symcount) * sizeof (asymbol *));
4027 memcpy (syms + old_symcount,
4028 extra_syms,
4029 symcount * sizeof (asymbol *));
4030 }
4031 }
4032
4033 symcount += old_symcount;
4034 }
4035 }
4036 }
4037
4038 if (dump_section_headers)
4039 dump_headers (abfd);
4040
4041 if (dump_dynamic_symtab || dump_dynamic_reloc_info
4042 || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
4043 dynsyms = slurp_dynamic_symtab (abfd);
4044
4045 if (disassemble)
4046 {
4047 synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
4048 dynsymcount, dynsyms, &synthsyms);
4049 if (synthcount < 0)
4050 synthcount = 0;
4051 }
4052
4053 if (dump_symtab)
4054 dump_symbols (abfd, FALSE);
4055 if (dump_dynamic_symtab)
4056 dump_symbols (abfd, TRUE);
4057 if (dump_dwarf_section_info)
4058 dump_dwarf (abfd);
4059 if (dump_ctf_section_info)
4060 dump_ctf (abfd, dump_ctf_section_name, dump_ctf_parent_name);
4061 if (dump_stab_section_info)
4062 dump_stabs (abfd);
4063 if (dump_reloc_info && ! disassemble)
4064 dump_relocs (abfd);
4065 if (dump_dynamic_reloc_info && ! disassemble)
4066 dump_dynamic_relocs (abfd);
4067 if (dump_section_contents)
4068 dump_data (abfd);
4069 if (disassemble)
4070 disassemble_data (abfd);
4071
4072 if (dump_debugging)
4073 {
4074 void *dhandle;
4075
4076 dhandle = read_debugging_info (abfd, syms, symcount, TRUE);
4077 if (dhandle != NULL)
4078 {
4079 if (!print_debugging_info (stdout, dhandle, abfd, syms,
4080 bfd_demangle,
4081 dump_debugging_tags ? TRUE : FALSE))
4082 {
4083 non_fatal (_("%s: printing debugging information failed"),
4084 bfd_get_filename (abfd));
4085 exit_status = 1;
4086 }
4087
4088 free (dhandle);
4089 }
4090 /* PR 6483: If there was no STABS debug info in the file, try
4091 DWARF instead. */
4092 else if (! dump_dwarf_section_info)
4093 {
4094 dwarf_select_sections_all ();
4095 dump_dwarf (abfd);
4096 }
4097 }
4098
4099 if (syms)
4100 {
4101 free (syms);
4102 syms = NULL;
4103 }
4104
4105 if (dynsyms)
4106 {
4107 free (dynsyms);
4108 dynsyms = NULL;
4109 }
4110
4111 if (synthsyms)
4112 {
4113 free (synthsyms);
4114 synthsyms = NULL;
4115 }
4116
4117 symcount = 0;
4118 dynsymcount = 0;
4119 synthcount = 0;
4120
4121 if (is_mainfile)
4122 free_debug_memory ();
4123 }
4124
4125 static void
4126 display_object_bfd (bfd *abfd)
4127 {
4128 char **matching;
4129
4130 if (bfd_check_format_matches (abfd, bfd_object, &matching))
4131 {
4132 dump_bfd (abfd, TRUE);
4133 return;
4134 }
4135
4136 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
4137 {
4138 nonfatal (bfd_get_filename (abfd));
4139 list_matching_formats (matching);
4140 free (matching);
4141 return;
4142 }
4143
4144 if (bfd_get_error () != bfd_error_file_not_recognized)
4145 {
4146 nonfatal (bfd_get_filename (abfd));
4147 return;
4148 }
4149
4150 if (bfd_check_format_matches (abfd, bfd_core, &matching))
4151 {
4152 dump_bfd (abfd, TRUE);
4153 return;
4154 }
4155
4156 nonfatal (bfd_get_filename (abfd));
4157
4158 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
4159 {
4160 list_matching_formats (matching);
4161 free (matching);
4162 }
4163 }
4164
4165 static void
4166 display_any_bfd (bfd *file, int level)
4167 {
4168 /* Decompress sections unless dumping the section contents. */
4169 if (!dump_section_contents)
4170 file->flags |= BFD_DECOMPRESS;
4171
4172 /* If the file is an archive, process all of its elements. */
4173 if (bfd_check_format (file, bfd_archive))
4174 {
4175 bfd *arfile = NULL;
4176 bfd *last_arfile = NULL;
4177
4178 if (level == 0)
4179 printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file)));
4180 else if (level > 100)
4181 {
4182 /* Prevent corrupted files from spinning us into an
4183 infinite loop. 100 is an arbitrary heuristic. */
4184 fatal (_("Archive nesting is too deep"));
4185 return;
4186 }
4187 else
4188 printf (_("In nested archive %s:\n"),
4189 sanitize_string (bfd_get_filename (file)));
4190
4191 for (;;)
4192 {
4193 bfd_set_error (bfd_error_no_error);
4194
4195 arfile = bfd_openr_next_archived_file (file, arfile);
4196 if (arfile == NULL)
4197 {
4198 if (bfd_get_error () != bfd_error_no_more_archived_files)
4199 nonfatal (bfd_get_filename (file));
4200 break;
4201 }
4202
4203 display_any_bfd (arfile, level + 1);
4204
4205 if (last_arfile != NULL)
4206 {
4207 bfd_close (last_arfile);
4208 /* PR 17512: file: ac585d01. */
4209 if (arfile == last_arfile)
4210 {
4211 last_arfile = NULL;
4212 break;
4213 }
4214 }
4215 last_arfile = arfile;
4216 }
4217
4218 if (last_arfile != NULL)
4219 bfd_close (last_arfile);
4220 }
4221 else
4222 display_object_bfd (file);
4223 }
4224
4225 static void
4226 display_file (char *filename, char *target, bfd_boolean last_file)
4227 {
4228 bfd *file;
4229
4230 if (get_file_size (filename) < 1)
4231 {
4232 exit_status = 1;
4233 return;
4234 }
4235
4236 file = bfd_openr (filename, target);
4237 if (file == NULL)
4238 {
4239 nonfatal (filename);
4240 return;
4241 }
4242
4243 display_any_bfd (file, 0);
4244
4245 /* This is an optimization to improve the speed of objdump, especially when
4246 dumping a file with lots of associated debug informatiom. Calling
4247 bfd_close on such a file can take a non-trivial amount of time as there
4248 are lots of lists to walk and buffers to free. This is only really
4249 necessary however if we are about to load another file and we need the
4250 memory back. Otherwise, if we are about to exit, then we can save (a lot
4251 of) time by only doing a quick close, and allowing the OS to reclaim the
4252 memory for us. */
4253 if (! last_file)
4254 bfd_close (file);
4255 else
4256 bfd_close_all_done (file);
4257 }
4258 \f
4259 int
4260 main (int argc, char **argv)
4261 {
4262 int c;
4263 char *target = default_target;
4264 bfd_boolean seenflag = FALSE;
4265
4266 #if defined (HAVE_SETLOCALE)
4267 #if defined (HAVE_LC_MESSAGES)
4268 setlocale (LC_MESSAGES, "");
4269 #endif
4270 setlocale (LC_CTYPE, "");
4271 #endif
4272
4273 bindtextdomain (PACKAGE, LOCALEDIR);
4274 textdomain (PACKAGE);
4275
4276 program_name = *argv;
4277 xmalloc_set_program_name (program_name);
4278 bfd_set_error_program_name (program_name);
4279
4280 START_PROGRESS (program_name, 0);
4281
4282 expandargv (&argc, &argv);
4283
4284 if (bfd_init () != BFD_INIT_MAGIC)
4285 fatal (_("fatal error: libbfd ABI mismatch"));
4286 set_default_bfd_target ();
4287
4288 while ((c = getopt_long (argc, argv,
4289 "pP:ib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::",
4290 long_options, (int *) 0))
4291 != EOF)
4292 {
4293 switch (c)
4294 {
4295 case 0:
4296 break; /* We've been given a long option. */
4297 case 'm':
4298 machine = optarg;
4299 break;
4300 case 'M':
4301 {
4302 char *options;
4303 if (disassembler_options)
4304 /* Ignore potential memory leak for now. */
4305 options = concat (disassembler_options, ",",
4306 optarg, (const char *) NULL);
4307 else
4308 options = optarg;
4309 disassembler_options = remove_whitespace_and_extra_commas (options);
4310 }
4311 break;
4312 case 'j':
4313 add_only (optarg);
4314 break;
4315 case 'F':
4316 display_file_offsets = TRUE;
4317 break;
4318 case 'l':
4319 with_line_numbers = TRUE;
4320 break;
4321 case 'b':
4322 target = optarg;
4323 break;
4324 case 'C':
4325 do_demangle = TRUE;
4326 if (optarg != NULL)
4327 {
4328 enum demangling_styles style;
4329
4330 style = cplus_demangle_name_to_style (optarg);
4331 if (style == unknown_demangling)
4332 fatal (_("unknown demangling style `%s'"),
4333 optarg);
4334
4335 cplus_demangle_set_style (style);
4336 }
4337 break;
4338 case OPTION_RECURSE_LIMIT:
4339 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
4340 break;
4341 case OPTION_NO_RECURSE_LIMIT:
4342 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
4343 break;
4344 case 'w':
4345 do_wide = wide_output = TRUE;
4346 break;
4347 case OPTION_ADJUST_VMA:
4348 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
4349 break;
4350 case OPTION_START_ADDRESS:
4351 start_address = parse_vma (optarg, "--start-address");
4352 if ((stop_address != (bfd_vma) -1) && stop_address <= start_address)
4353 fatal (_("error: the start address should be before the end address"));
4354 break;
4355 case OPTION_STOP_ADDRESS:
4356 stop_address = parse_vma (optarg, "--stop-address");
4357 if ((start_address != (bfd_vma) -1) && stop_address <= start_address)
4358 fatal (_("error: the stop address should be after the start address"));
4359 break;
4360 case OPTION_PREFIX:
4361 prefix = optarg;
4362 prefix_length = strlen (prefix);
4363 /* Remove an unnecessary trailing '/' */
4364 while (IS_DIR_SEPARATOR (prefix[prefix_length - 1]))
4365 prefix_length--;
4366 break;
4367 case OPTION_PREFIX_STRIP:
4368 prefix_strip = atoi (optarg);
4369 if (prefix_strip < 0)
4370 fatal (_("error: prefix strip must be non-negative"));
4371 break;
4372 case OPTION_INSN_WIDTH:
4373 insn_width = strtoul (optarg, NULL, 0);
4374 if (insn_width <= 0)
4375 fatal (_("error: instruction width must be positive"));
4376 break;
4377 case OPTION_INLINES:
4378 unwind_inlines = TRUE;
4379 break;
4380 case 'E':
4381 if (strcmp (optarg, "B") == 0)
4382 endian = BFD_ENDIAN_BIG;
4383 else if (strcmp (optarg, "L") == 0)
4384 endian = BFD_ENDIAN_LITTLE;
4385 else
4386 {
4387 nonfatal (_("unrecognized -E option"));
4388 usage (stderr, 1);
4389 }
4390 break;
4391 case OPTION_ENDIAN:
4392 if (strncmp (optarg, "big", strlen (optarg)) == 0)
4393 endian = BFD_ENDIAN_BIG;
4394 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
4395 endian = BFD_ENDIAN_LITTLE;
4396 else
4397 {
4398 non_fatal (_("unrecognized --endian type `%s'"), optarg);
4399 exit_status = 1;
4400 usage (stderr, 1);
4401 }
4402 break;
4403
4404 case 'f':
4405 dump_file_header = TRUE;
4406 seenflag = TRUE;
4407 break;
4408 case 'i':
4409 formats_info = TRUE;
4410 seenflag = TRUE;
4411 break;
4412 case 'I':
4413 add_include_path (optarg);
4414 break;
4415 case 'p':
4416 dump_private_headers = TRUE;
4417 seenflag = TRUE;
4418 break;
4419 case 'P':
4420 dump_private_options = optarg;
4421 seenflag = TRUE;
4422 break;
4423 case 'x':
4424 dump_private_headers = TRUE;
4425 dump_symtab = TRUE;
4426 dump_reloc_info = TRUE;
4427 dump_file_header = TRUE;
4428 dump_ar_hdrs = TRUE;
4429 dump_section_headers = TRUE;
4430 seenflag = TRUE;
4431 break;
4432 case 't':
4433 dump_symtab = TRUE;
4434 seenflag = TRUE;
4435 break;
4436 case 'T':
4437 dump_dynamic_symtab = TRUE;
4438 seenflag = TRUE;
4439 break;
4440 case 'd':
4441 disassemble = TRUE;
4442 seenflag = TRUE;
4443 disasm_sym = optarg;
4444 break;
4445 case 'z':
4446 disassemble_zeroes = TRUE;
4447 break;
4448 case 'D':
4449 disassemble = TRUE;
4450 disassemble_all = TRUE;
4451 seenflag = TRUE;
4452 break;
4453 case 'S':
4454 disassemble = TRUE;
4455 with_source_code = TRUE;
4456 seenflag = TRUE;
4457 break;
4458 case 'g':
4459 dump_debugging = 1;
4460 seenflag = TRUE;
4461 break;
4462 case 'e':
4463 dump_debugging = 1;
4464 dump_debugging_tags = 1;
4465 do_demangle = TRUE;
4466 seenflag = TRUE;
4467 break;
4468 case 'W':
4469 dump_dwarf_section_info = TRUE;
4470 seenflag = TRUE;
4471 if (optarg)
4472 dwarf_select_sections_by_letters (optarg);
4473 else
4474 dwarf_select_sections_all ();
4475 break;
4476 case OPTION_DWARF:
4477 dump_dwarf_section_info = TRUE;
4478 seenflag = TRUE;
4479 if (optarg)
4480 dwarf_select_sections_by_names (optarg);
4481 else
4482 dwarf_select_sections_all ();
4483 break;
4484 case OPTION_DWARF_DEPTH:
4485 {
4486 char *cp;
4487 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
4488 }
4489 break;
4490 case OPTION_DWARF_START:
4491 {
4492 char *cp;
4493 dwarf_start_die = strtoul (optarg, & cp, 0);
4494 suppress_bfd_header = 1;
4495 }
4496 break;
4497 case OPTION_DWARF_CHECK:
4498 dwarf_check = TRUE;
4499 break;
4500 case OPTION_CTF:
4501 dump_ctf_section_info = TRUE;
4502 dump_ctf_section_name = xstrdup (optarg);
4503 seenflag = TRUE;
4504 break;
4505 case OPTION_CTF_PARENT:
4506 dump_ctf_parent_name = xstrdup (optarg);
4507 break;
4508 case 'G':
4509 dump_stab_section_info = TRUE;
4510 seenflag = TRUE;
4511 break;
4512 case 's':
4513 dump_section_contents = TRUE;
4514 seenflag = TRUE;
4515 break;
4516 case 'r':
4517 dump_reloc_info = TRUE;
4518 seenflag = TRUE;
4519 break;
4520 case 'R':
4521 dump_dynamic_reloc_info = TRUE;
4522 seenflag = TRUE;
4523 break;
4524 case 'a':
4525 dump_ar_hdrs = TRUE;
4526 seenflag = TRUE;
4527 break;
4528 case 'h':
4529 dump_section_headers = TRUE;
4530 seenflag = TRUE;
4531 break;
4532 case 'v':
4533 case 'V':
4534 show_version = TRUE;
4535 seenflag = TRUE;
4536 break;
4537
4538 case 'H':
4539 usage (stdout, 0);
4540 /* No need to set seenflag or to break - usage() does not return. */
4541 default:
4542 usage (stderr, 1);
4543 }
4544 }
4545
4546 if (show_version)
4547 print_version ("objdump");
4548
4549 if (!seenflag)
4550 usage (stderr, 2);
4551
4552 if (formats_info)
4553 exit_status = display_info ();
4554 else
4555 {
4556 if (optind == argc)
4557 display_file ("a.out", target, TRUE);
4558 else
4559 for (; optind < argc;)
4560 {
4561 display_file (argv[optind], target, optind == argc - 1);
4562 optind++;
4563 }
4564 }
4565
4566 free_only_list ();
4567 free (dump_ctf_section_name);
4568 free (dump_ctf_parent_name);
4569
4570 END_PROGRESS (program_name);
4571
4572 return exit_status;
4573 }
This page took 0.185416 seconds and 5 git commands to generate.