2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
8 * This program converts ack.out format to XENIX386 x.out format.
9 * It uses ~em/modules/lib/libobject.a.
15 unsigned short x_magic;
17 unsigned short x_ext; /* 054 */
30 char x_relsym; /* 0144, not used */
31 unsigned short x_renv;
33 #define XVERS 0xc000 /* version mask */
35 #define XSEG 0x0800 /* segment table present */
36 #define XPURE 0x0004 /* pure text */
37 #define XSEP 0x0002 /* separate I & D */
38 #define XEXEC 0x0001 /* executable */
42 long xe_trsize, xe_drsize, xe_tbase, xe_dbase; /* 0 */
44 long xe_segpos; /* 0140 ??? */
45 long xe_segsize; /* 2 or 3 segments */
46 /* one for name table, one for text+data
47 or one for text + one for data(-i)
49 long xe_mdtpos, xe_mdtsize; /* 0 */
50 char xe_mdttype; /* 0 */
51 char xe_pagesize; /* 2 */
52 char xe_ostype; /* 1 */
53 char xe_osvers; /* 2 */
54 unsigned short xe_eseg; /* 077 ??? */
55 unsigned short xe_sres; /* 0 */
59 unsigned short xs_type;
63 unsigned short xs_attr;
66 #define X_APURE 0x0008
68 unsigned short xs_seg; /* 077 for text, 0107 for data, 0 for sym */
69 unsigned short xs_sres; /* 0 */
75 long xs_lres2; /* 0 */
81 static char rcs_id[] = "$Id: cv.c,v 1.3 1994/06/24 13:00:56 ceriel Exp $" ;
84 #define ENTRY 0x0L /* entry point */
87 * Header and section table of new format object file.
89 struct outhead outhead;
90 struct outsect outsect[S_MAX];
97 int outputfile_created;
107 #define LSECT BSSSG+1
108 #define NSECT LSECT+1
118 return ((n + a - 1) / a) * a;
129 case 1: rd_fdopen(0);
131 case 3: if ((output = creat(argv[2], 0644)) < 0) {
132 fatal("Can't write %s.\n", argv[2]);
134 output_file = argv[2];
135 outputfile_created = 1;
138 if (! rd_open(argv[1]))
139 fatal("Can't read %s.\n", argv[1]);
141 default:fatal("Usage: %s <ACK object> <Xenix object>.\n", argv[0]);
144 if (BADMAGIC(outhead))
145 fatal("Not an ack object file.\n");
146 if (outhead.oh_flags & HF_LINK)
147 fatal("Contains unresolved references.\n");
148 if (outhead.oh_nrelo > 0)
149 fprintf(stderr, "Warning: relocation information present.\n");
150 if ( outhead.oh_nsect!=LSECT && outhead.oh_nsect!=NSECT )
151 fatal("Input file must have %d sections, not %ld\n",
152 NSECT,outhead.oh_nsect) ;
153 rd_sect(outsect, outhead.oh_nsect);
154 while (outsect[TEXTSG].os_size % outsect[TEXTSG].os_lign)
155 outsect[TEXTSG].os_size++;
156 while (outsect[ROMSG].os_size % outsect[ROMSG].os_lign)
157 outsect[ROMSG].os_size++;
158 while (outsect[DATASG].os_size % outsect[DATASG].os_lign)
159 outsect[DATASG].os_size++;
161 if ( outsect[TEXTSG].os_base != ENTRY)
162 fatal("text must start at 0x%lx not at 0x%lx\n", ENTRY,
163 outsect[TEXTSG].os_base) ;
164 if ( outsect[BSSSG].os_flen != 0 )
165 fatal("bss space contains initialized data\n") ;
166 if ( outsect[BSSSG].os_base != outsect[DATASG].os_base+
167 outsect[DATASG].os_size )
168 fatal("bss segment must follow data segment\n") ;
170 exec.x_magic = XMAGIC;
172 exec.x_text = outsect[TEXTSG].os_size;
173 exec.x_data = outsect[ROMSG].os_size + outsect[DATASG].os_size;
174 exec.x_bss = outsect[BSSSG].os_size;
175 exec.x_entry = outsect[TEXTSG].os_base;
176 exec.x_cpu = XWSWAP | X386;
177 exec.x_relsym = 0144;
178 exec.x_renv = XV5 | XSEG | XLOCK | XEXEC;
179 if ( outsect[ROMSG].os_base == 0x1880000 ) {
182 exec.x_renv |= XPURE | XSEP;
183 if ( outsect[DATASG].os_base != outsect[ROMSG].os_base+
184 outsect[ROMSG].os_size )
185 fatal("data segment must follow rom\n") ;
187 if ( outsect[ROMSG].os_base != outsect[TEXTSG].os_base+
188 outsect[TEXTSG].os_size )
189 fatal("rom segment must follow text\n") ;
190 if ( outsect[DATASG].os_base != outsect[ROMSG].os_base+
191 outsect[ROMSG].os_size )
192 fatal("data segment must follow rom\n") ;
194 if ( outhead.oh_nsect==NSECT ) {
195 if ( outsect[LSECT].os_base != outsect[BSSSG].os_base+
196 outsect[BSSSG].os_size )
197 fatal("end segment must follow bss\n") ;
198 if ( outsect[LSECT].os_size != 0 )
199 fatal("end segment must be empty\n") ;
202 ext.xe_segpos = 0140;
203 ext.xe_segsize = (2 + sep_id) * 040;
208 seg[0].xs_type = XDATA;
209 seg[0].xs_attr = XAMEM | X_ABSS | X_A32B;
210 seg[0].xs_seg = 0107;
211 seg[0].xs_filpos = 02000;
212 seg[1].xs_type = XTSYMS;
213 seg[1].xs_attr = 1; /* ??? */
214 seg[1].xs_filpos = 02000 + outsect[TEXTSG].os_size +
215 outsect[DATASG].os_size + outsect[ROMSG].os_size;
218 seg[2].xs_filpos = 02000 + align(outsect[TEXTSG].os_size,02000) +
219 outsect[DATASG].os_size + outsect[ROMSG].os_size;
221 seg[1].xs_rbase = 0x1880000;
222 seg[0].xs_type = XTEXT;
223 seg[0].xs_attr = XAMEM | X_APURE | X_A32B;
225 seg[0].xs_psize = seg[0].xs_vsize = outsect[TEXTSG].os_size;
226 seg[1].xs_filpos = seg[0].xs_filpos + align(seg[0].xs_psize,02000);
227 seg[1].xs_psize = outsect[ROMSG].os_size + outsect[DATASG].os_size;
228 seg[1].xs_vsize = seg[1].xs_psize + outsect[BSSSG].os_size;
231 seg[0].xs_psize = outsect[TEXTSG].os_size +
232 outsect[ROMSG].os_size +
233 outsect[DATASG].os_size;
234 seg[0].xs_vsize = seg[0].xs_psize + outsect[BSSSG].os_size;
236 lseek(output, seg[1+sep_id].xs_filpos, 0);
237 if (! (exec.x_syms = seg[1+sep_id].xs_psize = emit_symtab())) {
238 /* not enough memory to produce symbol table, do without */
239 fprintf(stderr, "%s: warning: no symbol table produced\n",
241 ext.xe_segsize -= 040;
242 seg[1+sep_id].xs_type = 0;
244 lseek(output, seg[0].xs_filpos, 0);
246 emits(&outsect[TEXTSG]) ;
248 lseek(output, seg[1].xs_filpos, 0);
250 emits(&outsect[ROMSG]) ;
251 emits(&outsect[DATASG]) ;
253 lseek(output, 0L, 0);
256 if ( outputfile_created ) chmod(argv[2],0755);
260 #define shortcvt(val, p) (*p++ = val, *p++ = val >> 8)
261 #define longcvt(val, p) (*p++ = val, *p++ = val >> 8, *p++ = val >> 16, *p++ = val >> 24)
267 register char *p = buf;
270 shortcvt(exec.x_magic, p);
271 shortcvt(exec.x_ext, p);
272 longcvt(exec.x_text, p);
273 longcvt(exec.x_data, p);
274 longcvt(exec.x_bss, p);
275 longcvt(exec.x_syms, p);
276 longcvt(exec.x_reloc, p);
277 longcvt(exec.x_entry, p);
279 *p++ = exec.x_relsym;
280 shortcvt(exec.x_renv, p);
282 longcvt(ext.xe_trsize, p);
283 longcvt(ext.xe_drsize, p);
284 longcvt(ext.xe_tbase, p);
285 longcvt(ext.xe_dbase, p);
286 longcvt(ext.xe_stksize, p);
287 longcvt(ext.xe_segpos, p);
288 longcvt(ext.xe_segsize, p);
289 longcvt(ext.xe_mdtpos, p);
290 longcvt(ext.xe_mdtsize, p);
291 *p++ = ext.xe_mdttype;
292 *p++ = ext.xe_pagesize;
293 *p++ = ext.xe_ostype;
294 *p++ = ext.xe_osvers;
295 shortcvt(ext.xe_eseg, p);
296 shortcvt(ext.xe_sres, p);
299 for (i = 0; i <= 2 && seg[i].xs_type != 0; i++) {
300 shortcvt(seg[i].xs_type, p);
301 shortcvt(seg[i].xs_attr, p);
302 shortcvt(seg[i].xs_seg, p);
303 shortcvt(seg[i].xs_sres, p);
304 longcvt(seg[i].xs_filpos, p);
305 longcvt(seg[i].xs_psize, p);
306 longcvt(seg[i].xs_vsize, p);
307 longcvt(seg[i].xs_rbase, p);
308 longcvt(seg[i].xs_lres, p);
309 longcvt(seg[i].xs_lres2, p);
312 write(output, buf, 0140 + i * 040);
316 * Transfer the emitted byted from one file to another.
318 emits(section) struct outsect *section ; {
323 n= section->os_flen ;
324 rd_outsect(section - outsect);
326 blk = n > BUFSIZ ? BUFSIZ : n;
327 rd_emit(buffer, (long) blk);
328 write(output, buffer, blk);
331 if ((n = section->os_size - section->os_flen) > 0) {
332 for (blk = BUFSIZ - 1; blk >= 0; blk--) {
336 blk = n > BUFSIZ ? BUFSIZ : n;
337 write(output, buffer, blk);
348 unsigned short s_type, s_seg;
352 struct outname *names;
354 extern char *malloc();
355 long off = OFF_CHAR(outhead);
357 register struct outname *np;
359 chars = malloc((unsigned)(outhead.oh_nchar));
360 if (! chars) return 0;
361 np = names = (struct outname *)
362 malloc(outhead.oh_nname * sizeof(struct outname));
367 xptr = malloc((unsigned)(outhead.oh_nchar) + 9 * outhead.oh_nname);
374 rd_name(np, outhead.oh_nname);
375 rd_string(chars,outhead.oh_nchar);
376 for (i = 0; i < outhead.oh_nname; i++, np++) {
378 if (np->on_type & S_STB) {
379 xnm.s_seg = np->on_desc;
380 xnm.s_type = np->on_type;
382 else switch(np->on_type & S_ETC) {
389 if ((np->on_type & S_TYP) != S_MIN+TEXTSG) {
394 switch(np->on_type & S_TYP) {
416 fprintf(stderr,"warning: unknown s_type: %d\n",
417 (int)(np->on_type) & S_TYP);
420 if (np->on_type & S_EXT) xnm.s_type |= 0x20;
421 xnm.s_value = np->on_valu;
422 shortcvt(xnm.s_type, xptr);
423 shortcvt(xnm.s_seg, xptr);
424 longcvt(xnm.s_value, xptr);
425 if (np->on_foff == 0) {
429 long l = np->on_foff - off;
430 if (l < 0 || l >= outhead.oh_nchar) {
431 fatal("bad on_off: %ld\n",l);
439 write(output, xname, xptr - xname);
441 free((char *) names);
450 fprintf(stderr,"%s: ",program) ;
451 fprintf(stderr, s, a1, a2);
452 if (outputfile_created)
459 fatal("read error\n");