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 licence given in the file "Licence"
9 * distributed in the NASM archive.
25 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
26 #define VERBOSE_WARNINGS
30 #include "rdoff/rdoff.h"
32 /* This signature is written to start of RDOFF files */
33 static const char *RDOFF2Id = RDOFF2_SIGNATURE;
35 /* Note that whenever a segment is referred to in the RDOFF file, its number
36 * is always half of the segment number that NASM uses to refer to it; this
37 * is because NASM only allocates even numbered segments, so as to not
38 * waste any of the 16 bits of segment number written to the file - this
39 * allows up to 65533 external labels to be defined; otherwise it would be
42 #define COUNT_SEGTYPES 9
44 static char *segmenttypes[COUNT_SEGTYPES] = {
45 "null", "text", "code", "data",
46 "comment", "lcomment", "pcomment",
47 "symdebug", "linedebug"
50 static int segmenttypenumbers[COUNT_SEGTYPES] = {
51 0, 1, 1, 2, 3, 4, 5, 6, 7
54 /* code for managing buffers needed to separate code and data into individual
55 * sections until they are ready to be written to the file.
56 * We'd better hope that it all fits in memory else we're buggered... */
58 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
59 * on 80x86 machines for efficiency */
61 /***********************************************************************
62 * Actual code to deal with RDOFF2 ouput format begins here...
65 /* global variables set during the initialisation phase */
67 static struct SAA *seg[RDF_MAXSEGS]; /* seg 0 = code, seg 1 = data */
68 static struct SAA *header; /* relocation/import/export records */
74 static struct seginfo {
80 } segments[RDF_MAXSEGS];
84 static int32_t bsslength;
85 static int32_t headerlength;
87 static void rdf2_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
89 int segtext, segdata, segbss;
96 /* set up the initial segments */
97 segments[0].segname = ".text";
98 segments[0].segnumber = 0;
99 segments[0].segtype = 1;
100 segments[0].segreserved = 0;
101 segments[0].seglength = 0;
103 segments[1].segname = ".data";
104 segments[1].segnumber = 1;
105 segments[1].segtype = 2;
106 segments[1].segreserved = 0;
107 segments[1].seglength = 0;
109 segments[2].segname = ".bss";
110 segments[2].segnumber = 2;
111 segments[2].segtype = 0xFFFF; /* reserved - should never be produced */
112 segments[2].segreserved = 0;
113 segments[2].seglength = 0;
120 seg[0] = saa_init(1L);
121 seg[1] = saa_init(1L);
122 seg[2] = NULL; /* special case! */
124 header = saa_init(1L);
126 segtext = seg_alloc();
127 segdata = seg_alloc();
128 segbss = seg_alloc();
129 if (segtext != 0 || segdata != 2 || segbss != 4)
131 "rdf segment numbers not allocated as expected (%d,%d,%d)",
132 segtext, segdata, segbss);
137 static int32_t rdf2_section_names(char *name, int pass, int *bits)
148 * Default is 32 bits, in the text segment.
155 /* look for segment type code following segment name */
157 while (*p && !isspace(*p))
159 if (*p) { /* we're now in whitespace */
161 while (*p && isspace(80))
164 if (*p) { /* we're now in an attribute value */
166 * see if we have an optional ',number' following the type code
168 if ((q = strchr(p, ','))) {
171 reserved = readnum(q, &err);
174 "value following comma must be numeric");
179 * check it against the text strings in segmenttypes
182 for (i = 0; i < COUNT_SEGTYPES; i++)
183 if (!nasm_stricmp(p, segmenttypes[i])) {
184 code = segmenttypenumbers[i];
187 if (code == -1) { /* didn't find anything */
188 code = readnum(p, &err);
190 error(ERR_NONFATAL, "unrecognised RDF segment type (%s)",
196 for (i = 0; i < nsegments; i++) {
197 if (!strcmp(name, segments[i].segname)) {
198 if (code != -1 || reserved != 0)
199 error(ERR_NONFATAL, "segment attributes specified on"
200 " redeclaration of segment");
201 return segments[i].segnumber * 2;
205 /* declaring a new segment! */
208 error(ERR_NONFATAL, "new segment declared without type code");
211 if (nsegments == RDF_MAXSEGS) {
212 error(ERR_FATAL, "reached compiled-in maximum segment limit (%d)",
217 segments[nsegments].segname = nasm_strdup(name);
220 error(ERR_PANIC, "seg_alloc() returned odd number");
221 segments[nsegments].segnumber = i >> 1;
222 segments[nsegments].segtype = code;
223 segments[nsegments].segreserved = reserved;
224 segments[nsegments].seglength = 0;
226 seg[nsegments] = saa_init(1L);
232 * Write relocation record
234 static void write_reloc_rec(struct RelocRec *r)
238 if (r->refseg != (uint16_t) NO_SEG && (r->refseg & 1)) /* segment base ref */
239 r->type = RDFREC_SEGRELOC;
241 r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
243 saa_wbytes(header, &r->type, 1);
244 saa_wbytes(header, &r->reclen, 1);
245 saa_wbytes(header, &r->segment, 1);
247 WRITELONG(b, r->offset);
248 saa_wbytes(header, buf, 4);
249 saa_wbytes(header, &r->length, 1);
251 WRITESHORT(b, r->refseg);
252 saa_wbytes(header, buf, 2);
253 headerlength += r->reclen + 2;
257 * Write export record
259 static void write_export_rec(struct ExportRec *r)
265 saa_wbytes(header, &r->type, 1);
266 saa_wbytes(header, &r->reclen, 1);
267 saa_wbytes(header, &r->flags, 1);
268 saa_wbytes(header, &r->segment, 1);
270 WRITELONG(b, r->offset);
271 saa_wbytes(header, buf, 4);
272 saa_wbytes(header, r->label, strlen(r->label) + 1);
273 headerlength += r->reclen + 2;
276 static void write_import_rec(struct ImportRec *r)
282 saa_wbytes(header, &r->type, 1);
283 saa_wbytes(header, &r->reclen, 1);
284 saa_wbytes(header, &r->flags, 1);
286 WRITESHORT(b, r->segment);
287 saa_wbytes(header, buf, 2);
288 saa_wbytes(header, r->label, strlen(r->label) + 1);
289 headerlength += r->reclen + 2;
295 static void write_bss_rec(struct BSSRec *r)
299 saa_wbytes(header, &r->type, 1);
300 saa_wbytes(header, &r->reclen, 1);
302 WRITELONG(b, r->amount);
303 saa_wbytes(header, buf, 4);
304 headerlength += r->reclen + 2;
308 * Write common variable record
310 static void write_common_rec(struct CommonRec *r)
316 saa_wbytes(header, &r->type, 1);
317 saa_wbytes(header, &r->reclen, 1);
319 WRITESHORT(b, r->segment);
320 saa_wbytes(header, buf, 2);
322 WRITELONG(b, r->size);
323 saa_wbytes(header, buf, 4);
325 WRITESHORT(b, r->align);
326 saa_wbytes(header, buf, 2);
327 saa_wbytes(header, r->label, strlen(r->label) + 1);
328 headerlength += r->reclen + 2;
332 * Write library record
334 static void write_dll_rec(struct DLLRec *r)
336 saa_wbytes(header, &r->type, 1);
337 saa_wbytes(header, &r->reclen, 1);
338 saa_wbytes(header, r->libname, strlen(r->libname) + 1);
339 headerlength += r->reclen + 2;
343 * Write module name record
345 static void write_modname_rec(struct ModRec *r)
347 saa_wbytes(header, &r->type, 1);
348 saa_wbytes(header, &r->reclen, 1);
349 saa_wbytes(header, r->modname, strlen(r->modname) + 1);
350 headerlength += r->reclen + 2;
354 * Handle export, import and common records.
356 static void rdf2_deflabel(char *name, int32_t segment, int64_t offset,
357 int is_global, char *special)
362 static int farsym = 0;
367 /* Check if the label length is OK */
368 if ((len = strlen(name)) >= EXIM_LABEL_MAX) {
369 error(ERR_NONFATAL, "label size exceeds %d bytes", EXIM_LABEL_MAX);
373 error(ERR_NONFATAL, "zero-length label");
377 if (is_global == 2) {
378 /* Common variable */
379 ci.type = RDFREC_COMMON;
381 ci.segment = segment;
382 strcpy(ci.label, name);
387 * Check the special text to see if it's a valid number and power
388 * of two; if so, store it as the alignment for the common variable.
392 ci.align = readnum(special, &err);
394 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
395 " valid number", special);
396 else if ((ci.align | (ci.align - 1)) != 2 * ci.align - 1)
397 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
398 " power of two", special);
400 write_common_rec(&ci);
403 /* We don't care about local labels or fix-up hints */
408 while (*special == ' ' || *special == '\t')
411 if (!nasm_strnicmp(special, "export", 6)) {
413 symflags |= SYM_GLOBAL;
414 } else if (!nasm_strnicmp(special, "import", 6)) {
416 symflags |= SYM_IMPORT;
420 while (isspace(*special))
422 if (!nasm_stricmp(special, "far")) {
424 } else if (!nasm_stricmp(special, "near")) {
426 } else if (!nasm_stricmp(special, "proc") ||
427 !nasm_stricmp(special, "function")) {
428 symflags |= SYM_FUNCTION;
429 } else if (!nasm_stricmp(special, "data") ||
430 !nasm_stricmp(special, "object")) {
431 symflags |= SYM_DATA;
433 error(ERR_NONFATAL, "unrecognised symbol type `%s'",
438 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
439 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
443 for (i = 0; i < nsegments; i++) {
444 if (segments[i].segnumber == segment >> 1)
448 if (i >= nsegments) { /* EXTERN declaration */
449 ri.type = farsym ? RDFREC_FARIMPORT : RDFREC_IMPORT;
450 if (symflags & SYM_GLOBAL)
452 "symbol type conflict - EXTERN cannot be EXPORT");
454 ri.segment = segment;
455 strcpy(ri.label, name);
457 write_import_rec(&ri);
458 } else if (is_global) {
459 r.type = RDFREC_GLOBAL; /* GLOBAL declaration */
460 if (symflags & SYM_IMPORT)
462 "symbol type conflict - GLOBAL cannot be IMPORT");
466 strcpy(r.label, name);
468 write_export_rec(&r);
472 static void membufwrite(int segment, const void *data, int bytes)
477 for (i = 0; i < nsegments; i++) {
478 if (segments[i].segnumber == segment)
482 error(ERR_PANIC, "can't find segment %d", segment);
487 WRITESHORT(b, *(int16_t *)data);
489 WRITELONG(b, *(int32_t *)data);
493 segments[i].seglength += bytes;
494 saa_wbytes(seg[i], data, bytes);
497 static int getsegmentlength(int segment)
500 for (i = 0; i < nsegments; i++) {
501 if (segments[i].segnumber == segment)
505 error(ERR_PANIC, "can't find segment %d", segment);
507 return segments[i].seglength;
510 static void rdf2_out(int32_t segto, const void *data, uint32_t type,
511 int32_t segment, int32_t wrt)
513 int32_t bytes = type & OUT_SIZMASK;
515 uint8_t databuf[8], *pd;
518 if (segto == NO_SEG) {
519 if ((type & OUT_TYPMASK) != OUT_RESERVE)
521 "attempt to assemble code in ABSOLUTE space");
525 segto >>= 1; /* convert NASM segment no to RDF number */
527 for (seg = 0; seg < nsegments; seg++) {
528 if (segments[seg].segnumber == segto)
531 if (seg >= nsegments) {
533 "specified segment not supported by rdf output format");
538 wrt = NO_SEG; /* continue to do _something_ */
539 error(ERR_NONFATAL, "WRT not supported by rdf output format");
544 if (segto == 2 && type != OUT_RESERVE) {
545 error(ERR_NONFATAL, "BSS segments may not be initialized");
547 /* just reserve the space for now... */
549 if (type == OUT_REL2ADR)
556 if (type == OUT_RESERVE) {
557 if (segto == 2) /* BSS segment space reserverd */
561 membufwrite(segto, databuf, 1);
562 } else if (type == OUT_RAWDATA) {
563 if (segment != NO_SEG)
564 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
566 membufwrite(segto, data, bytes);
567 } else if (type == OUT_ADDRESS) {
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 = bytes; /* length of reference */
583 rr.refseg = segment; /* segment referred to */
584 write_reloc_rec(&rr);
587 pd = databuf; /* convert address to little-endian */
589 WRITESHORT(pd, *(int32_t *)data);
591 WRITEDLONG(pd, *(int64_t *)data);
593 WRITESHORT(pd, *(int32_t *)data);
595 membufwrite(segto, databuf, bytes);
597 } else if (type == OUT_REL2ADR) {
598 if (segment == segto)
599 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
602 rr.offset = getsegmentlength(segto); /* current offset */
603 rr.length = 2; /* length of reference */
604 rr.refseg = segment; /* segment referred to (will be >>1'd) */
606 if (segment != NO_SEG && segment % 2) {
607 rr.type = RDFREC_SEGRELOC;
608 rr.segment = segto; /* memory base refs *aren't ever* relative! */
609 write_reloc_rec(&rr);
611 /* what do we put in the code? Simply the data. This should almost
612 * always be zero, unless someone's doing segment arithmetic...
614 rr.offset = *(int32_t *)data;
616 rr.type = RDFREC_RELOC; /* type signature */
617 rr.segment = segto + 64; /* segment we're currently in + rel flag */
618 write_reloc_rec(&rr);
620 /* work out what to put in the code: offset of the end of this operand,
621 * subtracted from any data specified, so that loader can just add
622 * address of imported symbol onto it to get address relative to end of
623 * instruction: import_address + data(offset) - end_of_instrn */
625 rr.offset = *(int32_t *)data - (rr.offset + bytes);
628 membufwrite(segto, &rr.offset, -2);
629 } else if (type == OUT_REL4ADR) {
630 if ((segment == segto) && (globalbits != 64))
631 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
632 if (segment != NO_SEG && segment % 2) {
633 error(ERR_PANIC, "erm... 4 byte segment base ref?");
636 rr.type = RDFREC_RELOC; /* type signature */
637 rr.segment = segto + 64; /* segment we're currently in + rel tag */
638 rr.offset = getsegmentlength(segto); /* current offset */
639 rr.length = 4; /* length of reference */
640 rr.refseg = segment; /* segment referred to */
642 write_reloc_rec(&rr);
644 rr.offset = *(int32_t *)data - (rr.offset + bytes);
646 membufwrite(segto, &rr.offset, -4);
650 static void rdf2_cleanup(int debuginfo)
658 /* should write imported & exported symbol declarations to header here */
660 /* generate the output file... */
661 fwrite(RDOFF2Id, 6, 1, ofile); /* file type magic number */
663 if (bsslength != 0) { /* reserve BSS */
664 bs.type = RDFREC_BSS;
665 bs.amount = bsslength;
671 * calculate overall length of the output object
673 l = headerlength + 4;
675 for (i = 0; i < nsegments; i++) {
677 continue; /* skip BSS segment */
678 l += 10 + segments[i].seglength;
680 l += 10; /* null segment */
682 fwriteint32_t(l, ofile);
684 fwriteint32_t(headerlength, ofile);
685 saa_fpwrite(header, ofile); /* dump header */
688 for (i = 0; i < nsegments; i++) {
692 fwriteint16_t(segments[i].segtype, ofile);
693 fwriteint16_t(segments[i].segnumber, ofile);
694 fwriteint16_t(segments[i].segreserved, ofile);
695 fwriteint32_t(segments[i].seglength, ofile);
697 saa_fpwrite(seg[i], ofile);
701 /* null segment - write 10 bytes of zero */
702 fwriteint32_t(0, ofile);
703 fwriteint32_t(0, ofile);
704 fwriteint16_t(0, ofile);
709 static int32_t rdf2_segbase(int32_t segment)
715 * Handle RDOFF2 specific directives
717 static int rdf2_directive(char *directive, char *value, int pass)
721 /* Check if the name length is OK */
722 if ((n = strlen(value)) >= MODLIB_NAME_MAX) {
723 error(ERR_NONFATAL, "name size exceeds %d bytes", MODLIB_NAME_MAX);
727 if (!strcmp(directive, "library")) {
732 strcpy(r.libname, value);
738 if (!strcmp(directive, "module")) {
741 r.type = RDFREC_MODNAME;
743 strcpy(r.modname, value);
744 write_modname_rec(&r);
752 static void rdf2_filename(char *inname, char *outname, efunc error)
754 standard_extension(inname, outname, ".rdf", error);
757 static const char *rdf2_stdmac[] = {
758 "%define __SECT__ [section .text]",
759 "%imacro library 1+.nolist",
762 "%imacro module 1+.nolist",
765 "%macro __NASM_CDecl__ 1",
770 static int rdf2_set_info(enum geninfo type, char **val)
777 struct ofmt of_rdf2 = {
778 "Relocatable Dynamic Object File Format v2.0",