elf: Switch writting sections with Elf structs

The target of all this code rework is to
start using general backend engine with
native Elf types behind.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
This commit is contained in:
Cyrill Gorcunov 2017-03-08 21:32:54 +03:00
parent 3a994a92cc
commit c2df71b3ae

View File

@ -2127,39 +2127,47 @@ static void elf_section_header(int name, int type, uint64_t flags,
void *data, bool is_saa, uint64_t datalen, void *data, bool is_saa, uint64_t datalen,
int link, int info, int align, int eltsize) int link, int info, int align, int eltsize)
{ {
union {
Elf32_Shdr shdr32;
Elf64_Shdr shdr64;
} shdr;
elf_sects[elf_nsect].data = data; elf_sects[elf_nsect].data = data;
elf_sects[elf_nsect].len = datalen; elf_sects[elf_nsect].len = datalen;
elf_sects[elf_nsect].is_saa = is_saa; elf_sects[elf_nsect].is_saa = is_saa;
elf_nsect++; elf_nsect++;
if (is_elf32() || is_elfx32()) { if (is_elf32() || is_elfx32()) {
fwriteint32_t((int32_t)name, ofile); shdr.shdr32.sh_name = long_le(name);
fwriteint32_t((int32_t)type, ofile); shdr.shdr32.sh_type = long_le(type);
fwriteint32_t((int32_t)flags, ofile); shdr.shdr32.sh_flags = long_le(flags);
fwriteint32_t(0L, ofile); /* no address, ever, in object files */ shdr.shdr32.sh_addr = 0;
fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile); shdr.shdr32.sh_offset = long_le(type == SHT_NULL ? 0 : elf_foffs);
fwriteint32_t(datalen, ofile); shdr.shdr32.sh_size = long_le(datalen);
if (data) if (data)
elf_foffs += ALIGN(datalen, SEC_FILEALIGN); elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
fwriteint32_t((int32_t)link, ofile); shdr.shdr32.sh_link = long_le(link);
fwriteint32_t((int32_t)info, ofile); shdr.shdr32.sh_info = long_le(info);
fwriteint32_t((int32_t)align, ofile); shdr.shdr32.sh_addralign = long_le(align);
fwriteint32_t((int32_t)eltsize, ofile); shdr.shdr32.sh_entsize = long_le(eltsize);
} else { } else {
nasm_assert(is_elf64()); nasm_assert(is_elf64());
fwriteint32_t((int32_t)name, ofile);
fwriteint32_t((int32_t)type, ofile); shdr.shdr64.sh_name = long_le(name);
fwriteint64_t((int64_t)flags, ofile); shdr.shdr64.sh_type = long_le(type);
fwriteint64_t(0L, ofile); /* no address, ever, in object files */ shdr.shdr64.sh_flags = dlong_le(flags);
fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile); shdr.shdr64.sh_addr = 0;
fwriteint64_t(datalen, ofile); shdr.shdr64.sh_offset = dlong_le(type == SHT_NULL ? 0 : elf_foffs);
shdr.shdr64.sh_size = long_le(datalen);
if (data) if (data)
elf_foffs += ALIGN(datalen, SEC_FILEALIGN); elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
fwriteint32_t((int32_t)link, ofile); shdr.shdr64.sh_link = long_le(link);
fwriteint32_t((int32_t)info, ofile); shdr.shdr64.sh_info = long_le(info);
fwriteint64_t((int64_t)align, ofile); shdr.shdr64.sh_addralign = dlong_le(align);
fwriteint64_t((int64_t)eltsize, ofile); shdr.shdr64.sh_entsize = dlong_le(eltsize);
} }
nasm_write(&shdr, is_elf64() ? sizeof(shdr.shdr64) : sizeof(shdr.shdr32), ofile);
} }
static void elf_write_sections(void) static void elf_write_sections(void)