aboutsummaryrefslogtreecommitdiffstats
path: root/benchmarks/linux-gputest/files/apply-binary-patch.pl
blob: 0f4315ee04d207d2b297c5da647c645783fe6240 (plain) (blame)
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
#!/usr/bin/env perl

use strict;
use warnings;
use Sys::Mmap;

my $fh;
my $flen;
my $fdata;

sub commit {
	return unless $fh;
	munmap $fdata or die "munmap: $!";
	close $fh or die "close: $!";
}

while (<>) {
	chomp;
	if (/^([0-9A-F]{8}):? ([0-9A-F]{2}) ([0-9A-F]{2})$/i) {
		my ($offset, $old, $new) = map hex, $1, $2, $3;
		next unless $fdata;
		next unless $offset < $flen;
		next if ord substr($fdata, $offset, 1) != $old;
		substr($fdata, $offset, 1) = chr $new;
	} elsif (-e) {
		commit;
		open $fh, '+<', $_ or die "open: $!";
		mmap $fdata, 0, PROT_READ | PROT_WRITE, MAP_SHARED, $fh
		or die "mmap: $!";
		$flen = -s;
	}
}
commit;