]>
git.saurik.com Git - apple/boot.git/blob - i386/nasm/outrdf.c
9d271afa77f6fae25b45c6a0970f73f4d7ffd20e
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* outrdf.c output routines for the Netwide Assembler to produce
26 * RDOFF format object files (which are intended mainly
27 * for use in proprietary projects, as the code to load and
28 * execute them is very simple). They will also be used
29 * for device drivers and possibly some executable files
30 * in the MOSCOW operating system. See Rdoff.txt for
33 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
34 * Julian Hall. All rights reserved. The software is
35 * redistributable under the licence given in the file "Licence"
36 * distributed in the NASM archive.
49 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
50 #define VERBOSE_WARNINGS
54 typedef short int16
; /* not sure if this will be required to be altered
55 at all... best to typedef it just in case */
57 static const char *RDOFFId
= "RDOFF1"; /* written to start of RDOFF files */
59 /* the records that can be found in the RDOFF header */
61 /* Note that whenever a segment is referred to in the RDOFF file, its number
62 * is always half of the segment number that NASM uses to refer to it; this
63 * is because NASM only allocates even numbered segments, so as to not
64 * waste any of the 16 bits of segment number written to the file - this
65 * allows up to 65533 external labels to be defined; otherwise it would be
69 char type
; /* must be 1 */
70 char segment
; /* only 0 for code, or 1 for data supported,
71 * but add 64 for relative refs (ie do not require
72 * reloc @ loadtime, only linkage) */
73 long offset
; /* from start of segment in which reference is loc'd */
74 char length
; /* 1 2 or 4 bytes */
75 int16 refseg
; /* segment to which reference refers to */
79 char type
; /* must be 2 */
80 int16 segment
; /* segment number allocated to the label for reloc
81 * records - label is assumed to be at offset zero
82 * in this segment, so linker must fix up with offset
83 * of segment and of offset within segment */
84 char label
[33]; /* zero terminated... should be written to file until
85 * the zero, but not after it - max len = 32 chars */
89 char type
; /* must be 3 */
90 char segment
; /* segment referred to (0/1) */
91 long offset
; /* offset within segment */
92 char label
[33]; /* zero terminated as above. max len = 32 chars */
96 char type
; /* must be 4 */
97 char libname
[128]; /* name of library to link with at load time */
101 char type
; /* must be 5 */
102 long amount
; /* number of bytes BSS to reserve */
105 /* code for managing buffers needed to seperate code and data into individual
106 * sections until they are ready to be written to the file.
107 * We'd better hope that it all fits in memory else we're buggered... */
109 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
110 * on 80x86 machines for efficiency */
112 typedef struct memorybuffer
{
114 char buffer
[BUF_BLOCK_LEN
];
115 struct memorybuffer
*next
;
118 static memorybuffer
* newmembuf(void){
121 t
= nasm_malloc(sizeof(memorybuffer
));
128 static void membufwrite(memorybuffer
*b
, void *data
, int bytes
) {
132 if (b
->next
) { /* memory buffer full - use next buffer */
133 membufwrite(b
->next
,data
,bytes
);
136 if ((bytes
< 0 && b
->length
- bytes
> BUF_BLOCK_LEN
)
137 || (bytes
> 0 && b
->length
+ bytes
> BUF_BLOCK_LEN
)) {
139 /* buffer full and no next allocated... allocate and initialise next
142 b
->next
= newmembuf();
143 membufwrite(b
->next
,data
,bytes
);
148 case -4: /* convert to little-endian */
149 l
= * (long *) data
;
150 b
->buffer
[b
->length
++] = l
& 0xFF;
152 b
->buffer
[b
->length
++] = l
& 0xFF;
154 b
->buffer
[b
->length
++] = l
& 0xFF;
156 b
->buffer
[b
->length
++] = l
& 0xFF;
160 w
= * (int16
*) data
;
161 b
->buffer
[b
->length
++] = w
& 0xFF;
163 b
->buffer
[b
->length
++] = w
& 0xFF;
168 b
->buffer
[b
->length
++] = *(* (unsigned char **) &data
);
170 (* (unsigned char **) &data
)++ ;
176 static void membufdump(memorybuffer
*b
,FILE *fp
)
180 fwrite (b
->buffer
, 1, b
->length
, fp
);
182 membufdump(b
->next
,fp
);
185 static int membuflength(memorybuffer
*b
)
188 return b
->length
+ membuflength(b
->next
);
191 static void freemembuf(memorybuffer
*b
)
198 /***********************************************************************
199 * Actual code to deal with RDOFF ouput format begins here...
202 /* global variables set during the initialisation phase */
204 static memorybuffer
*seg
[2]; /* seg 0 = code, seg 1 = data */
205 static memorybuffer
*header
; /* relocation/import/export records */
211 static int segtext
,segdata
,segbss
;
212 static long bsslength
;
214 static void rdf_init(FILE *fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
218 seg
[0] = newmembuf();
219 seg
[1] = newmembuf();
220 header
= newmembuf();
221 segtext
= seg_alloc();
222 segdata
= seg_alloc();
223 segbss
= seg_alloc();
224 if (segtext
!= 0 || segdata
!= 2 || segbss
!= 4)
225 error(ERR_PANIC
,"rdf segment numbers not allocated as expected (%d,%d,%d)",
226 segtext
,segdata
,segbss
);
230 static long rdf_section_names(char *name
, int pass
, int *bits
)
233 * Default is 32 bits.
239 if (!strcmp(name
, ".text")) return 0;
240 else if (!strcmp(name
, ".data")) return 2;
241 else if (!strcmp(name
, ".bss")) return 4;
246 static void write_reloc_rec(struct RelocRec
*r
)
248 if (r
->refseg
!= NO_SEG
&& (r
->refseg
& 1))
249 error (ERR_NONFATAL
, "RDF format does not support segment base"
252 r
->refseg
>>= 1; /* adjust segment nos to RDF rather than NASM */
254 membufwrite(header
,&r
->type
,1);
255 membufwrite(header
,&r
->segment
,1);
256 membufwrite(header
,&r
->offset
,-4);
257 membufwrite(header
,&r
->length
,1);
258 membufwrite(header
,&r
->refseg
,-2); /* 9 bytes written */
261 static void write_export_rec(struct ExportRec
*r
)
265 membufwrite(header
,&r
->type
,1);
266 membufwrite(header
,&r
->segment
,1);
267 membufwrite(header
,&r
->offset
,-4);
268 membufwrite(header
,r
->label
,strlen(r
->label
) + 1);
271 static void write_import_rec(struct ImportRec
*r
)
275 membufwrite(header
,&r
->type
,1);
276 membufwrite(header
,&r
->segment
,-2);
277 membufwrite(header
,r
->label
,strlen(r
->label
) + 1);
280 static void write_bss_rec(struct BSSRec
*r
)
282 membufwrite(header
,&r
->type
,1);
283 membufwrite(header
,&r
->amount
,-4);
286 static void write_dll_rec(struct DLLRec
*r
)
288 membufwrite(header
,&r
->type
,1);
289 membufwrite(header
,r
->libname
,strlen(r
->libname
) + 1);
292 static void rdf_deflabel(char *name
, long segment
, long offset
,
293 int is_global
, char *special
)
297 #ifdef VERBOSE_WARNINGS
298 static int warned_common
= 0;
302 error (ERR_NONFATAL
, "RDOFF format does not support any"
303 " special symbol types");
305 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
306 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
310 if (is_global
== 2) {
311 #ifdef VERBOSE_WARNINGS
312 if (!warned_common
) {
313 error(ERR_WARNING
,"common declarations not supported: using extern");
320 if (segment
> 4) { /* EXTERN declaration */
322 ri
.segment
= segment
;
323 strncpy(ri
.label
,name
,32);
325 write_import_rec(&ri
);
326 } else if (is_global
) {
330 strncpy(r
.label
,name
,32);
332 write_export_rec(&r
);
336 static void rdf_out (long segto
, void *data
, unsigned long type
,
337 long segment
, long wrt
)
339 long bytes
= type
& OUT_SIZMASK
;
341 unsigned char databuf
[4],*pd
;
343 segto
>>= 1; /* convert NASM segment no to RDF number */
345 if (segto
!= 0 && segto
!= 1 && segto
!= 2) {
346 error(ERR_NONFATAL
,"specified segment not supported by rdf output format");
351 wrt
= NO_SEG
; /* continue to do _something_ */
352 error (ERR_NONFATAL
, "WRT not supported by rdf output format");
357 if (segto
== 2 && type
!= OUT_RESERVE
)
359 error(ERR_NONFATAL
, "BSS segments may not be initialised");
361 /* just reserve the space for now... */
363 if (type
== OUT_REL2ADR
)
370 if (type
== OUT_RESERVE
) {
371 if (segto
== 2) /* BSS segment space reserverd */
375 membufwrite(seg
[segto
],databuf
,1);
377 else if (type
== OUT_RAWDATA
) {
378 if (segment
!= NO_SEG
)
379 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
380 membufwrite(seg
[segto
],data
,bytes
);
382 else if (type
== OUT_ADDRESS
) {
384 /* if segment == NO_SEG then we are writing an address of an
385 object within the same segment - do not produce reloc rec. */
387 if (segment
!= NO_SEG
)
390 /* it's an address, so we must write a relocation record */
392 rr
.type
= 1; /* type signature */
393 rr
.segment
= segto
; /* segment we're currently in */
394 rr
.offset
= membuflength(seg
[segto
]); /* current offset */
395 rr
.length
= bytes
; /* length of reference */
396 rr
.refseg
= segment
; /* segment referred to */
397 write_reloc_rec(&rr
);
400 pd
= databuf
; /* convert address to little-endian */
402 WRITESHORT (pd
, *(long *)data
);
404 WRITELONG (pd
, *(long *)data
);
406 membufwrite(seg
[segto
],databuf
,bytes
);
409 else if (type
== OUT_REL2ADR
)
411 if (segment
== segto
)
412 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
413 if (segment
!= NO_SEG
&& segment
% 2) {
414 error(ERR_NONFATAL
, "rdf format does not support segment base refs");
417 rr
.type
= 1; /* type signature */
418 rr
.segment
= segto
+64; /* segment we're currently in + rel flag */
419 rr
.offset
= membuflength(seg
[segto
]); /* current offset */
420 rr
.length
= 2; /* length of reference */
421 rr
.refseg
= segment
; /* segment referred to */
422 write_reloc_rec(&rr
);
424 /* work out what to put in the code: offset of the end of this operand,
425 * subtracted from any data specified, so that loader can just add
426 * address of imported symbol onto it to get address relative to end of
427 * instruction: import_address + data(offset) - end_of_instrn */
429 rr
.offset
= *(long *)data
-(rr
.offset
+ bytes
);
431 membufwrite(seg
[segto
],&rr
.offset
,-2);
433 else if (type
== OUT_REL4ADR
)
435 if (segment
== segto
)
436 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
437 if (segment
!= NO_SEG
&& segment
% 2) {
438 error(ERR_NONFATAL
, "rdf format does not support segment base refs");
441 rr
.type
= 1; /* type signature */
442 rr
.segment
= segto
+64; /* segment we're currently in + rel tag */
443 rr
.offset
= membuflength(seg
[segto
]); /* current offset */
444 rr
.length
= 4; /* length of reference */
445 rr
.refseg
= segment
; /* segment referred to */
446 write_reloc_rec(&rr
);
448 rr
.offset
= *(long *)data
-(rr
.offset
+ bytes
);
449 membufwrite(seg
[segto
],&rr
.offset
,-4);
453 static void rdf_cleanup (void) {
455 unsigned char b
[4],*d
;
459 /* should write imported & exported symbol declarations to header here */
461 /* generate the output file... */
462 fwrite(RDOFFId
,6,1,ofile
); /* file type magic number */
464 if (bsslength
!= 0) /* reserve BSS */
467 bs
.amount
= bsslength
;
471 l
= membuflength(header
);d
=b
;
474 fwrite(b
,4,1,ofile
); /* write length of header */
475 membufdump(header
,ofile
); /* dump header */
477 l
= membuflength(seg
[0]);d
=b
; /* code segment */
481 membufdump(seg
[0],ofile
);
483 l
= membuflength(seg
[1]);d
=b
; /* data segment */
487 membufdump(seg
[1],ofile
);
495 static long rdf_segbase (long segment
) {
499 static int rdf_directive (char *directive
, char *value
, int pass
) {
502 if (! strcmp(directive
, "library")) {
505 strcpy(r
.libname
, value
);
514 static void rdf_filename (char *inname
, char *outname
, efunc error
) {
515 standard_extension(inname
,outname
,".rdf",error
);
518 static char *rdf_stdmac
[] = {
519 "%define __SECT__ [section .text]",
520 "%imacro library 1+.nolist",
526 struct ofmt of_rdf
= {
527 "Relocatable Dynamic Object File Format v1.1",