sysdump.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /* Sysroff object format dumper.
  2. Copyright (C) 1994-2022 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. /* Written by Steve Chamberlain <sac@cygnus.com>.
  17. This program reads a SYSROFF object file and prints it in an
  18. almost human readable form to stdout. */
  19. #include "sysdep.h"
  20. #include "bfd.h"
  21. #include "safe-ctype.h"
  22. #include "libiberty.h"
  23. #include "getopt.h"
  24. #include "bucomm.h"
  25. #include "sysroff.h"
  26. static int dump = 1;
  27. static int segmented_p;
  28. static int code;
  29. static int addrsize = 4;
  30. static FILE *file;
  31. static void derived_type (void);
  32. static char *
  33. getCHARS (unsigned char *ptr, int *idx, int size, int max)
  34. {
  35. int oc = *idx / 8;
  36. char *r;
  37. int b = size;
  38. if (b >= max)
  39. return _("*undefined*");
  40. if (b == 0)
  41. {
  42. /* PR 17512: file: 13caced2. */
  43. if (oc >= max)
  44. return _("*corrupt*");
  45. /* Got to work out the length of the string from self. */
  46. b = ptr[oc++];
  47. (*idx) += 8;
  48. }
  49. if (oc + b > size)
  50. {
  51. /* PR 28564 */
  52. return _("*corrupt*");
  53. }
  54. *idx += b * 8;
  55. r = xcalloc (b + 1, 1);
  56. memcpy (r, ptr + oc, b);
  57. r[b] = 0;
  58. return r;
  59. }
  60. static void
  61. dh (unsigned char *ptr, int size)
  62. {
  63. int i;
  64. int j;
  65. int span = 16;
  66. printf ("\n************************************************************\n");
  67. for (i = 0; i < size; i += span)
  68. {
  69. for (j = 0; j < span; j++)
  70. {
  71. if (j + i < size)
  72. printf ("%02x ", ptr[i + j]);
  73. else
  74. printf (" ");
  75. }
  76. for (j = 0; j < span && j + i < size; j++)
  77. {
  78. int c = ptr[i + j];
  79. if (c < 32 || c > 127)
  80. c = '.';
  81. printf ("%c", c);
  82. }
  83. printf ("\n");
  84. }
  85. }
  86. static int
  87. fillup (unsigned char *ptr)
  88. {
  89. int size;
  90. int sum;
  91. int i;
  92. size = getc (file);
  93. if (size == EOF
  94. || size <= 2)
  95. return 0;
  96. size -= 2;
  97. if (fread (ptr, size, 1, file) != 1)
  98. return 0;
  99. sum = code + size + 2;
  100. for (i = 0; i < size; i++)
  101. sum += ptr[i];
  102. if ((sum & 0xff) != 0xff)
  103. printf (_("SUM IS %x\n"), sum);
  104. if (dump)
  105. dh (ptr, size);
  106. return size;
  107. }
  108. static barray
  109. getBARRAY (unsigned char *ptr, int *idx, int dsize ATTRIBUTE_UNUSED, int max)
  110. {
  111. barray res;
  112. int i;
  113. int byte = *idx / 8;
  114. int size = 0;
  115. if (byte < max)
  116. size = ptr[byte++];
  117. res.len = size;
  118. res.data = (unsigned char *) xmalloc (size);
  119. for (i = 0; i < size; i++)
  120. res.data[i] = byte < max ? ptr[byte++] : 0;
  121. return res;
  122. }
  123. static int
  124. getINT (unsigned char *ptr, int *idx, int size, int max)
  125. {
  126. int n = 0;
  127. int byte = *idx / 8;
  128. if (byte >= max)
  129. {
  130. /* PR 17512: file: id:000001,src:000002,op:flip1,pos:45. */
  131. /* Prevent infinite loops re-reading beyond the end of the buffer. */
  132. fatal (_("ICE: getINT: Out of buffer space"));
  133. return 0;
  134. }
  135. if (size == -2)
  136. size = addrsize;
  137. if (size == -1)
  138. size = 0;
  139. switch (size)
  140. {
  141. case 0:
  142. return 0;
  143. case 1:
  144. n = (ptr[byte]);
  145. break;
  146. case 2:
  147. n = (ptr[byte + 0] << 8) + ptr[byte + 1];
  148. break;
  149. case 4:
  150. n = (((unsigned) ptr[byte + 0] << 24) + (ptr[byte + 1] << 16)
  151. + (ptr[byte + 2] << 8) + (ptr[byte + 3]));
  152. break;
  153. default:
  154. fatal (_("Unsupported read size: %d"), size);
  155. }
  156. *idx += size * 8;
  157. return n;
  158. }
  159. static int
  160. getBITS (unsigned char *ptr, int *idx, int size, int max)
  161. {
  162. int byte = *idx / 8;
  163. int bit = *idx % 8;
  164. if (byte >= max)
  165. return 0;
  166. *idx += size;
  167. return (ptr[byte] >> (8 - bit - size)) & ((1 << size) - 1);
  168. }
  169. static void
  170. itheader (char *name, int icode)
  171. {
  172. printf ("\n%s 0x%02x\n", name, icode);
  173. }
  174. static int indent;
  175. static void
  176. p (void)
  177. {
  178. int i;
  179. for (i = 0; i < indent; i++)
  180. printf ("| ");
  181. printf ("> ");
  182. }
  183. static void
  184. tabout (void)
  185. {
  186. p ();
  187. }
  188. static void
  189. pbarray (barray *y)
  190. {
  191. int x;
  192. printf ("%d (", y->len);
  193. for (x = 0; x < y->len; x++)
  194. printf ("(%02x %c)", y->data[x],
  195. ISPRINT (y->data[x]) ? y->data[x] : '.');
  196. printf (")\n");
  197. }
  198. #define SYSROFF_PRINT
  199. #define SYSROFF_SWAP_IN
  200. #include "sysroff.c"
  201. /* FIXME: sysinfo, which generates sysroff.[ch] from sysroff.info, can't
  202. hack the special case of the tr block, which has no contents. So we
  203. implement our own functions for reading in and printing out the tr
  204. block. */
  205. #define IT_tr_CODE 0x7f
  206. static void
  207. sysroff_swap_tr_in (void)
  208. {
  209. unsigned char raw[255];
  210. memset (raw, 0, 255);
  211. fillup (raw);
  212. }
  213. static void
  214. sysroff_print_tr_out (void)
  215. {
  216. itheader ("tr", IT_tr_CODE);
  217. }
  218. static int
  219. getone (int type)
  220. {
  221. int c = getc (file);
  222. code = c;
  223. if ((c & 0x7f) != type)
  224. {
  225. ungetc (c, file);
  226. return 0;
  227. }
  228. switch (c & 0x7f)
  229. {
  230. case IT_cs_CODE:
  231. {
  232. struct IT_cs dummy;
  233. sysroff_swap_cs_in (&dummy);
  234. sysroff_print_cs_out (&dummy);
  235. }
  236. break;
  237. case IT_dln_CODE:
  238. {
  239. struct IT_dln dummy;
  240. sysroff_swap_dln_in (&dummy);
  241. sysroff_print_dln_out (&dummy);
  242. }
  243. break;
  244. case IT_hd_CODE:
  245. {
  246. struct IT_hd dummy;
  247. sysroff_swap_hd_in (&dummy);
  248. addrsize = dummy.afl;
  249. sysroff_print_hd_out (&dummy);
  250. }
  251. break;
  252. case IT_dar_CODE:
  253. {
  254. struct IT_dar dummy;
  255. sysroff_swap_dar_in (&dummy);
  256. sysroff_print_dar_out (&dummy);
  257. }
  258. break;
  259. case IT_dsy_CODE:
  260. {
  261. struct IT_dsy dummy;
  262. sysroff_swap_dsy_in (&dummy);
  263. sysroff_print_dsy_out (&dummy);
  264. }
  265. break;
  266. case IT_dfp_CODE:
  267. {
  268. struct IT_dfp dummy;
  269. sysroff_swap_dfp_in (&dummy);
  270. sysroff_print_dfp_out (&dummy);
  271. }
  272. break;
  273. case IT_dso_CODE:
  274. {
  275. struct IT_dso dummy;
  276. sysroff_swap_dso_in (&dummy);
  277. sysroff_print_dso_out (&dummy);
  278. }
  279. break;
  280. case IT_dpt_CODE:
  281. {
  282. struct IT_dpt dummy;
  283. sysroff_swap_dpt_in (&dummy);
  284. sysroff_print_dpt_out (&dummy);
  285. }
  286. break;
  287. case IT_den_CODE:
  288. {
  289. struct IT_den dummy;
  290. sysroff_swap_den_in (&dummy);
  291. sysroff_print_den_out (&dummy);
  292. }
  293. break;
  294. case IT_dbt_CODE:
  295. {
  296. struct IT_dbt dummy;
  297. sysroff_swap_dbt_in (&dummy);
  298. sysroff_print_dbt_out (&dummy);
  299. }
  300. break;
  301. case IT_dty_CODE:
  302. {
  303. struct IT_dty dummy;
  304. sysroff_swap_dty_in (&dummy);
  305. sysroff_print_dty_out (&dummy);
  306. }
  307. break;
  308. case IT_un_CODE:
  309. {
  310. struct IT_un dummy;
  311. sysroff_swap_un_in (&dummy);
  312. sysroff_print_un_out (&dummy);
  313. }
  314. break;
  315. case IT_sc_CODE:
  316. {
  317. struct IT_sc dummy;
  318. sysroff_swap_sc_in (&dummy);
  319. sysroff_print_sc_out (&dummy);
  320. }
  321. break;
  322. case IT_er_CODE:
  323. {
  324. struct IT_er dummy;
  325. sysroff_swap_er_in (&dummy);
  326. sysroff_print_er_out (&dummy);
  327. }
  328. break;
  329. case IT_ed_CODE:
  330. {
  331. struct IT_ed dummy;
  332. sysroff_swap_ed_in (&dummy);
  333. sysroff_print_ed_out (&dummy);
  334. }
  335. break;
  336. case IT_sh_CODE:
  337. {
  338. struct IT_sh dummy;
  339. sysroff_swap_sh_in (&dummy);
  340. sysroff_print_sh_out (&dummy);
  341. }
  342. break;
  343. case IT_ob_CODE:
  344. {
  345. struct IT_ob dummy;
  346. sysroff_swap_ob_in (&dummy);
  347. sysroff_print_ob_out (&dummy);
  348. }
  349. break;
  350. case IT_rl_CODE:
  351. {
  352. struct IT_rl dummy;
  353. sysroff_swap_rl_in (&dummy);
  354. sysroff_print_rl_out (&dummy);
  355. }
  356. break;
  357. case IT_du_CODE:
  358. {
  359. struct IT_du dummy;
  360. sysroff_swap_du_in (&dummy);
  361. sysroff_print_du_out (&dummy);
  362. }
  363. break;
  364. case IT_dus_CODE:
  365. {
  366. struct IT_dus dummy;
  367. sysroff_swap_dus_in (&dummy);
  368. sysroff_print_dus_out (&dummy);
  369. }
  370. break;
  371. case IT_dul_CODE:
  372. {
  373. struct IT_dul dummy;
  374. sysroff_swap_dul_in (&dummy);
  375. sysroff_print_dul_out (&dummy);
  376. }
  377. break;
  378. case IT_dss_CODE:
  379. {
  380. struct IT_dss dummy;
  381. sysroff_swap_dss_in (&dummy);
  382. sysroff_print_dss_out (&dummy);
  383. }
  384. break;
  385. case IT_hs_CODE:
  386. {
  387. struct IT_hs dummy;
  388. sysroff_swap_hs_in (&dummy);
  389. sysroff_print_hs_out (&dummy);
  390. }
  391. break;
  392. case IT_dps_CODE:
  393. {
  394. struct IT_dps dummy;
  395. sysroff_swap_dps_in (&dummy);
  396. sysroff_print_dps_out (&dummy);
  397. }
  398. break;
  399. case IT_tr_CODE:
  400. sysroff_swap_tr_in ();
  401. sysroff_print_tr_out ();
  402. break;
  403. case IT_dds_CODE:
  404. {
  405. struct IT_dds dummy;
  406. sysroff_swap_dds_in (&dummy);
  407. sysroff_print_dds_out (&dummy);
  408. }
  409. break;
  410. default:
  411. printf (_("GOT A %x\n"), c);
  412. return 0;
  413. break;
  414. }
  415. return 1;
  416. }
  417. static int
  418. opt (int x)
  419. {
  420. return getone (x);
  421. }
  422. static void
  423. must (int x)
  424. {
  425. if (!getone (x))
  426. printf (_("WANTED %x!!\n"), x);
  427. }
  428. static void
  429. tab (int i, char *s)
  430. {
  431. indent += i;
  432. if (s)
  433. {
  434. p ();
  435. puts (s);
  436. }
  437. }
  438. static void
  439. dump_symbol_info (void)
  440. {
  441. tab (1, _("SYMBOL INFO"));
  442. while (opt (IT_dsy_CODE))
  443. {
  444. if (opt (IT_dty_CODE))
  445. {
  446. must (IT_dbt_CODE);
  447. derived_type ();
  448. must (IT_dty_CODE);
  449. }
  450. }
  451. tab (-1, "");
  452. }
  453. static void
  454. derived_type (void)
  455. {
  456. tab (1, _("DERIVED TYPE"));
  457. while (1)
  458. {
  459. if (opt (IT_dpp_CODE))
  460. {
  461. dump_symbol_info ();
  462. must (IT_dpp_CODE);
  463. }
  464. else if (opt (IT_dfp_CODE))
  465. {
  466. dump_symbol_info ();
  467. must (IT_dfp_CODE);
  468. }
  469. else if (opt (IT_den_CODE))
  470. {
  471. dump_symbol_info ();
  472. must (IT_den_CODE);
  473. }
  474. else if (opt (IT_den_CODE))
  475. {
  476. dump_symbol_info ();
  477. must (IT_den_CODE);
  478. }
  479. else if (opt (IT_dds_CODE))
  480. {
  481. dump_symbol_info ();
  482. must (IT_dds_CODE);
  483. }
  484. else if (opt (IT_dar_CODE))
  485. {
  486. }
  487. else if (opt (IT_dpt_CODE))
  488. {
  489. }
  490. else if (opt (IT_dul_CODE))
  491. {
  492. }
  493. else if (opt (IT_dse_CODE))
  494. {
  495. }
  496. else if (opt (IT_dot_CODE))
  497. {
  498. }
  499. else
  500. break;
  501. }
  502. tab (-1, "");
  503. }
  504. static void
  505. module (void)
  506. {
  507. int c = 0;
  508. int l = 0;
  509. tab (1, _("MODULE***\n"));
  510. do
  511. {
  512. c = getc (file);
  513. if (c == EOF)
  514. break;
  515. ungetc (c, file);
  516. c &= 0x7f;
  517. }
  518. while (getone (c) && c != IT_tr_CODE);
  519. tab (-1, "");
  520. c = getc (file);
  521. while (c != EOF)
  522. {
  523. printf ("%02x ", c);
  524. l++;
  525. if (l == 32)
  526. {
  527. printf ("\n");
  528. l = 0;
  529. }
  530. c = getc (file);
  531. }
  532. }
  533. ATTRIBUTE_NORETURN static void
  534. show_usage (FILE *ffile, int status)
  535. {
  536. fprintf (ffile, _("Usage: %s [option(s)] in-file\n"), program_name);
  537. fprintf (ffile, _("Print a human readable interpretation of a SYSROFF object file\n"));
  538. fprintf (ffile, _(" The options are:\n\
  539. -h --help Display this information\n\
  540. -v --version Print the program's version number\n"));
  541. if (REPORT_BUGS_TO[0] && status == 0)
  542. fprintf (ffile, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  543. exit (status);
  544. }
  545. int
  546. main (int ac, char **av)
  547. {
  548. char *input_file = NULL;
  549. int option;
  550. static struct option long_options[] =
  551. {
  552. {"help", no_argument, 0, 'h'},
  553. {"version", no_argument, 0, 'V'},
  554. {NULL, no_argument, 0, 0}
  555. };
  556. #ifdef HAVE_LC_MESSAGES
  557. setlocale (LC_MESSAGES, "");
  558. #endif
  559. setlocale (LC_CTYPE, "");
  560. bindtextdomain (PACKAGE, LOCALEDIR);
  561. textdomain (PACKAGE);
  562. program_name = av[0];
  563. xmalloc_set_program_name (program_name);
  564. bfd_set_error_program_name (program_name);
  565. expandargv (&ac, &av);
  566. while ((option = getopt_long (ac, av, "HhVv", long_options, (int *) NULL)) != EOF)
  567. {
  568. switch (option)
  569. {
  570. case 'H':
  571. case 'h':
  572. show_usage (stdout, 0);
  573. /*NOTREACHED*/
  574. case 'v':
  575. case 'V':
  576. print_version ("sysdump");
  577. exit (0);
  578. /*NOTREACHED*/
  579. case 0:
  580. break;
  581. default:
  582. show_usage (stderr, 1);
  583. /*NOTREACHED*/
  584. }
  585. }
  586. /* The input and output files may be named on the command line. */
  587. if (optind < ac)
  588. input_file = av[optind];
  589. if (!input_file)
  590. fatal (_("no input file specified"));
  591. file = fopen (input_file, FOPEN_RB);
  592. if (!file)
  593. fatal (_("cannot open input file %s"), input_file);
  594. module ();
  595. return 0;
  596. }