Handle the new ELF headers in the dependency generation
[nasm.git] / output / outrdf.c
blob8c5b2396c21026bcd64e132f5c24c49541b9b94e
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 license given in the file "LICENSE"
12 * distributed in the NASM archive.
15 #include "compiler.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include <assert.h>
23 #include "nasm.h"
24 #include "nasmlib.h"
25 #include "outform.h"
27 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
28 #define VERBOSE_WARNINGS
30 #ifdef OF_RDF
32 typedef int16_t int16; /* not sure if this will be required to be altered
33 at all... best to typedef it just in case */
35 static const char *RDOFFId = "RDOFF1"; /* written to start of RDOFF files */
37 /* the records that can be found in the RDOFF header */
39 /* Note that whenever a segment is referred to in the RDOFF file, its number
40 * is always half of the segment number that NASM uses to refer to it; this
41 * is because NASM only allocates even numbered segments, so as to not
42 * waste any of the 16 bits of segment number written to the file - this
43 * allows up to 65533 external labels to be defined; otherwise it would be
44 * 32764. */
46 struct RelocRec {
47 char type; /* must be 1 */
48 char segment; /* only 0 for code, or 1 for data supported,
49 * but add 64 for relative refs (ie do not require
50 * reloc @ loadtime, only linkage) */
51 int32_t offset; /* from start of segment in which reference is loc'd */
52 char length; /* 1 2 or 4 bytes */
53 int16 refseg; /* segment to which reference refers to */
56 struct ImportRec {
57 char type; /* must be 2 */
58 int16 segment; /* segment number allocated to the label for reloc
59 * records - label is assumed to be at offset zero
60 * in this segment, so linker must fix up with offset
61 * of segment and of offset within segment */
62 char label[33]; /* zero terminated... should be written to file until
63 * the zero, but not after it - max len = 32 chars */
66 struct ExportRec {
67 char type; /* must be 3 */
68 char segment; /* segment referred to (0/1) */
69 int32_t offset; /* offset within segment */
70 char label[33]; /* zero terminated as above. max len = 32 chars */
73 struct DLLRec {
74 char type; /* must be 4 */
75 char libname[128]; /* name of library to link with at load time */
78 struct BSSRec {
79 char type; /* must be 5 */
80 int32_t amount; /* number of bytes BSS to reserve */
83 /* code for managing buffers needed to seperate code and data into individual
84 * sections until they are ready to be written to the file.
85 * We'd better hope that it all fits in memory else we're buggered... */
87 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
88 * on 80x86 machines for efficiency */
90 typedef struct memorybuffer {
91 int length;
92 char buffer[BUF_BLOCK_LEN];
93 struct memorybuffer *next;
94 } memorybuffer;
96 static memorybuffer *newmembuf(void)
98 memorybuffer *t;
100 t = nasm_malloc(sizeof(memorybuffer));
102 t->length = 0;
103 t->next = NULL;
104 return t;
107 static void membufwrite(memorybuffer * b, void *data, int bytes)
109 int16 w;
110 int32_t l;
112 if (b->next) { /* memory buffer full - use next buffer */
113 membufwrite(b->next, data, bytes);
114 return;
116 if ((bytes < 0 && b->length - bytes > BUF_BLOCK_LEN)
117 || (bytes > 0 && b->length + bytes > BUF_BLOCK_LEN)) {
119 /* buffer full and no next allocated... allocate and initialize next
120 * buffer */
122 b->next = newmembuf();
123 membufwrite(b->next, data, bytes);
124 return;
127 switch (bytes) {
128 case -4: /* convert to little-endian */
129 l = *(int32_t *)data;
130 b->buffer[b->length++] = l & 0xFF;
131 l >>= 8;
132 b->buffer[b->length++] = l & 0xFF;
133 l >>= 8;
134 b->buffer[b->length++] = l & 0xFF;
135 l >>= 8;
136 b->buffer[b->length++] = l & 0xFF;
137 break;
139 case -2:
140 w = *(int16 *) data;
141 b->buffer[b->length++] = w & 0xFF;
142 w >>= 8;
143 b->buffer[b->length++] = w & 0xFF;
144 break;
146 default:
147 while (bytes--) {
148 b->buffer[b->length++] = *(*(uint8_t **)&data);
150 (*(uint8_t **)&data)++;
152 break;
156 static void membufdump(memorybuffer * b, FILE * fp)
158 if (!b)
159 return;
161 fwrite(b->buffer, 1, b->length, fp);
163 membufdump(b->next, fp);
166 static int membuflength(memorybuffer * b)
168 if (!b)
169 return 0;
170 return b->length + membuflength(b->next);
173 static void freemembuf(memorybuffer * b)
175 if (!b)
176 return;
177 freemembuf(b->next);
178 nasm_free(b);
181 /***********************************************************************
182 * Actual code to deal with RDOFF ouput format begins here...
185 /* global variables set during the initialisation phase */
187 static memorybuffer *seg[2]; /* seg 0 = code, seg 1 = data */
188 static memorybuffer *header; /* relocation/import/export records */
190 static FILE *ofile;
192 static efunc error;
194 static int segtext, segdata, segbss;
195 static int32_t bsslength;
197 static void rdf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
199 maxbits = 64;
200 ofile = fp;
201 error = errfunc;
202 seg[0] = newmembuf();
203 seg[1] = newmembuf();
204 header = newmembuf();
205 segtext = seg_alloc();
206 segdata = seg_alloc();
207 segbss = seg_alloc();
208 if (segtext != 0 || segdata != 2 || segbss != 4)
209 error(ERR_PANIC,
210 "rdf segment numbers not allocated as expected (%d,%d,%d)",
211 segtext, segdata, segbss);
212 bsslength = 0;
215 static int32_t rdf_section_names(char *name, int pass, int *bits)
218 * Default is 32 bits.
220 if (!name)
221 *bits = 32;
223 if (!name)
224 return 0;
225 if (!strcmp(name, ".text"))
226 return 0;
227 else if (!strcmp(name, ".data"))
228 return 2;
229 else if (!strcmp(name, ".bss"))
230 return 4;
231 else
232 return NO_SEG;
235 static void write_reloc_rec(struct RelocRec *r)
237 if (r->refseg != NO_SEG && (r->refseg & 1))
238 error(ERR_NONFATAL, "RDF format does not support segment base"
239 " references");
241 r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
243 membufwrite(header, &r->type, 1);
244 membufwrite(header, &r->segment, 1);
245 membufwrite(header, &r->offset, -4);
246 membufwrite(header, &r->length, 1);
247 membufwrite(header, &r->refseg, -2); /* 9 bytes written */
250 static void write_export_rec(struct ExportRec *r)
252 r->segment >>= 1;
254 membufwrite(header, &r->type, 1);
255 membufwrite(header, &r->segment, 1);
256 membufwrite(header, &r->offset, -4);
257 membufwrite(header, r->label, strlen(r->label) + 1);
260 static void write_import_rec(struct ImportRec *r)
262 r->segment >>= 1;
264 membufwrite(header, &r->type, 1);
265 membufwrite(header, &r->segment, -2);
266 membufwrite(header, r->label, strlen(r->label) + 1);
269 static void write_bss_rec(struct BSSRec *r)
271 membufwrite(header, &r->type, 1);
272 membufwrite(header, &r->amount, -4);
275 static void write_dll_rec(struct DLLRec *r)
277 membufwrite(header, &r->type, 1);
278 membufwrite(header, r->libname, strlen(r->libname) + 1);
281 static void rdf_deflabel(char *name, int32_t segment, int32_t offset,
282 int is_global, char *special)
284 struct ExportRec r;
285 struct ImportRec ri;
286 #ifdef VERBOSE_WARNINGS
287 static int warned_common = 0;
288 #endif
290 if (special)
291 error(ERR_NONFATAL, "RDOFF format does not support any"
292 " special symbol types");
294 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
295 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
296 return;
299 if (is_global == 2) {
300 #ifdef VERBOSE_WARNINGS
301 if (!warned_common) {
302 error(ERR_WARNING,
303 "common declarations not supported: using extern");
304 warned_common = 1;
306 #endif
307 is_global = 1;
310 if (segment > 4) { /* EXTERN declaration */
311 ri.type = 2;
312 ri.segment = segment;
313 strncpy(ri.label, name, 32);
314 ri.label[32] = 0;
315 write_import_rec(&ri);
316 } else if (is_global) {
317 r.type = 3;
318 r.segment = segment;
319 r.offset = offset;
320 strncpy(r.label, name, 32);
321 r.label[32] = 0;
322 write_export_rec(&r);
326 static void rdf_out(int32_t segto, void *data, uint32_t type,
327 int32_t segment, int32_t wrt)
329 int32_t bytes = type & OUT_SIZMASK;
330 struct RelocRec rr;
331 uint8_t databuf[8], *pd;
333 if (segto == NO_SEG) {
334 if ((type & OUT_TYPMASK) != OUT_RESERVE)
335 error(ERR_NONFATAL,
336 "attempt to assemble code in ABSOLUTE space");
337 return;
340 segto >>= 1; /* convert NASM segment no to RDF number */
342 if (segto != 0 && segto != 1 && segto != 2) {
343 error(ERR_NONFATAL,
344 "specified segment not supported by rdf output format");
345 return;
348 if (wrt != NO_SEG) {
349 wrt = NO_SEG; /* continue to do _something_ */
350 error(ERR_NONFATAL, "WRT not supported by rdf output format");
353 type &= OUT_TYPMASK;
355 if (segto == 2 && type != OUT_RESERVE) {
356 error(ERR_NONFATAL, "BSS segments may not be initialized");
358 /* just reserve the space for now... */
360 if (type == OUT_REL2ADR)
361 bytes = 2;
362 else
363 bytes = 4;
364 type = OUT_RESERVE;
367 if (type == OUT_RESERVE) {
368 if (segto == 2) /* BSS segment space reserverd */
369 bsslength += bytes;
370 else
371 while (bytes--)
372 membufwrite(seg[segto], databuf, 1);
373 } else if (type == OUT_RAWDATA) {
374 if (segment != NO_SEG)
375 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
376 membufwrite(seg[segto], data, bytes);
377 } else if (type == OUT_ADDRESS) {
379 /* if segment == NO_SEG then we are writing an address of an
380 object within the same segment - do not produce reloc rec. */
382 if (segment != NO_SEG) {
384 /* it's an address, so we must write a relocation record */
386 rr.type = 1; /* type signature */
387 rr.segment = segto; /* segment we're currently in */
388 rr.offset = membuflength(seg[segto]); /* current offset */
389 rr.length = bytes; /* length of reference */
390 rr.refseg = segment; /* segment referred to */
391 write_reloc_rec(&rr);
394 pd = databuf; /* convert address to little-endian */
395 WRITEADDR(pd, *(int64_t *)data, bytes);
396 membufwrite(seg[segto], databuf, bytes);
397 } else if (type == OUT_REL2ADR) {
398 if (segment == segto)
399 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
400 if (segment != NO_SEG && segment % 2) {
401 error(ERR_NONFATAL,
402 "rdf format does not support segment base refs");
405 rr.type = 1; /* type signature */
406 rr.segment = segto + 64; /* segment we're currently in + rel flag */
407 rr.offset = membuflength(seg[segto]); /* current offset */
408 rr.length = 2; /* length of reference */
409 rr.refseg = segment; /* segment referred to */
410 write_reloc_rec(&rr);
412 /* work out what to put in the code: offset of the end of this operand,
413 * subtracted from any data specified, so that loader can just add
414 * address of imported symbol onto it to get address relative to end of
415 * instruction: import_address + data(offset) - end_of_instrn */
417 rr.offset = *(int64_t *)data - (rr.offset + bytes);
419 membufwrite(seg[segto], &rr.offset, -2);
420 } else if (type == OUT_REL4ADR) {
421 if ((segment == segto) && (globalbits != 64))
422 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
423 if (segment != NO_SEG && segment % 2) {
424 error(ERR_NONFATAL,
425 "rdf format does not support segment base refs");
428 rr.type = 1; /* type signature */
429 rr.segment = segto + 64; /* segment we're currently in + rel tag */
430 rr.offset = membuflength(seg[segto]); /* current offset */
431 rr.length = 4; /* length of reference */
432 rr.refseg = segment; /* segment referred to */
433 write_reloc_rec(&rr);
435 rr.offset = *(int64_t *)data - (rr.offset + bytes);
436 membufwrite(seg[segto], &rr.offset, -4);
440 static void rdf_cleanup(int debuginfo)
442 int32_t l;
443 uint8_t b[4], *d;
444 struct BSSRec bs;
446 (void)debuginfo;
448 /* should write imported & exported symbol declarations to header here */
450 /* generate the output file... */
451 fwrite(RDOFFId, 6, 1, ofile); /* file type magic number */
453 if (bsslength != 0) { /* reserve BSS */
454 bs.type = 5;
455 bs.amount = bsslength;
456 write_bss_rec(&bs);
459 l = membuflength(header);
460 d = b;
461 WRITELONG(d, l);
463 fwrite(b, 4, 1, ofile); /* write length of header */
464 membufdump(header, ofile); /* dump header */
466 l = membuflength(seg[0]);
467 d = b; /* code segment */
468 WRITELONG(d, l);
470 fwrite(b, 4, 1, ofile);
471 membufdump(seg[0], ofile);
473 l = membuflength(seg[1]);
474 d = b; /* data segment */
475 WRITELONG(d, l);
477 fwrite(b, 4, 1, ofile);
478 membufdump(seg[1], ofile);
480 freemembuf(header);
481 freemembuf(seg[0]);
482 freemembuf(seg[1]);
483 fclose(ofile);
486 static int32_t rdf_segbase(int32_t segment)
488 return segment;
491 static int rdf_directive(char *directive, char *value, int pass)
493 struct DLLRec r;
495 if (!strcmp(directive, "library")) {
496 if (pass == 1) {
497 r.type = 4;
498 strcpy(r.libname, value);
499 write_dll_rec(&r);
501 return 1;
504 return 0;
507 static void rdf_filename(char *inname, char *outname, efunc error)
509 standard_extension(inname, outname, ".rdf", error);
512 extern macros_t rdf_stdmac[];
514 static int rdf_set_info(enum geninfo type, char **val)
516 return 0;
519 struct ofmt of_rdf = {
520 "Relocatable Dynamic Object File Format v1.1",
521 #ifdef OF_RDF2
522 "oldrdf",
523 #else
524 "rdf",
525 #endif
526 NULL,
527 null_debug_arr,
528 &null_debug_form,
529 rdf_stdmac,
530 rdf_init,
531 rdf_set_info,
532 rdf_out,
533 rdf_deflabel,
534 rdf_section_names,
535 rdf_segbase,
536 rdf_directive,
537 rdf_filename,
538 rdf_cleanup
541 #endif /* OF_RDF */