2 * outrdf2.c output routines for the Netwide Assembler to produce
3 * RDOFF version 2 format object files, which Julian originally
4 * planned to use it in his MOSCOW operating system.
6 * The Netwide Assembler is copyright (C) 1996-1998 Simon Tatham and
7 * Julian Hall. All rights reserved. The software is
8 * redistributable under the licence given in the file "Licence"
9 * distributed in the NASM archive.
25 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
26 #define VERBOSE_WARNINGS
30 #include "rdoff/rdoff.h"
32 /* This signature is written to start of RDOFF files */
33 static const char *RDOFF2Id
= RDOFF2_SIGNATURE
;
35 /* Note that whenever a segment is referred to in the RDOFF file, its number
36 * is always half of the segment number that NASM uses to refer to it; this
37 * is because NASM only allocates even numbered segments, so as to not
38 * waste any of the 16 bits of segment number written to the file - this
39 * allows up to 65533 external labels to be defined; otherwise it would be
42 #define COUNT_SEGTYPES 9
44 static char *segmenttypes
[COUNT_SEGTYPES
] = {
45 "null", "text", "code", "data",
46 "comment", "lcomment", "pcomment",
47 "symdebug", "linedebug"
50 static int segmenttypenumbers
[COUNT_SEGTYPES
] = {
51 0, 1, 1, 2, 3, 4, 5, 6, 7
54 /* code for managing buffers needed to separate code and data into individual
55 * sections until they are ready to be written to the file.
56 * We'd better hope that it all fits in memory else we're buggered... */
58 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
59 * on 80x86 machines for efficiency */
61 /***********************************************************************
62 * Actual code to deal with RDOFF2 ouput format begins here...
65 /* global variables set during the initialisation phase */
67 static struct SAA
*seg
[RDF_MAXSEGS
]; /* seg 0 = code, seg 1 = data */
68 static struct SAA
*header
; /* relocation/import/export records */
74 static struct seginfo
{
80 } segments
[RDF_MAXSEGS
];
84 static int32_t bsslength
;
85 static int32_t headerlength
;
87 static void rdf2_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
89 int segtext
, segdata
, segbss
;
96 /* set up the initial segments */
97 segments
[0].segname
= ".text";
98 segments
[0].segnumber
= 0;
99 segments
[0].segtype
= 1;
100 segments
[0].segreserved
= 0;
101 segments
[0].seglength
= 0;
103 segments
[1].segname
= ".data";
104 segments
[1].segnumber
= 1;
105 segments
[1].segtype
= 2;
106 segments
[1].segreserved
= 0;
107 segments
[1].seglength
= 0;
109 segments
[2].segname
= ".bss";
110 segments
[2].segnumber
= 2;
111 segments
[2].segtype
= 0xFFFF; /* reserved - should never be produced */
112 segments
[2].segreserved
= 0;
113 segments
[2].seglength
= 0;
120 seg
[0] = saa_init(1L);
121 seg
[1] = saa_init(1L);
122 seg
[2] = NULL
; /* special case! */
124 header
= saa_init(1L);
126 segtext
= seg_alloc();
127 segdata
= seg_alloc();
128 segbss
= seg_alloc();
129 if (segtext
!= 0 || segdata
!= 2 || segbss
!= 4)
131 "rdf segment numbers not allocated as expected (%d,%d,%d)",
132 segtext
, segdata
, segbss
);
137 static int32_t rdf2_section_names(char *name
, int pass
, int *bits
)
148 * Default is 32 bits, in the text segment.
155 /* look for segment type code following segment name */
157 while (*p
&& !isspace(*p
))
159 if (*p
) { /* we're now in whitespace */
161 while (*p
&& isspace(80))
164 if (*p
) { /* we're now in an attribute value */
166 * see if we have an optional ',number' following the type code
168 if ((q
= strchr(p
, ','))) {
171 reserved
= readnum(q
, &err
);
174 "value following comma must be numeric");
179 * check it against the text strings in segmenttypes
182 for (i
= 0; i
< COUNT_SEGTYPES
; i
++)
183 if (!nasm_stricmp(p
, segmenttypes
[i
])) {
184 code
= segmenttypenumbers
[i
];
187 if (code
== -1) { /* didn't find anything */
188 code
= readnum(p
, &err
);
190 error(ERR_NONFATAL
, "unrecognised RDF segment type (%s)",
196 for (i
= 0; i
< nsegments
; i
++) {
197 if (!strcmp(name
, segments
[i
].segname
)) {
198 if (code
!= -1 || reserved
!= 0)
199 error(ERR_NONFATAL
, "segment attributes specified on"
200 " redeclaration of segment");
201 return segments
[i
].segnumber
* 2;
205 /* declaring a new segment! */
208 error(ERR_NONFATAL
, "new segment declared without type code");
211 if (nsegments
== RDF_MAXSEGS
) {
212 error(ERR_FATAL
, "reached compiled-in maximum segment limit (%d)",
217 segments
[nsegments
].segname
= nasm_strdup(name
);
220 error(ERR_PANIC
, "seg_alloc() returned odd number");
221 segments
[nsegments
].segnumber
= i
>> 1;
222 segments
[nsegments
].segtype
= code
;
223 segments
[nsegments
].segreserved
= reserved
;
224 segments
[nsegments
].seglength
= 0;
226 seg
[nsegments
] = saa_init(1L);
232 * Write relocation record
234 static void write_reloc_rec(struct RelocRec
*r
)
238 if (r
->refseg
!= (uint16_t) NO_SEG
&& (r
->refseg
& 1)) /* segment base ref */
239 r
->type
= RDFREC_SEGRELOC
;
241 r
->refseg
>>= 1; /* adjust segment nos to RDF rather than NASM */
243 saa_wbytes(header
, &r
->type
, 1);
244 saa_wbytes(header
, &r
->reclen
, 1);
245 saa_wbytes(header
, &r
->segment
, 1);
247 WRITELONG(b
, r
->offset
);
248 saa_wbytes(header
, buf
, 4);
249 saa_wbytes(header
, &r
->length
, 1);
251 WRITESHORT(b
, r
->refseg
);
252 saa_wbytes(header
, buf
, 2);
253 headerlength
+= r
->reclen
+ 2;
257 * Write export record
259 static void write_export_rec(struct ExportRec
*r
)
265 saa_wbytes(header
, &r
->type
, 1);
266 saa_wbytes(header
, &r
->reclen
, 1);
267 saa_wbytes(header
, &r
->flags
, 1);
268 saa_wbytes(header
, &r
->segment
, 1);
270 WRITELONG(b
, r
->offset
);
271 saa_wbytes(header
, buf
, 4);
272 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
273 headerlength
+= r
->reclen
+ 2;
276 static void write_import_rec(struct ImportRec
*r
)
282 saa_wbytes(header
, &r
->type
, 1);
283 saa_wbytes(header
, &r
->reclen
, 1);
284 saa_wbytes(header
, &r
->flags
, 1);
286 WRITESHORT(b
, r
->segment
);
287 saa_wbytes(header
, buf
, 2);
288 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
289 headerlength
+= r
->reclen
+ 2;
295 static void write_bss_rec(struct BSSRec
*r
)
299 saa_wbytes(header
, &r
->type
, 1);
300 saa_wbytes(header
, &r
->reclen
, 1);
302 WRITELONG(b
, r
->amount
);
303 saa_wbytes(header
, buf
, 4);
304 headerlength
+= r
->reclen
+ 2;
308 * Write common variable record
310 static void write_common_rec(struct CommonRec
*r
)
316 saa_wbytes(header
, &r
->type
, 1);
317 saa_wbytes(header
, &r
->reclen
, 1);
319 WRITESHORT(b
, r
->segment
);
320 saa_wbytes(header
, buf
, 2);
322 WRITELONG(b
, r
->size
);
323 saa_wbytes(header
, buf
, 4);
325 WRITESHORT(b
, r
->align
);
326 saa_wbytes(header
, buf
, 2);
327 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
328 headerlength
+= r
->reclen
+ 2;
332 * Write library record
334 static void write_dll_rec(struct DLLRec
*r
)
336 saa_wbytes(header
, &r
->type
, 1);
337 saa_wbytes(header
, &r
->reclen
, 1);
338 saa_wbytes(header
, r
->libname
, strlen(r
->libname
) + 1);
339 headerlength
+= r
->reclen
+ 2;
343 * Write module name record
345 static void write_modname_rec(struct ModRec
*r
)
347 saa_wbytes(header
, &r
->type
, 1);
348 saa_wbytes(header
, &r
->reclen
, 1);
349 saa_wbytes(header
, r
->modname
, strlen(r
->modname
) + 1);
350 headerlength
+= r
->reclen
+ 2;
354 * Handle export, import and common records.
356 static void rdf2_deflabel(char *name
, int32_t segment
, int64_t offset
,
357 int is_global
, char *special
)
362 static int farsym
= 0;
367 /* Check if the label length is OK */
368 if ((len
= strlen(name
)) >= EXIM_LABEL_MAX
) {
369 error(ERR_NONFATAL
, "label size exceeds %d bytes", EXIM_LABEL_MAX
);
373 error(ERR_NONFATAL
, "zero-length label");
377 if (is_global
== 2) {
378 /* Common variable */
379 ci
.type
= RDFREC_COMMON
;
381 ci
.segment
= segment
;
382 strcpy(ci
.label
, name
);
387 * Check the special text to see if it's a valid number and power
388 * of two; if so, store it as the alignment for the common variable.
392 ci
.align
= readnum(special
, &err
);
394 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
395 " valid number", special
);
396 else if ((ci
.align
| (ci
.align
- 1)) != 2 * ci
.align
- 1)
397 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
398 " power of two", special
);
400 write_common_rec(&ci
);
403 /* We don't care about local labels or fix-up hints */
408 while (*special
== ' ' || *special
== '\t')
411 if (!nasm_strnicmp(special
, "export", 6)) {
413 symflags
|= SYM_GLOBAL
;
414 } else if (!nasm_strnicmp(special
, "import", 6)) {
416 symflags
|= SYM_IMPORT
;
420 while (isspace(*special
))
422 if (!nasm_stricmp(special
, "far")) {
424 } else if (!nasm_stricmp(special
, "near")) {
426 } else if (!nasm_stricmp(special
, "proc") ||
427 !nasm_stricmp(special
, "function")) {
428 symflags
|= SYM_FUNCTION
;
429 } else if (!nasm_stricmp(special
, "data") ||
430 !nasm_stricmp(special
, "object")) {
431 symflags
|= SYM_DATA
;
433 error(ERR_NONFATAL
, "unrecognised symbol type `%s'",
438 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
439 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
443 for (i
= 0; i
< nsegments
; i
++) {
444 if (segments
[i
].segnumber
== segment
>> 1)
448 if (i
>= nsegments
) { /* EXTERN declaration */
449 ri
.type
= farsym
? RDFREC_FARIMPORT
: RDFREC_IMPORT
;
450 if (symflags
& SYM_GLOBAL
)
452 "symbol type conflict - EXTERN cannot be EXPORT");
454 ri
.segment
= segment
;
455 strcpy(ri
.label
, name
);
457 write_import_rec(&ri
);
458 } else if (is_global
) {
459 r
.type
= RDFREC_GLOBAL
; /* GLOBAL declaration */
460 if (symflags
& SYM_IMPORT
)
462 "symbol type conflict - GLOBAL cannot be IMPORT");
466 strcpy(r
.label
, name
);
468 write_export_rec(&r
);
472 static void membufwrite(int segment
, const void *data
, int bytes
)
477 for (i
= 0; i
< nsegments
; i
++) {
478 if (segments
[i
].segnumber
== segment
)
482 error(ERR_PANIC
, "can't find segment %d", segment
);
487 WRITESHORT(b
, *(int16_t *)data
);
489 WRITELONG(b
, *(int32_t *)data
);
493 segments
[i
].seglength
+= bytes
;
494 saa_wbytes(seg
[i
], data
, bytes
);
497 static int getsegmentlength(int segment
)
500 for (i
= 0; i
< nsegments
; i
++) {
501 if (segments
[i
].segnumber
== segment
)
505 error(ERR_PANIC
, "can't find segment %d", segment
);
507 return segments
[i
].seglength
;
510 static void rdf2_out(int32_t segto
, const void *data
,
511 enum out_type type
, uint64_t size
,
512 int32_t segment
, int32_t wrt
)
515 uint8_t databuf
[8], *pd
;
518 if (segto
== NO_SEG
) {
519 if (type
!= OUT_RESERVE
)
521 "attempt to assemble code in ABSOLUTE space");
525 segto
>>= 1; /* convert NASM segment no to RDF number */
527 for (seg
= 0; seg
< nsegments
; seg
++) {
528 if (segments
[seg
].segnumber
== segto
)
531 if (seg
>= nsegments
) {
533 "specified segment not supported by rdf output format");
538 wrt
= NO_SEG
; /* continue to do _something_ */
539 error(ERR_NONFATAL
, "WRT not supported by rdf output format");
542 if (segto
== 2 && type
!= OUT_RESERVE
) {
543 error(ERR_NONFATAL
, "BSS segments may not be initialized");
545 /* just reserve the space for now... */
547 if (type
== OUT_REL2ADR
)
554 if (type
== OUT_RESERVE
) {
555 if (segto
== 2) /* BSS segment space reserverd */
559 membufwrite(segto
, databuf
, 1);
560 } else if (type
== OUT_RAWDATA
) {
561 if (segment
!= NO_SEG
)
562 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
564 membufwrite(segto
, data
, size
);
565 } else if (type
== OUT_ADDRESS
) {
567 /* if segment == NO_SEG then we are writing an address of an
568 object within the same segment - do not produce reloc rec. */
570 /* FIXME - is this behaviour sane? at first glance it doesn't
571 appear to be. Must test this thoroughly...! */
573 if (segment
!= NO_SEG
) {
574 /* it's an address, so we must write a relocation record */
576 rr
.type
= RDFREC_RELOC
; /* type signature */
578 rr
.segment
= segto
; /* segment we're currently in */
579 rr
.offset
= getsegmentlength(segto
); /* current offset */
580 rr
.length
= size
; /* length of reference */
581 rr
.refseg
= segment
; /* segment referred to */
582 write_reloc_rec(&rr
);
585 pd
= databuf
; /* convert address to little-endian */
586 WRITEADDR(pd
, *(int64_t *)data
, size
);
587 membufwrite(segto
, databuf
, size
);
588 } else if (type
== OUT_REL2ADR
) {
589 if (segment
== segto
)
590 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
593 rr
.offset
= getsegmentlength(segto
); /* current offset */
594 rr
.length
= 2; /* length of reference */
595 rr
.refseg
= segment
; /* segment referred to (will be >>1'd) */
597 if (segment
!= NO_SEG
&& segment
% 2) {
598 rr
.type
= RDFREC_SEGRELOC
;
599 rr
.segment
= segto
; /* memory base refs *aren't ever* relative! */
600 write_reloc_rec(&rr
);
602 /* what do we put in the code? Simply the data. This should almost
603 * always be zero, unless someone's doing segment arithmetic...
605 rr
.offset
= *(int64_t *)data
;
607 rr
.type
= RDFREC_RELOC
; /* type signature */
608 rr
.segment
= segto
+ 64; /* segment we're currently in + rel flag */
609 write_reloc_rec(&rr
);
611 /* work out what to put in the code: offset of the end of this operand,
612 * subtracted from any data specified, so that loader can just add
613 * address of imported symbol onto it to get address relative to end of
614 * instruction: import_address + data(offset) - end_of_instrn */
616 rr
.offset
= *(int32_t *)data
- (rr
.offset
+ size
);
619 membufwrite(segto
, &rr
.offset
, -2);
620 } else if (type
== OUT_REL4ADR
) {
621 if ((segment
== segto
) && (globalbits
!= 64))
622 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
623 if (segment
!= NO_SEG
&& segment
% 2) {
624 error(ERR_PANIC
, "erm... 4 byte segment base ref?");
627 rr
.type
= RDFREC_RELOC
; /* type signature */
628 rr
.segment
= segto
+ 64; /* segment we're currently in + rel tag */
629 rr
.offset
= getsegmentlength(segto
); /* current offset */
630 rr
.length
= 4; /* length of reference */
631 rr
.refseg
= segment
; /* segment referred to */
633 write_reloc_rec(&rr
);
635 rr
.offset
= *(int64_t *)data
- (rr
.offset
+ size
);
637 membufwrite(segto
, &rr
.offset
, -4);
641 static void rdf2_cleanup(int debuginfo
)
649 /* should write imported & exported symbol declarations to header here */
651 /* generate the output file... */
652 fwrite(RDOFF2Id
, 6, 1, ofile
); /* file type magic number */
654 if (bsslength
!= 0) { /* reserve BSS */
655 bs
.type
= RDFREC_BSS
;
656 bs
.amount
= bsslength
;
662 * calculate overall length of the output object
664 l
= headerlength
+ 4;
666 for (i
= 0; i
< nsegments
; i
++) {
668 continue; /* skip BSS segment */
669 l
+= 10 + segments
[i
].seglength
;
671 l
+= 10; /* null segment */
673 fwriteint32_t(l
, ofile
);
675 fwriteint32_t(headerlength
, ofile
);
676 saa_fpwrite(header
, ofile
); /* dump header */
679 for (i
= 0; i
< nsegments
; i
++) {
683 fwriteint16_t(segments
[i
].segtype
, ofile
);
684 fwriteint16_t(segments
[i
].segnumber
, ofile
);
685 fwriteint16_t(segments
[i
].segreserved
, ofile
);
686 fwriteint32_t(segments
[i
].seglength
, ofile
);
688 saa_fpwrite(seg
[i
], ofile
);
692 /* null segment - write 10 bytes of zero */
693 fwriteint32_t(0, ofile
);
694 fwriteint32_t(0, ofile
);
695 fwriteint16_t(0, ofile
);
700 static int32_t rdf2_segbase(int32_t segment
)
706 * Handle RDOFF2 specific directives
708 static int rdf2_directive(char *directive
, char *value
, int pass
)
712 /* Check if the name length is OK */
713 if ((n
= strlen(value
)) >= MODLIB_NAME_MAX
) {
714 error(ERR_NONFATAL
, "name size exceeds %d bytes", MODLIB_NAME_MAX
);
718 if (!strcmp(directive
, "library")) {
723 strcpy(r
.libname
, value
);
729 if (!strcmp(directive
, "module")) {
732 r
.type
= RDFREC_MODNAME
;
734 strcpy(r
.modname
, value
);
735 write_modname_rec(&r
);
743 static void rdf2_filename(char *inname
, char *outname
, efunc error
)
745 standard_extension(inname
, outname
, ".rdf", error
);
748 static const char *rdf2_stdmac
[] = {
749 "%define __SECT__ [section .text]",
750 "%imacro library 1+.nolist",
753 "%imacro module 1+.nolist",
756 "%macro __NASM_CDecl__ 1",
761 static int rdf2_set_info(enum geninfo type
, char **val
)
768 struct ofmt of_rdf2
= {
769 "Relocatable Dynamic Object File Format v2.0",