NASM 2.06rc1
[nasm/avx512.git] / output / outas86.c
blob307f10b2f07edc5a9d4cc906f59a07e26ad64384
1 /* outas86.c output routines for the Netwide Assembler to produce
2 * Linux as86 (bin86-0.3) object files
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
8 */
10 #include "compiler.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
18 #include "nasm.h"
19 #include "nasmlib.h"
20 #include "saa.h"
21 #include "raa.h"
22 #include "outform.h"
23 #include "outlib.h"
25 #ifdef OF_AS86
27 struct Piece {
28 struct Piece *next;
29 int type; /* 0 = absolute, 1 = seg, 2 = sym */
30 int32_t offset; /* relative offset */
31 int number; /* symbol/segment number (4=bss) */
32 int32_t bytes; /* size of reloc or of absolute data */
33 bool relative; /* relative address? */
36 struct Symbol {
37 int32_t strpos; /* string table position of name */
38 int flags; /* symbol flags */
39 int segment; /* 4=bss at this point */
40 int32_t value; /* address, or COMMON variable size */
44 * Section IDs - used in Piece.number and Symbol.segment.
46 #define SECT_TEXT 0 /* text section */
47 #define SECT_DATA 3 /* data section */
48 #define SECT_BSS 4 /* bss section */
51 * Flags used in Symbol.flags.
53 #define SYM_ENTRY (1<<8)
54 #define SYM_EXPORT (1<<7)
55 #define SYM_IMPORT (1<<6)
56 #define SYM_ABSOLUTE (1<<4)
58 struct Section {
59 struct SAA *data;
60 uint32_t datalen, size, len;
61 int32_t index;
62 struct Piece *head, *last, **tail;
65 static char as86_module[FILENAME_MAX];
67 static struct Section stext, sdata;
68 static uint32_t bsslen;
69 static int32_t bssindex;
71 static struct SAA *syms;
72 static uint32_t nsyms;
74 static struct RAA *bsym;
76 static struct SAA *strs;
77 static uint32_t strslen;
79 static int as86_reloc_size;
81 static FILE *as86fp;
82 static efunc error;
84 static void as86_write(void);
85 static void as86_write_section(struct Section *, int);
86 static int as86_add_string(char *name);
87 static void as86_sect_write(struct Section *, const uint8_t *,
88 uint32_t);
90 static void as86_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
92 as86fp = fp;
93 error = errfunc;
94 (void)ldef; /* placate optimisers */
95 (void)eval;
96 stext.data = saa_init(1L);
97 stext.datalen = 0L;
98 stext.head = stext.last = NULL;
99 stext.tail = &stext.head;
100 sdata.data = saa_init(1L);
101 sdata.datalen = 0L;
102 sdata.head = sdata.last = NULL;
103 sdata.tail = &sdata.head;
104 bsslen =
105 stext.len = stext.datalen = stext.size =
106 sdata.len = sdata.datalen = sdata.size = 0;
107 stext.index = seg_alloc();
108 sdata.index = seg_alloc();
109 bssindex = seg_alloc();
110 syms = saa_init((int32_t)sizeof(struct Symbol));
111 nsyms = 0;
112 bsym = raa_init();
113 strs = saa_init(1L);
114 strslen = 0;
116 as86_add_string(as86_module);
119 static void as86_cleanup(int debuginfo)
121 struct Piece *p;
123 (void)debuginfo;
125 as86_write();
126 fclose(as86fp);
127 saa_free(stext.data);
128 while (stext.head) {
129 p = stext.head;
130 stext.head = stext.head->next;
131 nasm_free(p);
133 saa_free(sdata.data);
134 while (sdata.head) {
135 p = sdata.head;
136 sdata.head = sdata.head->next;
137 nasm_free(p);
139 saa_free(syms);
140 raa_free(bsym);
141 saa_free(strs);
144 static int32_t as86_section_names(char *name, int pass, int *bits)
147 (void)pass;
150 * Default is 16 bits.
152 if (!name)
153 *bits = 16;
155 if (!name)
156 return stext.index;
158 if (!strcmp(name, ".text"))
159 return stext.index;
160 else if (!strcmp(name, ".data"))
161 return sdata.index;
162 else if (!strcmp(name, ".bss"))
163 return bssindex;
164 else
165 return NO_SEG;
168 static int as86_add_string(char *name)
170 int pos = strslen;
171 int length = strlen(name);
173 saa_wbytes(strs, name, (int32_t)(length + 1));
174 strslen += 1 + length;
176 return pos;
179 static void as86_deflabel(char *name, int32_t segment, int64_t offset,
180 int is_global, char *special)
182 struct Symbol *sym;
184 if (special)
185 error(ERR_NONFATAL, "as86 format does not support any"
186 " special symbol types");
188 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
189 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
190 return;
193 sym = saa_wstruct(syms);
195 sym->strpos = as86_add_string(name);
196 sym->flags = 0;
197 if (segment == NO_SEG)
198 sym->flags |= SYM_ABSOLUTE, sym->segment = 0;
199 else if (segment == stext.index)
200 sym->segment = SECT_TEXT;
201 else if (segment == sdata.index)
202 sym->segment = SECT_DATA;
203 else if (segment == bssindex)
204 sym->segment = SECT_BSS;
205 else {
206 sym->flags |= SYM_IMPORT;
207 sym->segment = 15;
210 if (is_global == 2)
211 sym->segment = 3; /* already have IMPORT */
213 if (is_global && !(sym->flags & SYM_IMPORT))
214 sym->flags |= SYM_EXPORT;
216 sym->value = offset;
219 * define the references from external-symbol segment numbers
220 * to these symbol records.
222 if (segment != NO_SEG && segment != stext.index &&
223 segment != sdata.index && segment != bssindex)
224 bsym = raa_write(bsym, segment, nsyms);
226 nsyms++;
229 static void as86_add_piece(struct Section *sect, int type, int32_t offset,
230 int32_t segment, int32_t bytes, int relative)
232 struct Piece *p;
234 sect->len += bytes;
236 if (type == 0 && sect->last && sect->last->type == 0) {
237 sect->last->bytes += bytes;
238 return;
241 p = sect->last = *sect->tail = nasm_malloc(sizeof(struct Piece));
242 sect->tail = &p->next;
243 p->next = NULL;
245 p->type = type;
246 p->offset = offset;
247 p->bytes = bytes;
248 p->relative = relative;
250 if (type == 1 && segment == stext.index)
251 p->number = SECT_TEXT;
252 else if (type == 1 && segment == sdata.index)
253 p->number = SECT_DATA;
254 else if (type == 1 && segment == bssindex)
255 p->number = SECT_BSS;
256 else if (type == 1)
257 p->number = raa_read(bsym, segment), p->type = 2;
260 static void as86_out(int32_t segto, const void *data,
261 enum out_type type, uint64_t size,
262 int32_t segment, int32_t wrt)
264 struct Section *s;
265 int32_t offset;
266 uint8_t mydata[4], *p;
268 if (wrt != NO_SEG) {
269 wrt = NO_SEG; /* continue to do _something_ */
270 error(ERR_NONFATAL, "WRT not supported by as86 output format");
274 * handle absolute-assembly (structure definitions)
276 if (segto == NO_SEG) {
277 if (type != OUT_RESERVE)
278 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
279 " space");
280 return;
283 if (segto == stext.index)
284 s = &stext;
285 else if (segto == sdata.index)
286 s = &sdata;
287 else if (segto == bssindex)
288 s = NULL;
289 else {
290 error(ERR_WARNING, "attempt to assemble code in"
291 " segment %d: defaulting to `.text'", segto);
292 s = &stext;
295 if (!s && type != OUT_RESERVE) {
296 error(ERR_WARNING, "attempt to initialize memory in the"
297 " BSS section: ignored");
298 bsslen += realsize(type, size);
299 return;
302 if (type == OUT_RESERVE) {
303 if (s) {
304 error(ERR_WARNING, "uninitialized space declared in"
305 " %s section: zeroing",
306 (segto == stext.index ? "code" : "data"));
307 as86_sect_write(s, NULL, size);
308 as86_add_piece(s, 0, 0L, 0L, size, 0);
309 } else
310 bsslen += size;
311 } else if (type == OUT_RAWDATA) {
312 if (segment != NO_SEG)
313 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
314 as86_sect_write(s, data, size);
315 as86_add_piece(s, 0, 0L, 0L, size, 0);
316 } else if (type == OUT_ADDRESS) {
317 if (segment != NO_SEG) {
318 if (segment % 2) {
319 error(ERR_NONFATAL, "as86 format does not support"
320 " segment base references");
321 } else {
322 offset = *(int64_t *)data;
323 as86_add_piece(s, 1, offset, segment, size, 0);
325 } else {
326 p = mydata;
327 WRITELONG(p, *(int64_t *)data);
328 as86_sect_write(s, data, size);
329 as86_add_piece(s, 0, 0L, 0L, size, 0);
331 } else if (type == OUT_REL2ADR) {
332 if (segment == segto)
333 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
334 if (segment != NO_SEG) {
335 if (segment % 2) {
336 error(ERR_NONFATAL, "as86 format does not support"
337 " segment base references");
338 } else {
339 offset = *(int64_t *)data;
340 as86_add_piece(s, 1, offset - size + 2, segment, 2L,
344 } else if (type == OUT_REL4ADR) {
345 if (segment == segto)
346 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
347 if (segment != NO_SEG) {
348 if (segment % 2) {
349 error(ERR_NONFATAL, "as86 format does not support"
350 " segment base references");
351 } else {
352 offset = *(int64_t *)data;
353 as86_add_piece(s, 1, offset - size + 4, segment, 4L,
360 static void as86_write(void)
362 uint32_t i;
363 int32_t symlen, seglen, segsize;
366 * First, go through the symbol records working out how big
367 * each will be. Also fix up BSS references at this time, and
368 * set the flags words up completely.
370 symlen = 0;
371 saa_rewind(syms);
372 for (i = 0; i < nsyms; i++) {
373 struct Symbol *sym = saa_rstruct(syms);
374 if (sym->segment == SECT_BSS)
375 sym->segment = SECT_DATA, sym->value += sdata.len;
376 sym->flags |= sym->segment;
377 if (sym->value == 0)
378 sym->flags |= 0 << 14, symlen += 4;
379 else if (sym->value >= 0 && sym->value <= 255)
380 sym->flags |= 1 << 14, symlen += 5;
381 else if (sym->value >= 0 && sym->value <= 65535L)
382 sym->flags |= 2 << 14, symlen += 6;
383 else
384 sym->flags |= 3 << 14, symlen += 8;
388 * Now do the same for the segments, and get the segment size
389 * descriptor word at the same time.
391 seglen = segsize = 0;
392 if ((uint32_t)stext.len > 65535L)
393 segsize |= 0x03000000L, seglen += 4;
394 else
395 segsize |= 0x02000000L, seglen += 2;
396 if ((uint32_t)sdata.len > 65535L)
397 segsize |= 0xC0000000L, seglen += 4;
398 else
399 segsize |= 0x80000000L, seglen += 2;
402 * Emit the as86 header.
404 fwriteint32_t(0x000186A3L, as86fp);
405 fputc(0x2A, as86fp);
406 fwriteint32_t(27 + symlen + seglen + strslen, as86fp); /* header length */
407 fwriteint32_t(stext.len + sdata.len + bsslen, as86fp);
408 fwriteint16_t(strslen, as86fp);
409 fwriteint16_t(0, as86fp); /* class = revision = 0 */
410 fwriteint32_t(0x55555555L, as86fp); /* segment max sizes: always this */
411 fwriteint32_t(segsize, as86fp); /* segment size descriptors */
412 if (segsize & 0x01000000L)
413 fwriteint32_t(stext.len, as86fp);
414 else
415 fwriteint16_t(stext.len, as86fp);
416 if (segsize & 0x40000000L)
417 fwriteint32_t(sdata.len + bsslen, as86fp);
418 else
419 fwriteint16_t(sdata.len + bsslen, as86fp);
420 fwriteint16_t(nsyms, as86fp);
423 * Write the symbol table.
425 saa_rewind(syms);
426 for (i = 0; i < nsyms; i++) {
427 struct Symbol *sym = saa_rstruct(syms);
428 fwriteint16_t(sym->strpos, as86fp);
429 fwriteint16_t(sym->flags, as86fp);
430 switch (sym->flags & (3 << 14)) {
431 case 0 << 14:
432 break;
433 case 1 << 14:
434 fputc(sym->value, as86fp);
435 break;
436 case 2 << 14:
437 fwriteint16_t(sym->value, as86fp);
438 break;
439 case 3 << 14:
440 fwriteint32_t(sym->value, as86fp);
441 break;
446 * Write out the string table.
448 saa_fpwrite(strs, as86fp);
451 * Write the program text.
453 as86_reloc_size = -1;
454 as86_write_section(&stext, SECT_TEXT);
455 as86_write_section(&sdata, SECT_DATA);
457 * Append the BSS section to the .data section
459 if (bsslen > 65535L) {
460 fputc(0x13, as86fp);
461 fwriteint32_t(bsslen, as86fp);
462 } else if (bsslen > 255) {
463 fputc(0x12, as86fp);
464 fwriteint16_t(bsslen, as86fp);
465 } else if (bsslen) {
466 fputc(0x11, as86fp);
467 fputc(bsslen, as86fp);
470 fputc(0, as86fp); /* termination */
473 static void as86_set_rsize(int size)
475 if (as86_reloc_size != size) {
476 switch (as86_reloc_size = size) {
477 case 1:
478 fputc(0x01, as86fp);
479 break;
480 case 2:
481 fputc(0x02, as86fp);
482 break;
483 case 4:
484 fputc(0x03, as86fp);
485 break;
486 default:
487 error(ERR_PANIC, "bizarre relocation size %d", size);
492 static void as86_write_section(struct Section *sect, int index)
494 struct Piece *p;
495 uint32_t s;
496 int32_t length;
498 fputc(0x20 + index, as86fp); /* select the right section */
500 saa_rewind(sect->data);
502 for (p = sect->head; p; p = p->next)
503 switch (p->type) {
504 case 0:
506 * Absolute data. Emit it in chunks of at most 64
507 * bytes.
509 length = p->bytes;
510 do {
511 char buf[64];
512 int32_t tmplen = (length > 64 ? 64 : length);
513 fputc(0x40 | (tmplen & 0x3F), as86fp);
514 saa_rnbytes(sect->data, buf, tmplen);
515 fwrite(buf, 1, tmplen, as86fp);
516 length -= tmplen;
517 } while (length > 0);
518 break;
519 case 1:
521 * A segment-type relocation. First fix up the BSS.
523 if (p->number == SECT_BSS)
524 p->number = SECT_DATA, p->offset += sdata.len;
525 as86_set_rsize(p->bytes);
526 fputc(0x80 | (p->relative ? 0x20 : 0) | p->number, as86fp);
527 if (as86_reloc_size == 2)
528 fwriteint16_t(p->offset, as86fp);
529 else
530 fwriteint32_t(p->offset, as86fp);
531 break;
532 case 2:
534 * A symbol-type relocation.
536 as86_set_rsize(p->bytes);
537 s = p->offset;
538 if (s > 65535L)
539 s = 3;
540 else if (s > 255)
541 s = 2;
542 else if (s > 0)
543 s = 1;
544 else
545 s = 0;
546 fputc(0xC0 |
547 (p->relative ? 0x20 : 0) |
548 (p->number > 255 ? 0x04 : 0) | s, as86fp);
549 if (p->number > 255)
550 fwriteint16_t(p->number, as86fp);
551 else
552 fputc(p->number, as86fp);
553 switch ((int)s) {
554 case 0:
555 break;
556 case 1:
557 fputc(p->offset, as86fp);
558 break;
559 case 2:
560 fwriteint16_t(p->offset, as86fp);
561 break;
562 case 3:
563 fwriteint32_t(p->offset, as86fp);
564 break;
566 break;
570 static void as86_sect_write(struct Section *sect,
571 const uint8_t *data, uint32_t len)
573 saa_wbytes(sect->data, data, len);
574 sect->datalen += len;
577 static int32_t as86_segbase(int32_t segment)
579 return segment;
582 static int as86_directive(char *directive, char *value, int pass)
584 (void)directive;
585 (void)value;
586 (void)pass;
587 return 0;
590 static void as86_filename(char *inname, char *outname, efunc error)
592 char *p;
594 if ((p = strrchr(inname, '.')) != NULL) {
595 strncpy(as86_module, inname, p - inname);
596 as86_module[p - inname] = '\0';
597 } else
598 strcpy(as86_module, inname);
600 standard_extension(inname, outname, ".o", error);
603 extern macros_t as86_stdmac[];
605 static int as86_set_info(enum geninfo type, char **val)
607 (void)type;
608 (void)val;
609 return 0;
611 void as86_linenumber(char *name, int32_t segment, int32_t offset, int is_main,
612 int lineno)
614 (void)name;
615 (void)segment;
616 (void)offset;
617 (void)is_main;
618 (void)lineno;
620 struct ofmt of_as86 = {
621 "Linux as86 (bin86 version 0.3) object files",
622 "as86",
623 NULL,
624 null_debug_arr,
625 &null_debug_form,
626 as86_stdmac,
627 as86_init,
628 as86_set_info,
629 as86_out,
630 as86_deflabel,
631 as86_section_names,
632 as86_segbase,
633 as86_directive,
634 as86_filename,
635 as86_cleanup
638 #endif /* OF_AS86 */