rbtree: add rb_search_exact()
[nasm.git] / output / outrdf2.c
blobd020d91ed207c811b9c0924ac2e7d7b03d6b1fb0
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2016 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 "nctype.h"
44 #include "nasm.h"
45 #include "nasmlib.h"
46 #include "error.h"
47 #include "saa.h"
48 #include "outform.h"
49 #include "outlib.h"
51 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
52 #define VERBOSE_WARNINGS
54 #ifdef OF_RDF2
56 #include "rdoff.h"
58 /* This signature is written to start of RDOFF files */
59 static const char *RDOFF2Id = RDOFF2_SIGNATURE;
61 /* Note that whenever a segment is referred to in the RDOFF file, its number
62 * is always half of the segment number that NASM uses to refer to it; this
63 * is because NASM only allocates even numbered segments, so as to not
64 * waste any of the 16 bits of segment number written to the file - this
65 * allows up to 65533 external labels to be defined; otherwise it would be
66 * 32764. */
68 #define COUNT_SEGTYPES 9
70 static char *segmenttypes[COUNT_SEGTYPES] = {
71 "null", "text", "code", "data",
72 "comment", "lcomment", "pcomment",
73 "symdebug", "linedebug"
76 static int segmenttypenumbers[COUNT_SEGTYPES] = {
77 0, 1, 1, 2, 3, 4, 5, 6, 7
80 /* code for managing buffers needed to separate code and data into individual
81 * sections until they are ready to be written to the file.
82 * We'd better hope that it all fits in memory else we're buggered... */
84 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
85 * on 80x86 machines for efficiency */
87 /***********************************************************************
88 * Actual code to deal with RDOFF2 ouput format begins here...
91 /* global variables set during the initialisation phase */
93 static struct SAA *seg[RDF_MAXSEGS]; /* seg 0 = code, seg 1 = data */
94 static struct SAA *header; /* relocation/import/export records */
96 static struct seginfo {
97 char *segname;
98 int segnumber;
99 uint16_t segtype;
100 uint16_t segreserved;
101 int32_t seglength;
102 } segments[RDF_MAXSEGS];
104 static int nsegments;
106 static int32_t bsslength;
107 static int32_t headerlength;
109 static void rdf2_init(void)
111 int segtext, segdata, segbss;
113 /* set up the initial segments */
114 segments[0].segname = ".text";
115 segments[0].segnumber = 0;
116 segments[0].segtype = 1;
117 segments[0].segreserved = 0;
118 segments[0].seglength = 0;
120 segments[1].segname = ".data";
121 segments[1].segnumber = 1;
122 segments[1].segtype = 2;
123 segments[1].segreserved = 0;
124 segments[1].seglength = 0;
126 segments[2].segname = ".bss";
127 segments[2].segnumber = 2;
128 segments[2].segtype = 0xFFFF; /* reserved - should never be produced */
129 segments[2].segreserved = 0;
130 segments[2].seglength = 0;
132 nsegments = 3;
134 seg[0] = saa_init(1L);
135 seg[1] = saa_init(1L);
136 seg[2] = NULL; /* special case! */
138 header = saa_init(1L);
140 segtext = seg_alloc();
141 segdata = seg_alloc();
142 segbss = seg_alloc();
143 if (segtext != 0 || segdata != 2 || segbss != 4)
144 nasm_panic("rdf segment numbers not allocated as expected (%d,%d,%d)",
145 segtext, segdata, segbss);
146 bsslength = 0;
147 headerlength = 0;
150 static int32_t rdf2_section_names(char *name, int *bits)
152 int i;
153 bool err;
154 char *p, *q;
155 int code = -1;
156 int reserved = 0;
159 * Default is 32 bits, in the text segment.
161 if (!name) {
162 *bits = 32;
163 return 0;
166 /* look for segment type code following segment name */
167 p = name;
168 while (*p && !nasm_isspace(*p))
169 p++;
170 if (*p) { /* we're now in whitespace */
171 *p++ = '\0';
172 while (*p && nasm_isspace(80))
173 *p++ = '\0';
175 if (*p) { /* we're now in an attribute value */
177 * see if we have an optional ',number' following the type code
179 if ((q = strchr(p, ','))) {
180 *q++ = '\0';
182 reserved = readnum(q, &err);
183 if (err) {
184 nasm_error(ERR_NONFATAL,
185 "value following comma must be numeric");
186 reserved = 0;
190 * check it against the text strings in segmenttypes
193 for (i = 0; i < COUNT_SEGTYPES; i++)
194 if (!nasm_stricmp(p, segmenttypes[i])) {
195 code = segmenttypenumbers[i];
196 break;
198 if (code == -1) { /* didn't find anything */
199 code = readnum(p, &err);
200 if (err) {
201 nasm_error(ERR_NONFATAL, "unrecognised RDF segment type (%s)",
203 code = 3;
207 for (i = 0; i < nsegments; i++) {
208 if (!strcmp(name, segments[i].segname)) {
209 if (code != -1 || reserved != 0)
210 nasm_error(ERR_NONFATAL, "segment attributes specified on"
211 " redeclaration of segment");
212 return segments[i].segnumber * 2;
216 /* declaring a new segment! */
218 if (code == -1) {
219 nasm_error(ERR_NONFATAL, "new segment declared without type code");
220 code = 3;
222 if (nsegments == RDF_MAXSEGS) {
223 nasm_fatal("reached compiled-in maximum segment limit (%d)",
224 RDF_MAXSEGS);
225 return NO_SEG;
228 segments[nsegments].segname = nasm_strdup(name);
229 i = seg_alloc();
230 if (i % 2 != 0)
231 nasm_panic("seg_alloc() returned odd number");
232 segments[nsegments].segnumber = i >> 1;
233 segments[nsegments].segtype = code;
234 segments[nsegments].segreserved = reserved;
235 segments[nsegments].seglength = 0;
237 seg[nsegments] = saa_init(1L);
239 return i;
243 * Write relocation record
245 static void write_reloc_rec(struct RelocRec *r)
247 char buf[4], *b;
249 if (r->refseg != (uint16_t) NO_SEG && (r->refseg & 1)) /* segment base ref */
250 r->type = RDFREC_SEGRELOC;
252 r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
254 saa_wbytes(header, &r->type, 1);
255 saa_wbytes(header, &r->reclen, 1);
256 saa_wbytes(header, &r->segment, 1);
257 b = buf;
258 WRITELONG(b, r->offset);
259 saa_wbytes(header, buf, 4);
260 saa_wbytes(header, &r->length, 1);
261 b = buf;
262 WRITESHORT(b, r->refseg);
263 saa_wbytes(header, buf, 2);
264 headerlength += r->reclen + 2;
268 * Write export record
270 static void write_export_rec(struct ExportRec *r)
272 char buf[4], *b;
274 r->segment >>= 1;
276 saa_wbytes(header, &r->type, 1);
277 saa_wbytes(header, &r->reclen, 1);
278 saa_wbytes(header, &r->flags, 1);
279 saa_wbytes(header, &r->segment, 1);
280 b = buf;
281 WRITELONG(b, r->offset);
282 saa_wbytes(header, buf, 4);
283 saa_wbytes(header, r->label, strlen(r->label) + 1);
284 headerlength += r->reclen + 2;
287 static void write_import_rec(struct ImportRec *r)
289 char buf[4], *b;
291 r->segment >>= 1;
293 saa_wbytes(header, &r->type, 1);
294 saa_wbytes(header, &r->reclen, 1);
295 saa_wbytes(header, &r->flags, 1);
296 b = buf;
297 WRITESHORT(b, r->segment);
298 saa_wbytes(header, buf, 2);
299 saa_wbytes(header, r->label, strlen(r->label) + 1);
300 headerlength += r->reclen + 2;
304 * Write BSS record
306 static void write_bss_rec(struct BSSRec *r)
308 char buf[4], *b;
310 saa_wbytes(header, &r->type, 1);
311 saa_wbytes(header, &r->reclen, 1);
312 b = buf;
313 WRITELONG(b, r->amount);
314 saa_wbytes(header, buf, 4);
315 headerlength += r->reclen + 2;
319 * Write common variable record
321 static void write_common_rec(struct CommonRec *r)
323 char buf[4], *b;
325 r->segment >>= 1;
327 saa_wbytes(header, &r->type, 1);
328 saa_wbytes(header, &r->reclen, 1);
329 b = buf;
330 WRITESHORT(b, r->segment);
331 saa_wbytes(header, buf, 2);
332 b = buf;
333 WRITELONG(b, r->size);
334 saa_wbytes(header, buf, 4);
335 b = buf;
336 WRITESHORT(b, r->align);
337 saa_wbytes(header, buf, 2);
338 saa_wbytes(header, r->label, strlen(r->label) + 1);
339 headerlength += r->reclen + 2;
343 * Write library record
345 static void write_dll_rec(struct DLLRec *r)
347 saa_wbytes(header, &r->type, 1);
348 saa_wbytes(header, &r->reclen, 1);
349 saa_wbytes(header, r->libname, strlen(r->libname) + 1);
350 headerlength += r->reclen + 2;
354 * Write module name record
356 static void write_modname_rec(struct ModRec *r)
358 saa_wbytes(header, &r->type, 1);
359 saa_wbytes(header, &r->reclen, 1);
360 saa_wbytes(header, r->modname, strlen(r->modname) + 1);
361 headerlength += r->reclen + 2;
365 * Handle export, import and common records.
367 static void rdf2_deflabel(char *name, int32_t segment, int64_t offset,
368 int is_global, char *special)
370 struct ExportRec r;
371 struct ImportRec ri;
372 struct CommonRec ci;
373 static int farsym = 0;
374 static int i;
375 char symflags = 0;
376 int len;
378 /* Check if the label length is OK */
379 if ((len = strlen(name)) >= EXIM_LABEL_MAX) {
380 nasm_error(ERR_NONFATAL, "label size exceeds %d bytes", EXIM_LABEL_MAX);
381 return;
383 if (!len) {
384 nasm_error(ERR_NONFATAL, "zero-length label");
385 return;
388 if (is_global == 2) {
389 /* Common variable */
390 ci.type = RDFREC_COMMON;
391 ci.size = offset;
392 ci.segment = segment;
393 strcpy(ci.label, name);
394 ci.reclen = 9 + len;
395 ci.align = 0;
398 * Check the special text to see if it's a valid number and power
399 * of two; if so, store it as the alignment for the common variable.
401 if (special) {
402 bool err;
403 ci.align = readnum(special, &err);
404 if (err)
405 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
406 " valid number", special);
407 else if ((ci.align | (ci.align - 1)) != 2 * ci.align - 1)
408 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
409 " power of two", special);
411 write_common_rec(&ci);
414 /* We don't care about local labels or fix-up hints */
415 if (is_global != 1)
416 return;
418 if (special) {
419 while (*special == ' ' || *special == '\t')
420 special++;
422 if (!nasm_strnicmp(special, "export", 6)) {
423 special += 6;
424 symflags |= SYM_GLOBAL;
425 } else if (!nasm_strnicmp(special, "import", 6)) {
426 special += 6;
427 symflags |= SYM_IMPORT;
430 if (*special) {
431 while (nasm_isspace(*special))
432 special++;
433 if (!nasm_stricmp(special, "far")) {
434 farsym = 1;
435 } else if (!nasm_stricmp(special, "near")) {
436 farsym = 0;
437 } else if (!nasm_stricmp(special, "proc") ||
438 !nasm_stricmp(special, "function")) {
439 symflags |= SYM_FUNCTION;
440 } else if (!nasm_stricmp(special, "data") ||
441 !nasm_stricmp(special, "object")) {
442 symflags |= SYM_DATA;
443 } else
444 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%s'",
445 special);
449 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
450 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
451 return;
454 for (i = 0; i < nsegments; i++) {
455 if (segments[i].segnumber == segment >> 1)
456 break;
459 if (i >= nsegments) { /* EXTERN declaration */
460 ri.type = farsym ? RDFREC_FARIMPORT : RDFREC_IMPORT;
461 if (symflags & SYM_GLOBAL)
462 nasm_error(ERR_NONFATAL,
463 "symbol type conflict - EXTERN cannot be EXPORT");
464 ri.flags = symflags;
465 ri.segment = segment;
466 strcpy(ri.label, name);
467 ri.reclen = 4 + len;
468 write_import_rec(&ri);
469 } else if (is_global) {
470 r.type = RDFREC_GLOBAL; /* GLOBAL declaration */
471 if (symflags & SYM_IMPORT)
472 nasm_error(ERR_NONFATAL,
473 "symbol type conflict - GLOBAL cannot be IMPORT");
474 r.flags = symflags;
475 r.segment = segment;
476 r.offset = offset;
477 strcpy(r.label, name);
478 r.reclen = 7 + len;
479 write_export_rec(&r);
483 static void membufwrite(int segment, const void *data, int bytes)
485 int i;
486 char buf[4], *b;
488 for (i = 0; i < nsegments; i++) {
489 if (segments[i].segnumber == segment)
490 break;
492 if (i == nsegments)
493 nasm_panic("can't find segment %d", segment);
495 if (bytes < 0) {
496 b = buf;
497 if (bytes == -2)
498 WRITESHORT(b, *(int16_t *)data);
499 else
500 WRITELONG(b, *(int32_t *)data);
501 data = buf;
502 bytes = -bytes;
504 segments[i].seglength += bytes;
505 saa_wbytes(seg[i], data, bytes);
508 static int getsegmentlength(int segment)
510 int i;
511 for (i = 0; i < nsegments; i++) {
512 if (segments[i].segnumber == segment)
513 break;
515 if (i == nsegments)
516 nasm_panic("can't find segment %d", segment);
518 return segments[i].seglength;
521 static void rdf2_out(int32_t segto, const void *data,
522 enum out_type type, uint64_t size,
523 int32_t segment, int32_t wrt)
525 struct RelocRec rr;
526 uint8_t databuf[8], *pd;
527 int seg;
529 segto >>= 1; /* convert NASM segment no to RDF number */
531 for (seg = 0; seg < nsegments; seg++) {
532 if (segments[seg].segnumber == segto)
533 break;
535 if (seg >= nsegments) {
536 nasm_error(ERR_NONFATAL,
537 "specified segment not supported by rdf output format");
538 return;
541 if (wrt != NO_SEG) {
542 wrt = NO_SEG; /* continue to do _something_ */
543 nasm_error(ERR_NONFATAL, "WRT not supported by rdf output format");
546 if (segto == 2 && type != OUT_RESERVE) {
547 nasm_error(ERR_NONFATAL, "BSS segments may not be initialized");
549 /* just reserve the space for now... */
551 if (type == OUT_REL2ADR)
552 size = 2;
553 else
554 size = 4;
555 type = OUT_RESERVE;
558 if (type == OUT_RESERVE) {
559 if (segto == 2) /* BSS segment space reserverd */
560 bsslength += size;
561 else
562 while (size--)
563 membufwrite(segto, databuf, 1);
564 } else if (type == OUT_RAWDATA) {
565 membufwrite(segto, data, size);
566 } else if (type == OUT_ADDRESS) {
567 int asize = abs((int)size);
569 /* if segment == NO_SEG then we are writing an address of an
570 object within the same segment - do not produce reloc rec. */
572 /* FIXME - is this behaviour sane? at first glance it doesn't
573 appear to be. Must test this thoroughly...! */
575 if (segment != NO_SEG) {
576 /* it's an address, so we must write a relocation record */
578 rr.type = RDFREC_RELOC; /* type signature */
579 rr.reclen = 8;
580 rr.segment = segto; /* segment we're currently in */
581 rr.offset = getsegmentlength(segto); /* current offset */
582 rr.length = asize; /* length of reference */
583 rr.refseg = segment; /* segment referred to */
584 write_reloc_rec(&rr);
587 pd = databuf; /* convert address to little-endian */
588 WRITEADDR(pd, *(int64_t *)data, asize);
589 membufwrite(segto, databuf, asize);
590 } else if (type == OUT_REL2ADR) {
591 if (segment == segto)
592 nasm_panic("intra-segment OUT_REL2ADR");
594 rr.reclen = 8;
595 rr.offset = getsegmentlength(segto); /* current offset */
596 rr.length = 2; /* length of reference */
597 rr.refseg = segment; /* segment referred to (will be >>1'd) */
599 if (segment != NO_SEG && segment % 2) {
600 rr.type = RDFREC_SEGRELOC;
601 rr.segment = segto; /* memory base refs *aren't ever* relative! */
602 write_reloc_rec(&rr);
604 /* what do we put in the code? Simply the data. This should almost
605 * always be zero, unless someone's doing segment arithmetic...
607 rr.offset = *(int64_t *)data;
608 } else {
609 rr.type = RDFREC_RELOC; /* type signature */
610 rr.segment = segto + 64; /* segment we're currently in + rel flag */
611 write_reloc_rec(&rr);
613 /* work out what to put in the code: offset of the end of this operand,
614 * subtracted from any data specified, so that loader can just add
615 * address of imported symbol onto it to get address relative to end of
616 * instruction: import_address + data(offset) - end_of_instrn */
618 rr.offset = *(int32_t *)data - (rr.offset + size);
621 membufwrite(segto, &rr.offset, -2);
622 } else if (type == OUT_REL4ADR) {
623 if ((segment == segto) && (globalbits != 64))
624 nasm_panic("intra-segment OUT_REL4ADR");
625 if (segment != NO_SEG && segment % 2) {
626 nasm_panic("erm... 4 byte segment base ref?");
629 rr.type = RDFREC_RELOC; /* type signature */
630 rr.segment = segto + 64; /* segment we're currently in + rel tag */
631 rr.offset = getsegmentlength(segto); /* current offset */
632 rr.length = 4; /* length of reference */
633 rr.refseg = segment; /* segment referred to */
634 rr.reclen = 8;
635 write_reloc_rec(&rr);
637 rr.offset = *(int64_t *)data - (rr.offset + size);
639 membufwrite(segto, &rr.offset, -4);
643 static void rdf2_cleanup(void)
645 int32_t l;
646 struct BSSRec bs;
647 int i;
649 /* should write imported & exported symbol declarations to header here */
651 /* generate the output file... */
652 nasm_write(RDOFF2Id, 6, 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);
699 * Handle RDOFF2 specific directives
701 static enum directive_result
702 rdf2_directive(enum directive directive, char *value)
704 size_t n;
706 switch (directive) {
707 case D_LIBRARY:
708 n = strlen(value);
709 if (n >= MODLIB_NAME_MAX) {
710 nasm_error(ERR_NONFATAL, "name size exceeds %d bytes", MODLIB_NAME_MAX);
711 return DIRR_ERROR;
713 if (pass_first()) { /* XXX */
714 struct DLLRec r;
715 r.type = RDFREC_DLL;
716 r.reclen = n + 1;
717 strcpy(r.libname, value);
718 write_dll_rec(&r);
720 return DIRR_OK;
722 case D_MODULE:
723 if ((n = strlen(value)) >= MODLIB_NAME_MAX) {
724 nasm_error(ERR_NONFATAL, "name size exceeds %d bytes", MODLIB_NAME_MAX);
725 return DIRR_ERROR;
727 if (pass_first()) { /* XXX */
728 struct ModRec r;
729 r.type = RDFREC_MODNAME;
730 r.reclen = n + 1;
731 strcpy(r.modname, value);
732 write_modname_rec(&r);
734 return DIRR_OK;
736 default:
737 return DIRR_UNKNOWN;
741 extern macros_t rdf2_stdmac[];
743 const struct ofmt of_rdf2 = {
744 "Relocatable Dynamic Object File Format v2.0",
745 "rdf",
746 ".rdf",
749 null_debug_arr,
750 &null_debug_form,
751 rdf2_stdmac,
752 rdf2_init,
753 null_reset,
754 nasm_do_legacy_output,
755 rdf2_out,
756 rdf2_deflabel,
757 rdf2_section_names,
758 NULL,
759 null_sectalign,
760 null_segbase,
761 rdf2_directive,
762 rdf2_cleanup,
763 NULL /* pragma list */
766 #endif /* OF_RDF2 */