Can I get symbol name by "GOT address"?

537 views Asked by At

Now I'm developing program like readelf using pyelftools libraries.

I want to print the table like below.
In here, Offset field is stderr@GOT address.

jiwon@jiwon$ readelf -a --wide libstdbuf.so
...
Relocation section '.rel.dyn' at offset 0x454 contains 12 entries:
 Offset     Info    Type                Sym. Value  Symbol's Name
00001c50  00000206 R_386_GLOB_DAT         00000000   stderr@GLIBC_2.0
...


Assume that I have only GOT address(00001c50) of unknown symbol.

My situation I want to make function like get_symname_by_GOTaddress for resolution from 00001c50 to stderr@GLIBC_2.0.

Question : However, I could not find any linkage information between GOT address and symbol name in binary. How can I get it?

1

There are 1 answers

0
izac89 On BEST ANSWER

Every entry in the .rel.dyn section has at least the following fields:

r_offset;
r_info;

So first scan the section until the address match an entry's r_offset field, then field r_info contains (also) the index to the symbol table, which is composed according to this:

/* for 32bit */
#define ELF32_R_INFO(sym, type)       (((sym)<<8)+(unsigned char)(type))

/* for 64bit */
#define ELF64_R_INFO(sym, type)       (((Elf64_Xword)(sym)<<32)+ \ 
                                        (Elf64_Xword)(type))

So you can extract the sym index, then fetch the entry from the symbol table, which has the field st_name, which is the index into the string table, which contains the symbol name in characters representation.

Links for more information:

rel.dyn section description -> symbol table description -> string table description