Ansi C patch using dlsym compiles OK under Linux but does not work on Mac Os X

I have a small patch that can be added to a specific application and track the calls of some functions. Among them are malloc () and open (). I use dlsym to keep the pointer to the source character and replace the function name with my own. It compiles and works fine under Linux. Here is the code:

#define _GNU_SOURCE
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <dlfcn.h>

/**
 * Interponemos nuestra funcion open
 * 
 * @param   char*    filename
 * @param   int      flags
 **/

int open(char * filename, int flags)
{
    static int (*real_open)(char*, int) = NULL;
    if (!real_open)
        real_open = dlsym(RTLD_NEXT, "open");

    // Entero
    int p = real_open(filename, flags);
    fprintf(stderr, "Abrimos %s) = %i\n", filename, flags);

    // Devolvemos
    return p;
}

//--------------------------------------------------------

/**
 * Interponemos nuestra funcion malloc
 * 
 * @param   size_t    size
 */

void* malloc(size_t size)
{
    static void* (*real_malloc)(size_t) = NULL;
    if (!real_malloc)
        real_malloc = dlsym(RTLD_NEXT, "malloc");

    void *p = real_malloc(size);

    // Memoria reservada
    fprintf(stderr, "Reserva de memoria (%d) = %p\n", size, p);

    // Devolvemos
    return p;
}

Then I will compile it with the following instruction, creating pi.so.

gcc -Wall -O2 -fPIC -shared -ldl -o pi.so pi.c 

And then I use the LD_PRELOAD directive to implement it in any application.

LD_PRELOAD=/home/.../injection/pi.so <binary>

And it excites wonderfully under Linux! But when I get home and try to compile it using GCC for Mac, it will not compile and the LD_PRELOAd directive does not work. What should i change? Thank you very much.

+3
3

DYLD_INSERT_LIBRARIES LD_PRELOAD, (1... *).

, . , , DYLD_INTERPOSE. (, open my_open) , . , .

, Linux, DYLD_FORCE_FLAT_NAMESPACE.

, . dyld.

, :

#define _GNU_SOURCE
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <dlfcn.h>
#include <mach-o/dyld-interposing.h>

/**
 * Interponemos nuestra funcion open
 * 
 * @param   char*    filename
 * @param   int      flags
 **/

int my_open(char * filename, int flags)
{
    // Entero
    int p = open(filename, flags);
    fprintf(stderr, "Abrimos %s) = %i\n", filename, flags);

    // Devolvemos
    return p;
}
DYLD_INTERPOSE(my_open, open)

//--------------------------------------------------------

/**
 * Interponemos nuestra funcion malloc
 * 
 * @param   size_t    size
 */

void* my_malloc(size_t size)
{
    void *p = malloc(size);

    // Memoria reservada
    fprintf(stderr, "Reserva de memoria (%d) = %p\n", size, p);

    // Devolvemos
    return p;
}
DYLD_INTERPOSE(my_malloc, malloc)
+4

Mac OS X (10.6.4) LD_PRELOAD - , , , . Linux.

, :

$ man dyld

...

DYLD_INSERT_LIBRARIES      , , ,               . ,                .                , , ,                DYLD_FORCE_FLAT_NAMESPACE .

+1

OSX DYLD_INSERT_LIBRARIES env. LD_PRELOAD.

However, I am sure that you will not get overrides to work. The closest I can find is that you will need to do some magic assembly tricks that someone neatly packaged into code under mach_override here: http://extendamac.svn.sourceforge.net/viewvc/extendamac/trunk/code/

+1
source

Source: https://habr.com/ru/post/1766908/


All Articles