wr.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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
  27. __wr_flush(ptr)
  28. register struct fil *ptr;
  29. {
  30. #ifdef OUTSEEK
  31. /* seek to correct position even if we aren't going to write now */
  32. if (currpos != ptr->currpos) {
  33. currpos = lseek(ptr->fd, ptr->currpos, 0);
  34. }
  35. #endif
  36. if (ptr->pnow > ptr->pbegin) {
  37. wr_bytes(ptr->fd, ptr->pbegin, (long)(ptr->pnow - ptr->pbegin));
  38. ptr->currpos += ptr->pnow - ptr->pbegin;
  39. #ifdef OUTSEEK
  40. currpos = ptr->currpos;
  41. #endif
  42. if (ptr < &__parts[PARTEMIT+SECTCNT]) {
  43. offset[sectionnr] = ptr->currpos;
  44. }
  45. }
  46. ptr->cnt = WBUFSIZ;
  47. ptr->pnow = ptr->pbuf;
  48. ptr->pbegin = ptr->pbuf;
  49. }
  50. static void
  51. 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 void
  115. BEGINSEEK(p, o)
  116. int p; /* part number */
  117. long o; /* offset in file */
  118. {
  119. register struct fil *ptr = &__parts[p];
  120. #ifdef OUTSEEK
  121. ptr->fd = outfile;
  122. ptr->currpos = o;
  123. #else
  124. ptr->currpos = lseek(ptr->fd, o, 0);
  125. #endif
  126. if (p >= PARTRELO) o = 0; /* no attempt to align writes
  127. for the time being */
  128. ptr->cnt = WBUFSIZ - ((int)o & (WBUFSIZ-1));
  129. ptr->pbegin = ptr->pbuf + (WBUFSIZ - ptr->cnt);
  130. ptr->pnow = ptr->pbegin;
  131. }
  132. /*
  133. * Open the output file according to the chosen strategy.
  134. */
  135. int
  136. wr_open(f)
  137. char *f;
  138. {
  139. register struct fil *fdp;
  140. close(creat(f, 0666));
  141. #ifdef OUTSEEK
  142. if ((outfile = open(f, 1)) < 0)
  143. return 0;
  144. currpos = 0;
  145. #else /* not OUTSEEK */
  146. for (fdp = &__parts[PARTEMIT]; fdp < &__parts[NPARTS]; fdp++)
  147. if ((fdp->fd = open(f, 1)) < 0)
  148. return 0;
  149. #endif /* not OUTSEEK */
  150. offcnt = 0;
  151. return 1;
  152. }
  153. void
  154. wr_close()
  155. {
  156. register struct fil *ptr;
  157. for (ptr = &__parts[PARTEMIT]; ptr < &__parts[NPARTS]; ptr++) {
  158. __wr_flush(ptr);
  159. #ifndef OUTSEEK
  160. close(ptr->fd);
  161. #endif /* not OUTSEEK */
  162. ptr->fd = -1;
  163. }
  164. #ifdef OUTSEEK
  165. close(outfile);
  166. outfile = -1;
  167. #endif /* OUTSEEK */
  168. }
  169. void
  170. wr_ohead(head)
  171. register struct outhead *head;
  172. {
  173. {
  174. register long off = OFF_RELO(*head);
  175. BEGINSEEK(PARTEMIT, 0L);
  176. BEGINSEEK(PARTRELO, off);
  177. off += (long) head->oh_nrelo * SZ_RELO;
  178. BEGINSEEK(PARTNAME, off);
  179. off += (long) head->oh_nname * SZ_NAME;
  180. BEGINSEEK(PARTCHAR, off);
  181. #ifdef SYMDBUG
  182. off += head->oh_nchar;
  183. BEGINSEEK(PARTDBUG, off);
  184. #endif
  185. }
  186. if (BYTE_ORDER != 0x0123 || sizeof(struct outhead) != SZ_HEAD)
  187. {
  188. char buf[SZ_HEAD];
  189. register char *c = &buf[0];
  190. put2(head->oh_magic, c); c += 2;
  191. put2(head->oh_stamp, c); c += 2;
  192. put2(head->oh_flags, c); c += 2;
  193. put2(head->oh_nsect, c); c += 2;
  194. put2(head->oh_nrelo, c); c += 2;
  195. put2(head->oh_nname, c); c += 2;
  196. put4(head->oh_nemit, c); c += 4;
  197. put4(head->oh_nchar, c);
  198. OUTWRITE(PARTEMIT, buf, (long)SZ_HEAD);
  199. }
  200. else OUTWRITE(PARTEMIT, (char *)head, (long)SZ_HEAD);
  201. }
  202. void
  203. wr_sect(sect, cnt)
  204. register struct outsect *sect;
  205. register unsigned int cnt;
  206. {
  207. { register unsigned int i = cnt;
  208. while (i--) {
  209. if (offcnt >= 1 && offcnt < SECTCNT) {
  210. BEGINSEEK(PARTEMIT+offcnt, sect->os_foff);
  211. }
  212. offset[offcnt++] = sect->os_foff;
  213. sect++;
  214. }
  215. sect -= cnt;
  216. }
  217. #if BYTE_ORDER == 0x0123
  218. if (sizeof(struct outsect) != SZ_SECT)
  219. #endif
  220. while (cnt)
  221. {
  222. register char *c;
  223. register unsigned int i;
  224. i = __parts[PARTEMIT].cnt/SZ_SECT;
  225. c = __parts[PARTEMIT].pnow;
  226. if (i > cnt) i = cnt;
  227. cnt -= i;
  228. __parts[PARTEMIT].cnt -= (i*SZ_SECT);
  229. while (i--) {
  230. put4(sect->os_base, c); c += 4;
  231. put4(sect->os_size, c); c += 4;
  232. put4(sect->os_foff, c); c += 4;
  233. put4(sect->os_flen, c); c += 4;
  234. put4(sect->os_lign, c); c += 4;
  235. sect++;
  236. }
  237. __parts[PARTEMIT].pnow = c;
  238. if (cnt) {
  239. __wr_flush(&__parts[PARTEMIT]);
  240. }
  241. }
  242. #if BYTE_ORDER == 0x0123
  243. else {
  244. OUTWRITE(PARTEMIT, (char *) sect, (long) cnt * SZ_SECT);
  245. }
  246. #endif
  247. }
  248. void
  249. wr_outsect(s)
  250. int s; /* section number */
  251. {
  252. register struct fil *ptr = &__parts[PARTEMIT + getsect(sectionnr)];
  253. if (s != sectionnr && s >= (SECTCNT-1) && sectionnr >= (SECTCNT-1)) {
  254. #ifdef OUTSEEK
  255. if (currpos != ptr->currpos)
  256. currpos = lseek(ptr->fd, ptr->currpos, 0);
  257. #endif
  258. wr_bytes(ptr->fd, ptr->pbegin, (long)(ptr->pnow - ptr->pbegin));
  259. ptr->currpos += ptr->pnow - ptr->pbegin;
  260. #ifdef OUTSEEK
  261. currpos = ptr->currpos;
  262. #endif
  263. offset[sectionnr] = ptr->currpos;
  264. if (offset[s] != ptr->currpos) {
  265. ptr->currpos = lseek(ptr->fd, offset[s], 0);
  266. #ifdef OUTSEEK
  267. currpos = ptr->currpos;
  268. #endif
  269. }
  270. ptr->cnt = WBUFSIZ - ((int)offset[s] & (WBUFSIZ-1));
  271. ptr->pbegin = ptr->pbuf + (WBUFSIZ - ptr->cnt);
  272. ptr->pnow = ptr->pbegin;
  273. }
  274. sectionnr = s;
  275. }
  276. /*
  277. * We don't have to worry about byte order here.
  278. */
  279. void
  280. wr_emit(emit, cnt)
  281. char *emit;
  282. long cnt;
  283. {
  284. OUTWRITE(PARTEMIT + getsect(sectionnr) , emit, cnt);
  285. }
  286. void
  287. wr_relo(relo, cnt)
  288. register struct outrelo *relo;
  289. unsigned int cnt;
  290. {
  291. #if BYTE_ORDER == 0x0123
  292. if (sizeof(struct outrelo) != SZ_RELO)
  293. #endif
  294. while (cnt)
  295. {
  296. register char *c;
  297. register unsigned int i;
  298. i = __parts[PARTRELO].cnt/SZ_RELO;
  299. c = __parts[PARTRELO].pnow;
  300. if (i > cnt) i = cnt;
  301. cnt -= i;
  302. __parts[PARTRELO].cnt -= (i*SZ_RELO);
  303. while (i--) {
  304. *c++ = relo->or_type;
  305. *c++ = relo->or_sect;
  306. put2(relo->or_nami, c); c += 2;
  307. put4(relo->or_addr, c); c += 4;
  308. relo++;
  309. }
  310. __parts[PARTRELO].pnow = c;
  311. if (cnt) {
  312. __wr_flush(&__parts[PARTRELO]);
  313. }
  314. }
  315. #if BYTE_ORDER == 0x0123
  316. else {
  317. OUTWRITE(PARTRELO, (char *) relo, (long) cnt * SZ_RELO);
  318. }
  319. #endif
  320. }
  321. void
  322. wr_name(name, cnt)
  323. register struct outname *name;
  324. unsigned int cnt;
  325. {
  326. #if BYTE_ORDER == 0x0123
  327. if (sizeof(struct outname) != SZ_NAME)
  328. #endif
  329. while (cnt)
  330. {
  331. register char *c;
  332. register unsigned int i;
  333. i = __parts[PARTNAME].cnt/SZ_NAME;
  334. c = __parts[PARTNAME].pnow;
  335. if (i > cnt) i = cnt;
  336. cnt -= i;
  337. __parts[PARTNAME].cnt -= (i*SZ_NAME);
  338. while (i--) {
  339. put4(name->on_foff, c); c += 4;
  340. put2(name->on_type, c); c += 2;
  341. put2(name->on_desc, c); c += 2;
  342. put4(name->on_valu, c); c += 4;
  343. name++;
  344. }
  345. __parts[PARTNAME].pnow = c;
  346. if (cnt) __wr_flush(&__parts[PARTNAME]);
  347. }
  348. #if BYTE_ORDER == 0x0123
  349. else {
  350. OUTWRITE(PARTNAME, (char *) name, (long)cnt * SZ_NAME);
  351. }
  352. #endif
  353. }
  354. void
  355. wr_string(addr, len)
  356. char *addr;
  357. long len;
  358. {
  359. OUTWRITE(PARTCHAR, addr, len);
  360. }
  361. #ifdef SYMDBUG
  362. void
  363. wr_dbug(buf, size)
  364. char *buf;
  365. long size;
  366. {
  367. OUTWRITE(PARTDBUG, buf, size);
  368. }
  369. #endif /* SYMDBUG */