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 PDP 11 V7 a.out format.
9 * It uses ~em/modules/lib/libobject.a.
34 static char rcs_id[] = "$Id: cv.c,v 1.3 1994/06/24 13:12:05 ceriel Exp $" ;
37 #define ENTRY 0x0 /* entry point */
40 * Header and section table of new format object file.
42 struct outhead outhead;
43 struct outsect outsect[S_MAX];
48 int outputfile_created;
56 /* Output file definitions and such */
69 register struct exec *e = &exec;
73 if ( argc>1 && argv[1][0]=='-' ) {
80 case 3: if ((output = fopen(argv[2], "w")) == NULL) {
81 fatal("Can't write %s.\n", argv[2]);
83 output_file = argv[2];
84 outputfile_created = 1;
87 if (! rd_open(argv[1]))
88 fatal("Can't read %s.\n", argv[1]);
90 default:fatal("Usage: %s <as object> <dl object>.\n", argv[0]);
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++;
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") ;
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);
128 if ( outsect[ROMSG].os_base == 0x0 ) {
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 */
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 */
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") ;
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") ;
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") ;
173 wr_int2(e->a_unused);
175 emits(&outsect[TEXTSG]) ;
176 emits(&outsect[ROMSG]) ;
177 emits(&outsect[DATASG]) ;
179 if ( outputfile_created ) chmod(argv[2],0755);
186 putc((n>>8), output);
193 putc((int)(l >> 16), output);
194 putc((int)(l >> 24), output);
195 putc((int) l, output);
196 putc(((int)l >> 8), output);
201 * Transfer the emitted byted from one file to another.
203 emits(section) struct outsect *section ; {
208 n= section->os_flen ;
209 rd_outsect(section - outsect);
211 blk = n > BUFSIZ ? BUFSIZ : n;
212 rd_emit(buffer, (long) blk);
213 fwrite(buffer, sizeof(char), blk, output);
216 if ((n = section->os_size - section->os_flen) > 0) {
217 for (blk = BUFSIZ - 1; blk >= 0; blk--) {
221 blk = n > BUFSIZ ? BUFSIZ : n;
222 fwrite(buffer, sizeof(char), blk, output);
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;
234 extern char *malloc();
237 long off = OFF_CHAR(outhead);
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) {
250 PDP_name.n_type = 01;
253 PDP_name.n_type = 02;
257 PDP_name.n_type = 02;
262 PDP_name.n_type = 03;
266 PDP_name.n_type = 04;
269 fprintf(stderr,"warning: unknown s_type: %d\n",
270 ACK_name.on_type & S_TYP);
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) {
278 l = ACK_name.on_foff - off;
279 if (l < 0 || l >= outhead.oh_nchar) {
280 fatal("bad on_off: %ld\n",l);
284 for (j = 0; j < 8; j++) {
285 PDP_name.n_name[j] = *p++;
286 if (*p == '\0') break;
288 for (j++; j < 8; j++) {
289 PDP_name.n_name[j] = 0;
291 fwrite((char *) &PDP_name, sizeof(char), 8, output);
292 wr_int2(PDP_name.n_type);
293 wr_int2(PDP_name.n_value);
301 fprintf(stderr,"%s: ",program) ;
302 fprintf(stderr, s, a1, a2);
303 if (outputfile_created)
310 fatal("read error\n");