ELF64: once again, fix generation of "naked" OUT_REL*ADR
[nasm/avx512.git] / output / outcoff.c
blob31e5cbefff5ef5c09031a838738c212d4cefb840
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(sizeof(struct Symbol));
193 nsyms = 0;
194 bsym = raa_init();
195 symval = raa_init();
196 strs = saa_init(1);
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(1);
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 sectlen += SECT_DELTA;
251 sects = nasm_realloc(sects, sectlen * sizeof(*sects));
253 sects[nsects++] = s;
255 return nsects - 1;
258 static int32_t coff_section_names(char *name, int pass, int *bits)
260 char *p;
261 uint32_t flags, align_and = ~0L, align_or = 0L;
262 int i;
265 * Set default bits.
267 if (!name) {
268 if(win64)
269 *bits = 64;
270 else
271 *bits = 32;
274 if (!name)
275 return def_seg;
277 p = name;
278 while (*p && !nasm_isspace(*p))
279 p++;
280 if (*p)
281 *p++ = '\0';
282 if (strlen(name) > 8) {
283 error(ERR_WARNING, "COFF section names limited to 8 characters:"
284 " truncating");
285 name[8] = '\0';
287 flags = 0;
289 while (*p && nasm_isspace(*p))
290 p++;
291 while (*p) {
292 char *q = p;
293 while (*p && !nasm_isspace(*p))
294 p++;
295 if (*p)
296 *p++ = '\0';
297 while (*p && nasm_isspace(*p))
298 p++;
300 if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
301 flags = TEXT_FLAGS;
302 } else if (!nasm_stricmp(q, "data")) {
303 flags = DATA_FLAGS;
304 } else if (!nasm_stricmp(q, "rdata")) {
305 if (win32 | win64)
306 flags = RDATA_FLAGS;
307 else {
308 flags = DATA_FLAGS; /* gotta do something */
309 error(ERR_NONFATAL, "standard COFF does not support"
310 " read-only data sections");
312 } else if (!nasm_stricmp(q, "bss")) {
313 flags = BSS_FLAGS;
314 } else if (!nasm_stricmp(q, "info")) {
315 if (win32 | win64)
316 flags = INFO_FLAGS;
317 else {
318 flags = DATA_FLAGS; /* gotta do something */
319 error(ERR_NONFATAL, "standard COFF does not support"
320 " informational sections");
322 } else if (!nasm_strnicmp(q, "align=", 6)) {
323 if (!(win32 | win64))
324 error(ERR_NONFATAL, "standard COFF does not support"
325 " section alignment specification");
326 else {
327 if (q[6 + strspn(q + 6, "0123456789")])
328 error(ERR_NONFATAL,
329 "argument to `align' is not numeric");
330 else {
331 unsigned int align = atoi(q + 6);
332 if (!align || ((align - 1) & align))
333 error(ERR_NONFATAL, "argument to `align' is not a"
334 " power of two");
335 else if (align > 64)
336 error(ERR_NONFATAL, "Win32 cannot align sections"
337 " to better than 64-byte boundaries");
338 else {
339 align_and = ~0x00F00000L;
340 align_or = (align == 1 ? 0x00100000L :
341 align == 2 ? 0x00200000L :
342 align == 4 ? 0x00300000L :
343 align == 8 ? 0x00400000L :
344 align == 16 ? 0x00500000L :
345 align ==
346 32 ? 0x00600000L : 0x00700000L);
353 for (i = 0; i < nsects; i++)
354 if (!strcmp(name, sects[i]->name))
355 break;
356 if (i == nsects) {
357 if (!flags) {
358 if (!strcmp(name, ".data"))
359 flags = DATA_FLAGS;
360 else if (!strcmp(name, ".rdata"))
361 flags = RDATA_FLAGS;
362 else if (!strcmp(name, ".bss"))
363 flags = BSS_FLAGS;
364 else if (win64 && !strcmp(name, ".pdata"))
365 flags = 0x40300040; /* rdata align=4 */
366 else if (win64 && !strcmp(name, ".xdata"))
367 flags = 0x40400040; /* rdate align=8 */
368 else
369 flags = TEXT_FLAGS;
371 i = coff_make_section(name, flags);
372 if (flags)
373 sects[i]->flags = flags;
374 sects[i]->flags &= align_and;
375 sects[i]->flags |= align_or;
376 } else if (pass == 1) {
377 if (flags)
378 error(ERR_WARNING, "section attributes ignored on"
379 " redeclaration of section `%s'", name);
382 return sects[i]->index;
385 static void coff_deflabel(char *name, int32_t segment, int64_t offset,
386 int is_global, char *special)
388 int pos = strslen + 4;
389 struct Symbol *sym;
391 if (special)
392 error(ERR_NONFATAL, "binary format does not support any"
393 " special symbol types");
395 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
396 if (strcmp(name,WRT_IMAGEBASE))
397 error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
398 return;
401 if (strlen(name) > 8) {
402 size_t nlen = strlen(name)+1;
403 saa_wbytes(strs, name, nlen);
404 strslen += nlen;
405 } else
406 pos = -1;
408 sym = saa_wstruct(syms);
410 sym->strpos = pos;
411 sym->namlen = strlen(name);
412 if (pos == -1)
413 strcpy(sym->name, name);
414 sym->is_global = !!is_global;
415 sym->type = 0; /* Default to T_NULL (no type) */
416 if (segment == NO_SEG)
417 sym->section = -1; /* absolute symbol */
418 else {
419 int i;
420 sym->section = 0;
421 for (i = 0; i < nsects; i++)
422 if (segment == sects[i]->index) {
423 sym->section = i + 1;
424 break;
426 if (!sym->section)
427 sym->is_global = true;
429 if (is_global == 2)
430 sym->value = offset;
431 else
432 sym->value = (sym->section == 0 ? 0 : offset);
435 * define the references from external-symbol segment numbers
436 * to these symbol records.
438 if (sym->section == 0) {
439 bsym = raa_write(bsym, segment, nsyms);
442 if (segment != NO_SEG)
443 symval = raa_write(symval, segment, sym->section ? 0 : sym->value);
445 nsyms++;
448 static int32_t coff_add_reloc(struct Section *sect, int32_t segment,
449 int16_t type)
451 struct Reloc *r;
453 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
454 sect->tail = &r->next;
455 r->next = NULL;
457 r->address = sect->len;
458 if (segment == NO_SEG)
459 r->symbol = 0, r->symbase = ABS_SYMBOL;
460 else {
461 int i;
462 r->symbase = REAL_SYMBOLS;
463 for (i = 0; i < nsects; i++)
464 if (segment == sects[i]->index) {
465 r->symbol = i * 2;
466 r->symbase = SECT_SYMBOLS;
467 break;
469 if (r->symbase == REAL_SYMBOLS)
470 r->symbol = raa_read(bsym, segment);
472 r->type = type;
474 sect->nrelocs++;
477 * Return the fixup for standard COFF common variables.
479 if (r->symbase == REAL_SYMBOLS && !(win32 | win64))
480 return raa_read(symval, segment);
481 else
482 return 0;
485 static void coff_out(int32_t segto, const void *data,
486 enum out_type type, uint64_t size,
487 int32_t segment, int32_t wrt)
489 struct Section *s;
490 uint8_t mydata[8], *p;
491 int i;
493 if (wrt != NO_SEG && !win64) {
494 wrt = NO_SEG; /* continue to do _something_ */
495 error(ERR_NONFATAL, "WRT not supported by COFF output formats");
499 * handle absolute-assembly (structure definitions)
501 if (segto == NO_SEG) {
502 if (type != OUT_RESERVE)
503 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
504 " space");
505 return;
508 s = NULL;
509 for (i = 0; i < nsects; i++)
510 if (segto == sects[i]->index) {
511 s = sects[i];
512 break;
514 if (!s) {
515 int tempint; /* ignored */
516 if (segto != coff_section_names(".text", 2, &tempint))
517 error(ERR_PANIC, "strange segment conditions in COFF driver");
518 else
519 s = sects[nsects - 1];
522 /* magically default to 'wrt ..imagebase' in .pdata and .xdata */
523 if (win64 && wrt == NO_SEG &&
524 (!strcmp(s->name,".pdata") || !strcmp(s->name,".xdata")))
525 wrt = imagebase_sect;
527 if (!s->data && type != OUT_RESERVE) {
528 error(ERR_WARNING, "attempt to initialize memory in"
529 " BSS section `%s': ignored", s->name);
530 if (type == OUT_REL2ADR)
531 size = 2;
532 else if (type == OUT_REL4ADR)
533 size = 4;
534 s->len += size;
535 return;
538 if (type == OUT_RESERVE) {
539 if (s->data) {
540 error(ERR_WARNING, "uninitialised space declared in"
541 " non-BSS section `%s': zeroing", s->name);
542 coff_sect_write(s, NULL, size);
543 } else
544 s->len += size;
545 } else if (type == OUT_RAWDATA) {
546 if (segment != NO_SEG)
547 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
548 coff_sect_write(s, data, size);
549 } else if (type == OUT_ADDRESS) {
550 if (!(win64)) {
551 if (size != 4 && (segment != NO_SEG || wrt != NO_SEG))
552 error(ERR_NONFATAL, "COFF format does not support non-32-bit"
553 " relocations");
554 else {
555 int32_t fix = 0;
556 if (segment != NO_SEG || wrt != NO_SEG) {
557 if (wrt != NO_SEG) {
558 error(ERR_NONFATAL, "COFF format does not support"
559 " WRT types");
560 } else if (segment % 2) {
561 error(ERR_NONFATAL, "COFF format does not support"
562 " segment base references");
563 } else
564 fix = coff_add_reloc(s, segment, IMAGE_REL_I386_DIR32);
566 p = mydata;
567 WRITELONG(p, *(int64_t *)data + fix);
568 coff_sect_write(s, mydata, size);
570 } else {
571 int32_t fix = 0;
572 p = mydata;
573 if (size == 8) {
574 if (wrt == imagebase_sect) {
575 error(ERR_NONFATAL, "operand size mismatch: 'wrt "
576 WRT_IMAGEBASE "' is a 32-bit operand");
578 fix = coff_add_reloc(s, segment, IMAGE_REL_AMD64_ADDR64);
579 WRITEDLONG(p, *(int64_t *)data + fix);
580 coff_sect_write(s, mydata, size);
581 } else {
582 fix = coff_add_reloc(s, segment,
583 wrt == imagebase_sect ? IMAGE_REL_AMD64_ADDR32NB:
584 IMAGE_REL_AMD64_ADDR32);
585 WRITELONG(p, *(int64_t *)data + fix);
586 coff_sect_write(s, mydata, size);
589 } else if (type == OUT_REL2ADR) {
590 error(ERR_NONFATAL, "COFF format does not support 16-bit"
591 " relocations");
592 } else if (type == OUT_REL4ADR) {
593 if (segment == segto && !(win64)) /* Acceptable for RIP-relative */
594 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
595 else if (segment == NO_SEG && win32)
596 error(ERR_NONFATAL, "Win32 COFF does not correctly support"
597 " relative references to absolute addresses");
598 else {
599 int32_t fix = 0;
600 if (segment != NO_SEG && segment % 2) {
601 error(ERR_NONFATAL, "COFF format does not support"
602 " segment base references");
603 } else
604 fix = coff_add_reloc(s, segment,
605 win64 ? IMAGE_REL_AMD64_REL32 : IMAGE_REL_I386_REL32);
606 p = mydata;
607 if (win32 | win64) {
608 WRITELONG(p, *(int64_t *)data + 4 - size + fix);
609 } else {
610 WRITELONG(p, *(int64_t *)data - (size + s->len) + fix);
612 coff_sect_write(s, mydata, 4L);
618 static void coff_sect_write(struct Section *sect,
619 const uint8_t *data, uint32_t len)
621 saa_wbytes(sect->data, data, len);
622 sect->len += len;
625 typedef struct tagString {
626 struct tagString *Next;
627 int len;
628 char *String;
629 } STRING;
631 #define EXPORT_SECTION_NAME ".drectve"
632 #define EXPORT_SECTION_FLAGS INFO_FLAGS
634 #define EXPORT_SECTION_NAME ".text"
635 #define EXPORT_SECTION_FLAGS TEXT_FLAGS
638 static STRING *Exports = NULL;
639 static struct Section *directive_sec;
640 void AddExport(char *name)
642 STRING *rvp = Exports, *newS;
644 newS = (STRING *) nasm_malloc(sizeof(STRING));
645 newS->len = strlen(name);
646 newS->Next = NULL;
647 newS->String = (char *)nasm_malloc(newS->len + 1);
648 strcpy(newS->String, name);
649 if (rvp == NULL) {
650 int i;
651 for (i = 0; i < nsects; i++)
653 if (!strcmp(EXPORT_SECTION_NAME, sects[i]->name))
654 break;
655 if (i == nsects)
656 directive_sec =
657 sects[coff_make_section
658 (EXPORT_SECTION_NAME, EXPORT_SECTION_FLAGS)];
659 else
660 directive_sec = sects[i];
661 Exports = newS;
662 } else {
663 while (rvp->Next) {
664 if (!strcmp(rvp->String, name))
665 return;
666 rvp = rvp->Next;
668 rvp->Next = newS;
672 void BuildExportTable(void)
674 STRING *rvp = Exports, *next;
675 uint8_t buf[256];
676 int len;
677 if (rvp == NULL)
678 return;
679 while (rvp) {
680 len = sprintf((char *)buf, "-export:%s ", rvp->String);
681 coff_sect_write(directive_sec, buf, len);
682 rvp = rvp->Next;
685 next = Exports;
686 while ((rvp = next)) {
687 next = rvp->Next;
688 nasm_free(rvp->String);
689 nasm_free(rvp);
691 Exports = NULL;
694 static int coff_directives(char *directive, char *value, int pass)
696 if (!strcmp(directive, "export")) {
697 char *q, *name;
699 if (pass == 2)
700 return 1; /* ignore in pass two */
701 name = q = value;
702 while (*q && !nasm_isspace(*q))
703 q++;
704 if (nasm_isspace(*q)) {
705 *q++ = '\0';
706 while (*q && nasm_isspace(*q))
707 q++;
710 if (!*name) {
711 error(ERR_NONFATAL, "`export' directive requires export name");
712 return 1;
714 if (*q) {
715 error(ERR_NONFATAL, "unrecognized export qualifier `%s'", q);
716 return 1;
718 AddExport(name);
719 return 1;
720 } else if (win32 && !strcmp(directive,"safeseh")) {
721 static int sxseg=-1;
722 int i;
723 if (sxseg==-1)
724 { for (i = 0; i < nsects; i++)
725 if (!strcmp(".sxdata",sects[i]->name))
726 break;
727 if (i == nsects)
728 sxseg = coff_make_section(".sxdata",0x200);
729 else
730 sxseg = i;
732 if (pass==2) {
733 uint32_t n;
734 saa_rewind(syms);
735 for (n = 0; n < nsyms; n++) {
736 struct Symbol *sym = saa_rstruct(syms);
737 bool equals;
739 /* sym->strpos is biased by 4, because symbol
740 * table is prefixed with table length */
741 if (sym->strpos >=4) {
742 char *name = nasm_malloc(sym->namlen+1);
743 saa_fread(strs, sym->strpos-4, name, sym->namlen);
744 name[sym->namlen] = '\0';
745 equals = !strcmp(value,name);
746 nasm_free(name);
748 else
749 equals = !strcmp(value,sym->name);
751 if (equals) {
752 /* this value arithmetics effectively reflects
753 * initsym in coff_write(): 2 for file, 1 for
754 * .absolute and two per each section */
755 unsigned char value[4],*p=value;
756 WRITELONG(p,n + 2 + 1 + nsects*2);
757 coff_sect_write(sects[sxseg],value,4);
758 sym->type = 0x20;
759 break;
762 if (n == nsyms) {
763 error(ERR_NONFATAL,
764 "`safeseh' directive requires valid symbol");
767 return 1;
769 return 0;
772 static void coff_write(void)
774 int32_t pos, sympos, vsize;
775 int i;
777 BuildExportTable(); /* fill in the .drectve section with -export's */
779 if (win32) {
780 /* add default value for @feat.00, this allows to 'link /safeseh' */
781 uint32_t n;
783 saa_rewind(syms);
784 for (n = 0; n < nsyms; n++) {
785 struct Symbol *sym = saa_rstruct(syms);
786 if (sym->strpos == -1 && !strcmp("@feat.00",sym->name))
787 break;
789 if (n == nsyms)
790 coff_deflabel("@feat.00",NO_SEG,1,0,NULL);
794 * Work out how big the file will get. Calculate the start of
795 * the `real' symbols at the same time.
797 pos = 0x14 + 0x28 * nsects;
798 initsym = 3; /* two for the file, one absolute */
799 for (i = 0; i < nsects; i++) {
800 if (sects[i]->data) {
801 sects[i]->pos = pos;
802 pos += sects[i]->len;
803 sects[i]->relpos = pos;
804 pos += 10 * sects[i]->nrelocs;
805 } else
806 sects[i]->pos = sects[i]->relpos = 0L;
807 initsym += 2; /* two for each section */
809 sympos = pos;
812 * Output the COFF header.
814 if (win64)
815 fwriteint16_t(0x8664, coffp); /* MACHINE_x86-64 */
816 else
817 fwriteint16_t(0x014C, coffp); /* MACHINE_i386 */
818 fwriteint16_t(nsects, coffp); /* number of sections */
819 fwriteint32_t(time(NULL), coffp); /* time stamp */
820 fwriteint32_t(sympos, coffp);
821 fwriteint32_t(nsyms + initsym, coffp);
822 fwriteint16_t(0, coffp); /* no optional header */
823 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
824 fwriteint16_t((win32 | win64) ? 0 : 0x104, coffp);
827 * Output the section headers.
829 vsize = 0L;
830 for (i = 0; i < nsects; i++) {
831 coff_section_header(sects[i]->name, vsize, sects[i]->len,
832 sects[i]->pos, sects[i]->relpos,
833 sects[i]->nrelocs, sects[i]->flags);
834 vsize += sects[i]->len;
838 * Output the sections and their relocations.
840 for (i = 0; i < nsects; i++)
841 if (sects[i]->data) {
842 saa_fpwrite(sects[i]->data, coffp);
843 coff_write_relocs(sects[i]);
847 * Output the symbol and string tables.
849 coff_write_symbols();
850 fwriteint32_t(strslen + 4, coffp); /* length includes length count */
851 saa_fpwrite(strs, coffp);
854 static void coff_section_header(char *name, int32_t vsize,
855 int32_t datalen, int32_t datapos,
856 int32_t relpos, int nrelocs, int32_t flags)
858 char padname[8];
860 (void)vsize;
862 memset(padname, 0, 8);
863 strncpy(padname, name, 8);
864 fwrite(padname, 8, 1, coffp);
865 fwriteint32_t(0, coffp); /* Virtual size field - set to 0 or vsize */
866 fwriteint32_t(0L, coffp); /* RVA/offset - we ignore */
867 fwriteint32_t(datalen, coffp);
868 fwriteint32_t(datapos, coffp);
869 fwriteint32_t(relpos, coffp);
870 fwriteint32_t(0L, coffp); /* no line numbers - we don't do 'em */
871 fwriteint16_t(nrelocs, coffp);
872 fwriteint16_t(0, coffp); /* again, no line numbers */
873 fwriteint32_t(flags, coffp);
876 static void coff_write_relocs(struct Section *s)
878 struct Reloc *r;
880 for (r = s->head; r; r = r->next) {
881 fwriteint32_t(r->address, coffp);
882 fwriteint32_t(r->symbol + (r->symbase == REAL_SYMBOLS ? initsym :
883 r->symbase == ABS_SYMBOL ? initsym - 1 :
884 r->symbase == SECT_SYMBOLS ? 2 : 0),
885 coffp);
886 fwriteint16_t(r->type, coffp);
890 static void coff_symbol(char *name, int32_t strpos, int32_t value,
891 int section, int type, int storageclass, int aux)
893 char padname[8];
895 if (name) {
896 memset(padname, 0, 8);
897 strncpy(padname, name, 8);
898 fwrite(padname, 8, 1, coffp);
899 } else {
900 fwriteint32_t(0, coffp);
901 fwriteint32_t(strpos, coffp);
903 fwriteint32_t(value, coffp);
904 fwriteint16_t(section, coffp);
905 fwriteint16_t(type, coffp);
906 fputc(storageclass, coffp);
907 fputc(aux, coffp);
910 static void coff_write_symbols(void)
912 char filename[18];
913 uint32_t i;
916 * The `.file' record, and the file name auxiliary record.
918 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
919 memset(filename, 0, 18);
920 strncpy(filename, coff_infile, 18);
921 fwrite(filename, 18, 1, coffp);
924 * The section records, with their auxiliaries.
926 memset(filename, 0, 18); /* useful zeroed buffer */
928 for (i = 0; i < (uint32_t) nsects; i++) {
929 coff_symbol(sects[i]->name, 0L, 0L, i + 1, 0, 3, 1);
930 fwriteint32_t(sects[i]->len, coffp);
931 fwriteint16_t(sects[i]->nrelocs, coffp);
932 fwrite(filename, 12, 1, coffp);
936 * The absolute symbol, for relative-to-absolute relocations.
938 coff_symbol(".absolut", 0L, 0L, -1, 0, 3, 0);
941 * The real symbols.
943 saa_rewind(syms);
944 for (i = 0; i < nsyms; i++) {
945 struct Symbol *sym = saa_rstruct(syms);
946 coff_symbol(sym->strpos == -1 ? sym->name : NULL,
947 sym->strpos, sym->value, sym->section,
948 sym->type, sym->is_global ? 2 : 3, 0);
952 static int32_t coff_segbase(int32_t segment)
954 return segment;
957 static void coff_std_filename(char *inname, char *outname, efunc error)
959 strcpy(coff_infile, inname);
960 standard_extension(inname, outname, ".o", error);
963 static void coff_win32_filename(char *inname, char *outname, efunc error)
965 strcpy(coff_infile, inname);
966 standard_extension(inname, outname, ".obj", error);
969 extern macros_t coff_stdmac[];
971 static int coff_set_info(enum geninfo type, char **val)
973 (void)type;
974 (void)val;
975 return 0;
977 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
979 #ifdef OF_COFF
981 struct ofmt of_coff = {
982 "COFF (i386) object files (e.g. DJGPP for DOS)",
983 "coff",
984 NULL,
985 null_debug_arr,
986 &null_debug_form,
987 coff_stdmac,
988 coff_std_init,
989 coff_set_info,
990 coff_out,
991 coff_deflabel,
992 coff_section_names,
993 coff_segbase,
994 coff_directives,
995 coff_std_filename,
996 coff_cleanup
999 #endif
1001 #ifdef OF_WIN32
1003 struct ofmt of_win32 = {
1004 "Microsoft Win32 (i386) object files",
1005 "win32",
1006 NULL,
1007 null_debug_arr,
1008 &null_debug_form,
1009 coff_stdmac,
1010 coff_win32_init,
1011 coff_set_info,
1012 coff_out,
1013 coff_deflabel,
1014 coff_section_names,
1015 coff_segbase,
1016 coff_directives,
1017 coff_win32_filename,
1018 coff_cleanup
1021 #endif
1023 #ifdef OF_WIN64
1025 struct ofmt of_win64 = {
1026 "Microsoft Win64 (x86-64) object files",
1027 "win64",
1028 NULL,
1029 null_debug_arr,
1030 &null_debug_form,
1031 coff_stdmac,
1032 coff_win64_init,
1033 coff_set_info,
1034 coff_out,
1035 coff_deflabel,
1036 coff_section_names,
1037 coff_segbase,
1038 coff_directives,
1039 coff_win32_filename,
1040 coff_cleanup
1043 #endif