]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/coffdump.c
* lib/insight-support.exp (_gdbtk_export_target_info): Add
[thirdparty/binutils-gdb.git] / binutils / coffdump.c
CommitLineData
252b5132 1/* Coff file dumper.
c32144ff 2 Copyright 1994, 1995, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
252b5132
RH
3
4This file is part of GNU Binutils.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
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
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20/* Written by Steve Chamberlain <sac@cygnus.com>
21
22 This module reads a type tree generated by coffgrok and prints
23 it out so we can test the grokker.
24*/
25
26#include <bfd.h>
27#include <getopt.h>
28#include <libiberty.h>
29
30#include "coffgrok.h"
31#include "bucomm.h"
32
33#define PROGRAM_VERSION "1.0"
34
35static int atnl;
c32144ff
AJ
36
37static void tab PARAMS ((int));
38static void nl PARAMS ((void));
39static void dump_coff_lines PARAMS ((struct coff_line *));
40static void dump_coff_type PARAMS ((struct coff_type *));
41static void dump_coff_where PARAMS ((struct coff_where *));
42static void dump_coff_visible PARAMS ((struct coff_visible *));
43extern void dump_coff_symbol PARAMS ((struct coff_symbol *));
44static void dump_coff_scope PARAMS ((struct coff_scope *));
45static void dump_coff_sfile PARAMS ((struct coff_sfile *));
46static void dump_coff_section PARAMS ((struct coff_section *));
47extern void coff_dump PARAMS ((struct coff_ofile *));
48static void show_usage PARAMS ((FILE *, int));
49static void show_help PARAMS ((void));
50extern int main PARAMS ((int, char **));
252b5132
RH
51
52static void
53tab (x)
54int x;
55{
56 static int indent;
57 int i;
58
59 if (atnl)
60 {
61 if (x < 0)
62 {
63 printf (")");
64 indent += x;
65
66 return;
67 }
68 else
69 {
70 printf ("\n");
71 atnl = 0;
72 }
73 }
74
75 if (x == -1)
76 {
77 for (i = 0; i < indent; i++)
78 printf (" ");
79
80 indent += x;
81 printf (")");
82 return;
83 }
84
85 indent += x;
86
87 for (i = 0; i < indent; i++)
88 printf (" ");
89
90 if (x)
91 {
92 printf ("(");
93 }
94}
95
96static void nl ()
97{
98 atnl = 1;
99}
100
101static void
102dump_coff_lines (p)
103 struct coff_line *p;
104{
105 int i;
106 int online = 0;
107 tab(1);
108 printf(_("#lines %d "),p->nlines);
109 for (i = 0; i < p->nlines; i++)
110 {
111 printf("(%d 0x%x)", p->lines[i], p->addresses[i]);
112 online++;
113 if (online > 6)
114 {
115 nl();
116 tab(0);
117 online = 0;
118 }
119 }
120 nl();
121 tab(-1);
122}
123
124static void
125dump_coff_type (p)
126 struct coff_type *p;
127{
128 tab (1);
129 printf ("size %d ", p->size);
130 switch (p->type)
131 {
132 case coff_secdef_type:
133 printf ("section definition at %x size %x\n",
134 p->u.asecdef.address,
135 p->u.asecdef.size);
136 nl();
137 break;
138 case coff_pointer_type:
139 printf ("pointer to");
140 nl ();
141 dump_coff_type (p->u.pointer.points_to);
142 break;
143 case coff_array_type:
144 printf ("array [%d] of", p->u.array.dim);
145 nl ();
146 dump_coff_type (p->u.array.array_of);
147 break;
148 case coff_function_type:
149 printf ("function returning");
150 nl ();
151 dump_coff_type (p->u.function.function_returns);
152 dump_coff_lines (p->u.function.lines);
153 printf ("arguments");
154 nl ();
155 dump_coff_scope (p->u.function.parameters);
156 tab (0);
157 printf ("code");
158 nl ();
159 dump_coff_scope (p->u.function.code);
160 tab(0);
161 break;
162 case coff_structdef_type:
163 printf ("structure definition");
164 nl ();
165 dump_coff_scope (p->u.astructdef.elements);
166 break;
167 case coff_structref_type:
168 if (!p->u.aenumref.ref)
169 printf ("structure ref to UNKNOWN struct");
170 else
171 printf ("structure ref to %s", p->u.aenumref.ref->name);
172 break;
173 case coff_enumref_type:
174 printf ("enum ref to %s", p->u.astructref.ref->name);
175 break;
176 case coff_enumdef_type:
177 printf ("enum definition");
178 nl ();
179 dump_coff_scope (p->u.aenumdef.elements);
180 break;
181 case coff_basic_type:
182 switch (p->u.basic)
183 {
184 case T_NULL:
185 printf ("NULL");
186 break;
187 case T_VOID:
188 printf ("VOID");
189 break;
190 case T_CHAR:
191 printf ("CHAR");
192 break;
193 case T_SHORT:
194 printf ("SHORT");
195 break;
196 case T_INT:
197 printf ("INT ");
198 break;
199 case T_LONG:
200 printf ("LONG");
201 break;
202 case T_FLOAT:
203 printf ("FLOAT");
204 break;
205 case T_DOUBLE:
206 printf ("DOUBLE");
207 break;
208 case T_STRUCT:
209 printf ("STRUCT");
210 break;
211 case T_UNION:
212 printf ("UNION");
213 break;
214 case T_ENUM:
215 printf ("ENUM");
216 break;
217 case T_MOE:
218 printf ("MOE ");
219 break;
220 case T_UCHAR:
221 printf ("UCHAR");
222 break;
223 case T_USHORT:
224 printf ("USHORT");
225 break;
226 case T_UINT:
227 printf ("UINT");
228 break;
229 case T_ULONG:
230 printf ("ULONG");
231 break;
232 case T_LNGDBL:
233 printf ("LNGDBL");
234 break;
235 default:
236 abort ();
237 }
238 }
239 nl ();
240 tab (-1);
241}
242
243static void
244dump_coff_where (p)
245 struct coff_where *p;
246{
247 tab (1);
248 switch (p->where)
249 {
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 case coff_where_entag:
271 printf ("ENTAG");
272 break;
273 case coff_where_typedef:
274 printf ("TYPEDEF");
275 break;
276 default:
277 abort ();
278 }
279 nl ();
280 tab (-1);
281}
282
283static void
284dump_coff_visible (p)
285 struct coff_visible *p;
286{
287 tab (1);
288 switch (p->type)
289 {
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 ();
325 }
326 nl ();
327 tab (-1);
328}
329
330
331void
332dump_coff_symbol (p)
333 struct coff_symbol *p;
334{
335 tab (1);
336 printf ("List of symbols");
337 nl ();
338 while (p)
339 {
340 tab (1);
341 tab (1);
342 printf ("Symbol %s, tag %d, number %d", p->name, p->tag, p->number);
343 nl ();
344 tab (-1);
345 tab (1);
346 printf ("Type");
347 nl ();
348 dump_coff_type (p->type);
349 tab (-1);
350 tab (1);
351 printf ("Where");
352 dump_coff_where (p->where);
353 tab (-1);
354 tab (1);
355 printf ("Visible");
356 dump_coff_visible (p->visible);
357 tab (-1);
358 p = p->next;
359 tab (-1);
360 }
361 tab (-1);
362}
363
364static void
365dump_coff_scope (p)
366 struct coff_scope *p;
367{
368if (p) {
369 tab (1);
370 printf ("List of blocks %lx ",(unsigned long) p);
371
372 if (p->sec) {
373 printf( " %s %x..%x", p->sec->name,p->offset, p->offset + p->size -1);
374 }
375 nl ();
376 tab (0);
377 printf ("*****************");
378 nl ();
379 while (p)
380 {
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;
390 }
391
392 tab (0);
393 printf ("*****************");
394 nl ();
395 tab (-1);
396}
397}
398
399static void
400dump_coff_sfile (p)
401 struct coff_sfile *p;
402{
403 tab (1);
404 printf ("List of source files");
405 nl ();
406 while (p)
407 {
408 tab (0);
409 printf ("Source file %s", p->name);
410 nl ();
411 dump_coff_scope (p->scope);
412 p = p->next;
413 }
414 tab (-1);
415}
416
417static void
418dump_coff_section(ptr)
419struct coff_section *ptr;
420{
421 int i;
422 tab(1);
423 printf("section %s %d %d address %x size %x number %d nrelocs %d",
424 ptr->name, ptr->code, ptr->data, ptr->address,ptr->size, ptr->number, ptr->nrelocs);
425 nl();
426
427 for (i = 0; i < ptr->nrelocs; i++)
428 {
429 tab(0);
430 printf("(%x %s %x)",
431 ptr->relocs[i].offset,
432 ptr->relocs[i].symbol->name,
433 ptr->relocs[i].addend);
434 nl();
435 }
436 tab(-1);
437
438}
439
440void
441coff_dump (ptr)
442 struct coff_ofile *ptr;
443{
444 int i;
445 printf ("Coff dump");
446 nl ();
447 printf ("#souces %d", ptr->nsources);
448 nl ();
449 dump_coff_sfile (ptr->source_head);
450 for (i = 0; i < ptr->nsections; i++)
451 dump_coff_section(ptr->sections + i);
452}
453
454
455
456char * program_name;
457
458static void
459show_usage (file, status)
460 FILE *file;
461 int status;
462{
463 fprintf (file, "Usage: %s [-hV] in-file\n", program_name);
464 exit (status);
465}
466
467static void
468show_help ()
469{
470 printf (_("%s: Print a human readable interpretation of a SYSROFF object file\n"),
471 program_name);
472 show_usage (stdout, 0);
473}
474
475
476int
477main (ac, av)
478 int ac;
479 char *av[];
480{
481 bfd *abfd;
482 struct coff_ofile *tree;
483 char **matching;
484 char *input_file = NULL;
485 int opt;
486 static struct option long_options[] =
487 {
488 { "help", no_argument, 0, 'h' },
489 { "version", no_argument, 0, 'V' },
490 { NULL, no_argument, 0, 0 }
491 };
492
493#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
494 setlocale (LC_MESSAGES, "");
495#endif
496 bindtextdomain (PACKAGE, LOCALEDIR);
497 textdomain (PACKAGE);
498
499 program_name = av[0];
500 xmalloc_set_program_name (program_name);
501
502 while ((opt = getopt_long (ac, av, "hV", long_options,
503 (int *) NULL))
504 != EOF)
505 {
506 switch (opt)
507 {
508 case 'h':
509 show_help ();
510 /*NOTREACHED*/
511 case 'V':
512 printf (_("GNU %s version %s\n"), program_name, PROGRAM_VERSION);
513 exit (0);
514 /*NOTREACHED*/
515 case 0:
516 break;
517 default:
518 show_usage (stderr, 1);
519 /*NOTREACHED*/
520 }
521 }
522
523 if (optind < ac)
524 {
525 input_file = av[optind];
526 }
527
528 if (!input_file)
529 {
37cc8ec1 530 fatal (_("no input file specified"));
252b5132
RH
531 }
532 abfd = bfd_openr (input_file, 0);
533
534 if (!abfd)
535 bfd_fatal (input_file);
536
537 if (! bfd_check_format_matches (abfd, bfd_object, &matching))
538 {
539 bfd_nonfatal (input_file);
540 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
541 {
542 list_matching_formats (matching);
543 free (matching);
544 }
545 exit (1);
546 }
547
548 tree = coff_grok (abfd);
549
550 coff_dump(tree);
551 printf("\n");
552 return 0;
553}