NASM 0.98p3.5
[nasm.git] / outcoff.c
blobf546a8e021b3f0f5b58b14a8ad1b260ccdd4d52e
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 licence given in the file "Licence"
7 * distributed in the NASM archive.
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <time.h>
16 #include "nasm.h"
17 #include "nasmlib.h"
18 #include "outform.h"
20 #if defined(OF_COFF) || defined(OF_WIN32)
23 * Notes on COFF:
25 * (0) When I say `standard COFF' below, I mean `COFF as output and
26 * used by DJGPP'. I assume DJGPP gets it right.
28 * (1) Win32 appears to interpret the term `relative relocation'
29 * differently from standard COFF. Standard COFF understands a
30 * relative relocation to mean that during relocation you add the
31 * address of the symbol you're referencing, and subtract the base
32 * address of the section you're in. Win32 COFF, by contrast, seems
33 * to add the address of the symbol and then subtract the address
34 * of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
35 * subtly incompatible.
37 * (2) Win32 doesn't bother putting any flags in the header flags
38 * field (at offset 0x12 into the file).
40 * (3) Win32 uses some extra flags into the section header table:
41 * it defines flags 0x80000000 (writable), 0x40000000 (readable)
42 * and 0x20000000 (executable), and uses them in the expected
43 * combinations. It also defines 0x00100000 through 0x00700000 for
44 * section alignments of 1 through 64 bytes.
46 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
47 * field directly after the section name in the section header
48 * table for something strange: they store what the address of the
49 * section start point _would_ be, if you laid all the sections end
50 * to end starting at zero. Dunno why. Microsoft's documentation
51 * lists this field as "Virtual Size of Section", which doesn't
52 * seem to fit at all. In fact, Win32 even includes non-linked
53 * sections such as .drectve in this calculation.
55 * (5) Standard COFF does something very strange to common
56 * variables: the relocation point for a common variable is as far
57 * _before_ the variable as its size stretches out _after_ it. So
58 * we must fix up common variable references. Win32 seems to be
59 * sensible on this one.
62 /* Flag which version of COFF we are currently outputting. */
63 static int win32;
65 struct Reloc {
66 struct Reloc *next;
67 long address; /* relative to _start_ of section */
68 long symbol; /* symbol number */
69 enum {
70 SECT_SYMBOLS,
71 ABS_SYMBOL,
72 REAL_SYMBOLS
73 } symbase; /* relocation for symbol number :) */
74 int relative; /* TRUE or FALSE */
77 struct Symbol {
78 char name[9];
79 long strpos; /* string table position of name */
80 int section; /* section number where it's defined
81 * - in COFF codes, not NASM codes */
82 int is_global; /* is it a global symbol or not? */
83 long value; /* address, or COMMON variable size */
86 static FILE *coffp;
87 static efunc error;
88 static char coff_infile[FILENAME_MAX];
90 struct Section {
91 struct SAA *data;
92 unsigned long len;
93 int nrelocs;
94 long index;
95 struct Reloc *head, **tail;
96 unsigned long flags; /* section flags */
97 char name[9];
98 long pos, relpos;
101 #define TEXT_FLAGS (win32 ? 0x60500020L : 0x20L)
102 #define DATA_FLAGS (win32 ? 0xC0300040L : 0x40L)
103 #define BSS_FLAGS (win32 ? 0xC0300080L : 0x80L)
104 #define INFO_FLAGS 0x00100A00L
106 #define SECT_DELTA 32
107 static struct Section **sects;
108 static int nsects, sectlen;
110 static struct SAA *syms;
111 static unsigned long nsyms;
113 static long def_seg;
115 static int initsym;
117 static struct RAA *bsym, *symval;
119 static struct SAA *strs;
120 static unsigned long strslen;
122 static void coff_gen_init(FILE *, efunc);
123 static void coff_sect_write (struct Section *, unsigned char *,
124 unsigned long);
125 static void coff_write (void);
126 static void coff_section_header (char *, long, long, long, long, int, long);
127 static void coff_write_relocs (struct Section *);
128 static void coff_write_symbols (void);
130 static void coff_win32_init(FILE *fp, efunc errfunc,
131 ldfunc ldef, evalfunc eval)
133 win32 = TRUE;
134 (void) ldef; /* placate optimisers */
135 coff_gen_init(fp, errfunc);
138 static void coff_std_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
140 win32 = FALSE;
141 (void) ldef; /* placate optimisers */
142 coff_gen_init(fp, errfunc);
145 static void coff_gen_init(FILE *fp, efunc errfunc)
148 coffp = fp;
149 error = errfunc;
150 sects = NULL;
151 nsects = sectlen = 0;
152 syms = saa_init((long)sizeof(struct Symbol));
153 nsyms = 0;
154 bsym = raa_init();
155 symval = raa_init();
156 strs = saa_init(1L);
157 strslen = 0;
158 def_seg = seg_alloc();
161 static void coff_cleanup(int debuginfo)
163 struct Reloc *r;
164 int i;
166 (void) debuginfo;
168 coff_write();
169 fclose (coffp);
170 for (i=0; i<nsects; i++) {
171 if (sects[i]->data)
172 saa_free (sects[i]->data);
173 while (sects[i]->head) {
174 r = sects[i]->head;
175 sects[i]->head = sects[i]->head->next;
176 nasm_free (r);
178 nasm_free (sects[i]);
180 nasm_free (sects);
181 saa_free (syms);
182 raa_free (bsym);
183 raa_free (symval);
184 saa_free (strs);
187 static int coff_make_section (char *name, unsigned long flags)
189 struct Section *s;
191 s = nasm_malloc (sizeof(*s));
193 if (flags != BSS_FLAGS)
194 s->data = saa_init (1L);
195 else
196 s->data = NULL;
197 s->head = NULL;
198 s->tail = &s->head;
199 s->len = 0;
200 s->nrelocs = 0;
201 if (!strcmp(name, ".text"))
202 s->index = def_seg;
203 else
204 s->index = seg_alloc();
205 strncpy (s->name, name, 8);
206 s->name[8] = '\0';
207 s->flags = flags;
209 if (nsects >= sectlen)
210 sects = nasm_realloc (sects, (sectlen += SECT_DELTA)*sizeof(*sects));
211 sects[nsects++] = s;
213 return nsects-1;
216 static long coff_section_names (char *name, int pass, int *bits)
218 char *p;
219 unsigned long flags, align_and = ~0L, align_or = 0L;
220 int i;
223 * Default is 32 bits.
225 if (!name)
226 *bits = 32;
228 if (!name)
229 return def_seg;
231 p = name;
232 while (*p && !isspace(*p)) p++;
233 if (*p) *p++ = '\0';
234 if (strlen(name) > 8) {
235 error (ERR_WARNING, "COFF section names limited to 8 characters:"
236 " truncating");
237 name[8] = '\0';
239 flags = 0;
241 while (*p && isspace(*p)) p++;
242 while (*p) {
243 char *q = p;
244 while (*p && !isspace(*p)) p++;
245 if (*p) *p++ = '\0';
246 while (*p && isspace(*p)) p++;
248 if (!nasm_stricmp(q, "code") || !nasm_stricmp(q, "text")) {
249 flags = TEXT_FLAGS;
250 } else if (!nasm_stricmp(q, "data")) {
251 flags = DATA_FLAGS;
252 } else if (!nasm_stricmp(q, "bss")) {
253 flags = BSS_FLAGS;
254 } else if (!nasm_stricmp(q, "info")) {
255 if (win32)
256 flags = INFO_FLAGS;
257 else {
258 flags = DATA_FLAGS; /* gotta do something */
259 error (ERR_NONFATAL, "standard COFF does not support"
260 " informational sections");
262 } else if (!nasm_strnicmp(q,"align=",6)) {
263 if (!win32)
264 error (ERR_NONFATAL, "standard COFF does not support"
265 " section alignment specification");
266 else {
267 if (q[6+strspn(q+6,"0123456789")])
268 error(ERR_NONFATAL, "argument to `align' is not numeric");
269 else {
270 unsigned int align = atoi(q+6);
271 if (!align || ((align-1) & align))
272 error(ERR_NONFATAL, "argument to `align' is not a"
273 " power of two");
274 else if (align > 64)
275 error(ERR_NONFATAL, "Win32 cannot align sections"
276 " to better than 64-byte boundaries");
277 else {
278 align_and = ~0x00F00000L;
279 align_or = (align == 1 ? 0x00100000L :
280 align == 2 ? 0x00200000L :
281 align == 4 ? 0x00300000L :
282 align == 8 ? 0x00400000L :
283 align == 16 ? 0x00500000L :
284 align == 32 ? 0x00600000L : 0x00700000L);
291 for (i=0; i<nsects; i++)
292 if (!strcmp(name, sects[i]->name))
293 break;
294 if (i == nsects) {
295 if (!flags) {
296 if (!strcmp(name, ".data"))
297 flags = DATA_FLAGS;
298 else if (!strcmp(name, ".bss"))
299 flags = BSS_FLAGS;
300 else
301 flags = TEXT_FLAGS;
303 i = coff_make_section (name, flags);
304 if (flags)
305 sects[i]->flags = flags;
306 sects[i]->flags &= align_and;
307 sects[i]->flags |= align_or;
308 } else if (pass == 1) {
309 if (flags)
310 error (ERR_WARNING, "section attributes ignored on"
311 " redeclaration of section `%s'", name);
314 return sects[i]->index;
317 static void coff_deflabel (char *name, long segment, long offset,
318 int is_global, char *special)
320 int pos = strslen+4;
321 struct Symbol *sym;
323 if (special)
324 error (ERR_NONFATAL, "binary format does not support any"
325 " special symbol types");
327 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
328 error (ERR_NONFATAL, "unrecognised special symbol `%s'", name);
329 return;
332 if (strlen(name) > 8) {
333 saa_wbytes (strs, name, (long)(1+strlen(name)));
334 strslen += 1+strlen(name);
335 } else
336 pos = -1;
338 sym = saa_wstruct (syms);
340 sym->strpos = pos;
341 if (pos == -1)
342 strcpy (sym->name, name);
343 sym->is_global = !!is_global;
344 if (segment == NO_SEG)
345 sym->section = -1; /* absolute symbol */
346 else {
347 int i;
348 sym->section = 0;
349 for (i=0; i<nsects; i++)
350 if (segment == sects[i]->index) {
351 sym->section = i+1;
352 break;
354 if (!sym->section)
355 sym->is_global = TRUE;
357 if (is_global == 2)
358 sym->value = offset;
359 else
360 sym->value = (sym->section == 0 ? 0 : offset);
363 * define the references from external-symbol segment numbers
364 * to these symbol records.
366 if (sym->section == 0)
367 bsym = raa_write (bsym, segment, nsyms);
369 if (segment != NO_SEG)
370 symval = raa_write (symval, segment, sym->section ? 0 : sym->value);
372 nsyms++;
375 static long coff_add_reloc (struct Section *sect, long segment,
376 int relative)
378 struct Reloc *r;
380 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
381 sect->tail = &r->next;
382 r->next = NULL;
384 r->address = sect->len;
385 if (segment == NO_SEG)
386 r->symbol = 0, r->symbase = ABS_SYMBOL;
387 else {
388 int i;
389 r->symbase = REAL_SYMBOLS;
390 for (i=0; i<nsects; i++)
391 if (segment == sects[i]->index) {
392 r->symbol = i*2;
393 r->symbase = SECT_SYMBOLS;
394 break;
396 if (r->symbase == REAL_SYMBOLS)
397 r->symbol = raa_read (bsym, segment);
399 r->relative = relative;
401 sect->nrelocs++;
404 * Return the fixup for standard COFF common variables.
406 if (r->symbase == REAL_SYMBOLS && !win32)
407 return raa_read (symval, segment);
408 else
409 return 0;
412 static void coff_out (long segto, void *data, unsigned long type,
413 long segment, long wrt)
415 struct Section *s;
416 long realbytes = type & OUT_SIZMASK;
417 unsigned char mydata[4], *p;
418 int i;
420 if (wrt != NO_SEG) {
421 wrt = NO_SEG; /* continue to do _something_ */
422 error (ERR_NONFATAL, "WRT not supported by COFF output formats");
425 type &= OUT_TYPMASK;
428 * handle absolute-assembly (structure definitions)
430 if (segto == NO_SEG) {
431 if (type != OUT_RESERVE)
432 error (ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
433 " space");
434 return;
437 s = NULL;
438 for (i=0; i<nsects; i++)
439 if (segto == sects[i]->index) {
440 s = sects[i];
441 break;
443 if (!s) {
444 int tempint; /* ignored */
445 if (segto != coff_section_names (".text", 2, &tempint))
446 error (ERR_PANIC, "strange segment conditions in COFF driver");
447 else
448 s = sects[nsects-1];
451 if (!s->data && type != OUT_RESERVE) {
452 error(ERR_WARNING, "attempt to initialise memory in"
453 " BSS section `%s': ignored", s->name);
454 if (type == OUT_REL2ADR)
455 realbytes = 2;
456 else if (type == OUT_REL4ADR)
457 realbytes = 4;
458 s->len += realbytes;
459 return;
462 if (type == OUT_RESERVE) {
463 if (s->data) {
464 error(ERR_WARNING, "uninitialised space declared in"
465 " non-BSS section `%s': zeroing", s->name);
466 coff_sect_write (s, NULL, realbytes);
467 } else
468 s->len += realbytes;
469 } else if (type == OUT_RAWDATA) {
470 if (segment != NO_SEG)
471 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
472 coff_sect_write (s, data, realbytes);
473 } else if (type == OUT_ADDRESS) {
474 if (realbytes != 4 && (segment != NO_SEG || wrt != NO_SEG))
475 error(ERR_NONFATAL, "COFF format does not support non-32-bit"
476 " relocations");
477 else {
478 long fix = 0;
479 if (segment != NO_SEG || wrt != NO_SEG) {
480 if (wrt != NO_SEG) {
481 error(ERR_NONFATAL, "COFF format does not support"
482 " WRT types");
483 } else if (segment % 2) {
484 error(ERR_NONFATAL, "COFF format does not support"
485 " segment base references");
486 } else
487 fix = coff_add_reloc (s, segment, FALSE);
489 p = mydata;
490 WRITELONG (p, *(long *)data + fix);
491 coff_sect_write (s, mydata, realbytes);
493 } else if (type == OUT_REL2ADR) {
494 error(ERR_NONFATAL, "COFF format does not support 16-bit"
495 " relocations");
496 } else if (type == OUT_REL4ADR) {
497 if (segment == segto)
498 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
499 else if (segment == NO_SEG && win32)
500 error(ERR_NONFATAL, "Win32 COFF does not correctly support"
501 " relative references to absolute addresses");
502 else {
503 long fix = 0;
504 if (segment != NO_SEG && segment % 2) {
505 error(ERR_NONFATAL, "COFF format does not support"
506 " segment base references");
507 } else
508 fix = coff_add_reloc (s, segment, TRUE);
509 p = mydata;
510 if (win32) {
511 WRITELONG (p, *(long*)data + 4 - realbytes + fix);
512 } else {
513 WRITELONG (p, *(long*)data-(realbytes + s->len) + fix);
515 coff_sect_write (s, mydata, 4L);
520 static void coff_sect_write (struct Section *sect,
521 unsigned char *data, unsigned long len)
523 saa_wbytes (sect->data, data, len);
524 sect->len += len;
527 static int coff_directives (char *directive, char *value, int pass)
529 return 0;
532 static void coff_write (void)
534 long pos, sympos, vsize;
535 int i;
538 * Work out how big the file will get. Calculate the start of
539 * the `real' symbols at the same time.
541 pos = 0x14 + 0x28 * nsects;
542 initsym = 3; /* two for the file, one absolute */
543 for (i=0; i<nsects; i++) {
544 if (sects[i]->data) {
545 sects[i]->pos = pos;
546 pos += sects[i]->len;
547 sects[i]->relpos = pos;
548 pos += 10 * sects[i]->nrelocs;
549 } else
550 sects[i]->pos = sects[i]->relpos = 0L;
551 initsym += 2; /* two for each section */
553 sympos = pos;
556 * Output the COFF header.
558 fwriteshort (0x14C, coffp); /* MACHINE_i386 */
559 fwriteshort (nsects, coffp); /* number of sections */
560 fwritelong (time(NULL), coffp); /* time stamp */
561 fwritelong (sympos, coffp);
562 fwritelong (nsyms + initsym, coffp);
563 fwriteshort (0, coffp); /* no optional header */
564 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
565 fwriteshort (win32 ? 0 : 0x104, coffp);
568 * Output the section headers.
570 vsize = 0L;
571 for (i=0; i<nsects; i++) {
572 coff_section_header (sects[i]->name, vsize, sects[i]->len,
573 sects[i]->pos, sects[i]->relpos,
574 sects[i]->nrelocs, sects[i]->flags);
575 vsize += sects[i]->len;
579 * Output the sections and their relocations.
581 for (i=0; i<nsects; i++)
582 if (sects[i]->data) {
583 saa_fpwrite (sects[i]->data, coffp);
584 coff_write_relocs (sects[i]);
588 * Output the symbol and string tables.
590 coff_write_symbols();
591 fwritelong (strslen+4, coffp); /* length includes length count */
592 saa_fpwrite (strs, coffp);
595 static void coff_section_header (char *name, long vsize,
596 long datalen, long datapos,
597 long relpos, int nrelocs, long flags)
599 char padname[8];
601 memset (padname, 0, 8);
602 strncpy (padname, name, 8);
603 fwrite (padname, 8, 1, coffp);
604 fwritelong (vsize, coffp);
605 fwritelong (0L, coffp); /* RVA/offset - we ignore */
606 fwritelong (datalen, coffp);
607 fwritelong (datapos, coffp);
608 fwritelong (relpos, coffp);
609 fwritelong (0L, coffp); /* no line numbers - we don't do 'em */
610 fwriteshort (nrelocs, coffp);
611 fwriteshort (0, coffp); /* again, no line numbers */
612 fwritelong (flags, coffp);
615 static void coff_write_relocs (struct Section *s)
617 struct Reloc *r;
619 for (r = s->head; r; r = r->next) {
620 fwritelong (r->address, coffp);
621 fwritelong (r->symbol + (r->symbase == REAL_SYMBOLS ? initsym :
622 r->symbase == ABS_SYMBOL ? initsym-1 :
623 r->symbase == SECT_SYMBOLS ? 2 : 0), coffp);
625 * Strange: Microsoft's COFF documentation says 0x03 for an
626 * absolute relocation, but both Visual C++ and DJGPP agree
627 * that in fact it's 0x06. I'll use 0x06 until someone
628 * argues.
630 fwriteshort (r->relative ? 0x14 : 0x06, coffp);
634 static void coff_symbol (char *name, long strpos, long value,
635 int section, int type, int aux)
637 char padname[8];
639 if (name) {
640 memset (padname, 0, 8);
641 strncpy (padname, name, 8);
642 fwrite (padname, 8, 1, coffp);
643 } else {
644 fwritelong (0L, coffp);
645 fwritelong (strpos, coffp);
647 fwritelong (value, coffp);
648 fwriteshort (section, coffp);
649 fwriteshort (0, coffp);
650 fputc (type, coffp);
651 fputc (aux, coffp);
654 static void coff_write_symbols (void)
656 char filename[18];
657 int i;
660 * The `.file' record, and the file name auxiliary record.
662 coff_symbol (".file", 0L, 0L, -2, 0x67, 1);
663 memset (filename, 0, 18);
664 strncpy (filename, coff_infile, 18);
665 fwrite (filename, 18, 1, coffp);
668 * The section records, with their auxiliaries.
670 memset (filename, 0, 18); /* useful zeroed buffer */
672 for (i=0; i<nsects; i++) {
673 coff_symbol (sects[i]->name, 0L, 0L, i+1, 3, 1);
674 fwritelong (sects[i]->len, coffp);
675 fwriteshort (sects[i]->nrelocs, coffp);
676 fwrite (filename, 12, 1, coffp);
680 * The absolute symbol, for relative-to-absolute relocations.
682 coff_symbol (".absolut", 0L, 0L, -1, 3, 0);
685 * The real symbols.
687 saa_rewind (syms);
688 for (i=0; i<nsyms; i++) {
689 struct Symbol *sym = saa_rstruct (syms);
690 coff_symbol (sym->strpos == -1 ? sym->name : NULL,
691 sym->strpos, sym->value, sym->section,
692 sym->is_global ? 2 : 3, 0);
696 static long coff_segbase (long segment)
698 return segment;
701 static void coff_std_filename (char *inname, char *outname, efunc error)
703 strcpy(coff_infile, inname);
704 standard_extension (inname, outname, ".o", error);
707 static void coff_win32_filename (char *inname, char *outname, efunc error)
709 strcpy(coff_infile, inname);
710 standard_extension (inname, outname, ".obj", error);
713 static char *coff_stdmac[] = {
714 "%define __SECT__ [section .text]",
715 "%macro __NASM_CDecl__ 1",
716 "%endmacro",
717 NULL
720 static int coff_set_info(enum geninfo type, char **val)
722 return 0;
724 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
726 #ifdef OF_COFF
728 struct ofmt of_coff = {
729 "COFF (i386) object files (e.g. DJGPP for DOS)",
730 "coff",
731 NULL,
732 null_debug_arr,
733 &null_debug_form,
734 coff_stdmac,
735 coff_std_init,
736 coff_set_info,
737 coff_out,
738 coff_deflabel,
739 coff_section_names,
740 coff_segbase,
741 coff_directives,
742 coff_std_filename,
743 coff_cleanup
746 #endif
748 #ifdef OF_WIN32
750 struct ofmt of_win32 = {
751 "Microsoft Win32 (i386) object files",
752 "win32",
753 NULL,
754 null_debug_arr,
755 &null_debug_form,
756 coff_stdmac,
757 coff_win32_init,
758 coff_set_info,
759 coff_out,
760 coff_deflabel,
761 coff_section_names,
762 coff_segbase,
763 coff_directives,
764 coff_win32_filename,
765 coff_cleanup
768 #endif