Dynamic loader
Hi,
As I mentioned earlier I am working on loadable module support for the PPC. I had a lot of problems with this so I decided to play a bit with grub-emu to see what is going on.
At the moment this is still a big hack (and a few minutes work), if Thomas does not mind I will make it work properly.
I found a big problem in kern/dl.c. Two pointers are added and GRUB is writing outside memory reserved for it, if I understand it correctly. I really hope Okuji can have a look at this and tell me what is going on. I am not familiar with this code and I am afraid it can take a while to understand what is going on for me.
In `grub_dl_resolve_symbols' the following things are done:
In the case STT_FUNC the `sym->st_value' has a valid pointer. After that the following thing happens:
sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod,
sym->st_shndx);Another (valid) pointer gets added. This results in an invalid pointer which is used afterwards. I think this happens consistently and that is why nothing seems wrong when using GRUB. In grub-emu this simply means a segfault. This segfault happens when `mod->init' is called, which points to a non-executable region of memory.
When I reserve a lot (100 MB is what I used) of memory using mmap and make sure it is executable it works because the memory that was accidently used was allocated.
Any kind of help is appreciated.
Thanks,
Marco
Quoted 6 lines · Marco Gerards
In the case
STT_FUNCthe `sym->st_value' has a valid pointer. After that the following thing happens:sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod,
sym->st_shndx);
I don't remember the ELF specification very precisely, so please consult the specification for better information. I think the value in STT_FUNC is an offset from the section address. So, in order to get the real address, you need to add the section address.
Okuji
Quoted 10 lines · On Sun, 19 Sep 2004 12:32:33 +0000 Marco Gerards <[email protected]>…
In `grub_dl_resolve_symbols' the following things are done:
In the case
STT_FUNCthe `sym->st_value' has a valid pointer. After that the following thing happens:sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod, sym->st_shndx);Another (valid) pointer gets added. This results in an invalid pointer which is used afterwards.
Not checking the ELF specification, here's what I think.
There are four values:
(1) section address specified in the object file (load module of GRUB2)
(2) symbol address specified in the object file (load module of GRUB2)
(3) section address in memory
(4) symbol address in memory
Here, we calculate the value of (4), using the value of (2) and (3). Note that grub_dl_get_section_addr returns (3).
In i386, (1) is zero. So, the expression you quoted works fine. In general cases where we cannot assume section address in the object file always starts at zero, it doesn't work.
Simple hack would be changing the line of grub_dl_load_segments
seg->addr = addr; into seg->addr = addr - s->sh_addr;
I think it works. You could try.
But it seems for me that this hack is not good, because the name of grub_dl_get_section_addr mismatches.
Perhaps, we need to record the value of (1) in the member of struct grub_dl_segment, and use it for the calculation of symbol address.
Hope this helps,