rd.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #include "obj.h"
  7. /*
  8. * Parts of the output file.
  9. */
  10. #undef PARTEMIT
  11. #undef PARTRELO
  12. #undef PARTNAME
  13. #undef PARTCHAR
  14. #undef PARTDBUG
  15. #undef NPARTS
  16. #define PARTEMIT 0
  17. #define PARTRELO 1
  18. #define PARTNAME 2
  19. #define PARTCHAR 3
  20. #ifdef SYMDBUG
  21. #define PARTDBUG 4
  22. #else
  23. #define PARTDBUG 3
  24. #endif
  25. #define NPARTS (PARTDBUG + 1)
  26. static long offset[MAXSECT];
  27. static int outfile;
  28. static long outseek[NPARTS];
  29. static long currpos;
  30. static long rd_base;
  31. #define OUTSECT(i) \
  32. (outseek[PARTEMIT] = offset[i])
  33. #define BEGINSEEK(p, o) \
  34. (outseek[(p)] = (o))
  35. static int sectionnr;
  36. static void
  37. OUTREAD(p, b, n)
  38. char *b;
  39. long n;
  40. {
  41. register long l = outseek[p];
  42. if (currpos != l) {
  43. lseek(outfile, l, 0);
  44. }
  45. rd_bytes(outfile, b, n);
  46. l += n;
  47. currpos = l;
  48. outseek[p] = l;
  49. }
  50. /*
  51. * Open the output file according to the chosen strategy.
  52. */
  53. int
  54. rd_open(f)
  55. char *f;
  56. {
  57. if ((outfile = open(f, 0)) < 0)
  58. return 0;
  59. return rd_fdopen(outfile);
  60. }
  61. static int offcnt;
  62. int
  63. rd_fdopen(fd)
  64. {
  65. register int i;
  66. for (i = 0; i < NPARTS; i++) outseek[i] = 0;
  67. offcnt = 0;
  68. rd_base = lseek(fd, 0L, 1);
  69. if (rd_base < 0) {
  70. return 0;
  71. }
  72. currpos = rd_base;
  73. outseek[PARTEMIT] = currpos;
  74. outfile = fd;
  75. sectionnr = 0;
  76. return 1;
  77. }
  78. void
  79. rd_close()
  80. {
  81. close(outfile);
  82. outfile = -1;
  83. }
  84. int
  85. rd_fd()
  86. {
  87. return outfile;
  88. }
  89. void
  90. rd_ohead(head)
  91. register struct outhead *head;
  92. {
  93. register long off;
  94. OUTREAD(PARTEMIT, (char *) head, (long) SZ_HEAD);
  95. #if BYTE_ORDER == 0x0123
  96. if (sizeof(struct outhead) != SZ_HEAD)
  97. #endif
  98. {
  99. register char *c = (char *) head + (SZ_HEAD-4);
  100. head->oh_nchar = get4(c);
  101. c -= 4; head->oh_nemit = get4(c);
  102. c -= 2; head->oh_nname = uget2(c);
  103. c -= 2; head->oh_nrelo = uget2(c);
  104. c -= 2; head->oh_nsect = uget2(c);
  105. c -= 2; head->oh_flags = uget2(c);
  106. c -= 2; head->oh_stamp = uget2(c);
  107. c -= 2; head->oh_magic = uget2(c);
  108. }
  109. off = OFF_RELO(*head) + rd_base;
  110. BEGINSEEK(PARTRELO, off);
  111. off += (long) head->oh_nrelo * SZ_RELO;
  112. BEGINSEEK(PARTNAME, off);
  113. off += (long) head->oh_nname * SZ_NAME;
  114. BEGINSEEK(PARTCHAR, off);
  115. #ifdef SYMDBUG
  116. off += head->oh_nchar;
  117. BEGINSEEK(PARTDBUG, off);
  118. #endif
  119. }
  120. void
  121. rd_rew_relos(head)
  122. register struct outhead *head;
  123. {
  124. register long off = OFF_RELO(*head) + rd_base;
  125. BEGINSEEK(PARTRELO, off);
  126. }
  127. void
  128. rd_sect(sect, cnt)
  129. register struct outsect *sect;
  130. register unsigned int cnt;
  131. {
  132. register char *c = (char *) sect + cnt * SZ_SECT;
  133. OUTREAD(PARTEMIT, (char *) sect, (long)cnt * SZ_SECT);
  134. sect += cnt;
  135. offcnt += cnt;
  136. while (cnt--) {
  137. sect--;
  138. #if BYTE_ORDER == 0x0123
  139. if (sizeof(struct outsect) != SZ_SECT)
  140. #endif
  141. {
  142. c -= 4; sect->os_lign = get4(c);
  143. c -= 4; sect->os_flen = get4(c);
  144. c -= 4; sect->os_foff = get4(c);
  145. c -= 4; sect->os_size = get4(c);
  146. c -= 4; sect->os_base = get4(c);
  147. }
  148. offset[--offcnt] = sect->os_foff + rd_base;
  149. }
  150. }
  151. void
  152. rd_outsect(s)
  153. {
  154. OUTSECT(s);
  155. sectionnr = s;
  156. }
  157. /*
  158. * We don't have to worry about byte order here.
  159. */
  160. void
  161. rd_emit(emit, cnt)
  162. char *emit;
  163. long cnt;
  164. {
  165. OUTREAD(PARTEMIT, emit, cnt);
  166. offset[sectionnr] += cnt;
  167. }
  168. void
  169. rd_relo(relo, cnt)
  170. register struct outrelo *relo;
  171. register unsigned int cnt;
  172. {
  173. OUTREAD(PARTRELO, (char *) relo, (long) cnt * SZ_RELO);
  174. #if BYTE_ORDER == 0x0123
  175. if (sizeof(struct outrelo) != SZ_RELO)
  176. #endif
  177. {
  178. register char *c = (char *) relo + (long) cnt * SZ_RELO;
  179. relo += cnt;
  180. while (cnt--) {
  181. relo--;
  182. c -= 4; relo->or_addr = get4(c);
  183. c -= 2; relo->or_nami = uget2(c);
  184. relo->or_sect = *--c;
  185. relo->or_type = *--c;
  186. }
  187. }
  188. }
  189. void
  190. rd_name(name, cnt)
  191. register struct outname *name;
  192. register unsigned int cnt;
  193. {
  194. OUTREAD(PARTNAME, (char *) name, (long) cnt * SZ_NAME);
  195. #if BYTE_ORDER == 0x0123
  196. if (sizeof(struct outname) != SZ_NAME)
  197. #endif
  198. {
  199. register char *c = (char *) name + (long) cnt * SZ_NAME;
  200. name += cnt;
  201. while (cnt--) {
  202. name--;
  203. c -= 4; name->on_valu = get4(c);
  204. c -= 2; name->on_desc = uget2(c);
  205. c -= 2; name->on_type = uget2(c);
  206. c -= 4; name->on_foff = get4(c);
  207. }
  208. }
  209. }
  210. void
  211. rd_string(addr, len)
  212. char *addr;
  213. long len;
  214. {
  215. OUTREAD(PARTCHAR, addr, len);
  216. }
  217. #ifdef SYMDBUG
  218. void
  219. rd_dbug(buf, size)
  220. char *buf;
  221. long size;
  222. {
  223. OUTREAD(PARTDBUG, buf, size);
  224. }
  225. #endif