ELF loading

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
zity
Member
Member
Posts: 99
Joined: Mon Jul 13, 2009 5:52 am
Location: Denmark

ELF loading

Post by zity »

Hello osdevers!
I have recently returned to OS development after a few years of inactivity. Last time it didn't went to well, so I've started all over this time. I have used JamesM's tutorial as a base for my new operating system and modified it for my needs and style.

I'm now trying to implement user programs into my system, because I don't want my shell to be a part of the kernel itself. I have chosen ELF as the executable format for my user programs, because of its wide support and easy of use in Linux (and because the kernel is in ELF format, too). I have been looking trough the ELF specification and I've coded a parser to parse the elf header and the program and sections headers. This was actually pretty easy, but I am confused about how the prepare the file for execution. There a several things I don't fully understand.

1. When I want to compile an elf executable for my operating system will I, as far as I've figured out, need an assembly file to fetch the programs arguments from the kernel and push them as arguments for the main function, right? And this assembly file should call an exit function as well, when the main function returns? These two files should of cause be linked together.

2. I'm not sure how to link my files together (and this is partially connected with the third question). Should I specify a virtual or a physical address, or both?

3. Before I can jump to the entry address, I need to make sure the program is loaded with the correct addresses for data and so on, but is this a matter of relocation or some memory mapping?

I'm overall a little confused about all this, I have searched the forum several times without finding the answers for my questions. As you may have guessed I'm not so much into OS development yet, so I would appreciate any help, also links to some explanations on this topic :)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: ELF loading

Post by Solar »

zity wrote:I have used JamesM's tutorial as a base for my new operating system...
Error #1. 8)

Read, learn, then implement yourself.
1. When I want to compile an elf executable for my operating system will I, as far as I've figured out, need an assembly file to fetch the programs arguments from the kernel and push them as arguments for the main function, right? And this assembly file should call an exit function as well, when the main function returns? These two files should of cause be linked together.
This is the C runtime. On a UNIX system, it usually resides in crt0.o. It also needs to do the cleanup on process shutdown, e.g. calling any functions registered with atexit(), closing I/O streams etc.
Every good solution is obvious once you've found it.
User avatar
zity
Member
Member
Posts: 99
Joined: Mon Jul 13, 2009 5:52 am
Location: Denmark

Re: ELF loading

Post by zity »

Solar wrote: Error #1. 8)
Read, learn, then implement yourself.
I knew someone would comment on this :) But I've read about it too and I've a fairly good understanding about how the stuff works. But I must admit that I should probably read more about what's most importent, paging. I'll do that tomorrow morning, maybe that will help me clear up some of my questions.

Thanks for the info according to the C runtime, I'll try to implement my own runtime functions :)
User avatar
zity
Member
Member
Posts: 99
Joined: Mon Jul 13, 2009 5:52 am
Location: Denmark

Re: ELF loading

Post by zity »

Well, I've spent most of the day reading and researching and it finally paid off. I figured out that I simply needed to map the addresses specified in the elf header and then copy the code to the address. With help from this thread (http://forum.osdev.org/viewtopic.php?f= ... 9&p=115457) and the source from Khaos, I've managed to get a (simple) working elf loader.

Code: Select all

void load_elf(u32int elf_ptr)
{
   int i;
   page_t *page;

   elf32_ehdr *ehdr = (elf32_ehdr*)elf_ptr;
   elf32_phdr *phdr = (elf32_phdr*)(elf_ptr + ehdr->e_phoff);
//   elf32_shdr *shdr = (elf32_shdr*)(elf_ptr + ehdr->e_shoff);

   for(i=0;i<ehdr->e_phnum;i++)
   {
      if(phdr->p_type == PT_LOAD)
      {
         printf("p_vaddr:  0x%x\n", phdr->p_vaddr);

         int j = phdr->p_vaddr & ~0xFFF;
	 u32int limit = ((u32int) phdr->p_vaddr + phdr->p_memsz + 0x1000) & ~0xFFF;

         for(;j < limit; j+= 0x1000)
         {
            page = get_page(phdr->p_vaddr, 1, current_directory);
            alloc_frame(page, 0, 1);
            printf("frame:  0x%x\n", page->frame);
         }

         printf("memcpy() the program data there\n");
         printf("0x%x - 0x%x - 0x%x\n",phdr->p_vaddr, elf_ptr+phdr->p_offset, phdr->p_filesz);
         printf("header: 0x%x, phdr->p_offset: 0x%x\n",elf_ptr, phdr->p_offset);

         memcpy((u32int*)phdr->p_vaddr, (const void*)elf_ptr+phdr->p_offset, phdr->p_memsz);
      }
      phdr++;
   }
     printf("calling executable\n");
     asm volatile("call *%0" : : "a" (ehdr->e_entry));
}
Now it just needs some expanding and so on :)
Post Reply