Makefiles: run "make alldeps"
[nasm.git] / output / outas86.c
blobb69c1d34f897e7f4bcddea4d47672e3fe6bd87bf
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2013 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(void)
143 struct Piece *p;
145 as86_write();
146 saa_free(stext.data);
147 while (stext.head) {
148 p = stext.head;
149 stext.head = stext.head->next;
150 nasm_free(p);
152 saa_free(sdata.data);
153 while (sdata.head) {
154 p = sdata.head;
155 sdata.head = sdata.head->next;
156 nasm_free(p);
158 saa_free(syms);
159 raa_free(bsym);
160 saa_free(strs);
163 static int32_t as86_section_names(char *name, int pass, int *bits)
166 (void)pass;
169 * Default is 16 bits.
171 if (!name)
172 *bits = 16;
174 if (!name)
175 return stext.index;
177 if (!strcmp(name, ".text"))
178 return stext.index;
179 else if (!strcmp(name, ".data"))
180 return sdata.index;
181 else if (!strcmp(name, ".bss"))
182 return bssindex;
183 else
184 return NO_SEG;
187 static int as86_add_string(char *name)
189 int pos = strslen;
190 int length = strlen(name);
192 saa_wbytes(strs, name, (int32_t)(length + 1));
193 strslen += 1 + length;
195 return pos;
198 static void as86_deflabel(char *name, int32_t segment, int64_t offset,
199 int is_global, char *special)
201 bool is_start = false;
202 struct Symbol *sym;
204 if (special)
205 nasm_error(ERR_NONFATAL, "as86 format does not support any"
206 " special symbol types");
209 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
210 if (strcmp(name, "..start")) {
211 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
212 return;
213 } else {
214 is_start = true;
218 sym = saa_wstruct(syms);
220 sym->strpos = as86_add_string(name);
221 sym->flags = 0;
223 if (is_start)
224 sym->flags = SYM_ENTRY;
226 if (segment == NO_SEG)
227 sym->flags |= SYM_ABSOLUTE, sym->segment = 0;
228 else if (segment == stext.index)
229 sym->segment = SECT_TEXT;
230 else if (segment == sdata.index)
231 sym->segment = SECT_DATA;
232 else if (segment == bssindex)
233 sym->segment = SECT_BSS;
234 else {
235 sym->flags |= SYM_IMPORT;
236 sym->segment = 15;
239 if (is_global == 2)
240 sym->segment = 3; /* already have IMPORT */
242 if (is_global && !(sym->flags & SYM_IMPORT))
243 sym->flags |= SYM_EXPORT;
245 sym->value = offset;
248 * define the references from external-symbol segment numbers
249 * to these symbol records.
251 if (segment != NO_SEG && segment != stext.index &&
252 segment != sdata.index && segment != bssindex)
253 bsym = raa_write(bsym, segment, nsyms);
255 nsyms++;
258 static void as86_add_piece(struct Section *sect, int type, int32_t offset,
259 int32_t segment, int32_t bytes, int relative)
261 struct Piece *p;
263 sect->len += bytes;
265 if (type == 0 && sect->last && sect->last->type == 0) {
266 sect->last->bytes += bytes;
267 return;
270 p = sect->last = *sect->tail = nasm_malloc(sizeof(struct Piece));
271 sect->tail = &p->next;
272 p->next = NULL;
274 p->type = type;
275 p->offset = offset;
276 p->bytes = bytes;
277 p->relative = relative;
279 if (type == 1 && segment == stext.index)
280 p->number = SECT_TEXT;
281 else if (type == 1 && segment == sdata.index)
282 p->number = SECT_DATA;
283 else if (type == 1 && segment == bssindex)
284 p->number = SECT_BSS;
285 else if (type == 1)
286 p->number = raa_read(bsym, segment), p->type = 2;
289 static void as86_out(int32_t segto, const void *data,
290 enum out_type type, uint64_t size,
291 int32_t segment, int32_t wrt)
293 struct Section *s;
294 int32_t offset;
295 uint8_t mydata[4], *p;
297 if (wrt != NO_SEG) {
298 wrt = NO_SEG; /* continue to do _something_ */
299 nasm_error(ERR_NONFATAL, "WRT not supported by as86 output format");
303 * handle absolute-assembly (structure definitions)
305 if (segto == NO_SEG) {
306 if (type != OUT_RESERVE)
307 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
308 " space");
309 return;
312 if (segto == stext.index)
313 s = &stext;
314 else if (segto == sdata.index)
315 s = &sdata;
316 else if (segto == bssindex)
317 s = NULL;
318 else {
319 nasm_error(ERR_WARNING, "attempt to assemble code in"
320 " segment %d: defaulting to `.text'", segto);
321 s = &stext;
324 if (!s && type != OUT_RESERVE) {
325 nasm_error(ERR_WARNING, "attempt to initialize memory in the"
326 " BSS section: ignored");
327 bsslen += realsize(type, size);
328 return;
331 memset(mydata, 0, sizeof(mydata));
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_panic(0, "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 int asize = abs((int)size);
349 if (segment != NO_SEG) {
350 if (segment % 2) {
351 nasm_error(ERR_NONFATAL, "as86 format does not support"
352 " segment base references");
353 } else {
354 offset = *(int64_t *)data;
355 as86_add_piece(s, 1, offset, segment, asize, 0);
357 } else {
358 p = mydata;
359 WRITELONG(p, *(int64_t *)data);
360 as86_sect_write(s, data, asize);
361 as86_add_piece(s, 0, 0L, 0L, asize, 0);
363 } else if (type == OUT_REL2ADR) {
364 if (segment == segto)
365 nasm_panic(0, "intra-segment OUT_REL2ADR");
366 if (segment != NO_SEG) {
367 if (segment % 2) {
368 nasm_error(ERR_NONFATAL, "as86 format does not support"
369 " segment base references");
370 } else {
371 offset = *(int64_t *)data;
372 as86_add_piece(s, 1, offset - size + 2, segment, 2L,
376 } else if (type == OUT_REL4ADR) {
377 if (segment == segto)
378 nasm_panic(0, "intra-segment OUT_REL4ADR");
379 if (segment != NO_SEG) {
380 if (segment % 2) {
381 nasm_error(ERR_NONFATAL, "as86 format does not support"
382 " segment base references");
383 } else {
384 offset = *(int64_t *)data;
385 as86_add_piece(s, 1, offset - size + 4, segment, 4L,
392 static void as86_write(void)
394 uint32_t i;
395 int32_t symlen, seglen, segsize;
398 * First, go through the symbol records working out how big
399 * each will be. Also fix up BSS references at this time, and
400 * set the flags words up completely.
402 symlen = 0;
403 saa_rewind(syms);
404 for (i = 0; i < nsyms; i++) {
405 struct Symbol *sym = saa_rstruct(syms);
406 if (sym->segment == SECT_BSS)
407 sym->segment = SECT_DATA, sym->value += sdata.len;
408 sym->flags |= sym->segment;
409 if (sym->value == 0)
410 sym->flags |= 0 << 14, symlen += 4;
411 else if (sym->value >= 0 && sym->value <= 255)
412 sym->flags |= 1 << 14, symlen += 5;
413 else if (sym->value >= 0 && sym->value <= 65535L)
414 sym->flags |= 2 << 14, symlen += 6;
415 else
416 sym->flags |= 3 << 14, symlen += 8;
420 * Now do the same for the segments, and get the segment size
421 * descriptor word at the same time.
423 seglen = segsize = 0;
424 if ((uint32_t)stext.len > 65535L)
425 segsize |= 0x03000000L, seglen += 4;
426 else
427 segsize |= 0x02000000L, seglen += 2;
428 if ((uint32_t)sdata.len > 65535L)
429 segsize |= 0xC0000000L, seglen += 4;
430 else
431 segsize |= 0x80000000L, seglen += 2;
434 * Emit the as86 header.
436 fwriteint32_t(0x000186A3L, ofile);
437 fputc(0x2A, ofile);
438 fwriteint32_t(27 + symlen + seglen + strslen, ofile); /* header length */
439 fwriteint32_t(stext.len + sdata.len + bsslen, ofile);
440 fwriteint16_t(strslen, ofile);
441 fwriteint16_t(0, ofile); /* class = revision = 0 */
442 fwriteint32_t(0x55555555L, ofile); /* segment max sizes: always this */
443 fwriteint32_t(segsize, ofile); /* segment size descriptors */
444 if (segsize & 0x01000000L)
445 fwriteint32_t(stext.len, ofile);
446 else
447 fwriteint16_t(stext.len, ofile);
448 if (segsize & 0x40000000L)
449 fwriteint32_t(sdata.len + bsslen, ofile);
450 else
451 fwriteint16_t(sdata.len + bsslen, ofile);
452 fwriteint16_t(nsyms, ofile);
455 * Write the symbol table.
457 saa_rewind(syms);
458 for (i = 0; i < nsyms; i++) {
459 struct Symbol *sym = saa_rstruct(syms);
460 fwriteint16_t(sym->strpos, ofile);
461 fwriteint16_t(sym->flags, ofile);
462 switch (sym->flags & (3 << 14)) {
463 case 0 << 14:
464 break;
465 case 1 << 14:
466 fputc(sym->value, ofile);
467 break;
468 case 2 << 14:
469 fwriteint16_t(sym->value, ofile);
470 break;
471 case 3 << 14:
472 fwriteint32_t(sym->value, ofile);
473 break;
478 * Write out the string table.
480 saa_fpwrite(strs, ofile);
483 * Write the program text.
485 as86_reloc_size = -1;
486 as86_write_section(&stext, SECT_TEXT);
487 as86_write_section(&sdata, SECT_DATA);
489 * Append the BSS section to the .data section
491 if (bsslen > 65535L) {
492 fputc(0x13, ofile);
493 fwriteint32_t(bsslen, ofile);
494 } else if (bsslen > 255) {
495 fputc(0x12, ofile);
496 fwriteint16_t(bsslen, ofile);
497 } else if (bsslen) {
498 fputc(0x11, ofile);
499 fputc(bsslen, ofile);
502 fputc(0, ofile); /* termination */
505 static void as86_set_rsize(int size)
507 if (as86_reloc_size != size) {
508 switch (as86_reloc_size = size) {
509 case 1:
510 fputc(0x01, ofile);
511 break;
512 case 2:
513 fputc(0x02, ofile);
514 break;
515 case 4:
516 fputc(0x03, ofile);
517 break;
518 default:
519 nasm_panic(0, "bizarre relocation size %d", size);
520 break;
525 static void as86_write_section(struct Section *sect, int index)
527 struct Piece *p;
528 uint32_t s;
529 int32_t length;
531 fputc(0x20 + index, ofile); /* select the right section */
533 saa_rewind(sect->data);
535 for (p = sect->head; p; p = p->next)
536 switch (p->type) {
537 case 0:
539 * Absolute data. Emit it in chunks of at most 64
540 * bytes.
542 length = p->bytes;
543 do {
544 char buf[64];
545 int32_t tmplen = (length > 64 ? 64 : length);
546 fputc(0x40 | (tmplen & 0x3F), ofile);
547 saa_rnbytes(sect->data, buf, tmplen);
548 nasm_write(buf, tmplen, ofile);
549 length -= tmplen;
550 } while (length > 0);
551 break;
552 case 1:
554 * A segment-type relocation. First fix up the BSS.
556 if (p->number == SECT_BSS)
557 p->number = SECT_DATA, p->offset += sdata.len;
558 as86_set_rsize(p->bytes);
559 fputc(0x80 | (p->relative ? 0x20 : 0) | p->number, ofile);
560 if (as86_reloc_size == 2)
561 fwriteint16_t(p->offset, ofile);
562 else
563 fwriteint32_t(p->offset, ofile);
564 break;
565 case 2:
567 * A symbol-type relocation.
569 as86_set_rsize(p->bytes);
570 s = p->offset;
571 if (s > 65535L)
572 s = 3;
573 else if (s > 255)
574 s = 2;
575 else if (s > 0)
576 s = 1;
577 else
578 s = 0;
579 fputc(0xC0 |
580 (p->relative ? 0x20 : 0) |
581 (p->number > 255 ? 0x04 : 0) | s, ofile);
582 if (p->number > 255)
583 fwriteint16_t(p->number, ofile);
584 else
585 fputc(p->number, ofile);
586 switch ((int)s) {
587 case 0:
588 break;
589 case 1:
590 fputc(p->offset, ofile);
591 break;
592 case 2:
593 fwriteint16_t(p->offset, ofile);
594 break;
595 case 3:
596 fwriteint32_t(p->offset, ofile);
597 break;
599 break;
603 static void as86_sect_write(struct Section *sect,
604 const uint8_t *data, uint32_t len)
606 saa_wbytes(sect->data, data, len);
607 sect->datalen += len;
610 static int32_t as86_segbase(int32_t segment)
612 return segment;
615 static void as86_filename(char *inname, char *outname)
617 char *p;
619 if ((p = strrchr(inname, '.')) != NULL) {
620 strncpy(as86_module, inname, p - inname);
621 as86_module[p - inname] = '\0';
622 } else
623 strcpy(as86_module, inname);
625 standard_extension(inname, outname, ".o");
628 extern macros_t as86_stdmac[];
630 struct ofmt of_as86 = {
631 "Linux as86 (bin86 version 0.3) object files",
632 "as86",
635 null_debug_arr,
636 &null_debug_form,
637 as86_stdmac,
638 as86_init,
639 null_setinfo,
640 as86_out,
641 as86_deflabel,
642 as86_section_names,
643 null_sectalign,
644 as86_segbase,
645 null_directive,
646 as86_filename,
647 as86_cleanup
650 #endif /* OF_AS86 */