9bc0379af4e2d96d16716bc0f9ebe65b26ba348f
[platform/upstream/nasm.git] / outas86.c
1 /* outas86.c    output routines for the Netwide Assembler to produce
2  *              Linux as86 (bin86-0.3) object files
3  *
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  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14
15 #include "nasm.h"
16 #include "nasmlib.h"
17 #include "outform.h"
18
19 #ifdef OF_AS86
20
21 struct Piece {
22     struct Piece *next;
23     int type;                          /* 0 = absolute, 1 = seg, 2 = sym */
24     long offset;                       /* relative offset */
25     int number;                        /* symbol/segment number (4=bss) */
26     long bytes;                        /* size of reloc or of absolute data */
27     int relative;                      /* TRUE or FALSE */
28 };
29
30 struct Symbol {
31     long strpos;                       /* string table position of name */
32     int flags;                         /* symbol flags */
33     int segment;                       /* 4=bss at this point */
34     long value;                        /* address, or COMMON variable size */
35 };
36
37 /*
38  * Section IDs - used in Piece.number and Symbol.segment.
39  */
40 #define SECT_TEXT 0                    /* text section */
41 #define SECT_DATA 3                    /* data section */
42 #define SECT_BSS 4                     /* bss section */
43
44 /*
45  * Flags used in Symbol.flags.
46  */
47 #define SYM_ENTRY (1<<8)
48 #define SYM_EXPORT (1<<7)
49 #define SYM_IMPORT (1<<6)
50 #define SYM_ABSOLUTE (1<<4)
51
52 struct Section {
53     struct SAA *data;
54     unsigned long datalen, size, len;
55     long index;
56     struct Piece *head, *last, **tail;
57 };
58
59 static char as86_module[FILENAME_MAX];
60
61 static struct Section stext, sdata;
62 static unsigned long bsslen;
63 static long bssindex;
64
65 static struct SAA *syms;
66 static unsigned long nsyms;
67
68 static struct RAA *bsym;
69
70 static struct SAA *strs;
71 static unsigned long strslen;
72
73 static int as86_reloc_size;
74
75 static FILE *as86fp;
76 static efunc error;
77
78 static void as86_write(void);
79 static void as86_write_section (struct Section *, int);
80 static int as86_add_string (char *name);
81 static void as86_sect_write(struct Section *, unsigned char *, unsigned long);
82
83 static void as86_init(FILE *fp, efunc errfunc, ldfunc ldef) {
84     as86fp = fp;
85     error = errfunc;
86     (void) ldef;                       /* placate optimisers */
87     stext.data = saa_init(1L); stext.datalen = 0L;
88     stext.head = stext.last = NULL;
89     stext.tail = &stext.head;
90     sdata.data = saa_init(1L); sdata.datalen = 0L;
91     sdata.head = sdata.last = NULL;
92     sdata.tail = &sdata.head;
93     bsslen =
94         stext.len = stext.datalen = stext.size =
95         sdata.len = sdata.datalen = sdata.size = 0;
96     stext.index = seg_alloc();
97     sdata.index = seg_alloc();
98     bssindex = seg_alloc();
99     syms = saa_init((long)sizeof(struct Symbol));
100     nsyms = 0;
101     bsym = raa_init();
102     strs = saa_init(1L);
103     strslen = 0;
104
105     as86_add_string (as86_module);
106 }
107
108 static void as86_cleanup(void) {
109     struct Piece *p;
110
111     as86_write();
112     fclose (as86fp);
113     saa_free (stext.data);
114     while (stext.head) {
115         p = stext.head;
116         stext.head = stext.head->next;
117         nasm_free (p);
118     }
119     saa_free (sdata.data);
120     while (sdata.head) {
121         p = sdata.head;
122         sdata.head = sdata.head->next;
123         nasm_free (p);
124     }
125     saa_free (syms);
126     raa_free (bsym);
127     saa_free (strs);
128 }
129
130 static long as86_section_names (char *name, int pass, int *bits) {
131     /*
132      * Default is 16 bits.
133      */
134     if (!name)
135         *bits = 16;
136
137     if (!name)
138         return stext.index;
139
140     if (!strcmp(name, ".text"))
141         return stext.index;
142     else if (!strcmp(name, ".data"))
143         return sdata.index;
144     else if (!strcmp(name, ".bss"))
145         return bssindex;
146     else
147         return NO_SEG;
148 }
149
150 static int as86_add_string (char *name) {
151     int pos = strslen;
152     int length = strlen(name);
153
154     saa_wbytes (strs, name, (long)(length+1));
155     strslen += 1+length;
156
157     return pos;
158 }
159
160 static void as86_deflabel (char *name, long segment, long offset,
161                            int is_global) {
162     struct Symbol *sym;
163
164     if (name[0] == '.' && name[1] == '.') {
165         return;
166     }
167
168     sym = saa_wstruct (syms);
169
170     sym->strpos = as86_add_string (name);
171     sym->flags = 0;
172     if (segment == NO_SEG)
173         sym->flags |= SYM_ABSOLUTE, sym->segment = 0;
174     else if (segment == stext.index)
175         sym->segment = SECT_TEXT;
176     else if (segment == sdata.index)
177         sym->segment = SECT_DATA;
178     else if (segment == bssindex)
179         sym->segment = SECT_BSS;
180     else {
181         sym->flags |= SYM_IMPORT;
182         sym->segment = 15;
183     }
184
185     if (is_global == 2)
186         sym->segment = 3;       /* already have IMPORT */
187
188     if (is_global && !(sym->flags & SYM_IMPORT))
189         sym->flags |= SYM_EXPORT;
190
191     sym->value = offset;
192
193     /*
194      * define the references from external-symbol segment numbers
195      * to these symbol records.
196      */
197     if (segment != NO_SEG && segment != stext.index &&
198         segment != sdata.index && segment != bssindex)
199         bsym = raa_write (bsym, segment, nsyms);
200
201     nsyms++;
202 }
203
204 static void as86_add_piece (struct Section *sect, int type, long offset,
205                             long segment, long bytes, int relative) {
206     struct Piece *p;
207
208     sect->len += bytes;
209
210     if (type == 0 && sect->last && sect->last->type == 0) {
211         sect->last->bytes += bytes;
212         return;
213     }
214
215     p = sect->last = *sect->tail = nasm_malloc(sizeof(struct Piece));
216     sect->tail = &p->next;
217     p->next = NULL;
218
219     p->type = type;
220     p->offset = offset;
221     p->bytes = bytes;
222     p->relative = relative;
223
224     if (type == 1 && segment == stext.index)
225         p->number = SECT_TEXT;
226     else if (type == 1 && segment == sdata.index)
227         p->number = SECT_DATA;
228     else if (type == 1 && segment == bssindex)
229         p->number = SECT_BSS;
230     else if (type == 1)
231         p->number = raa_read (bsym, segment), p->type = 2;
232 }
233
234 static void as86_out (long segto, void *data, unsigned long type,
235                       long segment, long wrt) {
236     struct Section *s;
237     long realbytes = type & OUT_SIZMASK;
238     long offset;
239     unsigned char mydata[4], *p;
240
241     if (wrt != NO_SEG) {
242         wrt = NO_SEG;                  /* continue to do _something_ */
243         error (ERR_NONFATAL, "WRT not supported by as86 output format");
244     }
245
246     type &= OUT_TYPMASK;
247
248     /*
249      * handle absolute-assembly (structure definitions)
250      */
251     if (segto == NO_SEG) {
252         if (type != OUT_RESERVE)
253             error (ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
254                    " space");
255         return;
256     }
257
258     if (segto == stext.index)
259         s = &stext;
260     else if (segto == sdata.index)
261         s = &sdata;
262     else if (segto == bssindex)
263         s = NULL;
264     else {
265         error(ERR_WARNING, "attempt to assemble code in"
266               " segment %d: defaulting to `.text'", segto);
267         s = &stext;
268     }
269
270     if (!s && type != OUT_RESERVE) {
271         error(ERR_WARNING, "attempt to initialise memory in the"
272               " BSS section: ignored");
273         if (type == OUT_REL2ADR)
274             realbytes = 2;
275         else if (type == OUT_REL4ADR)
276             realbytes = 4;
277         bsslen += realbytes;
278         return;
279     }
280
281     if (type == OUT_RESERVE) {
282         if (s) {
283             error(ERR_WARNING, "uninitialised space declared in"
284                   " %s section: zeroing",
285                   (segto == stext.index ? "code" : "data"));
286             as86_sect_write (s, NULL, realbytes);
287             as86_add_piece (s, 0, 0L, 0L, realbytes, 0);
288         } else
289             bsslen += realbytes;
290     } else if (type == OUT_RAWDATA) {
291         if (segment != NO_SEG)
292             error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
293         as86_sect_write (s, data, realbytes);
294         as86_add_piece (s, 0, 0L, 0L, realbytes, 0);
295     } else if (type == OUT_ADDRESS) {
296         if (segment != NO_SEG) {
297             if (segment % 2) {
298                 error(ERR_NONFATAL, "as86 format does not support"
299                       " segment base references");
300             } else{
301                 offset = * (long *) data;
302                 as86_add_piece (s, 1, offset, segment, realbytes, 0);
303             }
304         } else {
305             p = mydata;
306             WRITELONG (p, * (long *) data);
307             as86_sect_write (s, data, realbytes);
308             as86_add_piece (s, 0, 0L, 0L, realbytes, 0);
309         }
310     } else if (type == OUT_REL2ADR) {
311         if (segment == segto)
312             error(ERR_PANIC, "intra-segment OUT_REL2ADR");
313         if (segment != NO_SEG) {
314             if (segment % 2) {
315                 error(ERR_NONFATAL, "as86 format does not support"
316                       " segment base references");
317             } else {
318                 offset = * (long *) data;
319                 as86_add_piece (s, 1, offset-realbytes+2, segment, 2L, 1);
320             }
321         }
322     } else if (type == OUT_REL4ADR) {
323         if (segment == segto)
324             error(ERR_PANIC, "intra-segment OUT_REL4ADR");
325         if (segment != NO_SEG) {
326             if (segment % 2) {
327                 error(ERR_NONFATAL, "as86 format does not support"
328                       " segment base references");
329             } else {
330                 offset = * (long *) data;
331                 as86_add_piece (s, 1, offset-realbytes+4, segment, 4L, 1);
332             }
333         }
334     }
335 }
336
337 static void as86_write(void) {
338     int i;
339     long symlen, seglen, segsize;
340
341     /*
342      * First, go through the symbol records working out how big
343      * each will be. Also fix up BSS references at this time, and
344      * set the flags words up completely.
345      */
346     symlen = 0;
347     saa_rewind (syms);
348     for (i = 0; i < nsyms; i++) {
349         struct Symbol *sym = saa_rstruct (syms);
350         if (sym->segment == SECT_BSS)
351             sym->segment = SECT_DATA, sym->value += sdata.len;
352         sym->flags |= sym->segment;
353         if (sym->value == 0)
354             sym->flags |= 0 << 14, symlen += 4;
355         else if (sym->value >= 0 && sym->value <= 255)
356             sym->flags |= 1 << 14, symlen += 5;
357         else if (sym->value >= 0 && sym->value <= 65535L)
358             sym->flags |= 2 << 14, symlen += 6;
359         else
360             sym->flags |= 3 << 14, symlen += 8;
361     }
362
363     /*
364      * Now do the same for the segments, and get the segment size
365      * descriptor word at the same time.
366      */
367     seglen = segsize = 0;
368     if ((unsigned long) stext.len > 65535L)
369         segsize |= 0x03000000L, seglen += 4;
370     else
371         segsize |= 0x02000000L, seglen += 2;
372     if ((unsigned long) sdata.len > 65535L)
373         segsize |= 0xC0000000L, seglen += 4;
374     else
375         segsize |= 0x80000000L, seglen += 2;
376
377     /*
378      * Emit the as86 header.
379      */
380     fwritelong (0x000186A3L, as86fp);
381     fputc (0x2A, as86fp);
382     fwritelong (27+symlen+seglen+strslen, as86fp);   /* header length */
383     fwritelong (stext.len+sdata.len, as86fp);
384     fwriteshort (strslen, as86fp);
385     fwriteshort (0, as86fp);           /* class = revision = 0 */
386     fwritelong (0x55555555L, as86fp);   /* segment max sizes: always this */
387     fwritelong (segsize, as86fp);      /* segment size descriptors */
388     if (segsize & 0x01000000L)
389         fwritelong (stext.len, as86fp);
390     else
391         fwriteshort (stext.len, as86fp);
392     if (segsize & 0x40000000L)
393         fwritelong (sdata.len, as86fp);
394     else
395         fwriteshort (sdata.len, as86fp);
396     fwriteshort (nsyms, as86fp);
397
398     /*
399      * Write the symbol table.
400      */
401     saa_rewind (syms);
402     for (i = 0; i < nsyms; i++) {
403         struct Symbol *sym = saa_rstruct (syms);
404         fwriteshort (sym->strpos, as86fp);
405         fwriteshort (sym->flags, as86fp);
406         switch (sym->flags & (3<<14)) {
407           case 0<<14: break;
408           case 1<<14: fputc (sym->value, as86fp); break;
409           case 2<<14: fwriteshort (sym->value, as86fp); break;
410           case 3<<14: fwritelong (sym->value, as86fp); break;
411         }
412     }
413
414     /*
415      * Write out the string table.
416      */
417     saa_fpwrite (strs, as86fp);
418
419     /*
420      * Write the program text.
421      */
422     as86_reloc_size = -1;
423     as86_write_section (&stext, SECT_TEXT);
424     as86_write_section (&sdata, SECT_DATA);
425     fputc (0, as86fp);                 /* termination */
426 }
427
428 static void as86_set_rsize (int size) {
429     if (as86_reloc_size != size) {
430         switch (as86_reloc_size = size) {
431           case 1: fputc (0x01, as86fp); break;   /* shouldn't happen */
432           case 2: fputc (0x02, as86fp); break;
433           case 4: fputc (0x03, as86fp); break;
434           default: error (ERR_PANIC, "bizarre relocation size %d", size);
435         }
436     }
437 }
438
439 static void as86_write_section (struct Section *sect, int index) {
440     struct Piece *p;
441     unsigned long s;
442     long length;
443
444     fputc (0x20+index, as86fp);        /* select the right section */
445
446     saa_rewind (sect->data);
447
448     for (p = sect->head; p; p = p->next)
449         switch (p->type) {
450           case 0:
451             /*
452              * Absolute data. Emit it in chunks of at most 64
453              * bytes.
454              */
455             length = p->bytes;
456             do {
457                 char buf[64];
458                 long tmplen = (length > 64 ? 64 : length);
459                 fputc (0x40 | (tmplen & 0x3F), as86fp);
460                 saa_rnbytes (sect->data, buf, tmplen);
461                 fwrite (buf, 1, tmplen, as86fp);
462                 length -= tmplen;
463             } while (length > 0);
464             break;
465           case 1:
466             /*
467              * A segment-type relocation. First fix up the BSS.
468              */
469             if (p->number == SECT_BSS)
470                 p->number = SECT_DATA, p->offset += sdata.len;
471             as86_set_rsize (p->bytes);
472             fputc (0x80 | (p->relative ? 0x20 : 0) | p->number, as86fp);
473             if (as86_reloc_size == 2)
474                 fwriteshort (p->offset, as86fp);
475             else
476                 fwritelong (p->offset, as86fp);
477             break;
478           case 2:
479             /*
480              * A symbol-type relocation.
481              */
482             as86_set_rsize (p->bytes);
483             s = p->offset;
484             if (s > 65535L)
485                 s = 3;
486             else if (s > 255)
487                 s = 2;
488             else if (s > 0)
489                 s = 1;
490             else
491                 s = 0;
492             fputc (0xC0 |
493                    (p->relative ? 0x20 : 0) |
494                    (p->number > 255 ? 0x04 : 0) | s, as86fp);
495             if (p->number > 255)
496                 fwriteshort (p->number, as86fp);
497             else
498                 fputc (p->number, as86fp);
499             switch ((int)s) {
500               case 0: break;
501               case 1: fputc (p->offset, as86fp); break;
502               case 2: fwriteshort (p->offset, as86fp); break;
503               case 3: fwritelong (p->offset, as86fp); break;
504             }
505             break;
506         }
507 }
508
509 static void as86_sect_write (struct Section *sect,
510                              unsigned char *data, unsigned long len) {
511     saa_wbytes (sect->data, data, len);
512     sect->datalen += len;
513 }
514
515 static long as86_segbase (long segment) {
516     return segment;
517 }
518
519 static int as86_directive (char *directive, char *value, int pass) {
520     return 0;
521 }
522
523 static void as86_filename (char *inname, char *outname, efunc error) {
524     char *p;
525
526     if ( (p = strrchr (inname, '.')) != NULL) {
527         strncpy (as86_module, inname, p-inname);
528         as86_module[p-inname] = '\0';
529     } else
530         strcpy (as86_module, inname);
531
532     standard_extension (inname, outname, ".o", error);
533 }
534
535 struct ofmt of_as86 = {
536     "Linux as86 (bin86 version 0.3) object files",
537     "as86",
538     as86_init,
539     as86_out,
540     as86_deflabel,
541     as86_section_names,
542     as86_segbase,
543     as86_directive,
544     as86_filename,
545     as86_cleanup
546 };
547
548 #endif /* OF_AS86 */