rd.c 4.1 KB

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