-
Notifications
You must be signed in to change notification settings - Fork 33
/
ksplice-obj.pl.in
163 lines (143 loc) · 4.37 KB
/
ksplice-obj.pl.in
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
#!/usr/bin/perl
# Copyright (C) 2008-2009 Ksplice, Inc.
# Authors: Anders Kaseorg, Jeff Arnold, Tim Abbott
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
# 02110-1301, USA.
use strict;
use warnings;
use lib 'KSPLICE_DATA_DIR';
use Ksplice;
$Verbose::level = $ENV{KSPLICE_VERBOSE} if (defined $ENV{KSPLICE_VERBOSE});
sub empty_diff {
my ($out) = @_;
my ($obj) = $out =~ /^(.*)\.KSPLICE$/ or die;
unlink "$obj.KSPLICE_new_code" if (-e "$obj.KSPLICE_new_code");
unlink "$obj.KSPLICE_old_code" if (-e "$obj.KSPLICE_old_code");
open OUT, '>', "$out.tmp";
close OUT;
rename "$out.tmp", $out;
}
sub do_snap {
my ($out) = @_;
my ($obj) = $out =~ /^(.*)\.KSPLICE$/ or die;
die if (!-e $obj);
unlink "$obj.KSPLICE_pre" if (-e "$obj.KSPLICE_pre");
empty_diff($out);
}
sub do_diff {
my ($out) = @_;
my ($obj) = $out =~ /^(.*)\.KSPLICE$/ or die;
my $obj_pre = "$obj.KSPLICE_pre";
die if (!-e $obj);
die "Patch creates new object $obj" if (!-e $obj_pre);
if (system('cmp', '-s', '--', $obj_pre, $obj) == 0) {
unlink $obj_pre;
return empty_diff($out);
}
runval("$libexecdir/ksplice-objmanip", $obj, "$obj.KSPLICE_new_code", "keep-new-code", "$obj.KSPLICE_pre", $ENV{KSPLICE_KID});
return empty_diff($out) if (!-e "$obj.KSPLICE_new_code");
open OUT, '>', "$out.tmp";
print OUT "1\n";
close OUT;
rename "$out.tmp", $out;
runval("$libexecdir/ksplice-objmanip", $obj_pre, "$obj.KSPLICE_old_code", "keep-old-code");
}
sub do_old_code {
my ($out) = @_;
my ($obj) = $out =~ /^(.*)\.KSPLICE_old_code$/ or die;
my $obj_pre = "$obj.KSPLICE_pre";
-e $obj_pre or $obj_pre = $obj;
runval("$libexecdir/ksplice-objmanip", $obj_pre, "$obj.KSPLICE_old_code", "keep-old-code");
}
sub link_objs {
my ($out, @ins) = @_;
if (@ins == 0) {
runval(shellwords($ENV{AR}), "rcs", $out);
} elsif (@ins == 1) {
copy @ins, $out;
} else {
runval(shellwords($ENV{LD}), "-r", "-o", $out, @ins);
}
}
sub do_combine {
my ($out, @ins) = @_;
my @new_code_objs;
my @old_code_objs;
foreach my $in (@ins) {
if (my ($obj) = $in =~ /^(.*)\.KSPLICE$/) {
next if (!-s $in);
push @new_code_objs, "$obj.KSPLICE_new_code";
push @old_code_objs, "$obj.KSPLICE_old_code";
} elsif (($obj) = $in =~ /^(.*)\.KSPLICE_old_code$/) {
push @old_code_objs, "$obj.KSPLICE_old_code"
unless (@old_code_objs && $old_code_objs[$#old_code_objs] eq "$obj.KSPLICE_old_code");
} elsif ("$in.KSPLICE" eq $out) {
my $pre = "$in.KSPLICE_pre";
if (system('cmp', '-s', '--', $pre, $in) == 0) {
unlink $pre;
}
} else {
die "Unexpected input $in for $out";
}
}
return empty_diff($out) unless (@old_code_objs);
my ($obj) = $out =~ /^(.*)\.KSPLICE$/ or die;
link_objs("$obj.KSPLICE_new_code", @new_code_objs);
link_objs("$obj.KSPLICE_old_code", @old_code_objs);
open OUT, '>', "$out.tmp";
print OUT "1\n";
close OUT;
rename "$out.tmp", $out;
}
sub do_finalize {
my ($in, $out, $target) = @_;
my $ret = runval_raw("$libexecdir/ksplice-objmanip", $in, $out, "finalize", $target);
if ($ret == 0) {
} elsif ($ret >> 8 == 66) {
runval(shellwords($ENV{AR}), "rcs", $out);
} else {
child_error();
}
}
sub do_rmsyms {
my ($in, $out) = @_;
runval("$libexecdir/ksplice-objmanip", $in, $out, "rmsyms");
}
sub do_system_map_lookup {
my ($symarg) = @_;
open(SYMS, "<", "$ENV{KSPLICE_CONFIG_DIR}/System.map") or die;
my $line;
while (defined($line = <SYMS>)) {
my ($addr, $type, $sym, $mod) = split(/\s+/, $line);
if ($sym eq $symarg) { print $addr; last; }
}
close(SYMS);
}
my %handlers = (
'snap' => \&do_snap,
'diff' => \&do_diff,
'old_code' => \&do_old_code,
'combine' => \&do_combine,
'finalize' => \&do_finalize,
'rmsyms' => \&do_rmsyms,
'system_map_lookup' => \&do_system_map_lookup,
);
my ($cmd, @args) = @ARGV;
if (exists $handlers{$cmd}) {
my $handler = $handlers{$cmd};
&$handler(@args);
} else {
print "Usage: ksplice-obj.pl ", join('|', keys %handlers), " ...\n";
exit(1);
};