configure.in: add --enable-werror option
[nasm.git] / output / outcoff.c
bloba712f953bd3be1c1b693e093af935fe0523f680e
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2014 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 bool win32, win64;
105 static int32_t imagebase_sect;
106 #define WRT_IMAGEBASE "..imagebase"
108 char coff_infile[FILENAME_MAX];
109 char coff_outfile[FILENAME_MAX];
112 * Some common section flags by default
114 #define TEXT_FLAGS_WIN \
115 (IMAGE_SCN_CNT_CODE | \
116 IMAGE_SCN_ALIGN_16BYTES | \
117 IMAGE_SCN_MEM_EXECUTE | \
118 IMAGE_SCN_MEM_READ)
119 #define TEXT_FLAGS_DOS \
120 (IMAGE_SCN_CNT_CODE)
122 #define DATA_FLAGS_WIN \
123 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
124 IMAGE_SCN_ALIGN_4BYTES | \
125 IMAGE_SCN_MEM_READ | \
126 IMAGE_SCN_MEM_WRITE)
127 #define DATA_FLAGS_DOS \
128 (IMAGE_SCN_CNT_INITIALIZED_DATA)
130 #define BSS_FLAGS_WIN \
131 (IMAGE_SCN_CNT_UNINITIALIZED_DATA | \
132 IMAGE_SCN_ALIGN_4BYTES | \
133 IMAGE_SCN_MEM_READ | \
134 IMAGE_SCN_MEM_WRITE)
135 #define BSS_FLAGS_DOS \
136 (IMAGE_SCN_CNT_UNINITIALIZED_DATA)
138 #define RDATA_FLAGS_WIN \
139 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
140 IMAGE_SCN_ALIGN_8BYTES | \
141 IMAGE_SCN_MEM_READ)
143 #define RDATA_FLAGS_DOS \
144 (IMAGE_SCN_CNT_INITIALIZED_DATA)
146 #define PDATA_FLAGS \
147 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
148 IMAGE_SCN_ALIGN_4BYTES | \
149 IMAGE_SCN_MEM_READ)
151 #define XDATA_FLAGS \
152 (IMAGE_SCN_CNT_INITIALIZED_DATA | \
153 IMAGE_SCN_ALIGN_8BYTES | \
154 IMAGE_SCN_MEM_READ)
156 #define INFO_FLAGS \
157 (IMAGE_SCN_ALIGN_1BYTES | \
158 IMAGE_SCN_LNK_INFO | \
159 IMAGE_SCN_LNK_REMOVE)
161 #define TEXT_FLAGS ((win32 | win64) ? TEXT_FLAGS_WIN : TEXT_FLAGS_DOS)
162 #define DATA_FLAGS ((win32 | win64) ? DATA_FLAGS_WIN : DATA_FLAGS_DOS)
163 #define BSS_FLAGS ((win32 | win64) ? BSS_FLAGS_WIN : BSS_FLAGS_DOS)
164 #define RDATA_FLAGS ((win32 | win64) ? RDATA_FLAGS_WIN : RDATA_FLAGS_DOS)
166 #define SECT_DELTA 32
167 struct coff_Section **coff_sects;
168 static int sectlen;
169 int coff_nsects;
171 struct SAA *coff_syms;
172 uint32_t coff_nsyms;
174 static int32_t def_seg;
176 static int initsym;
178 static struct RAA *bsym, *symval;
180 struct SAA *coff_strs;
181 static uint32_t strslen;
183 static void coff_gen_init(void);
184 static void coff_sect_write(struct coff_Section *, const uint8_t *, uint32_t);
185 static void coff_write(void);
186 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int32_t, int, int32_t);
187 static void coff_write_relocs(struct coff_Section *);
188 static void coff_write_symbols(void);
190 static void coff_win32_init(void)
192 win32 = true;
193 win64 = false;
194 coff_gen_init();
197 static void coff_win64_init(void)
199 win32 = false;
200 win64 = true;
201 coff_gen_init();
202 imagebase_sect = seg_alloc()+1;
203 define_label(WRT_IMAGEBASE, imagebase_sect, 0, NULL, false, false);
206 static void coff_std_init(void)
208 win32 = win64 = false;
209 coff_gen_init();
212 static void coff_gen_init(void)
215 coff_sects = NULL;
216 coff_nsects = sectlen = 0;
217 coff_syms = saa_init(sizeof(struct coff_Symbol));
218 coff_nsyms = 0;
219 bsym = raa_init();
220 symval = raa_init();
221 coff_strs = saa_init(1);
222 strslen = 0;
223 def_seg = seg_alloc();
226 static void coff_cleanup(int debuginfo)
228 struct coff_Reloc *r;
229 int i;
231 if (debuginfo && ofmt->current_dfmt->cleanup)
232 ofmt->current_dfmt->cleanup();
234 coff_write();
235 for (i = 0; i < coff_nsects; i++) {
236 if (coff_sects[i]->data)
237 saa_free(coff_sects[i]->data);
238 while (coff_sects[i]->head) {
239 r = coff_sects[i]->head;
240 coff_sects[i]->head = coff_sects[i]->head->next;
241 nasm_free(r);
243 nasm_free(coff_sects[i]->name);
244 nasm_free(coff_sects[i]);
246 nasm_free(coff_sects);
247 saa_free(coff_syms);
248 raa_free(bsym);
249 raa_free(symval);
250 saa_free(coff_strs);
253 int coff_make_section(char *name, uint32_t flags)
255 struct coff_Section *s;
256 size_t namelen;
258 s = nasm_zalloc(sizeof(*s));
260 if (flags != BSS_FLAGS)
261 s->data = saa_init(1);
262 s->tail = &s->head;
263 if (!strcmp(name, ".text"))
264 s->index = def_seg;
265 else
266 s->index = seg_alloc();
267 s->namepos = -1;
268 namelen = strlen(name);
269 if (namelen > 8) {
270 if (win32 || win64) {
271 s->namepos = strslen + 4;
272 saa_wbytes(coff_strs, name, namelen + 1);
273 strslen += namelen + 1;
274 } else {
275 namelen = 8;
278 s->name = nasm_malloc(namelen + 1);
279 strncpy(s->name, name, namelen);
280 s->name[namelen] = '\0';
281 s->flags = flags;
283 if (coff_nsects >= sectlen) {
284 sectlen += SECT_DELTA;
285 coff_sects = nasm_realloc(coff_sects, sectlen * sizeof(*coff_sects));
287 coff_sects[coff_nsects++] = s;
289 return coff_nsects - 1;
292 static inline int32_t coff_sectalign_flags(unsigned int align)
294 return (ilog2_32(align) + 1) << 20;
297 static int32_t coff_section_names(char *name, int pass, int *bits)
299 char *p;
300 uint32_t flags, align_and = ~0L, align_or = 0L;
301 int i;
304 * Set default bits.
306 if (!name) {
307 if(win64)
308 *bits = 64;
309 else
310 *bits = 32;
313 if (!name)
314 return def_seg;
316 p = name;
317 while (*p && !nasm_isspace(*p))
318 p++;
319 if (*p)
320 *p++ = '\0';
321 if (strlen(name) > 8) {
322 if (!win32 && !win64) {
323 nasm_error(ERR_WARNING,
324 "COFF section names limited to 8 characters: truncating");
325 name[8] = '\0';
328 flags = 0;
330 while (*p && nasm_isspace(*p))
331 p++;
332 while (*p) {
333 char *q = p;
334 while (*p && !nasm_isspace(*p))
335 p++;
336 if (*p)
337 *p++ = '\0';
338 while (*p && nasm_isspace(*p))
339 p++;
341 if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
342 flags = TEXT_FLAGS;
343 } else if (!nasm_stricmp(q, "data")) {
344 flags = DATA_FLAGS;
345 } else if (!nasm_stricmp(q, "rdata")) {
346 if (win32 | win64)
347 flags = RDATA_FLAGS;
348 else {
349 flags = DATA_FLAGS; /* gotta do something */
350 nasm_error(ERR_NONFATAL, "standard COFF does not support"
351 " read-only data sections");
353 } else if (!nasm_stricmp(q, "bss")) {
354 flags = BSS_FLAGS;
355 } else if (!nasm_stricmp(q, "info")) {
356 if (win32 | win64)
357 flags = INFO_FLAGS;
358 else {
359 flags = DATA_FLAGS; /* gotta do something */
360 nasm_error(ERR_NONFATAL, "standard COFF does not support"
361 " informational sections");
363 } else if (!nasm_strnicmp(q, "align=", 6)) {
364 if (!(win32 | win64))
365 nasm_error(ERR_NONFATAL, "standard COFF does not support"
366 " section alignment specification");
367 else {
368 if (q[6 + strspn(q + 6, "0123456789")])
369 nasm_error(ERR_NONFATAL,
370 "argument to `align' is not numeric");
371 else {
372 unsigned int align = atoi(q + 6);
373 if (!align || ((align - 1) & align))
374 nasm_error(ERR_NONFATAL, "argument to `align' is not a"
375 " power of two");
376 else if (align > 64)
377 nasm_error(ERR_NONFATAL, "Win32 cannot align sections"
378 " to better than 64-byte boundaries");
379 else {
380 align_and = ~0x00F00000L;
381 align_or = coff_sectalign_flags(align);
388 for (i = 0; i < coff_nsects; i++)
389 if (!strcmp(name, coff_sects[i]->name))
390 break;
391 if (i == coff_nsects) {
392 if (!flags) {
393 if (!strcmp(name, ".data"))
394 flags = DATA_FLAGS;
395 else if (!strcmp(name, ".rdata"))
396 flags = RDATA_FLAGS;
397 else if (!strcmp(name, ".bss"))
398 flags = BSS_FLAGS;
399 else if (win64 && !strcmp(name, ".pdata"))
400 flags = PDATA_FLAGS;
401 else if (win64 && !strcmp(name, ".xdata"))
402 flags = XDATA_FLAGS;
403 else
404 flags = TEXT_FLAGS;
406 i = coff_make_section(name, flags);
407 if (flags)
408 coff_sects[i]->flags = flags;
409 coff_sects[i]->flags &= align_and;
410 coff_sects[i]->flags |= align_or;
411 } else if (pass == 1) {
412 /* Check if any flags are specified */
413 if (flags) {
414 unsigned int align_flags = flags & IMAGE_SCN_ALIGN_MASK;
416 /* Warn if non-alignment flags differ */
417 if ((flags ^ coff_sects[i]->flags) & ~IMAGE_SCN_ALIGN_MASK) {
418 nasm_error(ERR_WARNING, "section attributes ignored on"
419 " redeclaration of section `%s'", name);
421 /* Check if alignment might be needed */
422 if (align_flags > IMAGE_SCN_ALIGN_1BYTES) {
423 unsigned int sect_align_flags = coff_sects[i]->flags & IMAGE_SCN_ALIGN_MASK;
425 /* Compute the actual alignment */
426 unsigned int align = 1u << ((align_flags - IMAGE_SCN_ALIGN_1BYTES) >> 20);
428 /* Update section header as needed */
429 if (align_flags > sect_align_flags) {
430 coff_sects[i]->flags = (coff_sects[i]->flags & ~IMAGE_SCN_ALIGN_MASK) | align_flags;
432 /* Check if not already aligned */
433 if (coff_sects[i]->len % align) {
434 unsigned int padding = (align - coff_sects[i]->len) % align;
435 /* We need to write at most 8095 bytes */
436 char buffer[8095];
437 if (coff_sects[i]->flags & IMAGE_SCN_CNT_CODE) {
438 /* Fill with INT 3 instructions */
439 memset(buffer, 0xCC, padding);
440 } else {
441 memset(buffer, 0x00, padding);
443 saa_wbytes(coff_sects[i]->data, buffer, padding);
444 coff_sects[i]->len += padding;
450 return coff_sects[i]->index;
453 static void coff_deflabel(char *name, int32_t segment, int64_t offset,
454 int is_global, char *special)
456 int pos = strslen + 4;
457 struct coff_Symbol *sym;
459 if (special)
460 nasm_error(ERR_NONFATAL, "COFF format does not support any"
461 " special symbol types");
463 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
464 if (strcmp(name,WRT_IMAGEBASE))
465 nasm_error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
466 return;
469 if (strlen(name) > 8) {
470 size_t nlen = strlen(name)+1;
471 saa_wbytes(coff_strs, name, nlen);
472 strslen += nlen;
473 } else
474 pos = -1;
476 sym = saa_wstruct(coff_syms);
478 sym->strpos = pos;
479 sym->namlen = strlen(name);
480 if (pos == -1)
481 strcpy(sym->name, name);
482 sym->is_global = !!is_global;
483 sym->type = 0; /* Default to T_NULL (no type) */
484 if (segment == NO_SEG)
485 sym->section = -1; /* absolute symbol */
486 else {
487 int i;
488 sym->section = 0;
489 for (i = 0; i < coff_nsects; i++)
490 if (segment == coff_sects[i]->index) {
491 sym->section = i + 1;
492 break;
494 if (!sym->section)
495 sym->is_global = true;
497 if (is_global == 2)
498 sym->value = offset;
499 else
500 sym->value = (sym->section == 0 ? 0 : offset);
503 * define the references from external-symbol segment numbers
504 * to these symbol records.
506 if (sym->section == 0)
507 bsym = raa_write(bsym, segment, coff_nsyms);
509 if (segment != NO_SEG)
510 symval = raa_write(symval, segment, sym->section ? 0 : sym->value);
512 coff_nsyms++;
515 static int32_t coff_add_reloc(struct coff_Section *sect, int32_t segment,
516 int16_t type)
518 struct coff_Reloc *r;
520 r = *sect->tail = nasm_malloc(sizeof(struct coff_Reloc));
521 sect->tail = &r->next;
522 r->next = NULL;
524 r->address = sect->len;
525 if (segment == NO_SEG) {
526 r->symbol = 0, r->symbase = ABS_SYMBOL;
527 } else {
528 int i;
529 r->symbase = REAL_SYMBOLS;
530 for (i = 0; i < coff_nsects; i++) {
531 if (segment == coff_sects[i]->index) {
532 r->symbol = i * 2;
533 r->symbase = SECT_SYMBOLS;
534 break;
537 if (r->symbase == REAL_SYMBOLS)
538 r->symbol = raa_read(bsym, segment);
540 r->type = type;
542 sect->nrelocs++;
545 * Return the fixup for standard COFF common variables.
547 if (r->symbase == REAL_SYMBOLS && !(win32 | win64))
548 return raa_read(symval, segment);
550 return 0;
553 static void coff_out(int32_t segto, const void *data,
554 enum out_type type, uint64_t size,
555 int32_t segment, int32_t wrt)
557 struct coff_Section *s;
558 uint8_t mydata[8], *p;
559 int i;
561 if (wrt != NO_SEG && !win64) {
562 wrt = NO_SEG; /* continue to do _something_ */
563 nasm_error(ERR_NONFATAL, "WRT not supported by COFF output formats");
567 * handle absolute-assembly (structure definitions)
569 if (segto == NO_SEG) {
570 if (type != OUT_RESERVE)
571 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
572 " space");
573 return;
576 s = NULL;
577 for (i = 0; i < coff_nsects; i++) {
578 if (segto == coff_sects[i]->index) {
579 s = coff_sects[i];
580 break;
583 if (!s) {
584 int tempint; /* ignored */
585 if (segto != coff_section_names(".text", 2, &tempint))
586 nasm_error(ERR_PANIC, "strange segment conditions in COFF driver");
587 else
588 s = coff_sects[coff_nsects - 1];
591 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
592 if (win64 && wrt == NO_SEG) {
593 if (!strcmp(s->name,".pdata") || !strcmp(s->name,".xdata"))
594 wrt = imagebase_sect;
597 if (!s->data && type != OUT_RESERVE) {
598 nasm_error(ERR_WARNING, "attempt to initialize memory in"
599 " BSS section `%s': ignored", s->name);
600 s->len += realsize(type, size);
601 return;
604 memset(mydata, 0, sizeof(mydata));
606 if (ofmt->current_dfmt && ofmt->current_dfmt->debug_output) {
607 struct coff_DebugInfo dinfo;
608 dinfo.segto = segto;
609 dinfo.seg = segment;
610 dinfo.section = s;
612 if (type == OUT_ADDRESS)
613 dinfo.size = abs((int)size);
614 else
615 dinfo.size = realsize(type, size);
617 ofmt->current_dfmt->debug_output(type, &dinfo);
620 if (type == OUT_RESERVE) {
621 if (s->data) {
622 nasm_error(ERR_WARNING, "uninitialised space declared in"
623 " non-BSS section `%s': zeroing", s->name);
624 coff_sect_write(s, NULL, size);
625 } else
626 s->len += size;
627 } else if (type == OUT_RAWDATA) {
628 if (segment != NO_SEG)
629 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
630 coff_sect_write(s, data, size);
631 } else if (type == OUT_ADDRESS) {
632 int asize = abs((int)size);
633 if (!win64) {
634 if (asize != 4 && (segment != NO_SEG || wrt != NO_SEG)) {
635 nasm_error(ERR_NONFATAL, "COFF format does not support non-32-bit"
636 " relocations");
637 } else {
638 int32_t fix = 0;
639 if (segment != NO_SEG || wrt != NO_SEG) {
640 if (wrt != NO_SEG) {
641 nasm_error(ERR_NONFATAL, "COFF format does not support"
642 " WRT types");
643 } else if (segment % 2) {
644 nasm_error(ERR_NONFATAL, "COFF format does not support"
645 " segment base references");
646 } else
647 fix = coff_add_reloc(s, segment, IMAGE_REL_I386_DIR32);
649 p = mydata;
650 WRITELONG(p, *(int64_t *)data + fix);
651 coff_sect_write(s, mydata, asize);
653 } else {
654 int32_t fix = 0;
655 p = mydata;
656 if (asize == 8) {
657 if (wrt == imagebase_sect) {
658 nasm_error(ERR_NONFATAL, "operand size mismatch: 'wrt "
659 WRT_IMAGEBASE "' is a 32-bit operand");
661 fix = coff_add_reloc(s, segment, IMAGE_REL_AMD64_ADDR64);
662 WRITEDLONG(p, *(int64_t *)data + fix);
663 coff_sect_write(s, mydata, asize);
664 } else {
665 fix = coff_add_reloc(s, segment,
666 wrt == imagebase_sect ? IMAGE_REL_AMD64_ADDR32NB:
667 IMAGE_REL_AMD64_ADDR32);
668 WRITELONG(p, *(int64_t *)data + fix);
669 coff_sect_write(s, mydata, asize);
672 } else if (type == OUT_REL2ADR) {
673 nasm_error(ERR_NONFATAL, "COFF format does not support 16-bit"
674 " relocations");
675 } else if (type == OUT_REL4ADR) {
676 if (segment == segto && !(win64)) /* Acceptable for RIP-relative */
677 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
678 else if (segment == NO_SEG && win32)
679 nasm_error(ERR_NONFATAL, "Win32 COFF does not correctly support"
680 " relative references to absolute addresses");
681 else {
682 int32_t fix = 0;
683 if (segment != NO_SEG && segment % 2) {
684 nasm_error(ERR_NONFATAL, "COFF format does not support"
685 " segment base references");
686 } else
687 fix = coff_add_reloc(s, segment,
688 win64 ? IMAGE_REL_AMD64_REL32 : IMAGE_REL_I386_REL32);
689 p = mydata;
690 if (win32 | win64) {
691 WRITELONG(p, *(int64_t *)data + 4 - size + fix);
692 } else {
693 WRITELONG(p, *(int64_t *)data - (size + s->len) + fix);
695 coff_sect_write(s, mydata, 4L);
701 static void coff_sect_write(struct coff_Section *sect,
702 const uint8_t *data, uint32_t len)
704 saa_wbytes(sect->data, data, len);
705 sect->len += len;
708 typedef struct tagString {
709 struct tagString *next;
710 int len;
711 char *String;
712 } STRING;
714 #define EXPORT_SECTION_NAME ".drectve"
715 #define EXPORT_SECTION_FLAGS INFO_FLAGS
717 * #define EXPORT_SECTION_NAME ".text"
718 * #define EXPORT_SECTION_FLAGS TEXT_FLAGS
721 static STRING *Exports = NULL;
722 static struct coff_Section *directive_sec;
723 static void AddExport(char *name)
725 STRING *rvp = Exports, *newS;
727 newS = (STRING *) nasm_malloc(sizeof(STRING));
728 newS->len = strlen(name);
729 newS->next = NULL;
730 newS->String = (char *)nasm_malloc(newS->len + 1);
731 strcpy(newS->String, name);
732 if (rvp == NULL) {
733 int i;
735 for (i = 0; i < coff_nsects; i++) {
736 if (!strcmp(EXPORT_SECTION_NAME, coff_sects[i]->name))
737 break;
740 if (i == coff_nsects)
741 i = coff_make_section(EXPORT_SECTION_NAME, EXPORT_SECTION_FLAGS);
743 directive_sec = coff_sects[i];
744 Exports = newS;
745 } else {
746 while (rvp->next) {
747 if (!strcmp(rvp->String, name))
748 return;
749 rvp = rvp->next;
751 rvp->next = newS;
755 static void BuildExportTable(STRING **rvp)
757 STRING *p, *t;
759 if (!rvp || !*rvp)
760 return;
762 list_for_each_safe(p, t, *rvp) {
763 coff_sect_write(directive_sec, (uint8_t *)"-export:", 8);
764 coff_sect_write(directive_sec, (uint8_t *)p->String, p->len);
765 coff_sect_write(directive_sec, (uint8_t *)" ", 1);
766 nasm_free(p->String);
767 nasm_free(p);
770 *rvp = NULL;
773 static int coff_directives(enum directives directive, char *value, int pass)
775 switch (directive) {
776 case D_EXPORT:
778 char *q, *name;
780 if (pass == 2)
781 return 1; /* ignore in pass two */
782 name = q = value;
783 while (*q && !nasm_isspace(*q))
784 q++;
785 if (nasm_isspace(*q)) {
786 *q++ = '\0';
787 while (*q && nasm_isspace(*q))
788 q++;
791 if (!*name) {
792 nasm_error(ERR_NONFATAL, "`export' directive requires export name");
793 return 1;
795 if (*q) {
796 nasm_error(ERR_NONFATAL, "unrecognized export qualifier `%s'", q);
797 return 1;
799 AddExport(name);
800 return 1;
802 case D_SAFESEH:
804 static int sxseg=-1;
805 int i;
807 if (!win32) /* Only applicable for -f win32 */
808 return 0;
810 if (sxseg == -1) {
811 for (i = 0; i < coff_nsects; i++)
812 if (!strcmp(".sxdata",coff_sects[i]->name))
813 break;
814 if (i == coff_nsects)
815 sxseg = coff_make_section(".sxdata", IMAGE_SCN_LNK_INFO);
816 else
817 sxseg = i;
820 * pass0 == 2 is the only time when the full set of symbols are
821 * guaranteed to be present; it is the final output pass.
823 if (pass0 == 2) {
824 uint32_t n;
825 saa_rewind(coff_syms);
826 for (n = 0; n < coff_nsyms; n++) {
827 struct coff_Symbol *sym = saa_rstruct(coff_syms);
828 bool equals;
831 * sym->strpos is biased by 4, because symbol
832 * table is prefixed with table length
834 if (sym->strpos >=4) {
835 char *name = nasm_malloc(sym->namlen+1);
836 saa_fread(coff_strs, sym->strpos-4, name, sym->namlen);
837 name[sym->namlen] = '\0';
838 equals = !strcmp(value,name);
839 nasm_free(name);
840 } else {
841 equals = !strcmp(value,sym->name);
844 if (equals) {
846 * this value arithmetics effectively reflects
847 * initsym in coff_write(): 2 for file, 1 for
848 * .absolute and two per each section
850 unsigned char value[4],*p=value;
851 WRITELONG(p,n + 2 + 1 + coff_nsects*2);
852 coff_sect_write(coff_sects[sxseg],value,4);
853 sym->type = 0x20;
854 break;
857 if (n == coff_nsyms) {
858 nasm_error(ERR_NONFATAL,
859 "`safeseh' directive requires valid symbol");
862 return 1;
864 default:
865 return 0;
869 /* handle relocations storm, valid for win32/64 only */
870 static inline void coff_adjust_relocs(struct coff_Section *s)
872 if (s->nrelocs < IMAGE_SCN_MAX_RELOC)
873 return;
874 #ifdef OF_COFF
875 else
877 if (ofmt == &of_coff)
878 nasm_error(ERR_FATAL,
879 "Too many relocations (%d) for section `%s'",
880 s->nrelocs, s->name);
882 #endif
884 s->flags |= IMAGE_SCN_LNK_NRELOC_OVFL;
885 s->nrelocs++;
888 static void coff_write(void)
890 int32_t pos, sympos, vsize;
891 int i;
893 /* fill in the .drectve section with -export's */
894 BuildExportTable(&Exports);
896 if (win32) {
897 /* add default value for @feat.00, this allows to 'link /safeseh' */
898 uint32_t n;
900 saa_rewind(coff_syms);
901 for (n = 0; n < coff_nsyms; n++) {
902 struct coff_Symbol *sym = saa_rstruct(coff_syms);
903 if (sym->strpos == -1 && !strcmp("@feat.00",sym->name))
904 break;
906 if (n == coff_nsyms)
907 coff_deflabel("@feat.00", NO_SEG, 1, 0, NULL);
911 * Work out how big the file will get.
912 * Calculate the start of the `real' symbols at the same time.
913 * Check for massive relocations.
915 pos = 0x14 + 0x28 * coff_nsects;
916 initsym = 3; /* two for the file, one absolute */
917 for (i = 0; i < coff_nsects; i++) {
918 if (coff_sects[i]->data) {
919 coff_adjust_relocs(coff_sects[i]);
920 coff_sects[i]->pos = pos;
921 pos += coff_sects[i]->len;
922 coff_sects[i]->relpos = pos;
923 pos += 10 * coff_sects[i]->nrelocs;
924 } else
925 coff_sects[i]->pos = coff_sects[i]->relpos = 0L;
926 initsym += 2; /* two for each section */
928 sympos = pos;
931 * Output the COFF header.
933 if (win64)
934 i = IMAGE_FILE_MACHINE_AMD64;
935 else
936 i = IMAGE_FILE_MACHINE_I386;
937 fwriteint16_t(i, ofile); /* machine type */
938 fwriteint16_t(coff_nsects, ofile); /* number of sections */
939 fwriteint32_t(time(NULL), ofile); /* time stamp */
940 fwriteint32_t(sympos, ofile);
941 fwriteint32_t(coff_nsyms + initsym, ofile);
942 fwriteint16_t(0, ofile); /* no optional header */
943 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
944 fwriteint16_t((win32 | win64) ? 0 : 0x104, ofile);
947 * Output the section headers.
949 vsize = 0L;
950 for (i = 0; i < coff_nsects; i++) {
951 coff_section_header(coff_sects[i]->name, coff_sects[i]->namepos, vsize, coff_sects[i]->len,
952 coff_sects[i]->pos, coff_sects[i]->relpos,
953 coff_sects[i]->nrelocs, coff_sects[i]->flags);
954 vsize += coff_sects[i]->len;
958 * Output the sections and their relocations.
960 for (i = 0; i < coff_nsects; i++)
961 if (coff_sects[i]->data) {
962 saa_fpwrite(coff_sects[i]->data, ofile);
963 coff_write_relocs(coff_sects[i]);
967 * Output the symbol and string tables.
969 coff_write_symbols();
970 fwriteint32_t(strslen + 4, ofile); /* length includes length count */
971 saa_fpwrite(coff_strs, ofile);
974 static void coff_section_header(char *name, int32_t namepos, int32_t vsize,
975 int32_t datalen, int32_t datapos,
976 int32_t relpos, int nrelocs, int32_t flags)
978 char padname[8];
980 (void)vsize;
982 if (namepos == -1) {
983 strncpy(padname, name, 8);
984 nasm_write(padname, 8, ofile);
985 } else {
987 * If name is longer than 8 bytes, write '/' followed
988 * by offset into the strings table represented as
989 * decimal number.
991 namepos = namepos % 100000000;
992 padname[0] = '/';
993 padname[1] = '0' + (namepos / 1000000);
994 namepos = namepos % 1000000;
995 padname[2] = '0' + (namepos / 100000);
996 namepos = namepos % 100000;
997 padname[3] = '0' + (namepos / 10000);
998 namepos = namepos % 10000;
999 padname[4] = '0' + (namepos / 1000);
1000 namepos = namepos % 1000;
1001 padname[5] = '0' + (namepos / 100);
1002 namepos = namepos % 100;
1003 padname[6] = '0' + (namepos / 10);
1004 namepos = namepos % 10;
1005 padname[7] = '0' + (namepos);
1006 nasm_write(padname, 8, ofile);
1009 fwriteint32_t(0, ofile); /* Virtual size field - set to 0 or vsize */
1010 fwriteint32_t(0L, ofile); /* RVA/offset - we ignore */
1011 fwriteint32_t(datalen, ofile);
1012 fwriteint32_t(datapos, ofile);
1013 fwriteint32_t(relpos, ofile);
1014 fwriteint32_t(0L, ofile); /* no line numbers - we don't do 'em */
1017 * a special case -- if there are too many relocs
1018 * we have to put IMAGE_SCN_MAX_RELOC here and write
1019 * the real relocs number into VirtualAddress of first
1020 * relocation
1022 if (flags & IMAGE_SCN_LNK_NRELOC_OVFL)
1023 fwriteint16_t(IMAGE_SCN_MAX_RELOC, ofile);
1024 else
1025 fwriteint16_t(nrelocs, ofile);
1027 fwriteint16_t(0, ofile); /* again, no line numbers */
1028 fwriteint32_t(flags, ofile);
1031 static void coff_write_relocs(struct coff_Section *s)
1033 struct coff_Reloc *r;
1035 /* a real number of relocations if needed */
1036 if (s->flags & IMAGE_SCN_LNK_NRELOC_OVFL) {
1037 fwriteint32_t(s->nrelocs, ofile);
1038 fwriteint32_t(0, ofile);
1039 fwriteint16_t(0, ofile);
1042 for (r = s->head; r; r = r->next) {
1043 fwriteint32_t(r->address, ofile);
1044 fwriteint32_t(r->symbol + (r->symbase == REAL_SYMBOLS ? initsym :
1045 r->symbase == ABS_SYMBOL ? initsym - 1 :
1046 r->symbase == SECT_SYMBOLS ? 2 : 0),
1047 ofile);
1048 fwriteint16_t(r->type, ofile);
1052 static void coff_symbol(char *name, int32_t strpos, int32_t value,
1053 int section, int type, int storageclass, int aux)
1055 char padname[8];
1057 if (name) {
1058 strncpy(padname, name, 8);
1059 nasm_write(padname, 8, ofile);
1060 } else {
1061 fwriteint32_t(0, ofile);
1062 fwriteint32_t(strpos, ofile);
1065 fwriteint32_t(value, ofile);
1066 fwriteint16_t(section, ofile);
1067 fwriteint16_t(type, ofile);
1069 fputc(storageclass, ofile);
1070 fputc(aux, ofile);
1073 static void coff_write_symbols(void)
1075 char filename[18];
1076 uint32_t i;
1079 * The `.file' record, and the file name auxiliary record.
1081 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
1082 strncpy(filename, coff_infile, 18);
1083 nasm_write(filename, 18, ofile);
1086 * The section records, with their auxiliaries.
1088 memset(filename, 0, 18); /* useful zeroed buffer */
1090 for (i = 0; i < (uint32_t) coff_nsects; i++) {
1091 coff_symbol(coff_sects[i]->name, 0L, 0L, i + 1, 0, 3, 1);
1092 fwriteint32_t(coff_sects[i]->len, ofile);
1093 fwriteint16_t(coff_sects[i]->nrelocs,ofile);
1094 nasm_write(filename, 12, ofile);
1098 * The absolute symbol, for relative-to-absolute relocations.
1100 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
1103 * The real symbols.
1105 saa_rewind(coff_syms);
1106 for (i = 0; i < coff_nsyms; i++) {
1107 struct coff_Symbol *sym = saa_rstruct(coff_syms);
1108 coff_symbol(sym->strpos == -1 ? sym->name : NULL,
1109 sym->strpos, sym->value, sym->section,
1110 sym->type, sym->is_global ? 2 : 3, 0);
1114 static void coff_sectalign(int32_t seg, unsigned int value)
1116 struct coff_Section *s = NULL;
1117 uint32_t align;
1118 int i;
1120 for (i = 0; i < coff_nsects; i++) {
1121 if (coff_sects[i]->index == seg) {
1122 s = coff_sects[i];
1123 break;
1127 if (!s || !is_power2(value))
1128 return;
1130 /* DOS has limitation on 64 bytes */
1131 if (!(win32 | win64) && value > 64)
1132 return;
1134 align = (s->flags & IMAGE_SCN_ALIGN_MASK);
1135 value = coff_sectalign_flags(value);
1136 if (value > align)
1137 s->flags = (s->flags & ~IMAGE_SCN_ALIGN_MASK) | value;
1140 static int32_t coff_segbase(int32_t segment)
1142 return segment;
1145 static void coff_std_filename(char *inname, char *outname)
1147 strcpy(coff_infile, inname);
1148 standard_extension(inname, outname, ".o");
1149 strcpy(coff_outfile, outname);
1152 static void coff_win32_filename(char *inname, char *outname)
1154 strcpy(coff_infile, inname);
1155 standard_extension(inname, outname, ".obj");
1156 strcpy(coff_outfile, outname);
1159 extern macros_t coff_stdmac[];
1161 static int coff_set_info(enum geninfo type, char **val)
1163 (void)type;
1164 (void)val;
1165 return 0;
1167 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
1169 #ifdef OF_COFF
1171 struct ofmt of_coff = {
1172 "COFF (i386) object files (e.g. DJGPP for DOS)",
1173 "coff",
1176 null_debug_arr,
1177 &null_debug_form,
1178 coff_stdmac,
1179 coff_std_init,
1180 coff_set_info,
1181 coff_out,
1182 coff_deflabel,
1183 coff_section_names,
1184 coff_sectalign,
1185 coff_segbase,
1186 coff_directives,
1187 coff_std_filename,
1188 coff_cleanup
1191 #endif
1193 extern struct dfmt df_cv8;
1195 #ifdef OF_WIN32
1197 struct dfmt *win32_debug_arr[2] = { &df_cv8, NULL };
1199 struct ofmt of_win32 = {
1200 "Microsoft Win32 (i386) object files",
1201 "win32",
1204 win32_debug_arr,
1205 &df_cv8,
1206 coff_stdmac,
1207 coff_win32_init,
1208 coff_set_info,
1209 coff_out,
1210 coff_deflabel,
1211 coff_section_names,
1212 coff_sectalign,
1213 coff_segbase,
1214 coff_directives,
1215 coff_win32_filename,
1216 coff_cleanup
1219 #endif
1221 #ifdef OF_WIN64
1223 struct dfmt *win64_debug_arr[2] = { &df_cv8, NULL };
1225 struct ofmt of_win64 = {
1226 "Microsoft Win64 (x86-64) object files",
1227 "win64",
1230 win64_debug_arr,
1231 &df_cv8,
1232 coff_stdmac,
1233 coff_win64_init,
1234 coff_set_info,
1235 coff_out,
1236 coff_deflabel,
1237 coff_section_names,
1238 coff_sectalign,
1239 coff_segbase,
1240 coff_directives,
1241 coff_win32_filename,
1242 coff_cleanup
1245 #endif