Hi
I am currently not quite understanding how to do the relocation of the ELF format, so do you have any good tutorial?
thanks
from Peter ([email protected])
ELF relocation
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
you can use my application linker as a working sample (assembly)
-
- Member
- Posts: 37
- Joined: Sun Aug 05, 2007 4:23 pm
Relocation seems to be easy, but the information is really sparse on how it should be done.
First, elf specification has some information. Get the pdf that has additional books: "Book II: Processor Specific (Intel Architecture)" and "Book III: Operating System Specific (UNIX System V Release 4)". Secondly, google about "dynamic linking". In Linux dynamic linking is done by another dynamic library "ld.so" or "ld-linux.so". So google about those too.
I haven't found any tutorial, or doc that would be close to "step by step" guide. So you have to find something that would briefly mention how it's done, and then find the missing details using elf docs and google.
I've been using the following pages to get a rough idea of how it's done:
http://www.iecc.com/linker/linker10.html
http://www.securityfocus.com/infocus/1872
First, elf specification has some information. Get the pdf that has additional books: "Book II: Processor Specific (Intel Architecture)" and "Book III: Operating System Specific (UNIX System V Release 4)". Secondly, google about "dynamic linking". In Linux dynamic linking is done by another dynamic library "ld.so" or "ld-linux.so". So google about those too.
I haven't found any tutorial, or doc that would be close to "step by step" guide. So you have to find something that would briefly mention how it's done, and then find the missing details using elf docs and google.
I've been using the following pages to get a rough idea of how it's done:
http://www.iecc.com/linker/linker10.html
http://www.securityfocus.com/infocus/1872
- Kevin McGuire
- Member
- Posts: 843
- Joined: Tue Nov 09, 2004 12:00 am
- Location: United States
- Contact:
I was going to use a shared library '-fpic -shared', but I realized that this would include a dynamic linker '.dynamic section' and other stuff which I really did not want to deal with: got - global offset table' .. ect
Instead, I used a pure object file using the '-r' switch with ld when combining multiple object files.
http://kmcguire.org/pub/elf1/contents.html
The basic concept is pretty much the same. I have no experience in dealing with the .dynamic section and global offset table, but the following code should give you an idea about how to access the sections and data.
http://kmcguire.org/pub/git/hfmos/ldr/ldr.c
The only tricky part was the symbol table. I had trouble resolving my symbols into names using the field in the ELF header 'e_shstrndx". It seems that it kept pointing to a string section that I need to use. I would get corrupted looking strings.
My workaround was to not use the string section pointed to by e_shstrndx, but instead find the next one. (There were two SH_STRTAB sections).
Not sure why this was. I know this is a explanation, but for right now it works until I am forced to actually fix it.
Instead, I used a pure object file using the '-r' switch with ld when combining multiple object files.
http://kmcguire.org/pub/elf1/contents.html
The basic concept is pretty much the same. I have no experience in dealing with the .dynamic section and global offset table, but the following code should give you an idea about how to access the sections and data.
http://kmcguire.org/pub/git/hfmos/ldr/ldr.c
The only tricky part was the symbol table. I had trouble resolving my symbols into names using the field in the ELF header 'e_shstrndx". It seems that it kept pointing to a string section that I need to use. I would get corrupted looking strings.
My workaround was to not use the string section pointed to by e_shstrndx, but instead find the next one. (There were two SH_STRTAB sections).
Code: Select all
char* ldrmod_getsymstring(uint_least32_t strindex, uintptr_t offset)
{
uint_least32_t x, y;
elf32_hdr *hdr;
elf32_shdr *shdr;
hdr = (elf32_hdr*)offset;
shdr = (elf32_hdr*)(offset + hdr->e_shoff);
for(x = 0; x < hdr->e_shnum; ++x)
{
// -- use any other section that what _eshstrndx says. --
if((x != hdr->e_shstrndx) && (shdr[x].sh_type == ST_STRTAB))
{
return (char*)(offset + shdr[x].sh_offset + strindex);
}
}
return 0;
}
-
- Member
- Posts: 199
- Joined: Fri Jul 13, 2007 6:37 am
- Location: Stuttgart/Germany
- Contact:
i was also looking for some introductory stuff on elf and came across this one:
http://www.linuxjournal.com/article/1059
http://www.linuxjournal.com/article/1059