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 license given in the file "LICENSE"
9 * distributed in the NASM archive.
26 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
27 #define VERBOSE_WARNINGS
31 #include "rdoff/rdoff.h"
33 /* This signature is written to start of RDOFF files */
34 static const char *RDOFF2Id
= RDOFF2_SIGNATURE
;
36 /* Note that whenever a segment is referred to in the RDOFF file, its number
37 * is always half of the segment number that NASM uses to refer to it; this
38 * is because NASM only allocates even numbered segments, so as to not
39 * waste any of the 16 bits of segment number written to the file - this
40 * allows up to 65533 external labels to be defined; otherwise it would be
43 #define COUNT_SEGTYPES 9
45 static char *segmenttypes
[COUNT_SEGTYPES
] = {
46 "null", "text", "code", "data",
47 "comment", "lcomment", "pcomment",
48 "symdebug", "linedebug"
51 static int segmenttypenumbers
[COUNT_SEGTYPES
] = {
52 0, 1, 1, 2, 3, 4, 5, 6, 7
55 /* code for managing buffers needed to separate code and data into individual
56 * sections until they are ready to be written to the file.
57 * We'd better hope that it all fits in memory else we're buggered... */
59 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
60 * on 80x86 machines for efficiency */
62 /***********************************************************************
63 * Actual code to deal with RDOFF2 ouput format begins here...
66 /* global variables set during the initialisation phase */
68 static struct SAA
*seg
[RDF_MAXSEGS
]; /* seg 0 = code, seg 1 = data */
69 static struct SAA
*header
; /* relocation/import/export records */
75 static struct seginfo
{
81 } segments
[RDF_MAXSEGS
];
85 static int32_t bsslength
;
86 static int32_t headerlength
;
88 static void rdf2_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
90 int segtext
, segdata
, segbss
;
97 /* set up the initial segments */
98 segments
[0].segname
= ".text";
99 segments
[0].segnumber
= 0;
100 segments
[0].segtype
= 1;
101 segments
[0].segreserved
= 0;
102 segments
[0].seglength
= 0;
104 segments
[1].segname
= ".data";
105 segments
[1].segnumber
= 1;
106 segments
[1].segtype
= 2;
107 segments
[1].segreserved
= 0;
108 segments
[1].seglength
= 0;
110 segments
[2].segname
= ".bss";
111 segments
[2].segnumber
= 2;
112 segments
[2].segtype
= 0xFFFF; /* reserved - should never be produced */
113 segments
[2].segreserved
= 0;
114 segments
[2].seglength
= 0;
121 seg
[0] = saa_init(1L);
122 seg
[1] = saa_init(1L);
123 seg
[2] = NULL
; /* special case! */
125 header
= saa_init(1L);
127 segtext
= seg_alloc();
128 segdata
= seg_alloc();
129 segbss
= seg_alloc();
130 if (segtext
!= 0 || segdata
!= 2 || segbss
!= 4)
132 "rdf segment numbers not allocated as expected (%d,%d,%d)",
133 segtext
, segdata
, segbss
);
138 static int32_t rdf2_section_names(char *name
, int pass
, int *bits
)
149 * Default is 32 bits, in the text segment.
156 /* look for segment type code following segment name */
158 while (*p
&& !nasm_isspace(*p
))
160 if (*p
) { /* we're now in whitespace */
162 while (*p
&& nasm_isspace(80))
165 if (*p
) { /* we're now in an attribute value */
167 * see if we have an optional ',number' following the type code
169 if ((q
= strchr(p
, ','))) {
172 reserved
= readnum(q
, &err
);
175 "value following comma must be numeric");
180 * check it against the text strings in segmenttypes
183 for (i
= 0; i
< COUNT_SEGTYPES
; i
++)
184 if (!nasm_stricmp(p
, segmenttypes
[i
])) {
185 code
= segmenttypenumbers
[i
];
188 if (code
== -1) { /* didn't find anything */
189 code
= readnum(p
, &err
);
191 error(ERR_NONFATAL
, "unrecognised RDF segment type (%s)",
197 for (i
= 0; i
< nsegments
; i
++) {
198 if (!strcmp(name
, segments
[i
].segname
)) {
199 if (code
!= -1 || reserved
!= 0)
200 error(ERR_NONFATAL
, "segment attributes specified on"
201 " redeclaration of segment");
202 return segments
[i
].segnumber
* 2;
206 /* declaring a new segment! */
209 error(ERR_NONFATAL
, "new segment declared without type code");
212 if (nsegments
== RDF_MAXSEGS
) {
213 error(ERR_FATAL
, "reached compiled-in maximum segment limit (%d)",
218 segments
[nsegments
].segname
= nasm_strdup(name
);
221 error(ERR_PANIC
, "seg_alloc() returned odd number");
222 segments
[nsegments
].segnumber
= i
>> 1;
223 segments
[nsegments
].segtype
= code
;
224 segments
[nsegments
].segreserved
= reserved
;
225 segments
[nsegments
].seglength
= 0;
227 seg
[nsegments
] = saa_init(1L);
233 * Write relocation record
235 static void write_reloc_rec(struct RelocRec
*r
)
239 if (r
->refseg
!= (uint16_t) NO_SEG
&& (r
->refseg
& 1)) /* segment base ref */
240 r
->type
= RDFREC_SEGRELOC
;
242 r
->refseg
>>= 1; /* adjust segment nos to RDF rather than NASM */
244 saa_wbytes(header
, &r
->type
, 1);
245 saa_wbytes(header
, &r
->reclen
, 1);
246 saa_wbytes(header
, &r
->segment
, 1);
248 WRITELONG(b
, r
->offset
);
249 saa_wbytes(header
, buf
, 4);
250 saa_wbytes(header
, &r
->length
, 1);
252 WRITESHORT(b
, r
->refseg
);
253 saa_wbytes(header
, buf
, 2);
254 headerlength
+= r
->reclen
+ 2;
258 * Write export record
260 static void write_export_rec(struct ExportRec
*r
)
266 saa_wbytes(header
, &r
->type
, 1);
267 saa_wbytes(header
, &r
->reclen
, 1);
268 saa_wbytes(header
, &r
->flags
, 1);
269 saa_wbytes(header
, &r
->segment
, 1);
271 WRITELONG(b
, r
->offset
);
272 saa_wbytes(header
, buf
, 4);
273 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
274 headerlength
+= r
->reclen
+ 2;
277 static void write_import_rec(struct ImportRec
*r
)
283 saa_wbytes(header
, &r
->type
, 1);
284 saa_wbytes(header
, &r
->reclen
, 1);
285 saa_wbytes(header
, &r
->flags
, 1);
287 WRITESHORT(b
, r
->segment
);
288 saa_wbytes(header
, buf
, 2);
289 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
290 headerlength
+= r
->reclen
+ 2;
296 static void write_bss_rec(struct BSSRec
*r
)
300 saa_wbytes(header
, &r
->type
, 1);
301 saa_wbytes(header
, &r
->reclen
, 1);
303 WRITELONG(b
, r
->amount
);
304 saa_wbytes(header
, buf
, 4);
305 headerlength
+= r
->reclen
+ 2;
309 * Write common variable record
311 static void write_common_rec(struct CommonRec
*r
)
317 saa_wbytes(header
, &r
->type
, 1);
318 saa_wbytes(header
, &r
->reclen
, 1);
320 WRITESHORT(b
, r
->segment
);
321 saa_wbytes(header
, buf
, 2);
323 WRITELONG(b
, r
->size
);
324 saa_wbytes(header
, buf
, 4);
326 WRITESHORT(b
, r
->align
);
327 saa_wbytes(header
, buf
, 2);
328 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
329 headerlength
+= r
->reclen
+ 2;
333 * Write library record
335 static void write_dll_rec(struct DLLRec
*r
)
337 saa_wbytes(header
, &r
->type
, 1);
338 saa_wbytes(header
, &r
->reclen
, 1);
339 saa_wbytes(header
, r
->libname
, strlen(r
->libname
) + 1);
340 headerlength
+= r
->reclen
+ 2;
344 * Write module name record
346 static void write_modname_rec(struct ModRec
*r
)
348 saa_wbytes(header
, &r
->type
, 1);
349 saa_wbytes(header
, &r
->reclen
, 1);
350 saa_wbytes(header
, r
->modname
, strlen(r
->modname
) + 1);
351 headerlength
+= r
->reclen
+ 2;
355 * Handle export, import and common records.
357 static void rdf2_deflabel(char *name
, int32_t segment
, int64_t offset
,
358 int is_global
, char *special
)
363 static int farsym
= 0;
368 /* Check if the label length is OK */
369 if ((len
= strlen(name
)) >= EXIM_LABEL_MAX
) {
370 error(ERR_NONFATAL
, "label size exceeds %d bytes", EXIM_LABEL_MAX
);
374 error(ERR_NONFATAL
, "zero-length label");
378 if (is_global
== 2) {
379 /* Common variable */
380 ci
.type
= RDFREC_COMMON
;
382 ci
.segment
= segment
;
383 strcpy(ci
.label
, name
);
388 * Check the special text to see if it's a valid number and power
389 * of two; if so, store it as the alignment for the common variable.
393 ci
.align
= readnum(special
, &err
);
395 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
396 " valid number", special
);
397 else if ((ci
.align
| (ci
.align
- 1)) != 2 * ci
.align
- 1)
398 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
399 " power of two", special
);
401 write_common_rec(&ci
);
404 /* We don't care about local labels or fix-up hints */
409 while (*special
== ' ' || *special
== '\t')
412 if (!nasm_strnicmp(special
, "export", 6)) {
414 symflags
|= SYM_GLOBAL
;
415 } else if (!nasm_strnicmp(special
, "import", 6)) {
417 symflags
|= SYM_IMPORT
;
421 while (nasm_isspace(*special
))
423 if (!nasm_stricmp(special
, "far")) {
425 } else if (!nasm_stricmp(special
, "near")) {
427 } else if (!nasm_stricmp(special
, "proc") ||
428 !nasm_stricmp(special
, "function")) {
429 symflags
|= SYM_FUNCTION
;
430 } else if (!nasm_stricmp(special
, "data") ||
431 !nasm_stricmp(special
, "object")) {
432 symflags
|= SYM_DATA
;
434 error(ERR_NONFATAL
, "unrecognised symbol type `%s'",
439 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
440 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
444 for (i
= 0; i
< nsegments
; i
++) {
445 if (segments
[i
].segnumber
== segment
>> 1)
449 if (i
>= nsegments
) { /* EXTERN declaration */
450 ri
.type
= farsym
? RDFREC_FARIMPORT
: RDFREC_IMPORT
;
451 if (symflags
& SYM_GLOBAL
)
453 "symbol type conflict - EXTERN cannot be EXPORT");
455 ri
.segment
= segment
;
456 strcpy(ri
.label
, name
);
458 write_import_rec(&ri
);
459 } else if (is_global
) {
460 r
.type
= RDFREC_GLOBAL
; /* GLOBAL declaration */
461 if (symflags
& SYM_IMPORT
)
463 "symbol type conflict - GLOBAL cannot be IMPORT");
467 strcpy(r
.label
, name
);
469 write_export_rec(&r
);
473 static void membufwrite(int segment
, const void *data
, int bytes
)
478 for (i
= 0; i
< nsegments
; i
++) {
479 if (segments
[i
].segnumber
== segment
)
483 error(ERR_PANIC
, "can't find segment %d", segment
);
488 WRITESHORT(b
, *(int16_t *)data
);
490 WRITELONG(b
, *(int32_t *)data
);
494 segments
[i
].seglength
+= bytes
;
495 saa_wbytes(seg
[i
], data
, bytes
);
498 static int getsegmentlength(int segment
)
501 for (i
= 0; i
< nsegments
; i
++) {
502 if (segments
[i
].segnumber
== segment
)
506 error(ERR_PANIC
, "can't find segment %d", segment
);
508 return segments
[i
].seglength
;
511 static void rdf2_out(int32_t segto
, const void *data
,
512 enum out_type type
, uint64_t size
,
513 int32_t segment
, int32_t wrt
)
516 uint8_t databuf
[8], *pd
;
519 if (segto
== NO_SEG
) {
520 if (type
!= OUT_RESERVE
)
522 "attempt to assemble code in ABSOLUTE space");
526 segto
>>= 1; /* convert NASM segment no to RDF number */
528 for (seg
= 0; seg
< nsegments
; seg
++) {
529 if (segments
[seg
].segnumber
== segto
)
532 if (seg
>= nsegments
) {
534 "specified segment not supported by rdf output format");
539 wrt
= NO_SEG
; /* continue to do _something_ */
540 error(ERR_NONFATAL
, "WRT not supported by rdf output format");
543 if (segto
== 2 && type
!= OUT_RESERVE
) {
544 error(ERR_NONFATAL
, "BSS segments may not be initialized");
546 /* just reserve the space for now... */
548 if (type
== OUT_REL2ADR
)
555 if (type
== OUT_RESERVE
) {
556 if (segto
== 2) /* BSS segment space reserverd */
560 membufwrite(segto
, databuf
, 1);
561 } else if (type
== OUT_RAWDATA
) {
562 if (segment
!= NO_SEG
)
563 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
565 membufwrite(segto
, data
, size
);
566 } else if (type
== OUT_ADDRESS
) {
568 /* if segment == NO_SEG then we are writing an address of an
569 object within the same segment - do not produce reloc rec. */
571 /* FIXME - is this behaviour sane? at first glance it doesn't
572 appear to be. Must test this thoroughly...! */
574 if (segment
!= NO_SEG
) {
575 /* it's an address, so we must write a relocation record */
577 rr
.type
= RDFREC_RELOC
; /* type signature */
579 rr
.segment
= segto
; /* segment we're currently in */
580 rr
.offset
= getsegmentlength(segto
); /* current offset */
581 rr
.length
= size
; /* length of reference */
582 rr
.refseg
= segment
; /* segment referred to */
583 write_reloc_rec(&rr
);
586 pd
= databuf
; /* convert address to little-endian */
587 WRITEADDR(pd
, *(int64_t *)data
, size
);
588 membufwrite(segto
, databuf
, size
);
589 } else if (type
== OUT_REL2ADR
) {
590 if (segment
== segto
)
591 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
594 rr
.offset
= getsegmentlength(segto
); /* current offset */
595 rr
.length
= 2; /* length of reference */
596 rr
.refseg
= segment
; /* segment referred to (will be >>1'd) */
598 if (segment
!= NO_SEG
&& segment
% 2) {
599 rr
.type
= RDFREC_SEGRELOC
;
600 rr
.segment
= segto
; /* memory base refs *aren't ever* relative! */
601 write_reloc_rec(&rr
);
603 /* what do we put in the code? Simply the data. This should almost
604 * always be zero, unless someone's doing segment arithmetic...
606 rr
.offset
= *(int64_t *)data
;
608 rr
.type
= RDFREC_RELOC
; /* type signature */
609 rr
.segment
= segto
+ 64; /* segment we're currently in + rel flag */
610 write_reloc_rec(&rr
);
612 /* work out what to put in the code: offset of the end of this operand,
613 * subtracted from any data specified, so that loader can just add
614 * address of imported symbol onto it to get address relative to end of
615 * instruction: import_address + data(offset) - end_of_instrn */
617 rr
.offset
= *(int32_t *)data
- (rr
.offset
+ size
);
620 membufwrite(segto
, &rr
.offset
, -2);
621 } else if (type
== OUT_REL4ADR
) {
622 if ((segment
== segto
) && (globalbits
!= 64))
623 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
624 if (segment
!= NO_SEG
&& segment
% 2) {
625 error(ERR_PANIC
, "erm... 4 byte segment base ref?");
628 rr
.type
= RDFREC_RELOC
; /* type signature */
629 rr
.segment
= segto
+ 64; /* segment we're currently in + rel tag */
630 rr
.offset
= getsegmentlength(segto
); /* current offset */
631 rr
.length
= 4; /* length of reference */
632 rr
.refseg
= segment
; /* segment referred to */
634 write_reloc_rec(&rr
);
636 rr
.offset
= *(int64_t *)data
- (rr
.offset
+ size
);
638 membufwrite(segto
, &rr
.offset
, -4);
642 static void rdf2_cleanup(int debuginfo
)
650 /* should write imported & exported symbol declarations to header here */
652 /* generate the output file... */
653 fwrite(RDOFF2Id
, 6, 1, ofile
); /* file type magic number */
655 if (bsslength
!= 0) { /* reserve BSS */
656 bs
.type
= RDFREC_BSS
;
657 bs
.amount
= bsslength
;
663 * calculate overall length of the output object
665 l
= headerlength
+ 4;
667 for (i
= 0; i
< nsegments
; i
++) {
669 continue; /* skip BSS segment */
670 l
+= 10 + segments
[i
].seglength
;
672 l
+= 10; /* null segment */
674 fwriteint32_t(l
, ofile
);
676 fwriteint32_t(headerlength
, ofile
);
677 saa_fpwrite(header
, ofile
); /* dump header */
680 for (i
= 0; i
< nsegments
; i
++) {
684 fwriteint16_t(segments
[i
].segtype
, ofile
);
685 fwriteint16_t(segments
[i
].segnumber
, ofile
);
686 fwriteint16_t(segments
[i
].segreserved
, ofile
);
687 fwriteint32_t(segments
[i
].seglength
, ofile
);
689 saa_fpwrite(seg
[i
], ofile
);
693 /* null segment - write 10 bytes of zero */
694 fwriteint32_t(0, ofile
);
695 fwriteint32_t(0, ofile
);
696 fwriteint16_t(0, ofile
);
701 static int32_t rdf2_segbase(int32_t segment
)
707 * Handle RDOFF2 specific directives
709 static int rdf2_directive(char *directive
, char *value
, int pass
)
713 /* Check if the name length is OK */
714 if ((n
= strlen(value
)) >= MODLIB_NAME_MAX
) {
715 error(ERR_NONFATAL
, "name size exceeds %d bytes", MODLIB_NAME_MAX
);
719 if (!strcmp(directive
, "library")) {
724 strcpy(r
.libname
, value
);
730 if (!strcmp(directive
, "module")) {
733 r
.type
= RDFREC_MODNAME
;
735 strcpy(r
.modname
, value
);
736 write_modname_rec(&r
);
744 static void rdf2_filename(char *inname
, char *outname
, efunc error
)
746 standard_extension(inname
, outname
, ".rdf", error
);
749 extern macros_t rdf2_stdmac
[];
751 static int rdf2_set_info(enum geninfo type
, char **val
)
758 struct ofmt of_rdf2
= {
759 "Relocatable Dynamic Object File Format v2.0",