00001
00002
00003
00004
00005
00006 #include <out.h>
00007 #include "object.h"
00008
00009 extern long lseek();
00010
00011
00012
00013
00014 #undef PARTEMIT
00015 #undef PARTRELO
00016 #undef PARTNAME
00017 #undef PARTCHAR
00018 #undef PARTDBUG
00019 #undef NPARTS
00020
00021 #define PARTEMIT 0
00022 #define PARTRELO 1
00023 #define PARTNAME 2
00024 #define PARTCHAR 3
00025 #ifdef SYMDBUG
00026 #define PARTDBUG 4
00027 #else
00028 #define PARTDBUG 3
00029 #endif
00030 #define NPARTS (PARTDBUG + 1)
00031
00032 static long offset[MAXSECT];
00033
00034 static int outfile;
00035 static long outseek[NPARTS];
00036 static long currpos;
00037 static long rd_base;
00038 #define OUTSECT(i) \
00039 (outseek[PARTEMIT] = offset[i])
00040 #define BEGINSEEK(p, o) \
00041 (outseek[(p)] = (o))
00042
00043 static int sectionnr;
00044
00045 static
00046 OUTREAD(p, b, n)
00047 char *b;
00048 long n;
00049 {
00050 register long l = outseek[p];
00051
00052 if (currpos != l) {
00053 lseek(outfile, l, 0);
00054 }
00055 rd_bytes(outfile, b, n);
00056 l += n;
00057 currpos = l;
00058 outseek[p] = l;
00059 }
00060
00061
00062
00063
00064 int
00065 rd_open(f)
00066 char *f;
00067 {
00068
00069 if ((outfile = open(f, 0)) < 0)
00070 return 0;
00071 return rd_fdopen(outfile);
00072 }
00073
00074 static int offcnt;
00075
00076 rd_fdopen(fd)
00077 {
00078 register int i;
00079
00080 for (i = 0; i < NPARTS; i++) outseek[i] = 0;
00081 offcnt = 0;
00082 rd_base = lseek(fd, 0L, 1);
00083 if (rd_base < 0) {
00084 return 0;
00085 }
00086 currpos = rd_base;
00087 outseek[PARTEMIT] = currpos;
00088 outfile = fd;
00089 sectionnr = 0;
00090 return 1;
00091 }
00092
00093 rd_close()
00094 {
00095
00096 close(outfile);
00097 outfile = -1;
00098 }
00099
00100 rd_fd()
00101 {
00102 return outfile;
00103 }
00104
00105 rd_ohead(head)
00106 register struct outhead *head;
00107 {
00108 register long off;
00109
00110 OUTREAD(PARTEMIT, (char *) head, (long) SZ_HEAD);
00111 #if ! (BYTES_REVERSED || WORDS_REVERSED)
00112 if (sizeof(struct outhead) != SZ_HEAD)
00113 #endif
00114 {
00115 register char *c = (char *) head + (SZ_HEAD-4);
00116
00117 head->oh_nchar = get4(c);
00118 c -= 4; head->oh_nemit = get4(c);
00119 c -= 2; head->oh_nname = uget2(c);
00120 c -= 2; head->oh_nrelo = uget2(c);
00121 c -= 2; head->oh_nsect = uget2(c);
00122 c -= 2; head->oh_flags = uget2(c);
00123 c -= 2; head->oh_stamp = uget2(c);
00124 c -= 2; head->oh_magic = uget2(c);
00125 }
00126 off = OFF_RELO(*head) + rd_base;
00127 BEGINSEEK(PARTRELO, off);
00128 off += (long) head->oh_nrelo * SZ_RELO;
00129 BEGINSEEK(PARTNAME, off);
00130 off += (long) head->oh_nname * SZ_NAME;
00131 BEGINSEEK(PARTCHAR, off);
00132 #ifdef SYMDBUG
00133 off += head->oh_nchar;
00134 BEGINSEEK(PARTDBUG, off);
00135 #endif
00136 }
00137
00138 rd_rew_relos(head)
00139 register struct outhead *head;
00140 {
00141 register long off = OFF_RELO(*head) + rd_base;
00142
00143 BEGINSEEK(PARTRELO, off);
00144 }
00145
00146 rd_sect(sect, cnt)
00147 register struct outsect *sect;
00148 register unsigned int cnt;
00149 {
00150 register char *c = (char *) sect + cnt * SZ_SECT;
00151
00152 OUTREAD(PARTEMIT, (char *) sect, (long)cnt * SZ_SECT);
00153 sect += cnt;
00154 offcnt += cnt;
00155 while (cnt--) {
00156 sect--;
00157 #if ! (BYTES_REVERSED || WORDS_REVERSED)
00158 if (sizeof(struct outsect) != SZ_SECT)
00159 #endif
00160 {
00161 c -= 4; sect->os_lign = get4(c);
00162 c -= 4; sect->os_flen = get4(c);
00163 c -= 4; sect->os_foff = get4(c);
00164 }
00165 offset[--offcnt] = sect->os_foff + rd_base;
00166 #if ! (BYTES_REVERSED || WORDS_REVERSED)
00167 if (sizeof(struct outsect) != SZ_SECT)
00168 #endif
00169 {
00170 c -= 4; sect->os_size = get4(c);
00171 c -= 4; sect->os_base = get4(c);
00172 }
00173 }
00174 }
00175
00176 rd_outsect(s)
00177 {
00178 OUTSECT(s);
00179 sectionnr = s;
00180 }
00181
00182
00183
00184
00185 rd_emit(emit, cnt)
00186 char *emit;
00187 long cnt;
00188 {
00189 OUTREAD(PARTEMIT, emit, cnt);
00190 offset[sectionnr] += cnt;
00191 }
00192
00193 rd_relo(relo, cnt)
00194 register struct outrelo *relo;
00195 register unsigned int cnt;
00196 {
00197
00198 OUTREAD(PARTRELO, (char *) relo, (long) cnt * SZ_RELO);
00199 #if ! (BYTES_REVERSED || WORDS_REVERSED)
00200 if (sizeof(struct outrelo) != SZ_RELO)
00201 #endif
00202 {
00203 register char *c = (char *) relo + (long) cnt * SZ_RELO;
00204
00205 relo += cnt;
00206 while (cnt--) {
00207 relo--;
00208 c -= 4; relo->or_addr = get4(c);
00209 c -= 2; relo->or_nami = uget2(c);
00210 relo->or_sect = *--c;
00211 relo->or_type = *--c;
00212 }
00213 }
00214 }
00215
00216 rd_name(name, cnt)
00217 register struct outname *name;
00218 register unsigned int cnt;
00219 {
00220
00221 OUTREAD(PARTNAME, (char *) name, (long) cnt * SZ_NAME);
00222 #if ! (BYTES_REVERSED || WORDS_REVERSED)
00223 if (sizeof(struct outname) != SZ_NAME)
00224 #endif
00225 {
00226 register char *c = (char *) name + (long) cnt * SZ_NAME;
00227
00228 name += cnt;
00229 while (cnt--) {
00230 name--;
00231 c -= 4; name->on_valu = get4(c);
00232 c -= 2; name->on_desc = uget2(c);
00233 c -= 2; name->on_type = uget2(c);
00234 c -= 4; name->on_foff = get4(c);
00235 }
00236 }
00237 }
00238
00239 rd_string(addr, len)
00240 char *addr;
00241 long len;
00242 {
00243
00244 OUTREAD(PARTCHAR, addr, len);
00245 }
00246
00247 #ifdef SYMDBUG
00248 rd_dbug(buf, size)
00249 char *buf;
00250 long size;
00251 {
00252 OUTREAD(PARTDBUG, buf, size);
00253 }
00254 #endif