#!/usr/bin/perl -w =pod =head1 dep-libs.pl -- Create a Listing Of Dependent Libraries Resolves the output of C for a binary. Intended usage is to figure out the libraries and symlinks which need to be copied/replicated in order to run a program in a C environment. =head2 Command Line C Runs C and locates the actual symlinks and libraries which are referenced. More than one I can be specified, and their library references will be deduplicated. Outputs their fully-qualified names to STDOUT. The output can be collected to a file and used as the input to C to create a kit which can then be installed in the chrooted environment (the program names will be included as well). =head2 Copyright and License Copyright (c) 2009 by Fred Morris/Fred Morris Consulting, Seattle, WA, USA =head3 Contact Information Email: m3047@inwa.net Telephone: 206.297.6344 Web site: http://www.inwa.net/~m3047/contact.html =head3 License Licensed under the I. User assumes all liability for use and for verifying correct operation. Correct operation is not guaranteed, no warranty is expressed or implied. =cut use strict; my %Libs; foreach my $binary (@ARGV) { if (!(-x $binary)) { warn "$binary does not exist/is not executable."; next; } my $ldd_out = `ldd $binary`; foreach my $lib (map { chomp; m/=>\s(\S+)\s/; $1; } split "\n", $ldd_out) { next unless $lib; next unless ((-f $lib) || (-l $lib)); $Libs{$lib} = 1; } $Libs{$binary} = 1; } my @LibKeys = keys %Libs; foreach my $reference (@LibKeys) { while (-l $reference) { my $ls_out = `ls -l $reference`; $ls_out or die "ls -l $reference failed!"; my ($new_ref) = $ls_out =~ m/$reference\s*->\s*(\S+)/; if (! $new_ref) { warn "$reference did not resolve."; next; } if ($new_ref !~ m|^/|o) { my @path = split '/', $reference; pop @path; # Drop the file. while ($new_ref =~ s|\.\./||o) { pop @path; } push @path, $new_ref; $new_ref = join '/', @path; } $Libs{$new_ref} = 1; $reference = $new_ref; } } printf "%s\n", join "\n", keys %Libs; exit(0);