1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2013 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
) {
586 int asize
= abs(size
);
588 /* if segment == NO_SEG then we are writing an address of an
589 object within the same segment - do not produce reloc rec. */
591 /* FIXME - is this behaviour sane? at first glance it doesn't
592 appear to be. Must test this thoroughly...! */
594 if (segment
!= NO_SEG
) {
595 /* it's an address, so we must write a relocation record */
597 rr
.type
= RDFREC_RELOC
; /* type signature */
599 rr
.segment
= segto
; /* segment we're currently in */
600 rr
.offset
= getsegmentlength(segto
); /* current offset */
601 rr
.length
= asize
; /* length of reference */
602 rr
.refseg
= segment
; /* segment referred to */
603 write_reloc_rec(&rr
);
606 pd
= databuf
; /* convert address to little-endian */
607 WRITEADDR(pd
, *(int64_t *)data
, asize
);
608 membufwrite(segto
, databuf
, asize
);
609 } else if (type
== OUT_REL2ADR
) {
610 if (segment
== segto
)
611 nasm_error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
614 rr
.offset
= getsegmentlength(segto
); /* current offset */
615 rr
.length
= 2; /* length of reference */
616 rr
.refseg
= segment
; /* segment referred to (will be >>1'd) */
618 if (segment
!= NO_SEG
&& segment
% 2) {
619 rr
.type
= RDFREC_SEGRELOC
;
620 rr
.segment
= segto
; /* memory base refs *aren't ever* relative! */
621 write_reloc_rec(&rr
);
623 /* what do we put in the code? Simply the data. This should almost
624 * always be zero, unless someone's doing segment arithmetic...
626 rr
.offset
= *(int64_t *)data
;
628 rr
.type
= RDFREC_RELOC
; /* type signature */
629 rr
.segment
= segto
+ 64; /* segment we're currently in + rel flag */
630 write_reloc_rec(&rr
);
632 /* work out what to put in the code: offset of the end of this operand,
633 * subtracted from any data specified, so that loader can just add
634 * address of imported symbol onto it to get address relative to end of
635 * instruction: import_address + data(offset) - end_of_instrn */
637 rr
.offset
= *(int32_t *)data
- (rr
.offset
+ size
);
640 membufwrite(segto
, &rr
.offset
, -2);
641 } else if (type
== OUT_REL4ADR
) {
642 if ((segment
== segto
) && (globalbits
!= 64))
643 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
644 if (segment
!= NO_SEG
&& segment
% 2) {
645 nasm_error(ERR_PANIC
, "erm... 4 byte segment base ref?");
648 rr
.type
= RDFREC_RELOC
; /* type signature */
649 rr
.segment
= segto
+ 64; /* segment we're currently in + rel tag */
650 rr
.offset
= getsegmentlength(segto
); /* current offset */
651 rr
.length
= 4; /* length of reference */
652 rr
.refseg
= segment
; /* segment referred to */
654 write_reloc_rec(&rr
);
656 rr
.offset
= *(int64_t *)data
- (rr
.offset
+ size
);
658 membufwrite(segto
, &rr
.offset
, -4);
662 static void rdf2_cleanup(int debuginfo
)
670 /* should write imported & exported symbol declarations to header here */
672 /* generate the output file... */
673 nasm_write(RDOFF2Id
, 6, ofile
); /* file type magic number */
675 if (bsslength
!= 0) { /* reserve BSS */
676 bs
.type
= RDFREC_BSS
;
677 bs
.amount
= bsslength
;
683 * calculate overall length of the output object
685 l
= headerlength
+ 4;
687 for (i
= 0; i
< nsegments
; i
++) {
689 continue; /* skip BSS segment */
690 l
+= 10 + segments
[i
].seglength
;
692 l
+= 10; /* null segment */
694 fwriteint32_t(l
, ofile
);
696 fwriteint32_t(headerlength
, ofile
);
697 saa_fpwrite(header
, ofile
); /* dump header */
700 for (i
= 0; i
< nsegments
; i
++) {
704 fwriteint16_t(segments
[i
].segtype
, ofile
);
705 fwriteint16_t(segments
[i
].segnumber
, ofile
);
706 fwriteint16_t(segments
[i
].segreserved
, ofile
);
707 fwriteint32_t(segments
[i
].seglength
, ofile
);
709 saa_fpwrite(seg
[i
], ofile
);
713 /* null segment - write 10 bytes of zero */
714 fwriteint32_t(0, ofile
);
715 fwriteint32_t(0, ofile
);
716 fwriteint16_t(0, ofile
);
719 static int32_t rdf2_segbase(int32_t segment
)
725 * Handle RDOFF2 specific directives
727 static int rdf2_directive(enum directives directive
, char *value
, int pass
)
734 if (n
>= MODLIB_NAME_MAX
) {
735 nasm_error(ERR_NONFATAL
, "name size exceeds %d bytes", MODLIB_NAME_MAX
);
742 strcpy(r
.libname
, value
);
748 if ((n
= strlen(value
)) >= MODLIB_NAME_MAX
) {
749 nasm_error(ERR_NONFATAL
, "name size exceeds %d bytes", MODLIB_NAME_MAX
);
754 r
.type
= RDFREC_MODNAME
;
756 strcpy(r
.modname
, value
);
757 write_modname_rec(&r
);
766 static void rdf2_filename(char *inname
, char *outname
)
768 standard_extension(inname
, outname
, ".rdf");
771 extern macros_t rdf2_stdmac
[];
773 static int rdf2_set_info(enum geninfo type
, char **val
)
780 struct ofmt of_rdf2
= {
781 "Relocatable Dynamic Object File Format v2.0",