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.
26 int type
; /* 0 = absolute, 1 = seg, 2 = sym */
27 int32_t offset
; /* relative offset */
28 int number
; /* symbol/segment number (4=bss) */
29 int32_t bytes
; /* size of reloc or of absolute data */
30 bool relative
; /* relative address? */
34 int32_t strpos
; /* string table position of name */
35 int flags
; /* symbol flags */
36 int segment
; /* 4=bss at this point */
37 int32_t value
; /* address, or COMMON variable size */
41 * Section IDs - used in Piece.number and Symbol.segment.
43 #define SECT_TEXT 0 /* text section */
44 #define SECT_DATA 3 /* data section */
45 #define SECT_BSS 4 /* bss section */
48 * Flags used in Symbol.flags.
50 #define SYM_ENTRY (1<<8)
51 #define SYM_EXPORT (1<<7)
52 #define SYM_IMPORT (1<<6)
53 #define SYM_ABSOLUTE (1<<4)
57 uint32_t datalen
, size
, len
;
59 struct Piece
*head
, *last
, **tail
;
62 static char as86_module
[FILENAME_MAX
];
64 static struct Section stext
, sdata
;
65 static uint32_t bsslen
;
66 static int32_t bssindex
;
68 static struct SAA
*syms
;
69 static uint32_t nsyms
;
71 static struct RAA
*bsym
;
73 static struct SAA
*strs
;
74 static uint32_t strslen
;
76 static int as86_reloc_size
;
81 static void as86_write(void);
82 static void as86_write_section(struct Section
*, int);
83 static int as86_add_string(char *name
);
84 static void as86_sect_write(struct Section
*, const uint8_t *,
87 static void as86_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
91 (void)ldef
; /* placate optimisers */
93 stext
.data
= saa_init(1L);
95 stext
.head
= stext
.last
= NULL
;
96 stext
.tail
= &stext
.head
;
97 sdata
.data
= saa_init(1L);
99 sdata
.head
= sdata
.last
= NULL
;
100 sdata
.tail
= &sdata
.head
;
102 stext
.len
= stext
.datalen
= stext
.size
=
103 sdata
.len
= sdata
.datalen
= sdata
.size
= 0;
104 stext
.index
= seg_alloc();
105 sdata
.index
= seg_alloc();
106 bssindex
= seg_alloc();
107 syms
= saa_init((int32_t)sizeof(struct Symbol
));
113 as86_add_string(as86_module
);
116 static void as86_cleanup(int debuginfo
)
124 saa_free(stext
.data
);
127 stext
.head
= stext
.head
->next
;
130 saa_free(sdata
.data
);
133 sdata
.head
= sdata
.head
->next
;
141 static int32_t as86_section_names(char *name
, int pass
, int *bits
)
147 * Default is 16 bits.
155 if (!strcmp(name
, ".text"))
157 else if (!strcmp(name
, ".data"))
159 else if (!strcmp(name
, ".bss"))
165 static int as86_add_string(char *name
)
168 int length
= strlen(name
);
170 saa_wbytes(strs
, name
, (int32_t)(length
+ 1));
171 strslen
+= 1 + length
;
176 static void as86_deflabel(char *name
, int32_t segment
, int64_t offset
,
177 int is_global
, char *special
)
182 error(ERR_NONFATAL
, "as86 format does not support any"
183 " special symbol types");
185 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
186 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
190 sym
= saa_wstruct(syms
);
192 sym
->strpos
= as86_add_string(name
);
194 if (segment
== NO_SEG
)
195 sym
->flags
|= SYM_ABSOLUTE
, sym
->segment
= 0;
196 else if (segment
== stext
.index
)
197 sym
->segment
= SECT_TEXT
;
198 else if (segment
== sdata
.index
)
199 sym
->segment
= SECT_DATA
;
200 else if (segment
== bssindex
)
201 sym
->segment
= SECT_BSS
;
203 sym
->flags
|= SYM_IMPORT
;
208 sym
->segment
= 3; /* already have IMPORT */
210 if (is_global
&& !(sym
->flags
& SYM_IMPORT
))
211 sym
->flags
|= SYM_EXPORT
;
216 * define the references from external-symbol segment numbers
217 * to these symbol records.
219 if (segment
!= NO_SEG
&& segment
!= stext
.index
&&
220 segment
!= sdata
.index
&& segment
!= bssindex
)
221 bsym
= raa_write(bsym
, segment
, nsyms
);
226 static void as86_add_piece(struct Section
*sect
, int type
, int32_t offset
,
227 int32_t segment
, int32_t bytes
, int relative
)
233 if (type
== 0 && sect
->last
&& sect
->last
->type
== 0) {
234 sect
->last
->bytes
+= bytes
;
238 p
= sect
->last
= *sect
->tail
= nasm_malloc(sizeof(struct Piece
));
239 sect
->tail
= &p
->next
;
245 p
->relative
= relative
;
247 if (type
== 1 && segment
== stext
.index
)
248 p
->number
= SECT_TEXT
;
249 else if (type
== 1 && segment
== sdata
.index
)
250 p
->number
= SECT_DATA
;
251 else if (type
== 1 && segment
== bssindex
)
252 p
->number
= SECT_BSS
;
254 p
->number
= raa_read(bsym
, segment
), p
->type
= 2;
257 static void as86_out(int32_t segto
, const void *data
,
258 enum out_type type
, uint64_t size
,
259 int32_t segment
, int32_t wrt
)
263 uint8_t mydata
[4], *p
;
266 wrt
= NO_SEG
; /* continue to do _something_ */
267 error(ERR_NONFATAL
, "WRT not supported by as86 output format");
271 * handle absolute-assembly (structure definitions)
273 if (segto
== NO_SEG
) {
274 if (type
!= OUT_RESERVE
)
275 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
280 if (segto
== stext
.index
)
282 else if (segto
== sdata
.index
)
284 else if (segto
== bssindex
)
287 error(ERR_WARNING
, "attempt to assemble code in"
288 " segment %d: defaulting to `.text'", segto
);
292 if (!s
&& type
!= OUT_RESERVE
) {
293 error(ERR_WARNING
, "attempt to initialize memory in the"
294 " BSS section: ignored");
295 if (type
== OUT_REL2ADR
)
297 else if (type
== OUT_REL4ADR
)
303 if (type
== OUT_RESERVE
) {
305 error(ERR_WARNING
, "uninitialized space declared in"
306 " %s section: zeroing",
307 (segto
== stext
.index
? "code" : "data"));
308 as86_sect_write(s
, NULL
, size
);
309 as86_add_piece(s
, 0, 0L, 0L, size
, 0);
312 } else if (type
== OUT_RAWDATA
) {
313 if (segment
!= NO_SEG
)
314 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
315 as86_sect_write(s
, data
, size
);
316 as86_add_piece(s
, 0, 0L, 0L, size
, 0);
317 } else if (type
== OUT_ADDRESS
) {
318 if (segment
!= NO_SEG
) {
320 error(ERR_NONFATAL
, "as86 format does not support"
321 " segment base references");
323 offset
= *(int64_t *)data
;
324 as86_add_piece(s
, 1, offset
, segment
, size
, 0);
328 WRITELONG(p
, *(int64_t *)data
);
329 as86_sect_write(s
, data
, size
);
330 as86_add_piece(s
, 0, 0L, 0L, size
, 0);
332 } else if (type
== OUT_REL2ADR
) {
333 if (segment
== segto
)
334 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
335 if (segment
!= NO_SEG
) {
337 error(ERR_NONFATAL
, "as86 format does not support"
338 " segment base references");
340 offset
= *(int64_t *)data
;
341 as86_add_piece(s
, 1, offset
- size
+ 2, segment
, 2L,
345 } else if (type
== OUT_REL4ADR
) {
346 if (segment
== segto
)
347 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
348 if (segment
!= NO_SEG
) {
350 error(ERR_NONFATAL
, "as86 format does not support"
351 " segment base references");
353 offset
= *(int64_t *)data
;
354 as86_add_piece(s
, 1, offset
- size
+ 4, segment
, 4L,
361 static void as86_write(void)
364 int32_t symlen
, seglen
, segsize
;
367 * First, go through the symbol records working out how big
368 * each will be. Also fix up BSS references at this time, and
369 * set the flags words up completely.
373 for (i
= 0; i
< nsyms
; i
++) {
374 struct Symbol
*sym
= saa_rstruct(syms
);
375 if (sym
->segment
== SECT_BSS
)
376 sym
->segment
= SECT_DATA
, sym
->value
+= sdata
.len
;
377 sym
->flags
|= sym
->segment
;
379 sym
->flags
|= 0 << 14, symlen
+= 4;
380 else if (sym
->value
>= 0 && sym
->value
<= 255)
381 sym
->flags
|= 1 << 14, symlen
+= 5;
382 else if (sym
->value
>= 0 && sym
->value
<= 65535L)
383 sym
->flags
|= 2 << 14, symlen
+= 6;
385 sym
->flags
|= 3 << 14, symlen
+= 8;
389 * Now do the same for the segments, and get the segment size
390 * descriptor word at the same time.
392 seglen
= segsize
= 0;
393 if ((uint32_t)stext
.len
> 65535L)
394 segsize
|= 0x03000000L
, seglen
+= 4;
396 segsize
|= 0x02000000L
, seglen
+= 2;
397 if ((uint32_t)sdata
.len
> 65535L)
398 segsize
|= 0xC0000000L
, seglen
+= 4;
400 segsize
|= 0x80000000L
, seglen
+= 2;
403 * Emit the as86 header.
405 fwriteint32_t(0x000186A3L
, as86fp
);
407 fwriteint32_t(27 + symlen
+ seglen
+ strslen
, as86fp
); /* header length */
408 fwriteint32_t(stext
.len
+ sdata
.len
+ bsslen
, as86fp
);
409 fwriteint16_t(strslen
, as86fp
);
410 fwriteint16_t(0, as86fp
); /* class = revision = 0 */
411 fwriteint32_t(0x55555555L
, as86fp
); /* segment max sizes: always this */
412 fwriteint32_t(segsize
, as86fp
); /* segment size descriptors */
413 if (segsize
& 0x01000000L
)
414 fwriteint32_t(stext
.len
, as86fp
);
416 fwriteint16_t(stext
.len
, as86fp
);
417 if (segsize
& 0x40000000L
)
418 fwriteint32_t(sdata
.len
+ bsslen
, as86fp
);
420 fwriteint16_t(sdata
.len
+ bsslen
, as86fp
);
421 fwriteint16_t(nsyms
, as86fp
);
424 * Write the symbol table.
427 for (i
= 0; i
< nsyms
; i
++) {
428 struct Symbol
*sym
= saa_rstruct(syms
);
429 fwriteint16_t(sym
->strpos
, as86fp
);
430 fwriteint16_t(sym
->flags
, as86fp
);
431 switch (sym
->flags
& (3 << 14)) {
435 fputc(sym
->value
, as86fp
);
438 fwriteint16_t(sym
->value
, as86fp
);
441 fwriteint32_t(sym
->value
, as86fp
);
447 * Write out the string table.
449 saa_fpwrite(strs
, as86fp
);
452 * Write the program text.
454 as86_reloc_size
= -1;
455 as86_write_section(&stext
, SECT_TEXT
);
456 as86_write_section(&sdata
, SECT_DATA
);
458 * Append the BSS section to the .data section
460 if (bsslen
> 65535L) {
462 fwriteint32_t(bsslen
, as86fp
);
463 } else if (bsslen
> 255) {
465 fwriteint16_t(bsslen
, as86fp
);
468 fputc(bsslen
, as86fp
);
471 fputc(0, as86fp
); /* termination */
474 static void as86_set_rsize(int size
)
476 if (as86_reloc_size
!= size
) {
477 switch (as86_reloc_size
= size
) {
488 error(ERR_PANIC
, "bizarre relocation size %d", size
);
493 static void as86_write_section(struct Section
*sect
, int index
)
499 fputc(0x20 + index
, as86fp
); /* select the right section */
501 saa_rewind(sect
->data
);
503 for (p
= sect
->head
; p
; p
= p
->next
)
507 * Absolute data. Emit it in chunks of at most 64
513 int32_t tmplen
= (length
> 64 ? 64 : length
);
514 fputc(0x40 | (tmplen
& 0x3F), as86fp
);
515 saa_rnbytes(sect
->data
, buf
, tmplen
);
516 fwrite(buf
, 1, tmplen
, as86fp
);
518 } while (length
> 0);
522 * A segment-type relocation. First fix up the BSS.
524 if (p
->number
== SECT_BSS
)
525 p
->number
= SECT_DATA
, p
->offset
+= sdata
.len
;
526 as86_set_rsize(p
->bytes
);
527 fputc(0x80 | (p
->relative
? 0x20 : 0) | p
->number
, as86fp
);
528 if (as86_reloc_size
== 2)
529 fwriteint16_t(p
->offset
, as86fp
);
531 fwriteint32_t(p
->offset
, as86fp
);
535 * A symbol-type relocation.
537 as86_set_rsize(p
->bytes
);
548 (p
->relative
? 0x20 : 0) |
549 (p
->number
> 255 ? 0x04 : 0) | s
, as86fp
);
551 fwriteint16_t(p
->number
, as86fp
);
553 fputc(p
->number
, as86fp
);
558 fputc(p
->offset
, as86fp
);
561 fwriteint16_t(p
->offset
, as86fp
);
564 fwriteint32_t(p
->offset
, as86fp
);
571 static void as86_sect_write(struct Section
*sect
,
572 const uint8_t *data
, uint32_t len
)
574 saa_wbytes(sect
->data
, data
, len
);
575 sect
->datalen
+= len
;
578 static int32_t as86_segbase(int32_t segment
)
583 static int as86_directive(char *directive
, char *value
, int pass
)
591 static void as86_filename(char *inname
, char *outname
, efunc error
)
595 if ((p
= strrchr(inname
, '.')) != NULL
) {
596 strncpy(as86_module
, inname
, p
- inname
);
597 as86_module
[p
- inname
] = '\0';
599 strcpy(as86_module
, inname
);
601 standard_extension(inname
, outname
, ".o", error
);
604 static const char *as86_stdmac
[] = {
605 "%define __SECT__ [section .text]",
606 "%macro __NASM_CDecl__ 1",
611 static int as86_set_info(enum geninfo type
, char **val
)
617 void as86_linenumber(char *name
, int32_t segment
, int32_t offset
, int is_main
,
626 struct ofmt of_as86
= {
627 "Linux as86 (bin86 version 0.3) object files",