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 */
104 static struct seginfo {
108 uint16_t segreserved;
110 } segments[RDF_MAXSEGS];
112 static int nsegments;
114 static int32_t bsslength;
115 static int32_t headerlength;
117 static void rdf2_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
119 int segtext, segdata, segbss;
126 /* set up the initial segments */
127 segments[0].segname = ".text";
128 segments[0].segnumber = 0;
129 segments[0].segtype = 1;
130 segments[0].segreserved = 0;
131 segments[0].seglength = 0;
133 segments[1].segname = ".data";
134 segments[1].segnumber = 1;
135 segments[1].segtype = 2;
136 segments[1].segreserved = 0;
137 segments[1].seglength = 0;
139 segments[2].segname = ".bss";
140 segments[2].segnumber = 2;
141 segments[2].segtype = 0xFFFF; /* reserved - should never be produced */
142 segments[2].segreserved = 0;
143 segments[2].seglength = 0;
150 seg[0] = saa_init(1L);
151 seg[1] = saa_init(1L);
152 seg[2] = NULL; /* special case! */
154 header = saa_init(1L);
156 segtext = seg_alloc();
157 segdata = seg_alloc();
158 segbss = seg_alloc();
159 if (segtext != 0 || segdata != 2 || segbss != 4)
161 "rdf segment numbers not allocated as expected (%d,%d,%d)",
162 segtext, segdata, segbss);
167 static int32_t rdf2_section_names(char *name, int pass, int *bits)
178 * Default is 32 bits, in the text segment.
185 /* look for segment type code following segment name */
187 while (*p && !nasm_isspace(*p))
189 if (*p) { /* we're now in whitespace */
191 while (*p && nasm_isspace(80))
194 if (*p) { /* we're now in an attribute value */
196 * see if we have an optional ',number' following the type code
198 if ((q = strchr(p, ','))) {
201 reserved = readnum(q, &err);
204 "value following comma must be numeric");
209 * check it against the text strings in segmenttypes
212 for (i = 0; i < COUNT_SEGTYPES; i++)
213 if (!nasm_stricmp(p, segmenttypes[i])) {
214 code = segmenttypenumbers[i];
217 if (code == -1) { /* didn't find anything */
218 code = readnum(p, &err);
220 error(ERR_NONFATAL, "unrecognised RDF segment type (%s)",
226 for (i = 0; i < nsegments; i++) {
227 if (!strcmp(name, segments[i].segname)) {
228 if (code != -1 || reserved != 0)
229 error(ERR_NONFATAL, "segment attributes specified on"
230 " redeclaration of segment");
231 return segments[i].segnumber * 2;
235 /* declaring a new segment! */
238 error(ERR_NONFATAL, "new segment declared without type code");
241 if (nsegments == RDF_MAXSEGS) {
242 error(ERR_FATAL, "reached compiled-in maximum segment limit (%d)",
247 segments[nsegments].segname = nasm_strdup(name);
250 error(ERR_PANIC, "seg_alloc() returned odd number");
251 segments[nsegments].segnumber = i >> 1;
252 segments[nsegments].segtype = code;
253 segments[nsegments].segreserved = reserved;
254 segments[nsegments].seglength = 0;
256 seg[nsegments] = saa_init(1L);
262 * Write relocation record
264 static void write_reloc_rec(struct RelocRec *r)
268 if (r->refseg != (uint16_t) NO_SEG && (r->refseg & 1)) /* segment base ref */
269 r->type = RDFREC_SEGRELOC;
271 r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
273 saa_wbytes(header, &r->type, 1);
274 saa_wbytes(header, &r->reclen, 1);
275 saa_wbytes(header, &r->segment, 1);
277 WRITELONG(b, r->offset);
278 saa_wbytes(header, buf, 4);
279 saa_wbytes(header, &r->length, 1);
281 WRITESHORT(b, r->refseg);
282 saa_wbytes(header, buf, 2);
283 headerlength += r->reclen + 2;
287 * Write export record
289 static void write_export_rec(struct ExportRec *r)
295 saa_wbytes(header, &r->type, 1);
296 saa_wbytes(header, &r->reclen, 1);
297 saa_wbytes(header, &r->flags, 1);
298 saa_wbytes(header, &r->segment, 1);
300 WRITELONG(b, r->offset);
301 saa_wbytes(header, buf, 4);
302 saa_wbytes(header, r->label, strlen(r->label) + 1);
303 headerlength += r->reclen + 2;
306 static void write_import_rec(struct ImportRec *r)
312 saa_wbytes(header, &r->type, 1);
313 saa_wbytes(header, &r->reclen, 1);
314 saa_wbytes(header, &r->flags, 1);
316 WRITESHORT(b, r->segment);
317 saa_wbytes(header, buf, 2);
318 saa_wbytes(header, r->label, strlen(r->label) + 1);
319 headerlength += r->reclen + 2;
325 static void write_bss_rec(struct BSSRec *r)
329 saa_wbytes(header, &r->type, 1);
330 saa_wbytes(header, &r->reclen, 1);
332 WRITELONG(b, r->amount);
333 saa_wbytes(header, buf, 4);
334 headerlength += r->reclen + 2;
338 * Write common variable record
340 static void write_common_rec(struct CommonRec *r)
346 saa_wbytes(header, &r->type, 1);
347 saa_wbytes(header, &r->reclen, 1);
349 WRITESHORT(b, r->segment);
350 saa_wbytes(header, buf, 2);
352 WRITELONG(b, r->size);
353 saa_wbytes(header, buf, 4);
355 WRITESHORT(b, r->align);
356 saa_wbytes(header, buf, 2);
357 saa_wbytes(header, r->label, strlen(r->label) + 1);
358 headerlength += r->reclen + 2;
362 * Write library record
364 static void write_dll_rec(struct DLLRec *r)
366 saa_wbytes(header, &r->type, 1);
367 saa_wbytes(header, &r->reclen, 1);
368 saa_wbytes(header, r->libname, strlen(r->libname) + 1);
369 headerlength += r->reclen + 2;
373 * Write module name record
375 static void write_modname_rec(struct ModRec *r)
377 saa_wbytes(header, &r->type, 1);
378 saa_wbytes(header, &r->reclen, 1);
379 saa_wbytes(header, r->modname, strlen(r->modname) + 1);
380 headerlength += r->reclen + 2;
384 * Handle export, import and common records.
386 static void rdf2_deflabel(char *name, int32_t segment, int64_t offset,
387 int is_global, char *special)
392 static int farsym = 0;
397 /* Check if the label length is OK */
398 if ((len = strlen(name)) >= EXIM_LABEL_MAX) {
399 error(ERR_NONFATAL, "label size exceeds %d bytes", EXIM_LABEL_MAX);
403 error(ERR_NONFATAL, "zero-length label");
407 if (is_global == 2) {
408 /* Common variable */
409 ci.type = RDFREC_COMMON;
411 ci.segment = segment;
412 strcpy(ci.label, name);
417 * Check the special text to see if it's a valid number and power
418 * of two; if so, store it as the alignment for the common variable.
422 ci.align = readnum(special, &err);
424 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
425 " valid number", special);
426 else if ((ci.align | (ci.align - 1)) != 2 * ci.align - 1)
427 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
428 " power of two", special);
430 write_common_rec(&ci);
433 /* We don't care about local labels or fix-up hints */
438 while (*special == ' ' || *special == '\t')
441 if (!nasm_strnicmp(special, "export", 6)) {
443 symflags |= SYM_GLOBAL;
444 } else if (!nasm_strnicmp(special, "import", 6)) {
446 symflags |= SYM_IMPORT;
450 while (nasm_isspace(*special))
452 if (!nasm_stricmp(special, "far")) {
454 } else if (!nasm_stricmp(special, "near")) {
456 } else if (!nasm_stricmp(special, "proc") ||
457 !nasm_stricmp(special, "function")) {
458 symflags |= SYM_FUNCTION;
459 } else if (!nasm_stricmp(special, "data") ||
460 !nasm_stricmp(special, "object")) {
461 symflags |= SYM_DATA;
463 error(ERR_NONFATAL, "unrecognised symbol type `%s'",
468 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
469 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
473 for (i = 0; i < nsegments; i++) {
474 if (segments[i].segnumber == segment >> 1)
478 if (i >= nsegments) { /* EXTERN declaration */
479 ri.type = farsym ? RDFREC_FARIMPORT : RDFREC_IMPORT;
480 if (symflags & SYM_GLOBAL)
482 "symbol type conflict - EXTERN cannot be EXPORT");
484 ri.segment = segment;
485 strcpy(ri.label, name);
487 write_import_rec(&ri);
488 } else if (is_global) {
489 r.type = RDFREC_GLOBAL; /* GLOBAL declaration */
490 if (symflags & SYM_IMPORT)
492 "symbol type conflict - GLOBAL cannot be IMPORT");
496 strcpy(r.label, name);
498 write_export_rec(&r);
502 static void membufwrite(int segment, const void *data, int bytes)
507 for (i = 0; i < nsegments; i++) {
508 if (segments[i].segnumber == segment)
512 error(ERR_PANIC, "can't find segment %d", segment);
517 WRITESHORT(b, *(int16_t *)data);
519 WRITELONG(b, *(int32_t *)data);
523 segments[i].seglength += bytes;
524 saa_wbytes(seg[i], data, bytes);
527 static int getsegmentlength(int segment)
530 for (i = 0; i < nsegments; i++) {
531 if (segments[i].segnumber == segment)
535 error(ERR_PANIC, "can't find segment %d", segment);
537 return segments[i].seglength;
540 static void rdf2_out(int32_t segto, const void *data,
541 enum out_type type, uint64_t size,
542 int32_t segment, int32_t wrt)
545 uint8_t databuf[8], *pd;
548 if (segto == NO_SEG) {
549 if (type != OUT_RESERVE)
551 "attempt to assemble code in ABSOLUTE space");
555 segto >>= 1; /* convert NASM segment no to RDF number */
557 for (seg = 0; seg < nsegments; seg++) {
558 if (segments[seg].segnumber == segto)
561 if (seg >= nsegments) {
563 "specified segment not supported by rdf output format");
568 wrt = NO_SEG; /* continue to do _something_ */
569 error(ERR_NONFATAL, "WRT not supported by rdf output format");
572 if (segto == 2 && type != OUT_RESERVE) {
573 error(ERR_NONFATAL, "BSS segments may not be initialized");
575 /* just reserve the space for now... */
577 if (type == OUT_REL2ADR)
584 if (type == OUT_RESERVE) {
585 if (segto == 2) /* BSS segment space reserverd */
589 membufwrite(segto, databuf, 1);
590 } else if (type == OUT_RAWDATA) {
591 if (segment != NO_SEG)
592 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
594 membufwrite(segto, data, size);
595 } else if (type == OUT_ADDRESS) {
597 /* if segment == NO_SEG then we are writing an address of an
598 object within the same segment - do not produce reloc rec. */
600 /* FIXME - is this behaviour sane? at first glance it doesn't
601 appear to be. Must test this thoroughly...! */
603 if (segment != NO_SEG) {
604 /* it's an address, so we must write a relocation record */
606 rr.type = RDFREC_RELOC; /* type signature */
608 rr.segment = segto; /* segment we're currently in */
609 rr.offset = getsegmentlength(segto); /* current offset */
610 rr.length = size; /* length of reference */
611 rr.refseg = segment; /* segment referred to */
612 write_reloc_rec(&rr);
615 pd = databuf; /* convert address to little-endian */
616 WRITEADDR(pd, *(int64_t *)data, size);
617 membufwrite(segto, databuf, size);
618 } else if (type == OUT_REL2ADR) {
619 if (segment == segto)
620 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
623 rr.offset = getsegmentlength(segto); /* current offset */
624 rr.length = 2; /* length of reference */
625 rr.refseg = segment; /* segment referred to (will be >>1'd) */
627 if (segment != NO_SEG && segment % 2) {
628 rr.type = RDFREC_SEGRELOC;
629 rr.segment = segto; /* memory base refs *aren't ever* relative! */
630 write_reloc_rec(&rr);
632 /* what do we put in the code? Simply the data. This should almost
633 * always be zero, unless someone's doing segment arithmetic...
635 rr.offset = *(int64_t *)data;
637 rr.type = RDFREC_RELOC; /* type signature */
638 rr.segment = segto + 64; /* segment we're currently in + rel flag */
639 write_reloc_rec(&rr);
641 /* work out what to put in the code: offset of the end of this operand,
642 * subtracted from any data specified, so that loader can just add
643 * address of imported symbol onto it to get address relative to end of
644 * instruction: import_address + data(offset) - end_of_instrn */
646 rr.offset = *(int32_t *)data - (rr.offset + size);
649 membufwrite(segto, &rr.offset, -2);
650 } else if (type == OUT_REL4ADR) {
651 if ((segment == segto) && (globalbits != 64))
652 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
653 if (segment != NO_SEG && segment % 2) {
654 error(ERR_PANIC, "erm... 4 byte segment base ref?");
657 rr.type = RDFREC_RELOC; /* type signature */
658 rr.segment = segto + 64; /* segment we're currently in + rel tag */
659 rr.offset = getsegmentlength(segto); /* current offset */
660 rr.length = 4; /* length of reference */
661 rr.refseg = segment; /* segment referred to */
663 write_reloc_rec(&rr);
665 rr.offset = *(int64_t *)data - (rr.offset + size);
667 membufwrite(segto, &rr.offset, -4);
671 static void rdf2_cleanup(int debuginfo)
679 /* should write imported & exported symbol declarations to header here */
681 /* generate the output file... */
682 fwrite(RDOFF2Id, 6, 1, ofile); /* file type magic number */
684 if (bsslength != 0) { /* reserve BSS */
685 bs.type = RDFREC_BSS;
686 bs.amount = bsslength;
692 * calculate overall length of the output object
694 l = headerlength + 4;
696 for (i = 0; i < nsegments; i++) {
698 continue; /* skip BSS segment */
699 l += 10 + segments[i].seglength;
701 l += 10; /* null segment */
703 fwriteint32_t(l, ofile);
705 fwriteint32_t(headerlength, ofile);
706 saa_fpwrite(header, ofile); /* dump header */
709 for (i = 0; i < nsegments; i++) {
713 fwriteint16_t(segments[i].segtype, ofile);
714 fwriteint16_t(segments[i].segnumber, ofile);
715 fwriteint16_t(segments[i].segreserved, ofile);
716 fwriteint32_t(segments[i].seglength, ofile);
718 saa_fpwrite(seg[i], ofile);
722 /* null segment - write 10 bytes of zero */
723 fwriteint32_t(0, ofile);
724 fwriteint32_t(0, ofile);
725 fwriteint16_t(0, ofile);
728 static int32_t rdf2_segbase(int32_t segment)
734 * Handle RDOFF2 specific directives
736 static int rdf2_directive(enum directives directive, char *value, int pass)
743 if (n >= MODLIB_NAME_MAX) {
744 error(ERR_NONFATAL, "name size exceeds %d bytes", MODLIB_NAME_MAX);
751 strcpy(r.libname, value);
757 if ((n = strlen(value)) >= MODLIB_NAME_MAX) {
758 error(ERR_NONFATAL, "name size exceeds %d bytes", MODLIB_NAME_MAX);
763 r.type = RDFREC_MODNAME;
765 strcpy(r.modname, value);
766 write_modname_rec(&r);
775 static void rdf2_filename(char *inname, char *outname, efunc error)
777 standard_extension(inname, outname, ".rdf", error);
780 extern macros_t rdf2_stdmac[];
782 static int rdf2_set_info(enum geninfo type, char **val)
789 struct ofmt of_rdf2 = {
790 "Relocatable Dynamic Object File Format v2.0",