static void aout_write(void) { /* * Emit the a.out header. */ /* OMAGIC, M_386 or MID_I386, no flags */ fwritelong (bsd ? 0x07018600 | is_pic : 0x640107L, aoutfp); fwritelong (stext.len, aoutfp); fwritelong (sdata.len, aoutfp); fwritelong (sbss.len, aoutfp); fwritelong (nsyms * 12, aoutfp); /* length of symbol table */ fwritelong (0L, aoutfp); /* object files have no entry point */ fwritelong (stext.nrelocs * 8, aoutfp); /* size of text relocs */ fwritelong (sdata.nrelocs * 8, aoutfp); /* size of data relocs */ /* * Write out the code section and the data section. */ saa_fpwrite (stext.data, aoutfp); saa_fpwrite (sdata.data, aoutfp); /* * Write out the relocations. */ aout_write_relocs (stext.head); aout_write_relocs (sdata.head); /* * Write the symbol table. */ aout_write_syms (); /* * And the string table. */ fwritelong (strslen+4, aoutfp); /* length includes length count */ saa_fpwrite (strs, aoutfp); }
static void elf_write_sections(void) { int i; for (i = 0; i < elf_nsect; i++) if (elf_sects[i].data) { long len = elf_sects[i].len; long reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1; long align = reallen - len; if (elf_sects[i].is_saa) saa_fpwrite(elf_sects[i].data, elffp); else fwrite(elf_sects[i].data, len, 1, elffp); fwrite(align_str, align, 1, elffp); } }
static void as86_write(void) { uint32_t i; int32_t symlen, seglen, segsize; /* * First, go through the symbol records working out how big * each will be. Also fix up BSS references at this time, and * set the flags words up completely. */ symlen = 0; saa_rewind(syms); for (i = 0; i < nsyms; i++) { struct Symbol *sym = saa_rstruct(syms); if (sym->segment == SECT_BSS) sym->segment = SECT_DATA, sym->value += sdata.len; sym->flags |= sym->segment; if (sym->value == 0) sym->flags |= 0 << 14, symlen += 4; else if (sym->value >= 0 && sym->value <= 255) sym->flags |= 1 << 14, symlen += 5; else if (sym->value >= 0 && sym->value <= 65535L) sym->flags |= 2 << 14, symlen += 6; else sym->flags |= 3 << 14, symlen += 8; } /* * Now do the same for the segments, and get the segment size * descriptor word at the same time. */ seglen = segsize = 0; if ((uint32_t)stext.len > 65535L) segsize |= 0x03000000L, seglen += 4; else segsize |= 0x02000000L, seglen += 2; if ((uint32_t)sdata.len > 65535L) segsize |= 0xC0000000L, seglen += 4; else segsize |= 0x80000000L, seglen += 2; /* * Emit the as86 header. */ fwriteint32_t(0x000186A3L, ofile); fputc(0x2A, ofile); fwriteint32_t(27 + symlen + seglen + strslen, ofile); /* header length */ fwriteint32_t(stext.len + sdata.len + bsslen, ofile); fwriteint16_t(strslen, ofile); fwriteint16_t(0, ofile); /* class = revision = 0 */ fwriteint32_t(0x55555555L, ofile); /* segment max sizes: always this */ fwriteint32_t(segsize, ofile); /* segment size descriptors */ if (segsize & 0x01000000L) fwriteint32_t(stext.len, ofile); else fwriteint16_t(stext.len, ofile); if (segsize & 0x40000000L) fwriteint32_t(sdata.len + bsslen, ofile); else fwriteint16_t(sdata.len + bsslen, ofile); fwriteint16_t(nsyms, ofile); /* * Write the symbol table. */ saa_rewind(syms); for (i = 0; i < nsyms; i++) { struct Symbol *sym = saa_rstruct(syms); fwriteint16_t(sym->strpos, ofile); fwriteint16_t(sym->flags, ofile); switch (sym->flags & (3 << 14)) { case 0 << 14: break; case 1 << 14: fputc(sym->value, ofile); break; case 2 << 14: fwriteint16_t(sym->value, ofile); break; case 3 << 14: fwriteint32_t(sym->value, ofile); break; } } /* * Write out the string table. */ saa_fpwrite(strs, ofile); /* * Write the program text. */ as86_reloc_size = -1; as86_write_section(&stext, SECT_TEXT); as86_write_section(&sdata, SECT_DATA); /* * Append the BSS section to the .data section */ if (bsslen > 65535L) { fputc(0x13, ofile); fwriteint32_t(bsslen, ofile); } else if (bsslen > 255) { fputc(0x12, ofile); fwriteint16_t(bsslen, ofile); } else if (bsslen) { fputc(0x11, ofile); fputc(bsslen, ofile); } fputc(0, ofile); /* termination */ }
static void coff_write (void) { long pos, sympos, vsize; int i; /* * Work out how big the file will get. Calculate the start of * the `real' symbols at the same time. */ pos = 0x14 + 0x28 * nsects; initsym = 3; /* two for the file, one absolute */ for (i=0; i<nsects; i++) { if (sects[i]->data) { sects[i]->pos = pos; pos += sects[i]->len; sects[i]->relpos = pos; pos += 10 * sects[i]->nrelocs; } else sects[i]->pos = sects[i]->relpos = 0L; initsym += 2; /* two for each section */ } sympos = pos; /* * Output the COFF header. */ fwriteshort (0x14C, coffp); /* MACHINE_i386 */ fwriteshort (nsects, coffp); /* number of sections */ fwritelong (time(NULL), coffp); /* time stamp */ fwritelong (sympos, coffp); fwritelong (nsyms + initsym, coffp); fwriteshort (0, coffp); /* no optional header */ /* Flags: 32-bit, no line numbers. Win32 doesn't even bother with them. */ fwriteshort (win32 ? 0 : 0x104, coffp); /* * Output the section headers. */ vsize = 0L; for (i=0; i<nsects; i++) { coff_section_header (sects[i]->name, vsize, sects[i]->len, sects[i]->pos, sects[i]->relpos, sects[i]->nrelocs, sects[i]->flags); vsize += sects[i]->len; } /* * Output the sections and their relocations. */ for (i=0; i<nsects; i++) if (sects[i]->data) { saa_fpwrite (sects[i]->data, coffp); coff_write_relocs (sects[i]); } /* * Output the symbol and string tables. */ coff_write_symbols(); fwritelong (strslen+4, coffp); /* length includes length count */ saa_fpwrite (strs, coffp); }