Pristine Ack-5.5
[Ack-5.5.git] / mach / i386 / cv / cv.c
1 /*
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".
4  *
5  */
6
7 /*
8  * This program converts ack.out format to XENIX386 x.out format.
9  * It uses ~em/modules/lib/libobject.a.
10  */
11
12 #include <stdio.h>
13
14 struct xexec {
15         unsigned short  x_magic;
16 #define XMAGIC  01006
17         unsigned short  x_ext;          /* 054 */
18         long            x_text;
19         long            x_data;
20         long            x_bss;
21         long            x_syms;
22         long            x_reloc;        /* 0 */
23         long            x_entry;        /* 0 */
24         char            x_cpu;
25 #define XBSWAP  0x80
26 #define XWSWAP  0x40
27 #define X8086   0x04
28 #define X286    0x09
29 #define X386    0x0a
30         char            x_relsym;       /* 0144, not used */
31         unsigned short  x_renv;
32 #define XV5             0xc000
33 #define XVERS           0xc000          /* version mask */
34 #define XLOCK           0x1000
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 */
39 };
40
41 struct xext {
42         long            xe_trsize, xe_drsize, xe_tbase, xe_dbase; /* 0 */
43         long            xe_stksize;
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)
48                                         */
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 */
56 };
57
58 struct xseg {
59         unsigned short  xs_type;
60 #define XTEXT   1
61 #define XDATA   2
62 #define XTSYMS  3
63         unsigned short  xs_attr;
64 #define XAMEM   0x8000
65 #define X_ABSS  0x0004
66 #define X_APURE 0x0008
67 #define X_A32B  0x0040
68         unsigned short  xs_seg;         /* 077 for text, 0107 for data, 0 for sym */
69         unsigned short  xs_sres;        /* 0 */
70         long            xs_filpos;
71         long            xs_psize;
72         long            xs_vsize;
73         long            xs_rbase;
74         long            xs_lres;        /* 0 */
75         long            xs_lres2;       /* 0 */
76 };
77
78 #include <out.h>
79
80 #ifndef NORCSID
81 static char rcs_id[] = "$Id: cv.c,v 1.3 1994/06/24 13:00:56 ceriel Exp $" ;
82 #endif
83
84 #define ENTRY 0x0L      /* entry point */
85
86 /*
87  * Header and section table of new format object file.
88  */
89 struct outhead  outhead;
90 struct outsect  outsect[S_MAX];
91
92 struct xexec exec;
93 struct xext  ext;
94 struct xseg  seg[3];
95
96 char    *output_file;
97 int     outputfile_created;
98 int     output;
99
100 char *program ;
101
102 extern long lseek();
103 #define TEXTSG  0
104 #define ROMSG   1
105 #define DATASG  2
106 #define BSSSG   3
107 #define LSECT   BSSSG+1
108 #define NSECT   LSECT+1
109
110 long    emit_symtab();
111
112 int     sep_id;
113
114 long
115 align(n, a)
116         long n;
117 {
118         return ((n + a - 1) / a) * a;
119 }
120
121 main(argc, argv)
122         int     argc;
123         char    *argv[];
124 {
125
126         output = 1;
127         program= argv[0] ;
128         switch (argc) {
129         case 1: rd_fdopen(0);
130                 break;
131         case 3: if ((output = creat(argv[2], 0644)) < 0) {
132                         fatal("Can't write %s.\n", argv[2]);
133                 }
134                 output_file = argv[2];
135                 outputfile_created = 1;
136                 /* FALLTHROUGH */
137         case 2:
138                 if (! rd_open(argv[1]))
139                         fatal("Can't read %s.\n", argv[1]);
140                 break;
141         default:fatal("Usage: %s <ACK object> <Xenix object>.\n", argv[0]);
142         }
143         rd_ohead(&outhead);
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++;
160         /* A few checks */
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") ;
169
170         exec.x_magic = XMAGIC;
171         exec.x_ext = 054;
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 ) {
180                 /* Separate I/D */
181                 sep_id = 1;
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") ;
186         } else {
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") ;
193         }
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") ;
200         }
201         ext.xe_stksize = 0;
202         ext.xe_segpos = 0140;
203         ext.xe_segsize = (2 + sep_id) * 040;
204         ext.xe_pagesize = 2;
205         ext.xe_ostype = 1;
206         ext.xe_osvers = 2;
207         ext.xe_eseg = 077;
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;
216         if (sep_id) {
217                 seg[2] = seg[1];
218                 seg[2].xs_filpos = 02000 + align(outsect[TEXTSG].os_size,02000) +
219                            outsect[DATASG].os_size + outsect[ROMSG].os_size;
220                 seg[1] = seg[0];
221                 seg[1].xs_rbase = 0x1880000;
222                 seg[0].xs_type = XTEXT;
223                 seg[0].xs_attr = XAMEM | X_APURE | X_A32B;
224                 seg[0].xs_seg = 077;
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;
229         }
230         else {
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;
235         }
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", 
240                         program);
241                 ext.xe_segsize -= 040;
242                 seg[1+sep_id].xs_type = 0;
243         }
244         lseek(output, seg[0].xs_filpos, 0);
245
246         emits(&outsect[TEXTSG]) ;
247         if (sep_id) {
248                 lseek(output, seg[1].xs_filpos, 0);
249         }
250         emits(&outsect[ROMSG]) ;
251         emits(&outsect[DATASG]) ;
252
253         lseek(output, 0L, 0);
254         header();
255
256         if ( outputfile_created ) chmod(argv[2],0755);
257         exit(0);
258 }
259
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)
262
263 char buf[0300];
264
265 header()
266 {
267         register char *p = buf;
268         register int i;
269
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);
278         *p++ = exec.x_cpu;
279         *p++ = exec.x_relsym;
280         shortcvt(exec.x_renv, p);
281
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);
297
298         p = &buf[0140];
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);
310         }
311
312         write(output, buf, 0140 + i * 040);
313 }
314
315 /*
316  * Transfer the emitted byted from one file to another.
317  */
318 emits(section) struct outsect *section ; {
319         register long   n ;
320         register int    blk;
321         char            buffer[BUFSIZ];
322
323         n= section->os_flen ;
324         rd_outsect(section - outsect);
325         while (n > 0) {
326                 blk = n > BUFSIZ ? BUFSIZ : n;
327                 rd_emit(buffer, (long) blk);
328                 write(output, buffer, blk);
329                 n -= blk;
330         }
331         if ((n = section->os_size - section->os_flen) > 0) {
332                 for (blk = BUFSIZ - 1; blk >= 0; blk--) {
333                         buffer[blk] = 0;
334                 }
335                 while (n > 0) {
336                         blk = n > BUFSIZ ? BUFSIZ : n;
337                         write(output, buffer, blk);
338                         n -= blk;
339                 }
340         }
341 }
342
343 long
344 emit_symtab()
345 {
346         register int i;
347         struct xnm {
348                 unsigned short s_type, s_seg;
349                 long    s_value;
350         } xnm;
351         char *chars, *xname;
352         struct outname *names;
353         register char *xptr;
354         extern char *malloc();
355         long off = OFF_CHAR(outhead);
356         register char *p;
357         register struct outname *np;
358
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));
363         if (! np) {
364                 free(chars);
365                 return 0;
366         }
367         xptr = malloc((unsigned)(outhead.oh_nchar) + 9 * outhead.oh_nname);
368         if (! xptr) {
369                 free(chars);
370                 free((char *) np);
371                 return 0;
372         }
373         xname = xptr;
374         rd_name(np, outhead.oh_nname);
375         rd_string(chars,outhead.oh_nchar);
376         for (i = 0; i < outhead.oh_nname; i++, np++) {
377                 xnm.s_seg = 077;
378                 if (np->on_type & S_STB) {
379                         xnm.s_seg = np->on_desc;
380                         xnm.s_type = np->on_type;
381                 }
382                 else switch(np->on_type & S_ETC) {
383                 case S_FIL:
384                 case S_MOD:
385                         xnm.s_type = 0x1f;
386                         break;
387                 case S_SCT:
388                         xnm.s_type = 0x8;
389                         if ((np->on_type & S_TYP) != S_MIN+TEXTSG) {
390                                 xnm.s_seg = 0107;
391                         }
392                         break;
393                 default:
394                         switch(np->on_type & S_TYP) {
395                         case S_UND:
396                                 xnm.s_type = 0;
397                                 break;
398                         case S_ABS:
399                                 xnm.s_type = 1;
400                                 xnm.s_seg = 0107;
401                                 break;
402                         case S_MIN + TEXTSG:
403                                 xnm.s_type = 2;
404                                 break;
405                         case S_MIN + ROMSG:
406                         case S_MIN + DATASG:
407                                 xnm.s_type = 3;
408                                 xnm.s_seg = 0107;
409                                 break;
410                         case S_MIN + BSSSG:
411                         case S_MIN + LSECT:
412                                 xnm.s_type = 4;
413                                 xnm.s_seg = 0107;
414                                 break;
415                         default:
416                                 fprintf(stderr,"warning: unknown s_type: %d\n",
417                                         (int)(np->on_type) & S_TYP);
418                         }
419                 }
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) {
426                         *xptr++ = '\0';
427                 }
428                 else {
429                         long l = np->on_foff - off;
430                         if (l < 0 || l >= outhead.oh_nchar) {
431                                 fatal("bad on_off: %ld\n",l);
432                         }
433                         p = &chars[l];
434                         do {
435                                 *xptr++ = *p;
436                         } while (*p++);
437                 }
438         }
439         write(output, xname, xptr - xname);
440         free(xname);
441         free((char *) names);
442         free(chars);
443         return xptr - xname;
444 }
445
446 /* VARARGS1 */
447 fatal(s, a1, a2)
448         char    *s;
449 {
450         fprintf(stderr,"%s: ",program) ;
451         fprintf(stderr, s, a1, a2);
452         if (outputfile_created)
453                 unlink(output_file);
454         exit(1);
455 }
456
457 rd_fatal()
458 {
459         fatal("read error\n");
460 }