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
;
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 long bsslength
;
85 static long headerlength
;
87 static void rdf2_init(FILE *fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
89 int segtext
, segdata
, segbss
;
91 /* set up the initial segments */
92 segments
[0].segname
= ".text";
93 segments
[0].segnumber
= 0;
94 segments
[0].segtype
= 1;
95 segments
[0].segreserved
= 0;
96 segments
[0].seglength
= 0;
98 segments
[1].segname
= ".data";
99 segments
[1].segnumber
= 1;
100 segments
[1].segtype
= 2;
101 segments
[1].segreserved
= 0;
102 segments
[1].seglength
= 0;
104 segments
[2].segname
= ".bss";
105 segments
[2].segnumber
= 2;
106 segments
[2].segtype
= 0xFFFF; /* reserved - should never be produced */
107 segments
[2].segreserved
= 0;
108 segments
[2].seglength
= 0;
115 seg
[0] = saa_init(1L);
116 seg
[1] = saa_init(1L);
117 seg
[2] = NULL
; /* special case! */
119 header
= saa_init(1L);
121 segtext
= seg_alloc();
122 segdata
= seg_alloc();
123 segbss
= seg_alloc();
124 if (segtext
!= 0 || segdata
!= 2 || segbss
!= 4)
125 error(ERR_PANIC
,"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
)) p
++;
149 if (*p
) { /* we're now in whitespace */
151 while (*p
&& isspace(80)) *p
++ = '\0';
153 if (*p
) { /* we're now in an attribute value */
155 * see if we have an optional ',number' following the type code
157 if ((q
= strchr(p
, ','))) {
160 reserved
= readnum(q
, &i
);
162 error(ERR_NONFATAL
, "value following comma must be numeric");
167 * check it against the text strings in segmenttypes
170 for (i
= 0; i
< COUNT_SEGTYPES
; i
++)
171 if (!nasm_stricmp(p
, segmenttypes
[i
])) {
172 code
= segmenttypenumbers
[i
];
175 if (code
== -1) { /* didn't find anything */
176 code
= readnum(p
, &i
);
178 error(ERR_NONFATAL
, "unrecognised RDF segment type (%s)",p
);
183 for (i
= 0; i
< nsegments
; i
++) {
184 if (!strcmp(name
, segments
[i
].segname
)) {
185 if (code
!= -1 || reserved
!= 0)
186 error(ERR_NONFATAL
, "segment attributes specified on"
187 " redeclaration of segment");
188 return segments
[i
].segnumber
* 2;
192 /* declaring a new segment! */
195 error(ERR_NONFATAL
, "new segment declared without type code");
198 if (nsegments
== RDF_MAXSEGS
) {
199 error(ERR_FATAL
, "reached compiled-in maximum segment limit (%d)",
204 segments
[nsegments
].segname
= nasm_strdup(name
);
207 error(ERR_PANIC
, "seg_alloc() returned odd number");
208 segments
[nsegments
].segnumber
= i
>> 1;
209 segments
[nsegments
].segtype
= code
;
210 segments
[nsegments
].segreserved
= reserved
;
211 segments
[nsegments
].seglength
= 0;
213 seg
[nsegments
] = saa_init(1L);
220 * Write relocation record
222 static void write_reloc_rec(struct RelocRec
*r
)
226 if (r
->refseg
!= (uint16
)NO_SEG
&& (r
->refseg
& 1)) /* segment base ref */
227 r
->type
= RDFREC_SEGRELOC
;
229 r
->refseg
>>= 1; /* adjust segment nos to RDF rather than NASM */
231 saa_wbytes(header
,&r
->type
,1);
232 saa_wbytes(header
,&r
->reclen
,1);
233 saa_wbytes(header
,&r
->segment
,1);
234 b
= buf
; WRITELONG(b
,r
->offset
);
235 saa_wbytes(header
,buf
,4);
236 saa_wbytes(header
,&r
->length
,1);
237 b
= buf
; WRITESHORT(b
,r
->refseg
);
238 saa_wbytes(header
,buf
,2);
239 headerlength
+= r
->reclen
+ 2;
244 * Write export record
246 static void write_export_rec(struct ExportRec
*r
)
252 saa_wbytes(header
, &r
->type
, 1);
253 saa_wbytes(header
, &r
->reclen
, 1);
254 saa_wbytes(header
, &r
->flags
, 1);
255 saa_wbytes(header
, &r
->segment
, 1);
256 b
= buf
; WRITELONG(b
, r
->offset
);
257 saa_wbytes(header
, buf
, 4);
258 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
259 headerlength
+= r
->reclen
+ 2;
262 static void write_import_rec(struct ImportRec
*r
)
268 saa_wbytes(header
, &r
->type
, 1);
269 saa_wbytes(header
, &r
->reclen
, 1);
270 saa_wbytes(header
, &r
->flags
, 1);
271 b
= buf
; WRITESHORT(b
,r
->segment
);
272 saa_wbytes(header
, buf
, 2);
273 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
274 headerlength
+= r
->reclen
+ 2;
281 static void write_bss_rec(struct BSSRec
*r
)
285 saa_wbytes(header
,&r
->type
,1);
286 saa_wbytes(header
,&r
->reclen
,1);
287 b
= buf
; WRITELONG(b
,r
->amount
);
288 saa_wbytes(header
,buf
,4);
289 headerlength
+= r
->reclen
+ 2;
293 * Write common variable record
295 static void write_common_rec(struct CommonRec
*r
)
301 saa_wbytes(header
, &r
->type
, 1);
302 saa_wbytes(header
, &r
->reclen
, 1);
303 b
= buf
; WRITESHORT(b
,r
->segment
);
304 saa_wbytes(header
, buf
, 2);
305 b
= buf
; WRITELONG(b
, r
->size
);
306 saa_wbytes(header
,buf
,4);
307 b
= buf
; WRITESHORT(b
, r
->align
);
308 saa_wbytes(header
, buf
, 2);
309 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
310 headerlength
+= r
->reclen
+ 2;
314 * Write library record
316 static void write_dll_rec(struct DLLRec
*r
)
318 saa_wbytes(header
,&r
->type
,1);
319 saa_wbytes(header
,&r
->reclen
,1);
320 saa_wbytes(header
, r
->libname
, strlen(r
->libname
) + 1);
321 headerlength
+= r
->reclen
+ 2;
326 * Write module name record
328 static void write_modname_rec(struct ModRec
*r
)
330 saa_wbytes(header
,&r
->type
,1);
331 saa_wbytes(header
,&r
->reclen
,1);
332 saa_wbytes(header
, r
->modname
, strlen(r
->modname
) + 1);
333 headerlength
+= r
->reclen
+ 2;
338 * Handle export, import and common records.
340 static void rdf2_deflabel(char *name
, long segment
, long offset
,
341 int is_global
, char *special
)
346 static int farsym
= 0;
351 /* Check if the label length is OK */
352 if ((len
= strlen(name
)) >= EXIM_LABEL_MAX
) {
353 error(ERR_NONFATAL
, "label size exceeds %d bytes", EXIM_LABEL_MAX
);
357 error(ERR_NONFATAL
, "zero-length label");
361 if (is_global
== 2) {
362 /* Common variable */
363 ci
.type
= RDFREC_COMMON
;
365 ci
.segment
= segment
;
366 strcpy(ci
.label
, name
);
371 * Check the special text to see if it's a valid number and power
372 * of two; if so, store it as the alignment for the common variable.
376 ci
.align
= readnum(special
, &err
);
378 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
379 " valid number", special
);
380 else if ( (ci
.align
| (ci
.align
-1)) != 2*ci
.align
- 1)
381 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
382 " power of two", special
);
384 write_common_rec(&ci
);
387 /* We don't care about local labels or fix-up hints */
388 if (is_global
!= 1) return;
391 while(*special
== ' ' || *special
== '\t') special
++;
393 if (!nasm_strnicmp(special
, "export", 6)) {
395 symflags
|= SYM_GLOBAL
;
396 } else if (!nasm_strnicmp(special
, "import", 6)) {
398 symflags
|= SYM_IMPORT
;
402 while (isspace(*special
)) special
++;
403 if (!nasm_stricmp(special
, "far")) {
405 } else if (!nasm_stricmp(special
, "near")) {
407 } else if (!nasm_stricmp(special
, "proc") ||
408 !nasm_stricmp(special
, "function")) {
409 symflags
|= SYM_FUNCTION
;
410 } else if (!nasm_stricmp(special
, "data") ||
411 !nasm_stricmp(special
, "object")) {
412 symflags
|= SYM_DATA
;
414 error(ERR_NONFATAL
, "unrecognised symbol type `%s'", special
);
418 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
419 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
423 for (i
= 0; i
< nsegments
; i
++) {
424 if (segments
[i
].segnumber
== segment
>>1) break;
427 if (i
>= nsegments
) { /* EXTERN declaration */
428 ri
.type
= farsym
? RDFREC_FARIMPORT
: RDFREC_IMPORT
;
429 if (symflags
& SYM_GLOBAL
)
430 error(ERR_NONFATAL
, "symbol type conflict - EXTERN cannot be EXPORT");
432 ri
.segment
= segment
;
433 strcpy(ri
.label
, name
);
435 write_import_rec(&ri
);
436 } else if (is_global
) {
437 r
.type
= RDFREC_GLOBAL
; /* GLOBAL declaration */
438 if (symflags
& SYM_IMPORT
)
439 error(ERR_NONFATAL
, "symbol type conflict - GLOBAL cannot be IMPORT");
443 strcpy(r
.label
, name
);
445 write_export_rec(&r
);
449 static void membufwrite(int segment
, const void * data
, int bytes
)
454 for (i
= 0; i
< nsegments
; i
++) {
455 if (segments
[i
].segnumber
== segment
) break;
458 error(ERR_PANIC
, "can't find segment %d", segment
);
463 WRITESHORT(b
,*(short *)data
);
465 WRITELONG(b
,*(long *)data
);
469 segments
[i
].seglength
+= bytes
;
470 saa_wbytes(seg
[i
],data
,bytes
);
473 static int getsegmentlength(int segment
)
476 for (i
= 0; i
< nsegments
; i
++) {
477 if (segments
[i
].segnumber
== segment
) break;
480 error(ERR_PANIC
, "can't find segment %d", segment
);
482 return segments
[i
].seglength
;
485 static void rdf2_out (long segto
, const void *data
, unsigned long type
,
486 long segment
, long wrt
)
488 long bytes
= type
& OUT_SIZMASK
;
490 unsigned char databuf
[4],*pd
;
493 if (segto
== NO_SEG
) {
494 if ((type
& OUT_TYPMASK
) != OUT_RESERVE
)
495 error (ERR_NONFATAL
, "attempt to assemble code in ABSOLUTE space");
499 segto
>>= 1; /* convert NASM segment no to RDF number */
501 for (seg
= 0; seg
< nsegments
; seg
++) {
502 if (segments
[seg
].segnumber
== segto
) break;
504 if (seg
>= nsegments
) {
505 error(ERR_NONFATAL
,"specified segment not supported by rdf output format");
510 wrt
= NO_SEG
; /* continue to do _something_ */
511 error (ERR_NONFATAL
, "WRT not supported by rdf output format");
516 if (segto
== 2 && type
!= OUT_RESERVE
)
518 error(ERR_NONFATAL
, "BSS segments may not be initialised");
520 /* just reserve the space for now... */
522 if (type
== OUT_REL2ADR
)
529 if (type
== OUT_RESERVE
) {
530 if (segto
== 2) /* BSS segment space reserverd */
534 membufwrite(segto
,databuf
,1);
536 else if (type
== OUT_RAWDATA
) {
537 if (segment
!= NO_SEG
)
538 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
540 membufwrite(segto
,data
,bytes
);
542 else if (type
== OUT_ADDRESS
) {
544 /* if segment == NO_SEG then we are writing an address of an
545 object within the same segment - do not produce reloc rec. */
547 /* FIXME - is this behaviour sane? at first glance it doesn't
548 appear to be. Must test this thoroughly...! */
550 if (segment
!= NO_SEG
)
552 /* it's an address, so we must write a relocation record */
554 rr
.type
= RDFREC_RELOC
; /* type signature */
556 rr
.segment
= segto
; /* segment we're currently in */
557 rr
.offset
= getsegmentlength(segto
); /* current offset */
558 rr
.length
= bytes
; /* length of reference */
559 rr
.refseg
= segment
; /* segment referred to */
560 write_reloc_rec(&rr
);
563 pd
= databuf
; /* convert address to little-endian */
565 WRITESHORT (pd
, *(long *)data
);
567 WRITELONG (pd
, *(long *)data
);
569 membufwrite(segto
,databuf
,bytes
);
572 else if (type
== OUT_REL2ADR
)
574 if (segment
== segto
)
575 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
578 rr
.offset
= getsegmentlength(segto
); /* current offset */
579 rr
.length
= 2; /* length of reference */
580 rr
.refseg
= segment
; /* segment referred to (will be >>1'd)*/
582 if (segment
!= NO_SEG
&& segment
% 2) {
583 rr
.type
= RDFREC_SEGRELOC
;
584 rr
.segment
= segto
; /* memory base refs *aren't ever* relative! */
585 write_reloc_rec(&rr
);
587 /* what do we put in the code? Simply the data. This should almost
588 * always be zero, unless someone's doing segment arithmetic...
590 rr
.offset
= *(long *) data
;
594 rr
.type
= RDFREC_RELOC
; /* type signature */
595 rr
.segment
= segto
+64; /* segment we're currently in + rel flag */
596 write_reloc_rec(&rr
);
598 /* work out what to put in the code: offset of the end of this operand,
599 * subtracted from any data specified, so that loader can just add
600 * address of imported symbol onto it to get address relative to end of
601 * instruction: import_address + data(offset) - end_of_instrn */
603 rr
.offset
= *(long *)data
-(rr
.offset
+ bytes
);
606 membufwrite(segto
,&rr
.offset
,-2);
608 else if (type
== OUT_REL4ADR
)
610 if (segment
== segto
)
611 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
612 if (segment
!= NO_SEG
&& segment
% 2) {
613 error(ERR_PANIC
, "erm... 4 byte segment base ref?");
616 rr
.type
= RDFREC_RELOC
; /* type signature */
617 rr
.segment
= segto
+64; /* segment we're currently in + rel tag */
618 rr
.offset
= getsegmentlength(segto
); /* current offset */
619 rr
.length
= 4; /* length of reference */
620 rr
.refseg
= segment
; /* segment referred to */
622 write_reloc_rec(&rr
);
624 rr
.offset
= *(long *)data
-(rr
.offset
+ bytes
);
626 membufwrite(segto
,&rr
.offset
,-4);
630 static void rdf2_cleanup (int debuginfo
) {
637 /* should write imported & exported symbol declarations to header here */
639 /* generate the output file... */
640 fwrite(RDOFF2Id
,6,1,ofile
); /* file type magic number */
642 if (bsslength
!= 0) { /* reserve BSS */
643 bs
.type
= RDFREC_BSS
;
644 bs
.amount
= bsslength
;
650 * calculate overall length of the output object
652 l
= headerlength
+ 4;
654 for (i
= 0; i
< nsegments
; i
++) {
655 if (i
== 2) continue; /* skip BSS segment */
656 l
+= 10 + segments
[i
].seglength
;
658 l
+= 10; /* null segment */
660 fwritelong(l
, ofile
);
662 fwritelong(headerlength
, ofile
);
663 saa_fpwrite(header
,ofile
); /* dump header */
666 for (i
= 0; i
< nsegments
; i
++) {
667 if (i
== 2) continue;
669 fwriteshort(segments
[i
].segtype
, ofile
);
670 fwriteshort(segments
[i
].segnumber
, ofile
);
671 fwriteshort(segments
[i
].segreserved
, ofile
);
672 fwritelong(segments
[i
].seglength
, ofile
);
674 saa_fpwrite(seg
[i
], ofile
);
678 /* null segment - write 10 bytes of zero */
681 fwriteshort(0,ofile
);
686 static long rdf2_segbase (long segment
) {
692 * Handle RDOFF2 specific directives
694 static int rdf2_directive (char *directive
, char *value
, int pass
)
698 /* Check if the name length is OK */
699 if ((n
= strlen(value
)) >= MODLIB_NAME_MAX
) {
700 error(ERR_NONFATAL
, "name size exceeds %d bytes", MODLIB_NAME_MAX
);
704 if (! strcmp(directive
, "library")) {
709 strcpy(r
.libname
, value
);
715 if (! strcmp(directive
, "module")) {
718 r
.type
= RDFREC_MODNAME
;
720 strcpy(r
.modname
, value
);
721 write_modname_rec(&r
);
729 static void rdf2_filename (char *inname
, char *outname
, efunc error
) {
730 standard_extension(inname
,outname
,".rdf",error
);
733 static const char *rdf2_stdmac
[] = {
734 "%define __SECT__ [section .text]",
735 "%imacro library 1+.nolist",
738 "%imacro module 1+.nolist",
741 "%macro __NASM_CDecl__ 1",
746 static int rdf2_set_info(enum geninfo type
, char **val
)
752 struct ofmt of_rdf2
= {
753 "Relocatable Dynamic Object File Format v2.0",