Merge branch 'master' of git+ssh://fbkotler@repo.or.cz/srv/git/nasm
[nasm.git] / output / outcoff.c
blob8eb8c061fc002e2d15157fefcd995dcd4a3a8fa8
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 * Newer versions of MASM seem to have changed this to be zero, and
57 * that apparently matches the COFF spec, so go with that.
59 * (5) Standard COFF does something very strange to common
60 * variables: the relocation point for a common variable is as far
61 * _before_ the variable as its size stretches out _after_ it. So
62 * we must fix up common variable references. Win32 seems to be
63 * sensible on this one.
66 /* Flag which version of COFF we are currently outputting. */
67 static int win32, win64;
69 struct Reloc {
70 struct Reloc *next;
71 int32_t address; /* relative to _start_ of section */
72 int32_t symbol; /* symbol number */
73 enum {
74 SECT_SYMBOLS,
75 ABS_SYMBOL,
76 REAL_SYMBOLS
77 } symbase; /* relocation for symbol number :) */
78 int relative; /* TRUE or FALSE */
79 int size64; /* TRUE or FALSE */
82 struct Symbol {
83 char name[9];
84 int32_t strpos; /* string table position of name */
85 int section; /* section number where it's defined
86 * - in COFF codes, not NASM codes */
87 int is_global; /* is it a global symbol or not? */
88 int32_t value; /* address, or COMMON variable size */
91 static FILE *coffp;
92 static efunc error;
93 static char coff_infile[FILENAME_MAX];
95 struct Section {
96 struct SAA *data;
97 uint32_t len;
98 int nrelocs;
99 int32_t index;
100 struct Reloc *head, **tail;
101 uint32_t flags; /* section flags */
102 char name[9];
103 int32_t pos, relpos;
106 #define TEXT_FLAGS ((win32 | win64) ? 0x60500020L : 0x20L)
107 #define DATA_FLAGS ((win32 | win64) ? 0xC0300040L : 0x40L)
108 #define BSS_FLAGS ((win32 | win64) ? 0xC0300080L : 0x80L)
109 #define INFO_FLAGS 0x00100A00L
110 #define RDATA_FLAGS ((win32 | win64) ? 0x40400040L : 0x40L)
112 #define SECT_DELTA 32
113 static struct Section **sects;
114 static int nsects, sectlen;
116 static struct SAA *syms;
117 static uint32_t nsyms;
119 static int32_t def_seg;
121 static int initsym;
123 static struct RAA *bsym, *symval;
125 static struct SAA *strs;
126 static uint32_t strslen;
128 static void coff_gen_init(FILE *, efunc);
129 static void coff_sect_write(struct Section *, const uint8_t *,
130 uint32_t);
131 static void coff_write(void);
132 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
133 static void coff_write_relocs(struct Section *);
134 static void coff_write_symbols(void);
136 static void coff_win32_init(FILE * fp, efunc errfunc,
137 ldfunc ldef, evalfunc eval)
139 win32 = TRUE; win64 = FALSE;
140 (void)ldef; /* placate optimizers */
141 (void)eval;
142 coff_gen_init(fp, errfunc);
145 static void coff_win64_init(FILE * fp, efunc errfunc,
146 ldfunc ldef, evalfunc eval)
148 maxbits = 64;
149 win32 = FALSE; win64 = TRUE;
150 (void)ldef; /* placate optimizers */
151 (void)eval;
152 coff_gen_init(fp, errfunc);
155 static void coff_std_init(FILE * fp, efunc errfunc, ldfunc ldef,
156 evalfunc eval)
158 win32 = win64 = FALSE;
159 (void)ldef; /* placate optimizers */
160 (void)eval;
161 coff_gen_init(fp, errfunc);
164 static void coff_gen_init(FILE * fp, efunc errfunc)
167 coffp = fp;
168 error = errfunc;
169 sects = NULL;
170 nsects = sectlen = 0;
171 syms = saa_init((int32_t)sizeof(struct Symbol));
172 nsyms = 0;
173 bsym = raa_init();
174 symval = raa_init();
175 strs = saa_init(1L);
176 strslen = 0;
177 def_seg = seg_alloc();
180 static void coff_cleanup(int debuginfo)
182 struct Reloc *r;
183 int i;
185 (void)debuginfo;
187 coff_write();
188 fclose(coffp);
189 for (i = 0; i < nsects; i++) {
190 if (sects[i]->data)
191 saa_free(sects[i]->data);
192 while (sects[i]->head) {
193 r = sects[i]->head;
194 sects[i]->head = sects[i]->head->next;
195 nasm_free(r);
197 nasm_free(sects[i]);
199 nasm_free(sects);
200 saa_free(syms);
201 raa_free(bsym);
202 raa_free(symval);
203 saa_free(strs);
206 static int coff_make_section(char *name, uint32_t flags)
208 struct Section *s;
210 s = nasm_malloc(sizeof(*s));
212 if (flags != BSS_FLAGS)
213 s->data = saa_init(1L);
214 else
215 s->data = NULL;
216 s->head = NULL;
217 s->tail = &s->head;
218 s->len = 0;
219 s->nrelocs = 0;
220 if (!strcmp(name, ".text"))
221 s->index = def_seg;
222 else
223 s->index = seg_alloc();
224 strncpy(s->name, name, 8);
225 s->name[8] = '\0';
226 s->flags = flags;
228 if (nsects >= sectlen)
229 sects =
230 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
231 sects[nsects++] = s;
233 return nsects - 1;
236 static int32_t coff_section_names(char *name, int pass, int *bits)
238 char *p;
239 uint32_t flags, align_and = ~0L, align_or = 0L;
240 int i;
243 * Set default bits.
245 if (!name) {
246 if(win64)
247 *bits = 64;
248 else
249 *bits = 32;
252 if (!name)
253 return def_seg;
255 p = name;
256 while (*p && !isspace(*p))
257 p++;
258 if (*p)
259 *p++ = '\0';
260 if (strlen(name) > 8) {
261 error(ERR_WARNING, "COFF section names limited to 8 characters:"
262 " truncating");
263 name[8] = '\0';
265 flags = 0;
267 while (*p && isspace(*p))
268 p++;
269 while (*p) {
270 char *q = p;
271 while (*p && !isspace(*p))
272 p++;
273 if (*p)
274 *p++ = '\0';
275 while (*p && isspace(*p))
276 p++;
278 if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
279 flags = TEXT_FLAGS;
280 } else if (!nasm_stricmp(q, "data")) {
281 flags = DATA_FLAGS;
282 } else if (!nasm_stricmp(q, "rdata")) {
283 if (win32 | win64)
284 flags = RDATA_FLAGS;
285 else {
286 flags = DATA_FLAGS; /* gotta do something */
287 error(ERR_NONFATAL, "standard COFF does not support"
288 " read-only data sections");
290 } else if (!nasm_stricmp(q, "bss")) {
291 flags = BSS_FLAGS;
292 } else if (!nasm_stricmp(q, "info")) {
293 if (win32 | win64)
294 flags = INFO_FLAGS;
295 else {
296 flags = DATA_FLAGS; /* gotta do something */
297 error(ERR_NONFATAL, "standard COFF does not support"
298 " informational sections");
300 } else if (!nasm_strnicmp(q, "align=", 6)) {
301 if (!(win32 | win64))
302 error(ERR_NONFATAL, "standard COFF does not support"
303 " section alignment specification");
304 else {
305 if (q[6 + strspn(q + 6, "0123456789")])
306 error(ERR_NONFATAL,
307 "argument to `align' is not numeric");
308 else {
309 unsigned int align = atoi(q + 6);
310 if (!align || ((align - 1) & align))
311 error(ERR_NONFATAL, "argument to `align' is not a"
312 " power of two");
313 else if (align > 64)
314 error(ERR_NONFATAL, "Win32 cannot align sections"
315 " to better than 64-byte boundaries");
316 else {
317 align_and = ~0x00F00000L;
318 align_or = (align == 1 ? 0x00100000L :
319 align == 2 ? 0x00200000L :
320 align == 4 ? 0x00300000L :
321 align == 8 ? 0x00400000L :
322 align == 16 ? 0x00500000L :
323 align ==
324 32 ? 0x00600000L : 0x00700000L);
331 for (i = 0; i < nsects; i++)
332 if (!strcmp(name, sects[i]->name))
333 break;
334 if (i == nsects) {
335 if (!flags) {
336 if (!strcmp(name, ".data"))
337 flags = DATA_FLAGS;
338 else if (!strcmp(name, ".rdata"))
339 flags = RDATA_FLAGS;
340 else if (!strcmp(name, ".bss"))
341 flags = BSS_FLAGS;
342 else
343 flags = TEXT_FLAGS;
345 i = coff_make_section(name, flags);
346 if (flags)
347 sects[i]->flags = flags;
348 sects[i]->flags &= align_and;
349 sects[i]->flags |= align_or;
350 } else if (pass == 1) {
351 if (flags)
352 error(ERR_WARNING, "section attributes ignored on"
353 " redeclaration of section `%s'", name);
356 return sects[i]->index;
359 static void coff_deflabel(char *name, int32_t segment, int32_t offset,
360 int is_global, char *special)
362 int pos = strslen + 4;
363 struct Symbol *sym;
365 if (special)
366 error(ERR_NONFATAL, "binary format does not support any"
367 " special symbol types");
369 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
370 error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
371 return;
374 if (strlen(name) > 8) {
375 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
376 strslen += 1 + strlen(name);
377 } else
378 pos = -1;
380 sym = saa_wstruct(syms);
382 sym->strpos = pos;
383 if (pos == -1)
384 strcpy(sym->name, name);
385 sym->is_global = !!is_global;
386 if (segment == NO_SEG)
387 sym->section = -1; /* absolute symbol */
388 else {
389 int i;
390 sym->section = 0;
391 for (i = 0; i < nsects; i++)
392 if (segment == sects[i]->index) {
393 sym->section = i + 1;
394 break;
396 if (!sym->section)
397 sym->is_global = TRUE;
399 if (is_global == 2)
400 sym->value = offset;
401 else
402 sym->value = (sym->section == 0 ? 0 : offset);
405 * define the references from external-symbol segment numbers
406 * to these symbol records.
408 if (sym->section == 0) {
409 bsym = raa_write(bsym, segment, nsyms);
412 if (segment != NO_SEG)
413 symval = raa_write(symval, segment, sym->section ? 0 : sym->value);
415 nsyms++;
418 static int32_t coff_add_reloc(struct Section *sect, int32_t segment,
419 int relative, int size64)
421 struct Reloc *r;
423 (void)size64;
425 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
426 sect->tail = &r->next;
427 r->next = NULL;
429 r->address = sect->len;
430 if (segment == NO_SEG)
431 r->symbol = 0, r->symbase = ABS_SYMBOL;
432 else {
433 int i;
434 r->symbase = REAL_SYMBOLS;
435 for (i = 0; i < nsects; i++)
436 if (segment == sects[i]->index) {
437 r->symbol = i * 2;
438 r->symbase = SECT_SYMBOLS;
439 break;
441 if (r->symbase == REAL_SYMBOLS)
442 r->symbol = raa_read(bsym, segment);
444 r->relative = relative;
446 sect->nrelocs++;
449 * Return the fixup for standard COFF common variables.
451 if (r->symbase == REAL_SYMBOLS && !(win32 | win64))
452 return raa_read(symval, segment);
453 else
454 return 0;
457 static void coff_out(int32_t segto, const void *data, uint32_t type,
458 int32_t segment, int32_t wrt)
460 struct Section *s;
461 int32_t realbytes = type & OUT_SIZMASK;
462 uint8_t mydata[8], *p;
463 int i;
465 if (wrt != NO_SEG) {
466 wrt = NO_SEG; /* continue to do _something_ */
467 error(ERR_NONFATAL, "WRT not supported by COFF output formats");
470 type &= OUT_TYPMASK;
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 realbytes = 2;
501 else if (type == OUT_REL4ADR)
502 realbytes = 4;
503 s->len += realbytes;
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, realbytes);
512 } else
513 s->len += realbytes;
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, realbytes);
518 } else if (type == OUT_ADDRESS) {
519 if (!(win64)) {
520 if (realbytes != 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, *(int32_t *)data + fix);
537 coff_sect_write(s, mydata, realbytes);
539 } else {
540 int32_t fix = 0;
541 p = mydata;
542 if (realbytes == 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, realbytes);
556 } else {
557 fix = coff_add_reloc(s, segment, FALSE, FALSE);
558 WRITELONG(p, *(int32_t *)data + fix);
559 coff_sect_write(s, mydata, realbytes);
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, *(int32_t *)data + 4 - realbytes + fix);
581 } else {
582 WRITELONG(p, *(int32_t *)data - (realbytes + 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 < 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