Hi wrozowski,
You need to understand executable program formats considerably better. For instance, programs are linked at particular locations. When you load a program, you need to load each segment in the program to the appropriate memory locations (and potentially zero the end of the segment if the in-memory version is longer than the in-file version). Basically:
Code: Select all
verify_file_is_actually_a_program(file);
for ( segment in file->program_headers )
{
if ( segment is not loadable )
continue;
allocate(segment->load_here to segment->load_here, segment->load_size);
memcpy(segment->load_here, file + segment->offset_in_file, segment->size_in_file);
memset(segment->load_here + segment->size_in_file, 0, segment->load_size - segment->size_in_file);
}
stack st = make_stack_for_program();
registers regs = setup_registers(file->program_entry_point, st);
create_thread(regs);
Now, that's the basic idea and it's wrong. You also need to deal with paging and put the memory inside user-space for this to work. You also need to pay attention to where the program itself is loaded. You also need a scheduler if you wish to jump between running processes. All this requires you understand the ELF used for programs and a number of concepts related to scheduling and memory management. Perhaps reconsider doing this if you don't have a working user-space, a scheduler or memory management. Ideally, you should reach a point where you don't need to ask questions like this because you can figure out the answer from your understanding of executable programs.
And no - the kmalloc and copy method won't work. For one thing, you need to load the memory at the correct location and the linker might disagree with you on that point. Secondly, the programs generated by the linker are not flat binaries they won't work properly if loaded naively. If you somehow manage to reliably convert a program into a flat image and load it at the same place the linker thinks you would, then this would actually work. But flat binaries are really silly and you'll want to immediately implement a real format like ELF.
I recommend learning the ELF executable format and reading the system V ABI (though this might be a bit advanced - but needed). You should also play around with tools like readelf(1) and objdump(1).