* Many files: Added gettext invocations around user-visible
[external/binutils.git] / binutils / coffdump.c
1 /* Coff file dumper.
2    Copyright (C) 1994, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU Binutils.
5
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 2 of the License, or
9 (at your option) any later version.
10
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.
15
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., 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
35 char *xcalloc(a,b)
36 int a;
37 int b;
38 {
39   char *r = xmalloc(a*b);
40   memset (r, 0, a * b);
41   return r;
42 }
43
44 static int atnl;
45 static void dump_coff_scope ();
46
47 static void
48 tab (x)
49 int x;
50 {
51   static int indent;
52   int i;
53
54   if (atnl)
55     {
56       if (x < 0)
57         {
58           printf (")");
59           indent += x;
60
61           return;
62         }
63       else
64         {
65           printf ("\n");
66           atnl = 0;
67         }
68     }
69
70   if (x == -1)
71     {
72       for (i = 0; i < indent; i++)
73         printf ("   ");
74
75       indent += x;
76       printf (")");
77       return;
78     }
79
80   indent += x;
81
82   for (i = 0; i < indent; i++)
83     printf ("   ");
84
85   if (x)
86     {
87       printf ("(");
88     }
89 }
90
91 static void nl ()
92 {
93   atnl = 1;
94 }
95
96 static void
97 dump_coff_lines (p)
98      struct coff_line *p;
99 {
100   int i;
101   int online = 0;
102   tab(1);
103   printf(_("#lines %d "),p->nlines);
104   for (i = 0; i < p->nlines; i++) 
105     {
106       printf("(%d 0x%x)", p->lines[i], p->addresses[i]);
107       online++;
108       if (online > 6)
109         {
110           nl();
111           tab(0);
112           online = 0;
113         }
114     }
115   nl();
116   tab(-1);
117 }
118
119 static void
120 dump_coff_type (p)
121      struct coff_type *p;
122 {
123   tab (1);
124   printf ("size %d ", p->size);
125   switch (p->type)
126     {
127     case coff_secdef_type:
128       printf ("section definition at %x size %x\n", 
129               p->u.asecdef.address,
130               p->u.asecdef.size);
131       nl();
132       break;
133     case coff_pointer_type:
134       printf ("pointer to");
135       nl ();
136       dump_coff_type (p->u.pointer.points_to);
137       break;
138     case coff_array_type:
139       printf ("array [%d] of", p->u.array.dim);
140       nl ();
141       dump_coff_type (p->u.array.array_of);
142       break;
143     case coff_function_type:
144       printf ("function returning");
145       nl ();
146       dump_coff_type (p->u.function.function_returns);
147       dump_coff_lines (p->u.function.lines);
148       printf ("arguments");
149       nl ();
150       dump_coff_scope (p->u.function.parameters);
151       tab (0);
152       printf ("code");
153       nl ();
154       dump_coff_scope (p->u.function.code);
155       tab(0);
156       break;
157     case coff_structdef_type:
158       printf ("structure definition");
159       nl ();
160       dump_coff_scope (p->u.astructdef.elements);
161       break;
162     case coff_structref_type:
163       if (!p->u.aenumref.ref)
164         printf ("structure ref to UNKNOWN struct");
165       else
166         printf ("structure ref to %s", p->u.aenumref.ref->name);
167       break;
168     case coff_enumref_type:
169       printf ("enum ref to %s", p->u.astructref.ref->name);
170       break;
171     case coff_enumdef_type:
172       printf ("enum definition");
173       nl ();
174       dump_coff_scope (p->u.aenumdef.elements);
175       break;
176     case coff_basic_type:
177       switch (p->u.basic)
178         {
179         case T_NULL:
180           printf ("NULL");
181           break;
182         case T_VOID:
183           printf ("VOID");
184           break;
185         case T_CHAR:
186           printf ("CHAR");
187           break;
188         case T_SHORT:
189           printf ("SHORT");
190           break;
191         case T_INT:
192           printf ("INT ");
193           break;
194         case T_LONG:
195           printf ("LONG");
196           break;
197         case T_FLOAT:
198           printf ("FLOAT");
199           break;
200         case T_DOUBLE:
201           printf ("DOUBLE");
202           break;
203         case T_STRUCT:
204           printf ("STRUCT");
205           break;
206         case T_UNION:
207           printf ("UNION");
208           break;
209         case T_ENUM:
210           printf ("ENUM");
211           break;
212         case T_MOE:
213           printf ("MOE ");
214           break;
215         case T_UCHAR:
216           printf ("UCHAR");
217           break;
218         case T_USHORT:
219           printf ("USHORT");
220           break;
221         case T_UINT:
222           printf ("UINT");
223           break;
224         case T_ULONG:
225           printf ("ULONG");
226           break;
227         case T_LNGDBL:
228           printf ("LNGDBL");
229           break;
230         default:
231           abort ();
232         }
233     }
234   nl ();
235   tab (-1);
236 }
237
238 static void
239 dump_coff_where (p)
240      struct coff_where *p;
241 {
242   tab (1);
243   switch (p->where)
244     {
245     case coff_where_stack:
246       printf ("Stack offset %x", p->offset);
247       break;
248     case coff_where_memory:
249       printf ("Memory section %s+%x", p->section->name, p->offset);
250       break;
251     case coff_where_register:
252       printf ("Register %d", p->offset);
253       break;
254     case coff_where_member_of_struct:
255       printf ("Struct Member offset %x", p->offset);
256       break;
257     case coff_where_member_of_enum:
258       printf ("Enum Member offset %x", p->offset);
259       break;
260     case coff_where_unknown:
261       printf ("Undefined symbol");
262       break;
263     case coff_where_strtag:
264       printf ("STRTAG");
265     case coff_where_entag:
266       printf ("ENTAG");
267       break;
268     case coff_where_typedef:
269       printf ("TYPEDEF");
270       break;
271     default:
272       abort ();
273     }
274   nl ();
275   tab (-1);
276 }
277
278 static void
279 dump_coff_visible (p)
280      struct coff_visible *p;
281 {
282   tab (1);
283   switch (p->type)
284     {
285     case coff_vis_ext_def:
286       printf ("coff_vis_ext_def");
287       break;
288     case coff_vis_ext_ref:
289       printf ("coff_vis_ext_ref");
290       break;
291     case coff_vis_int_def:
292       printf ("coff_vis_int_def");
293       break;
294     case coff_vis_common:
295       printf ("coff_vis_common");
296       break;
297     case coff_vis_auto:
298       printf ("coff_vis_auto");
299       break;
300     case coff_vis_autoparam:
301       printf ("coff_vis_autoparam");
302       break;
303     case coff_vis_regparam:
304       printf ("coff_vis_regparam");
305       break;
306     case coff_vis_register:
307       printf ("coff_vis_register");
308       break;
309     case coff_vis_tag:
310       printf ("coff_vis_tag");
311       break;
312     case coff_vis_member_of_struct:
313       printf ("coff_vis_member_of_struct");
314       break;
315     case coff_vis_member_of_enum:
316       printf ("coff_vis_member_of_enum");
317       break;
318     default:
319       abort ();
320     }
321   nl ();
322   tab (-1);
323 }
324
325
326 void
327 dump_coff_symbol (p)
328      struct coff_symbol *p;
329 {
330   tab (1);
331   printf ("List of symbols");
332   nl ();
333   while (p)
334     {
335       tab (1);
336       tab (1);
337       printf ("Symbol  %s, tag %d, number %d", p->name, p->tag, p->number);
338       nl ();
339       tab (-1);
340       tab (1);
341       printf ("Type");
342       nl ();
343       dump_coff_type (p->type);
344       tab (-1);
345       tab (1);
346       printf ("Where");
347       dump_coff_where (p->where);
348       tab (-1);
349       tab (1);
350       printf ("Visible");
351       dump_coff_visible (p->visible);
352       tab (-1);
353       p = p->next;
354       tab (-1);
355     }
356   tab (-1);
357 }
358
359 static void
360 dump_coff_scope (p)
361      struct coff_scope *p;
362 {
363 if (p) {
364   tab (1);
365   printf ("List of blocks %lx ",(unsigned long) p);
366
367   if (p->sec) {
368     printf( "  %s %x..%x",  p->sec->name,p->offset, p->offset + p->size -1);
369   }
370   nl ();
371   tab (0);
372   printf ("*****************");
373   nl ();
374   while (p)
375     {
376       tab (0);
377       printf ("vars %d", p->nvars);
378       nl ();
379       dump_coff_symbol (p->vars_head);
380       printf ("blocks");
381       nl ();
382       dump_coff_scope (p->list_head);
383       nl ();
384       p = p->next;
385     }
386
387   tab (0);
388   printf ("*****************");
389   nl ();
390   tab (-1);
391 }
392 }
393
394 static void
395 dump_coff_sfile (p)
396      struct coff_sfile *p;
397 {
398   tab (1);
399   printf ("List of source files");
400   nl ();
401   while (p)
402     {
403       tab (0);
404       printf ("Source file %s", p->name);
405       nl ();
406       dump_coff_scope (p->scope);
407       p = p->next;
408     }
409   tab (-1);
410 }
411
412 static void
413 dump_coff_section(ptr)
414 struct coff_section *ptr;
415 {
416   int i;
417   tab(1);
418   printf("section %s %d %d address %x size %x number %d nrelocs %d", 
419          ptr->name, ptr->code, ptr->data, ptr->address,ptr->size, ptr->number, ptr->nrelocs);
420   nl();
421
422   for (i = 0; i < ptr->nrelocs; i++) 
423     {
424       tab(0);    
425       printf("(%x %s %x)",
426              ptr->relocs[i].offset,
427              ptr->relocs[i].symbol->name,
428              ptr->relocs[i].addend);
429       nl();
430     }
431   tab(-1);
432
433 }
434
435 void
436 coff_dump (ptr)
437      struct coff_ofile *ptr;
438 {
439   int i;
440   printf ("Coff dump");
441   nl ();
442   printf ("#souces %d", ptr->nsources);
443   nl ();
444   dump_coff_sfile (ptr->source_head);
445   for (i = 0; i < ptr->nsections; i++)
446     dump_coff_section(ptr->sections + i);
447 }
448
449
450
451 char * program_name;
452
453 static void
454 show_usage (file, status)
455      FILE *file;
456      int status;
457 {
458   fprintf (file, "Usage: %s [-hV] in-file\n",   program_name);
459   exit (status);
460 }
461
462 static void
463 show_help ()
464 {
465   printf (_("%s: Print a human readable interpretation of a SYSROFF object file\n"),
466           program_name);
467   show_usage (stdout, 0);
468 }
469
470
471 int
472 main (ac, av)
473      int ac;
474      char *av[];
475 {
476   bfd *abfd;
477   struct coff_ofile *tree;
478   char **matching;
479   char *input_file = NULL;
480   int opt;
481   static struct option long_options[] =
482     {
483       { "help", no_argument, 0, 'h' },
484       { "version", no_argument, 0, 'V' },
485       { NULL, no_argument, 0, 0 }
486     };
487
488   program_name = av[0];
489   xmalloc_set_program_name (program_name);
490
491   while ((opt = getopt_long (ac, av, "hV", long_options,
492                              (int *) NULL))
493          != EOF)
494     {
495       switch (opt)
496         {
497         case 'h':
498           show_help ();
499           /*NOTREACHED*/
500         case 'V':
501           printf (_("GNU %s version %s\n"), program_name, PROGRAM_VERSION);
502           exit (0);
503           /*NOTREACHED*/
504         case 0:
505           break;
506         default:
507           show_usage (stderr, 1);
508           /*NOTREACHED*/
509         }
510     }
511
512   if (optind < ac)
513     {
514       input_file = av[optind];
515     }
516
517   if (!input_file)
518     {
519       fprintf (stderr,_("%s: no input file specified\n"),
520                program_name);
521       exit(1);
522     }
523   abfd = bfd_openr (input_file, 0);
524
525   if (!abfd)
526     bfd_fatal (input_file);
527
528   if (! bfd_check_format_matches (abfd, bfd_object, &matching))
529     {
530       bfd_nonfatal (input_file);
531       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
532         {
533           list_matching_formats (matching);
534           free (matching);
535         }
536       exit (1);
537     }
538
539   tree = coff_grok (abfd);
540
541   coff_dump(tree);
542   printf("\n");
543   return 0;
544 }