[Solved] Finding *real* end of kernel image

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
User avatar
fontknocker
Posts: 16
Joined: Mon Dec 11, 2006 1:49 am
Location: Canberra, Australia

[Solved] Finding *real* end of kernel image

Post by fontknocker »

Hi,

Are there any suggestions for creating a symbol that marks the end of the kernel image and takes into account the ELF .strtab, .symtab and .shstrtab sections? The "standard" method of marking kernel end by placing a linker symbol at the very end of the linker script does not work; it does not include the space occupied by those three sections. Nor can I relocate those three sections by using the linker script - there are no errors from LD, they just stubbornly refuse to move.

Currently when I use my kernel end symbol to start allocating memory I trash the symbol table and I'd like to keep it intact for stack trace purposes.

Cheers!
Last edited by fontknocker on Wed Jan 02, 2008 5:49 am, edited 2 times in total.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

The "end symbol" method described in many tutorials should suffice: Notice that there should be a "*(.*)*" just before the "end = .;" in order to catch every section.
User avatar
fontknocker
Posts: 16
Joined: Mon Dec 11, 2006 1:49 am
Location: Canberra, Australia

Post by fontknocker »

Thanks for the suggestion, but putting in that catch-all doesn't solve the issue. Perhaps I'm linking the kernel incorrectly? My linker script is fairly standard (snipped out some stuff). I've placed *(.*) in the BSS section just for testing:

Code: Select all

// snip...
SECTIONS
{
    .setup phys : {
        *(.mboot)
        *(.setup)
    }
    
    . += virt;
    
    // snipping text, data, rodata.

    .bss ALIGN(0x1000) : AT(ADDR(.bss) - virt)
    {
        _BSS_START = .;
        *(.bss*)
        _BSS_END = .;
        *(.*)
    }   

    . = ALIGN(0x1000);

    _KERNEL_END = .;
}
The command to link is pretty straightforward. All the generated object files are linked straight into the final kernel image:

Code: Select all

$(KERNELIMAGE) : $(OBJECTS)
    @$(LD) -T link.ld -o $(KERNELIMAGE) $^
Interestingly, if I put the following in the linker script:

Code: Select all

symtabsize = SIZEOF(.symtab);
symtabsize comes out to zero, suggesting that the symtab section isn't considered by the linker when running through the script.

I do have a somewhat hacked solution now, but it's kludgy and I'd prefer a better one. I take the value of _KERNEL_END given by the linker and, whilst I'm iterating through the ELF section headers, increment it by the size of these extra sections.

Any thoughts? What obvious thing am I missing? :)
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Well could you give an "objdump -h" of the resulting kernel file and also attach the value of _KERNEL_END?
User avatar
fontknocker
Posts: 16
Joined: Mon Dec 11, 2006 1:49 am
Location: Canberra, Australia

Post by fontknocker »

Sure can:

Code: Select all

fontknocker@desktop:~/dev/kernel/build$ objdump -h kimage

kimage:     file format elf32-i386

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .setup        0000002a  00100000  00100000  00001000  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  1 .text         00003bf7  c0101000  00101000  00002000  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  2 .data         00000a4c  c0105000  00105000  00006000  2**5
                  CONTENTS, ALLOC, LOAD, DATA
  3 .bss          00002ee0  c0106000  00106000  00007000  2**5
                  ALLOC

Code: Select all

fontknocker@desktop:~/dev/kernel/build$ objdump -t kimage | grep _KERNEL_END
00108ee0 g       *ABS*  00000000 _KERNEL_END
For extra info, here's part of the result of elfdump:

Code: Select all

fontknocker@desktop:~/dev/kernel/build$ elfdump kimage

[ Section headers ]
Idx       Name       Type Flag   VMA     Offset   Size   Lnk Inf Algn Tbl
-------------------------------------------------------------------------
 0)                  NULL            0        0        0   0   0    0   0
 1) .setup           PROG  A    100000     4096       42   0   0    1   0
 2) .text            PROG XA  C0101000     8192    15351   0   0   16   0
 3) .data            PROG  AW C0105000    24576     2636   0   0   32   0
 4) .bss             NOSP  AW C0106000    28672    12000   0   0   32   0
 5) .shstrtab        STRT            0    40672       51   0   0    1   0
 6) .symtab          SYMT            0    41044     3776   7  62    4  16
 7) .strtab          STRT            0    44820     2713   0   0    1   0
_KERNEL_END points to the beginning of the .shstrtab section.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Code: Select all

5) .shstrtab        STRT            0    40672       51   0   0    1   0
 6) .symtab          SYMT            0    41044     3776   7  62    4  16
 7) .strtab          STRT            0    44820     2713   0   0    1   0 
Notice that the VMA for all these sections is zero. Therefore they don't have an assigned virtual memory address to be loaded in to. Therefore it seems logical that _KERNEL_END points to the end of the last section with an assigned VMA.

Seems correct to me. There is nothing in the elf header that says where .shstrtab etc should be loaded in to (if at all) so where they go is completely implementation dependent and thus unreliable.
User avatar
fontknocker
Posts: 16
Joined: Mon Dec 11, 2006 1:49 am
Location: Canberra, Australia

Post by fontknocker »

Yeah, that makes sense. I'll stick with the implementation I have now: use the last section header as the marker for calculating the end of the image.

I appreciate your help. Cheers!
Post Reply