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 5 */
79 byte reclen
; /* equals 4 */
80 long amount
; /* number of bytes BSS to reserve */
84 byte type
; /* 4 for DLLRec, 8 for ModRec */
85 byte reclen
; /* 1+lib name length for DLLRec, 1+mod name length */
86 char name
[128]; /* library to link at load time or module name */
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;
325 * Write library record. Also used for module name records.
327 static void write_dllmod_rec(struct DLLModRec
*r
)
329 saa_wbytes(header
,&r
->type
,1);
330 saa_wbytes(header
,&r
->reclen
,1);
331 saa_wbytes(header
,r
->name
,strlen(r
->name
) + 1);
332 headerlength
+= r
->reclen
+ 2;
335 static void rdf2_deflabel(char *name
, long segment
, long offset
,
336 int is_global
, char *special
)
340 static int farsym
= 0;
343 if (is_global
!= 1) return;
346 while(*special
== ' ' || *special
== '\t') special
++;
348 if (!nasm_stricmp(special
, "far")) {
351 else if (!nasm_stricmp(special
, "near")) {
355 error(ERR_NONFATAL
, "unrecognised symbol type `%s'", special
);
358 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
359 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
363 for (i
= 0; i
< nsegments
; i
++) {
364 if (segments
[i
].segnumber
== segment
>>1) break;
366 if (i
>= nsegments
) { /* EXTERN declaration */
371 ri
.segment
= segment
;
372 strncpy(ri
.label
,name
,32);
374 ri
.reclen
= 3 + strlen(ri
.label
);
375 write_import_rec(&ri
);
376 } else if (is_global
) {
380 strncpy(r
.label
,name
,32);
382 r
.reclen
= 6 + strlen(r
.label
);
383 write_export_rec(&r
);
387 static void membufwrite(int segment
, void * data
, int bytes
)
392 for (i
= 0; i
< nsegments
; i
++) {
393 if (segments
[i
].segnumber
== segment
) break;
396 error(ERR_PANIC
, "can't find segment %d", segment
);
401 WRITESHORT(b
,*(short *)data
);
403 WRITELONG(b
,*(long *)data
);
407 segments
[i
].seglength
+= bytes
;
408 saa_wbytes(seg
[i
],data
,bytes
);
411 static int getsegmentlength(int segment
)
414 for (i
= 0; i
< nsegments
; i
++) {
415 if (segments
[i
].segnumber
== segment
) break;
418 error(ERR_PANIC
, "can't find segment %d", segment
);
420 return segments
[i
].seglength
;
423 static void rdf2_out (long segto
, void *data
, unsigned long type
,
424 long segment
, long wrt
)
426 long bytes
= type
& OUT_SIZMASK
;
428 unsigned char databuf
[4],*pd
;
431 if (segto
== NO_SEG
) {
432 if ((type
& OUT_TYPMASK
) != OUT_RESERVE
)
433 error (ERR_NONFATAL
, "attempt to assemble code in ABSOLUTE space");
437 segto
>>= 1; /* convert NASM segment no to RDF number */
439 for (seg
= 0; seg
< nsegments
; seg
++) {
440 if (segments
[seg
].segnumber
== segto
) break;
442 if (seg
>= nsegments
) {
443 error(ERR_NONFATAL
,"specified segment not supported by rdf output format");
448 wrt
= NO_SEG
; /* continue to do _something_ */
449 error (ERR_NONFATAL
, "WRT not supported by rdf output format");
454 if (segto
== 2 && type
!= OUT_RESERVE
)
456 error(ERR_NONFATAL
, "BSS segments may not be initialised");
458 /* just reserve the space for now... */
460 if (type
== OUT_REL2ADR
)
467 if (type
== OUT_RESERVE
) {
468 if (segto
== 2) /* BSS segment space reserverd */
472 membufwrite(segto
,databuf
,1);
474 else if (type
== OUT_RAWDATA
) {
475 if (segment
!= NO_SEG
)
476 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
478 membufwrite(segto
,data
,bytes
);
480 else if (type
== OUT_ADDRESS
) {
482 /* if segment == NO_SEG then we are writing an address of an
483 object within the same segment - do not produce reloc rec. */
485 /* FIXME - is this behaviour sane? at first glance it doesn't
486 appear to be. Must test this thoroughly...! */
488 if (segment
!= NO_SEG
)
490 /* it's an address, so we must write a relocation record */
492 rr
.type
= 1; /* type signature */
494 rr
.segment
= segto
; /* segment we're currently in */
495 rr
.offset
= getsegmentlength(segto
); /* current offset */
496 rr
.length
= bytes
; /* length of reference */
497 rr
.refseg
= segment
; /* segment referred to */
498 write_reloc_rec(&rr
);
501 pd
= databuf
; /* convert address to little-endian */
503 WRITESHORT (pd
, *(long *)data
);
505 WRITELONG (pd
, *(long *)data
);
507 membufwrite(segto
,databuf
,bytes
);
510 else if (type
== OUT_REL2ADR
)
512 if (segment
== segto
)
513 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
516 rr
.offset
= getsegmentlength(segto
); /* current offset */
517 rr
.length
= 2; /* length of reference */
518 rr
.refseg
= segment
; /* segment referred to (will be >>1'd)*/
520 if (segment
!= NO_SEG
&& segment
% 2) {
522 rr
.segment
= segto
; /* memory base refs *aren't ever* relative! */
523 write_reloc_rec(&rr
);
525 /* what do we put in the code? Simply the data. This should almost
526 * always be zero, unless someone's doing segment arithmetic...
528 rr
.offset
= *(long *) data
;
532 rr
.type
= 1; /* type signature */
533 rr
.segment
= segto
+64; /* segment we're currently in + rel flag */
534 write_reloc_rec(&rr
);
536 /* work out what to put in the code: offset of the end of this operand,
537 * subtracted from any data specified, so that loader can just add
538 * address of imported symbol onto it to get address relative to end of
539 * instruction: import_address + data(offset) - end_of_instrn */
541 rr
.offset
= *(long *)data
-(rr
.offset
+ bytes
);
544 membufwrite(segto
,&rr
.offset
,-2);
546 else if (type
== OUT_REL4ADR
)
548 if (segment
== segto
)
549 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
550 if (segment
!= NO_SEG
&& segment
% 2) {
551 error(ERR_PANIC
, "erm... 4 byte segment base ref?");
554 rr
.type
= 1; /* type signature */
555 rr
.segment
= segto
+64; /* segment we're currently in + rel tag */
556 rr
.offset
= getsegmentlength(segto
); /* current offset */
557 rr
.length
= 4; /* length of reference */
558 rr
.refseg
= segment
; /* segment referred to */
560 write_reloc_rec(&rr
);
562 rr
.offset
= *(long *)data
-(rr
.offset
+ bytes
);
564 membufwrite(segto
,&rr
.offset
,-4);
568 static void rdf2_cleanup (int debuginfo
) {
575 /* should write imported & exported symbol declarations to header here */
577 /* generate the output file... */
578 fwrite(RDOFF2Id
,6,1,ofile
); /* file type magic number */
580 if (bsslength
!= 0) /* reserve BSS */
583 bs
.amount
= bsslength
;
589 * calculate overall length of the output object
591 l
= headerlength
+ 4;
593 for (i
= 0; i
< nsegments
; i
++) {
594 if (i
== 2) continue; /* skip BSS segment */
595 l
+= 10 + segments
[i
].seglength
;
597 l
+= 10; /* null segment */
599 fwritelong(l
, ofile
);
601 fwritelong(headerlength
, ofile
);
602 saa_fpwrite(header
,ofile
); /* dump header */
605 for (i
= 0; i
< nsegments
; i
++) {
606 if (i
== 2) continue;
608 fwriteshort(segments
[i
].segtype
, ofile
);
609 fwriteshort(segments
[i
].segnumber
, ofile
);
610 fwriteshort(segments
[i
].segreserved
, ofile
);
611 fwritelong(segments
[i
].seglength
, ofile
);
613 saa_fpwrite(seg
[i
], ofile
);
617 /* null segment - write 10 bytes of zero */
620 fwriteshort(0,ofile
);
625 static long rdf2_segbase (long segment
) {
629 static int rdf2_directive (char *directive
, char *value
, int pass
) {
632 if (! strcmp(directive
, "library")) {
635 r
.reclen
=strlen(value
)+1;
636 strcpy(r
.name
, value
);
637 write_dllmod_rec(&r
);
642 if (! strcmp(directive
, "module")) {
645 r
.reclen
=strlen(value
)+1;
646 strcpy(r
.name
, value
);
647 write_dllmod_rec(&r
);
655 static void rdf2_filename (char *inname
, char *outname
, efunc error
) {
656 standard_extension(inname
,outname
,".rdf",error
);
659 static char *rdf2_stdmac
[] = {
660 "%define __SECT__ [section .text]",
661 "%imacro library 1+.nolist",
664 "%imacro module 1+.nolist",
667 "%macro __NASM_CDecl__ 1",
672 static int rdf2_set_info(enum geninfo type
, char **val
)
678 struct ofmt of_rdf2
= {
679 "Relocatable Dynamic Object File Format v2.0",