Remove open-coded ilog2() implementations
[nasm.git] / output / outcoff.c
blob841e5fe7491a781b3e96b2a0585bd73cea044af9
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"
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 TEXT_FLAGS ((win32 | win64) ? TEXT_FLAGS_WIN : TEXT_FLAGS_DOS)
190 #define DATA_FLAGS ((win32 | win64) ? DATA_FLAGS_WIN : DATA_FLAGS_DOS)
191 #define BSS_FLAGS ((win32 | win64) ? BSS_FLAGS_WIN : BSS_FLAGS_DOS)
192 #define RDATA_FLAGS ((win32 | win64) ? RDATA_FLAGS_WIN : RDATA_FLAGS_DOS)
193 #define INFO_FLAGS 0x00100A00L
195 #define SECT_DELTA 32
196 static struct Section **sects;
197 static int nsects, sectlen;
199 static struct SAA *syms;
200 static uint32_t nsyms;
202 static int32_t def_seg;
204 static int initsym;
206 static struct RAA *bsym, *symval;
208 static struct SAA *strs;
209 static uint32_t strslen;
211 static void coff_gen_init(void);
212 static void coff_sect_write(struct Section *, const uint8_t *,
213 uint32_t);
214 static void coff_write(void);
215 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
216 static void coff_write_relocs(struct Section *);
217 static void coff_write_symbols(void);
219 static void coff_win32_init(void)
221 win32 = true;
222 win64 = false;
223 coff_gen_init();
226 static void coff_win64_init(void)
228 maxbits = 64;
229 win32 = false;
230 win64 = true;
231 coff_gen_init();
232 imagebase_sect = seg_alloc()+1;
233 define_label(WRT_IMAGEBASE, imagebase_sect, 0, NULL, false, false);
236 static void coff_std_init(void)
238 win32 = win64 = false;
239 coff_gen_init();
242 static void coff_gen_init(void)
245 sects = NULL;
246 nsects = sectlen = 0;
247 syms = saa_init(sizeof(struct Symbol));
248 nsyms = 0;
249 bsym = raa_init();
250 symval = raa_init();
251 strs = saa_init(1);
252 strslen = 0;
253 def_seg = seg_alloc();
256 static void coff_cleanup(int debuginfo)
258 struct Reloc *r;
259 int i;
261 (void)debuginfo;
263 coff_write();
264 for (i = 0; i < nsects; i++) {
265 if (sects[i]->data)
266 saa_free(sects[i]->data);
267 while (sects[i]->head) {
268 r = sects[i]->head;
269 sects[i]->head = sects[i]->head->next;
270 nasm_free(r);
272 nasm_free(sects[i]);
274 nasm_free(sects);
275 saa_free(syms);
276 raa_free(bsym);
277 raa_free(symval);
278 saa_free(strs);
281 static int coff_make_section(char *name, uint32_t flags)
283 struct Section *s;
285 s = nasm_malloc(sizeof(*s));
287 if (flags != BSS_FLAGS)
288 s->data = saa_init(1);
289 else
290 s->data = NULL;
291 s->head = NULL;
292 s->tail = &s->head;
293 s->len = 0;
294 s->nrelocs = 0;
295 if (!strcmp(name, ".text"))
296 s->index = def_seg;
297 else
298 s->index = seg_alloc();
299 strncpy(s->name, name, 8);
300 s->name[8] = '\0';
301 s->flags = flags;
303 if (nsects >= sectlen) {
304 sectlen += SECT_DELTA;
305 sects = nasm_realloc(sects, sectlen * sizeof(*sects));
307 sects[nsects++] = s;
309 return nsects - 1;
312 static int32_t coff_section_names(char *name, int pass, int *bits)
314 char *p;
315 uint32_t flags, align_and = ~0L, align_or = 0L;
316 int i;
319 * Set default bits.
321 if (!name) {
322 if(win64)
323 *bits = 64;
324 else
325 *bits = 32;
328 if (!name)
329 return def_seg;
331 p = name;
332 while (*p && !nasm_isspace(*p))
333 p++;
334 if (*p)
335 *p++ = '\0';
336 if (strlen(name) > 8) {
337 nasm_error(ERR_WARNING, "COFF section names limited to 8 characters:"
338 " truncating");
339 name[8] = '\0';
341 flags = 0;
343 while (*p && nasm_isspace(*p))
344 p++;
345 while (*p) {
346 char *q = p;
347 while (*p && !nasm_isspace(*p))
348 p++;
349 if (*p)
350 *p++ = '\0';
351 while (*p && nasm_isspace(*p))
352 p++;
354 if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
355 flags = TEXT_FLAGS;
356 } else if (!nasm_stricmp(q, "data")) {
357 flags = DATA_FLAGS;
358 } else if (!nasm_stricmp(q, "rdata")) {
359 if (win32 | win64)
360 flags = RDATA_FLAGS;
361 else {
362 flags = DATA_FLAGS; /* gotta do something */
363 nasm_error(ERR_NONFATAL, "standard COFF does not support"
364 " read-only data sections");
366 } else if (!nasm_stricmp(q, "bss")) {
367 flags = BSS_FLAGS;
368 } else if (!nasm_stricmp(q, "info")) {
369 if (win32 | win64)
370 flags = INFO_FLAGS;
371 else {
372 flags = DATA_FLAGS; /* gotta do something */
373 nasm_error(ERR_NONFATAL, "standard COFF does not support"
374 " informational sections");
376 } else if (!nasm_strnicmp(q, "align=", 6)) {
377 if (!(win32 | win64))
378 nasm_error(ERR_NONFATAL, "standard COFF does not support"
379 " section alignment specification");
380 else {
381 if (q[6 + strspn(q + 6, "0123456789")])
382 nasm_error(ERR_NONFATAL,
383 "argument to `align' is not numeric");
384 else {
385 unsigned int align = atoi(q + 6);
386 if (!align || ((align - 1) & align))
387 nasm_error(ERR_NONFATAL, "argument to `align' is not a"
388 " power of two");
389 else if (align > 64)
390 nasm_error(ERR_NONFATAL, "Win32 cannot align sections"
391 " to better than 64-byte boundaries");
392 else {
393 align_and = ~0x00F00000L;
394 align_or = ilog2_32(align) << 20;
401 for (i = 0; i < nsects; i++)
402 if (!strcmp(name, sects[i]->name))
403 break;
404 if (i == nsects) {
405 if (!flags) {
406 if (!strcmp(name, ".data"))
407 flags = DATA_FLAGS;
408 else if (!strcmp(name, ".rdata"))
409 flags = RDATA_FLAGS;
410 else if (!strcmp(name, ".bss"))
411 flags = BSS_FLAGS;
412 else if (win64 && !strcmp(name, ".pdata"))
413 flags = PDATA_FLAGS;
414 else if (win64 && !strcmp(name, ".xdata"))
415 flags = XDATA_FLAGS;
416 else
417 flags = TEXT_FLAGS;
419 i = coff_make_section(name, flags);
420 if (flags)
421 sects[i]->flags = flags;
422 sects[i]->flags &= align_and;
423 sects[i]->flags |= align_or;
424 } else if (pass == 1) {
425 if (flags)
426 nasm_error(ERR_WARNING, "section attributes ignored on"
427 " redeclaration of section `%s'", name);
430 return sects[i]->index;
433 static void coff_deflabel(char *name, int32_t segment, int64_t offset,
434 int is_global, char *special)
436 int pos = strslen + 4;
437 struct Symbol *sym;
439 if (special)
440 nasm_error(ERR_NONFATAL, "COFF format does not support any"
441 " special symbol types");
443 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
444 if (strcmp(name,WRT_IMAGEBASE))
445 nasm_error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
446 return;
449 if (strlen(name) > 8) {
450 size_t nlen = strlen(name)+1;
451 saa_wbytes(strs, name, nlen);
452 strslen += nlen;
453 } else
454 pos = -1;
456 sym = saa_wstruct(syms);
458 sym->strpos = pos;
459 sym->namlen = strlen(name);
460 if (pos == -1)
461 strcpy(sym->name, name);
462 sym->is_global = !!is_global;
463 sym->type = 0; /* Default to T_NULL (no type) */
464 if (segment == NO_SEG)
465 sym->section = -1; /* absolute symbol */
466 else {
467 int i;
468 sym->section = 0;
469 for (i = 0; i < nsects; i++)
470 if (segment == sects[i]->index) {
471 sym->section = i + 1;
472 break;
474 if (!sym->section)
475 sym->is_global = true;
477 if (is_global == 2)
478 sym->value = offset;
479 else
480 sym->value = (sym->section == 0 ? 0 : offset);
483 * define the references from external-symbol segment numbers
484 * to these symbol records.
486 if (sym->section == 0) {
487 bsym = raa_write(bsym, segment, nsyms);
490 if (segment != NO_SEG)
491 symval = raa_write(symval, segment, sym->section ? 0 : sym->value);
493 nsyms++;
496 static int32_t coff_add_reloc(struct Section *sect, int32_t segment,
497 int16_t type)
499 struct Reloc *r;
501 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
502 sect->tail = &r->next;
503 r->next = NULL;
505 r->address = sect->len;
506 if (segment == NO_SEG)
507 r->symbol = 0, r->symbase = ABS_SYMBOL;
508 else {
509 int i;
510 r->symbase = REAL_SYMBOLS;
511 for (i = 0; i < nsects; i++)
512 if (segment == sects[i]->index) {
513 r->symbol = i * 2;
514 r->symbase = SECT_SYMBOLS;
515 break;
517 if (r->symbase == REAL_SYMBOLS)
518 r->symbol = raa_read(bsym, segment);
520 r->type = type;
522 sect->nrelocs++;
525 * Return the fixup for standard COFF common variables.
527 if (r->symbase == REAL_SYMBOLS && !(win32 | win64))
528 return raa_read(symval, segment);
529 else
530 return 0;
533 static void coff_out(int32_t segto, const void *data,
534 enum out_type type, uint64_t size,
535 int32_t segment, int32_t wrt)
537 struct Section *s;
538 uint8_t mydata[8], *p;
539 int i;
541 if (wrt != NO_SEG && !win64) {
542 wrt = NO_SEG; /* continue to do _something_ */
543 nasm_error(ERR_NONFATAL, "WRT not supported by COFF output formats");
547 * handle absolute-assembly (structure definitions)
549 if (segto == NO_SEG) {
550 if (type != OUT_RESERVE)
551 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
552 " space");
553 return;
556 s = NULL;
557 for (i = 0; i < nsects; i++)
558 if (segto == sects[i]->index) {
559 s = sects[i];
560 break;
562 if (!s) {
563 int tempint; /* ignored */
564 if (segto != coff_section_names(".text", 2, &tempint))
565 nasm_error(ERR_PANIC, "strange segment conditions in COFF driver");
566 else
567 s = sects[nsects - 1];
570 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
571 if (win64 && wrt == NO_SEG &&
572 (!strcmp(s->name,".pdata") || !strcmp(s->name,".xdata")))
573 wrt = imagebase_sect;
575 if (!s->data && type != OUT_RESERVE) {
576 nasm_error(ERR_WARNING, "attempt to initialize memory in"
577 " BSS section `%s': ignored", s->name);
578 s->len += realsize(type, size);
579 return;
582 if (type == OUT_RESERVE) {
583 if (s->data) {
584 nasm_error(ERR_WARNING, "uninitialised space declared in"
585 " non-BSS section `%s': zeroing", s->name);
586 coff_sect_write(s, NULL, size);
587 } else
588 s->len += size;
589 } else if (type == OUT_RAWDATA) {
590 if (segment != NO_SEG)
591 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
592 coff_sect_write(s, data, size);
593 } else if (type == OUT_ADDRESS) {
594 if (!(win64)) {
595 if (size != 4 && (segment != NO_SEG || wrt != NO_SEG))
596 nasm_error(ERR_NONFATAL, "COFF format does not support non-32-bit"
597 " relocations");
598 else {
599 int32_t fix = 0;
600 if (segment != NO_SEG || wrt != NO_SEG) {
601 if (wrt != NO_SEG) {
602 nasm_error(ERR_NONFATAL, "COFF format does not support"
603 " WRT types");
604 } else if (segment % 2) {
605 nasm_error(ERR_NONFATAL, "COFF format does not support"
606 " segment base references");
607 } else
608 fix = coff_add_reloc(s, segment, IMAGE_REL_I386_DIR32);
610 p = mydata;
611 WRITELONG(p, *(int64_t *)data + fix);
612 coff_sect_write(s, mydata, size);
614 } else {
615 int32_t fix = 0;
616 p = mydata;
617 if (size == 8) {
618 if (wrt == imagebase_sect) {
619 nasm_error(ERR_NONFATAL, "operand size mismatch: 'wrt "
620 WRT_IMAGEBASE "' is a 32-bit operand");
622 fix = coff_add_reloc(s, segment, IMAGE_REL_AMD64_ADDR64);
623 WRITEDLONG(p, *(int64_t *)data + fix);
624 coff_sect_write(s, mydata, size);
625 } else {
626 fix = coff_add_reloc(s, segment,
627 wrt == imagebase_sect ? IMAGE_REL_AMD64_ADDR32NB:
628 IMAGE_REL_AMD64_ADDR32);
629 WRITELONG(p, *(int64_t *)data + fix);
630 coff_sect_write(s, mydata, size);
633 } else if (type == OUT_REL2ADR) {
634 nasm_error(ERR_NONFATAL, "COFF format does not support 16-bit"
635 " relocations");
636 } else if (type == OUT_REL4ADR) {
637 if (segment == segto && !(win64)) /* Acceptable for RIP-relative */
638 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
639 else if (segment == NO_SEG && win32)
640 nasm_error(ERR_NONFATAL, "Win32 COFF does not correctly support"
641 " relative references to absolute addresses");
642 else {
643 int32_t fix = 0;
644 if (segment != NO_SEG && segment % 2) {
645 nasm_error(ERR_NONFATAL, "COFF format does not support"
646 " segment base references");
647 } else
648 fix = coff_add_reloc(s, segment,
649 win64 ? IMAGE_REL_AMD64_REL32 : IMAGE_REL_I386_REL32);
650 p = mydata;
651 if (win32 | win64) {
652 WRITELONG(p, *(int64_t *)data + 4 - size + fix);
653 } else {
654 WRITELONG(p, *(int64_t *)data - (size + s->len) + fix);
656 coff_sect_write(s, mydata, 4L);
662 static void coff_sect_write(struct Section *sect,
663 const uint8_t *data, uint32_t len)
665 saa_wbytes(sect->data, data, len);
666 sect->len += len;
669 typedef struct tagString {
670 struct tagString *next;
671 int len;
672 char *String;
673 } STRING;
675 #define EXPORT_SECTION_NAME ".drectve"
676 #define EXPORT_SECTION_FLAGS INFO_FLAGS
678 #define EXPORT_SECTION_NAME ".text"
679 #define EXPORT_SECTION_FLAGS TEXT_FLAGS
682 static STRING *Exports = NULL;
683 static struct Section *directive_sec;
684 void AddExport(char *name)
686 STRING *rvp = Exports, *newS;
688 newS = (STRING *) nasm_malloc(sizeof(STRING));
689 newS->len = strlen(name);
690 newS->next = NULL;
691 newS->String = (char *)nasm_malloc(newS->len + 1);
692 strcpy(newS->String, name);
693 if (rvp == NULL) {
694 int i;
696 for (i = 0; i < nsects; i++) {
697 if (!strcmp(EXPORT_SECTION_NAME, sects[i]->name))
698 break;
701 if (i == nsects)
702 i = coff_make_section(EXPORT_SECTION_NAME, EXPORT_SECTION_FLAGS);
704 directive_sec = sects[i];
705 Exports = newS;
706 } else {
707 while (rvp->next) {
708 if (!strcmp(rvp->String, name))
709 return;
710 rvp = rvp->next;
712 rvp->next = newS;
716 static void BuildExportTable(STRING **rvp)
718 STRING *p, *t;
720 if (!rvp || !*rvp)
721 return;
723 list_for_each_safe(p, t, *rvp) {
724 coff_sect_write(directive_sec, (uint8_t *)"-export:", 8);
725 coff_sect_write(directive_sec, (uint8_t *)p->String, p->len);
726 coff_sect_write(directive_sec, (uint8_t *)" ", 1);
727 nasm_free(p->String);
728 nasm_free(p);
731 *rvp = NULL;
734 static int coff_directives(enum directives directive, char *value, int pass)
736 switch (directive) {
737 case D_EXPORT:
739 char *q, *name;
741 if (pass == 2)
742 return 1; /* ignore in pass two */
743 name = q = value;
744 while (*q && !nasm_isspace(*q))
745 q++;
746 if (nasm_isspace(*q)) {
747 *q++ = '\0';
748 while (*q && nasm_isspace(*q))
749 q++;
752 if (!*name) {
753 nasm_error(ERR_NONFATAL, "`export' directive requires export name");
754 return 1;
756 if (*q) {
757 nasm_error(ERR_NONFATAL, "unrecognized export qualifier `%s'", q);
758 return 1;
760 AddExport(name);
761 return 1;
763 case D_SAFESEH:
765 static int sxseg=-1;
766 int i;
768 if (!win32) /* Only applicable for -f win32 */
769 return 0;
771 if (sxseg==-1)
772 { for (i = 0; i < nsects; i++)
773 if (!strcmp(".sxdata",sects[i]->name))
774 break;
775 if (i == nsects)
776 sxseg = coff_make_section(".sxdata",0x200);
777 else
778 sxseg = i;
780 /* pass0 == 2 is the only time when the full set of symbols are
781 guaranteed to be present; it is the final output pass. */
782 if (pass0 == 2) {
783 uint32_t n;
784 saa_rewind(syms);
785 for (n = 0; n < nsyms; n++) {
786 struct Symbol *sym = saa_rstruct(syms);
787 bool equals;
789 /* sym->strpos is biased by 4, because symbol
790 * table is prefixed with table length */
791 if (sym->strpos >=4) {
792 char *name = nasm_malloc(sym->namlen+1);
793 saa_fread(strs, sym->strpos-4, name, sym->namlen);
794 name[sym->namlen] = '\0';
795 equals = !strcmp(value,name);
796 nasm_free(name);
798 else
799 equals = !strcmp(value,sym->name);
801 if (equals) {
802 /* this value arithmetics effectively reflects
803 * initsym in coff_write(): 2 for file, 1 for
804 * .absolute and two per each section */
805 unsigned char value[4],*p=value;
806 WRITELONG(p,n + 2 + 1 + nsects*2);
807 coff_sect_write(sects[sxseg],value,4);
808 sym->type = 0x20;
809 break;
812 if (n == nsyms) {
813 nasm_error(ERR_NONFATAL,
814 "`safeseh' directive requires valid symbol");
817 return 1;
819 default:
820 return 0;
824 static void coff_write(void)
826 int32_t pos, sympos, vsize;
827 int i;
829 /* fill in the .drectve section with -export's */
830 BuildExportTable(&Exports);
832 if (win32) {
833 /* add default value for @feat.00, this allows to 'link /safeseh' */
834 uint32_t n;
836 saa_rewind(syms);
837 for (n = 0; n < nsyms; n++) {
838 struct Symbol *sym = saa_rstruct(syms);
839 if (sym->strpos == -1 && !strcmp("@feat.00",sym->name))
840 break;
842 if (n == nsyms)
843 coff_deflabel("@feat.00",NO_SEG,1,0,NULL);
847 * Work out how big the file will get. Calculate the start of
848 * the `real' symbols at the same time.
850 pos = 0x14 + 0x28 * nsects;
851 initsym = 3; /* two for the file, one absolute */
852 for (i = 0; i < nsects; i++) {
853 if (sects[i]->data) {
854 sects[i]->pos = pos;
855 pos += sects[i]->len;
856 sects[i]->relpos = pos;
857 pos += 10 * sects[i]->nrelocs;
858 } else
859 sects[i]->pos = sects[i]->relpos = 0L;
860 initsym += 2; /* two for each section */
862 sympos = pos;
865 * Output the COFF header.
867 if (win64)
868 fwriteint16_t(0x8664, ofile); /* MACHINE_x86-64 */
869 else
870 fwriteint16_t(0x014C, ofile); /* MACHINE_i386 */
871 fwriteint16_t(nsects, ofile); /* number of sections */
872 fwriteint32_t(time(NULL), ofile); /* time stamp */
873 fwriteint32_t(sympos, ofile);
874 fwriteint32_t(nsyms + initsym, ofile);
875 fwriteint16_t(0, ofile); /* no optional header */
876 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
877 fwriteint16_t((win32 | win64) ? 0 : 0x104, ofile);
880 * Output the section headers.
882 vsize = 0L;
883 for (i = 0; i < nsects; i++) {
884 coff_section_header(sects[i]->name, vsize, sects[i]->len,
885 sects[i]->pos, sects[i]->relpos,
886 sects[i]->nrelocs, sects[i]->flags);
887 vsize += sects[i]->len;
891 * Output the sections and their relocations.
893 for (i = 0; i < nsects; i++)
894 if (sects[i]->data) {
895 saa_fpwrite(sects[i]->data, ofile);
896 coff_write_relocs(sects[i]);
900 * Output the symbol and string tables.
902 coff_write_symbols();
903 fwriteint32_t(strslen + 4, ofile); /* length includes length count */
904 saa_fpwrite(strs, ofile);
907 static void coff_section_header(char *name, int32_t vsize,
908 int32_t datalen, int32_t datapos,
909 int32_t relpos, int nrelocs, int32_t flags)
911 char padname[8];
913 (void)vsize;
915 memset(padname, 0, 8);
916 strncpy(padname, name, 8);
917 fwrite(padname, 8, 1, ofile);
918 fwriteint32_t(0, ofile); /* Virtual size field - set to 0 or vsize */
919 fwriteint32_t(0L, ofile); /* RVA/offset - we ignore */
920 fwriteint32_t(datalen, ofile);
921 fwriteint32_t(datapos, ofile);
922 fwriteint32_t(relpos, ofile);
923 fwriteint32_t(0L, ofile); /* no line numbers - we don't do 'em */
924 fwriteint16_t(nrelocs, ofile);
925 fwriteint16_t(0, ofile); /* again, no line numbers */
926 fwriteint32_t(flags, ofile);
929 static void coff_write_relocs(struct Section *s)
931 struct Reloc *r;
933 for (r = s->head; r; r = r->next) {
934 fwriteint32_t(r->address, ofile);
935 fwriteint32_t(r->symbol + (r->symbase == REAL_SYMBOLS ? initsym :
936 r->symbase == ABS_SYMBOL ? initsym - 1 :
937 r->symbase == SECT_SYMBOLS ? 2 : 0),
938 ofile);
939 fwriteint16_t(r->type, ofile);
943 static void coff_symbol(char *name, int32_t strpos, int32_t value,
944 int section, int type, int storageclass, int aux)
946 char padname[8];
948 if (name) {
949 memset(padname, 0, 8);
950 strncpy(padname, name, 8);
951 fwrite(padname, 8, 1, ofile);
952 } else {
953 fwriteint32_t(0, ofile);
954 fwriteint32_t(strpos, ofile);
956 fwriteint32_t(value, ofile);
957 fwriteint16_t(section, ofile);
958 fwriteint16_t(type, ofile);
959 fputc(storageclass, ofile);
960 fputc(aux, ofile);
963 static void coff_write_symbols(void)
965 char filename[18];
966 uint32_t i;
969 * The `.file' record, and the file name auxiliary record.
971 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
972 memset(filename, 0, 18);
973 strncpy(filename, coff_infile, 18);
974 fwrite(filename, 18, 1, ofile);
977 * The section records, with their auxiliaries.
979 memset(filename, 0, 18); /* useful zeroed buffer */
981 for (i = 0; i < (uint32_t) nsects; i++) {
982 coff_symbol(sects[i]->name, 0L, 0L, i + 1, 0, 3, 1);
983 fwriteint32_t(sects[i]->len, ofile);
984 fwriteint16_t(sects[i]->nrelocs, ofile);
985 fwrite(filename, 12, 1, ofile);
989 * The absolute symbol, for relative-to-absolute relocations.
991 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
994 * The real symbols.
996 saa_rewind(syms);
997 for (i = 0; i < nsyms; i++) {
998 struct Symbol *sym = saa_rstruct(syms);
999 coff_symbol(sym->strpos == -1 ? sym->name : NULL,
1000 sym->strpos, sym->value, sym->section,
1001 sym->type, sym->is_global ? 2 : 3, 0);
1005 static void coff_sectalign(int32_t seg, unsigned int value)
1007 struct Section *s = NULL;
1008 uint32_t align;
1009 int i;
1011 for (i = 0; i < nsects; i++) {
1012 if (sects[i]->index == seg) {
1013 s = sects[i];
1014 break;
1018 if (!s || !is_power2(value) || value > 64)
1019 return;
1021 align = (s->flags & 0x00F00000L);
1022 value = (value == 1 ? 0x00100000L :
1023 value == 2 ? 0x00200000L :
1024 value == 4 ? 0x00300000L :
1025 value == 8 ? 0x00400000L :
1026 value == 16 ? 0x00500000L :
1027 value == 32 ? 0x00600000L : 0x00700000L);
1029 if (value > align)
1030 s->flags = (s->flags & ~0x00F00000L) | value;
1033 static int32_t coff_segbase(int32_t segment)
1035 return segment;
1038 static void coff_std_filename(char *inname, char *outname)
1040 strcpy(coff_infile, inname);
1041 standard_extension(inname, outname, ".o");
1044 static void coff_win32_filename(char *inname, char *outname)
1046 strcpy(coff_infile, inname);
1047 standard_extension(inname, outname, ".obj");
1050 extern macros_t coff_stdmac[];
1052 static int coff_set_info(enum geninfo type, char **val)
1054 (void)type;
1055 (void)val;
1056 return 0;
1058 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
1060 #ifdef OF_COFF
1062 struct ofmt of_coff = {
1063 "COFF (i386) object files (e.g. DJGPP for DOS)",
1064 "coff",
1066 null_debug_arr,
1067 &null_debug_form,
1068 coff_stdmac,
1069 coff_std_init,
1070 coff_set_info,
1071 coff_out,
1072 coff_deflabel,
1073 coff_section_names,
1074 coff_sectalign,
1075 coff_segbase,
1076 coff_directives,
1077 coff_std_filename,
1078 coff_cleanup
1081 #endif
1083 #ifdef OF_WIN32
1085 struct ofmt of_win32 = {
1086 "Microsoft Win32 (i386) object files",
1087 "win32",
1089 null_debug_arr,
1090 &null_debug_form,
1091 coff_stdmac,
1092 coff_win32_init,
1093 coff_set_info,
1094 coff_out,
1095 coff_deflabel,
1096 coff_section_names,
1097 coff_sectalign,
1098 coff_segbase,
1099 coff_directives,
1100 coff_win32_filename,
1101 coff_cleanup
1104 #endif
1106 #ifdef OF_WIN64
1108 struct ofmt of_win64 = {
1109 "Microsoft Win64 (x86-64) object files",
1110 "win64",
1112 null_debug_arr,
1113 &null_debug_form,
1114 coff_stdmac,
1115 coff_win64_init,
1116 coff_set_info,
1117 coff_out,
1118 coff_deflabel,
1119 coff_section_names,
1120 coff_sectalign,
1121 coff_segbase,
1122 coff_directives,
1123 coff_win32_filename,
1124 coff_cleanup
1127 #endif