'Changing symbol in dynamic symbol table of shared library
Suppose I have a library, for example, the following tmp.c:
extern int counter;
void increment_counter() {
counter = counter + 1;
}
I can link it into a shared library, using the commands:
gcc -c -fPIC tmp.c -o tmp.o
gcc -shared -o libtmp.so tmp.o
This produces a shared library libtmp.so with the following dynamic symbol table:
objdump -TC libtmp.so
libtmp.so: file format elf64-x86-64
DYNAMIC SYMBOL TABLE:
0000000000000000 w D *UND* 0000000000000000 Base _ITM_deregisterTMCloneTable
0000000000000000 w D *UND* 0000000000000000 Base __gmon_start__
0000000000000000 D *UND* 0000000000000000 Base counter
0000000000000000 w D *UND* 0000000000000000 Base _ITM_registerTMCloneTable
0000000000000000 w DF *UND* 0000000000000000 (GLIBC_2.2.5) __cxa_finalize
00000000000010e9 g DF .text 000000000000001c Base increment_counter
Great! Now I want to change the counter symbol from an undefined external symbol, to a symbol which points to a specific address (which I only know at runtime).
I actually know how to do this if I re-link the library with a linker script. If I have the following linker_script.ld:
counter = 0xabababababababab;
and I link with it, I will get the correct thing:
gcc -shared -o libtmp_with_script.so -T linker_script.ld tmp.o
objdump -TC libtmp_with_script.so
libtmp_with_script.so: file format elf64-x86-64
DYNAMIC SYMBOL TABLE:
0000000000000000 w D *UND* 0000000000000000 Base _ITM_deregisterTMCloneTable
0000000000000000 w D *UND* 0000000000000000 Base __gmon_start__
0000000000000000 w D *UND* 0000000000000000 Base _ITM_registerTMCloneTable
0000000000000000 w DF *UND* 0000000000000000 (GLIBC_2.2.5) __cxa_finalize
abababababababab g D *ABS* 0000000000000000 Base counter
00000000000000c9 g DF .text 000000000000001c Base increment_counter
What I'm wondering is: is there a way of doing this if I only have access to the .so file?
I.e., I want a command that will take libtmp.so and convert it into libtmp_with_script.so.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
