Skip to content
Advertisement

How can I hook c function from ELF file, that uses library .so in Linux ?

I have executable ELF file, that uses library lib.so, and this ELF invokes function Func1 from lib.so

I need to hook this function, so it will be replaced with my function, that does something else. How can I do this without changing this executable file ?

Advertisement

Answer

Take a look at LD_PRELOAD. That environment variable can be set to an elf shared object that will be injected into the address space of your executable. Functions in that preloaded object can replace functions in other objects. In your case this will work so long as the lib.so is not built -Bsymbolic. The symbolic linker option resolves internal calls within the link pass. so, if lib.so contains and calls func1 and is built -Bsymbolic, then it’s quite tricky to replace that call and you probably will end up having to change lib.so.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement