grub module question [answered]

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
13postures
Posts: 13
Joined: Sat Apr 28, 2007 1:57 am

grub module question [answered]

Post by 13postures »

Hello all,

I have two modules for grub to load: one multiboot kernel module and an elf module. The first one is called with the "kernel" command and the second is called with the "module" command (in menu.lst).

Grub first boots the kernel with no problems. In this kernel, i have defined a global variable which stores the address (mbi->mods_addr)->mod_start which is the start address of the elf module. At the end of the kernel module i do a "jmp dest", where dest is the above global variable, because i want to jump to the entry point of the elf module and continue execution from there.

However, this doesn't work. The computer reboots when "jmp dest" is called. :?

Can anybody give me a helping hand? I would really appreciate it.
Last edited by 13postures on Fri May 25, 2007 8:35 am, edited 1 time in total.
Aali
Member
Member
Posts: 58
Joined: Sat Apr 14, 2007 12:13 pm

Post by Aali »

mod_start is not the entry point, its just a pointer to where the elf image is stored in memory

you would have to parse the elf headers yourself and most likely relocate the module (grub could put it anywhere, its probably linked to some other address)
13postures
Posts: 13
Joined: Sat Apr 28, 2007 1:57 am

Post by 13postures »

Thank you for your reply Aali.

Do you mean i have to load the elf image manually into vm? I haven't created a vm system in order to back an operation like that. What do you mean with "relocate the module"?

Parsing the elf headers is easy. But i don't understand how can i find the elf image's entry point from searching the headers :(. There is a field called e_entry in Elf Header, but that's not it.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post by jnc100 »

Read ELF and then run objdump -p filename on your module.

You'll get an output like

Code: Select all

test/testa:     file format elf32-i386

Program Header:
    LOAD off    0x00001000 vaddr 0x40000000 paddr 0x40000000 align 2**12
         filesz 0x0000605a memsz 0x0000605a flags r-x
    LOAD off    0x00008000 vaddr 0x40007000 paddr 0x40007000 align 2**12
         filesz 0x00000004 memsz 0x00001208 flags rw-

Each segment in the file expects to be loaded at the address specified by 'vaddr' but isn't really at that location in the file (or the file would be huge). Its actually at 'off', meaning offset, relative to the beginning of the file. Once you have loaded it to the correct location, then jumping to the location defined by e_entry should work.

Regards,
John.
13postures
Posts: 13
Joined: Sat Apr 28, 2007 1:57 am

Post by 13postures »

Jnc100, you have been very clear. Thank you for your answer. Just one thing though.
jnc100 wrote: ...Once you have loaded it to the correct location, then jumping to the location defined by e_entry should work.
This means that i must have a virtual memory system set up in the kernel module so that i can load the elf file at the correct memory location, right? Or is there another way to load it at the correct location?
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post by jnc100 »

13postures wrote:This means that i must have a virtual memory system set up in the kernel module so that i can load the elf file at the correct memory location, right? Or is there another way to load it at the correct location?
Generally yes, unless all the sections in the elf file are linked so that they exist within the physical memory of your system, in which case you can just copy them there. The executable sections and the kernel sections need to be mutually exclusive of course. This is generally a bad idea though, because 1) you don't know how much physical memory a user will have, whereas virtual memory is standard at 4GB in a 32-bit system and 2) all your user processes will need to be linked to run at different locations. With paging, you can use a separate address space for each process. No such luck if you're running with paging disabled.

Regards,
John.
13postures
Posts: 13
Joined: Sat Apr 28, 2007 1:57 am

Post by 13postures »

Thanks for sharing all that info jnc100!

You really helped.
Post Reply