Well I gave my thought a try, didn't fix the issue though it probably did fix an unnoticed issue, here's what the modified code now looks like:
Code:
bytes = init64.e_shoff + ld_sect_headers->data.size;
for ( i = 1; i < init64.e_shnum; ++i ) {
sect[i].sh_offset = prev.sh_offset + prev.sh_size;
prev = sect[i];
sect[i].sh_offset += bytes;
sect[i].sh_addr = sect[i].sh_offset;
}
bytes += prev.sh_offset + prev.sh_size;
exec = (elf64_exec_symbol_t*)ld_exec_symbols->data.data;
for ( i = 1; i < ld_exec_symbols->list.used; ++i ) {
exec[i].st_value += bytes;
}
Will be going to bed soon, not really expecting anyone to actually fix it but one can hope. I'm hoping that fresh eyes will it least notice problems I've yet (incidentally upto this point sizes are set but offsets are ignored, the elf file first has all needed bytes written as plain 0s then lseek64 is used with these offsets to write the data itself)
Just in case anyone wants to see the code where I write the actual bytes:
Code:
sect = GetSpot( ld_sect_headers, sh_exec_content_spot );
bytes = sect->sh_offset + sect->sh_size;
/* Prepare for seeking anywhere need to */
for ( ; bytes > BUFSIZ; bytes -= BUFSIZ ) {
if ( mitsy_write( fd, null_buff, BUFSIZ, NULL ) != BUFSIZ )
goto fail_writing;
}
if ( mitsy_write( fd, null_buff, (size_t)bytes, NULL ) != bytes )
goto fail_writing;
(void)fsync(fd);
/* Fill in Elf Header */
(void)mitsy_seek( fd, 0, SEEK_SET );
(void)mitsy_write( fd, &init64, sizeof(elf64_init_header_t), NULL );
/* Fill in .segments */
(void)mitsy_seek( fd, init64.e_phoff, SEEK_SET );
(void)mitsy_write( fd,
ld_prog_headers->data.data, ld_prog_headers->data.size, NULL );
/* Fill in .sections */
(void)mitsy_seek( fd, init64.e_shoff, SEEK_SET );
(void)mitsy_write( fd,
ld_sect_headers->data.data, ld_sect_headers->data.size, NULL );
/* Fill in .interp */
sect = GetSpot( ld_sect_headers, sh_dynamic_lder_spot );
(void)mitsy_seek( fd, sect->sh_offset, SEEK_SET );
(void)mitsy_write( fd, dynamic_lder, sect->sh_size, NULL );
/* Fill in .shstrtab */
sect = GetSpot( ld_sect_headers, sh_name_strings_spot );
(void)mitsy_seek( fd, sect->sh_offset, SEEK_SET );
(void)mitsy_write( fd,
ld_name_strings->data.data, sect->sh_size, NULL );
/* Fill in .symtab & .text */
sect = GetSpot( ld_sect_headers, sh_exec_symbols_spot );
(void)mitsy_seek( fd, sect->sh_offset, SEEK_SET );
(void)mitsy_write( fd,
ld_exec_symbols->data.data, sect->sh_size, NULL );
spots = (spot_t*)ld_exec_symbols->list.data.data;
exec_spots = (spot_t*)cc_exec_content.list.data.data;
for ( i = 1; i < ld_exec_symbols->list.used; ++i ) {
if ( !spots[i].size ) continue;
exec = GetSpot( ld_exec_symbols, spots[i] );
(void)mitsy_seek( fd, exec->st_value, SEEK_SET );
(void)mitsy_write( fd,
&(cc_exec_content.data.data[exec_spots[i-1].spot]),
exec_spots[i-1].size, NULL );
}
(void)fsync(fd);