NASM 2.09rc4
[nasm/avx512.git] / output / outcoff.c
blob5b232bb989882554726c8b123f84bb1fdfcd5b2e
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 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 * ----------------------------------------------------------------------- */
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"
55 #include "output/pecoff.h"
57 #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
60 * Notes on COFF:
62 * (0) When I say `standard COFF' below, I mean `COFF as output and
63 * used by DJGPP'. I assume DJGPP gets it right.
65 * (1) Win32 appears to interpret the term `relative relocation'
66 * differently from standard COFF. Standard COFF understands a
67 * relative relocation to mean that during relocation you add the
68 * address of the symbol you're referencing, and subtract the base
69 * address of the section you're in. Win32 COFF, by contrast, seems
70 * to add the address of the symbol and then subtract the address
71 * of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
72 * subtly incompatible.
74 * (2) Win32 doesn't bother putting any flags in the header flags
75 * field (at offset 0x12 into the file).
77 * (3) Win32 uses some extra flags into the section header table:
78 * it defines flags 0x80000000 (writable), 0x40000000 (readable)
79 * and 0x20000000 (executable), and uses them in the expected
80 * combinations. It also defines 0x00100000 through 0x00700000 for
81 * section alignments of 1 through 64 bytes.
83 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
84 * field directly after the section name in the section header
85 * table for something strange: they store what the address of the
86 * section start point _would_ be, if you laid all the sections end
87 * to end starting at zero. Dunno why. Microsoft's documentation
88 * lists this field as "Virtual Size of Section", which doesn't
89 * seem to fit at all. In fact, Win32 even includes non-linked
90 * sections such as .drectve in this calculation.
92 * Newer versions of MASM seem to have changed this to be zero, and
93 * that apparently matches the COFF spec, so go with that.
95 * (5) Standard COFF does something very strange to common
96 * variables: the relocation point for a common variable is as far
97 * _before_ the variable as its size stretches out _after_ it. So
98 * we must fix up common variable references. Win32 seems to be
99 * sensible on this one.
102 /* Flag which version of COFF we are currently outputting. */
103 static bool win32, win64;
105 static int32_t imagebase_sect;
106 #define WRT_IMAGEBASE "..imagebase"
108 struct Reloc {
109 struct Reloc *next;
110 int32_t address; /* relative to _start_ of section */
111 int32_t symbol; /* symbol number */
112 enum {
113 SECT_SYMBOLS,
114 ABS_SYMBOL,
115 REAL_SYMBOLS
116 } symbase; /* relocation for symbol number :) */
117 int16_t type;
120 struct Symbol {
121 char name[9];
122 int32_t strpos; /* string table position of name */
123 int32_t value; /* address, or COMMON variable size */
124 int section; /* section number where it's defined
125 * - in COFF codes, not NASM codes */
126 bool is_global; /* is it a global symbol or not? */
127 int16_t type; /* 0 - notype, 0x20 - function */
128 int32_t namlen; /* full name length */
131 static char coff_infile[FILENAME_MAX];
133 struct Section {
134 struct SAA *data;
135 uint32_t len;
136 int nrelocs;
137 int32_t index;
138 struct Reloc *head, **tail;
139 uint32_t flags; /* section flags */
140 char name[9];
141 int32_t pos, relpos;
145 * Some common section flags by default
147 #define TEXT_FLAGS_WIN \
148 (IMAGE_SCN_CNT_CODE | \
149 IMAGE_SCN_ALIGN_16BYTES | \
150 IMAGE_SCN_MEM_EXECUTE | \
151 IMAGE_SCN_MEM_READ)
152 #define TEXT_FLAGS_DOS \
153 (IMAGE_SCN_CNT_CODE)
155 #define DATA_FLAGS_WIN \
156 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
157 IMAGE_SCN_ALIGN_4BYTES | \
158 IMAGE_SCN_MEM_READ | \
159 IMAGE_SCN_MEM_WRITE)
160 #define DATA_FLAGS_DOS \
161 (IMAGE_SCN_CNT_INITIALIZED_DATA)
163 #define BSS_FLAGS_WIN \
164 (IMAGE_SCN_CNT_UNINITIALIZED_DATA | \
165 IMAGE_SCN_ALIGN_4BYTES | \
166 IMAGE_SCN_MEM_READ | \
167 IMAGE_SCN_MEM_WRITE)
168 #define BSS_FLAGS_DOS \
169 (IMAGE_SCN_CNT_UNINITIALIZED_DATA)
171 #define RDATA_FLAGS_WIN \
172 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
173 IMAGE_SCN_ALIGN_8BYTES | \
174 IMAGE_SCN_MEM_READ)
176 #define RDATA_FLAGS_DOS \
177 (IMAGE_SCN_CNT_INITIALIZED_DATA)
179 #define PDATA_FLAGS \
180 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
181 IMAGE_SCN_ALIGN_4BYTES | \
182 IMAGE_SCN_MEM_READ)
184 #define XDATA_FLAGS \
185 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
186 IMAGE_SCN_ALIGN_8BYTES | \
187 IMAGE_SCN_MEM_READ)
189 #define INFO_FLAGS \
190 (IMAGE_SCN_ALIGN_1BYTES | \
191 IMAGE_SCN_LNK_INFO | \
192 IMAGE_SCN_LNK_REMOVE)
194 #define TEXT_FLAGS ((win32 | win64) ? TEXT_FLAGS_WIN : TEXT_FLAGS_DOS)
195 #define DATA_FLAGS ((win32 | win64) ? DATA_FLAGS_WIN : DATA_FLAGS_DOS)
196 #define BSS_FLAGS ((win32 | win64) ? BSS_FLAGS_WIN : BSS_FLAGS_DOS)
197 #define RDATA_FLAGS ((win32 | win64) ? RDATA_FLAGS_WIN : RDATA_FLAGS_DOS)
199 #define SECT_DELTA 32
200 static struct Section **sects;
201 static int nsects, sectlen;
203 static struct SAA *syms;
204 static uint32_t nsyms;
206 static int32_t def_seg;
208 static int initsym;
210 static struct RAA *bsym, *symval;
212 static struct SAA *strs;
213 static uint32_t strslen;
215 static void coff_gen_init(void);
216 static void coff_sect_write(struct Section *, const uint8_t *, uint32_t);
217 static void coff_write(void);
218 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
219 static void coff_write_relocs(struct Section *);
220 static void coff_write_symbols(void);
222 static void coff_win32_init(void)
224 win32 = true;
225 win64 = false;
226 coff_gen_init();
229 static void coff_win64_init(void)
231 maxbits = 64;
232 win32 = false;
233 win64 = true;
234 coff_gen_init();
235 imagebase_sect = seg_alloc()+1;
236 define_label(WRT_IMAGEBASE, imagebase_sect, 0, NULL, false, false);
239 static void coff_std_init(void)
241 win32 = win64 = false;
242 coff_gen_init();
245 static void coff_gen_init(void)
248 sects = NULL;
249 nsects = sectlen = 0;
250 syms = saa_init(sizeof(struct Symbol));
251 nsyms = 0;
252 bsym = raa_init();
253 symval = raa_init();
254 strs = saa_init(1);
255 strslen = 0;
256 def_seg = seg_alloc();
259 static void coff_cleanup(int debuginfo)
261 struct Reloc *r;
262 int i;
264 (void)debuginfo;
266 coff_write();
267 for (i = 0; i < nsects; i++) {
268 if (sects[i]->data)
269 saa_free(sects[i]->data);
270 while (sects[i]->head) {
271 r = sects[i]->head;
272 sects[i]->head = sects[i]->head->next;
273 nasm_free(r);
275 nasm_free(sects[i]);
277 nasm_free(sects);
278 saa_free(syms);
279 raa_free(bsym);
280 raa_free(symval);
281 saa_free(strs);
284 static int coff_make_section(char *name, uint32_t flags)
286 struct Section *s;
288 s = nasm_malloc(sizeof(*s));
290 if (flags != BSS_FLAGS)
291 s->data = saa_init(1);
292 else
293 s->data = NULL;
294 s->head = NULL;
295 s->tail = &s->head;
296 s->len = 0;
297 s->nrelocs = 0;
298 if (!strcmp(name, ".text"))
299 s->index = def_seg;
300 else
301 s->index = seg_alloc();
302 strncpy(s->name, name, 8);
303 s->name[8] = '\0';
304 s->flags = flags;
306 if (nsects >= sectlen) {
307 sectlen += SECT_DELTA;
308 sects = nasm_realloc(sects, sectlen * sizeof(*sects));
310 sects[nsects++] = s;
312 return nsects - 1;
315 static inline int32_t coff_sectalign_flags(unsigned int align)
317 return (ilog2_32(align) + 1) << 20;
320 static int32_t coff_section_names(char *name, int pass, int *bits)
322 char *p;
323 uint32_t flags, align_and = ~0L, align_or = 0L;
324 int i;
327 * Set default bits.
329 if (!name) {
330 if(win64)
331 *bits = 64;
332 else
333 *bits = 32;
336 if (!name)
337 return def_seg;
339 p = name;
340 while (*p && !nasm_isspace(*p))
341 p++;
342 if (*p)
343 *p++ = '\0';
344 if (strlen(name) > 8) {
345 nasm_error(ERR_WARNING, "COFF section names limited to 8 characters:"
346 " truncating");
347 name[8] = '\0';
349 flags = 0;
351 while (*p && nasm_isspace(*p))
352 p++;
353 while (*p) {
354 char *q = p;
355 while (*p && !nasm_isspace(*p))
356 p++;
357 if (*p)
358 *p++ = '\0';
359 while (*p && nasm_isspace(*p))
360 p++;
362 if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
363 flags = TEXT_FLAGS;
364 } else if (!nasm_stricmp(q, "data")) {
365 flags = DATA_FLAGS;
366 } else if (!nasm_stricmp(q, "rdata")) {
367 if (win32 | win64)
368 flags = RDATA_FLAGS;
369 else {
370 flags = DATA_FLAGS; /* gotta do something */
371 nasm_error(ERR_NONFATAL, "standard COFF does not support"
372 " read-only data sections");
374 } else if (!nasm_stricmp(q, "bss")) {
375 flags = BSS_FLAGS;
376 } else if (!nasm_stricmp(q, "info")) {
377 if (win32 | win64)
378 flags = INFO_FLAGS;
379 else {
380 flags = DATA_FLAGS; /* gotta do something */
381 nasm_error(ERR_NONFATAL, "standard COFF does not support"
382 " informational sections");
384 } else if (!nasm_strnicmp(q, "align=", 6)) {
385 if (!(win32 | win64))
386 nasm_error(ERR_NONFATAL, "standard COFF does not support"
387 " section alignment specification");
388 else {
389 if (q[6 + strspn(q + 6, "0123456789")])
390 nasm_error(ERR_NONFATAL,
391 "argument to `align' is not numeric");
392 else {
393 unsigned int align = atoi(q + 6);
394 if (!align || ((align - 1) & align))
395 nasm_error(ERR_NONFATAL, "argument to `align' is not a"
396 " power of two");
397 else if (align > 64)
398 nasm_error(ERR_NONFATAL, "Win32 cannot align sections"
399 " to better than 64-byte boundaries");
400 else {
401 align_and = ~0x00F00000L;
402 align_or = coff_sectalign_flags(align);
409 for (i = 0; i < nsects; i++)
410 if (!strcmp(name, sects[i]->name))
411 break;
412 if (i == nsects) {
413 if (!flags) {
414 if (!strcmp(name, ".data"))
415 flags = DATA_FLAGS;
416 else if (!strcmp(name, ".rdata"))
417 flags = RDATA_FLAGS;
418 else if (!strcmp(name, ".bss"))
419 flags = BSS_FLAGS;
420 else if (win64 && !strcmp(name, ".pdata"))
421 flags = PDATA_FLAGS;
422 else if (win64 && !strcmp(name, ".xdata"))
423 flags = XDATA_FLAGS;
424 else
425 flags = TEXT_FLAGS;
427 i = coff_make_section(name, flags);
428 if (flags)
429 sects[i]->flags = flags;
430 sects[i]->flags &= align_and;
431 sects[i]->flags |= align_or;
432 } else if (pass == 1) {
433 if (flags)
434 nasm_error(ERR_WARNING, "section attributes ignored on"
435 " redeclaration of section `%s'", name);
438 return sects[i]->index;
441 static void coff_deflabel(char *name, int32_t segment, int64_t offset,
442 int is_global, char *special)
444 int pos = strslen + 4;
445 struct Symbol *sym;
447 if (special)
448 nasm_error(ERR_NONFATAL, "COFF format does not support any"
449 " special symbol types");
451 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
452 if (strcmp(name,WRT_IMAGEBASE))
453 nasm_error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
454 return;
457 if (strlen(name) > 8) {
458 size_t nlen = strlen(name)+1;
459 saa_wbytes(strs, name, nlen);
460 strslen += nlen;
461 } else
462 pos = -1;
464 sym = saa_wstruct(syms);
466 sym->strpos = pos;
467 sym->namlen = strlen(name);
468 if (pos == -1)
469 strcpy(sym->name, name);
470 sym->is_global = !!is_global;
471 sym->type = 0; /* Default to T_NULL (no type) */
472 if (segment == NO_SEG)
473 sym->section = -1; /* absolute symbol */
474 else {
475 int i;
476 sym->section = 0;
477 for (i = 0; i < nsects; i++)
478 if (segment == sects[i]->index) {
479 sym->section = i + 1;
480 break;
482 if (!sym->section)
483 sym->is_global = true;
485 if (is_global == 2)
486 sym->value = offset;
487 else
488 sym->value = (sym->section == 0 ? 0 : offset);
491 * define the references from external-symbol segment numbers
492 * to these symbol records.
494 if (sym->section == 0)
495 bsym = raa_write(bsym, segment, nsyms);
497 if (segment != NO_SEG)
498 symval = raa_write(symval, segment, sym->section ? 0 : sym->value);
500 nsyms++;
503 static int32_t coff_add_reloc(struct Section *sect, int32_t segment,
504 int16_t type)
506 struct Reloc *r;
508 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
509 sect->tail = &r->next;
510 r->next = NULL;
512 r->address = sect->len;
513 if (segment == NO_SEG) {
514 r->symbol = 0, r->symbase = ABS_SYMBOL;
515 } else {
516 int i;
517 r->symbase = REAL_SYMBOLS;
518 for (i = 0; i < nsects; i++) {
519 if (segment == sects[i]->index) {
520 r->symbol = i * 2;
521 r->symbase = SECT_SYMBOLS;
522 break;
525 if (r->symbase == REAL_SYMBOLS)
526 r->symbol = raa_read(bsym, segment);
528 r->type = type;
530 sect->nrelocs++;
533 * Return the fixup for standard COFF common variables.
535 if (r->symbase == REAL_SYMBOLS && !(win32 | win64))
536 return raa_read(symval, segment);
538 return 0;
541 static void coff_out(int32_t segto, const void *data,
542 enum out_type type, uint64_t size,
543 int32_t segment, int32_t wrt)
545 struct Section *s;
546 uint8_t mydata[8], *p;
547 int i;
549 if (wrt != NO_SEG && !win64) {
550 wrt = NO_SEG; /* continue to do _something_ */
551 nasm_error(ERR_NONFATAL, "WRT not supported by COFF output formats");
555 * handle absolute-assembly (structure definitions)
557 if (segto == NO_SEG) {
558 if (type != OUT_RESERVE)
559 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
560 " space");
561 return;
564 s = NULL;
565 for (i = 0; i < nsects; i++) {
566 if (segto == sects[i]->index) {
567 s = sects[i];
568 break;
571 if (!s) {
572 int tempint; /* ignored */
573 if (segto != coff_section_names(".text", 2, &tempint))
574 nasm_error(ERR_PANIC, "strange segment conditions in COFF driver");
575 else
576 s = sects[nsects - 1];
579 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
580 if (win64 && wrt == NO_SEG) {
581 if (!strcmp(s->name,".pdata") || !strcmp(s->name,".xdata"))
582 wrt = imagebase_sect;
585 if (!s->data && type != OUT_RESERVE) {
586 nasm_error(ERR_WARNING, "attempt to initialize memory in"
587 " BSS section `%s': ignored", s->name);
588 s->len += realsize(type, size);
589 return;
592 if (type == OUT_RESERVE) {
593 if (s->data) {
594 nasm_error(ERR_WARNING, "uninitialised space declared in"
595 " non-BSS section `%s': zeroing", s->name);
596 coff_sect_write(s, NULL, size);
597 } else
598 s->len += size;
599 } else if (type == OUT_RAWDATA) {
600 if (segment != NO_SEG)
601 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
602 coff_sect_write(s, data, size);
603 } else if (type == OUT_ADDRESS) {
604 if (!(win64)) {
605 if (size != 4 && (segment != NO_SEG || wrt != NO_SEG)) {
606 nasm_error(ERR_NONFATAL, "COFF format does not support non-32-bit"
607 " relocations");
608 } else {
609 int32_t fix = 0;
610 if (segment != NO_SEG || wrt != NO_SEG) {
611 if (wrt != NO_SEG) {
612 nasm_error(ERR_NONFATAL, "COFF format does not support"
613 " WRT types");
614 } else if (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, IMAGE_REL_I386_DIR32);
620 p = mydata;
621 WRITELONG(p, *(int64_t *)data + fix);
622 coff_sect_write(s, mydata, size);
624 } else {
625 int32_t fix = 0;
626 p = mydata;
627 if (size == 8) {
628 if (wrt == imagebase_sect) {
629 nasm_error(ERR_NONFATAL, "operand size mismatch: 'wrt "
630 WRT_IMAGEBASE "' is a 32-bit operand");
632 fix = coff_add_reloc(s, segment, IMAGE_REL_AMD64_ADDR64);
633 WRITEDLONG(p, *(int64_t *)data + fix);
634 coff_sect_write(s, mydata, size);
635 } else {
636 fix = coff_add_reloc(s, segment,
637 wrt == imagebase_sect ? IMAGE_REL_AMD64_ADDR32NB:
638 IMAGE_REL_AMD64_ADDR32);
639 WRITELONG(p, *(int64_t *)data + fix);
640 coff_sect_write(s, mydata, size);
643 } else if (type == OUT_REL2ADR) {
644 nasm_error(ERR_NONFATAL, "COFF format does not support 16-bit"
645 " relocations");
646 } else if (type == OUT_REL4ADR) {
647 if (segment == segto && !(win64)) /* Acceptable for RIP-relative */
648 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
649 else if (segment == NO_SEG && win32)
650 nasm_error(ERR_NONFATAL, "Win32 COFF does not correctly support"
651 " relative references to absolute addresses");
652 else {
653 int32_t fix = 0;
654 if (segment != NO_SEG && segment % 2) {
655 nasm_error(ERR_NONFATAL, "COFF format does not support"
656 " segment base references");
657 } else
658 fix = coff_add_reloc(s, segment,
659 win64 ? IMAGE_REL_AMD64_REL32 : IMAGE_REL_I386_REL32);
660 p = mydata;
661 if (win32 | win64) {
662 WRITELONG(p, *(int64_t *)data + 4 - size + fix);
663 } else {
664 WRITELONG(p, *(int64_t *)data - (size + s->len) + fix);
666 coff_sect_write(s, mydata, 4L);
672 static void coff_sect_write(struct Section *sect,
673 const uint8_t *data, uint32_t len)
675 saa_wbytes(sect->data, data, len);
676 sect->len += len;
679 typedef struct tagString {
680 struct tagString *next;
681 int len;
682 char *String;
683 } STRING;
685 #define EXPORT_SECTION_NAME ".drectve"
686 #define EXPORT_SECTION_FLAGS INFO_FLAGS
688 * #define EXPORT_SECTION_NAME ".text"
689 * #define EXPORT_SECTION_FLAGS TEXT_FLAGS
692 static STRING *Exports = NULL;
693 static struct Section *directive_sec;
694 void AddExport(char *name)
696 STRING *rvp = Exports, *newS;
698 newS = (STRING *) nasm_malloc(sizeof(STRING));
699 newS->len = strlen(name);
700 newS->next = NULL;
701 newS->String = (char *)nasm_malloc(newS->len + 1);
702 strcpy(newS->String, name);
703 if (rvp == NULL) {
704 int i;
706 for (i = 0; i < nsects; i++) {
707 if (!strcmp(EXPORT_SECTION_NAME, sects[i]->name))
708 break;
711 if (i == nsects)
712 i = coff_make_section(EXPORT_SECTION_NAME, EXPORT_SECTION_FLAGS);
714 directive_sec = sects[i];
715 Exports = newS;
716 } else {
717 while (rvp->next) {
718 if (!strcmp(rvp->String, name))
719 return;
720 rvp = rvp->next;
722 rvp->next = newS;
726 static void BuildExportTable(STRING **rvp)
728 STRING *p, *t;
730 if (!rvp || !*rvp)
731 return;
733 list_for_each_safe(p, t, *rvp) {
734 coff_sect_write(directive_sec, (uint8_t *)"-export:", 8);
735 coff_sect_write(directive_sec, (uint8_t *)p->String, p->len);
736 coff_sect_write(directive_sec, (uint8_t *)" ", 1);
737 nasm_free(p->String);
738 nasm_free(p);
741 *rvp = NULL;
744 static int coff_directives(enum directives directive, char *value, int pass)
746 switch (directive) {
747 case D_EXPORT:
749 char *q, *name;
751 if (pass == 2)
752 return 1; /* ignore in pass two */
753 name = q = value;
754 while (*q && !nasm_isspace(*q))
755 q++;
756 if (nasm_isspace(*q)) {
757 *q++ = '\0';
758 while (*q && nasm_isspace(*q))
759 q++;
762 if (!*name) {
763 nasm_error(ERR_NONFATAL, "`export' directive requires export name");
764 return 1;
766 if (*q) {
767 nasm_error(ERR_NONFATAL, "unrecognized export qualifier `%s'", q);
768 return 1;
770 AddExport(name);
771 return 1;
773 case D_SAFESEH:
775 static int sxseg=-1;
776 int i;
778 if (!win32) /* Only applicable for -f win32 */
779 return 0;
781 if (sxseg == -1) {
782 for (i = 0; i < nsects; i++)
783 if (!strcmp(".sxdata",sects[i]->name))
784 break;
785 if (i == nsects)
786 sxseg = coff_make_section(".sxdata", IMAGE_SCN_LNK_INFO);
787 else
788 sxseg = i;
791 * pass0 == 2 is the only time when the full set of symbols are
792 * guaranteed to be present; it is the final output pass.
794 if (pass0 == 2) {
795 uint32_t n;
796 saa_rewind(syms);
797 for (n = 0; n < nsyms; n++) {
798 struct Symbol *sym = saa_rstruct(syms);
799 bool equals;
802 * sym->strpos is biased by 4, because symbol
803 * table is prefixed with table length
805 if (sym->strpos >=4) {
806 char *name = nasm_malloc(sym->namlen+1);
807 saa_fread(strs, sym->strpos-4, name, sym->namlen);
808 name[sym->namlen] = '\0';
809 equals = !strcmp(value,name);
810 nasm_free(name);
811 } else {
812 equals = !strcmp(value,sym->name);
815 if (equals) {
817 * this value arithmetics effectively reflects
818 * initsym in coff_write(): 2 for file, 1 for
819 * .absolute and two per each section
821 unsigned char value[4],*p=value;
822 WRITELONG(p,n + 2 + 1 + nsects*2);
823 coff_sect_write(sects[sxseg],value,4);
824 sym->type = 0x20;
825 break;
828 if (n == nsyms) {
829 nasm_error(ERR_NONFATAL,
830 "`safeseh' directive requires valid symbol");
833 return 1;
835 default:
836 return 0;
840 static void coff_write(void)
842 int32_t pos, sympos, vsize;
843 int i;
845 /* fill in the .drectve section with -export's */
846 BuildExportTable(&Exports);
848 if (win32) {
849 /* add default value for @feat.00, this allows to 'link /safeseh' */
850 uint32_t n;
852 saa_rewind(syms);
853 for (n = 0; n < nsyms; n++) {
854 struct Symbol *sym = saa_rstruct(syms);
855 if (sym->strpos == -1 && !strcmp("@feat.00",sym->name))
856 break;
858 if (n == nsyms)
859 coff_deflabel("@feat.00", NO_SEG, 1, 0, NULL);
863 * Work out how big the file will get. Calculate the start of
864 * the `real' symbols at the same time.
866 pos = 0x14 + 0x28 * nsects;
867 initsym = 3; /* two for the file, one absolute */
868 for (i = 0; i < nsects; i++) {
869 if (sects[i]->data) {
870 sects[i]->pos = pos;
871 pos += sects[i]->len;
872 sects[i]->relpos = pos;
873 pos += 10 * sects[i]->nrelocs;
874 } else
875 sects[i]->pos = sects[i]->relpos = 0L;
876 initsym += 2; /* two for each section */
878 sympos = pos;
881 * Output the COFF header.
883 if (win64)
884 i = IMAGE_FILE_MACHINE_AMD64;
885 else
886 i = IMAGE_FILE_MACHINE_I386;
887 fwriteint16_t(i, ofile); /* machine type */
888 fwriteint16_t(nsects, ofile); /* number of sections */
889 fwriteint32_t(time(NULL), ofile); /* time stamp */
890 fwriteint32_t(sympos, ofile);
891 fwriteint32_t(nsyms + initsym, ofile);
892 fwriteint16_t(0, ofile); /* no optional header */
893 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
894 fwriteint16_t((win32 | win64) ? 0 : 0x104, ofile);
897 * Output the section headers.
899 vsize = 0L;
900 for (i = 0; i < nsects; i++) {
901 coff_section_header(sects[i]->name, vsize, sects[i]->len,
902 sects[i]->pos, sects[i]->relpos,
903 sects[i]->nrelocs, sects[i]->flags);
904 vsize += sects[i]->len;
908 * Output the sections and their relocations.
910 for (i = 0; i < nsects; i++)
911 if (sects[i]->data) {
912 saa_fpwrite(sects[i]->data, ofile);
913 coff_write_relocs(sects[i]);
917 * Output the symbol and string tables.
919 coff_write_symbols();
920 fwriteint32_t(strslen + 4, ofile); /* length includes length count */
921 saa_fpwrite(strs, ofile);
924 static void coff_section_header(char *name, int32_t vsize,
925 int32_t datalen, int32_t datapos,
926 int32_t relpos, int nrelocs, int32_t flags)
928 char padname[8];
930 (void)vsize;
932 memset(padname, 0, 8);
933 strncpy(padname, name, 8);
934 fwrite(padname, 8, 1, ofile);
936 fwriteint32_t(0, ofile); /* Virtual size field - set to 0 or vsize */
937 fwriteint32_t(0L, ofile); /* RVA/offset - we ignore */
938 fwriteint32_t(datalen, ofile);
939 fwriteint32_t(datapos, ofile);
940 fwriteint32_t(relpos, ofile);
941 fwriteint32_t(0L, ofile); /* no line numbers - we don't do 'em */
942 fwriteint16_t(nrelocs, ofile);
943 fwriteint16_t(0, ofile); /* again, no line numbers */
944 fwriteint32_t(flags, ofile);
947 static void coff_write_relocs(struct Section *s)
949 struct Reloc *r;
951 for (r = s->head; r; r = r->next) {
952 fwriteint32_t(r->address, ofile);
953 fwriteint32_t(r->symbol + (r->symbase == REAL_SYMBOLS ? initsym :
954 r->symbase == ABS_SYMBOL ? initsym - 1 :
955 r->symbase == SECT_SYMBOLS ? 2 : 0),
956 ofile);
957 fwriteint16_t(r->type, ofile);
961 static void coff_symbol(char *name, int32_t strpos, int32_t value,
962 int section, int type, int storageclass, int aux)
964 char padname[8];
966 if (name) {
967 memset(padname, 0, 8);
968 strncpy(padname, name, 8);
969 fwrite(padname, 8, 1, ofile);
970 } else {
971 fwriteint32_t(0, ofile);
972 fwriteint32_t(strpos, ofile);
975 fwriteint32_t(value, ofile);
976 fwriteint16_t(section, ofile);
977 fwriteint16_t(type, ofile);
979 fputc(storageclass, ofile);
980 fputc(aux, ofile);
983 static void coff_write_symbols(void)
985 char filename[18];
986 uint32_t i;
989 * The `.file' record, and the file name auxiliary record.
991 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
992 memset(filename, 0, 18);
993 strncpy(filename, coff_infile, 18);
994 fwrite(filename, 18, 1, ofile);
997 * The section records, with their auxiliaries.
999 memset(filename, 0, 18); /* useful zeroed buffer */
1001 for (i = 0; i < (uint32_t) nsects; i++) {
1002 coff_symbol(sects[i]->name, 0L, 0L, i + 1, 0, 3, 1);
1003 fwriteint32_t(sects[i]->len, ofile);
1004 fwriteint16_t(sects[i]->nrelocs,ofile);
1005 fwrite(filename, 12, 1, ofile);
1009 * The absolute symbol, for relative-to-absolute relocations.
1011 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
1014 * The real symbols.
1016 saa_rewind(syms);
1017 for (i = 0; i < nsyms; i++) {
1018 struct Symbol *sym = saa_rstruct(syms);
1019 coff_symbol(sym->strpos == -1 ? sym->name : NULL,
1020 sym->strpos, sym->value, sym->section,
1021 sym->type, sym->is_global ? 2 : 3, 0);
1025 static void coff_sectalign(int32_t seg, unsigned int value)
1027 struct Section *s = NULL;
1028 uint32_t align;
1029 int i;
1031 for (i = 0; i < nsects; i++) {
1032 if (sects[i]->index == seg) {
1033 s = sects[i];
1034 break;
1038 if (!s || !is_power2(value))
1039 return;
1041 /* DOS has limitation on 64 bytes */
1042 if (!(win32 | win64) && value > 64)
1043 return;
1045 align = (s->flags & IMAGE_SCN_ALIGN_MASK);
1046 value = coff_sectalign_flags(value);
1047 if (value > align)
1048 s->flags = (s->flags & ~IMAGE_SCN_ALIGN_MASK) | value;
1051 static int32_t coff_segbase(int32_t segment)
1053 return segment;
1056 static void coff_std_filename(char *inname, char *outname)
1058 strcpy(coff_infile, inname);
1059 standard_extension(inname, outname, ".o");
1062 static void coff_win32_filename(char *inname, char *outname)
1064 strcpy(coff_infile, inname);
1065 standard_extension(inname, outname, ".obj");
1068 extern macros_t coff_stdmac[];
1070 static int coff_set_info(enum geninfo type, char **val)
1072 (void)type;
1073 (void)val;
1074 return 0;
1076 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
1078 #ifdef OF_COFF
1080 struct ofmt of_coff = {
1081 "COFF (i386) object files (e.g. DJGPP for DOS)",
1082 "coff",
1084 null_debug_arr,
1085 &null_debug_form,
1086 coff_stdmac,
1087 coff_std_init,
1088 coff_set_info,
1089 coff_out,
1090 coff_deflabel,
1091 coff_section_names,
1092 coff_sectalign,
1093 coff_segbase,
1094 coff_directives,
1095 coff_std_filename,
1096 coff_cleanup
1099 #endif
1101 #ifdef OF_WIN32
1103 struct ofmt of_win32 = {
1104 "Microsoft Win32 (i386) object files",
1105 "win32",
1107 null_debug_arr,
1108 &null_debug_form,
1109 coff_stdmac,
1110 coff_win32_init,
1111 coff_set_info,
1112 coff_out,
1113 coff_deflabel,
1114 coff_section_names,
1115 coff_sectalign,
1116 coff_segbase,
1117 coff_directives,
1118 coff_win32_filename,
1119 coff_cleanup
1122 #endif
1124 #ifdef OF_WIN64
1126 struct ofmt of_win64 = {
1127 "Microsoft Win64 (x86-64) object files",
1128 "win64",
1130 null_debug_arr,
1131 &null_debug_form,
1132 coff_stdmac,
1133 coff_win64_init,
1134 coff_set_info,
1135 coff_out,
1136 coff_deflabel,
1137 coff_section_names,
1138 coff_sectalign,
1139 coff_segbase,
1140 coff_directives,
1141 coff_win32_filename,
1142 coff_cleanup
1145 #endif