wr.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /* $Id$ */
  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. * You can choose between two strategies:
  8. * - Open the output file several times, once for each logical part, and
  9. * write to it in multiple places.
  10. * - Open the output file once and seek back and forth to each logical
  11. * part. In this case #define OUTSEEK.
  12. */
  13. #include "obj.h"
  14. /*
  15. * Parts of the output file.
  16. */
  17. static long offset[MAXSECT];
  18. struct fil __parts[NPARTS];
  19. #ifdef OUTSEEK
  20. static int outfile;
  21. static long currpos;
  22. #endif
  23. int __sectionnr;
  24. #define sectionnr __sectionnr
  25. static int offcnt;
  26. void __wr_flush(struct fil *ptr)
  27. {
  28. #ifdef OUTSEEK
  29. /* seek to correct position even if we aren't going to write now */
  30. if (currpos != ptr->currpos) {
  31. currpos = lseek(ptr->fd, ptr->currpos, 0);
  32. }
  33. #endif
  34. if (ptr->pnow > ptr->pbegin) {
  35. wr_bytes(ptr->fd, ptr->pbegin, (long)(ptr->pnow - ptr->pbegin));
  36. ptr->currpos += ptr->pnow - ptr->pbegin;
  37. #ifdef OUTSEEK
  38. currpos = ptr->currpos;
  39. #endif
  40. if (ptr < &__parts[PARTEMIT+SECTCNT]) {
  41. offset[sectionnr] = ptr->currpos;
  42. }
  43. }
  44. ptr->cnt = WBUFSIZ;
  45. ptr->pnow = ptr->pbuf;
  46. ptr->pbegin = ptr->pbuf;
  47. }
  48. static void
  49. OUTWRITE(p, b, n)
  50. int p; /* part number */
  51. register char *b; /* buffer pointer */
  52. long n; /* write count */
  53. {
  54. register struct fil *ptr = &__parts[p];
  55. register char *pn = ptr->pnow;
  56. register int i;
  57. long m;
  58. i = ptr->cnt;
  59. if (i < WBUFSIZ) {
  60. if (n > i) {
  61. __wr_flush(ptr);
  62. wr_bytes(ptr->fd, b, (long) i);
  63. n -= i;
  64. b += i;
  65. ptr->currpos += i;
  66. #ifdef OUTSEEK
  67. currpos = ptr->currpos;
  68. #endif
  69. if (ptr < &__parts[PARTEMIT+SECTCNT]) {
  70. offset[sectionnr] = ptr->currpos;
  71. }
  72. }
  73. else {
  74. i = n;
  75. ptr->cnt -= i;
  76. n = 0;
  77. while (i > 0) {
  78. *pn++ = *b++;
  79. i--;
  80. }
  81. ptr->pnow = pn;
  82. }
  83. }
  84. if (ptr->cnt == 0 || ptr->cnt == WBUFSIZ) {
  85. __wr_flush(ptr);
  86. m = n & ~(WBUFSIZ - 1);
  87. if (m != 0) {
  88. wr_bytes(ptr->fd, b, m);
  89. b += m;
  90. n &= WBUFSIZ - 1;
  91. ptr->currpos += m;
  92. #ifdef OUTSEEK
  93. currpos = ptr->currpos;
  94. #endif
  95. if (ptr < &__parts[PARTEMIT+SECTCNT]) {
  96. offset[sectionnr] = ptr->currpos;
  97. }
  98. }
  99. i = n;
  100. if (i != 0) {
  101. n = 0;
  102. pn = ptr->pnow;
  103. ptr->cnt -= i;
  104. while (i > 0) {
  105. *pn++ = *b++;
  106. i--;
  107. }
  108. ptr->pnow = pn;
  109. }
  110. }
  111. }
  112. static void
  113. BEGINSEEK(p, o)
  114. int p; /* part number */
  115. long o; /* offset in file */
  116. {
  117. register struct fil *ptr = &__parts[p];
  118. #ifdef OUTSEEK
  119. ptr->fd = outfile;
  120. ptr->currpos = o;
  121. #else
  122. ptr->currpos = lseek(ptr->fd, o, 0);
  123. #endif
  124. if (p >= PARTRELO) o = 0; /* no attempt to align writes
  125. for the time being */
  126. ptr->cnt = WBUFSIZ - ((int)o & (WBUFSIZ-1));
  127. ptr->pbegin = ptr->pbuf + (WBUFSIZ - ptr->cnt);
  128. ptr->pnow = ptr->pbegin;
  129. }
  130. /*
  131. * Open the output file according to the chosen strategy.
  132. */
  133. int
  134. wr_open(f)
  135. char *f;
  136. {
  137. register struct fil *fdp;
  138. close(creat(f, 0666));
  139. #ifdef OUTSEEK
  140. if ((outfile = open(f, 1)) < 0)
  141. return 0;
  142. currpos = 0;
  143. #else /* not OUTSEEK */
  144. for (fdp = &__parts[PARTEMIT]; fdp < &__parts[NPARTS]; fdp++)
  145. if ((fdp->fd = open(f, 1)) < 0)
  146. return 0;
  147. #endif /* not OUTSEEK */
  148. offcnt = 0;
  149. return 1;
  150. }
  151. void
  152. wr_close()
  153. {
  154. register struct fil *ptr;
  155. for (ptr = &__parts[PARTEMIT]; ptr < &__parts[NPARTS]; ptr++) {
  156. __wr_flush(ptr);
  157. #ifndef OUTSEEK
  158. close(ptr->fd);
  159. #endif /* not OUTSEEK */
  160. ptr->fd = -1;
  161. }
  162. #ifdef OUTSEEK
  163. close(outfile);
  164. outfile = -1;
  165. #endif /* OUTSEEK */
  166. }
  167. void
  168. wr_ohead(head)
  169. register struct outhead *head;
  170. {
  171. {
  172. register long off = OFF_RELO(*head);
  173. BEGINSEEK(PARTEMIT, 0L);
  174. BEGINSEEK(PARTRELO, off);
  175. off += (long) head->oh_nrelo * SZ_RELO;
  176. BEGINSEEK(PARTNAME, off);
  177. off += (long) head->oh_nname * SZ_NAME;
  178. BEGINSEEK(PARTCHAR, off);
  179. #ifdef SYMDBUG
  180. off += head->oh_nchar;
  181. BEGINSEEK(PARTDBUG, off);
  182. #endif
  183. }
  184. if (BYTE_ORDER != 0x0123 || sizeof(struct outhead) != SZ_HEAD)
  185. {
  186. char buf[SZ_HEAD];
  187. register char *c = &buf[0];
  188. put2(head->oh_magic, c); c += 2;
  189. put2(head->oh_stamp, c); c += 2;
  190. put2(head->oh_flags, c); c += 2;
  191. put2(head->oh_nsect, c); c += 2;
  192. put2(head->oh_nrelo, c); c += 2;
  193. put2(head->oh_nname, c); c += 2;
  194. put4(head->oh_nemit, c); c += 4;
  195. put4(head->oh_nchar, c);
  196. OUTWRITE(PARTEMIT, buf, (long)SZ_HEAD);
  197. }
  198. else OUTWRITE(PARTEMIT, (char *)head, (long)SZ_HEAD);
  199. }
  200. void
  201. wr_sect(sect, cnt)
  202. register struct outsect *sect;
  203. register unsigned int cnt;
  204. {
  205. { register unsigned int i = cnt;
  206. while (i--) {
  207. if (offcnt >= 1 && offcnt < SECTCNT) {
  208. BEGINSEEK(PARTEMIT+offcnt, sect->os_foff);
  209. }
  210. offset[offcnt++] = sect->os_foff;
  211. sect++;
  212. }
  213. sect -= cnt;
  214. }
  215. #if BYTE_ORDER == 0x0123
  216. if (sizeof(struct outsect) != SZ_SECT)
  217. #endif
  218. while (cnt)
  219. {
  220. register char *c;
  221. register unsigned int i;
  222. i = __parts[PARTEMIT].cnt/SZ_SECT;
  223. c = __parts[PARTEMIT].pnow;
  224. if (i > cnt) i = cnt;
  225. cnt -= i;
  226. __parts[PARTEMIT].cnt -= (i*SZ_SECT);
  227. while (i--) {
  228. put4(sect->os_base, c); c += 4;
  229. put4(sect->os_size, c); c += 4;
  230. put4(sect->os_foff, c); c += 4;
  231. put4(sect->os_flen, c); c += 4;
  232. put4(sect->os_lign, c); c += 4;
  233. sect++;
  234. }
  235. __parts[PARTEMIT].pnow = c;
  236. if (cnt) {
  237. __wr_flush(&__parts[PARTEMIT]);
  238. }
  239. }
  240. #if BYTE_ORDER == 0x0123
  241. else {
  242. OUTWRITE(PARTEMIT, (char *) sect, (long) cnt * SZ_SECT);
  243. }
  244. #endif
  245. }
  246. void
  247. wr_outsect(s)
  248. int s; /* section number */
  249. {
  250. register struct fil *ptr = &__parts[PARTEMIT + getsect(sectionnr)];
  251. if (s != sectionnr && s >= (SECTCNT-1) && sectionnr >= (SECTCNT-1)) {
  252. #ifdef OUTSEEK
  253. if (currpos != ptr->currpos)
  254. currpos = lseek(ptr->fd, ptr->currpos, 0);
  255. #endif
  256. wr_bytes(ptr->fd, ptr->pbegin, (long)(ptr->pnow - ptr->pbegin));
  257. ptr->currpos += ptr->pnow - ptr->pbegin;
  258. #ifdef OUTSEEK
  259. currpos = ptr->currpos;
  260. #endif
  261. offset[sectionnr] = ptr->currpos;
  262. if (offset[s] != ptr->currpos) {
  263. ptr->currpos = lseek(ptr->fd, offset[s], 0);
  264. #ifdef OUTSEEK
  265. currpos = ptr->currpos;
  266. #endif
  267. }
  268. ptr->cnt = WBUFSIZ - ((int)offset[s] & (WBUFSIZ-1));
  269. ptr->pbegin = ptr->pbuf + (WBUFSIZ - ptr->cnt);
  270. ptr->pnow = ptr->pbegin;
  271. }
  272. sectionnr = s;
  273. }
  274. /*
  275. * We don't have to worry about byte order here.
  276. */
  277. void
  278. wr_emit(emit, cnt)
  279. char *emit;
  280. long cnt;
  281. {
  282. OUTWRITE(PARTEMIT + getsect(sectionnr) , emit, cnt);
  283. }
  284. void
  285. wr_relo(relo, cnt)
  286. register struct outrelo *relo;
  287. unsigned int cnt;
  288. {
  289. #if BYTE_ORDER == 0x0123
  290. if (sizeof(struct outrelo) != SZ_RELO)
  291. #endif
  292. while (cnt)
  293. {
  294. register char *c;
  295. register unsigned int i;
  296. i = __parts[PARTRELO].cnt/SZ_RELO;
  297. c = __parts[PARTRELO].pnow;
  298. if (i > cnt) i = cnt;
  299. cnt -= i;
  300. __parts[PARTRELO].cnt -= (i*SZ_RELO);
  301. while (i--) {
  302. *c++ = relo->or_type;
  303. *c++ = relo->or_sect;
  304. put2(relo->or_nami, c); c += 2;
  305. put4(relo->or_addr, c); c += 4;
  306. relo++;
  307. }
  308. __parts[PARTRELO].pnow = c;
  309. if (cnt) {
  310. __wr_flush(&__parts[PARTRELO]);
  311. }
  312. }
  313. #if BYTE_ORDER == 0x0123
  314. else {
  315. OUTWRITE(PARTRELO, (char *) relo, (long) cnt * SZ_RELO);
  316. }
  317. #endif
  318. }
  319. void
  320. wr_name(name, cnt)
  321. register struct outname *name;
  322. unsigned int cnt;
  323. {
  324. #if BYTE_ORDER == 0x0123
  325. if (sizeof(struct outname) != SZ_NAME)
  326. #endif
  327. while (cnt)
  328. {
  329. register char *c;
  330. register unsigned int i;
  331. i = __parts[PARTNAME].cnt/SZ_NAME;
  332. c = __parts[PARTNAME].pnow;
  333. if (i > cnt) i = cnt;
  334. cnt -= i;
  335. __parts[PARTNAME].cnt -= (i*SZ_NAME);
  336. while (i--) {
  337. put4(name->on_foff, c); c += 4;
  338. put2(name->on_type, c); c += 2;
  339. put2(name->on_desc, c); c += 2;
  340. put4(name->on_valu, c); c += 4;
  341. name++;
  342. }
  343. __parts[PARTNAME].pnow = c;
  344. if (cnt) __wr_flush(&__parts[PARTNAME]);
  345. }
  346. #if BYTE_ORDER == 0x0123
  347. else {
  348. OUTWRITE(PARTNAME, (char *) name, (long)cnt * SZ_NAME);
  349. }
  350. #endif
  351. }
  352. void
  353. wr_string(addr, len)
  354. char *addr;
  355. long len;
  356. {
  357. OUTWRITE(PARTCHAR, addr, len);
  358. }
  359. #ifdef SYMDBUG
  360. void
  361. wr_dbug(buf, size)
  362. char *buf;
  363. long size;
  364. {
  365. OUTWRITE(PARTDBUG, buf, size);
  366. }
  367. #endif /* SYMDBUG */