preproc: always reprocess smacros after %+ - pasting
[nasm/avx512.git] / output / outcoff.c
blob5a03bec560a12c8fa76c8cde8dc52ed478a19d74
1 /* ----------------------------------------------------------------------- *
2 *
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
9 * conditions are met:
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 * ----------------------------------------------------------------------- */
34 /*
35 * outcoff.c output routines for the Netwide Assembler to produce
36 * COFF object files (for DJGPP and Win32)
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <time.h>
46 #include <inttypes.h>
48 #include "nasm.h"
49 #include "nasmlib.h"
50 #include "saa.h"
51 #include "raa.h"
52 #include "output/outform.h"
53 #include "output/outlib.h"
55 #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
58 * Notes on COFF:
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"
106 struct Reloc {
107 struct Reloc *next;
108 int32_t address; /* relative to _start_ of section */
109 int32_t symbol; /* symbol number */
110 enum {
111 SECT_SYMBOLS,
112 ABS_SYMBOL,
113 REAL_SYMBOLS
114 } symbase; /* relocation for symbol number :) */
115 int16_t type;
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
127 struct Symbol {
128 char name[9];
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 */
138 static FILE *coffp;
139 static efunc error;
140 static char coff_infile[FILENAME_MAX];
142 struct Section {
143 struct SAA *data;
144 uint32_t len;
145 int nrelocs;
146 int32_t index;
147 struct Reloc *head, **tail;
148 uint32_t flags; /* section flags */
149 char name[9];
150 int32_t pos, relpos;
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;
168 static int initsym;
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 *,
177 uint32_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 */
188 (void)eval;
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;
197 maxbits = 64;
198 win32 = false; win64 = true;
199 (void)ldef; /* placate optimizers */
200 (void)eval;
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,
207 evalfunc eval)
209 win32 = win64 = false;
210 (void)ldef; /* placate optimizers */
211 (void)eval;
212 coff_gen_init(fp, errfunc);
215 static void coff_gen_init(FILE * fp, efunc errfunc)
218 coffp = fp;
219 error = errfunc;
220 sects = NULL;
221 nsects = sectlen = 0;
222 syms = saa_init(sizeof(struct Symbol));
223 nsyms = 0;
224 bsym = raa_init();
225 symval = raa_init();
226 strs = saa_init(1);
227 strslen = 0;
228 def_seg = seg_alloc();
231 static void coff_cleanup(int debuginfo)
233 struct Reloc *r;
234 int i;
236 (void)debuginfo;
238 coff_write();
239 fclose(coffp);
240 for (i = 0; i < nsects; i++) {
241 if (sects[i]->data)
242 saa_free(sects[i]->data);
243 while (sects[i]->head) {
244 r = sects[i]->head;
245 sects[i]->head = sects[i]->head->next;
246 nasm_free(r);
248 nasm_free(sects[i]);
250 nasm_free(sects);
251 saa_free(syms);
252 raa_free(bsym);
253 raa_free(symval);
254 saa_free(strs);
257 static int coff_make_section(char *name, uint32_t flags)
259 struct Section *s;
261 s = nasm_malloc(sizeof(*s));
263 if (flags != BSS_FLAGS)
264 s->data = saa_init(1);
265 else
266 s->data = NULL;
267 s->head = NULL;
268 s->tail = &s->head;
269 s->len = 0;
270 s->nrelocs = 0;
271 if (!strcmp(name, ".text"))
272 s->index = def_seg;
273 else
274 s->index = seg_alloc();
275 strncpy(s->name, name, 8);
276 s->name[8] = '\0';
277 s->flags = flags;
279 if (nsects >= sectlen) {
280 sectlen += SECT_DELTA;
281 sects = nasm_realloc(sects, sectlen * sizeof(*sects));
283 sects[nsects++] = s;
285 return nsects - 1;
288 static int32_t coff_section_names(char *name, int pass, int *bits)
290 char *p;
291 uint32_t flags, align_and = ~0L, align_or = 0L;
292 int i;
295 * Set default bits.
297 if (!name) {
298 if(win64)
299 *bits = 64;
300 else
301 *bits = 32;
304 if (!name)
305 return def_seg;
307 p = name;
308 while (*p && !nasm_isspace(*p))
309 p++;
310 if (*p)
311 *p++ = '\0';
312 if (strlen(name) > 8) {
313 error(ERR_WARNING, "COFF section names limited to 8 characters:"
314 " truncating");
315 name[8] = '\0';
317 flags = 0;
319 while (*p && nasm_isspace(*p))
320 p++;
321 while (*p) {
322 char *q = p;
323 while (*p && !nasm_isspace(*p))
324 p++;
325 if (*p)
326 *p++ = '\0';
327 while (*p && nasm_isspace(*p))
328 p++;
330 if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
331 flags = TEXT_FLAGS;
332 } else if (!nasm_stricmp(q, "data")) {
333 flags = DATA_FLAGS;
334 } else if (!nasm_stricmp(q, "rdata")) {
335 if (win32 | win64)
336 flags = RDATA_FLAGS;
337 else {
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")) {
343 flags = BSS_FLAGS;
344 } else if (!nasm_stricmp(q, "info")) {
345 if (win32 | win64)
346 flags = INFO_FLAGS;
347 else {
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");
356 else {
357 if (q[6 + strspn(q + 6, "0123456789")])
358 error(ERR_NONFATAL,
359 "argument to `align' is not numeric");
360 else {
361 unsigned int align = atoi(q + 6);
362 if (!align || ((align - 1) & align))
363 error(ERR_NONFATAL, "argument to `align' is not a"
364 " power of two");
365 else if (align > 64)
366 error(ERR_NONFATAL, "Win32 cannot align sections"
367 " to better than 64-byte boundaries");
368 else {
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 :
375 align ==
376 32 ? 0x00600000L : 0x00700000L);
383 for (i = 0; i < nsects; i++)
384 if (!strcmp(name, sects[i]->name))
385 break;
386 if (i == nsects) {
387 if (!flags) {
388 if (!strcmp(name, ".data"))
389 flags = DATA_FLAGS;
390 else if (!strcmp(name, ".rdata"))
391 flags = RDATA_FLAGS;
392 else if (!strcmp(name, ".bss"))
393 flags = BSS_FLAGS;
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 */
398 else
399 flags = TEXT_FLAGS;
401 i = coff_make_section(name, flags);
402 if (flags)
403 sects[i]->flags = flags;
404 sects[i]->flags &= align_and;
405 sects[i]->flags |= align_or;
406 } else if (pass == 1) {
407 if (flags)
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;
419 struct Symbol *sym;
421 if (special)
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);
428 return;
431 if (strlen(name) > 8) {
432 size_t nlen = strlen(name)+1;
433 saa_wbytes(strs, name, nlen);
434 strslen += nlen;
435 } else
436 pos = -1;
438 sym = saa_wstruct(syms);
440 sym->strpos = pos;
441 sym->namlen = strlen(name);
442 if (pos == -1)
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 */
448 else {
449 int i;
450 sym->section = 0;
451 for (i = 0; i < nsects; i++)
452 if (segment == sects[i]->index) {
453 sym->section = i + 1;
454 break;
456 if (!sym->section)
457 sym->is_global = true;
459 if (is_global == 2)
460 sym->value = offset;
461 else
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);
475 nsyms++;
478 static int32_t coff_add_reloc(struct Section *sect, int32_t segment,
479 int16_t type)
481 struct Reloc *r;
483 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
484 sect->tail = &r->next;
485 r->next = NULL;
487 r->address = sect->len;
488 if (segment == NO_SEG)
489 r->symbol = 0, r->symbase = ABS_SYMBOL;
490 else {
491 int i;
492 r->symbase = REAL_SYMBOLS;
493 for (i = 0; i < nsects; i++)
494 if (segment == sects[i]->index) {
495 r->symbol = i * 2;
496 r->symbase = SECT_SYMBOLS;
497 break;
499 if (r->symbase == REAL_SYMBOLS)
500 r->symbol = raa_read(bsym, segment);
502 r->type = type;
504 sect->nrelocs++;
507 * Return the fixup for standard COFF common variables.
509 if (r->symbase == REAL_SYMBOLS && !(win32 | win64))
510 return raa_read(symval, segment);
511 else
512 return 0;
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)
519 struct Section *s;
520 uint8_t mydata[8], *p;
521 int i;
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]"
534 " space");
535 return;
538 s = NULL;
539 for (i = 0; i < nsects; i++)
540 if (segto == sects[i]->index) {
541 s = sects[i];
542 break;
544 if (!s) {
545 int tempint; /* ignored */
546 if (segto != coff_section_names(".text", 2, &tempint))
547 error(ERR_PANIC, "strange segment conditions in COFF driver");
548 else
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);
561 return;
564 if (type == OUT_RESERVE) {
565 if (s->data) {
566 error(ERR_WARNING, "uninitialised space declared in"
567 " non-BSS section `%s': zeroing", s->name);
568 coff_sect_write(s, NULL, size);
569 } else
570 s->len += 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) {
576 if (!(win64)) {
577 if (size != 4 && (segment != NO_SEG || wrt != NO_SEG))
578 error(ERR_NONFATAL, "COFF format does not support non-32-bit"
579 " relocations");
580 else {
581 int32_t fix = 0;
582 if (segment != NO_SEG || wrt != NO_SEG) {
583 if (wrt != NO_SEG) {
584 error(ERR_NONFATAL, "COFF format does not support"
585 " WRT types");
586 } else if (segment % 2) {
587 error(ERR_NONFATAL, "COFF format does not support"
588 " segment base references");
589 } else
590 fix = coff_add_reloc(s, segment, IMAGE_REL_I386_DIR32);
592 p = mydata;
593 WRITELONG(p, *(int64_t *)data + fix);
594 coff_sect_write(s, mydata, size);
596 } else {
597 int32_t fix = 0;
598 p = mydata;
599 if (size == 8) {
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);
607 } else {
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"
617 " relocations");
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");
624 else {
625 int32_t fix = 0;
626 if (segment != NO_SEG && segment % 2) {
627 error(ERR_NONFATAL, "COFF format does not support"
628 " segment base references");
629 } else
630 fix = coff_add_reloc(s, segment,
631 win64 ? IMAGE_REL_AMD64_REL32 : IMAGE_REL_I386_REL32);
632 p = mydata;
633 if (win32 | win64) {
634 WRITELONG(p, *(int64_t *)data + 4 - size + fix);
635 } else {
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);
648 sect->len += len;
651 typedef struct tagString {
652 struct tagString *Next;
653 int len;
654 char *String;
655 } STRING;
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);
672 newS->Next = NULL;
673 newS->String = (char *)nasm_malloc(newS->len + 1);
674 strcpy(newS->String, name);
675 if (rvp == NULL) {
676 int i;
677 for (i = 0; i < nsects; i++)
679 if (!strcmp(EXPORT_SECTION_NAME, sects[i]->name))
680 break;
681 if (i == nsects)
682 directive_sec =
683 sects[coff_make_section
684 (EXPORT_SECTION_NAME, EXPORT_SECTION_FLAGS)];
685 else
686 directive_sec = sects[i];
687 Exports = newS;
688 } else {
689 while (rvp->Next) {
690 if (!strcmp(rvp->String, name))
691 return;
692 rvp = rvp->Next;
694 rvp->Next = newS;
698 void BuildExportTable(void)
700 STRING *rvp = Exports, *next;
701 uint8_t buf[256];
702 int len;
703 if (rvp == NULL)
704 return;
705 while (rvp) {
706 len = sprintf((char *)buf, "-export:%s ", rvp->String);
707 coff_sect_write(directive_sec, buf, len);
708 rvp = rvp->Next;
711 next = Exports;
712 while ((rvp = next)) {
713 next = rvp->Next;
714 nasm_free(rvp->String);
715 nasm_free(rvp);
717 Exports = NULL;
720 static int coff_directives(char *directive, char *value, int pass)
722 if (!strcmp(directive, "export")) {
723 char *q, *name;
725 if (pass == 2)
726 return 1; /* ignore in pass two */
727 name = q = value;
728 while (*q && !nasm_isspace(*q))
729 q++;
730 if (nasm_isspace(*q)) {
731 *q++ = '\0';
732 while (*q && nasm_isspace(*q))
733 q++;
736 if (!*name) {
737 error(ERR_NONFATAL, "`export' directive requires export name");
738 return 1;
740 if (*q) {
741 error(ERR_NONFATAL, "unrecognized export qualifier `%s'", q);
742 return 1;
744 AddExport(name);
745 return 1;
746 } else if (win32 && !strcmp(directive,"safeseh")) {
747 static int sxseg=-1;
748 int i;
749 if (sxseg==-1)
750 { for (i = 0; i < nsects; i++)
751 if (!strcmp(".sxdata",sects[i]->name))
752 break;
753 if (i == nsects)
754 sxseg = coff_make_section(".sxdata",0x200);
755 else
756 sxseg = i;
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. */
760 if (pass0 == 2) {
761 uint32_t n;
762 saa_rewind(syms);
763 for (n = 0; n < nsyms; n++) {
764 struct Symbol *sym = saa_rstruct(syms);
765 bool equals;
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);
774 nasm_free(name);
776 else
777 equals = !strcmp(value,sym->name);
779 if (equals) {
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);
786 sym->type = 0x20;
787 break;
790 if (n == nsyms) {
791 error(ERR_NONFATAL,
792 "`safeseh' directive requires valid symbol");
795 return 1;
797 return 0;
800 static void coff_write(void)
802 int32_t pos, sympos, vsize;
803 int i;
805 BuildExportTable(); /* fill in the .drectve section with -export's */
807 if (win32) {
808 /* add default value for @feat.00, this allows to 'link /safeseh' */
809 uint32_t n;
811 saa_rewind(syms);
812 for (n = 0; n < nsyms; n++) {
813 struct Symbol *sym = saa_rstruct(syms);
814 if (sym->strpos == -1 && !strcmp("@feat.00",sym->name))
815 break;
817 if (n == nsyms)
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) {
829 sects[i]->pos = pos;
830 pos += sects[i]->len;
831 sects[i]->relpos = pos;
832 pos += 10 * sects[i]->nrelocs;
833 } else
834 sects[i]->pos = sects[i]->relpos = 0L;
835 initsym += 2; /* two for each section */
837 sympos = pos;
840 * Output the COFF header.
842 if (win64)
843 fwriteint16_t(0x8664, coffp); /* MACHINE_x86-64 */
844 else
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.
857 vsize = 0L;
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)
886 char padname[8];
888 (void)vsize;
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)
906 struct Reloc *r;
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),
913 coffp);
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)
921 char padname[8];
923 if (name) {
924 memset(padname, 0, 8);
925 strncpy(padname, name, 8);
926 fwrite(padname, 8, 1, coffp);
927 } else {
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);
935 fputc(aux, coffp);
938 static void coff_write_symbols(void)
940 char filename[18];
941 uint32_t i;
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);
969 * The real symbols.
971 saa_rewind(syms);
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)
982 return 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)
1001 (void)type;
1002 (void)val;
1003 return 0;
1005 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
1007 #ifdef OF_COFF
1009 struct ofmt of_coff = {
1010 "COFF (i386) object files (e.g. DJGPP for DOS)",
1011 "coff",
1013 null_debug_arr,
1014 &null_debug_form,
1015 coff_stdmac,
1016 coff_std_init,
1017 coff_set_info,
1018 coff_out,
1019 coff_deflabel,
1020 coff_section_names,
1021 coff_segbase,
1022 coff_directives,
1023 coff_std_filename,
1024 coff_cleanup
1027 #endif
1029 #ifdef OF_WIN32
1031 struct ofmt of_win32 = {
1032 "Microsoft Win32 (i386) object files",
1033 "win32",
1035 null_debug_arr,
1036 &null_debug_form,
1037 coff_stdmac,
1038 coff_win32_init,
1039 coff_set_info,
1040 coff_out,
1041 coff_deflabel,
1042 coff_section_names,
1043 coff_segbase,
1044 coff_directives,
1045 coff_win32_filename,
1046 coff_cleanup
1049 #endif
1051 #ifdef OF_WIN64
1053 struct ofmt of_win64 = {
1054 "Microsoft Win64 (x86-64) object files",
1055 "win64",
1057 null_debug_arr,
1058 &null_debug_form,
1059 coff_stdmac,
1060 coff_win64_init,
1061 coff_set_info,
1062 coff_out,
1063 coff_deflabel,
1064 coff_section_names,
1065 coff_segbase,
1066 coff_directives,
1067 coff_win32_filename,
1068 coff_cleanup
1071 #endif