Remove function pointers in output, simplify error handling
[nasm/sigaren-mirror.git] / output / outcoff.c
blobf147ea98ed8813888ba764ba26bd1c8d8079cb75
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,
197 ofmt, nasm_error);
200 static void coff_std_init(void)
202 win32 = win64 = false;
203 coff_gen_init();
206 static void coff_gen_init(void)
209 sects = NULL;
210 nsects = sectlen = 0;
211 syms = saa_init(sizeof(struct Symbol));
212 nsyms = 0;
213 bsym = raa_init();
214 symval = raa_init();
215 strs = saa_init(1);
216 strslen = 0;
217 def_seg = seg_alloc();
220 static void coff_cleanup(int debuginfo)
222 struct Reloc *r;
223 int i;
225 (void)debuginfo;
227 coff_write();
228 for (i = 0; i < nsects; i++) {
229 if (sects[i]->data)
230 saa_free(sects[i]->data);
231 while (sects[i]->head) {
232 r = sects[i]->head;
233 sects[i]->head = sects[i]->head->next;
234 nasm_free(r);
236 nasm_free(sects[i]);
238 nasm_free(sects);
239 saa_free(syms);
240 raa_free(bsym);
241 raa_free(symval);
242 saa_free(strs);
245 static int coff_make_section(char *name, uint32_t flags)
247 struct Section *s;
249 s = nasm_malloc(sizeof(*s));
251 if (flags != BSS_FLAGS)
252 s->data = saa_init(1);
253 else
254 s->data = NULL;
255 s->head = NULL;
256 s->tail = &s->head;
257 s->len = 0;
258 s->nrelocs = 0;
259 if (!strcmp(name, ".text"))
260 s->index = def_seg;
261 else
262 s->index = seg_alloc();
263 strncpy(s->name, name, 8);
264 s->name[8] = '\0';
265 s->flags = flags;
267 if (nsects >= sectlen) {
268 sectlen += SECT_DELTA;
269 sects = nasm_realloc(sects, sectlen * sizeof(*sects));
271 sects[nsects++] = s;
273 return nsects - 1;
276 static int32_t coff_section_names(char *name, int pass, int *bits)
278 char *p;
279 uint32_t flags, align_and = ~0L, align_or = 0L;
280 int i;
283 * Set default bits.
285 if (!name) {
286 if(win64)
287 *bits = 64;
288 else
289 *bits = 32;
292 if (!name)
293 return def_seg;
295 p = name;
296 while (*p && !nasm_isspace(*p))
297 p++;
298 if (*p)
299 *p++ = '\0';
300 if (strlen(name) > 8) {
301 nasm_error(ERR_WARNING, "COFF section names limited to 8 characters:"
302 " truncating");
303 name[8] = '\0';
305 flags = 0;
307 while (*p && nasm_isspace(*p))
308 p++;
309 while (*p) {
310 char *q = p;
311 while (*p && !nasm_isspace(*p))
312 p++;
313 if (*p)
314 *p++ = '\0';
315 while (*p && nasm_isspace(*p))
316 p++;
318 if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
319 flags = TEXT_FLAGS;
320 } else if (!nasm_stricmp(q, "data")) {
321 flags = DATA_FLAGS;
322 } else if (!nasm_stricmp(q, "rdata")) {
323 if (win32 | win64)
324 flags = RDATA_FLAGS;
325 else {
326 flags = DATA_FLAGS; /* gotta do something */
327 nasm_error(ERR_NONFATAL, "standard COFF does not support"
328 " read-only data sections");
330 } else if (!nasm_stricmp(q, "bss")) {
331 flags = BSS_FLAGS;
332 } else if (!nasm_stricmp(q, "info")) {
333 if (win32 | win64)
334 flags = INFO_FLAGS;
335 else {
336 flags = DATA_FLAGS; /* gotta do something */
337 nasm_error(ERR_NONFATAL, "standard COFF does not support"
338 " informational sections");
340 } else if (!nasm_strnicmp(q, "align=", 6)) {
341 if (!(win32 | win64))
342 nasm_error(ERR_NONFATAL, "standard COFF does not support"
343 " section alignment specification");
344 else {
345 if (q[6 + strspn(q + 6, "0123456789")])
346 nasm_error(ERR_NONFATAL,
347 "argument to `align' is not numeric");
348 else {
349 unsigned int align = atoi(q + 6);
350 if (!align || ((align - 1) & align))
351 nasm_error(ERR_NONFATAL, "argument to `align' is not a"
352 " power of two");
353 else if (align > 64)
354 nasm_error(ERR_NONFATAL, "Win32 cannot align sections"
355 " to better than 64-byte boundaries");
356 else {
357 align_and = ~0x00F00000L;
358 align_or = (align == 1 ? 0x00100000L :
359 align == 2 ? 0x00200000L :
360 align == 4 ? 0x00300000L :
361 align == 8 ? 0x00400000L :
362 align == 16 ? 0x00500000L :
363 align ==
364 32 ? 0x00600000L : 0x00700000L);
371 for (i = 0; i < nsects; i++)
372 if (!strcmp(name, sects[i]->name))
373 break;
374 if (i == nsects) {
375 if (!flags) {
376 if (!strcmp(name, ".data"))
377 flags = DATA_FLAGS;
378 else if (!strcmp(name, ".rdata"))
379 flags = RDATA_FLAGS;
380 else if (!strcmp(name, ".bss"))
381 flags = BSS_FLAGS;
382 else if (win64 && !strcmp(name, ".pdata"))
383 flags = 0x40300040; /* rdata align=4 */
384 else if (win64 && !strcmp(name, ".xdata"))
385 flags = 0x40400040; /* rdate align=8 */
386 else
387 flags = TEXT_FLAGS;
389 i = coff_make_section(name, flags);
390 if (flags)
391 sects[i]->flags = flags;
392 sects[i]->flags &= align_and;
393 sects[i]->flags |= align_or;
394 } else if (pass == 1) {
395 if (flags)
396 nasm_error(ERR_WARNING, "section attributes ignored on"
397 " redeclaration of section `%s'", name);
400 return sects[i]->index;
403 static void coff_deflabel(char *name, int32_t segment, int64_t offset,
404 int is_global, char *special)
406 int pos = strslen + 4;
407 struct Symbol *sym;
409 if (special)
410 nasm_error(ERR_NONFATAL, "COFF format does not support any"
411 " special symbol types");
413 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
414 if (strcmp(name,WRT_IMAGEBASE))
415 nasm_error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
416 return;
419 if (strlen(name) > 8) {
420 size_t nlen = strlen(name)+1;
421 saa_wbytes(strs, name, nlen);
422 strslen += nlen;
423 } else
424 pos = -1;
426 sym = saa_wstruct(syms);
428 sym->strpos = pos;
429 sym->namlen = strlen(name);
430 if (pos == -1)
431 strcpy(sym->name, name);
432 sym->is_global = !!is_global;
433 sym->type = 0; /* Default to T_NULL (no type) */
434 if (segment == NO_SEG)
435 sym->section = -1; /* absolute symbol */
436 else {
437 int i;
438 sym->section = 0;
439 for (i = 0; i < nsects; i++)
440 if (segment == sects[i]->index) {
441 sym->section = i + 1;
442 break;
444 if (!sym->section)
445 sym->is_global = true;
447 if (is_global == 2)
448 sym->value = offset;
449 else
450 sym->value = (sym->section == 0 ? 0 : offset);
453 * define the references from external-symbol segment numbers
454 * to these symbol records.
456 if (sym->section == 0) {
457 bsym = raa_write(bsym, segment, nsyms);
460 if (segment != NO_SEG)
461 symval = raa_write(symval, segment, sym->section ? 0 : sym->value);
463 nsyms++;
466 static int32_t coff_add_reloc(struct Section *sect, int32_t segment,
467 int16_t type)
469 struct Reloc *r;
471 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
472 sect->tail = &r->next;
473 r->next = NULL;
475 r->address = sect->len;
476 if (segment == NO_SEG)
477 r->symbol = 0, r->symbase = ABS_SYMBOL;
478 else {
479 int i;
480 r->symbase = REAL_SYMBOLS;
481 for (i = 0; i < nsects; i++)
482 if (segment == sects[i]->index) {
483 r->symbol = i * 2;
484 r->symbase = SECT_SYMBOLS;
485 break;
487 if (r->symbase == REAL_SYMBOLS)
488 r->symbol = raa_read(bsym, segment);
490 r->type = type;
492 sect->nrelocs++;
495 * Return the fixup for standard COFF common variables.
497 if (r->symbase == REAL_SYMBOLS && !(win32 | win64))
498 return raa_read(symval, segment);
499 else
500 return 0;
503 static void coff_out(int32_t segto, const void *data,
504 enum out_type type, uint64_t size,
505 int32_t segment, int32_t wrt)
507 struct Section *s;
508 uint8_t mydata[8], *p;
509 int i;
511 if (wrt != NO_SEG && !win64) {
512 wrt = NO_SEG; /* continue to do _something_ */
513 nasm_error(ERR_NONFATAL, "WRT not supported by COFF output formats");
517 * handle absolute-assembly (structure definitions)
519 if (segto == NO_SEG) {
520 if (type != OUT_RESERVE)
521 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
522 " space");
523 return;
526 s = NULL;
527 for (i = 0; i < nsects; i++)
528 if (segto == sects[i]->index) {
529 s = sects[i];
530 break;
532 if (!s) {
533 int tempint; /* ignored */
534 if (segto != coff_section_names(".text", 2, &tempint))
535 nasm_error(ERR_PANIC, "strange segment conditions in COFF driver");
536 else
537 s = sects[nsects - 1];
540 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
541 if (win64 && wrt == NO_SEG &&
542 (!strcmp(s->name,".pdata") || !strcmp(s->name,".xdata")))
543 wrt = imagebase_sect;
545 if (!s->data && type != OUT_RESERVE) {
546 nasm_error(ERR_WARNING, "attempt to initialize memory in"
547 " BSS section `%s': ignored", s->name);
548 s->len += realsize(type, size);
549 return;
552 if (type == OUT_RESERVE) {
553 if (s->data) {
554 nasm_error(ERR_WARNING, "uninitialised space declared in"
555 " non-BSS section `%s': zeroing", s->name);
556 coff_sect_write(s, NULL, size);
557 } else
558 s->len += size;
559 } else if (type == OUT_RAWDATA) {
560 if (segment != NO_SEG)
561 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
562 coff_sect_write(s, data, size);
563 } else if (type == OUT_ADDRESS) {
564 if (!(win64)) {
565 if (size != 4 && (segment != NO_SEG || wrt != NO_SEG))
566 nasm_error(ERR_NONFATAL, "COFF format does not support non-32-bit"
567 " relocations");
568 else {
569 int32_t fix = 0;
570 if (segment != NO_SEG || wrt != NO_SEG) {
571 if (wrt != NO_SEG) {
572 nasm_error(ERR_NONFATAL, "COFF format does not support"
573 " WRT types");
574 } else if (segment % 2) {
575 nasm_error(ERR_NONFATAL, "COFF format does not support"
576 " segment base references");
577 } else
578 fix = coff_add_reloc(s, segment, IMAGE_REL_I386_DIR32);
580 p = mydata;
581 WRITELONG(p, *(int64_t *)data + fix);
582 coff_sect_write(s, mydata, size);
584 } else {
585 int32_t fix = 0;
586 p = mydata;
587 if (size == 8) {
588 if (wrt == imagebase_sect) {
589 nasm_error(ERR_NONFATAL, "operand size mismatch: 'wrt "
590 WRT_IMAGEBASE "' is a 32-bit operand");
592 fix = coff_add_reloc(s, segment, IMAGE_REL_AMD64_ADDR64);
593 WRITEDLONG(p, *(int64_t *)data + fix);
594 coff_sect_write(s, mydata, size);
595 } else {
596 fix = coff_add_reloc(s, segment,
597 wrt == imagebase_sect ? IMAGE_REL_AMD64_ADDR32NB:
598 IMAGE_REL_AMD64_ADDR32);
599 WRITELONG(p, *(int64_t *)data + fix);
600 coff_sect_write(s, mydata, size);
603 } else if (type == OUT_REL2ADR) {
604 nasm_error(ERR_NONFATAL, "COFF format does not support 16-bit"
605 " relocations");
606 } else if (type == OUT_REL4ADR) {
607 if (segment == segto && !(win64)) /* Acceptable for RIP-relative */
608 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
609 else if (segment == NO_SEG && win32)
610 nasm_error(ERR_NONFATAL, "Win32 COFF does not correctly support"
611 " relative references to absolute addresses");
612 else {
613 int32_t fix = 0;
614 if (segment != NO_SEG && segment % 2) {
615 nasm_error(ERR_NONFATAL, "COFF format does not support"
616 " segment base references");
617 } else
618 fix = coff_add_reloc(s, segment,
619 win64 ? IMAGE_REL_AMD64_REL32 : IMAGE_REL_I386_REL32);
620 p = mydata;
621 if (win32 | win64) {
622 WRITELONG(p, *(int64_t *)data + 4 - size + fix);
623 } else {
624 WRITELONG(p, *(int64_t *)data - (size + s->len) + fix);
626 coff_sect_write(s, mydata, 4L);
632 static void coff_sect_write(struct Section *sect,
633 const uint8_t *data, uint32_t len)
635 saa_wbytes(sect->data, data, len);
636 sect->len += len;
639 typedef struct tagString {
640 struct tagString *Next;
641 int len;
642 char *String;
643 } STRING;
645 #define EXPORT_SECTION_NAME ".drectve"
646 #define EXPORT_SECTION_FLAGS INFO_FLAGS
648 #define EXPORT_SECTION_NAME ".text"
649 #define EXPORT_SECTION_FLAGS TEXT_FLAGS
652 static STRING *Exports = NULL;
653 static struct Section *directive_sec;
654 void AddExport(char *name)
656 STRING *rvp = Exports, *newS;
658 newS = (STRING *) nasm_malloc(sizeof(STRING));
659 newS->len = strlen(name);
660 newS->Next = NULL;
661 newS->String = (char *)nasm_malloc(newS->len + 1);
662 strcpy(newS->String, name);
663 if (rvp == NULL) {
664 int i;
665 for (i = 0; i < nsects; i++)
667 if (!strcmp(EXPORT_SECTION_NAME, sects[i]->name))
668 break;
669 if (i == nsects)
670 directive_sec =
671 sects[coff_make_section
672 (EXPORT_SECTION_NAME, EXPORT_SECTION_FLAGS)];
673 else
674 directive_sec = sects[i];
675 Exports = newS;
676 } else {
677 while (rvp->Next) {
678 if (!strcmp(rvp->String, name))
679 return;
680 rvp = rvp->Next;
682 rvp->Next = newS;
686 void BuildExportTable(void)
688 STRING *rvp = Exports, *next;
689 uint8_t buf[256];
690 int len;
691 if (rvp == NULL)
692 return;
693 while (rvp) {
694 len = sprintf((char *)buf, "-export:%s ", rvp->String);
695 coff_sect_write(directive_sec, buf, len);
696 rvp = rvp->Next;
699 next = Exports;
700 while ((rvp = next)) {
701 next = rvp->Next;
702 nasm_free(rvp->String);
703 nasm_free(rvp);
705 Exports = NULL;
708 static int coff_directives(enum directives directive, char *value, int pass)
710 switch (directive) {
711 case D_EXPORT:
713 char *q, *name;
715 if (pass == 2)
716 return 1; /* ignore in pass two */
717 name = q = value;
718 while (*q && !nasm_isspace(*q))
719 q++;
720 if (nasm_isspace(*q)) {
721 *q++ = '\0';
722 while (*q && nasm_isspace(*q))
723 q++;
726 if (!*name) {
727 nasm_error(ERR_NONFATAL, "`export' directive requires export name");
728 return 1;
730 if (*q) {
731 nasm_error(ERR_NONFATAL, "unrecognized export qualifier `%s'", q);
732 return 1;
734 AddExport(name);
735 return 1;
737 case D_SAFESEH:
739 static int sxseg=-1;
740 int i;
742 if (!win32) /* Only applicable for -f win32 */
743 return 0;
745 if (sxseg==-1)
746 { for (i = 0; i < nsects; i++)
747 if (!strcmp(".sxdata",sects[i]->name))
748 break;
749 if (i == nsects)
750 sxseg = coff_make_section(".sxdata",0x200);
751 else
752 sxseg = i;
754 /* pass0 == 2 is the only time when the full set of symbols are
755 guaranteed to be present; it is the final output pass. */
756 if (pass0 == 2) {
757 uint32_t n;
758 saa_rewind(syms);
759 for (n = 0; n < nsyms; n++) {
760 struct Symbol *sym = saa_rstruct(syms);
761 bool equals;
763 /* sym->strpos is biased by 4, because symbol
764 * table is prefixed with table length */
765 if (sym->strpos >=4) {
766 char *name = nasm_malloc(sym->namlen+1);
767 saa_fread(strs, sym->strpos-4, name, sym->namlen);
768 name[sym->namlen] = '\0';
769 equals = !strcmp(value,name);
770 nasm_free(name);
772 else
773 equals = !strcmp(value,sym->name);
775 if (equals) {
776 /* this value arithmetics effectively reflects
777 * initsym in coff_write(): 2 for file, 1 for
778 * .absolute and two per each section */
779 unsigned char value[4],*p=value;
780 WRITELONG(p,n + 2 + 1 + nsects*2);
781 coff_sect_write(sects[sxseg],value,4);
782 sym->type = 0x20;
783 break;
786 if (n == nsyms) {
787 nasm_error(ERR_NONFATAL,
788 "`safeseh' directive requires valid symbol");
791 return 1;
793 default:
794 return 0;
798 static void coff_write(void)
800 int32_t pos, sympos, vsize;
801 int i;
803 BuildExportTable(); /* fill in the .drectve section with -export's */
805 if (win32) {
806 /* add default value for @feat.00, this allows to 'link /safeseh' */
807 uint32_t n;
809 saa_rewind(syms);
810 for (n = 0; n < nsyms; n++) {
811 struct Symbol *sym = saa_rstruct(syms);
812 if (sym->strpos == -1 && !strcmp("@feat.00",sym->name))
813 break;
815 if (n == nsyms)
816 coff_deflabel("@feat.00",NO_SEG,1,0,NULL);
820 * Work out how big the file will get. Calculate the start of
821 * the `real' symbols at the same time.
823 pos = 0x14 + 0x28 * nsects;
824 initsym = 3; /* two for the file, one absolute */
825 for (i = 0; i < nsects; i++) {
826 if (sects[i]->data) {
827 sects[i]->pos = pos;
828 pos += sects[i]->len;
829 sects[i]->relpos = pos;
830 pos += 10 * sects[i]->nrelocs;
831 } else
832 sects[i]->pos = sects[i]->relpos = 0L;
833 initsym += 2; /* two for each section */
835 sympos = pos;
838 * Output the COFF header.
840 if (win64)
841 fwriteint16_t(0x8664, ofile); /* MACHINE_x86-64 */
842 else
843 fwriteint16_t(0x014C, ofile); /* MACHINE_i386 */
844 fwriteint16_t(nsects, ofile); /* number of sections */
845 fwriteint32_t(time(NULL), ofile); /* time stamp */
846 fwriteint32_t(sympos, ofile);
847 fwriteint32_t(nsyms + initsym, ofile);
848 fwriteint16_t(0, ofile); /* no optional header */
849 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
850 fwriteint16_t((win32 | win64) ? 0 : 0x104, ofile);
853 * Output the section headers.
855 vsize = 0L;
856 for (i = 0; i < nsects; i++) {
857 coff_section_header(sects[i]->name, vsize, sects[i]->len,
858 sects[i]->pos, sects[i]->relpos,
859 sects[i]->nrelocs, sects[i]->flags);
860 vsize += sects[i]->len;
864 * Output the sections and their relocations.
866 for (i = 0; i < nsects; i++)
867 if (sects[i]->data) {
868 saa_fpwrite(sects[i]->data, ofile);
869 coff_write_relocs(sects[i]);
873 * Output the symbol and string tables.
875 coff_write_symbols();
876 fwriteint32_t(strslen + 4, ofile); /* length includes length count */
877 saa_fpwrite(strs, ofile);
880 static void coff_section_header(char *name, int32_t vsize,
881 int32_t datalen, int32_t datapos,
882 int32_t relpos, int nrelocs, int32_t flags)
884 char padname[8];
886 (void)vsize;
888 memset(padname, 0, 8);
889 strncpy(padname, name, 8);
890 fwrite(padname, 8, 1, ofile);
891 fwriteint32_t(0, ofile); /* Virtual size field - set to 0 or vsize */
892 fwriteint32_t(0L, ofile); /* RVA/offset - we ignore */
893 fwriteint32_t(datalen, ofile);
894 fwriteint32_t(datapos, ofile);
895 fwriteint32_t(relpos, ofile);
896 fwriteint32_t(0L, ofile); /* no line numbers - we don't do 'em */
897 fwriteint16_t(nrelocs, ofile);
898 fwriteint16_t(0, ofile); /* again, no line numbers */
899 fwriteint32_t(flags, ofile);
902 static void coff_write_relocs(struct Section *s)
904 struct Reloc *r;
906 for (r = s->head; r; r = r->next) {
907 fwriteint32_t(r->address, ofile);
908 fwriteint32_t(r->symbol + (r->symbase == REAL_SYMBOLS ? initsym :
909 r->symbase == ABS_SYMBOL ? initsym - 1 :
910 r->symbase == SECT_SYMBOLS ? 2 : 0),
911 ofile);
912 fwriteint16_t(r->type, ofile);
916 static void coff_symbol(char *name, int32_t strpos, int32_t value,
917 int section, int type, int storageclass, int aux)
919 char padname[8];
921 if (name) {
922 memset(padname, 0, 8);
923 strncpy(padname, name, 8);
924 fwrite(padname, 8, 1, ofile);
925 } else {
926 fwriteint32_t(0, ofile);
927 fwriteint32_t(strpos, ofile);
929 fwriteint32_t(value, ofile);
930 fwriteint16_t(section, ofile);
931 fwriteint16_t(type, ofile);
932 fputc(storageclass, ofile);
933 fputc(aux, ofile);
936 static void coff_write_symbols(void)
938 char filename[18];
939 uint32_t i;
942 * The `.file' record, and the file name auxiliary record.
944 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
945 memset(filename, 0, 18);
946 strncpy(filename, coff_infile, 18);
947 fwrite(filename, 18, 1, ofile);
950 * The section records, with their auxiliaries.
952 memset(filename, 0, 18); /* useful zeroed buffer */
954 for (i = 0; i < (uint32_t) nsects; i++) {
955 coff_symbol(sects[i]->name, 0L, 0L, i + 1, 0, 3, 1);
956 fwriteint32_t(sects[i]->len, ofile);
957 fwriteint16_t(sects[i]->nrelocs, ofile);
958 fwrite(filename, 12, 1, ofile);
962 * The absolute symbol, for relative-to-absolute relocations.
964 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
967 * The real symbols.
969 saa_rewind(syms);
970 for (i = 0; i < nsyms; i++) {
971 struct Symbol *sym = saa_rstruct(syms);
972 coff_symbol(sym->strpos == -1 ? sym->name : NULL,
973 sym->strpos, sym->value, sym->section,
974 sym->type, sym->is_global ? 2 : 3, 0);
978 static int32_t coff_segbase(int32_t segment)
980 return segment;
983 static void coff_std_filename(char *inname, char *outname)
985 strcpy(coff_infile, inname);
986 standard_extension(inname, outname, ".o");
989 static void coff_win32_filename(char *inname, char *outname)
991 strcpy(coff_infile, inname);
992 standard_extension(inname, outname, ".obj");
995 extern macros_t coff_stdmac[];
997 static int coff_set_info(enum geninfo type, char **val)
999 (void)type;
1000 (void)val;
1001 return 0;
1003 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
1005 #ifdef OF_COFF
1007 struct ofmt of_coff = {
1008 "COFF (i386) object files (e.g. DJGPP for DOS)",
1009 "coff",
1011 null_debug_arr,
1012 &null_debug_form,
1013 coff_stdmac,
1014 coff_std_init,
1015 coff_set_info,
1016 coff_out,
1017 coff_deflabel,
1018 coff_section_names,
1019 coff_segbase,
1020 coff_directives,
1021 coff_std_filename,
1022 coff_cleanup
1025 #endif
1027 #ifdef OF_WIN32
1029 struct ofmt of_win32 = {
1030 "Microsoft Win32 (i386) object files",
1031 "win32",
1033 null_debug_arr,
1034 &null_debug_form,
1035 coff_stdmac,
1036 coff_win32_init,
1037 coff_set_info,
1038 coff_out,
1039 coff_deflabel,
1040 coff_section_names,
1041 coff_segbase,
1042 coff_directives,
1043 coff_win32_filename,
1044 coff_cleanup
1047 #endif
1049 #ifdef OF_WIN64
1051 struct ofmt of_win64 = {
1052 "Microsoft Win64 (x86-64) object files",
1053 "win64",
1055 null_debug_arr,
1056 &null_debug_form,
1057 coff_stdmac,
1058 coff_win64_init,
1059 coff_set_info,
1060 coff_out,
1061 coff_deflabel,
1062 coff_section_names,
1063 coff_segbase,
1064 coff_directives,
1065 coff_win32_filename,
1066 coff_cleanup
1069 #endif