1 /* $Id: cv.c,v 1.7 1994/06/24 13:06:07 ceriel Exp $ */
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".
6 /* This is a special cv.c program for testing purposes ;
7 * it converts to a stripped version of COFF
12 typedef struct filehdr {
13 unsigned short f_magic; /* magic number */
14 unsigned short f_nscns; /* number of sections */
15 long f_timdat; /* time & date stamp */
16 long f_symptr; /* file pointer to symtab */
17 long f_nsyms; /* number of symtab entries */
18 unsigned short f_opthdr; /* sizeof(optional hdr) */
19 unsigned short f_flags; /* flags */
22 typedef struct aouthdr {
33 typedef struct scnhdr {
41 unsigned short s_nreloc;
42 unsigned short s_nlnno;
46 #define AOUTHSZ sizeof(AOUTHDR)
47 #define SCNHSZ sizeof(SCNHDR)
48 #define FILHSZ sizeof(FILHDR)
52 #define ASSERT(x) switch (2) { case 0: case (x): ; }
55 * Header and section table of new format object file.
57 struct outhead outhead;
58 struct outsect outsect[S_MAX];
61 int outputfile_created;
69 #define readf(a, b, c) fread((a), (b), (int)(c), input)
70 #define writef(a, b, c) fwrite((a), (b), (int)(c), output)
72 /* Output file definitions and such */
84 #define MC68MAGIC 0520
87 #define STYP_TEXT 0x20
88 #define STYP_DATA 0x40
100 #define NSECT LSECT+1
115 register struct outsect *pa, *pb;
117 /* return 1 if pa follows pb */
119 return pa->os_base == align(pb->os_base+pb->os_size, pa->os_lign);
128 long textsize, datasize, bsssize;
133 ASSERT(sizeof(struct outhead) == SZ_HEAD);
134 ASSERT(sizeof(struct outsect) == SZ_SECT);
136 input = stdin; output = stdout;
138 if ( argc>1 && argv[1][0]=='-' ) {
144 case 3: if ((output = fopen(argv[2], "w")) == (FILE *)0)
145 fatal("Can't write %s.\n", argv[2]);
146 output_file = argv[2];
147 outputfile_created = 1;
149 case 2: if ((input = fopen(argv[1], "r")) == (FILE *)0)
150 fatal("Can't read %s.\n", argv[1]);
152 default:fatal("Usage: %s <as object> <dl object>.\n", argv[0]);
154 if ( !rhead(input,&outhead) )
155 fatal("Reading header failed.\n");
156 if (BADMAGIC(outhead))
157 fatal("Not an ack object file.\n");
158 if (outhead.oh_nrelo > 0)
159 fprintf(stderr, "Warning: relocation information present.\n");
160 if ( outhead.oh_nsect!=LSECT && outhead.oh_nsect!=NSECT )
161 fatal("Input file must have %d sections, not %ld\n",
162 NSECT,outhead.oh_nsect) ;
163 for ( nsect=0 ; nsect<outhead.oh_nsect ; nsect++ )
164 if ( !rsect(input,&outsect[nsect]) )
165 fatal("Reading section table failed.\n");
167 if ( outsect[TEXTSG].os_base != ENTRY)
168 fatal("text must start at %lx not at 0x%lx\n", ENTRY,
169 outsect[TEXTSG].os_base) ;
170 if ( outsect[BSSSG].os_flen != 0 )
171 printf("Warning: bss space contains initialized data\n") ;
172 /* as actually writes zeroes in the bss segment */
173 if ( ! follows(&outsect[BSSSG], &outsect[DATASG]))
174 fatal("bss segment must follow data segment\n") ;
176 /* 410 file with ROMSG in instruction space */
179 textsize= (outsect[ROMSG].os_base - outsect[TEXTSG].os_base)
180 + outsect[ROMSG].os_size ;
181 datasize= outsect[BSSSG].os_base - outsect[DATASG].os_base ;
182 if ( ! follows(&outsect[ROMSG], &outsect[TEXTSG]))
183 fatal("rom segment must follow text\n") ;
185 outsect[TEXTSG].os_size = outsect[ROMSG].os_base - outsect[TEXTSG].os_base;
186 outsect[DATASG].os_size = datasize;
187 bsssize = outsect[BSSSG].os_size;
188 if ( outhead.oh_nsect==NSECT ) {
189 if ( ! follows(&outsect[LSECT], &outsect[BSSSG]))
190 fatal("end segment must follow bss\n") ;
191 if ( outsect[LSECT].os_size != 0 )
192 fatal("end segment must be empty\n") ;
195 filh.f_magic = MC68MAGIC;
196 filh.f_nscns = 3; /* three sections: .text, data, .bss */
197 filh.f_timdat = (long) time(0); /* doesn't really matter */
198 filh.f_symptr = 0L; /* no symbol table */
199 filh.f_nsyms = 0L; /* ditto */
200 filh.f_opthdr = AOUTHSZ;
201 filh.f_flags = MYFLAGS;
205 aouth.tsize = textsize;
206 aouth.dsize = datasize;
207 aouth.bsize = bsssize;
209 aouth.text_start= ENTRY;
210 aouth.data_start= outsect[DATASG].os_base;
212 strcpy(scnh[TS].s_name, _TEXT);
213 strcpy(scnh[DS].s_name, _DATA);
214 strcpy(scnh[BS].s_name, _BSS);
216 scnh[TS].s_vaddr = (scnh[TS].s_paddr = aouth.text_start);
217 scnh[DS].s_vaddr = (scnh[DS].s_paddr = aouth.data_start);
218 scnh[BS].s_vaddr = (scnh[BS].s_paddr = aouth.data_start + datasize);
220 scnh[TS].s_size = textsize;
221 scnh[DS].s_size = datasize;
222 scnh[BS].s_size = bsssize;
224 scnh[TS].s_scnptr = FILHSZ + AOUTHSZ + 3 * SCNHSZ;
225 scnh[DS].s_scnptr = FILHSZ + AOUTHSZ + 3 * SCNHSZ + textsize;
226 scnh[BS].s_scnptr = 0L; /* always 0 for .bss section */
228 for (scn=TS; scn<=BS; scn++) {
229 scnh[scn].s_relptr = 0L; /* no relocation */
230 scnh[scn].s_lnnoptr = 0L; /* no line numbers */
231 scnh[scn].s_nreloc = 0; /* no relocation */
232 scnh[scn].s_nlnno = 0; /* no line numbers */
235 scnh[TS].s_flags = STYP_TEXT;
236 scnh[DS].s_flags = STYP_DATA;
237 scnh[BS].s_flags = STYP_BSS;
239 /* now write the new file headers */
240 ffwrite(&filh, FILHSZ, 1);
241 ffwrite(&aouth, AOUTHSZ, 1);
242 ffwrite(scnh, SCNHSZ, 3); /* write all three section headers */
244 emits(&outsect[TEXTSG]) ;
245 emits(&outsect[ROMSG]) ;
246 emits(&outsect[DATASG]) ;
248 if ( outputfile_created ) chmod(argv[2],0755);
252 ffwrite(ptr, size, nitems)
255 writef(ptr, size, nitems);
257 fatal("output write error\n");
261 * Transfer the emitted bytes from one file to another.
263 emits(section) struct outsect *section ; {
268 n= section->os_flen ;
270 blk = n > BUFSIZ ? BUFSIZ : n;
271 readf(buffer, 1, blk);
272 writef(buffer, 1, blk);
275 if ((n=section->os_size - section->os_flen) > 0) {
276 fseek(output, n-1, 1);
282 rhead(f,head) struct outhead *head ; FILE *f ; {
284 if ( fread(buf,SZ_HEAD,1,f)!=1 ) return 0 ;
285 iconvert(buf,(char *)head,SF_HEAD) ;
289 rsect(f,sect) struct outsect *sect ; FILE *f ; {
291 if ( fread(buf,SZ_SECT,1,f)!=1 ) return 0 ;
292 iconvert(buf,(char *)sect,SF_SECT) ;
297 iconvert(buf,str,fmt) char *buf, *str, *fmt ; {
298 register char *nf, *ni, *no ;
301 ni=buf ; no=str ; nf=fmt ;
302 while ( last = *nf++ ) {
304 if ( last<1 || last >9 ) fatal("illegal out.h format string\n");
308 value = (value<<8) + (ni[i]&0xFF) ;
312 case 1 : *no= value ; break ;
313 case 2 : *(unsigned short *)no = value ; break ;
314 case 4 : *(long *)no = value ; break ;
316 fatal("illegal out.h format string\n");
318 ni += last ; no += last ;
327 fprintf(stderr,"%s: ",program) ;
328 fprintf(stderr, s, a1, a2);
329 if (outputfile_created)