]>
git.saurik.com Git - apple/boot.git/blob - i386/nasm/outrdf.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
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
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.
48 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
49 #define VERBOSE_WARNINGS
53 typedef short int16
; /* not sure if this will be required to be altered
54 at all... best to typedef it just in case */
56 static const char *RDOFFId
= "RDOFF1"; /* written to start of RDOFF files */
58 /* the records that can be found in the RDOFF header */
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
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 */
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 */
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 */
95 char type
; /* must be 4 */
96 char libname
[128]; /* name of library to link with at load time */
100 char type
; /* must be 5 */
101 long amount
; /* number of bytes BSS to reserve */
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... */
108 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
109 * on 80x86 machines for efficiency */
111 typedef struct memorybuffer
{
113 char buffer
[BUF_BLOCK_LEN
];
114 struct memorybuffer
*next
;
117 static memorybuffer
* newmembuf(void){
120 t
= nasm_malloc(sizeof(memorybuffer
));
127 static void membufwrite(memorybuffer
*b
, void *data
, int bytes
) {
131 if (b
->next
) { /* memory buffer full - use next buffer */
132 membufwrite(b
->next
,data
,bytes
);
135 if ((bytes
< 0 && b
->length
- bytes
> BUF_BLOCK_LEN
)
136 || (bytes
> 0 && b
->length
+ bytes
> BUF_BLOCK_LEN
)) {
138 /* buffer full and no next allocated... allocate and initialise next
141 b
->next
= newmembuf();
142 membufwrite(b
->next
,data
,bytes
);
147 case -4: /* convert to little-endian */
148 l
= * (long *) data
;
149 b
->buffer
[b
->length
++] = l
& 0xFF;
151 b
->buffer
[b
->length
++] = l
& 0xFF;
153 b
->buffer
[b
->length
++] = l
& 0xFF;
155 b
->buffer
[b
->length
++] = l
& 0xFF;
159 w
= * (int16
*) data
;
160 b
->buffer
[b
->length
++] = w
& 0xFF;
162 b
->buffer
[b
->length
++] = w
& 0xFF;
167 b
->buffer
[b
->length
++] = *(* (unsigned char **) &data
);
169 (* (unsigned char **) &data
)++ ;
175 static void membufdump(memorybuffer
*b
,FILE *fp
)
179 fwrite (b
->buffer
, 1, b
->length
, fp
);
181 membufdump(b
->next
,fp
);
184 static int membuflength(memorybuffer
*b
)
187 return b
->length
+ membuflength(b
->next
);
190 static void freemembuf(memorybuffer
*b
)
197 /***********************************************************************
198 * Actual code to deal with RDOFF ouput format begins here...
201 /* global variables set during the initialisation phase */
203 static memorybuffer
*seg
[2]; /* seg 0 = code, seg 1 = data */
204 static memorybuffer
*header
; /* relocation/import/export records */
210 static int segtext
,segdata
,segbss
;
211 static long bsslength
;
213 static void rdf_init(FILE *fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
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
);
229 static long rdf_section_names(char *name
, int pass
, int *bits
)
232 * Default is 32 bits.
238 if (!strcmp(name
, ".text")) return 0;
239 else if (!strcmp(name
, ".data")) return 2;
240 else if (!strcmp(name
, ".bss")) return 4;
245 static void write_reloc_rec(struct RelocRec
*r
)
247 if (r
->refseg
!= NO_SEG
&& (r
->refseg
& 1))
248 error (ERR_NONFATAL
, "RDF format does not support segment base"
251 r
->refseg
>>= 1; /* adjust segment nos to RDF rather than NASM */
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 */
260 static void write_export_rec(struct ExportRec
*r
)
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);
270 static void write_import_rec(struct ImportRec
*r
)
274 membufwrite(header
,&r
->type
,1);
275 membufwrite(header
,&r
->segment
,-2);
276 membufwrite(header
,r
->label
,strlen(r
->label
) + 1);
279 static void write_bss_rec(struct BSSRec
*r
)
281 membufwrite(header
,&r
->type
,1);
282 membufwrite(header
,&r
->amount
,-4);
285 static void write_dll_rec(struct DLLRec
*r
)
287 membufwrite(header
,&r
->type
,1);
288 membufwrite(header
,r
->libname
,strlen(r
->libname
) + 1);
291 static void rdf_deflabel(char *name
, long segment
, long offset
,
292 int is_global
, char *special
)
296 #ifdef VERBOSE_WARNINGS
297 static int warned_common
= 0;
301 error (ERR_NONFATAL
, "RDOFF format does not support any"
302 " special symbol types");
304 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
305 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
309 if (is_global
== 2) {
310 #ifdef VERBOSE_WARNINGS
311 if (!warned_common
) {
312 error(ERR_WARNING
,"common declarations not supported: using extern");
319 if (segment
> 4) { /* EXTERN declaration */
321 ri
.segment
= segment
;
322 strncpy(ri
.label
,name
,32);
324 write_import_rec(&ri
);
325 } else if (is_global
) {
329 strncpy(r
.label
,name
,32);
331 write_export_rec(&r
);
335 static void rdf_out (long segto
, void *data
, unsigned long type
,
336 long segment
, long wrt
)
338 long bytes
= type
& OUT_SIZMASK
;
340 unsigned char databuf
[4],*pd
;
342 segto
>>= 1; /* convert NASM segment no to RDF number */
344 if (segto
!= 0 && segto
!= 1 && segto
!= 2) {
345 error(ERR_NONFATAL
,"specified segment not supported by rdf output format");
350 wrt
= NO_SEG
; /* continue to do _something_ */
351 error (ERR_NONFATAL
, "WRT not supported by rdf output format");
356 if (segto
== 2 && type
!= OUT_RESERVE
)
358 error(ERR_NONFATAL
, "BSS segments may not be initialised");
360 /* just reserve the space for now... */
362 if (type
== OUT_REL2ADR
)
369 if (type
== OUT_RESERVE
) {
370 if (segto
== 2) /* BSS segment space reserverd */
374 membufwrite(seg
[segto
],databuf
,1);
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
);
381 else if (type
== OUT_ADDRESS
) {
383 /* if segment == NO_SEG then we are writing an address of an
384 object within the same segment - do not produce reloc rec. */
386 if (segment
!= NO_SEG
)
389 /* it's an address, so we must write a relocation record */
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
);
399 pd
= databuf
; /* convert address to little-endian */
401 WRITESHORT (pd
, *(long *)data
);
403 WRITELONG (pd
, *(long *)data
);
405 membufwrite(seg
[segto
],databuf
,bytes
);
408 else if (type
== OUT_REL2ADR
)
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");
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
);
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 */
428 rr
.offset
= *(long *)data
-(rr
.offset
+ bytes
);
430 membufwrite(seg
[segto
],&rr
.offset
,-2);
432 else if (type
== OUT_REL4ADR
)
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");
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
);
447 rr
.offset
= *(long *)data
-(rr
.offset
+ bytes
);
448 membufwrite(seg
[segto
],&rr
.offset
,-4);
452 static void rdf_cleanup (void) {
454 unsigned char b
[4],*d
;
458 /* should write imported & exported symbol declarations to header here */
460 /* generate the output file... */
461 fwrite(RDOFFId
,6,1,ofile
); /* file type magic number */
463 if (bsslength
!= 0) /* reserve BSS */
466 bs
.amount
= bsslength
;
470 l
= membuflength(header
);d
=b
;
473 fwrite(b
,4,1,ofile
); /* write length of header */
474 membufdump(header
,ofile
); /* dump header */
476 l
= membuflength(seg
[0]);d
=b
; /* code segment */
480 membufdump(seg
[0],ofile
);
482 l
= membuflength(seg
[1]);d
=b
; /* data segment */
486 membufdump(seg
[1],ofile
);
494 static long rdf_segbase (long segment
) {
498 static int rdf_directive (char *directive
, char *value
, int pass
) {
501 if (! strcmp(directive
, "library")) {
504 strcpy(r
.libname
, value
);
513 static void rdf_filename (char *inname
, char *outname
, efunc error
) {
514 standard_extension(inname
,outname
,".rdf",error
);
517 static char *rdf_stdmac
[] = {
518 "%define __SECT__ [section .text]",
519 "%imacro library 1+.nolist",
525 struct ofmt of_rdf
= {
526 "Relocatable Dynamic Object File Format v1.1",