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