PowerOverride is a lib to help you override symbols that loaded by dlsym
Just compile to a shared lib, and use LD_PRELOAD.
Just add_subdirectory().
Note: this project's CMakeLists is organized by
target_xxx
API. i.e.target_foo
must linkpoweroverride
to let src files intarget_foo
access header files ofpoweroverride
- Only override functions loaded by
dlsym
inlibfoo.so
. Just define a function by using POWER_OVERRIDE
#include
"poweroverride/power_override.hpp"
POWER_OVERRIDE(void, foo, (int a, int b), "libfoo.so") { // the filename rule is same as `dlopen`
auto raw = RawFn();
do_sth();
}
- If
libfoo.so
is also linked by some part of your code, you may want to add a normal function to override these linked symbol. Because PowerOverride will not override symbols that are resloved by dynamic linker.
extern "C"{
void foo(int a, int b){
auto raw = dlsym(RTLD_NEXT, "foo");
do_sth();
}
}
- If you dlopend many lib that contains same symbol, e.g.
color()
inlibapple.so
andliborange.so
You may want to add more POWER_OVERRIDE, to override other libs. because POWER_OVERRIDE only override one shared obj.
#include
"poweroverride/power_override.hpp"
POWER_OVERRIDE(void, foo, (int a, int b), "libbar.so") {
auto raw = RawFn(); // Note, this RawFn() will return raw function pointer from "libbar.so" , not "libfoo.so"
do_sth();
}
- All override are under your control. If you want to share some code, you could abstract a
do_sth()
just like previous example. If Not, just implement function as you want.