NASM 0.99.05
[nasm/avx512.git] / output / outas86.c
blob351aa047834ca332b848e134c9563e378a70ff41
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 licence given in the file "Licence"
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 "outform.h"
22 #ifdef OF_AS86
24 struct Piece {
25 struct Piece *next;
26 int type; /* 0 = absolute, 1 = seg, 2 = sym */
27 int32_t offset; /* relative offset */
28 int number; /* symbol/segment number (4=bss) */
29 int32_t bytes; /* size of reloc or of absolute data */
30 bool relative; /* relative address? */
33 struct Symbol {
34 int32_t strpos; /* string table position of name */
35 int flags; /* symbol flags */
36 int segment; /* 4=bss at this point */
37 int32_t value; /* address, or COMMON variable size */
41 * Section IDs - used in Piece.number and Symbol.segment.
43 #define SECT_TEXT 0 /* text section */
44 #define SECT_DATA 3 /* data section */
45 #define SECT_BSS 4 /* bss section */
48 * Flags used in Symbol.flags.
50 #define SYM_ENTRY (1<<8)
51 #define SYM_EXPORT (1<<7)
52 #define SYM_IMPORT (1<<6)
53 #define SYM_ABSOLUTE (1<<4)
55 struct Section {
56 struct SAA *data;
57 uint32_t datalen, size, len;
58 int32_t index;
59 struct Piece *head, *last, **tail;
62 static char as86_module[FILENAME_MAX];
64 static struct Section stext, sdata;
65 static uint32_t bsslen;
66 static int32_t bssindex;
68 static struct SAA *syms;
69 static uint32_t nsyms;
71 static struct RAA *bsym;
73 static struct SAA *strs;
74 static uint32_t strslen;
76 static int as86_reloc_size;
78 static FILE *as86fp;
79 static efunc error;
81 static void as86_write(void);
82 static void as86_write_section(struct Section *, int);
83 static int as86_add_string(char *name);
84 static void as86_sect_write(struct Section *, const uint8_t *,
85 uint32_t);
87 static void as86_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
89 as86fp = fp;
90 error = errfunc;
91 (void)ldef; /* placate optimisers */
92 (void)eval;
93 stext.data = saa_init(1L);
94 stext.datalen = 0L;
95 stext.head = stext.last = NULL;
96 stext.tail = &stext.head;
97 sdata.data = saa_init(1L);
98 sdata.datalen = 0L;
99 sdata.head = sdata.last = NULL;
100 sdata.tail = &sdata.head;
101 bsslen =
102 stext.len = stext.datalen = stext.size =
103 sdata.len = sdata.datalen = sdata.size = 0;
104 stext.index = seg_alloc();
105 sdata.index = seg_alloc();
106 bssindex = seg_alloc();
107 syms = saa_init((int32_t)sizeof(struct Symbol));
108 nsyms = 0;
109 bsym = raa_init();
110 strs = saa_init(1L);
111 strslen = 0;
113 as86_add_string(as86_module);
116 static void as86_cleanup(int debuginfo)
118 struct Piece *p;
120 (void)debuginfo;
122 as86_write();
123 fclose(as86fp);
124 saa_free(stext.data);
125 while (stext.head) {
126 p = stext.head;
127 stext.head = stext.head->next;
128 nasm_free(p);
130 saa_free(sdata.data);
131 while (sdata.head) {
132 p = sdata.head;
133 sdata.head = sdata.head->next;
134 nasm_free(p);
136 saa_free(syms);
137 raa_free(bsym);
138 saa_free(strs);
141 static int32_t as86_section_names(char *name, int pass, int *bits)
144 (void)pass;
147 * Default is 16 bits.
149 if (!name)
150 *bits = 16;
152 if (!name)
153 return stext.index;
155 if (!strcmp(name, ".text"))
156 return stext.index;
157 else if (!strcmp(name, ".data"))
158 return sdata.index;
159 else if (!strcmp(name, ".bss"))
160 return bssindex;
161 else
162 return NO_SEG;
165 static int as86_add_string(char *name)
167 int pos = strslen;
168 int length = strlen(name);
170 saa_wbytes(strs, name, (int32_t)(length + 1));
171 strslen += 1 + length;
173 return pos;
176 static void as86_deflabel(char *name, int32_t segment, int32_t offset,
177 int is_global, char *special)
179 struct Symbol *sym;
181 if (special)
182 error(ERR_NONFATAL, "as86 format does not support any"
183 " special symbol types");
185 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
186 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
187 return;
190 sym = saa_wstruct(syms);
192 sym->strpos = as86_add_string(name);
193 sym->flags = 0;
194 if (segment == NO_SEG)
195 sym->flags |= SYM_ABSOLUTE, sym->segment = 0;
196 else if (segment == stext.index)
197 sym->segment = SECT_TEXT;
198 else if (segment == sdata.index)
199 sym->segment = SECT_DATA;
200 else if (segment == bssindex)
201 sym->segment = SECT_BSS;
202 else {
203 sym->flags |= SYM_IMPORT;
204 sym->segment = 15;
207 if (is_global == 2)
208 sym->segment = 3; /* already have IMPORT */
210 if (is_global && !(sym->flags & SYM_IMPORT))
211 sym->flags |= SYM_EXPORT;
213 sym->value = offset;
216 * define the references from external-symbol segment numbers
217 * to these symbol records.
219 if (segment != NO_SEG && segment != stext.index &&
220 segment != sdata.index && segment != bssindex)
221 bsym = raa_write(bsym, segment, nsyms);
223 nsyms++;
226 static void as86_add_piece(struct Section *sect, int type, int32_t offset,
227 int32_t segment, int32_t bytes, int relative)
229 struct Piece *p;
231 sect->len += bytes;
233 if (type == 0 && sect->last && sect->last->type == 0) {
234 sect->last->bytes += bytes;
235 return;
238 p = sect->last = *sect->tail = nasm_malloc(sizeof(struct Piece));
239 sect->tail = &p->next;
240 p->next = NULL;
242 p->type = type;
243 p->offset = offset;
244 p->bytes = bytes;
245 p->relative = relative;
247 if (type == 1 && segment == stext.index)
248 p->number = SECT_TEXT;
249 else if (type == 1 && segment == sdata.index)
250 p->number = SECT_DATA;
251 else if (type == 1 && segment == bssindex)
252 p->number = SECT_BSS;
253 else if (type == 1)
254 p->number = raa_read(bsym, segment), p->type = 2;
257 static void as86_out(int32_t segto, const void *data, uint32_t type,
258 int32_t segment, int32_t wrt)
260 struct Section *s;
261 int32_t realbytes = type & OUT_SIZMASK;
262 int32_t offset;
263 uint8_t mydata[4], *p;
265 if (wrt != NO_SEG) {
266 wrt = NO_SEG; /* continue to do _something_ */
267 error(ERR_NONFATAL, "WRT not supported by as86 output format");
270 type &= OUT_TYPMASK;
273 * handle absolute-assembly (structure definitions)
275 if (segto == NO_SEG) {
276 if (type != OUT_RESERVE)
277 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
278 " space");
279 return;
282 if (segto == stext.index)
283 s = &stext;
284 else if (segto == sdata.index)
285 s = &sdata;
286 else if (segto == bssindex)
287 s = NULL;
288 else {
289 error(ERR_WARNING, "attempt to assemble code in"
290 " segment %d: defaulting to `.text'", segto);
291 s = &stext;
294 if (!s && type != OUT_RESERVE) {
295 error(ERR_WARNING, "attempt to initialize memory in the"
296 " BSS section: ignored");
297 if (type == OUT_REL2ADR)
298 realbytes = 2;
299 else if (type == OUT_REL4ADR)
300 realbytes = 4;
301 bsslen += realbytes;
302 return;
305 if (type == OUT_RESERVE) {
306 if (s) {
307 error(ERR_WARNING, "uninitialized space declared in"
308 " %s section: zeroing",
309 (segto == stext.index ? "code" : "data"));
310 as86_sect_write(s, NULL, realbytes);
311 as86_add_piece(s, 0, 0L, 0L, realbytes, 0);
312 } else
313 bsslen += realbytes;
314 } else if (type == OUT_RAWDATA) {
315 if (segment != NO_SEG)
316 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
317 as86_sect_write(s, data, realbytes);
318 as86_add_piece(s, 0, 0L, 0L, realbytes, 0);
319 } else if (type == OUT_ADDRESS) {
320 if (segment != NO_SEG) {
321 if (segment % 2) {
322 error(ERR_NONFATAL, "as86 format does not support"
323 " segment base references");
324 } else {
325 offset = *(int32_t *)data;
326 as86_add_piece(s, 1, offset, segment, realbytes, 0);
328 } else {
329 p = mydata;
330 WRITELONG(p, *(int32_t *)data);
331 as86_sect_write(s, data, realbytes);
332 as86_add_piece(s, 0, 0L, 0L, realbytes, 0);
334 } else if (type == OUT_REL2ADR) {
335 if (segment == segto)
336 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
337 if (segment != NO_SEG) {
338 if (segment % 2) {
339 error(ERR_NONFATAL, "as86 format does not support"
340 " segment base references");
341 } else {
342 offset = *(int32_t *)data;
343 as86_add_piece(s, 1, offset - realbytes + 2, segment, 2L,
347 } else if (type == OUT_REL4ADR) {
348 if (segment == segto)
349 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
350 if (segment != NO_SEG) {
351 if (segment % 2) {
352 error(ERR_NONFATAL, "as86 format does not support"
353 " segment base references");
354 } else {
355 offset = *(int32_t *)data;
356 as86_add_piece(s, 1, offset - realbytes + 4, segment, 4L,
363 static void as86_write(void)
365 uint32_t i;
366 int32_t symlen, seglen, segsize;
369 * First, go through the symbol records working out how big
370 * each will be. Also fix up BSS references at this time, and
371 * set the flags words up completely.
373 symlen = 0;
374 saa_rewind(syms);
375 for (i = 0; i < nsyms; i++) {
376 struct Symbol *sym = saa_rstruct(syms);
377 if (sym->segment == SECT_BSS)
378 sym->segment = SECT_DATA, sym->value += sdata.len;
379 sym->flags |= sym->segment;
380 if (sym->value == 0)
381 sym->flags |= 0 << 14, symlen += 4;
382 else if (sym->value >= 0 && sym->value <= 255)
383 sym->flags |= 1 << 14, symlen += 5;
384 else if (sym->value >= 0 && sym->value <= 65535L)
385 sym->flags |= 2 << 14, symlen += 6;
386 else
387 sym->flags |= 3 << 14, symlen += 8;
391 * Now do the same for the segments, and get the segment size
392 * descriptor word at the same time.
394 seglen = segsize = 0;
395 if ((uint32_t)stext.len > 65535L)
396 segsize |= 0x03000000L, seglen += 4;
397 else
398 segsize |= 0x02000000L, seglen += 2;
399 if ((uint32_t)sdata.len > 65535L)
400 segsize |= 0xC0000000L, seglen += 4;
401 else
402 segsize |= 0x80000000L, seglen += 2;
405 * Emit the as86 header.
407 fwriteint32_t(0x000186A3L, as86fp);
408 fputc(0x2A, as86fp);
409 fwriteint32_t(27 + symlen + seglen + strslen, as86fp); /* header length */
410 fwriteint32_t(stext.len + sdata.len + bsslen, as86fp);
411 fwriteint16_t(strslen, as86fp);
412 fwriteint16_t(0, as86fp); /* class = revision = 0 */
413 fwriteint32_t(0x55555555L, as86fp); /* segment max sizes: always this */
414 fwriteint32_t(segsize, as86fp); /* segment size descriptors */
415 if (segsize & 0x01000000L)
416 fwriteint32_t(stext.len, as86fp);
417 else
418 fwriteint16_t(stext.len, as86fp);
419 if (segsize & 0x40000000L)
420 fwriteint32_t(sdata.len + bsslen, as86fp);
421 else
422 fwriteint16_t(sdata.len + bsslen, as86fp);
423 fwriteint16_t(nsyms, as86fp);
426 * Write the symbol table.
428 saa_rewind(syms);
429 for (i = 0; i < nsyms; i++) {
430 struct Symbol *sym = saa_rstruct(syms);
431 fwriteint16_t(sym->strpos, as86fp);
432 fwriteint16_t(sym->flags, as86fp);
433 switch (sym->flags & (3 << 14)) {
434 case 0 << 14:
435 break;
436 case 1 << 14:
437 fputc(sym->value, as86fp);
438 break;
439 case 2 << 14:
440 fwriteint16_t(sym->value, as86fp);
441 break;
442 case 3 << 14:
443 fwriteint32_t(sym->value, as86fp);
444 break;
449 * Write out the string table.
451 saa_fpwrite(strs, as86fp);
454 * Write the program text.
456 as86_reloc_size = -1;
457 as86_write_section(&stext, SECT_TEXT);
458 as86_write_section(&sdata, SECT_DATA);
460 * Append the BSS section to the .data section
462 if (bsslen > 65535L) {
463 fputc(0x13, as86fp);
464 fwriteint32_t(bsslen, as86fp);
465 } else if (bsslen > 255) {
466 fputc(0x12, as86fp);
467 fwriteint16_t(bsslen, as86fp);
468 } else if (bsslen) {
469 fputc(0x11, as86fp);
470 fputc(bsslen, as86fp);
473 fputc(0, as86fp); /* termination */
476 static void as86_set_rsize(int size)
478 if (as86_reloc_size != size) {
479 switch (as86_reloc_size = size) {
480 case 1:
481 fputc(0x01, as86fp);
482 break;
483 case 2:
484 fputc(0x02, as86fp);
485 break;
486 case 4:
487 fputc(0x03, as86fp);
488 break;
489 default:
490 error(ERR_PANIC, "bizarre relocation size %d", size);
495 static void as86_write_section(struct Section *sect, int index)
497 struct Piece *p;
498 uint32_t s;
499 int32_t length;
501 fputc(0x20 + index, as86fp); /* select the right section */
503 saa_rewind(sect->data);
505 for (p = sect->head; p; p = p->next)
506 switch (p->type) {
507 case 0:
509 * Absolute data. Emit it in chunks of at most 64
510 * bytes.
512 length = p->bytes;
513 do {
514 char buf[64];
515 int32_t tmplen = (length > 64 ? 64 : length);
516 fputc(0x40 | (tmplen & 0x3F), as86fp);
517 saa_rnbytes(sect->data, buf, tmplen);
518 fwrite(buf, 1, tmplen, as86fp);
519 length -= tmplen;
520 } while (length > 0);
521 break;
522 case 1:
524 * A segment-type relocation. First fix up the BSS.
526 if (p->number == SECT_BSS)
527 p->number = SECT_DATA, p->offset += sdata.len;
528 as86_set_rsize(p->bytes);
529 fputc(0x80 | (p->relative ? 0x20 : 0) | p->number, as86fp);
530 if (as86_reloc_size == 2)
531 fwriteint16_t(p->offset, as86fp);
532 else
533 fwriteint32_t(p->offset, as86fp);
534 break;
535 case 2:
537 * A symbol-type relocation.
539 as86_set_rsize(p->bytes);
540 s = p->offset;
541 if (s > 65535L)
542 s = 3;
543 else if (s > 255)
544 s = 2;
545 else if (s > 0)
546 s = 1;
547 else
548 s = 0;
549 fputc(0xC0 |
550 (p->relative ? 0x20 : 0) |
551 (p->number > 255 ? 0x04 : 0) | s, as86fp);
552 if (p->number > 255)
553 fwriteint16_t(p->number, as86fp);
554 else
555 fputc(p->number, as86fp);
556 switch ((int)s) {
557 case 0:
558 break;
559 case 1:
560 fputc(p->offset, as86fp);
561 break;
562 case 2:
563 fwriteint16_t(p->offset, as86fp);
564 break;
565 case 3:
566 fwriteint32_t(p->offset, as86fp);
567 break;
569 break;
573 static void as86_sect_write(struct Section *sect,
574 const uint8_t *data, uint32_t len)
576 saa_wbytes(sect->data, data, len);
577 sect->datalen += len;
580 static int32_t as86_segbase(int32_t segment)
582 return segment;
585 static int as86_directive(char *directive, char *value, int pass)
587 (void)directive;
588 (void)value;
589 (void)pass;
590 return 0;
593 static void as86_filename(char *inname, char *outname, efunc error)
595 char *p;
597 if ((p = strrchr(inname, '.')) != NULL) {
598 strncpy(as86_module, inname, p - inname);
599 as86_module[p - inname] = '\0';
600 } else
601 strcpy(as86_module, inname);
603 standard_extension(inname, outname, ".o", error);
606 static const char *as86_stdmac[] = {
607 "%define __SECT__ [section .text]",
608 "%macro __NASM_CDecl__ 1",
609 "%endmacro",
610 NULL
613 static int as86_set_info(enum geninfo type, char **val)
615 (void)type;
616 (void)val;
617 return 0;
619 void as86_linenumber(char *name, int32_t segment, int32_t offset, int is_main,
620 int lineno)
622 (void)name;
623 (void)segment;
624 (void)offset;
625 (void)is_main;
626 (void)lineno;
628 struct ofmt of_as86 = {
629 "Linux as86 (bin86 version 0.3) object files",
630 "as86",
631 NULL,
632 null_debug_arr,
633 &null_debug_form,
634 as86_stdmac,
635 as86_init,
636 as86_set_info,
637 as86_out,
638 as86_deflabel,
639 as86_section_names,
640 as86_segbase,
641 as86_directive,
642 as86_filename,
643 as86_cleanup
646 #endif /* OF_AS86 */