wr.c 7.6 KB

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