rd.c 4.4 KB

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