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