LFS檢查工具鏈檢查

轉自: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 .

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章