2 * Conversion from ack a.out to Arthur output format.
3 * All this really does is to output the sections, text first,
4 * with no header or symbol table information.
6 * Mostly pinched from the code for the m68k2 cv.
14 #define ASSERT(x) switch (2) { case 0: case (x): ; }
17 * Header and section table of ack object file.
19 struct outhead outhead;
20 struct outsect outsect[S_MAX];
30 #define readf(a, b, c) fread((a), (b), (int)(c), input)
31 #define writef(a, b, c) fwrite((a), (b), (int)(c), output)
33 /* Output file definitions and such */
37 char hdr[HDR_LENGTH] ;
67 register struct outsect *pa, *pb;
69 /* return 1 if pa follows pb */
71 return pa->os_base == align(pb->os_base+pb->os_size, pa->os_lign);
84 ASSERT(sizeof(struct outhead) == SZ_HEAD);
85 ASSERT(sizeof(struct outsect) == SZ_SECT);
87 input = stdin; output = stdout;
89 if ( argc>1 && argv[1][0]=='-' ) {
95 case 3: if ((output = fopen(argv[2], "w")) == (FILE *)0)
96 fatal("Can't write %s.\n", argv[2]);
100 case 2: if ((input = fopen(argv[1], "r")) == (FILE *)0)
101 fatal("Can't read %s.\n", argv[1]);
103 default:fatal("Usage: %s <ack object> <riscos object>.\n", argv[0]);
105 if ( !rhead(input,&outhead) )
106 fatal("Reading header failed.\n");
107 if (BADMAGIC(outhead))
108 fatal("Not an ack object file.\n");
109 if (outhead.oh_nrelo > 0)
110 fprintf(stderr, "Warning: relocation information present.\n");
111 if ( outhead.oh_nsect!=LSECT && outhead.oh_nsect!=NSECT )
112 fatal("Input file must have %d sections, not %ld\n",
113 NSECT,outhead.oh_nsect) ;
114 for ( nsect=0 ; nsect<outhead.oh_nsect ; nsect++ )
115 if ( !rsect(input,&outsect[nsect]) )
116 fatal("Reading section table failed.\n");
120 if ( outsect[TEXT].os_base != 0x8000 )
121 fatal("text must start at 0x8000 not at 0x%lx\n",
122 outsect[TEXT].os_base) ;
124 if ( outsect[BSS].os_flen != 0 )
125 fatal("bss space contains initialized data\n") ;
127 if ( ! follows(&outsect[BSS], &outsect[DATA]))
128 fatal("bss segment must follow data segment\n") ;
130 if ( ! follows(& outsect[ROM], &outsect[TEXT]))
131 fatal("rom segment must follow text\n") ;
133 if ( ! follows(& outsect[DATA], &outsect[ROM]))
134 fatal("data segment must follow rom\n") ;
136 if ( outhead.oh_nsect==NSECT ) {
137 if (! follows(&outsect[LSECT], &outsect[BSS]))
138 fatal("end segment must follow bss\n") ;
139 if ( outsect[LSECT].os_size != 0 )
140 fatal("end segment must be empty\n") ;
144 fseek(output,0L,0); /* Start of output file */
145 emits(&outsect[TEXT]) ; /* Write out the text segment */
146 emits(&outsect[ROM]) ; /* Then the rom */
147 emits(&outsect[DATA]) ; /* Then the data */
149 if ( ferror(output) ) {
150 fatal("output write error\n") ;
152 if ( ofile_created ) chmod(argv[2],0755);
155 printf(" text = %ld\n",outsect[TEXT].os_size);
156 printf(" rom = %ld\n",outsect[ROM].os_size);
157 printf(" data = %ld\n",outsect[DATA].os_size);
158 printf(" bss = %ld\n",outsect[BSS].os_size);
159 ss += outsect[TEXT].os_size;
160 ss += outsect[ROM].os_size;
161 ss += outsect[DATA].os_size;
162 ss += outsect[BSS].os_size;
163 printf("Total memory %ld bytes, plus heap and stack\n",ss);
170 * Transfer the emitted byted from one file to another.
171 * and zero fill the uninitialized space
173 emits(section) struct outsect *section ; {
178 n= section->os_flen ;
180 blk = n > BUFSIZ ? BUFSIZ : n;
181 readf(buffer, 1, blk);
182 writef(buffer, 1, blk);
185 if ( section->os_flen!=section->os_size ) {
186 for ( n=BUFSIZ-1 ; n ; n-- ) buffer[n]=0 ;
187 n= section->os_size - section->os_flen ;
189 blk = n > BUFSIZ ? BUFSIZ : n;
190 writef(buffer, 1, blk);
197 rhead(f,head) struct outhead *head ; FILE *f ; {
199 if ( fread(buf,SZ_HEAD,1,f)!=1 ) return 0 ;
200 iconvert(buf,(char *)head,SF_HEAD) ;
204 rsect(f,sect) struct outsect *sect ; FILE *f ; {
206 if ( fread(buf,SZ_SECT,1,f)!=1 ) return 0 ;
207 iconvert(buf,(char *)sect,SF_SECT) ;
211 iconvert(buf,str,fmt) char *buf, *str, *fmt ; {
212 register char *nf, *ni, *no ;
215 ni=buf ; no=str ; nf=fmt ;
216 while ( last = *nf++ ) {
218 if ( last<1 || last >9 ) fatal("illegal out.h format string\n");
222 value = (value<<8) + (ni[i]&0xFF) ;
226 case 1 : *no= value ; break ;
227 case 2 : *(unsigned short *)no = value ; break ;
228 case 4 : *(long *)no = value ; break ;
230 fatal("illegal out.h format string\n");
232 ni += last ; no += last ;
240 fprintf(stderr,"%s: ",program) ;
241 fprintf(stderr, s, a1, a2);