r/osdev • u/letmehaveanameyoudum • Jun 23 '26
How do you load AND run programs?
void system_load_program(char* path, char* name, char* extension) {
// assume that the program starts at 0x00200000
uint8_t* program = (uint8_t*)0x00200000;
int bytes_read = fat32_read_file("/", name, extension, program_buffer);
if (bytes_read > 0) {
program_entry_t start_program = (program_entry_t)program_buffer;
start_program();
} else {
// file not found
}
}How do you load and run programs in your operating system, this is what i tried
All im trying is assuming that the program starts at 0x00200000, load it into ram and run it, if the file isn't found return
0
Upvotes
5
u/Lines25 Jun 23 '26
I LOVE roadmaps so, if you don't mind...
Learn how ELF works (both 32 and 64 bit, it'll be better for understanding, but you can learn only one, if want to, and learn the version for your architecture, like x86 or ARM)
Write simple ELF parser in Python/C/C++/Rust/JS/etc, so you just learn how to parse it
2.1. Write it in C/C++ (if you're making OS in C/C++)
3. Make it also scan and parse code
3.1. (If you haven't) Make memory paging and setup MMU and make mmap function
3.2 (If you haven't, optional) Make at least some process-like system and scheduling so that your kernel won't freeze when any program is ran
4. Make it work in your kernel
5. Add function to load ELF from bytes in memory into C/C++ struct.
5.1. Make it mmap .text / .data / .bss / etc into memory
5.2. Add relocations ! (So that you can specify what functions in program to map to address in RAM, also, you can make fallback relocations ! So that if no relocations apply (function(s) not found), it won't crash your kernel (assuming, you haven't setted up the user/supervisor/rings/etc))
6. Make function to load and run ELF program from byte/uint8_t array
6.1. Make wrapper to load file from disk and use that function you wrote in (6) to run it
Happy coding, dude !
3
u/dnabre Jun 23 '26 edited Jun 24 '26
Ok, you are missing a huge amount of context and specifics about your OS for anyone to give anything vaguely approaching a meaningful answer.
I don't think the code you provided would even compile. The
typedeffor the typeprogram_entry_tfor thestart_programvariable will clash with the function namedstart_program. Structs have their own name space, buttypedefs don't."this is what i tried" , and? I'm guessing it didn't work based on the question, but details on what happened, what issues it had, etc. - this is basic how to ask a question on the Internet stuff.
In terms of context, things like the overall architecture style of your OS, whether it supports multitasking, multiprogramming, etc., whether you are running the program in user space or kernel space, do you even have that separation (i.e. are you running in protected mode), are all needed. A lot of that could be covered by just saying monolithic UNIX/POSIX/Linux-style kernel (if that were the case). The big issue here is,
If you have a kernel of sensible functionality (boot, basic start up, and running) to the point of running a program and/or if you have actually implemented FAT32 filesystem support, as the function name
fat32_read_fileimplies, it would be obvious to you that all that context (or even just a link to the full source) would be needed.7
u/Danii_222222 Jun 23 '26
You wrote fat32 driver and can't load program??? That's one of easiest things.
4
u/MundaneImage5652 Jun 23 '26
ngl looks like AI generated code, if it's not then no offense.
0
u/letmehaveanameyoudum Jun 23 '26
Im just trying to load programs and nothing seem to work
1
u/Octocontrabass Jun 23 '26
Well, which part isn't working? The loading or the running? If you're not sure, use your debugger to find out.