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.
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 */
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)
55 uint32_t datalen
, size
, len
;
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
;
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 *,
85 static void as86_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
89 (void)ldef
; /* placate optimisers */
90 stext
.data
= saa_init(1L);
92 stext
.head
= stext
.last
= NULL
;
93 stext
.tail
= &stext
.head
;
94 sdata
.data
= saa_init(1L);
96 sdata
.head
= sdata
.last
= NULL
;
97 sdata
.tail
= &sdata
.head
;
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
));
110 as86_add_string(as86_module
);
113 static void as86_cleanup(int debuginfo
)
121 saa_free(stext
.data
);
124 stext
.head
= stext
.head
->next
;
127 saa_free(sdata
.data
);
130 sdata
.head
= sdata
.head
->next
;
138 static int32_t as86_section_names(char *name
, int pass
, int *bits
)
141 * Default is 16 bits.
149 if (!strcmp(name
, ".text"))
151 else if (!strcmp(name
, ".data"))
153 else if (!strcmp(name
, ".bss"))
159 static int as86_add_string(char *name
)
162 int length
= strlen(name
);
164 saa_wbytes(strs
, name
, (int32_t)(length
+ 1));
165 strslen
+= 1 + length
;
170 static void as86_deflabel(char *name
, int32_t segment
, int32_t offset
,
171 int is_global
, char *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
);
184 sym
= saa_wstruct(syms
);
186 sym
->strpos
= as86_add_string(name
);
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
;
197 sym
->flags
|= SYM_IMPORT
;
202 sym
->segment
= 3; /* already have IMPORT */
204 if (is_global
&& !(sym
->flags
& SYM_IMPORT
))
205 sym
->flags
|= SYM_EXPORT
;
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
);
220 static void as86_add_piece(struct Section
*sect
, int type
, int32_t offset
,
221 int32_t segment
, int32_t bytes
, int relative
)
227 if (type
== 0 && sect
->last
&& sect
->last
->type
== 0) {
228 sect
->last
->bytes
+= bytes
;
232 p
= sect
->last
= *sect
->tail
= nasm_malloc(sizeof(struct Piece
));
233 sect
->tail
= &p
->next
;
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
;
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
)
255 int32_t realbytes
= type
& OUT_SIZMASK
;
257 uint8_t mydata
[4], *p
;
260 wrt
= NO_SEG
; /* continue to do _something_ */
261 error(ERR_NONFATAL
, "WRT not supported by as86 output format");
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]"
276 if (segto
== stext
.index
)
278 else if (segto
== sdata
.index
)
280 else if (segto
== bssindex
)
283 error(ERR_WARNING
, "attempt to assemble code in"
284 " segment %d: defaulting to `.text'", segto
);
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
)
293 else if (type
== OUT_REL4ADR
)
299 if (type
== OUT_RESERVE
) {
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);
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
) {
316 error(ERR_NONFATAL
, "as86 format does not support"
317 " segment base references");
319 offset
= *(int32_t *)data
;
320 as86_add_piece(s
, 1, offset
, segment
, realbytes
, 0);
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
) {
333 error(ERR_NONFATAL
, "as86 format does not support"
334 " segment base references");
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
) {
346 error(ERR_NONFATAL
, "as86 format does not support"
347 " segment base references");
349 offset
= *(int32_t *)data
;
350 as86_add_piece(s
, 1, offset
- realbytes
+ 4, segment
, 4L,
357 static void as86_write(void)
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.
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
;
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;
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;
392 segsize
|= 0x02000000L
, seglen
+= 2;
393 if ((uint32_t)sdata
.len
> 65535L)
394 segsize
|= 0xC0000000L
, seglen
+= 4;
396 segsize
|= 0x80000000L
, seglen
+= 2;
399 * Emit the as86 header.
401 fwriteint32_t(0x000186A3L
, 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
);
412 fwriteint16_t(stext
.len
, as86fp
);
413 if (segsize
& 0x40000000L
)
414 fwriteint32_t(sdata
.len
+ bsslen
, as86fp
);
416 fwriteint16_t(sdata
.len
+ bsslen
, as86fp
);
417 fwriteint16_t(nsyms
, as86fp
);
420 * Write the symbol table.
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)) {
431 fputc(sym
->value
, as86fp
);
434 fwriteint16_t(sym
->value
, as86fp
);
437 fwriteint32_t(sym
->value
, as86fp
);
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) {
458 fwriteint32_t(bsslen
, as86fp
);
459 } else if (bsslen
> 255) {
461 fwriteint16_t(bsslen
, 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
) {
484 error(ERR_PANIC
, "bizarre relocation size %d", size
);
489 static void as86_write_section(struct Section
*sect
, int index
)
495 fputc(0x20 + index
, as86fp
); /* select the right section */
497 saa_rewind(sect
->data
);
499 for (p
= sect
->head
; p
; p
= p
->next
)
503 * Absolute data. Emit it in chunks of at most 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
);
514 } while (length
> 0);
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
);
527 fwriteint32_t(p
->offset
, as86fp
);
531 * A symbol-type relocation.
533 as86_set_rsize(p
->bytes
);
544 (p
->relative
? 0x20 : 0) |
545 (p
->number
> 255 ? 0x04 : 0) | s
, as86fp
);
547 fwriteint16_t(p
->number
, as86fp
);
549 fputc(p
->number
, as86fp
);
554 fputc(p
->offset
, as86fp
);
557 fwriteint16_t(p
->offset
, as86fp
);
560 fwriteint32_t(p
->offset
, as86fp
);
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
)
579 static int as86_directive(char *directive
, char *value
, int pass
)
584 static void as86_filename(char *inname
, char *outname
, efunc error
)
588 if ((p
= strrchr(inname
, '.')) != NULL
) {
589 strncpy(as86_module
, inname
, p
- inname
);
590 as86_module
[p
- inname
] = '\0';
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",
604 static int as86_set_info(enum geninfo type
, char **val
)
608 void as86_linenumber(char *name
, int32_t segment
, int32_t offset
, int is_main
,
612 struct ofmt of_as86
= {
613 "Linux as86 (bin86 version 0.3) object files",