GJDuck / e9patch

A powerful static binary rewriting tool
GNU General Public License v3.0
986 stars 67 forks source link

Do we need the parallel libc? #54

Closed joshop closed 2 years ago

joshop commented 2 years ago

The docs claim that libc can't be used in call instrumentation, and that you need to use the parallel libc for e.g. malloc. If you need to manipulate some memory that the main program needs to be able to deallocate or whatever, that probably wouldn't work. That being said, you could:

GJDuck commented 2 years ago

You can call glibc functions if you want to, but it takes some extra steps:

  1. Include stdlib.c with LIBDL defined:

    #define LIBDL
    #include "stdlib.c"
  2. In the init() function, use dlinit()/dlopen()/dlsym() get the pointer to desired libc function(s), e.g.:

    static void *free_ptr;    // global
    void init(int argc, char **argv, char **envp, void *dynamic)
    {
        dlinit(dynamic);
        void *handle = dlopen("libc.so.6", RTLD_LAZY);
        free_ptr = dlsym(handle, "free");    // free() function
        ...
    }
  3. Call the function pointer(s) in your instrumentation using dlcall():

    dlcall(free_ptr, arg);

Please see the Call Trampoline Dynamic Loading section in the E9Tool User's Guide for more information.