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)
56 #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
61 * (0) When I say `standard COFF' below, I mean `COFF as output and
62 * used by DJGPP'. I assume DJGPP gets it right.
64 * (1) Win32 appears to interpret the term `relative relocation'
65 * differently from standard COFF. Standard COFF understands a
66 * relative relocation to mean that during relocation you add the
67 * address of the symbol you're referencing, and subtract the base
68 * address of the section you're in. Win32 COFF, by contrast, seems
69 * to add the address of the symbol and then subtract the address
70 * of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
71 * subtly incompatible.
73 * (2) Win32 doesn't bother putting any flags in the header flags
74 * field (at offset 0x12 into the file).
76 * (3) Win32 uses some extra flags into the section header table:
77 * it defines flags 0x80000000 (writable), 0x40000000 (readable)
78 * and 0x20000000 (executable), and uses them in the expected
79 * combinations. It also defines 0x00100000 through 0x00700000 for
80 * section alignments of 1 through 64 bytes.
82 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
83 * field directly after the section name in the section header
84 * table for something strange: they store what the address of the
85 * section start point _would_ be, if you laid all the sections end
86 * to end starting at zero. Dunno why. Microsoft's documentation
87 * lists this field as "Virtual Size of Section", which doesn't
88 * seem to fit at all. In fact, Win32 even includes non-linked
89 * sections such as .drectve in this calculation.
91 * Newer versions of MASM seem to have changed this to be zero, and
92 * that apparently matches the COFF spec, so go with that.
94 * (5) Standard COFF does something very strange to common
95 * variables: the relocation point for a common variable is as far
96 * _before_ the variable as its size stretches out _after_ it. So
97 * we must fix up common variable references. Win32 seems to be
98 * sensible on this one.
101 /* Flag which version of COFF we are currently outputting. */
104 static int32_t imagebase_sect
;
105 #define WRT_IMAGEBASE "..imagebase"
107 char coff_infile
[FILENAME_MAX
];
108 char coff_outfile
[FILENAME_MAX
];
111 * Some common section flags by default
113 #define TEXT_FLAGS_WIN \
114 (IMAGE_SCN_CNT_CODE | \
115 IMAGE_SCN_ALIGN_16BYTES | \
116 IMAGE_SCN_MEM_EXECUTE | \
118 #define TEXT_FLAGS_DOS \
121 #define DATA_FLAGS_WIN \
122 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
123 IMAGE_SCN_ALIGN_4BYTES | \
124 IMAGE_SCN_MEM_READ | \
126 #define DATA_FLAGS_DOS \
127 (IMAGE_SCN_CNT_INITIALIZED_DATA)
129 #define BSS_FLAGS_WIN \
130 (IMAGE_SCN_CNT_UNINITIALIZED_DATA | \
131 IMAGE_SCN_ALIGN_4BYTES | \
132 IMAGE_SCN_MEM_READ | \
134 #define BSS_FLAGS_DOS \
135 (IMAGE_SCN_CNT_UNINITIALIZED_DATA)
137 #define RDATA_FLAGS_WIN \
138 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
139 IMAGE_SCN_ALIGN_8BYTES | \
142 #define RDATA_FLAGS_DOS \
143 (IMAGE_SCN_CNT_INITIALIZED_DATA)
145 #define PDATA_FLAGS \
146 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
147 IMAGE_SCN_ALIGN_4BYTES | \
150 #define XDATA_FLAGS \
151 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
152 IMAGE_SCN_ALIGN_8BYTES | \
156 (IMAGE_SCN_ALIGN_1BYTES | \
157 IMAGE_SCN_LNK_INFO | \
158 IMAGE_SCN_LNK_REMOVE)
160 #define TEXT_FLAGS ((win32 | win64) ? TEXT_FLAGS_WIN : TEXT_FLAGS_DOS)
161 #define DATA_FLAGS ((win32 | win64) ? DATA_FLAGS_WIN : DATA_FLAGS_DOS)
162 #define BSS_FLAGS ((win32 | win64) ? BSS_FLAGS_WIN : BSS_FLAGS_DOS)
163 #define RDATA_FLAGS ((win32 | win64) ? RDATA_FLAGS_WIN : RDATA_FLAGS_DOS)
165 #define SECT_DELTA 32
166 struct coff_Section
**coff_sects
;
170 struct SAA
*coff_syms
;
173 static int32_t def_seg
;
177 static struct RAA
*bsym
, *symval
;
179 struct SAA
*coff_strs
;
180 static uint32_t strslen
;
182 static void coff_gen_init(void);
183 static void coff_sect_write(struct coff_Section
*, const uint8_t *, uint32_t);
184 static void coff_write(void);
185 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int32_t, int, int32_t);
186 static void coff_write_relocs(struct coff_Section
*);
187 static void coff_write_symbols(void);
189 static void coff_win32_init(void)
196 static void coff_win64_init(void)
201 imagebase_sect
= seg_alloc()+1;
202 define_label(WRT_IMAGEBASE
, imagebase_sect
, 0, NULL
, false, false);
205 static void coff_std_init(void)
207 win32
= win64
= false;
211 static void coff_gen_init(void)
215 coff_nsects
= sectlen
= 0;
216 coff_syms
= saa_init(sizeof(struct coff_Symbol
));
220 coff_strs
= saa_init(1);
222 def_seg
= seg_alloc();
225 static void coff_cleanup(void)
227 struct coff_Reloc
*r
;
233 for (i
= 0; i
< coff_nsects
; i
++) {
234 if (coff_sects
[i
]->data
)
235 saa_free(coff_sects
[i
]->data
);
236 while (coff_sects
[i
]->head
) {
237 r
= coff_sects
[i
]->head
;
238 coff_sects
[i
]->head
= coff_sects
[i
]->head
->next
;
241 nasm_free(coff_sects
[i
]->name
);
242 nasm_free(coff_sects
[i
]);
244 nasm_free(coff_sects
);
251 int coff_make_section(char *name
, uint32_t flags
)
253 struct coff_Section
*s
;
256 s
= nasm_zalloc(sizeof(*s
));
258 if (flags
!= BSS_FLAGS
)
259 s
->data
= saa_init(1);
261 if (!strcmp(name
, ".text"))
264 s
->index
= seg_alloc();
266 namelen
= strlen(name
);
268 if (win32
|| win64
) {
269 s
->namepos
= strslen
+ 4;
270 saa_wbytes(coff_strs
, name
, namelen
+ 1);
271 strslen
+= namelen
+ 1;
276 s
->name
= nasm_malloc(namelen
+ 1);
277 strncpy(s
->name
, name
, namelen
);
278 s
->name
[namelen
] = '\0';
281 if (coff_nsects
>= sectlen
) {
282 sectlen
+= SECT_DELTA
;
283 coff_sects
= nasm_realloc(coff_sects
, sectlen
* sizeof(*coff_sects
));
285 coff_sects
[coff_nsects
++] = s
;
287 return coff_nsects
- 1;
290 static inline int32_t coff_sectalign_flags(unsigned int align
)
292 return (ilog2_32(align
) + 1) << 20;
295 static int32_t coff_section_names(char *name
, int pass
, int *bits
)
298 uint32_t flags
, align_and
= ~0L, align_or
= 0L;
315 while (*p
&& !nasm_isspace(*p
))
319 if (strlen(name
) > 8) {
320 if (!win32
&& !win64
) {
321 nasm_error(ERR_WARNING
,
322 "COFF section names limited to 8 characters: truncating");
328 while (*p
&& nasm_isspace(*p
))
332 while (*p
&& !nasm_isspace(*p
))
336 while (*p
&& nasm_isspace(*p
))
339 if (!nasm_stricmp(q
, "code") || !nasm_stricmp(q
, "text")) {
341 } else if (!nasm_stricmp(q
, "data")) {
343 } else if (!nasm_stricmp(q
, "rdata")) {
347 flags
= DATA_FLAGS
; /* gotta do something */
348 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
349 " read-only data sections");
351 } else if (!nasm_stricmp(q
, "bss")) {
353 } else if (!nasm_stricmp(q
, "info")) {
357 flags
= DATA_FLAGS
; /* gotta do something */
358 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
359 " informational sections");
361 } else if (!nasm_strnicmp(q
, "align=", 6)) {
362 if (!(win32
| win64
))
363 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
364 " section alignment specification");
366 if (q
[6 + strspn(q
+ 6, "0123456789")])
367 nasm_error(ERR_NONFATAL
,
368 "argument to `align' is not numeric");
370 unsigned int align
= atoi(q
+ 6);
371 if (!align
|| ((align
- 1) & align
))
372 nasm_error(ERR_NONFATAL
, "argument to `align' is not a"
375 nasm_error(ERR_NONFATAL
, "Win32 cannot align sections"
376 " to better than 64-byte boundaries");
378 align_and
= ~0x00F00000L
;
379 align_or
= coff_sectalign_flags(align
);
386 for (i
= 0; i
< coff_nsects
; i
++)
387 if (!strcmp(name
, coff_sects
[i
]->name
))
389 if (i
== coff_nsects
) {
391 if (!strcmp(name
, ".data"))
393 else if (!strcmp(name
, ".rdata"))
395 else if (!strcmp(name
, ".bss"))
397 else if (win64
&& !strcmp(name
, ".pdata"))
399 else if (win64
&& !strcmp(name
, ".xdata"))
404 i
= coff_make_section(name
, flags
);
406 coff_sects
[i
]->flags
= flags
;
407 coff_sects
[i
]->flags
&= align_and
;
408 coff_sects
[i
]->flags
|= align_or
;
409 } else if (pass
== 1) {
410 /* Check if any flags are specified */
412 unsigned int align_flags
= flags
& IMAGE_SCN_ALIGN_MASK
;
414 /* Warn if non-alignment flags differ */
415 if ((flags
^ coff_sects
[i
]->flags
) & ~IMAGE_SCN_ALIGN_MASK
) {
416 nasm_error(ERR_WARNING
, "section attributes ignored on"
417 " redeclaration of section `%s'", name
);
419 /* Check if alignment might be needed */
420 if (align_flags
> IMAGE_SCN_ALIGN_1BYTES
) {
421 unsigned int sect_align_flags
= coff_sects
[i
]->flags
& IMAGE_SCN_ALIGN_MASK
;
423 /* Compute the actual alignment */
424 unsigned int align
= 1u << ((align_flags
- IMAGE_SCN_ALIGN_1BYTES
) >> 20);
426 /* Update section header as needed */
427 if (align_flags
> sect_align_flags
) {
428 coff_sects
[i
]->flags
= (coff_sects
[i
]->flags
& ~IMAGE_SCN_ALIGN_MASK
) | align_flags
;
430 /* Check if not already aligned */
431 if (coff_sects
[i
]->len
% align
) {
432 unsigned int padding
= (align
- coff_sects
[i
]->len
) % align
;
433 /* We need to write at most 8095 bytes */
435 if (coff_sects
[i
]->flags
& IMAGE_SCN_CNT_CODE
) {
436 /* Fill with INT 3 instructions */
437 memset(buffer
, 0xCC, padding
);
439 memset(buffer
, 0x00, padding
);
441 saa_wbytes(coff_sects
[i
]->data
, buffer
, padding
);
442 coff_sects
[i
]->len
+= padding
;
448 return coff_sects
[i
]->index
;
451 static void coff_deflabel(char *name
, int32_t segment
, int64_t offset
,
452 int is_global
, char *special
)
454 int pos
= strslen
+ 4;
455 struct coff_Symbol
*sym
;
458 nasm_error(ERR_NONFATAL
, "COFF format does not support any"
459 " special symbol types");
461 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
462 if (strcmp(name
,WRT_IMAGEBASE
))
463 nasm_error(ERR_NONFATAL
, "unrecognized special symbol `%s'", name
);
467 if (strlen(name
) > 8) {
468 size_t nlen
= strlen(name
)+1;
469 saa_wbytes(coff_strs
, name
, nlen
);
474 sym
= saa_wstruct(coff_syms
);
477 sym
->namlen
= strlen(name
);
479 strcpy(sym
->name
, name
);
480 sym
->is_global
= !!is_global
;
481 sym
->type
= 0; /* Default to T_NULL (no type) */
482 if (segment
== NO_SEG
)
483 sym
->section
= -1; /* absolute symbol */
487 for (i
= 0; i
< coff_nsects
; i
++)
488 if (segment
== coff_sects
[i
]->index
) {
489 sym
->section
= i
+ 1;
493 sym
->is_global
= true;
498 sym
->value
= (sym
->section
== 0 ? 0 : offset
);
501 * define the references from external-symbol segment numbers
502 * to these symbol records.
504 if (sym
->section
== 0)
505 bsym
= raa_write(bsym
, segment
, coff_nsyms
);
507 if (segment
!= NO_SEG
)
508 symval
= raa_write(symval
, segment
, sym
->section
? 0 : sym
->value
);
513 static int32_t coff_add_reloc(struct coff_Section
*sect
, int32_t segment
,
516 struct coff_Reloc
*r
;
518 r
= *sect
->tail
= nasm_malloc(sizeof(struct coff_Reloc
));
519 sect
->tail
= &r
->next
;
522 r
->address
= sect
->len
;
523 if (segment
== NO_SEG
) {
524 r
->symbol
= 0, r
->symbase
= ABS_SYMBOL
;
527 r
->symbase
= REAL_SYMBOLS
;
528 for (i
= 0; i
< coff_nsects
; i
++) {
529 if (segment
== coff_sects
[i
]->index
) {
531 r
->symbase
= SECT_SYMBOLS
;
535 if (r
->symbase
== REAL_SYMBOLS
)
536 r
->symbol
= raa_read(bsym
, segment
);
543 * Return the fixup for standard COFF common variables.
545 if (r
->symbase
== REAL_SYMBOLS
&& !(win32
| win64
))
546 return raa_read(symval
, segment
);
551 static void coff_out(int32_t segto
, const void *data
,
552 enum out_type type
, uint64_t size
,
553 int32_t segment
, int32_t wrt
)
555 struct coff_Section
*s
;
556 uint8_t mydata
[8], *p
;
559 if (wrt
!= NO_SEG
&& !win64
) {
560 wrt
= NO_SEG
; /* continue to do _something_ */
561 nasm_error(ERR_NONFATAL
, "WRT not supported by COFF output formats");
565 * handle absolute-assembly (structure definitions)
567 if (segto
== NO_SEG
) {
568 if (type
!= OUT_RESERVE
)
569 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
575 for (i
= 0; i
< coff_nsects
; i
++) {
576 if (segto
== coff_sects
[i
]->index
) {
582 int tempint
; /* ignored */
583 if (segto
!= coff_section_names(".text", 2, &tempint
))
584 nasm_panic(0, "strange segment conditions in COFF driver");
586 s
= coff_sects
[coff_nsects
- 1];
589 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
590 if (win64
&& wrt
== NO_SEG
) {
591 if (!strcmp(s
->name
,".pdata") || !strcmp(s
->name
,".xdata"))
592 wrt
= imagebase_sect
;
595 if (!s
->data
&& type
!= OUT_RESERVE
) {
596 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
597 " BSS section `%s': ignored", s
->name
);
598 s
->len
+= realsize(type
, size
);
602 memset(mydata
, 0, sizeof(mydata
));
604 if (dfmt
&& dfmt
->debug_output
) {
605 struct coff_DebugInfo dinfo
;
610 if (type
== OUT_ADDRESS
)
611 dinfo
.size
= abs((int)size
);
613 dinfo
.size
= realsize(type
, size
);
615 dfmt
->debug_output(type
, &dinfo
);
618 if (type
== OUT_RESERVE
) {
620 nasm_error(ERR_WARNING
, "uninitialised space declared in"
621 " non-BSS section `%s': zeroing", s
->name
);
622 coff_sect_write(s
, NULL
, size
);
625 } else if (type
== OUT_RAWDATA
) {
626 if (segment
!= NO_SEG
)
627 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
628 coff_sect_write(s
, data
, size
);
629 } else if (type
== OUT_ADDRESS
) {
630 int asize
= abs((int)size
);
632 if (asize
!= 4 && (segment
!= NO_SEG
|| wrt
!= NO_SEG
)) {
633 nasm_error(ERR_NONFATAL
, "COFF format does not support non-32-bit"
637 if (segment
!= NO_SEG
|| wrt
!= NO_SEG
) {
639 nasm_error(ERR_NONFATAL
, "COFF format does not support"
641 } else if (segment
% 2) {
642 nasm_error(ERR_NONFATAL
, "COFF format does not support"
643 " segment base references");
645 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_I386_DIR32
);
648 WRITELONG(p
, *(int64_t *)data
+ fix
);
649 coff_sect_write(s
, mydata
, asize
);
655 if (wrt
== imagebase_sect
) {
656 nasm_error(ERR_NONFATAL
, "operand size mismatch: 'wrt "
657 WRT_IMAGEBASE
"' is a 32-bit operand");
659 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_AMD64_ADDR64
);
660 WRITEDLONG(p
, *(int64_t *)data
+ fix
);
661 coff_sect_write(s
, mydata
, asize
);
663 fix
= coff_add_reloc(s
, segment
,
664 wrt
== imagebase_sect
? IMAGE_REL_AMD64_ADDR32NB
:
665 IMAGE_REL_AMD64_ADDR32
);
666 WRITELONG(p
, *(int64_t *)data
+ fix
);
667 coff_sect_write(s
, mydata
, asize
);
670 } else if (type
== OUT_REL2ADR
) {
671 nasm_error(ERR_NONFATAL
, "COFF format does not support 16-bit"
673 } else if (type
== OUT_REL4ADR
) {
674 if (segment
== segto
&& !(win64
)) /* Acceptable for RIP-relative */
675 nasm_panic(0, "intra-segment OUT_REL4ADR");
676 else if (segment
== NO_SEG
&& win32
)
677 nasm_error(ERR_NONFATAL
, "Win32 COFF does not correctly support"
678 " relative references to absolute addresses");
681 if (segment
!= NO_SEG
&& segment
% 2) {
682 nasm_error(ERR_NONFATAL
, "COFF format does not support"
683 " segment base references");
685 fix
= coff_add_reloc(s
, segment
,
686 win64
? IMAGE_REL_AMD64_REL32
: IMAGE_REL_I386_REL32
);
689 WRITELONG(p
, *(int64_t *)data
+ 4 - size
+ fix
);
691 WRITELONG(p
, *(int64_t *)data
- (size
+ s
->len
) + fix
);
693 coff_sect_write(s
, mydata
, 4L);
699 static void coff_sect_write(struct coff_Section
*sect
,
700 const uint8_t *data
, uint32_t len
)
702 saa_wbytes(sect
->data
, data
, len
);
706 typedef struct tagString
{
707 struct tagString
*next
;
712 #define EXPORT_SECTION_NAME ".drectve"
713 #define EXPORT_SECTION_FLAGS INFO_FLAGS
715 * #define EXPORT_SECTION_NAME ".text"
716 * #define EXPORT_SECTION_FLAGS TEXT_FLAGS
719 static STRING
*Exports
= NULL
;
720 static struct coff_Section
*directive_sec
;
721 static void AddExport(char *name
)
723 STRING
*rvp
= Exports
, *newS
;
725 newS
= (STRING
*) nasm_malloc(sizeof(STRING
));
726 newS
->len
= strlen(name
);
728 newS
->String
= (char *)nasm_malloc(newS
->len
+ 1);
729 strcpy(newS
->String
, name
);
733 for (i
= 0; i
< coff_nsects
; i
++) {
734 if (!strcmp(EXPORT_SECTION_NAME
, coff_sects
[i
]->name
))
738 if (i
== coff_nsects
)
739 i
= coff_make_section(EXPORT_SECTION_NAME
, EXPORT_SECTION_FLAGS
);
741 directive_sec
= coff_sects
[i
];
745 if (!strcmp(rvp
->String
, name
))
753 static void BuildExportTable(STRING
**rvp
)
760 list_for_each_safe(p
, t
, *rvp
) {
761 coff_sect_write(directive_sec
, (uint8_t *)"-export:", 8);
762 coff_sect_write(directive_sec
, (uint8_t *)p
->String
, p
->len
);
763 coff_sect_write(directive_sec
, (uint8_t *)" ", 1);
764 nasm_free(p
->String
);
771 static int coff_directives(enum directives directive
, char *value
, int pass
)
779 return 1; /* ignore in pass two */
781 while (*q
&& !nasm_isspace(*q
))
783 if (nasm_isspace(*q
)) {
785 while (*q
&& nasm_isspace(*q
))
790 nasm_error(ERR_NONFATAL
, "`export' directive requires export name");
794 nasm_error(ERR_NONFATAL
, "unrecognized export qualifier `%s'", q
);
805 if (!win32
) /* Only applicable for -f win32 */
809 for (i
= 0; i
< coff_nsects
; i
++)
810 if (!strcmp(".sxdata",coff_sects
[i
]->name
))
812 if (i
== coff_nsects
)
813 sxseg
= coff_make_section(".sxdata", IMAGE_SCN_LNK_INFO
);
818 * pass0 == 2 is the only time when the full set of symbols are
819 * guaranteed to be present; it is the final output pass.
823 saa_rewind(coff_syms
);
824 for (n
= 0; n
< coff_nsyms
; n
++) {
825 struct coff_Symbol
*sym
= saa_rstruct(coff_syms
);
829 * sym->strpos is biased by 4, because symbol
830 * table is prefixed with table length
832 if (sym
->strpos
>=4) {
833 char *name
= nasm_malloc(sym
->namlen
+1);
834 saa_fread(coff_strs
, sym
->strpos
-4, name
, sym
->namlen
);
835 name
[sym
->namlen
] = '\0';
836 equals
= !strcmp(value
,name
);
839 equals
= !strcmp(value
,sym
->name
);
844 * this value arithmetics effectively reflects
845 * initsym in coff_write(): 2 for file, 1 for
846 * .absolute and two per each section
848 unsigned char value
[4],*p
=value
;
849 WRITELONG(p
,n
+ 2 + 1 + coff_nsects
*2);
850 coff_sect_write(coff_sects
[sxseg
],value
,4);
855 if (n
== coff_nsyms
) {
856 nasm_error(ERR_NONFATAL
,
857 "`safeseh' directive requires valid symbol");
867 /* handle relocations storm, valid for win32/64 only */
868 static inline void coff_adjust_relocs(struct coff_Section
*s
)
870 if (s
->nrelocs
< IMAGE_SCN_MAX_RELOC
)
875 if (ofmt
== &of_coff
)
877 "Too many relocations (%d) for section `%s'",
878 s
->nrelocs
, s
->name
);
882 s
->flags
|= IMAGE_SCN_LNK_NRELOC_OVFL
;
886 static void coff_write(void)
888 int32_t pos
, sympos
, vsize
;
891 /* fill in the .drectve section with -export's */
892 BuildExportTable(&Exports
);
895 /* add default value for @feat.00, this allows to 'link /safeseh' */
898 saa_rewind(coff_syms
);
899 for (n
= 0; n
< coff_nsyms
; n
++) {
900 struct coff_Symbol
*sym
= saa_rstruct(coff_syms
);
901 if (sym
->strpos
== -1 && !strcmp("@feat.00",sym
->name
))
905 coff_deflabel("@feat.00", NO_SEG
, 1, 0, NULL
);
909 * Work out how big the file will get.
910 * Calculate the start of the `real' symbols at the same time.
911 * Check for massive relocations.
913 pos
= 0x14 + 0x28 * coff_nsects
;
914 initsym
= 3; /* two for the file, one absolute */
915 for (i
= 0; i
< coff_nsects
; i
++) {
916 if (coff_sects
[i
]->data
) {
917 coff_adjust_relocs(coff_sects
[i
]);
918 coff_sects
[i
]->pos
= pos
;
919 pos
+= coff_sects
[i
]->len
;
920 coff_sects
[i
]->relpos
= pos
;
921 pos
+= 10 * coff_sects
[i
]->nrelocs
;
923 coff_sects
[i
]->pos
= coff_sects
[i
]->relpos
= 0L;
924 initsym
+= 2; /* two for each section */
929 * Output the COFF header.
932 i
= IMAGE_FILE_MACHINE_AMD64
;
934 i
= IMAGE_FILE_MACHINE_I386
;
935 fwriteint16_t(i
, ofile
); /* machine type */
936 fwriteint16_t(coff_nsects
, ofile
); /* number of sections */
937 fwriteint32_t(time(NULL
), ofile
); /* time stamp */
938 fwriteint32_t(sympos
, ofile
);
939 fwriteint32_t(coff_nsyms
+ initsym
, ofile
);
940 fwriteint16_t(0, ofile
); /* no optional header */
941 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
942 fwriteint16_t((win32
| win64
) ? 0 : 0x104, ofile
);
945 * Output the section headers.
948 for (i
= 0; i
< coff_nsects
; i
++) {
949 coff_section_header(coff_sects
[i
]->name
, coff_sects
[i
]->namepos
, vsize
, coff_sects
[i
]->len
,
950 coff_sects
[i
]->pos
, coff_sects
[i
]->relpos
,
951 coff_sects
[i
]->nrelocs
, coff_sects
[i
]->flags
);
952 vsize
+= coff_sects
[i
]->len
;
956 * Output the sections and their relocations.
958 for (i
= 0; i
< coff_nsects
; i
++)
959 if (coff_sects
[i
]->data
) {
960 saa_fpwrite(coff_sects
[i
]->data
, ofile
);
961 coff_write_relocs(coff_sects
[i
]);
965 * Output the symbol and string tables.
967 coff_write_symbols();
968 fwriteint32_t(strslen
+ 4, ofile
); /* length includes length count */
969 saa_fpwrite(coff_strs
, ofile
);
972 static void coff_section_header(char *name
, int32_t namepos
, int32_t vsize
,
973 int32_t datalen
, int32_t datapos
,
974 int32_t relpos
, int nrelocs
, int32_t flags
)
981 strncpy(padname
, name
, 8);
982 nasm_write(padname
, 8, ofile
);
985 * If name is longer than 8 bytes, write '/' followed
986 * by offset into the strings table represented as
989 namepos
= namepos
% 100000000;
991 padname
[1] = '0' + (namepos
/ 1000000);
992 namepos
= namepos
% 1000000;
993 padname
[2] = '0' + (namepos
/ 100000);
994 namepos
= namepos
% 100000;
995 padname
[3] = '0' + (namepos
/ 10000);
996 namepos
= namepos
% 10000;
997 padname
[4] = '0' + (namepos
/ 1000);
998 namepos
= namepos
% 1000;
999 padname
[5] = '0' + (namepos
/ 100);
1000 namepos
= namepos
% 100;
1001 padname
[6] = '0' + (namepos
/ 10);
1002 namepos
= namepos
% 10;
1003 padname
[7] = '0' + (namepos
);
1004 nasm_write(padname
, 8, ofile
);
1007 fwriteint32_t(0, ofile
); /* Virtual size field - set to 0 or vsize */
1008 fwriteint32_t(0L, ofile
); /* RVA/offset - we ignore */
1009 fwriteint32_t(datalen
, ofile
);
1010 fwriteint32_t(datapos
, ofile
);
1011 fwriteint32_t(relpos
, ofile
);
1012 fwriteint32_t(0L, ofile
); /* no line numbers - we don't do 'em */
1015 * a special case -- if there are too many relocs
1016 * we have to put IMAGE_SCN_MAX_RELOC here and write
1017 * the real relocs number into VirtualAddress of first
1020 if (flags
& IMAGE_SCN_LNK_NRELOC_OVFL
)
1021 fwriteint16_t(IMAGE_SCN_MAX_RELOC
, ofile
);
1023 fwriteint16_t(nrelocs
, ofile
);
1025 fwriteint16_t(0, ofile
); /* again, no line numbers */
1026 fwriteint32_t(flags
, ofile
);
1029 static void coff_write_relocs(struct coff_Section
*s
)
1031 struct coff_Reloc
*r
;
1033 /* a real number of relocations if needed */
1034 if (s
->flags
& IMAGE_SCN_LNK_NRELOC_OVFL
) {
1035 fwriteint32_t(s
->nrelocs
, ofile
);
1036 fwriteint32_t(0, ofile
);
1037 fwriteint16_t(0, ofile
);
1040 for (r
= s
->head
; r
; r
= r
->next
) {
1041 fwriteint32_t(r
->address
, ofile
);
1042 fwriteint32_t(r
->symbol
+ (r
->symbase
== REAL_SYMBOLS
? initsym
:
1043 r
->symbase
== ABS_SYMBOL
? initsym
- 1 :
1044 r
->symbase
== SECT_SYMBOLS
? 2 : 0),
1046 fwriteint16_t(r
->type
, ofile
);
1050 static void coff_symbol(char *name
, int32_t strpos
, int32_t value
,
1051 int section
, int type
, int storageclass
, int aux
)
1056 strncpy(padname
, name
, 8);
1057 nasm_write(padname
, 8, ofile
);
1059 fwriteint32_t(0, ofile
);
1060 fwriteint32_t(strpos
, ofile
);
1063 fwriteint32_t(value
, ofile
);
1064 fwriteint16_t(section
, ofile
);
1065 fwriteint16_t(type
, ofile
);
1067 fputc(storageclass
, ofile
);
1071 static void coff_write_symbols(void)
1077 * The `.file' record, and the file name auxiliary record.
1079 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
1080 strncpy(filename
, coff_infile
, 18);
1081 nasm_write(filename
, 18, ofile
);
1084 * The section records, with their auxiliaries.
1086 memset(filename
, 0, 18); /* useful zeroed buffer */
1088 for (i
= 0; i
< (uint32_t) coff_nsects
; i
++) {
1089 coff_symbol(coff_sects
[i
]->name
, 0L, 0L, i
+ 1, 0, 3, 1);
1090 fwriteint32_t(coff_sects
[i
]->len
, ofile
);
1091 fwriteint16_t(coff_sects
[i
]->nrelocs
,ofile
);
1092 nasm_write(filename
, 12, ofile
);
1096 * The absolute symbol, for relative-to-absolute relocations.
1098 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
1103 saa_rewind(coff_syms
);
1104 for (i
= 0; i
< coff_nsyms
; i
++) {
1105 struct coff_Symbol
*sym
= saa_rstruct(coff_syms
);
1106 coff_symbol(sym
->strpos
== -1 ? sym
->name
: NULL
,
1107 sym
->strpos
, sym
->value
, sym
->section
,
1108 sym
->type
, sym
->is_global
? 2 : 3, 0);
1112 static void coff_sectalign(int32_t seg
, unsigned int value
)
1114 struct coff_Section
*s
= NULL
;
1118 for (i
= 0; i
< coff_nsects
; i
++) {
1119 if (coff_sects
[i
]->index
== seg
) {
1125 if (!s
|| !is_power2(value
))
1128 /* DOS has limitation on 64 bytes */
1129 if (!(win32
| win64
) && value
> 64)
1132 align
= (s
->flags
& IMAGE_SCN_ALIGN_MASK
);
1133 value
= coff_sectalign_flags(value
);
1135 s
->flags
= (s
->flags
& ~IMAGE_SCN_ALIGN_MASK
) | value
;
1138 static int32_t coff_segbase(int32_t segment
)
1143 static void coff_std_filename(char *inname
, char *outname
)
1145 strcpy(coff_infile
, inname
);
1146 standard_extension(inname
, outname
, ".o");
1147 strcpy(coff_outfile
, outname
);
1150 static void coff_win32_filename(char *inname
, char *outname
)
1152 strcpy(coff_infile
, inname
);
1153 standard_extension(inname
, outname
, ".obj");
1154 strcpy(coff_outfile
, outname
);
1157 extern macros_t coff_stdmac
[];
1159 static int coff_set_info(enum geninfo type
, char **val
)
1165 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
1169 const struct ofmt of_coff
= {
1170 "COFF (i386) object files (e.g. DJGPP for DOS)",
1179 nasm_do_legacy_output
,
1192 extern const struct dfmt df_cv8
;
1196 static const struct dfmt
* const win32_debug_arr
[2] = { &df_cv8
, NULL
};
1198 const struct ofmt of_win32
= {
1199 "Microsoft Win32 (i386) object files",
1208 nasm_do_legacy_output
,
1215 coff_win32_filename
,
1223 static const struct dfmt
* const win64_debug_arr
[2] = { &df_cv8
, NULL
};
1225 const struct ofmt of_win64
= {
1226 "Microsoft Win64 (x86-64) object files",
1235 nasm_do_legacy_output
,
1242 coff_win32_filename
,