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.
25 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
26 #define VERBOSE_WARNINGS
30 typedef short int16
; /* not sure if this will be required to be altered
31 at all... best to typedef it just in case */
33 static const char *RDOFFId
= "RDOFF1"; /* written to start of RDOFF files */
35 /* the records that can be found in the RDOFF header */
37 /* Note that whenever a segment is referred to in the RDOFF file, its number
38 * is always half of the segment number that NASM uses to refer to it; this
39 * is because NASM only allocates even numbered segments, so as to not
40 * waste any of the 16 bits of segment number written to the file - this
41 * allows up to 65533 external labels to be defined; otherwise it would be
45 char type
; /* must be 1 */
46 char segment
; /* only 0 for code, or 1 for data supported,
47 * but add 64 for relative refs (ie do not require
48 * reloc @ loadtime, only linkage) */
49 long offset
; /* from start of segment in which reference is loc'd */
50 char length
; /* 1 2 or 4 bytes */
51 int16 refseg
; /* segment to which reference refers to */
55 char type
; /* must be 2 */
56 int16 segment
; /* segment number allocated to the label for reloc
57 * records - label is assumed to be at offset zero
58 * in this segment, so linker must fix up with offset
59 * of segment and of offset within segment */
60 char label
[33]; /* zero terminated... should be written to file until
61 * the zero, but not after it - max len = 32 chars */
65 char type
; /* must be 3 */
66 char segment
; /* segment referred to (0/1) */
67 long offset
; /* offset within segment */
68 char label
[33]; /* zero terminated as above. max len = 32 chars */
72 char type
; /* must be 4 */
73 char libname
[128]; /* name of library to link with at load time */
77 char type
; /* must be 5 */
78 long amount
; /* number of bytes BSS to reserve */
81 /* code for managing buffers needed to seperate code and data into individual
82 * sections until they are ready to be written to the file.
83 * We'd better hope that it all fits in memory else we're buggered... */
85 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
86 * on 80x86 machines for efficiency */
88 typedef struct memorybuffer
{
90 char buffer
[BUF_BLOCK_LEN
];
91 struct memorybuffer
*next
;
94 static memorybuffer
*newmembuf(void)
98 t
= nasm_malloc(sizeof(memorybuffer
));
105 static void membufwrite(memorybuffer
* b
, void *data
, int bytes
)
110 if (b
->next
) { /* memory buffer full - use next buffer */
111 membufwrite(b
->next
, data
, bytes
);
114 if ((bytes
< 0 && b
->length
- bytes
> BUF_BLOCK_LEN
)
115 || (bytes
> 0 && b
->length
+ bytes
> BUF_BLOCK_LEN
)) {
117 /* buffer full and no next allocated... allocate and initialise next
120 b
->next
= newmembuf();
121 membufwrite(b
->next
, data
, bytes
);
126 case -4: /* convert to little-endian */
128 b
->buffer
[b
->length
++] = l
& 0xFF;
130 b
->buffer
[b
->length
++] = l
& 0xFF;
132 b
->buffer
[b
->length
++] = l
& 0xFF;
134 b
->buffer
[b
->length
++] = l
& 0xFF;
139 b
->buffer
[b
->length
++] = w
& 0xFF;
141 b
->buffer
[b
->length
++] = w
& 0xFF;
146 b
->buffer
[b
->length
++] = *(*(unsigned char **)&data
);
148 (*(unsigned char **)&data
)++;
154 static void membufdump(memorybuffer
* b
, FILE * fp
)
159 fwrite(b
->buffer
, 1, b
->length
, fp
);
161 membufdump(b
->next
, fp
);
164 static int membuflength(memorybuffer
* b
)
168 return b
->length
+ membuflength(b
->next
);
171 static void freemembuf(memorybuffer
* b
)
179 /***********************************************************************
180 * Actual code to deal with RDOFF ouput format begins here...
183 /* global variables set during the initialisation phase */
185 static memorybuffer
*seg
[2]; /* seg 0 = code, seg 1 = data */
186 static memorybuffer
*header
; /* relocation/import/export records */
192 static int segtext
, segdata
, segbss
;
193 static long bsslength
;
195 static void rdf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
199 seg
[0] = newmembuf();
200 seg
[1] = newmembuf();
201 header
= newmembuf();
202 segtext
= seg_alloc();
203 segdata
= seg_alloc();
204 segbss
= seg_alloc();
205 if (segtext
!= 0 || segdata
!= 2 || segbss
!= 4)
207 "rdf segment numbers not allocated as expected (%d,%d,%d)",
208 segtext
, segdata
, segbss
);
212 static long rdf_section_names(char *name
, int pass
, int *bits
)
215 * Default is 32 bits.
222 if (!strcmp(name
, ".text"))
224 else if (!strcmp(name
, ".data"))
226 else if (!strcmp(name
, ".bss"))
232 static void write_reloc_rec(struct RelocRec
*r
)
234 if (r
->refseg
!= NO_SEG
&& (r
->refseg
& 1))
235 error(ERR_NONFATAL
, "RDF format does not support segment base"
238 r
->refseg
>>= 1; /* adjust segment nos to RDF rather than NASM */
240 membufwrite(header
, &r
->type
, 1);
241 membufwrite(header
, &r
->segment
, 1);
242 membufwrite(header
, &r
->offset
, -4);
243 membufwrite(header
, &r
->length
, 1);
244 membufwrite(header
, &r
->refseg
, -2); /* 9 bytes written */
247 static void write_export_rec(struct ExportRec
*r
)
251 membufwrite(header
, &r
->type
, 1);
252 membufwrite(header
, &r
->segment
, 1);
253 membufwrite(header
, &r
->offset
, -4);
254 membufwrite(header
, r
->label
, strlen(r
->label
) + 1);
257 static void write_import_rec(struct ImportRec
*r
)
261 membufwrite(header
, &r
->type
, 1);
262 membufwrite(header
, &r
->segment
, -2);
263 membufwrite(header
, r
->label
, strlen(r
->label
) + 1);
266 static void write_bss_rec(struct BSSRec
*r
)
268 membufwrite(header
, &r
->type
, 1);
269 membufwrite(header
, &r
->amount
, -4);
272 static void write_dll_rec(struct DLLRec
*r
)
274 membufwrite(header
, &r
->type
, 1);
275 membufwrite(header
, r
->libname
, strlen(r
->libname
) + 1);
278 static void rdf_deflabel(char *name
, long segment
, long offset
,
279 int is_global
, char *special
)
283 #ifdef VERBOSE_WARNINGS
284 static int warned_common
= 0;
288 error(ERR_NONFATAL
, "RDOFF format does not support any"
289 " special symbol types");
291 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
292 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
296 if (is_global
== 2) {
297 #ifdef VERBOSE_WARNINGS
298 if (!warned_common
) {
300 "common declarations not supported: using extern");
307 if (segment
> 4) { /* EXTERN declaration */
309 ri
.segment
= segment
;
310 strncpy(ri
.label
, name
, 32);
312 write_import_rec(&ri
);
313 } else if (is_global
) {
317 strncpy(r
.label
, name
, 32);
319 write_export_rec(&r
);
323 static void rdf_out(long segto
, void *data
, unsigned long type
,
324 long segment
, long wrt
)
326 long bytes
= type
& OUT_SIZMASK
;
328 unsigned char databuf
[4], *pd
;
330 if (segto
== NO_SEG
) {
331 if ((type
& OUT_TYPMASK
) != OUT_RESERVE
)
333 "attempt to assemble code in ABSOLUTE space");
337 segto
>>= 1; /* convert NASM segment no to RDF number */
339 if (segto
!= 0 && segto
!= 1 && segto
!= 2) {
341 "specified segment not supported by rdf output format");
346 wrt
= NO_SEG
; /* continue to do _something_ */
347 error(ERR_NONFATAL
, "WRT not supported by rdf output format");
352 if (segto
== 2 && type
!= OUT_RESERVE
) {
353 error(ERR_NONFATAL
, "BSS segments may not be initialised");
355 /* just reserve the space for now... */
357 if (type
== OUT_REL2ADR
)
364 if (type
== OUT_RESERVE
) {
365 if (segto
== 2) /* BSS segment space reserverd */
369 membufwrite(seg
[segto
], databuf
, 1);
370 } else if (type
== OUT_RAWDATA
) {
371 if (segment
!= NO_SEG
)
372 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
373 membufwrite(seg
[segto
], data
, bytes
);
374 } else if (type
== OUT_ADDRESS
) {
376 /* if segment == NO_SEG then we are writing an address of an
377 object within the same segment - do not produce reloc rec. */
379 if (segment
!= NO_SEG
) {
381 /* it's an address, so we must write a relocation record */
383 rr
.type
= 1; /* type signature */
384 rr
.segment
= segto
; /* segment we're currently in */
385 rr
.offset
= membuflength(seg
[segto
]); /* current offset */
386 rr
.length
= bytes
; /* length of reference */
387 rr
.refseg
= segment
; /* segment referred to */
388 write_reloc_rec(&rr
);
391 pd
= databuf
; /* convert address to little-endian */
393 WRITESHORT(pd
, *(long *)data
);
395 WRITELONG(pd
, *(long *)data
);
397 membufwrite(seg
[segto
], databuf
, bytes
);
399 } else if (type
== OUT_REL2ADR
) {
400 if (segment
== segto
)
401 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
402 if (segment
!= NO_SEG
&& segment
% 2) {
404 "rdf format does not support segment base refs");
407 rr
.type
= 1; /* type signature */
408 rr
.segment
= segto
+ 64; /* segment we're currently in + rel flag */
409 rr
.offset
= membuflength(seg
[segto
]); /* current offset */
410 rr
.length
= 2; /* length of reference */
411 rr
.refseg
= segment
; /* segment referred to */
412 write_reloc_rec(&rr
);
414 /* work out what to put in the code: offset of the end of this operand,
415 * subtracted from any data specified, so that loader can just add
416 * address of imported symbol onto it to get address relative to end of
417 * instruction: import_address + data(offset) - end_of_instrn */
419 rr
.offset
= *(long *)data
- (rr
.offset
+ bytes
);
421 membufwrite(seg
[segto
], &rr
.offset
, -2);
422 } else if (type
== OUT_REL4ADR
) {
423 if (segment
== segto
)
424 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
425 if (segment
!= NO_SEG
&& segment
% 2) {
427 "rdf format does not support segment base refs");
430 rr
.type
= 1; /* type signature */
431 rr
.segment
= segto
+ 64; /* segment we're currently in + rel tag */
432 rr
.offset
= membuflength(seg
[segto
]); /* current offset */
433 rr
.length
= 4; /* length of reference */
434 rr
.refseg
= segment
; /* segment referred to */
435 write_reloc_rec(&rr
);
437 rr
.offset
= *(long *)data
- (rr
.offset
+ bytes
);
438 membufwrite(seg
[segto
], &rr
.offset
, -4);
442 static void rdf_cleanup(int debuginfo
)
445 unsigned char b
[4], *d
;
450 /* should write imported & exported symbol declarations to header here */
452 /* generate the output file... */
453 fwrite(RDOFFId
, 6, 1, ofile
); /* file type magic number */
455 if (bsslength
!= 0) { /* reserve BSS */
457 bs
.amount
= bsslength
;
461 l
= membuflength(header
);
465 fwrite(b
, 4, 1, ofile
); /* write length of header */
466 membufdump(header
, ofile
); /* dump header */
468 l
= membuflength(seg
[0]);
469 d
= b
; /* code segment */
472 fwrite(b
, 4, 1, ofile
);
473 membufdump(seg
[0], ofile
);
475 l
= membuflength(seg
[1]);
476 d
= b
; /* data segment */
479 fwrite(b
, 4, 1, ofile
);
480 membufdump(seg
[1], ofile
);
488 static long rdf_segbase(long segment
)
493 static int rdf_directive(char *directive
, char *value
, int pass
)
497 if (!strcmp(directive
, "library")) {
500 strcpy(r
.libname
, value
);
509 static void rdf_filename(char *inname
, char *outname
, efunc error
)
511 standard_extension(inname
, outname
, ".rdf", error
);
514 static char *rdf_stdmac
[] = {
515 "%define __SECT__ [section .text]",
516 "%imacro library 1+.nolist",
519 "%macro __NASM_CDecl__ 1",
524 static int rdf_set_info(enum geninfo type
, char **val
)
529 struct ofmt of_rdf
= {
530 "Relocatable Dynamic Object File Format v1.1",