Version 0.99.03
[nasm/avx512.git] / output / outcoff.c
blob6669424cb696701c1df981863a04c61d0dc2b5d2
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 <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <time.h>
15 #include <inttypes.h>
17 #include "nasm.h"
18 #include "nasmlib.h"
19 #include "outform.h"
21 #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
24 * Notes on COFF:
26 * (0) When I say `standard COFF' below, I mean `COFF as output and
27 * used by DJGPP'. I assume DJGPP gets it right.
29 * (1) Win32 appears to interpret the term `relative relocation'
30 * differently from standard COFF. Standard COFF understands a
31 * relative relocation to mean that during relocation you add the
32 * address of the symbol you're referencing, and subtract the base
33 * address of the section you're in. Win32 COFF, by contrast, seems
34 * to add the address of the symbol and then subtract the address
35 * of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
36 * subtly incompatible.
38 * (2) Win32 doesn't bother putting any flags in the header flags
39 * field (at offset 0x12 into the file).
41 * (3) Win32 uses some extra flags into the section header table:
42 * it defines flags 0x80000000 (writable), 0x40000000 (readable)
43 * and 0x20000000 (executable), and uses them in the expected
44 * combinations. It also defines 0x00100000 through 0x00700000 for
45 * section alignments of 1 through 64 bytes.
47 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
48 * field directly after the section name in the section header
49 * table for something strange: they store what the address of the
50 * section start point _would_ be, if you laid all the sections end
51 * to end starting at zero. Dunno why. Microsoft's documentation
52 * lists this field as "Virtual Size of Section", which doesn't
53 * seem to fit at all. In fact, Win32 even includes non-linked
54 * sections such as .drectve in this calculation.
56 * (5) Standard COFF does something very strange to common
57 * variables: the relocation point for a common variable is as far
58 * _before_ the variable as its size stretches out _after_ it. So
59 * we must fix up common variable references. Win32 seems to be
60 * sensible on this one.
63 /* Flag which version of COFF we are currently outputting. */
64 static int win32, win64;
66 struct Reloc {
67 struct Reloc *next;
68 int32_t address; /* relative to _start_ of section */
69 int32_t symbol; /* symbol number */
70 enum {
71 SECT_SYMBOLS,
72 ABS_SYMBOL,
73 REAL_SYMBOLS
74 } symbase; /* relocation for symbol number :) */
75 int relative; /* TRUE or FALSE */
76 int size64; /* TRUE or FALSE */
79 struct Symbol {
80 char name[9];
81 int32_t strpos; /* string table position of name */
82 int section; /* section number where it's defined
83 * - in COFF codes, not NASM codes */
84 int is_global; /* is it a global symbol or not? */
85 int32_t value; /* address, or COMMON variable size */
88 static FILE *coffp;
89 static efunc error;
90 static char coff_infile[FILENAME_MAX];
92 struct Section {
93 struct SAA *data;
94 uint32_t len;
95 int nrelocs;
96 int32_t index;
97 struct Reloc *head, **tail;
98 uint32_t flags; /* section flags */
99 char name[9];
100 int32_t pos, relpos;
103 #define TEXT_FLAGS ((win32 | win64) ? 0x60500020L : 0x20L)
104 #define DATA_FLAGS ((win32 | win64) ? 0xC0300040L : 0x40L)
105 #define BSS_FLAGS ((win32 | win64) ? 0xC0300080L : 0x80L)
106 #define INFO_FLAGS 0x00100A00L
107 #define RDATA_FLAGS ((win32 | win64) ? 0x40400040L : 0x40L)
109 #define SECT_DELTA 32
110 static struct Section **sects;
111 static int nsects, sectlen;
113 static struct SAA *syms;
114 static uint32_t nsyms;
116 static int32_t def_seg;
118 static int initsym;
120 static struct RAA *bsym, *symval;
122 static struct SAA *strs;
123 static uint32_t strslen;
125 static void coff_gen_init(FILE *, efunc);
126 static void coff_sect_write(struct Section *, const uint8_t *,
127 uint32_t);
128 static void coff_write(void);
129 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
130 static void coff_write_relocs(struct Section *);
131 static void coff_write_symbols(void);
133 static void coff_win32_init(FILE * fp, efunc errfunc,
134 ldfunc ldef, evalfunc eval)
136 win32 = TRUE; win64 = FALSE;
137 (void)ldef; /* placate optimizers */
138 (void)eval;
139 coff_gen_init(fp, errfunc);
142 static void coff_win64_init(FILE * fp, efunc errfunc,
143 ldfunc ldef, evalfunc eval)
145 maxbits = 64;
146 win32 = FALSE; win64 = TRUE;
147 (void)ldef; /* placate optimizers */
148 (void)eval;
149 coff_gen_init(fp, errfunc);
152 static void coff_std_init(FILE * fp, efunc errfunc, ldfunc ldef,
153 evalfunc eval)
155 win32 = win64 = FALSE;
156 (void)ldef; /* placate optimizers */
157 (void)eval;
158 coff_gen_init(fp, errfunc);
161 static void coff_gen_init(FILE * fp, efunc errfunc)
164 coffp = fp;
165 error = errfunc;
166 sects = NULL;
167 nsects = sectlen = 0;
168 syms = saa_init((int32_t)sizeof(struct Symbol));
169 nsyms = 0;
170 bsym = raa_init();
171 symval = raa_init();
172 strs = saa_init(1L);
173 strslen = 0;
174 def_seg = seg_alloc();
177 static void coff_cleanup(int debuginfo)
179 struct Reloc *r;
180 int i;
182 (void)debuginfo;
184 coff_write();
185 fclose(coffp);
186 for (i = 0; i < nsects; i++) {
187 if (sects[i]->data)
188 saa_free(sects[i]->data);
189 while (sects[i]->head) {
190 r = sects[i]->head;
191 sects[i]->head = sects[i]->head->next;
192 nasm_free(r);
194 nasm_free(sects[i]);
196 nasm_free(sects);
197 saa_free(syms);
198 raa_free(bsym);
199 raa_free(symval);
200 saa_free(strs);
203 static int coff_make_section(char *name, uint32_t flags)
205 struct Section *s;
207 s = nasm_malloc(sizeof(*s));
209 if (flags != BSS_FLAGS)
210 s->data = saa_init(1L);
211 else
212 s->data = NULL;
213 s->head = NULL;
214 s->tail = &s->head;
215 s->len = 0;
216 s->nrelocs = 0;
217 if (!strcmp(name, ".text"))
218 s->index = def_seg;
219 else
220 s->index = seg_alloc();
221 strncpy(s->name, name, 8);
222 s->name[8] = '\0';
223 s->flags = flags;
225 if (nsects >= sectlen)
226 sects =
227 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
228 sects[nsects++] = s;
230 return nsects - 1;
233 static int32_t coff_section_names(char *name, int pass, int *bits)
235 char *p;
236 uint32_t flags, align_and = ~0L, align_or = 0L;
237 int i;
240 * Set default bits.
242 if (!name) {
243 if(win64)
244 *bits = 64;
245 else
246 *bits = 32;
249 if (!name)
250 return def_seg;
252 p = name;
253 while (*p && !isspace(*p))
254 p++;
255 if (*p)
256 *p++ = '\0';
257 if (strlen(name) > 8) {
258 error(ERR_WARNING, "COFF section names limited to 8 characters:"
259 " truncating");
260 name[8] = '\0';
262 flags = 0;
264 while (*p && isspace(*p))
265 p++;
266 while (*p) {
267 char *q = p;
268 while (*p && !isspace(*p))
269 p++;
270 if (*p)
271 *p++ = '\0';
272 while (*p && isspace(*p))
273 p++;
275 if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
276 flags = TEXT_FLAGS;
277 } else if (!nasm_stricmp(q, "data")) {
278 flags = DATA_FLAGS;
279 } else if (!nasm_stricmp(q, "rdata")) {
280 if (win32 | win64)
281 flags = RDATA_FLAGS;
282 else {
283 flags = DATA_FLAGS; /* gotta do something */
284 error(ERR_NONFATAL, "standard COFF does not support"
285 " read-only data sections");
287 } else if (!nasm_stricmp(q, "bss")) {
288 flags = BSS_FLAGS;
289 } else if (!nasm_stricmp(q, "info")) {
290 if (win32 | win64)
291 flags = INFO_FLAGS;
292 else {
293 flags = DATA_FLAGS; /* gotta do something */
294 error(ERR_NONFATAL, "standard COFF does not support"
295 " informational sections");
297 } else if (!nasm_strnicmp(q, "align=", 6)) {
298 if (!(win32 | win64))
299 error(ERR_NONFATAL, "standard COFF does not support"
300 " section alignment specification");
301 else {
302 if (q[6 + strspn(q + 6, "0123456789")])
303 error(ERR_NONFATAL,
304 "argument to `align' is not numeric");
305 else {
306 unsigned int align = atoi(q + 6);
307 if (!align || ((align - 1) & align))
308 error(ERR_NONFATAL, "argument to `align' is not a"
309 " power of two");
310 else if (align > 64)
311 error(ERR_NONFATAL, "Win32 cannot align sections"
312 " to better than 64-byte boundaries");
313 else {
314 align_and = ~0x00F00000L;
315 align_or = (align == 1 ? 0x00100000L :
316 align == 2 ? 0x00200000L :
317 align == 4 ? 0x00300000L :
318 align == 8 ? 0x00400000L :
319 align == 16 ? 0x00500000L :
320 align ==
321 32 ? 0x00600000L : 0x00700000L);
328 for (i = 0; i < nsects; i++)
329 if (!strcmp(name, sects[i]->name))
330 break;
331 if (i == nsects) {
332 if (!flags) {
333 if (!strcmp(name, ".data"))
334 flags = DATA_FLAGS;
335 else if (!strcmp(name, ".rdata"))
336 flags = RDATA_FLAGS;
337 else if (!strcmp(name, ".bss"))
338 flags = BSS_FLAGS;
339 else
340 flags = TEXT_FLAGS;
342 i = coff_make_section(name, flags);
343 if (flags)
344 sects[i]->flags = flags;
345 sects[i]->flags &= align_and;
346 sects[i]->flags |= align_or;
347 } else if (pass == 1) {
348 if (flags)
349 error(ERR_WARNING, "section attributes ignored on"
350 " redeclaration of section `%s'", name);
353 return sects[i]->index;
356 static void coff_deflabel(char *name, int32_t segment, int32_t offset,
357 int is_global, char *special)
359 int pos = strslen + 4;
360 struct Symbol *sym;
362 if (special)
363 error(ERR_NONFATAL, "binary format does not support any"
364 " special symbol types");
366 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
367 error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
368 return;
371 if (strlen(name) > 8) {
372 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
373 strslen += 1 + strlen(name);
374 } else
375 pos = -1;
377 sym = saa_wstruct(syms);
379 sym->strpos = pos;
380 if (pos == -1)
381 strcpy(sym->name, name);
382 sym->is_global = !!is_global;
383 if (segment == NO_SEG)
384 sym->section = -1; /* absolute symbol */
385 else {
386 int i;
387 sym->section = 0;
388 for (i = 0; i < nsects; i++)
389 if (segment == sects[i]->index) {
390 sym->section = i + 1;
391 break;
393 if (!sym->section)
394 sym->is_global = TRUE;
396 if (is_global == 2)
397 sym->value = offset;
398 else
399 sym->value = (sym->section == 0 ? 0 : offset);
402 * define the references from external-symbol segment numbers
403 * to these symbol records.
405 if (sym->section == 0) {
406 bsym = raa_write(bsym, segment, nsyms);
409 if (segment != NO_SEG)
410 symval = raa_write(symval, segment, sym->section ? 0 : sym->value);
412 nsyms++;
415 static int32_t coff_add_reloc(struct Section *sect, int32_t segment,
416 int relative, int size64)
418 struct Reloc *r;
420 (void)size64;
422 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
423 sect->tail = &r->next;
424 r->next = NULL;
426 r->address = sect->len;
427 if (segment == NO_SEG)
428 r->symbol = 0, r->symbase = ABS_SYMBOL;
429 else {
430 int i;
431 r->symbase = REAL_SYMBOLS;
432 for (i = 0; i < nsects; i++)
433 if (segment == sects[i]->index) {
434 r->symbol = i * 2;
435 r->symbase = SECT_SYMBOLS;
436 break;
438 if (r->symbase == REAL_SYMBOLS)
439 r->symbol = raa_read(bsym, segment);
441 r->relative = relative;
443 sect->nrelocs++;
446 * Return the fixup for standard COFF common variables.
448 if (r->symbase == REAL_SYMBOLS && !(win32 | win64))
449 return raa_read(symval, segment);
450 else
451 return 0;
454 static void coff_out(int32_t segto, const void *data, uint32_t type,
455 int32_t segment, int32_t wrt)
457 struct Section *s;
458 int32_t realbytes = type & OUT_SIZMASK;
459 uint8_t mydata[8], *p;
460 int i;
462 if (wrt != NO_SEG) {
463 wrt = NO_SEG; /* continue to do _something_ */
464 error(ERR_NONFATAL, "WRT not supported by COFF output formats");
467 type &= OUT_TYPMASK;
470 * handle absolute-assembly (structure definitions)
472 if (segto == NO_SEG) {
473 if (type != OUT_RESERVE)
474 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
475 " space");
476 return;
479 s = NULL;
480 for (i = 0; i < nsects; i++)
481 if (segto == sects[i]->index) {
482 s = sects[i];
483 break;
485 if (!s) {
486 int tempint; /* ignored */
487 if (segto != coff_section_names(".text", 2, &tempint))
488 error(ERR_PANIC, "strange segment conditions in COFF driver");
489 else
490 s = sects[nsects - 1];
493 if (!s->data && type != OUT_RESERVE) {
494 error(ERR_WARNING, "attempt to initialize memory in"
495 " BSS section `%s': ignored", s->name);
496 if (type == OUT_REL2ADR)
497 realbytes = 2;
498 else if (type == OUT_REL4ADR)
499 realbytes = 4;
500 s->len += realbytes;
501 return;
504 if (type == OUT_RESERVE) {
505 if (s->data) {
506 error(ERR_WARNING, "uninitialised space declared in"
507 " non-BSS section `%s': zeroing", s->name);
508 coff_sect_write(s, NULL, realbytes);
509 } else
510 s->len += realbytes;
511 } else if (type == OUT_RAWDATA) {
512 if (segment != NO_SEG)
513 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
514 coff_sect_write(s, data, realbytes);
515 } else if (type == OUT_ADDRESS) {
516 if (!(win64)) {
517 if (realbytes != 4 && (segment != NO_SEG || wrt != NO_SEG))
518 error(ERR_NONFATAL, "COFF format does not support non-32-bit"
519 " relocations");
520 else {
521 int32_t fix = 0;
522 if (segment != NO_SEG || wrt != NO_SEG) {
523 if (wrt != NO_SEG) {
524 error(ERR_NONFATAL, "COFF format does not support"
525 " WRT types");
526 } else if (segment % 2) {
527 error(ERR_NONFATAL, "COFF format does not support"
528 " segment base references");
529 } else
530 fix = coff_add_reloc(s, segment, FALSE, FALSE);
532 p = mydata;
533 WRITELONG(p, *(int32_t *)data + fix);
534 coff_sect_write(s, mydata, realbytes);
536 } else {
537 int32_t fix = 0;
538 p = mydata;
539 if (realbytes == 8) {
540 /* if (segment != NO_SEG || wrt != NO_SEG) {
541 if (wrt != NO_SEG) {
542 error(ERR_NONFATAL, "COFF format does not support"
543 " WRT types");
544 } else if (segment % 2) {
545 error(ERR_NONFATAL, "COFF format does not support"
546 " segment base references");
547 } else
548 fix = coff_add_reloc(s, segment, FALSE);
549 } */
550 fix = coff_add_reloc(s, segment, FALSE, TRUE);
551 WRITEDLONG(p, *(int64_t *)data + fix);
552 coff_sect_write(s, mydata, realbytes);
553 } else {
554 fix = coff_add_reloc(s, segment, FALSE, FALSE);
555 WRITELONG(p, *(int32_t *)data + fix);
556 coff_sect_write(s, mydata, realbytes);
559 } else if (type == OUT_REL2ADR) {
560 error(ERR_NONFATAL, "COFF format does not support 16-bit"
561 " relocations");
562 } else if (type == OUT_REL4ADR) {
563 if (segment == segto && !(win64)) /* Acceptable for RIP-relative */
564 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
565 else if (segment == NO_SEG && win32)
566 error(ERR_NONFATAL, "Win32 COFF does not correctly support"
567 " relative references to absolute addresses");
568 else {
569 int32_t fix = 0;
570 if (segment != NO_SEG && segment % 2) {
571 error(ERR_NONFATAL, "COFF format does not support"
572 " segment base references");
573 } else
574 fix = coff_add_reloc(s, segment, TRUE, FALSE);
575 p = mydata;
576 if (win32 | win64) {
577 WRITELONG(p, *(int32_t *)data + 4 - realbytes + fix);
578 } else {
579 WRITELONG(p, *(int32_t *)data - (realbytes + s->len) + fix);
581 coff_sect_write(s, mydata, 4L);
587 static void coff_sect_write(struct Section *sect,
588 const uint8_t *data, uint32_t len)
590 saa_wbytes(sect->data, data, len);
591 sect->len += len;
594 typedef struct tagString {
595 struct tagString *Next;
596 int len;
597 char *String;
598 } STRING;
600 #define EXPORT_SECTION_NAME ".drectve"
601 #define EXPORT_SECTION_FLAGS INFO_FLAGS
603 #define EXPORT_SECTION_NAME ".text"
604 #define EXPORT_SECTION_FLAGS TEXT_FLAGS
607 static STRING *Exports = NULL;
608 static struct Section *directive_sec;
609 void AddExport(char *name)
611 STRING *rvp = Exports, *newS;
613 newS = (STRING *) nasm_malloc(sizeof(STRING));
614 newS->len = strlen(name);
615 newS->Next = NULL;
616 newS->String = (char *)nasm_malloc(newS->len + 1);
617 strcpy(newS->String, name);
618 if (rvp == NULL) {
619 int i;
620 for (i = 0; i < nsects; i++)
622 if (!strcmp(EXPORT_SECTION_NAME, sects[i]->name))
623 break;
624 if (i == nsects)
625 directive_sec =
626 sects[coff_make_section
627 (EXPORT_SECTION_NAME, EXPORT_SECTION_FLAGS)];
628 else
629 directive_sec = sects[i];
630 Exports = newS;
631 } else {
632 while (rvp->Next) {
633 if (!strcmp(rvp->String, name))
634 return;
635 rvp = rvp->Next;
637 rvp->Next = newS;
641 void BuildExportTable(void)
643 STRING *rvp = Exports, *next;
644 uint8_t buf[256];
645 int len;
646 if (rvp == NULL)
647 return;
648 while (rvp) {
649 len = sprintf((char *)buf, "-export:%s ", rvp->String);
650 coff_sect_write(directive_sec, buf, len);
651 rvp = rvp->Next;
654 next = Exports;
655 while ((rvp = next)) {
656 next = rvp->Next;
657 nasm_free(rvp->String);
658 nasm_free(rvp);
660 Exports = NULL;
663 static int coff_directives(char *directive, char *value, int pass)
665 if (!strcmp(directive, "export")) {
666 char *q, *name;
668 if (pass == 2)
669 return 1; /* ignore in pass two */
670 name = q = value;
671 while (*q && !isspace(*q))
672 q++;
673 if (isspace(*q)) {
674 *q++ = '\0';
675 while (*q && isspace(*q))
676 q++;
679 if (!*name) {
680 error(ERR_NONFATAL, "`export' directive requires export name");
681 return 1;
683 if (*q) {
684 error(ERR_NONFATAL, "unrecognized export qualifier `%s'", q);
685 return 1;
687 AddExport(name);
688 return 1;
690 return 0;
693 static void coff_write(void)
695 int32_t pos, sympos, vsize;
696 int i;
698 BuildExportTable(); /* fill in the .drectve section with -export's */
700 * Work out how big the file will get. Calculate the start of
701 * the `real' symbols at the same time.
703 pos = 0x14 + 0x28 * nsects;
704 initsym = 3; /* two for the file, one absolute */
705 for (i = 0; i < nsects; i++) {
706 if (sects[i]->data) {
707 sects[i]->pos = pos;
708 pos += sects[i]->len;
709 sects[i]->relpos = pos;
710 pos += 10 * sects[i]->nrelocs;
711 } else
712 sects[i]->pos = sects[i]->relpos = 0L;
713 initsym += 2; /* two for each section */
715 sympos = pos;
718 * Output the COFF header.
720 if (win64)
721 fwriteint16_t(0x8664, coffp); /* MACHINE_x86-64 */
722 else
723 fwriteint16_t(0x014C, coffp); /* MACHINE_i386 */
724 fwriteint16_t(nsects, coffp); /* number of sections */
725 fwriteint32_t(time(NULL), coffp); /* time stamp */
726 fwriteint32_t(sympos, coffp);
727 fwriteint32_t(nsyms + initsym, coffp);
728 fwriteint16_t(0, coffp); /* no optional header */
729 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
730 fwriteint16_t((win32 | win64) ? 0 : 0x104, coffp);
733 * Output the section headers.
735 vsize = 0L;
736 for (i = 0; i < nsects; i++) {
737 coff_section_header(sects[i]->name, vsize, sects[i]->len,
738 sects[i]->pos, sects[i]->relpos,
739 sects[i]->nrelocs, sects[i]->flags);
740 vsize += sects[i]->len;
744 * Output the sections and their relocations.
746 for (i = 0; i < nsects; i++)
747 if (sects[i]->data) {
748 saa_fpwrite(sects[i]->data, coffp);
749 coff_write_relocs(sects[i]);
753 * Output the symbol and string tables.
755 coff_write_symbols();
756 fwriteint32_t(strslen + 4, coffp); /* length includes length count */
757 saa_fpwrite(strs, coffp);
760 static void coff_section_header(char *name, int32_t vsize,
761 int32_t datalen, int32_t datapos,
762 int32_t relpos, int nrelocs, int32_t flags)
764 char padname[8];
766 memset(padname, 0, 8);
767 strncpy(padname, name, 8);
768 fwrite(padname, 8, 1, coffp);
769 fwriteint32_t(vsize, coffp);
770 fwriteint32_t(0L, coffp); /* RVA/offset - we ignore */
771 fwriteint32_t(datalen, coffp);
772 fwriteint32_t(datapos, coffp);
773 fwriteint32_t(relpos, coffp);
774 fwriteint32_t(0L, coffp); /* no line numbers - we don't do 'em */
775 fwriteint16_t(nrelocs, coffp);
776 fwriteint16_t(0, coffp); /* again, no line numbers */
777 fwriteint32_t(flags, coffp);
780 static void coff_write_relocs(struct Section *s)
782 struct Reloc *r;
784 for (r = s->head; r; r = r->next) {
785 fwriteint32_t(r->address, coffp);
786 fwriteint32_t(r->symbol + (r->symbase == REAL_SYMBOLS ? initsym :
787 r->symbase == ABS_SYMBOL ? initsym - 1 :
788 r->symbase == SECT_SYMBOLS ? 2 : 0),
789 coffp);
791 * Strange: Microsoft's COFF documentation says 0x03 for an
792 * absolute relocation, but both Visual C++ and DJGPP agree
793 * that in fact it's 0x06. I'll use 0x06 until someone
794 * argues. ***** UPDATE: PE/COFF Ver.8 docs confirm this -kkanios *****
796 if (win64)
797 fwriteint16_t(r->relative ? 0x04 : r->size64 ? 0x01 : 0x02, coffp);
798 else
799 fwriteint16_t(r->relative ? 0x14 : 0x06, coffp);
803 static void coff_symbol(char *name, int32_t strpos, int32_t value,
804 int section, int type, int aux)
806 char padname[8];
808 if (name) {
809 memset(padname, 0, 8);
810 strncpy(padname, name, 8);
811 fwrite(padname, 8, 1, coffp);
812 } else {
813 fwriteint32_t(0L, coffp);
814 fwriteint32_t(strpos, coffp);
816 fwriteint32_t(value, coffp);
817 fwriteint16_t(section, coffp);
818 fwriteint16_t(0, coffp);
819 fputc(type, coffp);
820 fputc(aux, coffp);
823 static void coff_write_symbols(void)
825 char filename[18];
826 uint32_t i;
829 * The `.file' record, and the file name auxiliary record.
831 coff_symbol(".file", 0L, 0L, -2, 0x67, 1);
832 memset(filename, 0, 18);
833 strncpy(filename, coff_infile, 18);
834 fwrite(filename, 18, 1, coffp);
837 * The section records, with their auxiliaries.
839 memset(filename, 0, 18); /* useful zeroed buffer */
841 for (i = 0; i < nsects; i++) {
842 coff_symbol(sects[i]->name, 0L, 0L, i + 1, 3, 1);
843 fwriteint32_t(sects[i]->len, coffp);
844 fwriteint16_t(sects[i]->nrelocs, coffp);
845 fwrite(filename, 12, 1, coffp);
849 * The absolute symbol, for relative-to-absolute relocations.
851 coff_symbol(".absolut", 0L, 0L, -1, 3, 0);
854 * The real symbols.
856 saa_rewind(syms);
857 for (i = 0; i < nsyms; i++) {
858 struct Symbol *sym = saa_rstruct(syms);
859 coff_symbol(sym->strpos == -1 ? sym->name : NULL,
860 sym->strpos, sym->value, sym->section,
861 sym->is_global ? 2 : 3, 0);
865 static int32_t coff_segbase(int32_t segment)
867 return segment;
870 static void coff_std_filename(char *inname, char *outname, efunc error)
872 strcpy(coff_infile, inname);
873 standard_extension(inname, outname, ".o", error);
876 static void coff_win32_filename(char *inname, char *outname, efunc error)
878 strcpy(coff_infile, inname);
879 standard_extension(inname, outname, ".obj", error);
882 static const char *coff_stdmac[] = {
883 "%define __SECT__ [section .text]",
884 "%macro __NASM_CDecl__ 1",
885 "%endmacro",
886 "%imacro export 1+.nolist",
887 "[export %1]",
888 "%endmacro",
889 NULL
892 static int coff_set_info(enum geninfo type, char **val)
894 (void)type;
895 (void)val;
896 return 0;
898 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
900 #ifdef OF_COFF
902 struct ofmt of_coff = {
903 "COFF (i386) object files (e.g. DJGPP for DOS)",
904 "coff",
905 NULL,
906 null_debug_arr,
907 &null_debug_form,
908 coff_stdmac,
909 coff_std_init,
910 coff_set_info,
911 coff_out,
912 coff_deflabel,
913 coff_section_names,
914 coff_segbase,
915 coff_directives,
916 coff_std_filename,
917 coff_cleanup
920 #endif
922 #ifdef OF_WIN32
924 struct ofmt of_win32 = {
925 "Microsoft Win32 (i386) object files",
926 "win32",
927 NULL,
928 null_debug_arr,
929 &null_debug_form,
930 coff_stdmac,
931 coff_win32_init,
932 coff_set_info,
933 coff_out,
934 coff_deflabel,
935 coff_section_names,
936 coff_segbase,
937 coff_directives,
938 coff_win32_filename,
939 coff_cleanup
942 #endif
944 #ifdef OF_WIN64
946 struct ofmt of_win64 = {
947 "Microsoft Win64 (x86-64) object files",
948 "win64",
949 NULL,
950 null_debug_arr,
951 &null_debug_form,
952 coff_stdmac,
953 coff_win64_init,
954 coff_set_info,
955 coff_out,
956 coff_deflabel,
957 coff_section_names,
958 coff_segbase,
959 coff_directives,
960 coff_win32_filename,
961 coff_cleanup
964 #endif