* Makefile.in (syslex, sysinfo): Use CC_FOR_BUILD.
[external/binutils.git] / binutils / sysdump.c
1 /* Sysroff object format dumper.
2    Copyright (C) 1994 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20
21 /* Written by Steve Chamberlain <sac@cygnus.com>.
22
23    This program reads a SYSROFF object file and prints it in an
24    almost human readable form to stdout. */
25
26 #include <stdio.h>
27 #include <libiberty.h>
28 #include <getopt.h>
29 #include "sysroff.h"
30 #include <stdlib.h>
31
32 #define PROGRAM_VERSION "1.0"
33
34 static int dump = 1;
35 static int segmented_p;
36 static int code;
37 static FILE *file;
38
39 static char *xcalloc(a,b)
40 int a;
41 int b;
42 {
43   char *r = xmalloc(a,b);
44   memset (r, 0, a * b);
45   return r;
46 }
47
48 char *
49 getCHARS (ptr, idx, size, max)
50      unsigned char *ptr;
51      int *idx;
52      int size;
53      int max;
54 {
55   int oc = *idx / 8;
56   char *r;
57   int b = size;
58   if (b >= max)
59     {
60       return "*undefined*";
61     }
62
63   if (b == 0)
64     {
65       /* Got to work out the length of the string from self */
66       b = ptr[oc++];
67       (*idx) += 8;
68     }
69
70   *idx += b * 8;
71   r = calloc (b + 1, 1);
72   memcpy (r, ptr + oc, b);
73   r[b] = 0;
74   return r;
75 }
76 static void
77 dh (ptr, size)
78      unsigned char *ptr;
79      int size;
80 {
81   int i;
82   int j;
83   int span = 20;
84
85   printf ("\n************************************************************\n");
86
87   for (i = 0; i < size; i += span)
88     {
89       for (j = 0; j < span && j + i < size; j++)
90         {
91           printf ("%02x ", ptr[i + j]);
92         }
93       printf ("\n");
94     }
95
96   for (i = 0; i < size; i += span)
97     {
98       for (j = 0; j < span && j + i < size; j++)
99         {
100           int c = ptr[i + j];
101           if (c < 32 || c > 127)
102             c = '.';
103           printf (" %c ", c);
104         }
105       printf ("\n");
106     }
107 }
108
109 int 
110 fillup (ptr)
111      char *ptr;
112 {
113   int size;
114   int sum;
115   int i;
116   size = getc (file) - 2;
117   fread (ptr, 1, size, file);
118   sum = code + size + 2;
119   for (i = 0; i < size; i++)
120     {
121       sum += ptr[i];
122     }
123
124   if ((sum & 0xff) != 0xff)
125     {
126       printf ("SUM IS %x\n", sum);
127     }
128   if (dump)
129     dh (ptr, size);
130   return size;
131 }
132
133
134 barray 
135 getBARRAY (ptr, idx, dsize, max)
136      unsigned char *ptr;
137      int *idx;
138      int dsize;
139      int max;
140 {
141   barray res;
142   int i;
143   int byte = *idx / 8;
144   int size = ptr[byte++];
145   res.len = size;
146   res.data = (unsigned char *)xmalloc (size);
147   for (i = 0; i < size; i++)
148     {
149       res.data[i] = ptr[byte++];
150     }
151   return res;
152 }
153
154
155
156 int 
157 getINT (ptr, idx, size, max)
158      unsigned char *ptr;
159      int *idx;
160      int size;
161      int max;
162 {
163   int n = 0;
164   int byte = *idx / 8;
165
166   if (byte >= max)
167     {
168       return 0;
169     }
170   if (size == -2)
171     size = 4;
172   if (size == -1)
173     size = 0;
174   switch (size)
175     {
176     case 0:
177       return 0;
178     case 1:
179       n = (ptr[byte]);
180       break;
181     case 2:
182       n = (ptr[byte + 0] << 8) + ptr[byte + 1];
183       break;
184     case 4:
185       n = (ptr[byte + 0] << 24) + (ptr[byte + 1] << 16) + (ptr[byte + 2] << 8) + (ptr[byte + 3]);
186       break;
187     default:
188       abort ();
189     }
190   *idx += size * 8;
191   return n;
192 }
193
194 int
195 getBITS (ptr, idx, size)
196      char *ptr;
197      int *idx;
198      int size;
199 {
200   int byte = *idx / 8;
201   int bit = *idx % 8;
202
203   *idx += size;
204
205   return (ptr[byte] >> (8 - bit - size)) & ((1 << size) - 1);
206 }
207
208
209 static void
210 itheader (name, code)
211      char *name;
212      int code;
213 {
214   printf ("\n%s 0x%02x\n", name, code);
215 }
216 static int indent;
217 static void
218 p ()
219 {
220   int i;
221   for (i = 0; i < indent; i++)
222     {
223       printf ("| ");
224     }
225   printf ("> ");
226 }
227
228 static void 
229 tabout ()
230 {
231   p ();
232 }
233
234 static void
235 pbarray (y)
236      barray *y;
237 {
238   int x;
239   printf ("%d (", y->len);
240   for (x = 0; x < y->len; x++)
241     {
242       printf ("(%02x %c)", y->data[x], isprint (y->data[x]) ? y->data[x] : '.');
243     }
244   printf (")\n");
245 }
246
247 #define SYSROFF_PRINT
248 #define SYSROFF_SWAP_IN
249
250 #include "sysroff.c"
251
252
253 static int
254 getone (type)
255      int type;
256 {
257   int c = getc (file);
258   code = c;
259
260   if ((c & 0x7f) != type)
261     {
262       ungetc (c, file);
263       return 0;
264     }
265
266   switch (c & 0x7f)
267     {
268     case IT_cs_CODE:
269       {
270         struct IT_cs dummy;
271         sysroff_swap_cs_in (&dummy);
272         sysroff_print_cs_out (&dummy);
273       }
274       break;
275     case IT_dln_CODE:
276       {
277         struct IT_dln dummy;
278         sysroff_swap_dln_in (&dummy);
279         sysroff_print_dln_out (&dummy);
280       }
281       break;
282     case IT_hd_CODE:
283       {
284         struct IT_hd dummy;
285         sysroff_swap_hd_in (&dummy);
286         sysroff_print_hd_out (&dummy);
287       }
288       break;
289     case IT_dar_CODE:
290       {
291         struct IT_dar dummy;
292         sysroff_swap_dar_in (&dummy);
293         sysroff_print_dar_out (&dummy);
294       }
295       break;
296     case IT_dsy_CODE:
297       {
298         struct IT_dsy dummy;
299         sysroff_swap_dsy_in (&dummy);
300         sysroff_print_dsy_out (&dummy);
301       }
302       break;
303     case IT_dfp_CODE:
304       {
305         struct IT_dfp dummy;
306         sysroff_swap_dfp_in (&dummy);
307         sysroff_print_dfp_out (&dummy);
308       }
309       break;
310     case IT_dso_CODE:
311       {
312         struct IT_dso dummy;
313         sysroff_swap_dso_in (&dummy);
314         sysroff_print_dso_out (&dummy);
315       }
316       break;
317     case IT_dpt_CODE:
318       {
319         struct IT_dpt dummy;
320         sysroff_swap_dpt_in (&dummy);
321         sysroff_print_dpt_out (&dummy);
322       }
323       break;
324     case IT_den_CODE:
325       {
326         struct IT_den dummy;
327         sysroff_swap_den_in (&dummy);
328         sysroff_print_den_out (&dummy);
329       }
330       break;
331     case IT_dbt_CODE:
332       {
333         struct IT_dbt dummy;
334         sysroff_swap_dbt_in (&dummy);
335         sysroff_print_dbt_out (&dummy);
336       }
337       break;
338     case IT_dty_CODE:
339       {
340         struct IT_dty dummy;
341         sysroff_swap_dty_in (&dummy);
342         sysroff_print_dty_out (&dummy);
343       }
344       break;
345     case IT_un_CODE:
346       {
347         struct IT_un dummy;
348         sysroff_swap_un_in (&dummy);
349         sysroff_print_un_out (&dummy);
350       }
351       break;
352     case IT_sc_CODE:
353       {
354         struct IT_sc dummy;
355         sysroff_swap_sc_in (&dummy);
356         sysroff_print_sc_out (&dummy);
357       }
358       break;
359     case IT_er_CODE:
360       {
361         struct IT_er dummy;
362         sysroff_swap_er_in (&dummy);
363         sysroff_print_er_out (&dummy);
364       }
365       break;
366     case IT_ed_CODE:
367       {
368         struct IT_ed dummy;
369         sysroff_swap_ed_in (&dummy);
370         sysroff_print_ed_out (&dummy);
371       }
372       break;
373     case IT_sh_CODE:
374       {
375         struct IT_sh dummy;
376         sysroff_swap_sh_in (&dummy);
377         sysroff_print_sh_out (&dummy);
378       }
379       break;
380     case IT_ob_CODE:
381       {
382         struct IT_ob dummy;
383         sysroff_swap_ob_in (&dummy);
384         sysroff_print_ob_out (&dummy);
385       }
386       break;
387     case IT_rl_CODE:
388       {
389         struct IT_rl dummy;
390         sysroff_swap_rl_in (&dummy);
391         sysroff_print_rl_out (&dummy);
392       }
393       break;
394     case IT_du_CODE:
395       {
396         struct IT_du dummy;
397         sysroff_swap_du_in (&dummy);
398
399         sysroff_print_du_out (&dummy);
400       }
401       break;
402     case IT_dus_CODE:
403       {
404         struct IT_dus dummy;
405         sysroff_swap_dus_in (&dummy);
406         sysroff_print_dus_out (&dummy);
407       }
408       break;
409     case IT_dul_CODE:
410       {
411         struct IT_dul dummy;
412         sysroff_swap_dul_in (&dummy);
413         sysroff_print_dul_out (&dummy);
414       }
415       break;
416     case IT_dss_CODE:
417       {
418         struct IT_dss dummy;
419         sysroff_swap_dss_in (&dummy);
420         sysroff_print_dss_out (&dummy);
421       }
422     case IT_hs_CODE:
423       {
424         struct IT_hs dummy;
425         sysroff_swap_hs_in (&dummy);
426         sysroff_print_hs_out (&dummy);
427       }
428
429
430     case IT_dps_CODE:
431       {
432         struct IT_dps dummy;
433         sysroff_swap_dps_in (&dummy);
434         sysroff_print_dps_out (&dummy);
435       }
436       break;
437
438
439     case IT_tr_CODE:
440       {
441         struct IT_tr dummy;
442         sysroff_swap_tr_in (&dummy);
443         sysroff_print_tr_out (&dummy);
444       }
445       break;
446     case IT_dds_CODE:
447       {
448         struct IT_dds dummy;
449         sysroff_swap_dds_in (&dummy);
450         sysroff_print_dds_out (&dummy);
451       }
452       break;
453       break;
454     default:
455       printf ("GOT A %x\n", c);
456       return 0;
457       break;
458     }
459   return 1;
460
461 }
462
463 static int
464 opt (x)
465      int x;
466 {
467   return getone (x);
468 }
469
470 static void 
471 unit_info_list ()
472 {
473   if (opt (IT_un_CODE))
474     {
475       while (getone (IT_sc_CODE))
476         {
477           getone (IT_ss_CODE);
478         }
479
480       while (getone (IT_er_CODE))
481         {
482         }
483
484       while (getone (IT_ed_CODE))
485         {
486         }
487     }
488 }
489
490 static void
491 object_body_list ()
492 {
493   getone (IT_sh_CODE);
494   while (getone (IT_ob_CODE))
495     ;
496   while (getone (IT_rl_CODE))
497     ;
498 }
499
500 static void 
501 must (x)
502      int x;
503 {
504   if (!getone (x))
505     {
506       printf ("WANTED %x!!\n", x);
507     }
508 }
509
510 static void 
511 tab (i, s)
512      int i;
513      char *s;
514 {
515   indent += i;
516   if (s)
517     {
518       p ();
519       printf (s);
520       printf ("\n");
521     }
522 }
523 static void derived_type ();
524
525 static void
526 symbol_info ()
527 {
528   tab (1, "SYMBOL INFO");
529   while (opt (IT_dsy_CODE))
530     {
531       if (opt (IT_dty_CODE))
532         {
533           must (IT_dbt_CODE);
534           derived_type ();
535           must (IT_dty_CODE);
536         }
537     }
538   tab (-1, "");
539 }
540
541 static void
542 derived_type ()
543 {
544   tab (1, "DERIVED TYPE");
545   while (1)
546     {
547       if (opt (IT_dpp_CODE))
548         {
549           symbol_info ();
550           must (IT_dpp_CODE);
551         }
552       else if (opt (IT_dfp_CODE))
553         {
554           symbol_info ();
555           must (IT_dfp_CODE);
556         }
557       else if (opt (IT_den_CODE))
558         {
559           symbol_info ();
560           must (IT_den_CODE);
561         }
562       else if (opt (IT_den_CODE))
563         {
564           symbol_info ();
565           must (IT_den_CODE);
566         }
567       else if (opt (IT_dds_CODE))
568         {
569           symbol_info ();
570           must (IT_dds_CODE);
571         }
572       else if (opt (IT_dar_CODE))
573         {
574         }
575       else if (opt (IT_dpt_CODE))
576         {
577         }
578       else if (opt (IT_dul_CODE))
579         {
580         }
581       else if (opt (IT_dse_CODE))
582         {
583         }
584       else if (opt (IT_dot_CODE))
585         {
586         }
587       else
588         break;
589     }
590
591   tab (-1, "");
592 }
593
594 static void
595 program_structure ()
596 {
597   tab (1, "PROGRAM STRUCTURE");
598   while (opt (IT_dps_CODE))
599     {
600       opt (IT_dso_CODE);
601       opt (IT_dss_CODE);
602       symbol_info ();
603     }
604   tab (-1, "");
605 }
606 static void
607 debug_list ()
608 {
609   tab (1, "DEBUG LIST");
610   getone (IT_du_CODE);
611   while (getone (IT_dus_CODE))
612     ;
613   while (opt (IT_dfl_CODE))
614     ;
615   while (getone (IT_dus_CODE))
616     ;
617
618   program_structure ();
619
620   getone (IT_dln_CODE);
621   tab (-1, "");
622 }
623
624 static void
625 module ()
626 {
627   int c = 0;
628   int l = 0;
629   
630   tab (1, "MODULE***\n");
631
632   getone (IT_cs_CODE);
633   getone (IT_hd_CODE);
634   getone (IT_hs_CODE);
635
636   while (!opt (IT_tr_CODE) && c < 10)
637     {
638       unit_info_list ();
639       object_body_list ();
640       debug_list ();
641 c++;
642     }
643   tab (-1, "");
644
645   c = getc (file);
646   while (c != EOF)
647     {
648       printf ("%02x ", c);
649       l++;
650       if (l == 32)
651         {
652           printf ("\n");
653           l = 0;
654         }
655       c = getc (file);
656     }
657 }
658
659
660 char * program_name;
661
662 static void
663 show_usage (file, status)
664      FILE *file;
665      int status;
666 {
667   fprintf (file, "Usage: %s [-hV] in-file\n",   program_name);
668   exit (status);
669 }
670
671 static void
672 show_help ()
673 {
674   printf ("%s: Print a human readable interpretation of a SYSROFF object file\n",
675           program_name);
676   show_usage (stdout, 0);
677 }
678
679
680 int 
681 main (ac, av)
682      int ac;
683      char **av;
684 {
685   char *input_file = NULL;
686   int opt;
687   static struct option long_options[] =
688     {
689       { "help", no_argument, 0, 'h' },
690       { "version", no_argument, 0, 'V' },
691       { NULL, no_argument, 0, 0 }
692     };
693
694   program_name = av[0];
695   xmalloc_set_program_name (program_name);
696
697   while ((opt = getopt_long (ac, av, "hV", long_options,
698                              (int *) NULL))
699          != EOF)
700     {
701       switch (opt)
702         {
703         case 'h':
704           show_help ();
705           /*NOTREACHED*/
706         case 'V':
707           printf ("GNU %s version %s\n", program_name, PROGRAM_VERSION);
708           exit (0);
709           /*NOTREACHED*/
710         case 0:
711           break;
712         default:
713           show_usage (stderr, 1);
714           /*NOTREACHED*/
715         }
716     }
717
718   /* The input and output files may be named on the command line.  */
719
720   if (optind < ac)
721     {
722       input_file = av[optind];
723     }
724
725   if (!input_file)
726     {
727       fprintf (stderr,"%s: no input file specified\n",
728                program_name);
729       exit(1);
730     }
731
732   file = fopen (input_file, "r");
733   if (!file)
734     {
735       fprintf (stderr,"%s: cannot open input file %s\n",
736                program_name, input_file);
737       exit(1);
738     }
739
740
741   module ();
742   return 0;
743 }