程序如下:
- #!/usr/bin/perl
- use File::Find ();
- die "usage $0 file dir1 dir2 ..." if (@ARGV <2);
- $TOPDIR=`pwd`;
- chomp $TOPDIR;
- sub myfind
- {
- my $file;
- $fname=$_[0];
- eval { File::Find::find(sub{if($fname eq $_){$file=$File::Find::name;die "got";}}, @ARGV); };
- chdir $TOPDIR;
- return $file;
- }
- sub findinterp
- {
- my $file=$_[0];
- my $d1;
- my $f;
- open $f,qq(readelf -l $file |);
- while (<$f>)
- {
- if(/Requesting program interpreter: (\S+)\]/)
- {
- $d1="$1";
- }
- }
- close $f;
- return $d1;
- }
- sub findldd
- {
- my $file=$_[0];
- my @depends;
- my $f;
- open $f,qq(readelf -d $file |);
- while (<$f>)
- {
- if(/Shared library: \[(\S+)\]/)
- {
- my $d1;
- if(!exists $g{$1})
- {
- $g{$1} = 1;
- $d1=myfind($1);
-
- die "can not find $1\n" unless($d1);
- push @depends,$d1,findldd($d1);
- }
- }
- }
- close $f;
- return @depends;
- }
- for $name (findinterp($ARGV[0]),findldd($ARGV[0]))
- {
- $h{$name}=1;
- if( -l $name)
- {
- ($base=$name)=~s#[^/]*$##;
- $h{ $base . readlink($name) }=1;
- }
- }
- print qq(tar uf /tmp/$ARGV[0].tar $ARGV[0] \n);
- for (keys %h)
- {
- print qq(tar uf /tmp/$ARGV[0].tar $_ \n);
- }
- print qq(write to /tmp/getlib\n);
- print qq(please run "/tmp/getlib"\n);
- for (keys %h)
- {
- open STDOUT,">>/tmp/getlib";
- print qq(tar uf /tmp/$ARGV[0].tar $_ \n);
- }
- chmod 0777 ,qq(/tmp/getlib);
- open F,qq(>/tmp/runme);
- print F <<"AAA"
- D=\$(dirname \$0)
- \$D/lib/ld-linux.so.2 --library-path \$D/lib:\$D/usr/lib $ARGV[0] \$@
- AAA
- ;
- close F;
- chmod 0777, qq(/tmp/runme);
- print qq(tar uf /tmp/$ARGV[0].tar /tmp/runme \n);
由于不同的系统对/lib/ld-2.11.2.so 的软链接,名字稍有不同,
故写入 runme的 /lib/ld-linux.so.2 可根据实际系统修改调整
运行:
perl ./ldd-pack1 二进制文件 寻找依赖库的路径
例如:
- perl ./ldd-pack1 rdesktop /lib /usr/lib
运行完之后,会在 /tmp 目录下生成 getlib 文件,执行 /tmp/getlib 之后,在/tmp 下会得到 rdesktop .tar 文件,里面包括, rdesktop 所有依赖库,rdesktop 及 runme
阅读(613) | 评论(0) | 转发(0) |