LD_PRELOAD不适用于我的程序 [英] LD_PRELOAD not working with my program
问题描述
为了测试LD_PRELOAD,我编写了自己的 getpid
,它在使用dlsym调用原始 getpid
之前打印了一些内容。代码如下。
#define _GNU_SOURCE
#include< unistd.h>
#include< stdio.h>
#include< dlfcn.h>
typedef pid_t(* getpidType)(void);
pid_t getpid(void)
{
printf(Hello,getpid!\\\
);
getpidType f =(getpidType)dlsym(RTLD_NEXT,getpid);
return f();
}
但是,当我使用 getpid
在我的程序中,并通过键入 LD_PRELOAD =。/ prelib.so ./prog
使用LD_PRELOAD运行它,出现以下错误。
./ prog:符号查找错误:./prelib.so:undefined符号:dlsym
但是如果我做了 LD_PRELOAD =。/ prelib.so bash -c'echo $$'
,没有这样的错误。任何想法发生在这里。 c $ c>在makefile中使用 -ldl
解决了这个问题。
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.
Linking it with libdl.so.2
by using -ldl
in the makefile solved the problem.
这篇关于LD_PRELOAD不适用于我的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!