4 use File::Basename qw(&dirname);
12 open F,"$fname" or die $fname;
14 defined(my $r=<F>) or die $fname;
15 close F or die $fname;
20 # ==> build/Cadence-1.0.0-0.12.20200504git5787908.fc33.src.rpm.dtneeded <==
21 # /usr/lib/debug/.dwz/Cadence isdwzcommon 479079 NA
22 # /usr/bin/cadence-jackmeter /usr/lib/debug/.dwz/Cadence 717792 1329040
23 # /usr/bin/cadence-xycontroller /usr/lib/debug/.dwz/Cadence 1763616 2369832
24 # ==> build/CVector-1.0.3.1-21.fc33.src.rpm.dtneeded <==
25 # /usr/lib64/libCVector-1.0.3.so.2.0.0 nodwzcommon 28088 28896
26 for my $dtneededfn (glob "build/*.dtneeded") {
28 open F,$dtneededfn or die "$dtneededfn: $1";
32 my($fn,$dwzcommon,$dwzsize,$dtsize)=/^(\S+) (\S+) (\S+) (\S+)$/ or die "$dtneededfn: $_";
33 die if $dwzcommon eq "isdwzcommon" && $dtsize ne "NA";
36 die if $dwzcommon eq "isdwzcommon";
39 $$ref=[$dwzcommon,$dwzsize,$dtsize];
42 close F or die "close $dtneededfn: $1";
45 chdir "dtneeded.out" or die "dtneeded.out: $!";
54 die $File::Find::dir if $File::Find::dir=~m{/$};
55 my $binfn=$File::Find::name;
57 my $target=readlink $binfn or die $binfn;
59 my $ref=\$SYMLINK{$binfn};
63 #warn "$binfn,target=rel=$$ref\n";
65 my $base=dirname(".$binfn");
66 my $abs=File::Spec->rel2abs($target,$base);
67 $$ref="/".File::Spec->abs2rel($abs);
68 #warn "$binfn,base=$base,target=$target,abs=$abs,rel=$$ref\n";
73 die $binfn if $binfn!~m{^[.]/};
74 my $bin=readfile $binfn;
75 $binfn=~s{^[.]}{} or die;
76 my $rpath=($bin=~m{^.*\Q(R\E(?:UN)?\QPATH)\E\s*Library r(?:un)?path: \Q[\E(.*)\Q]\E$}m)[0];
78 my $dirname=$File::Find::dir;
79 die if $dirname!~s{^[.]/}{/};
80 $rpath=~s{\$ORIGIN}{$dirname}g;
82 my $soname=($bin=~m{^.*\Q(SONAME)\E\s*Library soname: \Q[\E(.*)\Q]\E$}m)[0];
85 "needed"=>[$bin=~m{^\s*0x0000000000000001\s*\Q(NEEDED)\E\s*\QShared library: [\E(.*)\Q]\E$}gm],
87 $h->{"rpath"}=$rpath if $rpath;
89 #warn "$binfn=".Dumper($h)."\n" if $binfn=~/libc.so.6/;
90 # my $total=keys %F; warn "$total...\n" if 0==$total%1000;
92 my $sonamefn=$File::Find::dir."/".$soname;
93 $sonamefn=~s{^[.]/}{/} or die $sonamefn;
94 my $ref=\$SONAME{$sonamefn};
95 # warn "soname=<$sonamefn> <$binfn> vs. <".$$ref->{"binfn"}.">\n" if $$ref;
98 push @DEBUG,$binfn if $bin=~m{ \Q(DEBUG) \E };
102 #while (my($src,$target)=each %SYMLINK) {
103 # die "$src->$target" if exists $SYMLINK{$target};
109 BINFN: for my $binfn (@DEBUG) {
110 # warn "$binfn...\n".Dumper([sort @{$F{$binfn}{"needed"}}]);
111 die $binfn if exists $SYMLINK{$binfn};
117 for my $needed (@{$h->{"needed"}}) {
119 if ($needed=~m{^/}) {
120 $needed=$SYMLINK{$needed} while exists $SYMLINK{$needed};
123 # die "$binfn: $l: $needed" if $needed=~m{/};
125 my $rpath=$h->{"rpath"};
126 push @rpath,split /:/,$rpath if $rpath;
127 push @rpath,qw(/lib64 /usr/lib64);
128 for my $rpath (@rpath) {
130 warn "$binfn: $l: $rpath";
133 my $fn="$rpath/$needed";
134 $fn=$SYMLINK{$fn} while exists $SYMLINK{$fn};
135 next if !$SONAME{$fn};
139 warn "$binfn: $l: $needed not found; rpath=".join(":",@rpath)."\n" if !$found;
141 push @l,$found if $found&&!$l{$found}++;
144 #warn Dumper $binfn,\%l;
148 for my $l (keys(%l)) {
151 warn "$binfn: $l: missing\n";
155 warn "$binfn: $l: ambiguous\n";
158 my $dtsize=$ref->[2];
159 die if !defined $dtsize;
161 my $dwzsize=$ref->[1];
162 die if !defined $dwzsize;
163 $dwzsizetot+=$dwzsize;
165 my $dwzcommon=$ref->[0];
166 next if $dwzcommon eq "nodwzcommon";
167 die if $dwzcommon eq "isdwzcommon";
168 next if $dwzcommons{$dwzcommon}++;
169 my $dwzcommonref=$D{$dwzcommon};
170 die if !$dwzcommonref;
171 die if $dwzcommonref->[0] ne "isdwzcommon";
172 die if $dwzcommonref->[2] ne "NA";
173 my $dwzcommonsize=$dwzcommonref->[1];
174 $dwzsizetot+=$dwzcommonsize;
176 print "$binfn: dwzsizetot=$dwzsizetot dtsizetot=$dtsizetot\n";
177 warn "$binfn: ".Dumper(\%dwzcommons);
178 $dwzsizeall+=$dwzsizetot;
179 $dtsizeall+=$dtsizetot;
180 # warn "$binfn done\n".Dumper([sort keys(%l)]);
182 print "dwzsizeall=$dwzsizeall dtsizeall=$dtsizeall\n";
183 print "computed=$computed of DEBUG=".(0+@DEBUG)."\n";