outcoff: default output symbols to T_NULL
[nasm.git] / output / outcoff.c
blob308dc77461ab5798c115fef1164a0cf08f5f6b72
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 "outform.h"
25 #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64)
28 * Notes on COFF:
30 * (0) When I say `standard COFF' below, I mean `COFF as output and
31 * used by DJGPP'. I assume DJGPP gets it right.
33 * (1) Win32 appears to interpret the term `relative relocation'
34 * differently from standard COFF. Standard COFF understands a
35 * relative relocation to mean that during relocation you add the
36 * address of the symbol you're referencing, and subtract the base
37 * address of the section you're in. Win32 COFF, by contrast, seems
38 * to add the address of the symbol and then subtract the address
39 * of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
40 * subtly incompatible.
42 * (2) Win32 doesn't bother putting any flags in the header flags
43 * field (at offset 0x12 into the file).
45 * (3) Win32 uses some extra flags into the section header table:
46 * it defines flags 0x80000000 (writable), 0x40000000 (readable)
47 * and 0x20000000 (executable), and uses them in the expected
48 * combinations. It also defines 0x00100000 through 0x00700000 for
49 * section alignments of 1 through 64 bytes.
51 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
52 * field directly after the section name in the section header
53 * table for something strange: they store what the address of the
54 * section start point _would_ be, if you laid all the sections end
55 * to end starting at zero. Dunno why. Microsoft's documentation
56 * lists this field as "Virtual Size of Section", which doesn't
57 * seem to fit at all. In fact, Win32 even includes non-linked
58 * sections such as .drectve in this calculation.
60 * Newer versions of MASM seem to have changed this to be zero, and
61 * that apparently matches the COFF spec, so go with that.
63 * (5) Standard COFF does something very strange to common
64 * variables: the relocation point for a common variable is as far
65 * _before_ the variable as its size stretches out _after_ it. So
66 * we must fix up common variable references. Win32 seems to be
67 * sensible on this one.
70 /* Flag which version of COFF we are currently outputting. */
71 static bool win32, win64;
73 static int32_t imagebase_sect;
74 #define WRT_IMAGEBASE "..imagebase"
76 struct Reloc {
77 struct Reloc *next;
78 int32_t address; /* relative to _start_ of section */
79 int32_t symbol; /* symbol number */
80 enum {
81 SECT_SYMBOLS,
82 ABS_SYMBOL,
83 REAL_SYMBOLS
84 } symbase; /* relocation for symbol number :) */
85 int16_t type;
88 /* possible values for Reloc->type */
89 #define IMAGE_REL_AMD64_ADDR64 0x0001
90 #define IMAGE_REL_AMD64_ADDR32 0x0002
91 #define IMAGE_REL_AMD64_ADDR32NB 0x0003
92 #define IMAGE_REL_AMD64_REL32 0x0004
93 #define IMAGE_REL_I386_DIR32 0x0006
94 #define IMAGE_REL_I386_DIR32NB 0x0007
95 #define IMAGE_REL_I386_REL32 0x0014
97 struct Symbol {
98 char name[9];
99 int32_t strpos; /* string table position of name */
100 int32_t value; /* address, or COMMON variable size */
101 int section; /* section number where it's defined
102 * - in COFF codes, not NASM codes */
103 bool is_global; /* is it a global symbol or not? */
104 int16_t type; /* 0 - notype, 0x20 - function */
105 int32_t namlen; /* full name length */
108 static FILE *coffp;
109 static efunc error;
110 static char coff_infile[FILENAME_MAX];
112 struct Section {
113 struct SAA *data;
114 uint32_t len;
115 int nrelocs;
116 int32_t index;
117 struct Reloc *head, **tail;
118 uint32_t flags; /* section flags */
119 char name[9];
120 int32_t pos, relpos;
123 #define TEXT_FLAGS ((win32 | win64) ? 0x60500020L : 0x20L)
124 #define DATA_FLAGS ((win32 | win64) ? 0xC0300040L : 0x40L)
125 #define BSS_FLAGS ((win32 | win64) ? 0xC0300080L : 0x80L)
126 #define INFO_FLAGS 0x00100A00L
127 #define RDATA_FLAGS ((win32 | win64) ? 0x40400040L : 0x40L)
129 #define SECT_DELTA 32
130 static struct Section **sects;
131 static int nsects, sectlen;
133 static struct SAA *syms;
134 static uint32_t nsyms;
136 static int32_t def_seg;
138 static int initsym;
140 static struct RAA *bsym, *symval;
142 static struct SAA *strs;
143 static uint32_t strslen;
145 static void coff_gen_init(FILE *, efunc);
146 static void coff_sect_write(struct Section *, const uint8_t *,
147 uint32_t);
148 static void coff_write(void);
149 static void coff_section_header(char *, int32_t, int32_t, int32_t, int32_t, int, int32_t);
150 static void coff_write_relocs(struct Section *);
151 static void coff_write_symbols(void);
153 static void coff_win32_init(FILE * fp, efunc errfunc,
154 ldfunc ldef, evalfunc eval)
156 win32 = true; win64 = false;
157 (void)ldef; /* placate optimizers */
158 (void)eval;
159 coff_gen_init(fp, errfunc);
162 static void coff_win64_init(FILE * fp, efunc errfunc,
163 ldfunc ldef, evalfunc eval)
165 extern struct ofmt of_win64;
167 maxbits = 64;
168 win32 = false; win64 = true;
169 (void)ldef; /* placate optimizers */
170 (void)eval;
171 coff_gen_init(fp, errfunc);
172 imagebase_sect = seg_alloc()+1;
173 ldef(WRT_IMAGEBASE,imagebase_sect,0,NULL,false,false,&of_win64,errfunc);
176 static void coff_std_init(FILE * fp, efunc errfunc, ldfunc ldef,
177 evalfunc eval)
179 win32 = win64 = false;
180 (void)ldef; /* placate optimizers */
181 (void)eval;
182 coff_gen_init(fp, errfunc);
185 static void coff_gen_init(FILE * fp, efunc errfunc)
188 coffp = fp;
189 error = errfunc;
190 sects = NULL;
191 nsects = sectlen = 0;
192 syms = saa_init((int32_t)sizeof(struct Symbol));
193 nsyms = 0;
194 bsym = raa_init();
195 symval = raa_init();
196 strs = saa_init(1L);
197 strslen = 0;
198 def_seg = seg_alloc();
201 static void coff_cleanup(int debuginfo)
203 struct Reloc *r;
204 int i;
206 (void)debuginfo;
208 coff_write();
209 fclose(coffp);
210 for (i = 0; i < nsects; i++) {
211 if (sects[i]->data)
212 saa_free(sects[i]->data);
213 while (sects[i]->head) {
214 r = sects[i]->head;
215 sects[i]->head = sects[i]->head->next;
216 nasm_free(r);
218 nasm_free(sects[i]);
220 nasm_free(sects);
221 saa_free(syms);
222 raa_free(bsym);
223 raa_free(symval);
224 saa_free(strs);
227 static int coff_make_section(char *name, uint32_t flags)
229 struct Section *s;
231 s = nasm_malloc(sizeof(*s));
233 if (flags != BSS_FLAGS)
234 s->data = saa_init(1L);
235 else
236 s->data = NULL;
237 s->head = NULL;
238 s->tail = &s->head;
239 s->len = 0;
240 s->nrelocs = 0;
241 if (!strcmp(name, ".text"))
242 s->index = def_seg;
243 else
244 s->index = seg_alloc();
245 strncpy(s->name, name, 8);
246 s->name[8] = '\0';
247 s->flags = flags;
249 if (nsects >= sectlen)
250 sects =
251 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
252 sects[nsects++] = s;
254 return nsects - 1;
257 static int32_t coff_section_names(char *name, int pass, int *bits)
259 char *p;
260 uint32_t flags, align_and = ~0L, align_or = 0L;
261 int i;
264 * Set default bits.
266 if (!name) {
267 if(win64)
268 *bits = 64;
269 else
270 *bits = 32;
273 if (!name)
274 return def_seg;
276 p = name;
277 while (*p && !nasm_isspace(*p))
278 p++;
279 if (*p)
280 *p++ = '\0';
281 if (strlen(name) > 8) {
282 error(ERR_WARNING, "COFF section names limited to 8 characters:"
283 " truncating");
284 name[8] = '\0';
286 flags = 0;
288 while (*p && nasm_isspace(*p))
289 p++;
290 while (*p) {
291 char *q = p;
292 while (*p && !nasm_isspace(*p))
293 p++;
294 if (*p)
295 *p++ = '\0';
296 while (*p && nasm_isspace(*p))
297 p++;
299 if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
300 flags = TEXT_FLAGS;
301 } else if (!nasm_stricmp(q, "data")) {
302 flags = DATA_FLAGS;
303 } else if (!nasm_stricmp(q, "rdata")) {
304 if (win32 | win64)
305 flags = RDATA_FLAGS;
306 else {
307 flags = DATA_FLAGS; /* gotta do something */
308 error(ERR_NONFATAL, "standard COFF does not support"
309 " read-only data sections");
311 } else if (!nasm_stricmp(q, "bss")) {
312 flags = BSS_FLAGS;
313 } else if (!nasm_stricmp(q, "info")) {
314 if (win32 | win64)
315 flags = INFO_FLAGS;
316 else {
317 flags = DATA_FLAGS; /* gotta do something */
318 error(ERR_NONFATAL, "standard COFF does not support"
319 " informational sections");
321 } else if (!nasm_strnicmp(q, "align=", 6)) {
322 if (!(win32 | win64))
323 error(ERR_NONFATAL, "standard COFF does not support"
324 " section alignment specification");
325 else {
326 if (q[6 + strspn(q + 6, "0123456789")])
327 error(ERR_NONFATAL,
328 "argument to `align' is not numeric");
329 else {
330 unsigned int align = atoi(q + 6);
331 if (!align || ((align - 1) & align))
332 error(ERR_NONFATAL, "argument to `align' is not a"
333 " power of two");
334 else if (align > 64)
335 error(ERR_NONFATAL, "Win32 cannot align sections"
336 " to better than 64-byte boundaries");
337 else {
338 align_and = ~0x00F00000L;
339 align_or = (align == 1 ? 0x00100000L :
340 align == 2 ? 0x00200000L :
341 align == 4 ? 0x00300000L :
342 align == 8 ? 0x00400000L :
343 align == 16 ? 0x00500000L :
344 align ==
345 32 ? 0x00600000L : 0x00700000L);
352 for (i = 0; i < nsects; i++)
353 if (!strcmp(name, sects[i]->name))
354 break;
355 if (i == nsects) {
356 if (!flags) {
357 if (!strcmp(name, ".data"))
358 flags = DATA_FLAGS;
359 else if (!strcmp(name, ".rdata"))
360 flags = RDATA_FLAGS;
361 else if (!strcmp(name, ".bss"))
362 flags = BSS_FLAGS;
363 else if (win64 && !strcmp(name, ".pdata"))
364 flags = 0x40300040; /* rdata align=4 */
365 else if (win64 && !strcmp(name, ".xdata"))
366 flags = 0x40400040; /* rdate align=8 */
367 else
368 flags = TEXT_FLAGS;
370 i = coff_make_section(name, flags);
371 if (flags)
372 sects[i]->flags = flags;
373 sects[i]->flags &= align_and;
374 sects[i]->flags |= align_or;
375 } else if (pass == 1) {
376 if (flags)
377 error(ERR_WARNING, "section attributes ignored on"
378 " redeclaration of section `%s'", name);
381 return sects[i]->index;
384 static void coff_deflabel(char *name, int32_t segment, int64_t offset,
385 int is_global, char *special)
387 int pos = strslen + 4;
388 struct Symbol *sym;
390 if (special)
391 error(ERR_NONFATAL, "binary format does not support any"
392 " special symbol types");
394 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
395 if (strcmp(name,WRT_IMAGEBASE))
396 error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
397 return;
400 if (strlen(name) > 8) {
401 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
402 strslen += 1 + strlen(name);
403 } else
404 pos = -1;
406 sym = saa_wstruct(syms);
408 sym->strpos = pos;
409 sym->namlen = strlen(name);
410 if (pos == -1)
411 strcpy(sym->name, name);
412 sym->is_global = !!is_global;
413 sym->type = 0; /* Default to T_NULL (no type) */
414 if (segment == NO_SEG)
415 sym->section = -1; /* absolute symbol */
416 else {
417 int i;
418 sym->section = 0;
419 for (i = 0; i < nsects; i++)
420 if (segment == sects[i]->index) {
421 sym->section = i + 1;
422 break;
424 if (!sym->section)
425 sym->is_global = true;
427 if (is_global == 2)
428 sym->value = offset;
429 else
430 sym->value = (sym->section == 0 ? 0 : offset);
433 * define the references from external-symbol segment numbers
434 * to these symbol records.
436 if (sym->section == 0) {
437 bsym = raa_write(bsym, segment, nsyms);
440 if (segment != NO_SEG)
441 symval = raa_write(symval, segment, sym->section ? 0 : sym->value);
443 nsyms++;
446 static int32_t coff_add_reloc(struct Section *sect, int32_t segment,
447 int16_t type)
449 struct Reloc *r;
451 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
452 sect->tail = &r->next;
453 r->next = NULL;
455 r->address = sect->len;
456 if (segment == NO_SEG)
457 r->symbol = 0, r->symbase = ABS_SYMBOL;
458 else {
459 int i;
460 r->symbase = REAL_SYMBOLS;
461 for (i = 0; i < nsects; i++)
462 if (segment == sects[i]->index) {
463 r->symbol = i * 2;
464 r->symbase = SECT_SYMBOLS;
465 break;
467 if (r->symbase == REAL_SYMBOLS)
468 r->symbol = raa_read(bsym, segment);
470 r->type = type;
472 sect->nrelocs++;
475 * Return the fixup for standard COFF common variables.
477 if (r->symbase == REAL_SYMBOLS && !(win32 | win64))
478 return raa_read(symval, segment);
479 else
480 return 0;
483 static void coff_out(int32_t segto, const void *data,
484 enum out_type type, uint64_t size,
485 int32_t segment, int32_t wrt)
487 struct Section *s;
488 uint8_t mydata[8], *p;
489 int i;
491 if (wrt != NO_SEG && !win64) {
492 wrt = NO_SEG; /* continue to do _something_ */
493 error(ERR_NONFATAL, "WRT not supported by COFF output formats");
497 * handle absolute-assembly (structure definitions)
499 if (segto == NO_SEG) {
500 if (type != OUT_RESERVE)
501 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
502 " space");
503 return;
506 s = NULL;
507 for (i = 0; i < nsects; i++)
508 if (segto == sects[i]->index) {
509 s = sects[i];
510 break;
512 if (!s) {
513 int tempint; /* ignored */
514 if (segto != coff_section_names(".text", 2, &tempint))
515 error(ERR_PANIC, "strange segment conditions in COFF driver");
516 else
517 s = sects[nsects - 1];
520 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
521 if (win64 && wrt == NO_SEG &&
522 (!strcmp(s->name,".pdata") || !strcmp(s->name,".xdata")))
523 wrt = imagebase_sect;
525 if (!s->data && type != OUT_RESERVE) {
526 error(ERR_WARNING, "attempt to initialize memory in"
527 " BSS section `%s': ignored", s->name);
528 if (type == OUT_REL2ADR)
529 size = 2;
530 else if (type == OUT_REL4ADR)
531 size = 4;
532 s->len += size;
533 return;
536 if (type == OUT_RESERVE) {
537 if (s->data) {
538 error(ERR_WARNING, "uninitialised space declared in"
539 " non-BSS section `%s': zeroing", s->name);
540 coff_sect_write(s, NULL, size);
541 } else
542 s->len += size;
543 } else if (type == OUT_RAWDATA) {
544 if (segment != NO_SEG)
545 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
546 coff_sect_write(s, data, size);
547 } else if (type == OUT_ADDRESS) {
548 if (!(win64)) {
549 if (size != 4 && (segment != NO_SEG || wrt != NO_SEG))
550 error(ERR_NONFATAL, "COFF format does not support non-32-bit"
551 " relocations");
552 else {
553 int32_t fix = 0;
554 if (segment != NO_SEG || wrt != NO_SEG) {
555 if (wrt != NO_SEG) {
556 error(ERR_NONFATAL, "COFF format does not support"
557 " WRT types");
558 } else if (segment % 2) {
559 error(ERR_NONFATAL, "COFF format does not support"
560 " segment base references");
561 } else
562 fix = coff_add_reloc(s, segment, IMAGE_REL_I386_DIR32);
564 p = mydata;
565 WRITELONG(p, *(int64_t *)data + fix);
566 coff_sect_write(s, mydata, size);
568 } else {
569 int32_t fix = 0;
570 p = mydata;
571 if (size == 8) {
572 if (wrt == imagebase_sect) {
573 error(ERR_NONFATAL, "operand size mismatch: 'wrt "
574 WRT_IMAGEBASE "' is a 32-bit operand");
576 fix = coff_add_reloc(s, segment, IMAGE_REL_AMD64_ADDR64);
577 WRITEDLONG(p, *(int64_t *)data + fix);
578 coff_sect_write(s, mydata, size);
579 } else {
580 fix = coff_add_reloc(s, segment,
581 wrt == imagebase_sect ? IMAGE_REL_AMD64_ADDR32NB:
582 IMAGE_REL_AMD64_ADDR32);
583 WRITELONG(p, *(int64_t *)data + fix);
584 coff_sect_write(s, mydata, size);
587 } else if (type == OUT_REL2ADR) {
588 error(ERR_NONFATAL, "COFF format does not support 16-bit"
589 " relocations");
590 } else if (type == OUT_REL4ADR) {
591 if (segment == segto && !(win64)) /* Acceptable for RIP-relative */
592 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
593 else if (segment == NO_SEG && win32)
594 error(ERR_NONFATAL, "Win32 COFF does not correctly support"
595 " relative references to absolute addresses");
596 else {
597 int32_t fix = 0;
598 if (segment != NO_SEG && segment % 2) {
599 error(ERR_NONFATAL, "COFF format does not support"
600 " segment base references");
601 } else
602 fix = coff_add_reloc(s, segment,
603 win64 ? IMAGE_REL_AMD64_REL32 : IMAGE_REL_I386_REL32);
604 p = mydata;
605 if (win32 | win64) {
606 WRITELONG(p, *(int64_t *)data + 4 - size + fix);
607 } else {
608 WRITELONG(p, *(int64_t *)data - (size + s->len) + fix);
610 coff_sect_write(s, mydata, 4L);
616 static void coff_sect_write(struct Section *sect,
617 const uint8_t *data, uint32_t len)
619 saa_wbytes(sect->data, data, len);
620 sect->len += len;
623 typedef struct tagString {
624 struct tagString *Next;
625 int len;
626 char *String;
627 } STRING;
629 #define EXPORT_SECTION_NAME ".drectve"
630 #define EXPORT_SECTION_FLAGS INFO_FLAGS
632 #define EXPORT_SECTION_NAME ".text"
633 #define EXPORT_SECTION_FLAGS TEXT_FLAGS
636 static STRING *Exports = NULL;
637 static struct Section *directive_sec;
638 void AddExport(char *name)
640 STRING *rvp = Exports, *newS;
642 newS = (STRING *) nasm_malloc(sizeof(STRING));
643 newS->len = strlen(name);
644 newS->Next = NULL;
645 newS->String = (char *)nasm_malloc(newS->len + 1);
646 strcpy(newS->String, name);
647 if (rvp == NULL) {
648 int i;
649 for (i = 0; i < nsects; i++)
651 if (!strcmp(EXPORT_SECTION_NAME, sects[i]->name))
652 break;
653 if (i == nsects)
654 directive_sec =
655 sects[coff_make_section
656 (EXPORT_SECTION_NAME, EXPORT_SECTION_FLAGS)];
657 else
658 directive_sec = sects[i];
659 Exports = newS;
660 } else {
661 while (rvp->Next) {
662 if (!strcmp(rvp->String, name))
663 return;
664 rvp = rvp->Next;
666 rvp->Next = newS;
670 void BuildExportTable(void)
672 STRING *rvp = Exports, *next;
673 uint8_t buf[256];
674 int len;
675 if (rvp == NULL)
676 return;
677 while (rvp) {
678 len = sprintf((char *)buf, "-export:%s ", rvp->String);
679 coff_sect_write(directive_sec, buf, len);
680 rvp = rvp->Next;
683 next = Exports;
684 while ((rvp = next)) {
685 next = rvp->Next;
686 nasm_free(rvp->String);
687 nasm_free(rvp);
689 Exports = NULL;
692 static int coff_directives(char *directive, char *value, int pass)
694 if (!strcmp(directive, "export")) {
695 char *q, *name;
697 if (pass == 2)
698 return 1; /* ignore in pass two */
699 name = q = value;
700 while (*q && !nasm_isspace(*q))
701 q++;
702 if (nasm_isspace(*q)) {
703 *q++ = '\0';
704 while (*q && nasm_isspace(*q))
705 q++;
708 if (!*name) {
709 error(ERR_NONFATAL, "`export' directive requires export name");
710 return 1;
712 if (*q) {
713 error(ERR_NONFATAL, "unrecognized export qualifier `%s'", q);
714 return 1;
716 AddExport(name);
717 return 1;
718 } else if (win32 && !strcmp(directive,"safeseh")) {
719 static int sxseg=-1;
720 int i;
721 if (sxseg==-1)
722 { for (i = 0; i < nsects; i++)
723 if (!strcmp(".sxdata",sects[i]->name))
724 break;
725 if (i == nsects)
726 sxseg = coff_make_section(".sxdata",0x200);
727 else
728 sxseg = i;
730 if (pass==2) {
731 uint32_t n;
732 saa_rewind(syms);
733 for (n = 0; n < nsyms; n++) {
734 struct Symbol *sym = saa_rstruct(syms);
735 bool equals;
737 /* sym->strpos is biased by 4, because symbol
738 * table is prefixed with table length */
739 if (sym->strpos >=4) {
740 char *name = nasm_malloc(sym->namlen+1);
741 saa_fread(strs,sym->strpos-4,name,sym->namlen);
742 name[sym->namlen]='\0';
743 equals = !strcmp(value,name);
744 nasm_free(name);
746 else
747 equals = !strcmp(value,sym->name);
749 if (equals) {
750 /* this value arithmetics effectively reflects
751 * initsym in coff_write(): 2 for file, 1 for
752 * .absolute and two per each section */
753 unsigned char value[4],*p=value;
754 WRITELONG(p,n + 2 + 1 + nsects*2);
755 coff_sect_write(sects[sxseg],value,4);
756 sym->type = 0x20;
757 break;
760 if (n == nsyms) {
761 error(ERR_NONFATAL,
762 "`safeseh' directive requires valid symbol");
765 return 1;
767 return 0;
770 static void coff_write(void)
772 int32_t pos, sympos, vsize;
773 int i;
775 BuildExportTable(); /* fill in the .drectve section with -export's */
777 if (win32) {
778 /* add default value for @feat.00, this allows to 'link /safeseh' */
779 uint32_t n;
781 saa_rewind(syms);
782 for (n = 0; n < nsyms; n++) {
783 struct Symbol *sym = saa_rstruct(syms);
784 if (sym->strpos == -1 && !strcmp("@feat.00",sym->name))
785 break;
787 if (n == nsyms)
788 coff_deflabel("@feat.00",NO_SEG,1,0,NULL);
792 * Work out how big the file will get. Calculate the start of
793 * the `real' symbols at the same time.
795 pos = 0x14 + 0x28 * nsects;
796 initsym = 3; /* two for the file, one absolute */
797 for (i = 0; i < nsects; i++) {
798 if (sects[i]->data) {
799 sects[i]->pos = pos;
800 pos += sects[i]->len;
801 sects[i]->relpos = pos;
802 pos += 10 * sects[i]->nrelocs;
803 } else
804 sects[i]->pos = sects[i]->relpos = 0L;
805 initsym += 2; /* two for each section */
807 sympos = pos;
810 * Output the COFF header.
812 if (win64)
813 fwriteint16_t(0x8664, coffp); /* MACHINE_x86-64 */
814 else
815 fwriteint16_t(0x014C, coffp); /* MACHINE_i386 */
816 fwriteint16_t(nsects, coffp); /* number of sections */
817 fwriteint32_t(time(NULL), coffp); /* time stamp */
818 fwriteint32_t(sympos, coffp);
819 fwriteint32_t(nsyms + initsym, coffp);
820 fwriteint16_t(0, coffp); /* no optional header */
821 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
822 fwriteint16_t((win32 | win64) ? 0 : 0x104, coffp);
825 * Output the section headers.
827 vsize = 0L;
828 for (i = 0; i < nsects; i++) {
829 coff_section_header(sects[i]->name, vsize, sects[i]->len,
830 sects[i]->pos, sects[i]->relpos,
831 sects[i]->nrelocs, sects[i]->flags);
832 vsize += sects[i]->len;
836 * Output the sections and their relocations.
838 for (i = 0; i < nsects; i++)
839 if (sects[i]->data) {
840 saa_fpwrite(sects[i]->data, coffp);
841 coff_write_relocs(sects[i]);
845 * Output the symbol and string tables.
847 coff_write_symbols();
848 fwriteint32_t(strslen + 4, coffp); /* length includes length count */
849 saa_fpwrite(strs, coffp);
852 static void coff_section_header(char *name, int32_t vsize,
853 int32_t datalen, int32_t datapos,
854 int32_t relpos, int nrelocs, int32_t flags)
856 char padname[8];
858 (void)vsize;
860 memset(padname, 0, 8);
861 strncpy(padname, name, 8);
862 fwrite(padname, 8, 1, coffp);
863 fwriteint32_t(0, coffp); /* Virtual size field - set to 0 or vsize */
864 fwriteint32_t(0L, coffp); /* RVA/offset - we ignore */
865 fwriteint32_t(datalen, coffp);
866 fwriteint32_t(datapos, coffp);
867 fwriteint32_t(relpos, coffp);
868 fwriteint32_t(0L, coffp); /* no line numbers - we don't do 'em */
869 fwriteint16_t(nrelocs, coffp);
870 fwriteint16_t(0, coffp); /* again, no line numbers */
871 fwriteint32_t(flags, coffp);
874 static void coff_write_relocs(struct Section *s)
876 struct Reloc *r;
878 for (r = s->head; r; r = r->next) {
879 fwriteint32_t(r->address, coffp);
880 fwriteint32_t(r->symbol + (r->symbase == REAL_SYMBOLS ? initsym :
881 r->symbase == ABS_SYMBOL ? initsym - 1 :
882 r->symbase == SECT_SYMBOLS ? 2 : 0),
883 coffp);
884 fwriteint16_t(r->type, coffp);
888 static void coff_symbol(char *name, int32_t strpos, int32_t value,
889 int section, int type, int storageclass, int aux)
891 char padname[8];
893 if (name) {
894 memset(padname, 0, 8);
895 strncpy(padname, name, 8);
896 fwrite(padname, 8, 1, coffp);
897 } else {
898 fwriteint32_t(0L, coffp);
899 fwriteint32_t(strpos, coffp);
901 fwriteint32_t(value, coffp);
902 fwriteint16_t(section, coffp);
903 fwriteint16_t(type, coffp);
904 fputc(storageclass, coffp);
905 fputc(aux, coffp);
908 static void coff_write_symbols(void)
910 char filename[18];
911 uint32_t i;
914 * The `.file' record, and the file name auxiliary record.
916 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
917 memset(filename, 0, 18);
918 strncpy(filename, coff_infile, 18);
919 fwrite(filename, 18, 1, coffp);
922 * The section records, with their auxiliaries.
924 memset(filename, 0, 18); /* useful zeroed buffer */
926 for (i = 0; i < (uint32_t) nsects; i++) {
927 coff_symbol(sects[i]->name, 0L, 0L, i + 1, 0, 3, 1);
928 fwriteint32_t(sects[i]->len, coffp);
929 fwriteint16_t(sects[i]->nrelocs, coffp);
930 fwrite(filename, 12, 1, coffp);
934 * The absolute symbol, for relative-to-absolute relocations.
936 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
939 * The real symbols.
941 saa_rewind(syms);
942 for (i = 0; i < nsyms; i++) {
943 struct Symbol *sym = saa_rstruct(syms);
944 coff_symbol(sym->strpos == -1 ? sym->name : NULL,
945 sym->strpos, sym->value, sym->section,
946 sym->type, sym->is_global ? 2 : 3, 0);
950 static int32_t coff_segbase(int32_t segment)
952 return segment;
955 static void coff_std_filename(char *inname, char *outname, efunc error)
957 strcpy(coff_infile, inname);
958 standard_extension(inname, outname, ".o", error);
961 static void coff_win32_filename(char *inname, char *outname, efunc error)
963 strcpy(coff_infile, inname);
964 standard_extension(inname, outname, ".obj", error);
967 extern macros_t coff_stdmac[];
969 static int coff_set_info(enum geninfo type, char **val)
971 (void)type;
972 (void)val;
973 return 0;
975 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
977 #ifdef OF_COFF
979 struct ofmt of_coff = {
980 "COFF (i386) object files (e.g. DJGPP for DOS)",
981 "coff",
982 NULL,
983 null_debug_arr,
984 &null_debug_form,
985 coff_stdmac,
986 coff_std_init,
987 coff_set_info,
988 coff_out,
989 coff_deflabel,
990 coff_section_names,
991 coff_segbase,
992 coff_directives,
993 coff_std_filename,
994 coff_cleanup
997 #endif
999 #ifdef OF_WIN32
1001 struct ofmt of_win32 = {
1002 "Microsoft Win32 (i386) object files",
1003 "win32",
1004 NULL,
1005 null_debug_arr,
1006 &null_debug_form,
1007 coff_stdmac,
1008 coff_win32_init,
1009 coff_set_info,
1010 coff_out,
1011 coff_deflabel,
1012 coff_section_names,
1013 coff_segbase,
1014 coff_directives,
1015 coff_win32_filename,
1016 coff_cleanup
1019 #endif
1021 #ifdef OF_WIN64
1023 struct ofmt of_win64 = {
1024 "Microsoft Win64 (x86-64) object files",
1025 "win64",
1026 NULL,
1027 null_debug_arr,
1028 &null_debug_form,
1029 coff_stdmac,
1030 coff_win64_init,
1031 coff_set_info,
1032 coff_out,
1033 coff_deflabel,
1034 coff_section_names,
1035 coff_segbase,
1036 coff_directives,
1037 coff_win32_filename,
1038 coff_cleanup
1041 #endif