Commit | Line | Data |
---|---|---|
d20f480f | 1 | /* objdump.c -- dump information about an object file. |
980ef031 | 2 | Copyright 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc. |
2fa0b342 | 3 | |
b3a2b497 | 4 | This file is part of GNU Binutils. |
2fa0b342 | 5 | |
b3a2b497 | 6 | This program is free software; you can redistribute it and/or modify |
2fa0b342 | 7 | it under the terms of the GNU General Public License as published by |
d20f480f | 8 | the Free Software Foundation; either version 2, or (at your option) |
2fa0b342 DHW |
9 | any later version. |
10 | ||
b3a2b497 | 11 | This program is distributed in the hope that it will be useful, |
2fa0b342 DHW |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
b3a2b497 | 17 | along with this program; if not, write to the Free Software |
a65619c8 | 18 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
2fa0b342 | 19 | |
2fa0b342 DHW |
20 | #include "bfd.h" |
21 | #include "getopt.h" | |
be1d162b | 22 | #include "progress.h" |
e1ec9f07 | 23 | #include "bucomm.h" |
2fa0b342 | 24 | #include <ctype.h> |
2e8adbd7 | 25 | #include "dis-asm.h" |
105da05c | 26 | #include "libiberty.h" |
e1c14599 ILT |
27 | #include "debug.h" |
28 | #include "budbg.h" | |
2fa0b342 | 29 | |
7fc01fc9 ILT |
30 | #ifdef ANSI_PROTOTYPES |
31 | #include <stdarg.h> | |
32 | #else | |
33 | #include <varargs.h> | |
34 | #endif | |
35 | ||
73b8f102 JG |
36 | /* Internal headers for the ELF .stab-dump code - sorry. */ |
37 | #define BYTES_IN_WORD 32 | |
38 | #include "aout/aout64.h" | |
bf661056 | 39 | |
746cffcf ILT |
40 | #ifdef NEED_DECLARATION_FPRINTF |
41 | /* This is needed by INIT_DISASSEMBLE_INFO. */ | |
7fc01fc9 | 42 | extern int fprintf PARAMS ((FILE *, const char *, ...)); |
80d19ec1 | 43 | #endif |
2fa0b342 DHW |
44 | |
45 | char *default_target = NULL; /* default at runtime */ | |
46 | ||
e1ec9f07 | 47 | extern char *program_version; |
2fa0b342 | 48 | |
249c6fc0 | 49 | int show_version = 0; /* show the version number */ |
2fa0b342 DHW |
50 | int dump_section_contents; /* -s */ |
51 | int dump_section_headers; /* -h */ | |
52 | boolean dump_file_header; /* -f */ | |
53 | int dump_symtab; /* -t */ | |
de3b08ac | 54 | int dump_dynamic_symtab; /* -T */ |
2fa0b342 | 55 | int dump_reloc_info; /* -r */ |
de3b08ac | 56 | int dump_dynamic_reloc_info; /* -R */ |
2fa0b342 | 57 | int dump_ar_hdrs; /* -a */ |
a65619c8 | 58 | int dump_private_headers; /* -p */ |
aa0a709a | 59 | int with_line_numbers; /* -l */ |
be1d162b | 60 | boolean with_source_code; /* -S */ |
458bbd1f | 61 | int show_raw_insn; /* --show-raw-insn */ |
9b018ecd | 62 | int dump_stab_section_info; /* --stabs */ |
aa0a709a | 63 | boolean disassemble; /* -d */ |
d5464baa | 64 | boolean disassemble_all; /* -D */ |
e1ec9f07 | 65 | boolean formats_info; /* -i */ |
195d1adf | 66 | char *only; /* -j secname */ |
13e4db2e | 67 | int wide_output; /* -w */ |
aa21a2a9 ILT |
68 | bfd_vma start_address = (bfd_vma) -1; /* --start-address */ |
69 | bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */ | |
e1c14599 | 70 | int dump_debugging; /* --debugging */ |
195d1adf | 71 | |
cef35d48 | 72 | /* Extra info to pass to the disassembler address printing function. */ |
195d1adf KR |
73 | struct objdump_disasm_info { |
74 | bfd *abfd; | |
75 | asection *sec; | |
8b129785 | 76 | boolean require_sec; |
195d1adf | 77 | }; |
2fa0b342 | 78 | |
cef35d48 | 79 | /* Architecture to disassemble for, or default if NULL. */ |
aa0a709a | 80 | char *machine = (char *) NULL; |
f7b839f7 DM |
81 | |
82 | /* The symbol table. */ | |
aa0a709a | 83 | asymbol **syms; |
2fa0b342 | 84 | |
f7b839f7 | 85 | /* Number of symbols in `syms'. */ |
ae5d2ff5 | 86 | long symcount = 0; |
2fa0b342 | 87 | |
be1d162b ILT |
88 | /* The sorted symbol table. */ |
89 | asymbol **sorted_syms; | |
90 | ||
91 | /* Number of symbols in `sorted_syms'. */ | |
92 | long sorted_symcount = 0; | |
93 | ||
de3b08ac ILT |
94 | /* The dynamic symbol table. */ |
95 | asymbol **dynsyms; | |
96 | ||
97 | /* Number of symbols in `dynsyms'. */ | |
98 | long dynsymcount = 0; | |
99 | ||
d9971b83 KR |
100 | /* Forward declarations. */ |
101 | ||
102 | static void | |
103 | display_file PARAMS ((char *filename, char *target)); | |
104 | ||
105 | static void | |
106 | dump_data PARAMS ((bfd *abfd)); | |
107 | ||
108 | static void | |
109 | dump_relocs PARAMS ((bfd *abfd)); | |
110 | ||
111 | static void | |
de3b08ac ILT |
112 | dump_dynamic_relocs PARAMS ((bfd * abfd)); |
113 | ||
114 | static void | |
115 | dump_reloc_set PARAMS ((bfd *, arelent **, long)); | |
116 | ||
117 | static void | |
118 | dump_symbols PARAMS ((bfd *abfd, boolean dynamic)); | |
02a68547 ILT |
119 | |
120 | static void | |
121 | display_bfd PARAMS ((bfd *abfd)); | |
8f197c94 | 122 | |
c5ba2759 | 123 | static void |
7fc01fc9 | 124 | objdump_print_value PARAMS ((bfd_vma, struct disassemble_info *)); |
c5ba2759 | 125 | |
8f197c94 ILT |
126 | static void |
127 | objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *)); | |
be1d162b ILT |
128 | |
129 | static void | |
130 | show_line PARAMS ((bfd *, asection *, bfd_vma)); | |
e1c14599 ILT |
131 | |
132 | static const char * | |
133 | endian_string PARAMS ((enum bfd_endian)); | |
d9971b83 | 134 | \f |
2fa0b342 | 135 | void |
b3a2b497 ILT |
136 | usage (stream, status) |
137 | FILE *stream; | |
138 | int status; | |
2fa0b342 | 139 | { |
b3a2b497 | 140 | fprintf (stream, "\ |
a65619c8 | 141 | Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\ |
e1c14599 | 142 | [--archive-headers] [--target=bfdname] [--debugging] [--disassemble]\n\ |
d5464baa | 143 | [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\ |
13e4db2e SC |
144 | [--info] [--section=section-name] [--line-numbers] [--source]\n", |
145 | program_name); | |
146 | fprintf (stream, "\ | |
d5464baa ILT |
147 | [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\ |
148 | [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\ | |
aa21a2a9 | 149 | [--wide] [--version] [--help] [--private-headers]\n\ |
458bbd1f DE |
150 | [--start-address=addr] [--stop-address=addr]\n\ |
151 | [--show-raw-insn] objfile...\n\ | |
13e4db2e | 152 | at least one option besides -l (--line-numbers) must be given\n"); |
be1d162b | 153 | list_supported_targets (program_name, stream); |
b3a2b497 | 154 | exit (status); |
2fa0b342 DHW |
155 | } |
156 | ||
aa21a2a9 ILT |
157 | /* 150 isn't special; it's just an arbitrary non-ASCII char value. */ |
158 | ||
159 | #define OPTION_START_ADDRESS (150) | |
160 | #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1) | |
161 | ||
aa0a709a SC |
162 | static struct option long_options[]= |
163 | { | |
02a68547 | 164 | {"all-headers", no_argument, NULL, 'x'}, |
a65619c8 | 165 | {"private-headers", no_argument, NULL, 'p'}, |
02a68547 ILT |
166 | {"architecture", required_argument, NULL, 'm'}, |
167 | {"archive-headers", no_argument, NULL, 'a'}, | |
e1c14599 | 168 | {"debugging", no_argument, &dump_debugging, 1}, |
02a68547 | 169 | {"disassemble", no_argument, NULL, 'd'}, |
d5464baa | 170 | {"disassemble-all", no_argument, NULL, 'D'}, |
de3b08ac ILT |
171 | {"dynamic-reloc", no_argument, NULL, 'R'}, |
172 | {"dynamic-syms", no_argument, NULL, 'T'}, | |
02a68547 ILT |
173 | {"file-headers", no_argument, NULL, 'f'}, |
174 | {"full-contents", no_argument, NULL, 's'}, | |
175 | {"headers", no_argument, NULL, 'h'}, | |
176 | {"help", no_argument, NULL, 'H'}, | |
177 | {"info", no_argument, NULL, 'i'}, | |
178 | {"line-numbers", no_argument, NULL, 'l'}, | |
179 | {"reloc", no_argument, NULL, 'r'}, | |
180 | {"section", required_argument, NULL, 'j'}, | |
181 | {"section-headers", no_argument, NULL, 'h'}, | |
458bbd1f | 182 | {"show-raw-insn", no_argument, &show_raw_insn, 1}, |
be1d162b | 183 | {"source", no_argument, NULL, 'S'}, |
73b8f102 | 184 | {"stabs", no_argument, &dump_stab_section_info, 1}, |
aa21a2a9 ILT |
185 | {"start-address", required_argument, NULL, OPTION_START_ADDRESS}, |
186 | {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS}, | |
02a68547 ILT |
187 | {"syms", no_argument, NULL, 't'}, |
188 | {"target", required_argument, NULL, 'b'}, | |
aa21a2a9 | 189 | {"version", no_argument, &show_version, 1}, |
13e4db2e | 190 | {"wide", no_argument, &wide_output, 'w'}, |
d2442698 DM |
191 | {0, no_argument, 0, 0} |
192 | }; | |
f7b839f7 | 193 | \f |
2fa0b342 | 194 | static void |
f7b839f7 | 195 | dump_section_header (abfd, section, ignored) |
aa0a709a | 196 | bfd *abfd; |
f7b839f7 DM |
197 | asection *section; |
198 | PTR ignored; | |
2fa0b342 | 199 | { |
f7b839f7 | 200 | char *comma = ""; |
aa0a709a | 201 | |
38d7c012 | 202 | printf ("%3d %-13s %08lx ", section->index, |
1efb2ef0 ILT |
203 | bfd_get_section_name (abfd, section), |
204 | (unsigned long) bfd_section_size (abfd, section)); | |
205 | printf_vma (bfd_get_section_vma (abfd, section)); | |
206 | printf (" "); | |
207 | printf_vma (section->lma); | |
208 | printf (" %08lx 2**%u", section->filepos, | |
209 | bfd_get_section_alignment (abfd, section)); | |
210 | if (! wide_output) | |
211 | printf ("\n "); | |
212 | printf (" "); | |
f7b839f7 | 213 | |
1efb2ef0 ILT |
214 | #define PF(x, y) \ |
215 | if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; } | |
f7b839f7 | 216 | |
1efb2ef0 | 217 | PF (SEC_HAS_CONTENTS, "CONTENTS"); |
f7b839f7 DM |
218 | PF (SEC_ALLOC, "ALLOC"); |
219 | PF (SEC_CONSTRUCTOR, "CONSTRUCTOR"); | |
220 | PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT"); | |
221 | PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA"); | |
222 | PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS"); | |
223 | PF (SEC_LOAD, "LOAD"); | |
224 | PF (SEC_RELOC, "RELOC"); | |
195d1adf | 225 | #ifdef SEC_BALIGN |
f7b839f7 | 226 | PF (SEC_BALIGN, "BALIGN"); |
195d1adf | 227 | #endif |
f7b839f7 DM |
228 | PF (SEC_READONLY, "READONLY"); |
229 | PF (SEC_CODE, "CODE"); | |
230 | PF (SEC_DATA, "DATA"); | |
231 | PF (SEC_ROM, "ROM"); | |
232 | PF (SEC_DEBUGGING, "DEBUGGING"); | |
28d1b01e | 233 | PF (SEC_NEVER_LOAD, "NEVER_LOAD"); |
83f4323e | 234 | PF (SEC_EXCLUDE, "EXCLUDE"); |
38d7c012 ILT |
235 | PF (SEC_SORT_ENTRIES, "SORT_ENTRIES"); |
236 | ||
237 | if ((section->flags & SEC_LINK_ONCE) != 0) | |
238 | { | |
239 | const char *ls; | |
240 | ||
241 | switch (section->flags & SEC_LINK_DUPLICATES) | |
242 | { | |
243 | default: | |
244 | abort (); | |
245 | case SEC_LINK_DUPLICATES_DISCARD: | |
246 | ls = "LINK_ONCE_DISCARD"; | |
247 | break; | |
248 | case SEC_LINK_DUPLICATES_ONE_ONLY: | |
249 | ls = "LINK_ONCE_ONE_ONLY"; | |
250 | break; | |
251 | case SEC_LINK_DUPLICATES_SAME_SIZE: | |
252 | ls = "LINK_ONCE_SAME_SIZE"; | |
253 | break; | |
254 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: | |
255 | ls = "LINK_ONCE_SAME_CONTENTS"; | |
256 | break; | |
257 | } | |
258 | printf ("%s%s", comma, ls); | |
259 | comma = ", "; | |
260 | } | |
261 | ||
f7b839f7 | 262 | printf ("\n"); |
2fa0b342 | 263 | #undef PF |
2fa0b342 DHW |
264 | } |
265 | ||
f7b839f7 DM |
266 | static void |
267 | dump_headers (abfd) | |
268 | bfd *abfd; | |
269 | { | |
1efb2ef0 ILT |
270 | printf ("Sections:\n"); |
271 | #ifndef BFD64 | |
272 | printf ("Idx Name Size VMA LMA File off Algn\n"); | |
273 | #else | |
274 | printf ("Idx Name Size VMA LMA File off Algn\n"); | |
275 | #endif | |
f7b839f7 DM |
276 | bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL); |
277 | } | |
278 | \f | |
2fa0b342 | 279 | static asymbol ** |
abdcac0f DM |
280 | slurp_symtab (abfd) |
281 | bfd *abfd; | |
2fa0b342 | 282 | { |
aa0a709a | 283 | asymbol **sy = (asymbol **) NULL; |
ae5d2ff5 | 284 | long storage; |
2fa0b342 | 285 | |
aa0a709a SC |
286 | if (!(bfd_get_file_flags (abfd) & HAS_SYMS)) |
287 | { | |
f7b839f7 | 288 | printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd)); |
c5ba2759 | 289 | symcount = 0; |
f7b839f7 | 290 | return NULL; |
aa0a709a SC |
291 | } |
292 | ||
ae5d2ff5 ILT |
293 | storage = bfd_get_symtab_upper_bound (abfd); |
294 | if (storage < 0) | |
295 | bfd_fatal (bfd_get_filename (abfd)); | |
296 | ||
aa0a709a SC |
297 | if (storage) |
298 | { | |
02a68547 | 299 | sy = (asymbol **) xmalloc (storage); |
aa0a709a SC |
300 | } |
301 | symcount = bfd_canonicalize_symtab (abfd, sy); | |
ae5d2ff5 ILT |
302 | if (symcount < 0) |
303 | bfd_fatal (bfd_get_filename (abfd)); | |
304 | if (symcount == 0) | |
de3b08ac ILT |
305 | fprintf (stderr, "%s: %s: No symbols\n", |
306 | program_name, bfd_get_filename (abfd)); | |
307 | return sy; | |
308 | } | |
309 | ||
310 | /* Read in the dynamic symbols. */ | |
311 | ||
312 | static asymbol ** | |
313 | slurp_dynamic_symtab (abfd) | |
314 | bfd *abfd; | |
315 | { | |
316 | asymbol **sy = (asymbol **) NULL; | |
317 | long storage; | |
318 | ||
de3b08ac ILT |
319 | storage = bfd_get_dynamic_symtab_upper_bound (abfd); |
320 | if (storage < 0) | |
28d1b01e ILT |
321 | { |
322 | if (!(bfd_get_file_flags (abfd) & DYNAMIC)) | |
323 | { | |
324 | fprintf (stderr, "%s: %s: not a dynamic object\n", | |
325 | program_name, bfd_get_filename (abfd)); | |
c5ba2759 | 326 | dynsymcount = 0; |
28d1b01e ILT |
327 | return NULL; |
328 | } | |
329 | ||
330 | bfd_fatal (bfd_get_filename (abfd)); | |
331 | } | |
de3b08ac ILT |
332 | |
333 | if (storage) | |
334 | { | |
335 | sy = (asymbol **) xmalloc (storage); | |
336 | } | |
337 | dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy); | |
338 | if (dynsymcount < 0) | |
339 | bfd_fatal (bfd_get_filename (abfd)); | |
340 | if (dynsymcount == 0) | |
341 | fprintf (stderr, "%s: %s: No dynamic symbols\n", | |
342 | program_name, bfd_get_filename (abfd)); | |
aa0a709a | 343 | return sy; |
2fa0b342 | 344 | } |
aa0a709a | 345 | |
f7b839f7 DM |
346 | /* Filter out (in place) symbols that are useless for disassembly. |
347 | COUNT is the number of elements in SYMBOLS. | |
348 | Return the number of useful symbols. */ | |
3ae36cb6 | 349 | |
ae5d2ff5 | 350 | long |
f7b839f7 DM |
351 | remove_useless_symbols (symbols, count) |
352 | asymbol **symbols; | |
ae5d2ff5 | 353 | long count; |
3ae36cb6 | 354 | { |
f7b839f7 | 355 | register asymbol **in_ptr = symbols, **out_ptr = symbols; |
3ae36cb6 | 356 | |
f7b839f7 | 357 | while (--count >= 0) |
3ae36cb6 PB |
358 | { |
359 | asymbol *sym = *in_ptr++; | |
360 | ||
361 | if (sym->name == NULL || sym->name[0] == '\0') | |
362 | continue; | |
363 | if (sym->flags & (BSF_DEBUGGING)) | |
364 | continue; | |
28d1b01e | 365 | if (bfd_is_und_section (sym->section) |
3ae36cb6 PB |
366 | || bfd_is_com_section (sym->section)) |
367 | continue; | |
368 | ||
369 | *out_ptr++ = sym; | |
370 | } | |
f7b839f7 | 371 | return out_ptr - symbols; |
3ae36cb6 PB |
372 | } |
373 | ||
e1c14599 | 374 | /* Sort symbols into value order. */ |
37853673 | 375 | |
aa0a709a | 376 | static int |
37853673 | 377 | compare_symbols (ap, bp) |
d5464baa ILT |
378 | const PTR ap; |
379 | const PTR bp; | |
2fa0b342 | 380 | { |
d5464baa ILT |
381 | const asymbol *a = *(const asymbol **)ap; |
382 | const asymbol *b = *(const asymbol **)bp; | |
db552bda ILT |
383 | const char *an, *bn; |
384 | size_t anl, bnl; | |
385 | boolean af, bf; | |
e1c14599 | 386 | flagword aflags, bflags; |
2fa0b342 | 387 | |
be1d162b | 388 | if (bfd_asymbol_value (a) > bfd_asymbol_value (b)) |
3ae36cb6 | 389 | return 1; |
be1d162b | 390 | else if (bfd_asymbol_value (a) < bfd_asymbol_value (b)) |
3ae36cb6 | 391 | return -1; |
2fa0b342 | 392 | |
3ae36cb6 PB |
393 | if (a->section > b->section) |
394 | return 1; | |
395 | else if (a->section < b->section) | |
396 | return -1; | |
db552bda ILT |
397 | |
398 | an = bfd_asymbol_name (a); | |
399 | bn = bfd_asymbol_name (b); | |
400 | anl = strlen (an); | |
401 | bnl = strlen (bn); | |
402 | ||
403 | /* The symbols gnu_compiled and gcc2_compiled convey no real | |
404 | information, so put them after other symbols with the same value. */ | |
405 | ||
406 | af = (strstr (an, "gnu_compiled") != NULL | |
407 | || strstr (an, "gcc2_compiled") != NULL); | |
408 | bf = (strstr (bn, "gnu_compiled") != NULL | |
409 | || strstr (bn, "gcc2_compiled") != NULL); | |
410 | ||
411 | if (af && ! bf) | |
412 | return 1; | |
413 | if (! af && bf) | |
414 | return -1; | |
415 | ||
416 | /* We use a heuristic for the file name, to try to sort it after | |
417 | more useful symbols. It may not work on non Unix systems, but it | |
418 | doesn't really matter; the only difference is precisely which | |
419 | symbol names get printed. */ | |
420 | ||
421 | #define file_symbol(s, sn, snl) \ | |
422 | (((s)->flags & BSF_FILE) != 0 \ | |
423 | || ((sn)[(snl) - 2] == '.' \ | |
424 | && ((sn)[(snl) - 1] == 'o' \ | |
425 | || (sn)[(snl) - 1] == 'a'))) | |
426 | ||
427 | af = file_symbol (a, an, anl); | |
428 | bf = file_symbol (b, bn, bnl); | |
429 | ||
430 | if (af && ! bf) | |
431 | return 1; | |
432 | if (! af && bf) | |
433 | return -1; | |
434 | ||
e1c14599 ILT |
435 | /* Finally, try to sort global symbols before local symbols before |
436 | debugging symbols. */ | |
437 | ||
438 | aflags = a->flags; | |
439 | bflags = b->flags; | |
440 | ||
441 | if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING)) | |
442 | { | |
443 | if ((aflags & BSF_DEBUGGING) != 0) | |
444 | return 1; | |
445 | else | |
446 | return -1; | |
447 | } | |
448 | if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL)) | |
449 | { | |
450 | if ((aflags & BSF_LOCAL) != 0) | |
451 | return 1; | |
452 | else | |
453 | return -1; | |
454 | } | |
455 | ||
3ae36cb6 | 456 | return 0; |
2fa0b342 DHW |
457 | } |
458 | ||
d5464baa ILT |
459 | /* Sort relocs into address order. */ |
460 | ||
461 | static int | |
462 | compare_relocs (ap, bp) | |
463 | const PTR ap; | |
464 | const PTR bp; | |
465 | { | |
466 | const arelent *a = *(const arelent **)ap; | |
467 | const arelent *b = *(const arelent **)bp; | |
468 | ||
469 | if (a->address > b->address) | |
470 | return 1; | |
471 | else if (a->address < b->address) | |
472 | return -1; | |
473 | ||
a65619c8 SC |
474 | /* So that associated relocations tied to the same address show up |
475 | in the correct order, we don't do any further sorting. */ | |
476 | if (a > b) | |
477 | return 1; | |
478 | else if (a < b) | |
479 | return -1; | |
480 | else | |
481 | return 0; | |
d5464baa ILT |
482 | } |
483 | ||
c5ba2759 ILT |
484 | /* Print VMA to STREAM with no leading zeroes. */ |
485 | ||
486 | static void | |
7fc01fc9 | 487 | objdump_print_value (vma, info) |
c5ba2759 | 488 | bfd_vma vma; |
7fc01fc9 | 489 | struct disassemble_info *info; |
c5ba2759 ILT |
490 | { |
491 | char buf[30]; | |
492 | char *p; | |
493 | ||
494 | sprintf_vma (buf, vma); | |
495 | for (p = buf; *p == '0'; ++p) | |
496 | ; | |
7fc01fc9 | 497 | (*info->fprintf_func) (info->stream, "%s", p); |
c5ba2759 ILT |
498 | } |
499 | ||
f7b839f7 DM |
500 | /* Print VMA symbolically to INFO if possible. */ |
501 | ||
8f197c94 | 502 | static void |
545a2768 | 503 | objdump_print_address (vma, info) |
aa0a709a | 504 | bfd_vma vma; |
545a2768 | 505 | struct disassemble_info *info; |
2fa0b342 | 506 | { |
7fc01fc9 ILT |
507 | char buf[30]; |
508 | ||
195d1adf KR |
509 | /* @@ Would it speed things up to cache the last two symbols returned, |
510 | and maybe their address ranges? For many processors, only one memory | |
511 | operand can be present at a time, so the 2-entry cache wouldn't be | |
512 | constantly churned by code doing heavy memory accesses. */ | |
2fa0b342 | 513 | |
be1d162b | 514 | /* Indices in `sorted_syms'. */ |
ae5d2ff5 | 515 | long min = 0; |
be1d162b | 516 | long max = sorted_symcount; |
ae5d2ff5 | 517 | long thisplace; |
2fa0b342 | 518 | |
7fc01fc9 ILT |
519 | sprintf_vma (buf, vma); |
520 | (*info->fprintf_func) (info->stream, "%s", buf); | |
3ae36cb6 | 521 | |
be1d162b | 522 | if (sorted_symcount < 1) |
f7b839f7 DM |
523 | return; |
524 | ||
8f197c94 ILT |
525 | /* Perform a binary search looking for the closest symbol to the |
526 | required value. We are searching the range (min, max]. */ | |
527 | while (min + 1 < max) | |
aa0a709a | 528 | { |
f7b839f7 | 529 | asymbol *sym; |
8f197c94 | 530 | |
f7b839f7 | 531 | thisplace = (max + min) / 2; |
be1d162b | 532 | sym = sorted_syms[thisplace]; |
8f197c94 | 533 | |
13e4db2e | 534 | if (bfd_asymbol_value (sym) > vma) |
f7b839f7 | 535 | max = thisplace; |
13e4db2e | 536 | else if (bfd_asymbol_value (sym) < vma) |
f7b839f7 DM |
537 | min = thisplace; |
538 | else | |
aa0a709a | 539 | { |
8f197c94 ILT |
540 | min = thisplace; |
541 | break; | |
aa0a709a | 542 | } |
f7b839f7 | 543 | } |
fc5d6074 | 544 | |
8f197c94 | 545 | /* The symbol we want is now in min, the low end of the range we |
e1c14599 ILT |
546 | were searching. If there are several symbols with the same |
547 | value, we want the first one. */ | |
8f197c94 | 548 | thisplace = min; |
db552bda ILT |
549 | while (thisplace > 0 |
550 | && (bfd_asymbol_value (sorted_syms[thisplace]) | |
551 | == bfd_asymbol_value (sorted_syms[thisplace - 1]))) | |
552 | --thisplace; | |
8f197c94 | 553 | |
f7b839f7 DM |
554 | { |
555 | /* If the file is relocateable, and the symbol could be from this | |
556 | section, prefer a symbol from this section over symbols from | |
557 | others, even if the other symbol's value might be closer. | |
558 | ||
559 | Note that this may be wrong for some symbol references if the | |
560 | sections have overlapping memory ranges, but in that case there's | |
561 | no way to tell what's desired without looking at the relocation | |
562 | table. */ | |
563 | struct objdump_disasm_info *aux; | |
ae5d2ff5 | 564 | long i; |
f7b839f7 DM |
565 | |
566 | aux = (struct objdump_disasm_info *) info->application_data; | |
be1d162b | 567 | if (sorted_syms[thisplace]->section != aux->sec |
2d054641 ILT |
568 | && (aux->require_sec |
569 | || ((aux->abfd->flags & HAS_RELOC) != 0 | |
570 | && vma >= bfd_get_section_vma (aux->abfd, aux->sec) | |
571 | && vma < (bfd_get_section_vma (aux->abfd, aux->sec) | |
c5ba2759 | 572 | + bfd_section_size (aux->abfd, aux->sec))))) |
f7b839f7 | 573 | { |
be1d162b | 574 | for (i = thisplace + 1; i < sorted_symcount; i++) |
8f197c94 | 575 | { |
be1d162b ILT |
576 | if (bfd_asymbol_value (sorted_syms[i]) |
577 | != bfd_asymbol_value (sorted_syms[thisplace])) | |
28d1b01e | 578 | break; |
8f197c94 | 579 | } |
28d1b01e | 580 | --i; |
8f197c94 ILT |
581 | for (; i >= 0; i--) |
582 | { | |
db552bda ILT |
583 | if (sorted_syms[i]->section == aux->sec |
584 | && (i == 0 | |
585 | || sorted_syms[i - 1]->section != aux->sec | |
586 | || (bfd_asymbol_value (sorted_syms[i]) | |
587 | != bfd_asymbol_value (sorted_syms[i - 1])))) | |
8f197c94 ILT |
588 | { |
589 | thisplace = i; | |
590 | break; | |
591 | } | |
592 | } | |
2d054641 | 593 | |
be1d162b | 594 | if (sorted_syms[thisplace]->section != aux->sec) |
2d054641 ILT |
595 | { |
596 | /* We didn't find a good symbol with a smaller value. | |
597 | Look for one with a larger value. */ | |
be1d162b | 598 | for (i = thisplace + 1; i < sorted_symcount; i++) |
2d054641 | 599 | { |
be1d162b | 600 | if (sorted_syms[i]->section == aux->sec) |
2d054641 ILT |
601 | { |
602 | thisplace = i; | |
603 | break; | |
604 | } | |
605 | } | |
606 | } | |
c5ba2759 ILT |
607 | |
608 | if (sorted_syms[thisplace]->section != aux->sec | |
609 | && (aux->require_sec | |
610 | || ((aux->abfd->flags & HAS_RELOC) != 0 | |
611 | && vma >= bfd_get_section_vma (aux->abfd, aux->sec) | |
612 | && vma < (bfd_get_section_vma (aux->abfd, aux->sec) | |
613 | + bfd_section_size (aux->abfd, aux->sec))))) | |
614 | { | |
615 | bfd_vma secaddr; | |
616 | ||
7fc01fc9 ILT |
617 | (*info->fprintf_func) (info->stream, " <%s", |
618 | bfd_get_section_name (aux->abfd, aux->sec)); | |
c5ba2759 ILT |
619 | secaddr = bfd_get_section_vma (aux->abfd, aux->sec); |
620 | if (vma < secaddr) | |
621 | { | |
7fc01fc9 ILT |
622 | (*info->fprintf_func) (info->stream, "-"); |
623 | objdump_print_value (secaddr - vma, info); | |
c5ba2759 ILT |
624 | } |
625 | else if (vma > secaddr) | |
626 | { | |
7fc01fc9 ILT |
627 | (*info->fprintf_func) (info->stream, "+"); |
628 | objdump_print_value (vma - secaddr, info); | |
c5ba2759 | 629 | } |
7fc01fc9 | 630 | (*info->fprintf_func) (info->stream, ">"); |
c5ba2759 ILT |
631 | return; |
632 | } | |
195d1adf | 633 | } |
f7b839f7 | 634 | } |
2d054641 | 635 | |
7fc01fc9 | 636 | (*info->fprintf_func) (info->stream, " <%s", sorted_syms[thisplace]->name); |
be1d162b | 637 | if (bfd_asymbol_value (sorted_syms[thisplace]) > vma) |
f7b839f7 | 638 | { |
7fc01fc9 | 639 | (*info->fprintf_func) (info->stream, "-"); |
c5ba2759 | 640 | objdump_print_value (bfd_asymbol_value (sorted_syms[thisplace]) - vma, |
7fc01fc9 | 641 | info); |
f7b839f7 | 642 | } |
be1d162b | 643 | else if (vma > bfd_asymbol_value (sorted_syms[thisplace])) |
f7b839f7 | 644 | { |
7fc01fc9 | 645 | (*info->fprintf_func) (info->stream, "+"); |
c5ba2759 | 646 | objdump_print_value (vma - bfd_asymbol_value (sorted_syms[thisplace]), |
7fc01fc9 | 647 | info); |
2fa0b342 | 648 | } |
7fc01fc9 | 649 | (*info->fprintf_func) (info->stream, ">"); |
2fa0b342 DHW |
650 | } |
651 | ||
be1d162b ILT |
652 | /* Hold the last function name and the last line number we displayed |
653 | in a disassembly. */ | |
654 | ||
655 | static char *prev_functionname; | |
656 | static unsigned int prev_line; | |
657 | ||
658 | /* We keep a list of all files that we have seen when doing a | |
659 | dissassembly with source, so that we know how much of the file to | |
660 | display. This can be important for inlined functions. */ | |
661 | ||
662 | struct print_file_list | |
663 | { | |
664 | struct print_file_list *next; | |
665 | char *filename; | |
666 | unsigned int line; | |
667 | FILE *f; | |
668 | }; | |
669 | ||
670 | static struct print_file_list *print_files; | |
671 | ||
672 | /* The number of preceding context lines to show when we start | |
673 | displaying a file for the first time. */ | |
674 | ||
675 | #define SHOW_PRECEDING_CONTEXT_LINES (5) | |
676 | ||
677 | /* Skip ahead to a given line in a file, optionally printing each | |
678 | line. */ | |
679 | ||
680 | static void | |
681 | skip_to_line PARAMS ((struct print_file_list *, unsigned int, boolean)); | |
682 | ||
683 | static void | |
684 | skip_to_line (p, line, show) | |
685 | struct print_file_list *p; | |
686 | unsigned int line; | |
687 | boolean show; | |
688 | { | |
689 | while (p->line < line) | |
690 | { | |
691 | char buf[100]; | |
692 | ||
693 | if (fgets (buf, sizeof buf, p->f) == NULL) | |
694 | { | |
695 | fclose (p->f); | |
696 | p->f = NULL; | |
697 | break; | |
698 | } | |
699 | ||
700 | if (show) | |
701 | printf ("%s", buf); | |
702 | ||
703 | if (strchr (buf, '\n') != NULL) | |
704 | ++p->line; | |
705 | } | |
706 | } | |
707 | ||
708 | /* Show the line number, or the source line, in a dissassembly | |
709 | listing. */ | |
710 | ||
711 | static void | |
712 | show_line (abfd, section, off) | |
aa0a709a | 713 | bfd *abfd; |
be1d162b ILT |
714 | asection *section; |
715 | bfd_vma off; | |
2fa0b342 | 716 | { |
be1d162b ILT |
717 | CONST char *filename; |
718 | CONST char *functionname; | |
719 | unsigned int line; | |
2e8adbd7 | 720 | |
be1d162b ILT |
721 | if (! with_line_numbers && ! with_source_code) |
722 | return; | |
d20f480f | 723 | |
be1d162b ILT |
724 | if (! bfd_find_nearest_line (abfd, section, syms, off, &filename, |
725 | &functionname, &line)) | |
726 | return; | |
aa0a709a | 727 | |
be1d162b ILT |
728 | if (filename != NULL && *filename == '\0') |
729 | filename = NULL; | |
730 | if (functionname != NULL && *functionname == '\0') | |
731 | functionname = NULL; | |
aa0a709a | 732 | |
be1d162b | 733 | if (with_line_numbers) |
d5464baa | 734 | { |
be1d162b ILT |
735 | if (functionname != NULL |
736 | && (prev_functionname == NULL | |
737 | || strcmp (functionname, prev_functionname) != 0)) | |
738 | printf ("%s():\n", functionname); | |
739 | if (line > 0 && line != prev_line) | |
740 | printf ("%s:%u\n", filename == NULL ? "???" : filename, line); | |
741 | } | |
742 | ||
743 | if (with_source_code | |
744 | && filename != NULL | |
745 | && line > 0) | |
746 | { | |
747 | struct print_file_list **pp, *p; | |
748 | ||
749 | for (pp = &print_files; *pp != NULL; pp = &(*pp)->next) | |
750 | if (strcmp ((*pp)->filename, filename) == 0) | |
751 | break; | |
752 | p = *pp; | |
753 | ||
754 | if (p != NULL) | |
d5464baa | 755 | { |
be1d162b ILT |
756 | if (p != print_files) |
757 | { | |
758 | int l; | |
759 | ||
760 | /* We have reencountered a file name which we saw | |
761 | earlier. This implies that either we are dumping out | |
762 | code from an included file, or the same file was | |
763 | linked in more than once. There are two common cases | |
764 | of an included file: inline functions in a header | |
765 | file, and a bison or flex skeleton file. In the | |
766 | former case we want to just start printing (but we | |
767 | back up a few lines to give context); in the latter | |
768 | case we want to continue from where we left off. I | |
769 | can't think of a good way to distinguish the cases, | |
770 | so I used a heuristic based on the file name. */ | |
771 | if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0) | |
772 | l = p->line; | |
773 | else | |
774 | { | |
775 | l = line - SHOW_PRECEDING_CONTEXT_LINES; | |
776 | if (l <= 0) | |
777 | l = 1; | |
778 | } | |
d5464baa | 779 | |
be1d162b ILT |
780 | if (p->f == NULL) |
781 | { | |
782 | p->f = fopen (p->filename, "r"); | |
783 | p->line = 0; | |
784 | } | |
785 | if (p->f != NULL) | |
786 | skip_to_line (p, l, false); | |
d5464baa | 787 | |
be1d162b ILT |
788 | if (print_files->f != NULL) |
789 | { | |
790 | fclose (print_files->f); | |
791 | print_files->f = NULL; | |
792 | } | |
793 | } | |
d5464baa | 794 | |
be1d162b ILT |
795 | if (p->f != NULL) |
796 | { | |
797 | skip_to_line (p, line, true); | |
798 | *pp = p->next; | |
799 | p->next = print_files; | |
800 | print_files = p; | |
801 | } | |
802 | } | |
803 | else | |
804 | { | |
805 | FILE *f; | |
d5464baa | 806 | |
be1d162b ILT |
807 | f = fopen (filename, "r"); |
808 | if (f != NULL) | |
d5464baa | 809 | { |
be1d162b | 810 | int l; |
d5464baa | 811 | |
be1d162b ILT |
812 | p = ((struct print_file_list *) |
813 | xmalloc (sizeof (struct print_file_list))); | |
814 | p->filename = xmalloc (strlen (filename) + 1); | |
815 | strcpy (p->filename, filename); | |
816 | p->line = 0; | |
817 | p->f = f; | |
818 | ||
819 | if (print_files != NULL && print_files->f != NULL) | |
820 | { | |
821 | fclose (print_files->f); | |
822 | print_files->f = NULL; | |
823 | } | |
824 | p->next = print_files; | |
825 | print_files = p; | |
826 | ||
827 | l = line - SHOW_PRECEDING_CONTEXT_LINES; | |
828 | if (l <= 0) | |
829 | l = 1; | |
830 | skip_to_line (p, l, false); | |
831 | if (p->f != NULL) | |
832 | skip_to_line (p, line, true); | |
d5464baa ILT |
833 | } |
834 | } | |
835 | } | |
836 | ||
be1d162b ILT |
837 | if (functionname != NULL |
838 | && (prev_functionname == NULL | |
839 | || strcmp (functionname, prev_functionname) != 0)) | |
aa0a709a | 840 | { |
be1d162b ILT |
841 | if (prev_functionname != NULL) |
842 | free (prev_functionname); | |
843 | prev_functionname = xmalloc (strlen (functionname) + 1); | |
844 | strcpy (prev_functionname, functionname); | |
aa0a709a | 845 | } |
2fa0b342 | 846 | |
be1d162b ILT |
847 | if (line > 0 && line != prev_line) |
848 | prev_line = line; | |
849 | } | |
850 | ||
7fc01fc9 ILT |
851 | /* Pseudo FILE object for strings. */ |
852 | typedef struct { | |
853 | char *buffer; | |
854 | char *current; | |
855 | } SFILE; | |
856 | ||
857 | /* sprintf to a "stream" */ | |
858 | ||
859 | #ifdef ANSI_PROTOTYPES | |
860 | static int | |
861 | objdump_sprintf (SFILE *f, const char *format, ...) | |
862 | { | |
863 | int n; | |
864 | va_list args; | |
865 | ||
866 | va_start (args, format); | |
867 | vsprintf (f->current, format, args); | |
868 | f->current += n = strlen (f->current); | |
869 | va_end (args); | |
870 | return n; | |
871 | } | |
872 | #else | |
873 | static int | |
874 | objdump_sprintf (va_alist) | |
875 | va_dcl | |
876 | { | |
877 | int n; | |
878 | SFILE *f; | |
879 | const char *format; | |
880 | va_list args; | |
881 | ||
882 | va_start (args); | |
883 | f = va_arg (args, SFILE *); | |
884 | format = va_arg (args, const char *); | |
885 | vsprintf (f->current, format, args); | |
886 | f->current += n = strlen (f->current); | |
887 | va_end (args); | |
888 | return n; | |
889 | } | |
890 | #endif | |
891 | ||
be1d162b ILT |
892 | void |
893 | disassemble_data (abfd) | |
894 | bfd *abfd; | |
895 | { | |
896 | long i; | |
be1d162b ILT |
897 | disassembler_ftype disassemble_fn = 0; /* New style */ |
898 | struct disassemble_info disasm_info; | |
899 | struct objdump_disasm_info aux; | |
900 | asection *section; | |
901 | boolean done_dot = false; | |
7fc01fc9 ILT |
902 | char buf[200]; |
903 | SFILE sfile; | |
be1d162b ILT |
904 | |
905 | print_files = NULL; | |
906 | prev_functionname = NULL; | |
907 | prev_line = -1; | |
908 | ||
909 | /* We make a copy of syms to sort. We don't want to sort syms | |
910 | because that will screw up the relocs. */ | |
911 | sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *)); | |
912 | memcpy (sorted_syms, syms, symcount * sizeof (asymbol *)); | |
913 | ||
914 | sorted_symcount = remove_useless_symbols (sorted_syms, symcount); | |
2fa0b342 DHW |
915 | |
916 | /* Sort the symbols into section and symbol order */ | |
be1d162b | 917 | qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols); |
2fa0b342 | 918 | |
4e050e3b | 919 | INIT_DISASSEMBLE_INFO(disasm_info, stdout, fprintf); |
cef35d48 DM |
920 | disasm_info.application_data = (PTR) &aux; |
921 | aux.abfd = abfd; | |
922 | disasm_info.print_address_func = objdump_print_address; | |
923 | ||
aa0a709a SC |
924 | if (machine != (char *) NULL) |
925 | { | |
db552bda | 926 | const bfd_arch_info_type *info = bfd_scan_arch (machine); |
cef35d48 | 927 | if (info == NULL) |
aa0a709a SC |
928 | { |
929 | fprintf (stderr, "%s: Can't use supplied machine %s\n", | |
930 | program_name, | |
931 | machine); | |
932 | exit (1); | |
933 | } | |
934 | abfd->arch_info = info; | |
2fa0b342 | 935 | } |
e779a58c | 936 | |
db552bda ILT |
937 | disassemble_fn = disassembler (abfd); |
938 | if (!disassemble_fn) | |
aa0a709a | 939 | { |
db552bda ILT |
940 | fprintf (stderr, "%s: Can't disassemble for architecture %s\n", |
941 | program_name, | |
942 | bfd_printable_arch_mach (bfd_get_arch (abfd), 0)); | |
943 | exit (1); | |
aa0a709a | 944 | } |
2fa0b342 | 945 | |
458bbd1f DE |
946 | disasm_info.arch = bfd_get_arch (abfd); |
947 | disasm_info.mach = bfd_get_mach (abfd); | |
948 | if (bfd_big_endian (abfd)) | |
949 | disasm_info.endian = BFD_ENDIAN_BIG; | |
38aa863c | 950 | else if (bfd_little_endian (abfd)) |
458bbd1f | 951 | disasm_info.endian = BFD_ENDIAN_LITTLE; |
38aa863c DE |
952 | else |
953 | /* ??? Aborting here seems too drastic. We could default to big or little | |
954 | instead. */ | |
955 | disasm_info.endian = BFD_ENDIAN_UNKNOWN; | |
458bbd1f | 956 | |
2fa0b342 | 957 | for (section = abfd->sections; |
aa0a709a SC |
958 | section != (asection *) NULL; |
959 | section = section->next) | |
65cceb78 | 960 | { |
cef35d48 DM |
961 | bfd_byte *data = NULL; |
962 | bfd_size_type datasize = 0; | |
be1d162b | 963 | arelent **relbuf = NULL; |
d5464baa ILT |
964 | arelent **relpp = NULL; |
965 | arelent **relppend = NULL; | |
aa21a2a9 | 966 | long stop; |
2fa0b342 | 967 | |
d5464baa ILT |
968 | if ((section->flags & SEC_LOAD) == 0 |
969 | || (! disassemble_all | |
970 | && only == NULL | |
971 | && (section->flags & SEC_CODE) == 0)) | |
cef35d48 DM |
972 | continue; |
973 | if (only != (char *) NULL && strcmp (only, section->name) != 0) | |
974 | continue; | |
2fa0b342 | 975 | |
d5464baa ILT |
976 | if (dump_reloc_info |
977 | && (section->flags & SEC_RELOC) != 0) | |
978 | { | |
be1d162b ILT |
979 | long relsize; |
980 | ||
981 | relsize = bfd_get_reloc_upper_bound (abfd, section); | |
982 | if (relsize < 0) | |
983 | bfd_fatal (bfd_get_filename (abfd)); | |
984 | ||
985 | if (relsize > 0) | |
986 | { | |
987 | long relcount; | |
988 | ||
989 | relbuf = (arelent **) xmalloc (relsize); | |
990 | relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms); | |
991 | if (relcount < 0) | |
992 | bfd_fatal (bfd_get_filename (abfd)); | |
993 | ||
994 | /* Sort the relocs by address. */ | |
995 | qsort (relbuf, relcount, sizeof (arelent *), compare_relocs); | |
996 | ||
997 | relpp = relbuf; | |
998 | relppend = relpp + relcount; | |
999 | } | |
d5464baa ILT |
1000 | } |
1001 | ||
cef35d48 | 1002 | printf ("Disassembly of section %s:\n", section->name); |
2fa0b342 | 1003 | |
cef35d48 DM |
1004 | datasize = bfd_get_section_size_before_reloc (section); |
1005 | if (datasize == 0) | |
1006 | continue; | |
2fa0b342 | 1007 | |
cef35d48 | 1008 | data = (bfd_byte *) xmalloc ((size_t) datasize); |
2fa0b342 | 1009 | |
cef35d48 | 1010 | bfd_get_section_contents (abfd, section, data, 0, datasize); |
2fa0b342 | 1011 | |
cef35d48 DM |
1012 | aux.sec = section; |
1013 | disasm_info.buffer = data; | |
1014 | disasm_info.buffer_vma = section->vma; | |
1015 | disasm_info.buffer_length = datasize; | |
aa21a2a9 ILT |
1016 | if (start_address == (bfd_vma) -1 |
1017 | || start_address < disasm_info.buffer_vma) | |
1018 | i = 0; | |
1019 | else | |
1020 | i = start_address - disasm_info.buffer_vma; | |
1021 | if (stop_address == (bfd_vma) -1) | |
1022 | stop = datasize; | |
1023 | else | |
1024 | { | |
1025 | if (stop_address < disasm_info.buffer_vma) | |
1026 | stop = 0; | |
1027 | else | |
1028 | stop = stop_address - disasm_info.buffer_vma; | |
1029 | if (stop > disasm_info.buffer_length) | |
1030 | stop = disasm_info.buffer_length; | |
1031 | } | |
1032 | while (i < stop) | |
cef35d48 | 1033 | { |
d5464baa | 1034 | int bytes; |
13e4db2e | 1035 | boolean need_nl = false; |
d5464baa | 1036 | |
cef35d48 DM |
1037 | if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 && |
1038 | data[i + 3] == 0) | |
aa0a709a | 1039 | { |
cef35d48 | 1040 | if (done_dot == false) |
aa0a709a | 1041 | { |
cef35d48 DM |
1042 | printf ("...\n"); |
1043 | done_dot = true; | |
65cceb78 | 1044 | } |
d5464baa | 1045 | bytes = 4; |
cef35d48 DM |
1046 | } |
1047 | else | |
1048 | { | |
1049 | done_dot = false; | |
be1d162b ILT |
1050 | if (with_line_numbers || with_source_code) |
1051 | show_line (abfd, section, i); | |
8b129785 | 1052 | aux.require_sec = true; |
cef35d48 | 1053 | objdump_print_address (section->vma + i, &disasm_info); |
8b129785 | 1054 | aux.require_sec = false; |
cef35d48 | 1055 | putchar (' '); |
65cceb78 | 1056 | |
7fc01fc9 ILT |
1057 | sfile.buffer = sfile.current = buf; |
1058 | disasm_info.fprintf_func = (fprintf_ftype) objdump_sprintf; | |
1059 | disasm_info.stream = (FILE *) &sfile; | |
db552bda | 1060 | bytes = (*disassemble_fn) (section->vma + i, &disasm_info); |
7fc01fc9 ILT |
1061 | disasm_info.fprintf_func = (fprintf_ftype) fprintf; |
1062 | disasm_info.stream = stdout; | |
db552bda ILT |
1063 | if (bytes < 0) |
1064 | break; | |
1065 | ||
7fc01fc9 ILT |
1066 | if (show_raw_insn) |
1067 | { | |
1068 | long j; | |
1069 | for (j = i; j < i + bytes; ++j) | |
1070 | { | |
1071 | printf ("%02x", (unsigned) data[j]); | |
1072 | putchar (' '); | |
1073 | } | |
1074 | /* Separate raw data from instruction by extra space. */ | |
1075 | putchar (' '); | |
1076 | } | |
1077 | ||
1078 | printf ("%s", sfile.buffer); | |
1079 | ||
13e4db2e SC |
1080 | if (!wide_output) |
1081 | putchar ('\n'); | |
1082 | else | |
1083 | need_nl = true; | |
96d7950b | 1084 | } |
d5464baa ILT |
1085 | |
1086 | if (dump_reloc_info | |
1087 | && (section->flags & SEC_RELOC) != 0) | |
1088 | { | |
1089 | while (relpp < relppend | |
746cffcf ILT |
1090 | && ((*relpp)->address >= (bfd_vma) i |
1091 | && (*relpp)->address < (bfd_vma) i + bytes)) | |
d5464baa ILT |
1092 | { |
1093 | arelent *q; | |
1094 | const char *sym_name; | |
1095 | ||
1096 | q = *relpp; | |
1097 | ||
1098 | printf ("\t\tRELOC: "); | |
1099 | ||
1100 | printf_vma (section->vma + q->address); | |
1101 | ||
1102 | printf (" %s ", q->howto->name); | |
1103 | ||
1104 | if (q->sym_ptr_ptr != NULL | |
1105 | && *q->sym_ptr_ptr != NULL) | |
1106 | { | |
1107 | sym_name = bfd_asymbol_name (*q->sym_ptr_ptr); | |
1108 | if (sym_name == NULL || *sym_name == '\0') | |
1109 | { | |
1110 | asection *sym_sec; | |
1111 | ||
1112 | sym_sec = bfd_get_section (*q->sym_ptr_ptr); | |
1113 | sym_name = bfd_get_section_name (abfd, sym_sec); | |
1114 | if (sym_name == NULL || *sym_name == '\0') | |
1115 | sym_name = "*unknown*"; | |
1116 | } | |
1117 | } | |
38d7c012 ILT |
1118 | else |
1119 | sym_name = "*unknown*"; | |
d5464baa ILT |
1120 | |
1121 | printf ("%s", sym_name); | |
1122 | ||
1123 | if (q->addend) | |
1124 | { | |
1125 | printf ("+0x"); | |
1126 | printf_vma (q->addend); | |
1127 | } | |
1128 | ||
1129 | printf ("\n"); | |
13e4db2e | 1130 | need_nl = false; |
d5464baa ILT |
1131 | ++relpp; |
1132 | } | |
1133 | } | |
1134 | ||
13e4db2e SC |
1135 | if (need_nl) |
1136 | printf ("\n"); | |
1137 | ||
d5464baa | 1138 | i += bytes; |
96d7950b | 1139 | } |
be1d162b | 1140 | |
cef35d48 | 1141 | free (data); |
be1d162b ILT |
1142 | if (relbuf != NULL) |
1143 | free (relbuf); | |
2fa0b342 | 1144 | } |
c5ba2759 | 1145 | free (sorted_syms); |
2fa0b342 | 1146 | } |
73b8f102 | 1147 | \f |
73b8f102 JG |
1148 | |
1149 | /* Define a table of stab values and print-strings. We wish the initializer | |
1150 | could be a direct-mapped table, but instead we build one the first | |
1151 | time we need it. */ | |
1152 | ||
250e36fe DM |
1153 | void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name, |
1154 | char *strsect_name)); | |
249c6fc0 | 1155 | |
250e36fe | 1156 | /* Dump the stabs sections from an object file that has a section that |
73b8f102 JG |
1157 | uses Sun stabs encoding. It has to use some hooks into BFD because |
1158 | string table sections are not normally visible to BFD callers. */ | |
1159 | ||
1160 | void | |
9b018ecd | 1161 | dump_stabs (abfd) |
73b8f102 JG |
1162 | bfd *abfd; |
1163 | { | |
250e36fe DM |
1164 | dump_section_stabs (abfd, ".stab", ".stabstr"); |
1165 | dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr"); | |
1166 | dump_section_stabs (abfd, ".stab.index", ".stab.indexstr"); | |
1167 | dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$"); | |
249c6fc0 RS |
1168 | } |
1169 | ||
250e36fe DM |
1170 | static struct internal_nlist *stabs; |
1171 | static bfd_size_type stab_size; | |
1172 | ||
1173 | static char *strtab; | |
1174 | static bfd_size_type stabstr_size; | |
1175 | ||
1176 | /* Read ABFD's stabs section STABSECT_NAME into `stabs' | |
1177 | and string table section STRSECT_NAME into `strtab'. | |
1178 | If the section exists and was read, allocate the space and return true. | |
1179 | Otherwise return false. */ | |
1180 | ||
1181 | boolean | |
1182 | read_section_stabs (abfd, stabsect_name, strsect_name) | |
249c6fc0 | 1183 | bfd *abfd; |
250e36fe DM |
1184 | char *stabsect_name; |
1185 | char *strsect_name; | |
249c6fc0 | 1186 | { |
9b018ecd | 1187 | asection *stabsect, *stabstrsect; |
9b018ecd | 1188 | |
d5671c53 ILT |
1189 | stabsect = bfd_get_section_by_name (abfd, stabsect_name); |
1190 | if (0 == stabsect) | |
73b8f102 | 1191 | { |
250e36fe DM |
1192 | printf ("No %s section present\n\n", stabsect_name); |
1193 | return false; | |
73b8f102 JG |
1194 | } |
1195 | ||
d5671c53 ILT |
1196 | stabstrsect = bfd_get_section_by_name (abfd, strsect_name); |
1197 | if (0 == stabstrsect) | |
73b8f102 | 1198 | { |
eae82145 | 1199 | fprintf (stderr, "%s: %s has no %s section\n", program_name, |
250e36fe DM |
1200 | bfd_get_filename (abfd), strsect_name); |
1201 | return false; | |
73b8f102 | 1202 | } |
9b018ecd | 1203 | |
d5671c53 ILT |
1204 | stab_size = bfd_section_size (abfd, stabsect); |
1205 | stabstr_size = bfd_section_size (abfd, stabstrsect); | |
73b8f102 | 1206 | |
9b018ecd ILT |
1207 | stabs = (struct internal_nlist *) xmalloc (stab_size); |
1208 | strtab = (char *) xmalloc (stabstr_size); | |
73b8f102 | 1209 | |
d5671c53 | 1210 | if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size)) |
9b018ecd | 1211 | { |
d5671c53 ILT |
1212 | fprintf (stderr, "%s: Reading %s section of %s failed: %s\n", |
1213 | program_name, stabsect_name, bfd_get_filename (abfd), | |
1214 | bfd_errmsg (bfd_get_error ())); | |
1215 | free (stabs); | |
1216 | free (strtab); | |
1217 | return false; | |
73b8f102 | 1218 | } |
2fa0b342 | 1219 | |
d5671c53 ILT |
1220 | if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0, |
1221 | stabstr_size)) | |
9b018ecd | 1222 | { |
d5671c53 ILT |
1223 | fprintf (stderr, "%s: Reading %s section of %s failed: %s\n", |
1224 | program_name, strsect_name, bfd_get_filename (abfd), | |
1225 | bfd_errmsg (bfd_get_error ())); | |
1226 | free (stabs); | |
1227 | free (strtab); | |
1228 | return false; | |
73b8f102 | 1229 | } |
d5671c53 | 1230 | |
250e36fe DM |
1231 | return true; |
1232 | } | |
73b8f102 JG |
1233 | |
1234 | #define SWAP_SYMBOL(symp, abfd) \ | |
250e36fe | 1235 | { \ |
73b8f102 | 1236 | (symp)->n_strx = bfd_h_get_32(abfd, \ |
250e36fe | 1237 | (unsigned char *)&(symp)->n_strx); \ |
73b8f102 | 1238 | (symp)->n_desc = bfd_h_get_16 (abfd, \ |
250e36fe | 1239 | (unsigned char *)&(symp)->n_desc); \ |
73b8f102 | 1240 | (symp)->n_value = bfd_h_get_32 (abfd, \ |
250e36fe DM |
1241 | (unsigned char *)&(symp)->n_value); \ |
1242 | } | |
73b8f102 | 1243 | |
250e36fe DM |
1244 | /* Print ABFD's stabs section STABSECT_NAME (in `stabs'), |
1245 | using string table section STRSECT_NAME (in `strtab'). */ | |
1246 | ||
1247 | void | |
1248 | print_section_stabs (abfd, stabsect_name, strsect_name) | |
1249 | bfd *abfd; | |
1250 | char *stabsect_name; | |
1251 | char *strsect_name; | |
1252 | { | |
1253 | int i; | |
1254 | unsigned file_string_table_offset = 0, next_file_string_table_offset = 0; | |
1255 | struct internal_nlist *stabp = stabs, | |
1256 | *stabs_end = (struct internal_nlist *) (stab_size + (char *) stabs); | |
73b8f102 | 1257 | |
250e36fe DM |
1258 | printf ("Contents of %s section:\n\n", stabsect_name); |
1259 | printf ("Symnum n_type n_othr n_desc n_value n_strx String\n"); | |
249c6fc0 RS |
1260 | |
1261 | /* Loop through all symbols and print them. | |
1262 | ||
1263 | We start the index at -1 because there is a dummy symbol on | |
250e36fe | 1264 | the front of stabs-in-{coff,elf} sections that supplies sizes. */ |
249c6fc0 | 1265 | |
250e36fe | 1266 | for (i = -1; stabp < stabs_end; stabp++, i++) |
73b8f102 | 1267 | { |
7fc01fc9 ILT |
1268 | const char *name; |
1269 | ||
250e36fe | 1270 | SWAP_SYMBOL (stabp, abfd); |
fe2750e1 | 1271 | printf ("\n%-6d ", i); |
250e36fe | 1272 | /* Either print the stab name, or, if unnamed, print its number |
fe2750e1 | 1273 | again (makes consistent formatting for tools like awk). */ |
7fc01fc9 ILT |
1274 | name = bfd_get_stab_name (stabp->n_type); |
1275 | if (name != NULL) | |
1276 | printf ("%-6s", name); | |
105da05c ILT |
1277 | else if (stabp->n_type == N_UNDF) |
1278 | printf ("HdrSym"); | |
fe2750e1 | 1279 | else |
105da05c | 1280 | printf ("%-6d", stabp->n_type); |
250e36fe DM |
1281 | printf (" %-6d %-6d ", stabp->n_other, stabp->n_desc); |
1282 | printf_vma (stabp->n_value); | |
1283 | printf (" %-6lu", stabp->n_strx); | |
249c6fc0 RS |
1284 | |
1285 | /* Symbols with type == 0 (N_UNDF) specify the length of the | |
1286 | string table associated with this file. We use that info | |
1287 | to know how to relocate the *next* file's string table indices. */ | |
1288 | ||
250e36fe | 1289 | if (stabp->n_type == N_UNDF) |
249c6fc0 RS |
1290 | { |
1291 | file_string_table_offset = next_file_string_table_offset; | |
250e36fe | 1292 | next_file_string_table_offset += stabp->n_value; |
249c6fc0 | 1293 | } |
249c6fc0 | 1294 | else |
e1ec9f07 | 1295 | { |
250e36fe | 1296 | /* Using the (possibly updated) string table offset, print the |
e1ec9f07 SS |
1297 | string (if any) associated with this symbol. */ |
1298 | ||
250e36fe DM |
1299 | if ((stabp->n_strx + file_string_table_offset) < stabstr_size) |
1300 | printf (" %s", &strtab[stabp->n_strx + file_string_table_offset]); | |
e1ec9f07 SS |
1301 | else |
1302 | printf (" *"); | |
1303 | } | |
73b8f102 JG |
1304 | } |
1305 | printf ("\n\n"); | |
1306 | } | |
249c6fc0 | 1307 | |
250e36fe DM |
1308 | void |
1309 | dump_section_stabs (abfd, stabsect_name, strsect_name) | |
1310 | bfd *abfd; | |
1311 | char *stabsect_name; | |
1312 | char *strsect_name; | |
1313 | { | |
13e4db2e | 1314 | asection *s; |
13e4db2e | 1315 | |
a65619c8 SC |
1316 | /* Check for section names for which stabsect_name is a prefix, to |
1317 | handle .stab0, etc. */ | |
13e4db2e | 1318 | for (s = abfd->sections; |
a65619c8 | 1319 | s != NULL; |
13e4db2e SC |
1320 | s = s->next) |
1321 | { | |
a65619c8 SC |
1322 | if (strncmp (stabsect_name, s->name, strlen (stabsect_name)) == 0 |
1323 | && strncmp (strsect_name, s->name, strlen (strsect_name)) != 0) | |
13e4db2e | 1324 | { |
a65619c8 SC |
1325 | if (read_section_stabs (abfd, s->name, strsect_name)) |
1326 | { | |
1327 | print_section_stabs (abfd, s->name, strsect_name); | |
1328 | free (stabs); | |
1329 | free (strtab); | |
1330 | } | |
13e4db2e SC |
1331 | } |
1332 | } | |
250e36fe DM |
1333 | } |
1334 | \f | |
eae82145 | 1335 | static void |
f7b839f7 DM |
1336 | dump_bfd_header (abfd) |
1337 | bfd *abfd; | |
1338 | { | |
1339 | char *comma = ""; | |
1340 | ||
1341 | printf ("architecture: %s, ", | |
1342 | bfd_printable_arch_mach (bfd_get_arch (abfd), | |
1343 | bfd_get_mach (abfd))); | |
1344 | printf ("flags 0x%08x:\n", abfd->flags); | |
1345 | ||
1346 | #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";} | |
1347 | PF (HAS_RELOC, "HAS_RELOC"); | |
1348 | PF (EXEC_P, "EXEC_P"); | |
1349 | PF (HAS_LINENO, "HAS_LINENO"); | |
1350 | PF (HAS_DEBUG, "HAS_DEBUG"); | |
1351 | PF (HAS_SYMS, "HAS_SYMS"); | |
1352 | PF (HAS_LOCALS, "HAS_LOCALS"); | |
1353 | PF (DYNAMIC, "DYNAMIC"); | |
1354 | PF (WP_TEXT, "WP_TEXT"); | |
1355 | PF (D_PAGED, "D_PAGED"); | |
1356 | PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE"); | |
1357 | printf ("\nstart address 0x"); | |
1358 | printf_vma (abfd->start_address); | |
38d7c012 | 1359 | printf ("\n"); |
f7b839f7 | 1360 | } |
a65619c8 SC |
1361 | \f |
1362 | static void | |
1363 | dump_bfd_private_header (abfd) | |
1364 | bfd *abfd; | |
1365 | { | |
1366 | bfd_print_private_bfd_data (abfd, stdout); | |
1367 | } | |
02a68547 | 1368 | static void |
2fa0b342 DHW |
1369 | display_bfd (abfd) |
1370 | bfd *abfd; | |
1371 | { | |
209e5610 DM |
1372 | char **matching; |
1373 | ||
1374 | if (!bfd_check_format_matches (abfd, bfd_object, &matching)) | |
aa0a709a | 1375 | { |
cef35d48 | 1376 | bfd_nonfatal (bfd_get_filename (abfd)); |
8f197c94 | 1377 | if (bfd_get_error () == bfd_error_file_ambiguously_recognized) |
209e5610 DM |
1378 | { |
1379 | list_matching_formats (matching); | |
1380 | free (matching); | |
1381 | } | |
aa0a709a SC |
1382 | return; |
1383 | } | |
f7b839f7 | 1384 | |
cef35d48 DM |
1385 | printf ("\n%s: file format %s\n", bfd_get_filename (abfd), |
1386 | abfd->xvec->name); | |
aa0a709a SC |
1387 | if (dump_ar_hdrs) |
1388 | print_arelt_descr (stdout, abfd, true); | |
aa0a709a | 1389 | if (dump_file_header) |
f7b839f7 | 1390 | dump_bfd_header (abfd); |
a65619c8 SC |
1391 | if (dump_private_headers) |
1392 | dump_bfd_private_header (abfd); | |
f7b839f7 | 1393 | putchar ('\n'); |
2fa0b342 | 1394 | if (dump_section_headers) |
aa0a709a | 1395 | dump_headers (abfd); |
83f4323e | 1396 | if (dump_symtab || dump_reloc_info || disassemble || dump_debugging) |
aa0a709a SC |
1397 | { |
1398 | syms = slurp_symtab (abfd); | |
1399 | } | |
de3b08ac ILT |
1400 | if (dump_dynamic_symtab || dump_dynamic_reloc_info) |
1401 | { | |
1402 | dynsyms = slurp_dynamic_symtab (abfd); | |
1403 | } | |
aa0a709a | 1404 | if (dump_symtab) |
de3b08ac ILT |
1405 | dump_symbols (abfd, false); |
1406 | if (dump_dynamic_symtab) | |
1407 | dump_symbols (abfd, true); | |
73b8f102 | 1408 | if (dump_stab_section_info) |
9b018ecd | 1409 | dump_stabs (abfd); |
d5464baa | 1410 | if (dump_reloc_info && ! disassemble) |
aa0a709a | 1411 | dump_relocs (abfd); |
de3b08ac ILT |
1412 | if (dump_dynamic_reloc_info) |
1413 | dump_dynamic_relocs (abfd); | |
aa0a709a SC |
1414 | if (dump_section_contents) |
1415 | dump_data (abfd); | |
1416 | if (disassemble) | |
1417 | disassemble_data (abfd); | |
e1c14599 ILT |
1418 | if (dump_debugging) |
1419 | { | |
1420 | PTR dhandle; | |
1421 | ||
83f4323e | 1422 | dhandle = read_debugging_info (abfd, syms, symcount); |
e1c14599 ILT |
1423 | if (dhandle != NULL) |
1424 | { | |
1425 | if (! print_debugging_info (stdout, dhandle)) | |
1426 | fprintf (stderr, "%s: printing debugging information failed\n", | |
1427 | bfd_get_filename (abfd)); | |
1428 | } | |
1429 | } | |
c5ba2759 ILT |
1430 | if (syms) |
1431 | { | |
1432 | free (syms); | |
1433 | syms = NULL; | |
1434 | } | |
1435 | if (dynsyms) | |
1436 | { | |
1437 | free (dynsyms); | |
1438 | dynsyms = NULL; | |
1439 | } | |
2fa0b342 DHW |
1440 | } |
1441 | ||
d9971b83 | 1442 | static void |
2fa0b342 DHW |
1443 | display_file (filename, target) |
1444 | char *filename; | |
1445 | char *target; | |
1446 | { | |
1447 | bfd *file, *arfile = (bfd *) NULL; | |
1448 | ||
1449 | file = bfd_openr (filename, target); | |
aa0a709a SC |
1450 | if (file == NULL) |
1451 | { | |
cef35d48 | 1452 | bfd_nonfatal (filename); |
aa0a709a SC |
1453 | return; |
1454 | } | |
2fa0b342 | 1455 | |
aa0a709a SC |
1456 | if (bfd_check_format (file, bfd_archive) == true) |
1457 | { | |
8f197c94 ILT |
1458 | bfd *last_arfile = NULL; |
1459 | ||
aa0a709a SC |
1460 | printf ("In archive %s:\n", bfd_get_filename (file)); |
1461 | for (;;) | |
1462 | { | |
8f197c94 | 1463 | bfd_set_error (bfd_error_no_error); |
aa0a709a SC |
1464 | |
1465 | arfile = bfd_openr_next_archived_file (file, arfile); | |
1466 | if (arfile == NULL) | |
1467 | { | |
8f197c94 | 1468 | if (bfd_get_error () != bfd_error_no_more_archived_files) |
d2442698 | 1469 | { |
cef35d48 | 1470 | bfd_nonfatal (bfd_get_filename (file)); |
d2442698 | 1471 | } |
8f197c94 | 1472 | break; |
aa0a709a | 1473 | } |
2fa0b342 | 1474 | |
aa0a709a | 1475 | display_bfd (arfile); |
8f197c94 ILT |
1476 | |
1477 | if (last_arfile != NULL) | |
1478 | bfd_close (last_arfile); | |
1479 | last_arfile = arfile; | |
aa0a709a | 1480 | } |
8f197c94 ILT |
1481 | |
1482 | if (last_arfile != NULL) | |
1483 | bfd_close (last_arfile); | |
2fa0b342 | 1484 | } |
2fa0b342 | 1485 | else |
aa0a709a | 1486 | display_bfd (file); |
2fa0b342 | 1487 | |
aa0a709a | 1488 | bfd_close (file); |
2fa0b342 DHW |
1489 | } |
1490 | \f | |
1491 | /* Actually display the various requested regions */ | |
1492 | ||
d9971b83 | 1493 | static void |
2fa0b342 DHW |
1494 | dump_data (abfd) |
1495 | bfd *abfd; | |
1496 | { | |
1497 | asection *section; | |
aa0a709a | 1498 | bfd_byte *data = 0; |
fc5d6074 SC |
1499 | bfd_size_type datasize = 0; |
1500 | bfd_size_type i; | |
aa21a2a9 | 1501 | bfd_size_type start, stop; |
2fa0b342 DHW |
1502 | |
1503 | for (section = abfd->sections; section != NULL; section = | |
aa0a709a SC |
1504 | section->next) |
1505 | { | |
1506 | int onaline = 16; | |
2fa0b342 | 1507 | |
aa0a709a SC |
1508 | if (only == (char *) NULL || |
1509 | strcmp (only, section->name) == 0) | |
60c80016 | 1510 | { |
aa0a709a SC |
1511 | if (section->flags & SEC_HAS_CONTENTS) |
1512 | { | |
1513 | printf ("Contents of section %s:\n", section->name); | |
1514 | ||
9b018ecd | 1515 | if (bfd_section_size (abfd, section) == 0) |
aa0a709a | 1516 | continue; |
02a68547 | 1517 | data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section)); |
9b018ecd | 1518 | datasize = bfd_section_size (abfd, section); |
2fa0b342 | 1519 | |
2fa0b342 | 1520 | |
9b018ecd | 1521 | bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section)); |
2fa0b342 | 1522 | |
aa21a2a9 ILT |
1523 | if (start_address == (bfd_vma) -1 |
1524 | || start_address < section->vma) | |
1525 | start = 0; | |
1526 | else | |
1527 | start = start_address - section->vma; | |
1528 | if (stop_address == (bfd_vma) -1) | |
1529 | stop = bfd_section_size (abfd, section); | |
1530 | else | |
1531 | { | |
1532 | if (stop_address < section->vma) | |
1533 | stop = 0; | |
1534 | else | |
1535 | stop = stop_address - section->vma; | |
1536 | if (stop > bfd_section_size (abfd, section)) | |
1537 | stop = bfd_section_size (abfd, section); | |
1538 | } | |
1539 | for (i = start; i < stop; i += onaline) | |
aa0a709a SC |
1540 | { |
1541 | bfd_size_type j; | |
1542 | ||
1543 | printf (" %04lx ", (unsigned long int) (i + section->vma)); | |
1544 | for (j = i; j < i + onaline; j++) | |
1545 | { | |
aa21a2a9 | 1546 | if (j < stop) |
aa0a709a SC |
1547 | printf ("%02x", (unsigned) (data[j])); |
1548 | else | |
1549 | printf (" "); | |
1550 | if ((j & 3) == 3) | |
1551 | printf (" "); | |
1552 | } | |
2fa0b342 | 1553 | |
aa0a709a SC |
1554 | printf (" "); |
1555 | for (j = i; j < i + onaline; j++) | |
1556 | { | |
aa21a2a9 | 1557 | if (j >= stop) |
aa0a709a SC |
1558 | printf (" "); |
1559 | else | |
1560 | printf ("%c", isprint (data[j]) ? data[j] : '.'); | |
1561 | } | |
1562 | putchar ('\n'); | |
1563 | } | |
d9971b83 | 1564 | free (data); |
60c80016 | 1565 | } |
2fa0b342 | 1566 | } |
2fa0b342 | 1567 | } |
2fa0b342 DHW |
1568 | } |
1569 | ||
2fa0b342 | 1570 | /* Should perhaps share code and display with nm? */ |
d9971b83 | 1571 | static void |
de3b08ac | 1572 | dump_symbols (abfd, dynamic) |
2fa0b342 | 1573 | bfd *abfd; |
de3b08ac | 1574 | boolean dynamic; |
2fa0b342 | 1575 | { |
de3b08ac ILT |
1576 | asymbol **current; |
1577 | long max; | |
ae5d2ff5 | 1578 | long count; |
2fa0b342 | 1579 | |
de3b08ac | 1580 | if (dynamic) |
aa0a709a | 1581 | { |
de3b08ac ILT |
1582 | current = dynsyms; |
1583 | max = dynsymcount; | |
1584 | if (max == 0) | |
1585 | return; | |
1586 | printf ("DYNAMIC SYMBOL TABLE:\n"); | |
1587 | } | |
1588 | else | |
1589 | { | |
1590 | current = syms; | |
1591 | max = symcount; | |
1592 | if (max == 0) | |
1593 | return; | |
1594 | printf ("SYMBOL TABLE:\n"); | |
1595 | } | |
2fa0b342 | 1596 | |
de3b08ac ILT |
1597 | for (count = 0; count < max; count++) |
1598 | { | |
d9971b83 | 1599 | if (*current) |
aa0a709a | 1600 | { |
d9971b83 KR |
1601 | bfd *cur_bfd = bfd_asymbol_bfd(*current); |
1602 | if (cur_bfd) | |
1603 | { | |
1604 | bfd_print_symbol (cur_bfd, | |
1605 | stdout, | |
1606 | *current, bfd_print_symbol_all); | |
1607 | printf ("\n"); | |
1608 | } | |
aa0a709a SC |
1609 | } |
1610 | current++; | |
2fa0b342 | 1611 | } |
aa0a709a SC |
1612 | printf ("\n"); |
1613 | printf ("\n"); | |
2fa0b342 DHW |
1614 | } |
1615 | ||
d9971b83 | 1616 | static void |
aa0a709a SC |
1617 | dump_relocs (abfd) |
1618 | bfd *abfd; | |
2fa0b342 DHW |
1619 | { |
1620 | arelent **relpp; | |
ae5d2ff5 | 1621 | long relcount; |
2fa0b342 | 1622 | asection *a; |
aa0a709a SC |
1623 | |
1624 | for (a = abfd->sections; a != (asection *) NULL; a = a->next) | |
1625 | { | |
ae5d2ff5 ILT |
1626 | long relsize; |
1627 | ||
28d1b01e | 1628 | if (bfd_is_abs_section (a)) |
aa0a709a | 1629 | continue; |
28d1b01e | 1630 | if (bfd_is_und_section (a)) |
aa0a709a | 1631 | continue; |
d9971b83 | 1632 | if (bfd_is_com_section (a)) |
aa0a709a SC |
1633 | continue; |
1634 | ||
195d1adf KR |
1635 | if (only) |
1636 | { | |
1637 | if (strcmp (only, a->name)) | |
1638 | continue; | |
1639 | } | |
1640 | else if ((a->flags & SEC_RELOC) == 0) | |
1641 | continue; | |
1642 | ||
ae5d2ff5 ILT |
1643 | relsize = bfd_get_reloc_upper_bound (abfd, a); |
1644 | if (relsize < 0) | |
1645 | bfd_fatal (bfd_get_filename (abfd)); | |
1646 | ||
c5ba2759 ILT |
1647 | printf ("RELOCATION RECORDS FOR [%s]:", a->name); |
1648 | ||
ae5d2ff5 | 1649 | if (relsize == 0) |
aa0a709a SC |
1650 | { |
1651 | printf (" (none)\n\n"); | |
d20f480f | 1652 | } |
aa0a709a SC |
1653 | else |
1654 | { | |
ae5d2ff5 | 1655 | relpp = (arelent **) xmalloc (relsize); |
aa0a709a | 1656 | relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms); |
ae5d2ff5 ILT |
1657 | if (relcount < 0) |
1658 | bfd_fatal (bfd_get_filename (abfd)); | |
1659 | else if (relcount == 0) | |
aa0a709a SC |
1660 | { |
1661 | printf (" (none)\n\n"); | |
d20f480f | 1662 | } |
aa0a709a SC |
1663 | else |
1664 | { | |
1665 | printf ("\n"); | |
de3b08ac | 1666 | dump_reloc_set (abfd, relpp, relcount); |
aa0a709a | 1667 | printf ("\n\n"); |
d20f480f | 1668 | } |
de3b08ac | 1669 | free (relpp); |
2fa0b342 | 1670 | } |
de3b08ac ILT |
1671 | } |
1672 | } | |
2fa0b342 | 1673 | |
de3b08ac ILT |
1674 | static void |
1675 | dump_dynamic_relocs (abfd) | |
1676 | bfd *abfd; | |
1677 | { | |
1678 | long relsize; | |
1679 | arelent **relpp; | |
1680 | long relcount; | |
1681 | ||
de3b08ac ILT |
1682 | relsize = bfd_get_dynamic_reloc_upper_bound (abfd); |
1683 | if (relsize < 0) | |
1684 | bfd_fatal (bfd_get_filename (abfd)); | |
1685 | ||
c5ba2759 ILT |
1686 | printf ("DYNAMIC RELOCATION RECORDS"); |
1687 | ||
de3b08ac ILT |
1688 | if (relsize == 0) |
1689 | { | |
1690 | printf (" (none)\n\n"); | |
1691 | } | |
1692 | else | |
1693 | { | |
1694 | relpp = (arelent **) xmalloc (relsize); | |
1695 | relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms); | |
1696 | if (relcount < 0) | |
1697 | bfd_fatal (bfd_get_filename (abfd)); | |
1698 | else if (relcount == 0) | |
1699 | { | |
1700 | printf (" (none)\n\n"); | |
1701 | } | |
1702 | else | |
1703 | { | |
1704 | printf ("\n"); | |
1705 | dump_reloc_set (abfd, relpp, relcount); | |
1706 | printf ("\n\n"); | |
1707 | } | |
1708 | free (relpp); | |
1709 | } | |
1710 | } | |
1711 | ||
1712 | static void | |
1713 | dump_reloc_set (abfd, relpp, relcount) | |
1714 | bfd *abfd; | |
1715 | arelent **relpp; | |
1716 | long relcount; | |
1717 | { | |
1718 | arelent **p; | |
1719 | ||
1720 | /* Get column headers lined up reasonably. */ | |
1721 | { | |
1722 | static int width; | |
1723 | if (width == 0) | |
1724 | { | |
1725 | char buf[30]; | |
1726 | sprintf_vma (buf, (bfd_vma) -1); | |
1727 | width = strlen (buf) - 7; | |
1728 | } | |
1729 | printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, ""); | |
1730 | } | |
1731 | ||
1732 | for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--) | |
1733 | { | |
1734 | arelent *q = *p; | |
1735 | CONST char *sym_name; | |
1736 | CONST char *section_name; | |
1737 | ||
aa21a2a9 ILT |
1738 | if (start_address != (bfd_vma) -1 |
1739 | && q->address < start_address) | |
1740 | continue; | |
1741 | if (stop_address != (bfd_vma) -1 | |
1742 | && q->address > stop_address) | |
1743 | continue; | |
1744 | ||
de3b08ac ILT |
1745 | if (q->sym_ptr_ptr && *q->sym_ptr_ptr) |
1746 | { | |
1747 | sym_name = (*(q->sym_ptr_ptr))->name; | |
1748 | section_name = (*(q->sym_ptr_ptr))->section->name; | |
1749 | } | |
1750 | else | |
1751 | { | |
1752 | sym_name = NULL; | |
1753 | section_name = NULL; | |
1754 | } | |
1755 | if (sym_name) | |
1756 | { | |
1757 | printf_vma (q->address); | |
1758 | printf (" %-16s %s", | |
1759 | q->howto->name, | |
1760 | sym_name); | |
1761 | } | |
1762 | else | |
1763 | { | |
1764 | if (section_name == (CONST char *) NULL) | |
1765 | section_name = "*unknown*"; | |
1766 | printf_vma (q->address); | |
1767 | printf (" %-16s [%s]", | |
1768 | q->howto->name, | |
1769 | section_name); | |
1770 | } | |
1771 | if (q->addend) | |
1772 | { | |
1773 | printf ("+0x"); | |
1774 | printf_vma (q->addend); | |
1775 | } | |
1776 | printf ("\n"); | |
d20f480f | 1777 | } |
2fa0b342 | 1778 | } |
f7b839f7 | 1779 | \f |
f7b839f7 DM |
1780 | /* The length of the longest architecture name + 1. */ |
1781 | #define LONGEST_ARCH sizeof("rs6000:6000") | |
1782 | ||
be1d162b ILT |
1783 | #ifndef L_tmpnam |
1784 | #define L_tmpnam 25 | |
1785 | #endif | |
1786 | ||
e1c14599 ILT |
1787 | static const char * |
1788 | endian_string (endian) | |
1789 | enum bfd_endian endian; | |
1790 | { | |
1791 | if (endian == BFD_ENDIAN_BIG) | |
1792 | return "big endian"; | |
1793 | else if (endian == BFD_ENDIAN_LITTLE) | |
1794 | return "little endian"; | |
1795 | else | |
1796 | return "endianness unknown"; | |
1797 | } | |
1798 | ||
f7b839f7 DM |
1799 | /* List the targets that BFD is configured to support, each followed |
1800 | by its endianness and the architectures it supports. */ | |
1801 | ||
1802 | static void | |
1803 | display_target_list () | |
1804 | { | |
de04bceb | 1805 | extern char *tmpnam (); |
f7b839f7 | 1806 | extern bfd_target *bfd_target_vector[]; |
be1d162b | 1807 | char tmparg[L_tmpnam]; |
de04bceb | 1808 | char *dummy_name; |
f7b839f7 DM |
1809 | int t; |
1810 | ||
be1d162b | 1811 | dummy_name = tmpnam (tmparg); |
f7b839f7 DM |
1812 | for (t = 0; bfd_target_vector[t]; t++) |
1813 | { | |
f7b839f7 | 1814 | bfd_target *p = bfd_target_vector[t]; |
de04bceb | 1815 | bfd *abfd = bfd_openw (dummy_name, p->name); |
105da05c ILT |
1816 | int a; |
1817 | ||
1818 | printf ("%s\n (header %s, data %s)\n", p->name, | |
e1c14599 ILT |
1819 | endian_string (p->header_byteorder), |
1820 | endian_string (p->byteorder)); | |
f7b839f7 | 1821 | |
334d6e76 SS |
1822 | if (abfd == NULL) |
1823 | { | |
de04bceb | 1824 | bfd_nonfatal (dummy_name); |
105da05c | 1825 | continue; |
334d6e76 | 1826 | } |
105da05c ILT |
1827 | |
1828 | if (! bfd_set_format (abfd, bfd_object)) | |
1829 | { | |
1830 | if (bfd_get_error () != bfd_error_invalid_operation) | |
1831 | bfd_nonfatal (p->name); | |
1832 | continue; | |
1833 | } | |
1834 | ||
f7b839f7 DM |
1835 | for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++) |
1836 | if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0)) | |
1837 | printf (" %s\n", | |
1838 | bfd_printable_arch_mach ((enum bfd_architecture) a, 0)); | |
1839 | } | |
de04bceb | 1840 | unlink (dummy_name); |
f7b839f7 DM |
1841 | } |
1842 | ||
1843 | /* Print a table showing which architectures are supported for entries | |
1844 | FIRST through LAST-1 of bfd_target_vector (targets across, | |
1845 | architectures down). */ | |
1846 | ||
9872a49c | 1847 | static void |
abdcac0f DM |
1848 | display_info_table (first, last) |
1849 | int first; | |
1850 | int last; | |
9872a49c | 1851 | { |
abdcac0f | 1852 | extern bfd_target *bfd_target_vector[]; |
de04bceb | 1853 | extern char *tmpnam (); |
be1d162b | 1854 | char tmparg[L_tmpnam]; |
de04bceb ILT |
1855 | int t, a; |
1856 | char *dummy_name; | |
9872a49c | 1857 | |
f7b839f7 | 1858 | /* Print heading of target names. */ |
ae5d2ff5 | 1859 | printf ("\n%*s", (int) LONGEST_ARCH, " "); |
105da05c | 1860 | for (t = first; t < last && bfd_target_vector[t]; t++) |
f7b839f7 DM |
1861 | printf ("%s ", bfd_target_vector[t]->name); |
1862 | putchar ('\n'); | |
9872a49c | 1863 | |
be1d162b | 1864 | dummy_name = tmpnam (tmparg); |
f7b839f7 DM |
1865 | for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++) |
1866 | if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0) | |
e779a58c | 1867 | { |
ae5d2ff5 ILT |
1868 | printf ("%*s ", (int) LONGEST_ARCH - 1, |
1869 | bfd_printable_arch_mach (a, 0)); | |
105da05c | 1870 | for (t = first; t < last && bfd_target_vector[t]; t++) |
aa0a709a | 1871 | { |
f7b839f7 | 1872 | bfd_target *p = bfd_target_vector[t]; |
105da05c | 1873 | boolean ok = true; |
de04bceb | 1874 | bfd *abfd = bfd_openw (dummy_name, p->name); |
aa0a709a | 1875 | |
334d6e76 SS |
1876 | if (abfd == NULL) |
1877 | { | |
105da05c ILT |
1878 | bfd_nonfatal (p->name); |
1879 | ok = false; | |
1880 | } | |
1881 | ||
1882 | if (ok) | |
1883 | { | |
1884 | if (! bfd_set_format (abfd, bfd_object)) | |
1885 | { | |
1886 | if (bfd_get_error () != bfd_error_invalid_operation) | |
1887 | bfd_nonfatal (p->name); | |
1888 | ok = false; | |
1889 | } | |
334d6e76 | 1890 | } |
105da05c ILT |
1891 | |
1892 | if (ok) | |
1893 | { | |
1894 | if (! bfd_set_arch_mach (abfd, a, 0)) | |
1895 | ok = false; | |
1896 | } | |
1897 | ||
1898 | if (ok) | |
aa0a709a SC |
1899 | printf ("%s ", p->name); |
1900 | else | |
e779a58c | 1901 | { |
f7b839f7 | 1902 | int l = strlen (p->name); |
aa0a709a | 1903 | while (l--) |
f7b839f7 DM |
1904 | putchar ('-'); |
1905 | putchar (' '); | |
e779a58c | 1906 | } |
e779a58c | 1907 | } |
f7b839f7 | 1908 | putchar ('\n'); |
e779a58c | 1909 | } |
de04bceb | 1910 | unlink (dummy_name); |
9872a49c | 1911 | } |
aa0a709a | 1912 | |
f7b839f7 DM |
1913 | /* Print tables of all the target-architecture combinations that |
1914 | BFD has been configured to support. */ | |
1915 | ||
aa0a709a | 1916 | static void |
f7b839f7 | 1917 | display_target_tables () |
aa0a709a | 1918 | { |
f7b839f7 | 1919 | int t, columns; |
abdcac0f | 1920 | extern bfd_target *bfd_target_vector[]; |
f7b839f7 | 1921 | char *colum; |
aa0a709a SC |
1922 | extern char *getenv (); |
1923 | ||
aa0a709a | 1924 | columns = 0; |
f7b839f7 DM |
1925 | colum = getenv ("COLUMNS"); |
1926 | if (colum != NULL) | |
aa0a709a | 1927 | columns = atoi (colum); |
f7b839f7 | 1928 | if (columns == 0) |
aa0a709a | 1929 | columns = 80; |
f7b839f7 | 1930 | |
105da05c ILT |
1931 | t = 0; |
1932 | while (bfd_target_vector[t] != NULL) | |
aa0a709a | 1933 | { |
f7b839f7 DM |
1934 | int oldt = t, wid; |
1935 | ||
105da05c ILT |
1936 | wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1; |
1937 | ++t; | |
1938 | while (wid < columns && bfd_target_vector[t] != NULL) | |
1939 | { | |
1940 | int newwid; | |
1941 | ||
1942 | newwid = wid + strlen (bfd_target_vector[t]->name) + 1; | |
1943 | if (newwid >= columns) | |
1944 | break; | |
1945 | wid = newwid; | |
1946 | ++t; | |
1947 | } | |
f7b839f7 | 1948 | display_info_table (oldt, t); |
aa0a709a SC |
1949 | } |
1950 | } | |
1951 | ||
f7b839f7 DM |
1952 | static void |
1953 | display_info () | |
1954 | { | |
1955 | printf ("BFD header file version %s\n", BFD_VERSION); | |
1956 | display_target_list (); | |
1957 | display_target_tables (); | |
1958 | } | |
1959 | ||
2fa0b342 DHW |
1960 | int |
1961 | main (argc, argv) | |
1962 | int argc; | |
1963 | char **argv; | |
1964 | { | |
1965 | int c; | |
2fa0b342 DHW |
1966 | char *target = default_target; |
1967 | boolean seenflag = false; | |
2fa0b342 DHW |
1968 | |
1969 | program_name = *argv; | |
8f197c94 ILT |
1970 | xmalloc_set_program_name (program_name); |
1971 | ||
be1d162b ILT |
1972 | START_PROGRESS (program_name, 0); |
1973 | ||
8f197c94 | 1974 | bfd_init (); |
2fa0b342 | 1975 | |
a65619c8 | 1976 | while ((c = getopt_long (argc, argv, "pib:m:VdDlfahrRtTxsSj:w", long_options, |
d2442698 | 1977 | (int *) 0)) |
aa0a709a SC |
1978 | != EOF) |
1979 | { | |
aa21a2a9 ILT |
1980 | if (c != 'l' && c != OPTION_START_ADDRESS && c != OPTION_STOP_ADDRESS) |
1981 | seenflag = true; | |
aa0a709a SC |
1982 | switch (c) |
1983 | { | |
b3a2b497 ILT |
1984 | case 0: |
1985 | break; /* we've been given a long option */ | |
aa0a709a SC |
1986 | case 'm': |
1987 | machine = optarg; | |
1988 | break; | |
1989 | case 'j': | |
1990 | only = optarg; | |
1991 | break; | |
1992 | case 'l': | |
1993 | with_line_numbers = 1; | |
1994 | break; | |
1995 | case 'b': | |
1996 | target = optarg; | |
1997 | break; | |
1998 | case 'f': | |
1999 | dump_file_header = true; | |
2000 | break; | |
2001 | case 'i': | |
e1ec9f07 | 2002 | formats_info = true; |
aa0a709a | 2003 | break; |
a65619c8 SC |
2004 | case 'p': |
2005 | dump_private_headers = 1; | |
2006 | break; | |
aa0a709a | 2007 | case 'x': |
a65619c8 | 2008 | dump_private_headers = 1; |
aa0a709a SC |
2009 | dump_symtab = 1; |
2010 | dump_reloc_info = 1; | |
2011 | dump_file_header = true; | |
2012 | dump_ar_hdrs = 1; | |
2013 | dump_section_headers = 1; | |
2014 | break; | |
aa0a709a SC |
2015 | case 't': |
2016 | dump_symtab = 1; | |
2017 | break; | |
de3b08ac ILT |
2018 | case 'T': |
2019 | dump_dynamic_symtab = 1; | |
2020 | break; | |
aa0a709a SC |
2021 | case 'd': |
2022 | disassemble = true; | |
2023 | break; | |
d5464baa ILT |
2024 | case 'D': |
2025 | disassemble = disassemble_all = true; | |
2026 | break; | |
be1d162b ILT |
2027 | case 'S': |
2028 | disassemble = true; | |
2029 | with_source_code = true; | |
2030 | break; | |
aa0a709a SC |
2031 | case 's': |
2032 | dump_section_contents = 1; | |
2033 | break; | |
2034 | case 'r': | |
2035 | dump_reloc_info = 1; | |
2036 | break; | |
de3b08ac ILT |
2037 | case 'R': |
2038 | dump_dynamic_reloc_info = 1; | |
2039 | break; | |
aa0a709a SC |
2040 | case 'a': |
2041 | dump_ar_hdrs = 1; | |
2042 | break; | |
2043 | case 'h': | |
2044 | dump_section_headers = 1; | |
2045 | break; | |
b3a2b497 ILT |
2046 | case 'H': |
2047 | usage (stdout, 0); | |
249c6fc0 RS |
2048 | case 'V': |
2049 | show_version = 1; | |
2050 | break; | |
13e4db2e SC |
2051 | case 'w': |
2052 | wide_output = 1; | |
2053 | break; | |
aa21a2a9 ILT |
2054 | case OPTION_START_ADDRESS: |
2055 | start_address = parse_vma (optarg, "--start-address"); | |
2056 | break; | |
2057 | case OPTION_STOP_ADDRESS: | |
2058 | stop_address = parse_vma (optarg, "--stop-address"); | |
2059 | break; | |
aa0a709a | 2060 | default: |
b3a2b497 | 2061 | usage (stderr, 1); |
aa0a709a | 2062 | } |
2fa0b342 | 2063 | } |
2fa0b342 | 2064 | |
249c6fc0 | 2065 | if (show_version) |
b3a2b497 ILT |
2066 | { |
2067 | printf ("GNU %s version %s\n", program_name, program_version); | |
2068 | exit (0); | |
2069 | } | |
249c6fc0 | 2070 | |
2fa0b342 | 2071 | if (seenflag == false) |
b3a2b497 | 2072 | usage (stderr, 1); |
2fa0b342 | 2073 | |
e1ec9f07 | 2074 | if (formats_info) |
aa0a709a SC |
2075 | { |
2076 | display_info (); | |
2077 | } | |
2078 | else | |
2079 | { | |
2080 | if (optind == argc) | |
2081 | display_file ("a.out", target); | |
2082 | else | |
2083 | for (; optind < argc;) | |
2084 | display_file (argv[optind++], target); | |
2085 | } | |
be1d162b ILT |
2086 | |
2087 | END_PROGRESS (program_name); | |
2088 | ||
2fa0b342 DHW |
2089 | return 0; |
2090 | } |