如何在ELF文件中挂钩c函数,在Linux中使用库.so?

时间:2017-09-18 20:50:44

标签: linux hook elf

我有可执行的ELF文件,它使用库lib.so,而这个ELF从lib.so调用函数Func1

我需要挂钩这个函数,所以它将替换为我的函数,它会做其他事情。如何在不更改此可执行文件的情况下执行此操作?

1 个答案:

答案 0 :(得分:3)

看看LD_PRELOAD。该环境变量可以设置为elf共享对象,该对象将被注入可执行文件的地址空间。该预加载对象中的函数可以替换其他对象中的函数。在你的情况下,只要lib.so没有构建public class Main { public static void main(String[] args) { Base<Child> b = new Base<>(); // b.get() returns an instance of Base, not Child (however it's mistakenly cast to Child) Child1 c = b.get(); } public static class Base<T extends Base>{ public T get(){ return (T) this; } } public static class Child extends Base { } } ,这将有效。符号链接器选项可解析链接传递中的内部调用。所以,如果lib.so包含并调用-Bsymbolic并构建func1,那么替换该调用非常棘手,您可能最终必须更改lib.so。