TL;DR
This blog post explains how to extract and disassemble a Linux kernel image. It will cover the extract-vmlinux
script, how to use objdump
, and how to use /boot/System.map
to locate functions and other symbols.
Extracting the Linux kernel image (vmlinuz)
First, you’ll need to get the extract-vmlinux
script so that you can decompress and extract the Linux kernel image.
You can download the latest version from GitHub:
It’s unlikely that the script will change, but to be safe you should use the extract-vmlinux
script that is from the same source tree as your kernel.
If you are extracting a kernel installed from your operating system, you can install the extract-linux
script with your package manager.
On Ubuntu, install linux-headers-$(uname -r)
:
You will be able to find the extract-linux
script at /usr/src/linux-headers-$(uname -r)/scripts/extract-vmlinux
.
On CentOS, install kernel-devel
:
You will be able to find the extract-linux
script at /usr/src/kernels/$(uname -r)/scripts/extract-vmlinux
.
Using extract-vmlinux
You can now use extract-vmlinux
to decompress and extract the kernel image.
A good first step is to create a temporary directory and copy the kernel image to it:
Now, run the extract-vmlinux
script to extract the image.
On Ubuntu:
On CentOS:
Disassmble the Linux kernel with objdump
Now that you have decompressed and extracted the kernel image, you can use objdump
to disassemble it. There’s quite a bit of code, so piping the output to less
is probably a good idea.
Using the same directory structure as before:
Finding symbols in /boot/System.map
So, you’ve extracted the kernel and are now looking at the disassembled kernel. You’ll notice that there are no symbol names, so you can’t easily find the starting point for functions you want to examine.
Luckily, all the symbols and their starting address can be found in the file /boot/System.map-$(uname -r)
.
For example, let’s lookup the address of tcp_v4_do_rcv
:
You can now search the objdump
output for the address ffffffff81590df0
to find the disassmbled net_ipv4_path
function:
Conclusion
Extracting the Linux kernel is relatively straightforward once you know what extract-vmlinux
is and where to find it. Extracting the kernel can be useful when you want to verify comments left by kernel code authors or are just curious to see how a particular function was compiled.