gcc - Using LD_PRELOAD to overload call to a C function of a shared library -
i'm following this answer override call c function of c library.
i think did correctly, doesn't work:
i want override "dibopen"-function. code of library pass ld_preload environment-variable when running application:
dibstatus dibopen(void **ctx, enum board b) { printf("look @ me, wrapped\n"); static dibstatus (*func)(void **, enum board) = null; if(!func) func = dlsym(rtld_next, "dibopen"); printf("overridden!\n"); return func(pcontextaddr, boardtype, boardhdl); }
the output of nm lib.so | grep dibopen
shows
000000000001d711 t dibopen
when run program this
ld_preload=libpreload.so ./program
i link program
-ldl ldd program
not show link libdl.so
it stops with
symbol lookup error: libpreload.so: undefined symbol: dlsym
. can debug further? mistake?
when create shared library (whether or not used in ld_preload
), need name of libraries it needs resolve dependencies. (under circumstances, dlopen
ed shared object can rely on executable provide symbols it, best practice not rely on this.) in case, need link libpreload.so
against libdl
. in makefile-ese:
libpreload.so: x.o y.o z.o $(cc) -shared -wl,-z,defs -wl,--as-needed -o $@ $^ -ldl
the option -wl,-z,defs
tells linker should issue error if shared library has unresolved undefined symbols, future problems of type caught earlier. option -wl,--as-needed
tells linker not record dependency on libraries don't satisfy undefined symbols. both of these should on default, historical reasons, aren't.