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