Page 1 of 1

What address does GRUB load my kernel to?

Posted: Sun May 31, 2009 11:08 pm
by chris95219
Hello everyone. I'm back with another question. I am stuck in memory management because I can't find out how large the kernel image is, where it ends and where it begins in memory. Is it possible to find out 1) the size of the kernel dynamically, 2) the beginning address of where GRUB loads your kernel, and 3) (if not 1) where the kernel "image" ends in memory? There is a "load_end_addr" member of the multiboot header specification, but that is for the header - GRUB doesn't pass that to the kernel.

Any help would be appreciated, thanks.

Re: What address does GRUB load my kernel to?

Posted: Sun May 31, 2009 11:31 pm
by salil_bhagurkar
The header that your kernel uses, is what is used by GRUB to load your kernel. So it includes the information about your kernel embedded into the header by the linker at compile time. So you should use it.

Re: What address does GRUB load my kernel to?

Posted: Sun May 31, 2009 11:40 pm
by chris95219
salil_bhagurkar wrote:The header that your kernel uses, is what is used by GRUB to load your kernel. So it includes the information about your kernel embedded into the header by the linker at compile time. So you should use it.
So what are safe values to use for this header? I mean, how can I find the length of my kernel? (and can this be done dynamically?) Am I supposed to choose am arbitrary number (such as 1MB) that I know for sure my kernel would fit in?

Re: What address does GRUB load my kernel to?

Posted: Mon Jun 01, 2009 2:31 am
by xenos
The easiest way to determine the size and location of your kernel is to define them as symbols in your linker script. For example, your linker script might look like this:

Code: Select all

// other stuff
. = 0x100000;
kernelStart = .;
// put your kernel sections here, i.e. code, data and bss
kernelEnd = .;
// more other stuff
To access these symbols from your C(++) code, you can do something like this:

Code: Select all

extern long kernelStart;
extern long kernelEnd;

printf("Kernel area: %x - %x, size: %d\n", &kernelStart, &kernelEnd, (unsigned long)&kernelEnd - (unsigned long)&kernelStart);

Re: What address does GRUB load my kernel to?

Posted: Mon Jun 01, 2009 2:39 am
by kop99
You could get some reference about linker script from following.
http://www.nacad.ufrj.br/sgi/860-0247-0 ... ripts.html

Re: What address does GRUB load my kernel to?

Posted: Mon Jun 01, 2009 3:11 am
by JamesM
It is fairly standard to get GRUB to load your kernel at 0x100000 - 1MB.

Re: What address does GRUB load my kernel to?

Posted: Mon Jun 01, 2009 1:48 pm
by chris95219
XenOS wrote:The easiest way to determine the size and location of your kernel is to define them as symbols in your linker script. For example, your linker script might look like this:
Thank you very much!