1 /* outrdf2.c output routines for the Netwide Assembler to produce
2 * RDOFF version 2 format object files (which are intended
3 * mainly for use in proprietary projects, as the code to
4 * load and execute them is very simple). They will also be
5 * used 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-1998 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 #define RDF_MAXSEGS 64 /* maximum number of segments - user configurable */
32 typedef unsigned short int16
;
33 typedef unsigned char byte
;
35 static const char *RDOFF2Id
= "RDOFF2"; /* written to start of RDOFF files */
38 /* the records that can be found in the RDOFF header */
40 /* Note that whenever a segment is referred to in the RDOFF file, its number
41 * is always half of the segment number that NASM uses to refer to it; this
42 * is because NASM only allocates even numbered segments, so as to not
43 * waste any of the 16 bits of segment number written to the file - this
44 * allows up to 65533 external labels to be defined; otherwise it would be
47 #define RDFREC_RELOC 1
48 #define RDFREC_IMPORT 2
49 #define RDFREC_GLOBAL 3
52 #define RDFREC_SEGRELOC 6
53 #define RDFREC_FARIMPORT 7
54 #define RDFREC_MODNAME 8
55 #define RDFREC_COMMON 10
56 #define RDFREC_GENERIC 0
60 byte type
; /* must be 1, or 6 for segment base ref */
61 byte reclen
; /* set to 8 */
62 byte segment
; /* only 0 for code, or 1 for data supported,
63 * but add 64 for relative refs (ie do not require
64 * reloc @ loadtime, only linkage) */
65 long offset
; /* from start of segment in which reference is loc'd */
66 byte length
; /* 1 2 or 4 bytes */
67 int16 refseg
; /* segment to which reference refers to */
71 byte type
; /* must be 2, or 7 for FAR import */
72 byte reclen
; /* equals 3+label length */
73 int16 segment
; /* segment number allocated to the label for reloc
74 * records - label is assumed to be at offset zero
75 * in this segment, so linker must fix up with offset
76 * of segment and of offset within segment */
77 char label
[33]; /* zero terminated... should be written to file until
78 * the zero, but not after it - max len = 32 chars */
82 byte type
; /* must be 3 */
83 byte reclen
; /* equals 7+label length */
84 byte flags
; /* SYM_* flags (see below) */
85 byte segment
; /* segment referred to (0/1) */
86 long offset
; /* offset within segment */
87 char label
[33]; /* zero terminated as above. max len = 32 chars */
91 byte type
; /* must be 5 */
92 byte reclen
; /* equals 4 */
93 long amount
; /* number of bytes BSS to reserve */
97 byte type
; /* 4 for DLLRec, 8 for ModRec */
98 byte reclen
; /* 1+lib name length for DLLRec, 1+mod name length */
99 char name
[128]; /* library to link at load time or module name */
103 byte type
; /* must be 10 */
104 byte reclen
; /* equals 9+label length */
105 int16 segment
; /* segment number */
106 long size
; /* size of common variable */
107 int16 align
; /* alignment (power of two) */
108 char label
[33]; /* zero terminated as above. max len = 32 chars */
111 /* Flags for ExportRec */
112 #define SYM_DATA 0x01
113 #define SYM_FUNCTION 0x02
114 #define SYM_GLOBAL 0x04
116 #define COUNT_SEGTYPES 9
118 static char * segmenttypes
[COUNT_SEGTYPES
] = {
119 "null", "text", "code", "data", "comment", "lcomment", "pcomment",
120 "symdebug", "linedebug"
123 static int segmenttypenumbers
[COUNT_SEGTYPES
] = {
124 0, 1, 1, 2, 3, 4, 5, 6, 7
127 /* code for managing buffers needed to seperate code and data into individual
128 * sections until they are ready to be written to the file.
129 * We'd better hope that it all fits in memory else we're buggered... */
131 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
132 * on 80x86 machines for efficiency */
134 /***********************************************************************
135 * Actual code to deal with RDOFF2 ouput format begins here...
138 /* global variables set during the initialisation phase */
140 static struct SAA
*seg
[RDF_MAXSEGS
]; /* seg 0 = code, seg 1 = data */
141 static struct SAA
*header
; /* relocation/import/export records */
147 static struct seginfo
{
153 } segments
[RDF_MAXSEGS
];
155 static int nsegments
;
157 static long bsslength
;
158 static long headerlength
;
160 static void rdf2_init(FILE *fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
162 int segtext
, segdata
, segbss
;
164 /* set up the initial segments */
165 segments
[0].segname
= ".text";
166 segments
[0].segnumber
= 0;
167 segments
[0].segtype
= 1;
168 segments
[0].segreserved
= 0;
169 segments
[0].seglength
= 0;
171 segments
[1].segname
= ".data";
172 segments
[1].segnumber
= 1;
173 segments
[1].segtype
= 2;
174 segments
[1].segreserved
= 0;
175 segments
[1].seglength
= 0;
177 segments
[2].segname
= ".bss";
178 segments
[2].segnumber
= 2;
179 segments
[2].segtype
= 0xFFFF; /* reserved - should never be produced */
180 segments
[2].segreserved
= 0;
181 segments
[2].seglength
= 0;
188 seg
[0] = saa_init(1L);
189 seg
[1] = saa_init(1L);
190 seg
[2] = NULL
; /* special case! */
192 header
= saa_init(1L);
194 segtext
= seg_alloc();
195 segdata
= seg_alloc();
196 segbss
= seg_alloc();
197 if (segtext
!= 0 || segdata
!= 2 || segbss
!= 4)
198 error(ERR_PANIC
,"rdf segment numbers not allocated as expected (%d,%d,%d)",
199 segtext
,segdata
,segbss
);
204 static long rdf2_section_names(char *name
, int pass
, int *bits
)
212 * Default is 32 bits, in the text segment.
219 /* look for segment type code following segment name */
221 while (*p
&& !isspace(*p
)) p
++;
222 if (*p
) { /* we're now in whitespace */
224 while (*p
&& isspace(80)) *p
++ = '\0';
226 if (*p
) { /* we're now in an attribute value */
228 * see if we have an optional ',number' following the type code
230 if ((q
= strchr(p
, ','))) {
233 reserved
= readnum(q
, &i
);
235 error(ERR_NONFATAL
, "value following comma must be numeric");
240 * check it against the text strings in segmenttypes
243 for (i
= 0; i
< COUNT_SEGTYPES
; i
++)
244 if (!nasm_stricmp(p
, segmenttypes
[i
])) {
245 code
= segmenttypenumbers
[i
];
248 if (code
== -1) { /* didn't find anything */
249 code
= readnum(p
, &i
);
251 error(ERR_NONFATAL
, "unrecognised RDF segment type (%s)",p
);
256 for (i
= 0; i
< nsegments
; i
++) {
257 if (!strcmp(name
, segments
[i
].segname
)) {
258 if (code
!= -1 || reserved
!= 0)
259 error(ERR_NONFATAL
, "segment attributes specified on"
260 " redeclaration of segment");
261 return segments
[i
].segnumber
* 2;
265 /* declaring a new segment! */
268 error(ERR_NONFATAL
, "new segment declared without type code");
271 if (nsegments
== RDF_MAXSEGS
) {
272 error(ERR_FATAL
, "reached compiled-in maximum segment limit (%d)",
277 segments
[nsegments
].segname
= nasm_strdup(name
);
280 error(ERR_PANIC
, "seg_alloc() returned odd number");
281 segments
[nsegments
].segnumber
= i
>> 1;
282 segments
[nsegments
].segtype
= code
;
283 segments
[nsegments
].segreserved
= reserved
;
284 segments
[nsegments
].seglength
= 0;
286 seg
[nsegments
] = saa_init(1L);
291 static void write_reloc_rec(struct RelocRec
*r
)
295 if (r
->refseg
!= (int16
)NO_SEG
&& (r
->refseg
& 1)) /* segment base ref */
296 r
->type
= RDFREC_SEGRELOC
;
298 r
->refseg
>>= 1; /* adjust segment nos to RDF rather than NASM */
300 saa_wbytes(header
,&r
->type
,1);
301 saa_wbytes(header
,&r
->reclen
,1);
302 saa_wbytes(header
,&r
->segment
,1);
303 b
= buf
; WRITELONG(b
,r
->offset
);
304 saa_wbytes(header
,buf
,4);
305 saa_wbytes(header
,&r
->length
,1);
306 b
= buf
; WRITESHORT(b
,r
->refseg
);
307 saa_wbytes(header
,buf
,2);
308 headerlength
+= r
->reclen
+ 2;
311 static void write_export_rec(struct ExportRec
*r
)
317 saa_wbytes(header
,&r
->type
,1);
318 saa_wbytes(header
,&r
->reclen
,1);
319 saa_wbytes(header
,&r
->flags
,1);
320 saa_wbytes(header
,&r
->segment
,1);
321 b
= buf
; WRITELONG(b
,r
->offset
);
322 saa_wbytes(header
,buf
,4);
323 saa_wbytes(header
,r
->label
,strlen(r
->label
) + 1);
324 headerlength
+= r
->reclen
+ 2;
327 static void write_import_rec(struct ImportRec
*r
)
333 saa_wbytes(header
,&r
->type
,1);
334 saa_wbytes(header
,&r
->reclen
,1);
335 b
= buf
; WRITESHORT(b
,r
->segment
);
336 saa_wbytes(header
,buf
,2);
337 saa_wbytes(header
,r
->label
,strlen(r
->label
) + 1);
338 headerlength
+= r
->reclen
+ 2;
341 static void write_bss_rec(struct BSSRec
*r
)
345 saa_wbytes(header
,&r
->type
,1);
346 saa_wbytes(header
,&r
->reclen
,1);
347 b
= buf
; WRITELONG(b
,r
->amount
);
348 saa_wbytes(header
,buf
,4);
349 headerlength
+= r
->reclen
+ 2;
353 * Write common variable record.
355 static void write_common_rec(struct CommonRec
*r
)
361 saa_wbytes(header
,&r
->type
,1);
362 saa_wbytes(header
,&r
->reclen
,1);
363 b
= buf
; WRITESHORT(b
,r
->segment
);
364 saa_wbytes(header
,buf
,2);
365 b
= buf
; WRITELONG(b
,r
->size
);
366 saa_wbytes(header
,buf
,4);
367 b
= buf
; WRITESHORT(b
,r
->align
);
368 saa_wbytes(header
,buf
,2);
369 saa_wbytes(header
,r
->label
,strlen(r
->label
) + 1);
370 headerlength
+= r
->reclen
+ 2;
374 * Write library record. Also used for module name records.
376 static void write_dllmod_rec(struct DLLModRec
*r
)
378 saa_wbytes(header
,&r
->type
,1);
379 saa_wbytes(header
,&r
->reclen
,1);
380 saa_wbytes(header
,r
->name
,strlen(r
->name
) + 1);
381 headerlength
+= r
->reclen
+ 2;
384 static void rdf2_deflabel(char *name
, long segment
, long offset
,
385 int is_global
, char *special
)
390 static int farsym
= 0;
392 byte export_flags
= 0;
394 if (is_global
== 2) {
395 /* Common variable */
396 ci
.type
= RDFREC_COMMON
;
398 ci
.segment
= segment
;
399 strncpy(ci
.label
, name
, 32);
401 ci
.reclen
= 9 + strlen(ci
.label
);
405 * Check the special text to see if it's a valid number and power
406 * of two; if so, store it as the alignment for the common variable.
410 ci
.align
= readnum(special
, &err
);
411 if (err
) error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
412 " valid number", special
);
413 else if ( (ci
.align
| (ci
.align
-1)) != 2*ci
.align
- 1)
414 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
415 " power of two", special
);
417 write_common_rec(&ci
);
420 /* We don't care about local labels or fix-up hints */
421 if (is_global
!= 1) return;
424 while(*special
== ' ' || *special
== '\t') special
++;
426 if (!nasm_strnicmp(special
, "export", 6)) {
428 export_flags
|= SYM_GLOBAL
;
432 while(isspace(*special
)) special
++;
433 if (!nasm_stricmp(special
, "far")) {
436 else if (!nasm_stricmp(special
, "near")) {
439 else if (!nasm_stricmp(special
, "proc") ||
440 !nasm_stricmp(special
, "function")) {
441 export_flags
|= SYM_FUNCTION
;
443 else if (!nasm_stricmp(special
, "data") ||
444 !nasm_stricmp(special
, "object")) {
445 export_flags
|= SYM_DATA
;
448 error(ERR_NONFATAL
, "unrecognised symbol type `%s'", special
);
452 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
453 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
457 for (i
= 0; i
< nsegments
; i
++) {
458 if (segments
[i
].segnumber
== segment
>>1) break;
460 if (i
>= nsegments
) { /* EXTERN declaration */
462 ri
.type
= RDFREC_FARIMPORT
;
464 ri
.type
= RDFREC_IMPORT
;
465 ri
.segment
= segment
;
466 strncpy(ri
.label
,name
,32);
468 ri
.reclen
= 3 + strlen(ri
.label
);
469 write_import_rec(&ri
);
470 } else if (is_global
) {
471 r
.type
= RDFREC_GLOBAL
;
472 r
.flags
= export_flags
;
475 strncpy(r
.label
,name
,32);
477 r
.reclen
= 7 + strlen(r
.label
);
478 write_export_rec(&r
);
482 static void membufwrite(int segment
, const void * data
, int bytes
)
487 for (i
= 0; i
< nsegments
; i
++) {
488 if (segments
[i
].segnumber
== segment
) break;
491 error(ERR_PANIC
, "can't find segment %d", segment
);
496 WRITESHORT(b
,*(short *)data
);
498 WRITELONG(b
,*(long *)data
);
502 segments
[i
].seglength
+= bytes
;
503 saa_wbytes(seg
[i
],data
,bytes
);
506 static int getsegmentlength(int segment
)
509 for (i
= 0; i
< nsegments
; i
++) {
510 if (segments
[i
].segnumber
== segment
) break;
513 error(ERR_PANIC
, "can't find segment %d", segment
);
515 return segments
[i
].seglength
;
518 static void rdf2_out (long segto
, const void *data
, unsigned long type
,
519 long segment
, long wrt
)
521 long bytes
= type
& OUT_SIZMASK
;
523 unsigned char databuf
[4],*pd
;
526 if (segto
== NO_SEG
) {
527 if ((type
& OUT_TYPMASK
) != OUT_RESERVE
)
528 error (ERR_NONFATAL
, "attempt to assemble code in ABSOLUTE space");
532 segto
>>= 1; /* convert NASM segment no to RDF number */
534 for (seg
= 0; seg
< nsegments
; seg
++) {
535 if (segments
[seg
].segnumber
== segto
) break;
537 if (seg
>= nsegments
) {
538 error(ERR_NONFATAL
,"specified segment not supported by rdf output format");
543 wrt
= NO_SEG
; /* continue to do _something_ */
544 error (ERR_NONFATAL
, "WRT not supported by rdf output format");
549 if (segto
== 2 && type
!= OUT_RESERVE
)
551 error(ERR_NONFATAL
, "BSS segments may not be initialised");
553 /* just reserve the space for now... */
555 if (type
== OUT_REL2ADR
)
562 if (type
== OUT_RESERVE
) {
563 if (segto
== 2) /* BSS segment space reserverd */
567 membufwrite(segto
,databuf
,1);
569 else if (type
== OUT_RAWDATA
) {
570 if (segment
!= NO_SEG
)
571 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
573 membufwrite(segto
,data
,bytes
);
575 else if (type
== OUT_ADDRESS
) {
577 /* if segment == NO_SEG then we are writing an address of an
578 object within the same segment - do not produce reloc rec. */
580 /* FIXME - is this behaviour sane? at first glance it doesn't
581 appear to be. Must test this thoroughly...! */
583 if (segment
!= NO_SEG
)
585 /* it's an address, so we must write a relocation record */
587 rr
.type
= RDFREC_RELOC
; /* type signature */
589 rr
.segment
= segto
; /* segment we're currently in */
590 rr
.offset
= getsegmentlength(segto
); /* current offset */
591 rr
.length
= bytes
; /* length of reference */
592 rr
.refseg
= segment
; /* segment referred to */
593 write_reloc_rec(&rr
);
596 pd
= databuf
; /* convert address to little-endian */
598 WRITESHORT (pd
, *(long *)data
);
600 WRITELONG (pd
, *(long *)data
);
602 membufwrite(segto
,databuf
,bytes
);
605 else if (type
== OUT_REL2ADR
)
607 if (segment
== segto
)
608 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
611 rr
.offset
= getsegmentlength(segto
); /* current offset */
612 rr
.length
= 2; /* length of reference */
613 rr
.refseg
= segment
; /* segment referred to (will be >>1'd)*/
615 if (segment
!= NO_SEG
&& segment
% 2) {
616 rr
.type
= RDFREC_SEGRELOC
;
617 rr
.segment
= segto
; /* memory base refs *aren't ever* relative! */
618 write_reloc_rec(&rr
);
620 /* what do we put in the code? Simply the data. This should almost
621 * always be zero, unless someone's doing segment arithmetic...
623 rr
.offset
= *(long *) data
;
627 rr
.type
= RDFREC_RELOC
; /* type signature */
628 rr
.segment
= segto
+64; /* segment we're currently in + rel flag */
629 write_reloc_rec(&rr
);
631 /* work out what to put in the code: offset of the end of this operand,
632 * subtracted from any data specified, so that loader can just add
633 * address of imported symbol onto it to get address relative to end of
634 * instruction: import_address + data(offset) - end_of_instrn */
636 rr
.offset
= *(long *)data
-(rr
.offset
+ bytes
);
639 membufwrite(segto
,&rr
.offset
,-2);
641 else if (type
== OUT_REL4ADR
)
643 if (segment
== segto
)
644 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
645 if (segment
!= NO_SEG
&& segment
% 2) {
646 error(ERR_PANIC
, "erm... 4 byte segment base ref?");
649 rr
.type
= RDFREC_RELOC
; /* type signature */
650 rr
.segment
= segto
+64; /* segment we're currently in + rel tag */
651 rr
.offset
= getsegmentlength(segto
); /* current offset */
652 rr
.length
= 4; /* length of reference */
653 rr
.refseg
= segment
; /* segment referred to */
655 write_reloc_rec(&rr
);
657 rr
.offset
= *(long *)data
-(rr
.offset
+ bytes
);
659 membufwrite(segto
,&rr
.offset
,-4);
663 static void rdf2_cleanup (int debuginfo
) {
670 /* should write imported & exported symbol declarations to header here */
672 /* generate the output file... */
673 fwrite(RDOFF2Id
,6,1,ofile
); /* file type magic number */
675 if (bsslength
!= 0) /* reserve BSS */
677 bs
.type
= RDFREC_BSS
;
678 bs
.amount
= bsslength
;
684 * calculate overall length of the output object
686 l
= headerlength
+ 4;
688 for (i
= 0; i
< nsegments
; i
++) {
689 if (i
== 2) continue; /* skip BSS segment */
690 l
+= 10 + segments
[i
].seglength
;
692 l
+= 10; /* null segment */
694 fwritelong(l
, ofile
);
696 fwritelong(headerlength
, ofile
);
697 saa_fpwrite(header
,ofile
); /* dump header */
700 for (i
= 0; i
< nsegments
; i
++) {
701 if (i
== 2) continue;
703 fwriteshort(segments
[i
].segtype
, ofile
);
704 fwriteshort(segments
[i
].segnumber
, ofile
);
705 fwriteshort(segments
[i
].segreserved
, ofile
);
706 fwritelong(segments
[i
].seglength
, ofile
);
708 saa_fpwrite(seg
[i
], ofile
);
712 /* null segment - write 10 bytes of zero */
715 fwriteshort(0,ofile
);
720 static long rdf2_segbase (long segment
) {
724 static int rdf2_directive (char *directive
, char *value
, int pass
) {
727 if (! strcmp(directive
, "library")) {
730 r
.reclen
=strlen(value
)+1;
731 strcpy(r
.name
, value
);
732 write_dllmod_rec(&r
);
737 if (! strcmp(directive
, "module")) {
739 r
.type
= RDFREC_MODNAME
;
740 r
.reclen
=strlen(value
)+1;
741 strcpy(r
.name
, value
);
742 write_dllmod_rec(&r
);
750 static void rdf2_filename (char *inname
, char *outname
, efunc error
) {
751 standard_extension(inname
,outname
,".rdf",error
);
754 static const char *rdf2_stdmac
[] = {
755 "%define __SECT__ [section .text]",
756 "%imacro library 1+.nolist",
759 "%imacro module 1+.nolist",
762 "%macro __NASM_CDecl__ 1",
767 static int rdf2_set_info(enum geninfo type
, char **val
)
773 struct ofmt of_rdf2
= {
774 "Relocatable Dynamic Object File Format v2.0",