Can gdb make a function pointer point to another location?

前端 未结 10 1411
庸人自扰
庸人自扰 2020-12-23 14:47

I\'ll explain:

Let\'s say I\'m interested in replacing the rand() function used by a certain application.

So I attach gdb to this process and ma

10条回答
  •  一整个雨季
    2020-12-23 15:12

    This question intrigued me, so I did a little research. What you are looking for is a 'dll injection'. You write a function to replace some library function, put it in a .so, and tell ld to preload your dll. I just tried it out and it worked great! I realize this doesn't really answer your question in relation to gdb, but I think it offers a viable workaround.

    For a gdb-only solution, see my other solution.


    // -*- compile-command: "gcc -Wall -ggdb -o test test.c"; -*-
    // test.c
    
    #include "stdio.h"
    #include "stdlib.h"
    
    int main(int argc, char** argv)
    {
        //should print a fairly random number...
        printf("Super random number: %d\n", rand());
    
        return 0;
    }
    

    / -*- compile-command: "gcc -Wall -fPIC -shared my_rand.c -o my_rand.so"; -*-
    //my_rand.c
    
    int rand(void)
    {
        return 42;
    }
    

    compile both files, then run: LD_PRELOAD="./my_rand.so" ./test

    Super random number: 42

提交回复
热议问题