1 /* outcoff.c output routines for the Netwide Assembler to produce
2 * COFF object files (for DJGPP and Win32)
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.
20 #if defined(OF_COFF) || defined(OF_WIN32)
25 * (0) When I say `standard COFF' below, I mean `COFF as output and
26 * used by DJGPP'. I assume DJGPP gets it right.
28 * (1) Win32 appears to interpret the term `relative relocation'
29 * differently from standard COFF. Standard COFF understands a
30 * relative relocation to mean that during relocation you add the
31 * address of the symbol you're referencing, and subtract the base
32 * address of the section you're in. Win32 COFF, by contrast, seems
33 * to add the address of the symbol and then subtract the address
34 * of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
35 * subtly incompatible.
37 * (2) Win32 doesn't bother putting any flags in the header flags
38 * field (at offset 0x12 into the file).
40 * (3) Win32 puts some weird flags into the section header table.
41 * It uses flags 0x80000000 (writable), 0x40000000 (readable) and
42 * 0x20000000 (executable) in the expected combinations, which
43 * standard COFF doesn't seem to bother with, but it also does
44 * something else strange: it also flags code sections as
45 * 0x00500000 and data/bss as 0x00300000. Even Microsoft's
46 * documentation doesn't explain what these things mean. I just go
47 * ahead and use them anyway - it seems to work.
49 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
50 * field directly after the section name in the section header
51 * table for something strange: they store what the address of the
52 * section start point _would_ be, if you laid all the sections end
53 * to end starting at zero. Dunno why. Microsoft's documentation
54 * lists this field as "Virtual Size of Section", which doesn't
55 * seem to fit at all. In fact, Win32 even includes non-linked
56 * sections such as .drectve in this calculation. Not that I can be
57 * bothered with those things anyway.
59 * (5) Standard COFF does something very strange to common
60 * variables: the relocation point for a common variable is as far
61 * _before_ the variable as its size stretches out _after_ it. So
62 * we must fix up common variable references. Win32 seems to be
63 * sensible on this one.
66 /* Flag which version of COFF we are currently outputting. */
71 long address
; /* relative to _start_ of section */
72 long symbol
; /* symbol number */
77 } symbase
; /* relocation for symbol number :) */
78 int relative
; /* TRUE or FALSE */
83 long strpos
; /* string table position of name */
84 int section
; /* section number where it's defined
85 * - in COFF codes, not NASM codes */
86 int is_global
; /* is it a global symbol or not? */
87 long value
; /* address, or COMMON variable size */
92 static char coff_infile
[FILENAME_MAX
];
99 struct Reloc
*head
, **tail
;
100 unsigned long flags
; /* section flags */
105 #define TEXT_FLAGS (win32 ? 0x60500020L : 0x20L)
106 #define DATA_FLAGS (win32 ? 0xC0300040L : 0x40L)
107 #define BSS_FLAGS (win32 ? 0xC0300080L : 0x80L)
108 #define INFO_FLAGS 0x00100A00L
110 #define SECT_DELTA 32
111 static struct Section
**sects
;
112 static int nsects
, sectlen
;
114 static struct SAA
*syms
;
115 static unsigned long nsyms
;
121 static struct RAA
*bsym
, *symval
;
123 static struct SAA
*strs
;
124 static unsigned long strslen
;
126 static void coff_gen_init(FILE *, efunc
);
127 static void coff_sect_write (struct Section
*, unsigned char *,
129 static void coff_write (void);
130 static void coff_section_header (char *, long, long, long, long, int, long);
131 static void coff_write_relocs (struct Section
*);
132 static void coff_write_symbols (void);
134 static void coff_win32_init(FILE *fp
, efunc errfunc
, ldfunc ldef
) {
136 (void) ldef
; /* placate optimisers */
137 coff_gen_init(fp
, errfunc
);
140 static void coff_std_init(FILE *fp
, efunc errfunc
, ldfunc ldef
) {
142 (void) ldef
; /* placate optimisers */
143 coff_gen_init(fp
, errfunc
);
146 static void coff_gen_init(FILE *fp
, efunc errfunc
) {
150 nsects
= sectlen
= 0;
151 syms
= saa_init((long)sizeof(struct Symbol
));
157 def_seg
= seg_alloc();
160 static void coff_cleanup(void) {
166 for (i
=0; i
<nsects
; i
++) {
168 saa_free (sects
[i
]->data
);
169 while (sects
[i
]->head
) {
171 sects
[i
]->head
= sects
[i
]->head
->next
;
182 static int coff_make_section (char *name
, unsigned long flags
) {
185 s
= nasm_malloc (sizeof(*s
));
187 if (flags
!= BSS_FLAGS
)
188 s
->data
= saa_init (1L);
195 if (!strcmp(name
, ".text"))
198 s
->index
= seg_alloc();
199 strncpy (s
->name
, name
, 8);
203 if (nsects
>= sectlen
)
204 sects
= nasm_realloc (sects
, (sectlen
+= SECT_DELTA
)*sizeof(*sects
));
210 static long coff_section_names (char *name
, int pass
, int *bits
) {
216 * Default is 32 bits.
225 while (*p
&& !isspace(*p
)) p
++;
228 error (ERR_WARNING
, "COFF section names limited to 8 characters:"
234 while (*p
&& isspace(*p
)) p
++;
237 while (*p
&& !isspace(*p
)) p
++;
239 while (*p
&& isspace(*p
)) p
++;
241 if (!nasm_stricmp(q
, "code") || !nasm_stricmp(q
, "text")) {
243 } else if (!nasm_stricmp(q
, "data")) {
245 } else if (!nasm_stricmp(q
, "bss")) {
247 } else if (!nasm_stricmp(q
, "info")) {
251 flags
= DATA_FLAGS
; /* gotta do something */
252 error (ERR_NONFATAL
, "standard COFF does not support"
253 " informational sections");
258 for (i
=0; i
<nsects
; i
++)
259 if (!strcmp(name
, sects
[i
]->name
))
262 if (!strcmp(name
, ".text") && !flags
)
263 i
= coff_make_section (name
, TEXT_FLAGS
);
264 else if (!strcmp(name
, ".data") && !flags
)
265 i
= coff_make_section (name
, DATA_FLAGS
);
266 else if (!strcmp(name
, ".bss") && !flags
)
267 i
= coff_make_section (name
, BSS_FLAGS
);
269 i
= coff_make_section (name
, flags
);
271 i
= coff_make_section (name
, TEXT_FLAGS
);
273 sects
[i
]->flags
= flags
;
274 } else if (pass
== 1) {
276 error (ERR_WARNING
, "section attributes ignored on"
277 " redeclaration of section `%s'", name
);
280 return sects
[i
]->index
;
283 static void coff_deflabel (char *name
, long segment
, long offset
,
288 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
289 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
293 if (strlen(name
) > 8) {
294 saa_wbytes (strs
, name
, (long)(1+strlen(name
)));
295 strslen
+= 1+strlen(name
);
299 sym
= saa_wstruct (syms
);
303 strcpy (sym
->name
, name
);
304 sym
->is_global
= !!is_global
;
305 if (segment
== NO_SEG
)
306 sym
->section
= -1; /* absolute symbol */
310 for (i
=0; i
<nsects
; i
++)
311 if (segment
== sects
[i
]->index
) {
316 sym
->is_global
= TRUE
;
321 sym
->value
= (sym
->section
== 0 ? 0 : offset
);
324 * define the references from external-symbol segment numbers
325 * to these symbol records.
327 if (sym
->section
== 0)
328 bsym
= raa_write (bsym
, segment
, nsyms
);
330 if (segment
!= NO_SEG
)
331 symval
= raa_write (symval
, segment
, sym
->section
? 0 : sym
->value
);
336 static long coff_add_reloc (struct Section
*sect
, long segment
,
340 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
341 sect
->tail
= &r
->next
;
344 r
->address
= sect
->len
;
345 if (segment
== NO_SEG
)
346 r
->symbol
= 0, r
->symbase
= ABS_SYMBOL
;
349 r
->symbase
= REAL_SYMBOLS
;
350 for (i
=0; i
<nsects
; i
++)
351 if (segment
== sects
[i
]->index
) {
353 r
->symbase
= SECT_SYMBOLS
;
356 if (r
->symbase
== REAL_SYMBOLS
)
357 r
->symbol
= raa_read (bsym
, segment
);
359 r
->relative
= relative
;
364 * Return the fixup for standard COFF common variables.
366 if (r
->symbase
== REAL_SYMBOLS
&& !win32
)
367 return raa_read (symval
, segment
);
372 static void coff_out (long segto
, void *data
, unsigned long type
,
373 long segment
, long wrt
) {
375 long realbytes
= type
& OUT_SIZMASK
;
376 unsigned char mydata
[4], *p
;
380 wrt
= NO_SEG
; /* continue to do _something_ */
381 error (ERR_NONFATAL
, "WRT not supported by COFF output formats");
387 * handle absolute-assembly (structure definitions)
389 if (segto
== NO_SEG
) {
390 if (type
!= OUT_RESERVE
)
391 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
397 for (i
=0; i
<nsects
; i
++)
398 if (segto
== sects
[i
]->index
) {
403 int tempint
; /* ignored */
404 if (segto
!= coff_section_names (".text", 2, &tempint
))
405 error (ERR_PANIC
, "strange segment conditions in COFF driver");
410 if (!s
->data
&& type
!= OUT_RESERVE
) {
411 error(ERR_WARNING
, "attempt to initialise memory in"
412 " BSS section `%s': ignored", s
->name
);
413 if (type
== OUT_REL2ADR
)
415 else if (type
== OUT_REL4ADR
)
421 if (type
== OUT_RESERVE
) {
423 error(ERR_WARNING
, "uninitialised space declared in"
424 " non-BSS section `%s': zeroing", s
->name
);
425 coff_sect_write (s
, NULL
, realbytes
);
428 } else if (type
== OUT_RAWDATA
) {
429 if (segment
!= NO_SEG
)
430 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
431 coff_sect_write (s
, data
, realbytes
);
432 } else if (type
== OUT_ADDRESS
) {
433 if (realbytes
== 2 && (segment
!= NO_SEG
|| wrt
!= NO_SEG
))
434 error(ERR_NONFATAL
, "COFF format does not support 16-bit"
438 if (segment
!= NO_SEG
|| wrt
!= NO_SEG
) {
440 error(ERR_NONFATAL
, "COFF format does not support"
442 } else if (segment
% 2) {
443 error(ERR_NONFATAL
, "COFF format does not support"
444 " segment base references");
446 fix
= coff_add_reloc (s
, segment
, FALSE
);
449 WRITELONG (p
, *(long *)data
+ fix
);
450 coff_sect_write (s
, mydata
, realbytes
);
452 } else if (type
== OUT_REL2ADR
) {
453 error(ERR_NONFATAL
, "COFF format does not support 16-bit"
455 } else if (type
== OUT_REL4ADR
) {
456 if (segment
== segto
)
457 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
458 else if (segment
== NO_SEG
&& win32
)
459 error(ERR_NONFATAL
, "Win32 COFF does not correctly support"
460 " relative references to absolute addresses");
463 if (segment
!= NO_SEG
&& segment
% 2) {
464 error(ERR_NONFATAL
, "COFF format does not support"
465 " segment base references");
467 fix
= coff_add_reloc (s
, segment
, TRUE
);
470 WRITELONG (p
, *(long*)data
+ 4 - realbytes
+ fix
);
472 WRITELONG (p
, *(long*)data
-(realbytes
+ s
->len
) + fix
);
474 coff_sect_write (s
, mydata
, 4L);
479 static void coff_sect_write (struct Section
*sect
,
480 unsigned char *data
, unsigned long len
) {
481 saa_wbytes (sect
->data
, data
, len
);
485 static int coff_directives (char *directive
, char *value
, int pass
) {
489 static void coff_write (void) {
490 long pos
, sympos
, vsize
;
494 * Work out how big the file will get. Calculate the start of
495 * the `real' symbols at the same time.
497 pos
= 0x14 + 0x28 * nsects
;
498 initsym
= 3; /* two for the file, one absolute */
499 for (i
=0; i
<nsects
; i
++) {
500 if (sects
[i
]->data
) {
502 pos
+= sects
[i
]->len
;
503 sects
[i
]->relpos
= pos
;
504 pos
+= 10 * sects
[i
]->nrelocs
;
506 sects
[i
]->pos
= sects
[i
]->relpos
= 0L;
507 initsym
+= 2; /* two for each section */
512 * Output the COFF header.
514 fwriteshort (0x14C, coffp
); /* MACHINE_i386 */
515 fwriteshort (nsects
, coffp
); /* number of sections */
516 fwritelong (time(NULL
), coffp
); /* time stamp */
517 fwritelong (sympos
, coffp
);
518 fwritelong (nsyms
+ initsym
, coffp
);
519 fwriteshort (0, coffp
); /* no optional header */
520 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
521 fwriteshort (win32
? 0 : 0x104, coffp
);
524 * Output the section headers.
527 for (i
=0; i
<nsects
; i
++) {
528 coff_section_header (sects
[i
]->name
, vsize
, sects
[i
]->len
,
529 sects
[i
]->pos
, sects
[i
]->relpos
,
530 sects
[i
]->nrelocs
, sects
[i
]->flags
);
531 vsize
+= sects
[i
]->len
;
535 * Output the sections and their relocations.
537 for (i
=0; i
<nsects
; i
++)
538 if (sects
[i
]->data
) {
539 saa_fpwrite (sects
[i
]->data
, coffp
);
540 coff_write_relocs (sects
[i
]);
544 * Output the symbol and string tables.
546 coff_write_symbols();
547 fwritelong (strslen
+4, coffp
); /* length includes length count */
548 saa_fpwrite (strs
, coffp
);
551 static void coff_section_header (char *name
, long vsize
,
552 long datalen
, long datapos
,
553 long relpos
, int nrelocs
, long flags
) {
556 memset (padname
, 0, 8);
557 strncpy (padname
, name
, 8);
558 fwrite (padname
, 8, 1, coffp
);
559 fwritelong (vsize
, coffp
);
560 fwritelong (0L, coffp
); /* RVA/offset - we ignore */
561 fwritelong (datalen
, coffp
);
562 fwritelong (datapos
, coffp
);
563 fwritelong (relpos
, coffp
);
564 fwritelong (0L, coffp
); /* no line numbers - we don't do 'em */
565 fwriteshort (nrelocs
, coffp
);
566 fwriteshort (0, coffp
); /* again, no line numbers */
567 fwritelong (flags
, coffp
);
570 static void coff_write_relocs (struct Section
*s
) {
573 for (r
= s
->head
; r
; r
= r
->next
) {
574 fwritelong (r
->address
, coffp
);
575 fwritelong (r
->symbol
+ (r
->symbase
== REAL_SYMBOLS
? initsym
:
576 r
->symbase
== ABS_SYMBOL
? initsym
-1 :
577 r
->symbase
== SECT_SYMBOLS
? 2 : 0), coffp
);
579 * Strange: Microsoft's COFF documentation says 0x03 for an
580 * absolute relocation, but both Visual C++ and DJGPP agree
581 * that in fact it's 0x06. I'll use 0x06 until someone
584 fwriteshort (r
->relative
? 0x14 : 0x06, coffp
);
588 static void coff_symbol (char *name
, long strpos
, long value
,
589 int section
, int type
, int aux
) {
593 memset (padname
, 0, 8);
594 strncpy (padname
, name
, 8);
595 fwrite (padname
, 8, 1, coffp
);
597 fwritelong (0L, coffp
);
598 fwritelong (strpos
, coffp
);
600 fwritelong (value
, coffp
);
601 fwriteshort (section
, coffp
);
602 fwriteshort (0, coffp
);
607 static void coff_write_symbols (void) {
612 * The `.file' record, and the file name auxiliary record.
614 coff_symbol (".file", 0L, 0L, -2, 0x67, 1);
615 memset (filename
, 0, 18);
616 strncpy (filename
, coff_infile
, 18);
617 fwrite (filename
, 18, 1, coffp
);
620 * The section records, with their auxiliaries.
622 memset (filename
, 0, 18); /* useful zeroed buffer */
624 for (i
=0; i
<nsects
; i
++) {
625 coff_symbol (sects
[i
]->name
, 0L, 0L, i
+1, 3, 1);
626 fwritelong (sects
[i
]->len
, coffp
);
627 fwriteshort (sects
[i
]->nrelocs
, coffp
);
628 fwrite (filename
, 12, 1, coffp
);
632 * The absolute symbol, for relative-to-absolute relocations.
634 coff_symbol (".absolut", 0L, 0L, -1, 3, 0);
640 for (i
=0; i
<nsyms
; i
++) {
641 struct Symbol
*sym
= saa_rstruct (syms
);
642 coff_symbol (sym
->strpos
== -1 ? sym
->name
: NULL
,
643 sym
->strpos
, sym
->value
, sym
->section
,
644 sym
->is_global
? 2 : 3, 0);
648 static long coff_segbase (long segment
) {
652 static void coff_std_filename (char *inname
, char *outname
, efunc error
) {
653 strcpy(coff_infile
, inname
);
654 standard_extension (inname
, outname
, ".o", error
);
657 static void coff_win32_filename (char *inname
, char *outname
, efunc error
) {
658 strcpy(coff_infile
, inname
);
659 standard_extension (inname
, outname
, ".obj", error
);
662 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
666 struct ofmt of_coff
= {
667 "COFF (i386) object files (e.g. DJGPP for DOS)",
683 struct ofmt of_win32
= {
684 "Microsoft Win32 (i386) object files",