Page 1 of 1
ELF relocation
Posted: Sat Aug 25, 2007 11:54 am
by mcheung63
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])
Posted: Sat Aug 25, 2007 1:18 pm
by JamesM
There doesn't seem to be a good tutorial on it, you'll have to work it out yourself, with the aid of references from google. thats how i did it.
Posted: Sun Aug 26, 2007 2:39 am
by Combuster
you can use
my application linker as a working sample (assembly)
Posted: Sun Aug 26, 2007 5:34 am
by Smilediver
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
Posted: Mon Aug 27, 2007 3:48 am
by mcheung63
thank you everybody
Posted: Wed Aug 29, 2007 2:35 am
by Kevin McGuire
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).
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;
}
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.
Posted: Mon Sep 03, 2007 10:27 am
by sancho1980
i was also looking for some introductory stuff on elf and came across this one:
http://www.linuxjournal.com/article/1059