1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2014 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outcoff.c output routines for the Netwide Assembler to produce
36 * COFF object files (for DJGPP and Win32)
53 #include "output/outform.h"
54 #include "output/outlib.h"
55 #include "output/pecoff.h"
57 #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
62 * (0) When I say `standard COFF' below, I mean `COFF as output and
63 * used by DJGPP'. I assume DJGPP gets it right.
65 * (1) Win32 appears to interpret the term `relative relocation'
66 * differently from standard COFF. Standard COFF understands a
67 * relative relocation to mean that during relocation you add the
68 * address of the symbol you're referencing, and subtract the base
69 * address of the section you're in. Win32 COFF, by contrast, seems
70 * to add the address of the symbol and then subtract the address
71 * of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
72 * subtly incompatible.
74 * (2) Win32 doesn't bother putting any flags in the header flags
75 * field (at offset 0x12 into the file).
77 * (3) Win32 uses some extra flags into the section header table:
78 * it defines flags 0x80000000 (writable), 0x40000000 (readable)
79 * and 0x20000000 (executable), and uses them in the expected
80 * combinations. It also defines 0x00100000 through 0x00700000 for
81 * section alignments of 1 through 64 bytes.
83 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
84 * field directly after the section name in the section header
85 * table for something strange: they store what the address of the
86 * section start point _would_ be, if you laid all the sections end
87 * to end starting at zero. Dunno why. Microsoft's documentation
88 * lists this field as "Virtual Size of Section", which doesn't
89 * seem to fit at all. In fact, Win32 even includes non-linked
90 * sections such as .drectve in this calculation.
92 * Newer versions of MASM seem to have changed this to be zero, and
93 * that apparently matches the COFF spec, so go with that.
95 * (5) Standard COFF does something very strange to common
96 * variables: the relocation point for a common variable is as far
97 * _before_ the variable as its size stretches out _after_ it. So
98 * we must fix up common variable references. Win32 seems to be
99 * sensible on this one.
102 /* Flag which version of COFF we are currently outputting. */
103 static bool win32
, win64
;
105 static int32_t imagebase_sect
;
106 #define WRT_IMAGEBASE "..imagebase"
110 int32_t address
; /* relative to _start_ of section */
111 int32_t symbol
; /* symbol number */
116 } symbase
; /* relocation for symbol number :) */
122 int32_t strpos
; /* string table position of name */
123 int32_t value
; /* address, or COMMON variable size */
124 int section
; /* section number where it's defined
125 * - in COFF codes, not NASM codes */
126 bool is_global
; /* is it a global symbol or not? */
127 int16_t type
; /* 0 - notype, 0x20 - function */
128 int32_t namlen
; /* full name length */
131 static char coff_infile
[FILENAME_MAX
];
138 struct Reloc
*head
, **tail
;
139 uint32_t flags
; /* section flags */
141 int32_t namepos
; /* Offset of name into the strings table */
146 * Some common section flags by default
148 #define TEXT_FLAGS_WIN \
149 (IMAGE_SCN_CNT_CODE | \
150 IMAGE_SCN_ALIGN_16BYTES | \
151 IMAGE_SCN_MEM_EXECUTE | \
153 #define TEXT_FLAGS_DOS \
156 #define DATA_FLAGS_WIN \
157 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
158 IMAGE_SCN_ALIGN_4BYTES | \
159 IMAGE_SCN_MEM_READ | \
161 #define DATA_FLAGS_DOS \
162 (IMAGE_SCN_CNT_INITIALIZED_DATA)
164 #define BSS_FLAGS_WIN \
165 (IMAGE_SCN_CNT_UNINITIALIZED_DATA | \
166 IMAGE_SCN_ALIGN_4BYTES | \
167 IMAGE_SCN_MEM_READ | \
169 #define BSS_FLAGS_DOS \
170 (IMAGE_SCN_CNT_UNINITIALIZED_DATA)
172 #define RDATA_FLAGS_WIN \
173 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
174 IMAGE_SCN_ALIGN_8BYTES | \
177 #define RDATA_FLAGS_DOS \
178 (IMAGE_SCN_CNT_INITIALIZED_DATA)
180 #define PDATA_FLAGS \
181 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
182 IMAGE_SCN_ALIGN_4BYTES | \
185 #define XDATA_FLAGS \
186 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
187 IMAGE_SCN_ALIGN_8BYTES | \
191 (IMAGE_SCN_ALIGN_1BYTES | \
192 IMAGE_SCN_LNK_INFO | \
193 IMAGE_SCN_LNK_REMOVE)
195 #define TEXT_FLAGS ((win32 | win64) ? TEXT_FLAGS_WIN : TEXT_FLAGS_DOS)
196 #define DATA_FLAGS ((win32 | win64) ? DATA_FLAGS_WIN : DATA_FLAGS_DOS)
197 #define BSS_FLAGS ((win32 | win64) ? BSS_FLAGS_WIN : BSS_FLAGS_DOS)
198 #define RDATA_FLAGS ((win32 | win64) ? RDATA_FLAGS_WIN : RDATA_FLAGS_DOS)
200 #define SECT_DELTA 32
201 static struct Section
**sects
;
202 static int nsects
, sectlen
;
204 static struct SAA
*syms
;
205 static uint32_t nsyms
;
207 static int32_t def_seg
;
211 static struct RAA
*bsym
, *symval
;
213 static struct SAA
*strs
;
214 static uint32_t strslen
;
216 static void coff_gen_init(void);
217 static void coff_sect_write(struct Section
*, const uint8_t *, uint32_t);
218 static void coff_write(void);
219 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int32_t, int, int32_t);
220 static void coff_write_relocs(struct Section
*);
221 static void coff_write_symbols(void);
223 static void coff_win32_init(void)
230 static void coff_win64_init(void)
236 imagebase_sect
= seg_alloc()+1;
237 define_label(WRT_IMAGEBASE
, imagebase_sect
, 0, NULL
, false, false);
240 static void coff_std_init(void)
242 win32
= win64
= false;
246 static void coff_gen_init(void)
250 nsects
= sectlen
= 0;
251 syms
= saa_init(sizeof(struct Symbol
));
257 def_seg
= seg_alloc();
260 static void coff_cleanup(int debuginfo
)
268 for (i
= 0; i
< nsects
; i
++) {
270 saa_free(sects
[i
]->data
);
271 while (sects
[i
]->head
) {
273 sects
[i
]->head
= sects
[i
]->head
->next
;
276 nasm_free(sects
[i
]->name
);
286 static int coff_make_section(char *name
, uint32_t flags
)
291 s
= nasm_zalloc(sizeof(*s
));
293 if (flags
!= BSS_FLAGS
)
294 s
->data
= saa_init(1);
296 if (!strcmp(name
, ".text"))
299 s
->index
= seg_alloc();
301 namelen
= strlen(name
);
303 if (win32
|| win64
) {
304 s
->namepos
= strslen
+ 4;
305 saa_wbytes(strs
, name
, namelen
+ 1);
306 strslen
+= namelen
+ 1;
311 s
->name
= nasm_malloc(namelen
+ 1);
312 strncpy(s
->name
, name
, namelen
);
313 s
->name
[namelen
] = '\0';
316 if (nsects
>= sectlen
) {
317 sectlen
+= SECT_DELTA
;
318 sects
= nasm_realloc(sects
, sectlen
* sizeof(*sects
));
325 static inline int32_t coff_sectalign_flags(unsigned int align
)
327 return (ilog2_32(align
) + 1) << 20;
330 static int32_t coff_section_names(char *name
, int pass
, int *bits
)
333 uint32_t flags
, align_and
= ~0L, align_or
= 0L;
350 while (*p
&& !nasm_isspace(*p
))
354 if (strlen(name
) > 8) {
355 if (!win32
&& !win64
) {
356 nasm_error(ERR_WARNING
,
357 "COFF section names limited to 8 characters: truncating");
363 while (*p
&& nasm_isspace(*p
))
367 while (*p
&& !nasm_isspace(*p
))
371 while (*p
&& nasm_isspace(*p
))
374 if (!nasm_stricmp(q
, "code") || !nasm_stricmp(q
, "text")) {
376 } else if (!nasm_stricmp(q
, "data")) {
378 } else if (!nasm_stricmp(q
, "rdata")) {
382 flags
= DATA_FLAGS
; /* gotta do something */
383 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
384 " read-only data sections");
386 } else if (!nasm_stricmp(q
, "bss")) {
388 } else if (!nasm_stricmp(q
, "info")) {
392 flags
= DATA_FLAGS
; /* gotta do something */
393 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
394 " informational sections");
396 } else if (!nasm_strnicmp(q
, "align=", 6)) {
397 if (!(win32
| win64
))
398 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
399 " section alignment specification");
401 if (q
[6 + strspn(q
+ 6, "0123456789")])
402 nasm_error(ERR_NONFATAL
,
403 "argument to `align' is not numeric");
405 unsigned int align
= atoi(q
+ 6);
406 if (!align
|| ((align
- 1) & align
))
407 nasm_error(ERR_NONFATAL
, "argument to `align' is not a"
410 nasm_error(ERR_NONFATAL
, "Win32 cannot align sections"
411 " to better than 64-byte boundaries");
413 align_and
= ~0x00F00000L
;
414 align_or
= coff_sectalign_flags(align
);
421 for (i
= 0; i
< nsects
; i
++)
422 if (!strcmp(name
, sects
[i
]->name
))
426 if (!strcmp(name
, ".data"))
428 else if (!strcmp(name
, ".rdata"))
430 else if (!strcmp(name
, ".bss"))
432 else if (win64
&& !strcmp(name
, ".pdata"))
434 else if (win64
&& !strcmp(name
, ".xdata"))
439 i
= coff_make_section(name
, flags
);
441 sects
[i
]->flags
= flags
;
442 sects
[i
]->flags
&= align_and
;
443 sects
[i
]->flags
|= align_or
;
444 } else if (pass
== 1) {
445 /* Check if any flags are specified */
447 unsigned int align_flags
= flags
& IMAGE_SCN_ALIGN_MASK
;
449 /* Warn if non-alignment flags differ */
450 if ((flags
^ sects
[i
]->flags
) & ~IMAGE_SCN_ALIGN_MASK
) {
451 nasm_error(ERR_WARNING
, "section attributes ignored on"
452 " redeclaration of section `%s'", name
);
454 /* Check if alignment might be needed */
455 if (align_flags
> IMAGE_SCN_ALIGN_1BYTES
) {
456 unsigned int sect_align_flags
= sects
[i
]->flags
& IMAGE_SCN_ALIGN_MASK
;
458 /* Compute the actual alignment */
459 unsigned int align
= 1u << ((align_flags
- IMAGE_SCN_ALIGN_1BYTES
) >> 20);
461 /* Update section header as needed */
462 if (align_flags
> sect_align_flags
) {
463 sects
[i
]->flags
= (sects
[i
]->flags
& ~IMAGE_SCN_ALIGN_MASK
) | align_flags
;
465 /* Check if not already aligned */
466 if (sects
[i
]->len
% align
) {
467 unsigned int padding
= (align
- sects
[i
]->len
) % align
;
468 /* We need to write at most 8095 bytes */
470 if (sects
[i
]->flags
& IMAGE_SCN_CNT_CODE
) {
471 /* Fill with INT 3 instructions */
472 memset(buffer
, 0xCC, padding
);
474 memset(buffer
, 0x00, padding
);
476 saa_wbytes(sects
[i
]->data
, buffer
, padding
);
477 sects
[i
]->len
+= padding
;
483 return sects
[i
]->index
;
486 static void coff_deflabel(char *name
, int32_t segment
, int64_t offset
,
487 int is_global
, char *special
)
489 int pos
= strslen
+ 4;
493 nasm_error(ERR_NONFATAL
, "COFF format does not support any"
494 " special symbol types");
496 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
497 if (strcmp(name
,WRT_IMAGEBASE
))
498 nasm_error(ERR_NONFATAL
, "unrecognized special symbol `%s'", name
);
502 if (strlen(name
) > 8) {
503 size_t nlen
= strlen(name
)+1;
504 saa_wbytes(strs
, name
, nlen
);
509 sym
= saa_wstruct(syms
);
512 sym
->namlen
= strlen(name
);
514 strcpy(sym
->name
, name
);
515 sym
->is_global
= !!is_global
;
516 sym
->type
= 0; /* Default to T_NULL (no type) */
517 if (segment
== NO_SEG
)
518 sym
->section
= -1; /* absolute symbol */
522 for (i
= 0; i
< nsects
; i
++)
523 if (segment
== sects
[i
]->index
) {
524 sym
->section
= i
+ 1;
528 sym
->is_global
= true;
533 sym
->value
= (sym
->section
== 0 ? 0 : offset
);
536 * define the references from external-symbol segment numbers
537 * to these symbol records.
539 if (sym
->section
== 0)
540 bsym
= raa_write(bsym
, segment
, nsyms
);
542 if (segment
!= NO_SEG
)
543 symval
= raa_write(symval
, segment
, sym
->section
? 0 : sym
->value
);
548 static int32_t coff_add_reloc(struct Section
*sect
, int32_t segment
,
553 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
554 sect
->tail
= &r
->next
;
557 r
->address
= sect
->len
;
558 if (segment
== NO_SEG
) {
559 r
->symbol
= 0, r
->symbase
= ABS_SYMBOL
;
562 r
->symbase
= REAL_SYMBOLS
;
563 for (i
= 0; i
< nsects
; i
++) {
564 if (segment
== sects
[i
]->index
) {
566 r
->symbase
= SECT_SYMBOLS
;
570 if (r
->symbase
== REAL_SYMBOLS
)
571 r
->symbol
= raa_read(bsym
, segment
);
578 * Return the fixup for standard COFF common variables.
580 if (r
->symbase
== REAL_SYMBOLS
&& !(win32
| win64
))
581 return raa_read(symval
, segment
);
586 static void coff_out(int32_t segto
, const void *data
,
587 enum out_type type
, uint64_t size
,
588 int32_t segment
, int32_t wrt
)
591 uint8_t mydata
[8], *p
;
594 if (wrt
!= NO_SEG
&& !win64
) {
595 wrt
= NO_SEG
; /* continue to do _something_ */
596 nasm_error(ERR_NONFATAL
, "WRT not supported by COFF output formats");
600 * handle absolute-assembly (structure definitions)
602 if (segto
== NO_SEG
) {
603 if (type
!= OUT_RESERVE
)
604 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
610 for (i
= 0; i
< nsects
; i
++) {
611 if (segto
== sects
[i
]->index
) {
617 int tempint
; /* ignored */
618 if (segto
!= coff_section_names(".text", 2, &tempint
))
619 nasm_error(ERR_PANIC
, "strange segment conditions in COFF driver");
621 s
= sects
[nsects
- 1];
624 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
625 if (win64
&& wrt
== NO_SEG
) {
626 if (!strcmp(s
->name
,".pdata") || !strcmp(s
->name
,".xdata"))
627 wrt
= imagebase_sect
;
630 if (!s
->data
&& type
!= OUT_RESERVE
) {
631 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
632 " BSS section `%s': ignored", s
->name
);
633 s
->len
+= realsize(type
, size
);
637 if (type
== OUT_RESERVE
) {
639 nasm_error(ERR_WARNING
, "uninitialised space declared in"
640 " non-BSS section `%s': zeroing", s
->name
);
641 coff_sect_write(s
, NULL
, size
);
644 } else if (type
== OUT_RAWDATA
) {
645 if (segment
!= NO_SEG
)
646 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
647 coff_sect_write(s
, data
, size
);
648 } else if (type
== OUT_ADDRESS
) {
649 int asize
= abs(size
);
651 if (asize
!= 4 && (segment
!= NO_SEG
|| wrt
!= NO_SEG
)) {
652 nasm_error(ERR_NONFATAL
, "COFF format does not support non-32-bit"
656 if (segment
!= NO_SEG
|| wrt
!= NO_SEG
) {
658 nasm_error(ERR_NONFATAL
, "COFF format does not support"
660 } else if (segment
% 2) {
661 nasm_error(ERR_NONFATAL
, "COFF format does not support"
662 " segment base references");
664 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_I386_DIR32
);
667 WRITELONG(p
, *(int64_t *)data
+ fix
);
668 coff_sect_write(s
, mydata
, asize
);
674 if (wrt
== imagebase_sect
) {
675 nasm_error(ERR_NONFATAL
, "operand size mismatch: 'wrt "
676 WRT_IMAGEBASE
"' is a 32-bit operand");
678 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_AMD64_ADDR64
);
679 WRITEDLONG(p
, *(int64_t *)data
+ fix
);
680 coff_sect_write(s
, mydata
, asize
);
682 fix
= coff_add_reloc(s
, segment
,
683 wrt
== imagebase_sect
? IMAGE_REL_AMD64_ADDR32NB
:
684 IMAGE_REL_AMD64_ADDR32
);
685 WRITELONG(p
, *(int64_t *)data
+ fix
);
686 coff_sect_write(s
, mydata
, asize
);
689 } else if (type
== OUT_REL2ADR
) {
690 nasm_error(ERR_NONFATAL
, "COFF format does not support 16-bit"
692 } else if (type
== OUT_REL4ADR
) {
693 if (segment
== segto
&& !(win64
)) /* Acceptable for RIP-relative */
694 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
695 else if (segment
== NO_SEG
&& win32
)
696 nasm_error(ERR_NONFATAL
, "Win32 COFF does not correctly support"
697 " relative references to absolute addresses");
700 if (segment
!= NO_SEG
&& segment
% 2) {
701 nasm_error(ERR_NONFATAL
, "COFF format does not support"
702 " segment base references");
704 fix
= coff_add_reloc(s
, segment
,
705 win64
? IMAGE_REL_AMD64_REL32
: IMAGE_REL_I386_REL32
);
708 WRITELONG(p
, *(int64_t *)data
+ 4 - size
+ fix
);
710 WRITELONG(p
, *(int64_t *)data
- (size
+ s
->len
) + fix
);
712 coff_sect_write(s
, mydata
, 4L);
718 static void coff_sect_write(struct Section
*sect
,
719 const uint8_t *data
, uint32_t len
)
721 saa_wbytes(sect
->data
, data
, len
);
725 typedef struct tagString
{
726 struct tagString
*next
;
731 #define EXPORT_SECTION_NAME ".drectve"
732 #define EXPORT_SECTION_FLAGS INFO_FLAGS
734 * #define EXPORT_SECTION_NAME ".text"
735 * #define EXPORT_SECTION_FLAGS TEXT_FLAGS
738 static STRING
*Exports
= NULL
;
739 static struct Section
*directive_sec
;
740 static void AddExport(char *name
)
742 STRING
*rvp
= Exports
, *newS
;
744 newS
= (STRING
*) nasm_malloc(sizeof(STRING
));
745 newS
->len
= strlen(name
);
747 newS
->String
= (char *)nasm_malloc(newS
->len
+ 1);
748 strcpy(newS
->String
, name
);
752 for (i
= 0; i
< nsects
; i
++) {
753 if (!strcmp(EXPORT_SECTION_NAME
, sects
[i
]->name
))
758 i
= coff_make_section(EXPORT_SECTION_NAME
, EXPORT_SECTION_FLAGS
);
760 directive_sec
= sects
[i
];
764 if (!strcmp(rvp
->String
, name
))
772 static void BuildExportTable(STRING
**rvp
)
779 list_for_each_safe(p
, t
, *rvp
) {
780 coff_sect_write(directive_sec
, (uint8_t *)"-export:", 8);
781 coff_sect_write(directive_sec
, (uint8_t *)p
->String
, p
->len
);
782 coff_sect_write(directive_sec
, (uint8_t *)" ", 1);
783 nasm_free(p
->String
);
790 static int coff_directives(enum directives directive
, char *value
, int pass
)
798 return 1; /* ignore in pass two */
800 while (*q
&& !nasm_isspace(*q
))
802 if (nasm_isspace(*q
)) {
804 while (*q
&& nasm_isspace(*q
))
809 nasm_error(ERR_NONFATAL
, "`export' directive requires export name");
813 nasm_error(ERR_NONFATAL
, "unrecognized export qualifier `%s'", q
);
824 if (!win32
) /* Only applicable for -f win32 */
828 for (i
= 0; i
< nsects
; i
++)
829 if (!strcmp(".sxdata",sects
[i
]->name
))
832 sxseg
= coff_make_section(".sxdata", IMAGE_SCN_LNK_INFO
);
837 * pass0 == 2 is the only time when the full set of symbols are
838 * guaranteed to be present; it is the final output pass.
843 for (n
= 0; n
< nsyms
; n
++) {
844 struct Symbol
*sym
= saa_rstruct(syms
);
848 * sym->strpos is biased by 4, because symbol
849 * table is prefixed with table length
851 if (sym
->strpos
>=4) {
852 char *name
= nasm_malloc(sym
->namlen
+1);
853 saa_fread(strs
, sym
->strpos
-4, name
, sym
->namlen
);
854 name
[sym
->namlen
] = '\0';
855 equals
= !strcmp(value
,name
);
858 equals
= !strcmp(value
,sym
->name
);
863 * this value arithmetics effectively reflects
864 * initsym in coff_write(): 2 for file, 1 for
865 * .absolute and two per each section
867 unsigned char value
[4],*p
=value
;
868 WRITELONG(p
,n
+ 2 + 1 + nsects
*2);
869 coff_sect_write(sects
[sxseg
],value
,4);
875 nasm_error(ERR_NONFATAL
,
876 "`safeseh' directive requires valid symbol");
886 /* handle relocations storm, valid for win32/64 only */
887 static inline void coff_adjust_relocs(struct Section
*s
)
889 if (s
->nrelocs
< IMAGE_SCN_MAX_RELOC
)
894 if (ofmt
== &of_coff
)
895 nasm_error(ERR_FATAL
,
896 "Too many relocations (%d) for section `%s'",
897 s
->nrelocs
, s
->name
);
901 s
->flags
|= IMAGE_SCN_LNK_NRELOC_OVFL
;
905 static void coff_write(void)
907 int32_t pos
, sympos
, vsize
;
910 /* fill in the .drectve section with -export's */
911 BuildExportTable(&Exports
);
914 /* add default value for @feat.00, this allows to 'link /safeseh' */
918 for (n
= 0; n
< nsyms
; n
++) {
919 struct Symbol
*sym
= saa_rstruct(syms
);
920 if (sym
->strpos
== -1 && !strcmp("@feat.00",sym
->name
))
924 coff_deflabel("@feat.00", NO_SEG
, 1, 0, NULL
);
928 * Work out how big the file will get.
929 * Calculate the start of the `real' symbols at the same time.
930 * Check for massive relocations.
932 pos
= 0x14 + 0x28 * nsects
;
933 initsym
= 3; /* two for the file, one absolute */
934 for (i
= 0; i
< nsects
; i
++) {
935 if (sects
[i
]->data
) {
936 coff_adjust_relocs(sects
[i
]);
938 pos
+= sects
[i
]->len
;
939 sects
[i
]->relpos
= pos
;
940 pos
+= 10 * sects
[i
]->nrelocs
;
942 sects
[i
]->pos
= sects
[i
]->relpos
= 0L;
943 initsym
+= 2; /* two for each section */
948 * Output the COFF header.
951 i
= IMAGE_FILE_MACHINE_AMD64
;
953 i
= IMAGE_FILE_MACHINE_I386
;
954 fwriteint16_t(i
, ofile
); /* machine type */
955 fwriteint16_t(nsects
, ofile
); /* number of sections */
956 fwriteint32_t(time(NULL
), ofile
); /* time stamp */
957 fwriteint32_t(sympos
, ofile
);
958 fwriteint32_t(nsyms
+ initsym
, ofile
);
959 fwriteint16_t(0, ofile
); /* no optional header */
960 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
961 fwriteint16_t((win32
| win64
) ? 0 : 0x104, ofile
);
964 * Output the section headers.
967 for (i
= 0; i
< nsects
; i
++) {
968 coff_section_header(sects
[i
]->name
, sects
[i
]->namepos
, vsize
, sects
[i
]->len
,
969 sects
[i
]->pos
, sects
[i
]->relpos
,
970 sects
[i
]->nrelocs
, sects
[i
]->flags
);
971 vsize
+= sects
[i
]->len
;
975 * Output the sections and their relocations.
977 for (i
= 0; i
< nsects
; i
++)
978 if (sects
[i
]->data
) {
979 saa_fpwrite(sects
[i
]->data
, ofile
);
980 coff_write_relocs(sects
[i
]);
984 * Output the symbol and string tables.
986 coff_write_symbols();
987 fwriteint32_t(strslen
+ 4, ofile
); /* length includes length count */
988 saa_fpwrite(strs
, ofile
);
991 static void coff_section_header(char *name
, int32_t namepos
, int32_t vsize
,
992 int32_t datalen
, int32_t datapos
,
993 int32_t relpos
, int nrelocs
, int32_t flags
)
1000 strncpy(padname
, name
, 8);
1001 nasm_write(padname
, 8, ofile
);
1004 * If name is longer than 8 bytes, write '/' followed
1005 * by offset into the strings table represented as
1008 namepos
= namepos
% 100000000;
1010 padname
[1] = '0' + (namepos
/ 1000000);
1011 namepos
= namepos
% 1000000;
1012 padname
[2] = '0' + (namepos
/ 100000);
1013 namepos
= namepos
% 100000;
1014 padname
[3] = '0' + (namepos
/ 10000);
1015 namepos
= namepos
% 10000;
1016 padname
[4] = '0' + (namepos
/ 1000);
1017 namepos
= namepos
% 1000;
1018 padname
[5] = '0' + (namepos
/ 100);
1019 namepos
= namepos
% 100;
1020 padname
[6] = '0' + (namepos
/ 10);
1021 namepos
= namepos
% 10;
1022 padname
[7] = '0' + (namepos
);
1023 nasm_write(padname
, 8, ofile
);
1026 fwriteint32_t(0, ofile
); /* Virtual size field - set to 0 or vsize */
1027 fwriteint32_t(0L, ofile
); /* RVA/offset - we ignore */
1028 fwriteint32_t(datalen
, ofile
);
1029 fwriteint32_t(datapos
, ofile
);
1030 fwriteint32_t(relpos
, ofile
);
1031 fwriteint32_t(0L, ofile
); /* no line numbers - we don't do 'em */
1034 * a special case -- if there are too many relocs
1035 * we have to put IMAGE_SCN_MAX_RELOC here and write
1036 * the real relocs number into VirtualAddress of first
1039 if (flags
& IMAGE_SCN_LNK_NRELOC_OVFL
)
1040 fwriteint16_t(IMAGE_SCN_MAX_RELOC
, ofile
);
1042 fwriteint16_t(nrelocs
, ofile
);
1044 fwriteint16_t(0, ofile
); /* again, no line numbers */
1045 fwriteint32_t(flags
, ofile
);
1048 static void coff_write_relocs(struct Section
*s
)
1052 /* a real number of relocations if needed */
1053 if (s
->flags
& IMAGE_SCN_LNK_NRELOC_OVFL
) {
1054 fwriteint32_t(s
->nrelocs
, ofile
);
1055 fwriteint32_t(0, ofile
);
1056 fwriteint16_t(0, ofile
);
1059 for (r
= s
->head
; r
; r
= r
->next
) {
1060 fwriteint32_t(r
->address
, ofile
);
1061 fwriteint32_t(r
->symbol
+ (r
->symbase
== REAL_SYMBOLS
? initsym
:
1062 r
->symbase
== ABS_SYMBOL
? initsym
- 1 :
1063 r
->symbase
== SECT_SYMBOLS
? 2 : 0),
1065 fwriteint16_t(r
->type
, ofile
);
1069 static void coff_symbol(char *name
, int32_t strpos
, int32_t value
,
1070 int section
, int type
, int storageclass
, int aux
)
1075 strncpy(padname
, name
, 8);
1076 nasm_write(padname
, 8, ofile
);
1078 fwriteint32_t(0, ofile
);
1079 fwriteint32_t(strpos
, ofile
);
1082 fwriteint32_t(value
, ofile
);
1083 fwriteint16_t(section
, ofile
);
1084 fwriteint16_t(type
, ofile
);
1086 fputc(storageclass
, ofile
);
1090 static void coff_write_symbols(void)
1096 * The `.file' record, and the file name auxiliary record.
1098 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
1099 strncpy(filename
, coff_infile
, 18);
1100 nasm_write(filename
, 18, ofile
);
1103 * The section records, with their auxiliaries.
1105 memset(filename
, 0, 18); /* useful zeroed buffer */
1107 for (i
= 0; i
< (uint32_t) nsects
; i
++) {
1108 coff_symbol(sects
[i
]->name
, 0L, 0L, i
+ 1, 0, 3, 1);
1109 fwriteint32_t(sects
[i
]->len
, ofile
);
1110 fwriteint16_t(sects
[i
]->nrelocs
,ofile
);
1111 nasm_write(filename
, 12, ofile
);
1115 * The absolute symbol, for relative-to-absolute relocations.
1117 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
1123 for (i
= 0; i
< nsyms
; i
++) {
1124 struct Symbol
*sym
= saa_rstruct(syms
);
1125 coff_symbol(sym
->strpos
== -1 ? sym
->name
: NULL
,
1126 sym
->strpos
, sym
->value
, sym
->section
,
1127 sym
->type
, sym
->is_global
? 2 : 3, 0);
1131 static void coff_sectalign(int32_t seg
, unsigned int value
)
1133 struct Section
*s
= NULL
;
1137 for (i
= 0; i
< nsects
; i
++) {
1138 if (sects
[i
]->index
== seg
) {
1144 if (!s
|| !is_power2(value
))
1147 /* DOS has limitation on 64 bytes */
1148 if (!(win32
| win64
) && value
> 64)
1151 align
= (s
->flags
& IMAGE_SCN_ALIGN_MASK
);
1152 value
= coff_sectalign_flags(value
);
1154 s
->flags
= (s
->flags
& ~IMAGE_SCN_ALIGN_MASK
) | value
;
1157 static int32_t coff_segbase(int32_t segment
)
1162 static void coff_std_filename(char *inname
, char *outname
)
1164 strcpy(coff_infile
, inname
);
1165 standard_extension(inname
, outname
, ".o");
1168 static void coff_win32_filename(char *inname
, char *outname
)
1170 strcpy(coff_infile
, inname
);
1171 standard_extension(inname
, outname
, ".obj");
1174 extern macros_t coff_stdmac
[];
1176 static int coff_set_info(enum geninfo type
, char **val
)
1182 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
1186 struct ofmt of_coff
= {
1187 "COFF (i386) object files (e.g. DJGPP for DOS)",
1209 struct ofmt of_win32
= {
1210 "Microsoft Win32 (i386) object files",
1224 coff_win32_filename
,
1232 struct ofmt of_win64
= {
1233 "Microsoft Win64 (x86-64) object files",
1247 coff_win32_filename
,