玩弄LD_LIBRARY_PATH

时间:2012-04-28 01:29:34

标签: linux gcc ld ld-preload

我读了this article recently,我尝试重写libc printf函数并执行以下操作: -

  • 创建一个使用printf打印this is a test(printer.c)
  • 的可执行文件
  • 创建一个带有自定义puts的打印文件muhahaha, this is a test(custom.c)
  • 创建目标文件gcc -fPIC -g -c -Wall custom.c
  • 创建一个so文件gcc -shared -Wl,-soname,libmystuff.so.1 -o libmystuff.so.1.0.1 custom.o
  • 我将包含so文件的目录添加到LD_PRELOAD环境变量中。 export LD_PRELOAD=$(pwd)
  • 尝试运行打印机

我认为muhahaha, this is a test会打印出来,但似乎我做错了。我有一些概念错了吗?或者我只是做错了什么?

[编辑]

所涉及的代码片段是: -

// printer.c
int main() {
printf("this is a test");
return 0;
}

// custom.c
void printf(char *t) {
puts("muhahaha, this is a test");
}

1 个答案:

答案 0 :(得分:1)

您应该在LD_PRELOAD环境变量中命名库,而不是目录。

export LD_PRELOAD=/path/to/libmystuff.so.1.0.1
相关问题