NASM 0.98p7
[nasm/avx512.git] / outaout.c
blobb82109a966d50fe339c9b5c679366c0b3563ae5d
1 /* outaout.c output routines for the Netwide Assembler to produce
2 * Linux a.out object files
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>
15 #include "nasm.h"
16 #include "nasmlib.h"
17 #include "outform.h"
19 #if defined OF_AOUT || defined OF_AOUTB
21 #define RELTYPE_ABSOLUTE 0x00
22 #define RELTYPE_RELATIVE 0x01
23 #define RELTYPE_GOTPC 0x01 /* no explicit GOTPC in a.out */
24 #define RELTYPE_GOTOFF 0x10
25 #define RELTYPE_GOT 0x10 /* distinct from GOTOFF bcos sym not sect */
26 #define RELTYPE_PLT 0x21
27 #define RELTYPE_SYMFLAG 0x08
29 struct Reloc {
30 struct Reloc *next;
31 long address; /* relative to _start_ of section */
32 long symbol; /* symbol number or -ve section id */
33 int bytes; /* 2 or 4 */
34 int reltype; /* see above */
37 struct Symbol {
38 long strpos; /* string table position of name */
39 int type; /* symbol type - see flags below */
40 long value; /* address, or COMMON variable size */
41 long size; /* size for data or function exports */
42 long segment; /* back-reference used by gsym_reloc */
43 struct Symbol *next; /* list of globals in each section */
44 struct Symbol *nextfwd; /* list of unresolved-size symbols */
45 char *name; /* for unresolved-size symbols */
46 long symnum; /* index into symbol table */
50 * Section IDs - used in Reloc.symbol when negative, and in
51 * Symbol.type when positive.
53 #define SECT_ABS 2 /* absolute value */
54 #define SECT_TEXT 4 /* text section */
55 #define SECT_DATA 6 /* data section */
56 #define SECT_BSS 8 /* bss section */
57 #define SECT_MASK 0xE /* mask out any of the above */
60 * More flags used in Symbol.type.
62 #define SYM_GLOBAL 1 /* it's a global symbol */
63 #define SYM_DATA 0x100 /* used for shared libs */
64 #define SYM_FUNCTION 0x200 /* used for shared libs */
65 #define SYM_WITH_SIZE 0x4000 /* not output; internal only */
68 * Bit more explanation of symbol types: SECT_xxx denotes a local
69 * symbol. SECT_xxx|SYM_GLOBAL denotes a global symbol, defined in
70 * this module. Just SYM_GLOBAL, with zero value, denotes an
71 * external symbol referenced in this module. And just SYM_GLOBAL,
72 * but with a non-zero value, declares a C `common' variable, of
73 * size `value'.
76 struct Section {
77 struct SAA *data;
78 unsigned long len, size, nrelocs;
79 long index;
80 struct Reloc *head, **tail;
81 struct Symbol *gsyms, *asym;
84 static struct Section stext, sdata, sbss;
86 static struct SAA *syms;
87 static unsigned long nsyms;
89 static struct RAA *bsym;
91 static struct SAA *strs;
92 static unsigned long strslen;
94 static struct Symbol *fwds;
96 static FILE *aoutfp;
97 static efunc error;
98 static evalfunc evaluate;
100 static int bsd;
101 static int is_pic;
103 static void aout_write(void);
104 static void aout_write_relocs(struct Reloc *);
105 static void aout_write_syms(void);
106 static void aout_sect_write(struct Section *, unsigned char *, unsigned long);
107 static void aout_pad_sections(void);
108 static void aout_fixup_relocs(struct Section *);
111 * Special section numbers which are used to define special
112 * symbols, which can be used with WRT to provide PIC relocation
113 * types.
115 static long aout_gotpc_sect, aout_gotoff_sect;
116 static long aout_got_sect, aout_plt_sect;
117 static long aout_sym_sect;
119 static void aoutg_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
121 aoutfp = fp;
122 error = errfunc;
123 evaluate = eval;
124 (void) ldef; /* placate optimisers */
125 stext.data = saa_init(1L); stext.head = NULL; stext.tail = &stext.head;
126 sdata.data = saa_init(1L); sdata.head = NULL; sdata.tail = &sdata.head;
127 stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
128 stext.nrelocs = sdata.nrelocs = 0;
129 stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
130 stext.index = seg_alloc();
131 sdata.index = seg_alloc();
132 sbss.index = seg_alloc();
133 stext.asym = sdata.asym = sbss.asym = NULL;
134 syms = saa_init((long)sizeof(struct Symbol));
135 nsyms = 0;
136 bsym = raa_init();
137 strs = saa_init(1L);
138 strslen = 0;
139 fwds = NULL;
142 #ifdef OF_AOUT
144 static void aout_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
146 bsd = FALSE;
147 aoutg_init (fp, errfunc, ldef, eval);
149 aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
150 aout_plt_sect = aout_sym_sect = NO_SEG;
153 #endif
155 #ifdef OF_AOUTB
157 extern struct ofmt of_aoutb;
159 static void aoutb_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
161 bsd = TRUE;
162 aoutg_init (fp, errfunc, ldef, eval);
164 is_pic = 0x00; /* may become 0x40 */
166 aout_gotpc_sect = seg_alloc();
167 ldef("..gotpc", aout_gotpc_sect+1, 0L, NULL, FALSE,FALSE,&of_aoutb,error);
168 aout_gotoff_sect = seg_alloc();
169 ldef("..gotoff", aout_gotoff_sect+1, 0L,NULL,FALSE,FALSE,&of_aoutb,error);
170 aout_got_sect = seg_alloc();
171 ldef("..got", aout_got_sect+1, 0L, NULL, FALSE,FALSE,&of_aoutb,error);
172 aout_plt_sect = seg_alloc();
173 ldef("..plt", aout_plt_sect+1, 0L, NULL, FALSE,FALSE,&of_aoutb,error);
174 aout_sym_sect = seg_alloc();
175 ldef("..sym", aout_sym_sect+1, 0L, NULL, FALSE,FALSE,&of_aoutb,error);
178 #endif
180 static void aout_cleanup(int debuginfo)
182 struct Reloc *r;
184 (void) debuginfo;
186 aout_pad_sections();
187 aout_fixup_relocs(&stext);
188 aout_fixup_relocs(&sdata);
189 aout_write();
190 fclose (aoutfp);
191 saa_free (stext.data);
192 while (stext.head) {
193 r = stext.head;
194 stext.head = stext.head->next;
195 nasm_free (r);
197 saa_free (sdata.data);
198 while (sdata.head) {
199 r = sdata.head;
200 sdata.head = sdata.head->next;
201 nasm_free (r);
203 saa_free (syms);
204 raa_free (bsym);
205 saa_free (strs);
208 static long aout_section_names (char *name, int pass, int *bits)
211 * Default to 32 bits.
213 if (!name)
214 *bits = 32;
216 if (!name)
217 return stext.index;
219 if (!strcmp(name, ".text"))
220 return stext.index;
221 else if (!strcmp(name, ".data"))
222 return sdata.index;
223 else if (!strcmp(name, ".bss"))
224 return sbss.index;
225 else
226 return NO_SEG;
229 static void aout_deflabel (char *name, long segment, long offset,
230 int is_global, char *special)
232 int pos = strslen+4;
233 struct Symbol *sym;
234 int special_used = FALSE;
236 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
238 * This is a NASM special symbol. We never allow it into
239 * the a.out symbol table, even if it's a valid one. If it
240 * _isn't_ a valid one, we should barf immediately.
242 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
243 strcmp(name, "..got") && strcmp(name, "..plt") &&
244 strcmp(name, "..sym"))
245 error (ERR_NONFATAL, "unrecognised special symbol `%s'", name);
246 return;
249 if (is_global == 3) {
250 struct Symbol **s;
252 * Fix up a forward-reference symbol size from the first
253 * pass.
255 for (s = &fwds; *s; s = &(*s)->nextfwd)
256 if (!strcmp((*s)->name, name)) {
257 struct tokenval tokval;
258 expr *e;
259 char *p = special;
261 while (*p && !isspace(*p)) p++;
262 while (*p && isspace(*p)) p++;
263 stdscan_reset();
264 stdscan_bufptr = p;
265 tokval.t_type = TOKEN_INVALID;
266 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
267 if (e) {
268 if (!is_simple(e))
269 error (ERR_NONFATAL, "cannot use relocatable"
270 " expression as symbol size");
271 else
272 (*s)->size = reloc_value(e);
276 * Remove it from the list of unresolved sizes.
278 nasm_free ((*s)->name);
279 *s = (*s)->nextfwd;
280 return;
282 return; /* it wasn't an important one */
285 saa_wbytes (strs, name, (long)(1+strlen(name)));
286 strslen += 1+strlen(name);
288 sym = saa_wstruct (syms);
290 sym->strpos = pos;
291 sym->type = is_global ? SYM_GLOBAL : 0;
292 sym->segment = segment;
293 if (segment == NO_SEG)
294 sym->type |= SECT_ABS;
295 else if (segment == stext.index) {
296 sym->type |= SECT_TEXT;
297 if (is_global) {
298 sym->next = stext.gsyms;
299 stext.gsyms = sym;
300 } else if (!stext.asym)
301 stext.asym = sym;
302 } else if (segment == sdata.index) {
303 sym->type |= SECT_DATA;
304 if (is_global) {
305 sym->next = sdata.gsyms;
306 sdata.gsyms = sym;
307 } else if (!sdata.asym)
308 sdata.asym = sym;
309 } else if (segment == sbss.index) {
310 sym->type |= SECT_BSS;
311 if (is_global) {
312 sym->next = sbss.gsyms;
313 sbss.gsyms = sym;
314 } else if (!sbss.asym)
315 sbss.asym = sym;
316 } else
317 sym->type = SYM_GLOBAL;
318 if (is_global == 2)
319 sym->value = offset;
320 else
321 sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
323 if (is_global && sym->type != SYM_GLOBAL) {
325 * Global symbol exported _from_ this module. We must check
326 * the special text for type information.
329 if (special) {
330 int n = strcspn(special, " ");
332 if (!nasm_strnicmp(special, "function", n))
333 sym->type |= SYM_FUNCTION;
334 else if (!nasm_strnicmp(special, "data", n) ||
335 !nasm_strnicmp(special, "object", n))
336 sym->type |= SYM_DATA;
337 else
338 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
339 n, special);
340 if (special[n]) {
341 struct tokenval tokval;
342 expr *e;
343 int fwd = FALSE;
345 if (!bsd) {
346 error(ERR_NONFATAL, "Linux a.out does not support"
347 " symbol size information");
348 } else {
349 while (special[n] && isspace(special[n]))
350 n++;
352 * We have a size expression; attempt to
353 * evaluate it.
355 sym->type |= SYM_WITH_SIZE;
356 stdscan_reset();
357 stdscan_bufptr = special+n;
358 tokval.t_type = TOKEN_INVALID;
359 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error, NULL);
360 if (fwd) {
361 sym->nextfwd = fwds;
362 fwds = sym;
363 sym->name = nasm_strdup(name);
364 } else if (e) {
365 if (!is_simple(e))
366 error (ERR_NONFATAL, "cannot use relocatable"
367 " expression as symbol size");
368 else
369 sym->size = reloc_value(e);
373 special_used = TRUE;
378 * define the references from external-symbol segment numbers
379 * to these symbol records.
381 if (segment != NO_SEG && segment != stext.index &&
382 segment != sdata.index && segment != sbss.index)
383 bsym = raa_write (bsym, segment, nsyms);
384 sym->symnum = nsyms;
386 nsyms++;
387 if (sym->type & SYM_WITH_SIZE)
388 nsyms++; /* and another for the size */
390 if (special && !special_used)
391 error(ERR_NONFATAL, "no special symbol features supported here");
394 static void aout_add_reloc (struct Section *sect, long segment,
395 int reltype, int bytes)
397 struct Reloc *r;
399 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
400 sect->tail = &r->next;
401 r->next = NULL;
403 r->address = sect->len;
404 r->symbol = (segment == NO_SEG ? -SECT_ABS :
405 segment == stext.index ? -SECT_TEXT :
406 segment == sdata.index ? -SECT_DATA :
407 segment == sbss.index ? -SECT_BSS :
408 raa_read(bsym, segment));
409 r->reltype = reltype;
410 if (r->symbol >= 0)
411 r->reltype |= RELTYPE_SYMFLAG;
412 r->bytes = bytes;
414 sect->nrelocs++;
418 * This routine deals with ..got and ..sym relocations: the more
419 * complicated kinds. In shared-library writing, some relocations
420 * with respect to global symbols must refer to the precise symbol
421 * rather than referring to an offset from the base of the section
422 * _containing_ the symbol. Such relocations call to this routine,
423 * which searches the symbol list for the symbol in question.
425 * RELTYPE_GOT references require the _exact_ symbol address to be
426 * used; RELTYPE_ABSOLUTE references can be at an offset from the
427 * symbol. The boolean argument `exact' tells us this.
429 * Return value is the adjusted value of `addr', having become an
430 * offset from the symbol rather than the section. Should always be
431 * zero when returning from an exact call.
433 * Limitation: if you define two symbols at the same place,
434 * confusion will occur.
436 * Inefficiency: we search, currently, using a linked list which
437 * isn't even necessarily sorted.
439 static long aout_add_gsym_reloc (struct Section *sect,
440 long segment, long offset,
441 int type, int bytes, int exact)
443 struct Symbol *sym, *sm, *shead;
444 struct Reloc *r;
447 * First look up the segment to find whether it's text, data,
448 * bss or an external symbol.
450 shead = NULL;
451 if (segment == stext.index)
452 shead = stext.gsyms;
453 else if (segment == sdata.index)
454 shead = sdata.gsyms;
455 else if (segment == sbss.index)
456 shead = sbss.gsyms;
457 if (!shead) {
458 if (exact && offset != 0)
459 error (ERR_NONFATAL, "unable to find a suitable global symbol"
460 " for this reference");
461 else
462 aout_add_reloc (sect, segment, type, bytes);
463 return offset;
466 if (exact) {
468 * Find a symbol pointing _exactly_ at this one.
470 for (sym = shead; sym; sym = sym->next)
471 if (sym->value == offset)
472 break;
473 } else {
475 * Find the nearest symbol below this one.
477 sym = NULL;
478 for (sm = shead; sm; sm = sm->next)
479 if (sm->value <= offset && (!sym || sm->value > sym->value))
480 sym = sm;
482 if (!sym && exact) {
483 error (ERR_NONFATAL, "unable to find a suitable global symbol"
484 " for this reference");
485 return 0;
488 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
489 sect->tail = &r->next;
490 r->next = NULL;
492 r->address = sect->len;
493 r->symbol = sym->symnum;
494 r->reltype = type | RELTYPE_SYMFLAG;
495 r->bytes = bytes;
497 sect->nrelocs++;
499 return offset - sym->value;
503 * This routine deals with ..gotoff relocations. These _must_ refer
504 * to a symbol, due to a perversity of *BSD's PIC implementation,
505 * and it must be a non-global one as well; so we store `asym', the
506 * first nonglobal symbol defined in each section, and always work
507 * from that. Relocation type is always RELTYPE_GOTOFF.
509 * Return value is the adjusted value of `addr', having become an
510 * offset from the `asym' symbol rather than the section.
512 static long aout_add_gotoff_reloc (struct Section *sect, long segment,
513 long offset, int bytes)
515 struct Reloc *r;
516 struct Symbol *asym;
519 * First look up the segment to find whether it's text, data,
520 * bss or an external symbol.
522 asym = NULL;
523 if (segment == stext.index)
524 asym = stext.asym;
525 else if (segment == sdata.index)
526 asym = sdata.asym;
527 else if (segment == sbss.index)
528 asym = sbss.asym;
529 if (!asym)
530 error (ERR_NONFATAL, "`..gotoff' relocations require a non-global"
531 " symbol in the section");
533 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
534 sect->tail = &r->next;
535 r->next = NULL;
537 r->address = sect->len;
538 r->symbol = asym->symnum;
539 r->reltype = RELTYPE_GOTOFF;
540 r->bytes = bytes;
542 sect->nrelocs++;
544 return offset - asym->value;
547 static void aout_out (long segto, void *data, unsigned long type,
548 long segment, long wrt)
550 struct Section *s;
551 long realbytes = type & OUT_SIZMASK;
552 long addr;
553 unsigned char mydata[4], *p;
555 type &= OUT_TYPMASK;
558 * handle absolute-assembly (structure definitions)
560 if (segto == NO_SEG) {
561 if (type != OUT_RESERVE)
562 error (ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
563 " space");
564 return;
567 if (segto == stext.index)
568 s = &stext;
569 else if (segto == sdata.index)
570 s = &sdata;
571 else if (segto == sbss.index)
572 s = NULL;
573 else {
574 error(ERR_WARNING, "attempt to assemble code in"
575 " segment %d: defaulting to `.text'", segto);
576 s = &stext;
579 if (!s && type != OUT_RESERVE) {
580 error(ERR_WARNING, "attempt to initialise memory in the"
581 " BSS section: ignored");
582 if (type == OUT_REL2ADR)
583 realbytes = 2;
584 else if (type == OUT_REL4ADR)
585 realbytes = 4;
586 sbss.len += realbytes;
587 return;
590 if (type == OUT_RESERVE) {
591 if (s) {
592 error(ERR_WARNING, "uninitialised space declared in"
593 " %s section: zeroing",
594 (segto == stext.index ? "code" : "data"));
595 aout_sect_write (s, NULL, realbytes);
596 } else
597 sbss.len += realbytes;
598 } else if (type == OUT_RAWDATA) {
599 if (segment != NO_SEG)
600 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
601 aout_sect_write (s, data, realbytes);
602 } else if (type == OUT_ADDRESS) {
603 addr = *(long *)data;
604 if (segment != NO_SEG) {
605 if (segment % 2) {
606 error(ERR_NONFATAL, "a.out format does not support"
607 " segment base references");
608 } else {
609 if (wrt == NO_SEG) {
610 aout_add_reloc (s, segment, RELTYPE_ABSOLUTE, realbytes);
611 } else if (!bsd) {
612 error (ERR_NONFATAL, "Linux a.out format does not support"
613 " any use of WRT");
614 wrt = NO_SEG; /* we can at least _try_ to continue */
615 } else if (wrt == aout_gotpc_sect+1) {
616 is_pic = 0x40;
617 aout_add_reloc (s, segment, RELTYPE_GOTPC, realbytes);
618 } else if (wrt == aout_gotoff_sect+1) {
619 is_pic = 0x40;
620 addr = aout_add_gotoff_reloc (s, segment,
621 addr, realbytes);
622 } else if (wrt == aout_got_sect+1) {
623 is_pic = 0x40;
624 addr = aout_add_gsym_reloc (s, segment, addr, RELTYPE_GOT,
625 realbytes, TRUE);
626 } else if (wrt == aout_sym_sect+1) {
627 addr = aout_add_gsym_reloc (s, segment, addr,
628 RELTYPE_ABSOLUTE, realbytes,
629 FALSE);
630 } else if (wrt == aout_plt_sect+1) {
631 is_pic = 0x40;
632 error(ERR_NONFATAL, "a.out format cannot produce non-PC-"
633 "relative PLT references");
634 } else {
635 error (ERR_NONFATAL, "a.out format does not support this"
636 " use of WRT");
637 wrt = NO_SEG; /* we can at least _try_ to continue */
641 p = mydata;
642 if (realbytes == 2)
643 WRITESHORT (p, addr);
644 else
645 WRITELONG (p, addr);
646 aout_sect_write (s, mydata, realbytes);
647 } else if (type == OUT_REL2ADR) {
648 if (segment == segto)
649 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
650 if (segment != NO_SEG && segment % 2) {
651 error(ERR_NONFATAL, "a.out format does not support"
652 " segment base references");
653 } else {
654 if (wrt == NO_SEG) {
655 aout_add_reloc (s, segment, RELTYPE_RELATIVE, 2);
656 } else if (!bsd) {
657 error (ERR_NONFATAL, "Linux a.out format does not support"
658 " any use of WRT");
659 wrt = NO_SEG; /* we can at least _try_ to continue */
660 } else if (wrt == aout_plt_sect+1) {
661 is_pic = 0x40;
662 aout_add_reloc (s, segment, RELTYPE_PLT, 2);
663 } else if (wrt == aout_gotpc_sect+1 ||
664 wrt == aout_gotoff_sect+1 ||
665 wrt == aout_got_sect+1) {
666 error(ERR_NONFATAL, "a.out format cannot produce PC-"
667 "relative GOT references");
668 } else {
669 error (ERR_NONFATAL, "a.out format does not support this"
670 " use of WRT");
671 wrt = NO_SEG; /* we can at least _try_ to continue */
674 p = mydata;
675 WRITESHORT (p, *(long*)data-(realbytes + s->len));
676 aout_sect_write (s, mydata, 2L);
677 } else if (type == OUT_REL4ADR) {
678 if (segment == segto)
679 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
680 if (segment != NO_SEG && segment % 2) {
681 error(ERR_NONFATAL, "a.out format does not support"
682 " segment base references");
683 } else {
684 if (wrt == NO_SEG) {
685 aout_add_reloc (s, segment, RELTYPE_RELATIVE, 4);
686 } else if (!bsd) {
687 error (ERR_NONFATAL, "Linux a.out format does not support"
688 " any use of WRT");
689 wrt = NO_SEG; /* we can at least _try_ to continue */
690 } else if (wrt == aout_plt_sect+1) {
691 is_pic = 0x40;
692 aout_add_reloc (s, segment, RELTYPE_PLT, 4);
693 } else if (wrt == aout_gotpc_sect+1 ||
694 wrt == aout_gotoff_sect+1 ||
695 wrt == aout_got_sect+1) {
696 error(ERR_NONFATAL, "a.out format cannot produce PC-"
697 "relative GOT references");
698 } else {
699 error (ERR_NONFATAL, "a.out format does not support this"
700 " use of WRT");
701 wrt = NO_SEG; /* we can at least _try_ to continue */
704 p = mydata;
705 WRITELONG (p, *(long*)data-(realbytes + s->len));
706 aout_sect_write (s, mydata, 4L);
710 static void aout_pad_sections(void)
712 static unsigned char pad[] = { 0x90, 0x90, 0x90, 0x90 };
714 * Pad each of the text and data sections with NOPs until their
715 * length is a multiple of four. (NOP == 0x90.) Also increase
716 * the length of the BSS section similarly.
718 aout_sect_write (&stext, pad, (-(long)stext.len) & 3);
719 aout_sect_write (&sdata, pad, (-(long)sdata.len) & 3);
720 sbss.len = (sbss.len + 3) & ~3;
724 * a.out files have the curious property that all references to
725 * things in the data or bss sections are done by addresses which
726 * are actually relative to the start of the _text_ section, in the
727 * _file_. (No relation to what happens after linking. No idea why
728 * this should be so. It's very strange.) So we have to go through
729 * the relocation table, _after_ the final size of each section is
730 * known, and fix up the relocations pointed to.
732 static void aout_fixup_relocs(struct Section *sect)
734 struct Reloc *r;
736 saa_rewind (sect->data);
737 for (r = sect->head; r; r = r->next) {
738 unsigned char *p, *q, blk[4];
739 long l;
741 saa_fread (sect->data, r->address, blk, (long)r->bytes);
742 p = q = blk;
743 l = *p++;
744 if (r->bytes > 1) {
745 l += ((long)*p++) << 8;
746 if (r->bytes == 4) {
747 l += ((long)*p++) << 16;
748 l += ((long)*p++) << 24;
751 if (r->symbol == -SECT_DATA)
752 l += stext.len;
753 else if (r->symbol == -SECT_BSS)
754 l += stext.len + sdata.len;
755 if (r->bytes == 4)
756 WRITELONG(q, l);
757 else if (r->bytes == 2)
758 WRITESHORT(q, l);
759 else
760 *q++ = l & 0xFF;
761 saa_fwrite (sect->data, r->address, blk, (long)r->bytes);
765 static void aout_write(void)
768 * Emit the a.out header.
770 /* OMAGIC, M_386 or MID_I386, no flags */
771 fwritelong (bsd ? 0x07018600 | is_pic : 0x640107L, aoutfp);
772 fwritelong (stext.len, aoutfp);
773 fwritelong (sdata.len, aoutfp);
774 fwritelong (sbss.len, aoutfp);
775 fwritelong (nsyms * 12, aoutfp); /* length of symbol table */
776 fwritelong (0L, aoutfp); /* object files have no entry point */
777 fwritelong (stext.nrelocs * 8, aoutfp); /* size of text relocs */
778 fwritelong (sdata.nrelocs * 8, aoutfp); /* size of data relocs */
781 * Write out the code section and the data section.
783 saa_fpwrite (stext.data, aoutfp);
784 saa_fpwrite (sdata.data, aoutfp);
787 * Write out the relocations.
789 aout_write_relocs (stext.head);
790 aout_write_relocs (sdata.head);
793 * Write the symbol table.
795 aout_write_syms ();
798 * And the string table.
800 fwritelong (strslen+4, aoutfp); /* length includes length count */
801 saa_fpwrite (strs, aoutfp);
804 static void aout_write_relocs (struct Reloc *r)
806 while (r) {
807 unsigned long word2;
809 fwritelong (r->address, aoutfp);
811 if (r->symbol >= 0)
812 word2 = r->symbol;
813 else
814 word2 = -r->symbol;
815 word2 |= r->reltype << 24;
816 word2 |= (r->bytes == 1 ? 0 :
817 r->bytes == 2 ? 0x2000000L : 0x4000000L);
818 fwritelong (word2, aoutfp);
820 r = r->next;
824 static void aout_write_syms (void)
826 int i;
828 saa_rewind (syms);
829 for (i=0; i<nsyms; i++) {
830 struct Symbol *sym = saa_rstruct(syms);
831 fwritelong (sym->strpos, aoutfp);
832 fwritelong ((long)sym->type & ~SYM_WITH_SIZE, aoutfp);
834 * Fix up the symbol value now we know the final section
835 * sizes.
837 if ((sym->type & SECT_MASK) == SECT_DATA)
838 sym->value += stext.len;
839 if ((sym->type & SECT_MASK) == SECT_BSS)
840 sym->value += stext.len + sdata.len;
841 fwritelong (sym->value, aoutfp);
843 * Output a size record if necessary.
845 if (sym->type & SYM_WITH_SIZE) {
846 fwritelong(sym->strpos, aoutfp);
847 fwritelong(0x0DL, aoutfp); /* special value: means size */
848 fwritelong(sym->size, aoutfp);
849 i++; /* use up another of `nsyms' */
854 static void aout_sect_write (struct Section *sect,
855 unsigned char *data, unsigned long len)
857 saa_wbytes (sect->data, data, len);
858 sect->len += len;
861 static long aout_segbase (long segment)
863 return segment;
866 static int aout_directive (char *directive, char *value, int pass)
868 return 0;
871 static void aout_filename (char *inname, char *outname, efunc error)
873 standard_extension (inname, outname, ".o", error);
876 static char *aout_stdmac[] = {
877 "%define __SECT__ [section .text]",
878 "%macro __NASM_CDecl__ 1",
879 "%endmacro",
880 NULL
883 static int aout_set_info(enum geninfo type, char **val)
885 return 0;
887 #endif /* OF_AOUT || OF_AOUTB */
889 #ifdef OF_AOUT
891 struct ofmt of_aout = {
892 "Linux a.out object files",
893 "aout",
894 NULL,
895 null_debug_arr,
896 &null_debug_form,
897 aout_stdmac,
898 aout_init,
899 aout_set_info,
900 aout_out,
901 aout_deflabel,
902 aout_section_names,
903 aout_segbase,
904 aout_directive,
905 aout_filename,
906 aout_cleanup
909 #endif
911 #ifdef OF_AOUTB
913 struct ofmt of_aoutb = {
914 "NetBSD/FreeBSD a.out object files",
915 "aoutb",
916 NULL,
917 null_debug_arr,
918 &null_debug_form,
919 aout_stdmac,
920 aoutb_init,
921 aout_set_info,
922 aout_out,
923 aout_deflabel,
924 aout_section_names,
925 aout_segbase,
926 aout_directive,
927 aout_filename,
928 aout_cleanup
931 #endif