NASM 0.91
[nasm/avx512.git] / outcoff.c
blobc3ae712d41d3d2129a7a3c27a08be2b4258b8ec7
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 puts some weird flags into the section header table.
41 * It uses flags 0x80000000 (writable), 0x40000000 (readable) and
42 * 0x20000000 (executable) in the expected combinations, which
43 * standard COFF doesn't seem to bother with, but it also does
44 * something else strange: it also flags code sections as
45 * 0x00500000 and data/bss as 0x00300000. Even Microsoft's
46 * documentation doesn't explain what these things mean. I just go
47 * ahead and use them anyway - it seems to work.
49 * (4) Both standard COFF and Win32 COFF seem to use the DWORD
50 * field directly after the section name in the section header
51 * table for something strange: they store what the address of the
52 * section start point _would_ be, if you laid all the sections end
53 * to end starting at zero. Dunno why. Microsoft's documentation
54 * lists this field as "Virtual Size of Section", which doesn't
55 * seem to fit at all. In fact, Win32 even includes non-linked
56 * sections such as .drectve in this calculation. Not that I can be
57 * bothered with those things anyway.
59 * (5) Standard COFF does something very strange to common
60 * variables: the relocation point for a common variable is as far
61 * _before_ the variable as its size stretches out _after_ it. So
62 * we must fix up common variable references. Win32 seems to be
63 * sensible on this one.
66 /* Flag which version of COFF we are currently outputting. */
67 static int win32;
69 struct Reloc {
70 struct Reloc *next;
71 long address; /* relative to _start_ of section */
72 long symbol; /* symbol number */
73 int relative; /* TRUE or FALSE */
76 struct Symbol {
77 char name[9];
78 long strpos; /* string table position of name */
79 int section; /* section number where it's defined
80 * - in COFF codes, not NASM codes */
81 int is_global; /* is it a global symbol or not? */
82 long value; /* address, or COMMON variable size */
85 static FILE *coffp;
86 static efunc error;
87 static char coff_infile[FILENAME_MAX];
89 struct Section {
90 struct SAA *data;
91 unsigned long len;
92 int nrelocs;
93 long index;
94 struct Reloc *head, **tail;
97 static struct Section stext, sdata;
98 static unsigned long bsslen;
99 static long bssindex;
101 static struct SAA *syms;
102 static unsigned long nsyms;
104 static struct RAA *bsym, *symval;
106 static struct SAA *strs;
107 static unsigned long strslen;
110 * The symbol table contains a double entry for the file name, a
111 * double entry for each of the three sections, and an absolute
112 * symbol referencing address zero, followed by the _real_ symbols.
113 * That's nine extra symbols.
115 #define SYM_INITIAL 9
118 * Symbol table indices we can relocate relative to.
120 #define SYM_ABS_SEG 8
121 #define SYM_TEXT_SEG 2
122 #define SYM_DATA_SEG 4
123 #define SYM_BSS_SEG 6
126 * The section header table ends at this offset: 0x14 for the
127 * header, plus 0x28 for each of three sections.
129 #define COFF_HDRS_END 0x8c
131 static void coff_gen_init(FILE *, efunc);
132 static void coff_sect_write (struct Section *, unsigned char *,
133 unsigned long);
134 static void coff_write (void);
135 static void coff_section_header (char *, long, long, long, long, int, long);
136 static void coff_write_relocs (struct Section *);
137 static void coff_write_symbols (void);
139 static void coff_win32_init(FILE *fp, efunc errfunc, ldfunc ldef) {
140 win32 = TRUE;
141 (void) ldef; /* placate optimisers */
142 coff_gen_init(fp, errfunc);
145 static void coff_std_init(FILE *fp, efunc errfunc, ldfunc ldef) {
146 win32 = FALSE;
147 (void) ldef; /* placate optimisers */
148 coff_gen_init(fp, errfunc);
151 static void coff_gen_init(FILE *fp, efunc errfunc) {
152 coffp = fp;
153 error = errfunc;
154 stext.data = saa_init(1L); stext.head = NULL; stext.tail = &stext.head;
155 sdata.data = saa_init(1L); sdata.head = NULL; sdata.tail = &sdata.head;
156 stext.len = sdata.len = bsslen = 0;
157 stext.nrelocs = sdata.nrelocs = 0;
158 stext.index = seg_alloc();
159 sdata.index = seg_alloc();
160 bssindex = seg_alloc();
161 syms = saa_init((long)sizeof(struct Symbol));
162 nsyms = 0;
163 bsym = raa_init();
164 symval = raa_init();
165 strs = saa_init(1L);
166 strslen = 0;
169 static void coff_cleanup(void) {
170 struct Reloc *r;
172 coff_write();
173 fclose (coffp);
174 saa_free (stext.data);
175 while (stext.head) {
176 r = stext.head;
177 stext.head = stext.head->next;
178 nasm_free (r);
180 saa_free (sdata.data);
181 while (sdata.head) {
182 r = sdata.head;
183 sdata.head = sdata.head->next;
184 nasm_free (r);
186 saa_free (syms);
187 raa_free (bsym);
188 raa_free (symval);
189 saa_free (strs);
192 static long coff_section_names (char *name, int pass, int *bits) {
194 * Default is 32 bits.
196 if (!name)
197 *bits = 32;
199 if (!name)
200 return stext.index;
202 if (!strcmp(name, ".text"))
203 return stext.index;
204 else if (!strcmp(name, ".data"))
205 return sdata.index;
206 else if (!strcmp(name, ".bss"))
207 return bssindex;
208 else
209 return NO_SEG;
212 static void coff_deflabel (char *name, long segment, long offset,
213 int is_global) {
214 int pos = strslen+4;
215 struct Symbol *sym;
217 if (name[0] == '.' && name[1] == '.') {
218 return;
221 if (strlen(name) > 8) {
222 saa_wbytes (strs, name, (long)(1+strlen(name)));
223 strslen += 1+strlen(name);
224 } else
225 pos = -1;
227 sym = saa_wstruct (syms);
229 sym->strpos = pos;
230 if (pos == -1)
231 strcpy (sym->name, name);
232 sym->is_global = !!is_global;
233 if (segment == NO_SEG)
234 sym->section = -1; /* absolute symbol */
235 else if (segment == stext.index)
236 sym->section = 1; /* .text */
237 else if (segment == sdata.index)
238 sym->section = 2; /* .data */
239 else if (segment == bssindex)
240 sym->section = 3; /* .bss */
241 else {
242 sym->section = 0; /* undefined */
243 sym->is_global = TRUE;
245 if (is_global == 2)
246 sym->value = offset;
247 else
248 sym->value = (sym->section == 0 ? 0 : offset);
251 * define the references from external-symbol segment numbers
252 * to these symbol records.
254 if (segment != NO_SEG && segment != stext.index &&
255 segment != sdata.index && segment != bssindex)
256 bsym = raa_write (bsym, segment, nsyms);
258 if (segment != NO_SEG)
259 symval = raa_write (symval, segment, sym->section ? 0 : sym->value);
261 nsyms++;
264 static long coff_add_reloc (struct Section *sect, long segment,
265 int relative) {
266 struct Reloc *r;
268 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
269 sect->tail = &r->next;
270 r->next = NULL;
272 r->address = sect->len;
273 r->symbol = (segment == NO_SEG ? SYM_ABS_SEG :
274 segment == stext.index ? SYM_TEXT_SEG :
275 segment == sdata.index ? SYM_DATA_SEG :
276 segment == bssindex ? SYM_BSS_SEG :
277 raa_read (bsym, segment) + SYM_INITIAL);
278 r->relative = relative;
280 sect->nrelocs++;
283 * Return the fixup for standard COFF common variables.
285 if (r->symbol >= SYM_INITIAL && !win32)
286 return raa_read (symval, segment);
287 else
288 return 0;
291 static void coff_out (long segto, void *data, unsigned long type,
292 long segment, long wrt) {
293 struct Section *s;
294 long realbytes = type & OUT_SIZMASK;
295 unsigned char mydata[4], *p;
297 if (wrt != NO_SEG) {
298 wrt = NO_SEG; /* continue to do _something_ */
299 error (ERR_NONFATAL, "WRT not supported by COFF output formats");
302 type &= OUT_TYPMASK;
305 * handle absolute-assembly (structure definitions)
307 if (segto == NO_SEG) {
308 if (type != OUT_RESERVE)
309 error (ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
310 " space");
311 return;
314 if (segto == stext.index)
315 s = &stext;
316 else if (segto == sdata.index)
317 s = &sdata;
318 else if (segto == bssindex)
319 s = NULL;
320 else {
321 error(ERR_WARNING, "attempt to assemble code in"
322 " segment %d: defaulting to `.text'", segto);
323 s = &stext;
326 if (!s && type != OUT_RESERVE) {
327 error(ERR_WARNING, "attempt to initialise memory in the"
328 " BSS section: ignored");
329 if (type == OUT_REL2ADR)
330 realbytes = 2;
331 else if (type == OUT_REL4ADR)
332 realbytes = 4;
333 bsslen += realbytes;
334 return;
337 if (type == OUT_RESERVE) {
338 if (s) {
339 error(ERR_WARNING, "uninitialised space declared in"
340 " %s section: zeroing",
341 (segto == stext.index ? "code" : "data"));
342 coff_sect_write (s, NULL, realbytes);
343 } else
344 bsslen += realbytes;
345 } else if (type == OUT_RAWDATA) {
346 if (segment != NO_SEG)
347 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
348 coff_sect_write (s, data, realbytes);
349 } else if (type == OUT_ADDRESS) {
350 if (realbytes == 2 && (segment != NO_SEG || wrt != NO_SEG))
351 error(ERR_NONFATAL, "COFF format does not support 16-bit"
352 " relocations");
353 else {
354 long fix = 0;
355 if (segment != NO_SEG || wrt != NO_SEG) {
356 if (wrt != NO_SEG) {
357 error(ERR_NONFATAL, "COFF format does not support"
358 " WRT types");
359 } else if (segment % 2) {
360 error(ERR_NONFATAL, "COFF format does not support"
361 " segment base references");
362 } else
363 fix = coff_add_reloc (s, segment, FALSE);
365 p = mydata;
366 WRITELONG (p, *(long *)data + fix);
367 coff_sect_write (s, mydata, realbytes);
369 } else if (type == OUT_REL2ADR) {
370 error(ERR_NONFATAL, "COFF format does not support 16-bit"
371 " relocations");
372 } else if (type == OUT_REL4ADR) {
373 if (segment == segto)
374 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
375 else if (segment == NO_SEG && win32)
376 error(ERR_NONFATAL, "Win32 COFF does not correctly support"
377 " relative references to absolute addresses");
378 else {
379 long fix = 0;
380 if (segment != NO_SEG && segment % 2) {
381 error(ERR_NONFATAL, "COFF format does not support"
382 " segment base references");
383 } else
384 fix = coff_add_reloc (s, segment, TRUE);
385 p = mydata;
386 if (win32) {
387 WRITELONG (p, *(long*)data + 4 - realbytes + fix);
388 } else {
389 WRITELONG (p, *(long*)data-(realbytes + s->len) + fix);
391 coff_sect_write (s, mydata, 4L);
396 static void coff_sect_write (struct Section *sect,
397 unsigned char *data, unsigned long len) {
398 saa_wbytes (sect->data, data, len);
399 sect->len += len;
402 static int coff_directives (char *directive, char *value, int pass) {
403 return 0;
406 static void coff_write (void) {
407 long textpos, textrelpos, datapos, datarelpos, sympos;
410 * Work out how big the file will get.
412 textpos = COFF_HDRS_END;
413 textrelpos = textpos + stext.len;
414 datapos = textrelpos + stext.nrelocs * 10;
415 datarelpos = datapos + sdata.len;
416 sympos = datarelpos + sdata.nrelocs * 10;
419 * Output the COFF header.
421 fwriteshort (0x14C, coffp); /* MACHINE_i386 */
422 fwriteshort (3, coffp); /* number of sections */
423 fwritelong (time(NULL), coffp); /* time stamp */
424 fwritelong (sympos, coffp);
425 fwritelong (nsyms + SYM_INITIAL, coffp);
426 fwriteshort (0, coffp); /* no optional header */
427 /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */
428 fwriteshort (win32 ? 0 : 0x104, coffp);
431 * Output the section headers.
434 coff_section_header (".text", 0L, stext.len, textpos,
435 textrelpos, stext.nrelocs,
436 (win32 ? 0x60500020L : 0x20L));
437 coff_section_header (".data", stext.len, sdata.len, datapos,
438 datarelpos, sdata.nrelocs,
439 (win32 ? 0xC0300040L : 0x40L));
440 coff_section_header (".bss", stext.len+sdata.len, bsslen, 0L, 0L, 0,
441 (win32 ? 0xC0300080L : 0x80L));
444 * Output the text section, and its relocations.
446 saa_fpwrite (stext.data, coffp);
447 coff_write_relocs (&stext);
450 * Output the data section, and its relocations.
452 saa_fpwrite (sdata.data, coffp);
453 coff_write_relocs (&sdata);
456 * Output the symbol and string tables.
458 coff_write_symbols();
459 fwritelong (strslen+4, coffp); /* length includes length count */
460 saa_fpwrite (strs, coffp);
463 static void coff_section_header (char *name, long vsize,
464 long datalen, long datapos,
465 long relpos, int nrelocs, long flags) {
466 char padname[8];
468 memset (padname, 0, 8);
469 strncpy (padname, name, 8);
470 fwrite (padname, 8, 1, coffp);
471 fwritelong (vsize, coffp);
472 fwritelong (0L, coffp); /* RVA/offset - we ignore */
473 fwritelong (datalen, coffp);
474 fwritelong (datapos, coffp);
475 fwritelong (relpos, coffp);
476 fwritelong (0L, coffp); /* no line numbers - we don't do 'em */
477 fwriteshort (nrelocs, coffp);
478 fwriteshort (0, coffp); /* again, no line numbers */
479 fwritelong (flags, coffp);
482 static void coff_write_relocs (struct Section *s) {
483 struct Reloc *r;
485 for (r = s->head; r; r = r->next) {
486 fwritelong (r->address, coffp);
487 fwritelong (r->symbol, coffp);
489 * Strange: Microsoft's COFF documentation says 0x03 for an
490 * absolute relocation, but both Visual C++ and DJGPP agree
491 * that in fact it's 0x06. I'll use 0x06 until someone
492 * argues.
494 fwriteshort (r->relative ? 0x14 : 0x06, coffp);
498 static void coff_symbol (char *name, long strpos, long value,
499 int section, int type, int aux) {
500 char padname[8];
502 if (name) {
503 memset (padname, 0, 8);
504 strncpy (padname, name, 8);
505 fwrite (padname, 8, 1, coffp);
506 } else {
507 fwritelong (0L, coffp);
508 fwritelong (strpos, coffp);
510 fwritelong (value, coffp);
511 fwriteshort (section, coffp);
512 fwriteshort (0, coffp);
513 fputc (type, coffp);
514 fputc (aux, coffp);
517 static void coff_write_symbols (void) {
518 char filename[18];
519 int i;
522 * The `.file' record, and the file name auxiliary record.
524 coff_symbol (".file", 0L, 0L, -2, 0x67, 1);
525 memset (filename, 0, 18);
526 strncpy (filename, coff_infile, 18);
527 fwrite (filename, 18, 1, coffp);
530 * The section records, with their auxiliaries.
532 memset (filename, 0, 18); /* useful zeroed buffer */
534 coff_symbol (".text", 0L, 0L, 1, 3, 1);
535 fwritelong (stext.len, coffp);
536 fwriteshort (stext.nrelocs, coffp);
537 fwrite (filename, 12, 1, coffp);
538 coff_symbol (".data", 0L, 0L, 2, 3, 1);
539 fwritelong (sdata.len, coffp);
540 fwriteshort (sdata.nrelocs, coffp);
541 fwrite (filename, 12, 1, coffp);
542 coff_symbol (".bss", 0L, 0L, 3, 3, 1);
543 fwritelong (bsslen, coffp);
544 fwrite (filename, 14, 1, coffp);
547 * The absolute symbol, for relative-to-absolute relocations.
549 coff_symbol (".absolut", 0L, 0L, -1, 3, 0);
552 * The real symbols.
554 saa_rewind (syms);
555 for (i=0; i<nsyms; i++) {
556 struct Symbol *sym = saa_rstruct (syms);
557 coff_symbol (sym->strpos == -1 ? sym->name : NULL,
558 sym->strpos, sym->value, sym->section,
559 sym->is_global ? 2 : 3, 0);
563 static long coff_segbase (long segment) {
564 return segment;
567 static void coff_std_filename (char *inname, char *outname, efunc error) {
568 strcpy(coff_infile, inname);
569 standard_extension (inname, outname, ".o", error);
572 static void coff_win32_filename (char *inname, char *outname, efunc error) {
573 strcpy(coff_infile, inname);
574 standard_extension (inname, outname, ".obj", error);
577 #endif /* defined(OF_COFF) || defined(OF_WIN32) */
579 #ifdef OF_COFF
581 struct ofmt of_coff = {
582 "COFF (i386) object files (e.g. DJGPP for DOS)",
583 "coff",
584 coff_std_init,
585 coff_out,
586 coff_deflabel,
587 coff_section_names,
588 coff_segbase,
589 coff_directives,
590 coff_std_filename,
591 coff_cleanup
594 #endif
596 #ifdef OF_WIN32
598 struct ofmt of_win32 = {
599 "Microsoft Win32 (i386) object files",
600 "win32",
601 coff_win32_init,
602 coff_out,
603 coff_deflabel,
604 coff_section_names,
605 coff_segbase,
606 coff_directives,
607 coff_win32_filename,
608 coff_cleanup
611 #endif