1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 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 */
145 * Some common section flags by default
147 #define TEXT_FLAGS_WIN \
148 (IMAGE_SCN_CNT_CODE | \
149 IMAGE_SCN_ALIGN_16BYTES | \
150 IMAGE_SCN_MEM_EXECUTE | \
152 #define TEXT_FLAGS_DOS \
155 #define DATA_FLAGS_WIN \
156 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
157 IMAGE_SCN_ALIGN_4BYTES | \
158 IMAGE_SCN_MEM_READ | \
160 #define DATA_FLAGS_DOS \
161 (IMAGE_SCN_CNT_INITIALIZED_DATA)
163 #define BSS_FLAGS_WIN \
164 (IMAGE_SCN_CNT_UNINITIALIZED_DATA | \
165 IMAGE_SCN_ALIGN_4BYTES | \
166 IMAGE_SCN_MEM_READ | \
168 #define BSS_FLAGS_DOS \
169 (IMAGE_SCN_CNT_UNINITIALIZED_DATA)
171 #define RDATA_FLAGS_WIN \
172 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
173 IMAGE_SCN_ALIGN_8BYTES | \
176 #define RDATA_FLAGS_DOS \
177 (IMAGE_SCN_CNT_INITIALIZED_DATA)
179 #define PDATA_FLAGS \
180 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
181 IMAGE_SCN_ALIGN_4BYTES | \
184 #define XDATA_FLAGS \
185 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
186 IMAGE_SCN_ALIGN_8BYTES | \
190 (IMAGE_SCN_ALIGN_1BYTES | \
191 IMAGE_SCN_LNK_INFO | \
192 IMAGE_SCN_LNK_REMOVE)
194 #define TEXT_FLAGS ((win32 | win64) ? TEXT_FLAGS_WIN : TEXT_FLAGS_DOS)
195 #define DATA_FLAGS ((win32 | win64) ? DATA_FLAGS_WIN : DATA_FLAGS_DOS)
196 #define BSS_FLAGS ((win32 | win64) ? BSS_FLAGS_WIN : BSS_FLAGS_DOS)
197 #define RDATA_FLAGS ((win32 | win64) ? RDATA_FLAGS_WIN : RDATA_FLAGS_DOS)
199 #define SECT_DELTA 32
200 static struct Section
**sects
;
201 static int nsects
, sectlen
;
203 static struct SAA
*syms
;
204 static uint32_t nsyms
;
206 static int32_t def_seg
;
210 static struct RAA
*bsym
, *symval
;
212 static struct SAA
*strs
;
213 static uint32_t strslen
;
215 static void coff_gen_init(void);
216 static void coff_sect_write(struct Section
*, const uint8_t *, uint32_t);
217 static void coff_write(void);
218 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
219 static void coff_write_relocs(struct Section
*);
220 static void coff_write_symbols(void);
222 static void coff_win32_init(void)
229 static void coff_win64_init(void)
235 imagebase_sect
= seg_alloc()+1;
236 define_label(WRT_IMAGEBASE
, imagebase_sect
, 0, NULL
, false, false);
239 static void coff_std_init(void)
241 win32
= win64
= false;
245 static void coff_gen_init(void)
249 nsects
= sectlen
= 0;
250 syms
= saa_init(sizeof(struct Symbol
));
256 def_seg
= seg_alloc();
259 static void coff_cleanup(int debuginfo
)
267 for (i
= 0; i
< nsects
; i
++) {
269 saa_free(sects
[i
]->data
);
270 while (sects
[i
]->head
) {
272 sects
[i
]->head
= sects
[i
]->head
->next
;
284 static int coff_make_section(char *name
, uint32_t flags
)
288 s
= nasm_zalloc(sizeof(*s
));
290 if (flags
!= BSS_FLAGS
)
291 s
->data
= saa_init(1);
293 if (!strcmp(name
, ".text"))
296 s
->index
= seg_alloc();
297 strncpy(s
->name
, name
, 8);
301 if (nsects
>= sectlen
) {
302 sectlen
+= SECT_DELTA
;
303 sects
= nasm_realloc(sects
, sectlen
* sizeof(*sects
));
310 static inline int32_t coff_sectalign_flags(unsigned int align
)
312 return (ilog2_32(align
) + 1) << 20;
315 static int32_t coff_section_names(char *name
, int pass
, int *bits
)
318 uint32_t flags
, align_and
= ~0L, align_or
= 0L;
335 while (*p
&& !nasm_isspace(*p
))
339 if (strlen(name
) > 8) {
340 nasm_error(ERR_WARNING
, "COFF section names limited to 8 characters:"
346 while (*p
&& nasm_isspace(*p
))
350 while (*p
&& !nasm_isspace(*p
))
354 while (*p
&& nasm_isspace(*p
))
357 if (!nasm_stricmp(q
, "code") || !nasm_stricmp(q
, "text")) {
359 } else if (!nasm_stricmp(q
, "data")) {
361 } else if (!nasm_stricmp(q
, "rdata")) {
365 flags
= DATA_FLAGS
; /* gotta do something */
366 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
367 " read-only data sections");
369 } else if (!nasm_stricmp(q
, "bss")) {
371 } else if (!nasm_stricmp(q
, "info")) {
375 flags
= DATA_FLAGS
; /* gotta do something */
376 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
377 " informational sections");
379 } else if (!nasm_strnicmp(q
, "align=", 6)) {
380 if (!(win32
| win64
))
381 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
382 " section alignment specification");
384 if (q
[6 + strspn(q
+ 6, "0123456789")])
385 nasm_error(ERR_NONFATAL
,
386 "argument to `align' is not numeric");
388 unsigned int align
= atoi(q
+ 6);
389 if (!align
|| ((align
- 1) & align
))
390 nasm_error(ERR_NONFATAL
, "argument to `align' is not a"
393 nasm_error(ERR_NONFATAL
, "Win32 cannot align sections"
394 " to better than 64-byte boundaries");
396 align_and
= ~0x00F00000L
;
397 align_or
= coff_sectalign_flags(align
);
404 for (i
= 0; i
< nsects
; i
++)
405 if (!strcmp(name
, sects
[i
]->name
))
409 if (!strcmp(name
, ".data"))
411 else if (!strcmp(name
, ".rdata"))
413 else if (!strcmp(name
, ".bss"))
415 else if (win64
&& !strcmp(name
, ".pdata"))
417 else if (win64
&& !strcmp(name
, ".xdata"))
422 i
= coff_make_section(name
, flags
);
424 sects
[i
]->flags
= flags
;
425 sects
[i
]->flags
&= align_and
;
426 sects
[i
]->flags
|= align_or
;
427 } else if (pass
== 1) {
429 nasm_error(ERR_WARNING
, "section attributes ignored on"
430 " redeclaration of section `%s'", name
);
433 return sects
[i
]->index
;
436 static void coff_deflabel(char *name
, int32_t segment
, int64_t offset
,
437 int is_global
, char *special
)
439 int pos
= strslen
+ 4;
443 nasm_error(ERR_NONFATAL
, "COFF format does not support any"
444 " special symbol types");
446 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
447 if (strcmp(name
,WRT_IMAGEBASE
))
448 nasm_error(ERR_NONFATAL
, "unrecognized special symbol `%s'", name
);
452 if (strlen(name
) > 8) {
453 size_t nlen
= strlen(name
)+1;
454 saa_wbytes(strs
, name
, nlen
);
459 sym
= saa_wstruct(syms
);
462 sym
->namlen
= strlen(name
);
464 strcpy(sym
->name
, name
);
465 sym
->is_global
= !!is_global
;
466 sym
->type
= 0; /* Default to T_NULL (no type) */
467 if (segment
== NO_SEG
)
468 sym
->section
= -1; /* absolute symbol */
472 for (i
= 0; i
< nsects
; i
++)
473 if (segment
== sects
[i
]->index
) {
474 sym
->section
= i
+ 1;
478 sym
->is_global
= true;
483 sym
->value
= (sym
->section
== 0 ? 0 : offset
);
486 * define the references from external-symbol segment numbers
487 * to these symbol records.
489 if (sym
->section
== 0)
490 bsym
= raa_write(bsym
, segment
, nsyms
);
492 if (segment
!= NO_SEG
)
493 symval
= raa_write(symval
, segment
, sym
->section
? 0 : sym
->value
);
498 static int32_t coff_add_reloc(struct Section
*sect
, int32_t segment
,
503 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
504 sect
->tail
= &r
->next
;
507 r
->address
= sect
->len
;
508 if (segment
== NO_SEG
) {
509 r
->symbol
= 0, r
->symbase
= ABS_SYMBOL
;
512 r
->symbase
= REAL_SYMBOLS
;
513 for (i
= 0; i
< nsects
; i
++) {
514 if (segment
== sects
[i
]->index
) {
516 r
->symbase
= SECT_SYMBOLS
;
520 if (r
->symbase
== REAL_SYMBOLS
)
521 r
->symbol
= raa_read(bsym
, segment
);
528 * Return the fixup for standard COFF common variables.
530 if (r
->symbase
== REAL_SYMBOLS
&& !(win32
| win64
))
531 return raa_read(symval
, segment
);
536 static void coff_out(int32_t segto
, const void *data
,
537 enum out_type type
, uint64_t size
,
538 int32_t segment
, int32_t wrt
)
541 uint8_t mydata
[8], *p
;
544 if (wrt
!= NO_SEG
&& !win64
) {
545 wrt
= NO_SEG
; /* continue to do _something_ */
546 nasm_error(ERR_NONFATAL
, "WRT not supported by COFF output formats");
550 * handle absolute-assembly (structure definitions)
552 if (segto
== NO_SEG
) {
553 if (type
!= OUT_RESERVE
)
554 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
560 for (i
= 0; i
< nsects
; i
++) {
561 if (segto
== sects
[i
]->index
) {
567 int tempint
; /* ignored */
568 if (segto
!= coff_section_names(".text", 2, &tempint
))
569 nasm_error(ERR_PANIC
, "strange segment conditions in COFF driver");
571 s
= sects
[nsects
- 1];
574 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
575 if (win64
&& wrt
== NO_SEG
) {
576 if (!strcmp(s
->name
,".pdata") || !strcmp(s
->name
,".xdata"))
577 wrt
= imagebase_sect
;
580 if (!s
->data
&& type
!= OUT_RESERVE
) {
581 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
582 " BSS section `%s': ignored", s
->name
);
583 s
->len
+= realsize(type
, size
);
587 if (type
== OUT_RESERVE
) {
589 nasm_error(ERR_WARNING
, "uninitialised space declared in"
590 " non-BSS section `%s': zeroing", s
->name
);
591 coff_sect_write(s
, NULL
, size
);
594 } else if (type
== OUT_RAWDATA
) {
595 if (segment
!= NO_SEG
)
596 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
597 coff_sect_write(s
, data
, size
);
598 } else if (type
== OUT_ADDRESS
) {
600 if (size
!= 4 && (segment
!= NO_SEG
|| wrt
!= NO_SEG
)) {
601 nasm_error(ERR_NONFATAL
, "COFF format does not support non-32-bit"
605 if (segment
!= NO_SEG
|| wrt
!= NO_SEG
) {
607 nasm_error(ERR_NONFATAL
, "COFF format does not support"
609 } else if (segment
% 2) {
610 nasm_error(ERR_NONFATAL
, "COFF format does not support"
611 " segment base references");
613 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_I386_DIR32
);
616 WRITELONG(p
, *(int64_t *)data
+ fix
);
617 coff_sect_write(s
, mydata
, size
);
623 if (wrt
== imagebase_sect
) {
624 nasm_error(ERR_NONFATAL
, "operand size mismatch: 'wrt "
625 WRT_IMAGEBASE
"' is a 32-bit operand");
627 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_AMD64_ADDR64
);
628 WRITEDLONG(p
, *(int64_t *)data
+ fix
);
629 coff_sect_write(s
, mydata
, size
);
631 fix
= coff_add_reloc(s
, segment
,
632 wrt
== imagebase_sect
? IMAGE_REL_AMD64_ADDR32NB
:
633 IMAGE_REL_AMD64_ADDR32
);
634 WRITELONG(p
, *(int64_t *)data
+ fix
);
635 coff_sect_write(s
, mydata
, size
);
638 } else if (type
== OUT_REL2ADR
) {
639 nasm_error(ERR_NONFATAL
, "COFF format does not support 16-bit"
641 } else if (type
== OUT_REL4ADR
) {
642 if (segment
== segto
&& !(win64
)) /* Acceptable for RIP-relative */
643 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
644 else if (segment
== NO_SEG
&& win32
)
645 nasm_error(ERR_NONFATAL
, "Win32 COFF does not correctly support"
646 " relative references to absolute addresses");
649 if (segment
!= NO_SEG
&& segment
% 2) {
650 nasm_error(ERR_NONFATAL
, "COFF format does not support"
651 " segment base references");
653 fix
= coff_add_reloc(s
, segment
,
654 win64
? IMAGE_REL_AMD64_REL32
: IMAGE_REL_I386_REL32
);
657 WRITELONG(p
, *(int64_t *)data
+ 4 - size
+ fix
);
659 WRITELONG(p
, *(int64_t *)data
- (size
+ s
->len
) + fix
);
661 coff_sect_write(s
, mydata
, 4L);
667 static void coff_sect_write(struct Section
*sect
,
668 const uint8_t *data
, uint32_t len
)
670 saa_wbytes(sect
->data
, data
, len
);
674 typedef struct tagString
{
675 struct tagString
*next
;
680 #define EXPORT_SECTION_NAME ".drectve"
681 #define EXPORT_SECTION_FLAGS INFO_FLAGS
683 * #define EXPORT_SECTION_NAME ".text"
684 * #define EXPORT_SECTION_FLAGS TEXT_FLAGS
687 static STRING
*Exports
= NULL
;
688 static struct Section
*directive_sec
;
689 void AddExport(char *name
)
691 STRING
*rvp
= Exports
, *newS
;
693 newS
= (STRING
*) nasm_malloc(sizeof(STRING
));
694 newS
->len
= strlen(name
);
696 newS
->String
= (char *)nasm_malloc(newS
->len
+ 1);
697 strcpy(newS
->String
, name
);
701 for (i
= 0; i
< nsects
; i
++) {
702 if (!strcmp(EXPORT_SECTION_NAME
, sects
[i
]->name
))
707 i
= coff_make_section(EXPORT_SECTION_NAME
, EXPORT_SECTION_FLAGS
);
709 directive_sec
= sects
[i
];
713 if (!strcmp(rvp
->String
, name
))
721 static void BuildExportTable(STRING
**rvp
)
728 list_for_each_safe(p
, t
, *rvp
) {
729 coff_sect_write(directive_sec
, (uint8_t *)"-export:", 8);
730 coff_sect_write(directive_sec
, (uint8_t *)p
->String
, p
->len
);
731 coff_sect_write(directive_sec
, (uint8_t *)" ", 1);
732 nasm_free(p
->String
);
739 static int coff_directives(enum directives directive
, char *value
, int pass
)
747 return 1; /* ignore in pass two */
749 while (*q
&& !nasm_isspace(*q
))
751 if (nasm_isspace(*q
)) {
753 while (*q
&& nasm_isspace(*q
))
758 nasm_error(ERR_NONFATAL
, "`export' directive requires export name");
762 nasm_error(ERR_NONFATAL
, "unrecognized export qualifier `%s'", q
);
773 if (!win32
) /* Only applicable for -f win32 */
777 for (i
= 0; i
< nsects
; i
++)
778 if (!strcmp(".sxdata",sects
[i
]->name
))
781 sxseg
= coff_make_section(".sxdata", IMAGE_SCN_LNK_INFO
);
786 * pass0 == 2 is the only time when the full set of symbols are
787 * guaranteed to be present; it is the final output pass.
792 for (n
= 0; n
< nsyms
; n
++) {
793 struct Symbol
*sym
= saa_rstruct(syms
);
797 * sym->strpos is biased by 4, because symbol
798 * table is prefixed with table length
800 if (sym
->strpos
>=4) {
801 char *name
= nasm_malloc(sym
->namlen
+1);
802 saa_fread(strs
, sym
->strpos
-4, name
, sym
->namlen
);
803 name
[sym
->namlen
] = '\0';
804 equals
= !strcmp(value
,name
);
807 equals
= !strcmp(value
,sym
->name
);
812 * this value arithmetics effectively reflects
813 * initsym in coff_write(): 2 for file, 1 for
814 * .absolute and two per each section
816 unsigned char value
[4],*p
=value
;
817 WRITELONG(p
,n
+ 2 + 1 + nsects
*2);
818 coff_sect_write(sects
[sxseg
],value
,4);
824 nasm_error(ERR_NONFATAL
,
825 "`safeseh' directive requires valid symbol");
835 /* handle relocations storm, valid for win32/64 only */
836 static inline void coff_adjust_relocs(struct Section
*s
)
838 if (s
->nrelocs
< IMAGE_SCN_MAX_RELOC
)
843 if (ofmt
== &of_coff
)
844 nasm_error(ERR_FATAL
,
845 "Too many relocations (%d) for section `%s'",
846 s
->nrelocs
, s
->name
);
850 s
->flags
|= IMAGE_SCN_LNK_NRELOC_OVFL
;
854 static void coff_write(void)
856 int32_t pos
, sympos
, vsize
;
859 /* fill in the .drectve section with -export's */
860 BuildExportTable(&Exports
);
863 /* add default value for @feat.00, this allows to 'link /safeseh' */
867 for (n
= 0; n
< nsyms
; n
++) {
868 struct Symbol
*sym
= saa_rstruct(syms
);
869 if (sym
->strpos
== -1 && !strcmp("@feat.00",sym
->name
))
873 coff_deflabel("@feat.00", NO_SEG
, 1, 0, NULL
);
877 * Work out how big the file will get.
878 * Calculate the start of the `real' symbols at the same time.
879 * Check for massive relocations.
881 pos
= 0x14 + 0x28 * nsects
;
882 initsym
= 3; /* two for the file, one absolute */
883 for (i
= 0; i
< nsects
; i
++) {
884 if (sects
[i
]->data
) {
885 coff_adjust_relocs(sects
[i
]);
887 pos
+= sects
[i
]->len
;
888 sects
[i
]->relpos
= pos
;
889 pos
+= 10 * sects
[i
]->nrelocs
;
891 sects
[i
]->pos
= sects
[i
]->relpos
= 0L;
892 initsym
+= 2; /* two for each section */
897 * Output the COFF header.
900 i
= IMAGE_FILE_MACHINE_AMD64
;
902 i
= IMAGE_FILE_MACHINE_I386
;
903 fwriteint16_t(i
, ofile
); /* machine type */
904 fwriteint16_t(nsects
, ofile
); /* number of sections */
905 fwriteint32_t(time(NULL
), ofile
); /* time stamp */
906 fwriteint32_t(sympos
, ofile
);
907 fwriteint32_t(nsyms
+ initsym
, ofile
);
908 fwriteint16_t(0, ofile
); /* no optional header */
909 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
910 fwriteint16_t((win32
| win64
) ? 0 : 0x104, ofile
);
913 * Output the section headers.
916 for (i
= 0; i
< nsects
; i
++) {
917 coff_section_header(sects
[i
]->name
, vsize
, sects
[i
]->len
,
918 sects
[i
]->pos
, sects
[i
]->relpos
,
919 sects
[i
]->nrelocs
, sects
[i
]->flags
);
920 vsize
+= sects
[i
]->len
;
924 * Output the sections and their relocations.
926 for (i
= 0; i
< nsects
; i
++)
927 if (sects
[i
]->data
) {
928 saa_fpwrite(sects
[i
]->data
, ofile
);
929 coff_write_relocs(sects
[i
]);
933 * Output the symbol and string tables.
935 coff_write_symbols();
936 fwriteint32_t(strslen
+ 4, ofile
); /* length includes length count */
937 saa_fpwrite(strs
, ofile
);
940 static void coff_section_header(char *name
, int32_t vsize
,
941 int32_t datalen
, int32_t datapos
,
942 int32_t relpos
, int nrelocs
, int32_t flags
)
948 strncpy(padname
, name
, 8);
949 fwrite(padname
, 8, 1, ofile
);
951 fwriteint32_t(0, ofile
); /* Virtual size field - set to 0 or vsize */
952 fwriteint32_t(0L, ofile
); /* RVA/offset - we ignore */
953 fwriteint32_t(datalen
, ofile
);
954 fwriteint32_t(datapos
, ofile
);
955 fwriteint32_t(relpos
, ofile
);
956 fwriteint32_t(0L, ofile
); /* no line numbers - we don't do 'em */
959 * a special case -- if there are too many relocs
960 * we have to put IMAGE_SCN_MAX_RELOC here and write
961 * the real relocs number into VirtualAddress of first
964 if (flags
& IMAGE_SCN_LNK_NRELOC_OVFL
)
965 fwriteint16_t(IMAGE_SCN_MAX_RELOC
, ofile
);
967 fwriteint16_t(nrelocs
, ofile
);
969 fwriteint16_t(0, ofile
); /* again, no line numbers */
970 fwriteint32_t(flags
, ofile
);
973 static void coff_write_relocs(struct Section
*s
)
977 /* a real number of relocations if needed */
978 if (s
->flags
& IMAGE_SCN_LNK_NRELOC_OVFL
) {
979 fwriteint32_t(s
->nrelocs
, ofile
);
980 fwriteint32_t(0, ofile
);
981 fwriteint16_t(0, ofile
);
984 for (r
= s
->head
; r
; r
= r
->next
) {
985 fwriteint32_t(r
->address
, ofile
);
986 fwriteint32_t(r
->symbol
+ (r
->symbase
== REAL_SYMBOLS
? initsym
:
987 r
->symbase
== ABS_SYMBOL
? initsym
- 1 :
988 r
->symbase
== SECT_SYMBOLS
? 2 : 0),
990 fwriteint16_t(r
->type
, ofile
);
994 static void coff_symbol(char *name
, int32_t strpos
, int32_t value
,
995 int section
, int type
, int storageclass
, int aux
)
1000 strncpy(padname
, name
, 8);
1001 fwrite(padname
, 8, 1, ofile
);
1003 fwriteint32_t(0, ofile
);
1004 fwriteint32_t(strpos
, ofile
);
1007 fwriteint32_t(value
, ofile
);
1008 fwriteint16_t(section
, ofile
);
1009 fwriteint16_t(type
, ofile
);
1011 fputc(storageclass
, ofile
);
1015 static void coff_write_symbols(void)
1021 * The `.file' record, and the file name auxiliary record.
1023 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
1024 strncpy(filename
, coff_infile
, 18);
1025 fwrite(filename
, 18, 1, ofile
);
1028 * The section records, with their auxiliaries.
1030 memset(filename
, 0, 18); /* useful zeroed buffer */
1032 for (i
= 0; i
< (uint32_t) nsects
; i
++) {
1033 coff_symbol(sects
[i
]->name
, 0L, 0L, i
+ 1, 0, 3, 1);
1034 fwriteint32_t(sects
[i
]->len
, ofile
);
1035 fwriteint16_t(sects
[i
]->nrelocs
,ofile
);
1036 fwrite(filename
, 12, 1, ofile
);
1040 * The absolute symbol, for relative-to-absolute relocations.
1042 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
1048 for (i
= 0; i
< nsyms
; i
++) {
1049 struct Symbol
*sym
= saa_rstruct(syms
);
1050 coff_symbol(sym
->strpos
== -1 ? sym
->name
: NULL
,
1051 sym
->strpos
, sym
->value
, sym
->section
,
1052 sym
->type
, sym
->is_global
? 2 : 3, 0);
1056 static void coff_sectalign(int32_t seg
, unsigned int value
)
1058 struct Section
*s
= NULL
;
1062 for (i
= 0; i
< nsects
; i
++) {
1063 if (sects
[i
]->index
== seg
) {
1069 if (!s
|| !is_power2(value
))
1072 /* DOS has limitation on 64 bytes */
1073 if (!(win32
| win64
) && value
> 64)
1076 align
= (s
->flags
& IMAGE_SCN_ALIGN_MASK
);
1077 value
= coff_sectalign_flags(value
);
1079 s
->flags
= (s
->flags
& ~IMAGE_SCN_ALIGN_MASK
) | value
;
1082 static int32_t coff_segbase(int32_t segment
)
1087 static void coff_std_filename(char *inname
, char *outname
)
1089 strcpy(coff_infile
, inname
);
1090 standard_extension(inname
, outname
, ".o");
1093 static void coff_win32_filename(char *inname
, char *outname
)
1095 strcpy(coff_infile
, inname
);
1096 standard_extension(inname
, outname
, ".obj");
1099 extern macros_t coff_stdmac
[];
1101 static int coff_set_info(enum geninfo type
, char **val
)
1107 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
1111 struct ofmt of_coff
= {
1112 "COFF (i386) object files (e.g. DJGPP for DOS)",
1134 struct ofmt of_win32
= {
1135 "Microsoft Win32 (i386) object files",
1149 coff_win32_filename
,
1157 struct ofmt of_win64
= {
1158 "Microsoft Win64 (x86-64) object files",
1172 coff_win32_filename
,