* warnings.m4 (ACX_PROG_CC_WARNING_ALMOST_PEDANTIC): Don't do
[binutils.git] / binutils / sysdump.c
blobe75de846daad31cb9a91427d497426e6f1f04744
1 /* Sysroff object format dumper.
2 Copyright 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* Written by Steve Chamberlain <sac@cygnus.com>.
25 This program reads a SYSROFF object file and prints it in an
26 almost human readable form to stdout. */
28 #include "bfd.h"
29 #include "bucomm.h"
30 #include "safe-ctype.h"
32 #include <stdio.h>
33 #include "libiberty.h"
34 #include "getopt.h"
35 #include "sysroff.h"
37 static int dump = 1;
38 static int segmented_p;
39 static int code;
40 static int addrsize = 4;
41 static FILE *file;
43 static void dh (unsigned char *, int);
44 static void itheader (char *, int);
45 static void p (void);
46 static void tabout (void);
47 static void pbarray (barray *);
48 static int getone (int);
49 static int opt (int);
50 static void must (int);
51 static void tab (int, char *);
52 static void dump_symbol_info (void);
53 static void derived_type (void);
54 static void module (void);
55 static void show_usage (FILE *, int);
57 extern char *getCHARS (unsigned char *, int *, int, int);
58 extern int fillup (char *);
59 extern barray getBARRAY (unsigned char *, int *, int, int);
60 extern int getINT (unsigned char *, int *, int, int);
61 extern int getBITS (char *, int *, int, int);
62 extern void sysroff_swap_tr_in (void);
63 extern void sysroff_print_tr_out (void);
64 extern int main (int, char **);
66 char *
67 getCHARS (unsigned char *ptr, int *idx, int size, int max)
69 int oc = *idx / 8;
70 char *r;
71 int b = size;
73 if (b >= max)
74 return "*undefined*";
76 if (b == 0)
78 /* Got to work out the length of the string from self. */
79 b = ptr[oc++];
80 (*idx) += 8;
83 *idx += b * 8;
84 r = xcalloc (b + 1, 1);
85 memcpy (r, ptr + oc, b);
86 r[b] = 0;
88 return r;
91 static void
92 dh (unsigned char *ptr, int size)
94 int i;
95 int j;
96 int span = 16;
98 printf ("\n************************************************************\n");
100 for (i = 0; i < size; i += span)
102 for (j = 0; j < span; j++)
104 if (j + i < size)
105 printf ("%02x ", ptr[i + j]);
106 else
107 printf (" ");
110 for (j = 0; j < span && j + i < size; j++)
112 int c = ptr[i + j];
114 if (c < 32 || c > 127)
115 c = '.';
116 printf ("%c", c);
119 printf ("\n");
124 fillup (char *ptr)
126 int size;
127 int sum;
128 int i;
130 size = getc (file) - 2;
131 fread (ptr, 1, size, file);
132 sum = code + size + 2;
134 for (i = 0; i < size; i++)
135 sum += ptr[i];
137 if ((sum & 0xff) != 0xff)
138 printf ("SUM IS %x\n", sum);
140 if (dump)
141 dh (ptr, size);
143 return size - 1;
146 barray
147 getBARRAY (unsigned char *ptr, int *idx, int dsize ATTRIBUTE_UNUSED,
148 int max ATTRIBUTE_UNUSED)
150 barray res;
151 int i;
152 int byte = *idx / 8;
153 int size = ptr[byte++];
155 res.len = size;
156 res.data = (unsigned char *) xmalloc (size);
158 for (i = 0; i < size; i++)
159 res.data[i] = ptr[byte++];
161 return res;
165 getINT (unsigned char *ptr, int *idx, int size, int max)
167 int n = 0;
168 int byte = *idx / 8;
170 if (byte >= max)
171 return 0;
173 if (size == -2)
174 size = addrsize;
176 if (size == -1)
177 size = 0;
179 switch (size)
181 case 0:
182 return 0;
183 case 1:
184 n = (ptr[byte]);
185 break;
186 case 2:
187 n = (ptr[byte + 0] << 8) + ptr[byte + 1];
188 break;
189 case 4:
190 n = (ptr[byte + 0] << 24) + (ptr[byte + 1] << 16) + (ptr[byte + 2] << 8) + (ptr[byte + 3]);
191 break;
192 default:
193 abort ();
196 *idx += size * 8;
197 return n;
201 getBITS (char *ptr, int *idx, int size, int max)
203 int byte = *idx / 8;
204 int bit = *idx % 8;
206 if (byte >= max)
207 return 0;
209 *idx += size;
211 return (ptr[byte] >> (8 - bit - size)) & ((1 << size) - 1);
214 static void
215 itheader (char *name, int code)
217 printf ("\n%s 0x%02x\n", name, code);
220 static int indent;
222 static void
223 p (void)
225 int i;
227 for (i = 0; i < indent; i++)
228 printf ("| ");
230 printf ("> ");
233 static void
234 tabout (void)
236 p ();
239 static void
240 pbarray (barray *y)
242 int x;
244 printf ("%d (", y->len);
246 for (x = 0; x < y->len; x++)
247 printf ("(%02x %c)", y->data[x],
248 ISPRINT (y->data[x]) ? y->data[x] : '.');
250 printf (")\n");
253 #define SYSROFF_PRINT
254 #define SYSROFF_SWAP_IN
256 #include "sysroff.c"
258 /* FIXME: sysinfo, which generates sysroff.[ch] from sysroff.info, can't
259 hack the special case of the tr block, which has no contents. So we
260 implement our own functions for reading in and printing out the tr
261 block. */
263 #define IT_tr_CODE 0x7f
265 void
266 sysroff_swap_tr_in (void)
268 char raw[255];
270 memset (raw, 0, 255);
271 fillup (raw);
274 void
275 sysroff_print_tr_out (void)
277 itheader ("tr", IT_tr_CODE);
280 static int
281 getone (int type)
283 int c = getc (file);
285 code = c;
287 if ((c & 0x7f) != type)
289 ungetc (c, file);
290 return 0;
293 switch (c & 0x7f)
295 case IT_cs_CODE:
297 struct IT_cs dummy;
298 sysroff_swap_cs_in (&dummy);
299 sysroff_print_cs_out (&dummy);
301 break;
303 case IT_dln_CODE:
305 struct IT_dln dummy;
306 sysroff_swap_dln_in (&dummy);
307 sysroff_print_dln_out (&dummy);
309 break;
311 case IT_hd_CODE:
313 struct IT_hd dummy;
314 sysroff_swap_hd_in (&dummy);
315 addrsize = dummy.afl;
316 sysroff_print_hd_out (&dummy);
318 break;
320 case IT_dar_CODE:
322 struct IT_dar dummy;
323 sysroff_swap_dar_in (&dummy);
324 sysroff_print_dar_out (&dummy);
326 break;
328 case IT_dsy_CODE:
330 struct IT_dsy dummy;
331 sysroff_swap_dsy_in (&dummy);
332 sysroff_print_dsy_out (&dummy);
334 break;
336 case IT_dfp_CODE:
338 struct IT_dfp dummy;
339 sysroff_swap_dfp_in (&dummy);
340 sysroff_print_dfp_out (&dummy);
342 break;
344 case IT_dso_CODE:
346 struct IT_dso dummy;
347 sysroff_swap_dso_in (&dummy);
348 sysroff_print_dso_out (&dummy);
350 break;
352 case IT_dpt_CODE:
354 struct IT_dpt dummy;
355 sysroff_swap_dpt_in (&dummy);
356 sysroff_print_dpt_out (&dummy);
358 break;
360 case IT_den_CODE:
362 struct IT_den dummy;
363 sysroff_swap_den_in (&dummy);
364 sysroff_print_den_out (&dummy);
366 break;
368 case IT_dbt_CODE:
370 struct IT_dbt dummy;
371 sysroff_swap_dbt_in (&dummy);
372 sysroff_print_dbt_out (&dummy);
374 break;
376 case IT_dty_CODE:
378 struct IT_dty dummy;
379 sysroff_swap_dty_in (&dummy);
380 sysroff_print_dty_out (&dummy);
382 break;
384 case IT_un_CODE:
386 struct IT_un dummy;
387 sysroff_swap_un_in (&dummy);
388 sysroff_print_un_out (&dummy);
390 break;
392 case IT_sc_CODE:
394 struct IT_sc dummy;
395 sysroff_swap_sc_in (&dummy);
396 sysroff_print_sc_out (&dummy);
398 break;
400 case IT_er_CODE:
402 struct IT_er dummy;
403 sysroff_swap_er_in (&dummy);
404 sysroff_print_er_out (&dummy);
406 break;
408 case IT_ed_CODE:
410 struct IT_ed dummy;
411 sysroff_swap_ed_in (&dummy);
412 sysroff_print_ed_out (&dummy);
414 break;
416 case IT_sh_CODE:
418 struct IT_sh dummy;
419 sysroff_swap_sh_in (&dummy);
420 sysroff_print_sh_out (&dummy);
422 break;
424 case IT_ob_CODE:
426 struct IT_ob dummy;
427 sysroff_swap_ob_in (&dummy);
428 sysroff_print_ob_out (&dummy);
430 break;
432 case IT_rl_CODE:
434 struct IT_rl dummy;
435 sysroff_swap_rl_in (&dummy);
436 sysroff_print_rl_out (&dummy);
438 break;
440 case IT_du_CODE:
442 struct IT_du dummy;
443 sysroff_swap_du_in (&dummy);
445 sysroff_print_du_out (&dummy);
447 break;
449 case IT_dus_CODE:
451 struct IT_dus dummy;
452 sysroff_swap_dus_in (&dummy);
453 sysroff_print_dus_out (&dummy);
455 break;
457 case IT_dul_CODE:
459 struct IT_dul dummy;
460 sysroff_swap_dul_in (&dummy);
461 sysroff_print_dul_out (&dummy);
463 break;
465 case IT_dss_CODE:
467 struct IT_dss dummy;
468 sysroff_swap_dss_in (&dummy);
469 sysroff_print_dss_out (&dummy);
471 break;
473 case IT_hs_CODE:
475 struct IT_hs dummy;
476 sysroff_swap_hs_in (&dummy);
477 sysroff_print_hs_out (&dummy);
479 break;
481 case IT_dps_CODE:
483 struct IT_dps dummy;
484 sysroff_swap_dps_in (&dummy);
485 sysroff_print_dps_out (&dummy);
487 break;
489 case IT_tr_CODE:
490 sysroff_swap_tr_in ();
491 sysroff_print_tr_out ();
492 break;
494 case IT_dds_CODE:
496 struct IT_dds dummy;
498 sysroff_swap_dds_in (&dummy);
499 sysroff_print_dds_out (&dummy);
501 break;
503 default:
504 printf ("GOT A %x\n", c);
505 return 0;
506 break;
509 return 1;
512 static int
513 opt (int x)
515 return getone (x);
518 static void
519 must (int x)
521 if (!getone (x))
522 printf ("WANTED %x!!\n", x);
525 static void
526 tab (int i, char *s)
528 indent += i;
530 if (s)
532 p ();
533 printf (s);
534 printf ("\n");
538 static void
539 dump_symbol_info (void)
541 tab (1, "SYMBOL INFO");
543 while (opt (IT_dsy_CODE))
545 if (opt (IT_dty_CODE))
547 must (IT_dbt_CODE);
548 derived_type ();
549 must (IT_dty_CODE);
553 tab (-1, "");
556 static void
557 derived_type (void)
559 tab (1, "DERIVED TYPE");
561 while (1)
563 if (opt (IT_dpp_CODE))
565 dump_symbol_info ();
566 must (IT_dpp_CODE);
568 else if (opt (IT_dfp_CODE))
570 dump_symbol_info ();
571 must (IT_dfp_CODE);
573 else if (opt (IT_den_CODE))
575 dump_symbol_info ();
576 must (IT_den_CODE);
578 else if (opt (IT_den_CODE))
580 dump_symbol_info ();
581 must (IT_den_CODE);
583 else if (opt (IT_dds_CODE))
585 dump_symbol_info ();
586 must (IT_dds_CODE);
588 else if (opt (IT_dar_CODE))
591 else if (opt (IT_dpt_CODE))
594 else if (opt (IT_dul_CODE))
597 else if (opt (IT_dse_CODE))
600 else if (opt (IT_dot_CODE))
603 else
604 break;
607 tab (-1, "");
610 static void
611 module (void)
613 int c = 0;
614 int l = 0;
616 tab (1, "MODULE***\n");
620 c = getc (file);
621 ungetc (c, file);
623 c &= 0x7f;
625 while (getone (c) && c != IT_tr_CODE);
627 tab (-1, "");
629 c = getc (file);
630 while (c != EOF)
632 printf ("%02x ", c);
633 l++;
634 if (l == 32)
636 printf ("\n");
637 l = 0;
639 c = getc (file);
643 char *program_name;
645 static void
646 show_usage (FILE *file, int status)
648 fprintf (file, _("Usage: %s [option(s)] in-file\n"), program_name);
649 fprintf (file, _("Print a human readable interpretation of a SYSROFF object file\n"));
650 fprintf (file, _(" The options are:\n\
651 -h --help Display this information\n\
652 -v --version Print the program's version number\n"));
654 if (status == 0)
655 fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
656 exit (status);
660 main (int ac, char **av)
662 char *input_file = NULL;
663 int opt;
664 static struct option long_options[] =
666 {"help", no_argument, 0, 'h'},
667 {"version", no_argument, 0, 'V'},
668 {NULL, no_argument, 0, 0}
671 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
672 setlocale (LC_MESSAGES, "");
673 #endif
674 #if defined (HAVE_SETLOCALE)
675 setlocale (LC_CTYPE, "");
676 #endif
677 bindtextdomain (PACKAGE, LOCALEDIR);
678 textdomain (PACKAGE);
680 program_name = av[0];
681 xmalloc_set_program_name (program_name);
683 while ((opt = getopt_long (ac, av, "HhVv", long_options, (int *) NULL)) != EOF)
685 switch (opt)
687 case 'H':
688 case 'h':
689 show_usage (stdout, 0);
690 /*NOTREACHED*/
691 case 'v':
692 case 'V':
693 print_version ("sysdump");
694 exit (0);
695 /*NOTREACHED*/
696 case 0:
697 break;
698 default:
699 show_usage (stderr, 1);
700 /*NOTREACHED*/
704 /* The input and output files may be named on the command line. */
706 if (optind < ac)
707 input_file = av[optind];
709 if (!input_file)
710 fatal (_("no input file specified"));
712 file = fopen (input_file, FOPEN_RB);
714 if (!file)
715 fatal (_("cannot open input file %s"), input_file);
717 module ();
718 return 0;