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.
21 #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
26 * (0) When I say `standard COFF' below, I mean `COFF as output and
27 * used by DJGPP'. I assume DJGPP gets it right.
29 * (1) Win32 appears to interpret the term `relative relocation'
30 * differently from standard COFF. Standard COFF understands a
31 * relative relocation to mean that during relocation you add the
32 * address of the symbol you're referencing, and subtract the base
33 * address of the section you're in. Win32 COFF, by contrast, seems
34 * to add the address of the symbol and then subtract the address
35 * of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
36 * subtly incompatible.
38 * (2) Win32 doesn't bother putting any flags in the header flags
39 * field (at offset 0x12 into the file).
41 * (3) Win32 uses some extra flags into the section header table:
42 * it defines flags 0x80000000 (writable), 0x40000000 (readable)
43 * and 0x20000000 (executable), and uses them in the expected
44 * combinations. It also defines 0x00100000 through 0x00700000 for
45 * section alignments of 1 through 64 bytes.
47 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
48 * field directly after the section name in the section header
49 * table for something strange: they store what the address of the
50 * section start point _would_ be, if you laid all the sections end
51 * to end starting at zero. Dunno why. Microsoft's documentation
52 * lists this field as "Virtual Size of Section", which doesn't
53 * seem to fit at all. In fact, Win32 even includes non-linked
54 * sections such as .drectve in this calculation.
56 * (5) Standard COFF does something very strange to common
57 * variables: the relocation point for a common variable is as far
58 * _before_ the variable as its size stretches out _after_ it. So
59 * we must fix up common variable references. Win32 seems to be
60 * sensible on this one.
63 /* Flag which version of COFF we are currently outputting. */
64 static int win32
, win64
;
68 int32_t address
; /* relative to _start_ of section */
69 int32_t symbol
; /* symbol number */
74 } symbase
; /* relocation for symbol number :) */
75 int relative
; /* TRUE or FALSE */
76 int size64
; /* TRUE or FALSE */
81 int32_t strpos
; /* string table position of name */
82 int section
; /* section number where it's defined
83 * - in COFF codes, not NASM codes */
84 int is_global
; /* is it a global symbol or not? */
85 int32_t value
; /* address, or COMMON variable size */
90 static char coff_infile
[FILENAME_MAX
];
97 struct Reloc
*head
, **tail
;
98 uint32_t flags
; /* section flags */
103 #define TEXT_FLAGS ((win32 | win64) ? 0x60500020L : 0x20L)
104 #define DATA_FLAGS ((win32 | win64) ? 0xC0300040L : 0x40L)
105 #define BSS_FLAGS ((win32 | win64) ? 0xC0300080L : 0x80L)
106 #define INFO_FLAGS 0x00100A00L
107 #define RDATA_FLAGS ((win32 | win64) ? 0x40400040L : 0x40L)
109 #define SECT_DELTA 32
110 static struct Section
**sects
;
111 static int nsects
, sectlen
;
113 static struct SAA
*syms
;
114 static uint32_t nsyms
;
116 static int32_t def_seg
;
120 static struct RAA
*bsym
, *symval
;
122 static struct SAA
*strs
;
123 static uint32_t strslen
;
125 static void coff_gen_init(FILE *, efunc
);
126 static void coff_sect_write(struct Section
*, const uint8_t *,
128 static void coff_write(void);
129 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
130 static void coff_write_relocs(struct Section
*);
131 static void coff_write_symbols(void);
133 static void coff_win32_init(FILE * fp
, efunc errfunc
,
134 ldfunc ldef
, evalfunc eval
)
136 win32
= TRUE
; win64
= FALSE
;
137 (void)ldef
; /* placate optimizers */
139 coff_gen_init(fp
, errfunc
);
142 static void coff_win64_init(FILE * fp
, efunc errfunc
,
143 ldfunc ldef
, evalfunc eval
)
146 win32
= FALSE
; win64
= TRUE
;
147 (void)ldef
; /* placate optimizers */
149 coff_gen_init(fp
, errfunc
);
152 static void coff_std_init(FILE * fp
, efunc errfunc
, ldfunc ldef
,
155 win32
= win64
= FALSE
;
156 (void)ldef
; /* placate optimizers */
158 coff_gen_init(fp
, errfunc
);
161 static void coff_gen_init(FILE * fp
, efunc errfunc
)
167 nsects
= sectlen
= 0;
168 syms
= saa_init((int32_t)sizeof(struct Symbol
));
174 def_seg
= seg_alloc();
177 static void coff_cleanup(int debuginfo
)
186 for (i
= 0; i
< nsects
; i
++) {
188 saa_free(sects
[i
]->data
);
189 while (sects
[i
]->head
) {
191 sects
[i
]->head
= sects
[i
]->head
->next
;
203 static int coff_make_section(char *name
, uint32_t flags
)
207 s
= nasm_malloc(sizeof(*s
));
209 if (flags
!= BSS_FLAGS
)
210 s
->data
= saa_init(1L);
217 if (!strcmp(name
, ".text"))
220 s
->index
= seg_alloc();
221 strncpy(s
->name
, name
, 8);
225 if (nsects
>= sectlen
)
227 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
233 static int32_t coff_section_names(char *name
, int pass
, int *bits
)
236 uint32_t flags
, align_and
= ~0L, align_or
= 0L;
253 while (*p
&& !isspace(*p
))
257 if (strlen(name
) > 8) {
258 error(ERR_WARNING
, "COFF section names limited to 8 characters:"
264 while (*p
&& isspace(*p
))
268 while (*p
&& !isspace(*p
))
272 while (*p
&& isspace(*p
))
275 if (!nasm_stricmp(q
, "code") || !nasm_stricmp(q
, "text")) {
277 } else if (!nasm_stricmp(q
, "data")) {
279 } else if (!nasm_stricmp(q
, "rdata")) {
283 flags
= DATA_FLAGS
; /* gotta do something */
284 error(ERR_NONFATAL
, "standard COFF does not support"
285 " read-only data sections");
287 } else if (!nasm_stricmp(q
, "bss")) {
289 } else if (!nasm_stricmp(q
, "info")) {
293 flags
= DATA_FLAGS
; /* gotta do something */
294 error(ERR_NONFATAL
, "standard COFF does not support"
295 " informational sections");
297 } else if (!nasm_strnicmp(q
, "align=", 6)) {
298 if (!(win32
| win64
))
299 error(ERR_NONFATAL
, "standard COFF does not support"
300 " section alignment specification");
302 if (q
[6 + strspn(q
+ 6, "0123456789")])
304 "argument to `align' is not numeric");
306 unsigned int align
= atoi(q
+ 6);
307 if (!align
|| ((align
- 1) & align
))
308 error(ERR_NONFATAL
, "argument to `align' is not a"
311 error(ERR_NONFATAL
, "Win32 cannot align sections"
312 " to better than 64-byte boundaries");
314 align_and
= ~0x00F00000L
;
315 align_or
= (align
== 1 ? 0x00100000L
:
316 align
== 2 ? 0x00200000L
:
317 align
== 4 ? 0x00300000L
:
318 align
== 8 ? 0x00400000L
:
319 align
== 16 ? 0x00500000L
:
321 32 ? 0x00600000L
: 0x00700000L
);
328 for (i
= 0; i
< nsects
; i
++)
329 if (!strcmp(name
, sects
[i
]->name
))
333 if (!strcmp(name
, ".data"))
335 else if (!strcmp(name
, ".rdata"))
337 else if (!strcmp(name
, ".bss"))
342 i
= coff_make_section(name
, flags
);
344 sects
[i
]->flags
= flags
;
345 sects
[i
]->flags
&= align_and
;
346 sects
[i
]->flags
|= align_or
;
347 } else if (pass
== 1) {
349 error(ERR_WARNING
, "section attributes ignored on"
350 " redeclaration of section `%s'", name
);
353 return sects
[i
]->index
;
356 static void coff_deflabel(char *name
, int32_t segment
, int32_t offset
,
357 int is_global
, char *special
)
359 int pos
= strslen
+ 4;
363 error(ERR_NONFATAL
, "binary format does not support any"
364 " special symbol types");
366 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
367 error(ERR_NONFATAL
, "unrecognized special symbol `%s'", name
);
371 if (strlen(name
) > 8) {
372 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
373 strslen
+= 1 + strlen(name
);
377 sym
= saa_wstruct(syms
);
381 strcpy(sym
->name
, name
);
382 sym
->is_global
= !!is_global
;
383 if (segment
== NO_SEG
)
384 sym
->section
= -1; /* absolute symbol */
388 for (i
= 0; i
< nsects
; i
++)
389 if (segment
== sects
[i
]->index
) {
390 sym
->section
= i
+ 1;
394 sym
->is_global
= TRUE
;
399 sym
->value
= (sym
->section
== 0 ? 0 : offset
);
402 * define the references from external-symbol segment numbers
403 * to these symbol records.
405 if (sym
->section
== 0) {
406 bsym
= raa_write(bsym
, segment
, nsyms
);
409 if (segment
!= NO_SEG
)
410 symval
= raa_write(symval
, segment
, sym
->section
? 0 : sym
->value
);
415 static int32_t coff_add_reloc(struct Section
*sect
, int32_t segment
,
416 int relative
, int size64
)
422 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
423 sect
->tail
= &r
->next
;
426 r
->address
= sect
->len
;
427 if (segment
== NO_SEG
)
428 r
->symbol
= 0, r
->symbase
= ABS_SYMBOL
;
431 r
->symbase
= REAL_SYMBOLS
;
432 for (i
= 0; i
< nsects
; i
++)
433 if (segment
== sects
[i
]->index
) {
435 r
->symbase
= SECT_SYMBOLS
;
438 if (r
->symbase
== REAL_SYMBOLS
)
439 r
->symbol
= raa_read(bsym
, segment
);
441 r
->relative
= relative
;
446 * Return the fixup for standard COFF common variables.
448 if (r
->symbase
== REAL_SYMBOLS
&& !(win32
| win64
))
449 return raa_read(symval
, segment
);
454 static void coff_out(int32_t segto
, const void *data
, uint32_t type
,
455 int32_t segment
, int32_t wrt
)
458 int32_t realbytes
= type
& OUT_SIZMASK
;
459 uint8_t mydata
[8], *p
;
463 wrt
= NO_SEG
; /* continue to do _something_ */
464 error(ERR_NONFATAL
, "WRT not supported by COFF output formats");
470 * handle absolute-assembly (structure definitions)
472 if (segto
== NO_SEG
) {
473 if (type
!= OUT_RESERVE
)
474 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
480 for (i
= 0; i
< nsects
; i
++)
481 if (segto
== sects
[i
]->index
) {
486 int tempint
; /* ignored */
487 if (segto
!= coff_section_names(".text", 2, &tempint
))
488 error(ERR_PANIC
, "strange segment conditions in COFF driver");
490 s
= sects
[nsects
- 1];
493 if (!s
->data
&& type
!= OUT_RESERVE
) {
494 error(ERR_WARNING
, "attempt to initialize memory in"
495 " BSS section `%s': ignored", s
->name
);
496 if (type
== OUT_REL2ADR
)
498 else if (type
== OUT_REL4ADR
)
504 if (type
== OUT_RESERVE
) {
506 error(ERR_WARNING
, "uninitialised space declared in"
507 " non-BSS section `%s': zeroing", s
->name
);
508 coff_sect_write(s
, NULL
, realbytes
);
511 } else if (type
== OUT_RAWDATA
) {
512 if (segment
!= NO_SEG
)
513 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
514 coff_sect_write(s
, data
, realbytes
);
515 } else if (type
== OUT_ADDRESS
) {
517 if (realbytes
!= 4 && (segment
!= NO_SEG
|| wrt
!= NO_SEG
))
518 error(ERR_NONFATAL
, "COFF format does not support non-32-bit"
522 if (segment
!= NO_SEG
|| wrt
!= NO_SEG
) {
524 error(ERR_NONFATAL
, "COFF format does not support"
526 } else if (segment
% 2) {
527 error(ERR_NONFATAL
, "COFF format does not support"
528 " segment base references");
530 fix
= coff_add_reloc(s
, segment
, FALSE
, FALSE
);
533 WRITELONG(p
, *(int32_t *)data
+ fix
);
534 coff_sect_write(s
, mydata
, realbytes
);
539 if (realbytes
== 8) {
540 /* if (segment != NO_SEG || wrt != NO_SEG) {
542 error(ERR_NONFATAL, "COFF format does not support"
544 } else if (segment % 2) {
545 error(ERR_NONFATAL, "COFF format does not support"
546 " segment base references");
548 fix = coff_add_reloc(s, segment, FALSE);
550 fix
= coff_add_reloc(s
, segment
, FALSE
, TRUE
);
551 WRITEDLONG(p
, *(int64_t *)data
+ fix
);
552 coff_sect_write(s
, mydata
, realbytes
);
554 fix
= coff_add_reloc(s
, segment
, FALSE
, FALSE
);
555 WRITELONG(p
, *(int32_t *)data
+ fix
);
556 coff_sect_write(s
, mydata
, realbytes
);
559 } else if (type
== OUT_REL2ADR
) {
560 error(ERR_NONFATAL
, "COFF format does not support 16-bit"
562 } else if (type
== OUT_REL4ADR
) {
563 if (segment
== segto
&& !(win64
)) /* Acceptable for RIP-relative */
564 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
565 else if (segment
== NO_SEG
&& win32
)
566 error(ERR_NONFATAL
, "Win32 COFF does not correctly support"
567 " relative references to absolute addresses");
570 if (segment
!= NO_SEG
&& segment
% 2) {
571 error(ERR_NONFATAL
, "COFF format does not support"
572 " segment base references");
574 fix
= coff_add_reloc(s
, segment
, TRUE
, FALSE
);
577 WRITELONG(p
, *(int32_t *)data
+ 4 - realbytes
+ fix
);
579 WRITELONG(p
, *(int32_t *)data
- (realbytes
+ s
->len
) + fix
);
581 coff_sect_write(s
, mydata
, 4L);
587 static void coff_sect_write(struct Section
*sect
,
588 const uint8_t *data
, uint32_t len
)
590 saa_wbytes(sect
->data
, data
, len
);
594 typedef struct tagString
{
595 struct tagString
*Next
;
600 #define EXPORT_SECTION_NAME ".drectve"
601 #define EXPORT_SECTION_FLAGS INFO_FLAGS
603 #define EXPORT_SECTION_NAME ".text"
604 #define EXPORT_SECTION_FLAGS TEXT_FLAGS
607 static STRING
*Exports
= NULL
;
608 static struct Section
*directive_sec
;
609 void AddExport(char *name
)
611 STRING
*rvp
= Exports
, *newS
;
613 newS
= (STRING
*) nasm_malloc(sizeof(STRING
));
614 newS
->len
= strlen(name
);
616 newS
->String
= (char *)nasm_malloc(newS
->len
+ 1);
617 strcpy(newS
->String
, name
);
620 for (i
= 0; i
< nsects
; i
++)
622 if (!strcmp(EXPORT_SECTION_NAME
, sects
[i
]->name
))
626 sects
[coff_make_section
627 (EXPORT_SECTION_NAME
, EXPORT_SECTION_FLAGS
)];
629 directive_sec
= sects
[i
];
633 if (!strcmp(rvp
->String
, name
))
641 void BuildExportTable(void)
643 STRING
*rvp
= Exports
, *next
;
649 len
= sprintf((char *)buf
, "-export:%s ", rvp
->String
);
650 coff_sect_write(directive_sec
, buf
, len
);
655 while ((rvp
= next
)) {
657 nasm_free(rvp
->String
);
663 static int coff_directives(char *directive
, char *value
, int pass
)
665 if (!strcmp(directive
, "export")) {
669 return 1; /* ignore in pass two */
671 while (*q
&& !isspace(*q
))
675 while (*q
&& isspace(*q
))
680 error(ERR_NONFATAL
, "`export' directive requires export name");
684 error(ERR_NONFATAL
, "unrecognized export qualifier `%s'", q
);
693 static void coff_write(void)
695 int32_t pos
, sympos
, vsize
;
698 BuildExportTable(); /* fill in the .drectve section with -export's */
700 * Work out how big the file will get. Calculate the start of
701 * the `real' symbols at the same time.
703 pos
= 0x14 + 0x28 * nsects
;
704 initsym
= 3; /* two for the file, one absolute */
705 for (i
= 0; i
< nsects
; i
++) {
706 if (sects
[i
]->data
) {
708 pos
+= sects
[i
]->len
;
709 sects
[i
]->relpos
= pos
;
710 pos
+= 10 * sects
[i
]->nrelocs
;
712 sects
[i
]->pos
= sects
[i
]->relpos
= 0L;
713 initsym
+= 2; /* two for each section */
718 * Output the COFF header.
721 fwriteint16_t(0x8664, coffp
); /* MACHINE_x86-64 */
723 fwriteint16_t(0x014C, coffp
); /* MACHINE_i386 */
724 fwriteint16_t(nsects
, coffp
); /* number of sections */
725 fwriteint32_t(time(NULL
), coffp
); /* time stamp */
726 fwriteint32_t(sympos
, coffp
);
727 fwriteint32_t(nsyms
+ initsym
, coffp
);
728 fwriteint16_t(0, coffp
); /* no optional header */
729 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
730 fwriteint16_t((win32
| win64
) ? 0 : 0x104, coffp
);
733 * Output the section headers.
736 for (i
= 0; i
< nsects
; i
++) {
737 coff_section_header(sects
[i
]->name
, vsize
, sects
[i
]->len
,
738 sects
[i
]->pos
, sects
[i
]->relpos
,
739 sects
[i
]->nrelocs
, sects
[i
]->flags
);
740 vsize
+= sects
[i
]->len
;
744 * Output the sections and their relocations.
746 for (i
= 0; i
< nsects
; i
++)
747 if (sects
[i
]->data
) {
748 saa_fpwrite(sects
[i
]->data
, coffp
);
749 coff_write_relocs(sects
[i
]);
753 * Output the symbol and string tables.
755 coff_write_symbols();
756 fwriteint32_t(strslen
+ 4, coffp
); /* length includes length count */
757 saa_fpwrite(strs
, coffp
);
760 static void coff_section_header(char *name
, int32_t vsize
,
761 int32_t datalen
, int32_t datapos
,
762 int32_t relpos
, int nrelocs
, int32_t flags
)
766 memset(padname
, 0, 8);
767 strncpy(padname
, name
, 8);
768 fwrite(padname
, 8, 1, coffp
);
769 fwriteint32_t(vsize
, coffp
);
770 fwriteint32_t(0L, coffp
); /* RVA/offset - we ignore */
771 fwriteint32_t(datalen
, coffp
);
772 fwriteint32_t(datapos
, coffp
);
773 fwriteint32_t(relpos
, coffp
);
774 fwriteint32_t(0L, coffp
); /* no line numbers - we don't do 'em */
775 fwriteint16_t(nrelocs
, coffp
);
776 fwriteint16_t(0, coffp
); /* again, no line numbers */
777 fwriteint32_t(flags
, coffp
);
780 static void coff_write_relocs(struct Section
*s
)
784 for (r
= s
->head
; r
; r
= r
->next
) {
785 fwriteint32_t(r
->address
, coffp
);
786 fwriteint32_t(r
->symbol
+ (r
->symbase
== REAL_SYMBOLS
? initsym
:
787 r
->symbase
== ABS_SYMBOL
? initsym
- 1 :
788 r
->symbase
== SECT_SYMBOLS
? 2 : 0),
791 * Strange: Microsoft's COFF documentation says 0x03 for an
792 * absolute relocation, but both Visual C++ and DJGPP agree
793 * that in fact it's 0x06. I'll use 0x06 until someone
794 * argues. ***** UPDATE: PE/COFF Ver.8 docs confirm this -kkanios *****
797 fwriteint16_t(r
->relative
? 0x04 : r
->size64
? 0x01 : 0x02, coffp
);
799 fwriteint16_t(r
->relative
? 0x14 : 0x06, coffp
);
803 static void coff_symbol(char *name
, int32_t strpos
, int32_t value
,
804 int section
, int type
, int aux
)
809 memset(padname
, 0, 8);
810 strncpy(padname
, name
, 8);
811 fwrite(padname
, 8, 1, coffp
);
813 fwriteint32_t(0L, coffp
);
814 fwriteint32_t(strpos
, coffp
);
816 fwriteint32_t(value
, coffp
);
817 fwriteint16_t(section
, coffp
);
818 fwriteint16_t(0, coffp
);
823 static void coff_write_symbols(void)
829 * The `.file' record, and the file name auxiliary record.
831 coff_symbol(".file", 0L, 0L, -2, 0x67, 1);
832 memset(filename
, 0, 18);
833 strncpy(filename
, coff_infile
, 18);
834 fwrite(filename
, 18, 1, coffp
);
837 * The section records, with their auxiliaries.
839 memset(filename
, 0, 18); /* useful zeroed buffer */
841 for (i
= 0; i
< nsects
; i
++) {
842 coff_symbol(sects
[i
]->name
, 0L, 0L, i
+ 1, 3, 1);
843 fwriteint32_t(sects
[i
]->len
, coffp
);
844 fwriteint16_t(sects
[i
]->nrelocs
, coffp
);
845 fwrite(filename
, 12, 1, coffp
);
849 * The absolute symbol, for relative-to-absolute relocations.
851 coff_symbol(".absolut", 0L, 0L, -1, 3, 0);
857 for (i
= 0; i
< nsyms
; i
++) {
858 struct Symbol
*sym
= saa_rstruct(syms
);
859 coff_symbol(sym
->strpos
== -1 ? sym
->name
: NULL
,
860 sym
->strpos
, sym
->value
, sym
->section
,
861 sym
->is_global
? 2 : 3, 0);
865 static int32_t coff_segbase(int32_t segment
)
870 static void coff_std_filename(char *inname
, char *outname
, efunc error
)
872 strcpy(coff_infile
, inname
);
873 standard_extension(inname
, outname
, ".o", error
);
876 static void coff_win32_filename(char *inname
, char *outname
, efunc error
)
878 strcpy(coff_infile
, inname
);
879 standard_extension(inname
, outname
, ".obj", error
);
882 static const char *coff_stdmac
[] = {
883 "%define __SECT__ [section .text]",
884 "%macro __NASM_CDecl__ 1",
886 "%imacro export 1+.nolist",
892 static int coff_set_info(enum geninfo type
, char **val
)
898 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
902 struct ofmt of_coff
= {
903 "COFF (i386) object files (e.g. DJGPP for DOS)",
924 struct ofmt of_win32
= {
925 "Microsoft Win32 (i386) object files",
946 struct ofmt of_win64
= {
947 "Microsoft Win64 (x86-64) object files",