NASM-2.09.10
[nasm.git] / output / outas86.c
blobb288637b2a7a84d4c07a6f5d0669f20a7ae467b6
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
34 /*
35 * outas86.c output routines for the Netwide Assembler to produce
36 * Linux as86 (bin86-0.3) object files
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "saa.h"
50 #include "raa.h"
51 #include "output/outform.h"
52 #include "output/outlib.h"
54 #ifdef OF_AS86
56 struct Piece {
57 struct Piece *next;
58 int type; /* 0 = absolute, 1 = seg, 2 = sym */
59 int32_t offset; /* relative offset */
60 int number; /* symbol/segment number (4=bss) */
61 int32_t bytes; /* size of reloc or of absolute data */
62 bool relative; /* relative address? */
65 struct Symbol {
66 int32_t strpos; /* string table position of name */
67 int flags; /* symbol flags */
68 int segment; /* 4=bss at this point */
69 int32_t value; /* address, or COMMON variable size */
73 * Section IDs - used in Piece.number and Symbol.segment.
75 #define SECT_TEXT 0 /* text section */
76 #define SECT_DATA 3 /* data section */
77 #define SECT_BSS 4 /* bss section */
80 * Flags used in Symbol.flags.
82 #define SYM_ENTRY (1<<8)
83 #define SYM_EXPORT (1<<7)
84 #define SYM_IMPORT (1<<6)
85 #define SYM_ABSOLUTE (1<<4)
87 struct Section {
88 struct SAA *data;
89 uint32_t datalen, size, len;
90 int32_t index;
91 struct Piece *head, *last, **tail;
94 static char as86_module[FILENAME_MAX];
96 static struct Section stext, sdata;
97 static uint32_t bsslen;
98 static int32_t bssindex;
100 static struct SAA *syms;
101 static uint32_t nsyms;
103 static struct RAA *bsym;
105 static struct SAA *strs;
106 static uint32_t strslen;
108 static int as86_reloc_size;
110 static void as86_write(void);
111 static void as86_write_section(struct Section *, int);
112 static int as86_add_string(char *name);
113 static void as86_sect_write(struct Section *, const uint8_t *,
114 uint32_t);
116 static void as86_init(void)
118 stext.data = saa_init(1L);
119 stext.datalen = 0L;
120 stext.head = stext.last = NULL;
121 stext.tail = &stext.head;
122 sdata.data = saa_init(1L);
123 sdata.datalen = 0L;
124 sdata.head = sdata.last = NULL;
125 sdata.tail = &sdata.head;
126 bsslen =
127 stext.len = stext.datalen = stext.size =
128 sdata.len = sdata.datalen = sdata.size = 0;
129 stext.index = seg_alloc();
130 sdata.index = seg_alloc();
131 bssindex = seg_alloc();
132 syms = saa_init((int32_t)sizeof(struct Symbol));
133 nsyms = 0;
134 bsym = raa_init();
135 strs = saa_init(1L);
136 strslen = 0;
138 as86_add_string(as86_module);
141 static void as86_cleanup(int debuginfo)
143 struct Piece *p;
145 (void)debuginfo;
147 as86_write();
148 saa_free(stext.data);
149 while (stext.head) {
150 p = stext.head;
151 stext.head = stext.head->next;
152 nasm_free(p);
154 saa_free(sdata.data);
155 while (sdata.head) {
156 p = sdata.head;
157 sdata.head = sdata.head->next;
158 nasm_free(p);
160 saa_free(syms);
161 raa_free(bsym);
162 saa_free(strs);
165 static int32_t as86_section_names(char *name, int pass, int *bits)
168 (void)pass;
171 * Default is 16 bits.
173 if (!name)
174 *bits = 16;
176 if (!name)
177 return stext.index;
179 if (!strcmp(name, ".text"))
180 return stext.index;
181 else if (!strcmp(name, ".data"))
182 return sdata.index;
183 else if (!strcmp(name, ".bss"))
184 return bssindex;
185 else
186 return NO_SEG;
189 static int as86_add_string(char *name)
191 int pos = strslen;
192 int length = strlen(name);
194 saa_wbytes(strs, name, (int32_t)(length + 1));
195 strslen += 1 + length;
197 return pos;
200 static void as86_deflabel(char *name, int32_t segment, int64_t offset,
201 int is_global, char *special)
203 bool is_start = false;
204 struct Symbol *sym;
206 if (special)
207 nasm_error(ERR_NONFATAL, "as86 format does not support any"
208 " special symbol types");
211 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
212 if (strcmp(name, "..start")) {
213 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
214 return;
215 } else {
216 is_start = true;
220 sym = saa_wstruct(syms);
222 sym->strpos = as86_add_string(name);
223 sym->flags = 0;
225 if (is_start)
226 sym->flags = SYM_ENTRY;
228 if (segment == NO_SEG)
229 sym->flags |= SYM_ABSOLUTE, sym->segment = 0;
230 else if (segment == stext.index)
231 sym->segment = SECT_TEXT;
232 else if (segment == sdata.index)
233 sym->segment = SECT_DATA;
234 else if (segment == bssindex)
235 sym->segment = SECT_BSS;
236 else {
237 sym->flags |= SYM_IMPORT;
238 sym->segment = 15;
241 if (is_global == 2)
242 sym->segment = 3; /* already have IMPORT */
244 if (is_global && !(sym->flags & SYM_IMPORT))
245 sym->flags |= SYM_EXPORT;
247 sym->value = offset;
250 * define the references from external-symbol segment numbers
251 * to these symbol records.
253 if (segment != NO_SEG && segment != stext.index &&
254 segment != sdata.index && segment != bssindex)
255 bsym = raa_write(bsym, segment, nsyms);
257 nsyms++;
260 static void as86_add_piece(struct Section *sect, int type, int32_t offset,
261 int32_t segment, int32_t bytes, int relative)
263 struct Piece *p;
265 sect->len += bytes;
267 if (type == 0 && sect->last && sect->last->type == 0) {
268 sect->last->bytes += bytes;
269 return;
272 p = sect->last = *sect->tail = nasm_malloc(sizeof(struct Piece));
273 sect->tail = &p->next;
274 p->next = NULL;
276 p->type = type;
277 p->offset = offset;
278 p->bytes = bytes;
279 p->relative = relative;
281 if (type == 1 && segment == stext.index)
282 p->number = SECT_TEXT;
283 else if (type == 1 && segment == sdata.index)
284 p->number = SECT_DATA;
285 else if (type == 1 && segment == bssindex)
286 p->number = SECT_BSS;
287 else if (type == 1)
288 p->number = raa_read(bsym, segment), p->type = 2;
291 static void as86_out(int32_t segto, const void *data,
292 enum out_type type, uint64_t size,
293 int32_t segment, int32_t wrt)
295 struct Section *s;
296 int32_t offset;
297 uint8_t mydata[4], *p;
299 if (wrt != NO_SEG) {
300 wrt = NO_SEG; /* continue to do _something_ */
301 nasm_error(ERR_NONFATAL, "WRT not supported by as86 output format");
305 * handle absolute-assembly (structure definitions)
307 if (segto == NO_SEG) {
308 if (type != OUT_RESERVE)
309 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
310 " space");
311 return;
314 if (segto == stext.index)
315 s = &stext;
316 else if (segto == sdata.index)
317 s = &sdata;
318 else if (segto == bssindex)
319 s = NULL;
320 else {
321 nasm_error(ERR_WARNING, "attempt to assemble code in"
322 " segment %d: defaulting to `.text'", segto);
323 s = &stext;
326 if (!s && type != OUT_RESERVE) {
327 nasm_error(ERR_WARNING, "attempt to initialize memory in the"
328 " BSS section: ignored");
329 bsslen += realsize(type, size);
330 return;
333 if (type == OUT_RESERVE) {
334 if (s) {
335 nasm_error(ERR_WARNING, "uninitialized space declared in"
336 " %s section: zeroing",
337 (segto == stext.index ? "code" : "data"));
338 as86_sect_write(s, NULL, size);
339 as86_add_piece(s, 0, 0L, 0L, size, 0);
340 } else
341 bsslen += size;
342 } else if (type == OUT_RAWDATA) {
343 if (segment != NO_SEG)
344 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
345 as86_sect_write(s, data, size);
346 as86_add_piece(s, 0, 0L, 0L, size, 0);
347 } else if (type == OUT_ADDRESS) {
348 if (segment != NO_SEG) {
349 if (segment % 2) {
350 nasm_error(ERR_NONFATAL, "as86 format does not support"
351 " segment base references");
352 } else {
353 offset = *(int64_t *)data;
354 as86_add_piece(s, 1, offset, segment, size, 0);
356 } else {
357 p = mydata;
358 WRITELONG(p, *(int64_t *)data);
359 as86_sect_write(s, data, size);
360 as86_add_piece(s, 0, 0L, 0L, size, 0);
362 } else if (type == OUT_REL2ADR) {
363 if (segment == segto)
364 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
365 if (segment != NO_SEG) {
366 if (segment % 2) {
367 nasm_error(ERR_NONFATAL, "as86 format does not support"
368 " segment base references");
369 } else {
370 offset = *(int64_t *)data;
371 as86_add_piece(s, 1, offset - size + 2, segment, 2L,
375 } else if (type == OUT_REL4ADR) {
376 if (segment == segto)
377 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
378 if (segment != NO_SEG) {
379 if (segment % 2) {
380 nasm_error(ERR_NONFATAL, "as86 format does not support"
381 " segment base references");
382 } else {
383 offset = *(int64_t *)data;
384 as86_add_piece(s, 1, offset - size + 4, segment, 4L,
391 static void as86_write(void)
393 uint32_t i;
394 int32_t symlen, seglen, segsize;
397 * First, go through the symbol records working out how big
398 * each will be. Also fix up BSS references at this time, and
399 * set the flags words up completely.
401 symlen = 0;
402 saa_rewind(syms);
403 for (i = 0; i < nsyms; i++) {
404 struct Symbol *sym = saa_rstruct(syms);
405 if (sym->segment == SECT_BSS)
406 sym->segment = SECT_DATA, sym->value += sdata.len;
407 sym->flags |= sym->segment;
408 if (sym->value == 0)
409 sym->flags |= 0 << 14, symlen += 4;
410 else if (sym->value >= 0 && sym->value <= 255)
411 sym->flags |= 1 << 14, symlen += 5;
412 else if (sym->value >= 0 && sym->value <= 65535L)
413 sym->flags |= 2 << 14, symlen += 6;
414 else
415 sym->flags |= 3 << 14, symlen += 8;
419 * Now do the same for the segments, and get the segment size
420 * descriptor word at the same time.
422 seglen = segsize = 0;
423 if ((uint32_t)stext.len > 65535L)
424 segsize |= 0x03000000L, seglen += 4;
425 else
426 segsize |= 0x02000000L, seglen += 2;
427 if ((uint32_t)sdata.len > 65535L)
428 segsize |= 0xC0000000L, seglen += 4;
429 else
430 segsize |= 0x80000000L, seglen += 2;
433 * Emit the as86 header.
435 fwriteint32_t(0x000186A3L, ofile);
436 fputc(0x2A, ofile);
437 fwriteint32_t(27 + symlen + seglen + strslen, ofile); /* header length */
438 fwriteint32_t(stext.len + sdata.len + bsslen, ofile);
439 fwriteint16_t(strslen, ofile);
440 fwriteint16_t(0, ofile); /* class = revision = 0 */
441 fwriteint32_t(0x55555555L, ofile); /* segment max sizes: always this */
442 fwriteint32_t(segsize, ofile); /* segment size descriptors */
443 if (segsize & 0x01000000L)
444 fwriteint32_t(stext.len, ofile);
445 else
446 fwriteint16_t(stext.len, ofile);
447 if (segsize & 0x40000000L)
448 fwriteint32_t(sdata.len + bsslen, ofile);
449 else
450 fwriteint16_t(sdata.len + bsslen, ofile);
451 fwriteint16_t(nsyms, ofile);
454 * Write the symbol table.
456 saa_rewind(syms);
457 for (i = 0; i < nsyms; i++) {
458 struct Symbol *sym = saa_rstruct(syms);
459 fwriteint16_t(sym->strpos, ofile);
460 fwriteint16_t(sym->flags, ofile);
461 switch (sym->flags & (3 << 14)) {
462 case 0 << 14:
463 break;
464 case 1 << 14:
465 fputc(sym->value, ofile);
466 break;
467 case 2 << 14:
468 fwriteint16_t(sym->value, ofile);
469 break;
470 case 3 << 14:
471 fwriteint32_t(sym->value, ofile);
472 break;
477 * Write out the string table.
479 saa_fpwrite(strs, ofile);
482 * Write the program text.
484 as86_reloc_size = -1;
485 as86_write_section(&stext, SECT_TEXT);
486 as86_write_section(&sdata, SECT_DATA);
488 * Append the BSS section to the .data section
490 if (bsslen > 65535L) {
491 fputc(0x13, ofile);
492 fwriteint32_t(bsslen, ofile);
493 } else if (bsslen > 255) {
494 fputc(0x12, ofile);
495 fwriteint16_t(bsslen, ofile);
496 } else if (bsslen) {
497 fputc(0x11, ofile);
498 fputc(bsslen, ofile);
501 fputc(0, ofile); /* termination */
504 static void as86_set_rsize(int size)
506 if (as86_reloc_size != size) {
507 switch (as86_reloc_size = size) {
508 case 1:
509 fputc(0x01, ofile);
510 break;
511 case 2:
512 fputc(0x02, ofile);
513 break;
514 case 4:
515 fputc(0x03, ofile);
516 break;
517 default:
518 nasm_error(ERR_PANIC, "bizarre relocation size %d", size);
519 break;
524 static void as86_write_section(struct Section *sect, int index)
526 struct Piece *p;
527 uint32_t s;
528 int32_t length;
530 fputc(0x20 + index, ofile); /* select the right section */
532 saa_rewind(sect->data);
534 for (p = sect->head; p; p = p->next)
535 switch (p->type) {
536 case 0:
538 * Absolute data. Emit it in chunks of at most 64
539 * bytes.
541 length = p->bytes;
542 do {
543 char buf[64];
544 int32_t tmplen = (length > 64 ? 64 : length);
545 fputc(0x40 | (tmplen & 0x3F), ofile);
546 saa_rnbytes(sect->data, buf, tmplen);
547 fwrite(buf, 1, tmplen, ofile);
548 length -= tmplen;
549 } while (length > 0);
550 break;
551 case 1:
553 * A segment-type relocation. First fix up the BSS.
555 if (p->number == SECT_BSS)
556 p->number = SECT_DATA, p->offset += sdata.len;
557 as86_set_rsize(p->bytes);
558 fputc(0x80 | (p->relative ? 0x20 : 0) | p->number, ofile);
559 if (as86_reloc_size == 2)
560 fwriteint16_t(p->offset, ofile);
561 else
562 fwriteint32_t(p->offset, ofile);
563 break;
564 case 2:
566 * A symbol-type relocation.
568 as86_set_rsize(p->bytes);
569 s = p->offset;
570 if (s > 65535L)
571 s = 3;
572 else if (s > 255)
573 s = 2;
574 else if (s > 0)
575 s = 1;
576 else
577 s = 0;
578 fputc(0xC0 |
579 (p->relative ? 0x20 : 0) |
580 (p->number > 255 ? 0x04 : 0) | s, ofile);
581 if (p->number > 255)
582 fwriteint16_t(p->number, ofile);
583 else
584 fputc(p->number, ofile);
585 switch ((int)s) {
586 case 0:
587 break;
588 case 1:
589 fputc(p->offset, ofile);
590 break;
591 case 2:
592 fwriteint16_t(p->offset, ofile);
593 break;
594 case 3:
595 fwriteint32_t(p->offset, ofile);
596 break;
598 break;
602 static void as86_sect_write(struct Section *sect,
603 const uint8_t *data, uint32_t len)
605 saa_wbytes(sect->data, data, len);
606 sect->datalen += len;
609 static int32_t as86_segbase(int32_t segment)
611 return segment;
614 static void as86_filename(char *inname, char *outname)
616 char *p;
618 if ((p = strrchr(inname, '.')) != NULL) {
619 strncpy(as86_module, inname, p - inname);
620 as86_module[p - inname] = '\0';
621 } else
622 strcpy(as86_module, inname);
624 standard_extension(inname, outname, ".o");
627 extern macros_t as86_stdmac[];
629 struct ofmt of_as86 = {
630 "Linux as86 (bin86 version 0.3) object files",
631 "as86",
633 null_debug_arr,
634 &null_debug_form,
635 as86_stdmac,
636 as86_init,
637 null_setinfo,
638 as86_out,
639 as86_deflabel,
640 as86_section_names,
641 null_sectalign,
642 as86_segbase,
643 null_directive,
644 as86_filename,
645 as86_cleanup
648 #endif /* OF_AS86 */