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.
25 #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
30 * (0) When I say `standard COFF' below, I mean `COFF as output and
31 * used by DJGPP'. I assume DJGPP gets it right.
33 * (1) Win32 appears to interpret the term `relative relocation'
34 * differently from standard COFF. Standard COFF understands a
35 * relative relocation to mean that during relocation you add the
36 * address of the symbol you're referencing, and subtract the base
37 * address of the section you're in. Win32 COFF, by contrast, seems
38 * to add the address of the symbol and then subtract the address
39 * of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
40 * subtly incompatible.
42 * (2) Win32 doesn't bother putting any flags in the header flags
43 * field (at offset 0x12 into the file).
45 * (3) Win32 uses some extra flags into the section header table:
46 * it defines flags 0x80000000 (writable), 0x40000000 (readable)
47 * and 0x20000000 (executable), and uses them in the expected
48 * combinations. It also defines 0x00100000 through 0x00700000 for
49 * section alignments of 1 through 64 bytes.
51 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
52 * field directly after the section name in the section header
53 * table for something strange: they store what the address of the
54 * section start point _would_ be, if you laid all the sections end
55 * to end starting at zero. Dunno why. Microsoft's documentation
56 * lists this field as "Virtual Size of Section", which doesn't
57 * seem to fit at all. In fact, Win32 even includes non-linked
58 * sections such as .drectve in this calculation.
60 * Newer versions of MASM seem to have changed this to be zero, and
61 * that apparently matches the COFF spec, so go with that.
63 * (5) Standard COFF does something very strange to common
64 * variables: the relocation point for a common variable is as far
65 * _before_ the variable as its size stretches out _after_ it. So
66 * we must fix up common variable references. Win32 seems to be
67 * sensible on this one.
70 /* Flag which version of COFF we are currently outputting. */
71 static bool win32
, win64
;
73 static int32_t imagebase_sect
;
74 #define WRT_IMAGEBASE "..imagebase"
78 int32_t address
; /* relative to _start_ of section */
79 int32_t symbol
; /* symbol number */
84 } symbase
; /* relocation for symbol number :) */
88 /* possible values for Reloc->type */
89 #define IMAGE_REL_AMD64_ADDR64 0x0001
90 #define IMAGE_REL_AMD64_ADDR32 0x0002
91 #define IMAGE_REL_AMD64_ADDR32NB 0x0003
92 #define IMAGE_REL_AMD64_REL32 0x0004
93 #define IMAGE_REL_I386_DIR32 0x0006
94 #define IMAGE_REL_I386_DIR32NB 0x0007
95 #define IMAGE_REL_I386_REL32 0x0014
99 int32_t strpos
; /* string table position of name */
100 int32_t value
; /* address, or COMMON variable size */
101 int section
; /* section number where it's defined
102 * - in COFF codes, not NASM codes */
103 bool is_global
; /* is it a global symbol or not? */
104 int16_t type
; /* 0 - notype, 0x20 - function */
105 int32_t namlen
; /* full name length */
110 static char coff_infile
[FILENAME_MAX
];
117 struct Reloc
*head
, **tail
;
118 uint32_t flags
; /* section flags */
123 #define TEXT_FLAGS ((win32 | win64) ? 0x60500020L : 0x20L)
124 #define DATA_FLAGS ((win32 | win64) ? 0xC0300040L : 0x40L)
125 #define BSS_FLAGS ((win32 | win64) ? 0xC0300080L : 0x80L)
126 #define INFO_FLAGS 0x00100A00L
127 #define RDATA_FLAGS ((win32 | win64) ? 0x40400040L : 0x40L)
129 #define SECT_DELTA 32
130 static struct Section
**sects
;
131 static int nsects
, sectlen
;
133 static struct SAA
*syms
;
134 static uint32_t nsyms
;
136 static int32_t def_seg
;
140 static struct RAA
*bsym
, *symval
;
142 static struct SAA
*strs
;
143 static uint32_t strslen
;
145 static void coff_gen_init(FILE *, efunc
);
146 static void coff_sect_write(struct Section
*, const uint8_t *,
148 static void coff_write(void);
149 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
150 static void coff_write_relocs(struct Section
*);
151 static void coff_write_symbols(void);
153 static void coff_win32_init(FILE * fp
, efunc errfunc
,
154 ldfunc ldef
, evalfunc eval
)
156 win32
= true; win64
= false;
157 (void)ldef
; /* placate optimizers */
159 coff_gen_init(fp
, errfunc
);
162 static void coff_win64_init(FILE * fp
, efunc errfunc
,
163 ldfunc ldef
, evalfunc eval
)
165 extern struct ofmt of_win64
;
168 win32
= false; win64
= true;
169 (void)ldef
; /* placate optimizers */
171 coff_gen_init(fp
, errfunc
);
172 imagebase_sect
= seg_alloc()+1;
173 ldef(WRT_IMAGEBASE
,imagebase_sect
,0,NULL
,false,false,&of_win64
,errfunc
);
176 static void coff_std_init(FILE * fp
, efunc errfunc
, ldfunc ldef
,
179 win32
= win64
= false;
180 (void)ldef
; /* placate optimizers */
182 coff_gen_init(fp
, errfunc
);
185 static void coff_gen_init(FILE * fp
, efunc errfunc
)
191 nsects
= sectlen
= 0;
192 syms
= saa_init((int32_t)sizeof(struct Symbol
));
198 def_seg
= seg_alloc();
201 static void coff_cleanup(int debuginfo
)
210 for (i
= 0; i
< nsects
; i
++) {
212 saa_free(sects
[i
]->data
);
213 while (sects
[i
]->head
) {
215 sects
[i
]->head
= sects
[i
]->head
->next
;
227 static int coff_make_section(char *name
, uint32_t flags
)
231 s
= nasm_malloc(sizeof(*s
));
233 if (flags
!= BSS_FLAGS
)
234 s
->data
= saa_init(1L);
241 if (!strcmp(name
, ".text"))
244 s
->index
= seg_alloc();
245 strncpy(s
->name
, name
, 8);
249 if (nsects
>= sectlen
)
251 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
257 static int32_t coff_section_names(char *name
, int pass
, int *bits
)
260 uint32_t flags
, align_and
= ~0L, align_or
= 0L;
277 while (*p
&& !nasm_isspace(*p
))
281 if (strlen(name
) > 8) {
282 error(ERR_WARNING
, "COFF section names limited to 8 characters:"
288 while (*p
&& nasm_isspace(*p
))
292 while (*p
&& !nasm_isspace(*p
))
296 while (*p
&& nasm_isspace(*p
))
299 if (!nasm_stricmp(q
, "code") || !nasm_stricmp(q
, "text")) {
301 } else if (!nasm_stricmp(q
, "data")) {
303 } else if (!nasm_stricmp(q
, "rdata")) {
307 flags
= DATA_FLAGS
; /* gotta do something */
308 error(ERR_NONFATAL
, "standard COFF does not support"
309 " read-only data sections");
311 } else if (!nasm_stricmp(q
, "bss")) {
313 } else if (!nasm_stricmp(q
, "info")) {
317 flags
= DATA_FLAGS
; /* gotta do something */
318 error(ERR_NONFATAL
, "standard COFF does not support"
319 " informational sections");
321 } else if (!nasm_strnicmp(q
, "align=", 6)) {
322 if (!(win32
| win64
))
323 error(ERR_NONFATAL
, "standard COFF does not support"
324 " section alignment specification");
326 if (q
[6 + strspn(q
+ 6, "0123456789")])
328 "argument to `align' is not numeric");
330 unsigned int align
= atoi(q
+ 6);
331 if (!align
|| ((align
- 1) & align
))
332 error(ERR_NONFATAL
, "argument to `align' is not a"
335 error(ERR_NONFATAL
, "Win32 cannot align sections"
336 " to better than 64-byte boundaries");
338 align_and
= ~0x00F00000L
;
339 align_or
= (align
== 1 ? 0x00100000L
:
340 align
== 2 ? 0x00200000L
:
341 align
== 4 ? 0x00300000L
:
342 align
== 8 ? 0x00400000L
:
343 align
== 16 ? 0x00500000L
:
345 32 ? 0x00600000L
: 0x00700000L
);
352 for (i
= 0; i
< nsects
; i
++)
353 if (!strcmp(name
, sects
[i
]->name
))
357 if (!strcmp(name
, ".data"))
359 else if (!strcmp(name
, ".rdata"))
361 else if (!strcmp(name
, ".bss"))
363 else if (win64
&& !strcmp(name
, ".pdata"))
364 flags
= 0x40300040; /* rdata align=4 */
365 else if (win64
&& !strcmp(name
, ".xdata"))
366 flags
= 0x40400040; /* rdate align=8 */
370 i
= coff_make_section(name
, flags
);
372 sects
[i
]->flags
= flags
;
373 sects
[i
]->flags
&= align_and
;
374 sects
[i
]->flags
|= align_or
;
375 } else if (pass
== 1) {
377 error(ERR_WARNING
, "section attributes ignored on"
378 " redeclaration of section `%s'", name
);
381 return sects
[i
]->index
;
384 static void coff_deflabel(char *name
, int32_t segment
, int64_t offset
,
385 int is_global
, char *special
)
387 int pos
= strslen
+ 4;
391 error(ERR_NONFATAL
, "binary format does not support any"
392 " special symbol types");
394 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
395 if (strcmp(name
,WRT_IMAGEBASE
))
396 error(ERR_NONFATAL
, "unrecognized special symbol `%s'", name
);
400 if (strlen(name
) > 8) {
401 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
402 strslen
+= 1 + strlen(name
);
406 sym
= saa_wstruct(syms
);
409 sym
->namlen
= strlen(name
);
411 strcpy(sym
->name
, name
);
412 sym
->is_global
= !!is_global
;
413 if (segment
== NO_SEG
)
414 sym
->section
= -1; /* absolute symbol */
418 for (i
= 0; i
< nsects
; i
++)
419 if (segment
== sects
[i
]->index
) {
420 sym
->section
= i
+ 1;
424 sym
->is_global
= true;
429 sym
->value
= (sym
->section
== 0 ? 0 : offset
);
432 * define the references from external-symbol segment numbers
433 * to these symbol records.
435 if (sym
->section
== 0) {
436 bsym
= raa_write(bsym
, segment
, nsyms
);
439 if (segment
!= NO_SEG
)
440 symval
= raa_write(symval
, segment
, sym
->section
? 0 : sym
->value
);
445 static int32_t coff_add_reloc(struct Section
*sect
, int32_t segment
,
450 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
451 sect
->tail
= &r
->next
;
454 r
->address
= sect
->len
;
455 if (segment
== NO_SEG
)
456 r
->symbol
= 0, r
->symbase
= ABS_SYMBOL
;
459 r
->symbase
= REAL_SYMBOLS
;
460 for (i
= 0; i
< nsects
; i
++)
461 if (segment
== sects
[i
]->index
) {
463 r
->symbase
= SECT_SYMBOLS
;
466 if (r
->symbase
== REAL_SYMBOLS
)
467 r
->symbol
= raa_read(bsym
, segment
);
474 * Return the fixup for standard COFF common variables.
476 if (r
->symbase
== REAL_SYMBOLS
&& !(win32
| win64
))
477 return raa_read(symval
, segment
);
482 static void coff_out(int32_t segto
, const void *data
,
483 enum out_type type
, uint64_t size
,
484 int32_t segment
, int32_t wrt
)
487 uint8_t mydata
[8], *p
;
490 if (wrt
!= NO_SEG
&& !win64
) {
491 wrt
= NO_SEG
; /* continue to do _something_ */
492 error(ERR_NONFATAL
, "WRT not supported by COFF output formats");
496 * handle absolute-assembly (structure definitions)
498 if (segto
== NO_SEG
) {
499 if (type
!= OUT_RESERVE
)
500 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
506 for (i
= 0; i
< nsects
; i
++)
507 if (segto
== sects
[i
]->index
) {
512 int tempint
; /* ignored */
513 if (segto
!= coff_section_names(".text", 2, &tempint
))
514 error(ERR_PANIC
, "strange segment conditions in COFF driver");
516 s
= sects
[nsects
- 1];
519 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
520 if (win64
&& wrt
== NO_SEG
&&
521 (!strcmp(s
->name
,".pdata") || !strcmp(s
->name
,".xdata")))
522 wrt
= imagebase_sect
;
524 if (!s
->data
&& type
!= OUT_RESERVE
) {
525 error(ERR_WARNING
, "attempt to initialize memory in"
526 " BSS section `%s': ignored", s
->name
);
527 if (type
== OUT_REL2ADR
)
529 else if (type
== OUT_REL4ADR
)
535 if (type
== OUT_RESERVE
) {
537 error(ERR_WARNING
, "uninitialised space declared in"
538 " non-BSS section `%s': zeroing", s
->name
);
539 coff_sect_write(s
, NULL
, size
);
542 } else if (type
== OUT_RAWDATA
) {
543 if (segment
!= NO_SEG
)
544 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
545 coff_sect_write(s
, data
, size
);
546 } else if (type
== OUT_ADDRESS
) {
548 if (size
!= 4 && (segment
!= NO_SEG
|| wrt
!= NO_SEG
))
549 error(ERR_NONFATAL
, "COFF format does not support non-32-bit"
553 if (segment
!= NO_SEG
|| wrt
!= NO_SEG
) {
555 error(ERR_NONFATAL
, "COFF format does not support"
557 } else if (segment
% 2) {
558 error(ERR_NONFATAL
, "COFF format does not support"
559 " segment base references");
561 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_I386_DIR32
);
564 WRITELONG(p
, *(int64_t *)data
+ fix
);
565 coff_sect_write(s
, mydata
, size
);
571 if (wrt
== imagebase_sect
) {
572 error(ERR_NONFATAL
, "operand size mismatch: 'wrt "
573 WRT_IMAGEBASE
"' is a 32-bit operand");
575 fix
= coff_add_reloc(s
, segment
, IMAGE_REL_AMD64_ADDR64
);
576 WRITEDLONG(p
, *(int64_t *)data
+ fix
);
577 coff_sect_write(s
, mydata
, size
);
579 fix
= coff_add_reloc(s
, segment
,
580 wrt
== imagebase_sect
? IMAGE_REL_AMD64_ADDR32NB
:
581 IMAGE_REL_AMD64_ADDR32
);
582 WRITELONG(p
, *(int64_t *)data
+ fix
);
583 coff_sect_write(s
, mydata
, size
);
586 } else if (type
== OUT_REL2ADR
) {
587 error(ERR_NONFATAL
, "COFF format does not support 16-bit"
589 } else if (type
== OUT_REL4ADR
) {
590 if (segment
== segto
&& !(win64
)) /* Acceptable for RIP-relative */
591 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
592 else if (segment
== NO_SEG
&& win32
)
593 error(ERR_NONFATAL
, "Win32 COFF does not correctly support"
594 " relative references to absolute addresses");
597 if (segment
!= NO_SEG
&& segment
% 2) {
598 error(ERR_NONFATAL
, "COFF format does not support"
599 " segment base references");
601 fix
= coff_add_reloc(s
, segment
,
602 win64
? IMAGE_REL_AMD64_REL32
: IMAGE_REL_I386_REL32
);
605 WRITELONG(p
, *(int64_t *)data
+ 4 - size
+ fix
);
607 WRITELONG(p
, *(int64_t *)data
- (size
+ s
->len
) + fix
);
609 coff_sect_write(s
, mydata
, 4L);
615 static void coff_sect_write(struct Section
*sect
,
616 const uint8_t *data
, uint32_t len
)
618 saa_wbytes(sect
->data
, data
, len
);
622 typedef struct tagString
{
623 struct tagString
*Next
;
628 #define EXPORT_SECTION_NAME ".drectve"
629 #define EXPORT_SECTION_FLAGS INFO_FLAGS
631 #define EXPORT_SECTION_NAME ".text"
632 #define EXPORT_SECTION_FLAGS TEXT_FLAGS
635 static STRING
*Exports
= NULL
;
636 static struct Section
*directive_sec
;
637 void AddExport(char *name
)
639 STRING
*rvp
= Exports
, *newS
;
641 newS
= (STRING
*) nasm_malloc(sizeof(STRING
));
642 newS
->len
= strlen(name
);
644 newS
->String
= (char *)nasm_malloc(newS
->len
+ 1);
645 strcpy(newS
->String
, name
);
648 for (i
= 0; i
< nsects
; i
++)
650 if (!strcmp(EXPORT_SECTION_NAME
, sects
[i
]->name
))
654 sects
[coff_make_section
655 (EXPORT_SECTION_NAME
, EXPORT_SECTION_FLAGS
)];
657 directive_sec
= sects
[i
];
661 if (!strcmp(rvp
->String
, name
))
669 void BuildExportTable(void)
671 STRING
*rvp
= Exports
, *next
;
677 len
= sprintf((char *)buf
, "-export:%s ", rvp
->String
);
678 coff_sect_write(directive_sec
, buf
, len
);
683 while ((rvp
= next
)) {
685 nasm_free(rvp
->String
);
691 static int coff_directives(char *directive
, char *value
, int pass
)
693 if (!strcmp(directive
, "export")) {
697 return 1; /* ignore in pass two */
699 while (*q
&& !nasm_isspace(*q
))
701 if (nasm_isspace(*q
)) {
703 while (*q
&& nasm_isspace(*q
))
708 error(ERR_NONFATAL
, "`export' directive requires export name");
712 error(ERR_NONFATAL
, "unrecognized export qualifier `%s'", q
);
717 } else if (win32
&& !strcmp(directive
,"safeseh")) {
721 { for (i
= 0; i
< nsects
; i
++)
722 if (!strcmp(".sxdata",sects
[i
]->name
))
725 sxseg
= coff_make_section(".sxdata",0x200);
732 for (n
= 0; n
< nsyms
; n
++) {
733 struct Symbol
*sym
= saa_rstruct(syms
);
736 /* sym->strpos is biased by 4, because symbol
737 * table is prefixed with table length */
738 if (sym
->strpos
>=4) {
739 char *name
= nasm_malloc(sym
->namlen
+1);
740 saa_fread(strs
,sym
->strpos
-4,name
,sym
->namlen
);
741 name
[sym
->namlen
]='\0';
742 equals
= !strcmp(value
,name
);
746 equals
= !strcmp(value
,sym
->name
);
749 /* this value arithmetics effectively reflects
750 * initsym in coff_write(): 2 for file, 1 for
751 * .absolute and two per each section */
752 unsigned char value
[4],*p
=value
;
753 WRITELONG(p
,n
+ 2 + 1 + nsects
*2);
754 coff_sect_write(sects
[sxseg
],value
,4);
761 "`safeseh' directive requires valid symbol");
769 static void coff_write(void)
771 int32_t pos
, sympos
, vsize
;
774 BuildExportTable(); /* fill in the .drectve section with -export's */
777 /* add default value for @feat.00, this allows to 'link /safeseh' */
781 for (n
= 0; n
< nsyms
; n
++) {
782 struct Symbol
*sym
= saa_rstruct(syms
);
783 if (sym
->strpos
== -1 && !strcmp("@feat.00",sym
->name
))
787 coff_deflabel("@feat.00",NO_SEG
,1,0,NULL
);
791 * Work out how big the file will get. Calculate the start of
792 * the `real' symbols at the same time.
794 pos
= 0x14 + 0x28 * nsects
;
795 initsym
= 3; /* two for the file, one absolute */
796 for (i
= 0; i
< nsects
; i
++) {
797 if (sects
[i
]->data
) {
799 pos
+= sects
[i
]->len
;
800 sects
[i
]->relpos
= pos
;
801 pos
+= 10 * sects
[i
]->nrelocs
;
803 sects
[i
]->pos
= sects
[i
]->relpos
= 0L;
804 initsym
+= 2; /* two for each section */
809 * Output the COFF header.
812 fwriteint16_t(0x8664, coffp
); /* MACHINE_x86-64 */
814 fwriteint16_t(0x014C, coffp
); /* MACHINE_i386 */
815 fwriteint16_t(nsects
, coffp
); /* number of sections */
816 fwriteint32_t(time(NULL
), coffp
); /* time stamp */
817 fwriteint32_t(sympos
, coffp
);
818 fwriteint32_t(nsyms
+ initsym
, coffp
);
819 fwriteint16_t(0, coffp
); /* no optional header */
820 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
821 fwriteint16_t((win32
| win64
) ? 0 : 0x104, coffp
);
824 * Output the section headers.
827 for (i
= 0; i
< nsects
; i
++) {
828 coff_section_header(sects
[i
]->name
, vsize
, sects
[i
]->len
,
829 sects
[i
]->pos
, sects
[i
]->relpos
,
830 sects
[i
]->nrelocs
, sects
[i
]->flags
);
831 vsize
+= sects
[i
]->len
;
835 * Output the sections and their relocations.
837 for (i
= 0; i
< nsects
; i
++)
838 if (sects
[i
]->data
) {
839 saa_fpwrite(sects
[i
]->data
, coffp
);
840 coff_write_relocs(sects
[i
]);
844 * Output the symbol and string tables.
846 coff_write_symbols();
847 fwriteint32_t(strslen
+ 4, coffp
); /* length includes length count */
848 saa_fpwrite(strs
, coffp
);
851 static void coff_section_header(char *name
, int32_t vsize
,
852 int32_t datalen
, int32_t datapos
,
853 int32_t relpos
, int nrelocs
, int32_t flags
)
859 memset(padname
, 0, 8);
860 strncpy(padname
, name
, 8);
861 fwrite(padname
, 8, 1, coffp
);
862 fwriteint32_t(0, coffp
); /* Virtual size field - set to 0 or vsize */
863 fwriteint32_t(0L, coffp
); /* RVA/offset - we ignore */
864 fwriteint32_t(datalen
, coffp
);
865 fwriteint32_t(datapos
, coffp
);
866 fwriteint32_t(relpos
, coffp
);
867 fwriteint32_t(0L, coffp
); /* no line numbers - we don't do 'em */
868 fwriteint16_t(nrelocs
, coffp
);
869 fwriteint16_t(0, coffp
); /* again, no line numbers */
870 fwriteint32_t(flags
, coffp
);
873 static void coff_write_relocs(struct Section
*s
)
877 for (r
= s
->head
; r
; r
= r
->next
) {
878 fwriteint32_t(r
->address
, coffp
);
879 fwriteint32_t(r
->symbol
+ (r
->symbase
== REAL_SYMBOLS
? initsym
:
880 r
->symbase
== ABS_SYMBOL
? initsym
- 1 :
881 r
->symbase
== SECT_SYMBOLS
? 2 : 0),
883 fwriteint16_t(r
->type
, coffp
);
887 static void coff_symbol(char *name
, int32_t strpos
, int32_t value
,
888 int section
, int type
, int storageclass
, int aux
)
893 memset(padname
, 0, 8);
894 strncpy(padname
, name
, 8);
895 fwrite(padname
, 8, 1, coffp
);
897 fwriteint32_t(0L, coffp
);
898 fwriteint32_t(strpos
, coffp
);
900 fwriteint32_t(value
, coffp
);
901 fwriteint16_t(section
, coffp
);
902 fwriteint16_t(type
, coffp
);
903 fputc(storageclass
, coffp
);
907 static void coff_write_symbols(void)
913 * The `.file' record, and the file name auxiliary record.
915 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
916 memset(filename
, 0, 18);
917 strncpy(filename
, coff_infile
, 18);
918 fwrite(filename
, 18, 1, coffp
);
921 * The section records, with their auxiliaries.
923 memset(filename
, 0, 18); /* useful zeroed buffer */
925 for (i
= 0; i
< (uint32_t) nsects
; i
++) {
926 coff_symbol(sects
[i
]->name
, 0L, 0L, i
+ 1, 0, 3, 1);
927 fwriteint32_t(sects
[i
]->len
, coffp
);
928 fwriteint16_t(sects
[i
]->nrelocs
, coffp
);
929 fwrite(filename
, 12, 1, coffp
);
933 * The absolute symbol, for relative-to-absolute relocations.
935 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
941 for (i
= 0; i
< nsyms
; i
++) {
942 struct Symbol
*sym
= saa_rstruct(syms
);
943 coff_symbol(sym
->strpos
== -1 ? sym
->name
: NULL
,
944 sym
->strpos
, sym
->value
, sym
->section
,
945 sym
->type
, sym
->is_global
? 2 : 3, 0);
949 static int32_t coff_segbase(int32_t segment
)
954 static void coff_std_filename(char *inname
, char *outname
, efunc error
)
956 strcpy(coff_infile
, inname
);
957 standard_extension(inname
, outname
, ".o", error
);
960 static void coff_win32_filename(char *inname
, char *outname
, efunc error
)
962 strcpy(coff_infile
, inname
);
963 standard_extension(inname
, outname
, ".obj", error
);
966 extern macros_t coff_stdmac
[];
968 static int coff_set_info(enum geninfo type
, char **val
)
974 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
978 struct ofmt of_coff
= {
979 "COFF (i386) object files (e.g. DJGPP for DOS)",
1000 struct ofmt of_win32
= {
1001 "Microsoft Win32 (i386) object files",
1014 coff_win32_filename
,
1022 struct ofmt of_win64
= {
1023 "Microsoft Win64 (x86-64) object files",
1036 coff_win32_filename
,