2

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.

4

1 回答 1

5

libdl.so.2通过-ldl在makefile中使用链接它解决了这个问题。

于 2012-05-22T14:17:51.843 回答