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_malloc(sizeof(*s
));
290 if (flags
!= BSS_FLAGS
)
291 s
->data
= saa_init(1);
298 if (!strcmp(name
, ".text"))
301 s
->index
= seg_alloc();
302 strncpy(s
->name
, name
, 8);
306 if (nsects
>= sectlen
) {
307 sectlen
+= SECT_DELTA
;
308 sects
= nasm_realloc(sects
, sectlen
* sizeof(*sects
));
315 static inline int32_t coff_sectalign_flags(unsigned int align
)
317 return (ilog2_32(align
) + 1) << 20;
320 static int32_t coff_section_names(char *name
, int pass
, int *bits
)
323 uint32_t flags
, align_and
= ~0L, align_or
= 0L;
340 while (*p
&& !nasm_isspace(*p
))
344 if (strlen(name
) > 8) {
345 nasm_error(ERR_WARNING
, "COFF section names limited to 8 characters:"
351 while (*p
&& nasm_isspace(*p
))
355 while (*p
&& !nasm_isspace(*p
))
359 while (*p
&& nasm_isspace(*p
))
362 if (!nasm_stricmp(q
, "code") || !nasm_stricmp(q
, "text")) {
364 } else if (!nasm_stricmp(q
, "data")) {
366 } else if (!nasm_stricmp(q
, "rdata")) {
370 flags
= DATA_FLAGS
; /* gotta do something */
371 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
372 " read-only data sections");
374 } else if (!nasm_stricmp(q
, "bss")) {
376 } else if (!nasm_stricmp(q
, "info")) {
380 flags
= DATA_FLAGS
; /* gotta do something */
381 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
382 " informational sections");
384 } else if (!nasm_strnicmp(q
, "align=", 6)) {
385 if (!(win32
| win64
))
386 nasm_error(ERR_NONFATAL
, "standard COFF does not support"
387 " section alignment specification");
389 if (q
[6 + strspn(q
+ 6, "0123456789")])
390 nasm_error(ERR_NONFATAL
,
391 "argument to `align' is not numeric");
393 unsigned int align
= atoi(q
+ 6);
394 if (!align
|| ((align
- 1) & align
))
395 nasm_error(ERR_NONFATAL
, "argument to `align' is not a"
398 nasm_error(ERR_NONFATAL
, "Win32 cannot align sections"
399 " to better than 64-byte boundaries");
401 align_and
= ~0x00F00000L
;
402 align_or
= coff_sectalign_flags(align
);
409 for (i
= 0; i
< nsects
; i
++)
410 if (!strcmp(name
, sects
[i
]->name
))
414 if (!strcmp(name
, ".data"))
416 else if (!strcmp(name
, ".rdata"))
418 else if (!strcmp(name
, ".bss"))
420 else if (win64
&& !strcmp(name
, ".pdata"))
422 else if (win64
&& !strcmp(name
, ".xdata"))
427 i
= coff_make_section(name
, flags
);
429 sects
[i
]->flags
= flags
;
430 sects
[i
]->flags
&= align_and
;
431 sects
[i
]->flags
|= align_or
;
432 } else if (pass
== 1) {
434 nasm_error(ERR_WARNING
, "section attributes ignored on"
435 " redeclaration of section `%s'", name
);
438 return sects
[i
]->index
;
441 static void coff_deflabel(char *name
, int32_t segment
, int64_t offset
,
442 int is_global
, char *special
)
444 int pos
= strslen
+ 4;
448 nasm_error(ERR_NONFATAL
, "COFF format does not support any"
449 " special symbol types");
451 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
452 if (strcmp(name
,WRT_IMAGEBASE
))
453 nasm_error(ERR_NONFATAL
, "unrecognized special symbol `%s'", name
);
457 if (strlen(name
) > 8) {
458 size_t nlen
= strlen(name
)+1;
459 saa_wbytes(strs
, name
, nlen
);
464 sym
= saa_wstruct(syms
);
467 sym
->namlen
= strlen(name
);
469 strcpy(sym
->name
, name
);
470 sym
->is_global
= !!is_global
;
471 sym
->type
= 0; /* Default to T_NULL (no type) */
472 if (segment
== NO_SEG
)
473 sym
->section
= -1; /* absolute symbol */
477 for (i
= 0; i
< nsects
; i
++)
478 if (segment
== sects
[i
]->index
) {
479 sym
->section
= i
+ 1;
483 sym
->is_global
= true;
488 sym
->value
= (sym
->section
== 0 ? 0 : offset
);
491 * define the references from external-symbol segment numbers
492 * to these symbol records.
494 if (sym
->section
== 0)
495 bsym
= raa_write(bsym
, segment
, nsyms
);
497 if (segment
!= NO_SEG
)
498 symval
= raa_write(symval
, segment
, sym
->section
? 0 : sym
->value
);
503 static int32_t coff_add_reloc(struct Section
*sect
, int32_t segment
,
508 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
509 sect
->tail
= &r
->next
;
512 r
->address
= sect
->len
;
513 if (segment
== NO_SEG
) {
514 r
->symbol
= 0, r
->symbase
= ABS_SYMBOL
;
517 r
->symbase
= REAL_SYMBOLS
;
518 for (i
= 0; i
< nsects
; i
++) {
519 if (segment
== sects
[i
]->index
) {
521 r
->symbase
= SECT_SYMBOLS
;
525 if (r
->symbase
== REAL_SYMBOLS
)
526 r
->symbol
= raa_read(bsym
, segment
);
533 * Return the fixup for standard COFF common variables.
535 if (r
->symbase
== REAL_SYMBOLS
&& !(win32
| win64
))
536 return raa_read(symval
, segment
);
541 static void coff_out(int32_t segto
, const void *data
,
542 enum out_type type
, uint64_t size
,
543 int32_t segment
, int32_t wrt
)
546 uint8_t mydata
[8], *p
;
549 if (wrt
!= NO_SEG
&& !win64
) {
550 wrt
= NO_SEG
; /* continue to do _something_ */
551 nasm_error(ERR_NONFATAL
, "WRT not supported by COFF output formats");
555 * handle absolute-assembly (structure definitions)
557 if (segto
== NO_SEG
) {
558 if (type
!= OUT_RESERVE
)
559 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
565 for (i
= 0; i
< nsects
; i
++) {
566 if (segto
== sects
[i
]->index
) {
572 int tempint
; /* ignored */
573 if (segto
!= coff_section_names(".text", 2, &tempint
))
574 nasm_error(ERR_PANIC
, "strange segment conditions in COFF driver");
576 s
= sects
[nsects
- 1];
579 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
580 if (win64
&& wrt
== NO_SEG
) {
581 if (!strcmp(s
->name
,".pdata") || !strcmp(s
->name
,".xdata"))
582 wrt
= imagebase_sect
;
585 if (!s
->data
&& type
!= OUT_RESERVE
) {
586 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
587 " BSS section `%s': ignored", s
->name
);
588 s
->len
+= realsize(type
, size
);
592 if (type
== OUT_RESERVE
) {
594 nasm_error(ERR_WARNING
, "uninitialised space declared in"
595 " non-BSS section `%s': zeroing", s
->name
);
596 coff_sect_write(s
, NULL
, size
);
599 } else if (type
== OUT_RAWDATA
) {
600 if (segment
!= NO_SEG
)
601 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
602 coff_sect_write(s
, data
, size
);
603 } else if (type
== OUT_ADDRESS
) {
605 if (size
!= 4 && (segment
!= NO_SEG
|| wrt
!= NO_SEG
)) {
606 nasm_error(ERR_NONFATAL
, "COFF format does not support non-32-bit"
610 if (segment
!= NO_SEG
|| wrt
!= NO_SEG
) {
612 nasm_error(ERR_NONFATAL
, "COFF format does not support"
614 } else if (segment
% 2) {
615 nasm_error(ERR_NONFATAL
, "COFF format does not support"
616 " segment base references");
618 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_I386_DIR32
);
621 WRITELONG(p
, *(int64_t *)data
+ fix
);
622 coff_sect_write(s
, mydata
, size
);
628 if (wrt
== imagebase_sect
) {
629 nasm_error(ERR_NONFATAL
, "operand size mismatch: 'wrt "
630 WRT_IMAGEBASE
"' is a 32-bit operand");
632 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_AMD64_ADDR64
);
633 WRITEDLONG(p
, *(int64_t *)data
+ fix
);
634 coff_sect_write(s
, mydata
, size
);
636 fix
= coff_add_reloc(s
, segment
,
637 wrt
== imagebase_sect
? IMAGE_REL_AMD64_ADDR32NB
:
638 IMAGE_REL_AMD64_ADDR32
);
639 WRITELONG(p
, *(int64_t *)data
+ fix
);
640 coff_sect_write(s
, mydata
, size
);
643 } else if (type
== OUT_REL2ADR
) {
644 nasm_error(ERR_NONFATAL
, "COFF format does not support 16-bit"
646 } else if (type
== OUT_REL4ADR
) {
647 if (segment
== segto
&& !(win64
)) /* Acceptable for RIP-relative */
648 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
649 else if (segment
== NO_SEG
&& win32
)
650 nasm_error(ERR_NONFATAL
, "Win32 COFF does not correctly support"
651 " relative references to absolute addresses");
654 if (segment
!= NO_SEG
&& segment
% 2) {
655 nasm_error(ERR_NONFATAL
, "COFF format does not support"
656 " segment base references");
658 fix
= coff_add_reloc(s
, segment
,
659 win64
? IMAGE_REL_AMD64_REL32
: IMAGE_REL_I386_REL32
);
662 WRITELONG(p
, *(int64_t *)data
+ 4 - size
+ fix
);
664 WRITELONG(p
, *(int64_t *)data
- (size
+ s
->len
) + fix
);
666 coff_sect_write(s
, mydata
, 4L);
672 static void coff_sect_write(struct Section
*sect
,
673 const uint8_t *data
, uint32_t len
)
675 saa_wbytes(sect
->data
, data
, len
);
679 typedef struct tagString
{
680 struct tagString
*next
;
685 #define EXPORT_SECTION_NAME ".drectve"
686 #define EXPORT_SECTION_FLAGS INFO_FLAGS
688 * #define EXPORT_SECTION_NAME ".text"
689 * #define EXPORT_SECTION_FLAGS TEXT_FLAGS
692 static STRING
*Exports
= NULL
;
693 static struct Section
*directive_sec
;
694 void AddExport(char *name
)
696 STRING
*rvp
= Exports
, *newS
;
698 newS
= (STRING
*) nasm_malloc(sizeof(STRING
));
699 newS
->len
= strlen(name
);
701 newS
->String
= (char *)nasm_malloc(newS
->len
+ 1);
702 strcpy(newS
->String
, name
);
706 for (i
= 0; i
< nsects
; i
++) {
707 if (!strcmp(EXPORT_SECTION_NAME
, sects
[i
]->name
))
712 i
= coff_make_section(EXPORT_SECTION_NAME
, EXPORT_SECTION_FLAGS
);
714 directive_sec
= sects
[i
];
718 if (!strcmp(rvp
->String
, name
))
726 static void BuildExportTable(STRING
**rvp
)
733 list_for_each_safe(p
, t
, *rvp
) {
734 coff_sect_write(directive_sec
, (uint8_t *)"-export:", 8);
735 coff_sect_write(directive_sec
, (uint8_t *)p
->String
, p
->len
);
736 coff_sect_write(directive_sec
, (uint8_t *)" ", 1);
737 nasm_free(p
->String
);
744 static int coff_directives(enum directives directive
, char *value
, int pass
)
752 return 1; /* ignore in pass two */
754 while (*q
&& !nasm_isspace(*q
))
756 if (nasm_isspace(*q
)) {
758 while (*q
&& nasm_isspace(*q
))
763 nasm_error(ERR_NONFATAL
, "`export' directive requires export name");
767 nasm_error(ERR_NONFATAL
, "unrecognized export qualifier `%s'", q
);
778 if (!win32
) /* Only applicable for -f win32 */
782 for (i
= 0; i
< nsects
; i
++)
783 if (!strcmp(".sxdata",sects
[i
]->name
))
786 sxseg
= coff_make_section(".sxdata", IMAGE_SCN_LNK_INFO
);
791 * pass0 == 2 is the only time when the full set of symbols are
792 * guaranteed to be present; it is the final output pass.
797 for (n
= 0; n
< nsyms
; n
++) {
798 struct Symbol
*sym
= saa_rstruct(syms
);
802 * sym->strpos is biased by 4, because symbol
803 * table is prefixed with table length
805 if (sym
->strpos
>=4) {
806 char *name
= nasm_malloc(sym
->namlen
+1);
807 saa_fread(strs
, sym
->strpos
-4, name
, sym
->namlen
);
808 name
[sym
->namlen
] = '\0';
809 equals
= !strcmp(value
,name
);
812 equals
= !strcmp(value
,sym
->name
);
817 * this value arithmetics effectively reflects
818 * initsym in coff_write(): 2 for file, 1 for
819 * .absolute and two per each section
821 unsigned char value
[4],*p
=value
;
822 WRITELONG(p
,n
+ 2 + 1 + nsects
*2);
823 coff_sect_write(sects
[sxseg
],value
,4);
829 nasm_error(ERR_NONFATAL
,
830 "`safeseh' directive requires valid symbol");
840 static void coff_write(void)
842 int32_t pos
, sympos
, vsize
;
845 /* fill in the .drectve section with -export's */
846 BuildExportTable(&Exports
);
849 /* add default value for @feat.00, this allows to 'link /safeseh' */
853 for (n
= 0; n
< nsyms
; n
++) {
854 struct Symbol
*sym
= saa_rstruct(syms
);
855 if (sym
->strpos
== -1 && !strcmp("@feat.00",sym
->name
))
859 coff_deflabel("@feat.00", NO_SEG
, 1, 0, NULL
);
863 * Work out how big the file will get. Calculate the start of
864 * the `real' symbols at the same time.
866 pos
= 0x14 + 0x28 * nsects
;
867 initsym
= 3; /* two for the file, one absolute */
868 for (i
= 0; i
< nsects
; i
++) {
869 if (sects
[i
]->data
) {
871 pos
+= sects
[i
]->len
;
872 sects
[i
]->relpos
= pos
;
873 pos
+= 10 * sects
[i
]->nrelocs
;
875 sects
[i
]->pos
= sects
[i
]->relpos
= 0L;
876 initsym
+= 2; /* two for each section */
881 * Output the COFF header.
884 i
= IMAGE_FILE_MACHINE_AMD64
;
886 i
= IMAGE_FILE_MACHINE_I386
;
887 fwriteint16_t(i
, ofile
); /* machine type */
888 fwriteint16_t(nsects
, ofile
); /* number of sections */
889 fwriteint32_t(time(NULL
), ofile
); /* time stamp */
890 fwriteint32_t(sympos
, ofile
);
891 fwriteint32_t(nsyms
+ initsym
, ofile
);
892 fwriteint16_t(0, ofile
); /* no optional header */
893 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
894 fwriteint16_t((win32
| win64
) ? 0 : 0x104, ofile
);
897 * Output the section headers.
900 for (i
= 0; i
< nsects
; i
++) {
901 coff_section_header(sects
[i
]->name
, vsize
, sects
[i
]->len
,
902 sects
[i
]->pos
, sects
[i
]->relpos
,
903 sects
[i
]->nrelocs
, sects
[i
]->flags
);
904 vsize
+= sects
[i
]->len
;
908 * Output the sections and their relocations.
910 for (i
= 0; i
< nsects
; i
++)
911 if (sects
[i
]->data
) {
912 saa_fpwrite(sects
[i
]->data
, ofile
);
913 coff_write_relocs(sects
[i
]);
917 * Output the symbol and string tables.
919 coff_write_symbols();
920 fwriteint32_t(strslen
+ 4, ofile
); /* length includes length count */
921 saa_fpwrite(strs
, ofile
);
924 static void coff_section_header(char *name
, int32_t vsize
,
925 int32_t datalen
, int32_t datapos
,
926 int32_t relpos
, int nrelocs
, int32_t flags
)
932 memset(padname
, 0, 8);
933 strncpy(padname
, name
, 8);
934 fwrite(padname
, 8, 1, ofile
);
936 fwriteint32_t(0, ofile
); /* Virtual size field - set to 0 or vsize */
937 fwriteint32_t(0L, ofile
); /* RVA/offset - we ignore */
938 fwriteint32_t(datalen
, ofile
);
939 fwriteint32_t(datapos
, ofile
);
940 fwriteint32_t(relpos
, ofile
);
941 fwriteint32_t(0L, ofile
); /* no line numbers - we don't do 'em */
942 fwriteint16_t(nrelocs
, ofile
);
943 fwriteint16_t(0, ofile
); /* again, no line numbers */
944 fwriteint32_t(flags
, ofile
);
947 static void coff_write_relocs(struct Section
*s
)
951 for (r
= s
->head
; r
; r
= r
->next
) {
952 fwriteint32_t(r
->address
, ofile
);
953 fwriteint32_t(r
->symbol
+ (r
->symbase
== REAL_SYMBOLS
? initsym
:
954 r
->symbase
== ABS_SYMBOL
? initsym
- 1 :
955 r
->symbase
== SECT_SYMBOLS
? 2 : 0),
957 fwriteint16_t(r
->type
, ofile
);
961 static void coff_symbol(char *name
, int32_t strpos
, int32_t value
,
962 int section
, int type
, int storageclass
, int aux
)
967 memset(padname
, 0, 8);
968 strncpy(padname
, name
, 8);
969 fwrite(padname
, 8, 1, ofile
);
971 fwriteint32_t(0, ofile
);
972 fwriteint32_t(strpos
, ofile
);
975 fwriteint32_t(value
, ofile
);
976 fwriteint16_t(section
, ofile
);
977 fwriteint16_t(type
, ofile
);
979 fputc(storageclass
, ofile
);
983 static void coff_write_symbols(void)
989 * The `.file' record, and the file name auxiliary record.
991 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
992 memset(filename
, 0, 18);
993 strncpy(filename
, coff_infile
, 18);
994 fwrite(filename
, 18, 1, ofile
);
997 * The section records, with their auxiliaries.
999 memset(filename
, 0, 18); /* useful zeroed buffer */
1001 for (i
= 0; i
< (uint32_t) nsects
; i
++) {
1002 coff_symbol(sects
[i
]->name
, 0L, 0L, i
+ 1, 0, 3, 1);
1003 fwriteint32_t(sects
[i
]->len
, ofile
);
1004 fwriteint16_t(sects
[i
]->nrelocs
,ofile
);
1005 fwrite(filename
, 12, 1, ofile
);
1009 * The absolute symbol, for relative-to-absolute relocations.
1011 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
1017 for (i
= 0; i
< nsyms
; i
++) {
1018 struct Symbol
*sym
= saa_rstruct(syms
);
1019 coff_symbol(sym
->strpos
== -1 ? sym
->name
: NULL
,
1020 sym
->strpos
, sym
->value
, sym
->section
,
1021 sym
->type
, sym
->is_global
? 2 : 3, 0);
1025 static void coff_sectalign(int32_t seg
, unsigned int value
)
1027 struct Section
*s
= NULL
;
1031 for (i
= 0; i
< nsects
; i
++) {
1032 if (sects
[i
]->index
== seg
) {
1038 if (!s
|| !is_power2(value
))
1041 /* DOS has limitation on 64 bytes */
1042 if (!(win32
| win64
) && value
> 64)
1045 align
= (s
->flags
& IMAGE_SCN_ALIGN_MASK
);
1046 value
= coff_sectalign_flags(value
);
1048 s
->flags
= (s
->flags
& ~IMAGE_SCN_ALIGN_MASK
) | value
;
1051 static int32_t coff_segbase(int32_t segment
)
1056 static void coff_std_filename(char *inname
, char *outname
)
1058 strcpy(coff_infile
, inname
);
1059 standard_extension(inname
, outname
, ".o");
1062 static void coff_win32_filename(char *inname
, char *outname
)
1064 strcpy(coff_infile
, inname
);
1065 standard_extension(inname
, outname
, ".obj");
1068 extern macros_t coff_stdmac
[];
1070 static int coff_set_info(enum geninfo type
, char **val
)
1076 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
1080 struct ofmt of_coff
= {
1081 "COFF (i386) object files (e.g. DJGPP for DOS)",
1103 struct ofmt of_win32
= {
1104 "Microsoft Win32 (i386) object files",
1118 coff_win32_filename
,
1126 struct ofmt of_win64
= {
1127 "Microsoft Win64 (x86-64) object files",
1141 coff_win32_filename
,