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
48 byte type
; /* must be 1, or 6 for segment base ref */
49 byte reclen
; /* set to 8 */
50 byte segment
; /* only 0 for code, or 1 for data supported,
51 * but add 64 for relative refs (ie do not require
52 * reloc @ loadtime, only linkage) */
53 long offset
; /* from start of segment in which reference is loc'd */
54 byte length
; /* 1 2 or 4 bytes */
55 int16 refseg
; /* segment to which reference refers to */
59 byte type
; /* must be 2, or 7 for FAR import */
60 byte reclen
; /* equals 3+label length */
61 int16 segment
; /* segment number allocated to the label for reloc
62 * records - label is assumed to be at offset zero
63 * in this segment, so linker must fix up with offset
64 * of segment and of offset within segment */
65 char label
[33]; /* zero terminated... should be written to file until
66 * the zero, but not after it - max len = 32 chars */
70 byte type
; /* must be 3 */
71 byte reclen
; /* equals 6+label length */
72 byte segment
; /* segment referred to (0/1) */
73 long offset
; /* offset within segment */
74 char label
[33]; /* zero terminated as above. max len = 32 chars */
78 byte type
; /* must be 4 */
79 byte reclen
; /* equals 1+library name */
80 char libname
[128]; /* name of library to link with at load time */
84 byte type
; /* must be 5 */
85 byte reclen
; /* equeals 4 */
86 long amount
; /* number of bytes BSS to reserve */
89 #define COUNT_SEGTYPES 9
91 static char * segmenttypes
[COUNT_SEGTYPES
] = {
92 "null", "text", "code", "data", "comment", "lcomment", "pcomment",
93 "symdebug", "linedebug"
96 static int segmenttypenumbers
[COUNT_SEGTYPES
] = {
97 0, 1, 1, 2, 3, 4, 5, 6, 7
100 /* code for managing buffers needed to seperate code and data into individual
101 * sections until they are ready to be written to the file.
102 * We'd better hope that it all fits in memory else we're buggered... */
104 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
105 * on 80x86 machines for efficiency */
107 /***********************************************************************
108 * Actual code to deal with RDOFF2 ouput format begins here...
111 /* global variables set during the initialisation phase */
113 static struct SAA
*seg
[RDF_MAXSEGS
]; /* seg 0 = code, seg 1 = data */
114 static struct SAA
*header
; /* relocation/import/export records */
120 static struct seginfo
{
126 } segments
[RDF_MAXSEGS
];
128 static int nsegments
;
130 static long bsslength
;
131 static long headerlength
;
133 static void rdf2_init(FILE *fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
135 int segtext
, segdata
, segbss
;
137 /* set up the initial segments */
138 segments
[0].segname
= ".text";
139 segments
[0].segnumber
= 0;
140 segments
[0].segtype
= 1;
141 segments
[0].segreserved
= 0;
142 segments
[0].seglength
= 0;
144 segments
[1].segname
= ".data";
145 segments
[1].segnumber
= 1;
146 segments
[1].segtype
= 2;
147 segments
[1].segreserved
= 0;
148 segments
[1].seglength
= 0;
150 segments
[2].segname
= ".bss";
151 segments
[2].segnumber
= 2;
152 segments
[2].segtype
= 0xFFFF; /* reserved - should never be produced */
153 segments
[2].segreserved
= 0;
154 segments
[2].seglength
= 0;
161 seg
[0] = saa_init(1L);
162 seg
[1] = saa_init(1L);
163 seg
[2] = NULL
; /* special case! */
165 header
= saa_init(1L);
167 segtext
= seg_alloc();
168 segdata
= seg_alloc();
169 segbss
= seg_alloc();
170 if (segtext
!= 0 || segdata
!= 2 || segbss
!= 4)
171 error(ERR_PANIC
,"rdf segment numbers not allocated as expected (%d,%d,%d)",
172 segtext
,segdata
,segbss
);
177 static long rdf2_section_names(char *name
, int pass
, int *bits
)
185 * Default is 32 bits, in the text segment.
192 /* look for segment type code following segment name */
194 while (*p
&& !isspace(*p
)) p
++;
195 if (*p
) { /* we're now in whitespace */
197 while (*p
&& isspace(80)) *p
++ = '\0';
199 if (*p
) { /* we're now in an attribute value */
201 * see if we have an optional ',number' following the type code
203 if ((q
= strchr(p
, ','))) {
206 reserved
= readnum(q
, &i
);
208 error(ERR_NONFATAL
, "value following comma must be numeric");
213 * check it against the text strings in segmenttypes
216 for (i
= 0; i
< COUNT_SEGTYPES
; i
++)
217 if (!nasm_stricmp(p
, segmenttypes
[i
])) {
218 code
= segmenttypenumbers
[i
];
221 if (code
== -1) { /* didn't find anything */
222 code
= readnum(p
, &i
);
224 error(ERR_NONFATAL
, "unrecognised RDF segment type (%s)",p
);
229 for (i
= 0; i
< nsegments
; i
++) {
230 if (!strcmp(name
, segments
[i
].segname
)) {
231 if (code
!= -1 || reserved
!= 0)
232 error(ERR_NONFATAL
, "segment attributes specified on"
233 " redeclaration of segment");
234 return segments
[i
].segnumber
* 2;
238 /* declaring a new segment! */
241 error(ERR_NONFATAL
, "new segment declared without type code");
244 if (nsegments
== RDF_MAXSEGS
) {
245 error(ERR_FATAL
, "reached compiled-in maximum segment limit (%d)",
250 segments
[nsegments
].segname
= nasm_strdup(name
);
253 error(ERR_PANIC
, "seg_alloc() returned odd number");
254 segments
[nsegments
].segnumber
= i
>> 1;
255 segments
[nsegments
].segtype
= code
;
256 segments
[nsegments
].segreserved
= reserved
;
257 segments
[nsegments
].seglength
= 0;
259 seg
[nsegments
] = saa_init(1L);
264 static void write_reloc_rec(struct RelocRec
*r
)
268 if (r
->refseg
!= (int16
)NO_SEG
&& (r
->refseg
& 1)) /* segment base ref */
271 r
->refseg
>>= 1; /* adjust segment nos to RDF rather than NASM */
273 saa_wbytes(header
,&r
->type
,1);
274 saa_wbytes(header
,&r
->reclen
,1);
275 saa_wbytes(header
,&r
->segment
,1);
276 b
= buf
; WRITELONG(b
,r
->offset
);
277 saa_wbytes(header
,buf
,4);
278 saa_wbytes(header
,&r
->length
,1);
279 b
= buf
; WRITESHORT(b
,r
->refseg
);
280 saa_wbytes(header
,buf
,2);
281 headerlength
+= r
->reclen
+ 2;
284 static void write_export_rec(struct ExportRec
*r
)
290 saa_wbytes(header
,&r
->type
,1);
291 saa_wbytes(header
,&r
->reclen
,1);
292 saa_wbytes(header
,&r
->segment
,1);
293 b
= buf
; WRITELONG(b
,r
->offset
);
294 saa_wbytes(header
,buf
,4);
295 saa_wbytes(header
,r
->label
,strlen(r
->label
) + 1);
296 headerlength
+= r
->reclen
+ 2;
299 static void write_import_rec(struct ImportRec
*r
)
305 saa_wbytes(header
,&r
->type
,1);
306 saa_wbytes(header
,&r
->reclen
,1);
307 b
= buf
; WRITESHORT(b
,r
->segment
);
308 saa_wbytes(header
,buf
,2);
309 saa_wbytes(header
,r
->label
,strlen(r
->label
) + 1);
310 headerlength
+= r
->reclen
+ 2;
313 static void write_bss_rec(struct BSSRec
*r
)
317 saa_wbytes(header
,&r
->type
,1);
318 saa_wbytes(header
,&r
->reclen
,1);
319 b
= buf
; WRITELONG(b
,r
->amount
);
320 saa_wbytes(header
,buf
,4);
321 headerlength
+= r
->reclen
+ 2;
324 static void write_dll_rec(struct DLLRec
*r
)
326 saa_wbytes(header
,&r
->type
,1);
327 saa_wbytes(header
,&r
->reclen
,1);
328 saa_wbytes(header
,r
->libname
,strlen(r
->libname
) + 1);
329 headerlength
+= r
->reclen
+ 2;
332 static void rdf2_deflabel(char *name
, long segment
, long offset
,
333 int is_global
, char *special
)
337 #ifdef VERBOSE_WARNINGS
338 static int warned_common
= 0;
340 static int farsym
= 0;
344 while(*special
== ' ' || *special
== '\t') special
++;
346 if (!nasm_stricmp(special
, "far")) {
349 else if (!nasm_stricmp(special
, "near")) {
353 error(ERR_NONFATAL
, "unrecognised symbol type `%s'", special
);
356 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
357 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
361 if (is_global
== 2) {
362 #ifdef VERBOSE_WARNINGS
363 if (!warned_common
) {
364 error(ERR_WARNING
,"common declarations not supported: using extern");
371 for (i
= 0; i
< nsegments
; i
++) {
372 if (segments
[i
].segnumber
== segment
>>1) break;
374 if (i
>= nsegments
) { /* EXTERN declaration */
379 ri
.segment
= segment
;
380 strncpy(ri
.label
,name
,32);
382 ri
.reclen
= 3 + strlen(ri
.label
);
383 write_import_rec(&ri
);
384 } else if (is_global
) {
388 strncpy(r
.label
,name
,32);
390 r
.reclen
= 6 + strlen(r
.label
);
391 write_export_rec(&r
);
395 static void membufwrite(int segment
, void * data
, int bytes
)
400 for (i
= 0; i
< nsegments
; i
++) {
401 if (segments
[i
].segnumber
== segment
) break;
404 error(ERR_PANIC
, "can't find segment %d", segment
);
409 WRITESHORT(b
,*(short *)data
);
411 WRITELONG(b
,*(long *)data
);
415 segments
[i
].seglength
+= bytes
;
416 saa_wbytes(seg
[i
],data
,bytes
);
419 static int getsegmentlength(int segment
)
422 for (i
= 0; i
< nsegments
; i
++) {
423 if (segments
[i
].segnumber
== segment
) break;
426 error(ERR_PANIC
, "can't find segment %d", segment
);
428 return segments
[i
].seglength
;
431 static void rdf2_out (long segto
, void *data
, unsigned long type
,
432 long segment
, long wrt
)
434 long bytes
= type
& OUT_SIZMASK
;
436 unsigned char databuf
[4],*pd
;
439 if (segto
== NO_SEG
) {
440 if ((type
& OUT_TYPMASK
) != OUT_RESERVE
)
441 error (ERR_NONFATAL
, "attempt to assemble code in ABSOLUTE space");
445 segto
>>= 1; /* convert NASM segment no to RDF number */
447 for (seg
= 0; seg
< nsegments
; seg
++) {
448 if (segments
[seg
].segnumber
== segto
) break;
450 if (seg
>= nsegments
) {
451 error(ERR_NONFATAL
,"specified segment not supported by rdf output format");
456 wrt
= NO_SEG
; /* continue to do _something_ */
457 error (ERR_NONFATAL
, "WRT not supported by rdf output format");
462 if (segto
== 2 && type
!= OUT_RESERVE
)
464 error(ERR_NONFATAL
, "BSS segments may not be initialised");
466 /* just reserve the space for now... */
468 if (type
== OUT_REL2ADR
)
475 if (type
== OUT_RESERVE
) {
476 if (segto
== 2) /* BSS segment space reserverd */
480 membufwrite(segto
,databuf
,1);
482 else if (type
== OUT_RAWDATA
) {
483 if (segment
!= NO_SEG
)
484 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
486 membufwrite(segto
,data
,bytes
);
488 else if (type
== OUT_ADDRESS
) {
490 /* if segment == NO_SEG then we are writing an address of an
491 object within the same segment - do not produce reloc rec. */
493 /* FIXME - is this behaviour sane? at first glance it doesn't
494 appear to be. Must test this thoroughly...! */
496 if (segment
!= NO_SEG
)
498 /* it's an address, so we must write a relocation record */
500 rr
.type
= 1; /* type signature */
502 rr
.segment
= segto
; /* segment we're currently in */
503 rr
.offset
= getsegmentlength(segto
); /* current offset */
504 rr
.length
= bytes
; /* length of reference */
505 rr
.refseg
= segment
; /* segment referred to */
506 write_reloc_rec(&rr
);
509 pd
= databuf
; /* convert address to little-endian */
511 WRITESHORT (pd
, *(long *)data
);
513 WRITELONG (pd
, *(long *)data
);
515 membufwrite(segto
,databuf
,bytes
);
518 else if (type
== OUT_REL2ADR
)
520 if (segment
== segto
)
521 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
524 rr
.offset
= getsegmentlength(segto
); /* current offset */
525 rr
.length
= 2; /* length of reference */
526 rr
.refseg
= segment
; /* segment referred to (will be >>1'd)*/
528 if (segment
!= NO_SEG
&& segment
% 2) {
530 rr
.segment
= segto
; /* memory base refs *aren't ever* relative! */
531 write_reloc_rec(&rr
);
533 /* what do we put in the code? Simply the data. This should almost
534 * always be zero, unless someone's doing segment arithmetic...
536 rr
.offset
= *(long *) data
;
540 rr
.type
= 1; /* type signature */
541 rr
.segment
= segto
+64; /* segment we're currently in + rel flag */
542 write_reloc_rec(&rr
);
544 /* work out what to put in the code: offset of the end of this operand,
545 * subtracted from any data specified, so that loader can just add
546 * address of imported symbol onto it to get address relative to end of
547 * instruction: import_address + data(offset) - end_of_instrn */
549 rr
.offset
= *(long *)data
-(rr
.offset
+ bytes
);
552 membufwrite(segto
,&rr
.offset
,-2);
554 else if (type
== OUT_REL4ADR
)
556 if (segment
== segto
)
557 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
558 if (segment
!= NO_SEG
&& segment
% 2) {
559 error(ERR_PANIC
, "erm... 4 byte segment base ref?");
562 rr
.type
= 1; /* type signature */
563 rr
.segment
= segto
+64; /* segment we're currently in + rel tag */
564 rr
.offset
= getsegmentlength(segto
); /* current offset */
565 rr
.length
= 4; /* length of reference */
566 rr
.refseg
= segment
; /* segment referred to */
568 write_reloc_rec(&rr
);
570 rr
.offset
= *(long *)data
-(rr
.offset
+ bytes
);
572 membufwrite(segto
,&rr
.offset
,-4);
576 static void rdf2_cleanup (int debuginfo
) {
583 /* should write imported & exported symbol declarations to header here */
585 /* generate the output file... */
586 fwrite(RDOFF2Id
,6,1,ofile
); /* file type magic number */
588 if (bsslength
!= 0) /* reserve BSS */
591 bs
.amount
= bsslength
;
597 * calculate overall length of the output object
599 l
= headerlength
+ 4;
601 for (i
= 0; i
< nsegments
; i
++) {
602 if (i
== 2) continue; /* skip BSS segment */
603 l
+= 10 + segments
[i
].seglength
;
605 l
+= 10; /* null segment */
607 fwritelong(l
, ofile
);
609 fwritelong(headerlength
, ofile
);
610 saa_fpwrite(header
,ofile
); /* dump header */
613 for (i
= 0; i
< nsegments
; i
++) {
614 if (i
== 2) continue;
616 fwriteshort(segments
[i
].segtype
, ofile
);
617 fwriteshort(segments
[i
].segnumber
, ofile
);
618 fwriteshort(segments
[i
].segreserved
, ofile
);
619 fwritelong(segments
[i
].seglength
, ofile
);
621 saa_fpwrite(seg
[i
], ofile
);
625 /* null segment - write 10 bytes of zero */
628 fwriteshort(0,ofile
);
633 static long rdf2_segbase (long segment
) {
637 static int rdf2_directive (char *directive
, char *value
, int pass
) {
640 if (! strcmp(directive
, "library")) {
643 strcpy(r
.libname
, value
);
652 static void rdf2_filename (char *inname
, char *outname
, efunc error
) {
653 standard_extension(inname
,outname
,".rdf",error
);
656 static char *rdf2_stdmac
[] = {
657 "%define __SECT__ [section .text]",
658 "%imacro library 1+.nolist",
661 "%macro __NASM_CDecl__ 1",
666 static int rdf2_set_info(enum geninfo type
, char **val
)
672 struct ofmt of_rdf2
= {
673 "Relocatable Dynamic Object File Format v2.0",