Reading kernel symbols from the grub header

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
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

Reading kernel symbols from the grub header

Post by AlfaOmega08 »

I need to read my kernel .symtab. Looking at the multiboot spec seems that when the bit 5 of flags, the informations below are valid for an ELF file. My flags are 0x7EF so bit 5 is enabled. I neither have a MBOOT_AOUT_KLUDGE in the multiboot header. However my info are:
Num: 0x2CA
Size: 0x28
Addr: 0x140000
Shndx: 0x2C7

I am in higher half so i translate 0x140000 to 0xC0140000. Everything I find there is a long list of 0s, while I expected to see a list of Elf section headers. Any ideas?
Please, correct my English...
Motherboard: ASUS Rampage II Extreme
CPU: Core i7 950 @ 3.06 GHz OC at 3.6 GHz
RAM: 4 GB 1600 MHz DDR3
Video: nVidia GeForce 210 GTS... it sucks...
florianh80
Posts: 6
Joined: Fri Mar 19, 2010 10:57 am
Location: Kiel - Germany

Re: Reading kernel symbols from the grub header

Post by florianh80 »

Hi,

I've got exactly the same problem here.
Anyone here has an idea whats going wrong?

Flo
torshie
Member
Member
Posts: 89
Joined: Sun Jan 11, 2009 7:41 pm

Re: Reading kernel symbols from the grub header

Post by torshie »

You can try to edit the linker script and put the symbol table into .text section.
Change your linker script something like this:

Code: Select all

.text {
  *(.text)
  *(.text.*)
__symbol_start__ = .;
  *(.dynsm) /* I don't know whether the symbol table is actually here. */
}
I haven't tried this, but it should work.

BTW, I'm using a second stage loader to load my kernel, so I can load whatever I want into anywhere :mrgreen: . The loader itself is loaded by grub. I wrote the loader because I found it was difficult to load a 64-bit higher-half kernel with grub.
florianh80
Posts: 6
Joined: Fri Mar 19, 2010 10:57 am
Location: Kiel - Germany

Re: Reading kernel symbols from the grub header

Post by florianh80 »

First, thanks for the reply!

But i tried something like this before.
And this will double the symbol informations in the elf file, which I don't like very much.
The second problem is, it won't work like expected...
I have this section in my linker script:

Code: Select all

    .text : AT(ADDR(.text) - 0xC0000000) {
        *(.text)
        *(.rodata)        
        start_ksyms = .;
        *(.symtab)
        end_ksyms = .;
    }
But that won't do the trick.

Code: Select all

readelf -s kernel.bin  | grep ksyms
   372: c0106005     0 NOTYPE  GLOBAL DEFAULT    1 start_ksyms
   544: c0106005     0 NOTYPE  GLOBAL DEFAULT    1 end_ksyms
The difference between start_ksyms and end_ksyms is zero...

Any more ideas?

P.S.: My first example Kernel used some EXPORT Macro like Linux does. That works fine, but my current project is written in C++ and I've found no suitable way to do something similar to C++ classes...
florianh80
Posts: 6
Joined: Fri Mar 19, 2010 10:57 am
Location: Kiel - Germany

Re: Reading kernel symbols from the grub header

Post by florianh80 »

Hmm, i figured out that my grub (0.96) gives some misleading output about the symbol table location.

Look at this code snippet from the Grub Stage2 file boot.c:

Code: Select all

...

	      tab_size = pu.elf->e_shentsize * pu.elf->e_shnum;
	      
	      grub_seek (pu.elf->e_shoff);
	      if (grub_read ((char *) RAW_ADDR (cur_addr), tab_size)
		  == tab_size)
		{
		  mbi.syms.e.addr = cur_addr;
		  shdr = (Elf32_Shdr *) mbi.syms.e.addr;
		  cur_addr += tab_size;
		  
		  printf (", shtab=0x%x", cur_addr);

...
Grub prints the address AFTER incrementing it with the size of the whole table.
And I was wondering why the address for the symtab in the multiboot structure was different from that was grub said.

I don't know if it is an error in grub, but it is not really easy to unterstand.

Flo
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

Re: Reading kernel symbols from the grub header

Post by AlfaOmega08 »

You're right. shtab=something shows the end of the section header table. But the address found in the grub header is without the size summed.
I get shtab=0x146F90 and but the address given is 0x140000
Please, correct my English...
Motherboard: ASUS Rampage II Extreme
CPU: Core i7 950 @ 3.06 GHz OC at 3.6 GHz
RAM: 4 GB 1600 MHz DDR3
Video: nVidia GeForce 210 GTS... it sucks...
florianh80
Posts: 6
Joined: Fri Mar 19, 2010 10:57 am
Location: Kiel - Germany

Re: Reading kernel symbols from the grub header

Post by florianh80 »

Hi,

yes, and I also figured out why there was strange data at the address where my section headers should be.
It was my mistake loading the kernel image. I load it from a floppy image like this:

Code: Select all

grub> kernel 200+400
But my Kernel was getting larger the last days...
With debugging stuff and things like that, it was bigger than 400*512bytes. The symbol table seems to be at the end of my ELF file and thus was not loaded. :oops:
Now everything works as expected.
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

Re: Reading kernel symbols from the grub header

Post by AlfaOmega08 »

:? I'm loading the elf from a CD. The image should be loaded by GRUB without specifing any size. ](*,)
Please, correct my English...
Motherboard: ASUS Rampage II Extreme
CPU: Core i7 950 @ 3.06 GHz OC at 3.6 GHz
RAM: 4 GB 1600 MHz DDR3
Video: nVidia GeForce 210 GTS... it sucks...
florianh80
Posts: 6
Joined: Fri Mar 19, 2010 10:57 am
Location: Kiel - Germany

Re: Reading kernel symbols from the grub header

Post by florianh80 »

hmm, does

Code: Select all

$ readelf -S kernel.bin 
show any symbol table section? (.symtab)
Maybe you stripped it off the final kernel image.

Flo
Post Reply