Page 1 of 1
Determining size of kernel
Posted: Thu Sep 05, 2002 9:26 pm
by Avery Fay
I'm working on a kernel that is currently being loaded into memory by GRUB. I would like to know how large the kernel is so that my page allocator won't allocate the page(s) taken by it. Can GRUB provide this information? If not, how would I get this information?
Re:Determining size of kernel
Posted: Fri Sep 06, 2002 6:08 am
by Whatever5k
Linux:
ls -l kernel.bin
Windows:
dir
Re:Determining size of kernel
Posted: Fri Sep 06, 2002 6:26 am
by Tim
Whatever5k's reply is a bit of a red herring

-- the size of the kernel on disk will usually be less than the size in memory, because the bss isn't present on disk, and because the alignment on disk can be less than that in memory.
You'll need to write a linker script for your kernel, if you haven't already done so.
Code: Select all
OUTPUT_FORMAT("pei-i386") /* or whatever */
ENTRY(_start32)
SECTIONS
{
/* kernel code */
.text 0xC0000000 :
{
_scode = .;
*(.text)
. = ALIGN(4096);
_ecode = .;
}
/* kernel data */
.data :
{
libc_first_ctor = . ;
*(.ctor)
libc_last_ctor = . ;
libc_first_dtor = . ;
*(.dtor)
libc_last_dtor = . ;
*(.data)
*(.eh_frame)
*(.gcc_except_table)
. = ALIGN(4096);
_edata = .;
}
/* kernel BSS */
.bss :
{
*(.bss)
*(COMMON)
_ebss = .;
}
_end = .;
}
Then you can declare the scode and end variables in your source file:
The total size of the kernel in memory is (end - scode) bytes. The size of the .text section is (ecode - scode); data is (edata - ecode); bss is (ebss - edata).
Re:Determining size of kernel
Posted: Fri Sep 06, 2002 6:24 pm
by Avery Fay
Thanks a lot. I think I understand the script for the most part. What linker are you using? And will the align directives cause each section to be page aligned or only the whole image? Also, do you know of any documentation on linking and ld (other than the man page)?
Re:Determining size of kernel
Posted: Sat Sep 07, 2002 3:00 am
by Tim
Avery Fay wrote:Thanks a lot. I think I understand the script for the most part. What linker are you using?
Cygwin ld, although this should work for any ld version.
And will the align directives cause each section to be page aligned or only the whole image?
They cause the start of each section to be page-aligned in memory. The on-disk alignment might be different if the format you are using supports that. BTW: note that the base address here is 0xC0000000; you might want to change that if you're not using paging.
Also, do you know of any documentation on linking and ld (other than the man page)?
info ld
I don't know of any apart from that :/
Re:Determining size of kernel
Posted: Mon Sep 23, 2002 7:47 pm
by Avery Fay
I got most of the it working... however I still have one question that maybe someone could answer. In the above example, does _end have the first memory location after the image? Or is the address pointed to by _end actually used. For example, suppose _end was 0x00100000. Would the whole page from 0x00100000 - 0x00100FFF be available or only 0x00100001 - 0x00100FFF?
Re:Determining size of kernel
Posted: Tue Sep 24, 2002 2:45 am
by Tim
_end is the first unused memory location.