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