Add copyright notice to insns.dat
[nasm.git] / output / outcoff.c
blobb8a9c7d4cd43810a5164abb6d91f7d3e198093de
1 /* outcoff.c output routines for the Netwide Assembler to produce
2 * COFF object files (for DJGPP and Win32)
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
8 */
10 #include "compiler.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <time.h>
17 #include <inttypes.h>
19 #include "nasm.h"
20 #include "nasmlib.h"
21 #include "saa.h"
22 #include "raa.h"
23 #include "output/outform.h"
24 #include "output/outlib.h"
26 #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
29 * Notes on COFF:
31 * (0) When I say `standard COFF' below, I mean `COFF as output and
32 * used by DJGPP'. I assume DJGPP gets it right.
34 * (1) Win32 appears to interpret the term `relative relocation'
35 * differently from standard COFF. Standard COFF understands a
36 * relative relocation to mean that during relocation you add the
37 * address of the symbol you're referencing, and subtract the base
38 * address of the section you're in. Win32 COFF, by contrast, seems
39 * to add the address of the symbol and then subtract the address
40 * of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
41 * subtly incompatible.
43 * (2) Win32 doesn't bother putting any flags in the header flags
44 * field (at offset 0x12 into the file).
46 * (3) Win32 uses some extra flags into the section header table:
47 * it defines flags 0x80000000 (writable), 0x40000000 (readable)
48 * and 0x20000000 (executable), and uses them in the expected
49 * combinations. It also defines 0x00100000 through 0x00700000 for
50 * section alignments of 1 through 64 bytes.
52 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
53 * field directly after the section name in the section header
54 * table for something strange: they store what the address of the
55 * section start point _would_ be, if you laid all the sections end
56 * to end starting at zero. Dunno why. Microsoft's documentation
57 * lists this field as "Virtual Size of Section", which doesn't
58 * seem to fit at all. In fact, Win32 even includes non-linked
59 * sections such as .drectve in this calculation.
61 * Newer versions of MASM seem to have changed this to be zero, and
62 * that apparently matches the COFF spec, so go with that.
64 * (5) Standard COFF does something very strange to common
65 * variables: the relocation point for a common variable is as far
66 * _before_ the variable as its size stretches out _after_ it. So
67 * we must fix up common variable references. Win32 seems to be
68 * sensible on this one.
71 /* Flag which version of COFF we are currently outputting. */
72 static bool win32, win64;
74 static int32_t imagebase_sect;
75 #define WRT_IMAGEBASE "..imagebase"
77 struct Reloc {
78 struct Reloc *next;
79 int32_t address; /* relative to _start_ of section */
80 int32_t symbol; /* symbol number */
81 enum {
82 SECT_SYMBOLS,
83 ABS_SYMBOL,
84 REAL_SYMBOLS
85 } symbase; /* relocation for symbol number :) */
86 int16_t type;
89 /* possible values for Reloc->type */
90 #define IMAGE_REL_AMD64_ADDR64 0x0001
91 #define IMAGE_REL_AMD64_ADDR32 0x0002
92 #define IMAGE_REL_AMD64_ADDR32NB 0x0003
93 #define IMAGE_REL_AMD64_REL32 0x0004
94 #define IMAGE_REL_I386_DIR32 0x0006
95 #define IMAGE_REL_I386_DIR32NB 0x0007
96 #define IMAGE_REL_I386_REL32 0x0014
98 struct Symbol {
99 char name[9];
100 int32_t strpos; /* string table position of name */
101 int32_t value; /* address, or COMMON variable size */
102 int section; /* section number where it's defined
103 * - in COFF codes, not NASM codes */
104 bool is_global; /* is it a global symbol or not? */
105 int16_t type; /* 0 - notype, 0x20 - function */
106 int32_t namlen; /* full name length */
109 static FILE *coffp;
110 static efunc error;
111 static char coff_infile[FILENAME_MAX];
113 struct Section {
114 struct SAA *data;
115 uint32_t len;
116 int nrelocs;
117 int32_t index;
118 struct Reloc *head, **tail;
119 uint32_t flags; /* section flags */
120 char name[9];
121 int32_t pos, relpos;
124 #define TEXT_FLAGS ((win32 | win64) ? 0x60500020L : 0x20L)
125 #define DATA_FLAGS ((win32 | win64) ? 0xC0300040L : 0x40L)
126 #define BSS_FLAGS ((win32 | win64) ? 0xC0300080L : 0x80L)
127 #define INFO_FLAGS 0x00100A00L
128 #define RDATA_FLAGS ((win32 | win64) ? 0x40400040L : 0x40L)
130 #define SECT_DELTA 32
131 static struct Section **sects;
132 static int nsects, sectlen;
134 static struct SAA *syms;
135 static uint32_t nsyms;
137 static int32_t def_seg;
139 static int initsym;
141 static struct RAA *bsym, *symval;
143 static struct SAA *strs;
144 static uint32_t strslen;
146 static void coff_gen_init(FILE *, efunc);
147 static void coff_sect_write(struct Section *, const uint8_t *,
148 uint32_t);
149 static void coff_write(void);
150 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
151 static void coff_write_relocs(struct Section *);
152 static void coff_write_symbols(void);
154 static void coff_win32_init(FILE * fp, efunc errfunc,
155 ldfunc ldef, evalfunc eval)
157 win32 = true; win64 = false;
158 (void)ldef; /* placate optimizers */
159 (void)eval;
160 coff_gen_init(fp, errfunc);
163 static void coff_win64_init(FILE * fp, efunc errfunc,
164 ldfunc ldef, evalfunc eval)
166 extern struct ofmt of_win64;
168 maxbits = 64;
169 win32 = false; win64 = true;
170 (void)ldef; /* placate optimizers */
171 (void)eval;
172 coff_gen_init(fp, errfunc);
173 imagebase_sect = seg_alloc()+1;
174 ldef(WRT_IMAGEBASE,imagebase_sect,0,NULL,false,false,&of_win64,errfunc);
177 static void coff_std_init(FILE * fp, efunc errfunc, ldfunc ldef,
178 evalfunc eval)
180 win32 = win64 = false;
181 (void)ldef; /* placate optimizers */
182 (void)eval;
183 coff_gen_init(fp, errfunc);
186 static void coff_gen_init(FILE * fp, efunc errfunc)
189 coffp = fp;
190 error = errfunc;
191 sects = NULL;
192 nsects = sectlen = 0;
193 syms = saa_init(sizeof(struct Symbol));
194 nsyms = 0;
195 bsym = raa_init();
196 symval = raa_init();
197 strs = saa_init(1);
198 strslen = 0;
199 def_seg = seg_alloc();
202 static void coff_cleanup(int debuginfo)
204 struct Reloc *r;
205 int i;
207 (void)debuginfo;
209 coff_write();
210 fclose(coffp);
211 for (i = 0; i < nsects; i++) {
212 if (sects[i]->data)
213 saa_free(sects[i]->data);
214 while (sects[i]->head) {
215 r = sects[i]->head;
216 sects[i]->head = sects[i]->head->next;
217 nasm_free(r);
219 nasm_free(sects[i]);
221 nasm_free(sects);
222 saa_free(syms);
223 raa_free(bsym);
224 raa_free(symval);
225 saa_free(strs);
228 static int coff_make_section(char *name, uint32_t flags)
230 struct Section *s;
232 s = nasm_malloc(sizeof(*s));
234 if (flags != BSS_FLAGS)
235 s->data = saa_init(1);
236 else
237 s->data = NULL;
238 s->head = NULL;
239 s->tail = &s->head;
240 s->len = 0;
241 s->nrelocs = 0;
242 if (!strcmp(name, ".text"))
243 s->index = def_seg;
244 else
245 s->index = seg_alloc();
246 strncpy(s->name, name, 8);
247 s->name[8] = '\0';
248 s->flags = flags;
250 if (nsects >= sectlen) {
251 sectlen += SECT_DELTA;
252 sects = nasm_realloc(sects, sectlen * sizeof(*sects));
254 sects[nsects++] = s;
256 return nsects - 1;
259 static int32_t coff_section_names(char *name, int pass, int *bits)
261 char *p;
262 uint32_t flags, align_and = ~0L, align_or = 0L;
263 int i;
266 * Set default bits.
268 if (!name) {
269 if(win64)
270 *bits = 64;
271 else
272 *bits = 32;
275 if (!name)
276 return def_seg;
278 p = name;
279 while (*p && !nasm_isspace(*p))
280 p++;
281 if (*p)
282 *p++ = '\0';
283 if (strlen(name) > 8) {
284 error(ERR_WARNING, "COFF section names limited to 8 characters:"
285 " truncating");
286 name[8] = '\0';
288 flags = 0;
290 while (*p && nasm_isspace(*p))
291 p++;
292 while (*p) {
293 char *q = p;
294 while (*p && !nasm_isspace(*p))
295 p++;
296 if (*p)
297 *p++ = '\0';
298 while (*p && nasm_isspace(*p))
299 p++;
301 if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
302 flags = TEXT_FLAGS;
303 } else if (!nasm_stricmp(q, "data")) {
304 flags = DATA_FLAGS;
305 } else if (!nasm_stricmp(q, "rdata")) {
306 if (win32 | win64)
307 flags = RDATA_FLAGS;
308 else {
309 flags = DATA_FLAGS; /* gotta do something */
310 error(ERR_NONFATAL, "standard COFF does not support"
311 " read-only data sections");
313 } else if (!nasm_stricmp(q, "bss")) {
314 flags = BSS_FLAGS;
315 } else if (!nasm_stricmp(q, "info")) {
316 if (win32 | win64)
317 flags = INFO_FLAGS;
318 else {
319 flags = DATA_FLAGS; /* gotta do something */
320 error(ERR_NONFATAL, "standard COFF does not support"
321 " informational sections");
323 } else if (!nasm_strnicmp(q, "align=", 6)) {
324 if (!(win32 | win64))
325 error(ERR_NONFATAL, "standard COFF does not support"
326 " section alignment specification");
327 else {
328 if (q[6 + strspn(q + 6, "0123456789")])
329 error(ERR_NONFATAL,
330 "argument to `align' is not numeric");
331 else {
332 unsigned int align = atoi(q + 6);
333 if (!align || ((align - 1) & align))
334 error(ERR_NONFATAL, "argument to `align' is not a"
335 " power of two");
336 else if (align > 64)
337 error(ERR_NONFATAL, "Win32 cannot align sections"
338 " to better than 64-byte boundaries");
339 else {
340 align_and = ~0x00F00000L;
341 align_or = (align == 1 ? 0x00100000L :
342 align == 2 ? 0x00200000L :
343 align == 4 ? 0x00300000L :
344 align == 8 ? 0x00400000L :
345 align == 16 ? 0x00500000L :
346 align ==
347 32 ? 0x00600000L : 0x00700000L);
354 for (i = 0; i < nsects; i++)
355 if (!strcmp(name, sects[i]->name))
356 break;
357 if (i == nsects) {
358 if (!flags) {
359 if (!strcmp(name, ".data"))
360 flags = DATA_FLAGS;
361 else if (!strcmp(name, ".rdata"))
362 flags = RDATA_FLAGS;
363 else if (!strcmp(name, ".bss"))
364 flags = BSS_FLAGS;
365 else if (win64 && !strcmp(name, ".pdata"))
366 flags = 0x40300040; /* rdata align=4 */
367 else if (win64 && !strcmp(name, ".xdata"))
368 flags = 0x40400040; /* rdate align=8 */
369 else
370 flags = TEXT_FLAGS;
372 i = coff_make_section(name, flags);
373 if (flags)
374 sects[i]->flags = flags;
375 sects[i]->flags &= align_and;
376 sects[i]->flags |= align_or;
377 } else if (pass == 1) {
378 if (flags)
379 error(ERR_WARNING, "section attributes ignored on"
380 " redeclaration of section `%s'", name);
383 return sects[i]->index;
386 static void coff_deflabel(char *name, int32_t segment, int64_t offset,
387 int is_global, char *special)
389 int pos = strslen + 4;
390 struct Symbol *sym;
392 if (special)
393 error(ERR_NONFATAL, "binary format does not support any"
394 " special symbol types");
396 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
397 if (strcmp(name,WRT_IMAGEBASE))
398 error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
399 return;
402 if (strlen(name) > 8) {
403 size_t nlen = strlen(name)+1;
404 saa_wbytes(strs, name, nlen);
405 strslen += nlen;
406 } else
407 pos = -1;
409 sym = saa_wstruct(syms);
411 sym->strpos = pos;
412 sym->namlen = strlen(name);
413 if (pos == -1)
414 strcpy(sym->name, name);
415 sym->is_global = !!is_global;
416 sym->type = 0; /* Default to T_NULL (no type) */
417 if (segment == NO_SEG)
418 sym->section = -1; /* absolute symbol */
419 else {
420 int i;
421 sym->section = 0;
422 for (i = 0; i < nsects; i++)
423 if (segment == sects[i]->index) {
424 sym->section = i + 1;
425 break;
427 if (!sym->section)
428 sym->is_global = true;
430 if (is_global == 2)
431 sym->value = offset;
432 else
433 sym->value = (sym->section == 0 ? 0 : offset);
436 * define the references from external-symbol segment numbers
437 * to these symbol records.
439 if (sym->section == 0) {
440 bsym = raa_write(bsym, segment, nsyms);
443 if (segment != NO_SEG)
444 symval = raa_write(symval, segment, sym->section ? 0 : sym->value);
446 nsyms++;
449 static int32_t coff_add_reloc(struct Section *sect, int32_t segment,
450 int16_t type)
452 struct Reloc *r;
454 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
455 sect->tail = &r->next;
456 r->next = NULL;
458 r->address = sect->len;
459 if (segment == NO_SEG)
460 r->symbol = 0, r->symbase = ABS_SYMBOL;
461 else {
462 int i;
463 r->symbase = REAL_SYMBOLS;
464 for (i = 0; i < nsects; i++)
465 if (segment == sects[i]->index) {
466 r->symbol = i * 2;
467 r->symbase = SECT_SYMBOLS;
468 break;
470 if (r->symbase == REAL_SYMBOLS)
471 r->symbol = raa_read(bsym, segment);
473 r->type = type;
475 sect->nrelocs++;
478 * Return the fixup for standard COFF common variables.
480 if (r->symbase == REAL_SYMBOLS && !(win32 | win64))
481 return raa_read(symval, segment);
482 else
483 return 0;
486 static void coff_out(int32_t segto, const void *data,
487 enum out_type type, uint64_t size,
488 int32_t segment, int32_t wrt)
490 struct Section *s;
491 uint8_t mydata[8], *p;
492 int i;
494 if (wrt != NO_SEG && !win64) {
495 wrt = NO_SEG; /* continue to do _something_ */
496 error(ERR_NONFATAL, "WRT not supported by COFF output formats");
500 * handle absolute-assembly (structure definitions)
502 if (segto == NO_SEG) {
503 if (type != OUT_RESERVE)
504 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
505 " space");
506 return;
509 s = NULL;
510 for (i = 0; i < nsects; i++)
511 if (segto == sects[i]->index) {
512 s = sects[i];
513 break;
515 if (!s) {
516 int tempint; /* ignored */
517 if (segto != coff_section_names(".text", 2, &tempint))
518 error(ERR_PANIC, "strange segment conditions in COFF driver");
519 else
520 s = sects[nsects - 1];
523 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
524 if (win64 && wrt == NO_SEG &&
525 (!strcmp(s->name,".pdata") || !strcmp(s->name,".xdata")))
526 wrt = imagebase_sect;
528 if (!s->data && type != OUT_RESERVE) {
529 error(ERR_WARNING, "attempt to initialize memory in"
530 " BSS section `%s': ignored", s->name);
531 s->len += realsize(type, size);
532 return;
535 if (type == OUT_RESERVE) {
536 if (s->data) {
537 error(ERR_WARNING, "uninitialised space declared in"
538 " non-BSS section `%s': zeroing", s->name);
539 coff_sect_write(s, NULL, size);
540 } else
541 s->len += size;
542 } else if (type == OUT_RAWDATA) {
543 if (segment != NO_SEG)
544 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
545 coff_sect_write(s, data, size);
546 } else if (type == OUT_ADDRESS) {
547 if (!(win64)) {
548 if (size != 4 && (segment != NO_SEG || wrt != NO_SEG))
549 error(ERR_NONFATAL, "COFF format does not support non-32-bit"
550 " relocations");
551 else {
552 int32_t fix = 0;
553 if (segment != NO_SEG || wrt != NO_SEG) {
554 if (wrt != NO_SEG) {
555 error(ERR_NONFATAL, "COFF format does not support"
556 " WRT types");
557 } else if (segment % 2) {
558 error(ERR_NONFATAL, "COFF format does not support"
559 " segment base references");
560 } else
561 fix = coff_add_reloc(s, segment, IMAGE_REL_I386_DIR32);
563 p = mydata;
564 WRITELONG(p, *(int64_t *)data + fix);
565 coff_sect_write(s, mydata, size);
567 } else {
568 int32_t fix = 0;
569 p = mydata;
570 if (size == 8) {
571 if (wrt == imagebase_sect) {
572 error(ERR_NONFATAL, "operand size mismatch: 'wrt "
573 WRT_IMAGEBASE "' is a 32-bit operand");
575 fix = coff_add_reloc(s, segment, IMAGE_REL_AMD64_ADDR64);
576 WRITEDLONG(p, *(int64_t *)data + fix);
577 coff_sect_write(s, mydata, size);
578 } else {
579 fix = coff_add_reloc(s, segment,
580 wrt == imagebase_sect ? IMAGE_REL_AMD64_ADDR32NB:
581 IMAGE_REL_AMD64_ADDR32);
582 WRITELONG(p, *(int64_t *)data + fix);
583 coff_sect_write(s, mydata, size);
586 } else if (type == OUT_REL2ADR) {
587 error(ERR_NONFATAL, "COFF format does not support 16-bit"
588 " relocations");
589 } else if (type == OUT_REL4ADR) {
590 if (segment == segto && !(win64)) /* Acceptable for RIP-relative */
591 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
592 else if (segment == NO_SEG && win32)
593 error(ERR_NONFATAL, "Win32 COFF does not correctly support"
594 " relative references to absolute addresses");
595 else {
596 int32_t fix = 0;
597 if (segment != NO_SEG && segment % 2) {
598 error(ERR_NONFATAL, "COFF format does not support"
599 " segment base references");
600 } else
601 fix = coff_add_reloc(s, segment,
602 win64 ? IMAGE_REL_AMD64_REL32 : IMAGE_REL_I386_REL32);
603 p = mydata;
604 if (win32 | win64) {
605 WRITELONG(p, *(int64_t *)data + 4 - size + fix);
606 } else {
607 WRITELONG(p, *(int64_t *)data - (size + s->len) + fix);
609 coff_sect_write(s, mydata, 4L);
615 static void coff_sect_write(struct Section *sect,
616 const uint8_t *data, uint32_t len)
618 saa_wbytes(sect->data, data, len);
619 sect->len += len;
622 typedef struct tagString {
623 struct tagString *Next;
624 int len;
625 char *String;
626 } STRING;
628 #define EXPORT_SECTION_NAME ".drectve"
629 #define EXPORT_SECTION_FLAGS INFO_FLAGS
631 #define EXPORT_SECTION_NAME ".text"
632 #define EXPORT_SECTION_FLAGS TEXT_FLAGS
635 static STRING *Exports = NULL;
636 static struct Section *directive_sec;
637 void AddExport(char *name)
639 STRING *rvp = Exports, *newS;
641 newS = (STRING *) nasm_malloc(sizeof(STRING));
642 newS->len = strlen(name);
643 newS->Next = NULL;
644 newS->String = (char *)nasm_malloc(newS->len + 1);
645 strcpy(newS->String, name);
646 if (rvp == NULL) {
647 int i;
648 for (i = 0; i < nsects; i++)
650 if (!strcmp(EXPORT_SECTION_NAME, sects[i]->name))
651 break;
652 if (i == nsects)
653 directive_sec =
654 sects[coff_make_section
655 (EXPORT_SECTION_NAME, EXPORT_SECTION_FLAGS)];
656 else
657 directive_sec = sects[i];
658 Exports = newS;
659 } else {
660 while (rvp->Next) {
661 if (!strcmp(rvp->String, name))
662 return;
663 rvp = rvp->Next;
665 rvp->Next = newS;
669 void BuildExportTable(void)
671 STRING *rvp = Exports, *next;
672 uint8_t buf[256];
673 int len;
674 if (rvp == NULL)
675 return;
676 while (rvp) {
677 len = sprintf((char *)buf, "-export:%s ", rvp->String);
678 coff_sect_write(directive_sec, buf, len);
679 rvp = rvp->Next;
682 next = Exports;
683 while ((rvp = next)) {
684 next = rvp->Next;
685 nasm_free(rvp->String);
686 nasm_free(rvp);
688 Exports = NULL;
691 static int coff_directives(char *directive, char *value, int pass)
693 if (!strcmp(directive, "export")) {
694 char *q, *name;
696 if (pass == 2)
697 return 1; /* ignore in pass two */
698 name = q = value;
699 while (*q && !nasm_isspace(*q))
700 q++;
701 if (nasm_isspace(*q)) {
702 *q++ = '\0';
703 while (*q && nasm_isspace(*q))
704 q++;
707 if (!*name) {
708 error(ERR_NONFATAL, "`export' directive requires export name");
709 return 1;
711 if (*q) {
712 error(ERR_NONFATAL, "unrecognized export qualifier `%s'", q);
713 return 1;
715 AddExport(name);
716 return 1;
717 } else if (win32 && !strcmp(directive,"safeseh")) {
718 static int sxseg=-1;
719 int i;
720 if (sxseg==-1)
721 { for (i = 0; i < nsects; i++)
722 if (!strcmp(".sxdata",sects[i]->name))
723 break;
724 if (i == nsects)
725 sxseg = coff_make_section(".sxdata",0x200);
726 else
727 sxseg = i;
729 /* pass0 == 2 is the only time when the full set of symbols are
730 guaranteed to be present; it is the final output pass. */
731 if (pass0 == 2) {
732 uint32_t n;
733 saa_rewind(syms);
734 for (n = 0; n < nsyms; n++) {
735 struct Symbol *sym = saa_rstruct(syms);
736 bool equals;
738 /* sym->strpos is biased by 4, because symbol
739 * table is prefixed with table length */
740 if (sym->strpos >=4) {
741 char *name = nasm_malloc(sym->namlen+1);
742 saa_fread(strs, sym->strpos-4, name, sym->namlen);
743 name[sym->namlen] = '\0';
744 equals = !strcmp(value,name);
745 nasm_free(name);
747 else
748 equals = !strcmp(value,sym->name);
750 if (equals) {
751 /* this value arithmetics effectively reflects
752 * initsym in coff_write(): 2 for file, 1 for
753 * .absolute and two per each section */
754 unsigned char value[4],*p=value;
755 WRITELONG(p,n + 2 + 1 + nsects*2);
756 coff_sect_write(sects[sxseg],value,4);
757 sym->type = 0x20;
758 break;
761 if (n == nsyms) {
762 error(ERR_NONFATAL,
763 "`safeseh' directive requires valid symbol");
766 return 1;
768 return 0;
771 static void coff_write(void)
773 int32_t pos, sympos, vsize;
774 int i;
776 BuildExportTable(); /* fill in the .drectve section with -export's */
778 if (win32) {
779 /* add default value for @feat.00, this allows to 'link /safeseh' */
780 uint32_t n;
782 saa_rewind(syms);
783 for (n = 0; n < nsyms; n++) {
784 struct Symbol *sym = saa_rstruct(syms);
785 if (sym->strpos == -1 && !strcmp("@feat.00",sym->name))
786 break;
788 if (n == nsyms)
789 coff_deflabel("@feat.00",NO_SEG,1,0,NULL);
793 * Work out how big the file will get. Calculate the start of
794 * the `real' symbols at the same time.
796 pos = 0x14 + 0x28 * nsects;
797 initsym = 3; /* two for the file, one absolute */
798 for (i = 0; i < nsects; i++) {
799 if (sects[i]->data) {
800 sects[i]->pos = pos;
801 pos += sects[i]->len;
802 sects[i]->relpos = pos;
803 pos += 10 * sects[i]->nrelocs;
804 } else
805 sects[i]->pos = sects[i]->relpos = 0L;
806 initsym += 2; /* two for each section */
808 sympos = pos;
811 * Output the COFF header.
813 if (win64)
814 fwriteint16_t(0x8664, coffp); /* MACHINE_x86-64 */
815 else
816 fwriteint16_t(0x014C, coffp); /* MACHINE_i386 */
817 fwriteint16_t(nsects, coffp); /* number of sections */
818 fwriteint32_t(time(NULL), coffp); /* time stamp */
819 fwriteint32_t(sympos, coffp);
820 fwriteint32_t(nsyms + initsym, coffp);
821 fwriteint16_t(0, coffp); /* no optional header */
822 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
823 fwriteint16_t((win32 | win64) ? 0 : 0x104, coffp);
826 * Output the section headers.
828 vsize = 0L;
829 for (i = 0; i < nsects; i++) {
830 coff_section_header(sects[i]->name, vsize, sects[i]->len,
831 sects[i]->pos, sects[i]->relpos,
832 sects[i]->nrelocs, sects[i]->flags);
833 vsize += sects[i]->len;
837 * Output the sections and their relocations.
839 for (i = 0; i < nsects; i++)
840 if (sects[i]->data) {
841 saa_fpwrite(sects[i]->data, coffp);
842 coff_write_relocs(sects[i]);
846 * Output the symbol and string tables.
848 coff_write_symbols();
849 fwriteint32_t(strslen + 4, coffp); /* length includes length count */
850 saa_fpwrite(strs, coffp);
853 static void coff_section_header(char *name, int32_t vsize,
854 int32_t datalen, int32_t datapos,
855 int32_t relpos, int nrelocs, int32_t flags)
857 char padname[8];
859 (void)vsize;
861 memset(padname, 0, 8);
862 strncpy(padname, name, 8);
863 fwrite(padname, 8, 1, coffp);
864 fwriteint32_t(0, coffp); /* Virtual size field - set to 0 or vsize */
865 fwriteint32_t(0L, coffp); /* RVA/offset - we ignore */
866 fwriteint32_t(datalen, coffp);
867 fwriteint32_t(datapos, coffp);
868 fwriteint32_t(relpos, coffp);
869 fwriteint32_t(0L, coffp); /* no line numbers - we don't do 'em */
870 fwriteint16_t(nrelocs, coffp);
871 fwriteint16_t(0, coffp); /* again, no line numbers */
872 fwriteint32_t(flags, coffp);
875 static void coff_write_relocs(struct Section *s)
877 struct Reloc *r;
879 for (r = s->head; r; r = r->next) {
880 fwriteint32_t(r->address, coffp);
881 fwriteint32_t(r->symbol + (r->symbase == REAL_SYMBOLS ? initsym :
882 r->symbase == ABS_SYMBOL ? initsym - 1 :
883 r->symbase == SECT_SYMBOLS ? 2 : 0),
884 coffp);
885 fwriteint16_t(r->type, coffp);
889 static void coff_symbol(char *name, int32_t strpos, int32_t value,
890 int section, int type, int storageclass, int aux)
892 char padname[8];
894 if (name) {
895 memset(padname, 0, 8);
896 strncpy(padname, name, 8);
897 fwrite(padname, 8, 1, coffp);
898 } else {
899 fwriteint32_t(0, coffp);
900 fwriteint32_t(strpos, coffp);
902 fwriteint32_t(value, coffp);
903 fwriteint16_t(section, coffp);
904 fwriteint16_t(type, coffp);
905 fputc(storageclass, coffp);
906 fputc(aux, coffp);
909 static void coff_write_symbols(void)
911 char filename[18];
912 uint32_t i;
915 * The `.file' record, and the file name auxiliary record.
917 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
918 memset(filename, 0, 18);
919 strncpy(filename, coff_infile, 18);
920 fwrite(filename, 18, 1, coffp);
923 * The section records, with their auxiliaries.
925 memset(filename, 0, 18); /* useful zeroed buffer */
927 for (i = 0; i < (uint32_t) nsects; i++) {
928 coff_symbol(sects[i]->name, 0L, 0L, i + 1, 0, 3, 1);
929 fwriteint32_t(sects[i]->len, coffp);
930 fwriteint16_t(sects[i]->nrelocs, coffp);
931 fwrite(filename, 12, 1, coffp);
935 * The absolute symbol, for relative-to-absolute relocations.
937 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
940 * The real symbols.
942 saa_rewind(syms);
943 for (i = 0; i < nsyms; i++) {
944 struct Symbol *sym = saa_rstruct(syms);
945 coff_symbol(sym->strpos == -1 ? sym->name : NULL,
946 sym->strpos, sym->value, sym->section,
947 sym->type, sym->is_global ? 2 : 3, 0);
951 static int32_t coff_segbase(int32_t segment)
953 return segment;
956 static void coff_std_filename(char *inname, char *outname, efunc error)
958 strcpy(coff_infile, inname);
959 standard_extension(inname, outname, ".o", error);
962 static void coff_win32_filename(char *inname, char *outname, efunc error)
964 strcpy(coff_infile, inname);
965 standard_extension(inname, outname, ".obj", error);
968 extern macros_t coff_stdmac[];
970 static int coff_set_info(enum geninfo type, char **val)
972 (void)type;
973 (void)val;
974 return 0;
976 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
978 #ifdef OF_COFF
980 struct ofmt of_coff = {
981 "COFF (i386) object files (e.g. DJGPP for DOS)",
982 "coff",
983 NULL,
984 null_debug_arr,
985 &null_debug_form,
986 coff_stdmac,
987 coff_std_init,
988 coff_set_info,
989 coff_out,
990 coff_deflabel,
991 coff_section_names,
992 coff_segbase,
993 coff_directives,
994 coff_std_filename,
995 coff_cleanup
998 #endif
1000 #ifdef OF_WIN32
1002 struct ofmt of_win32 = {
1003 "Microsoft Win32 (i386) object files",
1004 "win32",
1005 NULL,
1006 null_debug_arr,
1007 &null_debug_form,
1008 coff_stdmac,
1009 coff_win32_init,
1010 coff_set_info,
1011 coff_out,
1012 coff_deflabel,
1013 coff_section_names,
1014 coff_segbase,
1015 coff_directives,
1016 coff_win32_filename,
1017 coff_cleanup
1020 #endif
1022 #ifdef OF_WIN64
1024 struct ofmt of_win64 = {
1025 "Microsoft Win64 (x86-64) object files",
1026 "win64",
1027 NULL,
1028 null_debug_arr,
1029 &null_debug_form,
1030 coff_stdmac,
1031 coff_win64_init,
1032 coff_set_info,
1033 coff_out,
1034 coff_deflabel,
1035 coff_section_names,
1036 coff_segbase,
1037 coff_directives,
1038 coff_win32_filename,
1039 coff_cleanup
1042 #endif