ELF relocation

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
mcheung63
Member
Member
Posts: 175
Joined: Thu Jun 22, 2006 8:33 am
Location: Hong Kong
Contact:

ELF relocation

Post 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])
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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.
User avatar
Combuster
Member
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:

Post by Combuster »

you can use my application linker as a working sample (assembly)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Smilediver
Member
Member
Posts: 37
Joined: Sun Aug 05, 2007 4:23 pm

Post 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
User avatar
mcheung63
Member
Member
Posts: 175
Joined: Thu Jun 22, 2006 8:33 am
Location: Hong Kong
Contact:

Post by mcheung63 »

thank you everybody :D
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post 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.
sancho1980
Member
Member
Posts: 199
Joined: Fri Jul 13, 2007 6:37 am
Location: Stuttgart/Germany
Contact:

Post by sancho1980 »

i was also looking for some introductory stuff on elf and came across this one:

http://www.linuxjournal.com/article/1059
Post Reply