Apply Nindent to all .c and .h files
[nasm/avx512.git] / output / outrdf2.c
blob0baca45a31a61ec4c91a2cba2a0bb1c00569d31f
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;
34 /* Note that whenever a segment is referred to in the RDOFF file, its number
35 * is always half of the segment number that NASM uses to refer to it; this
36 * is because NASM only allocates even numbered segments, so as to not
37 * waste any of the 16 bits of segment number written to the file - this
38 * allows up to 65533 external labels to be defined; otherwise it would be
39 * 32764. */
41 #define COUNT_SEGTYPES 9
43 static char *segmenttypes[COUNT_SEGTYPES] = {
44 "null", "text", "code", "data",
45 "comment", "lcomment", "pcomment",
46 "symdebug", "linedebug"
49 static int segmenttypenumbers[COUNT_SEGTYPES] = {
50 0, 1, 1, 2, 3, 4, 5, 6, 7
53 /* code for managing buffers needed to separate code and data into individual
54 * sections until they are ready to be written to the file.
55 * We'd better hope that it all fits in memory else we're buggered... */
57 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
58 * on 80x86 machines for efficiency */
60 /***********************************************************************
61 * Actual code to deal with RDOFF2 ouput format begins here...
64 /* global variables set during the initialisation phase */
66 static struct SAA *seg[RDF_MAXSEGS]; /* seg 0 = code, seg 1 = data */
67 static struct SAA *header; /* relocation/import/export records */
69 static FILE *ofile;
71 static efunc error;
73 static struct seginfo {
74 char *segname;
75 int segnumber;
76 uint16 segtype;
77 uint16 segreserved;
78 long seglength;
79 } segments[RDF_MAXSEGS];
81 static int nsegments;
83 static long bsslength;
84 static long headerlength;
86 static void rdf2_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
88 int segtext, segdata, segbss;
90 /* set up the initial segments */
91 segments[0].segname = ".text";
92 segments[0].segnumber = 0;
93 segments[0].segtype = 1;
94 segments[0].segreserved = 0;
95 segments[0].seglength = 0;
97 segments[1].segname = ".data";
98 segments[1].segnumber = 1;
99 segments[1].segtype = 2;
100 segments[1].segreserved = 0;
101 segments[1].seglength = 0;
103 segments[2].segname = ".bss";
104 segments[2].segnumber = 2;
105 segments[2].segtype = 0xFFFF; /* reserved - should never be produced */
106 segments[2].segreserved = 0;
107 segments[2].seglength = 0;
109 nsegments = 3;
111 ofile = fp;
112 error = errfunc;
114 seg[0] = saa_init(1L);
115 seg[1] = saa_init(1L);
116 seg[2] = NULL; /* special case! */
118 header = saa_init(1L);
120 segtext = seg_alloc();
121 segdata = seg_alloc();
122 segbss = seg_alloc();
123 if (segtext != 0 || segdata != 2 || segbss != 4)
124 error(ERR_PANIC,
125 "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))
149 p++;
150 if (*p) { /* we're now in whitespace */
151 *p++ = '\0';
152 while (*p && isspace(80))
153 *p++ = '\0';
155 if (*p) { /* we're now in an attribute value */
157 * see if we have an optional ',number' following the type code
159 if ((q = strchr(p, ','))) {
160 *q++ = '\0';
162 reserved = readnum(q, &i);
163 if (i) {
164 error(ERR_NONFATAL,
165 "value following comma must be numeric");
166 reserved = 0;
170 * check it against the text strings in segmenttypes
173 for (i = 0; i < COUNT_SEGTYPES; i++)
174 if (!nasm_stricmp(p, segmenttypes[i])) {
175 code = segmenttypenumbers[i];
176 break;
178 if (code == -1) { /* didn't find anything */
179 code = readnum(p, &i);
180 if (i) {
181 error(ERR_NONFATAL, "unrecognised RDF segment type (%s)",
183 code = 3;
187 for (i = 0; i < nsegments; i++) {
188 if (!strcmp(name, segments[i].segname)) {
189 if (code != -1 || reserved != 0)
190 error(ERR_NONFATAL, "segment attributes specified on"
191 " redeclaration of segment");
192 return segments[i].segnumber * 2;
196 /* declaring a new segment! */
198 if (code == -1) {
199 error(ERR_NONFATAL, "new segment declared without type code");
200 code = 3;
202 if (nsegments == RDF_MAXSEGS) {
203 error(ERR_FATAL, "reached compiled-in maximum segment limit (%d)",
204 RDF_MAXSEGS);
205 return NO_SEG;
208 segments[nsegments].segname = nasm_strdup(name);
209 i = seg_alloc();
210 if (i % 2 != 0)
211 error(ERR_PANIC, "seg_alloc() returned odd number");
212 segments[nsegments].segnumber = i >> 1;
213 segments[nsegments].segtype = code;
214 segments[nsegments].segreserved = reserved;
215 segments[nsegments].seglength = 0;
217 seg[nsegments] = saa_init(1L);
219 return i;
223 * Write relocation record
225 static void write_reloc_rec(struct RelocRec *r)
227 char buf[4], *b;
229 if (r->refseg != (uint16) NO_SEG && (r->refseg & 1)) /* segment base ref */
230 r->type = RDFREC_SEGRELOC;
232 r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
234 saa_wbytes(header, &r->type, 1);
235 saa_wbytes(header, &r->reclen, 1);
236 saa_wbytes(header, &r->segment, 1);
237 b = buf;
238 WRITELONG(b, r->offset);
239 saa_wbytes(header, buf, 4);
240 saa_wbytes(header, &r->length, 1);
241 b = buf;
242 WRITESHORT(b, r->refseg);
243 saa_wbytes(header, buf, 2);
244 headerlength += r->reclen + 2;
248 * Write export record
250 static void write_export_rec(struct ExportRec *r)
252 char buf[4], *b;
254 r->segment >>= 1;
256 saa_wbytes(header, &r->type, 1);
257 saa_wbytes(header, &r->reclen, 1);
258 saa_wbytes(header, &r->flags, 1);
259 saa_wbytes(header, &r->segment, 1);
260 b = buf;
261 WRITELONG(b, r->offset);
262 saa_wbytes(header, buf, 4);
263 saa_wbytes(header, r->label, strlen(r->label) + 1);
264 headerlength += r->reclen + 2;
267 static void write_import_rec(struct ImportRec *r)
269 char buf[4], *b;
271 r->segment >>= 1;
273 saa_wbytes(header, &r->type, 1);
274 saa_wbytes(header, &r->reclen, 1);
275 saa_wbytes(header, &r->flags, 1);
276 b = buf;
277 WRITESHORT(b, r->segment);
278 saa_wbytes(header, buf, 2);
279 saa_wbytes(header, r->label, strlen(r->label) + 1);
280 headerlength += r->reclen + 2;
284 * Write BSS record
286 static void write_bss_rec(struct BSSRec *r)
288 char buf[4], *b;
290 saa_wbytes(header, &r->type, 1);
291 saa_wbytes(header, &r->reclen, 1);
292 b = buf;
293 WRITELONG(b, r->amount);
294 saa_wbytes(header, buf, 4);
295 headerlength += r->reclen + 2;
299 * Write common variable record
301 static void write_common_rec(struct CommonRec *r)
303 char buf[4], *b;
305 r->segment >>= 1;
307 saa_wbytes(header, &r->type, 1);
308 saa_wbytes(header, &r->reclen, 1);
309 b = buf;
310 WRITESHORT(b, r->segment);
311 saa_wbytes(header, buf, 2);
312 b = buf;
313 WRITELONG(b, r->size);
314 saa_wbytes(header, buf, 4);
315 b = buf;
316 WRITESHORT(b, r->align);
317 saa_wbytes(header, buf, 2);
318 saa_wbytes(header, r->label, strlen(r->label) + 1);
319 headerlength += r->reclen + 2;
323 * Write library record
325 static void write_dll_rec(struct DLLRec *r)
327 saa_wbytes(header, &r->type, 1);
328 saa_wbytes(header, &r->reclen, 1);
329 saa_wbytes(header, r->libname, strlen(r->libname) + 1);
330 headerlength += r->reclen + 2;
334 * Write module name record
336 static void write_modname_rec(struct ModRec *r)
338 saa_wbytes(header, &r->type, 1);
339 saa_wbytes(header, &r->reclen, 1);
340 saa_wbytes(header, r->modname, strlen(r->modname) + 1);
341 headerlength += r->reclen + 2;
345 * Handle export, import and common records.
347 static void rdf2_deflabel(char *name, long segment, long offset,
348 int is_global, char *special)
350 struct ExportRec r;
351 struct ImportRec ri;
352 struct CommonRec ci;
353 static int farsym = 0;
354 static int i;
355 byte symflags = 0;
356 int len;
358 /* Check if the label length is OK */
359 if ((len = strlen(name)) >= EXIM_LABEL_MAX) {
360 error(ERR_NONFATAL, "label size exceeds %d bytes", EXIM_LABEL_MAX);
361 return;
363 if (!len) {
364 error(ERR_NONFATAL, "zero-length label");
365 return;
368 if (is_global == 2) {
369 /* Common variable */
370 ci.type = RDFREC_COMMON;
371 ci.size = offset;
372 ci.segment = segment;
373 strcpy(ci.label, name);
374 ci.reclen = 9 + len;
375 ci.align = 0;
378 * Check the special text to see if it's a valid number and power
379 * of two; if so, store it as the alignment for the common variable.
381 if (special) {
382 int err;
383 ci.align = readnum(special, &err);
384 if (err)
385 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
386 " valid number", special);
387 else if ((ci.align | (ci.align - 1)) != 2 * ci.align - 1)
388 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
389 " power of two", special);
391 write_common_rec(&ci);
394 /* We don't care about local labels or fix-up hints */
395 if (is_global != 1)
396 return;
398 if (special) {
399 while (*special == ' ' || *special == '\t')
400 special++;
402 if (!nasm_strnicmp(special, "export", 6)) {
403 special += 6;
404 symflags |= SYM_GLOBAL;
405 } else if (!nasm_strnicmp(special, "import", 6)) {
406 special += 6;
407 symflags |= SYM_IMPORT;
410 if (*special) {
411 while (isspace(*special))
412 special++;
413 if (!nasm_stricmp(special, "far")) {
414 farsym = 1;
415 } else if (!nasm_stricmp(special, "near")) {
416 farsym = 0;
417 } else if (!nasm_stricmp(special, "proc") ||
418 !nasm_stricmp(special, "function")) {
419 symflags |= SYM_FUNCTION;
420 } else if (!nasm_stricmp(special, "data") ||
421 !nasm_stricmp(special, "object")) {
422 symflags |= SYM_DATA;
423 } else
424 error(ERR_NONFATAL, "unrecognised symbol type `%s'",
425 special);
429 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
430 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
431 return;
434 for (i = 0; i < nsegments; i++) {
435 if (segments[i].segnumber == segment >> 1)
436 break;
439 if (i >= nsegments) { /* EXTERN declaration */
440 ri.type = farsym ? RDFREC_FARIMPORT : RDFREC_IMPORT;
441 if (symflags & SYM_GLOBAL)
442 error(ERR_NONFATAL,
443 "symbol type conflict - EXTERN cannot be EXPORT");
444 ri.flags = symflags;
445 ri.segment = segment;
446 strcpy(ri.label, name);
447 ri.reclen = 4 + len;
448 write_import_rec(&ri);
449 } else if (is_global) {
450 r.type = RDFREC_GLOBAL; /* GLOBAL declaration */
451 if (symflags & SYM_IMPORT)
452 error(ERR_NONFATAL,
453 "symbol type conflict - GLOBAL cannot be IMPORT");
454 r.flags = symflags;
455 r.segment = segment;
456 r.offset = offset;
457 strcpy(r.label, name);
458 r.reclen = 7 + len;
459 write_export_rec(&r);
463 static void membufwrite(int segment, const void *data, int bytes)
465 int i;
466 char buf[4], *b;
468 for (i = 0; i < nsegments; i++) {
469 if (segments[i].segnumber == segment)
470 break;
472 if (i == nsegments)
473 error(ERR_PANIC, "can't find segment %d", segment);
475 if (bytes < 0) {
476 b = buf;
477 if (bytes == -2)
478 WRITESHORT(b, *(short *)data);
479 else
480 WRITELONG(b, *(long *)data);
481 data = buf;
482 bytes = -bytes;
484 segments[i].seglength += bytes;
485 saa_wbytes(seg[i], data, bytes);
488 static int getsegmentlength(int segment)
490 int i;
491 for (i = 0; i < nsegments; i++) {
492 if (segments[i].segnumber == segment)
493 break;
495 if (i == nsegments)
496 error(ERR_PANIC, "can't find segment %d", segment);
498 return segments[i].seglength;
501 static void rdf2_out(long segto, const void *data, unsigned long type,
502 long segment, long wrt)
504 long bytes = type & OUT_SIZMASK;
505 struct RelocRec rr;
506 unsigned char databuf[4], *pd;
507 int seg;
509 if (segto == NO_SEG) {
510 if ((type & OUT_TYPMASK) != OUT_RESERVE)
511 error(ERR_NONFATAL,
512 "attempt to assemble code in ABSOLUTE space");
513 return;
516 segto >>= 1; /* convert NASM segment no to RDF number */
518 for (seg = 0; seg < nsegments; seg++) {
519 if (segments[seg].segnumber == segto)
520 break;
522 if (seg >= nsegments) {
523 error(ERR_NONFATAL,
524 "specified segment not supported by rdf output format");
525 return;
528 if (wrt != NO_SEG) {
529 wrt = NO_SEG; /* continue to do _something_ */
530 error(ERR_NONFATAL, "WRT not supported by rdf output format");
533 type &= OUT_TYPMASK;
535 if (segto == 2 && type != OUT_RESERVE) {
536 error(ERR_NONFATAL, "BSS segments may not be initialised");
538 /* just reserve the space for now... */
540 if (type == OUT_REL2ADR)
541 bytes = 2;
542 else
543 bytes = 4;
544 type = OUT_RESERVE;
547 if (type == OUT_RESERVE) {
548 if (segto == 2) /* BSS segment space reserverd */
549 bsslength += bytes;
550 else
551 while (bytes--)
552 membufwrite(segto, databuf, 1);
553 } else if (type == OUT_RAWDATA) {
554 if (segment != NO_SEG)
555 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
557 membufwrite(segto, data, bytes);
558 } else if (type == OUT_ADDRESS) {
560 /* if segment == NO_SEG then we are writing an address of an
561 object within the same segment - do not produce reloc rec. */
563 /* FIXME - is this behaviour sane? at first glance it doesn't
564 appear to be. Must test this thoroughly...! */
566 if (segment != NO_SEG) {
567 /* it's an address, so we must write a relocation record */
569 rr.type = RDFREC_RELOC; /* type signature */
570 rr.reclen = 8;
571 rr.segment = segto; /* segment we're currently in */
572 rr.offset = getsegmentlength(segto); /* current offset */
573 rr.length = bytes; /* length of reference */
574 rr.refseg = segment; /* segment referred to */
575 write_reloc_rec(&rr);
578 pd = databuf; /* convert address to little-endian */
579 if (bytes == 2)
580 WRITESHORT(pd, *(long *)data);
581 else
582 WRITELONG(pd, *(long *)data);
584 membufwrite(segto, databuf, bytes);
586 } else if (type == OUT_REL2ADR) {
587 if (segment == segto)
588 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
590 rr.reclen = 8;
591 rr.offset = getsegmentlength(segto); /* current offset */
592 rr.length = 2; /* length of reference */
593 rr.refseg = segment; /* segment referred to (will be >>1'd) */
595 if (segment != NO_SEG && segment % 2) {
596 rr.type = RDFREC_SEGRELOC;
597 rr.segment = segto; /* memory base refs *aren't ever* relative! */
598 write_reloc_rec(&rr);
600 /* what do we put in the code? Simply the data. This should almost
601 * always be zero, unless someone's doing segment arithmetic...
603 rr.offset = *(long *)data;
604 } else {
605 rr.type = RDFREC_RELOC; /* type signature */
606 rr.segment = segto + 64; /* segment we're currently in + rel flag */
607 write_reloc_rec(&rr);
609 /* work out what to put in the code: offset of the end of this operand,
610 * subtracted from any data specified, so that loader can just add
611 * address of imported symbol onto it to get address relative to end of
612 * instruction: import_address + data(offset) - end_of_instrn */
614 rr.offset = *(long *)data - (rr.offset + bytes);
617 membufwrite(segto, &rr.offset, -2);
618 } else if (type == OUT_REL4ADR) {
619 if (segment == segto)
620 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
621 if (segment != NO_SEG && segment % 2) {
622 error(ERR_PANIC, "erm... 4 byte segment base ref?");
625 rr.type = RDFREC_RELOC; /* type signature */
626 rr.segment = segto + 64; /* segment we're currently in + rel tag */
627 rr.offset = getsegmentlength(segto); /* current offset */
628 rr.length = 4; /* length of reference */
629 rr.refseg = segment; /* segment referred to */
630 rr.reclen = 8;
631 write_reloc_rec(&rr);
633 rr.offset = *(long *)data - (rr.offset + bytes);
635 membufwrite(segto, &rr.offset, -4);
639 static void rdf2_cleanup(int debuginfo)
641 long l;
642 struct BSSRec bs;
643 int i;
645 (void)debuginfo;
647 /* should write imported & exported symbol declarations to header here */
649 /* generate the output file... */
650 fwrite(RDOFF2Id, 6, 1, ofile); /* file type magic number */
652 if (bsslength != 0) { /* reserve BSS */
653 bs.type = RDFREC_BSS;
654 bs.amount = bsslength;
655 bs.reclen = 4;
656 write_bss_rec(&bs);
660 * calculate overall length of the output object
662 l = headerlength + 4;
664 for (i = 0; i < nsegments; i++) {
665 if (i == 2)
666 continue; /* skip BSS segment */
667 l += 10 + segments[i].seglength;
669 l += 10; /* null segment */
671 fwritelong(l, ofile);
673 fwritelong(headerlength, ofile);
674 saa_fpwrite(header, ofile); /* dump header */
675 saa_free(header);
677 for (i = 0; i < nsegments; i++) {
678 if (i == 2)
679 continue;
681 fwriteshort(segments[i].segtype, ofile);
682 fwriteshort(segments[i].segnumber, ofile);
683 fwriteshort(segments[i].segreserved, ofile);
684 fwritelong(segments[i].seglength, ofile);
686 saa_fpwrite(seg[i], ofile);
687 saa_free(seg[i]);
690 /* null segment - write 10 bytes of zero */
691 fwritelong(0, ofile);
692 fwritelong(0, ofile);
693 fwriteshort(0, ofile);
695 fclose(ofile);
698 static long rdf2_segbase(long segment)
700 return segment;
704 * Handle RDOFF2 specific directives
706 static int rdf2_directive(char *directive, char *value, int pass)
708 int n;
710 /* Check if the name length is OK */
711 if ((n = strlen(value)) >= MODLIB_NAME_MAX) {
712 error(ERR_NONFATAL, "name size exceeds %d bytes", MODLIB_NAME_MAX);
713 return 0;
716 if (!strcmp(directive, "library")) {
717 if (pass == 1) {
718 struct DLLRec r;
719 r.type = RDFREC_DLL;
720 r.reclen = n + 1;
721 strcpy(r.libname, value);
722 write_dll_rec(&r);
724 return 1;
727 if (!strcmp(directive, "module")) {
728 if (pass == 1) {
729 struct ModRec r;
730 r.type = RDFREC_MODNAME;
731 r.reclen = n + 1;
732 strcpy(r.modname, value);
733 write_modname_rec(&r);
735 return 1;
738 return 0;
741 static void rdf2_filename(char *inname, char *outname, efunc error)
743 standard_extension(inname, outname, ".rdf", error);
746 static const char *rdf2_stdmac[] = {
747 "%define __SECT__ [section .text]",
748 "%imacro library 1+.nolist",
749 "[library %1]",
750 "%endmacro",
751 "%imacro module 1+.nolist",
752 "[module %1]",
753 "%endmacro",
754 "%macro __NASM_CDecl__ 1",
755 "%endmacro",
756 NULL
759 static int rdf2_set_info(enum geninfo type, char **val)
761 return 0;
764 struct ofmt of_rdf2 = {
765 "Relocatable Dynamic Object File Format v2.0",
766 "rdf",
767 NULL,
768 null_debug_arr,
769 &null_debug_form,
770 rdf2_stdmac,
771 rdf2_init,
772 rdf2_set_info,
773 rdf2_out,
774 rdf2_deflabel,
775 rdf2_section_names,
776 rdf2_segbase,
777 rdf2_directive,
778 rdf2_filename,
779 rdf2_cleanup
782 #endif /* OF_RDF2 */