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 license given in the file "LICENSE"
7 * distributed in the NASM archive.
28 int type
; /* 0 = absolute, 1 = seg, 2 = sym */
29 int32_t offset
; /* relative offset */
30 int number
; /* symbol/segment number (4=bss) */
31 int32_t bytes
; /* size of reloc or of absolute data */
32 bool relative
; /* relative address? */
36 int32_t strpos
; /* string table position of name */
37 int flags
; /* symbol flags */
38 int segment
; /* 4=bss at this point */
39 int32_t value
; /* address, or COMMON variable size */
43 * Section IDs - used in Piece.number and Symbol.segment.
45 #define SECT_TEXT 0 /* text section */
46 #define SECT_DATA 3 /* data section */
47 #define SECT_BSS 4 /* bss section */
50 * Flags used in Symbol.flags.
52 #define SYM_ENTRY (1<<8)
53 #define SYM_EXPORT (1<<7)
54 #define SYM_IMPORT (1<<6)
55 #define SYM_ABSOLUTE (1<<4)
59 uint32_t datalen
, size
, len
;
61 struct Piece
*head
, *last
, **tail
;
64 static char as86_module
[FILENAME_MAX
];
66 static struct Section stext
, sdata
;
67 static uint32_t bsslen
;
68 static int32_t bssindex
;
70 static struct SAA
*syms
;
71 static uint32_t nsyms
;
73 static struct RAA
*bsym
;
75 static struct SAA
*strs
;
76 static uint32_t strslen
;
78 static int as86_reloc_size
;
83 static void as86_write(void);
84 static void as86_write_section(struct Section
*, int);
85 static int as86_add_string(char *name
);
86 static void as86_sect_write(struct Section
*, const uint8_t *,
89 static void as86_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
93 (void)ldef
; /* placate optimisers */
95 stext
.data
= saa_init(1L);
97 stext
.head
= stext
.last
= NULL
;
98 stext
.tail
= &stext
.head
;
99 sdata
.data
= saa_init(1L);
101 sdata
.head
= sdata
.last
= NULL
;
102 sdata
.tail
= &sdata
.head
;
104 stext
.len
= stext
.datalen
= stext
.size
=
105 sdata
.len
= sdata
.datalen
= sdata
.size
= 0;
106 stext
.index
= seg_alloc();
107 sdata
.index
= seg_alloc();
108 bssindex
= seg_alloc();
109 syms
= saa_init((int32_t)sizeof(struct Symbol
));
115 as86_add_string(as86_module
);
118 static void as86_cleanup(int debuginfo
)
126 saa_free(stext
.data
);
129 stext
.head
= stext
.head
->next
;
132 saa_free(sdata
.data
);
135 sdata
.head
= sdata
.head
->next
;
143 static int32_t as86_section_names(char *name
, int pass
, int *bits
)
149 * Default is 16 bits.
157 if (!strcmp(name
, ".text"))
159 else if (!strcmp(name
, ".data"))
161 else if (!strcmp(name
, ".bss"))
167 static int as86_add_string(char *name
)
170 int length
= strlen(name
);
172 saa_wbytes(strs
, name
, (int32_t)(length
+ 1));
173 strslen
+= 1 + length
;
178 static void as86_deflabel(char *name
, int32_t segment
, int64_t offset
,
179 int is_global
, char *special
)
184 error(ERR_NONFATAL
, "as86 format does not support any"
185 " special symbol types");
187 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
188 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
192 sym
= saa_wstruct(syms
);
194 sym
->strpos
= as86_add_string(name
);
196 if (segment
== NO_SEG
)
197 sym
->flags
|= SYM_ABSOLUTE
, sym
->segment
= 0;
198 else if (segment
== stext
.index
)
199 sym
->segment
= SECT_TEXT
;
200 else if (segment
== sdata
.index
)
201 sym
->segment
= SECT_DATA
;
202 else if (segment
== bssindex
)
203 sym
->segment
= SECT_BSS
;
205 sym
->flags
|= SYM_IMPORT
;
210 sym
->segment
= 3; /* already have IMPORT */
212 if (is_global
&& !(sym
->flags
& SYM_IMPORT
))
213 sym
->flags
|= SYM_EXPORT
;
218 * define the references from external-symbol segment numbers
219 * to these symbol records.
221 if (segment
!= NO_SEG
&& segment
!= stext
.index
&&
222 segment
!= sdata
.index
&& segment
!= bssindex
)
223 bsym
= raa_write(bsym
, segment
, nsyms
);
228 static void as86_add_piece(struct Section
*sect
, int type
, int32_t offset
,
229 int32_t segment
, int32_t bytes
, int relative
)
235 if (type
== 0 && sect
->last
&& sect
->last
->type
== 0) {
236 sect
->last
->bytes
+= bytes
;
240 p
= sect
->last
= *sect
->tail
= nasm_malloc(sizeof(struct Piece
));
241 sect
->tail
= &p
->next
;
247 p
->relative
= relative
;
249 if (type
== 1 && segment
== stext
.index
)
250 p
->number
= SECT_TEXT
;
251 else if (type
== 1 && segment
== sdata
.index
)
252 p
->number
= SECT_DATA
;
253 else if (type
== 1 && segment
== bssindex
)
254 p
->number
= SECT_BSS
;
256 p
->number
= raa_read(bsym
, segment
), p
->type
= 2;
259 static void as86_out(int32_t segto
, const void *data
,
260 enum out_type type
, uint64_t size
,
261 int32_t segment
, int32_t wrt
)
265 uint8_t mydata
[4], *p
;
268 wrt
= NO_SEG
; /* continue to do _something_ */
269 error(ERR_NONFATAL
, "WRT not supported by as86 output format");
273 * handle absolute-assembly (structure definitions)
275 if (segto
== NO_SEG
) {
276 if (type
!= OUT_RESERVE
)
277 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
282 if (segto
== stext
.index
)
284 else if (segto
== sdata
.index
)
286 else if (segto
== bssindex
)
289 error(ERR_WARNING
, "attempt to assemble code in"
290 " segment %d: defaulting to `.text'", segto
);
294 if (!s
&& type
!= OUT_RESERVE
) {
295 error(ERR_WARNING
, "attempt to initialize memory in the"
296 " BSS section: ignored");
297 if (type
== OUT_REL2ADR
)
299 else if (type
== OUT_REL4ADR
)
305 if (type
== OUT_RESERVE
) {
307 error(ERR_WARNING
, "uninitialized space declared in"
308 " %s section: zeroing",
309 (segto
== stext
.index
? "code" : "data"));
310 as86_sect_write(s
, NULL
, size
);
311 as86_add_piece(s
, 0, 0L, 0L, size
, 0);
314 } else if (type
== OUT_RAWDATA
) {
315 if (segment
!= NO_SEG
)
316 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
317 as86_sect_write(s
, data
, size
);
318 as86_add_piece(s
, 0, 0L, 0L, size
, 0);
319 } else if (type
== OUT_ADDRESS
) {
320 if (segment
!= NO_SEG
) {
322 error(ERR_NONFATAL
, "as86 format does not support"
323 " segment base references");
325 offset
= *(int64_t *)data
;
326 as86_add_piece(s
, 1, offset
, segment
, size
, 0);
330 WRITELONG(p
, *(int64_t *)data
);
331 as86_sect_write(s
, data
, size
);
332 as86_add_piece(s
, 0, 0L, 0L, size
, 0);
334 } else if (type
== OUT_REL2ADR
) {
335 if (segment
== segto
)
336 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
337 if (segment
!= NO_SEG
) {
339 error(ERR_NONFATAL
, "as86 format does not support"
340 " segment base references");
342 offset
= *(int64_t *)data
;
343 as86_add_piece(s
, 1, offset
- size
+ 2, segment
, 2L,
347 } else if (type
== OUT_REL4ADR
) {
348 if (segment
== segto
)
349 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
350 if (segment
!= NO_SEG
) {
352 error(ERR_NONFATAL
, "as86 format does not support"
353 " segment base references");
355 offset
= *(int64_t *)data
;
356 as86_add_piece(s
, 1, offset
- size
+ 4, segment
, 4L,
363 static void as86_write(void)
366 int32_t symlen
, seglen
, segsize
;
369 * First, go through the symbol records working out how big
370 * each will be. Also fix up BSS references at this time, and
371 * set the flags words up completely.
375 for (i
= 0; i
< nsyms
; i
++) {
376 struct Symbol
*sym
= saa_rstruct(syms
);
377 if (sym
->segment
== SECT_BSS
)
378 sym
->segment
= SECT_DATA
, sym
->value
+= sdata
.len
;
379 sym
->flags
|= sym
->segment
;
381 sym
->flags
|= 0 << 14, symlen
+= 4;
382 else if (sym
->value
>= 0 && sym
->value
<= 255)
383 sym
->flags
|= 1 << 14, symlen
+= 5;
384 else if (sym
->value
>= 0 && sym
->value
<= 65535L)
385 sym
->flags
|= 2 << 14, symlen
+= 6;
387 sym
->flags
|= 3 << 14, symlen
+= 8;
391 * Now do the same for the segments, and get the segment size
392 * descriptor word at the same time.
394 seglen
= segsize
= 0;
395 if ((uint32_t)stext
.len
> 65535L)
396 segsize
|= 0x03000000L
, seglen
+= 4;
398 segsize
|= 0x02000000L
, seglen
+= 2;
399 if ((uint32_t)sdata
.len
> 65535L)
400 segsize
|= 0xC0000000L
, seglen
+= 4;
402 segsize
|= 0x80000000L
, seglen
+= 2;
405 * Emit the as86 header.
407 fwriteint32_t(0x000186A3L
, as86fp
);
409 fwriteint32_t(27 + symlen
+ seglen
+ strslen
, as86fp
); /* header length */
410 fwriteint32_t(stext
.len
+ sdata
.len
+ bsslen
, as86fp
);
411 fwriteint16_t(strslen
, as86fp
);
412 fwriteint16_t(0, as86fp
); /* class = revision = 0 */
413 fwriteint32_t(0x55555555L
, as86fp
); /* segment max sizes: always this */
414 fwriteint32_t(segsize
, as86fp
); /* segment size descriptors */
415 if (segsize
& 0x01000000L
)
416 fwriteint32_t(stext
.len
, as86fp
);
418 fwriteint16_t(stext
.len
, as86fp
);
419 if (segsize
& 0x40000000L
)
420 fwriteint32_t(sdata
.len
+ bsslen
, as86fp
);
422 fwriteint16_t(sdata
.len
+ bsslen
, as86fp
);
423 fwriteint16_t(nsyms
, as86fp
);
426 * Write the symbol table.
429 for (i
= 0; i
< nsyms
; i
++) {
430 struct Symbol
*sym
= saa_rstruct(syms
);
431 fwriteint16_t(sym
->strpos
, as86fp
);
432 fwriteint16_t(sym
->flags
, as86fp
);
433 switch (sym
->flags
& (3 << 14)) {
437 fputc(sym
->value
, as86fp
);
440 fwriteint16_t(sym
->value
, as86fp
);
443 fwriteint32_t(sym
->value
, as86fp
);
449 * Write out the string table.
451 saa_fpwrite(strs
, as86fp
);
454 * Write the program text.
456 as86_reloc_size
= -1;
457 as86_write_section(&stext
, SECT_TEXT
);
458 as86_write_section(&sdata
, SECT_DATA
);
460 * Append the BSS section to the .data section
462 if (bsslen
> 65535L) {
464 fwriteint32_t(bsslen
, as86fp
);
465 } else if (bsslen
> 255) {
467 fwriteint16_t(bsslen
, as86fp
);
470 fputc(bsslen
, as86fp
);
473 fputc(0, as86fp
); /* termination */
476 static void as86_set_rsize(int size
)
478 if (as86_reloc_size
!= size
) {
479 switch (as86_reloc_size
= size
) {
490 error(ERR_PANIC
, "bizarre relocation size %d", size
);
495 static void as86_write_section(struct Section
*sect
, int index
)
501 fputc(0x20 + index
, as86fp
); /* select the right section */
503 saa_rewind(sect
->data
);
505 for (p
= sect
->head
; p
; p
= p
->next
)
509 * Absolute data. Emit it in chunks of at most 64
515 int32_t tmplen
= (length
> 64 ? 64 : length
);
516 fputc(0x40 | (tmplen
& 0x3F), as86fp
);
517 saa_rnbytes(sect
->data
, buf
, tmplen
);
518 fwrite(buf
, 1, tmplen
, as86fp
);
520 } while (length
> 0);
524 * A segment-type relocation. First fix up the BSS.
526 if (p
->number
== SECT_BSS
)
527 p
->number
= SECT_DATA
, p
->offset
+= sdata
.len
;
528 as86_set_rsize(p
->bytes
);
529 fputc(0x80 | (p
->relative
? 0x20 : 0) | p
->number
, as86fp
);
530 if (as86_reloc_size
== 2)
531 fwriteint16_t(p
->offset
, as86fp
);
533 fwriteint32_t(p
->offset
, as86fp
);
537 * A symbol-type relocation.
539 as86_set_rsize(p
->bytes
);
550 (p
->relative
? 0x20 : 0) |
551 (p
->number
> 255 ? 0x04 : 0) | s
, as86fp
);
553 fwriteint16_t(p
->number
, as86fp
);
555 fputc(p
->number
, as86fp
);
560 fputc(p
->offset
, as86fp
);
563 fwriteint16_t(p
->offset
, as86fp
);
566 fwriteint32_t(p
->offset
, as86fp
);
573 static void as86_sect_write(struct Section
*sect
,
574 const uint8_t *data
, uint32_t len
)
576 saa_wbytes(sect
->data
, data
, len
);
577 sect
->datalen
+= len
;
580 static int32_t as86_segbase(int32_t segment
)
585 static int as86_directive(char *directive
, char *value
, int pass
)
593 static void as86_filename(char *inname
, char *outname
, efunc error
)
597 if ((p
= strrchr(inname
, '.')) != NULL
) {
598 strncpy(as86_module
, inname
, p
- inname
);
599 as86_module
[p
- inname
] = '\0';
601 strcpy(as86_module
, inname
);
603 standard_extension(inname
, outname
, ".o", error
);
606 extern macros_t as86_stdmac
[];
608 static int as86_set_info(enum geninfo type
, char **val
)
614 void as86_linenumber(char *name
, int32_t segment
, int32_t offset
, int is_main
,
623 struct ofmt of_as86
= {
624 "Linux as86 (bin86 version 0.3) object files",