Page 1 of 1

[NASM] Execute ELF kernel

Posted: Mon Apr 27, 2009 6:59 am
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.

Re: [NASM] Execute ELF kernel

Posted: Mon Apr 27, 2009 8:48 am
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.

Re: [NASM] Execute ELF kernel

Posted: Tue Apr 28, 2009 8:42 am
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...

Re: [NASM] Execute ELF kernel

Posted: Tue Apr 28, 2009 9:00 am
by PHPnerd
That is right.

// PHPnerd

Re: [NASM] Execute ELF kernel

Posted: Tue Apr 28, 2009 1:23 pm
by beausis
Thank you all. It seems to be working.