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