Document %? and %??
[nasm.git] / output / outcoff.c
blobae880cb259b7174c13dcf696b696c56ad141cfd4
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 (segment != NO_SEG || wrt != NO_SEG) {
570 if (wrt != NO_SEG) {
571 error(ERR_NONFATAL, "COFF format does not support"
572 " WRT types");
573 } else if (segment % 2) {
574 error(ERR_NONFATAL, "COFF format does not support"
575 " segment base references");
576 } else
577 fix = coff_add_reloc(s, segment, false);
578 } */
579 fix = coff_add_reloc(s, segment, IMAGE_REL_AMD64_ADDR64);
580 WRITEDLONG(p, *(int64_t *)data + fix);
581 coff_sect_write(s, mydata, size);
582 } else {
583 fix = coff_add_reloc(s, segment,
584 wrt == imagebase_sect ? IMAGE_REL_AMD64_ADDR32NB:
585 IMAGE_REL_AMD64_ADDR32);
586 WRITELONG(p, *(int64_t *)data + fix);
587 coff_sect_write(s, mydata, size);
590 } else if (type == OUT_REL2ADR) {
591 error(ERR_NONFATAL, "COFF format does not support 16-bit"
592 " relocations");
593 } else if (type == OUT_REL4ADR) {
594 if (segment == segto && !(win64)) /* Acceptable for RIP-relative */
595 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
596 else if (segment == NO_SEG && win32)
597 error(ERR_NONFATAL, "Win32 COFF does not correctly support"
598 " relative references to absolute addresses");
599 else {
600 int32_t fix = 0;
601 if (segment != NO_SEG && segment % 2) {
602 error(ERR_NONFATAL, "COFF format does not support"
603 " segment base references");
604 } else
605 fix = coff_add_reloc(s, segment,
606 win64 ? IMAGE_REL_AMD64_REL32 : IMAGE_REL_I386_REL32);
607 p = mydata;
608 if (win32 | win64) {
609 WRITELONG(p, *(int64_t *)data + 4 - size + fix);
610 } else {
611 WRITELONG(p, *(int64_t *)data - (size + s->len) + fix);
613 coff_sect_write(s, mydata, 4L);
619 static void coff_sect_write(struct Section *sect,
620 const uint8_t *data, uint32_t len)
622 saa_wbytes(sect->data, data, len);
623 sect->len += len;
626 typedef struct tagString {
627 struct tagString *Next;
628 int len;
629 char *String;
630 } STRING;
632 #define EXPORT_SECTION_NAME ".drectve"
633 #define EXPORT_SECTION_FLAGS INFO_FLAGS
635 #define EXPORT_SECTION_NAME ".text"
636 #define EXPORT_SECTION_FLAGS TEXT_FLAGS
639 static STRING *Exports = NULL;
640 static struct Section *directive_sec;
641 void AddExport(char *name)
643 STRING *rvp = Exports, *newS;
645 newS = (STRING *) nasm_malloc(sizeof(STRING));
646 newS->len = strlen(name);
647 newS->Next = NULL;
648 newS->String = (char *)nasm_malloc(newS->len + 1);
649 strcpy(newS->String, name);
650 if (rvp == NULL) {
651 int i;
652 for (i = 0; i < nsects; i++)
654 if (!strcmp(EXPORT_SECTION_NAME, sects[i]->name))
655 break;
656 if (i == nsects)
657 directive_sec =
658 sects[coff_make_section
659 (EXPORT_SECTION_NAME, EXPORT_SECTION_FLAGS)];
660 else
661 directive_sec = sects[i];
662 Exports = newS;
663 } else {
664 while (rvp->Next) {
665 if (!strcmp(rvp->String, name))
666 return;
667 rvp = rvp->Next;
669 rvp->Next = newS;
673 void BuildExportTable(void)
675 STRING *rvp = Exports, *next;
676 uint8_t buf[256];
677 int len;
678 if (rvp == NULL)
679 return;
680 while (rvp) {
681 len = sprintf((char *)buf, "-export:%s ", rvp->String);
682 coff_sect_write(directive_sec, buf, len);
683 rvp = rvp->Next;
686 next = Exports;
687 while ((rvp = next)) {
688 next = rvp->Next;
689 nasm_free(rvp->String);
690 nasm_free(rvp);
692 Exports = NULL;
695 static int coff_directives(char *directive, char *value, int pass)
697 if (!strcmp(directive, "export")) {
698 char *q, *name;
700 if (pass == 2)
701 return 1; /* ignore in pass two */
702 name = q = value;
703 while (*q && !isspace(*q))
704 q++;
705 if (isspace(*q)) {
706 *q++ = '\0';
707 while (*q && isspace(*q))
708 q++;
711 if (!*name) {
712 error(ERR_NONFATAL, "`export' directive requires export name");
713 return 1;
715 if (*q) {
716 error(ERR_NONFATAL, "unrecognized export qualifier `%s'", q);
717 return 1;
719 AddExport(name);
720 return 1;
721 } else if (win32 && !strcmp(directive,"safeseh")) {
722 static int sxseg=-1;
723 int i;
724 if (sxseg==-1)
725 { for (i = 0; i < nsects; i++)
726 if (!strcmp(".sxdata",sects[i]->name))
727 break;
728 if (i == nsects)
729 sxseg = coff_make_section(".sxdata",0x200);
730 else
731 sxseg = i;
733 if (pass==2) {
734 uint32_t n;
735 saa_rewind(syms);
736 for (n = 0; n < nsyms; n++) {
737 struct Symbol *sym = saa_rstruct(syms);
738 bool equals;
740 /* sym->strpos is biased by 4, because symbol
741 * table is prefixed with table length */
742 if (sym->strpos >=4) {
743 char *name = nasm_malloc(sym->namlen+1);
744 saa_fread(strs,sym->strpos-4,name,sym->namlen);
745 name[sym->namlen]='\0';
746 equals = !strcmp(value,name);
747 nasm_free(name);
749 else
750 equals = !strcmp(value,sym->name);
752 if (equals) {
753 /* this value arithmetics effectively reflects
754 * initsym in coff_write(): 2 for file, 1 for
755 * .absolute and two per each section */
756 unsigned char value[4],*p=value;
757 WRITELONG(p,n + 2 + 1 + nsects*2);
758 coff_sect_write(sects[sxseg],value,4);
759 sym->type = 0x20;
760 break;
763 if (n == nsyms) {
764 error(ERR_NONFATAL,
765 "`safeseh' directive requires valid symbol");
768 return 1;
770 return 0;
773 static void coff_write(void)
775 int32_t pos, sympos, vsize;
776 int i;
778 BuildExportTable(); /* fill in the .drectve section with -export's */
780 if (win32) {
781 /* add default value for @feat.00, this allows to 'link /safeseh' */
782 uint32_t n;
784 saa_rewind(syms);
785 for (n = 0; n < nsyms; n++) {
786 struct Symbol *sym = saa_rstruct(syms);
787 if (sym->strpos == -1 && !strcmp("@feat.00",sym->name))
788 break;
790 if (n == nsyms)
791 coff_deflabel("@feat.00",NO_SEG,1,0,NULL);
795 * Work out how big the file will get. Calculate the start of
796 * the `real' symbols at the same time.
798 pos = 0x14 + 0x28 * nsects;
799 initsym = 3; /* two for the file, one absolute */
800 for (i = 0; i < nsects; i++) {
801 if (sects[i]->data) {
802 sects[i]->pos = pos;
803 pos += sects[i]->len;
804 sects[i]->relpos = pos;
805 pos += 10 * sects[i]->nrelocs;
806 } else
807 sects[i]->pos = sects[i]->relpos = 0L;
808 initsym += 2; /* two for each section */
810 sympos = pos;
813 * Output the COFF header.
815 if (win64)
816 fwriteint16_t(0x8664, coffp); /* MACHINE_x86-64 */
817 else
818 fwriteint16_t(0x014C, coffp); /* MACHINE_i386 */
819 fwriteint16_t(nsects, coffp); /* number of sections */
820 fwriteint32_t(time(NULL), coffp); /* time stamp */
821 fwriteint32_t(sympos, coffp);
822 fwriteint32_t(nsyms + initsym, coffp);
823 fwriteint16_t(0, coffp); /* no optional header */
824 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
825 fwriteint16_t((win32 | win64) ? 0 : 0x104, coffp);
828 * Output the section headers.
830 vsize = 0L;
831 for (i = 0; i < nsects; i++) {
832 coff_section_header(sects[i]->name, vsize, sects[i]->len,
833 sects[i]->pos, sects[i]->relpos,
834 sects[i]->nrelocs, sects[i]->flags);
835 vsize += sects[i]->len;
839 * Output the sections and their relocations.
841 for (i = 0; i < nsects; i++)
842 if (sects[i]->data) {
843 saa_fpwrite(sects[i]->data, coffp);
844 coff_write_relocs(sects[i]);
848 * Output the symbol and string tables.
850 coff_write_symbols();
851 fwriteint32_t(strslen + 4, coffp); /* length includes length count */
852 saa_fpwrite(strs, coffp);
855 static void coff_section_header(char *name, int32_t vsize,
856 int32_t datalen, int32_t datapos,
857 int32_t relpos, int nrelocs, int32_t flags)
859 char padname[8];
861 (void)vsize;
863 memset(padname, 0, 8);
864 strncpy(padname, name, 8);
865 fwrite(padname, 8, 1, coffp);
866 fwriteint32_t(0, coffp); /* Virtual size field - set to 0 or vsize */
867 fwriteint32_t(0L, coffp); /* RVA/offset - we ignore */
868 fwriteint32_t(datalen, coffp);
869 fwriteint32_t(datapos, coffp);
870 fwriteint32_t(relpos, coffp);
871 fwriteint32_t(0L, coffp); /* no line numbers - we don't do 'em */
872 fwriteint16_t(nrelocs, coffp);
873 fwriteint16_t(0, coffp); /* again, no line numbers */
874 fwriteint32_t(flags, coffp);
877 static void coff_write_relocs(struct Section *s)
879 struct Reloc *r;
881 for (r = s->head; r; r = r->next) {
882 fwriteint32_t(r->address, coffp);
883 fwriteint32_t(r->symbol + (r->symbase == REAL_SYMBOLS ? initsym :
884 r->symbase == ABS_SYMBOL ? initsym - 1 :
885 r->symbase == SECT_SYMBOLS ? 2 : 0),
886 coffp);
887 fwriteint16_t(r->type, coffp);
891 static void coff_symbol(char *name, int32_t strpos, int32_t value,
892 int section, int type, int storageclass, int aux)
894 char padname[8];
896 if (name) {
897 memset(padname, 0, 8);
898 strncpy(padname, name, 8);
899 fwrite(padname, 8, 1, coffp);
900 } else {
901 fwriteint32_t(0L, coffp);
902 fwriteint32_t(strpos, coffp);
904 fwriteint32_t(value, coffp);
905 fwriteint16_t(section, coffp);
906 fwriteint16_t(type, coffp);
907 fputc(storageclass, coffp);
908 fputc(aux, coffp);
911 static void coff_write_symbols(void)
913 char filename[18];
914 uint32_t i;
917 * The `.file' record, and the file name auxiliary record.
919 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
920 memset(filename, 0, 18);
921 strncpy(filename, coff_infile, 18);
922 fwrite(filename, 18, 1, coffp);
925 * The section records, with their auxiliaries.
927 memset(filename, 0, 18); /* useful zeroed buffer */
929 for (i = 0; i < (uint32_t) nsects; i++) {
930 coff_symbol(sects[i]->name, 0L, 0L, i + 1, 0, 3, 1);
931 fwriteint32_t(sects[i]->len, coffp);
932 fwriteint16_t(sects[i]->nrelocs, coffp);
933 fwrite(filename, 12, 1, coffp);
937 * The absolute symbol, for relative-to-absolute relocations.
939 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
942 * The real symbols.
944 saa_rewind(syms);
945 for (i = 0; i < nsyms; i++) {
946 struct Symbol *sym = saa_rstruct(syms);
947 coff_symbol(sym->strpos == -1 ? sym->name : NULL,
948 sym->strpos, sym->value, sym->section,
949 sym->type, sym->is_global ? 2 : 3, 0);
953 static int32_t coff_segbase(int32_t segment)
955 return segment;
958 static void coff_std_filename(char *inname, char *outname, efunc error)
960 strcpy(coff_infile, inname);
961 standard_extension(inname, outname, ".o", error);
964 static void coff_win32_filename(char *inname, char *outname, efunc error)
966 strcpy(coff_infile, inname);
967 standard_extension(inname, outname, ".obj", error);
970 static const char *coff_stdmac[] = {
971 "%define __SECT__ [section .text]",
972 "%macro __NASM_CDecl__ 1",
973 "%endmacro",
974 "%imacro export 1+.nolist",
975 "[export %1]",
976 "%endmacro",
977 "%imacro safeseh 1.nolist",
978 "[safeseh %1]",
979 "%endmacro",
980 NULL
983 static int coff_set_info(enum geninfo type, char **val)
985 (void)type;
986 (void)val;
987 return 0;
989 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
991 #ifdef OF_COFF
993 struct ofmt of_coff = {
994 "COFF (i386) object files (e.g. DJGPP for DOS)",
995 "coff",
996 NULL,
997 null_debug_arr,
998 &null_debug_form,
999 coff_stdmac,
1000 coff_std_init,
1001 coff_set_info,
1002 coff_out,
1003 coff_deflabel,
1004 coff_section_names,
1005 coff_segbase,
1006 coff_directives,
1007 coff_std_filename,
1008 coff_cleanup
1011 #endif
1013 #ifdef OF_WIN32
1015 struct ofmt of_win32 = {
1016 "Microsoft Win32 (i386) object files",
1017 "win32",
1018 NULL,
1019 null_debug_arr,
1020 &null_debug_form,
1021 coff_stdmac,
1022 coff_win32_init,
1023 coff_set_info,
1024 coff_out,
1025 coff_deflabel,
1026 coff_section_names,
1027 coff_segbase,
1028 coff_directives,
1029 coff_win32_filename,
1030 coff_cleanup
1033 #endif
1035 #ifdef OF_WIN64
1037 struct ofmt of_win64 = {
1038 "Microsoft Win64 (x86-64) object files",
1039 "win64",
1040 NULL,
1041 null_debug_arr,
1042 &null_debug_form,
1043 coff_stdmac,
1044 coff_win64_init,
1045 coff_set_info,
1046 coff_out,
1047 coff_deflabel,
1048 coff_section_names,
1049 coff_segbase,
1050 coff_directives,
1051 coff_win32_filename,
1052 coff_cleanup
1055 #endif