Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2009003
  • 博文数量: 369
  • 博客积分: 10093
  • 博客等级: 上将
  • 技术积分: 4271
  • 用 户 组: 普通用户
  • 注册时间: 2005-03-21 00:59
文章分类

全部博文(369)

文章存档

2013年(1)

2011年(2)

2010年(10)

2009年(16)

2008年(33)

2007年(146)

2006年(160)

2005年(1)

分类:

2006-11-16 22:14:47

在制作小型系统的时候,我们往往在系统库的取舍上存在一些问题,多了浪费空间,少了又不能使程序正常运行。除了重新编译安装程序到新根,如果host和target机器的架构相同,我们完全可以从host系统上拷贝相关文件到新的根目录上,通过此种方式“移植”程序。Linux系统下的ldd能帮我们找到特定程序所依赖的所有系统库:

xiaosuo@gentux utils $ ldd /bin/ls
        linux-gate.so.1 => (0xffffe000)
        librt.so.1 => /lib/librt.so.1 (0xb7fbf000)
        libc.so.6 => /lib/libc.so.6 (0xb7e9b000)
        libpthread.so.0 => /lib/libpthread.so.0 (0xb7e85000)
        /lib/ld-linux.so.2 (0xb7fdc000)


我们得到的输出一共有三种,一种直接是文件的绝对路径,一种是相对路径后面跟绝对路径,另外的就是上面输出的第一行,也是最奇怪的,它没有绝对路径,并且如果你尝试从你的系统上找到它的话,你将永远不会成功,事实上他确实不是一个实实在在存在着的动态链接库。它的存在和Linux系统调用的实现方式有关,更过请看参考链接。

有了ldd输出的依赖库,一切都简单了,简单地分析一下输出,然后拷贝就行了,当然不能手动做这些粗活,脏活累活都应该留给脚本:

File: insprg

#!/bin/bash

#


help()
{
        cat <<EOF
$0: Intall the program and all the necessary libraries
        depended by the program to a special root directory.

Usage: $0 prog_path [new_root]
EOF
}

__basename()
{
        echo ${1##*/}

}

__dirname()
{
        ext=`__basename $1`
        echo ${1%$ext}
}

__is_abs()
{
        echo $1 | grep -q -e "^/.*"
        return $?
}

# Copy the file from src to dst, create the directorys if necessary

insfile()
{
        src=$1
        dst=$2
        [ -e $src ] && [ ! -d $src ] || return 1
        dst_dir=`__dirname $dst`
        [ -d $dst_dir ] || mkdir -p $dst_dir
        cp -dp $src $dst
        return 0
}

# Copy file to new root directory, If this is a link, copy the

# real file too.

insfile2()
{
        src=$1
        nroot=$2
        insfile $src $nroot/$src || return 1
        [ -L $src ] || return 0
        nsrc=`ls -l $src`
        nsrc=${nsrc##*> }

        if ! __is_abs $nsrc; then
                src_dir=`__dirname $src`
                nsrc=$src_dir/$nsrc
        fi
        insfile2 $nsrc $nroot
        return 0
}

inslibs()
{
        prg=$1
        nroot=$2
        [ -e $prg ] || return 1
        ldd $prg | while read line; do
                lib=`echo $line | cut -f 1 -d " "`
                __is_abs $lib || lib=`echo $line | cut -f 3 -d " "`
                __is_abs $lib || continue
                [ -e $nroot/$lib ] && continue
                insfile2 $lib $nroot
        done
        return 0
}

if [ $# -lt 1 ] || [ $# -gt 2 ]; then

        help $0
        exit 1
fi
prg=$1
if [ $# -eq 2 ]; then

        new_root=$2
else
        new_root=`pwd`
fi
insfile2 $prg $new_root
inslibs $prg $new_root


小试一下牛刀:

xiaosuo@gentux utils $ ./insprg /bin/ls nroot
xiaosuo@gentux utils $ find nroot/
nroot/
nroot/bin
nroot/bin/ls
nroot/lib
nroot/lib/librt.so.1
nroot/lib/librt-2.5.so
nroot/lib/libc.so.6
nroot/lib/libc-2.5.so
nroot/lib/libpthread.so.0
nroot/lib/libpthread-2.5.so
nroot/lib/ld-linux.so.2
nroot/lib/ld-2.5.so


大工告成,脚本真是个好东西!

参考链接:


Linux 2.6 对新型 CPU 快速系统调用的支持
阅读(1321) | 评论(0) | 转发(0) |
0

上一篇:“苦难片”

下一篇:聚餐

给主人留下些什么吧!~~