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