OSDN Git Service

Locale changes from Bruno Haible <haible@clisp.cons.org>.
[pf3gnuchains/pf3gnuchains3x.git] / binutils / coffdump.c
1 /* Coff file dumper.
2    Copyright 1994, 1995, 1998, 1999, 2000, 2001 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 static int atnl;
36
37 static void tab PARAMS ((int));
38 static void nl PARAMS ((void));
39 static void dump_coff_lines PARAMS ((struct coff_line *));
40 static void dump_coff_type PARAMS ((struct coff_type *));
41 static void dump_coff_where PARAMS ((struct coff_where *));
42 static void dump_coff_visible PARAMS ((struct coff_visible *));
43 extern void dump_coff_symbol PARAMS ((struct coff_symbol *));
44 static void dump_coff_scope PARAMS ((struct coff_scope *));
45 static void dump_coff_sfile PARAMS ((struct coff_sfile *));
46 static void dump_coff_section PARAMS ((struct coff_section *));
47 extern void coff_dump PARAMS ((struct coff_ofile *));
48 static void show_usage PARAMS ((FILE *, int));
49 static void show_help PARAMS ((void));
50 extern int main PARAMS ((int, char **));
51
52 static void
53 tab (x)
54 int 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
96 static void nl ()
97 {
98   atnl = 1;
99 }
100
101 static void
102 dump_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
124 static void
125 dump_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
243 static void
244 dump_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
283 static void
284 dump_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
331 void
332 dump_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
364 static void
365 dump_coff_scope (p)
366      struct coff_scope *p;
367 {
368 if (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
399 static void
400 dump_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
417 static void
418 dump_coff_section(ptr)
419 struct 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
440 void
441 coff_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
456 char * program_name;
457
458 static void
459 show_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
467 static void
468 show_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
476 int
477 main (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 #if defined (HAVE_SETLOCALE)
497   setlocale (LC_CTYPE, "");
498 #endif
499   bindtextdomain (PACKAGE, LOCALEDIR);
500   textdomain (PACKAGE);
501
502   program_name = av[0];
503   xmalloc_set_program_name (program_name);
504
505   while ((opt = getopt_long (ac, av, "hV", long_options,
506                              (int *) NULL))
507          != EOF)
508     {
509       switch (opt)
510         {
511         case 'h':
512           show_help ();
513           /*NOTREACHED*/
514         case 'V':
515           printf (_("GNU %s version %s\n"), program_name, PROGRAM_VERSION);
516           exit (0);
517           /*NOTREACHED*/
518         case 0:
519           break;
520         default:
521           show_usage (stderr, 1);
522           /*NOTREACHED*/
523         }
524     }
525
526   if (optind < ac)
527     {
528       input_file = av[optind];
529     }
530
531   if (!input_file)
532     {
533       fatal (_("no input file specified"));
534     }
535   abfd = bfd_openr (input_file, 0);
536
537   if (!abfd)
538     bfd_fatal (input_file);
539
540   if (! bfd_check_format_matches (abfd, bfd_object, &matching))
541     {
542       bfd_nonfatal (input_file);
543       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
544         {
545           list_matching_formats (matching);
546           free (matching);
547         }
548       exit (1);
549     }
550
551   tree = coff_grok (abfd);
552
553   coff_dump(tree);
554   printf("\n");
555   return 0;
556 }