Page 1 of 1
kernel symbols
Posted: Sat Nov 08, 2003 8:54 am
by norton
Hi, im new to this board.
I want to calculate the size of my kernel at runtime,
and i know that I can set symbols like _kernel_start and _kernel_end to do that. But what I want to know is how to do that. How to set them, and how to read them. I've tried to find out, but cant find anything. Im using gcc.
/norton
Re:kernel symbols
Posted: Sat Nov 08, 2003 10:33 am
by df
i put them into my linker script and link that way.
Code: Select all
ENTRY(start)
SECTIONS {
.text 0x100000 : {
code = .;
*(.text)
. = ALIGN(4096);
*(.rodata*)
. = ALIGN(4096);
_etext = .;
etext = _etext;
}
.data : {
*(.data)
. = ALIGN(4096);
_edata = .;
edata = _edata;
}
.bss : {
*(.bss)
*(COMMON)
. = ALIGN(4096);
_end = .;
eend = _end;
}
}
and I link with
Code: Select all
ld -X -x -E -e start -Ttext 0x100000 -Tstage1.ld -Map map.txt $(OBJS) -o kernel.img
the -Tstage1.ld is the link script. this gives you etext, edata and eend
Re:kernel symbols
Posted: Sat Nov 08, 2003 2:16 pm
by Neo
if what you want to know is how to get the real end address of you kernel during runtime then all you have to do is declare the linker symbol as an
in the example given by df this would be the symbol "eend" thus you would use
and this gives you a pointer to the symbol so all you need is to dereference it and voila! you have the kernel size i.e you might use
Code: Select all
char *kend=&eend;
printf("kernel end = %x",kend);
hope that was what you wanted
Re:kernel symbols
Posted: Sat Nov 08, 2003 3:18 pm
by Candy
Neo wrote:
if what you want to know is how to get the real end address of you kernel during runtime then all you have to do is declare the linker symbol as an
in the example given by df this would be the symbol "eend" thus you would use
and this gives you a pointer to the symbol so all you need is to dereference it and voila! you have the kernel size i.e you might use
Code: Select all
char *kend=&eend;
printf("kernel end = %x",kend);
hope that was what you wanted
Assuming that eend is at the end of the bss section, AND assuming nothing follows it, AND assuming that eend shouldn't count.
I would use a normal executable file format (such as ELF) which very plainly lists the size of each section, plus where it should be loaded. a+b=c, so that is the end address, nothing else.
[mod time="now"] Forgot, you also suppose that the kernel is linear, and has the bss section at the end (which I do not for example)[/mod]
Re:kernel symbols
Posted: Sun Nov 09, 2003 11:45 am
by df
Candy wrote:I would use a normal executable file format (such as ELF) which very plainly lists the size of each section, plus where it should be loaded. a+b=c, so that is the end address, nothing else.
well, considering its runtime, fileformat does not count since your ELF kernel is loaded into memory and built accoridng to its sections, its headers are not in memory, so unless your kernel plans to open its self ON DISK and parse its headers... what was your point again??
Re:kernel symbols
Posted: Sun Nov 09, 2003 12:18 pm
by Neo
Assuming that eend is at the end of the bss section, AND assuming nothing follows it, AND assuming that eend shouldn't count.
yeah that was assuming a lot but you could have the eend symbol after the last section as shown and assign the location counter to it.
Code: Select all
ENTRY(start)
SECTIONS {
.text 0x100000 : {
code = .;
*(.text)
. = ALIGN(4096);
*(.rodata*)
. = ALIGN(4096);
_etext = .;
etext = _etext;
}
.data : {
*(.data)
. = ALIGN(4096);
_edata = .;
edata = _edata;
}
.bss : {
*(.bss)
*(COMMON)
. = ALIGN(4096);
_end = .;
}
eend = .;
}
Re:kernel symbols
Posted: Sun Nov 09, 2003 1:20 pm
by Candy
df wrote:
fileformat does not count since your ELF kernel is loaded into memory and built accoridng to its sections, its headers are not in memory
who is doing that? If you are yourself, use that info for that purpose. Otherwise, load the headers from disk and use them.
Re:kernel symbols
Posted: Sun Nov 09, 2003 2:19 pm
by df
Candy wrote:who is doing that? If you are yourself, use that info for that purpose. Otherwise, load the headers from disk and use them.
read the OP. you know, the one at the very top. that says 'I want to calculate the size of the kernel at runtine'.
wouldnt it be easier to say
than 500 lines of code to read the kernel back in from disk. this is assuming you have a disk driver and all its bits setup...
Re:kernel symbols
Posted: Mon Nov 10, 2003 12:14 pm
by Neo
yeah this seems such an easy way to get the size. btw df are there any problems that might arise with this method or is it reliable enough?
Re:kernel symbols
Posted: Mon Nov 10, 2003 4:21 pm
by nullify
Neo wrote:
yeah this seems such an easy way to get the size. btw df are there any problems that might arise with this method or is it reliable enough?
Its reliable unless there's any "unusual" situation. Most of the assumptions Candy spoke of aren't really a big deal; you can ensure they are always true by the way you write your kernel linker script.
Re:kernel symbols
Posted: Tue Nov 11, 2003 11:31 am
by Neo
Yeah thanks nullify thats just what i wanted to hear.