1 /* outcoff.c output routines for the Netwide Assembler to produce
2 * COFF object files (for DJGPP and Win32)
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
23 #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
28 * (0) When I say `standard COFF' below, I mean `COFF as output and
29 * used by DJGPP'. I assume DJGPP gets it right.
31 * (1) Win32 appears to interpret the term `relative relocation'
32 * differently from standard COFF. Standard COFF understands a
33 * relative relocation to mean that during relocation you add the
34 * address of the symbol you're referencing, and subtract the base
35 * address of the section you're in. Win32 COFF, by contrast, seems
36 * to add the address of the symbol and then subtract the address
37 * of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
38 * subtly incompatible.
40 * (2) Win32 doesn't bother putting any flags in the header flags
41 * field (at offset 0x12 into the file).
43 * (3) Win32 uses some extra flags into the section header table:
44 * it defines flags 0x80000000 (writable), 0x40000000 (readable)
45 * and 0x20000000 (executable), and uses them in the expected
46 * combinations. It also defines 0x00100000 through 0x00700000 for
47 * section alignments of 1 through 64 bytes.
49 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
50 * field directly after the section name in the section header
51 * table for something strange: they store what the address of the
52 * section start point _would_ be, if you laid all the sections end
53 * to end starting at zero. Dunno why. Microsoft's documentation
54 * lists this field as "Virtual Size of Section", which doesn't
55 * seem to fit at all. In fact, Win32 even includes non-linked
56 * sections such as .drectve in this calculation.
58 * Newer versions of MASM seem to have changed this to be zero, and
59 * that apparently matches the COFF spec, so go with that.
61 * (5) Standard COFF does something very strange to common
62 * variables: the relocation point for a common variable is as far
63 * _before_ the variable as its size stretches out _after_ it. So
64 * we must fix up common variable references. Win32 seems to be
65 * sensible on this one.
68 /* Flag which version of COFF we are currently outputting. */
69 static bool win32
, win64
;
71 static int32_t imagebase_sect
;
72 #define WRT_IMAGEBASE "..imagebase"
76 int32_t address
; /* relative to _start_ of section */
77 int32_t symbol
; /* symbol number */
82 } symbase
; /* relocation for symbol number :) */
86 /* possible values for Reloc->type */
87 #define IMAGE_REL_AMD64_ADDR64 0x0001
88 #define IMAGE_REL_AMD64_ADDR32 0x0002
89 #define IMAGE_REL_AMD64_ADDR32NB 0x0003
90 #define IMAGE_REL_AMD64_REL32 0x0004
91 #define IMAGE_REL_I386_DIR32 0x0006
92 #define IMAGE_REL_I386_DIR32NB 0x0007
93 #define IMAGE_REL_I386_REL32 0x0014
97 int32_t strpos
; /* string table position of name */
98 int32_t value
; /* address, or COMMON variable size */
99 int section
; /* section number where it's defined
100 * - in COFF codes, not NASM codes */
101 bool is_global
; /* is it a global symbol or not? */
102 int16_t type
; /* 0 - notype, 0x20 - function */
103 int32_t namlen
; /* full name length */
108 static char coff_infile
[FILENAME_MAX
];
115 struct Reloc
*head
, **tail
;
116 uint32_t flags
; /* section flags */
121 #define TEXT_FLAGS ((win32 | win64) ? 0x60500020L : 0x20L)
122 #define DATA_FLAGS ((win32 | win64) ? 0xC0300040L : 0x40L)
123 #define BSS_FLAGS ((win32 | win64) ? 0xC0300080L : 0x80L)
124 #define INFO_FLAGS 0x00100A00L
125 #define RDATA_FLAGS ((win32 | win64) ? 0x40400040L : 0x40L)
127 #define SECT_DELTA 32
128 static struct Section
**sects
;
129 static int nsects
, sectlen
;
131 static struct SAA
*syms
;
132 static uint32_t nsyms
;
134 static int32_t def_seg
;
138 static struct RAA
*bsym
, *symval
;
140 static struct SAA
*strs
;
141 static uint32_t strslen
;
143 static void coff_gen_init(FILE *, efunc
);
144 static void coff_sect_write(struct Section
*, const uint8_t *,
146 static void coff_write(void);
147 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
148 static void coff_write_relocs(struct Section
*);
149 static void coff_write_symbols(void);
151 static void coff_win32_init(FILE * fp
, efunc errfunc
,
152 ldfunc ldef
, evalfunc eval
)
154 win32
= true; win64
= false;
155 (void)ldef
; /* placate optimizers */
157 coff_gen_init(fp
, errfunc
);
160 static void coff_win64_init(FILE * fp
, efunc errfunc
,
161 ldfunc ldef
, evalfunc eval
)
163 extern struct ofmt of_win64
;
166 win32
= false; win64
= true;
167 (void)ldef
; /* placate optimizers */
169 coff_gen_init(fp
, errfunc
);
170 imagebase_sect
= seg_alloc()+1;
171 ldef(WRT_IMAGEBASE
,imagebase_sect
,0,NULL
,false,false,&of_win64
,errfunc
);
174 static void coff_std_init(FILE * fp
, efunc errfunc
, ldfunc ldef
,
177 win32
= win64
= false;
178 (void)ldef
; /* placate optimizers */
180 coff_gen_init(fp
, errfunc
);
183 static void coff_gen_init(FILE * fp
, efunc errfunc
)
189 nsects
= sectlen
= 0;
190 syms
= saa_init((int32_t)sizeof(struct Symbol
));
196 def_seg
= seg_alloc();
199 static void coff_cleanup(int debuginfo
)
208 for (i
= 0; i
< nsects
; i
++) {
210 saa_free(sects
[i
]->data
);
211 while (sects
[i
]->head
) {
213 sects
[i
]->head
= sects
[i
]->head
->next
;
225 static int coff_make_section(char *name
, uint32_t flags
)
229 s
= nasm_malloc(sizeof(*s
));
231 if (flags
!= BSS_FLAGS
)
232 s
->data
= saa_init(1L);
239 if (!strcmp(name
, ".text"))
242 s
->index
= seg_alloc();
243 strncpy(s
->name
, name
, 8);
247 if (nsects
>= sectlen
)
249 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
255 static int32_t coff_section_names(char *name
, int pass
, int *bits
)
258 uint32_t flags
, align_and
= ~0L, align_or
= 0L;
275 while (*p
&& !isspace(*p
))
279 if (strlen(name
) > 8) {
280 error(ERR_WARNING
, "COFF section names limited to 8 characters:"
286 while (*p
&& isspace(*p
))
290 while (*p
&& !isspace(*p
))
294 while (*p
&& isspace(*p
))
297 if (!nasm_stricmp(q
, "code") || !nasm_stricmp(q
, "text")) {
299 } else if (!nasm_stricmp(q
, "data")) {
301 } else if (!nasm_stricmp(q
, "rdata")) {
305 flags
= DATA_FLAGS
; /* gotta do something */
306 error(ERR_NONFATAL
, "standard COFF does not support"
307 " read-only data sections");
309 } else if (!nasm_stricmp(q
, "bss")) {
311 } else if (!nasm_stricmp(q
, "info")) {
315 flags
= DATA_FLAGS
; /* gotta do something */
316 error(ERR_NONFATAL
, "standard COFF does not support"
317 " informational sections");
319 } else if (!nasm_strnicmp(q
, "align=", 6)) {
320 if (!(win32
| win64
))
321 error(ERR_NONFATAL
, "standard COFF does not support"
322 " section alignment specification");
324 if (q
[6 + strspn(q
+ 6, "0123456789")])
326 "argument to `align' is not numeric");
328 unsigned int align
= atoi(q
+ 6);
329 if (!align
|| ((align
- 1) & align
))
330 error(ERR_NONFATAL
, "argument to `align' is not a"
333 error(ERR_NONFATAL
, "Win32 cannot align sections"
334 " to better than 64-byte boundaries");
336 align_and
= ~0x00F00000L
;
337 align_or
= (align
== 1 ? 0x00100000L
:
338 align
== 2 ? 0x00200000L
:
339 align
== 4 ? 0x00300000L
:
340 align
== 8 ? 0x00400000L
:
341 align
== 16 ? 0x00500000L
:
343 32 ? 0x00600000L
: 0x00700000L
);
350 for (i
= 0; i
< nsects
; i
++)
351 if (!strcmp(name
, sects
[i
]->name
))
355 if (!strcmp(name
, ".data"))
357 else if (!strcmp(name
, ".rdata"))
359 else if (!strcmp(name
, ".bss"))
361 else if (win64
&& !strcmp(name
, ".pdata"))
362 flags
= 0x40300040; /* rdata align=4 */
363 else if (win64
&& !strcmp(name
, ".xdata"))
364 flags
= 0x40400040; /* rdate align=8 */
368 i
= coff_make_section(name
, flags
);
370 sects
[i
]->flags
= flags
;
371 sects
[i
]->flags
&= align_and
;
372 sects
[i
]->flags
|= align_or
;
373 } else if (pass
== 1) {
375 error(ERR_WARNING
, "section attributes ignored on"
376 " redeclaration of section `%s'", name
);
379 return sects
[i
]->index
;
382 static void coff_deflabel(char *name
, int32_t segment
, int64_t offset
,
383 int is_global
, char *special
)
385 int pos
= strslen
+ 4;
389 error(ERR_NONFATAL
, "binary format does not support any"
390 " special symbol types");
392 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
393 if (strcmp(name
,WRT_IMAGEBASE
))
394 error(ERR_NONFATAL
, "unrecognized special symbol `%s'", name
);
398 if (strlen(name
) > 8) {
399 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
400 strslen
+= 1 + strlen(name
);
404 sym
= saa_wstruct(syms
);
407 sym
->namlen
= strlen(name
);
409 strcpy(sym
->name
, name
);
410 sym
->is_global
= !!is_global
;
411 if (segment
== NO_SEG
)
412 sym
->section
= -1; /* absolute symbol */
416 for (i
= 0; i
< nsects
; i
++)
417 if (segment
== sects
[i
]->index
) {
418 sym
->section
= i
+ 1;
422 sym
->is_global
= true;
427 sym
->value
= (sym
->section
== 0 ? 0 : offset
);
430 * define the references from external-symbol segment numbers
431 * to these symbol records.
433 if (sym
->section
== 0) {
434 bsym
= raa_write(bsym
, segment
, nsyms
);
437 if (segment
!= NO_SEG
)
438 symval
= raa_write(symval
, segment
, sym
->section
? 0 : sym
->value
);
443 static int32_t coff_add_reloc(struct Section
*sect
, int32_t segment
,
448 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
449 sect
->tail
= &r
->next
;
452 r
->address
= sect
->len
;
453 if (segment
== NO_SEG
)
454 r
->symbol
= 0, r
->symbase
= ABS_SYMBOL
;
457 r
->symbase
= REAL_SYMBOLS
;
458 for (i
= 0; i
< nsects
; i
++)
459 if (segment
== sects
[i
]->index
) {
461 r
->symbase
= SECT_SYMBOLS
;
464 if (r
->symbase
== REAL_SYMBOLS
)
465 r
->symbol
= raa_read(bsym
, segment
);
472 * Return the fixup for standard COFF common variables.
474 if (r
->symbase
== REAL_SYMBOLS
&& !(win32
| win64
))
475 return raa_read(symval
, segment
);
480 static void coff_out(int32_t segto
, const void *data
,
481 enum out_type type
, uint64_t size
,
482 int32_t segment
, int32_t wrt
)
485 uint8_t mydata
[8], *p
;
488 if (wrt
!= NO_SEG
&& !win64
) {
489 wrt
= NO_SEG
; /* continue to do _something_ */
490 error(ERR_NONFATAL
, "WRT not supported by COFF output formats");
494 * handle absolute-assembly (structure definitions)
496 if (segto
== NO_SEG
) {
497 if (type
!= OUT_RESERVE
)
498 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
504 for (i
= 0; i
< nsects
; i
++)
505 if (segto
== sects
[i
]->index
) {
510 int tempint
; /* ignored */
511 if (segto
!= coff_section_names(".text", 2, &tempint
))
512 error(ERR_PANIC
, "strange segment conditions in COFF driver");
514 s
= sects
[nsects
- 1];
517 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
518 if (win64
&& wrt
== NO_SEG
&&
519 (!strcmp(s
->name
,".pdata") || !strcmp(s
->name
,".xdata")))
520 wrt
= imagebase_sect
;
522 if (!s
->data
&& type
!= OUT_RESERVE
) {
523 error(ERR_WARNING
, "attempt to initialize memory in"
524 " BSS section `%s': ignored", s
->name
);
525 if (type
== OUT_REL2ADR
)
527 else if (type
== OUT_REL4ADR
)
533 if (type
== OUT_RESERVE
) {
535 error(ERR_WARNING
, "uninitialised space declared in"
536 " non-BSS section `%s': zeroing", s
->name
);
537 coff_sect_write(s
, NULL
, size
);
540 } else if (type
== OUT_RAWDATA
) {
541 if (segment
!= NO_SEG
)
542 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
543 coff_sect_write(s
, data
, size
);
544 } else if (type
== OUT_ADDRESS
) {
546 if (size
!= 4 && (segment
!= NO_SEG
|| wrt
!= NO_SEG
))
547 error(ERR_NONFATAL
, "COFF format does not support non-32-bit"
551 if (segment
!= NO_SEG
|| wrt
!= NO_SEG
) {
553 error(ERR_NONFATAL
, "COFF format does not support"
555 } else if (segment
% 2) {
556 error(ERR_NONFATAL
, "COFF format does not support"
557 " segment base references");
559 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_I386_DIR32
);
562 WRITELONG(p
, *(int64_t *)data
+ fix
);
563 coff_sect_write(s
, mydata
, size
);
569 if (wrt
== imagebase_sect
) {
570 error(ERR_NONFATAL
, "operand size mismatch: 'wrt "
571 WRT_IMAGEBASE
"' is a 32-bit operand");
573 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_AMD64_ADDR64
);
574 WRITEDLONG(p
, *(int64_t *)data
+ fix
);
575 coff_sect_write(s
, mydata
, size
);
577 fix
= coff_add_reloc(s
, segment
,
578 wrt
== imagebase_sect
? IMAGE_REL_AMD64_ADDR32NB
:
579 IMAGE_REL_AMD64_ADDR32
);
580 WRITELONG(p
, *(int64_t *)data
+ fix
);
581 coff_sect_write(s
, mydata
, size
);
584 } else if (type
== OUT_REL2ADR
) {
585 error(ERR_NONFATAL
, "COFF format does not support 16-bit"
587 } else if (type
== OUT_REL4ADR
) {
588 if (segment
== segto
&& !(win64
)) /* Acceptable for RIP-relative */
589 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
590 else if (segment
== NO_SEG
&& win32
)
591 error(ERR_NONFATAL
, "Win32 COFF does not correctly support"
592 " relative references to absolute addresses");
595 if (segment
!= NO_SEG
&& segment
% 2) {
596 error(ERR_NONFATAL
, "COFF format does not support"
597 " segment base references");
599 fix
= coff_add_reloc(s
, segment
,
600 win64
? IMAGE_REL_AMD64_REL32
: IMAGE_REL_I386_REL32
);
603 WRITELONG(p
, *(int64_t *)data
+ 4 - size
+ fix
);
605 WRITELONG(p
, *(int64_t *)data
- (size
+ s
->len
) + fix
);
607 coff_sect_write(s
, mydata
, 4L);
613 static void coff_sect_write(struct Section
*sect
,
614 const uint8_t *data
, uint32_t len
)
616 saa_wbytes(sect
->data
, data
, len
);
620 typedef struct tagString
{
621 struct tagString
*Next
;
626 #define EXPORT_SECTION_NAME ".drectve"
627 #define EXPORT_SECTION_FLAGS INFO_FLAGS
629 #define EXPORT_SECTION_NAME ".text"
630 #define EXPORT_SECTION_FLAGS TEXT_FLAGS
633 static STRING
*Exports
= NULL
;
634 static struct Section
*directive_sec
;
635 void AddExport(char *name
)
637 STRING
*rvp
= Exports
, *newS
;
639 newS
= (STRING
*) nasm_malloc(sizeof(STRING
));
640 newS
->len
= strlen(name
);
642 newS
->String
= (char *)nasm_malloc(newS
->len
+ 1);
643 strcpy(newS
->String
, name
);
646 for (i
= 0; i
< nsects
; i
++)
648 if (!strcmp(EXPORT_SECTION_NAME
, sects
[i
]->name
))
652 sects
[coff_make_section
653 (EXPORT_SECTION_NAME
, EXPORT_SECTION_FLAGS
)];
655 directive_sec
= sects
[i
];
659 if (!strcmp(rvp
->String
, name
))
667 void BuildExportTable(void)
669 STRING
*rvp
= Exports
, *next
;
675 len
= sprintf((char *)buf
, "-export:%s ", rvp
->String
);
676 coff_sect_write(directive_sec
, buf
, len
);
681 while ((rvp
= next
)) {
683 nasm_free(rvp
->String
);
689 static int coff_directives(char *directive
, char *value
, int pass
)
691 if (!strcmp(directive
, "export")) {
695 return 1; /* ignore in pass two */
697 while (*q
&& !isspace(*q
))
701 while (*q
&& isspace(*q
))
706 error(ERR_NONFATAL
, "`export' directive requires export name");
710 error(ERR_NONFATAL
, "unrecognized export qualifier `%s'", q
);
715 } else if (win32
&& !strcmp(directive
,"safeseh")) {
719 { for (i
= 0; i
< nsects
; i
++)
720 if (!strcmp(".sxdata",sects
[i
]->name
))
723 sxseg
= coff_make_section(".sxdata",0x200);
730 for (n
= 0; n
< nsyms
; n
++) {
731 struct Symbol
*sym
= saa_rstruct(syms
);
734 /* sym->strpos is biased by 4, because symbol
735 * table is prefixed with table length */
736 if (sym
->strpos
>=4) {
737 char *name
= nasm_malloc(sym
->namlen
+1);
738 saa_fread(strs
,sym
->strpos
-4,name
,sym
->namlen
);
739 name
[sym
->namlen
]='\0';
740 equals
= !strcmp(value
,name
);
744 equals
= !strcmp(value
,sym
->name
);
747 /* this value arithmetics effectively reflects
748 * initsym in coff_write(): 2 for file, 1 for
749 * .absolute and two per each section */
750 unsigned char value
[4],*p
=value
;
751 WRITELONG(p
,n
+ 2 + 1 + nsects
*2);
752 coff_sect_write(sects
[sxseg
],value
,4);
759 "`safeseh' directive requires valid symbol");
767 static void coff_write(void)
769 int32_t pos
, sympos
, vsize
;
772 BuildExportTable(); /* fill in the .drectve section with -export's */
775 /* add default value for @feat.00, this allows to 'link /safeseh' */
779 for (n
= 0; n
< nsyms
; n
++) {
780 struct Symbol
*sym
= saa_rstruct(syms
);
781 if (sym
->strpos
== -1 && !strcmp("@feat.00",sym
->name
))
785 coff_deflabel("@feat.00",NO_SEG
,1,0,NULL
);
789 * Work out how big the file will get. Calculate the start of
790 * the `real' symbols at the same time.
792 pos
= 0x14 + 0x28 * nsects
;
793 initsym
= 3; /* two for the file, one absolute */
794 for (i
= 0; i
< nsects
; i
++) {
795 if (sects
[i
]->data
) {
797 pos
+= sects
[i
]->len
;
798 sects
[i
]->relpos
= pos
;
799 pos
+= 10 * sects
[i
]->nrelocs
;
801 sects
[i
]->pos
= sects
[i
]->relpos
= 0L;
802 initsym
+= 2; /* two for each section */
807 * Output the COFF header.
810 fwriteint16_t(0x8664, coffp
); /* MACHINE_x86-64 */
812 fwriteint16_t(0x014C, coffp
); /* MACHINE_i386 */
813 fwriteint16_t(nsects
, coffp
); /* number of sections */
814 fwriteint32_t(time(NULL
), coffp
); /* time stamp */
815 fwriteint32_t(sympos
, coffp
);
816 fwriteint32_t(nsyms
+ initsym
, coffp
);
817 fwriteint16_t(0, coffp
); /* no optional header */
818 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
819 fwriteint16_t((win32
| win64
) ? 0 : 0x104, coffp
);
822 * Output the section headers.
825 for (i
= 0; i
< nsects
; i
++) {
826 coff_section_header(sects
[i
]->name
, vsize
, sects
[i
]->len
,
827 sects
[i
]->pos
, sects
[i
]->relpos
,
828 sects
[i
]->nrelocs
, sects
[i
]->flags
);
829 vsize
+= sects
[i
]->len
;
833 * Output the sections and their relocations.
835 for (i
= 0; i
< nsects
; i
++)
836 if (sects
[i
]->data
) {
837 saa_fpwrite(sects
[i
]->data
, coffp
);
838 coff_write_relocs(sects
[i
]);
842 * Output the symbol and string tables.
844 coff_write_symbols();
845 fwriteint32_t(strslen
+ 4, coffp
); /* length includes length count */
846 saa_fpwrite(strs
, coffp
);
849 static void coff_section_header(char *name
, int32_t vsize
,
850 int32_t datalen
, int32_t datapos
,
851 int32_t relpos
, int nrelocs
, int32_t flags
)
857 memset(padname
, 0, 8);
858 strncpy(padname
, name
, 8);
859 fwrite(padname
, 8, 1, coffp
);
860 fwriteint32_t(0, coffp
); /* Virtual size field - set to 0 or vsize */
861 fwriteint32_t(0L, coffp
); /* RVA/offset - we ignore */
862 fwriteint32_t(datalen
, coffp
);
863 fwriteint32_t(datapos
, coffp
);
864 fwriteint32_t(relpos
, coffp
);
865 fwriteint32_t(0L, coffp
); /* no line numbers - we don't do 'em */
866 fwriteint16_t(nrelocs
, coffp
);
867 fwriteint16_t(0, coffp
); /* again, no line numbers */
868 fwriteint32_t(flags
, coffp
);
871 static void coff_write_relocs(struct Section
*s
)
875 for (r
= s
->head
; r
; r
= r
->next
) {
876 fwriteint32_t(r
->address
, coffp
);
877 fwriteint32_t(r
->symbol
+ (r
->symbase
== REAL_SYMBOLS
? initsym
:
878 r
->symbase
== ABS_SYMBOL
? initsym
- 1 :
879 r
->symbase
== SECT_SYMBOLS
? 2 : 0),
881 fwriteint16_t(r
->type
, coffp
);
885 static void coff_symbol(char *name
, int32_t strpos
, int32_t value
,
886 int section
, int type
, int storageclass
, int aux
)
891 memset(padname
, 0, 8);
892 strncpy(padname
, name
, 8);
893 fwrite(padname
, 8, 1, coffp
);
895 fwriteint32_t(0L, coffp
);
896 fwriteint32_t(strpos
, coffp
);
898 fwriteint32_t(value
, coffp
);
899 fwriteint16_t(section
, coffp
);
900 fwriteint16_t(type
, coffp
);
901 fputc(storageclass
, coffp
);
905 static void coff_write_symbols(void)
911 * The `.file' record, and the file name auxiliary record.
913 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
914 memset(filename
, 0, 18);
915 strncpy(filename
, coff_infile
, 18);
916 fwrite(filename
, 18, 1, coffp
);
919 * The section records, with their auxiliaries.
921 memset(filename
, 0, 18); /* useful zeroed buffer */
923 for (i
= 0; i
< (uint32_t) nsects
; i
++) {
924 coff_symbol(sects
[i
]->name
, 0L, 0L, i
+ 1, 0, 3, 1);
925 fwriteint32_t(sects
[i
]->len
, coffp
);
926 fwriteint16_t(sects
[i
]->nrelocs
, coffp
);
927 fwrite(filename
, 12, 1, coffp
);
931 * The absolute symbol, for relative-to-absolute relocations.
933 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
939 for (i
= 0; i
< nsyms
; i
++) {
940 struct Symbol
*sym
= saa_rstruct(syms
);
941 coff_symbol(sym
->strpos
== -1 ? sym
->name
: NULL
,
942 sym
->strpos
, sym
->value
, sym
->section
,
943 sym
->type
, sym
->is_global
? 2 : 3, 0);
947 static int32_t coff_segbase(int32_t segment
)
952 static void coff_std_filename(char *inname
, char *outname
, efunc error
)
954 strcpy(coff_infile
, inname
);
955 standard_extension(inname
, outname
, ".o", error
);
958 static void coff_win32_filename(char *inname
, char *outname
, efunc error
)
960 strcpy(coff_infile
, inname
);
961 standard_extension(inname
, outname
, ".obj", error
);
964 static const char *coff_stdmac
[] = {
965 "%define __SECT__ [section .text]",
966 "%macro __NASM_CDecl__ 1",
968 "%imacro export 1+.nolist",
971 "%imacro safeseh 1.nolist",
977 static int coff_set_info(enum geninfo type
, char **val
)
983 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
987 struct ofmt of_coff
= {
988 "COFF (i386) object files (e.g. DJGPP for DOS)",
1009 struct ofmt of_win32
= {
1010 "Microsoft Win32 (i386) object files",
1023 coff_win32_filename
,
1031 struct ofmt of_win64
= {
1032 "Microsoft Win64 (x86-64) object files",
1045 coff_win32_filename
,