[NASM] Execute ELF kernel

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
beausis
Posts: 3
Joined: Mon Apr 27, 2009 6:47 am

[NASM] Execute ELF kernel

Post by beausis »

I am writing bootloader with NASM and the last step which remains is to execute kernel.
I have written kernel with C, compiled with:

Code: Select all

gcc -c filename.c -nostdlib -nostdinc -fno-builtin -fno-stack-protector
Then linked with:

Code: Select all

ld -T link.ld filename.o -o kernel
link.ld:

Code: Select all

ENTRY(main)
SECTIONS
{
  .text 0x100000 :
  {
    code = .; _code = .; __code = .;
    *(.text)
    . = ALIGN(4096);
  }

  .data :
  {
     data = .; _data = .; __data = .;
     *(.data)
     *(.rodata)
     . = ALIGN(4096);
  }

  .bss :
  {
    bss = .; _bss = .; __bss = .;
    *(.bss)
    . = ALIGN(4096);
  }

  end = .; _end = .; __end = .;
}
The kernel is loaded at 1MB in RAM. I don't know how to execute the kernel and I am asking to help me. I have red ELF documentation, but haven't understand it enough to execute my kernel.

Sorry for poor English, I am not native speaker of English.
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: [NASM] Execute ELF kernel

Post by JohnnyTheDon »

There are two structures you need to worry about: the ELF header and the program headers. You just need to use the ELF header to find the program headers, and then follow the instructions in the program headers for loading your progam. Just copy the number of bytes indicated by the file size field, from the location indicated by the file offset field, to the location indicated by the address field.

In the future, tell everyone exactly what you don't understand so we can better help you.
beausis
Posts: 3
Joined: Mon Apr 27, 2009 6:47 am

Re: [NASM] Execute ELF kernel

Post by beausis »

I can not find information about executing ELF file step by step.

I found ELF header, I found ELF program header table's values:

Code: Select all

p_filesz 0x1000
p_offset 0x1000
p_vaddr 0x100000
p_align 0x1000
I do not know how I should use these values. In which registers should I load them? Which register should I call to execute? Or should I call some memory location?
Just copy the number of bytes indicated by the file size field, from the location indicated by the file offset field, to the location indicated by the address field.
Should I copy 0x1000 bytes from 0x1000 offset of file to 0x100000 memory location? And what next? Execute 0x100000? This looks the same as executing 0x101000...
PHPnerd
Member
Member
Posts: 34
Joined: Mon Nov 05, 2007 11:15 am
Location: The Netherlands
Contact:

Re: [NASM] Execute ELF kernel

Post by PHPnerd »

That is right.

// PHPnerd
Jinix
beausis
Posts: 3
Joined: Mon Apr 27, 2009 6:47 am

Re: [NASM] Execute ELF kernel

Post by beausis »

Thank you all. It seems to be working.
Post Reply