1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 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)
58 #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
63 * (0) When I say `standard COFF' below, I mean `COFF as output and
64 * used by DJGPP'. I assume DJGPP gets it right.
66 * (1) Win32 appears to interpret the term `relative relocation'
67 * differently from standard COFF. Standard COFF understands a
68 * relative relocation to mean that during relocation you add the
69 * address of the symbol you're referencing, and subtract the base
70 * address of the section you're in. Win32 COFF, by contrast, seems
71 * to add the address of the symbol and then subtract the address
72 * of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
73 * subtly incompatible.
75 * (2) Win32 doesn't bother putting any flags in the header flags
76 * field (at offset 0x12 into the file).
78 * (3) Win32 uses some extra flags into the section header table:
79 * it defines flags 0x80000000 (writable), 0x40000000 (readable)
80 * and 0x20000000 (executable), and uses them in the expected
81 * combinations. It also defines 0x00100000 through 0x00700000 for
82 * section alignments of 1 through 64 bytes.
84 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
85 * field directly after the section name in the section header
86 * table for something strange: they store what the address of the
87 * section start point _would_ be, if you laid all the sections end
88 * to end starting at zero. Dunno why. Microsoft's documentation
89 * lists this field as "Virtual Size of Section", which doesn't
90 * seem to fit at all. In fact, Win32 even includes non-linked
91 * sections such as .drectve in this calculation.
93 * Newer versions of MASM seem to have changed this to be zero, and
94 * that apparently matches the COFF spec, so go with that.
96 * (5) Standard COFF does something very strange to common
97 * variables: the relocation point for a common variable is as far
98 * _before_ the variable as its size stretches out _after_ it. So
99 * we must fix up common variable references. Win32 seems to be
100 * sensible on this one.
103 /* Flag which version of COFF we are currently outputting. */
106 static int32_t imagebase_sect
;
107 #define WRT_IMAGEBASE "..imagebase"
110 * Some common section flags by default
112 #define TEXT_FLAGS_WIN \
113 (IMAGE_SCN_CNT_CODE | \
114 IMAGE_SCN_ALIGN_16BYTES | \
115 IMAGE_SCN_MEM_EXECUTE | \
117 #define TEXT_FLAGS_DOS \
120 #define DATA_FLAGS_WIN \
121 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
122 IMAGE_SCN_ALIGN_4BYTES | \
123 IMAGE_SCN_MEM_READ | \
125 #define DATA_FLAGS_DOS \
126 (IMAGE_SCN_CNT_INITIALIZED_DATA)
128 #define BSS_FLAGS_WIN \
129 (IMAGE_SCN_CNT_UNINITIALIZED_DATA | \
130 IMAGE_SCN_ALIGN_4BYTES | \
131 IMAGE_SCN_MEM_READ | \
133 #define BSS_FLAGS_DOS \
134 (IMAGE_SCN_CNT_UNINITIALIZED_DATA)
136 #define RDATA_FLAGS_WIN \
137 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
138 IMAGE_SCN_ALIGN_8BYTES | \
141 #define RDATA_FLAGS_DOS \
142 (IMAGE_SCN_CNT_INITIALIZED_DATA)
144 #define PDATA_FLAGS \
145 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
146 IMAGE_SCN_ALIGN_4BYTES | \
149 #define XDATA_FLAGS \
150 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
151 IMAGE_SCN_ALIGN_8BYTES | \
155 (IMAGE_SCN_ALIGN_1BYTES | \
156 IMAGE_SCN_LNK_INFO | \
157 IMAGE_SCN_LNK_REMOVE)
159 #define TEXT_FLAGS ((win32 | win64) ? TEXT_FLAGS_WIN : TEXT_FLAGS_DOS)
160 #define DATA_FLAGS ((win32 | win64) ? DATA_FLAGS_WIN : DATA_FLAGS_DOS)
161 #define BSS_FLAGS ((win32 | win64) ? BSS_FLAGS_WIN : BSS_FLAGS_DOS)
162 #define RDATA_FLAGS ((win32 | win64) ? RDATA_FLAGS_WIN : RDATA_FLAGS_DOS)
164 #define SECT_DELTA 32
165 struct coff_Section
**coff_sects
;
169 struct SAA
*coff_syms
;
172 static int32_t def_seg
;
176 static struct RAA
*bsym
, *symval
;
178 struct SAA
*coff_strs
;
179 static uint32_t strslen
;
181 static void coff_gen_init(void);
182 static void coff_sect_write(struct coff_Section
*, const uint8_t *, uint32_t);
183 static void coff_write(void);
184 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int32_t, int, int32_t);
185 static void coff_write_relocs(struct coff_Section
*);
186 static void coff_write_symbols(void);
188 static void coff_win32_init(void)
195 static void coff_win64_init(void)
200 imagebase_sect
= seg_alloc()+1;
201 backend_label(WRT_IMAGEBASE
, imagebase_sect
, 0);
204 static void coff_std_init(void)
206 win32
= win64
= false;
210 static void coff_gen_init(void)
214 coff_nsects
= sectlen
= 0;
215 coff_syms
= saa_init(sizeof(struct coff_Symbol
));
219 coff_strs
= saa_init(1);
221 def_seg
= seg_alloc();
224 static void coff_cleanup(void)
226 struct coff_Reloc
*r
;
232 for (i
= 0; i
< coff_nsects
; i
++) {
233 if (coff_sects
[i
]->data
)
234 saa_free(coff_sects
[i
]->data
);
235 while (coff_sects
[i
]->head
) {
236 r
= coff_sects
[i
]->head
;
237 coff_sects
[i
]->head
= coff_sects
[i
]->head
->next
;
240 nasm_free(coff_sects
[i
]->name
);
241 nasm_free(coff_sects
[i
]);
243 nasm_free(coff_sects
);
250 int coff_make_section(char *name
, uint32_t flags
)
252 struct coff_Section
*s
;
255 s
= nasm_zalloc(sizeof(*s
));
257 if (flags
!= BSS_FLAGS
)
258 s
->data
= saa_init(1);
260 if (!strcmp(name
, ".text"))
263 s
->index
= seg_alloc();
265 namelen
= strlen(name
);
267 if (win32
|| win64
) {
268 s
->namepos
= strslen
+ 4;
269 saa_wbytes(coff_strs
, name
, namelen
+ 1);
270 strslen
+= namelen
+ 1;
275 s
->name
= nasm_malloc(namelen
+ 1);
276 strncpy(s
->name
, name
, namelen
);
277 s
->name
[namelen
] = '\0';
280 if (coff_nsects
>= sectlen
) {
281 sectlen
+= SECT_DELTA
;
282 coff_sects
= nasm_realloc(coff_sects
, sectlen
* sizeof(*coff_sects
));
284 coff_sects
[coff_nsects
++] = s
;
286 return coff_nsects
- 1;
289 static inline int32_t coff_sectalign_flags(unsigned int align
)
291 return (ilog2_32(align
) + 1) << 20;
294 static int32_t coff_section_names(char *name
, int pass
, int *bits
)
297 uint32_t flags
, align_and
= ~0L, align_or
= 0L;
313 while (*p
&& !nasm_isspace(*p
))
317 if (strlen(name
) > 8) {
318 if (!win32
&& !win64
) {
319 nasm_error(ERR_WARNING
,
320 "COFF section names limited to 8 characters: truncating");
326 while (*p
&& nasm_isspace(*p
))
330 while (*p
&& !nasm_isspace(*p
))
334 while (*p
&& nasm_isspace(*p
))
337 if (!nasm_stricmp(q
, "code") || !nasm_stricmp(q
, "text")) {
339 } else if (!nasm_stricmp(q
, "data")) {
341 } else if (!nasm_stricmp(q
, "rdata")) {
345 flags
= DATA_FLAGS
; /* gotta do something */
346 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
347 " read-only data sections");
349 } else if (!nasm_stricmp(q
, "bss")) {
351 } else if (!nasm_stricmp(q
, "info")) {
355 flags
= DATA_FLAGS
; /* gotta do something */
356 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
357 " informational sections");
359 } else if (!nasm_strnicmp(q
, "align=", 6)) {
360 if (!(win32
| win64
))
361 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
362 " section alignment specification");
364 if (q
[6 + strspn(q
+ 6, "0123456789")])
365 nasm_error(ERR_NONFATAL
,
366 "argument to `align' is not numeric");
368 unsigned int align
= atoi(q
+ 6);
369 if (!align
|| ((align
- 1) & align
))
370 nasm_error(ERR_NONFATAL
, "argument to `align' is not a"
373 nasm_error(ERR_NONFATAL
, "Win32 cannot align sections"
374 " to better than 64-byte boundaries");
376 align_and
= ~0x00F00000L
;
377 align_or
= coff_sectalign_flags(align
);
384 for (i
= 0; i
< coff_nsects
; i
++)
385 if (!strcmp(name
, coff_sects
[i
]->name
))
387 if (i
== coff_nsects
) {
389 if (!strcmp(name
, ".data"))
391 else if (!strcmp(name
, ".rdata"))
393 else if (!strcmp(name
, ".bss"))
395 else if (win64
&& !strcmp(name
, ".pdata"))
397 else if (win64
&& !strcmp(name
, ".xdata"))
402 i
= coff_make_section(name
, flags
);
404 coff_sects
[i
]->flags
= flags
;
405 coff_sects
[i
]->flags
&= align_and
;
406 coff_sects
[i
]->flags
|= align_or
;
407 } else if (pass
== 1) {
408 /* Check if any flags are specified */
410 unsigned int align_flags
= flags
& IMAGE_SCN_ALIGN_MASK
;
412 /* Warn if non-alignment flags differ */
413 if ((flags
^ coff_sects
[i
]->flags
) & ~IMAGE_SCN_ALIGN_MASK
) {
414 nasm_error(ERR_WARNING
, "section attributes ignored on"
415 " redeclaration of section `%s'", name
);
417 /* Check if alignment might be needed */
418 if (align_flags
> IMAGE_SCN_ALIGN_1BYTES
) {
419 unsigned int sect_align_flags
= coff_sects
[i
]->flags
& IMAGE_SCN_ALIGN_MASK
;
421 /* Compute the actual alignment */
422 unsigned int align
= 1u << ((align_flags
- IMAGE_SCN_ALIGN_1BYTES
) >> 20);
424 /* Update section header as needed */
425 if (align_flags
> sect_align_flags
) {
426 coff_sects
[i
]->flags
= (coff_sects
[i
]->flags
& ~IMAGE_SCN_ALIGN_MASK
) | align_flags
;
428 /* Check if not already aligned */
429 if (coff_sects
[i
]->len
% align
) {
430 unsigned int padding
= (align
- coff_sects
[i
]->len
) % align
;
431 /* We need to write at most 8095 bytes */
433 if (coff_sects
[i
]->flags
& IMAGE_SCN_CNT_CODE
) {
434 /* Fill with INT 3 instructions */
435 memset(buffer
, 0xCC, padding
);
437 memset(buffer
, 0x00, padding
);
439 saa_wbytes(coff_sects
[i
]->data
, buffer
, padding
);
440 coff_sects
[i
]->len
+= padding
;
446 return coff_sects
[i
]->index
;
449 static void coff_deflabel(char *name
, int32_t segment
, int64_t offset
,
450 int is_global
, char *special
)
452 int pos
= strslen
+ 4;
453 struct coff_Symbol
*sym
;
456 nasm_error(ERR_NONFATAL
, "COFF format does not support any"
457 " special symbol types");
459 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
460 if (strcmp(name
,WRT_IMAGEBASE
))
461 nasm_error(ERR_NONFATAL
, "unrecognized special symbol `%s'", name
);
465 if (strlen(name
) > 8) {
466 size_t nlen
= strlen(name
)+1;
467 saa_wbytes(coff_strs
, name
, nlen
);
472 sym
= saa_wstruct(coff_syms
);
475 sym
->namlen
= strlen(name
);
477 strcpy(sym
->name
, name
);
478 sym
->is_global
= !!is_global
;
479 sym
->type
= 0; /* Default to T_NULL (no type) */
480 if (segment
== NO_SEG
)
481 sym
->section
= -1; /* absolute symbol */
485 for (i
= 0; i
< coff_nsects
; i
++)
486 if (segment
== coff_sects
[i
]->index
) {
487 sym
->section
= i
+ 1;
491 sym
->is_global
= true;
496 sym
->value
= (sym
->section
== 0 ? 0 : offset
);
499 * define the references from external-symbol segment numbers
500 * to these symbol records.
502 if (sym
->section
== 0)
503 bsym
= raa_write(bsym
, segment
, coff_nsyms
);
505 if (segment
!= NO_SEG
)
506 symval
= raa_write(symval
, segment
, sym
->section
? 0 : sym
->value
);
511 static int32_t coff_add_reloc(struct coff_Section
*sect
, int32_t segment
,
514 struct coff_Reloc
*r
;
516 r
= *sect
->tail
= nasm_malloc(sizeof(struct coff_Reloc
));
517 sect
->tail
= &r
->next
;
520 r
->address
= sect
->len
;
521 if (segment
== NO_SEG
) {
522 r
->symbol
= 0, r
->symbase
= ABS_SYMBOL
;
525 r
->symbase
= REAL_SYMBOLS
;
526 for (i
= 0; i
< coff_nsects
; i
++) {
527 if (segment
== coff_sects
[i
]->index
) {
529 r
->symbase
= SECT_SYMBOLS
;
533 if (r
->symbase
== REAL_SYMBOLS
)
534 r
->symbol
= raa_read(bsym
, segment
);
541 * Return the fixup for standard COFF common variables.
543 if (r
->symbase
== REAL_SYMBOLS
&& !(win32
| win64
))
544 return raa_read(symval
, segment
);
549 static void coff_out(int32_t segto
, const void *data
,
550 enum out_type type
, uint64_t size
,
551 int32_t segment
, int32_t wrt
)
553 struct coff_Section
*s
;
554 uint8_t mydata
[8], *p
;
557 if (wrt
!= NO_SEG
&& !win64
) {
558 wrt
= NO_SEG
; /* continue to do _something_ */
559 nasm_error(ERR_NONFATAL
, "WRT not supported by COFF output formats");
563 for (i
= 0; i
< coff_nsects
; i
++) {
564 if (segto
== coff_sects
[i
]->index
) {
570 int tempint
; /* ignored */
571 if (segto
!= coff_section_names(".text", 2, &tempint
))
572 nasm_panic("strange segment conditions in COFF driver");
574 s
= coff_sects
[coff_nsects
- 1];
577 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
578 if (win64
&& wrt
== NO_SEG
) {
579 if (!strcmp(s
->name
,".pdata") || !strcmp(s
->name
,".xdata"))
580 wrt
= imagebase_sect
;
583 if (!s
->data
&& type
!= OUT_RESERVE
) {
584 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
585 " BSS section `%s': ignored", s
->name
);
586 s
->len
+= realsize(type
, size
);
590 memset(mydata
, 0, sizeof(mydata
));
592 if (dfmt
&& dfmt
->debug_output
) {
593 struct coff_DebugInfo dinfo
;
598 if (type
== OUT_ADDRESS
)
599 dinfo
.size
= abs((int)size
);
601 dinfo
.size
= realsize(type
, size
);
603 dfmt
->debug_output(type
, &dinfo
);
606 if (type
== OUT_RESERVE
) {
608 nasm_error(ERR_WARNING
, "uninitialised space declared in"
609 " non-BSS section `%s': zeroing", s
->name
);
610 coff_sect_write(s
, NULL
, size
);
613 } else if (type
== OUT_RAWDATA
) {
614 coff_sect_write(s
, data
, size
);
615 } else if (type
== OUT_ADDRESS
) {
616 int asize
= abs((int)size
);
618 if (asize
!= 4 && (segment
!= NO_SEG
|| wrt
!= NO_SEG
)) {
619 nasm_error(ERR_NONFATAL
, "COFF format does not support non-32-bit"
623 if (segment
!= NO_SEG
|| wrt
!= NO_SEG
) {
625 nasm_error(ERR_NONFATAL
, "COFF format does not support"
627 } else if (segment
% 2) {
628 nasm_error(ERR_NONFATAL
, "COFF format does not support"
629 " segment base references");
631 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_I386_DIR32
);
634 WRITELONG(p
, *(int64_t *)data
+ fix
);
635 coff_sect_write(s
, mydata
, asize
);
641 if (wrt
== imagebase_sect
) {
642 nasm_error(ERR_NONFATAL
, "operand size mismatch: 'wrt "
643 WRT_IMAGEBASE
"' is a 32-bit operand");
645 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_AMD64_ADDR64
);
646 WRITEDLONG(p
, *(int64_t *)data
+ fix
);
647 coff_sect_write(s
, mydata
, asize
);
649 fix
= coff_add_reloc(s
, segment
,
650 wrt
== imagebase_sect
? IMAGE_REL_AMD64_ADDR32NB
:
651 IMAGE_REL_AMD64_ADDR32
);
652 WRITELONG(p
, *(int64_t *)data
+ fix
);
653 coff_sect_write(s
, mydata
, asize
);
656 } else if (type
== OUT_REL2ADR
) {
657 nasm_error(ERR_NONFATAL
, "COFF format does not support 16-bit"
659 } else if (type
== OUT_REL4ADR
) {
660 if (segment
== segto
&& !(win64
)) /* Acceptable for RIP-relative */
661 nasm_panic("intra-segment OUT_REL4ADR");
662 else if (segment
== NO_SEG
&& win32
)
663 nasm_error(ERR_NONFATAL
, "Win32 COFF does not correctly support"
664 " relative references to absolute addresses");
667 if (segment
!= NO_SEG
&& segment
% 2) {
668 nasm_error(ERR_NONFATAL
, "COFF format does not support"
669 " segment base references");
671 fix
= coff_add_reloc(s
, segment
,
672 win64
? IMAGE_REL_AMD64_REL32
: IMAGE_REL_I386_REL32
);
675 WRITELONG(p
, *(int64_t *)data
+ 4 - size
+ fix
);
677 WRITELONG(p
, *(int64_t *)data
- (size
+ s
->len
) + fix
);
679 coff_sect_write(s
, mydata
, 4L);
685 static void coff_sect_write(struct coff_Section
*sect
,
686 const uint8_t *data
, uint32_t len
)
688 saa_wbytes(sect
->data
, data
, len
);
692 typedef struct tagString
{
693 struct tagString
*next
;
698 #define EXPORT_SECTION_NAME ".drectve"
699 #define EXPORT_SECTION_FLAGS INFO_FLAGS
701 * #define EXPORT_SECTION_NAME ".text"
702 * #define EXPORT_SECTION_FLAGS TEXT_FLAGS
705 static STRING
*Exports
= NULL
;
706 static struct coff_Section
*directive_sec
;
707 static void AddExport(char *name
)
709 STRING
*rvp
= Exports
, *newS
;
711 newS
= (STRING
*) nasm_malloc(sizeof(STRING
));
712 newS
->len
= strlen(name
);
714 newS
->String
= (char *)nasm_malloc(newS
->len
+ 1);
715 strcpy(newS
->String
, name
);
719 for (i
= 0; i
< coff_nsects
; i
++) {
720 if (!strcmp(EXPORT_SECTION_NAME
, coff_sects
[i
]->name
))
724 if (i
== coff_nsects
)
725 i
= coff_make_section(EXPORT_SECTION_NAME
, EXPORT_SECTION_FLAGS
);
727 directive_sec
= coff_sects
[i
];
731 if (!strcmp(rvp
->String
, name
))
739 static void BuildExportTable(STRING
**rvp
)
746 list_for_each_safe(p
, t
, *rvp
) {
747 coff_sect_write(directive_sec
, (uint8_t *)"-export:", 8);
748 coff_sect_write(directive_sec
, (uint8_t *)p
->String
, p
->len
);
749 coff_sect_write(directive_sec
, (uint8_t *)" ", 1);
750 nasm_free(p
->String
);
757 static enum directive_result
758 coff_directives(enum directive directive
, char *value
, int pass
)
766 return DIRR_OK
; /* ignore in pass two */
768 while (*q
&& !nasm_isspace(*q
))
770 if (nasm_isspace(*q
)) {
772 while (*q
&& nasm_isspace(*q
))
777 nasm_error(ERR_NONFATAL
, "`export' directive requires export name");
781 nasm_error(ERR_NONFATAL
, "unrecognized export qualifier `%s'", q
);
792 if (!win32
) /* Only applicable for -f win32 */
796 for (i
= 0; i
< coff_nsects
; i
++)
797 if (!strcmp(".sxdata",coff_sects
[i
]->name
))
799 if (i
== coff_nsects
)
800 sxseg
= coff_make_section(".sxdata", IMAGE_SCN_LNK_INFO
);
805 * pass0 == 2 is the only time when the full set of symbols are
806 * guaranteed to be present; it is the final output pass.
810 saa_rewind(coff_syms
);
811 for (n
= 0; n
< coff_nsyms
; n
++) {
812 struct coff_Symbol
*sym
= saa_rstruct(coff_syms
);
816 * sym->strpos is biased by 4, because symbol
817 * table is prefixed with table length
819 if (sym
->strpos
>=4) {
820 char *name
= nasm_malloc(sym
->namlen
+1);
821 saa_fread(coff_strs
, sym
->strpos
-4, name
, sym
->namlen
);
822 name
[sym
->namlen
] = '\0';
823 equals
= !strcmp(value
,name
);
826 equals
= !strcmp(value
,sym
->name
);
831 * this value arithmetics effectively reflects
832 * initsym in coff_write(): 2 for file, 1 for
833 * .absolute and two per each section
835 unsigned char value
[4],*p
=value
;
836 WRITELONG(p
,n
+ 2 + 1 + coff_nsects
*2);
837 coff_sect_write(coff_sects
[sxseg
],value
,4);
842 if (n
== coff_nsyms
) {
843 nasm_error(ERR_NONFATAL
,
844 "`safeseh' directive requires valid symbol");
855 /* handle relocations storm, valid for win32/64 only */
856 static inline void coff_adjust_relocs(struct coff_Section
*s
)
858 if (s
->nrelocs
< IMAGE_SCN_MAX_RELOC
)
863 if (ofmt
== &of_coff
)
864 nasm_fatal("Too many relocations (%d) for section `%s'",
865 s
->nrelocs
, s
->name
);
869 s
->flags
|= IMAGE_SCN_LNK_NRELOC_OVFL
;
873 static void coff_write(void)
875 int32_t pos
, sympos
, vsize
;
878 /* fill in the .drectve section with -export's */
879 BuildExportTable(&Exports
);
882 /* add default value for @feat.00, this allows to 'link /safeseh' */
885 saa_rewind(coff_syms
);
886 for (n
= 0; n
< coff_nsyms
; n
++) {
887 struct coff_Symbol
*sym
= saa_rstruct(coff_syms
);
888 if (sym
->strpos
== -1 && !strcmp("@feat.00",sym
->name
))
892 coff_deflabel("@feat.00", NO_SEG
, 1, 0, NULL
);
896 * Work out how big the file will get.
897 * Calculate the start of the `real' symbols at the same time.
898 * Check for massive relocations.
900 pos
= 0x14 + 0x28 * coff_nsects
;
901 initsym
= 3; /* two for the file, one absolute */
902 for (i
= 0; i
< coff_nsects
; i
++) {
903 if (coff_sects
[i
]->data
) {
904 coff_adjust_relocs(coff_sects
[i
]);
905 coff_sects
[i
]->pos
= pos
;
906 pos
+= coff_sects
[i
]->len
;
907 coff_sects
[i
]->relpos
= pos
;
908 pos
+= 10 * coff_sects
[i
]->nrelocs
;
910 coff_sects
[i
]->pos
= coff_sects
[i
]->relpos
= 0L;
911 initsym
+= 2; /* two for each section */
916 * Output the COFF header.
919 i
= IMAGE_FILE_MACHINE_AMD64
;
921 i
= IMAGE_FILE_MACHINE_I386
;
922 fwriteint16_t(i
, ofile
); /* machine type */
923 fwriteint16_t(coff_nsects
, ofile
); /* number of sections */
924 fwriteint32_t(time(NULL
), ofile
); /* time stamp */
925 fwriteint32_t(sympos
, ofile
);
926 fwriteint32_t(coff_nsyms
+ initsym
, ofile
);
927 fwriteint16_t(0, ofile
); /* no optional header */
928 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
929 fwriteint16_t((win32
| win64
) ? 0 : 0x104, ofile
);
932 * Output the section headers.
935 for (i
= 0; i
< coff_nsects
; i
++) {
936 coff_section_header(coff_sects
[i
]->name
, coff_sects
[i
]->namepos
, vsize
, coff_sects
[i
]->len
,
937 coff_sects
[i
]->pos
, coff_sects
[i
]->relpos
,
938 coff_sects
[i
]->nrelocs
, coff_sects
[i
]->flags
);
939 vsize
+= coff_sects
[i
]->len
;
943 * Output the sections and their relocations.
945 for (i
= 0; i
< coff_nsects
; i
++)
946 if (coff_sects
[i
]->data
) {
947 saa_fpwrite(coff_sects
[i
]->data
, ofile
);
948 coff_write_relocs(coff_sects
[i
]);
952 * Output the symbol and string tables.
954 coff_write_symbols();
955 fwriteint32_t(strslen
+ 4, ofile
); /* length includes length count */
956 saa_fpwrite(coff_strs
, ofile
);
959 static void coff_section_header(char *name
, int32_t namepos
, int32_t vsize
,
960 int32_t datalen
, int32_t datapos
,
961 int32_t relpos
, int nrelocs
, int32_t flags
)
968 strncpy(padname
, name
, 8);
969 nasm_write(padname
, 8, ofile
);
972 * If name is longer than 8 bytes, write '/' followed
973 * by offset into the strings table represented as
976 namepos
= namepos
% 100000000;
978 padname
[1] = '0' + (namepos
/ 1000000);
979 namepos
= namepos
% 1000000;
980 padname
[2] = '0' + (namepos
/ 100000);
981 namepos
= namepos
% 100000;
982 padname
[3] = '0' + (namepos
/ 10000);
983 namepos
= namepos
% 10000;
984 padname
[4] = '0' + (namepos
/ 1000);
985 namepos
= namepos
% 1000;
986 padname
[5] = '0' + (namepos
/ 100);
987 namepos
= namepos
% 100;
988 padname
[6] = '0' + (namepos
/ 10);
989 namepos
= namepos
% 10;
990 padname
[7] = '0' + (namepos
);
991 nasm_write(padname
, 8, ofile
);
994 fwriteint32_t(0, ofile
); /* Virtual size field - set to 0 or vsize */
995 fwriteint32_t(0L, ofile
); /* RVA/offset - we ignore */
996 fwriteint32_t(datalen
, ofile
);
997 fwriteint32_t(datapos
, ofile
);
998 fwriteint32_t(relpos
, ofile
);
999 fwriteint32_t(0L, ofile
); /* no line numbers - we don't do 'em */
1002 * a special case -- if there are too many relocs
1003 * we have to put IMAGE_SCN_MAX_RELOC here and write
1004 * the real relocs number into VirtualAddress of first
1007 if (flags
& IMAGE_SCN_LNK_NRELOC_OVFL
)
1008 fwriteint16_t(IMAGE_SCN_MAX_RELOC
, ofile
);
1010 fwriteint16_t(nrelocs
, ofile
);
1012 fwriteint16_t(0, ofile
); /* again, no line numbers */
1013 fwriteint32_t(flags
, ofile
);
1016 static void coff_write_relocs(struct coff_Section
*s
)
1018 struct coff_Reloc
*r
;
1020 /* a real number of relocations if needed */
1021 if (s
->flags
& IMAGE_SCN_LNK_NRELOC_OVFL
) {
1022 fwriteint32_t(s
->nrelocs
, ofile
);
1023 fwriteint32_t(0, ofile
);
1024 fwriteint16_t(0, ofile
);
1027 for (r
= s
->head
; r
; r
= r
->next
) {
1028 fwriteint32_t(r
->address
, ofile
);
1029 fwriteint32_t(r
->symbol
+ (r
->symbase
== REAL_SYMBOLS
? initsym
:
1030 r
->symbase
== ABS_SYMBOL
? initsym
- 1 :
1031 r
->symbase
== SECT_SYMBOLS
? 2 : 0),
1033 fwriteint16_t(r
->type
, ofile
);
1037 static void coff_symbol(char *name
, int32_t strpos
, int32_t value
,
1038 int section
, int type
, int storageclass
, int aux
)
1043 strncpy(padname
, name
, 8);
1044 nasm_write(padname
, 8, ofile
);
1046 fwriteint32_t(0, ofile
);
1047 fwriteint32_t(strpos
, ofile
);
1050 fwriteint32_t(value
, ofile
);
1051 fwriteint16_t(section
, ofile
);
1052 fwriteint16_t(type
, ofile
);
1054 fputc(storageclass
, ofile
);
1058 static void coff_write_symbols(void)
1064 * The `.file' record, and the file name auxiliary record.
1066 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
1067 strncpy(filename
, inname
, 18);
1068 nasm_write(filename
, 18, ofile
);
1071 * The section records, with their auxiliaries.
1073 memset(filename
, 0, 18); /* useful zeroed buffer */
1075 for (i
= 0; i
< (uint32_t) coff_nsects
; i
++) {
1076 coff_symbol(coff_sects
[i
]->name
, 0L, 0L, i
+ 1, 0, 3, 1);
1077 fwriteint32_t(coff_sects
[i
]->len
, ofile
);
1078 fwriteint16_t(coff_sects
[i
]->nrelocs
,ofile
);
1079 nasm_write(filename
, 12, ofile
);
1083 * The absolute symbol, for relative-to-absolute relocations.
1085 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
1090 saa_rewind(coff_syms
);
1091 for (i
= 0; i
< coff_nsyms
; i
++) {
1092 struct coff_Symbol
*sym
= saa_rstruct(coff_syms
);
1093 coff_symbol(sym
->strpos
== -1 ? sym
->name
: NULL
,
1094 sym
->strpos
, sym
->value
, sym
->section
,
1095 sym
->type
, sym
->is_global
? 2 : 3, 0);
1099 static void coff_sectalign(int32_t seg
, unsigned int value
)
1101 struct coff_Section
*s
= NULL
;
1105 for (i
= 0; i
< coff_nsects
; i
++) {
1106 if (coff_sects
[i
]->index
== seg
) {
1112 if (!s
|| !is_power2(value
))
1115 /* DOS has limitation on 64 bytes */
1116 if (!(win32
| win64
) && value
> 64)
1119 align
= (s
->flags
& IMAGE_SCN_ALIGN_MASK
);
1120 value
= coff_sectalign_flags(value
);
1122 s
->flags
= (s
->flags
& ~IMAGE_SCN_ALIGN_MASK
) | value
;
1125 extern macros_t coff_stdmac
[];
1127 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
1131 const struct ofmt of_coff
= {
1132 "COFF (i386) object files (e.g. DJGPP for DOS)",
1142 nasm_do_legacy_output
,
1151 NULL
/* pragma list */
1156 extern const struct dfmt df_cv8
;
1160 static const struct dfmt
* const win32_debug_arr
[2] = { &df_cv8
, NULL
};
1162 const struct ofmt of_win32
= {
1163 "Microsoft Win32 (i386) object files",
1173 nasm_do_legacy_output
,
1182 NULL
/* pragma list */
1189 static const struct dfmt
* const win64_debug_arr
[2] = { &df_cv8
, NULL
};
1191 const struct ofmt of_win64
= {
1192 "Microsoft Win64 (x86-64) object files",
1202 nasm_do_legacy_output
,
1211 NULL
/* pragma list */