static void elf_x86_x86_write_proghead(unsigned char **bufpp, elf_offset secthead_addr, unsigned long secthead_count, elf_section_index shstrtab_index) { unsigned char *bufp = *bufpp; unsigned char *buf = bufp-4; YASM_WRITE_8(bufp, ELFCLASS32); /* elf class */ YASM_WRITE_8(bufp, ELFDATA2LSB); /* data encoding :: MSB? */ YASM_WRITE_8(bufp, EV_CURRENT); /* elf version */ while (bufp-buf < EI_NIDENT) /* e_ident padding */ YASM_WRITE_8(bufp, 0); YASM_WRITE_16_L(bufp, ET_REL); /* e_type - object file */ YASM_WRITE_16_L(bufp, EM_386); /* e_machine - or others */ YASM_WRITE_32_L(bufp, EV_CURRENT); /* elf version */ YASM_WRITE_32_L(bufp, 0); /* e_entry exection startaddr */ YASM_WRITE_32_L(bufp, 0); /* e_phoff program header off */ YASM_WRITE_32_L(bufp, secthead_addr); /* e_shoff section header off */ YASM_WRITE_32_L(bufp, 0); /* e_flags also by arch */ YASM_WRITE_16_L(bufp, EHDR32_SIZE); /* e_ehsize */ YASM_WRITE_16_L(bufp, 0); /* e_phentsize */ YASM_WRITE_16_L(bufp, 0); /* e_phnum */ YASM_WRITE_16_L(bufp, SHDR32_SIZE); /* e_shentsize */ YASM_WRITE_16_L(bufp, secthead_count); /* e_shnum */ YASM_WRITE_16_L(bufp, shstrtab_index); /* e_shstrndx */ *bufpp = bufp; }
static void elf_x86_amd64_write_symtab_entry(unsigned char *bufp, elf_symtab_entry *entry, yasm_intnum *value_intn, yasm_intnum *size_intn) { YASM_WRITE_32_L(bufp, entry->name ? entry->name->index : 0); YASM_WRITE_8(bufp, ELF64_ST_INFO(entry->bind, entry->type)); YASM_WRITE_8(bufp, ELF64_ST_OTHER(entry->vis)); if (entry->sect) { elf_secthead *shead = yasm_section_get_data(entry->sect, &elf_section_data); if (!shead) yasm_internal_error(N_("symbol references section without data")); YASM_WRITE_16_L(bufp, shead->index); } else { YASM_WRITE_16_L(bufp, entry->index); } YASM_WRITE_64I_L(bufp, value_intn); YASM_WRITE_64I_L(bufp, size_intn); }
static void elf_x86_amd64_write_proghead(unsigned char **bufpp, elf_offset secthead_addr, unsigned long secthead_count, elf_section_index shstrtab_index) { unsigned char *bufp = *bufpp; unsigned char *buf = bufp-4; YASM_WRITE_8(bufp, ELFCLASS64); /* elf class */ YASM_WRITE_8(bufp, ELFDATA2LSB); /* data encoding :: MSB? */ YASM_WRITE_8(bufp, EV_CURRENT); /* elf version */ YASM_WRITE_8(bufp, ELFOSABI_SYSV); /* os/abi */ YASM_WRITE_8(bufp, 0); /* SYSV v3 ABI=0 */ while (bufp-buf < EI_NIDENT) /* e_ident padding */ YASM_WRITE_8(bufp, 0); YASM_WRITE_16_L(bufp, ET_REL); /* e_type - object file */ YASM_WRITE_16_L(bufp, EM_X86_64); /* e_machine - or others */ YASM_WRITE_32_L(bufp, EV_CURRENT); /* elf version */ YASM_WRITE_64Z_L(bufp, 0); /* e_entry */ YASM_WRITE_64Z_L(bufp, 0); /* e_phoff */ YASM_WRITE_64Z_L(bufp, secthead_addr); /* e_shoff secthead off */ YASM_WRITE_32_L(bufp, 0); /* e_flags */ YASM_WRITE_16_L(bufp, EHDR64_SIZE); /* e_ehsize */ YASM_WRITE_16_L(bufp, 0); /* e_phentsize */ YASM_WRITE_16_L(bufp, 0); /* e_phnum */ YASM_WRITE_16_L(bufp, SHDR64_SIZE); /* e_shentsize */ YASM_WRITE_16_L(bufp, secthead_count); /* e_shnum */ YASM_WRITE_16_L(bufp, shstrtab_index); /* e_shstrndx */ *bufpp = bufp; }
static int bc_org_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, yasm_output_value_func output_value, /*@unused@*/ yasm_output_reloc_func output_reloc) { bytecode_org *org = (bytecode_org *)bc->contents; unsigned long len, i; /* Sanity check for overrun */ if (bc->offset > org->start) { yasm_error_set(YASM_ERROR_GENERAL, N_("ORG overlap with already existing data")); return 1; } len = org->start - bc->offset; for (i=0; i<len; i++) YASM_WRITE_8(*bufp, org->fill); /* XXX: handle more than 8 bit? */ return 0; }
static int xdf_objfmt_output_section(yasm_section *sect, /*@null@*/ void *d) { /*@null@*/ xdf_objfmt_output_info *info = (xdf_objfmt_output_info *)d; /*@dependent@*/ /*@null@*/ xdf_section_data *xsd; long pos; xdf_reloc *reloc; assert(info != NULL); xsd = yasm_section_get_data(sect, &xdf_section_data_cb); assert(xsd != NULL); if (xsd->flags & XDF_SECT_BSS) { /* Don't output BSS sections. * TODO: Check for non-reserve bytecodes? */ pos = 0; /* position = 0 because it's not in the file */ xsd->size = yasm_bc_next_offset(yasm_section_bcs_last(sect)); } else { pos = ftell(info->f); if (pos == -1) { yasm__fatal(N_("could not get file position on output file")); /*@notreached@*/ return 1; } info->sect = sect; info->xsd = xsd; yasm_section_bcs_traverse(sect, info->errwarns, info, xdf_objfmt_output_bytecode); /* Sanity check final section size */ if (xsd->size != yasm_bc_next_offset(yasm_section_bcs_last(sect))) yasm_internal_error( N_("xdf: section computed size did not match actual size")); } /* Empty? Go on to next section */ if (xsd->size == 0) return 0; xsd->scnptr = (unsigned long)pos; /* No relocations to output? Go on to next section */ if (xsd->nreloc == 0) return 0; pos = ftell(info->f); if (pos == -1) { yasm__fatal(N_("could not get file position on output file")); /*@notreached@*/ return 1; } xsd->relptr = (unsigned long)pos; reloc = (xdf_reloc *)yasm_section_relocs_first(sect); while (reloc) { unsigned char *localbuf = info->buf; /*@null@*/ xdf_symrec_data *xsymd; xsymd = yasm_symrec_get_data(reloc->reloc.sym, &xdf_symrec_data_cb); if (!xsymd) yasm_internal_error( N_("xdf: no symbol data for relocated symbol")); yasm_intnum_get_sized(reloc->reloc.addr, localbuf, 4, 32, 0, 0, 0); localbuf += 4; /* address of relocation */ YASM_WRITE_32_L(localbuf, xsymd->index); /* relocated symbol */ if (reloc->base) { xsymd = yasm_symrec_get_data(reloc->base, &xdf_symrec_data_cb); if (!xsymd) yasm_internal_error( N_("xdf: no symbol data for relocated base symbol")); YASM_WRITE_32_L(localbuf, xsymd->index); /* base symbol */ } else { if (reloc->type == XDF_RELOC_WRT) yasm_internal_error( N_("xdf: no base symbol for WRT relocation")); YASM_WRITE_32_L(localbuf, 0); /* no base symbol */ } YASM_WRITE_8(localbuf, reloc->type); /* type of relocation */ YASM_WRITE_8(localbuf, reloc->size); /* size of relocation */ YASM_WRITE_8(localbuf, reloc->shift); /* relocation shift */ YASM_WRITE_8(localbuf, 0); /* flags */ fwrite(info->buf, 16, 1, info->f); reloc = (xdf_reloc *)yasm_section_reloc_next((yasm_reloc *)reloc); } return 0; }