Setting my lib for LD_PRELOAD makes some processes produce loader errors

被刻印的时光 ゝ 提交于 2019-12-03 02:30:41

I assume that your a.out file is a shared object and not a executable and move on...

dlsym() is a function from the libdl library, which usually resides in the libdl.so.2 shared object on modern Linux systems.

I'll hazzard a guess that your a.out shared object is not linked to libdl. That means that when you preload in a simple binary like uname that does not pull in a lot of other libraries, libdl.so.2 may not be pulled in and you get an undefined symbol error.

If, on the other hand, you preload it to a binary that is linked to and finally pulls in libdl.so.2, your shared object works fine.

I'd check with ldd if your own shared object is linked against libdl as it should, and also what libraries are directly or indirectly pulled in when uname and ls run.

EDIT:

I just confirmed this. The way to fix this error is to link your shared object against libdl. Adding -ldl to its LDFLAGS should do the trick.

I am unable to comment to the accepted answer, however it is worth to mention here that one can encounter issue of not having libdl.so.2 linked properly when -ldl is used in front of compile command (assuming that linking and compilation is executed by the same command; this is possible since LD_PRELOAD libs are usually based on one source file).

So call gcc with -ldl at the end:

gcc -shared -fPIC fakeuname.c -o libfakeuname.so -ldl

In my case, having -ldl at front resulted in error same as in question:

uname: symbol lookup error: ./libfakehostname.so: undefined symbol: dlsym

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!