2 * outrdf2.c output routines for the Netwide Assembler to produce
3 * RDOFF version 2 format object files, which is used as a
4 * main binary format in the RadiOS (http://radios.sf.net).
5 * Originally Julian planned to use it in his MOSCOW
8 * The Netwide Assembler is copyright (C) 1996-1998 Simon Tatham and
9 * Julian Hall. All rights reserved. The software is
10 * redistributable under the licence given in the file "Licence"
11 * distributed in the NASM archive.
24 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
25 #define VERBOSE_WARNINGS
29 #include "rdoff/rdoff.h"
31 /* This signature is written to start of RDOFF files */
32 static const char *RDOFF2Id
= RDOFF2_SIGNATURE
;
34 /* Note that whenever a segment is referred to in the RDOFF file, its number
35 * is always half of the segment number that NASM uses to refer to it; this
36 * is because NASM only allocates even numbered segments, so as to not
37 * waste any of the 16 bits of segment number written to the file - this
38 * allows up to 65533 external labels to be defined; otherwise it would be
41 #define COUNT_SEGTYPES 9
43 static char *segmenttypes
[COUNT_SEGTYPES
] = {
44 "null", "text", "code", "data",
45 "comment", "lcomment", "pcomment",
46 "symdebug", "linedebug"
49 static int segmenttypenumbers
[COUNT_SEGTYPES
] = {
50 0, 1, 1, 2, 3, 4, 5, 6, 7
53 /* code for managing buffers needed to separate code and data into individual
54 * sections until they are ready to be written to the file.
55 * We'd better hope that it all fits in memory else we're buggered... */
57 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
58 * on 80x86 machines for efficiency */
60 /***********************************************************************
61 * Actual code to deal with RDOFF2 ouput format begins here...
64 /* global variables set during the initialisation phase */
66 static struct SAA
*seg
[RDF_MAXSEGS
]; /* seg 0 = code, seg 1 = data */
67 static struct SAA
*header
; /* relocation/import/export records */
73 static struct seginfo
{
79 } segments
[RDF_MAXSEGS
];
83 static long bsslength
;
84 static long headerlength
;
86 static void rdf2_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
88 int segtext
, segdata
, segbss
;
90 /* set up the initial segments */
91 segments
[0].segname
= ".text";
92 segments
[0].segnumber
= 0;
93 segments
[0].segtype
= 1;
94 segments
[0].segreserved
= 0;
95 segments
[0].seglength
= 0;
97 segments
[1].segname
= ".data";
98 segments
[1].segnumber
= 1;
99 segments
[1].segtype
= 2;
100 segments
[1].segreserved
= 0;
101 segments
[1].seglength
= 0;
103 segments
[2].segname
= ".bss";
104 segments
[2].segnumber
= 2;
105 segments
[2].segtype
= 0xFFFF; /* reserved - should never be produced */
106 segments
[2].segreserved
= 0;
107 segments
[2].seglength
= 0;
114 seg
[0] = saa_init(1L);
115 seg
[1] = saa_init(1L);
116 seg
[2] = NULL
; /* special case! */
118 header
= saa_init(1L);
120 segtext
= seg_alloc();
121 segdata
= seg_alloc();
122 segbss
= seg_alloc();
123 if (segtext
!= 0 || segdata
!= 2 || segbss
!= 4)
125 "rdf segment numbers not allocated as expected (%d,%d,%d)",
126 segtext
, segdata
, segbss
);
131 static long rdf2_section_names(char *name
, int pass
, int *bits
)
139 * Default is 32 bits, in the text segment.
146 /* look for segment type code following segment name */
148 while (*p
&& !isspace(*p
))
150 if (*p
) { /* we're now in whitespace */
152 while (*p
&& isspace(80))
155 if (*p
) { /* we're now in an attribute value */
157 * see if we have an optional ',number' following the type code
159 if ((q
= strchr(p
, ','))) {
162 reserved
= readnum(q
, &i
);
165 "value following comma must be numeric");
170 * check it against the text strings in segmenttypes
173 for (i
= 0; i
< COUNT_SEGTYPES
; i
++)
174 if (!nasm_stricmp(p
, segmenttypes
[i
])) {
175 code
= segmenttypenumbers
[i
];
178 if (code
== -1) { /* didn't find anything */
179 code
= readnum(p
, &i
);
181 error(ERR_NONFATAL
, "unrecognised RDF segment type (%s)",
187 for (i
= 0; i
< nsegments
; i
++) {
188 if (!strcmp(name
, segments
[i
].segname
)) {
189 if (code
!= -1 || reserved
!= 0)
190 error(ERR_NONFATAL
, "segment attributes specified on"
191 " redeclaration of segment");
192 return segments
[i
].segnumber
* 2;
196 /* declaring a new segment! */
199 error(ERR_NONFATAL
, "new segment declared without type code");
202 if (nsegments
== RDF_MAXSEGS
) {
203 error(ERR_FATAL
, "reached compiled-in maximum segment limit (%d)",
208 segments
[nsegments
].segname
= nasm_strdup(name
);
211 error(ERR_PANIC
, "seg_alloc() returned odd number");
212 segments
[nsegments
].segnumber
= i
>> 1;
213 segments
[nsegments
].segtype
= code
;
214 segments
[nsegments
].segreserved
= reserved
;
215 segments
[nsegments
].seglength
= 0;
217 seg
[nsegments
] = saa_init(1L);
223 * Write relocation record
225 static void write_reloc_rec(struct RelocRec
*r
)
229 if (r
->refseg
!= (uint16
) NO_SEG
&& (r
->refseg
& 1)) /* segment base ref */
230 r
->type
= RDFREC_SEGRELOC
;
232 r
->refseg
>>= 1; /* adjust segment nos to RDF rather than NASM */
234 saa_wbytes(header
, &r
->type
, 1);
235 saa_wbytes(header
, &r
->reclen
, 1);
236 saa_wbytes(header
, &r
->segment
, 1);
238 WRITELONG(b
, r
->offset
);
239 saa_wbytes(header
, buf
, 4);
240 saa_wbytes(header
, &r
->length
, 1);
242 WRITESHORT(b
, r
->refseg
);
243 saa_wbytes(header
, buf
, 2);
244 headerlength
+= r
->reclen
+ 2;
248 * Write export record
250 static void write_export_rec(struct ExportRec
*r
)
256 saa_wbytes(header
, &r
->type
, 1);
257 saa_wbytes(header
, &r
->reclen
, 1);
258 saa_wbytes(header
, &r
->flags
, 1);
259 saa_wbytes(header
, &r
->segment
, 1);
261 WRITELONG(b
, r
->offset
);
262 saa_wbytes(header
, buf
, 4);
263 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
264 headerlength
+= r
->reclen
+ 2;
267 static void write_import_rec(struct ImportRec
*r
)
273 saa_wbytes(header
, &r
->type
, 1);
274 saa_wbytes(header
, &r
->reclen
, 1);
275 saa_wbytes(header
, &r
->flags
, 1);
277 WRITESHORT(b
, r
->segment
);
278 saa_wbytes(header
, buf
, 2);
279 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
280 headerlength
+= r
->reclen
+ 2;
286 static void write_bss_rec(struct BSSRec
*r
)
290 saa_wbytes(header
, &r
->type
, 1);
291 saa_wbytes(header
, &r
->reclen
, 1);
293 WRITELONG(b
, r
->amount
);
294 saa_wbytes(header
, buf
, 4);
295 headerlength
+= r
->reclen
+ 2;
299 * Write common variable record
301 static void write_common_rec(struct CommonRec
*r
)
307 saa_wbytes(header
, &r
->type
, 1);
308 saa_wbytes(header
, &r
->reclen
, 1);
310 WRITESHORT(b
, r
->segment
);
311 saa_wbytes(header
, buf
, 2);
313 WRITELONG(b
, r
->size
);
314 saa_wbytes(header
, buf
, 4);
316 WRITESHORT(b
, r
->align
);
317 saa_wbytes(header
, buf
, 2);
318 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
319 headerlength
+= r
->reclen
+ 2;
323 * Write library record
325 static void write_dll_rec(struct DLLRec
*r
)
327 saa_wbytes(header
, &r
->type
, 1);
328 saa_wbytes(header
, &r
->reclen
, 1);
329 saa_wbytes(header
, r
->libname
, strlen(r
->libname
) + 1);
330 headerlength
+= r
->reclen
+ 2;
334 * Write module name record
336 static void write_modname_rec(struct ModRec
*r
)
338 saa_wbytes(header
, &r
->type
, 1);
339 saa_wbytes(header
, &r
->reclen
, 1);
340 saa_wbytes(header
, r
->modname
, strlen(r
->modname
) + 1);
341 headerlength
+= r
->reclen
+ 2;
345 * Handle export, import and common records.
347 static void rdf2_deflabel(char *name
, long segment
, long offset
,
348 int is_global
, char *special
)
353 static int farsym
= 0;
358 /* Check if the label length is OK */
359 if ((len
= strlen(name
)) >= EXIM_LABEL_MAX
) {
360 error(ERR_NONFATAL
, "label size exceeds %d bytes", EXIM_LABEL_MAX
);
364 error(ERR_NONFATAL
, "zero-length label");
368 if (is_global
== 2) {
369 /* Common variable */
370 ci
.type
= RDFREC_COMMON
;
372 ci
.segment
= segment
;
373 strcpy(ci
.label
, name
);
378 * Check the special text to see if it's a valid number and power
379 * of two; if so, store it as the alignment for the common variable.
383 ci
.align
= readnum(special
, &err
);
385 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
386 " valid number", special
);
387 else if ((ci
.align
| (ci
.align
- 1)) != 2 * ci
.align
- 1)
388 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
389 " power of two", special
);
391 write_common_rec(&ci
);
394 /* We don't care about local labels or fix-up hints */
399 while (*special
== ' ' || *special
== '\t')
402 if (!nasm_strnicmp(special
, "export", 6)) {
404 symflags
|= SYM_GLOBAL
;
405 } else if (!nasm_strnicmp(special
, "import", 6)) {
407 symflags
|= SYM_IMPORT
;
411 while (isspace(*special
))
413 if (!nasm_stricmp(special
, "far")) {
415 } else if (!nasm_stricmp(special
, "near")) {
417 } else if (!nasm_stricmp(special
, "proc") ||
418 !nasm_stricmp(special
, "function")) {
419 symflags
|= SYM_FUNCTION
;
420 } else if (!nasm_stricmp(special
, "data") ||
421 !nasm_stricmp(special
, "object")) {
422 symflags
|= SYM_DATA
;
424 error(ERR_NONFATAL
, "unrecognised symbol type `%s'",
429 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
430 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
434 for (i
= 0; i
< nsegments
; i
++) {
435 if (segments
[i
].segnumber
== segment
>> 1)
439 if (i
>= nsegments
) { /* EXTERN declaration */
440 ri
.type
= farsym
? RDFREC_FARIMPORT
: RDFREC_IMPORT
;
441 if (symflags
& SYM_GLOBAL
)
443 "symbol type conflict - EXTERN cannot be EXPORT");
445 ri
.segment
= segment
;
446 strcpy(ri
.label
, name
);
448 write_import_rec(&ri
);
449 } else if (is_global
) {
450 r
.type
= RDFREC_GLOBAL
; /* GLOBAL declaration */
451 if (symflags
& SYM_IMPORT
)
453 "symbol type conflict - GLOBAL cannot be IMPORT");
457 strcpy(r
.label
, name
);
459 write_export_rec(&r
);
463 static void membufwrite(int segment
, const void *data
, int bytes
)
468 for (i
= 0; i
< nsegments
; i
++) {
469 if (segments
[i
].segnumber
== segment
)
473 error(ERR_PANIC
, "can't find segment %d", segment
);
478 WRITESHORT(b
, *(short *)data
);
480 WRITELONG(b
, *(long *)data
);
484 segments
[i
].seglength
+= bytes
;
485 saa_wbytes(seg
[i
], data
, bytes
);
488 static int getsegmentlength(int segment
)
491 for (i
= 0; i
< nsegments
; i
++) {
492 if (segments
[i
].segnumber
== segment
)
496 error(ERR_PANIC
, "can't find segment %d", segment
);
498 return segments
[i
].seglength
;
501 static void rdf2_out(long segto
, const void *data
, unsigned long type
,
502 long segment
, long wrt
)
504 long bytes
= type
& OUT_SIZMASK
;
506 unsigned char databuf
[4], *pd
;
509 if (segto
== NO_SEG
) {
510 if ((type
& OUT_TYPMASK
) != OUT_RESERVE
)
512 "attempt to assemble code in ABSOLUTE space");
516 segto
>>= 1; /* convert NASM segment no to RDF number */
518 for (seg
= 0; seg
< nsegments
; seg
++) {
519 if (segments
[seg
].segnumber
== segto
)
522 if (seg
>= nsegments
) {
524 "specified segment not supported by rdf output format");
529 wrt
= NO_SEG
; /* continue to do _something_ */
530 error(ERR_NONFATAL
, "WRT not supported by rdf output format");
535 if (segto
== 2 && type
!= OUT_RESERVE
) {
536 error(ERR_NONFATAL
, "BSS segments may not be initialised");
538 /* just reserve the space for now... */
540 if (type
== OUT_REL2ADR
)
547 if (type
== OUT_RESERVE
) {
548 if (segto
== 2) /* BSS segment space reserverd */
552 membufwrite(segto
, databuf
, 1);
553 } else if (type
== OUT_RAWDATA
) {
554 if (segment
!= NO_SEG
)
555 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
557 membufwrite(segto
, data
, bytes
);
558 } else if (type
== OUT_ADDRESS
) {
560 /* if segment == NO_SEG then we are writing an address of an
561 object within the same segment - do not produce reloc rec. */
563 /* FIXME - is this behaviour sane? at first glance it doesn't
564 appear to be. Must test this thoroughly...! */
566 if (segment
!= NO_SEG
) {
567 /* it's an address, so we must write a relocation record */
569 rr
.type
= RDFREC_RELOC
; /* type signature */
571 rr
.segment
= segto
; /* segment we're currently in */
572 rr
.offset
= getsegmentlength(segto
); /* current offset */
573 rr
.length
= bytes
; /* length of reference */
574 rr
.refseg
= segment
; /* segment referred to */
575 write_reloc_rec(&rr
);
578 pd
= databuf
; /* convert address to little-endian */
580 WRITESHORT(pd
, *(long *)data
);
582 WRITELONG(pd
, *(long *)data
);
584 membufwrite(segto
, databuf
, bytes
);
586 } else if (type
== OUT_REL2ADR
) {
587 if (segment
== segto
)
588 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
591 rr
.offset
= getsegmentlength(segto
); /* current offset */
592 rr
.length
= 2; /* length of reference */
593 rr
.refseg
= segment
; /* segment referred to (will be >>1'd) */
595 if (segment
!= NO_SEG
&& segment
% 2) {
596 rr
.type
= RDFREC_SEGRELOC
;
597 rr
.segment
= segto
; /* memory base refs *aren't ever* relative! */
598 write_reloc_rec(&rr
);
600 /* what do we put in the code? Simply the data. This should almost
601 * always be zero, unless someone's doing segment arithmetic...
603 rr
.offset
= *(long *)data
;
605 rr
.type
= RDFREC_RELOC
; /* type signature */
606 rr
.segment
= segto
+ 64; /* segment we're currently in + rel flag */
607 write_reloc_rec(&rr
);
609 /* work out what to put in the code: offset of the end of this operand,
610 * subtracted from any data specified, so that loader can just add
611 * address of imported symbol onto it to get address relative to end of
612 * instruction: import_address + data(offset) - end_of_instrn */
614 rr
.offset
= *(long *)data
- (rr
.offset
+ bytes
);
617 membufwrite(segto
, &rr
.offset
, -2);
618 } else if (type
== OUT_REL4ADR
) {
619 if (segment
== segto
)
620 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
621 if (segment
!= NO_SEG
&& segment
% 2) {
622 error(ERR_PANIC
, "erm... 4 byte segment base ref?");
625 rr
.type
= RDFREC_RELOC
; /* type signature */
626 rr
.segment
= segto
+ 64; /* segment we're currently in + rel tag */
627 rr
.offset
= getsegmentlength(segto
); /* current offset */
628 rr
.length
= 4; /* length of reference */
629 rr
.refseg
= segment
; /* segment referred to */
631 write_reloc_rec(&rr
);
633 rr
.offset
= *(long *)data
- (rr
.offset
+ bytes
);
635 membufwrite(segto
, &rr
.offset
, -4);
639 static void rdf2_cleanup(int debuginfo
)
647 /* should write imported & exported symbol declarations to header here */
649 /* generate the output file... */
650 fwrite(RDOFF2Id
, 6, 1, ofile
); /* file type magic number */
652 if (bsslength
!= 0) { /* reserve BSS */
653 bs
.type
= RDFREC_BSS
;
654 bs
.amount
= bsslength
;
660 * calculate overall length of the output object
662 l
= headerlength
+ 4;
664 for (i
= 0; i
< nsegments
; i
++) {
666 continue; /* skip BSS segment */
667 l
+= 10 + segments
[i
].seglength
;
669 l
+= 10; /* null segment */
671 fwritelong(l
, ofile
);
673 fwritelong(headerlength
, ofile
);
674 saa_fpwrite(header
, ofile
); /* dump header */
677 for (i
= 0; i
< nsegments
; i
++) {
681 fwriteshort(segments
[i
].segtype
, ofile
);
682 fwriteshort(segments
[i
].segnumber
, ofile
);
683 fwriteshort(segments
[i
].segreserved
, ofile
);
684 fwritelong(segments
[i
].seglength
, ofile
);
686 saa_fpwrite(seg
[i
], ofile
);
690 /* null segment - write 10 bytes of zero */
691 fwritelong(0, ofile
);
692 fwritelong(0, ofile
);
693 fwriteshort(0, ofile
);
698 static long rdf2_segbase(long segment
)
704 * Handle RDOFF2 specific directives
706 static int rdf2_directive(char *directive
, char *value
, int pass
)
710 /* Check if the name length is OK */
711 if ((n
= strlen(value
)) >= MODLIB_NAME_MAX
) {
712 error(ERR_NONFATAL
, "name size exceeds %d bytes", MODLIB_NAME_MAX
);
716 if (!strcmp(directive
, "library")) {
721 strcpy(r
.libname
, value
);
727 if (!strcmp(directive
, "module")) {
730 r
.type
= RDFREC_MODNAME
;
732 strcpy(r
.modname
, value
);
733 write_modname_rec(&r
);
741 static void rdf2_filename(char *inname
, char *outname
, efunc error
)
743 standard_extension(inname
, outname
, ".rdf", error
);
746 static const char *rdf2_stdmac
[] = {
747 "%define __SECT__ [section .text]",
748 "%imacro library 1+.nolist",
751 "%imacro module 1+.nolist",
754 "%macro __NASM_CDecl__ 1",
759 static int rdf2_set_info(enum geninfo type
, char **val
)
764 struct ofmt of_rdf2
= {
765 "Relocatable Dynamic Object File Format v2.0",