]> git.saurik.com Git - apple/boot.git/blob - i386/nasm/outrdf.c
boot-132.tar.gz
[apple/boot.git] / i386 / nasm / outrdf.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /* outrdf.c output routines for the Netwide Assembler to produce
25 * RDOFF format object files (which are intended mainly
26 * for use in proprietary projects, as the code to load and
27 * execute them is very simple). They will also be used
28 * for device drivers and possibly some executable files
29 * in the MOSCOW operating system. See Rdoff.txt for
30 * details.
31 *
32 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
33 * Julian Hall. All rights reserved. The software is
34 * redistributable under the licence given in the file "Licence"
35 * distributed in the NASM archive.
36 */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <assert.h>
43
44 #include "nasm.h"
45 #include "nasmlib.h"
46 #include "outform.h"
47
48 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
49 #define VERBOSE_WARNINGS
50
51 #ifdef OF_RDF
52
53 typedef short int16; /* not sure if this will be required to be altered
54 at all... best to typedef it just in case */
55
56 static const char *RDOFFId = "RDOFF1"; /* written to start of RDOFF files */
57
58 /* the records that can be found in the RDOFF header */
59
60 /* Note that whenever a segment is referred to in the RDOFF file, its number
61 * is always half of the segment number that NASM uses to refer to it; this
62 * is because NASM only allocates even numbered segments, so as to not
63 * waste any of the 16 bits of segment number written to the file - this
64 * allows up to 65533 external labels to be defined; otherwise it would be
65 * 32764. */
66
67 struct RelocRec {
68 char type; /* must be 1 */
69 char segment; /* only 0 for code, or 1 for data supported,
70 * but add 64 for relative refs (ie do not require
71 * reloc @ loadtime, only linkage) */
72 long offset; /* from start of segment in which reference is loc'd */
73 char length; /* 1 2 or 4 bytes */
74 int16 refseg; /* segment to which reference refers to */
75 };
76
77 struct ImportRec {
78 char type; /* must be 2 */
79 int16 segment; /* segment number allocated to the label for reloc
80 * records - label is assumed to be at offset zero
81 * in this segment, so linker must fix up with offset
82 * of segment and of offset within segment */
83 char label[33]; /* zero terminated... should be written to file until
84 * the zero, but not after it - max len = 32 chars */
85 };
86
87 struct ExportRec {
88 char type; /* must be 3 */
89 char segment; /* segment referred to (0/1) */
90 long offset; /* offset within segment */
91 char label[33]; /* zero terminated as above. max len = 32 chars */
92 };
93
94 struct DLLRec {
95 char type; /* must be 4 */
96 char libname[128]; /* name of library to link with at load time */
97 };
98
99 struct BSSRec {
100 char type; /* must be 5 */
101 long amount; /* number of bytes BSS to reserve */
102 };
103
104 /* code for managing buffers needed to seperate code and data into individual
105 * sections until they are ready to be written to the file.
106 * We'd better hope that it all fits in memory else we're buggered... */
107
108 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
109 * on 80x86 machines for efficiency */
110
111 typedef struct memorybuffer {
112 int length;
113 char buffer[BUF_BLOCK_LEN];
114 struct memorybuffer *next;
115 } memorybuffer;
116
117 static memorybuffer * newmembuf(void){
118 memorybuffer * t;
119
120 t = nasm_malloc(sizeof(memorybuffer));
121
122 t->length = 0;
123 t->next = NULL;
124 return t;
125 }
126
127 static void membufwrite(memorybuffer *b, void *data, int bytes) {
128 int16 w;
129 long l;
130
131 if (b->next) { /* memory buffer full - use next buffer */
132 membufwrite(b->next,data,bytes);
133 return;
134 }
135 if ((bytes < 0 && b->length - bytes > BUF_BLOCK_LEN)
136 || (bytes > 0 && b->length + bytes > BUF_BLOCK_LEN)) {
137
138 /* buffer full and no next allocated... allocate and initialise next
139 * buffer */
140
141 b->next = newmembuf();
142 membufwrite(b->next,data,bytes);
143 return;
144 }
145
146 switch(bytes) {
147 case -4: /* convert to little-endian */
148 l = * (long *) data ;
149 b->buffer[b->length++] = l & 0xFF;
150 l >>= 8 ;
151 b->buffer[b->length++] = l & 0xFF;
152 l >>= 8 ;
153 b->buffer[b->length++] = l & 0xFF;
154 l >>= 8 ;
155 b->buffer[b->length++] = l & 0xFF;
156 break;
157
158 case -2:
159 w = * (int16 *) data ;
160 b->buffer[b->length++] = w & 0xFF;
161 w >>= 8 ;
162 b->buffer[b->length++] = w & 0xFF;
163 break;
164
165 default:
166 while(bytes--) {
167 b->buffer[b->length++] = *(* (unsigned char **) &data);
168
169 (* (unsigned char **) &data)++ ;
170 }
171 break;
172 }
173 }
174
175 static void membufdump(memorybuffer *b,FILE *fp)
176 {
177 if (!b) return;
178
179 fwrite (b->buffer, 1, b->length, fp);
180
181 membufdump(b->next,fp);
182 }
183
184 static int membuflength(memorybuffer *b)
185 {
186 if (!b) return 0;
187 return b->length + membuflength(b->next);
188 }
189
190 static void freemembuf(memorybuffer *b)
191 {
192 if (!b) return;
193 freemembuf(b->next);
194 nasm_free(b);
195 }
196
197 /***********************************************************************
198 * Actual code to deal with RDOFF ouput format begins here...
199 */
200
201 /* global variables set during the initialisation phase */
202
203 static memorybuffer *seg[2]; /* seg 0 = code, seg 1 = data */
204 static memorybuffer *header; /* relocation/import/export records */
205
206 static FILE *ofile;
207
208 static efunc error;
209
210 static int segtext,segdata,segbss;
211 static long bsslength;
212
213 static void rdf_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
214 {
215 ofile = fp;
216 error = errfunc;
217 seg[0] = newmembuf();
218 seg[1] = newmembuf();
219 header = newmembuf();
220 segtext = seg_alloc();
221 segdata = seg_alloc();
222 segbss = seg_alloc();
223 if (segtext != 0 || segdata != 2 || segbss != 4)
224 error(ERR_PANIC,"rdf segment numbers not allocated as expected (%d,%d,%d)",
225 segtext,segdata,segbss);
226 bsslength=0;
227 }
228
229 static long rdf_section_names(char *name, int pass, int *bits)
230 {
231 /*
232 * Default is 32 bits.
233 */
234 if (!name)
235 *bits = 32;
236
237 if (!name) return 0;
238 if (!strcmp(name, ".text")) return 0;
239 else if (!strcmp(name, ".data")) return 2;
240 else if (!strcmp(name, ".bss")) return 4;
241 else
242 return NO_SEG;
243 }
244
245 static void write_reloc_rec(struct RelocRec *r)
246 {
247 if (r->refseg != NO_SEG && (r->refseg & 1))
248 error (ERR_NONFATAL, "RDF format does not support segment base"
249 " references");
250
251 r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
252
253 membufwrite(header,&r->type,1);
254 membufwrite(header,&r->segment,1);
255 membufwrite(header,&r->offset,-4);
256 membufwrite(header,&r->length,1);
257 membufwrite(header,&r->refseg,-2); /* 9 bytes written */
258 }
259
260 static void write_export_rec(struct ExportRec *r)
261 {
262 r->segment >>= 1;
263
264 membufwrite(header,&r->type,1);
265 membufwrite(header,&r->segment,1);
266 membufwrite(header,&r->offset,-4);
267 membufwrite(header,r->label,strlen(r->label) + 1);
268 }
269
270 static void write_import_rec(struct ImportRec *r)
271 {
272 r->segment >>= 1;
273
274 membufwrite(header,&r->type,1);
275 membufwrite(header,&r->segment,-2);
276 membufwrite(header,r->label,strlen(r->label) + 1);
277 }
278
279 static void write_bss_rec(struct BSSRec *r)
280 {
281 membufwrite(header,&r->type,1);
282 membufwrite(header,&r->amount,-4);
283 }
284
285 static void write_dll_rec(struct DLLRec *r)
286 {
287 membufwrite(header,&r->type,1);
288 membufwrite(header,r->libname,strlen(r->libname) + 1);
289 }
290
291 static void rdf_deflabel(char *name, long segment, long offset,
292 int is_global, char *special)
293 {
294 struct ExportRec r;
295 struct ImportRec ri;
296 #ifdef VERBOSE_WARNINGS
297 static int warned_common = 0;
298 #endif
299
300 if (special)
301 error (ERR_NONFATAL, "RDOFF format does not support any"
302 " special symbol types");
303
304 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
305 error (ERR_NONFATAL, "unrecognised special symbol `%s'", name);
306 return;
307 }
308
309 if (is_global == 2) {
310 #ifdef VERBOSE_WARNINGS
311 if (!warned_common) {
312 error(ERR_WARNING,"common declarations not supported: using extern");
313 warned_common = 1;
314 }
315 #endif
316 is_global = 1;
317 }
318
319 if (segment > 4) { /* EXTERN declaration */
320 ri.type = 2;
321 ri.segment = segment;
322 strncpy(ri.label,name,32);
323 ri.label[32] = 0;
324 write_import_rec(&ri);
325 } else if (is_global) {
326 r.type = 3;
327 r.segment = segment;
328 r.offset = offset;
329 strncpy(r.label,name,32);
330 r.label[32] = 0;
331 write_export_rec(&r);
332 }
333 }
334
335 static void rdf_out (long segto, void *data, unsigned long type,
336 long segment, long wrt)
337 {
338 long bytes = type & OUT_SIZMASK;
339 struct RelocRec rr;
340 unsigned char databuf[4],*pd;
341
342 segto >>= 1; /* convert NASM segment no to RDF number */
343
344 if (segto != 0 && segto != 1 && segto != 2) {
345 error(ERR_NONFATAL,"specified segment not supported by rdf output format");
346 return;
347 }
348
349 if (wrt != NO_SEG) {
350 wrt = NO_SEG; /* continue to do _something_ */
351 error (ERR_NONFATAL, "WRT not supported by rdf output format");
352 }
353
354 type &= OUT_TYPMASK;
355
356 if (segto == 2 && type != OUT_RESERVE)
357 {
358 error(ERR_NONFATAL, "BSS segments may not be initialised");
359
360 /* just reserve the space for now... */
361
362 if (type == OUT_REL2ADR)
363 bytes = 2;
364 else
365 bytes = 4;
366 type = OUT_RESERVE;
367 }
368
369 if (type == OUT_RESERVE) {
370 if (segto == 2) /* BSS segment space reserverd */
371 bsslength += bytes;
372 else
373 while (bytes --)
374 membufwrite(seg[segto],databuf,1);
375 }
376 else if (type == OUT_RAWDATA) {
377 if (segment != NO_SEG)
378 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
379 membufwrite(seg[segto],data,bytes);
380 }
381 else if (type == OUT_ADDRESS) {
382
383 /* if segment == NO_SEG then we are writing an address of an
384 object within the same segment - do not produce reloc rec. */
385
386 if (segment != NO_SEG)
387 {
388
389 /* it's an address, so we must write a relocation record */
390
391 rr.type = 1; /* type signature */
392 rr.segment = segto; /* segment we're currently in */
393 rr.offset = membuflength(seg[segto]); /* current offset */
394 rr.length = bytes; /* length of reference */
395 rr.refseg = segment; /* segment referred to */
396 write_reloc_rec(&rr);
397 }
398
399 pd = databuf; /* convert address to little-endian */
400 if (bytes == 2)
401 WRITESHORT (pd, *(long *)data);
402 else
403 WRITELONG (pd, *(long *)data);
404
405 membufwrite(seg[segto],databuf,bytes);
406
407 }
408 else if (type == OUT_REL2ADR)
409 {
410 if (segment == segto)
411 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
412 if (segment != NO_SEG && segment % 2) {
413 error(ERR_NONFATAL, "rdf format does not support segment base refs");
414 }
415
416 rr.type = 1; /* type signature */
417 rr.segment = segto+64; /* segment we're currently in + rel flag */
418 rr.offset = membuflength(seg[segto]); /* current offset */
419 rr.length = 2; /* length of reference */
420 rr.refseg = segment; /* segment referred to */
421 write_reloc_rec(&rr);
422
423 /* work out what to put in the code: offset of the end of this operand,
424 * subtracted from any data specified, so that loader can just add
425 * address of imported symbol onto it to get address relative to end of
426 * instruction: import_address + data(offset) - end_of_instrn */
427
428 rr.offset = *(long *)data -(rr.offset + bytes);
429
430 membufwrite(seg[segto],&rr.offset,-2);
431 }
432 else if (type == OUT_REL4ADR)
433 {
434 if (segment == segto)
435 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
436 if (segment != NO_SEG && segment % 2) {
437 error(ERR_NONFATAL, "rdf format does not support segment base refs");
438 }
439
440 rr.type = 1; /* type signature */
441 rr.segment = segto+64; /* segment we're currently in + rel tag */
442 rr.offset = membuflength(seg[segto]); /* current offset */
443 rr.length = 4; /* length of reference */
444 rr.refseg = segment; /* segment referred to */
445 write_reloc_rec(&rr);
446
447 rr.offset = *(long *)data -(rr.offset + bytes);
448 membufwrite(seg[segto],&rr.offset,-4);
449 }
450 }
451
452 static void rdf_cleanup (void) {
453 long l;
454 unsigned char b[4],*d;
455 struct BSSRec bs;
456
457
458 /* should write imported & exported symbol declarations to header here */
459
460 /* generate the output file... */
461 fwrite(RDOFFId,6,1,ofile); /* file type magic number */
462
463 if (bsslength != 0) /* reserve BSS */
464 {
465 bs.type = 5;
466 bs.amount = bsslength;
467 write_bss_rec(&bs);
468 }
469
470 l = membuflength(header);d=b;
471 WRITELONG(d,l);
472
473 fwrite(b,4,1,ofile); /* write length of header */
474 membufdump(header,ofile); /* dump header */
475
476 l = membuflength(seg[0]);d=b; /* code segment */
477 WRITELONG(d,l);
478
479 fwrite(b,4,1,ofile);
480 membufdump(seg[0],ofile);
481
482 l = membuflength(seg[1]);d=b; /* data segment */
483 WRITELONG(d,l);
484
485 fwrite(b,4,1,ofile);
486 membufdump(seg[1],ofile);
487
488 freemembuf(header);
489 freemembuf(seg[0]);
490 freemembuf(seg[1]);
491 fclose(ofile);
492 }
493
494 static long rdf_segbase (long segment) {
495 return segment;
496 }
497
498 static int rdf_directive (char *directive, char *value, int pass) {
499 struct DLLRec r;
500
501 if (! strcmp(directive, "library")) {
502 if (pass == 1) {
503 r.type = 4;
504 strcpy(r.libname, value);
505 write_dll_rec(&r);
506 }
507 return 1;
508 }
509
510 return 0;
511 }
512
513 static void rdf_filename (char *inname, char *outname, efunc error) {
514 standard_extension(inname,outname,".rdf",error);
515 }
516
517 static char *rdf_stdmac[] = {
518 "%define __SECT__ [section .text]",
519 "%imacro library 1+.nolist",
520 "[library %1]",
521 "%endmacro",
522 NULL
523 };
524
525 struct ofmt of_rdf = {
526 "Relocatable Dynamic Object File Format v1.1",
527 "rdf",
528 rdf_stdmac,
529 rdf_init,
530 rdf_out,
531 rdf_deflabel,
532 rdf_section_names,
533 rdf_segbase,
534 rdf_directive,
535 rdf_filename,
536 rdf_cleanup
537 };
538
539 #endif /* OF_RDF */