Merge branch 'nasm-2.07.xx'
[nasm.git] / output / outcoff.c
blobd508f9893458c38a84552912a15265afdf87707d
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
34 /*
35 * outcoff.c output routines for the Netwide Assembler to produce
36 * COFF object files (for DJGPP and Win32)
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <time.h>
46 #include <inttypes.h>
48 #include "nasm.h"
49 #include "nasmlib.h"
50 #include "saa.h"
51 #include "raa.h"
52 #include "eval.h"
53 #include "output/outform.h"
54 #include "output/outlib.h"
56 #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
59 * Notes on COFF:
61 * (0) When I say `standard COFF' below, I mean `COFF as output and
62 * used by DJGPP'. I assume DJGPP gets it right.
64 * (1) Win32 appears to interpret the term `relative relocation'
65 * differently from standard COFF. Standard COFF understands a
66 * relative relocation to mean that during relocation you add the
67 * address of the symbol you're referencing, and subtract the base
68 * address of the section you're in. Win32 COFF, by contrast, seems
69 * to add the address of the symbol and then subtract the address
70 * of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
71 * subtly incompatible.
73 * (2) Win32 doesn't bother putting any flags in the header flags
74 * field (at offset 0x12 into the file).
76 * (3) Win32 uses some extra flags into the section header table:
77 * it defines flags 0x80000000 (writable), 0x40000000 (readable)
78 * and 0x20000000 (executable), and uses them in the expected
79 * combinations. It also defines 0x00100000 through 0x00700000 for
80 * section alignments of 1 through 64 bytes.
82 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
83 * field directly after the section name in the section header
84 * table for something strange: they store what the address of the
85 * section start point _would_ be, if you laid all the sections end
86 * to end starting at zero. Dunno why. Microsoft's documentation
87 * lists this field as "Virtual Size of Section", which doesn't
88 * seem to fit at all. In fact, Win32 even includes non-linked
89 * sections such as .drectve in this calculation.
91 * Newer versions of MASM seem to have changed this to be zero, and
92 * that apparently matches the COFF spec, so go with that.
94 * (5) Standard COFF does something very strange to common
95 * variables: the relocation point for a common variable is as far
96 * _before_ the variable as its size stretches out _after_ it. So
97 * we must fix up common variable references. Win32 seems to be
98 * sensible on this one.
101 /* Flag which version of COFF we are currently outputting. */
102 static bool win32, win64;
104 static int32_t imagebase_sect;
105 #define WRT_IMAGEBASE "..imagebase"
107 struct Reloc {
108 struct Reloc *next;
109 int32_t address; /* relative to _start_ of section */
110 int32_t symbol; /* symbol number */
111 enum {
112 SECT_SYMBOLS,
113 ABS_SYMBOL,
114 REAL_SYMBOLS
115 } symbase; /* relocation for symbol number :) */
116 int16_t type;
119 /* possible values for Reloc->type */
120 #define IMAGE_REL_AMD64_ADDR64 0x0001
121 #define IMAGE_REL_AMD64_ADDR32 0x0002
122 #define IMAGE_REL_AMD64_ADDR32NB 0x0003
123 #define IMAGE_REL_AMD64_REL32 0x0004
124 #define IMAGE_REL_I386_DIR32 0x0006
125 #define IMAGE_REL_I386_DIR32NB 0x0007
126 #define IMAGE_REL_I386_REL32 0x0014
128 struct Symbol {
129 char name[9];
130 int32_t strpos; /* string table position of name */
131 int32_t value; /* address, or COMMON variable size */
132 int section; /* section number where it's defined
133 * - in COFF codes, not NASM codes */
134 bool is_global; /* is it a global symbol or not? */
135 int16_t type; /* 0 - notype, 0x20 - function */
136 int32_t namlen; /* full name length */
139 static char coff_infile[FILENAME_MAX];
141 struct Section {
142 struct SAA *data;
143 uint32_t len;
144 int nrelocs;
145 int32_t index;
146 struct Reloc *head, **tail;
147 uint32_t flags; /* section flags */
148 char name[9];
149 int32_t pos, relpos;
152 #define TEXT_FLAGS ((win32 | win64) ? 0x60500020L : 0x20L)
153 #define DATA_FLAGS ((win32 | win64) ? 0xC0300040L : 0x40L)
154 #define BSS_FLAGS ((win32 | win64) ? 0xC0300080L : 0x80L)
155 #define INFO_FLAGS 0x00100A00L
156 #define RDATA_FLAGS ((win32 | win64) ? 0x40400040L : 0x40L)
158 #define SECT_DELTA 32
159 static struct Section **sects;
160 static int nsects, sectlen;
162 static struct SAA *syms;
163 static uint32_t nsyms;
165 static int32_t def_seg;
167 static int initsym;
169 static struct RAA *bsym, *symval;
171 static struct SAA *strs;
172 static uint32_t strslen;
174 static void coff_gen_init(void);
175 static void coff_sect_write(struct Section *, const uint8_t *,
176 uint32_t);
177 static void coff_write(void);
178 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
179 static void coff_write_relocs(struct Section *);
180 static void coff_write_symbols(void);
182 static void coff_win32_init(void)
184 win32 = true;
185 win64 = false;
186 coff_gen_init();
189 static void coff_win64_init(void)
191 maxbits = 64;
192 win32 = false;
193 win64 = true;
194 coff_gen_init();
195 imagebase_sect = seg_alloc()+1;
196 define_label(WRT_IMAGEBASE, imagebase_sect, 0, NULL, false, false);
199 static void coff_std_init(void)
201 win32 = win64 = false;
202 coff_gen_init();
205 static void coff_gen_init(void)
208 sects = NULL;
209 nsects = sectlen = 0;
210 syms = saa_init(sizeof(struct Symbol));
211 nsyms = 0;
212 bsym = raa_init();
213 symval = raa_init();
214 strs = saa_init(1);
215 strslen = 0;
216 def_seg = seg_alloc();
219 static void coff_cleanup(int debuginfo)
221 struct Reloc *r;
222 int i;
224 (void)debuginfo;
226 coff_write();
227 for (i = 0; i < nsects; i++) {
228 if (sects[i]->data)
229 saa_free(sects[i]->data);
230 while (sects[i]->head) {
231 r = sects[i]->head;
232 sects[i]->head = sects[i]->head->next;
233 nasm_free(r);
235 nasm_free(sects[i]);
237 nasm_free(sects);
238 saa_free(syms);
239 raa_free(bsym);
240 raa_free(symval);
241 saa_free(strs);
244 static int coff_make_section(char *name, uint32_t flags)
246 struct Section *s;
248 s = nasm_malloc(sizeof(*s));
250 if (flags != BSS_FLAGS)
251 s->data = saa_init(1);
252 else
253 s->data = NULL;
254 s->head = NULL;
255 s->tail = &s->head;
256 s->len = 0;
257 s->nrelocs = 0;
258 if (!strcmp(name, ".text"))
259 s->index = def_seg;
260 else
261 s->index = seg_alloc();
262 strncpy(s->name, name, 8);
263 s->name[8] = '\0';
264 s->flags = flags;
266 if (nsects >= sectlen) {
267 sectlen += SECT_DELTA;
268 sects = nasm_realloc(sects, sectlen * sizeof(*sects));
270 sects[nsects++] = s;
272 return nsects - 1;
275 static int32_t coff_section_names(char *name, int pass, int *bits)
277 char *p;
278 uint32_t flags, align_and = ~0L, align_or = 0L;
279 int i;
282 * Set default bits.
284 if (!name) {
285 if(win64)
286 *bits = 64;
287 else
288 *bits = 32;
291 if (!name)
292 return def_seg;
294 p = name;
295 while (*p && !nasm_isspace(*p))
296 p++;
297 if (*p)
298 *p++ = '\0';
299 if (strlen(name) > 8) {
300 nasm_error(ERR_WARNING, "COFF section names limited to 8 characters:"
301 " truncating");
302 name[8] = '\0';
304 flags = 0;
306 while (*p && nasm_isspace(*p))
307 p++;
308 while (*p) {
309 char *q = p;
310 while (*p && !nasm_isspace(*p))
311 p++;
312 if (*p)
313 *p++ = '\0';
314 while (*p && nasm_isspace(*p))
315 p++;
317 if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
318 flags = TEXT_FLAGS;
319 } else if (!nasm_stricmp(q, "data")) {
320 flags = DATA_FLAGS;
321 } else if (!nasm_stricmp(q, "rdata")) {
322 if (win32 | win64)
323 flags = RDATA_FLAGS;
324 else {
325 flags = DATA_FLAGS; /* gotta do something */
326 nasm_error(ERR_NONFATAL, "standard COFF does not support"
327 " read-only data sections");
329 } else if (!nasm_stricmp(q, "bss")) {
330 flags = BSS_FLAGS;
331 } else if (!nasm_stricmp(q, "info")) {
332 if (win32 | win64)
333 flags = INFO_FLAGS;
334 else {
335 flags = DATA_FLAGS; /* gotta do something */
336 nasm_error(ERR_NONFATAL, "standard COFF does not support"
337 " informational sections");
339 } else if (!nasm_strnicmp(q, "align=", 6)) {
340 if (!(win32 | win64))
341 nasm_error(ERR_NONFATAL, "standard COFF does not support"
342 " section alignment specification");
343 else {
344 if (q[6 + strspn(q + 6, "0123456789")])
345 nasm_error(ERR_NONFATAL,
346 "argument to `align' is not numeric");
347 else {
348 unsigned int align = atoi(q + 6);
349 if (!align || ((align - 1) & align))
350 nasm_error(ERR_NONFATAL, "argument to `align' is not a"
351 " power of two");
352 else if (align > 64)
353 nasm_error(ERR_NONFATAL, "Win32 cannot align sections"
354 " to better than 64-byte boundaries");
355 else {
356 align_and = ~0x00F00000L;
357 align_or = (align == 1 ? 0x00100000L :
358 align == 2 ? 0x00200000L :
359 align == 4 ? 0x00300000L :
360 align == 8 ? 0x00400000L :
361 align == 16 ? 0x00500000L :
362 align ==
363 32 ? 0x00600000L : 0x00700000L);
370 for (i = 0; i < nsects; i++)
371 if (!strcmp(name, sects[i]->name))
372 break;
373 if (i == nsects) {
374 if (!flags) {
375 if (!strcmp(name, ".data"))
376 flags = DATA_FLAGS;
377 else if (!strcmp(name, ".rdata"))
378 flags = RDATA_FLAGS;
379 else if (!strcmp(name, ".bss"))
380 flags = BSS_FLAGS;
381 else if (win64 && !strcmp(name, ".pdata"))
382 flags = 0x40300040; /* rdata align=4 */
383 else if (win64 && !strcmp(name, ".xdata"))
384 flags = 0x40400040; /* rdate align=8 */
385 else
386 flags = TEXT_FLAGS;
388 i = coff_make_section(name, flags);
389 if (flags)
390 sects[i]->flags = flags;
391 sects[i]->flags &= align_and;
392 sects[i]->flags |= align_or;
393 } else if (pass == 1) {
394 if (flags)
395 nasm_error(ERR_WARNING, "section attributes ignored on"
396 " redeclaration of section `%s'", name);
399 return sects[i]->index;
402 static void coff_deflabel(char *name, int32_t segment, int64_t offset,
403 int is_global, char *special)
405 int pos = strslen + 4;
406 struct Symbol *sym;
408 if (special)
409 nasm_error(ERR_NONFATAL, "COFF format does not support any"
410 " special symbol types");
412 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
413 if (strcmp(name,WRT_IMAGEBASE))
414 nasm_error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
415 return;
418 if (strlen(name) > 8) {
419 size_t nlen = strlen(name)+1;
420 saa_wbytes(strs, name, nlen);
421 strslen += nlen;
422 } else
423 pos = -1;
425 sym = saa_wstruct(syms);
427 sym->strpos = pos;
428 sym->namlen = strlen(name);
429 if (pos == -1)
430 strcpy(sym->name, name);
431 sym->is_global = !!is_global;
432 sym->type = 0; /* Default to T_NULL (no type) */
433 if (segment == NO_SEG)
434 sym->section = -1; /* absolute symbol */
435 else {
436 int i;
437 sym->section = 0;
438 for (i = 0; i < nsects; i++)
439 if (segment == sects[i]->index) {
440 sym->section = i + 1;
441 break;
443 if (!sym->section)
444 sym->is_global = true;
446 if (is_global == 2)
447 sym->value = offset;
448 else
449 sym->value = (sym->section == 0 ? 0 : offset);
452 * define the references from external-symbol segment numbers
453 * to these symbol records.
455 if (sym->section == 0) {
456 bsym = raa_write(bsym, segment, nsyms);
459 if (segment != NO_SEG)
460 symval = raa_write(symval, segment, sym->section ? 0 : sym->value);
462 nsyms++;
465 static int32_t coff_add_reloc(struct Section *sect, int32_t segment,
466 int16_t type)
468 struct Reloc *r;
470 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
471 sect->tail = &r->next;
472 r->next = NULL;
474 r->address = sect->len;
475 if (segment == NO_SEG)
476 r->symbol = 0, r->symbase = ABS_SYMBOL;
477 else {
478 int i;
479 r->symbase = REAL_SYMBOLS;
480 for (i = 0; i < nsects; i++)
481 if (segment == sects[i]->index) {
482 r->symbol = i * 2;
483 r->symbase = SECT_SYMBOLS;
484 break;
486 if (r->symbase == REAL_SYMBOLS)
487 r->symbol = raa_read(bsym, segment);
489 r->type = type;
491 sect->nrelocs++;
494 * Return the fixup for standard COFF common variables.
496 if (r->symbase == REAL_SYMBOLS && !(win32 | win64))
497 return raa_read(symval, segment);
498 else
499 return 0;
502 static void coff_out(int32_t segto, const void *data,
503 enum out_type type, uint64_t size,
504 int32_t segment, int32_t wrt)
506 struct Section *s;
507 uint8_t mydata[8], *p;
508 int i;
510 if (wrt != NO_SEG && !win64) {
511 wrt = NO_SEG; /* continue to do _something_ */
512 nasm_error(ERR_NONFATAL, "WRT not supported by COFF output formats");
516 * handle absolute-assembly (structure definitions)
518 if (segto == NO_SEG) {
519 if (type != OUT_RESERVE)
520 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
521 " space");
522 return;
525 s = NULL;
526 for (i = 0; i < nsects; i++)
527 if (segto == sects[i]->index) {
528 s = sects[i];
529 break;
531 if (!s) {
532 int tempint; /* ignored */
533 if (segto != coff_section_names(".text", 2, &tempint))
534 nasm_error(ERR_PANIC, "strange segment conditions in COFF driver");
535 else
536 s = sects[nsects - 1];
539 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
540 if (win64 && wrt == NO_SEG &&
541 (!strcmp(s->name,".pdata") || !strcmp(s->name,".xdata")))
542 wrt = imagebase_sect;
544 if (!s->data && type != OUT_RESERVE) {
545 nasm_error(ERR_WARNING, "attempt to initialize memory in"
546 " BSS section `%s': ignored", s->name);
547 s->len += realsize(type, size);
548 return;
551 if (type == OUT_RESERVE) {
552 if (s->data) {
553 nasm_error(ERR_WARNING, "uninitialised space declared in"
554 " non-BSS section `%s': zeroing", s->name);
555 coff_sect_write(s, NULL, size);
556 } else
557 s->len += size;
558 } else if (type == OUT_RAWDATA) {
559 if (segment != NO_SEG)
560 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
561 coff_sect_write(s, data, size);
562 } else if (type == OUT_ADDRESS) {
563 if (!(win64)) {
564 if (size != 4 && (segment != NO_SEG || wrt != NO_SEG))
565 nasm_error(ERR_NONFATAL, "COFF format does not support non-32-bit"
566 " relocations");
567 else {
568 int32_t fix = 0;
569 if (segment != NO_SEG || wrt != NO_SEG) {
570 if (wrt != NO_SEG) {
571 nasm_error(ERR_NONFATAL, "COFF format does not support"
572 " WRT types");
573 } else if (segment % 2) {
574 nasm_error(ERR_NONFATAL, "COFF format does not support"
575 " segment base references");
576 } else
577 fix = coff_add_reloc(s, segment, IMAGE_REL_I386_DIR32);
579 p = mydata;
580 WRITELONG(p, *(int64_t *)data + fix);
581 coff_sect_write(s, mydata, size);
583 } else {
584 int32_t fix = 0;
585 p = mydata;
586 if (size == 8) {
587 if (wrt == imagebase_sect) {
588 nasm_error(ERR_NONFATAL, "operand size mismatch: 'wrt "
589 WRT_IMAGEBASE "' is a 32-bit operand");
591 fix = coff_add_reloc(s, segment, IMAGE_REL_AMD64_ADDR64);
592 WRITEDLONG(p, *(int64_t *)data + fix);
593 coff_sect_write(s, mydata, size);
594 } else {
595 fix = coff_add_reloc(s, segment,
596 wrt == imagebase_sect ? IMAGE_REL_AMD64_ADDR32NB:
597 IMAGE_REL_AMD64_ADDR32);
598 WRITELONG(p, *(int64_t *)data + fix);
599 coff_sect_write(s, mydata, size);
602 } else if (type == OUT_REL2ADR) {
603 nasm_error(ERR_NONFATAL, "COFF format does not support 16-bit"
604 " relocations");
605 } else if (type == OUT_REL4ADR) {
606 if (segment == segto && !(win64)) /* Acceptable for RIP-relative */
607 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
608 else if (segment == NO_SEG && win32)
609 nasm_error(ERR_NONFATAL, "Win32 COFF does not correctly support"
610 " relative references to absolute addresses");
611 else {
612 int32_t fix = 0;
613 if (segment != NO_SEG && segment % 2) {
614 nasm_error(ERR_NONFATAL, "COFF format does not support"
615 " segment base references");
616 } else
617 fix = coff_add_reloc(s, segment,
618 win64 ? IMAGE_REL_AMD64_REL32 : IMAGE_REL_I386_REL32);
619 p = mydata;
620 if (win32 | win64) {
621 WRITELONG(p, *(int64_t *)data + 4 - size + fix);
622 } else {
623 WRITELONG(p, *(int64_t *)data - (size + s->len) + fix);
625 coff_sect_write(s, mydata, 4L);
631 static void coff_sect_write(struct Section *sect,
632 const uint8_t *data, uint32_t len)
634 saa_wbytes(sect->data, data, len);
635 sect->len += len;
638 typedef struct tagString {
639 struct tagString *Next;
640 int len;
641 char *String;
642 } STRING;
644 #define EXPORT_SECTION_NAME ".drectve"
645 #define EXPORT_SECTION_FLAGS INFO_FLAGS
647 #define EXPORT_SECTION_NAME ".text"
648 #define EXPORT_SECTION_FLAGS TEXT_FLAGS
651 static STRING *Exports = NULL;
652 static struct Section *directive_sec;
653 void AddExport(char *name)
655 STRING *rvp = Exports, *newS;
657 newS = (STRING *) nasm_malloc(sizeof(STRING));
658 newS->len = strlen(name);
659 newS->Next = NULL;
660 newS->String = (char *)nasm_malloc(newS->len + 1);
661 strcpy(newS->String, name);
662 if (rvp == NULL) {
663 int i;
665 for (i = 0; i < nsects; i++) {
666 if (!strcmp(EXPORT_SECTION_NAME, sects[i]->name))
667 break;
670 if (i == nsects)
671 i = coff_make_section(EXPORT_SECTION_NAME, EXPORT_SECTION_FLAGS);
673 directive_sec = sects[i];
674 Exports = newS;
675 } else {
676 while (rvp->Next) {
677 if (!strcmp(rvp->String, name))
678 return;
679 rvp = rvp->Next;
681 rvp->Next = newS;
685 void BuildExportTable(void)
687 STRING *rvp = Exports, *next;
688 uint8_t buf[256];
689 int len;
690 if (rvp == NULL)
691 return;
692 while (rvp) {
693 len = sprintf((char *)buf, "-export:%s ", rvp->String);
694 coff_sect_write(directive_sec, buf, len);
695 rvp = rvp->Next;
698 next = Exports;
699 while ((rvp = next)) {
700 next = rvp->Next;
701 nasm_free(rvp->String);
702 nasm_free(rvp);
704 Exports = NULL;
707 static int coff_directives(enum directives directive, char *value, int pass)
709 switch (directive) {
710 case D_EXPORT:
712 char *q, *name;
714 if (pass == 2)
715 return 1; /* ignore in pass two */
716 name = q = value;
717 while (*q && !nasm_isspace(*q))
718 q++;
719 if (nasm_isspace(*q)) {
720 *q++ = '\0';
721 while (*q && nasm_isspace(*q))
722 q++;
725 if (!*name) {
726 nasm_error(ERR_NONFATAL, "`export' directive requires export name");
727 return 1;
729 if (*q) {
730 nasm_error(ERR_NONFATAL, "unrecognized export qualifier `%s'", q);
731 return 1;
733 AddExport(name);
734 return 1;
736 case D_SAFESEH:
738 static int sxseg=-1;
739 int i;
741 if (!win32) /* Only applicable for -f win32 */
742 return 0;
744 if (sxseg==-1)
745 { for (i = 0; i < nsects; i++)
746 if (!strcmp(".sxdata",sects[i]->name))
747 break;
748 if (i == nsects)
749 sxseg = coff_make_section(".sxdata",0x200);
750 else
751 sxseg = i;
753 /* pass0 == 2 is the only time when the full set of symbols are
754 guaranteed to be present; it is the final output pass. */
755 if (pass0 == 2) {
756 uint32_t n;
757 saa_rewind(syms);
758 for (n = 0; n < nsyms; n++) {
759 struct Symbol *sym = saa_rstruct(syms);
760 bool equals;
762 /* sym->strpos is biased by 4, because symbol
763 * table is prefixed with table length */
764 if (sym->strpos >=4) {
765 char *name = nasm_malloc(sym->namlen+1);
766 saa_fread(strs, sym->strpos-4, name, sym->namlen);
767 name[sym->namlen] = '\0';
768 equals = !strcmp(value,name);
769 nasm_free(name);
771 else
772 equals = !strcmp(value,sym->name);
774 if (equals) {
775 /* this value arithmetics effectively reflects
776 * initsym in coff_write(): 2 for file, 1 for
777 * .absolute and two per each section */
778 unsigned char value[4],*p=value;
779 WRITELONG(p,n + 2 + 1 + nsects*2);
780 coff_sect_write(sects[sxseg],value,4);
781 sym->type = 0x20;
782 break;
785 if (n == nsyms) {
786 nasm_error(ERR_NONFATAL,
787 "`safeseh' directive requires valid symbol");
790 return 1;
792 default:
793 return 0;
797 static void coff_write(void)
799 int32_t pos, sympos, vsize;
800 int i;
802 BuildExportTable(); /* fill in the .drectve section with -export's */
804 if (win32) {
805 /* add default value for @feat.00, this allows to 'link /safeseh' */
806 uint32_t n;
808 saa_rewind(syms);
809 for (n = 0; n < nsyms; n++) {
810 struct Symbol *sym = saa_rstruct(syms);
811 if (sym->strpos == -1 && !strcmp("@feat.00",sym->name))
812 break;
814 if (n == nsyms)
815 coff_deflabel("@feat.00",NO_SEG,1,0,NULL);
819 * Work out how big the file will get. Calculate the start of
820 * the `real' symbols at the same time.
822 pos = 0x14 + 0x28 * nsects;
823 initsym = 3; /* two for the file, one absolute */
824 for (i = 0; i < nsects; i++) {
825 if (sects[i]->data) {
826 sects[i]->pos = pos;
827 pos += sects[i]->len;
828 sects[i]->relpos = pos;
829 pos += 10 * sects[i]->nrelocs;
830 } else
831 sects[i]->pos = sects[i]->relpos = 0L;
832 initsym += 2; /* two for each section */
834 sympos = pos;
837 * Output the COFF header.
839 if (win64)
840 fwriteint16_t(0x8664, ofile); /* MACHINE_x86-64 */
841 else
842 fwriteint16_t(0x014C, ofile); /* MACHINE_i386 */
843 fwriteint16_t(nsects, ofile); /* number of sections */
844 fwriteint32_t(time(NULL), ofile); /* time stamp */
845 fwriteint32_t(sympos, ofile);
846 fwriteint32_t(nsyms + initsym, ofile);
847 fwriteint16_t(0, ofile); /* no optional header */
848 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
849 fwriteint16_t((win32 | win64) ? 0 : 0x104, ofile);
852 * Output the section headers.
854 vsize = 0L;
855 for (i = 0; i < nsects; i++) {
856 coff_section_header(sects[i]->name, vsize, sects[i]->len,
857 sects[i]->pos, sects[i]->relpos,
858 sects[i]->nrelocs, sects[i]->flags);
859 vsize += sects[i]->len;
863 * Output the sections and their relocations.
865 for (i = 0; i < nsects; i++)
866 if (sects[i]->data) {
867 saa_fpwrite(sects[i]->data, ofile);
868 coff_write_relocs(sects[i]);
872 * Output the symbol and string tables.
874 coff_write_symbols();
875 fwriteint32_t(strslen + 4, ofile); /* length includes length count */
876 saa_fpwrite(strs, ofile);
879 static void coff_section_header(char *name, int32_t vsize,
880 int32_t datalen, int32_t datapos,
881 int32_t relpos, int nrelocs, int32_t flags)
883 char padname[8];
885 (void)vsize;
887 memset(padname, 0, 8);
888 strncpy(padname, name, 8);
889 fwrite(padname, 8, 1, ofile);
890 fwriteint32_t(0, ofile); /* Virtual size field - set to 0 or vsize */
891 fwriteint32_t(0L, ofile); /* RVA/offset - we ignore */
892 fwriteint32_t(datalen, ofile);
893 fwriteint32_t(datapos, ofile);
894 fwriteint32_t(relpos, ofile);
895 fwriteint32_t(0L, ofile); /* no line numbers - we don't do 'em */
896 fwriteint16_t(nrelocs, ofile);
897 fwriteint16_t(0, ofile); /* again, no line numbers */
898 fwriteint32_t(flags, ofile);
901 static void coff_write_relocs(struct Section *s)
903 struct Reloc *r;
905 for (r = s->head; r; r = r->next) {
906 fwriteint32_t(r->address, ofile);
907 fwriteint32_t(r->symbol + (r->symbase == REAL_SYMBOLS ? initsym :
908 r->symbase == ABS_SYMBOL ? initsym - 1 :
909 r->symbase == SECT_SYMBOLS ? 2 : 0),
910 ofile);
911 fwriteint16_t(r->type, ofile);
915 static void coff_symbol(char *name, int32_t strpos, int32_t value,
916 int section, int type, int storageclass, int aux)
918 char padname[8];
920 if (name) {
921 memset(padname, 0, 8);
922 strncpy(padname, name, 8);
923 fwrite(padname, 8, 1, ofile);
924 } else {
925 fwriteint32_t(0, ofile);
926 fwriteint32_t(strpos, ofile);
928 fwriteint32_t(value, ofile);
929 fwriteint16_t(section, ofile);
930 fwriteint16_t(type, ofile);
931 fputc(storageclass, ofile);
932 fputc(aux, ofile);
935 static void coff_write_symbols(void)
937 char filename[18];
938 uint32_t i;
941 * The `.file' record, and the file name auxiliary record.
943 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
944 memset(filename, 0, 18);
945 strncpy(filename, coff_infile, 18);
946 fwrite(filename, 18, 1, ofile);
949 * The section records, with their auxiliaries.
951 memset(filename, 0, 18); /* useful zeroed buffer */
953 for (i = 0; i < (uint32_t) nsects; i++) {
954 coff_symbol(sects[i]->name, 0L, 0L, i + 1, 0, 3, 1);
955 fwriteint32_t(sects[i]->len, ofile);
956 fwriteint16_t(sects[i]->nrelocs, ofile);
957 fwrite(filename, 12, 1, ofile);
961 * The absolute symbol, for relative-to-absolute relocations.
963 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
966 * The real symbols.
968 saa_rewind(syms);
969 for (i = 0; i < nsyms; i++) {
970 struct Symbol *sym = saa_rstruct(syms);
971 coff_symbol(sym->strpos == -1 ? sym->name : NULL,
972 sym->strpos, sym->value, sym->section,
973 sym->type, sym->is_global ? 2 : 3, 0);
977 static int32_t coff_segbase(int32_t segment)
979 return segment;
982 static void coff_std_filename(char *inname, char *outname)
984 strcpy(coff_infile, inname);
985 standard_extension(inname, outname, ".o");
988 static void coff_win32_filename(char *inname, char *outname)
990 strcpy(coff_infile, inname);
991 standard_extension(inname, outname, ".obj");
994 extern macros_t coff_stdmac[];
996 static int coff_set_info(enum geninfo type, char **val)
998 (void)type;
999 (void)val;
1000 return 0;
1002 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
1004 #ifdef OF_COFF
1006 struct ofmt of_coff = {
1007 "COFF (i386) object files (e.g. DJGPP for DOS)",
1008 "coff",
1010 null_debug_arr,
1011 &null_debug_form,
1012 coff_stdmac,
1013 coff_std_init,
1014 coff_set_info,
1015 coff_out,
1016 coff_deflabel,
1017 coff_section_names,
1018 coff_segbase,
1019 coff_directives,
1020 coff_std_filename,
1021 coff_cleanup
1024 #endif
1026 #ifdef OF_WIN32
1028 struct ofmt of_win32 = {
1029 "Microsoft Win32 (i386) object files",
1030 "win32",
1032 null_debug_arr,
1033 &null_debug_form,
1034 coff_stdmac,
1035 coff_win32_init,
1036 coff_set_info,
1037 coff_out,
1038 coff_deflabel,
1039 coff_section_names,
1040 coff_segbase,
1041 coff_directives,
1042 coff_win32_filename,
1043 coff_cleanup
1046 #endif
1048 #ifdef OF_WIN64
1050 struct ofmt of_win64 = {
1051 "Microsoft Win64 (x86-64) object files",
1052 "win64",
1054 null_debug_arr,
1055 &null_debug_form,
1056 coff_stdmac,
1057 coff_win64_init,
1058 coff_set_info,
1059 coff_out,
1060 coff_deflabel,
1061 coff_section_names,
1062 coff_segbase,
1063 coff_directives,
1064 coff_win32_filename,
1065 coff_cleanup
1068 #endif