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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
# Copyright (C) 2008-2010 Modestas Vainius <modax@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
package Debian::PkgKde::SymbolsHelper::SymbolFile;
use strict;
use warnings;
use base 'Dpkg::Shlibs::SymbolFile';
use File::Temp qw();
use File::Copy qw();
use Storable qw();
use IO::Handle;
use Dpkg::ErrorHandling;
use Dpkg::Version;
use Debian::PkgKde::SymbolsHelper::Symbol;
use Debian::PkgKde::SymbolsHelper::Substs;
# Use Debian::PkgKde::SymbolsHelper::Symbol as base symbol
sub parse {
my ($self, $fh, $file, $seen, $obj_ref, $base_symbol) = @_;
unless (defined $base_symbol) {
$base_symbol = 'Debian::PkgKde::SymbolsHelper::Symbol';
}
if (!defined $seen) {
# Read 'SymbolsHelper-Confirmed' header
open(my $fh, "<", $file)
or error("unable to open symbol file '$file' for reading");
my $line = <$fh>;
close $fh;
chop $line;
if ($line =~ /^#\s*SymbolsHelper-Confirmed:\s+(.+)$/) {
$self->set_confirmed(split(/\s+/, $1));
}
}
return $self->SUPER::parse($fh, $file, $seen, $obj_ref, $base_symbol);
}
sub set_confirmed {
my ($self, $version, @arches) = @_;
$self->{h_confirmed_version} = $version;
$self->{h_confirmed_arches} = (@arches) ? \@arches : undef;
}
sub get_confirmed_version {
my $self = shift;
return $self->{h_confirmed_version};
}
sub get_confirmed_arches {
my $self = shift;
return (defined $self->{h_confirmed_arches}) ?
@{$self->{h_confirmed_arches}} : ();
}
sub create_symbol {
my ($self, $spec, %opts) = @_;
$opts{base} = Debian::PkgKde::SymbolsHelper::Symbol->new()
unless exists $opts{base};
return $self->SUPER::create_symbol($spec, %opts);
}
sub fork_symbol {
my ($self, $sym, $arch) = @_;
$arch = $self->get_arch() unless $arch;
my $nsym = $sym->clone(symbol => $sym->get_symboltempl());
$nsym->initialize(arch => $arch);
return $nsym;
}
sub output {
my ($self, $fh, %opts) = @_;
$opts{with_confirmed} = 1 unless exists $opts{with_confirmed};
# Write SymbolsHelper-Confirmed header
if ($opts{with_confirmed}) {
my @carches = $self->get_confirmed_arches();
if (@carches) {
print $fh '# SymbolsHelper-Confirmed: ', $self->get_confirmed_version(),
" ", join(" ", sort @carches), "\n" if defined $fh;
}
}
return $self->SUPER::output($fh, %opts);
}
sub _resync_symbol_cache {
my ($self, $soname, $cache) = @_;
my %rename;
foreach my $symkey (keys %$cache) {
my $sym = $cache->{$symkey};
if ($sym->get_symbolname() ne $symkey) {
$rename{$sym->get_symbolname()} = $sym;
delete $cache->{$symkey};
}
}
foreach my $newname (keys %rename) {
my $e = $self->get_symbol_object($rename{$newname}, $soname);
if ($e && ! $rename{$newname}->equals($e)) {
warning("caution: newly generated symbol '%s' will replace not exactly equal '%s'. Please readd if unappropriate",
$rename{$newname}->get_symbolspec(1),
$e->get_symbolspec(1));
}
$self->add_symbol($rename{$newname}, $soname);
}
}
sub resync_soname_symbol_caches {
my ($self, $soname) = @_;
my $obj = $self->get_object($soname);
# We need this to avoid removal of symbols which names clash when renaming
$self->_resync_symbol_cache($obj, $obj->{syms});
# Resync aliases too
foreach my $alias (values %{$obj->{patterns}{aliases}}) {
$self->_resync_symbol_cache($obj, $alias);
}
}
sub resync_soname_with_h_name {
my ($self, $soname) = @_;
my $obj = $self->get_object($soname);
sub _resync_with_h_name {
my $cache = shift;
foreach my $symkey (keys %$cache) {
$cache->{$symkey}->resync_name_with_h_name();
}
}
# First resync h_name with symbol name and templ
_resync_with_h_name($obj->{syms});
foreach my $alias (values %{$obj->{patterns}{aliases}}) {
_resync_with_h_name($alias);
}
return $self->resync_soname_symbol_caches($soname);
}
# Detects (or just neutralizes) substitutes which can be guessed from the
# symbol name alone. Currently unused.
#sub detect_standalone_substs {
# my ($self, $detect) = @_;
#
# foreach my $sym ($self->get_symbols()) {
# my $str = $sym->get_h_name();
# foreach my $subst (@STANDALONE_SUBSTS) {
# if ($detect) {
# $subst->detect($str, $self->{arch});
# } else {
# $subst->neutralize($str);
# }
# }
# }
# foreach my $soname (keys %{$self->{objects}}) {
# # Rename soname object with data in h_name
# $self->resync_soname_with_h_name($soname);
# }
#}
# Upgrade virtual table symbols. Needed for templating.
sub prepare_for_templating {
my $self = shift;
my %sonames;
foreach my $soname ($self->get_sonames()) {
foreach my $sym ($self->get_symbols($soname)) {
if ($sym->upgrade_virtual_table_symbol($self->get_arch())) {
$sonames{$soname} = 1;
}
}
}
foreach my $soname (keys %sonames) {
$self->resync_soname_symbol_caches($soname);
}
}
sub patch_template {
my ($self, @patches) = @_;
my @symfiles;
my %dumped;
foreach my $patch (@patches) {
my $package = $patch->{package} || '';
my $tmpfile;
if (!exists $dumped{$package}) {
$tmpfile = File::Temp->new(
TEMPLATE => "${package}_orig.symbolsXXXXXX",
UNLINK => 0,
);
$self->output($tmpfile,
package => $package,
template_mode => 1,
with_confirmed => 0,
);
$tmpfile->close();
$dumped{$package} = $tmpfile->filename;
}
$tmpfile = File::Temp->new(
TEMPLATE => "${package}_patched.symbolsXXXXXX",
UNLINK => 1,
);
$tmpfile->close();
unless (File::Copy::copy($dumped{$package}, $tmpfile->filename)) {
syserror("unable to copy file '%s' to '%s'",
$dumped{$package}, $tmpfile->filename);
}
if ($patch->apply($tmpfile->filename)) {
# Patching was successful. Parse new SymbolFile and return it
my $symfile = Debian::PkgKde::SymbolsHelper::SymbolFile->new(
file => $tmpfile->filename,
arch => $patch->{arch},
);
if ($patch->has_info()) {
$symfile->set_confirmed($patch->{version}, $patch->{arch});
} else {
$symfile->set_confirmed(undef);
}
push @symfiles, $symfile;
last unless wantarray;
}
}
foreach my $file (values %dumped) {
unless ($File::Temp::KEEP_ALL) {
unlink $file;
}
}
return (wantarray) ? @symfiles : $symfiles[0];
}
sub _dclone_exclude {
my ($target, @exclude) = @_;
my %saved;
foreach my $e (@exclude) {
if (exists $target->{$e}) {
$saved{$e} = $target->{$e};
delete $target->{$e};
}
}
my $clone = Storable::dclone($target);
$target->{$_} = $saved{$_} foreach @exclude;
return $clone;
}
# Forks an empty symbol file (without symbols and patterns) from the current
# one. Other properties are retained.
sub fork_empty {
my $self = shift;
my $symfile = _dclone_exclude($self, qw(objects));
$symfile->clear();
foreach my $soname ($self->get_sonames()) {
$symfile->create_object($soname);
my $obj = $symfile->get_object($soname);
my $cloned = _dclone_exclude($self->get_object($soname),
qw(syms patterns minver_cache));
$obj->{$_} = $cloned->{$_} foreach keys %$cloned;
}
return $symfile;
}
sub fork {
my ($self, @optinstances) = @_;
unshift @optinstances, {} unless @optinstances;
@optinstances = ( $optinstances[0] ) unless wantarray;
my @symfiles;
foreach my $opts (@optinstances) {
my $symfile = $self->fork_empty();
$symfile->{$_} = $opts->{$_} foreach keys %$opts;
$symfile->{file} = '';
push @symfiles, $symfile;
}
# Fork symbols
foreach my $soname ($self->get_sonames()) {
foreach my $sym ($self->get_symbols($soname),
$self->get_patterns($soname))
{
foreach my $symfile (@symfiles) {
my $nsym = $self->fork_symbol($sym, $symfile->get_arch());
$nsym->{h_origin_symbol} = $sym;
$symfile->add_symbol($nsym, $soname);
}
}
}
return (wantarray) ? @symfiles : shift @symfiles;
}
sub get_highest_version {
my $self = shift;
my $maxver;
foreach my $sym ($self->get_symbols(),
$self->get_patterns()) {
if (!$sym->{deprecated} &&
(!defined $maxver || version_compare($sym->{minver}, $maxver) > 0))
{
$maxver = $sym->{minver};
}
}
return $maxver;
}
1;
|