match: Check the number of elements in broadcasting operands
[nasm/avx512.git] / output / outrdf2.c
blob02518a1aa5e94d77877479bd16f0a3fdfd540386
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outrdf2.c output routines for the Netwide Assembler to produce
36 * RDOFF version 2 format object files, which Julian originally
37 * planned to use it in his MOSCOW operating system.
40 #include "compiler.h"
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <ctype.h>
46 #include <assert.h>
47 #include <inttypes.h>
49 #include "nasm.h"
50 #include "nasmlib.h"
51 #include "saa.h"
52 #include "output/outform.h"
53 #include "output/outlib.h"
55 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
56 #define VERBOSE_WARNINGS
58 #ifdef OF_RDF2
60 #include "rdoff/rdoff.h"
62 /* This signature is written to start of RDOFF files */
63 static const char *RDOFF2Id = RDOFF2_SIGNATURE;
65 /* Note that whenever a segment is referred to in the RDOFF file, its number
66 * is always half of the segment number that NASM uses to refer to it; this
67 * is because NASM only allocates even numbered segments, so as to not
68 * waste any of the 16 bits of segment number written to the file - this
69 * allows up to 65533 external labels to be defined; otherwise it would be
70 * 32764. */
72 #define COUNT_SEGTYPES 9
74 static char *segmenttypes[COUNT_SEGTYPES] = {
75 "null", "text", "code", "data",
76 "comment", "lcomment", "pcomment",
77 "symdebug", "linedebug"
80 static int segmenttypenumbers[COUNT_SEGTYPES] = {
81 0, 1, 1, 2, 3, 4, 5, 6, 7
84 /* code for managing buffers needed to separate code and data into individual
85 * sections until they are ready to be written to the file.
86 * We'd better hope that it all fits in memory else we're buggered... */
88 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
89 * on 80x86 machines for efficiency */
91 /***********************************************************************
92 * Actual code to deal with RDOFF2 ouput format begins here...
95 /* global variables set during the initialisation phase */
97 static struct SAA *seg[RDF_MAXSEGS]; /* seg 0 = code, seg 1 = data */
98 static struct SAA *header; /* relocation/import/export records */
100 static struct seginfo {
101 char *segname;
102 int segnumber;
103 uint16_t segtype;
104 uint16_t segreserved;
105 int32_t seglength;
106 } segments[RDF_MAXSEGS];
108 static int nsegments;
110 static int32_t bsslength;
111 static int32_t headerlength;
113 static void rdf2_init(void)
115 int segtext, segdata, segbss;
117 maxbits = 64;
119 /* set up the initial segments */
120 segments[0].segname = ".text";
121 segments[0].segnumber = 0;
122 segments[0].segtype = 1;
123 segments[0].segreserved = 0;
124 segments[0].seglength = 0;
126 segments[1].segname = ".data";
127 segments[1].segnumber = 1;
128 segments[1].segtype = 2;
129 segments[1].segreserved = 0;
130 segments[1].seglength = 0;
132 segments[2].segname = ".bss";
133 segments[2].segnumber = 2;
134 segments[2].segtype = 0xFFFF; /* reserved - should never be produced */
135 segments[2].segreserved = 0;
136 segments[2].seglength = 0;
138 nsegments = 3;
140 seg[0] = saa_init(1L);
141 seg[1] = saa_init(1L);
142 seg[2] = NULL; /* special case! */
144 header = saa_init(1L);
146 segtext = seg_alloc();
147 segdata = seg_alloc();
148 segbss = seg_alloc();
149 if (segtext != 0 || segdata != 2 || segbss != 4)
150 nasm_error(ERR_PANIC,
151 "rdf segment numbers not allocated as expected (%d,%d,%d)",
152 segtext, segdata, segbss);
153 bsslength = 0;
154 headerlength = 0;
157 static int32_t rdf2_section_names(char *name, int pass, int *bits)
159 int i;
160 bool err;
161 char *p, *q;
162 int code = -1;
163 int reserved = 0;
165 (void)pass;
168 * Default is 32 bits, in the text segment.
170 if (!name) {
171 *bits = 32;
172 return 0;
175 /* look for segment type code following segment name */
176 p = name;
177 while (*p && !nasm_isspace(*p))
178 p++;
179 if (*p) { /* we're now in whitespace */
180 *p++ = '\0';
181 while (*p && nasm_isspace(80))
182 *p++ = '\0';
184 if (*p) { /* we're now in an attribute value */
186 * see if we have an optional ',number' following the type code
188 if ((q = strchr(p, ','))) {
189 *q++ = '\0';
191 reserved = readnum(q, &err);
192 if (err) {
193 nasm_error(ERR_NONFATAL,
194 "value following comma must be numeric");
195 reserved = 0;
199 * check it against the text strings in segmenttypes
202 for (i = 0; i < COUNT_SEGTYPES; i++)
203 if (!nasm_stricmp(p, segmenttypes[i])) {
204 code = segmenttypenumbers[i];
205 break;
207 if (code == -1) { /* didn't find anything */
208 code = readnum(p, &err);
209 if (err) {
210 nasm_error(ERR_NONFATAL, "unrecognised RDF segment type (%s)",
212 code = 3;
216 for (i = 0; i < nsegments; i++) {
217 if (!strcmp(name, segments[i].segname)) {
218 if (code != -1 || reserved != 0)
219 nasm_error(ERR_NONFATAL, "segment attributes specified on"
220 " redeclaration of segment");
221 return segments[i].segnumber * 2;
225 /* declaring a new segment! */
227 if (code == -1) {
228 nasm_error(ERR_NONFATAL, "new segment declared without type code");
229 code = 3;
231 if (nsegments == RDF_MAXSEGS) {
232 nasm_error(ERR_FATAL, "reached compiled-in maximum segment limit (%d)",
233 RDF_MAXSEGS);
234 return NO_SEG;
237 segments[nsegments].segname = nasm_strdup(name);
238 i = seg_alloc();
239 if (i % 2 != 0)
240 nasm_error(ERR_PANIC, "seg_alloc() returned odd number");
241 segments[nsegments].segnumber = i >> 1;
242 segments[nsegments].segtype = code;
243 segments[nsegments].segreserved = reserved;
244 segments[nsegments].seglength = 0;
246 seg[nsegments] = saa_init(1L);
248 return i;
252 * Write relocation record
254 static void write_reloc_rec(struct RelocRec *r)
256 char buf[4], *b;
258 if (r->refseg != (uint16_t) NO_SEG && (r->refseg & 1)) /* segment base ref */
259 r->type = RDFREC_SEGRELOC;
261 r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
263 saa_wbytes(header, &r->type, 1);
264 saa_wbytes(header, &r->reclen, 1);
265 saa_wbytes(header, &r->segment, 1);
266 b = buf;
267 WRITELONG(b, r->offset);
268 saa_wbytes(header, buf, 4);
269 saa_wbytes(header, &r->length, 1);
270 b = buf;
271 WRITESHORT(b, r->refseg);
272 saa_wbytes(header, buf, 2);
273 headerlength += r->reclen + 2;
277 * Write export record
279 static void write_export_rec(struct ExportRec *r)
281 char buf[4], *b;
283 r->segment >>= 1;
285 saa_wbytes(header, &r->type, 1);
286 saa_wbytes(header, &r->reclen, 1);
287 saa_wbytes(header, &r->flags, 1);
288 saa_wbytes(header, &r->segment, 1);
289 b = buf;
290 WRITELONG(b, r->offset);
291 saa_wbytes(header, buf, 4);
292 saa_wbytes(header, r->label, strlen(r->label) + 1);
293 headerlength += r->reclen + 2;
296 static void write_import_rec(struct ImportRec *r)
298 char buf[4], *b;
300 r->segment >>= 1;
302 saa_wbytes(header, &r->type, 1);
303 saa_wbytes(header, &r->reclen, 1);
304 saa_wbytes(header, &r->flags, 1);
305 b = buf;
306 WRITESHORT(b, r->segment);
307 saa_wbytes(header, buf, 2);
308 saa_wbytes(header, r->label, strlen(r->label) + 1);
309 headerlength += r->reclen + 2;
313 * Write BSS record
315 static void write_bss_rec(struct BSSRec *r)
317 char buf[4], *b;
319 saa_wbytes(header, &r->type, 1);
320 saa_wbytes(header, &r->reclen, 1);
321 b = buf;
322 WRITELONG(b, r->amount);
323 saa_wbytes(header, buf, 4);
324 headerlength += r->reclen + 2;
328 * Write common variable record
330 static void write_common_rec(struct CommonRec *r)
332 char buf[4], *b;
334 r->segment >>= 1;
336 saa_wbytes(header, &r->type, 1);
337 saa_wbytes(header, &r->reclen, 1);
338 b = buf;
339 WRITESHORT(b, r->segment);
340 saa_wbytes(header, buf, 2);
341 b = buf;
342 WRITELONG(b, r->size);
343 saa_wbytes(header, buf, 4);
344 b = buf;
345 WRITESHORT(b, r->align);
346 saa_wbytes(header, buf, 2);
347 saa_wbytes(header, r->label, strlen(r->label) + 1);
348 headerlength += r->reclen + 2;
352 * Write library record
354 static void write_dll_rec(struct DLLRec *r)
356 saa_wbytes(header, &r->type, 1);
357 saa_wbytes(header, &r->reclen, 1);
358 saa_wbytes(header, r->libname, strlen(r->libname) + 1);
359 headerlength += r->reclen + 2;
363 * Write module name record
365 static void write_modname_rec(struct ModRec *r)
367 saa_wbytes(header, &r->type, 1);
368 saa_wbytes(header, &r->reclen, 1);
369 saa_wbytes(header, r->modname, strlen(r->modname) + 1);
370 headerlength += r->reclen + 2;
374 * Handle export, import and common records.
376 static void rdf2_deflabel(char *name, int32_t segment, int64_t offset,
377 int is_global, char *special)
379 struct ExportRec r;
380 struct ImportRec ri;
381 struct CommonRec ci;
382 static int farsym = 0;
383 static int i;
384 char symflags = 0;
385 int len;
387 /* Check if the label length is OK */
388 if ((len = strlen(name)) >= EXIM_LABEL_MAX) {
389 nasm_error(ERR_NONFATAL, "label size exceeds %d bytes", EXIM_LABEL_MAX);
390 return;
392 if (!len) {
393 nasm_error(ERR_NONFATAL, "zero-length label");
394 return;
397 if (is_global == 2) {
398 /* Common variable */
399 ci.type = RDFREC_COMMON;
400 ci.size = offset;
401 ci.segment = segment;
402 strcpy(ci.label, name);
403 ci.reclen = 9 + len;
404 ci.align = 0;
407 * Check the special text to see if it's a valid number and power
408 * of two; if so, store it as the alignment for the common variable.
410 if (special) {
411 bool err;
412 ci.align = readnum(special, &err);
413 if (err)
414 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
415 " valid number", special);
416 else if ((ci.align | (ci.align - 1)) != 2 * ci.align - 1)
417 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
418 " power of two", special);
420 write_common_rec(&ci);
423 /* We don't care about local labels or fix-up hints */
424 if (is_global != 1)
425 return;
427 if (special) {
428 while (*special == ' ' || *special == '\t')
429 special++;
431 if (!nasm_strnicmp(special, "export", 6)) {
432 special += 6;
433 symflags |= SYM_GLOBAL;
434 } else if (!nasm_strnicmp(special, "import", 6)) {
435 special += 6;
436 symflags |= SYM_IMPORT;
439 if (*special) {
440 while (nasm_isspace(*special))
441 special++;
442 if (!nasm_stricmp(special, "far")) {
443 farsym = 1;
444 } else if (!nasm_stricmp(special, "near")) {
445 farsym = 0;
446 } else if (!nasm_stricmp(special, "proc") ||
447 !nasm_stricmp(special, "function")) {
448 symflags |= SYM_FUNCTION;
449 } else if (!nasm_stricmp(special, "data") ||
450 !nasm_stricmp(special, "object")) {
451 symflags |= SYM_DATA;
452 } else
453 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%s'",
454 special);
458 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
459 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
460 return;
463 for (i = 0; i < nsegments; i++) {
464 if (segments[i].segnumber == segment >> 1)
465 break;
468 if (i >= nsegments) { /* EXTERN declaration */
469 ri.type = farsym ? RDFREC_FARIMPORT : RDFREC_IMPORT;
470 if (symflags & SYM_GLOBAL)
471 nasm_error(ERR_NONFATAL,
472 "symbol type conflict - EXTERN cannot be EXPORT");
473 ri.flags = symflags;
474 ri.segment = segment;
475 strcpy(ri.label, name);
476 ri.reclen = 4 + len;
477 write_import_rec(&ri);
478 } else if (is_global) {
479 r.type = RDFREC_GLOBAL; /* GLOBAL declaration */
480 if (symflags & SYM_IMPORT)
481 nasm_error(ERR_NONFATAL,
482 "symbol type conflict - GLOBAL cannot be IMPORT");
483 r.flags = symflags;
484 r.segment = segment;
485 r.offset = offset;
486 strcpy(r.label, name);
487 r.reclen = 7 + len;
488 write_export_rec(&r);
492 static void membufwrite(int segment, const void *data, int bytes)
494 int i;
495 char buf[4], *b;
497 for (i = 0; i < nsegments; i++) {
498 if (segments[i].segnumber == segment)
499 break;
501 if (i == nsegments)
502 nasm_error(ERR_PANIC, "can't find segment %d", segment);
504 if (bytes < 0) {
505 b = buf;
506 if (bytes == -2)
507 WRITESHORT(b, *(int16_t *)data);
508 else
509 WRITELONG(b, *(int32_t *)data);
510 data = buf;
511 bytes = -bytes;
513 segments[i].seglength += bytes;
514 saa_wbytes(seg[i], data, bytes);
517 static int getsegmentlength(int segment)
519 int i;
520 for (i = 0; i < nsegments; i++) {
521 if (segments[i].segnumber == segment)
522 break;
524 if (i == nsegments)
525 nasm_error(ERR_PANIC, "can't find segment %d", segment);
527 return segments[i].seglength;
530 static void rdf2_out(int32_t segto, const void *data,
531 enum out_type type, uint64_t size,
532 int32_t segment, int32_t wrt)
534 struct RelocRec rr;
535 uint8_t databuf[8], *pd;
536 int seg;
538 if (segto == NO_SEG) {
539 if (type != OUT_RESERVE)
540 nasm_error(ERR_NONFATAL,
541 "attempt to assemble code in ABSOLUTE space");
542 return;
545 segto >>= 1; /* convert NASM segment no to RDF number */
547 for (seg = 0; seg < nsegments; seg++) {
548 if (segments[seg].segnumber == segto)
549 break;
551 if (seg >= nsegments) {
552 nasm_error(ERR_NONFATAL,
553 "specified segment not supported by rdf output format");
554 return;
557 if (wrt != NO_SEG) {
558 wrt = NO_SEG; /* continue to do _something_ */
559 nasm_error(ERR_NONFATAL, "WRT not supported by rdf output format");
562 if (segto == 2 && type != OUT_RESERVE) {
563 nasm_error(ERR_NONFATAL, "BSS segments may not be initialized");
565 /* just reserve the space for now... */
567 if (type == OUT_REL2ADR)
568 size = 2;
569 else
570 size = 4;
571 type = OUT_RESERVE;
574 if (type == OUT_RESERVE) {
575 if (segto == 2) /* BSS segment space reserverd */
576 bsslength += size;
577 else
578 while (size--)
579 membufwrite(segto, databuf, 1);
580 } else if (type == OUT_RAWDATA) {
581 if (segment != NO_SEG)
582 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
584 membufwrite(segto, data, size);
585 } else if (type == OUT_ADDRESS) {
587 /* if segment == NO_SEG then we are writing an address of an
588 object within the same segment - do not produce reloc rec. */
590 /* FIXME - is this behaviour sane? at first glance it doesn't
591 appear to be. Must test this thoroughly...! */
593 if (segment != NO_SEG) {
594 /* it's an address, so we must write a relocation record */
596 rr.type = RDFREC_RELOC; /* type signature */
597 rr.reclen = 8;
598 rr.segment = segto; /* segment we're currently in */
599 rr.offset = getsegmentlength(segto); /* current offset */
600 rr.length = size; /* length of reference */
601 rr.refseg = segment; /* segment referred to */
602 write_reloc_rec(&rr);
605 pd = databuf; /* convert address to little-endian */
606 WRITEADDR(pd, *(int64_t *)data, size);
607 membufwrite(segto, databuf, size);
608 } else if (type == OUT_REL2ADR) {
609 if (segment == segto)
610 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
612 rr.reclen = 8;
613 rr.offset = getsegmentlength(segto); /* current offset */
614 rr.length = 2; /* length of reference */
615 rr.refseg = segment; /* segment referred to (will be >>1'd) */
617 if (segment != NO_SEG && segment % 2) {
618 rr.type = RDFREC_SEGRELOC;
619 rr.segment = segto; /* memory base refs *aren't ever* relative! */
620 write_reloc_rec(&rr);
622 /* what do we put in the code? Simply the data. This should almost
623 * always be zero, unless someone's doing segment arithmetic...
625 rr.offset = *(int64_t *)data;
626 } 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 = *(int32_t *)data - (rr.offset + size);
639 membufwrite(segto, &rr.offset, -2);
640 } else if (type == OUT_REL4ADR) {
641 if ((segment == segto) && (globalbits != 64))
642 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
643 if (segment != NO_SEG && segment % 2) {
644 nasm_error(ERR_PANIC, "erm... 4 byte segment base ref?");
647 rr.type = RDFREC_RELOC; /* type signature */
648 rr.segment = segto + 64; /* segment we're currently in + rel tag */
649 rr.offset = getsegmentlength(segto); /* current offset */
650 rr.length = 4; /* length of reference */
651 rr.refseg = segment; /* segment referred to */
652 rr.reclen = 8;
653 write_reloc_rec(&rr);
655 rr.offset = *(int64_t *)data - (rr.offset + size);
657 membufwrite(segto, &rr.offset, -4);
661 static void rdf2_cleanup(int debuginfo)
663 int32_t l;
664 struct BSSRec bs;
665 int i;
667 (void)debuginfo;
669 /* should write imported & exported symbol declarations to header here */
671 /* generate the output file... */
672 fwrite(RDOFF2Id, 6, 1, ofile); /* file type magic number */
674 if (bsslength != 0) { /* reserve BSS */
675 bs.type = RDFREC_BSS;
676 bs.amount = bsslength;
677 bs.reclen = 4;
678 write_bss_rec(&bs);
682 * calculate overall length of the output object
684 l = headerlength + 4;
686 for (i = 0; i < nsegments; i++) {
687 if (i == 2)
688 continue; /* skip BSS segment */
689 l += 10 + segments[i].seglength;
691 l += 10; /* null segment */
693 fwriteint32_t(l, ofile);
695 fwriteint32_t(headerlength, ofile);
696 saa_fpwrite(header, ofile); /* dump header */
697 saa_free(header);
699 for (i = 0; i < nsegments; i++) {
700 if (i == 2)
701 continue;
703 fwriteint16_t(segments[i].segtype, ofile);
704 fwriteint16_t(segments[i].segnumber, ofile);
705 fwriteint16_t(segments[i].segreserved, ofile);
706 fwriteint32_t(segments[i].seglength, ofile);
708 saa_fpwrite(seg[i], ofile);
709 saa_free(seg[i]);
712 /* null segment - write 10 bytes of zero */
713 fwriteint32_t(0, ofile);
714 fwriteint32_t(0, ofile);
715 fwriteint16_t(0, ofile);
718 static int32_t rdf2_segbase(int32_t segment)
720 return segment;
724 * Handle RDOFF2 specific directives
726 static int rdf2_directive(enum directives directive, char *value, int pass)
728 size_t n;
730 switch (directive) {
731 case D_LIBRARY:
732 n = strlen(value);
733 if (n >= MODLIB_NAME_MAX) {
734 nasm_error(ERR_NONFATAL, "name size exceeds %d bytes", MODLIB_NAME_MAX);
735 return 1;
737 if (pass == 1) {
738 struct DLLRec r;
739 r.type = RDFREC_DLL;
740 r.reclen = n + 1;
741 strcpy(r.libname, value);
742 write_dll_rec(&r);
744 return 1;
746 case D_MODULE:
747 if ((n = strlen(value)) >= MODLIB_NAME_MAX) {
748 nasm_error(ERR_NONFATAL, "name size exceeds %d bytes", MODLIB_NAME_MAX);
749 return 1;
751 if (pass == 1) {
752 struct ModRec r;
753 r.type = RDFREC_MODNAME;
754 r.reclen = n + 1;
755 strcpy(r.modname, value);
756 write_modname_rec(&r);
758 return 1;
760 default:
761 return 0;
765 static void rdf2_filename(char *inname, char *outname)
767 standard_extension(inname, outname, ".rdf");
770 extern macros_t rdf2_stdmac[];
772 static int rdf2_set_info(enum geninfo type, char **val)
774 (void)type;
775 (void)val;
776 return 0;
779 struct ofmt of_rdf2 = {
780 "Relocatable Dynamic Object File Format v2.0",
781 "rdf",
783 null_debug_arr,
784 &null_debug_form,
785 rdf2_stdmac,
786 rdf2_init,
787 rdf2_set_info,
788 rdf2_out,
789 rdf2_deflabel,
790 rdf2_section_names,
791 null_sectalign,
792 rdf2_segbase,
793 rdf2_directive,
794 rdf2_filename,
795 rdf2_cleanup
798 #endif /* OF_RDF2 */