Update dependencies
[nasm/avx512.git] / output / outrdf2.c
blob6c7a1027cebf49531b31f0dcd5d52ade2fde3608
1 /*
2 * outrdf2.c output routines for the Netwide Assembler to produce
3 * RDOFF version 2 format object files, which is used as a
4 * main binary format in the RadiOS (http://radios.sf.net).
5 * Originally Julian planned to use it in his MOSCOW
6 * operating system.
8 * The Netwide Assembler is copyright (C) 1996-1998 Simon Tatham and
9 * Julian Hall. All rights reserved. The software is
10 * redistributable under the licence given in the file "Licence"
11 * distributed in the NASM archive.
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <assert.h>
20 #include "nasm.h"
21 #include "nasmlib.h"
22 #include "outform.h"
24 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
25 #define VERBOSE_WARNINGS
27 #ifdef OF_RDF2
29 #include "rdoff/rdoff.h"
31 /* This signature is written to start of RDOFF files */
32 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 segtype;
78 uint16 segreserved;
79 long seglength;
80 } segments[RDF_MAXSEGS];
82 static int nsegments;
84 static long bsslength;
85 static long headerlength;
87 static void rdf2_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
89 int segtext, segdata, segbss;
91 /* set up the initial segments */
92 segments[0].segname = ".text";
93 segments[0].segnumber = 0;
94 segments[0].segtype = 1;
95 segments[0].segreserved = 0;
96 segments[0].seglength = 0;
98 segments[1].segname = ".data";
99 segments[1].segnumber = 1;
100 segments[1].segtype = 2;
101 segments[1].segreserved = 0;
102 segments[1].seglength = 0;
104 segments[2].segname = ".bss";
105 segments[2].segnumber = 2;
106 segments[2].segtype = 0xFFFF; /* reserved - should never be produced */
107 segments[2].segreserved = 0;
108 segments[2].seglength = 0;
110 nsegments = 3;
112 ofile = fp;
113 error = errfunc;
115 seg[0] = saa_init(1L);
116 seg[1] = saa_init(1L);
117 seg[2] = NULL; /* special case! */
119 header = saa_init(1L);
121 segtext = seg_alloc();
122 segdata = seg_alloc();
123 segbss = seg_alloc();
124 if (segtext != 0 || segdata != 2 || segbss != 4)
125 error(ERR_PANIC,"rdf segment numbers not allocated as expected (%d,%d,%d)",
126 segtext,segdata,segbss);
127 bsslength=0;
128 headerlength = 0;
131 static long rdf2_section_names(char *name, int pass, int *bits)
133 int i;
134 char * p, * q;
135 int code = -1;
136 int reserved = 0;
139 * Default is 32 bits, in the text segment.
141 if (!name) {
142 *bits = 32;
143 return 0;
146 /* look for segment type code following segment name */
147 p = name;
148 while (*p && !isspace(*p)) p++;
149 if (*p) { /* we're now in whitespace */
150 *p++ = '\0';
151 while (*p && isspace(80)) *p++ = '\0';
153 if (*p) { /* we're now in an attribute value */
155 * see if we have an optional ',number' following the type code
157 if ((q = strchr(p, ','))) {
158 *q++ = '\0';
160 reserved = readnum(q, &i);
161 if (i) {
162 error(ERR_NONFATAL, "value following comma must be numeric");
163 reserved = 0;
167 * check it against the text strings in segmenttypes
170 for (i = 0; i < COUNT_SEGTYPES; i++)
171 if (!nasm_stricmp(p, segmenttypes[i])) {
172 code = segmenttypenumbers[i];
173 break;
175 if (code == -1) { /* didn't find anything */
176 code = readnum(p, &i);
177 if (i) {
178 error(ERR_NONFATAL, "unrecognised RDF segment type (%s)",p);
179 code = 3;
183 for (i = 0; i < nsegments; i++) {
184 if (!strcmp(name, segments[i].segname)) {
185 if (code != -1 || reserved != 0)
186 error(ERR_NONFATAL, "segment attributes specified on"
187 " redeclaration of segment");
188 return segments[i].segnumber * 2;
192 /* declaring a new segment! */
194 if (code == -1) {
195 error(ERR_NONFATAL, "new segment declared without type code");
196 code = 3;
198 if (nsegments == RDF_MAXSEGS) {
199 error(ERR_FATAL, "reached compiled-in maximum segment limit (%d)",
200 RDF_MAXSEGS);
201 return NO_SEG;
204 segments[nsegments].segname = nasm_strdup(name);
205 i = seg_alloc();
206 if (i % 2 != 0)
207 error(ERR_PANIC, "seg_alloc() returned odd number");
208 segments[nsegments].segnumber = i >> 1;
209 segments[nsegments].segtype = code;
210 segments[nsegments].segreserved = reserved;
211 segments[nsegments].seglength = 0;
213 seg[nsegments] = saa_init(1L);
215 return i;
220 * Write relocation record
222 static void write_reloc_rec(struct RelocRec *r)
224 char buf[4],*b;
226 if (r->refseg != (uint16)NO_SEG && (r->refseg & 1)) /* segment base ref */
227 r->type = RDFREC_SEGRELOC;
229 r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
231 saa_wbytes(header,&r->type,1);
232 saa_wbytes(header,&r->reclen,1);
233 saa_wbytes(header,&r->segment,1);
234 b = buf; WRITELONG(b,r->offset);
235 saa_wbytes(header,buf,4);
236 saa_wbytes(header,&r->length,1);
237 b = buf; WRITESHORT(b,r->refseg);
238 saa_wbytes(header,buf,2);
239 headerlength += r->reclen + 2;
244 * Write export record
246 static void write_export_rec(struct ExportRec *r)
248 char buf[4], *b;
250 r->segment >>= 1;
252 saa_wbytes(header, &r->type, 1);
253 saa_wbytes(header, &r->reclen, 1);
254 saa_wbytes(header, &r->flags, 1);
255 saa_wbytes(header, &r->segment, 1);
256 b = buf; WRITELONG(b, r->offset);
257 saa_wbytes(header, buf, 4);
258 saa_wbytes(header, r->label, strlen(r->label) + 1);
259 headerlength += r->reclen + 2;
262 static void write_import_rec(struct ImportRec *r)
264 char buf[4], *b;
266 r->segment >>= 1;
268 saa_wbytes(header, &r->type, 1);
269 saa_wbytes(header, &r->reclen, 1);
270 saa_wbytes(header, &r->flags, 1);
271 b = buf; WRITESHORT(b,r->segment);
272 saa_wbytes(header, buf, 2);
273 saa_wbytes(header, r->label, strlen(r->label) + 1);
274 headerlength += r->reclen + 2;
279 * Write BSS record
281 static void write_bss_rec(struct BSSRec *r)
283 char buf[4], *b;
285 saa_wbytes(header,&r->type,1);
286 saa_wbytes(header,&r->reclen,1);
287 b = buf; WRITELONG(b,r->amount);
288 saa_wbytes(header,buf,4);
289 headerlength += r->reclen + 2;
293 * Write common variable record
295 static void write_common_rec(struct CommonRec *r)
297 char buf[4], *b;
299 r->segment >>= 1;
301 saa_wbytes(header, &r->type, 1);
302 saa_wbytes(header, &r->reclen, 1);
303 b = buf; WRITESHORT(b,r->segment);
304 saa_wbytes(header, buf, 2);
305 b = buf; WRITELONG(b, r->size);
306 saa_wbytes(header,buf,4);
307 b = buf; WRITESHORT(b, r->align);
308 saa_wbytes(header, buf, 2);
309 saa_wbytes(header, r->label, strlen(r->label) + 1);
310 headerlength += r->reclen + 2;
314 * Write library record
316 static void write_dll_rec(struct DLLRec *r)
318 saa_wbytes(header,&r->type,1);
319 saa_wbytes(header,&r->reclen,1);
320 saa_wbytes(header, r->libname, strlen(r->libname) + 1);
321 headerlength += r->reclen + 2;
326 * Write module name record
328 static void write_modname_rec(struct ModRec *r)
330 saa_wbytes(header,&r->type,1);
331 saa_wbytes(header,&r->reclen,1);
332 saa_wbytes(header, r->modname, strlen(r->modname) + 1);
333 headerlength += r->reclen + 2;
338 * Handle export, import and common records.
340 static void rdf2_deflabel(char *name, long segment, long offset,
341 int is_global, char *special)
343 struct ExportRec r;
344 struct ImportRec ri;
345 struct CommonRec ci;
346 static int farsym = 0;
347 static int i;
348 byte symflags = 0;
349 int len;
351 /* Check if the label length is OK */
352 if ((len = strlen(name)) >= EXIM_LABEL_MAX) {
353 error(ERR_NONFATAL, "label size exceeds %d bytes", EXIM_LABEL_MAX);
354 return;
356 if (!len) {
357 error(ERR_NONFATAL, "zero-length label");
358 return;
361 if (is_global == 2) {
362 /* Common variable */
363 ci.type = RDFREC_COMMON;
364 ci.size = offset;
365 ci.segment = segment;
366 strcpy(ci.label, name);
367 ci.reclen = 9 + len;
368 ci.align = 0;
371 * Check the special text to see if it's a valid number and power
372 * of two; if so, store it as the alignment for the common variable.
374 if (special) {
375 int err;
376 ci.align = readnum(special, &err);
377 if (err)
378 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
379 " valid number", special);
380 else if ( (ci.align | (ci.align-1)) != 2*ci.align - 1)
381 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
382 " power of two", special);
384 write_common_rec(&ci);
387 /* We don't care about local labels or fix-up hints */
388 if (is_global != 1) return;
390 if (special) {
391 while(*special == ' ' || *special == '\t') special++;
393 if (!nasm_strnicmp(special, "export", 6)) {
394 special += 6;
395 symflags |= SYM_GLOBAL;
396 } else if (!nasm_strnicmp(special, "import", 6)) {
397 special += 6;
398 symflags |= SYM_IMPORT;
401 if (*special) {
402 while (isspace(*special)) special++;
403 if (!nasm_stricmp(special, "far")) {
404 farsym = 1;
405 } else if (!nasm_stricmp(special, "near")) {
406 farsym = 0;
407 } else if (!nasm_stricmp(special, "proc") ||
408 !nasm_stricmp(special, "function")) {
409 symflags |= SYM_FUNCTION;
410 } else if (!nasm_stricmp(special, "data") ||
411 !nasm_stricmp(special, "object")) {
412 symflags |= SYM_DATA;
413 } else
414 error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
418 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
419 error (ERR_NONFATAL, "unrecognised special symbol `%s'", name);
420 return;
423 for (i = 0; i < nsegments; i++) {
424 if (segments[i].segnumber == segment>>1) break;
427 if (i >= nsegments) { /* EXTERN declaration */
428 ri.type = farsym ? RDFREC_FARIMPORT : RDFREC_IMPORT;
429 if (symflags & SYM_GLOBAL)
430 error(ERR_NONFATAL, "symbol type conflict - EXTERN cannot be EXPORT");
431 ri.flags = symflags;
432 ri.segment = segment;
433 strcpy(ri.label, name);
434 ri.reclen = 4 + len;
435 write_import_rec(&ri);
436 } else if (is_global) {
437 r.type = RDFREC_GLOBAL; /* GLOBAL declaration */
438 if (symflags & SYM_IMPORT)
439 error(ERR_NONFATAL, "symbol type conflict - GLOBAL cannot be IMPORT");
440 r.flags = symflags;
441 r.segment = segment;
442 r.offset = offset;
443 strcpy(r.label, name);
444 r.reclen = 7 + len;
445 write_export_rec(&r);
449 static void membufwrite(int segment, const void * data, int bytes)
451 int i;
452 char buf[4], * b;
454 for (i = 0; i < nsegments; i++) {
455 if (segments[i].segnumber == segment) break;
457 if (i == nsegments)
458 error(ERR_PANIC, "can't find segment %d", segment);
460 if (bytes < 0) {
461 b = buf;
462 if (bytes == -2)
463 WRITESHORT(b,*(short *)data);
464 else
465 WRITELONG(b,*(long *)data);
466 data = buf;
467 bytes = -bytes;
469 segments[i].seglength += bytes;
470 saa_wbytes(seg[i],data,bytes);
473 static int getsegmentlength(int segment)
475 int i;
476 for (i = 0; i < nsegments; i++) {
477 if (segments[i].segnumber == segment) break;
479 if (i == nsegments)
480 error(ERR_PANIC, "can't find segment %d", segment);
482 return segments[i].seglength;
485 static void rdf2_out (long segto, const void *data, unsigned long type,
486 long segment, long wrt)
488 long bytes = type & OUT_SIZMASK;
489 struct RelocRec rr;
490 unsigned char databuf[4],*pd;
491 int seg;
493 if (segto == NO_SEG) {
494 if ((type & OUT_TYPMASK) != OUT_RESERVE)
495 error (ERR_NONFATAL, "attempt to assemble code in ABSOLUTE space");
496 return;
499 segto >>= 1; /* convert NASM segment no to RDF number */
501 for (seg = 0; seg < nsegments; seg++) {
502 if (segments[seg].segnumber == segto) break;
504 if (seg >= nsegments) {
505 error(ERR_NONFATAL,"specified segment not supported by rdf output format");
506 return;
509 if (wrt != NO_SEG) {
510 wrt = NO_SEG; /* continue to do _something_ */
511 error (ERR_NONFATAL, "WRT not supported by rdf output format");
514 type &= OUT_TYPMASK;
516 if (segto == 2 && type != OUT_RESERVE)
518 error(ERR_NONFATAL, "BSS segments may not be initialised");
520 /* just reserve the space for now... */
522 if (type == OUT_REL2ADR)
523 bytes = 2;
524 else
525 bytes = 4;
526 type = OUT_RESERVE;
529 if (type == OUT_RESERVE) {
530 if (segto == 2) /* BSS segment space reserverd */
531 bsslength += bytes;
532 else
533 while (bytes --)
534 membufwrite(segto,databuf,1);
536 else if (type == OUT_RAWDATA) {
537 if (segment != NO_SEG)
538 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
540 membufwrite(segto,data,bytes);
542 else if (type == OUT_ADDRESS) {
544 /* if segment == NO_SEG then we are writing an address of an
545 object within the same segment - do not produce reloc rec. */
547 /* FIXME - is this behaviour sane? at first glance it doesn't
548 appear to be. Must test this thoroughly...! */
550 if (segment != NO_SEG)
552 /* it's an address, so we must write a relocation record */
554 rr.type = RDFREC_RELOC; /* type signature */
555 rr.reclen = 8;
556 rr.segment = segto; /* segment we're currently in */
557 rr.offset = getsegmentlength(segto); /* current offset */
558 rr.length = bytes; /* length of reference */
559 rr.refseg = segment; /* segment referred to */
560 write_reloc_rec(&rr);
563 pd = databuf; /* convert address to little-endian */
564 if (bytes == 2)
565 WRITESHORT (pd, *(long *)data);
566 else
567 WRITELONG (pd, *(long *)data);
569 membufwrite(segto,databuf,bytes);
572 else if (type == OUT_REL2ADR)
574 if (segment == segto)
575 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
577 rr.reclen = 8;
578 rr.offset = getsegmentlength(segto); /* current offset */
579 rr.length = 2; /* length of reference */
580 rr.refseg = segment; /* segment referred to (will be >>1'd)*/
582 if (segment != NO_SEG && segment % 2) {
583 rr.type = RDFREC_SEGRELOC;
584 rr.segment = segto; /* memory base refs *aren't ever* relative! */
585 write_reloc_rec(&rr);
587 /* what do we put in the code? Simply the data. This should almost
588 * always be zero, unless someone's doing segment arithmetic...
590 rr.offset = *(long *) data;
592 else
594 rr.type = RDFREC_RELOC; /* type signature */
595 rr.segment = segto+64; /* segment we're currently in + rel flag */
596 write_reloc_rec(&rr);
598 /* work out what to put in the code: offset of the end of this operand,
599 * subtracted from any data specified, so that loader can just add
600 * address of imported symbol onto it to get address relative to end of
601 * instruction: import_address + data(offset) - end_of_instrn */
603 rr.offset = *(long *)data -(rr.offset + bytes);
606 membufwrite(segto,&rr.offset,-2);
608 else if (type == OUT_REL4ADR)
610 if (segment == segto)
611 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
612 if (segment != NO_SEG && segment % 2) {
613 error(ERR_PANIC, "erm... 4 byte segment base ref?");
616 rr.type = RDFREC_RELOC; /* type signature */
617 rr.segment = segto+64; /* segment we're currently in + rel tag */
618 rr.offset = getsegmentlength(segto); /* current offset */
619 rr.length = 4; /* length of reference */
620 rr.refseg = segment; /* segment referred to */
621 rr.reclen = 8;
622 write_reloc_rec(&rr);
624 rr.offset = *(long *)data -(rr.offset + bytes);
626 membufwrite(segto,&rr.offset,-4);
630 static void rdf2_cleanup (int debuginfo) {
631 long l;
632 struct BSSRec bs;
633 int i;
635 (void) debuginfo;
637 /* should write imported & exported symbol declarations to header here */
639 /* generate the output file... */
640 fwrite(RDOFF2Id,6,1,ofile); /* file type magic number */
642 if (bsslength != 0) { /* reserve BSS */
643 bs.type = RDFREC_BSS;
644 bs.amount = bsslength;
645 bs.reclen = 4;
646 write_bss_rec(&bs);
650 * calculate overall length of the output object
652 l = headerlength + 4;
654 for (i = 0; i < nsegments; i++) {
655 if (i == 2) continue; /* skip BSS segment */
656 l += 10 + segments[i].seglength;
658 l += 10; /* null segment */
660 fwritelong(l, ofile);
662 fwritelong(headerlength, ofile);
663 saa_fpwrite(header,ofile); /* dump header */
664 saa_free(header);
666 for (i = 0; i < nsegments; i++) {
667 if (i == 2) continue;
669 fwriteshort(segments[i].segtype, ofile);
670 fwriteshort(segments[i].segnumber, ofile);
671 fwriteshort(segments[i].segreserved, ofile);
672 fwritelong(segments[i].seglength, ofile);
674 saa_fpwrite(seg[i], ofile);
675 saa_free(seg[i]);
678 /* null segment - write 10 bytes of zero */
679 fwritelong(0,ofile);
680 fwritelong(0,ofile);
681 fwriteshort(0,ofile);
683 fclose(ofile);
686 static long rdf2_segbase (long segment) {
687 return segment;
692 * Handle RDOFF2 specific directives
694 static int rdf2_directive (char *directive, char *value, int pass)
696 int n;
698 /* Check if the name length is OK */
699 if ((n = strlen(value)) >= MODLIB_NAME_MAX) {
700 error(ERR_NONFATAL, "name size exceeds %d bytes", MODLIB_NAME_MAX);
701 return 0;
704 if (! strcmp(directive, "library")) {
705 if (pass == 1) {
706 struct DLLRec r;
707 r.type = RDFREC_DLL;
708 r.reclen = n+1;
709 strcpy(r.libname, value);
710 write_dll_rec(&r);
712 return 1;
715 if (! strcmp(directive, "module")) {
716 if (pass == 1) {
717 struct ModRec r;
718 r.type = RDFREC_MODNAME;
719 r.reclen = n+1;
720 strcpy(r.modname, value);
721 write_modname_rec(&r);
723 return 1;
726 return 0;
729 static void rdf2_filename (char *inname, char *outname, efunc error) {
730 standard_extension(inname,outname,".rdf",error);
733 static const char *rdf2_stdmac[] = {
734 "%define __SECT__ [section .text]",
735 "%imacro library 1+.nolist",
736 "[library %1]",
737 "%endmacro",
738 "%imacro module 1+.nolist",
739 "[module %1]",
740 "%endmacro",
741 "%macro __NASM_CDecl__ 1",
742 "%endmacro",
743 NULL
746 static int rdf2_set_info(enum geninfo type, char **val)
748 return 0;
752 struct ofmt of_rdf2 = {
753 "Relocatable Dynamic Object File Format v2.0",
754 "rdf",
755 NULL,
756 null_debug_arr,
757 &null_debug_form,
758 rdf2_stdmac,
759 rdf2_init,
760 rdf2_set_info,
761 rdf2_out,
762 rdf2_deflabel,
763 rdf2_section_names,
764 rdf2_segbase,
765 rdf2_directive,
766 rdf2_filename,
767 rdf2_cleanup
770 #endif /* OF_RDF2 */