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