转自:http://www.linuxsir.org/bbs/showthread.php?t=244102
1.在做好 /tools -> /mnt/lfs/tools 下的第一遍工具链之后, chroot 之前, 检查 /tools 里是不是有宿主系统的影响,如果有,就是调整工具链失败,最好从调整工具链开始重来.
checkpath.pass1
#!/bin/sh checkso(){ file=$1 what=$2 if ! [ -d $file ] ; then ( ldd $file | egrep "$what" ) 2>/dev/null if [ $? -eq 0 ] ; then echo $file had been linked to a wrong so ; echo ; fi fi } checklink(){ file=$1 what=$2 if [ -L $file ] ; then if [ -r $file ] ; then ( ls -l $file | egrep "$what" ) 2>/dev/null 1>/dev/null if [ $? -eq 0 ] ; then ls -l $file ; echo $file links to a wrong path ; echo ; fi else echo $file cannot be read , may be a wrong link ; echo ; fi fi } if [ "${LD_LIBRARY_PATH}z" != "z" ] ; then echo LD_LIBRARY_PATH error; echo Please make sure to unset LD_LIBRARY_PATH before you do anything. echo I will unset LD_LIBRARY_PATH for you this time. unset LD_LIBRARY_PATH; fi for file in `find -L /tools` ; do checkso $file "(/usr)|( /lib)|( /bin)|( /sbin)|(not found)" checklink $file "(/usr)|( /lib)|( /bin)|( /sbin)" done ;
2.在 /usr 上的工具链全部完成, 已经不需要 /tools 了,这时检查 /usr /lib /bin 等是不是受到 /tools 的影响,同样如果有,就是调整工具链失败,最好从调整工具链开始重来.
如果检查通过的话,就可以 rm -rf /tools 了.
checkpath.final
#!/bin/sh checkso(){ file=$1 what=$2 if ! [ -d $file ] ; then ( ldd $file | egrep "$what" ) 2>/dev/null if [ $? -eq 0 ] ; then echo $file had been linked to a wrong so ; echo ; fi fi } checklink(){ file=$1 what=$2 if [ -L $file ] ; then if [ -r $file ] ; then ( ls -l $file | egrep "$what" ) 2>/dev/null 1>/dev/null if [ $? -eq 0 ] ; then ls -l $file ; echo $file links to a wrong path ; echo ; fi else echo $file cannot be read , may be a wrong link ; echo ; fi fi } if [ "${LD_LIBRARY_PATH}z" != "z" ] ; then echo LD_LIBRARY_PATH error; echo Please make sure to unset LD_LIBRARY_PATH before you do anything. echo I will unset LD_LIBRARY_PATH for you this time. unset LD_LIBRARY_PATH; fi for dir in "/bin /sbin /usr/bin /usr/sbin" ; do for file in `find $dir` ; do checkso $file "(/tools)|(not found)" checklink $file /tools done ; done ; for dir in "/lib /usr/lib" ; do for file in `find $dir -iname *so*` ; do checkso $file "(/tools)|(not found)" checklink $file /tools done ; done ;
原理:
ldd 一个程序时, 我们可以看见, 除了 ld-linux.so 其他程序都没有绝对路径, 可以通过 ld.so 的配置, LD_LIBRARY_PATH 和 /etc/ld.so.conf 改变.
在做好 /tools -> /mnt/lfs/tools 下的第一遍工具链之后, chroot 之前, /tools 里所有的 bin ,so ,如下:
ldd /tools/bin/msgfmt
libc.so.6 -> /tools/lib/libc.so.6
/tools/lib/ld-linux.so
/tools/lib/ld-linux.so 的配置文件是 /tools/etc/ld.so.conf , 在没有 /tools/etc/ld.so.conf 和 LD_LIBRARY_PATH 的情况下, 默认搜索 /tools/lib
在 /usr 上的工具链全部完成, 已经不需要 /tools 了,这时, 检查 /usr /lib 下的 bin 和 so
ldd /usr/bin/msgfmt
libc.so.6 -> /lib/libc.so.6
/lib/ld-linux.so
/lib/ld-linux.so 的配置文件是 /etc/ld.so.conf , 在没有 /etc/ld.so.conf 和 LD_LIBRARY_PATH 的情况下, 默认搜索 /lib 和 /usr/lib
我们必须确保 /tools 里所有都链接 /tools/lib/ld-linux.so , /usr /lib /bin 里的链接 /lib/ld-linux.so .
在上面的条件满足时, 只要没有 LD_LIBRARY_PATH 和 /etc/ld.so.conf , /tools/etc/ld.so.conf 的干扰, 我们就能保证, 所有的没有绝对路径的so, 都能链接到正确的位置:
/tools 里只用 /tools 里的 so
/usr /bin 里只用 /lib /usr/lib 里的 so
ld-linux.so 是 glibc 里的, 所以, 我们安装过 glibc 后, 立刻要调整工具链, 使 gcc (不管是 /tools 的还是 /usr 的) , 编译程序时使用正确的 ld-linux.so , 就是把 ld-linux.so 的绝对路径写入被编译的程序.
如果出错会怎样:
1. LD_LIBRARY_PATH=/lib:/usr/lib:/tools/lib , 这时 /tools 里的程序就会链接到 /lib:/usr/lib 里, 但可能因为我的 /tools 和 /usr 中配置差距较大, 必然 core dump.
ldd /tools/bin/bash
libc.so.6 -> /lib/libc.so.6
/tools/lib/ld-linux.so
/tools/bin/bash: core dump
2. LD_LIBRARY_PATH=/tools/lib:/lib:/usr/lib , 这时 就换 /usr/bin 里的程序 core dump 了.
ldd /usr/bin/ld
libc.so.6 -> /tools/lib/libc.so.6
/lib/ld-linux.so
/usr/bin/ld: core dump
在配置差距不大时, 混用不同来源的 so ,一般不会有问题, 我就经常 cp mdv 的 so 到 lfs 中使用, 但一定要注意配置差距不大, 比如 我的 mdv 和 lfs 都是最终版, 都是 gcc4.x 编译的.
而 lfs 中 的 /tools 和 /usr ,一个是临时的, 一个是最终版, /tools 中有许多参数未用, 像 nls 等, 混用, 几乎必然导致 core dump .