NASM 0.98p3.5
[nasm.git] / outrdf2.c
blob4ae6799b439a6ce12a27a6c915f5c0b1b0699c1e
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 DLLRec {
78 byte type; /* must be 4 */
79 byte reclen; /* equals 1+library name */
80 char libname[128]; /* name of library to link with at load time */
83 struct BSSRec {
84 byte type; /* must be 5 */
85 byte reclen; /* equeals 4 */
86 long amount; /* number of bytes BSS to reserve */
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;
324 static void write_dll_rec(struct DLLRec *r)
326 saa_wbytes(header,&r->type,1);
327 saa_wbytes(header,&r->reclen,1);
328 saa_wbytes(header,r->libname,strlen(r->libname) + 1);
329 headerlength += r->reclen + 2;
332 static void rdf2_deflabel(char *name, long segment, long offset,
333 int is_global, char *special)
335 struct ExportRec r;
336 struct ImportRec ri;
337 #ifdef VERBOSE_WARNINGS
338 static int warned_common = 0;
339 #endif
340 static int farsym = 0;
341 static int i;
343 if (special) {
344 while(*special == ' ' || *special == '\t') special++;
346 if (!nasm_stricmp(special, "far")) {
347 farsym = 1;
349 else if (!nasm_stricmp(special, "near")) {
350 farsym = 0;
352 else
353 error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
356 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
357 error (ERR_NONFATAL, "unrecognised special symbol `%s'", name);
358 return;
361 if (is_global == 2) {
362 #ifdef VERBOSE_WARNINGS
363 if (!warned_common) {
364 error(ERR_WARNING,"common declarations not supported: using extern");
365 warned_common = 1;
367 #endif
368 is_global = 1;
371 for (i = 0; i < nsegments; i++) {
372 if (segments[i].segnumber == segment>>1) break;
374 if (i >= nsegments) { /* EXTERN declaration */
375 if (farsym)
376 ri.type = 7;
377 else
378 ri.type = 2;
379 ri.segment = segment;
380 strncpy(ri.label,name,32);
381 ri.label[32] = 0;
382 ri.reclen = 3 + strlen(ri.label);
383 write_import_rec(&ri);
384 } else if (is_global) {
385 r.type = 3;
386 r.segment = segment;
387 r.offset = offset;
388 strncpy(r.label,name,32);
389 r.label[32] = 0;
390 r.reclen = 6 + strlen(r.label);
391 write_export_rec(&r);
395 static void membufwrite(int segment, void * data, int bytes)
397 int i;
398 char buf[4], * b;
400 for (i = 0; i < nsegments; i++) {
401 if (segments[i].segnumber == segment) break;
403 if (i == nsegments)
404 error(ERR_PANIC, "can't find segment %d", segment);
406 if (bytes < 0) {
407 b = buf;
408 if (bytes == -2)
409 WRITESHORT(b,*(short *)data);
410 else
411 WRITELONG(b,*(long *)data);
412 data = buf;
413 bytes = -bytes;
415 segments[i].seglength += bytes;
416 saa_wbytes(seg[i],data,bytes);
419 static int getsegmentlength(int segment)
421 int i;
422 for (i = 0; i < nsegments; i++) {
423 if (segments[i].segnumber == segment) break;
425 if (i == nsegments)
426 error(ERR_PANIC, "can't find segment %d", segment);
428 return segments[i].seglength;
431 static void rdf2_out (long segto, void *data, unsigned long type,
432 long segment, long wrt)
434 long bytes = type & OUT_SIZMASK;
435 struct RelocRec rr;
436 unsigned char databuf[4],*pd;
437 int seg;
439 if (segto == NO_SEG) {
440 if ((type & OUT_TYPMASK) != OUT_RESERVE)
441 error (ERR_NONFATAL, "attempt to assemble code in ABSOLUTE space");
442 return;
445 segto >>= 1; /* convert NASM segment no to RDF number */
447 for (seg = 0; seg < nsegments; seg++) {
448 if (segments[seg].segnumber == segto) break;
450 if (seg >= nsegments) {
451 error(ERR_NONFATAL,"specified segment not supported by rdf output format");
452 return;
455 if (wrt != NO_SEG) {
456 wrt = NO_SEG; /* continue to do _something_ */
457 error (ERR_NONFATAL, "WRT not supported by rdf output format");
460 type &= OUT_TYPMASK;
462 if (segto == 2 && type != OUT_RESERVE)
464 error(ERR_NONFATAL, "BSS segments may not be initialised");
466 /* just reserve the space for now... */
468 if (type == OUT_REL2ADR)
469 bytes = 2;
470 else
471 bytes = 4;
472 type = OUT_RESERVE;
475 if (type == OUT_RESERVE) {
476 if (segto == 2) /* BSS segment space reserverd */
477 bsslength += bytes;
478 else
479 while (bytes --)
480 membufwrite(segto,databuf,1);
482 else if (type == OUT_RAWDATA) {
483 if (segment != NO_SEG)
484 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
486 membufwrite(segto,data,bytes);
488 else if (type == OUT_ADDRESS) {
490 /* if segment == NO_SEG then we are writing an address of an
491 object within the same segment - do not produce reloc rec. */
493 /* FIXME - is this behaviour sane? at first glance it doesn't
494 appear to be. Must test this thoroughly...! */
496 if (segment != NO_SEG)
498 /* it's an address, so we must write a relocation record */
500 rr.type = 1; /* type signature */
501 rr.reclen = 8;
502 rr.segment = segto; /* segment we're currently in */
503 rr.offset = getsegmentlength(segto); /* current offset */
504 rr.length = bytes; /* length of reference */
505 rr.refseg = segment; /* segment referred to */
506 write_reloc_rec(&rr);
509 pd = databuf; /* convert address to little-endian */
510 if (bytes == 2)
511 WRITESHORT (pd, *(long *)data);
512 else
513 WRITELONG (pd, *(long *)data);
515 membufwrite(segto,databuf,bytes);
518 else if (type == OUT_REL2ADR)
520 if (segment == segto)
521 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
523 rr.reclen = 8;
524 rr.offset = getsegmentlength(segto); /* current offset */
525 rr.length = 2; /* length of reference */
526 rr.refseg = segment; /* segment referred to (will be >>1'd)*/
528 if (segment != NO_SEG && segment % 2) {
529 rr.type = 6;
530 rr.segment = segto; /* memory base refs *aren't ever* relative! */
531 write_reloc_rec(&rr);
533 /* what do we put in the code? Simply the data. This should almost
534 * always be zero, unless someone's doing segment arithmetic...
536 rr.offset = *(long *) data;
538 else
540 rr.type = 1; /* type signature */
541 rr.segment = segto+64; /* segment we're currently in + rel flag */
542 write_reloc_rec(&rr);
544 /* work out what to put in the code: offset of the end of this operand,
545 * subtracted from any data specified, so that loader can just add
546 * address of imported symbol onto it to get address relative to end of
547 * instruction: import_address + data(offset) - end_of_instrn */
549 rr.offset = *(long *)data -(rr.offset + bytes);
552 membufwrite(segto,&rr.offset,-2);
554 else if (type == OUT_REL4ADR)
556 if (segment == segto)
557 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
558 if (segment != NO_SEG && segment % 2) {
559 error(ERR_PANIC, "erm... 4 byte segment base ref?");
562 rr.type = 1; /* type signature */
563 rr.segment = segto+64; /* segment we're currently in + rel tag */
564 rr.offset = getsegmentlength(segto); /* current offset */
565 rr.length = 4; /* length of reference */
566 rr.refseg = segment; /* segment referred to */
567 rr.reclen = 8;
568 write_reloc_rec(&rr);
570 rr.offset = *(long *)data -(rr.offset + bytes);
572 membufwrite(segto,&rr.offset,-4);
576 static void rdf2_cleanup (int debuginfo) {
577 long l;
578 struct BSSRec bs;
579 int i;
581 (void) debuginfo;
583 /* should write imported & exported symbol declarations to header here */
585 /* generate the output file... */
586 fwrite(RDOFF2Id,6,1,ofile); /* file type magic number */
588 if (bsslength != 0) /* reserve BSS */
590 bs.type = 5;
591 bs.amount = bsslength;
592 bs.reclen = 4;
593 write_bss_rec(&bs);
597 * calculate overall length of the output object
599 l = headerlength + 4;
601 for (i = 0; i < nsegments; i++) {
602 if (i == 2) continue; /* skip BSS segment */
603 l += 10 + segments[i].seglength;
605 l += 10; /* null segment */
607 fwritelong(l, ofile);
609 fwritelong(headerlength, ofile);
610 saa_fpwrite(header,ofile); /* dump header */
611 saa_free(header);
613 for (i = 0; i < nsegments; i++) {
614 if (i == 2) continue;
616 fwriteshort(segments[i].segtype, ofile);
617 fwriteshort(segments[i].segnumber, ofile);
618 fwriteshort(segments[i].segreserved, ofile);
619 fwritelong(segments[i].seglength, ofile);
621 saa_fpwrite(seg[i], ofile);
622 saa_free(seg[i]);
625 /* null segment - write 10 bytes of zero */
626 fwritelong(0,ofile);
627 fwritelong(0,ofile);
628 fwriteshort(0,ofile);
630 fclose(ofile);
633 static long rdf2_segbase (long segment) {
634 return segment;
637 static int rdf2_directive (char *directive, char *value, int pass) {
638 struct DLLRec r;
640 if (! strcmp(directive, "library")) {
641 if (pass == 1) {
642 r.type = 4;
643 strcpy(r.libname, value);
644 write_dll_rec(&r);
646 return 1;
649 return 0;
652 static void rdf2_filename (char *inname, char *outname, efunc error) {
653 standard_extension(inname,outname,".rdf",error);
656 static char *rdf2_stdmac[] = {
657 "%define __SECT__ [section .text]",
658 "%imacro library 1+.nolist",
659 "[library %1]",
660 "%endmacro",
661 "%macro __NASM_CDecl__ 1",
662 "%endmacro",
663 NULL
666 static int rdf2_set_info(enum geninfo type, char **val)
668 return 0;
672 struct ofmt of_rdf2 = {
673 "Relocatable Dynamic Object File Format v2.0",
674 "rdf",
675 NULL,
676 null_debug_arr,
677 &null_debug_form,
678 rdf2_stdmac,
679 rdf2_init,
680 rdf2_set_info,
681 rdf2_out,
682 rdf2_deflabel,
683 rdf2_section_names,
684 rdf2_segbase,
685 rdf2_directive,
686 rdf2_filename,
687 rdf2_cleanup
690 #endif /* OF_RDF2 */