Question

I have assingment to make loader for ARM elf relocatible files. I have managed to parse elf somehow, and managed to make some simple relocations, but i have to do the following and i have trouble. I need to call printf function from inside this ELF file that i am trying to load, and it must be the same printf function as the one from program that is trying to load it. (thats my host program, writen in c++ from ARM arch). How can i get this printf address, and after that how can i relocate symbol in elf file. (I googled and found out that this type of relocation uses 24-bit PC relative addressing) Can anyone help me extract address of printf? Thanx in advance!

Was it helpful?

Solution 2

If you can understand C++, use the source Luke.

See: Assemblers and Loader, by David Solomon. [some pearls of wisdom, some archaic trivia]

As I understand, the printf is in the same file and you need to patch the address at run-time. In this case, the relocation is a 24-bit PC relative relocation. The answer is you do nothing in this case. As the executable and the implementation are both in the same binary, the 24-bit PC relative is already relocated no matter what the load address is.

If you were relocating a shared library, the process would be different.

OTHER TIPS

Since it is impossible to understand where are you with your progress, best suggestion would be to point some source code. Check Android's linker under bionic git.

I found solution. When I get (unsigned int)printf I get actual 32-bit address of function. Patch code must be added to end of text section in ELF file. Now on place where 24-bit offset is needed, is inserted offset to patch code. Now this patch code is what is hard to make.

edit:

Something like this:

bl printf should be resolved like bl offset_to_patch_code

patch code should be on bottom of .text section. Watch out when calculating offset pipeline made pc point 2 instructions ahead. Patch code should only server to read into pc address of printf. You do not need to save link register.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top