LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 879|回复: 3

LD_PRELOAD 变量

[复制链接]
发表于 2009-6-3 23:25:04 | 显示全部楼层 |阅读模式
有很多程序会用到这个变量:
aoss 用于使访问 oss 设备文件的程序转而访问 alsa 设备文件
sbox2 用于重新映射路径,建立一个伪装成本地系统的交叉编译环境
fakeroot 用于给普通用户伪装一个 root 权限环境,建立安装包的好助手
compiz 偶的 ati 卡需要执行 "LD_PRELOAD=/usr/lib/opengl/xorg-x11/lib/libGL.so compiz"才能启动,暂不明原因
checkinstall 等很多包管理器,用于截获文件系统操作,从而分析得到打包需要的文件列表

最近在琢磨:
能否用类似方法,完成一个在 x86_64 系统环境,不用执行 "linux32 chroot /x86_rootfs",直接执行 x86 程序的 程序。
或者已经有这样的软件包存在,只是还没被发现,dchroot、schroot是两个可疑目标。
原因如下:
multilib 系统环境,系统结构性复杂,吾恶之。
x86_64 系统环境下,用 linux32 chroot 结合 init,可以给出一个同时运行 x86_64 和 x86 两套系统的环境,但两套系统集成不如 multilib 系统,不能方便的在 x86_64 的桌面环境 启动 x86 的应用。
 楼主| 发表于 2009-6-3 23:28:46 | 显示全部楼层

一些资料

http://en.wikipedia.org/wiki/Dynamic_linker
Unix and Unix-likes

On Unix and Unix-like operating systems, the dynamic linker shared libraries vary. Two common ones are ld.so on BSD and ld-linux.so on Linux. These typically change their behaviour based on a common set of environment variables, including LD_LIBRARY_PATH and LD_PRELOAD.

LD_PRELOAD

LD_PRELOAD instructs the loader to load additional libraries into a program, beyond what was specified when it was compiled. It allows users to add or replace functionality when they run a program and can be used for beneficial purposes, such as implementing userspace virtual file systems or debuggers, or malicious purposes, to run code the program authors didn't intend. For this reason, LD_PRELOAD is often ignored when a program is running setuid and under a few other circumstances. More generally, this technique is called DLL injection.
Examples of programs using LD_PRELOAD: authbind, IPv6 CARE, fakeroot.


http://www.linuxsir.cn/bbs/post264482-16.html
     7.为应用程序提供动态转载服务。

环境变量LD_PRELOAD设置共享库名或者用":"把文件名隔开。动态连接器在
任何那些请求的共享库之前把环境变量LD_PRELOAD的共享库装载到进程地址
空间去。例如:

# LD_PRELOAD=./mylibc.so myprog

这里./mylibc.so将第一时间map到程序myprog的空间。因为动态连接器在找
寻标号的时候总是使用第一次碰到的标号,所以我们可以使用LD_PRELOAD来
覆盖标准共享库中的函数。这个特性对程序员来说是很有用的,可用来在还
没有建好整个共享库的时候对单个函数功能先做调试实验。

我们可以这样:
#gcc -c -fPIC -O3 print.c
#gcc -shared print.o -o print.so.1.0
创建自己的共享连接库
回复 支持 反对

使用道具 举报

发表于 2009-6-13 07:58:46 | 显示全部楼层
谢谢,最近忽然对 LDFLAGS 也感兴趣,是否可以更前卫,带来更激进的效果?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-6-13 10:58:04 | 显示全部楼层
LDFLAGS 讨论的满多的,不知还能不能玩出什么新花样。
看到好多人说 "-s" 是安全的,可以直接完成包括 库文件的 strip 动作,但看过 ld manpage 很难相信安全,有时间验证下。

偶上面是受 sbox2 和 qemu 用户空间模拟 启发,想做个类似程序。
x86 环境下,qemu-arm -L /arm-rootfs/lib/ld-linux.so.2 /arm-rootfs/bin/echo a 可以直接运行。
x86_64 环境下,qemu-i386 -L /i686-rootfs/lib/ld-linux.so.2 /i686-rootfs/bin/echo a 可以直接运行。
如果能把用户空间模拟去掉,会好很多。
x86_64 下直接用 i686 的 ld-linux.so 调用执行 i686 的动态连接程序是可行的,但要维护一大堆环境变量。

其实,偶是想在 x86_64 环境下直接运行 i686 的 wine,但又不想用静态连接版本。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表