NASM 0.91
[nasm/avx512.git] / outrdf.c
blob24fd4808b2c2f144e51359991dd1e3f5d2b7919f
1 /* outrdf.c output routines for the Netwide Assembler to produce
2 * RDOFF format object files (which are intended mainly
3 * for use in proprietary projects, as the code to load and
4 * execute them is very simple). They will also be used
5 * 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 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 #ifdef OF_RDF
27 typedef short int16; /* not sure if this will be required to be altered
28 at all... best to typedef it just in case */
30 const char *RDOFFId = "RDOFF1"; /* written to the start of RDOFF files */
32 /* the records that can be found in the RDOFF header */
34 /* Note that whenever a segment is referred to in the RDOFF file, its number
35 * is always half of the segment number that NASM uses to refer to it; this
36 * is because NASM only allocates even numbered segments, so as to not
37 * waste any of the 16 bits of segment number written to the file - this
38 * allows up to 65533 external labels to be defined; otherwise it would be
39 * 32764. */
41 struct RelocRec {
42 char type; /* must be 1 */
43 char segment; /* only 0 for code, or 1 for data supported,
44 * but add 64 for relative refs (ie do not require
45 * reloc @ loadtime, only linkage) */
46 long offset; /* from start of segment in which reference is loc'd */
47 char length; /* 1 2 or 4 bytes */
48 int16 refseg; /* segment to which reference refers to */
51 struct ImportRec {
52 char type; /* must be 2 */
53 int16 segment; /* segment number allocated to the label for reloc
54 * records - label is assumed to be at offset zero
55 * in this segment, so linker must fix up with offset
56 * of segment and of offset within segment */
57 char label[33]; /* zero terminated... should be written to file until
58 * the zero, but not after it - max len = 32 chars */
61 struct ExportRec {
62 char type; /* must be 3 */
63 char segment; /* segment referred to (0/1) */
64 long offset; /* offset within segment */
65 char label[33]; /* zero terminated as above. max len = 32 chars */
68 struct DLLRec {
69 char type; /* must be 4 */
70 char libname[128]; /* name of library to link with at load time */
73 struct BSSRec {
74 char type; /* must be 5 */
75 long amount; /* number of bytes BSS to reserve */
78 /* code for managing buffers needed to seperate code and data into individual
79 * sections until they are ready to be written to the file.
80 * We'd better hope that it all fits in memory else we're buggered... */
82 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
83 * on 80x86 machines for efficiency */
85 typedef struct memorybuffer {
86 int length;
87 char buffer[BUF_BLOCK_LEN];
88 struct memorybuffer *next;
89 } memorybuffer;
91 memorybuffer * newmembuf(){
92 memorybuffer * t;
94 t = nasm_malloc(sizeof(memorybuffer));
96 t->length = 0;
97 t->next = NULL;
98 return t;
101 void membufwrite(memorybuffer *b, void *data, int bytes) {
102 int16 w;
103 long l;
105 if (b->next) { /* memory buffer full - use next buffer */
106 membufwrite(b->next,data,bytes);
107 return;
109 if ((bytes < 0 && b->length - bytes > BUF_BLOCK_LEN)
110 || (bytes > 0 && b->length + bytes > BUF_BLOCK_LEN)) {
112 /* buffer full and no next allocated... allocate and initialise next
113 * buffer */
115 b->next = newmembuf();
116 membufwrite(b->next,data,bytes);
119 switch(bytes) {
120 case -4: /* convert to little-endian */
121 l = * (long *) data ;
122 b->buffer[b->length++] = l & 0xFF;
123 l >>= 8 ;
124 b->buffer[b->length++] = l & 0xFF;
125 l >>= 8 ;
126 b->buffer[b->length++] = l & 0xFF;
127 l >>= 8 ;
128 b->buffer[b->length++] = l & 0xFF;
129 break;
131 case -2:
132 w = * (int16 *) data ;
133 b->buffer[b->length++] = w & 0xFF;
134 w >>= 8 ;
135 b->buffer[b->length++] = w & 0xFF;
136 break;
138 default:
139 while(bytes--) {
140 b->buffer[b->length++] = *(* (unsigned char **) &data);
142 (* (unsigned char **) &data)++ ;
144 break;
148 void membufdump(memorybuffer *b,FILE *fp)
150 if (!b) return;
152 fwrite (b->buffer, 1, b->length, fp);
154 membufdump(b->next,fp);
157 int membuflength(memorybuffer *b)
159 if (!b) return 0;
160 return b->length + membuflength(b->next);
163 void freemembuf(memorybuffer *b)
165 if (!b) return;
166 freemembuf(b->next);
167 nasm_free(b);
170 /***********************************************************************
171 * Actual code to deal with RDOFF ouput format begins here...
174 /* global variables set during the initialisation phase */
176 memorybuffer *seg[2]; /* seg 0 = code, seg 1 = data */
177 memorybuffer *header; /* relocation/import/export records */
179 FILE *ofile;
181 int seg_warned;
182 static efunc error;
184 int segtext,segdata,segbss;
185 long bsslength;
187 static void rdf_init(FILE *fp, efunc errfunc, ldfunc ldef)
189 ofile = fp;
190 error = errfunc;
191 seg[0] = newmembuf();
192 seg[1] = newmembuf();
193 header = newmembuf();
194 segtext = seg_alloc();
195 segdata = seg_alloc();
196 segbss = seg_alloc();
197 if (segtext != 0 || segdata != 2 || segbss != 4)
198 error(ERR_PANIC,"rdf segment numbers not allocated as expected (%d,%d,%d)",
199 segtext,segdata,segbss);
200 bsslength=0;
203 static long rdf_section_names(char *name, int pass, int *bits)
206 * Default is 32 bits.
208 if (!name)
209 *bits = 32;
211 if (!name) return 0;
212 if (!strcmp(name, ".text")) return 0;
213 else if (!strcmp(name, ".data")) return 2;
214 else if (!strcmp(name, ".bss")) return 4;
215 else
216 return NO_SEG;
219 static void write_reloc_rec(struct RelocRec *r)
221 r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
223 membufwrite(header,&r->type,1);
224 membufwrite(header,&r->segment,1);
225 membufwrite(header,&r->offset,-4);
226 membufwrite(header,&r->length,1);
227 membufwrite(header,&r->refseg,-2); /* 9 bytes written */
230 static void write_export_rec(struct ExportRec *r)
232 r->segment >>= 1;
234 membufwrite(header,&r->type,1);
235 membufwrite(header,&r->segment,1);
236 membufwrite(header,&r->offset,-4);
237 membufwrite(header,r->label,strlen(r->label) + 1);
240 static void write_import_rec(struct ImportRec *r)
242 r->segment >>= 1;
244 membufwrite(header,&r->type,1);
245 membufwrite(header,&r->segment,-2);
246 membufwrite(header,r->label,strlen(r->label) + 1);
249 static void write_bss_rec(struct BSSRec *r)
251 membufwrite(header,&r->type,1);
252 membufwrite(header,&r->amount,-4);
255 static void rdf_deflabel(char *name, long segment, long offset, int is_global)
257 struct ExportRec r;
258 struct ImportRec ri;
260 if (is_global && segment > 4) {
261 error(ERR_WARNING,"common declarations not supported... using extern");
262 is_global = 0;
265 if (is_global) {
266 r.type = 3;
267 r.segment = segment;
268 r.offset = offset;
269 strncpy(r.label,name,32);
270 r.label[32] = 0;
271 write_export_rec(&r);
274 if (segment > 4) { /* EXTERN declaration */
275 ri.type = 2;
276 ri.segment = segment;
277 strncpy(ri.label,name,32);
278 ri.label[32] = 0;
279 write_import_rec(&ri);
283 static void rdf_out (long segto, void *data, unsigned long type,
284 long segment, long wrt)
286 long bytes = type & OUT_SIZMASK;
287 struct RelocRec rr;
288 unsigned char databuf[4],*pd;
290 segto >>= 1; /* convert NASM segment no to RDF number */
292 if (segto != 0 && segto != 1 && segto != 2) {
293 error(ERR_NONFATAL,"specified segment not supported by rdf output format");
294 return;
297 if (wrt != NO_SEG) {
298 wrt = NO_SEG; /* continue to do _something_ */
299 error (ERR_NONFATAL, "WRT not supported by rdf output format");
302 type &= OUT_TYPMASK;
304 if (segto == 2 && type != OUT_RESERVE)
306 error(ERR_NONFATAL, "BSS segments may not be initialised");
308 /* just reserve the space for now... */
310 if (type == OUT_REL2ADR)
311 bytes = 2;
312 else
313 bytes = 4;
314 type = OUT_RESERVE;
317 if (type == OUT_RESERVE) {
318 if (segto == 2) /* BSS segment space reserverd */
319 bsslength += bytes;
320 else
321 while (bytes --)
322 membufwrite(seg[segto],databuf,1);
324 else if (type == OUT_RAWDATA) {
325 if (segment != NO_SEG)
326 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
327 membufwrite(seg[segto],data,bytes);
329 else if (type == OUT_ADDRESS) {
331 /* if segment == NO_SEG then we are writing an address of an
332 object within the same segment - do not produce reloc rec. */
334 if (segment != NO_SEG)
337 /* it's an address, so we must write a relocation record */
339 rr.type = 1; /* type signature */
340 rr.segment = segto; /* segment we're currently in */
341 rr.offset = membuflength(seg[segto]); /* current offset */
342 rr.length = bytes; /* length of reference */
343 rr.refseg = segment; /* segment referred to */
344 write_reloc_rec(&rr);
347 pd = databuf; /* convert address to little-endian */
348 if (bytes == 2)
349 WRITESHORT (pd, *(long *)data);
350 else
351 WRITELONG (pd, *(long *)data);
353 membufwrite(seg[segto],databuf,bytes);
356 else if (type == OUT_REL2ADR)
358 if (segment == segto)
359 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
360 if (segment != NO_SEG && segment % 2) {
361 error(ERR_NONFATAL, "rdf format does not support segment base refs");
364 rr.type = 1; /* type signature */
365 rr.segment = segto+64; /* segment we're currently in + rel flag */
366 rr.offset = membuflength(seg[segto]); /* current offset */
367 rr.length = 2; /* length of reference */
368 rr.refseg = segment; /* segment referred to */
369 write_reloc_rec(&rr);
371 /* work out what to put in the code: offset of the end of this operand,
372 * subtracted from any data specified, so that loader can just add
373 * address of imported symbol onto it to get address relative to end of
374 * instruction: import_address + data(offset) - end_of_instrn */
376 rr.offset = *(long *)data -(rr.offset + bytes);
378 membufwrite(seg[segto],&rr.offset,-2);
380 else if (type == OUT_REL4ADR)
382 if (segment == segto)
383 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
384 if (segment != NO_SEG && segment % 2) {
385 error(ERR_NONFATAL, "rdf format does not support segment base refs");
388 rr.type = 1; /* type signature */
389 rr.segment = segto+64; /* segment we're currently in + rel tag */
390 rr.offset = membuflength(seg[segto]); /* current offset */
391 rr.length = 4; /* length of reference */
392 rr.refseg = segment; /* segment referred to */
393 write_reloc_rec(&rr);
395 rr.offset = *(long *)data -(rr.offset + bytes);
396 membufwrite(seg[segto],&rr.offset,-4);
400 static void rdf_cleanup (void) {
401 long l;
402 unsigned char b[4],*d;
403 struct BSSRec bs;
406 /* should write imported & exported symbol declarations to header here */
408 /* generate the output file... */
409 fwrite("RDOFF1",6,1,ofile); /* file type magic number */
411 if (bsslength != 0) /* reserve BSS */
413 bs.type = 5;
414 bs.amount = bsslength;
415 write_bss_rec(&bs);
418 l = membuflength(header);d=b;
419 WRITELONG(d,l);
421 fwrite(b,4,1,ofile); /* write length of header */
422 membufdump(header,ofile); /* dump header */
424 l = membuflength(seg[0]);d=b; /* code segment */
425 WRITELONG(d,l);
427 fwrite(b,4,1,ofile);
428 membufdump(seg[0],ofile);
430 l = membuflength(seg[1]);d=b; /* data segment */
431 WRITELONG(d,l);
433 fwrite(b,4,1,ofile);
434 membufdump(seg[1],ofile);
436 freemembuf(header);
437 freemembuf(seg[0]);
438 freemembuf(seg[1]);
439 fclose(ofile);
442 static long rdf_segbase (long segment) {
443 return 0;
446 static int rdf_directive (char *directive, char *value, int pass) {
447 return 0;
450 static void rdf_filename (char *inname, char *outname, efunc error) {
451 standard_extension(inname,outname,".rdf",error);
454 struct ofmt of_rdf = {
455 "Relocatable Dynamic Object File Format v1.1",
456 "rdf",
457 rdf_init,
458 rdf_out,
459 rdf_deflabel,
460 rdf_section_names,
461 rdf_segbase,
462 rdf_directive,
463 rdf_filename,
464 rdf_cleanup
467 #endif /* OF_RDF */