* objcopy.c (copy_object): Copy all applicable file flags.
[deliverable/binutils-gdb.git] / binutils / objdump.c
CommitLineData
d20f480f 1/* objdump.c -- dump information about an object file.
b3a2b497 2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
2fa0b342 3
b3a2b497 4This file is part of GNU Binutils.
2fa0b342 5
b3a2b497 6This program is free software; you can redistribute it and/or modify
2fa0b342 7it under the terms of the GNU General Public License as published by
d20f480f 8the Free Software Foundation; either version 2, or (at your option)
2fa0b342
DHW
9any later version.
10
b3a2b497 11This program is distributed in the hope that it will be useful,
2fa0b342
DHW
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
b3a2b497
ILT
17along with this program; if not, write to the Free Software
18Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
2fa0b342 19
2fa0b342 20#include "bfd.h"
d20f480f 21#include "sysdep.h"
2fa0b342
DHW
22#include "getopt.h"
23#include <stdio.h>
24#include <ctype.h>
2e8adbd7 25#include "dis-asm.h"
2fa0b342 26
73b8f102
JG
27#define ELF_STAB_DISPLAY /* This code works, but uses internal
28 bfd and elf stuff. Flip this define
29 off if you need to just use generic
30 BFD interfaces. */
31
32#ifdef ELF_STAB_DISPLAY
33/* Internal headers for the ELF .stab-dump code - sorry. */
34#define BYTES_IN_WORD 32
35#include "aout/aout64.h"
36#include "elf/internal.h"
d086adf8 37extern Elf_Internal_Shdr *bfd_elf_find_section();
73b8f102 38#endif /* ELF_STAB_DISPLAY */
bf661056 39
2e8adbd7 40extern char *xmalloc ();
6f575704 41extern int fprintf PARAMS ((FILE *, CONST char *, ...));
2fa0b342
DHW
42
43char *default_target = NULL; /* default at runtime */
44
249c6fc0 45extern *program_version;
2fa0b342
DHW
46char *program_name = NULL;
47
249c6fc0 48int show_version = 0; /* show the version number */
2fa0b342
DHW
49int dump_section_contents; /* -s */
50int dump_section_headers; /* -h */
51boolean dump_file_header; /* -f */
52int dump_symtab; /* -t */
53int dump_reloc_info; /* -r */
54int dump_ar_hdrs; /* -a */
aa0a709a 55int with_line_numbers; /* -l */
73b8f102 56int dump_stab_section_info; /* -stabs */
aa0a709a
SC
57boolean disassemble; /* -d */
58boolean info; /* -i */
195d1adf
KR
59char *only; /* -j secname */
60
61struct objdump_disasm_info {
62 bfd *abfd;
63 asection *sec;
64};
2fa0b342 65
aa0a709a
SC
66char *machine = (char *) NULL;
67asymbol **syms;
2fa0b342 68
2fa0b342
DHW
69unsigned int storage;
70
71unsigned int symcount = 0;
72
d9971b83
KR
73/* Forward declarations. */
74
75static void
76display_file PARAMS ((char *filename, char *target));
77
78static void
79dump_data PARAMS ((bfd *abfd));
80
81static void
82dump_relocs PARAMS ((bfd *abfd));
83
84static void
85dump_symbols PARAMS ((bfd *abfd));
86\f
2fa0b342 87void
b3a2b497
ILT
88usage (stream, status)
89 FILE *stream;
90 int status;
2fa0b342 91{
b3a2b497 92 fprintf (stream, "\
d2442698 93Usage: %s [-ahifdrtxsl] [-m machine] [-j section_name] [-b bfdname]\n\
195d1adf
KR
94 [--syms] [--reloc] [--header] [--version] [--help] objfile...\n\
95 at least one option besides -l must be given\n",
b3a2b497
ILT
96 program_name);
97 exit (status);
2fa0b342
DHW
98}
99
aa0a709a
SC
100static struct option long_options[]=
101{
102 {"syms", no_argument, &dump_symtab, 1},
103 {"reloc", no_argument, &dump_reloc_info, 1},
104 {"header", no_argument, &dump_section_headers, 1},
249c6fc0 105 {"version", no_argument, &show_version, 1},
b3a2b497 106 {"help", no_argument, 0, 'H'},
73b8f102
JG
107#ifdef ELF_STAB_DISPLAY
108 {"stabs", no_argument, &dump_stab_section_info, 1},
109#endif
d2442698
DM
110 {0, no_argument, 0, 0}
111};
2fa0b342
DHW
112
113
2fa0b342 114static void
aa0a709a
SC
115dump_headers (abfd)
116 bfd *abfd;
2fa0b342
DHW
117{
118 asection *section;
aa0a709a 119
2fa0b342
DHW
120 for (section = abfd->sections;
121 section != (asection *) NULL;
aa0a709a
SC
122 section = section->next)
123 {
124 char *comma = "";
125
2fa0b342 126#define PF(x,y) \
e779a58c
JG
127 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
128
129
aa0a709a
SC
130 printf ("SECTION %d [%s]\t: size %08x",
131 section->index,
132 section->name,
133 (unsigned) bfd_get_section_size_before_reloc (section));
134 printf (" vma ");
135 printf_vma (section->vma);
136 printf (" align 2**%u\n ",
137 section->alignment_power);
138 PF (SEC_ALLOC, "ALLOC");
139 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
140 PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
141 PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
142 PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
143 PF (SEC_LOAD, "LOAD");
144 PF (SEC_RELOC, "RELOC");
195d1adf 145#ifdef SEC_BALIGN
aa0a709a 146 PF (SEC_BALIGN, "BALIGN");
195d1adf 147#endif
aa0a709a
SC
148 PF (SEC_READONLY, "READONLY");
149 PF (SEC_CODE, "CODE");
150 PF (SEC_DATA, "DATA");
151 PF (SEC_ROM, "ROM");
152 printf ("\n");
2fa0b342 153#undef PF
aa0a709a 154 }
2fa0b342
DHW
155}
156
157static asymbol **
aa0a709a
SC
158DEFUN (slurp_symtab, (abfd),
159 bfd * abfd)
2fa0b342 160{
aa0a709a 161 asymbol **sy = (asymbol **) NULL;
2fa0b342 162
aa0a709a
SC
163 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
164 {
165 (void) printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
166 return (NULL);
167 }
168
169 storage = get_symtab_upper_bound (abfd);
170 if (storage)
171 {
172 sy = (asymbol **) malloc (storage);
173 if (sy == NULL)
174 {
175 fprintf (stderr, "%s: out of memory.\n", program_name);
176 exit (1);
d20f480f 177 }
aa0a709a
SC
178 }
179 symcount = bfd_canonicalize_symtab (abfd, sy);
f50af42b
KR
180 if (symcount <= 0)
181 {
182 fprintf (stderr, "%s: Bad symbol table in \"%s\".\n",
183 program_name, bfd_get_filename (abfd));
184 exit (1);
185 }
aa0a709a 186 return sy;
2fa0b342 187}
aa0a709a 188
3ae36cb6
PB
189/* Filter out (in place) symbols that are useless for dis-assemble.
190 Return count of useful symbols. */
191
192int remove_useless_symbols (syms, count)
193 asymbol **syms;
194 int count;
195{
196 register asymbol **in_ptr = syms;
197 register asymbol **out_ptr = syms;
198
199 while ( --count >= 0 )
200 {
201 asymbol *sym = *in_ptr++;
202
203 if (sym->name == NULL || sym->name[0] == '\0')
204 continue;
205 if (sym->flags & (BSF_DEBUGGING))
206 continue;
207 if (sym->section == &bfd_und_section
208 || bfd_is_com_section (sym->section))
209 continue;
210
211 *out_ptr++ = sym;
212 }
213 return out_ptr - syms;
214}
215
216
2fa0b342 217/* Sort symbols into value order */
aa0a709a
SC
218static int
219comp (ap, bp)
770cde30
JG
220 PTR ap;
221 PTR bp;
2fa0b342 222{
770cde30
JG
223 asymbol *a = *(asymbol **)ap;
224 asymbol *b = *(asymbol **)bp;
2fa0b342 225
3ae36cb6
PB
226 if (a->value > b->value)
227 return 1;
228 else if (a->value < b->value)
229 return -1;
2fa0b342 230
3ae36cb6
PB
231 if (a->section > b->section)
232 return 1;
233 else if (a->section < b->section)
234 return -1;
235 return 0;
2fa0b342
DHW
236}
237
238/* Print the supplied address symbolically if possible */
239void
545a2768 240objdump_print_address (vma, info)
aa0a709a 241 bfd_vma vma;
545a2768 242 struct disassemble_info *info;
2fa0b342
DHW
243{
244 /* Perform a binary search looking for the closest symbol to
195d1adf
KR
245 the required value. */
246 /* @@ For relocateable files, should filter out symbols belonging to
247 the wrong section. Unfortunately, not enough information is supplied
248 to this routine to determine the correct section in all cases. */
249 /* @@ Would it speed things up to cache the last two symbols returned,
250 and maybe their address ranges? For many processors, only one memory
251 operand can be present at a time, so the 2-entry cache wouldn't be
252 constantly churned by code doing heavy memory accesses. */
2fa0b342
DHW
253
254 unsigned int min = 0;
255 unsigned int max = symcount;
256
257 unsigned int thisplace = 1;
aa0a709a 258 unsigned int oldthisplace;
2fa0b342
DHW
259
260 int vardiff;
2fa0b342 261
3ae36cb6
PB
262 fprintf_vma (info->stream, vma);
263
264 if (symcount > 0)
aa0a709a
SC
265 {
266 while (true)
267 {
3ae36cb6 268 asymbol *sym; asection *sym_sec;
aa0a709a
SC
269 oldthisplace = thisplace;
270 thisplace = (max + min) / 2;
271 if (thisplace == oldthisplace)
272 break;
3ae36cb6
PB
273 sym = syms[thisplace];
274 vardiff = sym->value - vma;
275 sym_sec = sym->section;
276
277 if (vardiff > 0)
278 max = thisplace;
279 else if (vardiff < 0)
280 min = thisplace;
aa0a709a 281 else
3ae36cb6 282 goto found;
aa0a709a
SC
283 }
284 /* We've run out of places to look, print the symbol before this one
285 see if this or the symbol before describes this location the best */
fc5d6074 286
aa0a709a
SC
287 if (thisplace != 0)
288 {
289 if (syms[thisplace - 1]->value - vma >
290 syms[thisplace]->value - vma)
291 {
292 /* Previous symbol is in correct section and is closer */
293 thisplace--;
294 }
295 }
fc5d6074 296
3ae36cb6 297 found:
195d1adf
KR
298 {
299 bfd_vma val = syms[thisplace]->value;
300 int i;
301 if (syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING))
302 for (i = thisplace - 1; i >= 0; i--)
303 {
304 if (syms[i]->value == val
305 && (!(syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING))
306 || ((syms[thisplace]->flags & BSF_DEBUGGING)
307 && !(syms[i]->flags & BSF_DEBUGGING))))
308 {
309 thisplace = i;
310 break;
311 }
312 }
313 if (syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING))
314 for (i = thisplace + 1; i < symcount; i++)
315 {
316 if (syms[i]->value == val
317 && (!(syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING))
318 || ((syms[thisplace]->flags & BSF_DEBUGGING)
319 && !(syms[i]->flags & BSF_DEBUGGING))))
320 {
321 thisplace = i;
322 break;
323 }
324 }
325 }
326 {
327 /* If the file is relocateable, and the symbol could be from this
328 section, prefer a symbol from this section over symbols from
329 others, even if the other symbol's value might be closer.
330
331 Note that this may be wrong for some symbol references if the
332 sections have overlapping memory ranges, but in that case there's
333 no way to tell what's desired without looking at the relocation
334 table. */
335 struct objdump_disasm_info *aux;
336 int i;
337
338 aux = (struct objdump_disasm_info *) info->application_data;
339 if (aux->abfd->flags & HAS_RELOC
340 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
341 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
342 + bfd_get_section_size_before_reloc (aux->sec))
343 && syms[thisplace]->section != aux->sec)
344 {
345 for (i = thisplace + 1; i < symcount; i++)
346 if (syms[i]->value != syms[thisplace]->value)
347 break;
348 while (--i >= 0)
349 if (syms[i]->section == aux->sec)
350 {
351 thisplace = i;
352 break;
353 }
354 }
355 }
3ae36cb6 356 fprintf (info->stream, " <%s", syms[thisplace]->name);
aa0a709a
SC
357 if (syms[thisplace]->value > vma)
358 {
d086adf8
KR
359 char buf[30], *p = buf;
360 sprintf_vma (buf, syms[thisplace]->value - vma);
361 while (*p == '0')
362 p++;
363 fprintf (info->stream, "-%s", p);
aa0a709a 364 }
3ae36cb6 365 else if (vma > syms[thisplace]->value)
aa0a709a 366 {
d086adf8
KR
367 char buf[30], *p = buf;
368 sprintf_vma (buf, vma - syms[thisplace]->value);
369 while (*p == '0')
370 p++;
371 fprintf (info->stream, "+%s", p);
aa0a709a 372 }
3ae36cb6 373 fprintf (info->stream, ">");
2fa0b342 374 }
2fa0b342
DHW
375}
376
195d1adf
KR
377#ifdef ARCH_all
378#define ARCH_a29k
379#define ARCH_alpha
380#define ARCH_h8300
381#define ARCH_h8500
382#define ARCH_hppa
383#define ARCH_i386
384#define ARCH_i960
385#define ARCH_m68k
386#define ARCH_m88k
387#define ARCH_mips
388#define ARCH_sh
389#define ARCH_sparc
390#define ARCH_z8k
391#endif
392
2fa0b342 393void
aa0a709a
SC
394disassemble_data (abfd)
395 bfd *abfd;
2fa0b342
DHW
396{
397 bfd_byte *data = NULL;
aa0a709a 398 bfd_arch_info_type *info;
fc5d6074
SC
399 bfd_size_type datasize = 0;
400 bfd_size_type i;
2e8adbd7
PB
401 unsigned int (*print) ()= 0; /* Old style */
402 disassembler_ftype disassemble = 0; /* New style */
2fa0b342 403 enum bfd_architecture a;
2e8adbd7 404 struct disassemble_info disasm_info;
195d1adf 405 struct objdump_disasm_info aux;
2e8adbd7
PB
406
407 int prevline;
408 CONST char *prev_function = "";
d20f480f 409
2fa0b342 410 asection *section;
aa0a709a 411
2fa0b342 412 /* Replace symbol section relative values with abs values */
96d7950b 413 boolean done_dot = false;
aa0a709a 414
2e8adbd7 415 INIT_DISASSEMBLE_INFO(disasm_info, stdout);
195d1adf
KR
416 disasm_info.application_data = (PTR) &aux;
417 aux.abfd = abfd;
545a2768 418 disasm_info.print_address_func = objdump_print_address;
2e8adbd7 419
aa0a709a
SC
420 for (i = 0; i < symcount; i++)
421 {
2fa0b342 422 syms[i]->value += syms[i]->section->vma;
aa0a709a 423 }
2fa0b342 424
3ae36cb6 425 symcount = remove_useless_symbols (syms, symcount);
2fa0b342
DHW
426
427 /* Sort the symbols into section and symbol order */
aa0a709a 428 (void) qsort (syms, symcount, sizeof (asymbol *), comp);
2fa0b342 429
aa0a709a
SC
430 if (machine != (char *) NULL)
431 {
432 info = bfd_scan_arch (machine);
433 if (info == 0)
434 {
435 fprintf (stderr, "%s: Can't use supplied machine %s\n",
436 program_name,
437 machine);
438 exit (1);
439 }
440 abfd->arch_info = info;
2fa0b342 441 }
e779a58c
JG
442
443 /* See if we can disassemble using bfd */
444
aa0a709a
SC
445 if (abfd->arch_info->disassemble)
446 {
447 print = abfd->arch_info->disassemble;
e779a58c 448 }
aa0a709a
SC
449 else
450 {
451 a = bfd_get_arch (abfd);
452 switch (a)
453 {
195d1adf
KR
454 /* If you add a case to this table, also add it to the
455 ARCH_all definition right above this function. */
456#ifdef ARCH_a29k
457 case bfd_arch_a29k:
458 /* As far as I know we only handle big-endian 29k objects. */
459 disassemble = print_insn_big_a29k;
aa0a709a 460 break;
195d1adf
KR
461#endif
462#ifdef ARCH_alpha
463 case bfd_arch_alpha:
464 disassemble = print_insn_alpha;
12da1775 465 break;
195d1adf
KR
466#endif
467#ifdef ARCH_h8300
3ae36cb6
PB
468 case bfd_arch_h8300:
469 if (bfd_get_mach(abfd) == bfd_mach_h8300h)
470 disassemble = print_insn_h8300h;
471 else
472 disassemble = print_insn_h8300;
473 break;
195d1adf
KR
474#endif
475#ifdef ARCH_h8500
476 case bfd_arch_h8500:
477 disassemble = print_insn_h8500;
6f575704 478 break;
195d1adf
KR
479#endif
480#ifdef ARCH_hppa
481 case bfd_arch_hppa:
482 disassemble = print_insn_hppa;
aa0a709a 483 break;
195d1adf
KR
484#endif
485#ifdef ARCH_i386
486 case bfd_arch_i386:
487 disassemble = print_insn_i386;
aa0a709a 488 break;
195d1adf
KR
489#endif
490#ifdef ARCH_i960
aa0a709a 491 case bfd_arch_i960:
545a2768 492 disassemble = print_insn_i960;
aa0a709a 493 break;
195d1adf
KR
494#endif
495#ifdef ARCH_m68k
496 case bfd_arch_m68k:
497 disassemble = print_insn_m68k;
498 break;
499#endif
500#ifdef ARCH_m88k
b3a2b497
ILT
501 case bfd_arch_m88k:
502 disassemble = print_insn_m88k;
503 break;
195d1adf
KR
504#endif
505#ifdef ARCH_mips
d9971b83 506 case bfd_arch_mips:
2e8adbd7
PB
507 if (abfd->xvec->byteorder_big_p)
508 disassemble = print_insn_big_mips;
509 else
510 disassemble = print_insn_little_mips;
d9971b83 511 break;
195d1adf
KR
512#endif
513#ifdef ARCH_sh
514 case bfd_arch_sh:
515 disassemble = print_insn_sh;
516 break;
517#endif
518#ifdef ARCH_sparc
519 case bfd_arch_sparc:
520 disassemble = print_insn_sparc;
521 break;
522#endif
523#ifdef ARCH_z8k
524 case bfd_arch_z8k:
525 if (bfd_get_mach(abfd) == bfd_mach_z8001)
526 disassemble = print_insn_z8001;
527 else
528 disassemble = print_insn_z8002;
529 break;
530#endif
aa0a709a
SC
531 default:
532 fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
533 program_name,
534 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
535 exit (1);
536 }
2fa0b342 537
aa0a709a 538 }
2fa0b342
DHW
539
540 for (section = abfd->sections;
aa0a709a
SC
541 section != (asection *) NULL;
542 section = section->next)
65cceb78 543 {
195d1adf 544 aux.sec = section;
2fa0b342 545
aa0a709a
SC
546 if ((section->flags & SEC_LOAD)
547 && (only == (char *) NULL || strcmp (only, section->name) == 0))
548 {
549 printf ("Disassembly of section %s:\n", section->name);
2fa0b342 550
aa0a709a
SC
551 if (bfd_get_section_size_before_reloc (section) == 0)
552 continue;
2fa0b342 553
195d1adf 554 data = (bfd_byte *) malloc ((size_t) bfd_get_section_size_before_reloc (section));
2fa0b342 555
aa0a709a
SC
556 if (data == (bfd_byte *) NULL)
557 {
558 fprintf (stderr, "%s: memory exhausted.\n", program_name);
559 exit (1);
560 }
561 datasize = bfd_get_section_size_before_reloc (section);
2fa0b342 562
aa0a709a 563 bfd_get_section_contents (abfd, section, data, 0, bfd_get_section_size_before_reloc (section));
2fa0b342 564
5d0734a7
JK
565 disasm_info.buffer = data;
566 disasm_info.buffer_vma = section->vma;
567 disasm_info.buffer_length =
568 bfd_get_section_size_before_reloc (section);
aa0a709a 569 i = 0;
5d0734a7 570 while (i < disasm_info.buffer_length)
aa0a709a
SC
571 {
572 if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 &&
573 data[i + 3] == 0)
574 {
575 if (done_dot == false)
576 {
577 printf ("...\n");
578 done_dot = true;
65cceb78 579 }
aa0a709a 580 i += 4;
65cceb78 581 }
aa0a709a
SC
582 else
583 {
584 done_dot = false;
585 if (with_line_numbers)
586 {
aa0a709a
SC
587 CONST char *filename;
588 CONST char *functionname;
589 unsigned int line;
590
d9971b83
KR
591 if (bfd_find_nearest_line (abfd,
592 section,
593 syms,
594 section->vma + i,
595 &filename,
596 &functionname,
2e8adbd7 597 &line))
aa0a709a 598 {
2e8adbd7
PB
599 if (functionname && *functionname
600 && strcmp(functionname, prev_function))
601 {
602 printf ("%s():\n", functionname);
603 prev_function = functionname;
604 }
605 if (!filename)
606 filename = "???";
607 if (line && line != prevline)
608 {
609 printf ("%s:%u\n", filename, line);
610 prevline = line;
611 }
aa0a709a
SC
612 }
613 }
545a2768 614 objdump_print_address (section->vma + i, &disasm_info);
aa0a709a 615 printf (" ");
65cceb78 616
2e8adbd7 617 if (disassemble) /* New style */
5d0734a7
JK
618 {
619 int bytes = (*disassemble)(section->vma + i,
620 &disasm_info);
621 if (bytes < 0)
622 break;
623 i += bytes;
624 }
2e8adbd7 625 else /* Old style */
d9971b83
KR
626 i += print (section->vma + i,
627 data + i,
628 stdout);
aa0a709a
SC
629 putchar ('\n');
630 }
96d7950b 631 }
aa0a709a 632 free (data);
96d7950b 633 }
2fa0b342 634 }
2fa0b342 635}
73b8f102
JG
636\f
637#ifdef ELF_STAB_DISPLAY
638
639/* Define a table of stab values and print-strings. We wish the initializer
640 could be a direct-mapped table, but instead we build one the first
641 time we need it. */
642
643#define STAB_STRING_LENGTH 6
644
645char stab_name[256][STAB_STRING_LENGTH];
646
647struct stab_print {
648 int value;
649 char string[STAB_STRING_LENGTH];
650};
651
652struct stab_print stab_print[] = {
653#define __define_stab(NAME, CODE, STRING) {CODE, STRING},
654#include "aout/stab.def"
655#undef __define_stab
656 {0, 0}
657};
658
249c6fc0
RS
659void dump_elf_stabs_1 ();
660
73b8f102
JG
661/* This is a kludge for dumping the stabs section from an ELF file that
662 uses Sun stabs encoding. It has to use some hooks into BFD because
663 string table sections are not normally visible to BFD callers. */
664
665void
666dump_elf_stabs (abfd)
667 bfd *abfd;
668{
249c6fc0 669 int i;
73b8f102
JG
670
671 /* Initialize stab name array if first time. */
672 if (stab_name[0][0] == 0)
673 {
674 /* Fill in numeric values for all possible strings. */
675 for (i = 0; i < 256; i++)
676 {
677 sprintf (stab_name[i], "%d", i);
678 }
679 for (i = 0; stab_print[i].string[0]; i++)
680 strcpy (stab_name[stab_print[i].value], stab_print[i].string);
681 }
682
683 if (0 != strncmp ("elf", abfd->xvec->name, 3))
684 {
685 fprintf (stderr, "%s: %s is not in ELF format.\n", program_name,
686 abfd->filename);
687 return;
688 }
689
249c6fc0
RS
690 dump_elf_stabs_1 (abfd, ".stab", ".stabstr");
691 dump_elf_stabs_1 (abfd, ".stab.excl", ".stab.exclstr");
692 dump_elf_stabs_1 (abfd, ".stab.index", ".stab.indexstr");
693}
694
695void
696dump_elf_stabs_1 (abfd, name1, name2)
697 bfd *abfd;
698 char *name1; /* Section name of .stab */
699 char *name2; /* Section name of its string section */
700{
d086adf8 701 Elf_Internal_Shdr *stab_hdr, *stabstr_hdr;
249c6fc0
RS
702 char *strtab;
703 struct internal_nlist *stabs, *stabs_end;
704 int i;
705 unsigned file_string_table_offset, next_file_string_table_offset;
706
d086adf8 707 stab_hdr = bfd_elf_find_section (abfd, name1);
73b8f102
JG
708 if (0 == stab_hdr)
709 {
249c6fc0 710 printf ("Contents of %s section: none.\n\n", name1);
73b8f102
JG
711 return;
712 }
713
d086adf8 714 stabstr_hdr = bfd_elf_find_section (abfd, name2);
73b8f102
JG
715 if (0 == stabstr_hdr)
716 {
249c6fc0
RS
717 fprintf (stderr, "%s: %s has no %s section.\n", program_name,
718 abfd->filename, name2);
73b8f102
JG
719 return;
720 }
721
722 stabs = (struct internal_nlist *) xmalloc (stab_hdr ->sh_size);
723 strtab = (char *) xmalloc (stabstr_hdr->sh_size);
724 stabs_end = (struct internal_nlist *) (stab_hdr->sh_size + (char *)stabs);
725
d9971b83 726 if (bfd_seek (abfd, stab_hdr->sh_offset, SEEK_SET) < 0 ||
73b8f102
JG
727 stab_hdr->sh_size != bfd_read ((PTR)stabs, stab_hdr->sh_size, 1, abfd))
728 {
249c6fc0
RS
729 fprintf (stderr, "%s: reading %s section of %s failed.\n",
730 program_name, name1,
73b8f102
JG
731 abfd->filename);
732 return;
733 }
2fa0b342 734
d9971b83 735 if (bfd_seek (abfd, stabstr_hdr->sh_offset, SEEK_SET) < 0 ||
73b8f102
JG
736 stabstr_hdr->sh_size != bfd_read ((PTR)strtab, stabstr_hdr->sh_size,
737 1, abfd))
738 {
249c6fc0
RS
739 fprintf (stderr, "%s: reading %s section of %s failed.\n",
740 program_name, name2,
73b8f102
JG
741 abfd->filename);
742 return;
743 }
744
745#define SWAP_SYMBOL(symp, abfd) \
746 { \
747 (symp)->n_strx = bfd_h_get_32(abfd, \
748 (unsigned char *)&(symp)->n_strx); \
749 (symp)->n_desc = bfd_h_get_16 (abfd, \
750 (unsigned char *)&(symp)->n_desc); \
751 (symp)->n_value = bfd_h_get_32 (abfd, \
752 (unsigned char *)&(symp)->n_value); \
753 }
754
249c6fc0 755 printf ("Contents of %s section:\n\n", name1);
73b8f102
JG
756 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
757
249c6fc0
RS
758 file_string_table_offset = 0;
759 next_file_string_table_offset = 0;
760
761 /* Loop through all symbols and print them.
762
763 We start the index at -1 because there is a dummy symbol on
73b8f102 764 the front of Sun's stabs-in-elf sections. */
249c6fc0 765
73b8f102
JG
766 for (i = -1; stabs < stabs_end; stabs++, i++)
767 {
768 SWAP_SYMBOL (stabs, abfd);
769 printf ("\n%-6d %-6s %-6d %-6d %08x %-6d", i,
770 stab_name [stabs->n_type],
771 stabs->n_other, stabs->n_desc, stabs->n_value,
772 stabs->n_strx);
249c6fc0
RS
773
774 /* Symbols with type == 0 (N_UNDF) specify the length of the
775 string table associated with this file. We use that info
776 to know how to relocate the *next* file's string table indices. */
777
778 if (stabs->n_type == N_UNDF)
779 {
780 file_string_table_offset = next_file_string_table_offset;
781 next_file_string_table_offset += stabs->n_value;
782 }
783
784 /* Now, using the possibly updated string table offset, print the
785 string (if any) associated with this symbol. */
786
787 if ((stabs->n_strx + file_string_table_offset) < stabstr_hdr->sh_size)
788 printf (" %s", &strtab[stabs->n_strx + file_string_table_offset]);
789 else
790 printf (" *");
73b8f102
JG
791 }
792 printf ("\n\n");
793}
794#endif /* ELF_STAB_DISPLAY */
249c6fc0 795
2fa0b342
DHW
796display_bfd (abfd)
797 bfd *abfd;
798{
799
aa0a709a
SC
800 if (!bfd_check_format (abfd, bfd_object))
801 {
b3a2b497
ILT
802 fprintf (stderr, "%s:%s: %s\n", program_name, abfd->filename,
803 bfd_errmsg (bfd_error));
aa0a709a
SC
804 return;
805 }
2fa0b342 806 printf ("\n%s: file format %s\n", abfd->filename, abfd->xvec->name);
aa0a709a
SC
807 if (dump_ar_hdrs)
808 print_arelt_descr (stdout, abfd, true);
2fa0b342 809
aa0a709a
SC
810 if (dump_file_header)
811 {
812 char *comma = "";
813
814 printf ("architecture: %s, ",
815 bfd_printable_arch_mach (bfd_get_arch (abfd),
816 bfd_get_mach (abfd)));
817 printf ("flags 0x%08x:\n", abfd->flags);
2fa0b342 818
2fa0b342 819#define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
aa0a709a
SC
820 PF (HAS_RELOC, "HAS_RELOC");
821 PF (EXEC_P, "EXEC_P");
822 PF (HAS_LINENO, "HAS_LINENO");
823 PF (HAS_DEBUG, "HAS_DEBUG");
824 PF (HAS_SYMS, "HAS_SYMS");
825 PF (HAS_LOCALS, "HAS_LOCALS");
826 PF (DYNAMIC, "DYNAMIC");
827 PF (WP_TEXT, "WP_TEXT");
828 PF (D_PAGED, "D_PAGED");
249c6fc0 829 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
aa0a709a
SC
830 printf ("\nstart address 0x");
831 printf_vma (abfd->start_address);
832 }
833 printf ("\n");
2fa0b342
DHW
834
835 if (dump_section_headers)
aa0a709a
SC
836 dump_headers (abfd);
837 if (dump_symtab || dump_reloc_info || disassemble)
838 {
839 syms = slurp_symtab (abfd);
840 }
841 if (dump_symtab)
842 dump_symbols (abfd);
73b8f102
JG
843#ifdef ELF_STAB_DISPLAY
844 if (dump_stab_section_info)
845 dump_elf_stabs (abfd);
846#endif
aa0a709a
SC
847 if (dump_reloc_info)
848 dump_relocs (abfd);
849 if (dump_section_contents)
850 dump_data (abfd);
3ae36cb6
PB
851 /* Note that disassemble_data re-orders the syms table, but that is
852 safe - as long as it is done last! */
aa0a709a
SC
853 if (disassemble)
854 disassemble_data (abfd);
2fa0b342
DHW
855}
856
d9971b83 857static void
2fa0b342
DHW
858display_file (filename, target)
859 char *filename;
860 char *target;
861{
862 bfd *file, *arfile = (bfd *) NULL;
863
864 file = bfd_openr (filename, target);
aa0a709a
SC
865 if (file == NULL)
866 {
d2442698 867 fprintf (stderr, "%s: ", program_name);
aa0a709a
SC
868 bfd_perror (filename);
869 return;
870 }
2fa0b342 871
aa0a709a
SC
872 if (bfd_check_format (file, bfd_archive) == true)
873 {
874 printf ("In archive %s:\n", bfd_get_filename (file));
875 for (;;)
876 {
877 bfd_error = no_error;
878
879 arfile = bfd_openr_next_archived_file (file, arfile);
880 if (arfile == NULL)
881 {
882 if (bfd_error != no_more_archived_files)
d2442698
DM
883 {
884 fprintf (stderr, "%s: ", program_name);
885 bfd_perror (bfd_get_filename (file));
886 }
aa0a709a
SC
887 return;
888 }
2fa0b342 889
aa0a709a
SC
890 display_bfd (arfile);
891 /* Don't close the archive elements; we need them for next_archive */
892 }
2fa0b342 893 }
2fa0b342 894 else
aa0a709a 895 display_bfd (file);
2fa0b342 896
aa0a709a 897 bfd_close (file);
2fa0b342
DHW
898}
899\f
900/* Actually display the various requested regions */
901
d9971b83 902static void
2fa0b342
DHW
903dump_data (abfd)
904 bfd *abfd;
905{
906 asection *section;
aa0a709a 907 bfd_byte *data = 0;
fc5d6074
SC
908 bfd_size_type datasize = 0;
909 bfd_size_type i;
2fa0b342
DHW
910
911 for (section = abfd->sections; section != NULL; section =
aa0a709a
SC
912 section->next)
913 {
914 int onaline = 16;
2fa0b342 915
aa0a709a
SC
916 if (only == (char *) NULL ||
917 strcmp (only, section->name) == 0)
60c80016 918 {
aa0a709a
SC
919 if (section->flags & SEC_HAS_CONTENTS)
920 {
921 printf ("Contents of section %s:\n", section->name);
922
923 if (bfd_get_section_size_before_reloc (section) == 0)
924 continue;
195d1adf 925 data = (bfd_byte *) malloc ((size_t) bfd_get_section_size_before_reloc (section));
aa0a709a
SC
926 if (data == (bfd_byte *) NULL)
927 {
928 fprintf (stderr, "%s: memory exhausted.\n", program_name);
929 exit (1);
930 }
931 datasize = bfd_get_section_size_before_reloc (section);
2fa0b342 932
2fa0b342 933
aa0a709a 934 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_get_section_size_before_reloc (section));
2fa0b342 935
aa0a709a
SC
936 for (i = 0; i < bfd_get_section_size_before_reloc (section); i += onaline)
937 {
938 bfd_size_type j;
939
940 printf (" %04lx ", (unsigned long int) (i + section->vma));
941 for (j = i; j < i + onaline; j++)
942 {
943 if (j < bfd_get_section_size_before_reloc (section))
944 printf ("%02x", (unsigned) (data[j]));
945 else
946 printf (" ");
947 if ((j & 3) == 3)
948 printf (" ");
949 }
2fa0b342 950
aa0a709a
SC
951 printf (" ");
952 for (j = i; j < i + onaline; j++)
953 {
954 if (j >= bfd_get_section_size_before_reloc (section))
955 printf (" ");
956 else
957 printf ("%c", isprint (data[j]) ? data[j] : '.');
958 }
959 putchar ('\n');
960 }
d9971b83 961 free (data);
60c80016 962 }
2fa0b342 963 }
2fa0b342 964 }
2fa0b342
DHW
965}
966
2fa0b342 967/* Should perhaps share code and display with nm? */
d9971b83 968static void
2fa0b342
DHW
969dump_symbols (abfd)
970 bfd *abfd;
971{
972
973 unsigned int count;
974 asymbol **current = syms;
2fa0b342 975
aa0a709a 976 printf ("SYMBOL TABLE:\n");
e779a58c 977
aa0a709a
SC
978 for (count = 0; count < symcount; count++)
979 {
2fa0b342 980
d9971b83 981 if (*current)
aa0a709a 982 {
d9971b83
KR
983 bfd *cur_bfd = bfd_asymbol_bfd(*current);
984 if (cur_bfd)
985 {
986 bfd_print_symbol (cur_bfd,
987 stdout,
988 *current, bfd_print_symbol_all);
989 printf ("\n");
990 }
aa0a709a
SC
991
992 }
993 current++;
2fa0b342 994 }
aa0a709a
SC
995 printf ("\n");
996 printf ("\n");
2fa0b342
DHW
997}
998
d9971b83 999static void
aa0a709a
SC
1000dump_relocs (abfd)
1001 bfd *abfd;
2fa0b342
DHW
1002{
1003 arelent **relpp;
1004 unsigned int relcount;
1005 asection *a;
aa0a709a
SC
1006
1007 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
1008 {
1009 if (a == &bfd_abs_section)
1010 continue;
1011 if (a == &bfd_und_section)
1012 continue;
d9971b83 1013 if (bfd_is_com_section (a))
aa0a709a
SC
1014 continue;
1015
195d1adf
KR
1016 if (only)
1017 {
1018 if (strcmp (only, a->name))
1019 continue;
1020 }
1021 else if ((a->flags & SEC_RELOC) == 0)
1022 continue;
1023
aa0a709a
SC
1024 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
1025
1026 if (bfd_get_reloc_upper_bound (abfd, a) == 0)
1027 {
1028 printf (" (none)\n\n");
d20f480f 1029 }
aa0a709a
SC
1030 else
1031 {
d20f480f
SC
1032 arelent **p;
1033
aa0a709a 1034 relpp = (arelent **) xmalloc (bfd_get_reloc_upper_bound (abfd, a));
3ae36cb6 1035 /* Note that this must be done *before* we sort the syms table. */
aa0a709a
SC
1036 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
1037 if (relcount == 0)
1038 {
1039 printf (" (none)\n\n");
d20f480f 1040 }
aa0a709a
SC
1041 else
1042 {
1043 printf ("\n");
195d1adf
KR
1044 /* Get column headers lined up reasonably. */
1045 {
1046 static int width;
1047 if (width == 0)
1048 {
1049 char buf[30];
1050 sprintf_vma (buf, (bfd_vma) -1);
1051 width = strlen (buf) - 7;
1052 }
1053 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
1054 }
d20f480f 1055
aa0a709a
SC
1056 for (p = relpp; relcount && *p != (arelent *) NULL; p++,
1057 relcount--)
1058 {
d20f480f
SC
1059 arelent *q = *p;
1060 CONST char *sym_name;
aa0a709a
SC
1061 CONST char *section_name = (*(q->sym_ptr_ptr))->section->name;
1062
1063 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
1064 {
1065 sym_name = (*(q->sym_ptr_ptr))->name;
d20f480f 1066 }
aa0a709a
SC
1067 else
1068 {
d20f480f
SC
1069 sym_name = 0;
1070 }
aa0a709a
SC
1071 if (sym_name)
1072 {
1073 printf_vma (q->address);
195d1adf 1074 printf (" %-16s %s",
aa0a709a
SC
1075 q->howto->name,
1076 sym_name);
d20f480f 1077 }
aa0a709a
SC
1078 else
1079 {
1080 printf_vma (q->address);
195d1adf 1081 printf (" %-16s [%s]",
aa0a709a
SC
1082 q->howto->name,
1083 section_name);
d20f480f 1084 }
aa0a709a
SC
1085 if (q->addend)
1086 {
1087 printf ("+0x");
1088 printf_vma (q->addend);
d20f480f 1089 }
aa0a709a 1090 printf ("\n");
d20f480f 1091 }
aa0a709a
SC
1092 printf ("\n\n");
1093 free (relpp);
d20f480f 1094 }
2fa0b342 1095 }
2fa0b342 1096
d20f480f 1097 }
2fa0b342
DHW
1098}
1099
aa0a709a
SC
1100#ifdef unix
1101#define _DUMMY_NAME_ "/dev/null"
1102#else
1103#define _DUMMY_NAME_ "##dummy"
1104#endif
9872a49c 1105static void
aa0a709a
SC
1106DEFUN (display_info_table, (first, last),
1107 int first AND int last)
9872a49c 1108{
3fdbfe8d 1109 unsigned int i, j;
9872a49c
SC
1110 extern bfd_target *target_vector[];
1111
aa0a709a
SC
1112 printf ("\n%12s", " ");
1113 for (i = first; i++ < last && target_vector[i];)
1114 printf ("%s ", target_vector[i]->name);
1115 printf ("\n");
9872a49c 1116
aa0a709a
SC
1117 for (j = (int) bfd_arch_obscure + 1; (int) j < (int) bfd_arch_last; j++)
1118 if (strcmp (bfd_printable_arch_mach (j, 0), "UNKNOWN!") != 0)
e779a58c 1119 {
aa0a709a
SC
1120 printf ("%11s ", bfd_printable_arch_mach (j, 0));
1121 for (i = first; i++ < last && target_vector[i];)
1122 {
1123 bfd_target *p = target_vector[i];
1124 bfd *abfd = bfd_openw (_DUMMY_NAME_, p->name);
1125 int l = strlen (p->name);
249c6fc0
RS
1126 int ok;
1127 bfd_set_format (abfd, bfd_object);
1128 ok = bfd_set_arch_mach (abfd, j, 0);
aa0a709a
SC
1129
1130 if (ok)
1131 printf ("%s ", p->name);
1132 else
e779a58c 1133 {
aa0a709a
SC
1134 while (l--)
1135 printf ("%c", ok ? '*' : '-');
1136 printf (" ");
e779a58c 1137 }
e779a58c 1138 }
aa0a709a 1139 printf ("\n");
e779a58c 1140 }
9872a49c 1141}
aa0a709a
SC
1142
1143static void
1144DEFUN_VOID (display_info)
1145{
1146 char *colum;
1147 unsigned int i, j, columns;
1148 extern bfd_target *target_vector[];
1149 extern char *getenv ();
1150
1151 printf ("BFD header file version %s\n", BFD_VERSION);
1152 for (i = 0; target_vector[i]; i++)
1153 {
1154 bfd_target *p = target_vector[i];
1155 bfd *abfd = bfd_openw (_DUMMY_NAME_, p->name);
249c6fc0 1156 bfd_set_format (abfd, bfd_object);
aa0a709a
SC
1157 printf ("%s\n (header %s, data %s)\n", p->name,
1158 p->header_byteorder_big_p ? "big endian" : "little endian",
1159 p->byteorder_big_p ? "big endian" : "little endian");
1160 for (j = (int) bfd_arch_obscure + 1; j < (int) bfd_arch_last; j++)
1161 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) j, 0))
1162 printf (" %s\n",
1163 bfd_printable_arch_mach ((enum bfd_architecture) j, 0));
1164 }
1165 columns = 0;
1166 if (colum = getenv ("COLUMNS"))
1167 columns = atoi (colum);
1168 if (!columns)
1169 columns = 80;
1170 for (i = 0; target_vector[i];)
1171 {
1172 int old;
1173 old = i;
1174 for (j = 12; target_vector[i] && j < columns; i++)
1175 j += strlen (target_vector[i]->name) + 1;
1176 i--;
1177 if (old == i)
1178 break;
1179 display_info_table (old, i);
1180 }
1181}
1182
2fa0b342
DHW
1183/** main and like trivia */
1184int
1185main (argc, argv)
1186 int argc;
1187 char **argv;
1188{
1189 int c;
1190 extern int optind;
1191 extern char *optarg;
1192 char *target = default_target;
1193 boolean seenflag = false;
2fa0b342 1194
aa0a709a 1195 bfd_init ();
2fa0b342
DHW
1196 program_name = *argv;
1197
d2442698
DM
1198 while ((c = getopt_long (argc, argv, "ib:m:Vdlfahrtxsj:", long_options,
1199 (int *) 0))
aa0a709a
SC
1200 != EOF)
1201 {
1202 seenflag = true;
1203 switch (c)
1204 {
b3a2b497
ILT
1205 case 0:
1206 break; /* we've been given a long option */
aa0a709a
SC
1207 case 'm':
1208 machine = optarg;
1209 break;
1210 case 'j':
1211 only = optarg;
1212 break;
1213 case 'l':
1214 with_line_numbers = 1;
1215 break;
1216 case 'b':
1217 target = optarg;
1218 break;
1219 case 'f':
1220 dump_file_header = true;
1221 break;
1222 case 'i':
1223 info = true;
1224 break;
1225 case 'x':
1226 dump_symtab = 1;
1227 dump_reloc_info = 1;
1228 dump_file_header = true;
1229 dump_ar_hdrs = 1;
1230 dump_section_headers = 1;
1231 break;
aa0a709a
SC
1232 case 't':
1233 dump_symtab = 1;
1234 break;
1235 case 'd':
1236 disassemble = true;
1237 break;
1238 case 's':
1239 dump_section_contents = 1;
1240 break;
1241 case 'r':
1242 dump_reloc_info = 1;
1243 break;
1244 case 'a':
1245 dump_ar_hdrs = 1;
1246 break;
1247 case 'h':
1248 dump_section_headers = 1;
1249 break;
b3a2b497
ILT
1250 case 'H':
1251 usage (stdout, 0);
249c6fc0
RS
1252 case 'V':
1253 show_version = 1;
1254 break;
aa0a709a 1255 default:
b3a2b497 1256 usage (stderr, 1);
aa0a709a 1257 }
2fa0b342 1258 }
2fa0b342 1259
249c6fc0 1260 if (show_version)
b3a2b497
ILT
1261 {
1262 printf ("GNU %s version %s\n", program_name, program_version);
1263 exit (0);
1264 }
249c6fc0 1265
2fa0b342 1266 if (seenflag == false)
b3a2b497 1267 usage (stderr, 1);
2fa0b342 1268
aa0a709a
SC
1269 if (info)
1270 {
1271 display_info ();
1272 }
1273 else
1274 {
1275 if (optind == argc)
1276 display_file ("a.out", target);
1277 else
1278 for (; optind < argc;)
1279 display_file (argv[optind++], target);
1280 }
2fa0b342
DHW
1281 return 0;
1282}
This page took 0.144146 seconds and 4 git commands to generate.