1 /* outrdf.c output routines for the Netwide Assembler to produce
2 * RDOFF format object files (which are intended mainly
3 * for use in proprietary projects, as the code to load and
4 * execute them is very simple). They will also be used
5 * for device drivers and possibly some executable files
6 * in the MOSCOW operating system. See Rdoff.txt for
9 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
10 * Julian Hall. All rights reserved. The software is
11 * redistributable under the licence given in the file "Licence"
12 * distributed in the NASM archive.
27 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
28 #define VERBOSE_WARNINGS
32 typedef int16_t int16
; /* not sure if this will be required to be altered
33 at all... best to typedef it just in case */
35 static const char *RDOFFId
= "RDOFF1"; /* written to start of RDOFF files */
37 /* the records that can be found in the RDOFF header */
39 /* Note that whenever a segment is referred to in the RDOFF file, its number
40 * is always half of the segment number that NASM uses to refer to it; this
41 * is because NASM only allocates even numbered segments, so as to not
42 * waste any of the 16 bits of segment number written to the file - this
43 * allows up to 65533 external labels to be defined; otherwise it would be
47 char type
; /* must be 1 */
48 char segment
; /* only 0 for code, or 1 for data supported,
49 * but add 64 for relative refs (ie do not require
50 * reloc @ loadtime, only linkage) */
51 int32_t offset
; /* from start of segment in which reference is loc'd */
52 char length
; /* 1 2 or 4 bytes */
53 int16 refseg
; /* segment to which reference refers to */
57 char type
; /* must be 2 */
58 int16 segment
; /* segment number allocated to the label for reloc
59 * records - label is assumed to be at offset zero
60 * in this segment, so linker must fix up with offset
61 * of segment and of offset within segment */
62 char label
[33]; /* zero terminated... should be written to file until
63 * the zero, but not after it - max len = 32 chars */
67 char type
; /* must be 3 */
68 char segment
; /* segment referred to (0/1) */
69 int32_t offset
; /* offset within segment */
70 char label
[33]; /* zero terminated as above. max len = 32 chars */
74 char type
; /* must be 4 */
75 char libname
[128]; /* name of library to link with at load time */
79 char type
; /* must be 5 */
80 int32_t amount
; /* number of bytes BSS to reserve */
83 /* code for managing buffers needed to seperate code and data into individual
84 * sections until they are ready to be written to the file.
85 * We'd better hope that it all fits in memory else we're buggered... */
87 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
88 * on 80x86 machines for efficiency */
90 typedef struct memorybuffer
{
92 char buffer
[BUF_BLOCK_LEN
];
93 struct memorybuffer
*next
;
96 static memorybuffer
*newmembuf(void)
100 t
= nasm_malloc(sizeof(memorybuffer
));
107 static void membufwrite(memorybuffer
* b
, void *data
, int bytes
)
112 if (b
->next
) { /* memory buffer full - use next buffer */
113 membufwrite(b
->next
, data
, bytes
);
116 if ((bytes
< 0 && b
->length
- bytes
> BUF_BLOCK_LEN
)
117 || (bytes
> 0 && b
->length
+ bytes
> BUF_BLOCK_LEN
)) {
119 /* buffer full and no next allocated... allocate and initialize next
122 b
->next
= newmembuf();
123 membufwrite(b
->next
, data
, bytes
);
128 case -4: /* convert to little-endian */
129 l
= *(int32_t *)data
;
130 b
->buffer
[b
->length
++] = l
& 0xFF;
132 b
->buffer
[b
->length
++] = l
& 0xFF;
134 b
->buffer
[b
->length
++] = l
& 0xFF;
136 b
->buffer
[b
->length
++] = l
& 0xFF;
141 b
->buffer
[b
->length
++] = w
& 0xFF;
143 b
->buffer
[b
->length
++] = w
& 0xFF;
148 b
->buffer
[b
->length
++] = *(*(uint8_t **)&data
);
150 (*(uint8_t **)&data
)++;
156 static void membufdump(memorybuffer
* b
, FILE * fp
)
161 fwrite(b
->buffer
, 1, b
->length
, fp
);
163 membufdump(b
->next
, fp
);
166 static int membuflength(memorybuffer
* b
)
170 return b
->length
+ membuflength(b
->next
);
173 static void freemembuf(memorybuffer
* b
)
181 /***********************************************************************
182 * Actual code to deal with RDOFF ouput format begins here...
185 /* global variables set during the initialisation phase */
187 static memorybuffer
*seg
[2]; /* seg 0 = code, seg 1 = data */
188 static memorybuffer
*header
; /* relocation/import/export records */
194 static int segtext
, segdata
, segbss
;
195 static int32_t bsslength
;
197 static void rdf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
202 seg
[0] = newmembuf();
203 seg
[1] = newmembuf();
204 header
= newmembuf();
205 segtext
= seg_alloc();
206 segdata
= seg_alloc();
207 segbss
= seg_alloc();
208 if (segtext
!= 0 || segdata
!= 2 || segbss
!= 4)
210 "rdf segment numbers not allocated as expected (%d,%d,%d)",
211 segtext
, segdata
, segbss
);
215 static int32_t rdf_section_names(char *name
, int pass
, int *bits
)
218 * Default is 32 bits.
225 if (!strcmp(name
, ".text"))
227 else if (!strcmp(name
, ".data"))
229 else if (!strcmp(name
, ".bss"))
235 static void write_reloc_rec(struct RelocRec
*r
)
237 if (r
->refseg
!= NO_SEG
&& (r
->refseg
& 1))
238 error(ERR_NONFATAL
, "RDF format does not support segment base"
241 r
->refseg
>>= 1; /* adjust segment nos to RDF rather than NASM */
243 membufwrite(header
, &r
->type
, 1);
244 membufwrite(header
, &r
->segment
, 1);
245 membufwrite(header
, &r
->offset
, -4);
246 membufwrite(header
, &r
->length
, 1);
247 membufwrite(header
, &r
->refseg
, -2); /* 9 bytes written */
250 static void write_export_rec(struct ExportRec
*r
)
254 membufwrite(header
, &r
->type
, 1);
255 membufwrite(header
, &r
->segment
, 1);
256 membufwrite(header
, &r
->offset
, -4);
257 membufwrite(header
, r
->label
, strlen(r
->label
) + 1);
260 static void write_import_rec(struct ImportRec
*r
)
264 membufwrite(header
, &r
->type
, 1);
265 membufwrite(header
, &r
->segment
, -2);
266 membufwrite(header
, r
->label
, strlen(r
->label
) + 1);
269 static void write_bss_rec(struct BSSRec
*r
)
271 membufwrite(header
, &r
->type
, 1);
272 membufwrite(header
, &r
->amount
, -4);
275 static void write_dll_rec(struct DLLRec
*r
)
277 membufwrite(header
, &r
->type
, 1);
278 membufwrite(header
, r
->libname
, strlen(r
->libname
) + 1);
281 static void rdf_deflabel(char *name
, int32_t segment
, int32_t offset
,
282 int is_global
, char *special
)
286 #ifdef VERBOSE_WARNINGS
287 static int warned_common
= 0;
291 error(ERR_NONFATAL
, "RDOFF format does not support any"
292 " special symbol types");
294 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
295 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
299 if (is_global
== 2) {
300 #ifdef VERBOSE_WARNINGS
301 if (!warned_common
) {
303 "common declarations not supported: using extern");
310 if (segment
> 4) { /* EXTERN declaration */
312 ri
.segment
= segment
;
313 strncpy(ri
.label
, name
, 32);
315 write_import_rec(&ri
);
316 } else if (is_global
) {
320 strncpy(r
.label
, name
, 32);
322 write_export_rec(&r
);
326 static void rdf_out(int32_t segto
, void *data
, uint32_t type
,
327 int32_t segment
, int32_t wrt
)
329 int32_t bytes
= type
& OUT_SIZMASK
;
331 uint8_t databuf
[8], *pd
;
333 if (segto
== NO_SEG
) {
334 if ((type
& OUT_TYPMASK
) != OUT_RESERVE
)
336 "attempt to assemble code in ABSOLUTE space");
340 segto
>>= 1; /* convert NASM segment no to RDF number */
342 if (segto
!= 0 && segto
!= 1 && segto
!= 2) {
344 "specified segment not supported by rdf output format");
349 wrt
= NO_SEG
; /* continue to do _something_ */
350 error(ERR_NONFATAL
, "WRT not supported by rdf output format");
355 if (segto
== 2 && type
!= OUT_RESERVE
) {
356 error(ERR_NONFATAL
, "BSS segments may not be initialized");
358 /* just reserve the space for now... */
360 if (type
== OUT_REL2ADR
)
367 if (type
== OUT_RESERVE
) {
368 if (segto
== 2) /* BSS segment space reserverd */
372 membufwrite(seg
[segto
], databuf
, 1);
373 } else if (type
== OUT_RAWDATA
) {
374 if (segment
!= NO_SEG
)
375 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
376 membufwrite(seg
[segto
], data
, bytes
);
377 } else if (type
== OUT_ADDRESS
) {
379 /* if segment == NO_SEG then we are writing an address of an
380 object within the same segment - do not produce reloc rec. */
382 if (segment
!= NO_SEG
) {
384 /* it's an address, so we must write a relocation record */
386 rr
.type
= 1; /* type signature */
387 rr
.segment
= segto
; /* segment we're currently in */
388 rr
.offset
= membuflength(seg
[segto
]); /* current offset */
389 rr
.length
= bytes
; /* length of reference */
390 rr
.refseg
= segment
; /* segment referred to */
391 write_reloc_rec(&rr
);
394 pd
= databuf
; /* convert address to little-endian */
395 WRITEADDR(pd
, *(int64_t *)data
, bytes
);
396 membufwrite(seg
[segto
], databuf
, bytes
);
397 } else if (type
== OUT_REL2ADR
) {
398 if (segment
== segto
)
399 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
400 if (segment
!= NO_SEG
&& segment
% 2) {
402 "rdf format does not support segment base refs");
405 rr
.type
= 1; /* type signature */
406 rr
.segment
= segto
+ 64; /* segment we're currently in + rel flag */
407 rr
.offset
= membuflength(seg
[segto
]); /* current offset */
408 rr
.length
= 2; /* length of reference */
409 rr
.refseg
= segment
; /* segment referred to */
410 write_reloc_rec(&rr
);
412 /* work out what to put in the code: offset of the end of this operand,
413 * subtracted from any data specified, so that loader can just add
414 * address of imported symbol onto it to get address relative to end of
415 * instruction: import_address + data(offset) - end_of_instrn */
417 rr
.offset
= *(int64_t *)data
- (rr
.offset
+ bytes
);
419 membufwrite(seg
[segto
], &rr
.offset
, -2);
420 } else if (type
== OUT_REL4ADR
) {
421 if ((segment
== segto
) && (globalbits
!= 64))
422 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
423 if (segment
!= NO_SEG
&& segment
% 2) {
425 "rdf format does not support segment base refs");
428 rr
.type
= 1; /* type signature */
429 rr
.segment
= segto
+ 64; /* segment we're currently in + rel tag */
430 rr
.offset
= membuflength(seg
[segto
]); /* current offset */
431 rr
.length
= 4; /* length of reference */
432 rr
.refseg
= segment
; /* segment referred to */
433 write_reloc_rec(&rr
);
435 rr
.offset
= *(int64_t *)data
- (rr
.offset
+ bytes
);
436 membufwrite(seg
[segto
], &rr
.offset
, -4);
440 static void rdf_cleanup(int debuginfo
)
448 /* should write imported & exported symbol declarations to header here */
450 /* generate the output file... */
451 fwrite(RDOFFId
, 6, 1, ofile
); /* file type magic number */
453 if (bsslength
!= 0) { /* reserve BSS */
455 bs
.amount
= bsslength
;
459 l
= membuflength(header
);
463 fwrite(b
, 4, 1, ofile
); /* write length of header */
464 membufdump(header
, ofile
); /* dump header */
466 l
= membuflength(seg
[0]);
467 d
= b
; /* code segment */
470 fwrite(b
, 4, 1, ofile
);
471 membufdump(seg
[0], ofile
);
473 l
= membuflength(seg
[1]);
474 d
= b
; /* data segment */
477 fwrite(b
, 4, 1, ofile
);
478 membufdump(seg
[1], ofile
);
486 static int32_t rdf_segbase(int32_t segment
)
491 static int rdf_directive(char *directive
, char *value
, int pass
)
495 if (!strcmp(directive
, "library")) {
498 strcpy(r
.libname
, value
);
507 static void rdf_filename(char *inname
, char *outname
, efunc error
)
509 standard_extension(inname
, outname
, ".rdf", error
);
512 static char *rdf_stdmac
[] = {
513 "%define __SECT__ [section .text]",
514 "%imacro library 1+.nolist",
517 "%macro __NASM_CDecl__ 1",
522 static int rdf_set_info(enum geninfo type
, char **val
)
527 struct ofmt of_rdf
= {
528 "Relocatable Dynamic Object File Format v1.1",