Fix "make doc"
[nasm/avx512.git] / output / outrdf2.c
blob1dfef93f93543eebf6d05fe3ad8d01d3c573ffd2
1 /* outrdf2.c output routines for the Netwide Assembler to produce
2 * RDOFF version 2 format object files (which are intended
3 * mainly for use in proprietary projects, as the code to
4 * load and execute them is very simple). They will also be
5 * used for device drivers and possibly some executable files
6 * in the MOSCOW operating system. See Rdoff.txt for
7 * details.
9 * The Netwide Assembler is copyright (C) 1996-1998 Simon Tatham and
10 * Julian Hall. All rights reserved. The software is
11 * redistributable under the licence given in the file "Licence"
12 * distributed in the NASM archive.
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <assert.h>
21 #include "nasm.h"
22 #include "nasmlib.h"
23 #include "outform.h"
25 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
26 #define VERBOSE_WARNINGS
28 #ifdef OF_RDF2
30 #define RDF_MAXSEGS 64 /* maximum number of segments - user configurable */
32 typedef unsigned short int16;
33 typedef unsigned char byte;
35 static const char *RDOFF2Id = "RDOFF2"; /* written to start of RDOFF files */
38 /* the records that can be found in the RDOFF header */
40 /* Note that whenever a segment is referred to in the RDOFF file, its number
41 * is always half of the segment number that NASM uses to refer to it; this
42 * is because NASM only allocates even numbered segments, so as to not
43 * waste any of the 16 bits of segment number written to the file - this
44 * allows up to 65533 external labels to be defined; otherwise it would be
45 * 32764. */
47 #define RDFREC_RELOC 1
48 #define RDFREC_IMPORT 2
49 #define RDFREC_GLOBAL 3
50 #define RDFREC_DLL 4
51 #define RDFREC_BSS 5
52 #define RDFREC_SEGRELOC 6
53 #define RDFREC_FARIMPORT 7
54 #define RDFREC_MODNAME 8
55 #define RDFREC_COMMON 10
56 #define RDFREC_GENERIC 0
59 struct RelocRec {
60 byte type; /* must be 1, or 6 for segment base ref */
61 byte reclen; /* set to 8 */
62 byte segment; /* only 0 for code, or 1 for data supported,
63 * but add 64 for relative refs (ie do not require
64 * reloc @ loadtime, only linkage) */
65 long offset; /* from start of segment in which reference is loc'd */
66 byte length; /* 1 2 or 4 bytes */
67 int16 refseg; /* segment to which reference refers to */
70 struct ImportRec {
71 byte type; /* must be 2, or 7 for FAR import */
72 byte reclen; /* equals 3+label length */
73 int16 segment; /* segment number allocated to the label for reloc
74 * records - label is assumed to be at offset zero
75 * in this segment, so linker must fix up with offset
76 * of segment and of offset within segment */
77 char label[33]; /* zero terminated... should be written to file until
78 * the zero, but not after it - max len = 32 chars */
81 struct ExportRec {
82 byte type; /* must be 3 */
83 byte reclen; /* equals 7+label length */
84 byte flags; /* SYM_* flags (see below) */
85 byte segment; /* segment referred to (0/1) */
86 long offset; /* offset within segment */
87 char label[33]; /* zero terminated as above. max len = 32 chars */
90 struct BSSRec {
91 byte type; /* must be 5 */
92 byte reclen; /* equals 4 */
93 long amount; /* number of bytes BSS to reserve */
96 struct DLLModRec {
97 byte type; /* 4 for DLLRec, 8 for ModRec */
98 byte reclen; /* 1+lib name length for DLLRec, 1+mod name length */
99 char name[128]; /* library to link at load time or module name */
102 struct CommonRec {
103 byte type; /* must be 10 */
104 byte reclen; /* equals 9+label length */
105 int16 segment; /* segment number */
106 long size; /* size of common variable */
107 int16 align; /* alignment (power of two) */
108 char label[33]; /* zero terminated as above. max len = 32 chars */
111 /* Flags for ExportRec */
112 #define SYM_DATA 0x01
113 #define SYM_FUNCTION 0x02
114 #define SYM_GLOBAL 0x04
116 #define COUNT_SEGTYPES 9
118 static char * segmenttypes[COUNT_SEGTYPES] = {
119 "null", "text", "code", "data", "comment", "lcomment", "pcomment",
120 "symdebug", "linedebug"
123 static int segmenttypenumbers[COUNT_SEGTYPES] = {
124 0, 1, 1, 2, 3, 4, 5, 6, 7
127 /* code for managing buffers needed to seperate code and data into individual
128 * sections until they are ready to be written to the file.
129 * We'd better hope that it all fits in memory else we're buggered... */
131 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
132 * on 80x86 machines for efficiency */
134 /***********************************************************************
135 * Actual code to deal with RDOFF2 ouput format begins here...
138 /* global variables set during the initialisation phase */
140 static struct SAA *seg[RDF_MAXSEGS]; /* seg 0 = code, seg 1 = data */
141 static struct SAA *header; /* relocation/import/export records */
143 static FILE *ofile;
145 static efunc error;
147 static struct seginfo {
148 char *segname;
149 int segnumber;
150 int16 segtype;
151 int16 segreserved;
152 long seglength;
153 } segments[RDF_MAXSEGS];
155 static int nsegments;
157 static long bsslength;
158 static long headerlength;
160 static void rdf2_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
162 int segtext, segdata, segbss;
164 /* set up the initial segments */
165 segments[0].segname = ".text";
166 segments[0].segnumber = 0;
167 segments[0].segtype = 1;
168 segments[0].segreserved = 0;
169 segments[0].seglength = 0;
171 segments[1].segname = ".data";
172 segments[1].segnumber = 1;
173 segments[1].segtype = 2;
174 segments[1].segreserved = 0;
175 segments[1].seglength = 0;
177 segments[2].segname = ".bss";
178 segments[2].segnumber = 2;
179 segments[2].segtype = 0xFFFF; /* reserved - should never be produced */
180 segments[2].segreserved = 0;
181 segments[2].seglength = 0;
183 nsegments = 3;
185 ofile = fp;
186 error = errfunc;
188 seg[0] = saa_init(1L);
189 seg[1] = saa_init(1L);
190 seg[2] = NULL; /* special case! */
192 header = saa_init(1L);
194 segtext = seg_alloc();
195 segdata = seg_alloc();
196 segbss = seg_alloc();
197 if (segtext != 0 || segdata != 2 || segbss != 4)
198 error(ERR_PANIC,"rdf segment numbers not allocated as expected (%d,%d,%d)",
199 segtext,segdata,segbss);
200 bsslength=0;
201 headerlength = 0;
204 static long rdf2_section_names(char *name, int pass, int *bits)
206 int i;
207 char * p, * q;
208 int code = -1;
209 int reserved = 0;
212 * Default is 32 bits, in the text segment.
214 if (!name) {
215 *bits = 32;
216 return 0;
219 /* look for segment type code following segment name */
220 p = name;
221 while (*p && !isspace(*p)) p++;
222 if (*p) { /* we're now in whitespace */
223 *p++ = '\0';
224 while (*p && isspace(80)) *p++ = '\0';
226 if (*p) { /* we're now in an attribute value */
228 * see if we have an optional ',number' following the type code
230 if ((q = strchr(p, ','))) {
231 *q++ = '\0';
233 reserved = readnum(q, &i);
234 if (i) {
235 error(ERR_NONFATAL, "value following comma must be numeric");
236 reserved = 0;
240 * check it against the text strings in segmenttypes
243 for (i = 0; i < COUNT_SEGTYPES; i++)
244 if (!nasm_stricmp(p, segmenttypes[i])) {
245 code = segmenttypenumbers[i];
246 break;
248 if (code == -1) { /* didn't find anything */
249 code = readnum(p, &i);
250 if (i) {
251 error(ERR_NONFATAL, "unrecognised RDF segment type (%s)",p);
252 code = 3;
256 for (i = 0; i < nsegments; i++) {
257 if (!strcmp(name, segments[i].segname)) {
258 if (code != -1 || reserved != 0)
259 error(ERR_NONFATAL, "segment attributes specified on"
260 " redeclaration of segment");
261 return segments[i].segnumber * 2;
265 /* declaring a new segment! */
267 if (code == -1) {
268 error(ERR_NONFATAL, "new segment declared without type code");
269 code = 3;
271 if (nsegments == RDF_MAXSEGS) {
272 error(ERR_FATAL, "reached compiled-in maximum segment limit (%d)",
273 RDF_MAXSEGS);
274 return NO_SEG;
277 segments[nsegments].segname = nasm_strdup(name);
278 i = seg_alloc();
279 if (i % 2 != 0)
280 error(ERR_PANIC, "seg_alloc() returned odd number");
281 segments[nsegments].segnumber = i >> 1;
282 segments[nsegments].segtype = code;
283 segments[nsegments].segreserved = reserved;
284 segments[nsegments].seglength = 0;
286 seg[nsegments] = saa_init(1L);
288 return i;
291 static void write_reloc_rec(struct RelocRec *r)
293 char buf[4],*b;
295 if (r->refseg != (int16)NO_SEG && (r->refseg & 1)) /* segment base ref */
296 r->type = RDFREC_SEGRELOC;
298 r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
300 saa_wbytes(header,&r->type,1);
301 saa_wbytes(header,&r->reclen,1);
302 saa_wbytes(header,&r->segment,1);
303 b = buf; WRITELONG(b,r->offset);
304 saa_wbytes(header,buf,4);
305 saa_wbytes(header,&r->length,1);
306 b = buf; WRITESHORT(b,r->refseg);
307 saa_wbytes(header,buf,2);
308 headerlength += r->reclen + 2;
311 static void write_export_rec(struct ExportRec *r)
313 char buf[4], *b;
315 r->segment >>= 1;
317 saa_wbytes(header,&r->type,1);
318 saa_wbytes(header,&r->reclen,1);
319 saa_wbytes(header,&r->flags,1);
320 saa_wbytes(header,&r->segment,1);
321 b = buf; WRITELONG(b,r->offset);
322 saa_wbytes(header,buf,4);
323 saa_wbytes(header,r->label,strlen(r->label) + 1);
324 headerlength += r->reclen + 2;
327 static void write_import_rec(struct ImportRec *r)
329 char buf[4], *b;
331 r->segment >>= 1;
333 saa_wbytes(header,&r->type,1);
334 saa_wbytes(header,&r->reclen,1);
335 b = buf; WRITESHORT(b,r->segment);
336 saa_wbytes(header,buf,2);
337 saa_wbytes(header,r->label,strlen(r->label) + 1);
338 headerlength += r->reclen + 2;
341 static void write_bss_rec(struct BSSRec *r)
343 char buf[4], *b;
345 saa_wbytes(header,&r->type,1);
346 saa_wbytes(header,&r->reclen,1);
347 b = buf; WRITELONG(b,r->amount);
348 saa_wbytes(header,buf,4);
349 headerlength += r->reclen + 2;
353 * Write common variable record.
355 static void write_common_rec(struct CommonRec *r)
357 char buf[4], *b;
359 r->segment >>= 1;
361 saa_wbytes(header,&r->type,1);
362 saa_wbytes(header,&r->reclen,1);
363 b = buf; WRITESHORT(b,r->segment);
364 saa_wbytes(header,buf,2);
365 b = buf; WRITELONG(b,r->size);
366 saa_wbytes(header,buf,4);
367 b = buf; WRITESHORT(b,r->align);
368 saa_wbytes(header,buf,2);
369 saa_wbytes(header,r->label,strlen(r->label) + 1);
370 headerlength += r->reclen + 2;
374 * Write library record. Also used for module name records.
376 static void write_dllmod_rec(struct DLLModRec *r)
378 saa_wbytes(header,&r->type,1);
379 saa_wbytes(header,&r->reclen,1);
380 saa_wbytes(header,r->name,strlen(r->name) + 1);
381 headerlength += r->reclen + 2;
384 static void rdf2_deflabel(char *name, long segment, long offset,
385 int is_global, char *special)
387 struct ExportRec r;
388 struct ImportRec ri;
389 struct CommonRec ci;
390 static int farsym = 0;
391 static int i;
392 byte export_flags = 0;
394 if (is_global == 2) {
395 /* Common variable */
396 ci.type = RDFREC_COMMON;
397 ci.size = offset;
398 ci.segment = segment;
399 strncpy(ci.label, name, 32);
400 ci.label[32] = 0;
401 ci.reclen = 9 + strlen(ci.label);
402 ci.align = 0;
405 * Check the special text to see if it's a valid number and power
406 * of two; if so, store it as the alignment for the common variable.
408 if (special) {
409 int err;
410 ci.align = readnum(special, &err);
411 if (err) error(ERR_NONFATAL, "alignment constraint `%s' is not a"
412 " valid number", special);
413 else if ( (ci.align | (ci.align-1)) != 2*ci.align - 1)
414 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
415 " power of two", special);
417 write_common_rec(&ci);
420 /* We don't care about local labels or fix-up hints */
421 if (is_global != 1) return;
423 if (special) {
424 while(*special == ' ' || *special == '\t') special++;
426 if (!nasm_strnicmp(special, "export", 6)) {
427 special += 6;
428 export_flags |= SYM_GLOBAL;
431 if (*special) {
432 while(isspace(*special)) special++;
433 if (!nasm_stricmp(special, "far")) {
434 farsym = 1;
436 else if (!nasm_stricmp(special, "near")) {
437 farsym = 0;
439 else if (!nasm_stricmp(special, "proc") ||
440 !nasm_stricmp(special, "function")) {
441 export_flags |= SYM_FUNCTION;
443 else if (!nasm_stricmp(special, "data") ||
444 !nasm_stricmp(special, "object")) {
445 export_flags |= SYM_DATA;
447 else
448 error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
452 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
453 error (ERR_NONFATAL, "unrecognised special symbol `%s'", name);
454 return;
457 for (i = 0; i < nsegments; i++) {
458 if (segments[i].segnumber == segment>>1) break;
460 if (i >= nsegments) { /* EXTERN declaration */
461 if (farsym)
462 ri.type = RDFREC_FARIMPORT;
463 else
464 ri.type = RDFREC_IMPORT;
465 ri.segment = segment;
466 strncpy(ri.label,name,32);
467 ri.label[32] = 0;
468 ri.reclen = 3 + strlen(ri.label);
469 write_import_rec(&ri);
470 } else if (is_global) {
471 r.type = RDFREC_GLOBAL;
472 r.flags = export_flags;
473 r.segment = segment;
474 r.offset = offset;
475 strncpy(r.label,name,32);
476 r.label[32] = 0;
477 r.reclen = 7 + strlen(r.label);
478 write_export_rec(&r);
482 static void membufwrite(int segment, const void * data, int bytes)
484 int i;
485 char buf[4], * b;
487 for (i = 0; i < nsegments; i++) {
488 if (segments[i].segnumber == segment) break;
490 if (i == nsegments)
491 error(ERR_PANIC, "can't find segment %d", segment);
493 if (bytes < 0) {
494 b = buf;
495 if (bytes == -2)
496 WRITESHORT(b,*(short *)data);
497 else
498 WRITELONG(b,*(long *)data);
499 data = buf;
500 bytes = -bytes;
502 segments[i].seglength += bytes;
503 saa_wbytes(seg[i],data,bytes);
506 static int getsegmentlength(int segment)
508 int i;
509 for (i = 0; i < nsegments; i++) {
510 if (segments[i].segnumber == segment) break;
512 if (i == nsegments)
513 error(ERR_PANIC, "can't find segment %d", segment);
515 return segments[i].seglength;
518 static void rdf2_out (long segto, const void *data, unsigned long type,
519 long segment, long wrt)
521 long bytes = type & OUT_SIZMASK;
522 struct RelocRec rr;
523 unsigned char databuf[4],*pd;
524 int seg;
526 if (segto == NO_SEG) {
527 if ((type & OUT_TYPMASK) != OUT_RESERVE)
528 error (ERR_NONFATAL, "attempt to assemble code in ABSOLUTE space");
529 return;
532 segto >>= 1; /* convert NASM segment no to RDF number */
534 for (seg = 0; seg < nsegments; seg++) {
535 if (segments[seg].segnumber == segto) break;
537 if (seg >= nsegments) {
538 error(ERR_NONFATAL,"specified segment not supported by rdf output format");
539 return;
542 if (wrt != NO_SEG) {
543 wrt = NO_SEG; /* continue to do _something_ */
544 error (ERR_NONFATAL, "WRT not supported by rdf output format");
547 type &= OUT_TYPMASK;
549 if (segto == 2 && type != OUT_RESERVE)
551 error(ERR_NONFATAL, "BSS segments may not be initialised");
553 /* just reserve the space for now... */
555 if (type == OUT_REL2ADR)
556 bytes = 2;
557 else
558 bytes = 4;
559 type = OUT_RESERVE;
562 if (type == OUT_RESERVE) {
563 if (segto == 2) /* BSS segment space reserverd */
564 bsslength += bytes;
565 else
566 while (bytes --)
567 membufwrite(segto,databuf,1);
569 else if (type == OUT_RAWDATA) {
570 if (segment != NO_SEG)
571 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
573 membufwrite(segto,data,bytes);
575 else if (type == OUT_ADDRESS) {
577 /* if segment == NO_SEG then we are writing an address of an
578 object within the same segment - do not produce reloc rec. */
580 /* FIXME - is this behaviour sane? at first glance it doesn't
581 appear to be. Must test this thoroughly...! */
583 if (segment != NO_SEG)
585 /* it's an address, so we must write a relocation record */
587 rr.type = RDFREC_RELOC; /* type signature */
588 rr.reclen = 8;
589 rr.segment = segto; /* segment we're currently in */
590 rr.offset = getsegmentlength(segto); /* current offset */
591 rr.length = bytes; /* length of reference */
592 rr.refseg = segment; /* segment referred to */
593 write_reloc_rec(&rr);
596 pd = databuf; /* convert address to little-endian */
597 if (bytes == 2)
598 WRITESHORT (pd, *(long *)data);
599 else
600 WRITELONG (pd, *(long *)data);
602 membufwrite(segto,databuf,bytes);
605 else if (type == OUT_REL2ADR)
607 if (segment == segto)
608 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
610 rr.reclen = 8;
611 rr.offset = getsegmentlength(segto); /* current offset */
612 rr.length = 2; /* length of reference */
613 rr.refseg = segment; /* segment referred to (will be >>1'd)*/
615 if (segment != NO_SEG && segment % 2) {
616 rr.type = RDFREC_SEGRELOC;
617 rr.segment = segto; /* memory base refs *aren't ever* relative! */
618 write_reloc_rec(&rr);
620 /* what do we put in the code? Simply the data. This should almost
621 * always be zero, unless someone's doing segment arithmetic...
623 rr.offset = *(long *) data;
625 else
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 = *(long *)data -(rr.offset + bytes);
639 membufwrite(segto,&rr.offset,-2);
641 else if (type == OUT_REL4ADR)
643 if (segment == segto)
644 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
645 if (segment != NO_SEG && segment % 2) {
646 error(ERR_PANIC, "erm... 4 byte segment base ref?");
649 rr.type = RDFREC_RELOC; /* type signature */
650 rr.segment = segto+64; /* segment we're currently in + rel tag */
651 rr.offset = getsegmentlength(segto); /* current offset */
652 rr.length = 4; /* length of reference */
653 rr.refseg = segment; /* segment referred to */
654 rr.reclen = 8;
655 write_reloc_rec(&rr);
657 rr.offset = *(long *)data -(rr.offset + bytes);
659 membufwrite(segto,&rr.offset,-4);
663 static void rdf2_cleanup (int debuginfo) {
664 long l;
665 struct BSSRec bs;
666 int i;
668 (void) debuginfo;
670 /* should write imported & exported symbol declarations to header here */
672 /* generate the output file... */
673 fwrite(RDOFF2Id,6,1,ofile); /* file type magic number */
675 if (bsslength != 0) /* reserve BSS */
677 bs.type = RDFREC_BSS;
678 bs.amount = bsslength;
679 bs.reclen = 4;
680 write_bss_rec(&bs);
684 * calculate overall length of the output object
686 l = headerlength + 4;
688 for (i = 0; i < nsegments; i++) {
689 if (i == 2) continue; /* skip BSS segment */
690 l += 10 + segments[i].seglength;
692 l += 10; /* null segment */
694 fwritelong(l, ofile);
696 fwritelong(headerlength, ofile);
697 saa_fpwrite(header,ofile); /* dump header */
698 saa_free(header);
700 for (i = 0; i < nsegments; i++) {
701 if (i == 2) continue;
703 fwriteshort(segments[i].segtype, ofile);
704 fwriteshort(segments[i].segnumber, ofile);
705 fwriteshort(segments[i].segreserved, ofile);
706 fwritelong(segments[i].seglength, ofile);
708 saa_fpwrite(seg[i], ofile);
709 saa_free(seg[i]);
712 /* null segment - write 10 bytes of zero */
713 fwritelong(0,ofile);
714 fwritelong(0,ofile);
715 fwriteshort(0,ofile);
717 fclose(ofile);
720 static long rdf2_segbase (long segment) {
721 return segment;
724 static int rdf2_directive (char *directive, char *value, int pass) {
725 struct DLLModRec r;
727 if (! strcmp(directive, "library")) {
728 if (pass == 1) {
729 r.type = RDFREC_DLL;
730 r.reclen=strlen(value)+1;
731 strcpy(r.name, value);
732 write_dllmod_rec(&r);
734 return 1;
737 if (! strcmp(directive, "module")) {
738 if (pass == 1) {
739 r.type = RDFREC_MODNAME;
740 r.reclen=strlen(value)+1;
741 strcpy(r.name, value);
742 write_dllmod_rec(&r);
744 return 1;
747 return 0;
750 static void rdf2_filename (char *inname, char *outname, efunc error) {
751 standard_extension(inname,outname,".rdf",error);
754 static const char *rdf2_stdmac[] = {
755 "%define __SECT__ [section .text]",
756 "%imacro library 1+.nolist",
757 "[library %1]",
758 "%endmacro",
759 "%imacro module 1+.nolist",
760 "[module %1]",
761 "%endmacro",
762 "%macro __NASM_CDecl__ 1",
763 "%endmacro",
764 NULL
767 static int rdf2_set_info(enum geninfo type, char **val)
769 return 0;
773 struct ofmt of_rdf2 = {
774 "Relocatable Dynamic Object File Format v2.0",
775 "rdf",
776 NULL,
777 null_debug_arr,
778 &null_debug_form,
779 rdf2_stdmac,
780 rdf2_init,
781 rdf2_set_info,
782 rdf2_out,
783 rdf2_deflabel,
784 rdf2_section_names,
785 rdf2_segbase,
786 rdf2_directive,
787 rdf2_filename,
788 rdf2_cleanup
791 #endif /* OF_RDF2 */