phash.ph: yet another attempt at getting Perl to behave, arithmetically
[nasm/avx512.git] / output / outas86.c
blob135ddda348d675a8da6c92ddf1998079a8c81d6a
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 (void)eval;
91 stext.data = saa_init(1L);
92 stext.datalen = 0L;
93 stext.head = stext.last = NULL;
94 stext.tail = &stext.head;
95 sdata.data = saa_init(1L);
96 sdata.datalen = 0L;
97 sdata.head = sdata.last = NULL;
98 sdata.tail = &sdata.head;
99 bsslen =
100 stext.len = stext.datalen = stext.size =
101 sdata.len = sdata.datalen = sdata.size = 0;
102 stext.index = seg_alloc();
103 sdata.index = seg_alloc();
104 bssindex = seg_alloc();
105 syms = saa_init((int32_t)sizeof(struct Symbol));
106 nsyms = 0;
107 bsym = raa_init();
108 strs = saa_init(1L);
109 strslen = 0;
111 as86_add_string(as86_module);
114 static void as86_cleanup(int debuginfo)
116 struct Piece *p;
118 (void)debuginfo;
120 as86_write();
121 fclose(as86fp);
122 saa_free(stext.data);
123 while (stext.head) {
124 p = stext.head;
125 stext.head = stext.head->next;
126 nasm_free(p);
128 saa_free(sdata.data);
129 while (sdata.head) {
130 p = sdata.head;
131 sdata.head = sdata.head->next;
132 nasm_free(p);
134 saa_free(syms);
135 raa_free(bsym);
136 saa_free(strs);
139 static int32_t as86_section_names(char *name, int pass, int *bits)
142 (void)pass;
145 * Default is 16 bits.
147 if (!name)
148 *bits = 16;
150 if (!name)
151 return stext.index;
153 if (!strcmp(name, ".text"))
154 return stext.index;
155 else if (!strcmp(name, ".data"))
156 return sdata.index;
157 else if (!strcmp(name, ".bss"))
158 return bssindex;
159 else
160 return NO_SEG;
163 static int as86_add_string(char *name)
165 int pos = strslen;
166 int length = strlen(name);
168 saa_wbytes(strs, name, (int32_t)(length + 1));
169 strslen += 1 + length;
171 return pos;
174 static void as86_deflabel(char *name, int32_t segment, int32_t offset,
175 int is_global, char *special)
177 struct Symbol *sym;
179 if (special)
180 error(ERR_NONFATAL, "as86 format does not support any"
181 " special symbol types");
183 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
184 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
185 return;
188 sym = saa_wstruct(syms);
190 sym->strpos = as86_add_string(name);
191 sym->flags = 0;
192 if (segment == NO_SEG)
193 sym->flags |= SYM_ABSOLUTE, sym->segment = 0;
194 else if (segment == stext.index)
195 sym->segment = SECT_TEXT;
196 else if (segment == sdata.index)
197 sym->segment = SECT_DATA;
198 else if (segment == bssindex)
199 sym->segment = SECT_BSS;
200 else {
201 sym->flags |= SYM_IMPORT;
202 sym->segment = 15;
205 if (is_global == 2)
206 sym->segment = 3; /* already have IMPORT */
208 if (is_global && !(sym->flags & SYM_IMPORT))
209 sym->flags |= SYM_EXPORT;
211 sym->value = offset;
214 * define the references from external-symbol segment numbers
215 * to these symbol records.
217 if (segment != NO_SEG && segment != stext.index &&
218 segment != sdata.index && segment != bssindex)
219 bsym = raa_write(bsym, segment, nsyms);
221 nsyms++;
224 static void as86_add_piece(struct Section *sect, int type, int32_t offset,
225 int32_t segment, int32_t bytes, int relative)
227 struct Piece *p;
229 sect->len += bytes;
231 if (type == 0 && sect->last && sect->last->type == 0) {
232 sect->last->bytes += bytes;
233 return;
236 p = sect->last = *sect->tail = nasm_malloc(sizeof(struct Piece));
237 sect->tail = &p->next;
238 p->next = NULL;
240 p->type = type;
241 p->offset = offset;
242 p->bytes = bytes;
243 p->relative = relative;
245 if (type == 1 && segment == stext.index)
246 p->number = SECT_TEXT;
247 else if (type == 1 && segment == sdata.index)
248 p->number = SECT_DATA;
249 else if (type == 1 && segment == bssindex)
250 p->number = SECT_BSS;
251 else if (type == 1)
252 p->number = raa_read(bsym, segment), p->type = 2;
255 static void as86_out(int32_t segto, const void *data, uint32_t type,
256 int32_t segment, int32_t wrt)
258 struct Section *s;
259 int32_t realbytes = type & OUT_SIZMASK;
260 int32_t offset;
261 uint8_t mydata[4], *p;
263 if (wrt != NO_SEG) {
264 wrt = NO_SEG; /* continue to do _something_ */
265 error(ERR_NONFATAL, "WRT not supported by as86 output format");
268 type &= OUT_TYPMASK;
271 * handle absolute-assembly (structure definitions)
273 if (segto == NO_SEG) {
274 if (type != OUT_RESERVE)
275 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
276 " space");
277 return;
280 if (segto == stext.index)
281 s = &stext;
282 else if (segto == sdata.index)
283 s = &sdata;
284 else if (segto == bssindex)
285 s = NULL;
286 else {
287 error(ERR_WARNING, "attempt to assemble code in"
288 " segment %d: defaulting to `.text'", segto);
289 s = &stext;
292 if (!s && type != OUT_RESERVE) {
293 error(ERR_WARNING, "attempt to initialize memory in the"
294 " BSS section: ignored");
295 if (type == OUT_REL2ADR)
296 realbytes = 2;
297 else if (type == OUT_REL4ADR)
298 realbytes = 4;
299 bsslen += realbytes;
300 return;
303 if (type == OUT_RESERVE) {
304 if (s) {
305 error(ERR_WARNING, "uninitialized space declared in"
306 " %s section: zeroing",
307 (segto == stext.index ? "code" : "data"));
308 as86_sect_write(s, NULL, realbytes);
309 as86_add_piece(s, 0, 0L, 0L, realbytes, 0);
310 } else
311 bsslen += realbytes;
312 } else if (type == OUT_RAWDATA) {
313 if (segment != NO_SEG)
314 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
315 as86_sect_write(s, data, realbytes);
316 as86_add_piece(s, 0, 0L, 0L, realbytes, 0);
317 } else if (type == OUT_ADDRESS) {
318 if (segment != NO_SEG) {
319 if (segment % 2) {
320 error(ERR_NONFATAL, "as86 format does not support"
321 " segment base references");
322 } else {
323 offset = *(int32_t *)data;
324 as86_add_piece(s, 1, offset, segment, realbytes, 0);
326 } else {
327 p = mydata;
328 WRITELONG(p, *(int32_t *)data);
329 as86_sect_write(s, data, realbytes);
330 as86_add_piece(s, 0, 0L, 0L, realbytes, 0);
332 } else if (type == OUT_REL2ADR) {
333 if (segment == segto)
334 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
335 if (segment != NO_SEG) {
336 if (segment % 2) {
337 error(ERR_NONFATAL, "as86 format does not support"
338 " segment base references");
339 } else {
340 offset = *(int32_t *)data;
341 as86_add_piece(s, 1, offset - realbytes + 2, segment, 2L,
345 } else if (type == OUT_REL4ADR) {
346 if (segment == segto)
347 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
348 if (segment != NO_SEG) {
349 if (segment % 2) {
350 error(ERR_NONFATAL, "as86 format does not support"
351 " segment base references");
352 } else {
353 offset = *(int32_t *)data;
354 as86_add_piece(s, 1, offset - realbytes + 4, segment, 4L,
361 static void as86_write(void)
363 uint32_t i;
364 int32_t symlen, seglen, segsize;
367 * First, go through the symbol records working out how big
368 * each will be. Also fix up BSS references at this time, and
369 * set the flags words up completely.
371 symlen = 0;
372 saa_rewind(syms);
373 for (i = 0; i < nsyms; i++) {
374 struct Symbol *sym = saa_rstruct(syms);
375 if (sym->segment == SECT_BSS)
376 sym->segment = SECT_DATA, sym->value += sdata.len;
377 sym->flags |= sym->segment;
378 if (sym->value == 0)
379 sym->flags |= 0 << 14, symlen += 4;
380 else if (sym->value >= 0 && sym->value <= 255)
381 sym->flags |= 1 << 14, symlen += 5;
382 else if (sym->value >= 0 && sym->value <= 65535L)
383 sym->flags |= 2 << 14, symlen += 6;
384 else
385 sym->flags |= 3 << 14, symlen += 8;
389 * Now do the same for the segments, and get the segment size
390 * descriptor word at the same time.
392 seglen = segsize = 0;
393 if ((uint32_t)stext.len > 65535L)
394 segsize |= 0x03000000L, seglen += 4;
395 else
396 segsize |= 0x02000000L, seglen += 2;
397 if ((uint32_t)sdata.len > 65535L)
398 segsize |= 0xC0000000L, seglen += 4;
399 else
400 segsize |= 0x80000000L, seglen += 2;
403 * Emit the as86 header.
405 fwriteint32_t(0x000186A3L, as86fp);
406 fputc(0x2A, as86fp);
407 fwriteint32_t(27 + symlen + seglen + strslen, as86fp); /* header length */
408 fwriteint32_t(stext.len + sdata.len + bsslen, as86fp);
409 fwriteint16_t(strslen, as86fp);
410 fwriteint16_t(0, as86fp); /* class = revision = 0 */
411 fwriteint32_t(0x55555555L, as86fp); /* segment max sizes: always this */
412 fwriteint32_t(segsize, as86fp); /* segment size descriptors */
413 if (segsize & 0x01000000L)
414 fwriteint32_t(stext.len, as86fp);
415 else
416 fwriteint16_t(stext.len, as86fp);
417 if (segsize & 0x40000000L)
418 fwriteint32_t(sdata.len + bsslen, as86fp);
419 else
420 fwriteint16_t(sdata.len + bsslen, as86fp);
421 fwriteint16_t(nsyms, as86fp);
424 * Write the symbol table.
426 saa_rewind(syms);
427 for (i = 0; i < nsyms; i++) {
428 struct Symbol *sym = saa_rstruct(syms);
429 fwriteint16_t(sym->strpos, as86fp);
430 fwriteint16_t(sym->flags, as86fp);
431 switch (sym->flags & (3 << 14)) {
432 case 0 << 14:
433 break;
434 case 1 << 14:
435 fputc(sym->value, as86fp);
436 break;
437 case 2 << 14:
438 fwriteint16_t(sym->value, as86fp);
439 break;
440 case 3 << 14:
441 fwriteint32_t(sym->value, as86fp);
442 break;
447 * Write out the string table.
449 saa_fpwrite(strs, as86fp);
452 * Write the program text.
454 as86_reloc_size = -1;
455 as86_write_section(&stext, SECT_TEXT);
456 as86_write_section(&sdata, SECT_DATA);
458 * Append the BSS section to the .data section
460 if (bsslen > 65535L) {
461 fputc(0x13, as86fp);
462 fwriteint32_t(bsslen, as86fp);
463 } else if (bsslen > 255) {
464 fputc(0x12, as86fp);
465 fwriteint16_t(bsslen, as86fp);
466 } else if (bsslen) {
467 fputc(0x11, as86fp);
468 fputc(bsslen, as86fp);
471 fputc(0, as86fp); /* termination */
474 static void as86_set_rsize(int size)
476 if (as86_reloc_size != size) {
477 switch (as86_reloc_size = size) {
478 case 1:
479 fputc(0x01, as86fp);
480 break;
481 case 2:
482 fputc(0x02, as86fp);
483 break;
484 case 4:
485 fputc(0x03, as86fp);
486 break;
487 default:
488 error(ERR_PANIC, "bizarre relocation size %d", size);
493 static void as86_write_section(struct Section *sect, int index)
495 struct Piece *p;
496 uint32_t s;
497 int32_t length;
499 fputc(0x20 + index, as86fp); /* select the right section */
501 saa_rewind(sect->data);
503 for (p = sect->head; p; p = p->next)
504 switch (p->type) {
505 case 0:
507 * Absolute data. Emit it in chunks of at most 64
508 * bytes.
510 length = p->bytes;
511 do {
512 char buf[64];
513 int32_t tmplen = (length > 64 ? 64 : length);
514 fputc(0x40 | (tmplen & 0x3F), as86fp);
515 saa_rnbytes(sect->data, buf, tmplen);
516 fwrite(buf, 1, tmplen, as86fp);
517 length -= tmplen;
518 } while (length > 0);
519 break;
520 case 1:
522 * A segment-type relocation. First fix up the BSS.
524 if (p->number == SECT_BSS)
525 p->number = SECT_DATA, p->offset += sdata.len;
526 as86_set_rsize(p->bytes);
527 fputc(0x80 | (p->relative ? 0x20 : 0) | p->number, as86fp);
528 if (as86_reloc_size == 2)
529 fwriteint16_t(p->offset, as86fp);
530 else
531 fwriteint32_t(p->offset, as86fp);
532 break;
533 case 2:
535 * A symbol-type relocation.
537 as86_set_rsize(p->bytes);
538 s = p->offset;
539 if (s > 65535L)
540 s = 3;
541 else if (s > 255)
542 s = 2;
543 else if (s > 0)
544 s = 1;
545 else
546 s = 0;
547 fputc(0xC0 |
548 (p->relative ? 0x20 : 0) |
549 (p->number > 255 ? 0x04 : 0) | s, as86fp);
550 if (p->number > 255)
551 fwriteint16_t(p->number, as86fp);
552 else
553 fputc(p->number, as86fp);
554 switch ((int)s) {
555 case 0:
556 break;
557 case 1:
558 fputc(p->offset, as86fp);
559 break;
560 case 2:
561 fwriteint16_t(p->offset, as86fp);
562 break;
563 case 3:
564 fwriteint32_t(p->offset, as86fp);
565 break;
567 break;
571 static void as86_sect_write(struct Section *sect,
572 const uint8_t *data, uint32_t len)
574 saa_wbytes(sect->data, data, len);
575 sect->datalen += len;
578 static int32_t as86_segbase(int32_t segment)
580 return segment;
583 static int as86_directive(char *directive, char *value, int pass)
585 (void)directive;
586 (void)value;
587 (void)pass;
588 return 0;
591 static void as86_filename(char *inname, char *outname, efunc error)
593 char *p;
595 if ((p = strrchr(inname, '.')) != NULL) {
596 strncpy(as86_module, inname, p - inname);
597 as86_module[p - inname] = '\0';
598 } else
599 strcpy(as86_module, inname);
601 standard_extension(inname, outname, ".o", error);
604 static const char *as86_stdmac[] = {
605 "%define __SECT__ [section .text]",
606 "%macro __NASM_CDecl__ 1",
607 "%endmacro",
608 NULL
611 static int as86_set_info(enum geninfo type, char **val)
613 (void)type;
614 (void)val;
615 return 0;
617 void as86_linenumber(char *name, int32_t segment, int32_t offset, int is_main,
618 int lineno)
620 (void)name;
621 (void)segment;
622 (void)offset;
623 (void)is_main;
624 (void)lineno;
626 struct ofmt of_as86 = {
627 "Linux as86 (bin86 version 0.3) object files",
628 "as86",
629 NULL,
630 null_debug_arr,
631 &null_debug_form,
632 as86_stdmac,
633 as86_init,
634 as86_set_info,
635 as86_out,
636 as86_deflabel,
637 as86_section_names,
638 as86_segbase,
639 as86_directive,
640 as86_filename,
641 as86_cleanup
644 #endif /* OF_AS86 */