Makefile/dependency updates
[nasm/avx512.git] / output / outrdf2.c
bloba1e0b9a7ef00881efe11f014004d4e8ce73ac07f
1 /*
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 license given in the file "LICENSE"
9 * distributed in the NASM archive.
12 #include "compiler.h"
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <assert.h>
19 #include <inttypes.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 #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
40 * 32764. */
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 */
70 static FILE *ofile;
72 static efunc error;
74 static struct seginfo {
75 char *segname;
76 int segnumber;
77 uint16_t segtype;
78 uint16_t segreserved;
79 int32_t seglength;
80 } segments[RDF_MAXSEGS];
82 static int nsegments;
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;
91 (void)ldef;
92 (void)eval;
94 maxbits = 64;
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;
115 nsegments = 3;
117 ofile = fp;
118 error = errfunc;
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)
130 error(ERR_PANIC,
131 "rdf segment numbers not allocated as expected (%d,%d,%d)",
132 segtext, segdata, segbss);
133 bsslength = 0;
134 headerlength = 0;
137 static int32_t rdf2_section_names(char *name, int pass, int *bits)
139 int i;
140 bool err;
141 char *p, *q;
142 int code = -1;
143 int reserved = 0;
145 (void)pass;
148 * Default is 32 bits, in the text segment.
150 if (!name) {
151 *bits = 32;
152 return 0;
155 /* look for segment type code following segment name */
156 p = name;
157 while (*p && !isspace(*p))
158 p++;
159 if (*p) { /* we're now in whitespace */
160 *p++ = '\0';
161 while (*p && isspace(80))
162 *p++ = '\0';
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, ','))) {
169 *q++ = '\0';
171 reserved = readnum(q, &err);
172 if (err) {
173 error(ERR_NONFATAL,
174 "value following comma must be numeric");
175 reserved = 0;
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];
185 break;
187 if (code == -1) { /* didn't find anything */
188 code = readnum(p, &err);
189 if (err) {
190 error(ERR_NONFATAL, "unrecognised RDF segment type (%s)",
192 code = 3;
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! */
207 if (code == -1) {
208 error(ERR_NONFATAL, "new segment declared without type code");
209 code = 3;
211 if (nsegments == RDF_MAXSEGS) {
212 error(ERR_FATAL, "reached compiled-in maximum segment limit (%d)",
213 RDF_MAXSEGS);
214 return NO_SEG;
217 segments[nsegments].segname = nasm_strdup(name);
218 i = seg_alloc();
219 if (i % 2 != 0)
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);
228 return i;
232 * Write relocation record
234 static void write_reloc_rec(struct RelocRec *r)
236 char buf[4], *b;
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);
246 b = buf;
247 WRITELONG(b, r->offset);
248 saa_wbytes(header, buf, 4);
249 saa_wbytes(header, &r->length, 1);
250 b = buf;
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)
261 char buf[4], *b;
263 r->segment >>= 1;
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);
269 b = buf;
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)
278 char buf[4], *b;
280 r->segment >>= 1;
282 saa_wbytes(header, &r->type, 1);
283 saa_wbytes(header, &r->reclen, 1);
284 saa_wbytes(header, &r->flags, 1);
285 b = buf;
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;
293 * Write BSS record
295 static void write_bss_rec(struct BSSRec *r)
297 char buf[4], *b;
299 saa_wbytes(header, &r->type, 1);
300 saa_wbytes(header, &r->reclen, 1);
301 b = buf;
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)
312 char buf[4], *b;
314 r->segment >>= 1;
316 saa_wbytes(header, &r->type, 1);
317 saa_wbytes(header, &r->reclen, 1);
318 b = buf;
319 WRITESHORT(b, r->segment);
320 saa_wbytes(header, buf, 2);
321 b = buf;
322 WRITELONG(b, r->size);
323 saa_wbytes(header, buf, 4);
324 b = buf;
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)
359 struct ExportRec r;
360 struct ImportRec ri;
361 struct CommonRec ci;
362 static int farsym = 0;
363 static int i;
364 char symflags = 0;
365 int len;
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);
370 return;
372 if (!len) {
373 error(ERR_NONFATAL, "zero-length label");
374 return;
377 if (is_global == 2) {
378 /* Common variable */
379 ci.type = RDFREC_COMMON;
380 ci.size = offset;
381 ci.segment = segment;
382 strcpy(ci.label, name);
383 ci.reclen = 9 + len;
384 ci.align = 0;
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.
390 if (special) {
391 bool err;
392 ci.align = readnum(special, &err);
393 if (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 */
404 if (is_global != 1)
405 return;
407 if (special) {
408 while (*special == ' ' || *special == '\t')
409 special++;
411 if (!nasm_strnicmp(special, "export", 6)) {
412 special += 6;
413 symflags |= SYM_GLOBAL;
414 } else if (!nasm_strnicmp(special, "import", 6)) {
415 special += 6;
416 symflags |= SYM_IMPORT;
419 if (*special) {
420 while (isspace(*special))
421 special++;
422 if (!nasm_stricmp(special, "far")) {
423 farsym = 1;
424 } else if (!nasm_stricmp(special, "near")) {
425 farsym = 0;
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;
432 } else
433 error(ERR_NONFATAL, "unrecognised symbol type `%s'",
434 special);
438 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
439 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
440 return;
443 for (i = 0; i < nsegments; i++) {
444 if (segments[i].segnumber == segment >> 1)
445 break;
448 if (i >= nsegments) { /* EXTERN declaration */
449 ri.type = farsym ? RDFREC_FARIMPORT : RDFREC_IMPORT;
450 if (symflags & SYM_GLOBAL)
451 error(ERR_NONFATAL,
452 "symbol type conflict - EXTERN cannot be EXPORT");
453 ri.flags = symflags;
454 ri.segment = segment;
455 strcpy(ri.label, name);
456 ri.reclen = 4 + len;
457 write_import_rec(&ri);
458 } else if (is_global) {
459 r.type = RDFREC_GLOBAL; /* GLOBAL declaration */
460 if (symflags & SYM_IMPORT)
461 error(ERR_NONFATAL,
462 "symbol type conflict - GLOBAL cannot be IMPORT");
463 r.flags = symflags;
464 r.segment = segment;
465 r.offset = offset;
466 strcpy(r.label, name);
467 r.reclen = 7 + len;
468 write_export_rec(&r);
472 static void membufwrite(int segment, const void *data, int bytes)
474 int i;
475 char buf[4], *b;
477 for (i = 0; i < nsegments; i++) {
478 if (segments[i].segnumber == segment)
479 break;
481 if (i == nsegments)
482 error(ERR_PANIC, "can't find segment %d", segment);
484 if (bytes < 0) {
485 b = buf;
486 if (bytes == -2)
487 WRITESHORT(b, *(int16_t *)data);
488 else
489 WRITELONG(b, *(int32_t *)data);
490 data = buf;
491 bytes = -bytes;
493 segments[i].seglength += bytes;
494 saa_wbytes(seg[i], data, bytes);
497 static int getsegmentlength(int segment)
499 int i;
500 for (i = 0; i < nsegments; i++) {
501 if (segments[i].segnumber == segment)
502 break;
504 if (i == nsegments)
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,
511 enum out_type type, uint64_t size,
512 int32_t segment, int32_t wrt)
514 struct RelocRec rr;
515 uint8_t databuf[8], *pd;
516 int seg;
518 if (segto == NO_SEG) {
519 if (type != OUT_RESERVE)
520 error(ERR_NONFATAL,
521 "attempt to assemble code in ABSOLUTE space");
522 return;
525 segto >>= 1; /* convert NASM segment no to RDF number */
527 for (seg = 0; seg < nsegments; seg++) {
528 if (segments[seg].segnumber == segto)
529 break;
531 if (seg >= nsegments) {
532 error(ERR_NONFATAL,
533 "specified segment not supported by rdf output format");
534 return;
537 if (wrt != NO_SEG) {
538 wrt = NO_SEG; /* continue to do _something_ */
539 error(ERR_NONFATAL, "WRT not supported by rdf output format");
542 if (segto == 2 && type != OUT_RESERVE) {
543 error(ERR_NONFATAL, "BSS segments may not be initialized");
545 /* just reserve the space for now... */
547 if (type == OUT_REL2ADR)
548 size = 2;
549 else
550 size = 4;
551 type = OUT_RESERVE;
554 if (type == OUT_RESERVE) {
555 if (segto == 2) /* BSS segment space reserverd */
556 bsslength += size;
557 else
558 while (size--)
559 membufwrite(segto, databuf, 1);
560 } else if (type == OUT_RAWDATA) {
561 if (segment != NO_SEG)
562 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
564 membufwrite(segto, data, size);
565 } else if (type == OUT_ADDRESS) {
567 /* if segment == NO_SEG then we are writing an address of an
568 object within the same segment - do not produce reloc rec. */
570 /* FIXME - is this behaviour sane? at first glance it doesn't
571 appear to be. Must test this thoroughly...! */
573 if (segment != NO_SEG) {
574 /* it's an address, so we must write a relocation record */
576 rr.type = RDFREC_RELOC; /* type signature */
577 rr.reclen = 8;
578 rr.segment = segto; /* segment we're currently in */
579 rr.offset = getsegmentlength(segto); /* current offset */
580 rr.length = size; /* length of reference */
581 rr.refseg = segment; /* segment referred to */
582 write_reloc_rec(&rr);
585 pd = databuf; /* convert address to little-endian */
586 WRITEADDR(pd, *(int64_t *)data, size);
587 membufwrite(segto, databuf, size);
588 } else if (type == OUT_REL2ADR) {
589 if (segment == segto)
590 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
592 rr.reclen = 8;
593 rr.offset = getsegmentlength(segto); /* current offset */
594 rr.length = 2; /* length of reference */
595 rr.refseg = segment; /* segment referred to (will be >>1'd) */
597 if (segment != NO_SEG && segment % 2) {
598 rr.type = RDFREC_SEGRELOC;
599 rr.segment = segto; /* memory base refs *aren't ever* relative! */
600 write_reloc_rec(&rr);
602 /* what do we put in the code? Simply the data. This should almost
603 * always be zero, unless someone's doing segment arithmetic...
605 rr.offset = *(int64_t *)data;
606 } else {
607 rr.type = RDFREC_RELOC; /* type signature */
608 rr.segment = segto + 64; /* segment we're currently in + rel flag */
609 write_reloc_rec(&rr);
611 /* work out what to put in the code: offset of the end of this operand,
612 * subtracted from any data specified, so that loader can just add
613 * address of imported symbol onto it to get address relative to end of
614 * instruction: import_address + data(offset) - end_of_instrn */
616 rr.offset = *(int32_t *)data - (rr.offset + size);
619 membufwrite(segto, &rr.offset, -2);
620 } else if (type == OUT_REL4ADR) {
621 if ((segment == segto) && (globalbits != 64))
622 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
623 if (segment != NO_SEG && segment % 2) {
624 error(ERR_PANIC, "erm... 4 byte segment base ref?");
627 rr.type = RDFREC_RELOC; /* type signature */
628 rr.segment = segto + 64; /* segment we're currently in + rel tag */
629 rr.offset = getsegmentlength(segto); /* current offset */
630 rr.length = 4; /* length of reference */
631 rr.refseg = segment; /* segment referred to */
632 rr.reclen = 8;
633 write_reloc_rec(&rr);
635 rr.offset = *(int64_t *)data - (rr.offset + size);
637 membufwrite(segto, &rr.offset, -4);
641 static void rdf2_cleanup(int debuginfo)
643 int32_t l;
644 struct BSSRec bs;
645 int i;
647 (void)debuginfo;
649 /* should write imported & exported symbol declarations to header here */
651 /* generate the output file... */
652 fwrite(RDOFF2Id, 6, 1, ofile); /* file type magic number */
654 if (bsslength != 0) { /* reserve BSS */
655 bs.type = RDFREC_BSS;
656 bs.amount = bsslength;
657 bs.reclen = 4;
658 write_bss_rec(&bs);
662 * calculate overall length of the output object
664 l = headerlength + 4;
666 for (i = 0; i < nsegments; i++) {
667 if (i == 2)
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 */
677 saa_free(header);
679 for (i = 0; i < nsegments; i++) {
680 if (i == 2)
681 continue;
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);
689 saa_free(seg[i]);
692 /* null segment - write 10 bytes of zero */
693 fwriteint32_t(0, ofile);
694 fwriteint32_t(0, ofile);
695 fwriteint16_t(0, ofile);
697 fclose(ofile);
700 static int32_t rdf2_segbase(int32_t segment)
702 return segment;
706 * Handle RDOFF2 specific directives
708 static int rdf2_directive(char *directive, char *value, int pass)
710 int n;
712 /* Check if the name length is OK */
713 if ((n = strlen(value)) >= MODLIB_NAME_MAX) {
714 error(ERR_NONFATAL, "name size exceeds %d bytes", MODLIB_NAME_MAX);
715 return 0;
718 if (!strcmp(directive, "library")) {
719 if (pass == 1) {
720 struct DLLRec r;
721 r.type = RDFREC_DLL;
722 r.reclen = n + 1;
723 strcpy(r.libname, value);
724 write_dll_rec(&r);
726 return 1;
729 if (!strcmp(directive, "module")) {
730 if (pass == 1) {
731 struct ModRec r;
732 r.type = RDFREC_MODNAME;
733 r.reclen = n + 1;
734 strcpy(r.modname, value);
735 write_modname_rec(&r);
737 return 1;
740 return 0;
743 static void rdf2_filename(char *inname, char *outname, efunc error)
745 standard_extension(inname, outname, ".rdf", error);
748 static const char *rdf2_stdmac[] = {
749 "%define __SECT__ [section .text]",
750 "%imacro library 1+.nolist",
751 "[library %1]",
752 "%endmacro",
753 "%imacro module 1+.nolist",
754 "[module %1]",
755 "%endmacro",
756 "%macro __NASM_CDecl__ 1",
757 "%endmacro",
758 NULL
761 static int rdf2_set_info(enum geninfo type, char **val)
763 (void)type;
764 (void)val;
765 return 0;
768 struct ofmt of_rdf2 = {
769 "Relocatable Dynamic Object File Format v2.0",
770 "rdf",
771 NULL,
772 null_debug_arr,
773 &null_debug_form,
774 rdf2_stdmac,
775 rdf2_init,
776 rdf2_set_info,
777 rdf2_out,
778 rdf2_deflabel,
779 rdf2_section_names,
780 rdf2_segbase,
781 rdf2_directive,
782 rdf2_filename,
783 rdf2_cleanup
786 #endif /* OF_RDF2 */