forked from alexhuangster/csapp3e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getasmfun.pl
69 lines (61 loc) · 1.54 KB
/
getasmfun.pl
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
#!/usr/bin/perl -w
#!/usr/local/bin/perl -w
use Getopt::Std;
#################################################################
# getasmfun - extract function from disassembled assembly code
#
# Randal E. Bryant, Carnegie Mellon, 2005
#
# Usage getasmfun [-h] [-f <function>] < <asmfile> > <outfile>
# -f <function> Extract only named function
# -h Print help message
#
# Scans stdin looking for named function. Then emits lines until
# it hits an "ret" instruction
# Skips lines containing compiler directives
#
#################################################################
getopts('hf:');
my ($opt_h);
if ($opt_h) {
print STDOUT "Usage $0 [-h] -a|-f function\n";
print STDOUT " -h Print this message\n";
print STDOUT " -f function Extract named function\n";
}
$fname = "***";
if ($opt_f) {
$fname = $opt_f;
$all_funs = 0;
} else {
$fname = "XXX";
$all_funs = 1;
}
$active_reset = 1;
$active = $active_reset;
while (<>) {
$line = $_;
if ($line =~ /^_*$fname:/) {
$active = 1;
print $line;
} elsif ($all_funs && ($line =~ /^[a-zA-Z0-9_]+:/)) {
$active = 1;
print $line;
} elsif ($line =~ /^\t+\./) {
# Skip line
} elsif ($line =~ /^\.L[A-Z]/) {
# Skip line
} elsif ($line =~ /^\.[a-z]/) {
# Skip line
} elsif ($line =~ /^LM/) {
# Skip line
} elsif ($active &&
($line =~ /^\tret/ || $line =~ /^\trep ; ret/)) {
print $line;
$active = $active_reset;
print "\n";
} elsif ($active == 1) {
print $line;
} else {
# print "SKIP: $line";
}
}