Pristine Ack-5.5
[Ack-5.5.git] / mach / pdp / 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 PDP 11 V7 a.out format.
9  * It uses ~em/modules/lib/libobject.a.
10  */
11
12 #include <stdio.h>
13
14 struct exec {
15         short a_magic;
16         short a_text;
17         short a_data;
18         short a_bss;
19         short a_syms;
20         short a_entry;
21         short a_unused;
22         short a_flag;
23 };
24
25 struct  nlist
26 {       char            n_name[8];
27         short           n_type;
28         short           n_value; 
29 };
30
31 #include <out.h>
32
33 #ifndef NORCSID
34 static char rcs_id[] = "$Id: cv.c,v 1.3 1994/06/24 13:12:05 ceriel Exp $" ;
35 #endif
36
37 #define ENTRY 0x0       /* entry point */
38
39 /*
40  * Header and section table of new format object file.
41  */
42 struct outhead  outhead;
43 struct outsect  outsect[S_MAX];
44
45 struct exec exec;
46
47 char    *output_file;
48 int     outputfile_created;
49 FILE    *output;
50 int     rom_in_text;
51
52 char *program ;
53
54 char flag ;
55
56 /* Output file definitions and such */
57
58 #define TEXTSG  0
59 #define ROMSG   1
60 #define DATASG  2
61 #define BSSSG   3
62 #define LSECT   BSSSG+1
63 #define NSECT   LSECT+1
64
65 main(argc, argv)
66         int     argc;
67         char    *argv[];
68 {
69         register struct exec *e = &exec;
70
71         output = stdout;
72         program= argv[0] ;
73         if ( argc>1 && argv[1][0]=='-' ) {
74                 flag=argv[1][1] ;
75                 argc-- ; argv++ ;
76         }
77         switch (argc) {
78         case 1: rd_fdopen(0);
79                 break;
80         case 3: if ((output = fopen(argv[2], "w")) == NULL) {
81                         fatal("Can't write %s.\n", argv[2]);
82                 }
83                 output_file = argv[2];
84                 outputfile_created = 1;
85                 /* FALLTHROUGH */
86         case 2:
87                 if (! rd_open(argv[1]))
88                         fatal("Can't read %s.\n", argv[1]);
89                 break;
90         default:fatal("Usage: %s <as object> <dl object>.\n", argv[0]);
91         }
92         rd_ohead(&outhead);
93         if (BADMAGIC(outhead))
94                 fatal("Not an ack object file.\n");
95         if (outhead.oh_flags & HF_LINK)
96                 fatal("Contains unresolved references.\n");
97         if (outhead.oh_nrelo > 0)
98                 fprintf(stderr, "Warning: relocation information present.\n");
99         if ( outhead.oh_nsect!=LSECT && outhead.oh_nsect!=NSECT )
100                 fatal("Input file must have %d sections, not %ld\n",
101                         NSECT,outhead.oh_nsect) ;
102         rd_sect(outsect, outhead.oh_nsect);
103         if (outsect[TEXTSG].os_size & 1)
104                 outsect[TEXTSG].os_size++;
105         if (outsect[ROMSG].os_size & 1)
106                 outsect[ROMSG].os_size++;
107         if (outsect[DATASG].os_size & 1)
108                 outsect[DATASG].os_size++;
109         if (outsect[BSSSG].os_size & 1)
110                 outsect[BSSSG].os_size++;
111         /* A few checks */
112         if ( outsect[TEXTSG].os_base != ENTRY)
113                 fatal("text must start at %d not at 0x%lx\n", ENTRY,
114                         outsect[TEXTSG].os_base) ;
115         if ( outsect[BSSSG].os_flen != 0 )
116                 fatal("bss space contains initialized data\n") ;
117         if ( outsect[BSSSG].os_base != outsect[DATASG].os_base+
118                                         outsect[DATASG].os_size )
119                 fatal("bss segment must follow data segment\n") ;
120
121         e->a_magic = 0407;
122         e->a_text = outsect[TEXTSG].os_size;
123         e->a_data = outsect[ROMSG].os_size + outsect[DATASG].os_size;
124         e->a_bss = outsect[BSSSG].os_size;
125         e->a_entry = outsect[TEXTSG].os_base;
126         e->a_syms = outhead.oh_nname * sizeof (struct nlist);
127         e->a_flag = 1;
128         if ( outsect[ROMSG].os_base == 0x0 ) {
129                 /* Separate I/D */
130                 e->a_magic = 0411;
131                 if ( outsect[DATASG].os_base != outsect[ROMSG].os_base+
132                                                 outsect[ROMSG].os_size )
133                         fatal("data segment must follow rom\n") ;
134         } else  if ( outsect[ROMSG].os_lign == 0x2000 ) {
135                 /* -n, rom in data */
136                 e->a_magic = 0410;
137                 if ( outsect[DATASG].os_base != outsect[ROMSG].os_base+
138                                                 outsect[ROMSG].os_size )
139                         fatal("data segment must follow rom\n") ;
140         } else  if ( outsect[DATASG].os_lign == 0x2000 ) {
141                 /* -n, rom in text */
142                 rom_in_text = 1;
143                 e->a_magic = 0410;
144                 e->a_text += outsect[ROMSG].os_size;
145                 e->a_data -= outsect[ROMSG].os_size;
146                 if ( outsect[ROMSG].os_base != outsect[TEXTSG].os_base+
147                                                 outsect[TEXTSG].os_size )
148                         fatal("rom segment must follow text\n") ;
149         }
150         else {
151                 if ( outsect[ROMSG].os_base != outsect[TEXTSG].os_base+
152                                                 outsect[TEXTSG].os_size )
153                         fatal("rom segment must follow text\n") ;
154                 if ( outsect[DATASG].os_base != outsect[ROMSG].os_base+
155                                                 outsect[ROMSG].os_size )
156                         fatal("data segment must follow rom\n") ;
157         }
158         if ( outhead.oh_nsect==NSECT ) {
159                 if ( outsect[LSECT].os_base != outsect[BSSSG].os_base+
160                                                 outsect[BSSSG].os_size )
161                         fatal("end segment must follow bss\n") ;
162                 if ( outsect[LSECT].os_size != 0 )
163                         fatal("end segment must be empty\n") ;
164         }
165
166         /* Action at last */
167         wr_int2(e->a_magic);
168         wr_int2(e->a_text);
169         wr_int2(e->a_data);
170         wr_int2(e->a_bss);
171         wr_int2(e->a_syms);
172         wr_int2(e->a_entry);
173         wr_int2(e->a_unused);
174         wr_int2(e->a_flag);
175         emits(&outsect[TEXTSG]) ;
176         emits(&outsect[ROMSG]) ;
177         emits(&outsect[DATASG]) ;
178         emit_symtab();
179         if ( outputfile_created ) chmod(argv[2],0755);
180         return 0;
181 }
182
183 wr_int2(n)
184 {
185         putc(n, output);
186         putc((n>>8), output);
187 }
188
189 /*
190 wr_long(l)
191         long l;
192 {
193         putc((int)(l >> 16), output);
194         putc((int)(l >> 24), output);
195         putc((int) l, output);
196         putc(((int)l >> 8), output);
197 }
198 */
199
200 /*
201  * Transfer the emitted byted from one file to another.
202  */
203 emits(section) struct outsect *section ; {
204         register long   n ;
205         register int    blk;
206         char            buffer[BUFSIZ];
207
208         n= section->os_flen ;
209         rd_outsect(section - outsect);
210         while (n > 0) {
211                 blk = n > BUFSIZ ? BUFSIZ : n;
212                 rd_emit(buffer, (long) blk);
213                 fwrite(buffer, sizeof(char), blk, output);
214                 n -= blk;
215         }
216         if ((n = section->os_size - section->os_flen) > 0) {
217                 for (blk = BUFSIZ - 1; blk >= 0; blk--) {
218                         buffer[blk] = 0;
219                 }
220                 while (n > 0) {
221                         blk = n > BUFSIZ ? BUFSIZ : n;
222                         fwrite(buffer, sizeof(char), blk, output);
223                         n -= blk;
224                 }
225         }
226 }
227
228 emit_symtab()
229 {
230         struct outname ACK_name;  /* symbol table entry in ACK format */
231         struct nlist PDP_name;    /* symbol table entry in PDP V7 format */
232         register unsigned short i;
233
234         extern char *malloc();
235         char *chars;
236         long l;
237         long off = OFF_CHAR(outhead);
238         int j;
239         char *p;
240
241         chars = malloc((unsigned) (outhead.oh_nchar));
242         rd_string(chars,outhead.oh_nchar);
243         for (i = 0; i < outhead.oh_nname; i++) {
244                 rd_name(&ACK_name, 1);
245                 switch(ACK_name.on_type & S_TYP) {
246                         case S_UND:
247                                 PDP_name.n_type = 0;    
248                                 break;
249                         case S_ABS:
250                                 PDP_name.n_type = 01;
251                                 break;
252                         case S_MIN + TEXTSG:
253                                 PDP_name.n_type = 02; 
254                                 break;
255                         case S_MIN + ROMSG:
256                                 if (rom_in_text) {
257                                         PDP_name.n_type = 02;
258                                         break;
259                                 }
260                                 /* Fall through */
261                         case S_MIN + DATASG:
262                                 PDP_name.n_type = 03;
263                                 break;
264                         case S_MIN + BSSSG:
265                         case S_MIN + LSECT:
266                                 PDP_name.n_type = 04;
267                                 break;
268                         default:
269                                 fprintf(stderr,"warning: unknown s_type: %d\n",
270                                         ACK_name.on_type & S_TYP);
271                 }
272                 if (ACK_name.on_type & S_EXT) PDP_name.n_type |= 040; 
273                 PDP_name.n_value = ACK_name.on_valu;
274                 if (ACK_name.on_foff == 0) {
275                         p = "\0\0";
276                 }
277                 else {
278                         l = ACK_name.on_foff - off;
279                         if (l < 0 || l >= outhead.oh_nchar) {
280                                 fatal("bad on_off: %ld\n",l);
281                         }
282                         p = &chars[l];
283                 }
284                 for (j = 0; j < 8; j++) {
285                         PDP_name.n_name[j] = *p++;
286                         if (*p == '\0') break;
287                 }
288                 for (j++; j < 8; j++) {
289                         PDP_name.n_name[j] = 0;
290                 }
291                 fwrite((char *) &PDP_name, sizeof(char), 8, output);
292                 wr_int2(PDP_name.n_type);
293                 wr_int2(PDP_name.n_value);
294         }
295 }
296
297 /* VARARGS1 */
298 fatal(s, a1, a2)
299         char    *s;
300 {
301         fprintf(stderr,"%s: ",program) ;
302         fprintf(stderr, s, a1, a2);
303         if (outputfile_created)
304                 unlink(output_file);
305         exit(1);
306 }
307
308 rd_fatal()
309 {
310         fatal("read error\n");
311 }