cv.c 11 KB

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