Page 1 of 1

Executables

Posted: Thu Feb 14, 2008 10:37 am
by Lunatic
Good day.

I am writing my kernel and so far I got some basic command line that supports FAT16. The command will be integrated into the kernel itself (I know it's not a very good idea but for starters it's ok).
Now, I decided that it's about time to make my kernel support any kind of executable files to actually be able to do some tasks. The problem is I have no idea where and how to start. Not that I do not know which format to choose or anything but I don't even know how to actually implement the whole thing. I tried reading some articles but they were not really what I was looking for. Any help is appreciated.

Thanks!

Posted: Thu Feb 14, 2008 10:52 am
by AJ
Hi,

Although there are several formats about, I would recommend looking at ELF due to its widespread use and the fact that there is a huge amount of documentation about. I found that the best way to learn how to implement this is to read the official documentation which is written in a very helpful manner.

Of course, if your toolchain does not support ELF, that's not much help to you, but you may then like to look at building a cross compiler. With ELF, the basic system for program loading goes something like:

1) Load the ELF header, which is at the start of the ELF file.
2) Read the program headers, which are pointed to by the ELF header.
3) For each program header of type PT_LOAD, copy the section over to the location in virtual RAM and zero the rest of the executable.
4) Jump to the entry point (contained in the ELF header).

Dynamic linking is a little more complex, but not much. At a more basic level, are you intending each program to run in its own memory space? Do you already have multitasking? What are the basic design goals of your os?

Cheers,
Adam

Posted: Thu Feb 14, 2008 11:05 am
by gzaloprgm
Hello Lunatic.

I agree with AJ about elf format documentations, but you could also load plain binary files, ms dos com files, windows pe files, just for compatibility, or if you'r using a compiler like MSVC 2005 which can only output in pe, etc.

Look at Mike's tutorials in which the bootloader loads an exe file from a fat12 floppy (real mode, but you can adapt it)

Also this site seems good http://www.wotsit.org/list.asp?fc=5#.

Cheers,
Gonzalo

Posted: Fri Feb 15, 2008 9:54 am
by Lunatic
AJ wrote:At a more basic level, are you intending each program to run in its own memory space? Do you already have multitasking? What are the basic design goals of your os?


Yes, I am intending to make programs run their own memory. I do not have multitasking yet. All I got is a very basic console. Now I wish to make memory allocations, multitasking and also executables. The design goals are personal education. I wish to make a basic working kernel and by that I expect to learn a lot about OS'es.

Thanks for the info, guys, I will probably try to support ELF format and some others. Of course, when I'm done making preparations.

Posted: Mon Mar 03, 2008 8:53 am
by bucket_brigade
AJ wrote:Hi,

Although there are several formats about, I would recommend looking at ELF due to its widespread use and the fact that there is a huge amount of documentation about. I found that the best way to learn how to implement this is to read the official documentation which is written in a very helpful manner.

Of course, if your toolchain does not support ELF, that's not much help to you, but you may then like to look at building a cross compiler. With ELF, the basic system for program loading goes something like:

1) Load the ELF header, which is at the start of the ELF file.
2) Read the program headers, which are pointed to by the ELF header.
3) For each program header of type PT_LOAD, copy the section over to the location in virtual RAM and zero the rest of the executable.
4) Jump to the entry point (contained in the ELF header).

Dynamic linking is a little more complex, but not much. At a more basic level, are you intending each program to run in its own memory space? Do you already have multitasking? What are the basic design goals of your os?

Cheers,
Adam
Hello. Concerning copying each program header of type PT_LOAD to the designated location in virtual RAM... What are the general guidelines of how to set up paging tables to do that? I read some tutorials and skimmed through some books but i still don't understand.

Posted: Mon Mar 03, 2008 9:11 am
by JamesM
Hi.
What are the general guidelines of how to set up paging tables to do that?
These are two very seperate areas of the kernel. The virtual memory manager should have a function that, given a virtual address and a length, can create/modify page tables to map a certain area of virtual memory to some physical memory (obtained from the physical memory manager).

Then, your ELF loader calls your virtual memory manager, saying "I want a region of memory made at x with length y", and loads the ELF data in.

Posted: Mon Mar 03, 2008 2:12 pm
by bucket_brigade
Thanks i'll try to figure this out then. Your tutorials are lovely btw.

Posted: Mon Mar 03, 2008 2:37 pm
by lukem95
haha i wonder how many compliments JamesM has got about those... but its all deserved. theyre the best tutorials out there.

the link with the PE/COM explanation looks very useful, i may try to get those working soon.

Posted: Tue Mar 04, 2008 2:17 am
by JamesM
haha i wonder how many compliments JamesM has got about those... but its all deserved. theyre the best tutorials out there.
:oops: :oops: :oops: :oops: :oops: