forked from dtrace4linux/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-vmlinux.pl
More file actions
executable file
·212 lines (184 loc) · 5.2 KB
/
get-vmlinux.pl
File metadata and controls
executable file
·212 lines (184 loc) · 5.2 KB
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
#! /usr/bin/perl
# $Header:$
# This script is used to extract a working ELF executable from vmlinuz.
# I want to see whats in the kernels in my VMs, so this helps when I
# am dealing with old and legacy distros.
# Author: Paul Fox
# March 2011
use strict;
use warnings;
no warnings 'portable'; # Support for 64-bit ints required
use File::Basename;
use FileHandle;
use Getopt::Long;
use IO::File;
use POSIX;
#######################################################################
# Command line switches. #
#######################################################################
my %opts;
sub main
{
Getopt::Long::Configure('no_ignore_case');
usage() unless GetOptions(\%opts,
'help',
'n',
);
usage() if ($opts{help});
my $fname = shift @ARGV;
my $system_map = shift @ARGV;
if (!$fname) {
$fname = "/boot/vmlinuz-" . `uname -r`;
chomp($fname);
}
if (!$system_map) {
$system_map = "/boot/System.map-" . `uname -r`;
chomp($system_map);
}
my $fh;
my $fh1;
###############################################
# Read system.map. #
###############################################
print "Reading: $system_map\n";
$fh = new FileHandle($system_map);
die "Cannot open $system_map -- $!\n" if !$fh;
my %syms;
my %addr;
while (<$fh>) {
chomp;
my ($addr, $type, $name) = split(/ /);
my $seen = $syms{$name};
$syms{$name}{type} = $type;
$syms{$name}{addr} = $addr;
$addr{$addr} = $name if !$seen;
}
my $_text = $syms{_text}{addr};
print "Reading $fname\n";
$fh = new FileHandle($fname);
# $fh->binmode();
###############################################
# Find the compressed payload, and write #
# it out uncompressed. #
###############################################
my $data;
my $pos = 0;
do {
$data = "";
$fh->seek($pos++, 0);
$fh->read($data, 4);
} until ($data eq "\x1f\x8b\x08\x00" || $fh->eof());
printf "Kernel at offset 0x%x\n", $pos - 1;
if ($fh->eof()) {
print STDERR "Couldnt find gzip marker\n";
exit(1);
}
$fh->seek(--$pos, 0);
$data = "";
while (!$fh->eof()) {
my $buf;
$fh->read($buf, 4096);
$data .= $buf;
}
print "Generating /tmp/vmlinux.tmp\n";
$fh = new FileHandle("| gunzip >/tmp/vmlinux.tmp");
print $fh $data;
$fh->close();
###############################################
# Now create an assembly file so we can #
# create an ELF file. #
###############################################
$fh1 = new FileHandle(">/tmp/vmlinux.s");
print "Generating /tmp/vmlinux.s\n";
# foreach my $s (sort(keys(%syms))) {
# print $fh1 ".globl $s\n";
# if ($syms{$s}{type} =~ /t/i) {
# print $fh1 "\t.type $s, \@function\n";
# print $fh1 "\t.size $s, 4\n";
# } else {
# print $fh1 "\t.set $s, 0x$syms{$s}{addr}\n";
# }
# }
$fh = new FileHandle("/tmp/vmlinux.tmp");
print $fh1 "\t.text\n";
print $fh1 "linuxstart:\n";
print $fh1 "_start:\n";
print $fh1 "\t .globl _start\n";
my $a = hex($_text);
printf "_start=%x\n", $a;
while (!$fh->eof()) {
$data = "";
$fh->read($data, 1);
last if !defined($data);
my $astr = sprintf("%x", $a);
if (defined($addr{$astr})) {
print $fh1 ".globl $addr{$astr}\n";
print $fh1 "\t.type $addr{$astr}, \@function\n";
print $fh1 "$addr{$astr}:\n";
}
printf $fh1 "\t.byte 0x%x\n", ord($data);
$a += 1;
}
print $fh1 "\t.size linuxstart, .-linuxstart\n";
###############################################
# Create linker script. #
###############################################
print "Generating /tmp/vmlinux.ld\n";
$fh1 = new FileHandle(">/tmp/vmlinux.ld");
print $fh1 "SECTIONS {\n";
print $fh1 " .text 0x$_text : { *(.text) }\n";
print $fh1 " .data 0 : { *(.data) }\n";
print $fh1 " .bss : { *(.bss) *(COMMON) }\n";
print $fh1 "}\n";
$fh1->close();
###############################################
# Now build the new ELF file. #
###############################################
spawn("cd /tmp ; gcc -c vmlinux.s");
spawn("cd /tmp ; ld vmlinux.ld -o v vmlinux.o");
###############################################
# This extracts the boot block code - we #
# dont want/care about this. #
###############################################
# $fh = new FileHandle("/tmp/vmlinux.tmp");
# my $fh1 = new FileHandle(">/tmp/vmlinux");
# print "Generating /tmp/vmlinux\n";
# while (1) {
# $data = "";
# $fh->read($data, 4);
# if ($data eq "\x7fELF") {
# print $fh1 $data;
# while (!$fh->eof()) {
# $data = "";
# $fh->read($data, 4096);
# print $fh1 $data;
# }
# last;
# }
# }
}
sub spawn
{ my $cmd = shift;
print $cmd, "\n";
return if $opts{n};
return system($cmd);
}
#######################################################################
# Print out command line usage. #
#######################################################################
sub usage
{
print <<EOF;
get-vmlinux.pl -- tool to extract kernel from a vmlinuz file
Usage: get-vmlinux.pl [vmlinuz] [System.map]
This tool is used to extract the kernel image from a boot up
vmlinuz file. It helps to have the System.map file for the
image, so that the symbol table can be populated.
The goal of this exercise is to allow browsing a distro kernel
when the original kernel source/object files are not available.
Switches:
EOF
exit(1);
}
main();
0;