1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outrdf2.c output routines for the Netwide Assembler to produce
36 * RDOFF version 2 format object files, which Julian originally
37 * planned to use it in his MOSCOW operating system.
52 #include "output/outform.h"
53 #include "output/outlib.h"
55 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
56 #define VERBOSE_WARNINGS
60 #include "rdoff/rdoff.h"
62 /* This signature is written to start of RDOFF files */
63 static const char *RDOFF2Id
= RDOFF2_SIGNATURE
;
65 /* Note that whenever a segment is referred to in the RDOFF file, its number
66 * is always half of the segment number that NASM uses to refer to it; this
67 * is because NASM only allocates even numbered segments, so as to not
68 * waste any of the 16 bits of segment number written to the file - this
69 * allows up to 65533 external labels to be defined; otherwise it would be
72 #define COUNT_SEGTYPES 9
74 static char *segmenttypes
[COUNT_SEGTYPES
] = {
75 "null", "text", "code", "data",
76 "comment", "lcomment", "pcomment",
77 "symdebug", "linedebug"
80 static int segmenttypenumbers
[COUNT_SEGTYPES
] = {
81 0, 1, 1, 2, 3, 4, 5, 6, 7
84 /* code for managing buffers needed to separate code and data into individual
85 * sections until they are ready to be written to the file.
86 * We'd better hope that it all fits in memory else we're buggered... */
88 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
89 * on 80x86 machines for efficiency */
91 /***********************************************************************
92 * Actual code to deal with RDOFF2 ouput format begins here...
95 /* global variables set during the initialisation phase */
97 static struct SAA
*seg
[RDF_MAXSEGS
]; /* seg 0 = code, seg 1 = data */
98 static struct SAA
*header
; /* relocation/import/export records */
100 static struct seginfo
{
104 uint16_t segreserved
;
106 } segments
[RDF_MAXSEGS
];
108 static int nsegments
;
110 static int32_t bsslength
;
111 static int32_t headerlength
;
113 static void rdf2_init(void)
115 int segtext
, segdata
, segbss
;
119 /* set up the initial segments */
120 segments
[0].segname
= ".text";
121 segments
[0].segnumber
= 0;
122 segments
[0].segtype
= 1;
123 segments
[0].segreserved
= 0;
124 segments
[0].seglength
= 0;
126 segments
[1].segname
= ".data";
127 segments
[1].segnumber
= 1;
128 segments
[1].segtype
= 2;
129 segments
[1].segreserved
= 0;
130 segments
[1].seglength
= 0;
132 segments
[2].segname
= ".bss";
133 segments
[2].segnumber
= 2;
134 segments
[2].segtype
= 0xFFFF; /* reserved - should never be produced */
135 segments
[2].segreserved
= 0;
136 segments
[2].seglength
= 0;
140 seg
[0] = saa_init(1L);
141 seg
[1] = saa_init(1L);
142 seg
[2] = NULL
; /* special case! */
144 header
= saa_init(1L);
146 segtext
= seg_alloc();
147 segdata
= seg_alloc();
148 segbss
= seg_alloc();
149 if (segtext
!= 0 || segdata
!= 2 || segbss
!= 4)
150 nasm_error(ERR_PANIC
,
151 "rdf segment numbers not allocated as expected (%d,%d,%d)",
152 segtext
, segdata
, segbss
);
157 static int32_t rdf2_section_names(char *name
, int pass
, int *bits
)
168 * Default is 32 bits, in the text segment.
175 /* look for segment type code following segment name */
177 while (*p
&& !nasm_isspace(*p
))
179 if (*p
) { /* we're now in whitespace */
181 while (*p
&& nasm_isspace(80))
184 if (*p
) { /* we're now in an attribute value */
186 * see if we have an optional ',number' following the type code
188 if ((q
= strchr(p
, ','))) {
191 reserved
= readnum(q
, &err
);
193 nasm_error(ERR_NONFATAL
,
194 "value following comma must be numeric");
199 * check it against the text strings in segmenttypes
202 for (i
= 0; i
< COUNT_SEGTYPES
; i
++)
203 if (!nasm_stricmp(p
, segmenttypes
[i
])) {
204 code
= segmenttypenumbers
[i
];
207 if (code
== -1) { /* didn't find anything */
208 code
= readnum(p
, &err
);
210 nasm_error(ERR_NONFATAL
, "unrecognised RDF segment type (%s)",
216 for (i
= 0; i
< nsegments
; i
++) {
217 if (!strcmp(name
, segments
[i
].segname
)) {
218 if (code
!= -1 || reserved
!= 0)
219 nasm_error(ERR_NONFATAL
, "segment attributes specified on"
220 " redeclaration of segment");
221 return segments
[i
].segnumber
* 2;
225 /* declaring a new segment! */
228 nasm_error(ERR_NONFATAL
, "new segment declared without type code");
231 if (nsegments
== RDF_MAXSEGS
) {
232 nasm_error(ERR_FATAL
, "reached compiled-in maximum segment limit (%d)",
237 segments
[nsegments
].segname
= nasm_strdup(name
);
240 nasm_error(ERR_PANIC
, "seg_alloc() returned odd number");
241 segments
[nsegments
].segnumber
= i
>> 1;
242 segments
[nsegments
].segtype
= code
;
243 segments
[nsegments
].segreserved
= reserved
;
244 segments
[nsegments
].seglength
= 0;
246 seg
[nsegments
] = saa_init(1L);
252 * Write relocation record
254 static void write_reloc_rec(struct RelocRec
*r
)
258 if (r
->refseg
!= (uint16_t) NO_SEG
&& (r
->refseg
& 1)) /* segment base ref */
259 r
->type
= RDFREC_SEGRELOC
;
261 r
->refseg
>>= 1; /* adjust segment nos to RDF rather than NASM */
263 saa_wbytes(header
, &r
->type
, 1);
264 saa_wbytes(header
, &r
->reclen
, 1);
265 saa_wbytes(header
, &r
->segment
, 1);
267 WRITELONG(b
, r
->offset
);
268 saa_wbytes(header
, buf
, 4);
269 saa_wbytes(header
, &r
->length
, 1);
271 WRITESHORT(b
, r
->refseg
);
272 saa_wbytes(header
, buf
, 2);
273 headerlength
+= r
->reclen
+ 2;
277 * Write export record
279 static void write_export_rec(struct ExportRec
*r
)
285 saa_wbytes(header
, &r
->type
, 1);
286 saa_wbytes(header
, &r
->reclen
, 1);
287 saa_wbytes(header
, &r
->flags
, 1);
288 saa_wbytes(header
, &r
->segment
, 1);
290 WRITELONG(b
, r
->offset
);
291 saa_wbytes(header
, buf
, 4);
292 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
293 headerlength
+= r
->reclen
+ 2;
296 static void write_import_rec(struct ImportRec
*r
)
302 saa_wbytes(header
, &r
->type
, 1);
303 saa_wbytes(header
, &r
->reclen
, 1);
304 saa_wbytes(header
, &r
->flags
, 1);
306 WRITESHORT(b
, r
->segment
);
307 saa_wbytes(header
, buf
, 2);
308 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
309 headerlength
+= r
->reclen
+ 2;
315 static void write_bss_rec(struct BSSRec
*r
)
319 saa_wbytes(header
, &r
->type
, 1);
320 saa_wbytes(header
, &r
->reclen
, 1);
322 WRITELONG(b
, r
->amount
);
323 saa_wbytes(header
, buf
, 4);
324 headerlength
+= r
->reclen
+ 2;
328 * Write common variable record
330 static void write_common_rec(struct CommonRec
*r
)
336 saa_wbytes(header
, &r
->type
, 1);
337 saa_wbytes(header
, &r
->reclen
, 1);
339 WRITESHORT(b
, r
->segment
);
340 saa_wbytes(header
, buf
, 2);
342 WRITELONG(b
, r
->size
);
343 saa_wbytes(header
, buf
, 4);
345 WRITESHORT(b
, r
->align
);
346 saa_wbytes(header
, buf
, 2);
347 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
348 headerlength
+= r
->reclen
+ 2;
352 * Write library record
354 static void write_dll_rec(struct DLLRec
*r
)
356 saa_wbytes(header
, &r
->type
, 1);
357 saa_wbytes(header
, &r
->reclen
, 1);
358 saa_wbytes(header
, r
->libname
, strlen(r
->libname
) + 1);
359 headerlength
+= r
->reclen
+ 2;
363 * Write module name record
365 static void write_modname_rec(struct ModRec
*r
)
367 saa_wbytes(header
, &r
->type
, 1);
368 saa_wbytes(header
, &r
->reclen
, 1);
369 saa_wbytes(header
, r
->modname
, strlen(r
->modname
) + 1);
370 headerlength
+= r
->reclen
+ 2;
374 * Handle export, import and common records.
376 static void rdf2_deflabel(char *name
, int32_t segment
, int64_t offset
,
377 int is_global
, char *special
)
382 static int farsym
= 0;
387 /* Check if the label length is OK */
388 if ((len
= strlen(name
)) >= EXIM_LABEL_MAX
) {
389 nasm_error(ERR_NONFATAL
, "label size exceeds %d bytes", EXIM_LABEL_MAX
);
393 nasm_error(ERR_NONFATAL
, "zero-length label");
397 if (is_global
== 2) {
398 /* Common variable */
399 ci
.type
= RDFREC_COMMON
;
401 ci
.segment
= segment
;
402 strcpy(ci
.label
, name
);
407 * Check the special text to see if it's a valid number and power
408 * of two; if so, store it as the alignment for the common variable.
412 ci
.align
= readnum(special
, &err
);
414 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
415 " valid number", special
);
416 else if ((ci
.align
| (ci
.align
- 1)) != 2 * ci
.align
- 1)
417 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
418 " power of two", special
);
420 write_common_rec(&ci
);
423 /* We don't care about local labels or fix-up hints */
428 while (*special
== ' ' || *special
== '\t')
431 if (!nasm_strnicmp(special
, "export", 6)) {
433 symflags
|= SYM_GLOBAL
;
434 } else if (!nasm_strnicmp(special
, "import", 6)) {
436 symflags
|= SYM_IMPORT
;
440 while (nasm_isspace(*special
))
442 if (!nasm_stricmp(special
, "far")) {
444 } else if (!nasm_stricmp(special
, "near")) {
446 } else if (!nasm_stricmp(special
, "proc") ||
447 !nasm_stricmp(special
, "function")) {
448 symflags
|= SYM_FUNCTION
;
449 } else if (!nasm_stricmp(special
, "data") ||
450 !nasm_stricmp(special
, "object")) {
451 symflags
|= SYM_DATA
;
453 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%s'",
458 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
459 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
463 for (i
= 0; i
< nsegments
; i
++) {
464 if (segments
[i
].segnumber
== segment
>> 1)
468 if (i
>= nsegments
) { /* EXTERN declaration */
469 ri
.type
= farsym
? RDFREC_FARIMPORT
: RDFREC_IMPORT
;
470 if (symflags
& SYM_GLOBAL
)
471 nasm_error(ERR_NONFATAL
,
472 "symbol type conflict - EXTERN cannot be EXPORT");
474 ri
.segment
= segment
;
475 strcpy(ri
.label
, name
);
477 write_import_rec(&ri
);
478 } else if (is_global
) {
479 r
.type
= RDFREC_GLOBAL
; /* GLOBAL declaration */
480 if (symflags
& SYM_IMPORT
)
481 nasm_error(ERR_NONFATAL
,
482 "symbol type conflict - GLOBAL cannot be IMPORT");
486 strcpy(r
.label
, name
);
488 write_export_rec(&r
);
492 static void membufwrite(int segment
, const void *data
, int bytes
)
497 for (i
= 0; i
< nsegments
; i
++) {
498 if (segments
[i
].segnumber
== segment
)
502 nasm_error(ERR_PANIC
, "can't find segment %d", segment
);
507 WRITESHORT(b
, *(int16_t *)data
);
509 WRITELONG(b
, *(int32_t *)data
);
513 segments
[i
].seglength
+= bytes
;
514 saa_wbytes(seg
[i
], data
, bytes
);
517 static int getsegmentlength(int segment
)
520 for (i
= 0; i
< nsegments
; i
++) {
521 if (segments
[i
].segnumber
== segment
)
525 nasm_error(ERR_PANIC
, "can't find segment %d", segment
);
527 return segments
[i
].seglength
;
530 static void rdf2_out(int32_t segto
, const void *data
,
531 enum out_type type
, uint64_t size
,
532 int32_t segment
, int32_t wrt
)
535 uint8_t databuf
[8], *pd
;
538 if (segto
== NO_SEG
) {
539 if (type
!= OUT_RESERVE
)
540 nasm_error(ERR_NONFATAL
,
541 "attempt to assemble code in ABSOLUTE space");
545 segto
>>= 1; /* convert NASM segment no to RDF number */
547 for (seg
= 0; seg
< nsegments
; seg
++) {
548 if (segments
[seg
].segnumber
== segto
)
551 if (seg
>= nsegments
) {
552 nasm_error(ERR_NONFATAL
,
553 "specified segment not supported by rdf output format");
558 wrt
= NO_SEG
; /* continue to do _something_ */
559 nasm_error(ERR_NONFATAL
, "WRT not supported by rdf output format");
562 if (segto
== 2 && type
!= OUT_RESERVE
) {
563 nasm_error(ERR_NONFATAL
, "BSS segments may not be initialized");
565 /* just reserve the space for now... */
567 if (type
== OUT_REL2ADR
)
574 if (type
== OUT_RESERVE
) {
575 if (segto
== 2) /* BSS segment space reserverd */
579 membufwrite(segto
, databuf
, 1);
580 } else if (type
== OUT_RAWDATA
) {
581 if (segment
!= NO_SEG
)
582 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
584 membufwrite(segto
, data
, size
);
585 } else if (type
== OUT_ADDRESS
) {
587 /* if segment == NO_SEG then we are writing an address of an
588 object within the same segment - do not produce reloc rec. */
590 /* FIXME - is this behaviour sane? at first glance it doesn't
591 appear to be. Must test this thoroughly...! */
593 if (segment
!= NO_SEG
) {
594 /* it's an address, so we must write a relocation record */
596 rr
.type
= RDFREC_RELOC
; /* type signature */
598 rr
.segment
= segto
; /* segment we're currently in */
599 rr
.offset
= getsegmentlength(segto
); /* current offset */
600 rr
.length
= size
; /* length of reference */
601 rr
.refseg
= segment
; /* segment referred to */
602 write_reloc_rec(&rr
);
605 pd
= databuf
; /* convert address to little-endian */
606 WRITEADDR(pd
, *(int64_t *)data
, size
);
607 membufwrite(segto
, databuf
, size
);
608 } else if (type
== OUT_REL2ADR
) {
609 if (segment
== segto
)
610 nasm_error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
613 rr
.offset
= getsegmentlength(segto
); /* current offset */
614 rr
.length
= 2; /* length of reference */
615 rr
.refseg
= segment
; /* segment referred to (will be >>1'd) */
617 if (segment
!= NO_SEG
&& segment
% 2) {
618 rr
.type
= RDFREC_SEGRELOC
;
619 rr
.segment
= segto
; /* memory base refs *aren't ever* relative! */
620 write_reloc_rec(&rr
);
622 /* what do we put in the code? Simply the data. This should almost
623 * always be zero, unless someone's doing segment arithmetic...
625 rr
.offset
= *(int64_t *)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
= *(int32_t *)data
- (rr
.offset
+ size
);
639 membufwrite(segto
, &rr
.offset
, -2);
640 } else if (type
== OUT_REL4ADR
) {
641 if ((segment
== segto
) && (globalbits
!= 64))
642 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
643 if (segment
!= NO_SEG
&& segment
% 2) {
644 nasm_error(ERR_PANIC
, "erm... 4 byte segment base ref?");
647 rr
.type
= RDFREC_RELOC
; /* type signature */
648 rr
.segment
= segto
+ 64; /* segment we're currently in + rel tag */
649 rr
.offset
= getsegmentlength(segto
); /* current offset */
650 rr
.length
= 4; /* length of reference */
651 rr
.refseg
= segment
; /* segment referred to */
653 write_reloc_rec(&rr
);
655 rr
.offset
= *(int64_t *)data
- (rr
.offset
+ size
);
657 membufwrite(segto
, &rr
.offset
, -4);
661 static void rdf2_cleanup(int debuginfo
)
669 /* should write imported & exported symbol declarations to header here */
671 /* generate the output file... */
672 fwrite(RDOFF2Id
, 6, 1, ofile
); /* file type magic number */
674 if (bsslength
!= 0) { /* reserve BSS */
675 bs
.type
= RDFREC_BSS
;
676 bs
.amount
= bsslength
;
682 * calculate overall length of the output object
684 l
= headerlength
+ 4;
686 for (i
= 0; i
< nsegments
; i
++) {
688 continue; /* skip BSS segment */
689 l
+= 10 + segments
[i
].seglength
;
691 l
+= 10; /* null segment */
693 fwriteint32_t(l
, ofile
);
695 fwriteint32_t(headerlength
, ofile
);
696 saa_fpwrite(header
, ofile
); /* dump header */
699 for (i
= 0; i
< nsegments
; i
++) {
703 fwriteint16_t(segments
[i
].segtype
, ofile
);
704 fwriteint16_t(segments
[i
].segnumber
, ofile
);
705 fwriteint16_t(segments
[i
].segreserved
, ofile
);
706 fwriteint32_t(segments
[i
].seglength
, ofile
);
708 saa_fpwrite(seg
[i
], ofile
);
712 /* null segment - write 10 bytes of zero */
713 fwriteint32_t(0, ofile
);
714 fwriteint32_t(0, ofile
);
715 fwriteint16_t(0, ofile
);
718 static int32_t rdf2_segbase(int32_t segment
)
724 * Handle RDOFF2 specific directives
726 static int rdf2_directive(enum directives directive
, char *value
, int pass
)
733 if (n
>= MODLIB_NAME_MAX
) {
734 nasm_error(ERR_NONFATAL
, "name size exceeds %d bytes", MODLIB_NAME_MAX
);
741 strcpy(r
.libname
, value
);
747 if ((n
= strlen(value
)) >= MODLIB_NAME_MAX
) {
748 nasm_error(ERR_NONFATAL
, "name size exceeds %d bytes", MODLIB_NAME_MAX
);
753 r
.type
= RDFREC_MODNAME
;
755 strcpy(r
.modname
, value
);
756 write_modname_rec(&r
);
765 static void rdf2_filename(char *inname
, char *outname
)
767 standard_extension(inname
, outname
, ".rdf");
770 extern macros_t rdf2_stdmac
[];
772 static int rdf2_set_info(enum geninfo type
, char **val
)
779 struct ofmt of_rdf2
= {
780 "Relocatable Dynamic Object File Format v2.0",