cv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 ACK a.out file to SUN3 object format.
  9. */
  10. #include <stdio.h>
  11. #include <out.h>
  12. #include <assert.h>
  13. long lseek();
  14. #define OMAGIC 0407 /* old-fashioned */
  15. #define NMAGIC 0410 /* text write protexted */
  16. #define ZMAGIC 0413 /* demand paging */
  17. struct bhdr {
  18. short machtype;
  19. short magic;
  20. long tsize;
  21. long dsize;
  22. long bsize;
  23. long ssize;
  24. long entry;
  25. long rtsize;
  26. long rdsize;
  27. };
  28. struct machrelo {
  29. long address;
  30. long relodata;
  31. };
  32. #define setpcrel(X,f) (X |= (f<<7))
  33. #define setsymbolnum(X,n) (X = (X & 0377) | ((long)n << 8))
  34. #define setextern(X,f) (X |= (f << 4))
  35. #define setlength(X,l) (X = (X & ~0x60)|((long) l << 5))
  36. struct sym {
  37. long name;
  38. char type;
  39. char other;
  40. short desc;
  41. long value;
  42. };
  43. #define N_UNDF 0
  44. #define N_ABS 02
  45. #define N_TEXT 04
  46. #define N_DATA 06
  47. #define N_BSS 010
  48. #define N_EXT 01
  49. #define N_FN 0x1f
  50. /*
  51. * Header and section table of new format object file.
  52. */
  53. struct outhead outhead;
  54. struct outsect outsect[S_MAX];
  55. char *output_file;
  56. int outputfile_created;
  57. long magic;
  58. int rom_in_data;
  59. char *program ;
  60. char flag ;
  61. /* Output file definitions and such */
  62. struct bhdr bh;
  63. #define ENTRY 0x02000
  64. #define TOT_HDRSIZE (sizeof(struct bhdr))
  65. #define TEXTSG 0
  66. #define ROMSG 1
  67. #define DATASG 2
  68. #define BSSSG 3
  69. #define LSECT BSSSG+1
  70. #define NSECT LSECT+1
  71. int output;
  72. int unresolved;
  73. long textsize ;
  74. long datasize ;
  75. long bsssize;
  76. main(argc, argv)
  77. int argc;
  78. char *argv[];
  79. {
  80. register int nsect;
  81. program= argv[0] ;
  82. if ( argc>1 && argv[1][0]=='-' ) {
  83. flag=argv[1][1] ;
  84. argc-- ; argv++ ;
  85. }
  86. switch (argc) {
  87. case 3: if ((output = creat(argv[2], 0644)) < 0 ||
  88. (close(output), output = open(argv[2],2)) < 0)
  89. fatal("Can't write %s.\n", argv[2]);
  90. output_file = argv[2];
  91. outputfile_created = 1;
  92. if (! rd_open(argv[1]))
  93. fatal("Can't read %s.\n", argv[1]);
  94. break;
  95. default:fatal("Usage: %s <ACK object> <Sun object>.\n", argv[0]);
  96. }
  97. rd_ohead(&outhead);
  98. if (BADMAGIC(outhead))
  99. fatal("Not an ack object file.\n");
  100. if (outhead.oh_flags & HF_LINK) {
  101. unresolved++;
  102. fprintf(stderr,"Warning: contains unresolved references.\n");
  103. }
  104. else if (outhead.oh_nrelo > 0)
  105. fprintf(stderr, "Warning: relocation information present.\n");
  106. if ( outhead.oh_nsect!=LSECT && outhead.oh_nsect!=NSECT )
  107. fatal("Input file must have %d sections, not %ld\n",
  108. NSECT,outhead.oh_nsect) ;
  109. rd_sect(outsect, outhead.oh_nsect);
  110. /* A few checks */
  111. if ( outsect[BSSSG].os_flen != 0 )
  112. fatal("bss space contains initialized data\n") ;
  113. if ( !unresolved && outsect[BSSSG].os_base != outsect[DATASG].os_base+
  114. outsect[DATASG].os_size )
  115. fatal("bss segment must follow data segment\n") ;
  116. if ( outsect[ROMSG].os_lign == 0x20000 ) {
  117. /* 410/413 file with ROMSG in data space */
  118. rom_in_data = 1;
  119. magic= NMAGIC ;
  120. textsize= outsect[TEXTSG].os_size ;
  121. datasize= outsect[ROMSG].os_size + outsect[DATASG].os_size ;
  122. if ( outsect[DATASG].os_base != outsect[ROMSG].os_base+
  123. outsect[ROMSG].os_size )
  124. fatal("data segment must follow rom\n") ;
  125. } else
  126. if ( outsect[DATASG].os_lign == 0x20000 ) {
  127. /* 410/413 file with ROMSG in instruction space */
  128. rom_in_data = 0;
  129. magic= NMAGIC ;
  130. textsize= outsect[TEXTSG].os_size + outsect[ROMSG].os_size ;
  131. datasize= outsect[DATASG].os_size ;
  132. if ( outsect[ROMSG].os_base != outsect[TEXTSG].os_base+
  133. outsect[TEXTSG].os_size )
  134. fatal("rom segment must follow text\n") ;
  135. } else {
  136. /* Plain 407 file */
  137. rom_in_data = 0;
  138. magic= OMAGIC ;
  139. textsize= outsect[TEXTSG].os_size + outsect[ROMSG].os_size ;
  140. datasize= outsect[DATASG].os_size ;
  141. if (!unresolved) {
  142. if (outsect[ROMSG].os_base != outsect[TEXTSG].os_base+
  143. outsect[TEXTSG].os_size )
  144. fatal("rom segment must follow text\n") ;
  145. if ( outsect[DATASG].os_base != outsect[ROMSG].os_base+
  146. outsect[ROMSG].os_size )
  147. fatal("data segment must follow rom\n") ;
  148. }
  149. }
  150. if (outsect[TEXTSG].os_base == TOT_HDRSIZE+ENTRY) {
  151. if (magic != NMAGIC) {
  152. fatal("illegal alignments.\n");
  153. }
  154. magic = ZMAGIC;
  155. textsize = (textsize + TOT_HDRSIZE + (0x2000 - 1)) & ~(0x2000 - 1);
  156. datasize = (datasize + (0x2000 - 1)) & ~(0x2000 - 1);
  157. }
  158. bsssize = outsect[BSSSG].os_size;
  159. if ( outhead.oh_nsect==NSECT ) {
  160. if ( outsect[LSECT].os_base != outsect[BSSSG].os_base+
  161. outsect[BSSSG].os_size )
  162. fatal("end segment must follow bss\n") ;
  163. if ( outsect[LSECT].os_size != 0 )
  164. fatal("end segment must be empty\n") ;
  165. }
  166. if (magic != OMAGIC && unresolved) {
  167. fatal("unresolved references with wrong magic number\n");
  168. }
  169. if ((magic == ZMAGIC && outsect[TEXTSG].os_base != TOT_HDRSIZE+ENTRY) ||
  170. (magic != ZMAGIC && !unresolved && outsect[TEXTSG].os_base != ENTRY)) {
  171. fatal("Illegal entry point.\n");
  172. }
  173. bh.magic = magic;
  174. bh.machtype = MACH;
  175. bh.tsize = textsize;
  176. bh.bsize = bsssize;
  177. bh.dsize = datasize;
  178. bh.rtsize = 0;
  179. bh.rdsize = 0;
  180. if (magic == ZMAGIC) bh.entry = TOT_HDRSIZE+ENTRY;
  181. else if (!unresolved) bh.entry = ENTRY;
  182. else bh.entry = 0;
  183. /* Action at last */
  184. lseek(output,(long) TOT_HDRSIZE,0);
  185. emits(&outsect[TEXTSG]) ;
  186. if (rom_in_data && magic == ZMAGIC) {
  187. lseek(output,textsize,0);
  188. }
  189. emits(&outsect[ROMSG]) ;
  190. if (!rom_in_data && magic == ZMAGIC) {
  191. lseek(output,textsize,0);
  192. }
  193. emits(&outsect[DATASG]) ;
  194. if (magic == ZMAGIC) {
  195. lseek(output,textsize + datasize,0);
  196. }
  197. if (unresolved) emit_relo();
  198. emit_symtab();
  199. bh.ssize = outhead.oh_nname * sizeof(struct sym);
  200. lseek(output,0L,0);
  201. cvshort(&(bh.machtype));
  202. cvshort(&(bh.magic));
  203. cvlong(&(bh.tsize));
  204. cvlong(&(bh.dsize));
  205. cvlong(&(bh.bsize));
  206. cvlong(&(bh.ssize));
  207. cvlong(&(bh.entry));
  208. cvlong(&(bh.rtsize));
  209. cvlong(&(bh.rdsize));
  210. writef(&bh, 1, (long) TOT_HDRSIZE);
  211. if ( outputfile_created && !unresolved ) chmod(argv[2],0755);
  212. return 0;
  213. }
  214. writef(addr,sz,cnt)
  215. char *addr;
  216. long cnt;
  217. {
  218. cnt *= sz;
  219. while (cnt) {
  220. int i = cnt >= 0x4000 ? 0x4000 : cnt;
  221. cnt -= i;
  222. if (write(output, addr, i) < i) {
  223. fatal("write error\n");
  224. }
  225. addr += i;
  226. }
  227. }
  228. /*
  229. * Transfer the emitted byted from one file to another.
  230. */
  231. emits(section) struct outsect *section ; {
  232. char *p;
  233. char *calloc();
  234. long sz = section->os_flen;
  235. rd_outsect(section - outsect);
  236. while (sz) {
  237. unsigned int i = (sz >= 0x4000 ? 0x4000 : sz);
  238. if (!(p = calloc(i, 1))) {
  239. fatal("No memory.\n");
  240. }
  241. rd_emit(p, i);
  242. if (write(output, p, i) < i) {
  243. fatal("write error.\n");
  244. }
  245. free(p);
  246. sz -= i;
  247. }
  248. sz = section->os_size - section->os_flen;
  249. while (sz) {
  250. unsigned int i = (sz >= 0x4000 ? 0x4000 : sz);
  251. if (!(p = calloc(i, 1))) {
  252. fatal("No memory.\n");
  253. }
  254. if (write(output, p, i) < i) {
  255. fatal("write error.\n");
  256. }
  257. free(p);
  258. sz -= i;
  259. }
  260. }
  261. struct outname *ACKnames;
  262. emit_relo()
  263. {
  264. struct outrelo *ACKrelo;
  265. struct machrelo *MACHtrelo,*MACHdrelo;
  266. register struct outrelo *ap;
  267. register struct machrelo *mtp, *mdp;
  268. unsigned int cnt = outhead.oh_nrelo;
  269. ACKrelo = (struct outrelo *) calloc(cnt, sizeof(struct outrelo));
  270. MACHtrelo = (struct machrelo *) calloc(cnt, sizeof(struct machrelo));
  271. MACHdrelo = (struct machrelo *) calloc(cnt, sizeof(struct machrelo));
  272. ACKnames = (struct outname *) calloc(outhead.oh_nname, sizeof(struct outname));
  273. if (!(ap = ACKrelo) || !(mtp = MACHtrelo) || !(mdp = MACHdrelo) ||
  274. !ACKnames) {
  275. fatal("No memory.\n");
  276. }
  277. rd_relo(ACKrelo, cnt);
  278. rd_name(ACKnames, outhead.oh_nname);
  279. while (cnt-- != 0) {
  280. register struct machrelo *mp;
  281. if (ap->or_sect - S_MIN <= ROMSG) mp = mtp++;
  282. else mp = mdp++;
  283. setlength(mp->relodata,(ap->or_type&RELSZ) >> 1);
  284. setpcrel(mp->relodata,(ap->or_type&RELPC != 0));
  285. mp->address = ap->or_addr;
  286. if (ap->or_sect == ROMSG+S_MIN) {
  287. mp->address += outsect[TEXTSG].os_size;
  288. }
  289. if (ap->or_nami < outhead.oh_nname) {
  290. if (ACKnames[ap->or_nami].on_type & S_EXT) {
  291. setsymbolnum(mp->relodata, ap->or_nami);
  292. setextern(mp->relodata,1);
  293. }
  294. else {
  295. patch(ap, &ACKnames[ap->or_nami], mp);
  296. }
  297. }
  298. else {
  299. setsymbolnum(mp->relodata, N_ABS);
  300. }
  301. cvlong(&(mp->address));
  302. cvlong(&(mp->relodata));
  303. ap++;
  304. }
  305. bh.rtsize = (char *) mtp - (char *) MACHtrelo;
  306. bh.rdsize = (char *) mdp - (char *) MACHdrelo;
  307. writef(MACHtrelo, 1, bh.rtsize);
  308. writef(MACHdrelo, 1, bh.rdsize);
  309. free(ACKrelo);
  310. free(MACHtrelo);
  311. free(MACHdrelo);
  312. }
  313. long
  314. get(sz)
  315. {
  316. char buf[10];
  317. long l = 0;
  318. register char *p = buf;
  319. read(output,buf,sz);
  320. while (sz--) {
  321. l = (l << 8) | (*p++ & 0377);
  322. }
  323. }
  324. put(l,sz)
  325. long l;
  326. {
  327. char buf[10];
  328. register char *p;
  329. *p++ = l >> 24;
  330. *p++ = l >> 16;
  331. *p++ = l >> 8;
  332. *p++ = l;
  333. p -= sz;
  334. if (write(output, p, sz) < sz) {
  335. fatal("write error.\n");
  336. }
  337. }
  338. patch(ap, an, mp)
  339. register struct outrelo *ap;
  340. register struct outname *an;
  341. register struct machrelo *mp;
  342. {
  343. int whichsect = (an->on_type & S_TYP) - S_MIN;
  344. long correction = 0;
  345. long where = TOT_HDRSIZE+ap->or_addr;
  346. long X;
  347. long here;
  348. int sz;
  349. if (!(an->on_type & S_SCT)) {
  350. fprintf(stderr,"funny on_type %x\n", an->on_type);
  351. }
  352. switch(whichsect) {
  353. case TEXTSG:
  354. setsymbolnum(mp->relodata,N_TEXT);
  355. return;
  356. case DATASG:
  357. correction = outsect[ROMSG].os_size + outsect[TEXTSG].os_size;
  358. setsymbolnum(mp->relodata,N_DATA);
  359. break;
  360. case ROMSG:
  361. correction += outsect[TEXTSG].os_size;
  362. setsymbolnum(mp->relodata,N_TEXT);
  363. break;
  364. default:
  365. assert(0);
  366. }
  367. switch(ap->or_sect - S_MIN) {
  368. case DATASG:
  369. where += outsect[ROMSG].os_size;
  370. case ROMSG:
  371. where += outsect[TEXTSG].os_size;
  372. case TEXTSG:
  373. break;
  374. default:
  375. assert(0);
  376. }
  377. here = lseek(output, 0L, 1);
  378. lseek(output, where, 0);
  379. sz = ap->or_type & RELSZ;
  380. X = get(sz) + correction;
  381. lseek(output, where, 0);
  382. put(X,sz);
  383. lseek(output, here, 0);
  384. }
  385. cvlong(l)
  386. long *l;
  387. {
  388. long x = *l;
  389. char *p = (char *) l;
  390. *p++ = x >> 24;
  391. *p++ = x >> 16;
  392. *p++ = x >> 8;
  393. *p = x;
  394. }
  395. cvshort(s)
  396. short *s;
  397. {
  398. short x = *s;
  399. char *p = (char *) s;
  400. *p++ = x >> 8;
  401. *p = x;
  402. }
  403. emit_symtab()
  404. {
  405. register unsigned short i = outhead.oh_nname;
  406. register struct outname *A;
  407. struct sym *MACHnames;
  408. register struct sym *M;
  409. extern char *malloc(), *calloc();
  410. char *chars;
  411. long offX = OFF_CHAR(outhead) - 4;
  412. if (!(A = ACKnames)) {
  413. if (!(A = (struct outname *)
  414. calloc(i, sizeof(struct outname)))) {
  415. fatal("No memory.\n");
  416. }
  417. rd_name(A, outhead.oh_nname);
  418. }
  419. if (!(M = (struct sym *) calloc(i, sizeof(struct sym)))) {
  420. fatal("No memory.\n");
  421. }
  422. MACHnames = M;
  423. ACKnames = A;
  424. for (; i; i--, A++, M++) {
  425. M->value = A->on_valu;
  426. switch(A->on_type & S_TYP) {
  427. case S_UND:
  428. switch(A->on_type & S_ETC) {
  429. default:
  430. M->type = N_UNDF;
  431. break;
  432. case S_MOD:
  433. case S_FIL:
  434. M->type = N_FN;
  435. break;
  436. case S_LIN:
  437. M->type = N_ABS;
  438. break;
  439. }
  440. break;
  441. case S_ABS:
  442. M->type = N_ABS;
  443. break;
  444. case S_MIN + TEXTSG:
  445. if (! A->on_type & S_COM) {
  446. M->value += outsect[TEXTSG].os_base;
  447. }
  448. M->type = N_TEXT;
  449. break;
  450. case S_MIN + ROMSG:
  451. M->type = (rom_in_data ? N_DATA : N_TEXT);
  452. if (! A->on_type & S_COM) {
  453. M->value += outsect[ROMSG].os_base;
  454. }
  455. break;
  456. case S_MIN + DATASG:
  457. M->type = N_DATA;
  458. if (! A->on_type & S_COM) {
  459. M->value += outsect[DATASG].os_base;
  460. }
  461. break;
  462. case S_MIN + BSSSG:
  463. M->type = N_BSS;
  464. if (! A->on_type & S_COM) {
  465. M->value += outsect[BSSSG].os_base;
  466. }
  467. break;
  468. case S_MIN + LSECT:
  469. M->type = N_BSS;
  470. if (! A->on_type & S_COM) {
  471. M->value += outsect[LSECT].os_base;
  472. }
  473. break;
  474. default:
  475. fprintf(stderr,"warning: unknown s_type: %d\n",
  476. A->on_type & S_TYP);
  477. }
  478. if (A->on_type & S_EXT) M->type |= N_EXT;
  479. if (M->name = A->on_foff) {
  480. M->name -= offX;
  481. }
  482. else M->name = outhead.oh_nchar + 3; /* pointer to nullbyte */
  483. cvlong(&(M->name));
  484. cvlong(&(M->value));
  485. }
  486. writef(MACHnames, sizeof(struct sym), (long) outhead.oh_nname);
  487. free(MACHnames);
  488. free(ACKnames);
  489. if ((unsigned) outhead.oh_nchar != outhead.oh_nchar ||
  490. !( chars = malloc((unsigned) outhead.oh_nchar))) {
  491. fatal("No memory\n.");
  492. }
  493. put(outhead.oh_nchar+4,4);
  494. rd_string(chars,outhead.oh_nchar);
  495. writef(chars, 1, outhead.oh_nchar);
  496. free(chars);
  497. }
  498. /* VARARGS1 */
  499. fatal(s, a1, a2)
  500. char *s;
  501. {
  502. fprintf(stderr,"%s: ",program) ;
  503. fprintf(stderr, s, a1, a2);
  504. if (outputfile_created)
  505. unlink(output_file);
  506. exit(-1);
  507. }
  508. rd_fatal() { fatal("read error.\n"); }