Bra*_*don 0 c++ shared-libraries
#include <iostream>
#include <cstring>
#include <dlfcn.h>
#include <fcntl.h>
#include <X11/X.h>
#include <X11/Xlib.h>
void* Module = nullptr;
typedef int (*XNextEventPtr)(Display *display, XEvent *event_return);
XNextEventPtr XNextEventHook = nullptr;
extern "C" int XNextEvent(Display* display, XEvent* event_return)
{
if (event_return)
{
event_return->xany.send_event = false;
}
if (XNextEventHook)
{
return XNextEventHook(display, event_return);
}
return 0;
}
void __attribute__((constructor)) initialize() //DLLMain.
{
char Root[256] = {0};
strcat(Root, "/usr/lib");
#if defined(__x86_64__)
strcat(Root, "/x86_64-linux-gnu");
#else
strcat(Root, "/i386-linux-gnu");
#endif // defined
strcat(Root, "/libX11.so");
Module = dlopen(Root, RTLD_GLOBAL | RTLD_LAZY);
std::cout<<"Loaded"<<std::flush;
XNextEventHook = reinterpret_cast<XNextEventPtr>(dlsym(Module, "XNextEvent")); //some reason RTLD_NEXT didn't find it.
if (XNextEventHook)
{
std::cout<<"Found XNextEvent"<<std::flush;
}
}
void __attribute__((destructor)) deinitialize() //DLLMain.
{
if (Module)
{
dlclose(Module);
Module = nullptr;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我做:
LD_PRELOAD="~/Desktop/MyHook.so" /usr/lib64/firefox
Run Code Online (Sandbox Code Playgroud)
它会崩溃 但是,我更换std::cout和std::cerr使用printf或者perror,它完美的作品!
有没有理由std::cout让应用程序崩溃?
似乎当我使用C++编写的应用程序加载模块时,它加载正常.但是如果我用C编写的应用程序加载模块,它就会崩溃.
想法?
| 归档时间: |
|
| 查看次数: |
559 次 |
| 最近记录: |