sysdump.c 12 KB

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