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.
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 */
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 */
38 * Section IDs - used in Piece.number and Symbol.segment.
40 #define SECT_TEXT 0 /* text section */
41 #define SECT_DATA 3 /* data section */
42 #define SECT_BSS 4 /* bss section */
45 * Flags used in Symbol.flags.
47 #define SYM_ENTRY (1<<8)
48 #define SYM_EXPORT (1<<7)
49 #define SYM_IMPORT (1<<6)
50 #define SYM_ABSOLUTE (1<<4)
54 unsigned long datalen
, size
, len
;
56 struct Piece
*head
, *last
, **tail
;
59 static char as86_module
[FILENAME_MAX
];
61 static struct Section stext
, sdata
;
62 static unsigned long bsslen
;
65 static struct SAA
*syms
;
66 static unsigned long nsyms
;
68 static struct RAA
*bsym
;
70 static struct SAA
*strs
;
71 static unsigned long strslen
;
73 static int as86_reloc_size
;
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);
83 static void as86_init(FILE *fp
, efunc errfunc
, ldfunc ldef
) {
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
;
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
));
105 as86_add_string (as86_module
);
108 static void as86_cleanup(void) {
113 saa_free (stext
.data
);
116 stext
.head
= stext
.head
->next
;
119 saa_free (sdata
.data
);
122 sdata
.head
= sdata
.head
->next
;
130 static long as86_section_names (char *name
, int pass
, int *bits
) {
132 * Default is 16 bits.
140 if (!strcmp(name
, ".text"))
142 else if (!strcmp(name
, ".data"))
144 else if (!strcmp(name
, ".bss"))
150 static int as86_add_string (char *name
) {
152 int length
= strlen(name
);
154 saa_wbytes (strs
, name
, (long)(length
+1));
160 static void as86_deflabel (char *name
, long segment
, long offset
,
164 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
165 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
169 sym
= saa_wstruct (syms
);
171 sym
->strpos
= as86_add_string (name
);
173 if (segment
== NO_SEG
)
174 sym
->flags
|= SYM_ABSOLUTE
, sym
->segment
= 0;
175 else if (segment
== stext
.index
)
176 sym
->segment
= SECT_TEXT
;
177 else if (segment
== sdata
.index
)
178 sym
->segment
= SECT_DATA
;
179 else if (segment
== bssindex
)
180 sym
->segment
= SECT_BSS
;
182 sym
->flags
|= SYM_IMPORT
;
187 sym
->segment
= 3; /* already have IMPORT */
189 if (is_global
&& !(sym
->flags
& SYM_IMPORT
))
190 sym
->flags
|= SYM_EXPORT
;
195 * define the references from external-symbol segment numbers
196 * to these symbol records.
198 if (segment
!= NO_SEG
&& segment
!= stext
.index
&&
199 segment
!= sdata
.index
&& segment
!= bssindex
)
200 bsym
= raa_write (bsym
, segment
, nsyms
);
205 static void as86_add_piece (struct Section
*sect
, int type
, long offset
,
206 long segment
, long bytes
, int relative
) {
211 if (type
== 0 && sect
->last
&& sect
->last
->type
== 0) {
212 sect
->last
->bytes
+= bytes
;
216 p
= sect
->last
= *sect
->tail
= nasm_malloc(sizeof(struct Piece
));
217 sect
->tail
= &p
->next
;
223 p
->relative
= relative
;
225 if (type
== 1 && segment
== stext
.index
)
226 p
->number
= SECT_TEXT
;
227 else if (type
== 1 && segment
== sdata
.index
)
228 p
->number
= SECT_DATA
;
229 else if (type
== 1 && segment
== bssindex
)
230 p
->number
= SECT_BSS
;
232 p
->number
= raa_read (bsym
, segment
), p
->type
= 2;
235 static void as86_out (long segto
, void *data
, unsigned long type
,
236 long segment
, long wrt
) {
238 long realbytes
= type
& OUT_SIZMASK
;
240 unsigned char mydata
[4], *p
;
243 wrt
= NO_SEG
; /* continue to do _something_ */
244 error (ERR_NONFATAL
, "WRT not supported by as86 output format");
250 * handle absolute-assembly (structure definitions)
252 if (segto
== NO_SEG
) {
253 if (type
!= OUT_RESERVE
)
254 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
259 if (segto
== stext
.index
)
261 else if (segto
== sdata
.index
)
263 else if (segto
== bssindex
)
266 error(ERR_WARNING
, "attempt to assemble code in"
267 " segment %d: defaulting to `.text'", segto
);
271 if (!s
&& type
!= OUT_RESERVE
) {
272 error(ERR_WARNING
, "attempt to initialise memory in the"
273 " BSS section: ignored");
274 if (type
== OUT_REL2ADR
)
276 else if (type
== OUT_REL4ADR
)
282 if (type
== OUT_RESERVE
) {
284 error(ERR_WARNING
, "uninitialised space declared in"
285 " %s section: zeroing",
286 (segto
== stext
.index
? "code" : "data"));
287 as86_sect_write (s
, NULL
, realbytes
);
288 as86_add_piece (s
, 0, 0L, 0L, realbytes
, 0);
291 } else if (type
== OUT_RAWDATA
) {
292 if (segment
!= NO_SEG
)
293 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
294 as86_sect_write (s
, data
, realbytes
);
295 as86_add_piece (s
, 0, 0L, 0L, realbytes
, 0);
296 } else if (type
== OUT_ADDRESS
) {
297 if (segment
!= NO_SEG
) {
299 error(ERR_NONFATAL
, "as86 format does not support"
300 " segment base references");
302 offset
= * (long *) data
;
303 as86_add_piece (s
, 1, offset
, segment
, realbytes
, 0);
307 WRITELONG (p
, * (long *) data
);
308 as86_sect_write (s
, data
, realbytes
);
309 as86_add_piece (s
, 0, 0L, 0L, realbytes
, 0);
311 } else if (type
== OUT_REL2ADR
) {
312 if (segment
== segto
)
313 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
314 if (segment
!= NO_SEG
) {
316 error(ERR_NONFATAL
, "as86 format does not support"
317 " segment base references");
319 offset
= * (long *) data
;
320 as86_add_piece (s
, 1, offset
-realbytes
+2, segment
, 2L, 1);
323 } else if (type
== OUT_REL4ADR
) {
324 if (segment
== segto
)
325 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
326 if (segment
!= NO_SEG
) {
328 error(ERR_NONFATAL
, "as86 format does not support"
329 " segment base references");
331 offset
= * (long *) data
;
332 as86_add_piece (s
, 1, offset
-realbytes
+4, segment
, 4L, 1);
338 static void as86_write(void) {
340 long symlen
, seglen
, segsize
;
343 * First, go through the symbol records working out how big
344 * each will be. Also fix up BSS references at this time, and
345 * set the flags words up completely.
349 for (i
= 0; i
< nsyms
; i
++) {
350 struct Symbol
*sym
= saa_rstruct (syms
);
351 if (sym
->segment
== SECT_BSS
)
352 sym
->segment
= SECT_DATA
, sym
->value
+= sdata
.len
;
353 sym
->flags
|= sym
->segment
;
355 sym
->flags
|= 0 << 14, symlen
+= 4;
356 else if (sym
->value
>= 0 && sym
->value
<= 255)
357 sym
->flags
|= 1 << 14, symlen
+= 5;
358 else if (sym
->value
>= 0 && sym
->value
<= 65535L)
359 sym
->flags
|= 2 << 14, symlen
+= 6;
361 sym
->flags
|= 3 << 14, symlen
+= 8;
365 * Now do the same for the segments, and get the segment size
366 * descriptor word at the same time.
368 seglen
= segsize
= 0;
369 if ((unsigned long) stext
.len
> 65535L)
370 segsize
|= 0x03000000L
, seglen
+= 4;
372 segsize
|= 0x02000000L
, seglen
+= 2;
373 if ((unsigned long) sdata
.len
> 65535L)
374 segsize
|= 0xC0000000L
, seglen
+= 4;
376 segsize
|= 0x80000000L
, seglen
+= 2;
379 * Emit the as86 header.
381 fwritelong (0x000186A3L
, as86fp
);
382 fputc (0x2A, as86fp
);
383 fwritelong (27+symlen
+seglen
+strslen
, as86fp
); /* header length */
384 fwritelong (stext
.len
+sdata
.len
, as86fp
);
385 fwriteshort (strslen
, as86fp
);
386 fwriteshort (0, as86fp
); /* class = revision = 0 */
387 fwritelong (0x55555555L
, as86fp
); /* segment max sizes: always this */
388 fwritelong (segsize
, as86fp
); /* segment size descriptors */
389 if (segsize
& 0x01000000L
)
390 fwritelong (stext
.len
, as86fp
);
392 fwriteshort (stext
.len
, as86fp
);
393 if (segsize
& 0x40000000L
)
394 fwritelong (sdata
.len
, as86fp
);
396 fwriteshort (sdata
.len
, as86fp
);
397 fwriteshort (nsyms
, as86fp
);
400 * Write the symbol table.
403 for (i
= 0; i
< nsyms
; i
++) {
404 struct Symbol
*sym
= saa_rstruct (syms
);
405 fwriteshort (sym
->strpos
, as86fp
);
406 fwriteshort (sym
->flags
, as86fp
);
407 switch (sym
->flags
& (3<<14)) {
409 case 1<<14: fputc (sym
->value
, as86fp
); break;
410 case 2<<14: fwriteshort (sym
->value
, as86fp
); break;
411 case 3<<14: fwritelong (sym
->value
, as86fp
); break;
416 * Write out the string table.
418 saa_fpwrite (strs
, as86fp
);
421 * Write the program text.
423 as86_reloc_size
= -1;
424 as86_write_section (&stext
, SECT_TEXT
);
425 as86_write_section (&sdata
, SECT_DATA
);
426 fputc (0, as86fp
); /* termination */
429 static void as86_set_rsize (int size
) {
430 if (as86_reloc_size
!= size
) {
431 switch (as86_reloc_size
= size
) {
432 case 1: fputc (0x01, as86fp
); break; /* shouldn't happen */
433 case 2: fputc (0x02, as86fp
); break;
434 case 4: fputc (0x03, as86fp
); break;
435 default: error (ERR_PANIC
, "bizarre relocation size %d", size
);
440 static void as86_write_section (struct Section
*sect
, int index
) {
445 fputc (0x20+index
, as86fp
); /* select the right section */
447 saa_rewind (sect
->data
);
449 for (p
= sect
->head
; p
; p
= p
->next
)
453 * Absolute data. Emit it in chunks of at most 64
459 long tmplen
= (length
> 64 ? 64 : length
);
460 fputc (0x40 | (tmplen
& 0x3F), as86fp
);
461 saa_rnbytes (sect
->data
, buf
, tmplen
);
462 fwrite (buf
, 1, tmplen
, as86fp
);
464 } while (length
> 0);
468 * A segment-type relocation. First fix up the BSS.
470 if (p
->number
== SECT_BSS
)
471 p
->number
= SECT_DATA
, p
->offset
+= sdata
.len
;
472 as86_set_rsize (p
->bytes
);
473 fputc (0x80 | (p
->relative
? 0x20 : 0) | p
->number
, as86fp
);
474 if (as86_reloc_size
== 2)
475 fwriteshort (p
->offset
, as86fp
);
477 fwritelong (p
->offset
, as86fp
);
481 * A symbol-type relocation.
483 as86_set_rsize (p
->bytes
);
494 (p
->relative
? 0x20 : 0) |
495 (p
->number
> 255 ? 0x04 : 0) | s
, as86fp
);
497 fwriteshort (p
->number
, as86fp
);
499 fputc (p
->number
, as86fp
);
502 case 1: fputc (p
->offset
, as86fp
); break;
503 case 2: fwriteshort (p
->offset
, as86fp
); break;
504 case 3: fwritelong (p
->offset
, as86fp
); break;
510 static void as86_sect_write (struct Section
*sect
,
511 unsigned char *data
, unsigned long len
) {
512 saa_wbytes (sect
->data
, data
, len
);
513 sect
->datalen
+= len
;
516 static long as86_segbase (long segment
) {
520 static int as86_directive (char *directive
, char *value
, int pass
) {
524 static void as86_filename (char *inname
, char *outname
, efunc error
) {
527 if ( (p
= strrchr (inname
, '.')) != NULL
) {
528 strncpy (as86_module
, inname
, p
-inname
);
529 as86_module
[p
-inname
] = '\0';
531 strcpy (as86_module
, inname
);
533 standard_extension (inname
, outname
, ".o", error
);
536 struct ofmt of_as86
= {
537 "Linux as86 (bin86 version 0.3) object files",