NASM 0.98.17
[nasm/avx512.git] / outrdf2.c
blobbe33643d85e7ac7c2dfcecb7d3b3b144fcebac90
1 /* outrdf2.c output routines for the Netwide Assembler to produce
2 * RDOFF version 2 format object files (which are intended
3 * mainly for use in proprietary projects, as the code to
4 * load and execute them is very simple). They will also be
5 * used for device drivers and possibly some executable files
6 * in the MOSCOW operating system. See Rdoff.txt for
7 * details.
9 * The Netwide Assembler is copyright (C) 1996-1998 Simon Tatham and
10 * Julian Hall. All rights reserved. The software is
11 * redistributable under the licence given in the file "Licence"
12 * distributed in the NASM archive.
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <assert.h>
21 #include "nasm.h"
22 #include "nasmlib.h"
23 #include "outform.h"
25 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
26 #define VERBOSE_WARNINGS
28 #ifdef OF_RDF2
30 #define RDF_MAXSEGS 64 /* maximum number of segments - user configurable */
32 typedef unsigned short int16;
33 typedef unsigned char byte;
35 static const char *RDOFF2Id = "RDOFF2"; /* written to start of RDOFF files */
38 /* the records that can be found in the RDOFF header */
40 /* Note that whenever a segment is referred to in the RDOFF file, its number
41 * is always half of the segment number that NASM uses to refer to it; this
42 * is because NASM only allocates even numbered segments, so as to not
43 * waste any of the 16 bits of segment number written to the file - this
44 * allows up to 65533 external labels to be defined; otherwise it would be
45 * 32764. */
47 struct RelocRec {
48 byte type; /* must be 1, or 6 for segment base ref */
49 byte reclen; /* set to 8 */
50 byte segment; /* only 0 for code, or 1 for data supported,
51 * but add 64 for relative refs (ie do not require
52 * reloc @ loadtime, only linkage) */
53 long offset; /* from start of segment in which reference is loc'd */
54 byte length; /* 1 2 or 4 bytes */
55 int16 refseg; /* segment to which reference refers to */
58 struct ImportRec {
59 byte type; /* must be 2, or 7 for FAR import */
60 byte reclen; /* equals 3+label length */
61 int16 segment; /* segment number allocated to the label for reloc
62 * records - label is assumed to be at offset zero
63 * in this segment, so linker must fix up with offset
64 * of segment and of offset within segment */
65 char label[33]; /* zero terminated... should be written to file until
66 * the zero, but not after it - max len = 32 chars */
69 struct ExportRec {
70 byte type; /* must be 3 */
71 byte reclen; /* equals 6+label length */
72 byte segment; /* segment referred to (0/1) */
73 long offset; /* offset within segment */
74 char label[33]; /* zero terminated as above. max len = 32 chars */
77 struct BSSRec {
78 byte type; /* must be 5 */
79 byte reclen; /* equals 4 */
80 long amount; /* number of bytes BSS to reserve */
83 struct DLLModRec {
84 byte type; /* 4 for DLLRec, 8 for ModRec */
85 byte reclen; /* 1+lib name length for DLLRec, 1+mod name length */
86 char name[128]; /* library to link at load time or module name */
89 #define COUNT_SEGTYPES 9
91 static char * segmenttypes[COUNT_SEGTYPES] = {
92 "null", "text", "code", "data", "comment", "lcomment", "pcomment",
93 "symdebug", "linedebug"
96 static int segmenttypenumbers[COUNT_SEGTYPES] = {
97 0, 1, 1, 2, 3, 4, 5, 6, 7
100 /* code for managing buffers needed to seperate code and data into individual
101 * sections until they are ready to be written to the file.
102 * We'd better hope that it all fits in memory else we're buggered... */
104 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
105 * on 80x86 machines for efficiency */
107 /***********************************************************************
108 * Actual code to deal with RDOFF2 ouput format begins here...
111 /* global variables set during the initialisation phase */
113 static struct SAA *seg[RDF_MAXSEGS]; /* seg 0 = code, seg 1 = data */
114 static struct SAA *header; /* relocation/import/export records */
116 static FILE *ofile;
118 static efunc error;
120 static struct seginfo {
121 char *segname;
122 int segnumber;
123 int16 segtype;
124 int16 segreserved;
125 long seglength;
126 } segments[RDF_MAXSEGS];
128 static int nsegments;
130 static long bsslength;
131 static long headerlength;
133 static void rdf2_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
135 int segtext, segdata, segbss;
137 /* set up the initial segments */
138 segments[0].segname = ".text";
139 segments[0].segnumber = 0;
140 segments[0].segtype = 1;
141 segments[0].segreserved = 0;
142 segments[0].seglength = 0;
144 segments[1].segname = ".data";
145 segments[1].segnumber = 1;
146 segments[1].segtype = 2;
147 segments[1].segreserved = 0;
148 segments[1].seglength = 0;
150 segments[2].segname = ".bss";
151 segments[2].segnumber = 2;
152 segments[2].segtype = 0xFFFF; /* reserved - should never be produced */
153 segments[2].segreserved = 0;
154 segments[2].seglength = 0;
156 nsegments = 3;
158 ofile = fp;
159 error = errfunc;
161 seg[0] = saa_init(1L);
162 seg[1] = saa_init(1L);
163 seg[2] = NULL; /* special case! */
165 header = saa_init(1L);
167 segtext = seg_alloc();
168 segdata = seg_alloc();
169 segbss = seg_alloc();
170 if (segtext != 0 || segdata != 2 || segbss != 4)
171 error(ERR_PANIC,"rdf segment numbers not allocated as expected (%d,%d,%d)",
172 segtext,segdata,segbss);
173 bsslength=0;
174 headerlength = 0;
177 static long rdf2_section_names(char *name, int pass, int *bits)
179 int i;
180 char * p, * q;
181 int code = -1;
182 int reserved = 0;
185 * Default is 32 bits, in the text segment.
187 if (!name) {
188 *bits = 32;
189 return 0;
192 /* look for segment type code following segment name */
193 p = name;
194 while (*p && !isspace(*p)) p++;
195 if (*p) { /* we're now in whitespace */
196 *p++ = '\0';
197 while (*p && isspace(80)) *p++ = '\0';
199 if (*p) { /* we're now in an attribute value */
201 * see if we have an optional ',number' following the type code
203 if ((q = strchr(p, ','))) {
204 *q++ = '\0';
206 reserved = readnum(q, &i);
207 if (i) {
208 error(ERR_NONFATAL, "value following comma must be numeric");
209 reserved = 0;
213 * check it against the text strings in segmenttypes
216 for (i = 0; i < COUNT_SEGTYPES; i++)
217 if (!nasm_stricmp(p, segmenttypes[i])) {
218 code = segmenttypenumbers[i];
219 break;
221 if (code == -1) { /* didn't find anything */
222 code = readnum(p, &i);
223 if (i) {
224 error(ERR_NONFATAL, "unrecognised RDF segment type (%s)",p);
225 code = 3;
229 for (i = 0; i < nsegments; i++) {
230 if (!strcmp(name, segments[i].segname)) {
231 if (code != -1 || reserved != 0)
232 error(ERR_NONFATAL, "segment attributes specified on"
233 " redeclaration of segment");
234 return segments[i].segnumber * 2;
238 /* declaring a new segment! */
240 if (code == -1) {
241 error(ERR_NONFATAL, "new segment declared without type code");
242 code = 3;
244 if (nsegments == RDF_MAXSEGS) {
245 error(ERR_FATAL, "reached compiled-in maximum segment limit (%d)",
246 RDF_MAXSEGS);
247 return NO_SEG;
250 segments[nsegments].segname = nasm_strdup(name);
251 i = seg_alloc();
252 if (i % 2 != 0)
253 error(ERR_PANIC, "seg_alloc() returned odd number");
254 segments[nsegments].segnumber = i >> 1;
255 segments[nsegments].segtype = code;
256 segments[nsegments].segreserved = reserved;
257 segments[nsegments].seglength = 0;
259 seg[nsegments] = saa_init(1L);
261 return i;
264 static void write_reloc_rec(struct RelocRec *r)
266 char buf[4],*b;
268 if (r->refseg != (int16)NO_SEG && (r->refseg & 1)) /* segment base ref */
269 r->type = 6;
271 r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
273 saa_wbytes(header,&r->type,1);
274 saa_wbytes(header,&r->reclen,1);
275 saa_wbytes(header,&r->segment,1);
276 b = buf; WRITELONG(b,r->offset);
277 saa_wbytes(header,buf,4);
278 saa_wbytes(header,&r->length,1);
279 b = buf; WRITESHORT(b,r->refseg);
280 saa_wbytes(header,buf,2);
281 headerlength += r->reclen + 2;
284 static void write_export_rec(struct ExportRec *r)
286 char buf[4], *b;
288 r->segment >>= 1;
290 saa_wbytes(header,&r->type,1);
291 saa_wbytes(header,&r->reclen,1);
292 saa_wbytes(header,&r->segment,1);
293 b = buf; WRITELONG(b,r->offset);
294 saa_wbytes(header,buf,4);
295 saa_wbytes(header,r->label,strlen(r->label) + 1);
296 headerlength += r->reclen + 2;
299 static void write_import_rec(struct ImportRec *r)
301 char buf[4], *b;
303 r->segment >>= 1;
305 saa_wbytes(header,&r->type,1);
306 saa_wbytes(header,&r->reclen,1);
307 b = buf; WRITESHORT(b,r->segment);
308 saa_wbytes(header,buf,2);
309 saa_wbytes(header,r->label,strlen(r->label) + 1);
310 headerlength += r->reclen + 2;
313 static void write_bss_rec(struct BSSRec *r)
315 char buf[4], *b;
317 saa_wbytes(header,&r->type,1);
318 saa_wbytes(header,&r->reclen,1);
319 b = buf; WRITELONG(b,r->amount);
320 saa_wbytes(header,buf,4);
321 headerlength += r->reclen + 2;
325 * Write library record. Also used for module name records.
327 static void write_dllmod_rec(struct DLLModRec *r)
329 saa_wbytes(header,&r->type,1);
330 saa_wbytes(header,&r->reclen,1);
331 saa_wbytes(header,r->name,strlen(r->name) + 1);
332 headerlength += r->reclen + 2;
335 static void rdf2_deflabel(char *name, long segment, long offset,
336 int is_global, char *special)
338 struct ExportRec r;
339 struct ImportRec ri;
340 static int farsym = 0;
341 static int i;
343 if (is_global != 1) return;
345 if (special) {
346 while(*special == ' ' || *special == '\t') special++;
348 if (!nasm_stricmp(special, "far")) {
349 farsym = 1;
351 else if (!nasm_stricmp(special, "near")) {
352 farsym = 0;
354 else
355 error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
358 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
359 error (ERR_NONFATAL, "unrecognised special symbol `%s'", name);
360 return;
363 for (i = 0; i < nsegments; i++) {
364 if (segments[i].segnumber == segment>>1) break;
366 if (i >= nsegments) { /* EXTERN declaration */
367 if (farsym)
368 ri.type = 7;
369 else
370 ri.type = 2;
371 ri.segment = segment;
372 strncpy(ri.label,name,32);
373 ri.label[32] = 0;
374 ri.reclen = 3 + strlen(ri.label);
375 write_import_rec(&ri);
376 } else if (is_global) {
377 r.type = 3;
378 r.segment = segment;
379 r.offset = offset;
380 strncpy(r.label,name,32);
381 r.label[32] = 0;
382 r.reclen = 6 + strlen(r.label);
383 write_export_rec(&r);
387 static void membufwrite(int segment, void * data, int bytes)
389 int i;
390 char buf[4], * b;
392 for (i = 0; i < nsegments; i++) {
393 if (segments[i].segnumber == segment) break;
395 if (i == nsegments)
396 error(ERR_PANIC, "can't find segment %d", segment);
398 if (bytes < 0) {
399 b = buf;
400 if (bytes == -2)
401 WRITESHORT(b,*(short *)data);
402 else
403 WRITELONG(b,*(long *)data);
404 data = buf;
405 bytes = -bytes;
407 segments[i].seglength += bytes;
408 saa_wbytes(seg[i],data,bytes);
411 static int getsegmentlength(int segment)
413 int i;
414 for (i = 0; i < nsegments; i++) {
415 if (segments[i].segnumber == segment) break;
417 if (i == nsegments)
418 error(ERR_PANIC, "can't find segment %d", segment);
420 return segments[i].seglength;
423 static void rdf2_out (long segto, void *data, unsigned long type,
424 long segment, long wrt)
426 long bytes = type & OUT_SIZMASK;
427 struct RelocRec rr;
428 unsigned char databuf[4],*pd;
429 int seg;
431 if (segto == NO_SEG) {
432 if ((type & OUT_TYPMASK) != OUT_RESERVE)
433 error (ERR_NONFATAL, "attempt to assemble code in ABSOLUTE space");
434 return;
437 segto >>= 1; /* convert NASM segment no to RDF number */
439 for (seg = 0; seg < nsegments; seg++) {
440 if (segments[seg].segnumber == segto) break;
442 if (seg >= nsegments) {
443 error(ERR_NONFATAL,"specified segment not supported by rdf output format");
444 return;
447 if (wrt != NO_SEG) {
448 wrt = NO_SEG; /* continue to do _something_ */
449 error (ERR_NONFATAL, "WRT not supported by rdf output format");
452 type &= OUT_TYPMASK;
454 if (segto == 2 && type != OUT_RESERVE)
456 error(ERR_NONFATAL, "BSS segments may not be initialised");
458 /* just reserve the space for now... */
460 if (type == OUT_REL2ADR)
461 bytes = 2;
462 else
463 bytes = 4;
464 type = OUT_RESERVE;
467 if (type == OUT_RESERVE) {
468 if (segto == 2) /* BSS segment space reserverd */
469 bsslength += bytes;
470 else
471 while (bytes --)
472 membufwrite(segto,databuf,1);
474 else if (type == OUT_RAWDATA) {
475 if (segment != NO_SEG)
476 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
478 membufwrite(segto,data,bytes);
480 else if (type == OUT_ADDRESS) {
482 /* if segment == NO_SEG then we are writing an address of an
483 object within the same segment - do not produce reloc rec. */
485 /* FIXME - is this behaviour sane? at first glance it doesn't
486 appear to be. Must test this thoroughly...! */
488 if (segment != NO_SEG)
490 /* it's an address, so we must write a relocation record */
492 rr.type = 1; /* type signature */
493 rr.reclen = 8;
494 rr.segment = segto; /* segment we're currently in */
495 rr.offset = getsegmentlength(segto); /* current offset */
496 rr.length = bytes; /* length of reference */
497 rr.refseg = segment; /* segment referred to */
498 write_reloc_rec(&rr);
501 pd = databuf; /* convert address to little-endian */
502 if (bytes == 2)
503 WRITESHORT (pd, *(long *)data);
504 else
505 WRITELONG (pd, *(long *)data);
507 membufwrite(segto,databuf,bytes);
510 else if (type == OUT_REL2ADR)
512 if (segment == segto)
513 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
515 rr.reclen = 8;
516 rr.offset = getsegmentlength(segto); /* current offset */
517 rr.length = 2; /* length of reference */
518 rr.refseg = segment; /* segment referred to (will be >>1'd)*/
520 if (segment != NO_SEG && segment % 2) {
521 rr.type = 6;
522 rr.segment = segto; /* memory base refs *aren't ever* relative! */
523 write_reloc_rec(&rr);
525 /* what do we put in the code? Simply the data. This should almost
526 * always be zero, unless someone's doing segment arithmetic...
528 rr.offset = *(long *) data;
530 else
532 rr.type = 1; /* type signature */
533 rr.segment = segto+64; /* segment we're currently in + rel flag */
534 write_reloc_rec(&rr);
536 /* work out what to put in the code: offset of the end of this operand,
537 * subtracted from any data specified, so that loader can just add
538 * address of imported symbol onto it to get address relative to end of
539 * instruction: import_address + data(offset) - end_of_instrn */
541 rr.offset = *(long *)data -(rr.offset + bytes);
544 membufwrite(segto,&rr.offset,-2);
546 else if (type == OUT_REL4ADR)
548 if (segment == segto)
549 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
550 if (segment != NO_SEG && segment % 2) {
551 error(ERR_PANIC, "erm... 4 byte segment base ref?");
554 rr.type = 1; /* type signature */
555 rr.segment = segto+64; /* segment we're currently in + rel tag */
556 rr.offset = getsegmentlength(segto); /* current offset */
557 rr.length = 4; /* length of reference */
558 rr.refseg = segment; /* segment referred to */
559 rr.reclen = 8;
560 write_reloc_rec(&rr);
562 rr.offset = *(long *)data -(rr.offset + bytes);
564 membufwrite(segto,&rr.offset,-4);
568 static void rdf2_cleanup (int debuginfo) {
569 long l;
570 struct BSSRec bs;
571 int i;
573 (void) debuginfo;
575 /* should write imported & exported symbol declarations to header here */
577 /* generate the output file... */
578 fwrite(RDOFF2Id,6,1,ofile); /* file type magic number */
580 if (bsslength != 0) /* reserve BSS */
582 bs.type = 5;
583 bs.amount = bsslength;
584 bs.reclen = 4;
585 write_bss_rec(&bs);
589 * calculate overall length of the output object
591 l = headerlength + 4;
593 for (i = 0; i < nsegments; i++) {
594 if (i == 2) continue; /* skip BSS segment */
595 l += 10 + segments[i].seglength;
597 l += 10; /* null segment */
599 fwritelong(l, ofile);
601 fwritelong(headerlength, ofile);
602 saa_fpwrite(header,ofile); /* dump header */
603 saa_free(header);
605 for (i = 0; i < nsegments; i++) {
606 if (i == 2) continue;
608 fwriteshort(segments[i].segtype, ofile);
609 fwriteshort(segments[i].segnumber, ofile);
610 fwriteshort(segments[i].segreserved, ofile);
611 fwritelong(segments[i].seglength, ofile);
613 saa_fpwrite(seg[i], ofile);
614 saa_free(seg[i]);
617 /* null segment - write 10 bytes of zero */
618 fwritelong(0,ofile);
619 fwritelong(0,ofile);
620 fwriteshort(0,ofile);
622 fclose(ofile);
625 static long rdf2_segbase (long segment) {
626 return segment;
629 static int rdf2_directive (char *directive, char *value, int pass) {
630 struct DLLModRec r;
632 if (! strcmp(directive, "library")) {
633 if (pass == 1) {
634 r.type = 4;
635 r.reclen=strlen(value)+1;
636 strcpy(r.name, value);
637 write_dllmod_rec(&r);
639 return 1;
642 if (! strcmp(directive, "module")) {
643 if (pass == 1) {
644 r.type = 8;
645 r.reclen=strlen(value)+1;
646 strcpy(r.name, value);
647 write_dllmod_rec(&r);
649 return 1;
652 return 0;
655 static void rdf2_filename (char *inname, char *outname, efunc error) {
656 standard_extension(inname,outname,".rdf",error);
659 static char *rdf2_stdmac[] = {
660 "%define __SECT__ [section .text]",
661 "%imacro library 1+.nolist",
662 "[library %1]",
663 "%endmacro",
664 "%imacro module 1+.nolist",
665 "[module %1]",
666 "%endmacro",
667 "%macro __NASM_CDecl__ 1",
668 "%endmacro",
669 NULL
672 static int rdf2_set_info(enum geninfo type, char **val)
674 return 0;
678 struct ofmt of_rdf2 = {
679 "Relocatable Dynamic Object File Format v2.0",
680 "rdf",
681 NULL,
682 null_debug_arr,
683 &null_debug_form,
684 rdf2_stdmac,
685 rdf2_init,
686 rdf2_set_info,
687 rdf2_out,
688 rdf2_deflabel,
689 rdf2_section_names,
690 rdf2_segbase,
691 rdf2_directive,
692 rdf2_filename,
693 rdf2_cleanup
696 #endif /* OF_RDF2 */