wr.c 7.7 KB

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