Fixed distinction between char and int8_t data types.
[nasm.git] / output / outas86.c
blobc2c5e92bfe9c6a300ac3a7e8ee61220e5acbcb88
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 <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <inttypes.h>
16 #include "nasm.h"
17 #include "nasmlib.h"
18 #include "outform.h"
20 #ifdef OF_AS86
22 struct Piece {
23 struct Piece *next;
24 int type; /* 0 = absolute, 1 = seg, 2 = sym */
25 int32_t offset; /* relative offset */
26 int number; /* symbol/segment number (4=bss) */
27 int32_t bytes; /* size of reloc or of absolute data */
28 int relative; /* TRUE or FALSE */
31 struct Symbol {
32 int32_t strpos; /* string table position of name */
33 int flags; /* symbol flags */
34 int segment; /* 4=bss at this point */
35 int32_t value; /* address, or COMMON variable size */
39 * Section IDs - used in Piece.number and Symbol.segment.
41 #define SECT_TEXT 0 /* text section */
42 #define SECT_DATA 3 /* data section */
43 #define SECT_BSS 4 /* bss section */
46 * Flags used in Symbol.flags.
48 #define SYM_ENTRY (1<<8)
49 #define SYM_EXPORT (1<<7)
50 #define SYM_IMPORT (1<<6)
51 #define SYM_ABSOLUTE (1<<4)
53 struct Section {
54 struct SAA *data;
55 uint32_t datalen, size, len;
56 int32_t index;
57 struct Piece *head, *last, **tail;
60 static char as86_module[FILENAME_MAX];
62 static struct Section stext, sdata;
63 static uint32_t bsslen;
64 static int32_t bssindex;
66 static struct SAA *syms;
67 static uint32_t nsyms;
69 static struct RAA *bsym;
71 static struct SAA *strs;
72 static uint32_t strslen;
74 static int as86_reloc_size;
76 static FILE *as86fp;
77 static efunc error;
79 static void as86_write(void);
80 static void as86_write_section(struct Section *, int);
81 static int as86_add_string(char *name);
82 static void as86_sect_write(struct Section *, const uint8_t *,
83 uint32_t);
85 static void as86_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
87 as86fp = fp;
88 error = errfunc;
89 (void)ldef; /* placate optimisers */
90 stext.data = saa_init(1L);
91 stext.datalen = 0L;
92 stext.head = stext.last = NULL;
93 stext.tail = &stext.head;
94 sdata.data = saa_init(1L);
95 sdata.datalen = 0L;
96 sdata.head = sdata.last = NULL;
97 sdata.tail = &sdata.head;
98 bsslen =
99 stext.len = stext.datalen = stext.size =
100 sdata.len = sdata.datalen = sdata.size = 0;
101 stext.index = seg_alloc();
102 sdata.index = seg_alloc();
103 bssindex = seg_alloc();
104 syms = saa_init((int32_t)sizeof(struct Symbol));
105 nsyms = 0;
106 bsym = raa_init();
107 strs = saa_init(1L);
108 strslen = 0;
110 as86_add_string(as86_module);
113 static void as86_cleanup(int debuginfo)
115 struct Piece *p;
117 (void)debuginfo;
119 as86_write();
120 fclose(as86fp);
121 saa_free(stext.data);
122 while (stext.head) {
123 p = stext.head;
124 stext.head = stext.head->next;
125 nasm_free(p);
127 saa_free(sdata.data);
128 while (sdata.head) {
129 p = sdata.head;
130 sdata.head = sdata.head->next;
131 nasm_free(p);
133 saa_free(syms);
134 raa_free(bsym);
135 saa_free(strs);
138 static int32_t as86_section_names(char *name, int pass, int *bits)
141 * Default is 16 bits.
143 if (!name)
144 *bits = 16;
146 if (!name)
147 return stext.index;
149 if (!strcmp(name, ".text"))
150 return stext.index;
151 else if (!strcmp(name, ".data"))
152 return sdata.index;
153 else if (!strcmp(name, ".bss"))
154 return bssindex;
155 else
156 return NO_SEG;
159 static int as86_add_string(char *name)
161 int pos = strslen;
162 int length = strlen(name);
164 saa_wbytes(strs, name, (int32_t)(length + 1));
165 strslen += 1 + length;
167 return pos;
170 static void as86_deflabel(char *name, int32_t segment, int32_t offset,
171 int is_global, char *special)
173 struct Symbol *sym;
175 if (special)
176 error(ERR_NONFATAL, "as86 format does not support any"
177 " special symbol types");
179 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
180 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
181 return;
184 sym = saa_wstruct(syms);
186 sym->strpos = as86_add_string(name);
187 sym->flags = 0;
188 if (segment == NO_SEG)
189 sym->flags |= SYM_ABSOLUTE, sym->segment = 0;
190 else if (segment == stext.index)
191 sym->segment = SECT_TEXT;
192 else if (segment == sdata.index)
193 sym->segment = SECT_DATA;
194 else if (segment == bssindex)
195 sym->segment = SECT_BSS;
196 else {
197 sym->flags |= SYM_IMPORT;
198 sym->segment = 15;
201 if (is_global == 2)
202 sym->segment = 3; /* already have IMPORT */
204 if (is_global && !(sym->flags & SYM_IMPORT))
205 sym->flags |= SYM_EXPORT;
207 sym->value = offset;
210 * define the references from external-symbol segment numbers
211 * to these symbol records.
213 if (segment != NO_SEG && segment != stext.index &&
214 segment != sdata.index && segment != bssindex)
215 bsym = raa_write(bsym, segment, nsyms);
217 nsyms++;
220 static void as86_add_piece(struct Section *sect, int type, int32_t offset,
221 int32_t segment, int32_t bytes, int relative)
223 struct Piece *p;
225 sect->len += bytes;
227 if (type == 0 && sect->last && sect->last->type == 0) {
228 sect->last->bytes += bytes;
229 return;
232 p = sect->last = *sect->tail = nasm_malloc(sizeof(struct Piece));
233 sect->tail = &p->next;
234 p->next = NULL;
236 p->type = type;
237 p->offset = offset;
238 p->bytes = bytes;
239 p->relative = relative;
241 if (type == 1 && segment == stext.index)
242 p->number = SECT_TEXT;
243 else if (type == 1 && segment == sdata.index)
244 p->number = SECT_DATA;
245 else if (type == 1 && segment == bssindex)
246 p->number = SECT_BSS;
247 else if (type == 1)
248 p->number = raa_read(bsym, segment), p->type = 2;
251 static void as86_out(int32_t segto, const void *data, uint32_t type,
252 int32_t segment, int32_t wrt)
254 struct Section *s;
255 int32_t realbytes = type & OUT_SIZMASK;
256 int32_t offset;
257 uint8_t mydata[4], *p;
259 if (wrt != NO_SEG) {
260 wrt = NO_SEG; /* continue to do _something_ */
261 error(ERR_NONFATAL, "WRT not supported by as86 output format");
264 type &= OUT_TYPMASK;
267 * handle absolute-assembly (structure definitions)
269 if (segto == NO_SEG) {
270 if (type != OUT_RESERVE)
271 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
272 " space");
273 return;
276 if (segto == stext.index)
277 s = &stext;
278 else if (segto == sdata.index)
279 s = &sdata;
280 else if (segto == bssindex)
281 s = NULL;
282 else {
283 error(ERR_WARNING, "attempt to assemble code in"
284 " segment %d: defaulting to `.text'", segto);
285 s = &stext;
288 if (!s && type != OUT_RESERVE) {
289 error(ERR_WARNING, "attempt to initialize memory in the"
290 " BSS section: ignored");
291 if (type == OUT_REL2ADR)
292 realbytes = 2;
293 else if (type == OUT_REL4ADR)
294 realbytes = 4;
295 bsslen += realbytes;
296 return;
299 if (type == OUT_RESERVE) {
300 if (s) {
301 error(ERR_WARNING, "uninitialized space declared in"
302 " %s section: zeroing",
303 (segto == stext.index ? "code" : "data"));
304 as86_sect_write(s, NULL, realbytes);
305 as86_add_piece(s, 0, 0L, 0L, realbytes, 0);
306 } else
307 bsslen += realbytes;
308 } else if (type == OUT_RAWDATA) {
309 if (segment != NO_SEG)
310 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
311 as86_sect_write(s, data, realbytes);
312 as86_add_piece(s, 0, 0L, 0L, realbytes, 0);
313 } else if (type == OUT_ADDRESS) {
314 if (segment != NO_SEG) {
315 if (segment % 2) {
316 error(ERR_NONFATAL, "as86 format does not support"
317 " segment base references");
318 } else {
319 offset = *(int32_t *)data;
320 as86_add_piece(s, 1, offset, segment, realbytes, 0);
322 } else {
323 p = mydata;
324 WRITELONG(p, *(int32_t *)data);
325 as86_sect_write(s, data, realbytes);
326 as86_add_piece(s, 0, 0L, 0L, realbytes, 0);
328 } else if (type == OUT_REL2ADR) {
329 if (segment == segto)
330 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
331 if (segment != NO_SEG) {
332 if (segment % 2) {
333 error(ERR_NONFATAL, "as86 format does not support"
334 " segment base references");
335 } else {
336 offset = *(int32_t *)data;
337 as86_add_piece(s, 1, offset - realbytes + 2, segment, 2L,
341 } else if (type == OUT_REL4ADR) {
342 if (segment == segto)
343 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
344 if (segment != NO_SEG) {
345 if (segment % 2) {
346 error(ERR_NONFATAL, "as86 format does not support"
347 " segment base references");
348 } else {
349 offset = *(int32_t *)data;
350 as86_add_piece(s, 1, offset - realbytes + 4, segment, 4L,
357 static void as86_write(void)
359 uint32_t i;
360 int32_t symlen, seglen, segsize;
363 * First, go through the symbol records working out how big
364 * each will be. Also fix up BSS references at this time, and
365 * set the flags words up completely.
367 symlen = 0;
368 saa_rewind(syms);
369 for (i = 0; i < nsyms; i++) {
370 struct Symbol *sym = saa_rstruct(syms);
371 if (sym->segment == SECT_BSS)
372 sym->segment = SECT_DATA, sym->value += sdata.len;
373 sym->flags |= sym->segment;
374 if (sym->value == 0)
375 sym->flags |= 0 << 14, symlen += 4;
376 else if (sym->value >= 0 && sym->value <= 255)
377 sym->flags |= 1 << 14, symlen += 5;
378 else if (sym->value >= 0 && sym->value <= 65535L)
379 sym->flags |= 2 << 14, symlen += 6;
380 else
381 sym->flags |= 3 << 14, symlen += 8;
385 * Now do the same for the segments, and get the segment size
386 * descriptor word at the same time.
388 seglen = segsize = 0;
389 if ((uint32_t)stext.len > 65535L)
390 segsize |= 0x03000000L, seglen += 4;
391 else
392 segsize |= 0x02000000L, seglen += 2;
393 if ((uint32_t)sdata.len > 65535L)
394 segsize |= 0xC0000000L, seglen += 4;
395 else
396 segsize |= 0x80000000L, seglen += 2;
399 * Emit the as86 header.
401 fwriteint32_t(0x000186A3L, as86fp);
402 fputc(0x2A, as86fp);
403 fwriteint32_t(27 + symlen + seglen + strslen, as86fp); /* header length */
404 fwriteint32_t(stext.len + sdata.len + bsslen, as86fp);
405 fwriteint16_t(strslen, as86fp);
406 fwriteint16_t(0, as86fp); /* class = revision = 0 */
407 fwriteint32_t(0x55555555L, as86fp); /* segment max sizes: always this */
408 fwriteint32_t(segsize, as86fp); /* segment size descriptors */
409 if (segsize & 0x01000000L)
410 fwriteint32_t(stext.len, as86fp);
411 else
412 fwriteint16_t(stext.len, as86fp);
413 if (segsize & 0x40000000L)
414 fwriteint32_t(sdata.len + bsslen, as86fp);
415 else
416 fwriteint16_t(sdata.len + bsslen, as86fp);
417 fwriteint16_t(nsyms, as86fp);
420 * Write the symbol table.
422 saa_rewind(syms);
423 for (i = 0; i < nsyms; i++) {
424 struct Symbol *sym = saa_rstruct(syms);
425 fwriteint16_t(sym->strpos, as86fp);
426 fwriteint16_t(sym->flags, as86fp);
427 switch (sym->flags & (3 << 14)) {
428 case 0 << 14:
429 break;
430 case 1 << 14:
431 fputc(sym->value, as86fp);
432 break;
433 case 2 << 14:
434 fwriteint16_t(sym->value, as86fp);
435 break;
436 case 3 << 14:
437 fwriteint32_t(sym->value, as86fp);
438 break;
443 * Write out the string table.
445 saa_fpwrite(strs, as86fp);
448 * Write the program text.
450 as86_reloc_size = -1;
451 as86_write_section(&stext, SECT_TEXT);
452 as86_write_section(&sdata, SECT_DATA);
454 * Append the BSS section to the .data section
456 if (bsslen > 65535L) {
457 fputc(0x13, as86fp);
458 fwriteint32_t(bsslen, as86fp);
459 } else if (bsslen > 255) {
460 fputc(0x12, as86fp);
461 fwriteint16_t(bsslen, as86fp);
462 } else if (bsslen) {
463 fputc(0x11, as86fp);
464 fputc(bsslen, as86fp);
467 fputc(0, as86fp); /* termination */
470 static void as86_set_rsize(int size)
472 if (as86_reloc_size != size) {
473 switch (as86_reloc_size = size) {
474 case 1:
475 fputc(0x01, as86fp);
476 break;
477 case 2:
478 fputc(0x02, as86fp);
479 break;
480 case 4:
481 fputc(0x03, as86fp);
482 break;
483 default:
484 error(ERR_PANIC, "bizarre relocation size %d", size);
489 static void as86_write_section(struct Section *sect, int index)
491 struct Piece *p;
492 uint32_t s;
493 int32_t length;
495 fputc(0x20 + index, as86fp); /* select the right section */
497 saa_rewind(sect->data);
499 for (p = sect->head; p; p = p->next)
500 switch (p->type) {
501 case 0:
503 * Absolute data. Emit it in chunks of at most 64
504 * bytes.
506 length = p->bytes;
507 do {
508 char buf[64];
509 int32_t tmplen = (length > 64 ? 64 : length);
510 fputc(0x40 | (tmplen & 0x3F), as86fp);
511 saa_rnbytes(sect->data, buf, tmplen);
512 fwrite(buf, 1, tmplen, as86fp);
513 length -= tmplen;
514 } while (length > 0);
515 break;
516 case 1:
518 * A segment-type relocation. First fix up the BSS.
520 if (p->number == SECT_BSS)
521 p->number = SECT_DATA, p->offset += sdata.len;
522 as86_set_rsize(p->bytes);
523 fputc(0x80 | (p->relative ? 0x20 : 0) | p->number, as86fp);
524 if (as86_reloc_size == 2)
525 fwriteint16_t(p->offset, as86fp);
526 else
527 fwriteint32_t(p->offset, as86fp);
528 break;
529 case 2:
531 * A symbol-type relocation.
533 as86_set_rsize(p->bytes);
534 s = p->offset;
535 if (s > 65535L)
536 s = 3;
537 else if (s > 255)
538 s = 2;
539 else if (s > 0)
540 s = 1;
541 else
542 s = 0;
543 fputc(0xC0 |
544 (p->relative ? 0x20 : 0) |
545 (p->number > 255 ? 0x04 : 0) | s, as86fp);
546 if (p->number > 255)
547 fwriteint16_t(p->number, as86fp);
548 else
549 fputc(p->number, as86fp);
550 switch ((int)s) {
551 case 0:
552 break;
553 case 1:
554 fputc(p->offset, as86fp);
555 break;
556 case 2:
557 fwriteint16_t(p->offset, as86fp);
558 break;
559 case 3:
560 fwriteint32_t(p->offset, as86fp);
561 break;
563 break;
567 static void as86_sect_write(struct Section *sect,
568 const uint8_t *data, uint32_t len)
570 saa_wbytes(sect->data, data, len);
571 sect->datalen += len;
574 static int32_t as86_segbase(int32_t segment)
576 return segment;
579 static int as86_directive(char *directive, char *value, int pass)
581 return 0;
584 static void as86_filename(char *inname, char *outname, efunc error)
586 char *p;
588 if ((p = strrchr(inname, '.')) != NULL) {
589 strncpy(as86_module, inname, p - inname);
590 as86_module[p - inname] = '\0';
591 } else
592 strcpy(as86_module, inname);
594 standard_extension(inname, outname, ".o", error);
597 static const char *as86_stdmac[] = {
598 "%define __SECT__ [section .text]",
599 "%macro __NASM_CDecl__ 1",
600 "%endmacro",
601 NULL
604 static int as86_set_info(enum geninfo type, char **val)
606 return 0;
608 void as86_linenumber(char *name, int32_t segment, int32_t offset, int is_main,
609 int lineno)
612 struct ofmt of_as86 = {
613 "Linux as86 (bin86 version 0.3) object files",
614 "as86",
615 NULL,
616 null_debug_arr,
617 &null_debug_form,
618 as86_stdmac,
619 as86_init,
620 as86_set_info,
621 as86_out,
622 as86_deflabel,
623 as86_section_names,
624 as86_segbase,
625 as86_directive,
626 as86_filename,
627 as86_cleanup
630 #endif /* OF_AS86 */