ELF32 relocation for R_386_PC32 entries

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
brunexgeek
Member
Member
Posts: 45
Joined: Wed Dec 25, 2013 11:51 am

ELF32 relocation for R_386_PC32 entries

Post by brunexgeek »

Hey guys,

I'm finishing the implementation of my ELF32 loader. Currently I'm testing everything on GNU/Linux with GCC. My tests consists of load an ELF32 library, look for a specific function and call it.

My loader already supports R_386_32, R_386_PC32, R_386_GLOB_DAT and R_386_JMP_SLOT relocation types, but I have a problem with R_386_PC32 when relocating an external function (e.g. some libc function).

When compiling some shared library, I got a relocation table like this:

Code: Select all

Offset    Info     Type              Sym.Value  Sym. Name
000005d2  00000702 R_386_PC32        000005a0   dummy
000005e8  00000202 R_386_PC32        00000000   printf
For the function dummy it's easy: I take the value at the offset in the entry, add the address of the symbol to it (base address of the library in memory + Sym.Value) and subtract the address of the offset. But for an external function, like printf, I can't do that because I don't have the Sym.Value. I mean, I tried to replace "the address of the symbol" by the return of the dlsym function, by it crashes.

What could I do to relocate that symbols?
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: ELF32 relocation for R_386_PC32 entries

Post by xenos »

I guess the most simple approach would be parsing the library's symbol table, looking for the printf entry and taking the symbol value from there.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
Post Reply