cv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. */
  6. /*
  7. * This program converts ack.out format to XENIX386 x.out format.
  8. * It uses ~em/modules/lib/libobject.a.
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. struct xexec {
  13. unsigned short x_magic;
  14. #define XMAGIC 01006
  15. unsigned short x_ext; /* 054 */
  16. long x_text;
  17. long x_data;
  18. long x_bss;
  19. long x_syms;
  20. long x_reloc; /* 0 */
  21. long x_entry; /* 0 */
  22. char x_cpu;
  23. #define XBSWAP 0x80
  24. #define XWSWAP 0x40
  25. #define X8086 0x04
  26. #define X286 0x09
  27. #define X386 0x0a
  28. char x_relsym; /* 0144, not used */
  29. unsigned short x_renv;
  30. #define XV5 0xc000
  31. #define XVERS 0xc000 /* version mask */
  32. #define XLOCK 0x1000
  33. #define XSEG 0x0800 /* segment table present */
  34. #define XPURE 0x0004 /* pure text */
  35. #define XSEP 0x0002 /* separate I & D */
  36. #define XEXEC 0x0001 /* executable */
  37. };
  38. struct xext {
  39. long xe_trsize, xe_drsize, xe_tbase, xe_dbase; /* 0 */
  40. long xe_stksize;
  41. long xe_segpos; /* 0140 ??? */
  42. long xe_segsize; /* 2 or 3 segments */
  43. /* one for name table, one for text+data
  44. or one for text + one for data(-i)
  45. */
  46. long xe_mdtpos, xe_mdtsize; /* 0 */
  47. char xe_mdttype; /* 0 */
  48. char xe_pagesize; /* 2 */
  49. char xe_ostype; /* 1 */
  50. char xe_osvers; /* 2 */
  51. unsigned short xe_eseg; /* 077 ??? */
  52. unsigned short xe_sres; /* 0 */
  53. };
  54. struct xseg {
  55. unsigned short xs_type;
  56. #define XTEXT 1
  57. #define XDATA 2
  58. #define XTSYMS 3
  59. unsigned short xs_attr;
  60. #define XAMEM 0x8000
  61. #define X_ABSS 0x0004
  62. #define X_APURE 0x0008
  63. #define X_A32B 0x0040
  64. unsigned short xs_seg; /* 077 for text, 0107 for data, 0 for sym */
  65. unsigned short xs_sres; /* 0 */
  66. long xs_filpos;
  67. long xs_psize;
  68. long xs_vsize;
  69. long xs_rbase;
  70. long xs_lres; /* 0 */
  71. long xs_lres2; /* 0 */
  72. };
  73. #include <out.h>
  74. #ifndef NORCSID
  75. static char rcs_id[] = "$Id$" ;
  76. #endif
  77. #define ENTRY 0x0L /* entry point */
  78. /*
  79. * Header and section table of new format object file.
  80. */
  81. struct outhead outhead;
  82. struct outsect outsect[S_MAX];
  83. struct xexec exec;
  84. struct xext ext;
  85. struct xseg seg[3];
  86. char *output_file;
  87. int outputfile_created;
  88. int output;
  89. char *program ;
  90. #define TEXTSG 0
  91. #define ROMSG 1
  92. #define DATASG 2
  93. #define BSSSG 3
  94. #define LSECT BSSSG+1
  95. #define NSECT LSECT+1
  96. long emit_symtab();
  97. int sep_id;
  98. long
  99. align(n, a)
  100. long n;
  101. {
  102. return ((n + a - 1) / a) * a;
  103. }
  104. main(argc, argv)
  105. int argc;
  106. char *argv[];
  107. {
  108. output = 1;
  109. program= argv[0] ;
  110. switch (argc) {
  111. case 1: rd_fdopen(0);
  112. break;
  113. case 3: if ((output = creat(argv[2], 0644)) < 0) {
  114. fatal("Can't write %s.\n", argv[2]);
  115. }
  116. output_file = argv[2];
  117. outputfile_created = 1;
  118. /* FALLTHROUGH */
  119. case 2:
  120. if (! rd_open(argv[1]))
  121. fatal("Can't read %s.\n", argv[1]);
  122. break;
  123. default:fatal("Usage: %s <ACK object> <Xenix object>.\n", argv[0]);
  124. }
  125. rd_ohead(&outhead);
  126. if (BADMAGIC(outhead))
  127. fatal("Not an ack object file.\n");
  128. if (outhead.oh_flags & HF_LINK)
  129. fatal("Contains unresolved references.\n");
  130. if (outhead.oh_nrelo > 0)
  131. fprintf(stderr, "Warning: relocation information present.\n");
  132. if ( outhead.oh_nsect!=LSECT && outhead.oh_nsect!=NSECT )
  133. fatal("Input file must have %d sections, not %ld\n",
  134. NSECT,outhead.oh_nsect) ;
  135. rd_sect(outsect, outhead.oh_nsect);
  136. while (outsect[TEXTSG].os_size % outsect[TEXTSG].os_lign)
  137. outsect[TEXTSG].os_size++;
  138. while (outsect[ROMSG].os_size % outsect[ROMSG].os_lign)
  139. outsect[ROMSG].os_size++;
  140. while (outsect[DATASG].os_size % outsect[DATASG].os_lign)
  141. outsect[DATASG].os_size++;
  142. /* A few checks */
  143. if ( outsect[TEXTSG].os_base != ENTRY)
  144. fatal("text must start at 0x%lx not at 0x%lx\n", ENTRY,
  145. outsect[TEXTSG].os_base) ;
  146. if ( outsect[BSSSG].os_flen != 0 )
  147. fatal("bss space contains initialized data\n") ;
  148. if ( outsect[BSSSG].os_base != outsect[DATASG].os_base+
  149. outsect[DATASG].os_size )
  150. fatal("bss segment must follow data segment\n") ;
  151. exec.x_magic = XMAGIC;
  152. exec.x_ext = 054;
  153. exec.x_text = outsect[TEXTSG].os_size;
  154. exec.x_data = outsect[ROMSG].os_size + outsect[DATASG].os_size;
  155. exec.x_bss = outsect[BSSSG].os_size;
  156. exec.x_entry = outsect[TEXTSG].os_base;
  157. exec.x_cpu = XWSWAP | X386;
  158. exec.x_relsym = 0144;
  159. exec.x_renv = XV5 | XSEG | XLOCK | XEXEC;
  160. if ( outsect[ROMSG].os_base == 0x1880000 ) {
  161. /* Separate I/D */
  162. sep_id = 1;
  163. exec.x_renv |= XPURE | XSEP;
  164. if ( outsect[DATASG].os_base != outsect[ROMSG].os_base+
  165. outsect[ROMSG].os_size )
  166. fatal("data segment must follow rom\n") ;
  167. } else {
  168. if ( outsect[ROMSG].os_base != outsect[TEXTSG].os_base+
  169. outsect[TEXTSG].os_size )
  170. fatal("rom segment must follow text\n") ;
  171. if ( outsect[DATASG].os_base != outsect[ROMSG].os_base+
  172. outsect[ROMSG].os_size )
  173. fatal("data segment must follow rom\n") ;
  174. }
  175. if ( outhead.oh_nsect==NSECT ) {
  176. if ( outsect[LSECT].os_base != outsect[BSSSG].os_base+
  177. outsect[BSSSG].os_size )
  178. fatal("end segment must follow bss\n") ;
  179. if ( outsect[LSECT].os_size != 0 )
  180. fatal("end segment must be empty\n") ;
  181. }
  182. ext.xe_stksize = 0;
  183. ext.xe_segpos = 0140;
  184. ext.xe_segsize = (2 + sep_id) * 040;
  185. ext.xe_pagesize = 2;
  186. ext.xe_ostype = 1;
  187. ext.xe_osvers = 2;
  188. ext.xe_eseg = 077;
  189. seg[0].xs_type = XDATA;
  190. seg[0].xs_attr = XAMEM | X_ABSS | X_A32B;
  191. seg[0].xs_seg = 0107;
  192. seg[0].xs_filpos = 02000;
  193. seg[1].xs_type = XTSYMS;
  194. seg[1].xs_attr = 1; /* ??? */
  195. seg[1].xs_filpos = 02000 + outsect[TEXTSG].os_size +
  196. outsect[DATASG].os_size + outsect[ROMSG].os_size;
  197. if (sep_id) {
  198. seg[2] = seg[1];
  199. seg[2].xs_filpos = 02000 + align(outsect[TEXTSG].os_size,02000) +
  200. outsect[DATASG].os_size + outsect[ROMSG].os_size;
  201. seg[1] = seg[0];
  202. seg[1].xs_rbase = 0x1880000;
  203. seg[0].xs_type = XTEXT;
  204. seg[0].xs_attr = XAMEM | X_APURE | X_A32B;
  205. seg[0].xs_seg = 077;
  206. seg[0].xs_psize = seg[0].xs_vsize = outsect[TEXTSG].os_size;
  207. seg[1].xs_filpos = seg[0].xs_filpos + align(seg[0].xs_psize,02000);
  208. seg[1].xs_psize = outsect[ROMSG].os_size + outsect[DATASG].os_size;
  209. seg[1].xs_vsize = seg[1].xs_psize + outsect[BSSSG].os_size;
  210. }
  211. else {
  212. seg[0].xs_psize = outsect[TEXTSG].os_size +
  213. outsect[ROMSG].os_size +
  214. outsect[DATASG].os_size;
  215. seg[0].xs_vsize = seg[0].xs_psize + outsect[BSSSG].os_size;
  216. }
  217. lseek(output, seg[1+sep_id].xs_filpos, 0);
  218. if (! (exec.x_syms = seg[1+sep_id].xs_psize = emit_symtab())) {
  219. /* not enough memory to produce symbol table, do without */
  220. fprintf(stderr, "%s: warning: no symbol table produced\n",
  221. program);
  222. ext.xe_segsize -= 040;
  223. seg[1+sep_id].xs_type = 0;
  224. }
  225. lseek(output, seg[0].xs_filpos, 0);
  226. emits(&outsect[TEXTSG]) ;
  227. if (sep_id) {
  228. lseek(output, seg[1].xs_filpos, 0);
  229. }
  230. emits(&outsect[ROMSG]) ;
  231. emits(&outsect[DATASG]) ;
  232. lseek(output, 0L, 0);
  233. header();
  234. if ( outputfile_created ) chmod(argv[2],0755);
  235. exit(0);
  236. }
  237. #define shortcvt(val, p) (*p++ = val, *p++ = val >> 8)
  238. #define longcvt(val, p) (*p++ = val, *p++ = val >> 8, *p++ = val >> 16, *p++ = val >> 24)
  239. char buf[0300];
  240. header()
  241. {
  242. register char *p = buf;
  243. register int i;
  244. shortcvt(exec.x_magic, p);
  245. shortcvt(exec.x_ext, p);
  246. longcvt(exec.x_text, p);
  247. longcvt(exec.x_data, p);
  248. longcvt(exec.x_bss, p);
  249. longcvt(exec.x_syms, p);
  250. longcvt(exec.x_reloc, p);
  251. longcvt(exec.x_entry, p);
  252. *p++ = exec.x_cpu;
  253. *p++ = exec.x_relsym;
  254. shortcvt(exec.x_renv, p);
  255. longcvt(ext.xe_trsize, p);
  256. longcvt(ext.xe_drsize, p);
  257. longcvt(ext.xe_tbase, p);
  258. longcvt(ext.xe_dbase, p);
  259. longcvt(ext.xe_stksize, p);
  260. longcvt(ext.xe_segpos, p);
  261. longcvt(ext.xe_segsize, p);
  262. longcvt(ext.xe_mdtpos, p);
  263. longcvt(ext.xe_mdtsize, p);
  264. *p++ = ext.xe_mdttype;
  265. *p++ = ext.xe_pagesize;
  266. *p++ = ext.xe_ostype;
  267. *p++ = ext.xe_osvers;
  268. shortcvt(ext.xe_eseg, p);
  269. shortcvt(ext.xe_sres, p);
  270. p = &buf[0140];
  271. for (i = 0; i <= 2 && seg[i].xs_type != 0; i++) {
  272. shortcvt(seg[i].xs_type, p);
  273. shortcvt(seg[i].xs_attr, p);
  274. shortcvt(seg[i].xs_seg, p);
  275. shortcvt(seg[i].xs_sres, p);
  276. longcvt(seg[i].xs_filpos, p);
  277. longcvt(seg[i].xs_psize, p);
  278. longcvt(seg[i].xs_vsize, p);
  279. longcvt(seg[i].xs_rbase, p);
  280. longcvt(seg[i].xs_lres, p);
  281. longcvt(seg[i].xs_lres2, p);
  282. }
  283. write(output, buf, 0140 + i * 040);
  284. }
  285. /*
  286. * Transfer the emitted byted from one file to another.
  287. */
  288. emits(section) struct outsect *section ; {
  289. register long n ;
  290. register int blk;
  291. char buffer[BUFSIZ];
  292. n= section->os_flen ;
  293. rd_outsect(section - outsect);
  294. while (n > 0) {
  295. blk = n > BUFSIZ ? BUFSIZ : n;
  296. rd_emit(buffer, (long) blk);
  297. write(output, buffer, blk);
  298. n -= blk;
  299. }
  300. if ((n = section->os_size - section->os_flen) > 0) {
  301. for (blk = BUFSIZ - 1; blk >= 0; blk--) {
  302. buffer[blk] = 0;
  303. }
  304. while (n > 0) {
  305. blk = n > BUFSIZ ? BUFSIZ : n;
  306. write(output, buffer, blk);
  307. n -= blk;
  308. }
  309. }
  310. }
  311. long
  312. emit_symtab()
  313. {
  314. register int i;
  315. struct xnm {
  316. unsigned short s_type, s_seg;
  317. long s_value;
  318. } xnm;
  319. char *chars, *xname;
  320. struct outname *names;
  321. register char *xptr;
  322. long off = OFF_CHAR(outhead);
  323. register char *p;
  324. register struct outname *np;
  325. chars = malloc((unsigned)(outhead.oh_nchar));
  326. if (! chars) return 0;
  327. np = names = (struct outname *)
  328. malloc(outhead.oh_nname * sizeof(struct outname));
  329. if (! np) {
  330. free(chars);
  331. return 0;
  332. }
  333. xptr = malloc((unsigned)(outhead.oh_nchar) + 9 * outhead.oh_nname);
  334. if (! xptr) {
  335. free(chars);
  336. free((char *) np);
  337. return 0;
  338. }
  339. xname = xptr;
  340. rd_name(np, outhead.oh_nname);
  341. rd_string(chars,outhead.oh_nchar);
  342. for (i = 0; i < outhead.oh_nname; i++, np++) {
  343. xnm.s_seg = 077;
  344. if (np->on_type & S_STB) {
  345. xnm.s_seg = np->on_desc;
  346. xnm.s_type = np->on_type;
  347. }
  348. else switch(np->on_type & S_ETC) {
  349. case S_FIL:
  350. case S_MOD:
  351. xnm.s_type = 0x1f;
  352. break;
  353. case S_SCT:
  354. xnm.s_type = 0x8;
  355. if ((np->on_type & S_TYP) != S_MIN+TEXTSG) {
  356. xnm.s_seg = 0107;
  357. }
  358. break;
  359. default:
  360. switch(np->on_type & S_TYP) {
  361. case S_UND:
  362. xnm.s_type = 0;
  363. break;
  364. case S_ABS:
  365. xnm.s_type = 1;
  366. xnm.s_seg = 0107;
  367. break;
  368. case S_MIN + TEXTSG:
  369. xnm.s_type = 2;
  370. break;
  371. case S_MIN + ROMSG:
  372. case S_MIN + DATASG:
  373. xnm.s_type = 3;
  374. xnm.s_seg = 0107;
  375. break;
  376. case S_MIN + BSSSG:
  377. case S_MIN + LSECT:
  378. xnm.s_type = 4;
  379. xnm.s_seg = 0107;
  380. break;
  381. default:
  382. fprintf(stderr,"warning: unknown s_type: %d\n",
  383. (int)(np->on_type) & S_TYP);
  384. }
  385. }
  386. if (np->on_type & S_EXT) xnm.s_type |= 0x20;
  387. xnm.s_value = np->on_valu;
  388. shortcvt(xnm.s_type, xptr);
  389. shortcvt(xnm.s_seg, xptr);
  390. longcvt(xnm.s_value, xptr);
  391. if (np->on_foff == 0) {
  392. *xptr++ = '\0';
  393. }
  394. else {
  395. long l = np->on_foff - off;
  396. if (l < 0 || l >= outhead.oh_nchar) {
  397. fatal("bad on_off: %ld\n",l);
  398. }
  399. p = &chars[l];
  400. do {
  401. *xptr++ = *p;
  402. } while (*p++);
  403. }
  404. }
  405. write(output, xname, xptr - xname);
  406. free(xname);
  407. free((char *) names);
  408. free(chars);
  409. return xptr - xname;
  410. }
  411. /* VARARGS1 */
  412. fatal(s, a1, a2)
  413. char *s;
  414. {
  415. fprintf(stderr,"%s: ",program) ;
  416. fprintf(stderr, s, a1, a2);
  417. if (outputfile_created)
  418. unlink(output_file);
  419. exit(1);
  420. }
  421. rd_fatal()
  422. {
  423. fatal("read error\n");
  424. }