Automatic date update in version.in
[binutils-gdb.git] / binutils / coffdump.c
blob00a32ccae977c4e974923bde694a711dfcbc042b
1 /* Coff file dumper.
2 Copyright (C) 1994-2022 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or (at
9 your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
22 /* Written by Steve Chamberlain <sac@cygnus.com>
24 This module reads a type tree generated by coffgrok and prints
25 it out so we can test the grokker. */
27 #include "sysdep.h"
28 #include "bfd.h"
29 #include <stdint.h>
30 #include "libiberty.h"
31 #include "bucomm.h"
33 #include "coffgrok.h"
34 #include "getopt.h"
36 static int atnl;
38 static void tab (int);
39 static void nl (void);
40 static void dump_coff_lines (struct coff_line *);
41 static void dump_coff_type (struct coff_type *);
42 static void dump_coff_where (struct coff_where *);
43 static void dump_coff_visible (struct coff_visible *);
44 static void dump_coff_scope (struct coff_scope *);
45 static void dump_coff_sfile (struct coff_sfile *);
46 static void dump_coff_section (struct coff_section *);
47 static void show_usage (FILE *, int);
48 extern int main (int, char **);
50 static void
51 tab (int x)
53 static int indent;
54 int i;
56 if (atnl)
58 if (x < 0)
60 printf (")");
61 indent += x;
63 return;
65 else
67 printf ("\n");
68 atnl = 0;
72 if (x == -1)
74 for (i = 0; i < indent; i++)
75 printf (" ");
77 indent += x;
78 printf (")");
79 return;
82 indent += x;
84 for (i = 0; i < indent; i++)
85 printf (" ");
87 if (x)
89 printf ("(");
93 static void
94 nl (void)
96 atnl = 1;
99 static void
100 dump_coff_lines (struct coff_line *p)
102 int i;
103 int online = 0;
105 tab (1);
106 printf (_("#lines %d "),p->nlines);
108 for (i = 0; i < p->nlines; i++)
110 printf ("(%d 0x%x)", p->lines[i], p->addresses[i]);
112 online++;
114 if (online > 6)
116 nl ();
117 tab (0);
118 online = 0;
121 nl ();
122 tab (-1);
125 static void
126 dump_coff_type (struct coff_type *p)
128 tab (1);
129 printf (_("size %d "), p->size);
131 switch (p->type)
133 case coff_secdef_type:
134 printf (_("section definition at %x size %x\n"),
135 p->u.asecdef.address,
136 p->u.asecdef.size);
137 nl ();
138 break;
139 case coff_pointer_type:
140 printf (_("pointer to"));
141 nl ();
142 dump_coff_type (p->u.pointer.points_to);
143 break;
144 case coff_array_type:
145 printf (_("array [%d] of"), p->u.array.dim);
146 nl ();
147 dump_coff_type (p->u.array.array_of);
148 break;
149 case coff_function_type:
150 printf (_("function returning"));
151 nl ();
152 dump_coff_type (p->u.function.function_returns);
153 dump_coff_lines (p->u.function.lines);
154 printf (_("arguments"));
155 nl ();
156 dump_coff_scope (p->u.function.parameters);
157 tab (0);
158 printf (_("code"));
159 nl ();
160 dump_coff_scope (p->u.function.code);
161 tab(0);
162 break;
163 case coff_structdef_type:
164 printf (_("structure definition"));
165 nl ();
166 dump_coff_scope (p->u.astructdef.elements);
167 break;
168 case coff_structref_type:
169 if (!p->u.aenumref.ref)
170 printf (_("structure ref to UNKNOWN struct"));
171 else
172 printf (_("structure ref to %s"), p->u.aenumref.ref->name);
173 break;
174 case coff_enumref_type:
175 printf (_("enum ref to %s"), p->u.astructref.ref->name);
176 break;
177 case coff_enumdef_type:
178 printf (_("enum definition"));
179 nl ();
180 dump_coff_scope (p->u.aenumdef.elements);
181 break;
182 case coff_basic_type:
183 switch (p->u.basic)
185 case T_NULL:
186 printf ("NULL");
187 break;
188 case T_VOID:
189 printf ("VOID");
190 break;
191 case T_CHAR:
192 printf ("CHAR");
193 break;
194 case T_SHORT:
195 printf ("SHORT");
196 break;
197 case T_INT:
198 printf ("INT ");
199 break;
200 case T_LONG:
201 printf ("LONG");
202 break;
203 case T_FLOAT:
204 printf ("FLOAT");
205 break;
206 case T_DOUBLE:
207 printf ("DOUBLE");
208 break;
209 case T_STRUCT:
210 printf ("STRUCT");
211 break;
212 case T_UNION:
213 printf ("UNION");
214 break;
215 case T_ENUM:
216 printf ("ENUM");
217 break;
218 case T_MOE:
219 printf ("MOE ");
220 break;
221 case T_UCHAR:
222 printf ("UCHAR");
223 break;
224 case T_USHORT:
225 printf ("USHORT");
226 break;
227 case T_UINT:
228 printf ("UINT");
229 break;
230 case T_ULONG:
231 printf ("ULONG");
232 break;
233 case T_LNGDBL:
234 printf ("LNGDBL");
235 break;
236 default:
237 abort ();
240 nl ();
241 tab (-1);
244 static void
245 dump_coff_where (struct coff_where *p)
247 tab (1);
248 switch (p->where)
250 case coff_where_stack:
251 printf (_("Stack offset %x"), p->offset);
252 break;
253 case coff_where_memory:
254 printf (_("Memory section %s+%x"), p->section->name, p->offset);
255 break;
256 case coff_where_register:
257 printf (_("Register %d"), p->offset);
258 break;
259 case coff_where_member_of_struct:
260 printf (_("Struct Member offset %x"), p->offset);
261 break;
262 case coff_where_member_of_enum:
263 printf (_("Enum Member offset %x"), p->offset);
264 break;
265 case coff_where_unknown:
266 printf (_("Undefined symbol"));
267 break;
268 case coff_where_strtag:
269 printf ("STRTAG");
270 break;
271 case coff_where_entag:
272 printf ("ENTAG");
273 break;
274 case coff_where_typedef:
275 printf ("TYPEDEF");
276 break;
277 default:
278 abort ();
280 nl ();
281 tab (-1);
284 static void
285 dump_coff_visible (struct coff_visible *p)
287 tab (1);
288 switch (p->type)
290 case coff_vis_ext_def:
291 printf ("coff_vis_ext_def");
292 break;
293 case coff_vis_ext_ref:
294 printf ("coff_vis_ext_ref");
295 break;
296 case coff_vis_int_def:
297 printf ("coff_vis_int_def");
298 break;
299 case coff_vis_common:
300 printf ("coff_vis_common");
301 break;
302 case coff_vis_auto:
303 printf ("coff_vis_auto");
304 break;
305 case coff_vis_autoparam:
306 printf ("coff_vis_autoparam");
307 break;
308 case coff_vis_regparam:
309 printf ("coff_vis_regparam");
310 break;
311 case coff_vis_register:
312 printf ("coff_vis_register");
313 break;
314 case coff_vis_tag:
315 printf ("coff_vis_tag");
316 break;
317 case coff_vis_member_of_struct:
318 printf ("coff_vis_member_of_struct");
319 break;
320 case coff_vis_member_of_enum:
321 printf ("coff_vis_member_of_enum");
322 break;
323 default:
324 abort ();
326 nl ();
327 tab (-1);
330 static void
331 dump_coff_symbol (struct coff_symbol *p)
333 tab (1);
334 printf (_("List of symbols"));
335 nl ();
337 while (p)
339 tab (1);
340 tab (1);
341 printf (_("Symbol %s, tag %d, number %d"), p->name, p->tag, p->number);
342 nl ();
343 tab (-1);
344 tab (1);
345 printf (_("Type"));
346 nl ();
347 dump_coff_type (p->type);
348 tab (-1);
349 tab (1);
350 printf (_("Where"));
351 dump_coff_where (p->where);
352 tab (-1);
353 tab (1);
354 printf (_("Visible"));
355 dump_coff_visible (p->visible);
356 tab (-1);
357 p = p->next;
358 tab (-1);
360 tab (-1);
363 static void
364 dump_coff_scope (struct coff_scope *p)
366 if (p)
368 tab (1);
369 printf ("%s %p ", _("List of blocks "), p);
371 if (p->sec)
372 printf( " %s %x..%x", p->sec->name,p->offset, p->offset + p->size -1);
374 nl ();
375 tab (0);
376 printf ("*****************");
377 nl ();
379 while (p)
381 tab (0);
382 printf (_("vars %d"), p->nvars);
383 nl ();
384 dump_coff_symbol (p->vars_head);
385 printf (_("blocks"));
386 nl ();
387 dump_coff_scope (p->list_head);
388 nl ();
389 p = p->next;
392 tab (0);
393 printf ("*****************");
394 nl ();
395 tab (-1);
399 static void
400 dump_coff_sfile (struct coff_sfile *p)
402 tab (1);
403 printf (_("List of source files"));
404 nl ();
406 while (p)
408 tab (0);
409 printf (_("Source file %s"), p->name);
410 nl ();
411 dump_coff_scope (p->scope);
412 p = p->next;
414 tab (-1);
417 static void
418 dump_coff_section (struct coff_section *ptr)
420 unsigned int i;
422 tab (1);
423 printf (_("section %s %d %d address %x size %x number %d nrelocs %u"),
424 ptr->name, ptr->code, ptr->data, ptr->address,ptr->size,
425 ptr->number, ptr->nrelocs);
426 nl ();
428 for (i = 0; i < ptr->nrelocs; i++)
430 struct coff_reloc * r = ptr->relocs + i;
431 tab (0);
432 printf ("(%x %s %x)",
433 r->offset,
434 /* PR 17512: file: 0a38fb7c. */
435 r->symbol == NULL ? _("<no sym>") : r->symbol->name,
436 r->addend);
437 nl ();
440 tab (-1);
443 static void
444 coff_dump (struct coff_ofile *ptr)
446 int i;
448 printf ("Coff dump");
449 nl ();
450 printf (_("#sources %d"), ptr->nsources);
451 nl ();
452 dump_coff_sfile (ptr->source_head);
454 for (i = 0; i < ptr->nsections; i++)
455 dump_coff_section (ptr->sections + i);
458 static void
459 show_usage (FILE *file, int status)
461 fprintf (file, _("Usage: %s [option(s)] in-file\n"), program_name);
462 fprintf (file, _(" Print a human readable interpretation of a COFF object file\n"));
463 fprintf (file, _(" The options are:\n\
464 @<file> Read options from <file>\n\
465 -h --help Display this information\n\
466 -v --version Display the program's version\n\
467 \n"));
469 if (REPORT_BUGS_TO[0] && status == 0)
470 fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
472 exit (status);
476 main (int ac, char **av)
478 bfd *abfd;
479 struct coff_ofile *tree;
480 char **matching;
481 char *input_file = NULL;
482 int opt;
483 static struct option long_options[] =
485 { "help", no_argument, 0, 'h' },
486 { "version", no_argument, 0, 'V' },
487 { NULL, no_argument, 0, 0 }
490 #ifdef HAVE_LC_MESSAGES
491 setlocale (LC_MESSAGES, "");
492 #endif
493 setlocale (LC_CTYPE, "");
494 bindtextdomain (PACKAGE, LOCALEDIR);
495 textdomain (PACKAGE);
497 program_name = av[0];
498 xmalloc_set_program_name (program_name);
499 bfd_set_error_program_name (program_name);
501 expandargv (&ac, &av);
503 while ((opt = getopt_long (ac, av, "HhVv", long_options,
504 (int *) NULL))
505 != EOF)
507 switch (opt)
509 case 'H':
510 case 'h':
511 show_usage (stdout, 0);
512 break;
513 case 'v':
514 case 'V':
515 print_version ("coffdump");
516 exit (0);
517 case 0:
518 break;
519 default:
520 show_usage (stderr, 1);
521 break;
525 if (optind < ac)
527 input_file = av[optind];
530 if (!input_file)
531 fatal (_("no input file specified"));
533 abfd = bfd_openr (input_file, 0);
535 if (!abfd)
536 bfd_fatal (input_file);
538 if (! bfd_check_format_matches (abfd, bfd_object, &matching))
540 bfd_nonfatal (input_file);
542 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
543 list_matching_formats (matching);
544 exit (1);
547 tree = coff_grok (abfd);
548 if (tree)
550 coff_dump (tree);
551 printf ("\n");
554 return 0;