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