Allow global declaration after symbol definition
[nasm/avx512.git] / output / outrdf2.c
blobb502715b7994bda02343207b59f9bb00aa6df48b
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 "saa.h"
24 #include "outform.h"
26 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
27 #define VERBOSE_WARNINGS
29 #ifdef OF_RDF2
31 #include "rdoff/rdoff.h"
33 /* This signature is written to start of RDOFF files */
34 static const char *RDOFF2Id = RDOFF2_SIGNATURE;
36 /* Note that whenever a segment is referred to in the RDOFF file, its number
37 * is always half of the segment number that NASM uses to refer to it; this
38 * is because NASM only allocates even numbered segments, so as to not
39 * waste any of the 16 bits of segment number written to the file - this
40 * allows up to 65533 external labels to be defined; otherwise it would be
41 * 32764. */
43 #define COUNT_SEGTYPES 9
45 static char *segmenttypes[COUNT_SEGTYPES] = {
46 "null", "text", "code", "data",
47 "comment", "lcomment", "pcomment",
48 "symdebug", "linedebug"
51 static int segmenttypenumbers[COUNT_SEGTYPES] = {
52 0, 1, 1, 2, 3, 4, 5, 6, 7
55 /* code for managing buffers needed to separate code and data into individual
56 * sections until they are ready to be written to the file.
57 * We'd better hope that it all fits in memory else we're buggered... */
59 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
60 * on 80x86 machines for efficiency */
62 /***********************************************************************
63 * Actual code to deal with RDOFF2 ouput format begins here...
66 /* global variables set during the initialisation phase */
68 static struct SAA *seg[RDF_MAXSEGS]; /* seg 0 = code, seg 1 = data */
69 static struct SAA *header; /* relocation/import/export records */
71 static FILE *ofile;
73 static efunc error;
75 static struct seginfo {
76 char *segname;
77 int segnumber;
78 uint16_t segtype;
79 uint16_t segreserved;
80 int32_t seglength;
81 } segments[RDF_MAXSEGS];
83 static int nsegments;
85 static int32_t bsslength;
86 static int32_t headerlength;
88 static void rdf2_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
90 int segtext, segdata, segbss;
92 (void)ldef;
93 (void)eval;
95 maxbits = 64;
97 /* set up the initial segments */
98 segments[0].segname = ".text";
99 segments[0].segnumber = 0;
100 segments[0].segtype = 1;
101 segments[0].segreserved = 0;
102 segments[0].seglength = 0;
104 segments[1].segname = ".data";
105 segments[1].segnumber = 1;
106 segments[1].segtype = 2;
107 segments[1].segreserved = 0;
108 segments[1].seglength = 0;
110 segments[2].segname = ".bss";
111 segments[2].segnumber = 2;
112 segments[2].segtype = 0xFFFF; /* reserved - should never be produced */
113 segments[2].segreserved = 0;
114 segments[2].seglength = 0;
116 nsegments = 3;
118 ofile = fp;
119 error = errfunc;
121 seg[0] = saa_init(1L);
122 seg[1] = saa_init(1L);
123 seg[2] = NULL; /* special case! */
125 header = saa_init(1L);
127 segtext = seg_alloc();
128 segdata = seg_alloc();
129 segbss = seg_alloc();
130 if (segtext != 0 || segdata != 2 || segbss != 4)
131 error(ERR_PANIC,
132 "rdf segment numbers not allocated as expected (%d,%d,%d)",
133 segtext, segdata, segbss);
134 bsslength = 0;
135 headerlength = 0;
138 static int32_t rdf2_section_names(char *name, int pass, int *bits)
140 int i;
141 bool err;
142 char *p, *q;
143 int code = -1;
144 int reserved = 0;
146 (void)pass;
149 * Default is 32 bits, in the text segment.
151 if (!name) {
152 *bits = 32;
153 return 0;
156 /* look for segment type code following segment name */
157 p = name;
158 while (*p && !nasm_isspace(*p))
159 p++;
160 if (*p) { /* we're now in whitespace */
161 *p++ = '\0';
162 while (*p && nasm_isspace(80))
163 *p++ = '\0';
165 if (*p) { /* we're now in an attribute value */
167 * see if we have an optional ',number' following the type code
169 if ((q = strchr(p, ','))) {
170 *q++ = '\0';
172 reserved = readnum(q, &err);
173 if (err) {
174 error(ERR_NONFATAL,
175 "value following comma must be numeric");
176 reserved = 0;
180 * check it against the text strings in segmenttypes
183 for (i = 0; i < COUNT_SEGTYPES; i++)
184 if (!nasm_stricmp(p, segmenttypes[i])) {
185 code = segmenttypenumbers[i];
186 break;
188 if (code == -1) { /* didn't find anything */
189 code = readnum(p, &err);
190 if (err) {
191 error(ERR_NONFATAL, "unrecognised RDF segment type (%s)",
193 code = 3;
197 for (i = 0; i < nsegments; i++) {
198 if (!strcmp(name, segments[i].segname)) {
199 if (code != -1 || reserved != 0)
200 error(ERR_NONFATAL, "segment attributes specified on"
201 " redeclaration of segment");
202 return segments[i].segnumber * 2;
206 /* declaring a new segment! */
208 if (code == -1) {
209 error(ERR_NONFATAL, "new segment declared without type code");
210 code = 3;
212 if (nsegments == RDF_MAXSEGS) {
213 error(ERR_FATAL, "reached compiled-in maximum segment limit (%d)",
214 RDF_MAXSEGS);
215 return NO_SEG;
218 segments[nsegments].segname = nasm_strdup(name);
219 i = seg_alloc();
220 if (i % 2 != 0)
221 error(ERR_PANIC, "seg_alloc() returned odd number");
222 segments[nsegments].segnumber = i >> 1;
223 segments[nsegments].segtype = code;
224 segments[nsegments].segreserved = reserved;
225 segments[nsegments].seglength = 0;
227 seg[nsegments] = saa_init(1L);
229 return i;
233 * Write relocation record
235 static void write_reloc_rec(struct RelocRec *r)
237 char buf[4], *b;
239 if (r->refseg != (uint16_t) NO_SEG && (r->refseg & 1)) /* segment base ref */
240 r->type = RDFREC_SEGRELOC;
242 r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
244 saa_wbytes(header, &r->type, 1);
245 saa_wbytes(header, &r->reclen, 1);
246 saa_wbytes(header, &r->segment, 1);
247 b = buf;
248 WRITELONG(b, r->offset);
249 saa_wbytes(header, buf, 4);
250 saa_wbytes(header, &r->length, 1);
251 b = buf;
252 WRITESHORT(b, r->refseg);
253 saa_wbytes(header, buf, 2);
254 headerlength += r->reclen + 2;
258 * Write export record
260 static void write_export_rec(struct ExportRec *r)
262 char buf[4], *b;
264 r->segment >>= 1;
266 saa_wbytes(header, &r->type, 1);
267 saa_wbytes(header, &r->reclen, 1);
268 saa_wbytes(header, &r->flags, 1);
269 saa_wbytes(header, &r->segment, 1);
270 b = buf;
271 WRITELONG(b, r->offset);
272 saa_wbytes(header, buf, 4);
273 saa_wbytes(header, r->label, strlen(r->label) + 1);
274 headerlength += r->reclen + 2;
277 static void write_import_rec(struct ImportRec *r)
279 char buf[4], *b;
281 r->segment >>= 1;
283 saa_wbytes(header, &r->type, 1);
284 saa_wbytes(header, &r->reclen, 1);
285 saa_wbytes(header, &r->flags, 1);
286 b = buf;
287 WRITESHORT(b, r->segment);
288 saa_wbytes(header, buf, 2);
289 saa_wbytes(header, r->label, strlen(r->label) + 1);
290 headerlength += r->reclen + 2;
294 * Write BSS record
296 static void write_bss_rec(struct BSSRec *r)
298 char buf[4], *b;
300 saa_wbytes(header, &r->type, 1);
301 saa_wbytes(header, &r->reclen, 1);
302 b = buf;
303 WRITELONG(b, r->amount);
304 saa_wbytes(header, buf, 4);
305 headerlength += r->reclen + 2;
309 * Write common variable record
311 static void write_common_rec(struct CommonRec *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 b = buf;
320 WRITESHORT(b, r->segment);
321 saa_wbytes(header, buf, 2);
322 b = buf;
323 WRITELONG(b, r->size);
324 saa_wbytes(header, buf, 4);
325 b = buf;
326 WRITESHORT(b, r->align);
327 saa_wbytes(header, buf, 2);
328 saa_wbytes(header, r->label, strlen(r->label) + 1);
329 headerlength += r->reclen + 2;
333 * Write library record
335 static void write_dll_rec(struct DLLRec *r)
337 saa_wbytes(header, &r->type, 1);
338 saa_wbytes(header, &r->reclen, 1);
339 saa_wbytes(header, r->libname, strlen(r->libname) + 1);
340 headerlength += r->reclen + 2;
344 * Write module name record
346 static void write_modname_rec(struct ModRec *r)
348 saa_wbytes(header, &r->type, 1);
349 saa_wbytes(header, &r->reclen, 1);
350 saa_wbytes(header, r->modname, strlen(r->modname) + 1);
351 headerlength += r->reclen + 2;
355 * Handle export, import and common records.
357 static void rdf2_deflabel(char *name, int32_t segment, int64_t offset,
358 int is_global, char *special)
360 struct ExportRec r;
361 struct ImportRec ri;
362 struct CommonRec ci;
363 static int farsym = 0;
364 static int i;
365 char symflags = 0;
366 int len;
368 /* Check if the label length is OK */
369 if ((len = strlen(name)) >= EXIM_LABEL_MAX) {
370 error(ERR_NONFATAL, "label size exceeds %d bytes", EXIM_LABEL_MAX);
371 return;
373 if (!len) {
374 error(ERR_NONFATAL, "zero-length label");
375 return;
378 if (is_global == 2) {
379 /* Common variable */
380 ci.type = RDFREC_COMMON;
381 ci.size = offset;
382 ci.segment = segment;
383 strcpy(ci.label, name);
384 ci.reclen = 9 + len;
385 ci.align = 0;
388 * Check the special text to see if it's a valid number and power
389 * of two; if so, store it as the alignment for the common variable.
391 if (special) {
392 bool err;
393 ci.align = readnum(special, &err);
394 if (err)
395 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
396 " valid number", special);
397 else if ((ci.align | (ci.align - 1)) != 2 * ci.align - 1)
398 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
399 " power of two", special);
401 write_common_rec(&ci);
404 /* We don't care about local labels or fix-up hints */
405 if (is_global != 1)
406 return;
408 if (special) {
409 while (*special == ' ' || *special == '\t')
410 special++;
412 if (!nasm_strnicmp(special, "export", 6)) {
413 special += 6;
414 symflags |= SYM_GLOBAL;
415 } else if (!nasm_strnicmp(special, "import", 6)) {
416 special += 6;
417 symflags |= SYM_IMPORT;
420 if (*special) {
421 while (nasm_isspace(*special))
422 special++;
423 if (!nasm_stricmp(special, "far")) {
424 farsym = 1;
425 } else if (!nasm_stricmp(special, "near")) {
426 farsym = 0;
427 } else if (!nasm_stricmp(special, "proc") ||
428 !nasm_stricmp(special, "function")) {
429 symflags |= SYM_FUNCTION;
430 } else if (!nasm_stricmp(special, "data") ||
431 !nasm_stricmp(special, "object")) {
432 symflags |= SYM_DATA;
433 } else
434 error(ERR_NONFATAL, "unrecognised symbol type `%s'",
435 special);
439 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
440 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
441 return;
444 for (i = 0; i < nsegments; i++) {
445 if (segments[i].segnumber == segment >> 1)
446 break;
449 if (i >= nsegments) { /* EXTERN declaration */
450 ri.type = farsym ? RDFREC_FARIMPORT : RDFREC_IMPORT;
451 if (symflags & SYM_GLOBAL)
452 error(ERR_NONFATAL,
453 "symbol type conflict - EXTERN cannot be EXPORT");
454 ri.flags = symflags;
455 ri.segment = segment;
456 strcpy(ri.label, name);
457 ri.reclen = 4 + len;
458 write_import_rec(&ri);
459 } else if (is_global) {
460 r.type = RDFREC_GLOBAL; /* GLOBAL declaration */
461 if (symflags & SYM_IMPORT)
462 error(ERR_NONFATAL,
463 "symbol type conflict - GLOBAL cannot be IMPORT");
464 r.flags = symflags;
465 r.segment = segment;
466 r.offset = offset;
467 strcpy(r.label, name);
468 r.reclen = 7 + len;
469 write_export_rec(&r);
473 static void membufwrite(int segment, const void *data, int bytes)
475 int i;
476 char buf[4], *b;
478 for (i = 0; i < nsegments; i++) {
479 if (segments[i].segnumber == segment)
480 break;
482 if (i == nsegments)
483 error(ERR_PANIC, "can't find segment %d", segment);
485 if (bytes < 0) {
486 b = buf;
487 if (bytes == -2)
488 WRITESHORT(b, *(int16_t *)data);
489 else
490 WRITELONG(b, *(int32_t *)data);
491 data = buf;
492 bytes = -bytes;
494 segments[i].seglength += bytes;
495 saa_wbytes(seg[i], data, bytes);
498 static int getsegmentlength(int segment)
500 int i;
501 for (i = 0; i < nsegments; i++) {
502 if (segments[i].segnumber == segment)
503 break;
505 if (i == nsegments)
506 error(ERR_PANIC, "can't find segment %d", segment);
508 return segments[i].seglength;
511 static void rdf2_out(int32_t segto, const void *data,
512 enum out_type type, uint64_t size,
513 int32_t segment, int32_t wrt)
515 struct RelocRec rr;
516 uint8_t databuf[8], *pd;
517 int seg;
519 if (segto == NO_SEG) {
520 if (type != OUT_RESERVE)
521 error(ERR_NONFATAL,
522 "attempt to assemble code in ABSOLUTE space");
523 return;
526 segto >>= 1; /* convert NASM segment no to RDF number */
528 for (seg = 0; seg < nsegments; seg++) {
529 if (segments[seg].segnumber == segto)
530 break;
532 if (seg >= nsegments) {
533 error(ERR_NONFATAL,
534 "specified segment not supported by rdf output format");
535 return;
538 if (wrt != NO_SEG) {
539 wrt = NO_SEG; /* continue to do _something_ */
540 error(ERR_NONFATAL, "WRT not supported by rdf output format");
543 if (segto == 2 && type != OUT_RESERVE) {
544 error(ERR_NONFATAL, "BSS segments may not be initialized");
546 /* just reserve the space for now... */
548 if (type == OUT_REL2ADR)
549 size = 2;
550 else
551 size = 4;
552 type = OUT_RESERVE;
555 if (type == OUT_RESERVE) {
556 if (segto == 2) /* BSS segment space reserverd */
557 bsslength += size;
558 else
559 while (size--)
560 membufwrite(segto, databuf, 1);
561 } else if (type == OUT_RAWDATA) {
562 if (segment != NO_SEG)
563 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
565 membufwrite(segto, data, size);
566 } else if (type == OUT_ADDRESS) {
568 /* if segment == NO_SEG then we are writing an address of an
569 object within the same segment - do not produce reloc rec. */
571 /* FIXME - is this behaviour sane? at first glance it doesn't
572 appear to be. Must test this thoroughly...! */
574 if (segment != NO_SEG) {
575 /* it's an address, so we must write a relocation record */
577 rr.type = RDFREC_RELOC; /* type signature */
578 rr.reclen = 8;
579 rr.segment = segto; /* segment we're currently in */
580 rr.offset = getsegmentlength(segto); /* current offset */
581 rr.length = size; /* length of reference */
582 rr.refseg = segment; /* segment referred to */
583 write_reloc_rec(&rr);
586 pd = databuf; /* convert address to little-endian */
587 WRITEADDR(pd, *(int64_t *)data, size);
588 membufwrite(segto, databuf, size);
589 } else if (type == OUT_REL2ADR) {
590 if (segment == segto)
591 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
593 rr.reclen = 8;
594 rr.offset = getsegmentlength(segto); /* current offset */
595 rr.length = 2; /* length of reference */
596 rr.refseg = segment; /* segment referred to (will be >>1'd) */
598 if (segment != NO_SEG && segment % 2) {
599 rr.type = RDFREC_SEGRELOC;
600 rr.segment = segto; /* memory base refs *aren't ever* relative! */
601 write_reloc_rec(&rr);
603 /* what do we put in the code? Simply the data. This should almost
604 * always be zero, unless someone's doing segment arithmetic...
606 rr.offset = *(int64_t *)data;
607 } else {
608 rr.type = RDFREC_RELOC; /* type signature */
609 rr.segment = segto + 64; /* segment we're currently in + rel flag */
610 write_reloc_rec(&rr);
612 /* work out what to put in the code: offset of the end of this operand,
613 * subtracted from any data specified, so that loader can just add
614 * address of imported symbol onto it to get address relative to end of
615 * instruction: import_address + data(offset) - end_of_instrn */
617 rr.offset = *(int32_t *)data - (rr.offset + size);
620 membufwrite(segto, &rr.offset, -2);
621 } else if (type == OUT_REL4ADR) {
622 if ((segment == segto) && (globalbits != 64))
623 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
624 if (segment != NO_SEG && segment % 2) {
625 error(ERR_PANIC, "erm... 4 byte segment base ref?");
628 rr.type = RDFREC_RELOC; /* type signature */
629 rr.segment = segto + 64; /* segment we're currently in + rel tag */
630 rr.offset = getsegmentlength(segto); /* current offset */
631 rr.length = 4; /* length of reference */
632 rr.refseg = segment; /* segment referred to */
633 rr.reclen = 8;
634 write_reloc_rec(&rr);
636 rr.offset = *(int64_t *)data - (rr.offset + size);
638 membufwrite(segto, &rr.offset, -4);
642 static void rdf2_cleanup(int debuginfo)
644 int32_t l;
645 struct BSSRec bs;
646 int i;
648 (void)debuginfo;
650 /* should write imported & exported symbol declarations to header here */
652 /* generate the output file... */
653 fwrite(RDOFF2Id, 6, 1, ofile); /* file type magic number */
655 if (bsslength != 0) { /* reserve BSS */
656 bs.type = RDFREC_BSS;
657 bs.amount = bsslength;
658 bs.reclen = 4;
659 write_bss_rec(&bs);
663 * calculate overall length of the output object
665 l = headerlength + 4;
667 for (i = 0; i < nsegments; i++) {
668 if (i == 2)
669 continue; /* skip BSS segment */
670 l += 10 + segments[i].seglength;
672 l += 10; /* null segment */
674 fwriteint32_t(l, ofile);
676 fwriteint32_t(headerlength, ofile);
677 saa_fpwrite(header, ofile); /* dump header */
678 saa_free(header);
680 for (i = 0; i < nsegments; i++) {
681 if (i == 2)
682 continue;
684 fwriteint16_t(segments[i].segtype, ofile);
685 fwriteint16_t(segments[i].segnumber, ofile);
686 fwriteint16_t(segments[i].segreserved, ofile);
687 fwriteint32_t(segments[i].seglength, ofile);
689 saa_fpwrite(seg[i], ofile);
690 saa_free(seg[i]);
693 /* null segment - write 10 bytes of zero */
694 fwriteint32_t(0, ofile);
695 fwriteint32_t(0, ofile);
696 fwriteint16_t(0, ofile);
698 fclose(ofile);
701 static int32_t rdf2_segbase(int32_t segment)
703 return segment;
707 * Handle RDOFF2 specific directives
709 static int rdf2_directive(char *directive, char *value, int pass)
711 int n;
713 /* Check if the name length is OK */
714 if ((n = strlen(value)) >= MODLIB_NAME_MAX) {
715 error(ERR_NONFATAL, "name size exceeds %d bytes", MODLIB_NAME_MAX);
716 return 0;
719 if (!strcmp(directive, "library")) {
720 if (pass == 1) {
721 struct DLLRec r;
722 r.type = RDFREC_DLL;
723 r.reclen = n + 1;
724 strcpy(r.libname, value);
725 write_dll_rec(&r);
727 return 1;
730 if (!strcmp(directive, "module")) {
731 if (pass == 1) {
732 struct ModRec r;
733 r.type = RDFREC_MODNAME;
734 r.reclen = n + 1;
735 strcpy(r.modname, value);
736 write_modname_rec(&r);
738 return 1;
741 return 0;
744 static void rdf2_filename(char *inname, char *outname, efunc error)
746 standard_extension(inname, outname, ".rdf", error);
749 extern macros_t rdf2_stdmac[];
751 static int rdf2_set_info(enum geninfo type, char **val)
753 (void)type;
754 (void)val;
755 return 0;
758 struct ofmt of_rdf2 = {
759 "Relocatable Dynamic Object File Format v2.0",
760 "rdf",
761 NULL,
762 null_debug_arr,
763 &null_debug_form,
764 rdf2_stdmac,
765 rdf2_init,
766 rdf2_set_info,
767 rdf2_out,
768 rdf2_deflabel,
769 rdf2_section_names,
770 rdf2_segbase,
771 rdf2_directive,
772 rdf2_filename,
773 rdf2_cleanup
776 #endif /* OF_RDF2 */