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
*, const unsigned char *, unsigned long);
83 static void as86_init(FILE *fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
87 (void) ldef
; /* placate optimisers */
88 stext
.data
= saa_init(1L); stext
.datalen
= 0L;
89 stext
.head
= stext
.last
= NULL
;
90 stext
.tail
= &stext
.head
;
91 sdata
.data
= saa_init(1L); sdata
.datalen
= 0L;
92 sdata
.head
= sdata
.last
= NULL
;
93 sdata
.tail
= &sdata
.head
;
95 stext
.len
= stext
.datalen
= stext
.size
=
96 sdata
.len
= sdata
.datalen
= sdata
.size
= 0;
97 stext
.index
= seg_alloc();
98 sdata
.index
= seg_alloc();
99 bssindex
= seg_alloc();
100 syms
= saa_init((long)sizeof(struct Symbol
));
106 as86_add_string (as86_module
);
109 static void as86_cleanup(int debuginfo
)
117 saa_free (stext
.data
);
120 stext
.head
= stext
.head
->next
;
123 saa_free (sdata
.data
);
126 sdata
.head
= sdata
.head
->next
;
134 static long as86_section_names (char *name
, int pass
, int *bits
)
137 * Default is 16 bits.
145 if (!strcmp(name
, ".text"))
147 else if (!strcmp(name
, ".data"))
149 else if (!strcmp(name
, ".bss"))
155 static int as86_add_string (char *name
)
158 int length
= strlen(name
);
160 saa_wbytes (strs
, name
, (long)(length
+1));
166 static void as86_deflabel (char *name
, long segment
, long offset
,
167 int is_global
, char *special
)
172 error (ERR_NONFATAL
, "as86 format does not support any"
173 " special symbol types");
175 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
176 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
180 sym
= saa_wstruct (syms
);
182 sym
->strpos
= as86_add_string (name
);
184 if (segment
== NO_SEG
)
185 sym
->flags
|= SYM_ABSOLUTE
, sym
->segment
= 0;
186 else if (segment
== stext
.index
)
187 sym
->segment
= SECT_TEXT
;
188 else if (segment
== sdata
.index
)
189 sym
->segment
= SECT_DATA
;
190 else if (segment
== bssindex
)
191 sym
->segment
= SECT_BSS
;
193 sym
->flags
|= SYM_IMPORT
;
198 sym
->segment
= 3; /* already have IMPORT */
200 if (is_global
&& !(sym
->flags
& SYM_IMPORT
))
201 sym
->flags
|= SYM_EXPORT
;
206 * define the references from external-symbol segment numbers
207 * to these symbol records.
209 if (segment
!= NO_SEG
&& segment
!= stext
.index
&&
210 segment
!= sdata
.index
&& segment
!= bssindex
)
211 bsym
= raa_write (bsym
, segment
, nsyms
);
216 static void as86_add_piece (struct Section
*sect
, int type
, long offset
,
217 long segment
, long bytes
, int relative
)
223 if (type
== 0 && sect
->last
&& sect
->last
->type
== 0) {
224 sect
->last
->bytes
+= bytes
;
228 p
= sect
->last
= *sect
->tail
= nasm_malloc(sizeof(struct Piece
));
229 sect
->tail
= &p
->next
;
235 p
->relative
= relative
;
237 if (type
== 1 && segment
== stext
.index
)
238 p
->number
= SECT_TEXT
;
239 else if (type
== 1 && segment
== sdata
.index
)
240 p
->number
= SECT_DATA
;
241 else if (type
== 1 && segment
== bssindex
)
242 p
->number
= SECT_BSS
;
244 p
->number
= raa_read (bsym
, segment
), p
->type
= 2;
247 static void as86_out (long segto
, const void *data
, unsigned long type
,
248 long segment
, long wrt
)
251 long realbytes
= type
& OUT_SIZMASK
;
253 unsigned char mydata
[4], *p
;
256 wrt
= NO_SEG
; /* continue to do _something_ */
257 error (ERR_NONFATAL
, "WRT not supported by as86 output format");
263 * handle absolute-assembly (structure definitions)
265 if (segto
== NO_SEG
) {
266 if (type
!= OUT_RESERVE
)
267 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
272 if (segto
== stext
.index
)
274 else if (segto
== sdata
.index
)
276 else if (segto
== bssindex
)
279 error(ERR_WARNING
, "attempt to assemble code in"
280 " segment %d: defaulting to `.text'", segto
);
284 if (!s
&& type
!= OUT_RESERVE
) {
285 error(ERR_WARNING
, "attempt to initialise memory in the"
286 " BSS section: ignored");
287 if (type
== OUT_REL2ADR
)
289 else if (type
== OUT_REL4ADR
)
295 if (type
== OUT_RESERVE
) {
297 error(ERR_WARNING
, "uninitialised space declared in"
298 " %s section: zeroing",
299 (segto
== stext
.index
? "code" : "data"));
300 as86_sect_write (s
, NULL
, realbytes
);
301 as86_add_piece (s
, 0, 0L, 0L, realbytes
, 0);
304 } else if (type
== OUT_RAWDATA
) {
305 if (segment
!= NO_SEG
)
306 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
307 as86_sect_write (s
, data
, realbytes
);
308 as86_add_piece (s
, 0, 0L, 0L, realbytes
, 0);
309 } else if (type
== OUT_ADDRESS
) {
310 if (segment
!= NO_SEG
) {
312 error(ERR_NONFATAL
, "as86 format does not support"
313 " segment base references");
315 offset
= * (long *) data
;
316 as86_add_piece (s
, 1, offset
, segment
, realbytes
, 0);
320 WRITELONG (p
, * (long *) data
);
321 as86_sect_write (s
, data
, realbytes
);
322 as86_add_piece (s
, 0, 0L, 0L, realbytes
, 0);
324 } else if (type
== OUT_REL2ADR
) {
325 if (segment
== segto
)
326 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
327 if (segment
!= NO_SEG
) {
329 error(ERR_NONFATAL
, "as86 format does not support"
330 " segment base references");
332 offset
= * (long *) data
;
333 as86_add_piece (s
, 1, offset
-realbytes
+2, segment
, 2L, 1);
336 } else if (type
== OUT_REL4ADR
) {
337 if (segment
== segto
)
338 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
339 if (segment
!= NO_SEG
) {
341 error(ERR_NONFATAL
, "as86 format does not support"
342 " segment base references");
344 offset
= * (long *) data
;
345 as86_add_piece (s
, 1, offset
-realbytes
+4, segment
, 4L, 1);
351 static void as86_write(void)
354 long symlen
, seglen
, segsize
;
357 * First, go through the symbol records working out how big
358 * each will be. Also fix up BSS references at this time, and
359 * set the flags words up completely.
363 for (i
= 0; i
< nsyms
; i
++) {
364 struct Symbol
*sym
= saa_rstruct (syms
);
365 if (sym
->segment
== SECT_BSS
)
366 sym
->segment
= SECT_DATA
, sym
->value
+= sdata
.len
;
367 sym
->flags
|= sym
->segment
;
369 sym
->flags
|= 0 << 14, symlen
+= 4;
370 else if (sym
->value
>= 0 && sym
->value
<= 255)
371 sym
->flags
|= 1 << 14, symlen
+= 5;
372 else if (sym
->value
>= 0 && sym
->value
<= 65535L)
373 sym
->flags
|= 2 << 14, symlen
+= 6;
375 sym
->flags
|= 3 << 14, symlen
+= 8;
379 * Now do the same for the segments, and get the segment size
380 * descriptor word at the same time.
382 seglen
= segsize
= 0;
383 if ((unsigned long) stext
.len
> 65535L)
384 segsize
|= 0x03000000L
, seglen
+= 4;
386 segsize
|= 0x02000000L
, seglen
+= 2;
387 if ((unsigned long) sdata
.len
> 65535L)
388 segsize
|= 0xC0000000L
, seglen
+= 4;
390 segsize
|= 0x80000000L
, seglen
+= 2;
393 * Emit the as86 header.
395 fwritelong (0x000186A3L
, as86fp
);
396 fputc (0x2A, as86fp
);
397 fwritelong (27+symlen
+seglen
+strslen
, as86fp
); /* header length */
398 fwritelong (stext
.len
+sdata
.len
, as86fp
);
399 fwriteshort (strslen
, as86fp
);
400 fwriteshort (0, as86fp
); /* class = revision = 0 */
401 fwritelong (0x55555555L
, as86fp
); /* segment max sizes: always this */
402 fwritelong (segsize
, as86fp
); /* segment size descriptors */
403 if (segsize
& 0x01000000L
)
404 fwritelong (stext
.len
, as86fp
);
406 fwriteshort (stext
.len
, as86fp
);
407 if (segsize
& 0x40000000L
)
408 fwritelong (sdata
.len
, as86fp
);
410 fwriteshort (sdata
.len
, as86fp
);
411 fwriteshort (nsyms
, as86fp
);
414 * Write the symbol table.
417 for (i
= 0; i
< nsyms
; i
++) {
418 struct Symbol
*sym
= saa_rstruct (syms
);
419 fwriteshort (sym
->strpos
, as86fp
);
420 fwriteshort (sym
->flags
, as86fp
);
421 switch (sym
->flags
& (3<<14)) {
423 case 1<<14: fputc (sym
->value
, as86fp
); break;
424 case 2<<14: fwriteshort (sym
->value
, as86fp
); break;
425 case 3<<14: fwritelong (sym
->value
, as86fp
); break;
430 * Write out the string table.
432 saa_fpwrite (strs
, as86fp
);
435 * Write the program text.
437 as86_reloc_size
= -1;
438 as86_write_section (&stext
, SECT_TEXT
);
439 as86_write_section (&sdata
, SECT_DATA
);
440 fputc (0, as86fp
); /* termination */
443 static void as86_set_rsize (int size
)
445 if (as86_reloc_size
!= size
) {
446 switch (as86_reloc_size
= size
) {
447 case 1: fputc (0x01, as86fp
); break;
448 case 2: fputc (0x02, as86fp
); break;
449 case 4: fputc (0x03, as86fp
); break;
450 default: error (ERR_PANIC
, "bizarre relocation size %d", size
);
455 static void as86_write_section (struct Section
*sect
, int index
)
461 fputc (0x20+index
, as86fp
); /* select the right section */
463 saa_rewind (sect
->data
);
465 for (p
= sect
->head
; p
; p
= p
->next
)
469 * Absolute data. Emit it in chunks of at most 64
475 long tmplen
= (length
> 64 ? 64 : length
);
476 fputc (0x40 | (tmplen
& 0x3F), as86fp
);
477 saa_rnbytes (sect
->data
, buf
, tmplen
);
478 fwrite (buf
, 1, tmplen
, as86fp
);
480 } while (length
> 0);
484 * A segment-type relocation. First fix up the BSS.
486 if (p
->number
== SECT_BSS
)
487 p
->number
= SECT_DATA
, p
->offset
+= sdata
.len
;
488 as86_set_rsize (p
->bytes
);
489 fputc (0x80 | (p
->relative
? 0x20 : 0) | p
->number
, as86fp
);
490 if (as86_reloc_size
== 2)
491 fwriteshort (p
->offset
, as86fp
);
493 fwritelong (p
->offset
, as86fp
);
497 * A symbol-type relocation.
499 as86_set_rsize (p
->bytes
);
510 (p
->relative
? 0x20 : 0) |
511 (p
->number
> 255 ? 0x04 : 0) | s
, as86fp
);
513 fwriteshort (p
->number
, as86fp
);
515 fputc (p
->number
, as86fp
);
518 case 1: fputc (p
->offset
, as86fp
); break;
519 case 2: fwriteshort (p
->offset
, as86fp
); break;
520 case 3: fwritelong (p
->offset
, as86fp
); break;
526 static void as86_sect_write (struct Section
*sect
,
527 const unsigned char *data
, unsigned long len
)
529 saa_wbytes (sect
->data
, data
, len
);
530 sect
->datalen
+= len
;
533 static long as86_segbase (long segment
)
538 static int as86_directive (char *directive
, char *value
, int pass
)
543 static void as86_filename (char *inname
, char *outname
, efunc error
)
547 if ( (p
= strrchr (inname
, '.')) != NULL
) {
548 strncpy (as86_module
, inname
, p
-inname
);
549 as86_module
[p
-inname
] = '\0';
551 strcpy (as86_module
, inname
);
553 standard_extension (inname
, outname
, ".o", error
);
556 static const char *as86_stdmac
[] = {
557 "%define __SECT__ [section .text]",
558 "%macro __NASM_CDecl__ 1",
563 static int as86_set_info(enum geninfo type
, char **val
)
567 void as86_linenumber (char *name
, long segment
, long offset
, int is_main
,
571 struct ofmt of_as86
= {
572 "Linux as86 (bin86 version 0.3) object files",