File: ldd.fakechroot.pl

package info (click to toggle)
fakechroot 2.20.1%2Bds-18
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,904 kB
  • sloc: ansic: 7,688; sh: 1,917; makefile: 375; perl: 191; java: 5
file content (258 lines) | stat: -rwxr-xr-x 6,069 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!@PERL@

# fakeldd
#
# Replacement for ldd with usage of objdump
#
# (c) 2003-2010, 2013, 2016 Piotr Roszatycki <dexter@debian.org>, LGPL

use strict;

$ENV{LANG} = $ENV{LC_ALL} = 'C';

my @Libs = ();
my %Libs = ();

my $Status = 0;
my $Dynamic = 0;
my $Format = '';

my $Ldsodir = "/lib";
my @Ld_Library_Path = qw(/usr/lib /lib /usr/lib32 /lib32 /usr/lib64 /lib64);

my $Cwd = `pwd`;
chomp $Cwd;

my $Base = defined $ENV{FAKECHROOT_BASE_ORIG} ? $ENV{FAKECHROOT_BASE_ORIG} : '';

sub ldso {
    my ($lib) = @_;

    return if $Libs{$lib};

    my $path;

    if ($lib =~ m{^/}) {
        $path = $lib;
    }
    else {
        foreach my $dir (@Ld_Library_Path) {
            next unless -f "$dir/$lib";

            my $badformat = 0;
            local *PIPE;
            open PIPE, "objdump -p '$dir/$lib' 2>/dev/null |";
            while (my $line = <PIPE>) {
                if ($line =~ /file format (\S*)$/) {
                    $badformat = 1 unless $1 eq $Format;
                    last;
                }
            }
            close PIPE;

            next if $badformat;

            $path = "$dir/$lib";
            last;
        }
    }

    push @Libs, $lib;
    if (-f $path) {
        $path =~ s{^\Q$Base/\E}{/} if $Base;
        $Libs{$lib} = $path;
        objdump($path);
    }
}


sub objdump {
    my (@files) = @_;

    foreach my $file (@files) {
        $file = $file =~ m{^/} ? "$Base$file" : "$Cwd/$file";

        local *PIPE;
        open PIPE, "objdump -p '$file' 2>/dev/null |";

        while (my $line = <PIPE>) {
            $line =~ s/^\s+//;

            if ($line =~ /file format (\S*)$/) {
                if (not $Format) {
                    $Format = $1;

                    if ($^O eq 'linux') {
                        if ($Format =~ /^elf64-(x86-64|sparc)$/) {
                            $Ldsodir = "/lib64";
                        }
                        elsif ($Format =~ /^elf32-x86-64$/) {
                            $Ldsodir = "/libx32";
                        }

                        if ($Format =~ /^elf64-/) {
                            push @Libs, 'linux-vdso.so.1';
                            $Libs{'linux-vdso.so.1'} = '';
                        }
                        else {
                            push @Libs, 'linux-gate.so.1';
                            $Libs{'linux-gate.so.1'} = '';
                        }
                    }

                    foreach my $lib (split /[:\s]/, $ENV{LD_PRELOAD}||'') {
                        ldso($lib);
                    }
                }
                else {
                    next unless $Format eq $1;
                }
            }
            if (not $Dynamic and $line =~ /^Dynamic Section:/) {
                $Dynamic = 1;
            }

            next unless $line =~ /^ \s* NEEDED \s+ (.*) \s* $/x;

            my $needed = $1;
            if ($needed =~ /^ld(-linux)?(\.|-)/) {
                $needed = "$Ldsodir/$needed";
            }
            ldso($needed);
        }
        close PIPE;
    }
}

# mips64el, ppc64el and s390x do not list the linker itself
# if it's missing, obtain it from the .interp section
#
# mips64el: /lib64/ld.so.1
# ppc64el: /lib64/ld64.so.2
# s390x: /lib/ld64.so.1
sub elfinterp {
    my $file = shift;
    my $res = '';
    local *PIPE;
    open PIPE, "objdump -sj .interp '$file' 2>/dev/null |";
    while (my $line = <PIPE>) {
        if ( $line !~ /^ [a-f0-9]+ ([a-f0-9][a-f0-9][a-f0-9 ]{6} [a-f0-9 ]{8} [a-f0-9 ]{8} [a-f0-9 ]{8})  /) {
            next;
        }
        $line = $1;
        $line =~ s/ //g;
        $line =~ s/(..)/chr(hex($1))/eg;
        $res .= $line;
    }
    close PIPE;

    # remove trailing NUL byte
    $res =~ s/\000$//;

    # only add if it is missing
    if ( $res && !exists $Libs{$res} ) {
        push @Libs, $res;
        $Libs{$res} = '';
    }
}


sub load_ldsoconf {
    my ($file) = @_;
    my @paths;

    local *FH;
    open FH, $file;
    while (my $line = <FH>) {
        chomp $line;
        $line =~ s/#.*//;
        next if $line =~ /^\s*$/;

        if ($line =~ /^include\s+(.*)\s*/) {
            my $include = $1;
            foreach my $incfile (glob $include) {
                load_ldsoconf($incfile);
            }
            next;
        }

        push @paths, $line;
    }
    close FH;

    unshift @Ld_Library_Path, @paths;
}


MAIN: {
    my @args = @ARGV;

    if (not `sh -c 'command -v objdump'`) {
        print STDERR "fakeldd: objdump: command not found: install binutils package\n";
        exit 1;
    }

    load_ldsoconf('/etc/ld.so.conf');
    unshift @Ld_Library_Path, split(/:/, $ENV{LD_LIBRARY_PATH}||'');

    while ($args[0] =~ /^-/) {
        my $arg = $args[0];
        shift @args;
        last if $arg eq "--";
    }

    if (not @args) {
        print STDERR "fakeldd: missing file arguments\n";
        exit 1;
    }

    foreach my $file (@args) {
        %Libs = ();
        $Dynamic = 0;

        if (@args > 1) {
            print "$file:\n";
        }

        my $file_in_chroot = $file =~ m{^/} ? "$Base$file" : "$file";

        if (not -f $file_in_chroot) {
            print STDERR "fakeldd: $file: No such file or directory\n";
            $Status = 1;
            next;
        }

        objdump($file);
        elfinterp($file_in_chroot);

        if ($Dynamic == 0) {
            print "\tnot a dynamic executable\n";
            $Status = 1;
        }
        elsif (scalar %Libs eq "0") {
            print "\tstatically linked\n";
        }

        my $address = '0x' . '0' x ($Format =~ /^elf64-/ ? 16 : 8);

        my %seen;

        foreach my $lib (@Libs) {
            next if $seen{$lib}++;
            if ($lib =~ m{^/} or $lib =~ /^linux-/) {
                printf "\t%s (%s)\n", $lib, $address;
            }
            elsif ($Libs{$lib}) {
                printf "\t%s => %s (%s)\n", $lib, $Libs{$lib}, $address;
            }
            else {
                printf "\t%s => not found\n", $lib;
            }
        }

    }
}

END {
    $? = $Status;
}