operating-systemelfosdev

entry() get into the different address from the entry point I set in the Elf


Recently I'm learning about the OS. And I want to write a simple bootloader, which change the real mode to protect mode and then load the simple kernel.
But I can't figure out the entry address problem.
At first I put the bootloader in the first sector of the OS.img(qemu), and then the kernel begin at the second sector.
Here's readelf result of my kernel: enter image description here
The entry point address is 0x800c.
And the LMA and VMA are below:
enter image description here
A part of the bootloader which read elf-type kernel and then get into the entry(),which is the entry point address.
enter image description here
However, when I disassemble the bootloader, the entry() is below: enter image description here
Call *0x8018, not *0x800c.
I don't know why this happen. Could you please help me?


Solution

  • call *0x8018 performs a call to an address that is stored at 0x8018, that's correct since ELFHDR is 0x8000 and offset of e_entry in the header is 0x18.

    The real problem is in the way you load segments into memory. Each segment should be loaded at address p_vaddr from file offset p_offset. Notice that in your case p_vaddr is 0x8000, that the same place in memory you loaded elf header to and that's why ELFHDR->e_entry gets overwritten. The easiest solution would be to load elf header at different address.

    Source: http://www.skyfree.org/linux/references/ELF_Format.pdf