1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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.
51 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
52 #define VERBOSE_WARNINGS
58 /* This signature is written to start of RDOFF files */
59 static const char *RDOFF2Id
= RDOFF2_SIGNATURE
;
61 /* Note that whenever a segment is referred to in the RDOFF file, its number
62 * is always half of the segment number that NASM uses to refer to it; this
63 * is because NASM only allocates even numbered segments, so as to not
64 * waste any of the 16 bits of segment number written to the file - this
65 * allows up to 65533 external labels to be defined; otherwise it would be
68 #define COUNT_SEGTYPES 9
70 static char *segmenttypes
[COUNT_SEGTYPES
] = {
71 "null", "text", "code", "data",
72 "comment", "lcomment", "pcomment",
73 "symdebug", "linedebug"
76 static int segmenttypenumbers
[COUNT_SEGTYPES
] = {
77 0, 1, 1, 2, 3, 4, 5, 6, 7
80 /* code for managing buffers needed to separate code and data into individual
81 * sections until they are ready to be written to the file.
82 * We'd better hope that it all fits in memory else we're buggered... */
84 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
85 * on 80x86 machines for efficiency */
87 /***********************************************************************
88 * Actual code to deal with RDOFF2 ouput format begins here...
91 /* global variables set during the initialisation phase */
93 static struct SAA
*seg
[RDF_MAXSEGS
]; /* seg 0 = code, seg 1 = data */
94 static struct SAA
*header
; /* relocation/import/export records */
96 static struct seginfo
{
100 uint16_t segreserved
;
102 } segments
[RDF_MAXSEGS
];
104 static int nsegments
;
106 static int32_t bsslength
;
107 static int32_t headerlength
;
109 static void rdf2_init(void)
111 int segtext
, segdata
, segbss
;
113 /* set up the initial segments */
114 segments
[0].segname
= ".text";
115 segments
[0].segnumber
= 0;
116 segments
[0].segtype
= 1;
117 segments
[0].segreserved
= 0;
118 segments
[0].seglength
= 0;
120 segments
[1].segname
= ".data";
121 segments
[1].segnumber
= 1;
122 segments
[1].segtype
= 2;
123 segments
[1].segreserved
= 0;
124 segments
[1].seglength
= 0;
126 segments
[2].segname
= ".bss";
127 segments
[2].segnumber
= 2;
128 segments
[2].segtype
= 0xFFFF; /* reserved - should never be produced */
129 segments
[2].segreserved
= 0;
130 segments
[2].seglength
= 0;
134 seg
[0] = saa_init(1L);
135 seg
[1] = saa_init(1L);
136 seg
[2] = NULL
; /* special case! */
138 header
= saa_init(1L);
140 segtext
= seg_alloc();
141 segdata
= seg_alloc();
142 segbss
= seg_alloc();
143 if (segtext
!= 0 || segdata
!= 2 || segbss
!= 4)
144 nasm_panic("rdf segment numbers not allocated as expected (%d,%d,%d)",
145 segtext
, segdata
, segbss
);
150 static int32_t rdf2_section_names(char *name
, int *bits
)
159 * Default is 32 bits, in the text segment.
166 /* look for segment type code following segment name */
168 while (*p
&& !nasm_isspace(*p
))
170 if (*p
) { /* we're now in whitespace */
172 while (*p
&& nasm_isspace(80))
175 if (*p
) { /* we're now in an attribute value */
177 * see if we have an optional ',number' following the type code
179 if ((q
= strchr(p
, ','))) {
182 reserved
= readnum(q
, &err
);
184 nasm_error(ERR_NONFATAL
,
185 "value following comma must be numeric");
190 * check it against the text strings in segmenttypes
193 for (i
= 0; i
< COUNT_SEGTYPES
; i
++)
194 if (!nasm_stricmp(p
, segmenttypes
[i
])) {
195 code
= segmenttypenumbers
[i
];
198 if (code
== -1) { /* didn't find anything */
199 code
= readnum(p
, &err
);
201 nasm_error(ERR_NONFATAL
, "unrecognised RDF segment type (%s)",
207 for (i
= 0; i
< nsegments
; i
++) {
208 if (!strcmp(name
, segments
[i
].segname
)) {
209 if (code
!= -1 || reserved
!= 0)
210 nasm_error(ERR_NONFATAL
, "segment attributes specified on"
211 " redeclaration of segment");
212 return segments
[i
].segnumber
* 2;
216 /* declaring a new segment! */
219 nasm_error(ERR_NONFATAL
, "new segment declared without type code");
222 if (nsegments
== RDF_MAXSEGS
) {
223 nasm_fatal("reached compiled-in maximum segment limit (%d)",
228 segments
[nsegments
].segname
= nasm_strdup(name
);
231 nasm_panic("seg_alloc() returned odd number");
232 segments
[nsegments
].segnumber
= i
>> 1;
233 segments
[nsegments
].segtype
= code
;
234 segments
[nsegments
].segreserved
= reserved
;
235 segments
[nsegments
].seglength
= 0;
237 seg
[nsegments
] = saa_init(1L);
243 * Write relocation record
245 static void write_reloc_rec(struct RelocRec
*r
)
249 if (r
->refseg
!= (uint16_t) NO_SEG
&& (r
->refseg
& 1)) /* segment base ref */
250 r
->type
= RDFREC_SEGRELOC
;
252 r
->refseg
>>= 1; /* adjust segment nos to RDF rather than NASM */
254 saa_wbytes(header
, &r
->type
, 1);
255 saa_wbytes(header
, &r
->reclen
, 1);
256 saa_wbytes(header
, &r
->segment
, 1);
258 WRITELONG(b
, r
->offset
);
259 saa_wbytes(header
, buf
, 4);
260 saa_wbytes(header
, &r
->length
, 1);
262 WRITESHORT(b
, r
->refseg
);
263 saa_wbytes(header
, buf
, 2);
264 headerlength
+= r
->reclen
+ 2;
268 * Write export record
270 static void write_export_rec(struct ExportRec
*r
)
276 saa_wbytes(header
, &r
->type
, 1);
277 saa_wbytes(header
, &r
->reclen
, 1);
278 saa_wbytes(header
, &r
->flags
, 1);
279 saa_wbytes(header
, &r
->segment
, 1);
281 WRITELONG(b
, r
->offset
);
282 saa_wbytes(header
, buf
, 4);
283 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
284 headerlength
+= r
->reclen
+ 2;
287 static void write_import_rec(struct ImportRec
*r
)
293 saa_wbytes(header
, &r
->type
, 1);
294 saa_wbytes(header
, &r
->reclen
, 1);
295 saa_wbytes(header
, &r
->flags
, 1);
297 WRITESHORT(b
, r
->segment
);
298 saa_wbytes(header
, buf
, 2);
299 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
300 headerlength
+= r
->reclen
+ 2;
306 static void write_bss_rec(struct BSSRec
*r
)
310 saa_wbytes(header
, &r
->type
, 1);
311 saa_wbytes(header
, &r
->reclen
, 1);
313 WRITELONG(b
, r
->amount
);
314 saa_wbytes(header
, buf
, 4);
315 headerlength
+= r
->reclen
+ 2;
319 * Write common variable record
321 static void write_common_rec(struct CommonRec
*r
)
327 saa_wbytes(header
, &r
->type
, 1);
328 saa_wbytes(header
, &r
->reclen
, 1);
330 WRITESHORT(b
, r
->segment
);
331 saa_wbytes(header
, buf
, 2);
333 WRITELONG(b
, r
->size
);
334 saa_wbytes(header
, buf
, 4);
336 WRITESHORT(b
, r
->align
);
337 saa_wbytes(header
, buf
, 2);
338 saa_wbytes(header
, r
->label
, strlen(r
->label
) + 1);
339 headerlength
+= r
->reclen
+ 2;
343 * Write library record
345 static void write_dll_rec(struct DLLRec
*r
)
347 saa_wbytes(header
, &r
->type
, 1);
348 saa_wbytes(header
, &r
->reclen
, 1);
349 saa_wbytes(header
, r
->libname
, strlen(r
->libname
) + 1);
350 headerlength
+= r
->reclen
+ 2;
354 * Write module name record
356 static void write_modname_rec(struct ModRec
*r
)
358 saa_wbytes(header
, &r
->type
, 1);
359 saa_wbytes(header
, &r
->reclen
, 1);
360 saa_wbytes(header
, r
->modname
, strlen(r
->modname
) + 1);
361 headerlength
+= r
->reclen
+ 2;
365 * Handle export, import and common records.
367 static void rdf2_deflabel(char *name
, int32_t segment
, int64_t offset
,
368 int is_global
, char *special
)
373 static int farsym
= 0;
378 /* Check if the label length is OK */
379 if ((len
= strlen(name
)) >= EXIM_LABEL_MAX
) {
380 nasm_error(ERR_NONFATAL
, "label size exceeds %d bytes", EXIM_LABEL_MAX
);
384 nasm_error(ERR_NONFATAL
, "zero-length label");
388 if (is_global
== 2) {
389 /* Common variable */
390 ci
.type
= RDFREC_COMMON
;
392 ci
.segment
= segment
;
393 strcpy(ci
.label
, name
);
398 * Check the special text to see if it's a valid number and power
399 * of two; if so, store it as the alignment for the common variable.
403 ci
.align
= readnum(special
, &err
);
405 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
406 " valid number", special
);
407 else if ((ci
.align
| (ci
.align
- 1)) != 2 * ci
.align
- 1)
408 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
409 " power of two", special
);
411 write_common_rec(&ci
);
414 /* We don't care about local labels or fix-up hints */
419 while (*special
== ' ' || *special
== '\t')
422 if (!nasm_strnicmp(special
, "export", 6)) {
424 symflags
|= SYM_GLOBAL
;
425 } else if (!nasm_strnicmp(special
, "import", 6)) {
427 symflags
|= SYM_IMPORT
;
431 while (nasm_isspace(*special
))
433 if (!nasm_stricmp(special
, "far")) {
435 } else if (!nasm_stricmp(special
, "near")) {
437 } else if (!nasm_stricmp(special
, "proc") ||
438 !nasm_stricmp(special
, "function")) {
439 symflags
|= SYM_FUNCTION
;
440 } else if (!nasm_stricmp(special
, "data") ||
441 !nasm_stricmp(special
, "object")) {
442 symflags
|= SYM_DATA
;
444 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%s'",
449 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
450 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
454 for (i
= 0; i
< nsegments
; i
++) {
455 if (segments
[i
].segnumber
== segment
>> 1)
459 if (i
>= nsegments
) { /* EXTERN declaration */
460 ri
.type
= farsym
? RDFREC_FARIMPORT
: RDFREC_IMPORT
;
461 if (symflags
& SYM_GLOBAL
)
462 nasm_error(ERR_NONFATAL
,
463 "symbol type conflict - EXTERN cannot be EXPORT");
465 ri
.segment
= segment
;
466 strcpy(ri
.label
, name
);
468 write_import_rec(&ri
);
469 } else if (is_global
) {
470 r
.type
= RDFREC_GLOBAL
; /* GLOBAL declaration */
471 if (symflags
& SYM_IMPORT
)
472 nasm_error(ERR_NONFATAL
,
473 "symbol type conflict - GLOBAL cannot be IMPORT");
477 strcpy(r
.label
, name
);
479 write_export_rec(&r
);
483 static void membufwrite(int segment
, const void *data
, int bytes
)
488 for (i
= 0; i
< nsegments
; i
++) {
489 if (segments
[i
].segnumber
== segment
)
493 nasm_panic("can't find segment %d", segment
);
498 WRITESHORT(b
, *(int16_t *)data
);
500 WRITELONG(b
, *(int32_t *)data
);
504 segments
[i
].seglength
+= bytes
;
505 saa_wbytes(seg
[i
], data
, bytes
);
508 static int getsegmentlength(int segment
)
511 for (i
= 0; i
< nsegments
; i
++) {
512 if (segments
[i
].segnumber
== segment
)
516 nasm_panic("can't find segment %d", segment
);
518 return segments
[i
].seglength
;
521 static void rdf2_out(int32_t segto
, const void *data
,
522 enum out_type type
, uint64_t size
,
523 int32_t segment
, int32_t wrt
)
526 uint8_t databuf
[8], *pd
;
529 segto
>>= 1; /* convert NASM segment no to RDF number */
531 for (seg
= 0; seg
< nsegments
; seg
++) {
532 if (segments
[seg
].segnumber
== segto
)
535 if (seg
>= nsegments
) {
536 nasm_error(ERR_NONFATAL
,
537 "specified segment not supported by rdf output format");
542 wrt
= NO_SEG
; /* continue to do _something_ */
543 nasm_error(ERR_NONFATAL
, "WRT not supported by rdf output format");
546 if (segto
== 2 && type
!= OUT_RESERVE
) {
547 nasm_error(ERR_NONFATAL
, "BSS segments may not be initialized");
549 /* just reserve the space for now... */
551 if (type
== OUT_REL2ADR
)
558 if (type
== OUT_RESERVE
) {
559 if (segto
== 2) /* BSS segment space reserverd */
563 membufwrite(segto
, databuf
, 1);
564 } else if (type
== OUT_RAWDATA
) {
565 membufwrite(segto
, data
, size
);
566 } else if (type
== OUT_ADDRESS
) {
567 int asize
= abs((int)size
);
569 /* if segment == NO_SEG then we are writing an address of an
570 object within the same segment - do not produce reloc rec. */
572 /* FIXME - is this behaviour sane? at first glance it doesn't
573 appear to be. Must test this thoroughly...! */
575 if (segment
!= NO_SEG
) {
576 /* it's an address, so we must write a relocation record */
578 rr
.type
= RDFREC_RELOC
; /* type signature */
580 rr
.segment
= segto
; /* segment we're currently in */
581 rr
.offset
= getsegmentlength(segto
); /* current offset */
582 rr
.length
= asize
; /* length of reference */
583 rr
.refseg
= segment
; /* segment referred to */
584 write_reloc_rec(&rr
);
587 pd
= databuf
; /* convert address to little-endian */
588 WRITEADDR(pd
, *(int64_t *)data
, asize
);
589 membufwrite(segto
, databuf
, asize
);
590 } else if (type
== OUT_REL2ADR
) {
591 if (segment
== segto
)
592 nasm_panic("intra-segment OUT_REL2ADR");
595 rr
.offset
= getsegmentlength(segto
); /* current offset */
596 rr
.length
= 2; /* length of reference */
597 rr
.refseg
= segment
; /* segment referred to (will be >>1'd) */
599 if (segment
!= NO_SEG
&& segment
% 2) {
600 rr
.type
= RDFREC_SEGRELOC
;
601 rr
.segment
= segto
; /* memory base refs *aren't ever* relative! */
602 write_reloc_rec(&rr
);
604 /* what do we put in the code? Simply the data. This should almost
605 * always be zero, unless someone's doing segment arithmetic...
607 rr
.offset
= *(int64_t *)data
;
609 rr
.type
= RDFREC_RELOC
; /* type signature */
610 rr
.segment
= segto
+ 64; /* segment we're currently in + rel flag */
611 write_reloc_rec(&rr
);
613 /* work out what to put in the code: offset of the end of this operand,
614 * subtracted from any data specified, so that loader can just add
615 * address of imported symbol onto it to get address relative to end of
616 * instruction: import_address + data(offset) - end_of_instrn */
618 rr
.offset
= *(int32_t *)data
- (rr
.offset
+ size
);
621 membufwrite(segto
, &rr
.offset
, -2);
622 } else if (type
== OUT_REL4ADR
) {
623 if ((segment
== segto
) && (globalbits
!= 64))
624 nasm_panic("intra-segment OUT_REL4ADR");
625 if (segment
!= NO_SEG
&& segment
% 2) {
626 nasm_panic("erm... 4 byte segment base ref?");
629 rr
.type
= RDFREC_RELOC
; /* type signature */
630 rr
.segment
= segto
+ 64; /* segment we're currently in + rel tag */
631 rr
.offset
= getsegmentlength(segto
); /* current offset */
632 rr
.length
= 4; /* length of reference */
633 rr
.refseg
= segment
; /* segment referred to */
635 write_reloc_rec(&rr
);
637 rr
.offset
= *(int64_t *)data
- (rr
.offset
+ size
);
639 membufwrite(segto
, &rr
.offset
, -4);
643 static void rdf2_cleanup(void)
649 /* should write imported & exported symbol declarations to header here */
651 /* generate the output file... */
652 nasm_write(RDOFF2Id
, 6, ofile
); /* file type magic number */
654 if (bsslength
!= 0) { /* reserve BSS */
655 bs
.type
= RDFREC_BSS
;
656 bs
.amount
= bsslength
;
662 * calculate overall length of the output object
664 l
= headerlength
+ 4;
666 for (i
= 0; i
< nsegments
; i
++) {
668 continue; /* skip BSS segment */
669 l
+= 10 + segments
[i
].seglength
;
671 l
+= 10; /* null segment */
673 fwriteint32_t(l
, ofile
);
675 fwriteint32_t(headerlength
, ofile
);
676 saa_fpwrite(header
, ofile
); /* dump header */
679 for (i
= 0; i
< nsegments
; i
++) {
683 fwriteint16_t(segments
[i
].segtype
, ofile
);
684 fwriteint16_t(segments
[i
].segnumber
, ofile
);
685 fwriteint16_t(segments
[i
].segreserved
, ofile
);
686 fwriteint32_t(segments
[i
].seglength
, ofile
);
688 saa_fpwrite(seg
[i
], ofile
);
692 /* null segment - write 10 bytes of zero */
693 fwriteint32_t(0, ofile
);
694 fwriteint32_t(0, ofile
);
695 fwriteint16_t(0, ofile
);
699 * Handle RDOFF2 specific directives
701 static enum directive_result
702 rdf2_directive(enum directive directive
, char *value
)
709 if (n
>= MODLIB_NAME_MAX
) {
710 nasm_error(ERR_NONFATAL
, "name size exceeds %d bytes", MODLIB_NAME_MAX
);
713 if (pass_first()) { /* XXX */
717 strcpy(r
.libname
, value
);
723 if ((n
= strlen(value
)) >= MODLIB_NAME_MAX
) {
724 nasm_error(ERR_NONFATAL
, "name size exceeds %d bytes", MODLIB_NAME_MAX
);
727 if (pass_first()) { /* XXX */
729 r
.type
= RDFREC_MODNAME
;
731 strcpy(r
.modname
, value
);
732 write_modname_rec(&r
);
741 extern macros_t rdf2_stdmac
[];
743 const struct ofmt of_rdf2
= {
744 "Relocatable Dynamic Object File Format v2.0",
754 nasm_do_legacy_output
,
763 NULL
/* pragma list */