LD_PRELOAD not working with my program

拥有回忆 提交于 2019-12-10 23:37:42

问题


For testing LD_PRELOAD, I wrote my own getpid, which prints something before calling the original getpid using dlsym. The code is given below.

#define _GNU_SOURCE

#include <unistd.h>
#include <stdio.h>
#include <dlfcn.h>

typedef pid_t (*getpidType)(void);

pid_t getpid(void)
{
    printf("Hello, getpid!\n");
    getpidType f = (getpidType)dlsym(RTLD_NEXT, "getpid");
    return f();
}

However when I use such getpid in my program and run it using LD_PRELOAD, by typing LD_PRELOAD=./prelib.so ./prog, I get the following error.

./prog: symbol lookup error: ./prelib.so: undefined symbol: dlsym

But If I do LD_PRELOAD=./prelib.so bash -c 'echo $$', there is no such error. Any idea what is going on here.


回答1:


Linking it with libdl.so.2 by using -ldl in the makefile solved the problem.



来源:https://stackoverflow.com/questions/10703413/ld-preload-not-working-with-my-program

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