kernel symbols

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
norton

kernel symbols

Post 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
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:kernel symbols

Post 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
-- Stu --
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:kernel symbols

Post 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

Code: Select all

extern char
in the example given by df this would be the symbol "eend" thus you would use

Code: Select all

extern char eend
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
Only Human
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:kernel symbols

Post 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

Code: Select all

extern char
in the example given by df this would be the symbol "eend" thus you would use

Code: Select all

extern char eend
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]
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:kernel symbols

Post 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??
-- Stu --
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:kernel symbols

Post 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 = .;
}
Only Human
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:kernel symbols

Post 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.
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:kernel symbols

Post 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

Code: Select all

intKernelSizze = _eend;
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...
-- Stu --
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:kernel symbols

Post 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?
Only Human
nullify

Re:kernel symbols

Post 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.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:kernel symbols

Post by Neo »

Yeah thanks nullify thats just what i wanted to hear. 8)
Only Human
Post Reply