Xcv.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* $Header$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. *
  6. */
  7. /*
  8. * Convert Mantra object format to ACK object format
  9. */
  10. #include <stdio.h>
  11. #include <out.h>
  12. #define FMAGIC 0407
  13. #define REG 06
  14. #define RTEXT 00
  15. #define RDATA 01
  16. #define RBSS 02
  17. #define REXT 03
  18. #define RBYTE 00
  19. #define RWORD 01
  20. #define RLONG 02
  21. struct bhdr {
  22. long fmagic;
  23. long tsize;
  24. long dsize;
  25. long bsize;
  26. long ssize;
  27. long rtsize;
  28. long rdsize;
  29. long entry;
  30. };
  31. struct reloc {
  32. char x;
  33. #define setseg(p,s) (p->x = (p->x & ~03) | s)
  34. #define setsiz(p,s) (p->x = (p->x & ~014) | (s << 2))
  35. #define setdis(p,s) (p->x = (p->x & ~020) | (s << 4))
  36. #define getseg(p) (p->x&03)
  37. #define getsiz(p) ((p->x >> 2) & 03)
  38. #define getdis(p) ((p->x >> 4) & 01)
  39. char relpad2;
  40. short rsymbol;
  41. long rpos;
  42. };
  43. #define N_UNDF 0
  44. #define N_ABS 01
  45. #define N_TEXT 02
  46. #define N_DATA 03
  47. #define N_BSS 04
  48. #define N_TYPE 037
  49. #define N_REG 024
  50. #define N_FN 037
  51. #define N_EXT 040
  52. #define ASSERT(x) switch (2) { case 0: case (x): ; }
  53. /*
  54. * Header and section table of new format object file.
  55. */
  56. struct outhead outhead;
  57. struct outsect outsect[4];
  58. #define TEXTSG 0
  59. #define ROMSG 1
  60. #define DATASG 2
  61. #define BSSSG 3
  62. char *output_file;
  63. char *program ;
  64. char flag ;
  65. #define readf(a, b, c) fread((a), (b), (int)(c), input)
  66. /* Output file definitions and such */
  67. struct bhdr bh;
  68. #define TOT_HDRSIZE (sizeof(struct bhdr))
  69. FILE *input;
  70. struct outrelo *orelo = 0;
  71. struct outname *oname = 0;
  72. char *strings = 0;
  73. char *txt = 0;
  74. char *data = 0;
  75. main(argc, argv)
  76. int argc;
  77. char *argv[];
  78. {
  79. register struct outsect *p;
  80. char *malloc();
  81. input = stdin;
  82. program= argv[0] ;
  83. if ( argc>1 && argv[1][0]=='-' ) {
  84. flag=argv[1][1] ;
  85. argc-- ; argv++ ;
  86. }
  87. switch (argc) {
  88. case 3: if (! wr_open(argv[2]))
  89. fatal("Can't write %s.\n", argv[2]);
  90. output_file = argv[2];
  91. if ((input = fopen(argv[1], "r")) == (FILE *)0)
  92. fatal("Can't read %s.\n", argv[1]);
  93. break;
  94. default:fatal("Usage: %s <Mantra object> <ACK object>.\n", argv[0]);
  95. }
  96. if (! readf(&bh, sizeof(struct bhdr), 1)) rd_fatal();
  97. if (bh.fmagic != FMAGIC) fatal("bad magic number\n");
  98. outhead.oh_magic = O_MAGIC;
  99. outhead.oh_stamp = 0;
  100. outhead.oh_nsect = 4;
  101. outhead.oh_nrelo = (bh.rtsize + bh.rdsize) / sizeof(struct reloc);
  102. outhead.oh_flags = outhead.oh_nrelo ? HF_LINK : 0;
  103. outhead.oh_nemit = bh.tsize + bh.dsize;
  104. strings = malloc((unsigned) bh.ssize + 20);
  105. orelo = (struct outrelo *)
  106. malloc(outhead.oh_nrelo * sizeof(struct outrelo));
  107. oname = (struct outname *)
  108. malloc((unsigned) (bh.ssize / 6 + 3) * sizeof (struct outname));
  109. txt = malloc(bh.tsize);
  110. data = malloc(bh.dsize);
  111. if (!strings || !orelo || !oname || !txt || !data) {
  112. fatal("No memory\n");
  113. }
  114. p = &outsect[TEXTSG];
  115. p->os_base = 0; p->os_size = p->os_flen = bh.tsize;
  116. p->os_foff = OFF_EMIT(outhead); p->os_lign = 1;
  117. p = &outsect[ROMSG];
  118. p->os_base = p->os_size = p->os_flen = 0;
  119. p->os_foff = OFF_EMIT(outhead) + bh.tsize; p->os_lign = 1;
  120. p = &outsect[DATASG];
  121. p->os_base = 0; p->os_size = p->os_flen = bh.dsize;
  122. p->os_foff = OFF_EMIT(outhead) + bh.tsize; p->os_lign = 1;
  123. p = &outsect[BSSSG];
  124. p->os_base = p->os_flen = 0; p->os_size = bh.bsize;
  125. p->os_foff = OFF_EMIT(outhead)+bh.tsize+bh.dsize; p->os_lign = 1;
  126. if (bh.tsize && ! readf(txt, 1, bh.tsize)) rd_fatal();
  127. if (bh.dsize && ! readf(data, 1, bh.dsize)) rd_fatal();
  128. symtab(oname, strings);
  129. relo(orelo, txt, data);
  130. wr_ohead(&outhead);
  131. wr_sect(outsect, 4);
  132. wr_outsect(TEXTSG);
  133. wr_emit(txt, bh.tsize);
  134. wr_outsect(DATASG);
  135. wr_emit(data,bh.dsize);
  136. wr_relo(orelo, outhead.oh_nrelo);
  137. wr_name(oname, outhead.oh_nname);
  138. wr_string(strings, outhead.oh_nchar);
  139. wr_close();
  140. return 0;
  141. }
  142. symtab(name, string)
  143. struct outname *name;
  144. char *string;
  145. {
  146. register struct outname *oname = name;
  147. register char *strings = string;
  148. register int c;
  149. unsigned nnames = 3;
  150. register long ssize = bh.ssize;
  151. register char *b;
  152. oname->on_valu = 0; oname->on_foff = 0; oname->on_desc = 0;
  153. oname->on_type = (S_MIN+TEXTSG) | S_SCT;
  154. b = ".text"; while (*strings++ = *b) b++;
  155. oname++;
  156. oname->on_valu = 0; oname->on_foff = strings - string; oname->on_desc = 0;
  157. oname->on_type = (S_MIN+DATASG) | S_SCT;
  158. b = ".data"; while (*strings++ = *b) b++;
  159. oname++;
  160. oname->on_valu = 0; oname->on_foff = strings - string; oname->on_desc = 0;
  161. oname->on_type = (S_MIN+BSSSG) | S_SCT;
  162. b = ".bss"; while (*strings++ = *b) b++;
  163. oname++;
  164. while (ssize > 0) {
  165. c = getc(input);
  166. getc(input);
  167. oname->on_valu = getw(input);
  168. ssize -= 6;
  169. switch(c & ~N_EXT) {
  170. case N_ABS:
  171. oname->on_type = S_ABS;
  172. break;
  173. case N_TEXT:
  174. oname->on_type = S_MIN + TEXTSG;
  175. break;
  176. case N_DATA:
  177. oname->on_type = S_MIN + DATASG;
  178. oname->on_valu -= bh.tsize;
  179. break;
  180. case N_BSS:
  181. oname->on_type = S_MIN + BSSSG;
  182. oname->on_valu -= bh.tsize + bh.dsize;
  183. break;
  184. case N_UNDF:
  185. if (! oname->on_valu) {
  186. oname->on_type = S_UND;
  187. break;
  188. }
  189. oname->on_type = (S_MIN + BSSSG) | S_COM;
  190. break;
  191. case REG:
  192. case N_REG:
  193. case N_FN:
  194. while (ssize > 0 && (getc(input) > 0))
  195. ssize--;
  196. continue;
  197. default:
  198. fatal("Illegal type field %d in namelist\n", c);
  199. }
  200. if (c & N_EXT) oname->on_type |= S_EXT;
  201. oname->on_desc = 0;
  202. oname->on_foff = strings - string;
  203. nnames++;
  204. while (ssize > 0 && (c = getc(input)) > 0) {
  205. *strings++ = c;
  206. ssize--;
  207. }
  208. ssize--;
  209. *strings++ = c;
  210. oname++;
  211. }
  212. outhead.oh_nname = nnames;
  213. outhead.oh_nchar = strings - string;
  214. oname = name;
  215. ssize = OFF_CHAR(outhead);
  216. while (nnames--) {
  217. oname->on_foff += ssize;
  218. oname++;
  219. }
  220. }
  221. long
  222. get4(c)
  223. register char *c;
  224. {
  225. register long l = (long) (*c++ & 0377) << 24;
  226. l |= (long) (*c++ & 0377) << 16;
  227. l |= (long) (*c++ & 0377) << 8;
  228. l |= (long) (*c++ & 0377);
  229. return l;
  230. }
  231. put4(l, c)
  232. register char *c;
  233. register long l;
  234. {
  235. *c++ = (l >> 24);
  236. *c++ = (l >> 16);
  237. *c++ = (l >> 8);
  238. *c = l;
  239. }
  240. relo(orelo, txt, data)
  241. struct outrelo *orelo;
  242. char *txt, *data;
  243. {
  244. register struct outrelo *relo = orelo;
  245. register relocnt = outhead.oh_nrelo;
  246. struct reloc rel;
  247. register struct reloc *relp = &rel;
  248. int ndat = bh.rdsize / sizeof(struct reloc);
  249. while (relocnt-- > 0) {
  250. readf(relp, sizeof(struct reloc), 1);
  251. relo->or_sect = (relocnt >= ndat) ? S_MIN + TEXTSG :
  252. S_MIN + DATASG;
  253. relo->or_addr = relp->rpos;
  254. relo->or_nami = relp->rsymbol + 3;
  255. switch(getseg(relp)) {
  256. case RTEXT:
  257. relo->or_nami = 0;
  258. break;
  259. case RDATA:
  260. relo->or_nami = 1;
  261. break;
  262. case RBSS:
  263. relo->or_nami = 2;
  264. break;
  265. case REXT:
  266. break;
  267. default:
  268. fatal("Error 1 in relocation\n");
  269. }
  270. switch(getsiz(relp)) {
  271. case RBYTE:
  272. relo->or_type = RELO1|RELBR|RELWR;
  273. break;
  274. case RWORD:
  275. relo->or_type = RELO2|RELBR|RELWR;
  276. break;
  277. case RLONG: {
  278. register char *sct = (relocnt >= ndat ? txt : data) +
  279. relo->or_addr;
  280. long x;
  281. relo->or_type = RELO4|RELBR|RELWR;
  282. switch((oname + relo->or_nami)->on_type & S_TYP) {
  283. case (S_MIN+DATASG):
  284. x = get4(sct) - bh.tsize;
  285. put4(x, sct);
  286. break;
  287. case (S_MIN+BSSSG):
  288. if (! ((oname+relo->or_nami)->on_type & S_COM))
  289. x = get4(sct) - (bh.tsize + bh.dsize);
  290. put4(x, sct);
  291. }
  292. break;
  293. }
  294. default:
  295. fatal("Error 2 in relocation\n");
  296. }
  297. if (getdis(relp)) relo->or_type |= RELPC;
  298. relo++;
  299. }
  300. }
  301. /* VARARGS1 */
  302. fatal(s, a1, a2)
  303. char *s;
  304. {
  305. fprintf(stderr,"%s: ",program) ;
  306. fprintf(stderr, s, a1, a2);
  307. unlink(output_file);
  308. exit(-1);
  309. }
  310. wr_fatal() { fatal("Write error\n"); }
  311. rd_fatal() { fatal("Read error\n"); }