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 PC/IX a.out format.
9 * It uses ~em/modules/lib/libobject.a.
14 /* unsigned char: not portable */
22 unsigned short a_version;
36 unsigned short n_type;
42 static char rcs_id[] = "$Id: cv.c,v 1.7 1994/06/24 12:58:44 ceriel Exp $" ;
45 #define ENTRY 0x0 /* entry point */
48 * Header and section table of new format object file.
50 struct outhead outhead;
51 struct outsect outsect[S_MAX];
56 int outputfile_created;
65 /* Output file definitions and such */
83 register struct outsect *pa, *pb;
85 /* return 1 if pa follows pb */
87 return pa->os_base == align(pb->os_base+pb->os_size, pa->os_lign);
94 register struct exec *e = &exec;
98 if ( argc>1 && argv[1][0]=='-' ) {
103 case 1: rd_fdopen(0);
105 case 3: if ((output = fopen(argv[2], "w")) == 0) {
106 fatal("Can't write %s.\n", argv[2]);
108 output_file = argv[2];
109 outputfile_created = 1;
112 if (! rd_open(argv[1]))
113 fatal("Can't read %s.\n", argv[1]);
115 default:fatal("Usage: %s <as object> <dl object>.\n", argv[0]);
118 if (BADMAGIC(outhead))
119 fatal("Not an ack object file.\n");
120 if (outhead.oh_flags & HF_LINK)
121 fatal("Contains unresolved references.\n");
122 if (outhead.oh_nrelo > 0)
123 fprintf(stderr, "Warning: relocation information present.\n");
124 if ( outhead.oh_nsect!=LSECT && outhead.oh_nsect!=NSECT )
125 fatal("Input file must have %d sections, not %ld\n",
126 NSECT,outhead.oh_nsect) ;
127 rd_sect(outsect, outhead.oh_nsect);
129 if ( outsect[TEXTSG].os_base != ENTRY)
130 fatal("text must start at %d not at 0x%lx\n", ENTRY,
131 outsect[TEXTSG].os_base) ;
132 if ( outsect[BSSSG].os_flen != 0 )
133 fatal("bss space contains initialized data\n") ;
134 if (! follows(&outsect[BSSSG], &outsect[DATASG]))
135 fatal("bss segment must follow data segment\n") ;
136 if (! follows(&outsect[DATASG], &outsect[ROMSG]))
137 fatal("data segment must follow rom\n") ;
139 outsect[ROMSG].os_size = outsect[DATASG].os_base - outsect[ROMSG].os_base;
140 outsect[DATASG].os_size = outsect[BSSSG].os_base - outsect[DATASG].os_base;
145 e->a_data = outsect[BSSSG].os_base - outsect[ROMSG].os_base;
146 e->a_bss = outsect[BSSSG].os_size;
147 e->a_entry = outsect[TEXTSG].os_base;
148 e->a_syms = (long) outhead.oh_nname * sizeof (struct nlist);
150 if ( outsect[ROMSG].os_base == 0x0 ) {
153 outsect[TEXTSG].os_size = e->a_text =
154 align(outsect[TEXTSG].os_size,16L);
157 outsect[TEXTSG].os_size = e->a_text =
158 outsect[ROMSG].os_base - outsect[TEXTSG].os_base;
159 if (! follows(&outsect[ROMSG], &outsect[TEXTSG]))
160 fatal("rom segment must follow text\n") ;
162 if ( outhead.oh_nsect==NSECT ) {
163 if (! follows(&outsect[LSECT], &outsect[BSSSG]))
164 fatal("end segment must follow bss\n") ;
165 if ( outsect[LSECT].os_size != 0 )
166 fatal("end segment must be empty\n") ;
169 if (((unsigned) outhead.oh_nchar != outhead.oh_nchar) ||
170 (outhead.oh_nchar != 0 &&
171 (chars = malloc((unsigned)outhead.oh_nchar)) == 0)) {
172 fprintf(stderr, "%s: (warning) No name list generated\n", program);
177 fwrite((char *) e, 1, 6, output);
178 wr_int2((int) e->a_version);
185 emits(&outsect[TEXTSG]) ;
186 emits(&outsect[ROMSG]) ;
187 emits(&outsect[DATASG]) ;
188 if (e->a_syms) emit_symtab();
190 if ( outputfile_created ) chmod(argv[2],0755);
201 fwrite(buf, 1, 2, output);
213 fwrite(buf, 1, 4, output);
217 * Transfer the emitted byted from one file to another.
219 emits(section) struct outsect *section ; {
224 n= section->os_flen ;
225 rd_outsect(section - outsect);
227 blk = n > BUFSIZ ? BUFSIZ : n;
228 rd_emit(buffer, (long) blk);
229 fwrite(buffer, 1, blk, output);
232 if ((n = section->os_size - section->os_flen) > 0) {
233 for (blk = BUFSIZ - 1; blk >= 0; blk--) {
237 blk = n > BUFSIZ ? BUFSIZ : n;
238 fwrite(buffer, 1, blk, output);
246 struct outname ACK_name; /* symbol table entry in ACK format */
247 struct nlist IX_name; /* symbol table entry in PC/IX format */
248 register unsigned short i;
251 long off = OFF_CHAR(outhead);
255 rd_string(chars,outhead.oh_nchar);
256 for (i = 0; i < outhead.oh_nname; i++) {
257 rd_name(&ACK_name, 1);
258 switch(ACK_name.on_type & S_TYP) {
260 IX_name.n_sclass = 0;
263 IX_name.n_sclass = 01;
266 IX_name.n_sclass = 02;
270 IX_name.n_sclass = 03;
274 IX_name.n_sclass = 04;
277 fprintf(stderr,"warning: unknown s_type: %d\n",
278 ACK_name.on_type & S_TYP);
280 if (ACK_name.on_type & S_EXT) IX_name.n_sclass |= 020;
281 IX_name.n_value = ACK_name.on_valu;
282 if (ACK_name.on_foff == 0) {
286 l = ACK_name.on_foff - off;
287 if (l < 0 || l >= outhead.oh_nchar) {
288 fatal("bad on_off: %ld\n",l);
292 for (j = 0; j < 8; j++) {
293 IX_name.n_name[j] = *p++;
294 if (*p == '\0') break;
296 for (j++; j < 8; j++) {
297 IX_name.n_name[j] = 0;
299 fwrite((char *) &IX_name, 1, 8, output);
300 wr_long(IX_name.n_value);
301 fwrite(&(IX_name.n_sclass), 1, 2, output);
302 wr_int2((int) IX_name.n_type);
310 fprintf(stderr,"%s: ",program) ;
311 fprintf(stderr, s, a1, a2);
312 if (outputfile_created) {
321 fatal("read error\n");