ThatCodingGuy89 wrote:
I feel like this is a really dumb question, but I honestly can't figure this out. The instructions on the wiki at
https://wiki.osdev.org/Creating_a_64-bit_kernelare unclear on how you would get the VMA of the kernel, as isn't it inconsistent on where it's mapped from boot to boot, and so the linker script can't possibly know the VMA of the kernel??
Why? You can use paging to abstract the physical address of the kernel away.
I have pretty much the same approach as you, with the bootloader UEFI executable. That executable loads the kernel to any page-aligned address it can get. Then it maps the load segments from the ELF headers as required. The main kernel is a normal ELF64 executable (ET_EXEC), linked to -2GB. As part of the parameter structure the bootloader hands to the main kernel, there is also a memory map, and the bootloader simply sets the place where the kernel image is, as well as the bootstrap page tables, as reserved.
That means the kernel can always be linked to -2GB, no matter where it is loaded. It also means the kernel can always find its ELF header at that address, and can then re-initialize the paging stuff with its own mappings. That brings the paging entirely under the kernel's control.