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 */
73 int relative
; /* TRUE or FALSE */
78 long strpos
; /* string table position of name */
79 int section
; /* section number where it's defined
80 * - in COFF codes, not NASM codes */
81 int is_global
; /* is it a global symbol or not? */
82 long value
; /* address, or COMMON variable size */
87 static char coff_infile
[FILENAME_MAX
];
94 struct Reloc
*head
, **tail
;
97 static struct Section stext
, sdata
;
98 static unsigned long bsslen
;
101 static struct SAA
*syms
;
102 static unsigned long nsyms
;
104 static struct RAA
*bsym
, *symval
;
106 static struct SAA
*strs
;
107 static unsigned long strslen
;
110 * The symbol table contains a double entry for the file name, a
111 * double entry for each of the three sections, and an absolute
112 * symbol referencing address zero, followed by the _real_ symbols.
113 * That's nine extra symbols.
115 #define SYM_INITIAL 9
118 * Symbol table indices we can relocate relative to.
120 #define SYM_ABS_SEG 8
121 #define SYM_TEXT_SEG 2
122 #define SYM_DATA_SEG 4
123 #define SYM_BSS_SEG 6
126 * The section header table ends at this offset: 0x14 for the
127 * header, plus 0x28 for each of three sections.
129 #define COFF_HDRS_END 0x8c
131 static void coff_gen_init(FILE *, efunc
);
132 static void coff_sect_write (struct Section
*, unsigned char *,
134 static void coff_write (void);
135 static void coff_section_header (char *, long, long, long, long, int, long);
136 static void coff_write_relocs (struct Section
*);
137 static void coff_write_symbols (void);
139 static void coff_win32_init(FILE *fp
, efunc errfunc
, ldfunc ldef
) {
141 (void) ldef
; /* placate optimisers */
142 coff_gen_init(fp
, errfunc
);
145 static void coff_std_init(FILE *fp
, efunc errfunc
, ldfunc ldef
) {
147 (void) ldef
; /* placate optimisers */
148 coff_gen_init(fp
, errfunc
);
151 static void coff_gen_init(FILE *fp
, efunc errfunc
) {
154 stext
.data
= saa_init(1L); stext
.head
= NULL
; stext
.tail
= &stext
.head
;
155 sdata
.data
= saa_init(1L); sdata
.head
= NULL
; sdata
.tail
= &sdata
.head
;
156 stext
.len
= sdata
.len
= bsslen
= 0;
157 stext
.nrelocs
= sdata
.nrelocs
= 0;
158 stext
.index
= seg_alloc();
159 sdata
.index
= seg_alloc();
160 bssindex
= seg_alloc();
161 syms
= saa_init((long)sizeof(struct Symbol
));
169 static void coff_cleanup(void) {
174 saa_free (stext
.data
);
177 stext
.head
= stext
.head
->next
;
180 saa_free (sdata
.data
);
183 sdata
.head
= sdata
.head
->next
;
192 static long coff_section_names (char *name
, int pass
, int *bits
) {
194 * Default is 32 bits.
202 if (!strcmp(name
, ".text"))
204 else if (!strcmp(name
, ".data"))
206 else if (!strcmp(name
, ".bss"))
212 static void coff_deflabel (char *name
, long segment
, long offset
,
217 if (name
[0] == '.' && name
[1] == '.') {
221 if (strlen(name
) > 8) {
222 saa_wbytes (strs
, name
, (long)(1+strlen(name
)));
223 strslen
+= 1+strlen(name
);
227 sym
= saa_wstruct (syms
);
231 strcpy (sym
->name
, name
);
232 sym
->is_global
= !!is_global
;
233 if (segment
== NO_SEG
)
234 sym
->section
= -1; /* absolute symbol */
235 else if (segment
== stext
.index
)
236 sym
->section
= 1; /* .text */
237 else if (segment
== sdata
.index
)
238 sym
->section
= 2; /* .data */
239 else if (segment
== bssindex
)
240 sym
->section
= 3; /* .bss */
242 sym
->section
= 0; /* undefined */
243 sym
->is_global
= TRUE
;
248 sym
->value
= (sym
->section
== 0 ? 0 : offset
);
251 * define the references from external-symbol segment numbers
252 * to these symbol records.
254 if (segment
!= NO_SEG
&& segment
!= stext
.index
&&
255 segment
!= sdata
.index
&& segment
!= bssindex
)
256 bsym
= raa_write (bsym
, segment
, nsyms
);
258 if (segment
!= NO_SEG
)
259 symval
= raa_write (symval
, segment
, sym
->section
? 0 : sym
->value
);
264 static long coff_add_reloc (struct Section
*sect
, long segment
,
268 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
269 sect
->tail
= &r
->next
;
272 r
->address
= sect
->len
;
273 r
->symbol
= (segment
== NO_SEG
? SYM_ABS_SEG
:
274 segment
== stext
.index
? SYM_TEXT_SEG
:
275 segment
== sdata
.index
? SYM_DATA_SEG
:
276 segment
== bssindex
? SYM_BSS_SEG
:
277 raa_read (bsym
, segment
) + SYM_INITIAL
);
278 r
->relative
= relative
;
283 * Return the fixup for standard COFF common variables.
285 if (r
->symbol
>= SYM_INITIAL
&& !win32
)
286 return raa_read (symval
, segment
);
291 static void coff_out (long segto
, void *data
, unsigned long type
,
292 long segment
, long wrt
) {
294 long realbytes
= type
& OUT_SIZMASK
;
295 unsigned char mydata
[4], *p
;
298 wrt
= NO_SEG
; /* continue to do _something_ */
299 error (ERR_NONFATAL
, "WRT not supported by COFF output formats");
305 * handle absolute-assembly (structure definitions)
307 if (segto
== NO_SEG
) {
308 if (type
!= OUT_RESERVE
)
309 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
314 if (segto
== stext
.index
)
316 else if (segto
== sdata
.index
)
318 else if (segto
== bssindex
)
321 error(ERR_WARNING
, "attempt to assemble code in"
322 " segment %d: defaulting to `.text'", segto
);
326 if (!s
&& type
!= OUT_RESERVE
) {
327 error(ERR_WARNING
, "attempt to initialise memory in the"
328 " BSS section: ignored");
329 if (type
== OUT_REL2ADR
)
331 else if (type
== OUT_REL4ADR
)
337 if (type
== OUT_RESERVE
) {
339 error(ERR_WARNING
, "uninitialised space declared in"
340 " %s section: zeroing",
341 (segto
== stext
.index
? "code" : "data"));
342 coff_sect_write (s
, NULL
, realbytes
);
345 } else if (type
== OUT_RAWDATA
) {
346 if (segment
!= NO_SEG
)
347 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
348 coff_sect_write (s
, data
, realbytes
);
349 } else if (type
== OUT_ADDRESS
) {
350 if (realbytes
== 2 && (segment
!= NO_SEG
|| wrt
!= NO_SEG
))
351 error(ERR_NONFATAL
, "COFF format does not support 16-bit"
355 if (segment
!= NO_SEG
|| wrt
!= NO_SEG
) {
357 error(ERR_NONFATAL
, "COFF format does not support"
359 } else if (segment
% 2) {
360 error(ERR_NONFATAL
, "COFF format does not support"
361 " segment base references");
363 fix
= coff_add_reloc (s
, segment
, FALSE
);
366 WRITELONG (p
, *(long *)data
+ fix
);
367 coff_sect_write (s
, mydata
, realbytes
);
369 } else if (type
== OUT_REL2ADR
) {
370 error(ERR_NONFATAL
, "COFF format does not support 16-bit"
372 } else if (type
== OUT_REL4ADR
) {
373 if (segment
== segto
)
374 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
375 else if (segment
== NO_SEG
&& win32
)
376 error(ERR_NONFATAL
, "Win32 COFF does not correctly support"
377 " relative references to absolute addresses");
380 if (segment
!= NO_SEG
&& segment
% 2) {
381 error(ERR_NONFATAL
, "COFF format does not support"
382 " segment base references");
384 fix
= coff_add_reloc (s
, segment
, TRUE
);
387 WRITELONG (p
, *(long*)data
+ 4 - realbytes
+ fix
);
389 WRITELONG (p
, *(long*)data
-(realbytes
+ s
->len
) + fix
);
391 coff_sect_write (s
, mydata
, 4L);
396 static void coff_sect_write (struct Section
*sect
,
397 unsigned char *data
, unsigned long len
) {
398 saa_wbytes (sect
->data
, data
, len
);
402 static int coff_directives (char *directive
, char *value
, int pass
) {
406 static void coff_write (void) {
407 long textpos
, textrelpos
, datapos
, datarelpos
, sympos
;
410 * Work out how big the file will get.
412 textpos
= COFF_HDRS_END
;
413 textrelpos
= textpos
+ stext
.len
;
414 datapos
= textrelpos
+ stext
.nrelocs
* 10;
415 datarelpos
= datapos
+ sdata
.len
;
416 sympos
= datarelpos
+ sdata
.nrelocs
* 10;
419 * Output the COFF header.
421 fwriteshort (0x14C, coffp
); /* MACHINE_i386 */
422 fwriteshort (3, coffp
); /* number of sections */
423 fwritelong (time(NULL
), coffp
); /* time stamp */
424 fwritelong (sympos
, coffp
);
425 fwritelong (nsyms
+ SYM_INITIAL
, coffp
);
426 fwriteshort (0, coffp
); /* no optional header */
427 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
428 fwriteshort (win32
? 0 : 0x104, coffp
);
431 * Output the section headers.
434 coff_section_header (".text", 0L, stext
.len
, textpos
,
435 textrelpos
, stext
.nrelocs
,
436 (win32
? 0x60500020L
: 0x20L
));
437 coff_section_header (".data", stext
.len
, sdata
.len
, datapos
,
438 datarelpos
, sdata
.nrelocs
,
439 (win32
? 0xC0300040L
: 0x40L
));
440 coff_section_header (".bss", stext
.len
+sdata
.len
, bsslen
, 0L, 0L, 0,
441 (win32
? 0xC0300080L
: 0x80L
));
444 * Output the text section, and its relocations.
446 saa_fpwrite (stext
.data
, coffp
);
447 coff_write_relocs (&stext
);
450 * Output the data section, and its relocations.
452 saa_fpwrite (sdata
.data
, coffp
);
453 coff_write_relocs (&sdata
);
456 * Output the symbol and string tables.
458 coff_write_symbols();
459 fwritelong (strslen
+4, coffp
); /* length includes length count */
460 saa_fpwrite (strs
, coffp
);
463 static void coff_section_header (char *name
, long vsize
,
464 long datalen
, long datapos
,
465 long relpos
, int nrelocs
, long flags
) {
468 memset (padname
, 0, 8);
469 strncpy (padname
, name
, 8);
470 fwrite (padname
, 8, 1, coffp
);
471 fwritelong (vsize
, coffp
);
472 fwritelong (0L, coffp
); /* RVA/offset - we ignore */
473 fwritelong (datalen
, coffp
);
474 fwritelong (datapos
, coffp
);
475 fwritelong (relpos
, coffp
);
476 fwritelong (0L, coffp
); /* no line numbers - we don't do 'em */
477 fwriteshort (nrelocs
, coffp
);
478 fwriteshort (0, coffp
); /* again, no line numbers */
479 fwritelong (flags
, coffp
);
482 static void coff_write_relocs (struct Section
*s
) {
485 for (r
= s
->head
; r
; r
= r
->next
) {
486 fwritelong (r
->address
, coffp
);
487 fwritelong (r
->symbol
, coffp
);
489 * Strange: Microsoft's COFF documentation says 0x03 for an
490 * absolute relocation, but both Visual C++ and DJGPP agree
491 * that in fact it's 0x06. I'll use 0x06 until someone
494 fwriteshort (r
->relative
? 0x14 : 0x06, coffp
);
498 static void coff_symbol (char *name
, long strpos
, long value
,
499 int section
, int type
, int aux
) {
503 memset (padname
, 0, 8);
504 strncpy (padname
, name
, 8);
505 fwrite (padname
, 8, 1, coffp
);
507 fwritelong (0L, coffp
);
508 fwritelong (strpos
, coffp
);
510 fwritelong (value
, coffp
);
511 fwriteshort (section
, coffp
);
512 fwriteshort (0, coffp
);
517 static void coff_write_symbols (void) {
522 * The `.file' record, and the file name auxiliary record.
524 coff_symbol (".file", 0L, 0L, -2, 0x67, 1);
525 memset (filename
, 0, 18);
526 strncpy (filename
, coff_infile
, 18);
527 fwrite (filename
, 18, 1, coffp
);
530 * The section records, with their auxiliaries.
532 memset (filename
, 0, 18); /* useful zeroed buffer */
534 coff_symbol (".text", 0L, 0L, 1, 3, 1);
535 fwritelong (stext
.len
, coffp
);
536 fwriteshort (stext
.nrelocs
, coffp
);
537 fwrite (filename
, 12, 1, coffp
);
538 coff_symbol (".data", 0L, 0L, 2, 3, 1);
539 fwritelong (sdata
.len
, coffp
);
540 fwriteshort (sdata
.nrelocs
, coffp
);
541 fwrite (filename
, 12, 1, coffp
);
542 coff_symbol (".bss", 0L, 0L, 3, 3, 1);
543 fwritelong (bsslen
, coffp
);
544 fwrite (filename
, 14, 1, coffp
);
547 * The absolute symbol, for relative-to-absolute relocations.
549 coff_symbol (".absolut", 0L, 0L, -1, 3, 0);
555 for (i
=0; i
<nsyms
; i
++) {
556 struct Symbol
*sym
= saa_rstruct (syms
);
557 coff_symbol (sym
->strpos
== -1 ? sym
->name
: NULL
,
558 sym
->strpos
, sym
->value
, sym
->section
,
559 sym
->is_global
? 2 : 3, 0);
563 static long coff_segbase (long segment
) {
567 static void coff_std_filename (char *inname
, char *outname
, efunc error
) {
568 strcpy(coff_infile
, inname
);
569 standard_extension (inname
, outname
, ".o", error
);
572 static void coff_win32_filename (char *inname
, char *outname
, efunc error
) {
573 strcpy(coff_infile
, inname
);
574 standard_extension (inname
, outname
, ".obj", error
);
577 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
581 struct ofmt of_coff
= {
582 "COFF (i386) object files (e.g. DJGPP for DOS)",
598 struct ofmt of_win32
= {
599 "Microsoft Win32 (i386) object files",