NASM 2.00rc1
[nasm.git] / output / outcoff.c
blob9be558bdb107e5fd6c578de73362b5c86bd9de5f
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 licence given in the file "Licence"
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 struct Reloc {
72 struct Reloc *next;
73 int32_t address; /* relative to _start_ of section */
74 int32_t symbol; /* symbol number */
75 enum {
76 SECT_SYMBOLS,
77 ABS_SYMBOL,
78 REAL_SYMBOLS
79 } symbase; /* relocation for symbol number :) */
80 bool relative;
81 bool size64;
84 struct Symbol {
85 char name[9];
86 int32_t strpos; /* string table position of name */
87 int32_t value; /* address, or COMMON variable size */
88 int section; /* section number where it's defined
89 * - in COFF codes, not NASM codes */
90 bool is_global; /* is it a global symbol or not? */
93 static FILE *coffp;
94 static efunc error;
95 static char coff_infile[FILENAME_MAX];
97 struct Section {
98 struct SAA *data;
99 uint32_t len;
100 int nrelocs;
101 int32_t index;
102 struct Reloc *head, **tail;
103 uint32_t flags; /* section flags */
104 char name[9];
105 int32_t pos, relpos;
108 #define TEXT_FLAGS ((win32 | win64) ? 0x60500020L : 0x20L)
109 #define DATA_FLAGS ((win32 | win64) ? 0xC0300040L : 0x40L)
110 #define BSS_FLAGS ((win32 | win64) ? 0xC0300080L : 0x80L)
111 #define INFO_FLAGS 0x00100A00L
112 #define RDATA_FLAGS ((win32 | win64) ? 0x40400040L : 0x40L)
114 #define SECT_DELTA 32
115 static struct Section **sects;
116 static int nsects, sectlen;
118 static struct SAA *syms;
119 static uint32_t nsyms;
121 static int32_t def_seg;
123 static int initsym;
125 static struct RAA *bsym, *symval;
127 static struct SAA *strs;
128 static uint32_t strslen;
130 static void coff_gen_init(FILE *, efunc);
131 static void coff_sect_write(struct Section *, const uint8_t *,
132 uint32_t);
133 static void coff_write(void);
134 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
135 static void coff_write_relocs(struct Section *);
136 static void coff_write_symbols(void);
138 static void coff_win32_init(FILE * fp, efunc errfunc,
139 ldfunc ldef, evalfunc eval)
141 win32 = true; win64 = false;
142 (void)ldef; /* placate optimizers */
143 (void)eval;
144 coff_gen_init(fp, errfunc);
147 static void coff_win64_init(FILE * fp, efunc errfunc,
148 ldfunc ldef, evalfunc eval)
150 maxbits = 64;
151 win32 = false; win64 = true;
152 (void)ldef; /* placate optimizers */
153 (void)eval;
154 coff_gen_init(fp, errfunc);
157 static void coff_std_init(FILE * fp, efunc errfunc, ldfunc ldef,
158 evalfunc eval)
160 win32 = win64 = false;
161 (void)ldef; /* placate optimizers */
162 (void)eval;
163 coff_gen_init(fp, errfunc);
166 static void coff_gen_init(FILE * fp, efunc errfunc)
169 coffp = fp;
170 error = errfunc;
171 sects = NULL;
172 nsects = sectlen = 0;
173 syms = saa_init((int32_t)sizeof(struct Symbol));
174 nsyms = 0;
175 bsym = raa_init();
176 symval = raa_init();
177 strs = saa_init(1L);
178 strslen = 0;
179 def_seg = seg_alloc();
182 static void coff_cleanup(int debuginfo)
184 struct Reloc *r;
185 int i;
187 (void)debuginfo;
189 coff_write();
190 fclose(coffp);
191 for (i = 0; i < nsects; i++) {
192 if (sects[i]->data)
193 saa_free(sects[i]->data);
194 while (sects[i]->head) {
195 r = sects[i]->head;
196 sects[i]->head = sects[i]->head->next;
197 nasm_free(r);
199 nasm_free(sects[i]);
201 nasm_free(sects);
202 saa_free(syms);
203 raa_free(bsym);
204 raa_free(symval);
205 saa_free(strs);
208 static int coff_make_section(char *name, uint32_t flags)
210 struct Section *s;
212 s = nasm_malloc(sizeof(*s));
214 if (flags != BSS_FLAGS)
215 s->data = saa_init(1L);
216 else
217 s->data = NULL;
218 s->head = NULL;
219 s->tail = &s->head;
220 s->len = 0;
221 s->nrelocs = 0;
222 if (!strcmp(name, ".text"))
223 s->index = def_seg;
224 else
225 s->index = seg_alloc();
226 strncpy(s->name, name, 8);
227 s->name[8] = '\0';
228 s->flags = flags;
230 if (nsects >= sectlen)
231 sects =
232 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
233 sects[nsects++] = s;
235 return nsects - 1;
238 static int32_t coff_section_names(char *name, int pass, int *bits)
240 char *p;
241 uint32_t flags, align_and = ~0L, align_or = 0L;
242 int i;
245 * Set default bits.
247 if (!name) {
248 if(win64)
249 *bits = 64;
250 else
251 *bits = 32;
254 if (!name)
255 return def_seg;
257 p = name;
258 while (*p && !isspace(*p))
259 p++;
260 if (*p)
261 *p++ = '\0';
262 if (strlen(name) > 8) {
263 error(ERR_WARNING, "COFF section names limited to 8 characters:"
264 " truncating");
265 name[8] = '\0';
267 flags = 0;
269 while (*p && isspace(*p))
270 p++;
271 while (*p) {
272 char *q = p;
273 while (*p && !isspace(*p))
274 p++;
275 if (*p)
276 *p++ = '\0';
277 while (*p && isspace(*p))
278 p++;
280 if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
281 flags = TEXT_FLAGS;
282 } else if (!nasm_stricmp(q, "data")) {
283 flags = DATA_FLAGS;
284 } else if (!nasm_stricmp(q, "rdata")) {
285 if (win32 | win64)
286 flags = RDATA_FLAGS;
287 else {
288 flags = DATA_FLAGS; /* gotta do something */
289 error(ERR_NONFATAL, "standard COFF does not support"
290 " read-only data sections");
292 } else if (!nasm_stricmp(q, "bss")) {
293 flags = BSS_FLAGS;
294 } else if (!nasm_stricmp(q, "info")) {
295 if (win32 | win64)
296 flags = INFO_FLAGS;
297 else {
298 flags = DATA_FLAGS; /* gotta do something */
299 error(ERR_NONFATAL, "standard COFF does not support"
300 " informational sections");
302 } else if (!nasm_strnicmp(q, "align=", 6)) {
303 if (!(win32 | win64))
304 error(ERR_NONFATAL, "standard COFF does not support"
305 " section alignment specification");
306 else {
307 if (q[6 + strspn(q + 6, "0123456789")])
308 error(ERR_NONFATAL,
309 "argument to `align' is not numeric");
310 else {
311 unsigned int align = atoi(q + 6);
312 if (!align || ((align - 1) & align))
313 error(ERR_NONFATAL, "argument to `align' is not a"
314 " power of two");
315 else if (align > 64)
316 error(ERR_NONFATAL, "Win32 cannot align sections"
317 " to better than 64-byte boundaries");
318 else {
319 align_and = ~0x00F00000L;
320 align_or = (align == 1 ? 0x00100000L :
321 align == 2 ? 0x00200000L :
322 align == 4 ? 0x00300000L :
323 align == 8 ? 0x00400000L :
324 align == 16 ? 0x00500000L :
325 align ==
326 32 ? 0x00600000L : 0x00700000L);
333 for (i = 0; i < nsects; i++)
334 if (!strcmp(name, sects[i]->name))
335 break;
336 if (i == nsects) {
337 if (!flags) {
338 if (!strcmp(name, ".data"))
339 flags = DATA_FLAGS;
340 else if (!strcmp(name, ".rdata"))
341 flags = RDATA_FLAGS;
342 else if (!strcmp(name, ".bss"))
343 flags = BSS_FLAGS;
344 else
345 flags = TEXT_FLAGS;
347 i = coff_make_section(name, flags);
348 if (flags)
349 sects[i]->flags = flags;
350 sects[i]->flags &= align_and;
351 sects[i]->flags |= align_or;
352 } else if (pass == 1) {
353 if (flags)
354 error(ERR_WARNING, "section attributes ignored on"
355 " redeclaration of section `%s'", name);
358 return sects[i]->index;
361 static void coff_deflabel(char *name, int32_t segment, int64_t offset,
362 int is_global, char *special)
364 int pos = strslen + 4;
365 struct Symbol *sym;
367 if (special)
368 error(ERR_NONFATAL, "binary format does not support any"
369 " special symbol types");
371 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
372 error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
373 return;
376 if (strlen(name) > 8) {
377 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
378 strslen += 1 + strlen(name);
379 } else
380 pos = -1;
382 sym = saa_wstruct(syms);
384 sym->strpos = pos;
385 if (pos == -1)
386 strcpy(sym->name, name);
387 sym->is_global = !!is_global;
388 if (segment == NO_SEG)
389 sym->section = -1; /* absolute symbol */
390 else {
391 int i;
392 sym->section = 0;
393 for (i = 0; i < nsects; i++)
394 if (segment == sects[i]->index) {
395 sym->section = i + 1;
396 break;
398 if (!sym->section)
399 sym->is_global = true;
401 if (is_global == 2)
402 sym->value = offset;
403 else
404 sym->value = (sym->section == 0 ? 0 : offset);
407 * define the references from external-symbol segment numbers
408 * to these symbol records.
410 if (sym->section == 0) {
411 bsym = raa_write(bsym, segment, nsyms);
414 if (segment != NO_SEG)
415 symval = raa_write(symval, segment, sym->section ? 0 : sym->value);
417 nsyms++;
420 static int32_t coff_add_reloc(struct Section *sect, int32_t segment,
421 int relative, int size64)
423 struct Reloc *r;
425 (void)size64;
427 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
428 sect->tail = &r->next;
429 r->next = NULL;
431 r->address = sect->len;
432 if (segment == NO_SEG)
433 r->symbol = 0, r->symbase = ABS_SYMBOL;
434 else {
435 int i;
436 r->symbase = REAL_SYMBOLS;
437 for (i = 0; i < nsects; i++)
438 if (segment == sects[i]->index) {
439 r->symbol = i * 2;
440 r->symbase = SECT_SYMBOLS;
441 break;
443 if (r->symbase == REAL_SYMBOLS)
444 r->symbol = raa_read(bsym, segment);
446 r->relative = relative;
448 sect->nrelocs++;
451 * Return the fixup for standard COFF common variables.
453 if (r->symbase == REAL_SYMBOLS && !(win32 | win64))
454 return raa_read(symval, segment);
455 else
456 return 0;
459 static void coff_out(int32_t segto, const void *data,
460 enum out_type type, uint64_t size,
461 int32_t segment, int32_t wrt)
463 struct Section *s;
464 uint8_t mydata[8], *p;
465 int i;
467 if (wrt != NO_SEG) {
468 wrt = NO_SEG; /* continue to do _something_ */
469 error(ERR_NONFATAL, "WRT not supported by COFF output formats");
473 * handle absolute-assembly (structure definitions)
475 if (segto == NO_SEG) {
476 if (type != OUT_RESERVE)
477 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
478 " space");
479 return;
482 s = NULL;
483 for (i = 0; i < nsects; i++)
484 if (segto == sects[i]->index) {
485 s = sects[i];
486 break;
488 if (!s) {
489 int tempint; /* ignored */
490 if (segto != coff_section_names(".text", 2, &tempint))
491 error(ERR_PANIC, "strange segment conditions in COFF driver");
492 else
493 s = sects[nsects - 1];
496 if (!s->data && type != OUT_RESERVE) {
497 error(ERR_WARNING, "attempt to initialize memory in"
498 " BSS section `%s': ignored", s->name);
499 if (type == OUT_REL2ADR)
500 size = 2;
501 else if (type == OUT_REL4ADR)
502 size = 4;
503 s->len += size;
504 return;
507 if (type == OUT_RESERVE) {
508 if (s->data) {
509 error(ERR_WARNING, "uninitialised space declared in"
510 " non-BSS section `%s': zeroing", s->name);
511 coff_sect_write(s, NULL, size);
512 } else
513 s->len += size;
514 } else if (type == OUT_RAWDATA) {
515 if (segment != NO_SEG)
516 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
517 coff_sect_write(s, data, size);
518 } else if (type == OUT_ADDRESS) {
519 if (!(win64)) {
520 if (size != 4 && (segment != NO_SEG || wrt != NO_SEG))
521 error(ERR_NONFATAL, "COFF format does not support non-32-bit"
522 " relocations");
523 else {
524 int32_t fix = 0;
525 if (segment != NO_SEG || wrt != NO_SEG) {
526 if (wrt != NO_SEG) {
527 error(ERR_NONFATAL, "COFF format does not support"
528 " WRT types");
529 } else if (segment % 2) {
530 error(ERR_NONFATAL, "COFF format does not support"
531 " segment base references");
532 } else
533 fix = coff_add_reloc(s, segment, false, false);
535 p = mydata;
536 WRITELONG(p, *(int64_t *)data + fix);
537 coff_sect_write(s, mydata, size);
539 } else {
540 int32_t fix = 0;
541 p = mydata;
542 if (size == 8) {
543 /* if (segment != NO_SEG || wrt != NO_SEG) {
544 if (wrt != NO_SEG) {
545 error(ERR_NONFATAL, "COFF format does not support"
546 " WRT types");
547 } else if (segment % 2) {
548 error(ERR_NONFATAL, "COFF format does not support"
549 " segment base references");
550 } else
551 fix = coff_add_reloc(s, segment, false);
552 } */
553 fix = coff_add_reloc(s, segment, false, true);
554 WRITEDLONG(p, *(int64_t *)data + fix);
555 coff_sect_write(s, mydata, size);
556 } else {
557 fix = coff_add_reloc(s, segment, false, false);
558 WRITELONG(p, *(int64_t *)data + fix);
559 coff_sect_write(s, mydata, size);
562 } else if (type == OUT_REL2ADR) {
563 error(ERR_NONFATAL, "COFF format does not support 16-bit"
564 " relocations");
565 } else if (type == OUT_REL4ADR) {
566 if (segment == segto && !(win64)) /* Acceptable for RIP-relative */
567 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
568 else if (segment == NO_SEG && win32)
569 error(ERR_NONFATAL, "Win32 COFF does not correctly support"
570 " relative references to absolute addresses");
571 else {
572 int32_t fix = 0;
573 if (segment != NO_SEG && segment % 2) {
574 error(ERR_NONFATAL, "COFF format does not support"
575 " segment base references");
576 } else
577 fix = coff_add_reloc(s, segment, true, false);
578 p = mydata;
579 if (win32 | win64) {
580 WRITELONG(p, *(int64_t *)data + 4 - size + fix);
581 } else {
582 WRITELONG(p, *(int64_t *)data - (size + s->len) + fix);
584 coff_sect_write(s, mydata, 4L);
590 static void coff_sect_write(struct Section *sect,
591 const uint8_t *data, uint32_t len)
593 saa_wbytes(sect->data, data, len);
594 sect->len += len;
597 typedef struct tagString {
598 struct tagString *Next;
599 int len;
600 char *String;
601 } STRING;
603 #define EXPORT_SECTION_NAME ".drectve"
604 #define EXPORT_SECTION_FLAGS INFO_FLAGS
606 #define EXPORT_SECTION_NAME ".text"
607 #define EXPORT_SECTION_FLAGS TEXT_FLAGS
610 static STRING *Exports = NULL;
611 static struct Section *directive_sec;
612 void AddExport(char *name)
614 STRING *rvp = Exports, *newS;
616 newS = (STRING *) nasm_malloc(sizeof(STRING));
617 newS->len = strlen(name);
618 newS->Next = NULL;
619 newS->String = (char *)nasm_malloc(newS->len + 1);
620 strcpy(newS->String, name);
621 if (rvp == NULL) {
622 int i;
623 for (i = 0; i < nsects; i++)
625 if (!strcmp(EXPORT_SECTION_NAME, sects[i]->name))
626 break;
627 if (i == nsects)
628 directive_sec =
629 sects[coff_make_section
630 (EXPORT_SECTION_NAME, EXPORT_SECTION_FLAGS)];
631 else
632 directive_sec = sects[i];
633 Exports = newS;
634 } else {
635 while (rvp->Next) {
636 if (!strcmp(rvp->String, name))
637 return;
638 rvp = rvp->Next;
640 rvp->Next = newS;
644 void BuildExportTable(void)
646 STRING *rvp = Exports, *next;
647 uint8_t buf[256];
648 int len;
649 if (rvp == NULL)
650 return;
651 while (rvp) {
652 len = sprintf((char *)buf, "-export:%s ", rvp->String);
653 coff_sect_write(directive_sec, buf, len);
654 rvp = rvp->Next;
657 next = Exports;
658 while ((rvp = next)) {
659 next = rvp->Next;
660 nasm_free(rvp->String);
661 nasm_free(rvp);
663 Exports = NULL;
666 static int coff_directives(char *directive, char *value, int pass)
668 if (!strcmp(directive, "export")) {
669 char *q, *name;
671 if (pass == 2)
672 return 1; /* ignore in pass two */
673 name = q = value;
674 while (*q && !isspace(*q))
675 q++;
676 if (isspace(*q)) {
677 *q++ = '\0';
678 while (*q && isspace(*q))
679 q++;
682 if (!*name) {
683 error(ERR_NONFATAL, "`export' directive requires export name");
684 return 1;
686 if (*q) {
687 error(ERR_NONFATAL, "unrecognized export qualifier `%s'", q);
688 return 1;
690 AddExport(name);
691 return 1;
693 return 0;
696 static void coff_write(void)
698 int32_t pos, sympos, vsize;
699 int i;
701 BuildExportTable(); /* fill in the .drectve section with -export's */
703 * Work out how big the file will get. Calculate the start of
704 * the `real' symbols at the same time.
706 pos = 0x14 + 0x28 * nsects;
707 initsym = 3; /* two for the file, one absolute */
708 for (i = 0; i < nsects; i++) {
709 if (sects[i]->data) {
710 sects[i]->pos = pos;
711 pos += sects[i]->len;
712 sects[i]->relpos = pos;
713 pos += 10 * sects[i]->nrelocs;
714 } else
715 sects[i]->pos = sects[i]->relpos = 0L;
716 initsym += 2; /* two for each section */
718 sympos = pos;
721 * Output the COFF header.
723 if (win64)
724 fwriteint16_t(0x8664, coffp); /* MACHINE_x86-64 */
725 else
726 fwriteint16_t(0x014C, coffp); /* MACHINE_i386 */
727 fwriteint16_t(nsects, coffp); /* number of sections */
728 fwriteint32_t(time(NULL), coffp); /* time stamp */
729 fwriteint32_t(sympos, coffp);
730 fwriteint32_t(nsyms + initsym, coffp);
731 fwriteint16_t(0, coffp); /* no optional header */
732 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
733 fwriteint16_t((win32 | win64) ? 0 : 0x104, coffp);
736 * Output the section headers.
738 vsize = 0L;
739 for (i = 0; i < nsects; i++) {
740 coff_section_header(sects[i]->name, vsize, sects[i]->len,
741 sects[i]->pos, sects[i]->relpos,
742 sects[i]->nrelocs, sects[i]->flags);
743 vsize += sects[i]->len;
747 * Output the sections and their relocations.
749 for (i = 0; i < nsects; i++)
750 if (sects[i]->data) {
751 saa_fpwrite(sects[i]->data, coffp);
752 coff_write_relocs(sects[i]);
756 * Output the symbol and string tables.
758 coff_write_symbols();
759 fwriteint32_t(strslen + 4, coffp); /* length includes length count */
760 saa_fpwrite(strs, coffp);
763 static void coff_section_header(char *name, int32_t vsize,
764 int32_t datalen, int32_t datapos,
765 int32_t relpos, int nrelocs, int32_t flags)
767 char padname[8];
769 (void)vsize;
771 memset(padname, 0, 8);
772 strncpy(padname, name, 8);
773 fwrite(padname, 8, 1, coffp);
774 fwriteint32_t(0, coffp); /* Virtual size field - set to 0 or vsize */
775 fwriteint32_t(0L, coffp); /* RVA/offset - we ignore */
776 fwriteint32_t(datalen, coffp);
777 fwriteint32_t(datapos, coffp);
778 fwriteint32_t(relpos, coffp);
779 fwriteint32_t(0L, coffp); /* no line numbers - we don't do 'em */
780 fwriteint16_t(nrelocs, coffp);
781 fwriteint16_t(0, coffp); /* again, no line numbers */
782 fwriteint32_t(flags, coffp);
785 static void coff_write_relocs(struct Section *s)
787 struct Reloc *r;
789 for (r = s->head; r; r = r->next) {
790 fwriteint32_t(r->address, coffp);
791 fwriteint32_t(r->symbol + (r->symbase == REAL_SYMBOLS ? initsym :
792 r->symbase == ABS_SYMBOL ? initsym - 1 :
793 r->symbase == SECT_SYMBOLS ? 2 : 0),
794 coffp);
796 * Strange: Microsoft's COFF documentation says 0x03 for an
797 * absolute relocation, but both Visual C++ and DJGPP agree
798 * that in fact it's 0x06. I'll use 0x06 until someone
799 * argues. ***** UPDATE: PE/COFF Ver.8 docs confirm this -kkanios *****
801 if (win64)
802 fwriteint16_t(r->relative ? 0x04 : r->size64 ? 0x01 : 0x02, coffp);
803 else
804 fwriteint16_t(r->relative ? 0x14 : 0x06, coffp);
808 static void coff_symbol(char *name, int32_t strpos, int32_t value,
809 int section, int type, int aux)
811 char padname[8];
813 if (name) {
814 memset(padname, 0, 8);
815 strncpy(padname, name, 8);
816 fwrite(padname, 8, 1, coffp);
817 } else {
818 fwriteint32_t(0L, coffp);
819 fwriteint32_t(strpos, coffp);
821 fwriteint32_t(value, coffp);
822 fwriteint16_t(section, coffp);
823 fwriteint16_t(0, coffp);
824 fputc(type, coffp);
825 fputc(aux, coffp);
828 static void coff_write_symbols(void)
830 char filename[18];
831 uint32_t i;
834 * The `.file' record, and the file name auxiliary record.
836 coff_symbol(".file", 0L, 0L, -2, 0x67, 1);
837 memset(filename, 0, 18);
838 strncpy(filename, coff_infile, 18);
839 fwrite(filename, 18, 1, coffp);
842 * The section records, with their auxiliaries.
844 memset(filename, 0, 18); /* useful zeroed buffer */
846 for (i = 0; i < (uint32_t) nsects; i++) {
847 coff_symbol(sects[i]->name, 0L, 0L, i + 1, 3, 1);
848 fwriteint32_t(sects[i]->len, coffp);
849 fwriteint16_t(sects[i]->nrelocs, coffp);
850 fwrite(filename, 12, 1, coffp);
854 * The absolute symbol, for relative-to-absolute relocations.
856 coff_symbol(".absolut", 0L, 0L, -1, 3, 0);
859 * The real symbols.
861 saa_rewind(syms);
862 for (i = 0; i < nsyms; i++) {
863 struct Symbol *sym = saa_rstruct(syms);
864 coff_symbol(sym->strpos == -1 ? sym->name : NULL,
865 sym->strpos, sym->value, sym->section,
866 sym->is_global ? 2 : 3, 0);
870 static int32_t coff_segbase(int32_t segment)
872 return segment;
875 static void coff_std_filename(char *inname, char *outname, efunc error)
877 strcpy(coff_infile, inname);
878 standard_extension(inname, outname, ".o", error);
881 static void coff_win32_filename(char *inname, char *outname, efunc error)
883 strcpy(coff_infile, inname);
884 standard_extension(inname, outname, ".obj", error);
887 static const char *coff_stdmac[] = {
888 "%define __SECT__ [section .text]",
889 "%macro __NASM_CDecl__ 1",
890 "%endmacro",
891 "%imacro export 1+.nolist",
892 "[export %1]",
893 "%endmacro",
894 NULL
897 static int coff_set_info(enum geninfo type, char **val)
899 (void)type;
900 (void)val;
901 return 0;
903 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
905 #ifdef OF_COFF
907 struct ofmt of_coff = {
908 "COFF (i386) object files (e.g. DJGPP for DOS)",
909 "coff",
910 NULL,
911 null_debug_arr,
912 &null_debug_form,
913 coff_stdmac,
914 coff_std_init,
915 coff_set_info,
916 coff_out,
917 coff_deflabel,
918 coff_section_names,
919 coff_segbase,
920 coff_directives,
921 coff_std_filename,
922 coff_cleanup
925 #endif
927 #ifdef OF_WIN32
929 struct ofmt of_win32 = {
930 "Microsoft Win32 (i386) object files",
931 "win32",
932 NULL,
933 null_debug_arr,
934 &null_debug_form,
935 coff_stdmac,
936 coff_win32_init,
937 coff_set_info,
938 coff_out,
939 coff_deflabel,
940 coff_section_names,
941 coff_segbase,
942 coff_directives,
943 coff_win32_filename,
944 coff_cleanup
947 #endif
949 #ifdef OF_WIN64
951 struct ofmt of_win64 = {
952 "Microsoft Win64 (x86-64) object files",
953 "win64",
954 NULL,
955 null_debug_arr,
956 &null_debug_form,
957 coff_stdmac,
958 coff_win64_init,
959 coff_set_info,
960 coff_out,
961 coff_deflabel,
962 coff_section_names,
963 coff_segbase,
964 coff_directives,
965 coff_win32_filename,
966 coff_cleanup
969 #endif