1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 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)
52 #include "output/outform.h"
53 #include "output/outlib.h"
55 #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
60 * (0) When I say `standard COFF' below, I mean `COFF as output and
61 * used by DJGPP'. I assume DJGPP gets it right.
63 * (1) Win32 appears to interpret the term `relative relocation'
64 * differently from standard COFF. Standard COFF understands a
65 * relative relocation to mean that during relocation you add the
66 * address of the symbol you're referencing, and subtract the base
67 * address of the section you're in. Win32 COFF, by contrast, seems
68 * to add the address of the symbol and then subtract the address
69 * of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
70 * subtly incompatible.
72 * (2) Win32 doesn't bother putting any flags in the header flags
73 * field (at offset 0x12 into the file).
75 * (3) Win32 uses some extra flags into the section header table:
76 * it defines flags 0x80000000 (writable), 0x40000000 (readable)
77 * and 0x20000000 (executable), and uses them in the expected
78 * combinations. It also defines 0x00100000 through 0x00700000 for
79 * section alignments of 1 through 64 bytes.
81 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
82 * field directly after the section name in the section header
83 * table for something strange: they store what the address of the
84 * section start point _would_ be, if you laid all the sections end
85 * to end starting at zero. Dunno why. Microsoft's documentation
86 * lists this field as "Virtual Size of Section", which doesn't
87 * seem to fit at all. In fact, Win32 even includes non-linked
88 * sections such as .drectve in this calculation.
90 * Newer versions of MASM seem to have changed this to be zero, and
91 * that apparently matches the COFF spec, so go with that.
93 * (5) Standard COFF does something very strange to common
94 * variables: the relocation point for a common variable is as far
95 * _before_ the variable as its size stretches out _after_ it. So
96 * we must fix up common variable references. Win32 seems to be
97 * sensible on this one.
100 /* Flag which version of COFF we are currently outputting. */
101 static bool win32
, win64
;
103 static int32_t imagebase_sect
;
104 #define WRT_IMAGEBASE "..imagebase"
108 int32_t address
; /* relative to _start_ of section */
109 int32_t symbol
; /* symbol number */
114 } symbase
; /* relocation for symbol number :) */
118 /* possible values for Reloc->type */
119 #define IMAGE_REL_AMD64_ADDR64 0x0001
120 #define IMAGE_REL_AMD64_ADDR32 0x0002
121 #define IMAGE_REL_AMD64_ADDR32NB 0x0003
122 #define IMAGE_REL_AMD64_REL32 0x0004
123 #define IMAGE_REL_I386_DIR32 0x0006
124 #define IMAGE_REL_I386_DIR32NB 0x0007
125 #define IMAGE_REL_I386_REL32 0x0014
129 int32_t strpos
; /* string table position of name */
130 int32_t value
; /* address, or COMMON variable size */
131 int section
; /* section number where it's defined
132 * - in COFF codes, not NASM codes */
133 bool is_global
; /* is it a global symbol or not? */
134 int16_t type
; /* 0 - notype, 0x20 - function */
135 int32_t namlen
; /* full name length */
140 static char coff_infile
[FILENAME_MAX
];
147 struct Reloc
*head
, **tail
;
148 uint32_t flags
; /* section flags */
153 #define TEXT_FLAGS ((win32 | win64) ? 0x60500020L : 0x20L)
154 #define DATA_FLAGS ((win32 | win64) ? 0xC0300040L : 0x40L)
155 #define BSS_FLAGS ((win32 | win64) ? 0xC0300080L : 0x80L)
156 #define INFO_FLAGS 0x00100A00L
157 #define RDATA_FLAGS ((win32 | win64) ? 0x40400040L : 0x40L)
159 #define SECT_DELTA 32
160 static struct Section
**sects
;
161 static int nsects
, sectlen
;
163 static struct SAA
*syms
;
164 static uint32_t nsyms
;
166 static int32_t def_seg
;
170 static struct RAA
*bsym
, *symval
;
172 static struct SAA
*strs
;
173 static uint32_t strslen
;
175 static void coff_gen_init(FILE *, efunc
);
176 static void coff_sect_write(struct Section
*, const uint8_t *,
178 static void coff_write(void);
179 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
180 static void coff_write_relocs(struct Section
*);
181 static void coff_write_symbols(void);
183 static void coff_win32_init(FILE * fp
, efunc errfunc
,
184 ldfunc ldef
, evalfunc eval
)
186 win32
= true; win64
= false;
187 (void)ldef
; /* placate optimizers */
189 coff_gen_init(fp
, errfunc
);
192 static void coff_win64_init(FILE * fp
, efunc errfunc
,
193 ldfunc ldef
, evalfunc eval
)
195 extern struct ofmt of_win64
;
198 win32
= false; win64
= true;
199 (void)ldef
; /* placate optimizers */
201 coff_gen_init(fp
, errfunc
);
202 imagebase_sect
= seg_alloc()+1;
203 ldef(WRT_IMAGEBASE
,imagebase_sect
,0,NULL
,false,false,&of_win64
,errfunc
);
206 static void coff_std_init(FILE * fp
, efunc errfunc
, ldfunc ldef
,
209 win32
= win64
= false;
210 (void)ldef
; /* placate optimizers */
212 coff_gen_init(fp
, errfunc
);
215 static void coff_gen_init(FILE * fp
, efunc errfunc
)
221 nsects
= sectlen
= 0;
222 syms
= saa_init(sizeof(struct Symbol
));
228 def_seg
= seg_alloc();
231 static void coff_cleanup(int debuginfo
)
240 for (i
= 0; i
< nsects
; i
++) {
242 saa_free(sects
[i
]->data
);
243 while (sects
[i
]->head
) {
245 sects
[i
]->head
= sects
[i
]->head
->next
;
257 static int coff_make_section(char *name
, uint32_t flags
)
261 s
= nasm_malloc(sizeof(*s
));
263 if (flags
!= BSS_FLAGS
)
264 s
->data
= saa_init(1);
271 if (!strcmp(name
, ".text"))
274 s
->index
= seg_alloc();
275 strncpy(s
->name
, name
, 8);
279 if (nsects
>= sectlen
) {
280 sectlen
+= SECT_DELTA
;
281 sects
= nasm_realloc(sects
, sectlen
* sizeof(*sects
));
288 static int32_t coff_section_names(char *name
, int pass
, int *bits
)
291 uint32_t flags
, align_and
= ~0L, align_or
= 0L;
308 while (*p
&& !nasm_isspace(*p
))
312 if (strlen(name
) > 8) {
313 error(ERR_WARNING
, "COFF section names limited to 8 characters:"
319 while (*p
&& nasm_isspace(*p
))
323 while (*p
&& !nasm_isspace(*p
))
327 while (*p
&& nasm_isspace(*p
))
330 if (!nasm_stricmp(q
, "code") || !nasm_stricmp(q
, "text")) {
332 } else if (!nasm_stricmp(q
, "data")) {
334 } else if (!nasm_stricmp(q
, "rdata")) {
338 flags
= DATA_FLAGS
; /* gotta do something */
339 error(ERR_NONFATAL
, "standard COFF does not support"
340 " read-only data sections");
342 } else if (!nasm_stricmp(q
, "bss")) {
344 } else if (!nasm_stricmp(q
, "info")) {
348 flags
= DATA_FLAGS
; /* gotta do something */
349 error(ERR_NONFATAL
, "standard COFF does not support"
350 " informational sections");
352 } else if (!nasm_strnicmp(q
, "align=", 6)) {
353 if (!(win32
| win64
))
354 error(ERR_NONFATAL
, "standard COFF does not support"
355 " section alignment specification");
357 if (q
[6 + strspn(q
+ 6, "0123456789")])
359 "argument to `align' is not numeric");
361 unsigned int align
= atoi(q
+ 6);
362 if (!align
|| ((align
- 1) & align
))
363 error(ERR_NONFATAL
, "argument to `align' is not a"
366 error(ERR_NONFATAL
, "Win32 cannot align sections"
367 " to better than 64-byte boundaries");
369 align_and
= ~0x00F00000L
;
370 align_or
= (align
== 1 ? 0x00100000L
:
371 align
== 2 ? 0x00200000L
:
372 align
== 4 ? 0x00300000L
:
373 align
== 8 ? 0x00400000L
:
374 align
== 16 ? 0x00500000L
:
376 32 ? 0x00600000L
: 0x00700000L
);
383 for (i
= 0; i
< nsects
; i
++)
384 if (!strcmp(name
, sects
[i
]->name
))
388 if (!strcmp(name
, ".data"))
390 else if (!strcmp(name
, ".rdata"))
392 else if (!strcmp(name
, ".bss"))
394 else if (win64
&& !strcmp(name
, ".pdata"))
395 flags
= 0x40300040; /* rdata align=4 */
396 else if (win64
&& !strcmp(name
, ".xdata"))
397 flags
= 0x40400040; /* rdate align=8 */
401 i
= coff_make_section(name
, flags
);
403 sects
[i
]->flags
= flags
;
404 sects
[i
]->flags
&= align_and
;
405 sects
[i
]->flags
|= align_or
;
406 } else if (pass
== 1) {
408 error(ERR_WARNING
, "section attributes ignored on"
409 " redeclaration of section `%s'", name
);
412 return sects
[i
]->index
;
415 static void coff_deflabel(char *name
, int32_t segment
, int64_t offset
,
416 int is_global
, char *special
)
418 int pos
= strslen
+ 4;
422 error(ERR_NONFATAL
, "binary format does not support any"
423 " special symbol types");
425 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
426 if (strcmp(name
,WRT_IMAGEBASE
))
427 error(ERR_NONFATAL
, "unrecognized special symbol `%s'", name
);
431 if (strlen(name
) > 8) {
432 size_t nlen
= strlen(name
)+1;
433 saa_wbytes(strs
, name
, nlen
);
438 sym
= saa_wstruct(syms
);
441 sym
->namlen
= strlen(name
);
443 strcpy(sym
->name
, name
);
444 sym
->is_global
= !!is_global
;
445 sym
->type
= 0; /* Default to T_NULL (no type) */
446 if (segment
== NO_SEG
)
447 sym
->section
= -1; /* absolute symbol */
451 for (i
= 0; i
< nsects
; i
++)
452 if (segment
== sects
[i
]->index
) {
453 sym
->section
= i
+ 1;
457 sym
->is_global
= true;
462 sym
->value
= (sym
->section
== 0 ? 0 : offset
);
465 * define the references from external-symbol segment numbers
466 * to these symbol records.
468 if (sym
->section
== 0) {
469 bsym
= raa_write(bsym
, segment
, nsyms
);
472 if (segment
!= NO_SEG
)
473 symval
= raa_write(symval
, segment
, sym
->section
? 0 : sym
->value
);
478 static int32_t coff_add_reloc(struct Section
*sect
, int32_t segment
,
483 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
484 sect
->tail
= &r
->next
;
487 r
->address
= sect
->len
;
488 if (segment
== NO_SEG
)
489 r
->symbol
= 0, r
->symbase
= ABS_SYMBOL
;
492 r
->symbase
= REAL_SYMBOLS
;
493 for (i
= 0; i
< nsects
; i
++)
494 if (segment
== sects
[i
]->index
) {
496 r
->symbase
= SECT_SYMBOLS
;
499 if (r
->symbase
== REAL_SYMBOLS
)
500 r
->symbol
= raa_read(bsym
, segment
);
507 * Return the fixup for standard COFF common variables.
509 if (r
->symbase
== REAL_SYMBOLS
&& !(win32
| win64
))
510 return raa_read(symval
, segment
);
515 static void coff_out(int32_t segto
, const void *data
,
516 enum out_type type
, uint64_t size
,
517 int32_t segment
, int32_t wrt
)
520 uint8_t mydata
[8], *p
;
523 if (wrt
!= NO_SEG
&& !win64
) {
524 wrt
= NO_SEG
; /* continue to do _something_ */
525 error(ERR_NONFATAL
, "WRT not supported by COFF output formats");
529 * handle absolute-assembly (structure definitions)
531 if (segto
== NO_SEG
) {
532 if (type
!= OUT_RESERVE
)
533 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
539 for (i
= 0; i
< nsects
; i
++)
540 if (segto
== sects
[i
]->index
) {
545 int tempint
; /* ignored */
546 if (segto
!= coff_section_names(".text", 2, &tempint
))
547 error(ERR_PANIC
, "strange segment conditions in COFF driver");
549 s
= sects
[nsects
- 1];
552 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
553 if (win64
&& wrt
== NO_SEG
&&
554 (!strcmp(s
->name
,".pdata") || !strcmp(s
->name
,".xdata")))
555 wrt
= imagebase_sect
;
557 if (!s
->data
&& type
!= OUT_RESERVE
) {
558 error(ERR_WARNING
, "attempt to initialize memory in"
559 " BSS section `%s': ignored", s
->name
);
560 s
->len
+= realsize(type
, size
);
564 if (type
== OUT_RESERVE
) {
566 error(ERR_WARNING
, "uninitialised space declared in"
567 " non-BSS section `%s': zeroing", s
->name
);
568 coff_sect_write(s
, NULL
, size
);
571 } else if (type
== OUT_RAWDATA
) {
572 if (segment
!= NO_SEG
)
573 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
574 coff_sect_write(s
, data
, size
);
575 } else if (type
== OUT_ADDRESS
) {
577 if (size
!= 4 && (segment
!= NO_SEG
|| wrt
!= NO_SEG
))
578 error(ERR_NONFATAL
, "COFF format does not support non-32-bit"
582 if (segment
!= NO_SEG
|| wrt
!= NO_SEG
) {
584 error(ERR_NONFATAL
, "COFF format does not support"
586 } else if (segment
% 2) {
587 error(ERR_NONFATAL
, "COFF format does not support"
588 " segment base references");
590 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_I386_DIR32
);
593 WRITELONG(p
, *(int64_t *)data
+ fix
);
594 coff_sect_write(s
, mydata
, size
);
600 if (wrt
== imagebase_sect
) {
601 error(ERR_NONFATAL
, "operand size mismatch: 'wrt "
602 WRT_IMAGEBASE
"' is a 32-bit operand");
604 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_AMD64_ADDR64
);
605 WRITEDLONG(p
, *(int64_t *)data
+ fix
);
606 coff_sect_write(s
, mydata
, size
);
608 fix
= coff_add_reloc(s
, segment
,
609 wrt
== imagebase_sect
? IMAGE_REL_AMD64_ADDR32NB
:
610 IMAGE_REL_AMD64_ADDR32
);
611 WRITELONG(p
, *(int64_t *)data
+ fix
);
612 coff_sect_write(s
, mydata
, size
);
615 } else if (type
== OUT_REL2ADR
) {
616 error(ERR_NONFATAL
, "COFF format does not support 16-bit"
618 } else if (type
== OUT_REL4ADR
) {
619 if (segment
== segto
&& !(win64
)) /* Acceptable for RIP-relative */
620 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
621 else if (segment
== NO_SEG
&& win32
)
622 error(ERR_NONFATAL
, "Win32 COFF does not correctly support"
623 " relative references to absolute addresses");
626 if (segment
!= NO_SEG
&& segment
% 2) {
627 error(ERR_NONFATAL
, "COFF format does not support"
628 " segment base references");
630 fix
= coff_add_reloc(s
, segment
,
631 win64
? IMAGE_REL_AMD64_REL32
: IMAGE_REL_I386_REL32
);
634 WRITELONG(p
, *(int64_t *)data
+ 4 - size
+ fix
);
636 WRITELONG(p
, *(int64_t *)data
- (size
+ s
->len
) + fix
);
638 coff_sect_write(s
, mydata
, 4L);
644 static void coff_sect_write(struct Section
*sect
,
645 const uint8_t *data
, uint32_t len
)
647 saa_wbytes(sect
->data
, data
, len
);
651 typedef struct tagString
{
652 struct tagString
*Next
;
657 #define EXPORT_SECTION_NAME ".drectve"
658 #define EXPORT_SECTION_FLAGS INFO_FLAGS
660 #define EXPORT_SECTION_NAME ".text"
661 #define EXPORT_SECTION_FLAGS TEXT_FLAGS
664 static STRING
*Exports
= NULL
;
665 static struct Section
*directive_sec
;
666 void AddExport(char *name
)
668 STRING
*rvp
= Exports
, *newS
;
670 newS
= (STRING
*) nasm_malloc(sizeof(STRING
));
671 newS
->len
= strlen(name
);
673 newS
->String
= (char *)nasm_malloc(newS
->len
+ 1);
674 strcpy(newS
->String
, name
);
677 for (i
= 0; i
< nsects
; i
++)
679 if (!strcmp(EXPORT_SECTION_NAME
, sects
[i
]->name
))
683 sects
[coff_make_section
684 (EXPORT_SECTION_NAME
, EXPORT_SECTION_FLAGS
)];
686 directive_sec
= sects
[i
];
690 if (!strcmp(rvp
->String
, name
))
698 void BuildExportTable(void)
700 STRING
*rvp
= Exports
, *next
;
706 len
= sprintf((char *)buf
, "-export:%s ", rvp
->String
);
707 coff_sect_write(directive_sec
, buf
, len
);
712 while ((rvp
= next
)) {
714 nasm_free(rvp
->String
);
720 static int coff_directives(char *directive
, char *value
, int pass
)
722 if (!strcmp(directive
, "export")) {
726 return 1; /* ignore in pass two */
728 while (*q
&& !nasm_isspace(*q
))
730 if (nasm_isspace(*q
)) {
732 while (*q
&& nasm_isspace(*q
))
737 error(ERR_NONFATAL
, "`export' directive requires export name");
741 error(ERR_NONFATAL
, "unrecognized export qualifier `%s'", q
);
746 } else if (win32
&& !strcmp(directive
,"safeseh")) {
750 { for (i
= 0; i
< nsects
; i
++)
751 if (!strcmp(".sxdata",sects
[i
]->name
))
754 sxseg
= coff_make_section(".sxdata",0x200);
758 /* pass0 == 2 is the only time when the full set of symbols are
759 guaranteed to be present; it is the final output pass. */
763 for (n
= 0; n
< nsyms
; n
++) {
764 struct Symbol
*sym
= saa_rstruct(syms
);
767 /* sym->strpos is biased by 4, because symbol
768 * table is prefixed with table length */
769 if (sym
->strpos
>=4) {
770 char *name
= nasm_malloc(sym
->namlen
+1);
771 saa_fread(strs
, sym
->strpos
-4, name
, sym
->namlen
);
772 name
[sym
->namlen
] = '\0';
773 equals
= !strcmp(value
,name
);
777 equals
= !strcmp(value
,sym
->name
);
780 /* this value arithmetics effectively reflects
781 * initsym in coff_write(): 2 for file, 1 for
782 * .absolute and two per each section */
783 unsigned char value
[4],*p
=value
;
784 WRITELONG(p
,n
+ 2 + 1 + nsects
*2);
785 coff_sect_write(sects
[sxseg
],value
,4);
792 "`safeseh' directive requires valid symbol");
800 static void coff_write(void)
802 int32_t pos
, sympos
, vsize
;
805 BuildExportTable(); /* fill in the .drectve section with -export's */
808 /* add default value for @feat.00, this allows to 'link /safeseh' */
812 for (n
= 0; n
< nsyms
; n
++) {
813 struct Symbol
*sym
= saa_rstruct(syms
);
814 if (sym
->strpos
== -1 && !strcmp("@feat.00",sym
->name
))
818 coff_deflabel("@feat.00",NO_SEG
,1,0,NULL
);
822 * Work out how big the file will get. Calculate the start of
823 * the `real' symbols at the same time.
825 pos
= 0x14 + 0x28 * nsects
;
826 initsym
= 3; /* two for the file, one absolute */
827 for (i
= 0; i
< nsects
; i
++) {
828 if (sects
[i
]->data
) {
830 pos
+= sects
[i
]->len
;
831 sects
[i
]->relpos
= pos
;
832 pos
+= 10 * sects
[i
]->nrelocs
;
834 sects
[i
]->pos
= sects
[i
]->relpos
= 0L;
835 initsym
+= 2; /* two for each section */
840 * Output the COFF header.
843 fwriteint16_t(0x8664, coffp
); /* MACHINE_x86-64 */
845 fwriteint16_t(0x014C, coffp
); /* MACHINE_i386 */
846 fwriteint16_t(nsects
, coffp
); /* number of sections */
847 fwriteint32_t(time(NULL
), coffp
); /* time stamp */
848 fwriteint32_t(sympos
, coffp
);
849 fwriteint32_t(nsyms
+ initsym
, coffp
);
850 fwriteint16_t(0, coffp
); /* no optional header */
851 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
852 fwriteint16_t((win32
| win64
) ? 0 : 0x104, coffp
);
855 * Output the section headers.
858 for (i
= 0; i
< nsects
; i
++) {
859 coff_section_header(sects
[i
]->name
, vsize
, sects
[i
]->len
,
860 sects
[i
]->pos
, sects
[i
]->relpos
,
861 sects
[i
]->nrelocs
, sects
[i
]->flags
);
862 vsize
+= sects
[i
]->len
;
866 * Output the sections and their relocations.
868 for (i
= 0; i
< nsects
; i
++)
869 if (sects
[i
]->data
) {
870 saa_fpwrite(sects
[i
]->data
, coffp
);
871 coff_write_relocs(sects
[i
]);
875 * Output the symbol and string tables.
877 coff_write_symbols();
878 fwriteint32_t(strslen
+ 4, coffp
); /* length includes length count */
879 saa_fpwrite(strs
, coffp
);
882 static void coff_section_header(char *name
, int32_t vsize
,
883 int32_t datalen
, int32_t datapos
,
884 int32_t relpos
, int nrelocs
, int32_t flags
)
890 memset(padname
, 0, 8);
891 strncpy(padname
, name
, 8);
892 fwrite(padname
, 8, 1, coffp
);
893 fwriteint32_t(0, coffp
); /* Virtual size field - set to 0 or vsize */
894 fwriteint32_t(0L, coffp
); /* RVA/offset - we ignore */
895 fwriteint32_t(datalen
, coffp
);
896 fwriteint32_t(datapos
, coffp
);
897 fwriteint32_t(relpos
, coffp
);
898 fwriteint32_t(0L, coffp
); /* no line numbers - we don't do 'em */
899 fwriteint16_t(nrelocs
, coffp
);
900 fwriteint16_t(0, coffp
); /* again, no line numbers */
901 fwriteint32_t(flags
, coffp
);
904 static void coff_write_relocs(struct Section
*s
)
908 for (r
= s
->head
; r
; r
= r
->next
) {
909 fwriteint32_t(r
->address
, coffp
);
910 fwriteint32_t(r
->symbol
+ (r
->symbase
== REAL_SYMBOLS
? initsym
:
911 r
->symbase
== ABS_SYMBOL
? initsym
- 1 :
912 r
->symbase
== SECT_SYMBOLS
? 2 : 0),
914 fwriteint16_t(r
->type
, coffp
);
918 static void coff_symbol(char *name
, int32_t strpos
, int32_t value
,
919 int section
, int type
, int storageclass
, int aux
)
924 memset(padname
, 0, 8);
925 strncpy(padname
, name
, 8);
926 fwrite(padname
, 8, 1, coffp
);
928 fwriteint32_t(0, coffp
);
929 fwriteint32_t(strpos
, coffp
);
931 fwriteint32_t(value
, coffp
);
932 fwriteint16_t(section
, coffp
);
933 fwriteint16_t(type
, coffp
);
934 fputc(storageclass
, coffp
);
938 static void coff_write_symbols(void)
944 * The `.file' record, and the file name auxiliary record.
946 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
947 memset(filename
, 0, 18);
948 strncpy(filename
, coff_infile
, 18);
949 fwrite(filename
, 18, 1, coffp
);
952 * The section records, with their auxiliaries.
954 memset(filename
, 0, 18); /* useful zeroed buffer */
956 for (i
= 0; i
< (uint32_t) nsects
; i
++) {
957 coff_symbol(sects
[i
]->name
, 0L, 0L, i
+ 1, 0, 3, 1);
958 fwriteint32_t(sects
[i
]->len
, coffp
);
959 fwriteint16_t(sects
[i
]->nrelocs
, coffp
);
960 fwrite(filename
, 12, 1, coffp
);
964 * The absolute symbol, for relative-to-absolute relocations.
966 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
972 for (i
= 0; i
< nsyms
; i
++) {
973 struct Symbol
*sym
= saa_rstruct(syms
);
974 coff_symbol(sym
->strpos
== -1 ? sym
->name
: NULL
,
975 sym
->strpos
, sym
->value
, sym
->section
,
976 sym
->type
, sym
->is_global
? 2 : 3, 0);
980 static int32_t coff_segbase(int32_t segment
)
985 static void coff_std_filename(char *inname
, char *outname
, efunc error
)
987 strcpy(coff_infile
, inname
);
988 standard_extension(inname
, outname
, ".o", error
);
991 static void coff_win32_filename(char *inname
, char *outname
, efunc error
)
993 strcpy(coff_infile
, inname
);
994 standard_extension(inname
, outname
, ".obj", error
);
997 extern macros_t coff_stdmac
[];
999 static int coff_set_info(enum geninfo type
, char **val
)
1005 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
1009 struct ofmt of_coff
= {
1010 "COFF (i386) object files (e.g. DJGPP for DOS)",
1031 struct ofmt of_win32
= {
1032 "Microsoft Win32 (i386) object files",
1045 coff_win32_filename
,
1053 struct ofmt of_win64
= {
1054 "Microsoft Win64 (x86-64) object files",
1067 coff_win32_filename
,