phash.ph: yet another attempt at getting Perl to behave, arithmetically
[nasm/avx512.git] / output / outrdf.c
blobcd311de5c518203b2971cfd97aa1d0412d27098a
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 /* VERBOSE_WARNINGS: define this to add some extra warnings... */
26 #define VERBOSE_WARNINGS
28 #ifdef OF_RDF
30 typedef int16_t int16; /* not sure if this will be required to be altered
31 at all... best to typedef it just in case */
33 static const char *RDOFFId = "RDOFF1"; /* written to start of RDOFF files */
35 /* the records that can be found in the RDOFF header */
37 /* Note that whenever a segment is referred to in the RDOFF file, its number
38 * is always half of the segment number that NASM uses to refer to it; this
39 * is because NASM only allocates even numbered segments, so as to not
40 * waste any of the 16 bits of segment number written to the file - this
41 * allows up to 65533 external labels to be defined; otherwise it would be
42 * 32764. */
44 struct RelocRec {
45 char type; /* must be 1 */
46 char segment; /* only 0 for code, or 1 for data supported,
47 * but add 64 for relative refs (ie do not require
48 * reloc @ loadtime, only linkage) */
49 int32_t offset; /* from start of segment in which reference is loc'd */
50 char length; /* 1 2 or 4 bytes */
51 int16 refseg; /* segment to which reference refers to */
54 struct ImportRec {
55 char type; /* must be 2 */
56 int16 segment; /* segment number allocated to the label for reloc
57 * records - label is assumed to be at offset zero
58 * in this segment, so linker must fix up with offset
59 * of segment and of offset within segment */
60 char label[33]; /* zero terminated... should be written to file until
61 * the zero, but not after it - max len = 32 chars */
64 struct ExportRec {
65 char type; /* must be 3 */
66 char segment; /* segment referred to (0/1) */
67 int32_t offset; /* offset within segment */
68 char label[33]; /* zero terminated as above. max len = 32 chars */
71 struct DLLRec {
72 char type; /* must be 4 */
73 char libname[128]; /* name of library to link with at load time */
76 struct BSSRec {
77 char type; /* must be 5 */
78 int32_t amount; /* number of bytes BSS to reserve */
81 /* code for managing buffers needed to seperate code and data into individual
82 * sections until they are ready to be written to the file.
83 * We'd better hope that it all fits in memory else we're buggered... */
85 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
86 * on 80x86 machines for efficiency */
88 typedef struct memorybuffer {
89 int length;
90 char buffer[BUF_BLOCK_LEN];
91 struct memorybuffer *next;
92 } memorybuffer;
94 static memorybuffer *newmembuf(void)
96 memorybuffer *t;
98 t = nasm_malloc(sizeof(memorybuffer));
100 t->length = 0;
101 t->next = NULL;
102 return t;
105 static void membufwrite(memorybuffer * b, void *data, int bytes)
107 int16 w;
108 int32_t l;
110 if (b->next) { /* memory buffer full - use next buffer */
111 membufwrite(b->next, data, bytes);
112 return;
114 if ((bytes < 0 && b->length - bytes > BUF_BLOCK_LEN)
115 || (bytes > 0 && b->length + bytes > BUF_BLOCK_LEN)) {
117 /* buffer full and no next allocated... allocate and initialize next
118 * buffer */
120 b->next = newmembuf();
121 membufwrite(b->next, data, bytes);
122 return;
125 switch (bytes) {
126 case -4: /* convert to little-endian */
127 l = *(int32_t *)data;
128 b->buffer[b->length++] = l & 0xFF;
129 l >>= 8;
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 break;
137 case -2:
138 w = *(int16 *) data;
139 b->buffer[b->length++] = w & 0xFF;
140 w >>= 8;
141 b->buffer[b->length++] = w & 0xFF;
142 break;
144 default:
145 while (bytes--) {
146 b->buffer[b->length++] = *(*(uint8_t **)&data);
148 (*(uint8_t **)&data)++;
150 break;
154 static void membufdump(memorybuffer * b, FILE * fp)
156 if (!b)
157 return;
159 fwrite(b->buffer, 1, b->length, fp);
161 membufdump(b->next, fp);
164 static int membuflength(memorybuffer * b)
166 if (!b)
167 return 0;
168 return b->length + membuflength(b->next);
171 static void freemembuf(memorybuffer * b)
173 if (!b)
174 return;
175 freemembuf(b->next);
176 nasm_free(b);
179 /***********************************************************************
180 * Actual code to deal with RDOFF ouput format begins here...
183 /* global variables set during the initialisation phase */
185 static memorybuffer *seg[2]; /* seg 0 = code, seg 1 = data */
186 static memorybuffer *header; /* relocation/import/export records */
188 static FILE *ofile;
190 static efunc error;
192 static int segtext, segdata, segbss;
193 static int32_t bsslength;
195 static void rdf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
197 maxbits = 64;
198 ofile = fp;
199 error = errfunc;
200 seg[0] = newmembuf();
201 seg[1] = newmembuf();
202 header = newmembuf();
203 segtext = seg_alloc();
204 segdata = seg_alloc();
205 segbss = seg_alloc();
206 if (segtext != 0 || segdata != 2 || segbss != 4)
207 error(ERR_PANIC,
208 "rdf segment numbers not allocated as expected (%d,%d,%d)",
209 segtext, segdata, segbss);
210 bsslength = 0;
213 static int32_t rdf_section_names(char *name, int pass, int *bits)
216 * Default is 32 bits.
218 if (!name)
219 *bits = 32;
221 if (!name)
222 return 0;
223 if (!strcmp(name, ".text"))
224 return 0;
225 else if (!strcmp(name, ".data"))
226 return 2;
227 else if (!strcmp(name, ".bss"))
228 return 4;
229 else
230 return NO_SEG;
233 static void write_reloc_rec(struct RelocRec *r)
235 if (r->refseg != NO_SEG && (r->refseg & 1))
236 error(ERR_NONFATAL, "RDF format does not support segment base"
237 " references");
239 r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
241 membufwrite(header, &r->type, 1);
242 membufwrite(header, &r->segment, 1);
243 membufwrite(header, &r->offset, -4);
244 membufwrite(header, &r->length, 1);
245 membufwrite(header, &r->refseg, -2); /* 9 bytes written */
248 static void write_export_rec(struct ExportRec *r)
250 r->segment >>= 1;
252 membufwrite(header, &r->type, 1);
253 membufwrite(header, &r->segment, 1);
254 membufwrite(header, &r->offset, -4);
255 membufwrite(header, r->label, strlen(r->label) + 1);
258 static void write_import_rec(struct ImportRec *r)
260 r->segment >>= 1;
262 membufwrite(header, &r->type, 1);
263 membufwrite(header, &r->segment, -2);
264 membufwrite(header, r->label, strlen(r->label) + 1);
267 static void write_bss_rec(struct BSSRec *r)
269 membufwrite(header, &r->type, 1);
270 membufwrite(header, &r->amount, -4);
273 static void write_dll_rec(struct DLLRec *r)
275 membufwrite(header, &r->type, 1);
276 membufwrite(header, r->libname, strlen(r->libname) + 1);
279 static void rdf_deflabel(char *name, int32_t segment, int32_t offset,
280 int is_global, char *special)
282 struct ExportRec r;
283 struct ImportRec ri;
284 #ifdef VERBOSE_WARNINGS
285 static int warned_common = 0;
286 #endif
288 if (special)
289 error(ERR_NONFATAL, "RDOFF format does not support any"
290 " special symbol types");
292 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
293 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
294 return;
297 if (is_global == 2) {
298 #ifdef VERBOSE_WARNINGS
299 if (!warned_common) {
300 error(ERR_WARNING,
301 "common declarations not supported: using extern");
302 warned_common = 1;
304 #endif
305 is_global = 1;
308 if (segment > 4) { /* EXTERN declaration */
309 ri.type = 2;
310 ri.segment = segment;
311 strncpy(ri.label, name, 32);
312 ri.label[32] = 0;
313 write_import_rec(&ri);
314 } else if (is_global) {
315 r.type = 3;
316 r.segment = segment;
317 r.offset = offset;
318 strncpy(r.label, name, 32);
319 r.label[32] = 0;
320 write_export_rec(&r);
324 static void rdf_out(int32_t segto, void *data, uint32_t type,
325 int32_t segment, int32_t wrt)
327 int32_t bytes = type & OUT_SIZMASK;
328 struct RelocRec rr;
329 uint8_t databuf[8], *pd;
331 if (segto == NO_SEG) {
332 if ((type & OUT_TYPMASK) != OUT_RESERVE)
333 error(ERR_NONFATAL,
334 "attempt to assemble code in ABSOLUTE space");
335 return;
338 segto >>= 1; /* convert NASM segment no to RDF number */
340 if (segto != 0 && segto != 1 && segto != 2) {
341 error(ERR_NONFATAL,
342 "specified segment not supported by rdf output format");
343 return;
346 if (wrt != NO_SEG) {
347 wrt = NO_SEG; /* continue to do _something_ */
348 error(ERR_NONFATAL, "WRT not supported by rdf output format");
351 type &= OUT_TYPMASK;
353 if (segto == 2 && type != OUT_RESERVE) {
354 error(ERR_NONFATAL, "BSS segments may not be initialized");
356 /* just reserve the space for now... */
358 if (type == OUT_REL2ADR)
359 bytes = 2;
360 else
361 bytes = 4;
362 type = OUT_RESERVE;
365 if (type == OUT_RESERVE) {
366 if (segto == 2) /* BSS segment space reserverd */
367 bsslength += bytes;
368 else
369 while (bytes--)
370 membufwrite(seg[segto], databuf, 1);
371 } else if (type == OUT_RAWDATA) {
372 if (segment != NO_SEG)
373 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
374 membufwrite(seg[segto], data, bytes);
375 } else if (type == OUT_ADDRESS) {
377 /* if segment == NO_SEG then we are writing an address of an
378 object within the same segment - do not produce reloc rec. */
380 if (segment != NO_SEG) {
382 /* it's an address, so we must write a relocation record */
384 rr.type = 1; /* type signature */
385 rr.segment = segto; /* segment we're currently in */
386 rr.offset = membuflength(seg[segto]); /* current offset */
387 rr.length = bytes; /* length of reference */
388 rr.refseg = segment; /* segment referred to */
389 write_reloc_rec(&rr);
392 pd = databuf; /* convert address to little-endian */
393 if (bytes == 4)
394 WRITELONG(pd, *(int32_t *)data);
395 else if (bytes == 8)
396 WRITEDLONG(pd, *(int64_t *)data);
397 else
398 WRITESHORT(pd, *(int32_t *)data);
401 membufwrite(seg[segto], databuf, bytes);
403 } else if (type == OUT_REL2ADR) {
404 if (segment == segto)
405 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
406 if (segment != NO_SEG && segment % 2) {
407 error(ERR_NONFATAL,
408 "rdf format does not support segment base refs");
411 rr.type = 1; /* type signature */
412 rr.segment = segto + 64; /* segment we're currently in + rel flag */
413 rr.offset = membuflength(seg[segto]); /* current offset */
414 rr.length = 2; /* length of reference */
415 rr.refseg = segment; /* segment referred to */
416 write_reloc_rec(&rr);
418 /* work out what to put in the code: offset of the end of this operand,
419 * subtracted from any data specified, so that loader can just add
420 * address of imported symbol onto it to get address relative to end of
421 * instruction: import_address + data(offset) - end_of_instrn */
423 rr.offset = *(int32_t *)data - (rr.offset + bytes);
425 membufwrite(seg[segto], &rr.offset, -2);
426 } else if (type == OUT_REL4ADR) {
427 if ((segment == segto) && (globalbits != 64))
428 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
429 if (segment != NO_SEG && segment % 2) {
430 error(ERR_NONFATAL,
431 "rdf format does not support segment base refs");
434 rr.type = 1; /* type signature */
435 rr.segment = segto + 64; /* segment we're currently in + rel tag */
436 rr.offset = membuflength(seg[segto]); /* current offset */
437 rr.length = 4; /* length of reference */
438 rr.refseg = segment; /* segment referred to */
439 write_reloc_rec(&rr);
441 rr.offset = *(int32_t *)data - (rr.offset + bytes);
442 membufwrite(seg[segto], &rr.offset, -4);
446 static void rdf_cleanup(int debuginfo)
448 int32_t l;
449 uint8_t b[4], *d;
450 struct BSSRec bs;
452 (void)debuginfo;
454 /* should write imported & exported symbol declarations to header here */
456 /* generate the output file... */
457 fwrite(RDOFFId, 6, 1, ofile); /* file type magic number */
459 if (bsslength != 0) { /* reserve BSS */
460 bs.type = 5;
461 bs.amount = bsslength;
462 write_bss_rec(&bs);
465 l = membuflength(header);
466 d = b;
467 WRITELONG(d, l);
469 fwrite(b, 4, 1, ofile); /* write length of header */
470 membufdump(header, ofile); /* dump header */
472 l = membuflength(seg[0]);
473 d = b; /* code segment */
474 WRITELONG(d, l);
476 fwrite(b, 4, 1, ofile);
477 membufdump(seg[0], ofile);
479 l = membuflength(seg[1]);
480 d = b; /* data segment */
481 WRITELONG(d, l);
483 fwrite(b, 4, 1, ofile);
484 membufdump(seg[1], ofile);
486 freemembuf(header);
487 freemembuf(seg[0]);
488 freemembuf(seg[1]);
489 fclose(ofile);
492 static int32_t rdf_segbase(int32_t segment)
494 return segment;
497 static int rdf_directive(char *directive, char *value, int pass)
499 struct DLLRec r;
501 if (!strcmp(directive, "library")) {
502 if (pass == 1) {
503 r.type = 4;
504 strcpy(r.libname, value);
505 write_dll_rec(&r);
507 return 1;
510 return 0;
513 static void rdf_filename(char *inname, char *outname, efunc error)
515 standard_extension(inname, outname, ".rdf", error);
518 static char *rdf_stdmac[] = {
519 "%define __SECT__ [section .text]",
520 "%imacro library 1+.nolist",
521 "[library %1]",
522 "%endmacro",
523 "%macro __NASM_CDecl__ 1",
524 "%endmacro",
525 NULL
528 static int rdf_set_info(enum geninfo type, char **val)
530 return 0;
533 struct ofmt of_rdf = {
534 "Relocatable Dynamic Object File Format v1.1",
535 #ifdef OF_RDF2
536 "oldrdf",
537 #else
538 "rdf",
539 #endif
540 NULL,
541 null_debug_arr,
542 &null_debug_form,
543 rdf_stdmac,
544 rdf_init,
545 rdf_set_info,
546 rdf_out,
547 rdf_deflabel,
548 rdf_section_names,
549 rdf_segbase,
550 rdf_directive,
551 rdf_filename,
552 rdf_cleanup
555 #endif /* OF_RDF */