Makefile/dependency updates
[nasm/avx512.git] / output / outcoff.c
blob4655e21f55d0ac05fa019d0a3d60ba6f05ecf5e7
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.
8 */
10 #include "compiler.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <time.h>
17 #include <inttypes.h>
19 #include "nasm.h"
20 #include "nasmlib.h"
21 #include "outform.h"
23 #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
26 * Notes on COFF:
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"
74 struct Reloc {
75 struct Reloc *next;
76 int32_t address; /* relative to _start_ of section */
77 int32_t symbol; /* symbol number */
78 enum {
79 SECT_SYMBOLS,
80 ABS_SYMBOL,
81 REAL_SYMBOLS
82 } symbase; /* relocation for symbol number :) */
83 int16_t type;
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
95 struct Symbol {
96 char name[9];
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 */
106 static FILE *coffp;
107 static efunc error;
108 static char coff_infile[FILENAME_MAX];
110 struct Section {
111 struct SAA *data;
112 uint32_t len;
113 int nrelocs;
114 int32_t index;
115 struct Reloc *head, **tail;
116 uint32_t flags; /* section flags */
117 char name[9];
118 int32_t pos, relpos;
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;
136 static int initsym;
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 *,
145 uint32_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 */
156 (void)eval;
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;
165 maxbits = 64;
166 win32 = false; win64 = true;
167 (void)ldef; /* placate optimizers */
168 (void)eval;
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,
175 evalfunc eval)
177 win32 = win64 = false;
178 (void)ldef; /* placate optimizers */
179 (void)eval;
180 coff_gen_init(fp, errfunc);
183 static void coff_gen_init(FILE * fp, efunc errfunc)
186 coffp = fp;
187 error = errfunc;
188 sects = NULL;
189 nsects = sectlen = 0;
190 syms = saa_init((int32_t)sizeof(struct Symbol));
191 nsyms = 0;
192 bsym = raa_init();
193 symval = raa_init();
194 strs = saa_init(1L);
195 strslen = 0;
196 def_seg = seg_alloc();
199 static void coff_cleanup(int debuginfo)
201 struct Reloc *r;
202 int i;
204 (void)debuginfo;
206 coff_write();
207 fclose(coffp);
208 for (i = 0; i < nsects; i++) {
209 if (sects[i]->data)
210 saa_free(sects[i]->data);
211 while (sects[i]->head) {
212 r = sects[i]->head;
213 sects[i]->head = sects[i]->head->next;
214 nasm_free(r);
216 nasm_free(sects[i]);
218 nasm_free(sects);
219 saa_free(syms);
220 raa_free(bsym);
221 raa_free(symval);
222 saa_free(strs);
225 static int coff_make_section(char *name, uint32_t flags)
227 struct Section *s;
229 s = nasm_malloc(sizeof(*s));
231 if (flags != BSS_FLAGS)
232 s->data = saa_init(1L);
233 else
234 s->data = NULL;
235 s->head = NULL;
236 s->tail = &s->head;
237 s->len = 0;
238 s->nrelocs = 0;
239 if (!strcmp(name, ".text"))
240 s->index = def_seg;
241 else
242 s->index = seg_alloc();
243 strncpy(s->name, name, 8);
244 s->name[8] = '\0';
245 s->flags = flags;
247 if (nsects >= sectlen)
248 sects =
249 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
250 sects[nsects++] = s;
252 return nsects - 1;
255 static int32_t coff_section_names(char *name, int pass, int *bits)
257 char *p;
258 uint32_t flags, align_and = ~0L, align_or = 0L;
259 int i;
262 * Set default bits.
264 if (!name) {
265 if(win64)
266 *bits = 64;
267 else
268 *bits = 32;
271 if (!name)
272 return def_seg;
274 p = name;
275 while (*p && !isspace(*p))
276 p++;
277 if (*p)
278 *p++ = '\0';
279 if (strlen(name) > 8) {
280 error(ERR_WARNING, "COFF section names limited to 8 characters:"
281 " truncating");
282 name[8] = '\0';
284 flags = 0;
286 while (*p && isspace(*p))
287 p++;
288 while (*p) {
289 char *q = p;
290 while (*p && !isspace(*p))
291 p++;
292 if (*p)
293 *p++ = '\0';
294 while (*p && isspace(*p))
295 p++;
297 if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
298 flags = TEXT_FLAGS;
299 } else if (!nasm_stricmp(q, "data")) {
300 flags = DATA_FLAGS;
301 } else if (!nasm_stricmp(q, "rdata")) {
302 if (win32 | win64)
303 flags = RDATA_FLAGS;
304 else {
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")) {
310 flags = BSS_FLAGS;
311 } else if (!nasm_stricmp(q, "info")) {
312 if (win32 | win64)
313 flags = INFO_FLAGS;
314 else {
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");
323 else {
324 if (q[6 + strspn(q + 6, "0123456789")])
325 error(ERR_NONFATAL,
326 "argument to `align' is not numeric");
327 else {
328 unsigned int align = atoi(q + 6);
329 if (!align || ((align - 1) & align))
330 error(ERR_NONFATAL, "argument to `align' is not a"
331 " power of two");
332 else if (align > 64)
333 error(ERR_NONFATAL, "Win32 cannot align sections"
334 " to better than 64-byte boundaries");
335 else {
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 :
342 align ==
343 32 ? 0x00600000L : 0x00700000L);
350 for (i = 0; i < nsects; i++)
351 if (!strcmp(name, sects[i]->name))
352 break;
353 if (i == nsects) {
354 if (!flags) {
355 if (!strcmp(name, ".data"))
356 flags = DATA_FLAGS;
357 else if (!strcmp(name, ".rdata"))
358 flags = RDATA_FLAGS;
359 else if (!strcmp(name, ".bss"))
360 flags = BSS_FLAGS;
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 */
365 else
366 flags = TEXT_FLAGS;
368 i = coff_make_section(name, flags);
369 if (flags)
370 sects[i]->flags = flags;
371 sects[i]->flags &= align_and;
372 sects[i]->flags |= align_or;
373 } else if (pass == 1) {
374 if (flags)
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;
386 struct Symbol *sym;
388 if (special)
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);
395 return;
398 if (strlen(name) > 8) {
399 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
400 strslen += 1 + strlen(name);
401 } else
402 pos = -1;
404 sym = saa_wstruct(syms);
406 sym->strpos = pos;
407 sym->namlen = strlen(name);
408 if (pos == -1)
409 strcpy(sym->name, name);
410 sym->is_global = !!is_global;
411 if (segment == NO_SEG)
412 sym->section = -1; /* absolute symbol */
413 else {
414 int i;
415 sym->section = 0;
416 for (i = 0; i < nsects; i++)
417 if (segment == sects[i]->index) {
418 sym->section = i + 1;
419 break;
421 if (!sym->section)
422 sym->is_global = true;
424 if (is_global == 2)
425 sym->value = offset;
426 else
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);
440 nsyms++;
443 static int32_t coff_add_reloc(struct Section *sect, int32_t segment,
444 int16_t type)
446 struct Reloc *r;
448 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
449 sect->tail = &r->next;
450 r->next = NULL;
452 r->address = sect->len;
453 if (segment == NO_SEG)
454 r->symbol = 0, r->symbase = ABS_SYMBOL;
455 else {
456 int i;
457 r->symbase = REAL_SYMBOLS;
458 for (i = 0; i < nsects; i++)
459 if (segment == sects[i]->index) {
460 r->symbol = i * 2;
461 r->symbase = SECT_SYMBOLS;
462 break;
464 if (r->symbase == REAL_SYMBOLS)
465 r->symbol = raa_read(bsym, segment);
467 r->type = type;
469 sect->nrelocs++;
472 * Return the fixup for standard COFF common variables.
474 if (r->symbase == REAL_SYMBOLS && !(win32 | win64))
475 return raa_read(symval, segment);
476 else
477 return 0;
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)
484 struct Section *s;
485 uint8_t mydata[8], *p;
486 int i;
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]"
499 " space");
500 return;
503 s = NULL;
504 for (i = 0; i < nsects; i++)
505 if (segto == sects[i]->index) {
506 s = sects[i];
507 break;
509 if (!s) {
510 int tempint; /* ignored */
511 if (segto != coff_section_names(".text", 2, &tempint))
512 error(ERR_PANIC, "strange segment conditions in COFF driver");
513 else
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)
526 size = 2;
527 else if (type == OUT_REL4ADR)
528 size = 4;
529 s->len += size;
530 return;
533 if (type == OUT_RESERVE) {
534 if (s->data) {
535 error(ERR_WARNING, "uninitialised space declared in"
536 " non-BSS section `%s': zeroing", s->name);
537 coff_sect_write(s, NULL, size);
538 } else
539 s->len += 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) {
545 if (!(win64)) {
546 if (size != 4 && (segment != NO_SEG || wrt != NO_SEG))
547 error(ERR_NONFATAL, "COFF format does not support non-32-bit"
548 " relocations");
549 else {
550 int32_t fix = 0;
551 if (segment != NO_SEG || wrt != NO_SEG) {
552 if (wrt != NO_SEG) {
553 error(ERR_NONFATAL, "COFF format does not support"
554 " WRT types");
555 } else if (segment % 2) {
556 error(ERR_NONFATAL, "COFF format does not support"
557 " segment base references");
558 } else
559 fix = coff_add_reloc(s, segment, IMAGE_REL_I386_DIR32);
561 p = mydata;
562 WRITELONG(p, *(int64_t *)data + fix);
563 coff_sect_write(s, mydata, size);
565 } else {
566 int32_t fix = 0;
567 p = mydata;
568 if (size == 8) {
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);
576 } else {
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"
586 " relocations");
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");
593 else {
594 int32_t fix = 0;
595 if (segment != NO_SEG && segment % 2) {
596 error(ERR_NONFATAL, "COFF format does not support"
597 " segment base references");
598 } else
599 fix = coff_add_reloc(s, segment,
600 win64 ? IMAGE_REL_AMD64_REL32 : IMAGE_REL_I386_REL32);
601 p = mydata;
602 if (win32 | win64) {
603 WRITELONG(p, *(int64_t *)data + 4 - size + fix);
604 } else {
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);
617 sect->len += len;
620 typedef struct tagString {
621 struct tagString *Next;
622 int len;
623 char *String;
624 } STRING;
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);
641 newS->Next = NULL;
642 newS->String = (char *)nasm_malloc(newS->len + 1);
643 strcpy(newS->String, name);
644 if (rvp == NULL) {
645 int i;
646 for (i = 0; i < nsects; i++)
648 if (!strcmp(EXPORT_SECTION_NAME, sects[i]->name))
649 break;
650 if (i == nsects)
651 directive_sec =
652 sects[coff_make_section
653 (EXPORT_SECTION_NAME, EXPORT_SECTION_FLAGS)];
654 else
655 directive_sec = sects[i];
656 Exports = newS;
657 } else {
658 while (rvp->Next) {
659 if (!strcmp(rvp->String, name))
660 return;
661 rvp = rvp->Next;
663 rvp->Next = newS;
667 void BuildExportTable(void)
669 STRING *rvp = Exports, *next;
670 uint8_t buf[256];
671 int len;
672 if (rvp == NULL)
673 return;
674 while (rvp) {
675 len = sprintf((char *)buf, "-export:%s ", rvp->String);
676 coff_sect_write(directive_sec, buf, len);
677 rvp = rvp->Next;
680 next = Exports;
681 while ((rvp = next)) {
682 next = rvp->Next;
683 nasm_free(rvp->String);
684 nasm_free(rvp);
686 Exports = NULL;
689 static int coff_directives(char *directive, char *value, int pass)
691 if (!strcmp(directive, "export")) {
692 char *q, *name;
694 if (pass == 2)
695 return 1; /* ignore in pass two */
696 name = q = value;
697 while (*q && !isspace(*q))
698 q++;
699 if (isspace(*q)) {
700 *q++ = '\0';
701 while (*q && isspace(*q))
702 q++;
705 if (!*name) {
706 error(ERR_NONFATAL, "`export' directive requires export name");
707 return 1;
709 if (*q) {
710 error(ERR_NONFATAL, "unrecognized export qualifier `%s'", q);
711 return 1;
713 AddExport(name);
714 return 1;
715 } else if (win32 && !strcmp(directive,"safeseh")) {
716 static int sxseg=-1;
717 int i;
718 if (sxseg==-1)
719 { for (i = 0; i < nsects; i++)
720 if (!strcmp(".sxdata",sects[i]->name))
721 break;
722 if (i == nsects)
723 sxseg = coff_make_section(".sxdata",0x200);
724 else
725 sxseg = i;
727 if (pass==2) {
728 uint32_t n;
729 saa_rewind(syms);
730 for (n = 0; n < nsyms; n++) {
731 struct Symbol *sym = saa_rstruct(syms);
732 bool equals;
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);
741 nasm_free(name);
743 else
744 equals = !strcmp(value,sym->name);
746 if (equals) {
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);
753 sym->type = 0x20;
754 break;
757 if (n == nsyms) {
758 error(ERR_NONFATAL,
759 "`safeseh' directive requires valid symbol");
762 return 1;
764 return 0;
767 static void coff_write(void)
769 int32_t pos, sympos, vsize;
770 int i;
772 BuildExportTable(); /* fill in the .drectve section with -export's */
774 if (win32) {
775 /* add default value for @feat.00, this allows to 'link /safeseh' */
776 uint32_t n;
778 saa_rewind(syms);
779 for (n = 0; n < nsyms; n++) {
780 struct Symbol *sym = saa_rstruct(syms);
781 if (sym->strpos == -1 && !strcmp("@feat.00",sym->name))
782 break;
784 if (n == nsyms)
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) {
796 sects[i]->pos = pos;
797 pos += sects[i]->len;
798 sects[i]->relpos = pos;
799 pos += 10 * sects[i]->nrelocs;
800 } else
801 sects[i]->pos = sects[i]->relpos = 0L;
802 initsym += 2; /* two for each section */
804 sympos = pos;
807 * Output the COFF header.
809 if (win64)
810 fwriteint16_t(0x8664, coffp); /* MACHINE_x86-64 */
811 else
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.
824 vsize = 0L;
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)
853 char padname[8];
855 (void)vsize;
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)
873 struct Reloc *r;
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),
880 coffp);
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)
888 char padname[8];
890 if (name) {
891 memset(padname, 0, 8);
892 strncpy(padname, name, 8);
893 fwrite(padname, 8, 1, coffp);
894 } else {
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);
902 fputc(aux, coffp);
905 static void coff_write_symbols(void)
907 char filename[18];
908 uint32_t i;
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);
936 * The real symbols.
938 saa_rewind(syms);
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)
949 return 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",
967 "%endmacro",
968 "%imacro export 1+.nolist",
969 "[export %1]",
970 "%endmacro",
971 "%imacro safeseh 1.nolist",
972 "[safeseh %1]",
973 "%endmacro",
974 NULL
977 static int coff_set_info(enum geninfo type, char **val)
979 (void)type;
980 (void)val;
981 return 0;
983 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
985 #ifdef OF_COFF
987 struct ofmt of_coff = {
988 "COFF (i386) object files (e.g. DJGPP for DOS)",
989 "coff",
990 NULL,
991 null_debug_arr,
992 &null_debug_form,
993 coff_stdmac,
994 coff_std_init,
995 coff_set_info,
996 coff_out,
997 coff_deflabel,
998 coff_section_names,
999 coff_segbase,
1000 coff_directives,
1001 coff_std_filename,
1002 coff_cleanup
1005 #endif
1007 #ifdef OF_WIN32
1009 struct ofmt of_win32 = {
1010 "Microsoft Win32 (i386) object files",
1011 "win32",
1012 NULL,
1013 null_debug_arr,
1014 &null_debug_form,
1015 coff_stdmac,
1016 coff_win32_init,
1017 coff_set_info,
1018 coff_out,
1019 coff_deflabel,
1020 coff_section_names,
1021 coff_segbase,
1022 coff_directives,
1023 coff_win32_filename,
1024 coff_cleanup
1027 #endif
1029 #ifdef OF_WIN64
1031 struct ofmt of_win64 = {
1032 "Microsoft Win64 (x86-64) object files",
1033 "win64",
1034 NULL,
1035 null_debug_arr,
1036 &null_debug_form,
1037 coff_stdmac,
1038 coff_win64_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