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.
29 int type
; /* 0 = absolute, 1 = seg, 2 = sym */
30 int32_t offset
; /* relative offset */
31 int number
; /* symbol/segment number (4=bss) */
32 int32_t bytes
; /* size of reloc or of absolute data */
33 bool relative
; /* relative address? */
37 int32_t strpos
; /* string table position of name */
38 int flags
; /* symbol flags */
39 int segment
; /* 4=bss at this point */
40 int32_t value
; /* address, or COMMON variable size */
44 * Section IDs - used in Piece.number and Symbol.segment.
46 #define SECT_TEXT 0 /* text section */
47 #define SECT_DATA 3 /* data section */
48 #define SECT_BSS 4 /* bss section */
51 * Flags used in Symbol.flags.
53 #define SYM_ENTRY (1<<8)
54 #define SYM_EXPORT (1<<7)
55 #define SYM_IMPORT (1<<6)
56 #define SYM_ABSOLUTE (1<<4)
60 uint32_t datalen
, size
, len
;
62 struct Piece
*head
, *last
, **tail
;
65 static char as86_module
[FILENAME_MAX
];
67 static struct Section stext
, sdata
;
68 static uint32_t bsslen
;
69 static int32_t bssindex
;
71 static struct SAA
*syms
;
72 static uint32_t nsyms
;
74 static struct RAA
*bsym
;
76 static struct SAA
*strs
;
77 static uint32_t strslen
;
79 static int as86_reloc_size
;
84 static void as86_write(void);
85 static void as86_write_section(struct Section
*, int);
86 static int as86_add_string(char *name
);
87 static void as86_sect_write(struct Section
*, const uint8_t *,
90 static void as86_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
94 (void)ldef
; /* placate optimisers */
96 stext
.data
= saa_init(1L);
98 stext
.head
= stext
.last
= NULL
;
99 stext
.tail
= &stext
.head
;
100 sdata
.data
= saa_init(1L);
102 sdata
.head
= sdata
.last
= NULL
;
103 sdata
.tail
= &sdata
.head
;
105 stext
.len
= stext
.datalen
= stext
.size
=
106 sdata
.len
= sdata
.datalen
= sdata
.size
= 0;
107 stext
.index
= seg_alloc();
108 sdata
.index
= seg_alloc();
109 bssindex
= seg_alloc();
110 syms
= saa_init((int32_t)sizeof(struct Symbol
));
116 as86_add_string(as86_module
);
119 static void as86_cleanup(int debuginfo
)
127 saa_free(stext
.data
);
130 stext
.head
= stext
.head
->next
;
133 saa_free(sdata
.data
);
136 sdata
.head
= sdata
.head
->next
;
144 static int32_t as86_section_names(char *name
, int pass
, int *bits
)
150 * Default is 16 bits.
158 if (!strcmp(name
, ".text"))
160 else if (!strcmp(name
, ".data"))
162 else if (!strcmp(name
, ".bss"))
168 static int as86_add_string(char *name
)
171 int length
= strlen(name
);
173 saa_wbytes(strs
, name
, (int32_t)(length
+ 1));
174 strslen
+= 1 + length
;
179 static void as86_deflabel(char *name
, int32_t segment
, int64_t offset
,
180 int is_global
, char *special
)
185 error(ERR_NONFATAL
, "as86 format does not support any"
186 " special symbol types");
188 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
189 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
193 sym
= saa_wstruct(syms
);
195 sym
->strpos
= as86_add_string(name
);
197 if (segment
== NO_SEG
)
198 sym
->flags
|= SYM_ABSOLUTE
, sym
->segment
= 0;
199 else if (segment
== stext
.index
)
200 sym
->segment
= SECT_TEXT
;
201 else if (segment
== sdata
.index
)
202 sym
->segment
= SECT_DATA
;
203 else if (segment
== bssindex
)
204 sym
->segment
= SECT_BSS
;
206 sym
->flags
|= SYM_IMPORT
;
211 sym
->segment
= 3; /* already have IMPORT */
213 if (is_global
&& !(sym
->flags
& SYM_IMPORT
))
214 sym
->flags
|= SYM_EXPORT
;
219 * define the references from external-symbol segment numbers
220 * to these symbol records.
222 if (segment
!= NO_SEG
&& segment
!= stext
.index
&&
223 segment
!= sdata
.index
&& segment
!= bssindex
)
224 bsym
= raa_write(bsym
, segment
, nsyms
);
229 static void as86_add_piece(struct Section
*sect
, int type
, int32_t offset
,
230 int32_t segment
, int32_t bytes
, int relative
)
236 if (type
== 0 && sect
->last
&& sect
->last
->type
== 0) {
237 sect
->last
->bytes
+= bytes
;
241 p
= sect
->last
= *sect
->tail
= nasm_malloc(sizeof(struct Piece
));
242 sect
->tail
= &p
->next
;
248 p
->relative
= relative
;
250 if (type
== 1 && segment
== stext
.index
)
251 p
->number
= SECT_TEXT
;
252 else if (type
== 1 && segment
== sdata
.index
)
253 p
->number
= SECT_DATA
;
254 else if (type
== 1 && segment
== bssindex
)
255 p
->number
= SECT_BSS
;
257 p
->number
= raa_read(bsym
, segment
), p
->type
= 2;
260 static void as86_out(int32_t segto
, const void *data
,
261 enum out_type type
, uint64_t size
,
262 int32_t segment
, int32_t wrt
)
266 uint8_t mydata
[4], *p
;
269 wrt
= NO_SEG
; /* continue to do _something_ */
270 error(ERR_NONFATAL
, "WRT not supported by as86 output format");
274 * handle absolute-assembly (structure definitions)
276 if (segto
== NO_SEG
) {
277 if (type
!= OUT_RESERVE
)
278 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
283 if (segto
== stext
.index
)
285 else if (segto
== sdata
.index
)
287 else if (segto
== bssindex
)
290 error(ERR_WARNING
, "attempt to assemble code in"
291 " segment %d: defaulting to `.text'", segto
);
295 if (!s
&& type
!= OUT_RESERVE
) {
296 error(ERR_WARNING
, "attempt to initialize memory in the"
297 " BSS section: ignored");
298 bsslen
+= realsize(type
, size
);
302 if (type
== OUT_RESERVE
) {
304 error(ERR_WARNING
, "uninitialized space declared in"
305 " %s section: zeroing",
306 (segto
== stext
.index
? "code" : "data"));
307 as86_sect_write(s
, NULL
, size
);
308 as86_add_piece(s
, 0, 0L, 0L, size
, 0);
311 } else if (type
== OUT_RAWDATA
) {
312 if (segment
!= NO_SEG
)
313 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
314 as86_sect_write(s
, data
, size
);
315 as86_add_piece(s
, 0, 0L, 0L, size
, 0);
316 } else if (type
== OUT_ADDRESS
) {
317 if (segment
!= NO_SEG
) {
319 error(ERR_NONFATAL
, "as86 format does not support"
320 " segment base references");
322 offset
= *(int64_t *)data
;
323 as86_add_piece(s
, 1, offset
, segment
, size
, 0);
327 WRITELONG(p
, *(int64_t *)data
);
328 as86_sect_write(s
, data
, size
);
329 as86_add_piece(s
, 0, 0L, 0L, size
, 0);
331 } else if (type
== OUT_REL2ADR
) {
332 if (segment
== segto
)
333 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
334 if (segment
!= NO_SEG
) {
336 error(ERR_NONFATAL
, "as86 format does not support"
337 " segment base references");
339 offset
= *(int64_t *)data
;
340 as86_add_piece(s
, 1, offset
- size
+ 2, segment
, 2L,
344 } else if (type
== OUT_REL4ADR
) {
345 if (segment
== segto
)
346 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
347 if (segment
!= NO_SEG
) {
349 error(ERR_NONFATAL
, "as86 format does not support"
350 " segment base references");
352 offset
= *(int64_t *)data
;
353 as86_add_piece(s
, 1, offset
- size
+ 4, segment
, 4L,
360 static void as86_write(void)
363 int32_t symlen
, seglen
, segsize
;
366 * First, go through the symbol records working out how big
367 * each will be. Also fix up BSS references at this time, and
368 * set the flags words up completely.
372 for (i
= 0; i
< nsyms
; i
++) {
373 struct Symbol
*sym
= saa_rstruct(syms
);
374 if (sym
->segment
== SECT_BSS
)
375 sym
->segment
= SECT_DATA
, sym
->value
+= sdata
.len
;
376 sym
->flags
|= sym
->segment
;
378 sym
->flags
|= 0 << 14, symlen
+= 4;
379 else if (sym
->value
>= 0 && sym
->value
<= 255)
380 sym
->flags
|= 1 << 14, symlen
+= 5;
381 else if (sym
->value
>= 0 && sym
->value
<= 65535L)
382 sym
->flags
|= 2 << 14, symlen
+= 6;
384 sym
->flags
|= 3 << 14, symlen
+= 8;
388 * Now do the same for the segments, and get the segment size
389 * descriptor word at the same time.
391 seglen
= segsize
= 0;
392 if ((uint32_t)stext
.len
> 65535L)
393 segsize
|= 0x03000000L
, seglen
+= 4;
395 segsize
|= 0x02000000L
, seglen
+= 2;
396 if ((uint32_t)sdata
.len
> 65535L)
397 segsize
|= 0xC0000000L
, seglen
+= 4;
399 segsize
|= 0x80000000L
, seglen
+= 2;
402 * Emit the as86 header.
404 fwriteint32_t(0x000186A3L
, as86fp
);
406 fwriteint32_t(27 + symlen
+ seglen
+ strslen
, as86fp
); /* header length */
407 fwriteint32_t(stext
.len
+ sdata
.len
+ bsslen
, as86fp
);
408 fwriteint16_t(strslen
, as86fp
);
409 fwriteint16_t(0, as86fp
); /* class = revision = 0 */
410 fwriteint32_t(0x55555555L
, as86fp
); /* segment max sizes: always this */
411 fwriteint32_t(segsize
, as86fp
); /* segment size descriptors */
412 if (segsize
& 0x01000000L
)
413 fwriteint32_t(stext
.len
, as86fp
);
415 fwriteint16_t(stext
.len
, as86fp
);
416 if (segsize
& 0x40000000L
)
417 fwriteint32_t(sdata
.len
+ bsslen
, as86fp
);
419 fwriteint16_t(sdata
.len
+ bsslen
, as86fp
);
420 fwriteint16_t(nsyms
, as86fp
);
423 * Write the symbol table.
426 for (i
= 0; i
< nsyms
; i
++) {
427 struct Symbol
*sym
= saa_rstruct(syms
);
428 fwriteint16_t(sym
->strpos
, as86fp
);
429 fwriteint16_t(sym
->flags
, as86fp
);
430 switch (sym
->flags
& (3 << 14)) {
434 fputc(sym
->value
, as86fp
);
437 fwriteint16_t(sym
->value
, as86fp
);
440 fwriteint32_t(sym
->value
, as86fp
);
446 * Write out the string table.
448 saa_fpwrite(strs
, as86fp
);
451 * Write the program text.
453 as86_reloc_size
= -1;
454 as86_write_section(&stext
, SECT_TEXT
);
455 as86_write_section(&sdata
, SECT_DATA
);
457 * Append the BSS section to the .data section
459 if (bsslen
> 65535L) {
461 fwriteint32_t(bsslen
, as86fp
);
462 } else if (bsslen
> 255) {
464 fwriteint16_t(bsslen
, as86fp
);
467 fputc(bsslen
, as86fp
);
470 fputc(0, as86fp
); /* termination */
473 static void as86_set_rsize(int size
)
475 if (as86_reloc_size
!= size
) {
476 switch (as86_reloc_size
= size
) {
487 error(ERR_PANIC
, "bizarre relocation size %d", size
);
492 static void as86_write_section(struct Section
*sect
, int index
)
498 fputc(0x20 + index
, as86fp
); /* select the right section */
500 saa_rewind(sect
->data
);
502 for (p
= sect
->head
; p
; p
= p
->next
)
506 * Absolute data. Emit it in chunks of at most 64
512 int32_t tmplen
= (length
> 64 ? 64 : length
);
513 fputc(0x40 | (tmplen
& 0x3F), as86fp
);
514 saa_rnbytes(sect
->data
, buf
, tmplen
);
515 fwrite(buf
, 1, tmplen
, as86fp
);
517 } while (length
> 0);
521 * A segment-type relocation. First fix up the BSS.
523 if (p
->number
== SECT_BSS
)
524 p
->number
= SECT_DATA
, p
->offset
+= sdata
.len
;
525 as86_set_rsize(p
->bytes
);
526 fputc(0x80 | (p
->relative
? 0x20 : 0) | p
->number
, as86fp
);
527 if (as86_reloc_size
== 2)
528 fwriteint16_t(p
->offset
, as86fp
);
530 fwriteint32_t(p
->offset
, as86fp
);
534 * A symbol-type relocation.
536 as86_set_rsize(p
->bytes
);
547 (p
->relative
? 0x20 : 0) |
548 (p
->number
> 255 ? 0x04 : 0) | s
, as86fp
);
550 fwriteint16_t(p
->number
, as86fp
);
552 fputc(p
->number
, as86fp
);
557 fputc(p
->offset
, as86fp
);
560 fwriteint16_t(p
->offset
, as86fp
);
563 fwriteint32_t(p
->offset
, as86fp
);
570 static void as86_sect_write(struct Section
*sect
,
571 const uint8_t *data
, uint32_t len
)
573 saa_wbytes(sect
->data
, data
, len
);
574 sect
->datalen
+= len
;
577 static int32_t as86_segbase(int32_t segment
)
582 static int as86_directive(char *directive
, char *value
, int pass
)
590 static void as86_filename(char *inname
, char *outname
, efunc error
)
594 if ((p
= strrchr(inname
, '.')) != NULL
) {
595 strncpy(as86_module
, inname
, p
- inname
);
596 as86_module
[p
- inname
] = '\0';
598 strcpy(as86_module
, inname
);
600 standard_extension(inname
, outname
, ".o", error
);
603 extern macros_t as86_stdmac
[];
605 static int as86_set_info(enum geninfo type
, char **val
)
611 void as86_linenumber(char *name
, int32_t segment
, int32_t offset
, int is_main
,
620 struct ofmt of_as86
= {
621 "Linux as86 (bin86 version 0.3) object files",