我在最新的 Ubuntu Linux 上。
这是一个共享库,其函数应该在卸载时调用:
shared.c
:
#include <fcntl.h>
#include <sys/stat.h>
void fini() {
open("fini", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
}
这是用
编译的gcc -shared -fPIC -Wl,-fini=fini shared.c -o shared.so
然后我这样做:
$ LD_PRELOAD=$PWD/shared.so dash -c "> /dev/null"
$ echo $?
0
$ ls fini
ls: cannot access 'fini': No such file or directory
所以...没有调用卸载函数...
但是如果我替换为
$ LD_PRELOAD=$PWD/shared.so dash -c "touch /dev/null"
$ ls fini
fini
所以通过这个破折号调用(使用触摸命令而不是重定向),卸载函数被调用,正如预期的那样。
第一种情况为什么不调用卸载函数??
顺便说一下:我知道库确实被预加载了,因为如果我包含
init
例程,它在两种情况下都会执行,但在第一种情况下不会执行fini
。
根据 Marco Bonelli 的要求添加:
$ file $(which dash)
/usr/bin/dash: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=f7ab02fc1b8ff61b41647c1e16ec9d95ba5de9f0, for GNU/Linux 3.2.0, stripped
$ ldd $(which dash)
linux-vdso.so.1 (0x00007ffd931c0000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6b19e84000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6b1a0ec000)
我应该把它作为评论,但我把它作为格式化目的的答案。
问题不在于
redirection
和touch
之间的区别,因为在你的dash/touch运行中,shared.so被加载了两次,一次用于dash,一次用于touch。
fini
只用 touch
开火。
你可以看到这两个运行的差异:
$ LD_PRELOAD=$PWD/shared.so dash /dev/null
# fini is not called
$ LD_PRELOAD=$PWD/shared.so touch /dev/null
# fini is called
所以这与
dash
有关。
希望这能为你的调查提供更多的材料。