Linux 给 vmlinux 添加符号

文章目录

  • 前言
  • [一、centos7 给 vmlinux 添加符号](#一、centos7 给 vmlinux 添加符号)
  • [二、ubuntu22.04 给 vmlinux 添加符号](#二、ubuntu22.04 给 vmlinux 添加符号)

前言

使用内核源码下的script文件:scripts/extract-vmlinux 可以从/boot/vmlinuz 提取出来 内核镜像文件vmlinux:

c 复制代码
# ./extract-vmlinux vmlinuz-3.10.0-693.el7.x86_64 > vmlinux
# nm vmlinux
nm: vmlinux:无符号

Linux 给 vmlinux 添加符号 这里有两个开源的项目,这里我分别已centos7 和 ubuntu22.04演示。

一、centos7 给 vmlinux 添加符号

c 复制代码
# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"

# uname -r
3.10.0-693.el7.x86_64

参考:https://github.com/elfmaster/kdress

c 复制代码
# git clone https://github.com/elfmaster/kdress
# cd kdress/
# make
gcc -O2 build_ksyms.c -o build_ksyms
gcc -O2 kunpress.c -o kunpress
# ./kdress vmlinuz-3.10.0-693.el7.x86_64 vmlinux System.map-3.10.0-693.el7.x86_64

[+] vmlinux has been successfully extracted
[+] vmlinux has been successfully instrumented with a complete ELF symbol table.
c 复制代码
# nm vmlinux | grep '\<sys_call_table\>'
ffffffff816beee0 R sys_call_table
# cat /boot/System.map-3.10.0-693.el7.x86_64 | grep '\<sys_call_table\>'
ffffffff816beee0 R sys_call_table
c 复制代码
# gdb -q vmlinux
Reading symbols from /root/vmlinux/kdress/vmlinux...(no debugging symbols found)...done.
(gdb) print &sys_call_table
$1 = (<data variable, no debug info> *) 0xffffffff816beee0 <sys_call_table>
(gdb) x/gx 0xffffffff816beee0
0xffffffff816beee0 <sys_call_table>:    0xffffffff812019e0
(gdb) x/10i 0xffffffff812019e0
   0xffffffff812019e0 <sys_read>:       callq  0xffffffff816b6c80 <__fentry__>
   0xffffffff812019e5 <sys_read+5>:     push   %rbp
   0xffffffff812019e6 <sys_read+6>:     mov    %rsp,%rbp
   0xffffffff812019e9 <sys_read+9>:     push   %r14
   0xffffffff812019eb <sys_read+11>:    mov    %rdx,%r14
   0xffffffff812019ee <sys_read+14>:    push   %r13
   0xffffffff812019f0 <sys_read+16>:    mov    %rsi,%r13
   0xffffffff812019f3 <sys_read+19>:    lea    -0x30(%rbp),%rsi
   0xffffffff812019f7 <sys_read+23>:    push   %r12
   0xffffffff812019f9 <sys_read+25>:    push   %rbx
(gdb) x/gx 0xffffffff816beee0+8
0xffffffff816beee8 <sys_call_table+8>:  0xffffffff81201ac0
(gdb) x/10i 0xffffffff81201ac0
   0xffffffff81201ac0 <sys_write>:      callq  0xffffffff816b6c80 <__fentry__>
   0xffffffff81201ac5 <sys_write+5>:    push   %rbp
   0xffffffff81201ac6 <sys_write+6>:    mov    %rsp,%rbp
   0xffffffff81201ac9 <sys_write+9>:    push   %r14
   0xffffffff81201acb <sys_write+11>:   mov    %rdx,%r14
   0xffffffff81201ace <sys_write+14>:   push   %r13
   0xffffffff81201ad0 <sys_write+16>:   mov    %rsi,%r13
   0xffffffff81201ad3 <sys_write+19>:   lea    -0x30(%rbp),%rsi
   0xffffffff81201ad7 <sys_write+23>:   push   %r12
   0xffffffff81201ad9 <sys_write+25>:   push   %rbx

系统调用编号请参考:https://elixir.bootlin.com/linux/v3.10/source/arch/x86/syscalls/syscall_64.tbl

二、ubuntu22.04 给 vmlinux 添加符号

c 复制代码
# cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.4 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"

# uname -r
5.15.0-122-generic

参考项目:https://github.com/marin-m/vmlinux-to-elf

c 复制代码
sudo apt install python3-pip liblzo2-dev
sudo pip3 install --upgrade lz4 zstandard git+https://github.com/clubby789/python-lzo@b4e39df
sudo pip3 install --upgrade git+https://github.com/marin-m/vmlinux-to-elf
c 复制代码
Usage:
./vmlinux-to-elf <input_kernel.bin> <output_kernel.elf>
c 复制代码
# whereis vmlinux-to-elf
vmlinux-to-elf: /usr/local/bin/vmlinux-to-elf
c 复制代码
x# vmlinux-to-elf vmlinuz-5.15.0-122-generic vmlinux_sym
[+] Kernel successfully decompressed in-memory (the offsets that follow will be given relative to the decompressed binary)
[+] Version string: Linux version 5.15.0-122-generic (buildd@lcy02-amd64-034) (gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #132-Ubuntu SMP Thu Aug 29 13:45:52 UTC 2024 (Ubuntu 5.15.0-122.132-generic 5.15.163)
[+] Guessed architecture: x86_64 successfully in 9.65 seconds
[+] Found kallsyms_token_table at file offset 0x017b69d0
[+] Found kallsyms_token_index at file offset 0x017b6d80
[+] Found kallsyms_markers at file offset 0x017b60c0
[+] Found kallsyms_names at file offset 0x015e3c98
[+] Found kallsyms_num_syms at file offset 0x015e3c90
[i] Negative offsets overall: 99.736 %
[i] Null addresses overall: 0.00135036 %
[+] Found kallsyms_offsets at file offset 0x01553250
[+] Successfully wrote the new ELF kernel to vmlinux_sym
c 复制代码
# nm vmlinux_sym | grep '\<sys_call_table\>'
ffffffff82200320 D sys_call_table
# cat /boot/System.map-5.15.0-122-generic | grep '\<sys_call_table\>'
ffffffff82200320 D sys_call_table
相关推荐
七七powerful8 分钟前
ansible play-book玩法
linux·服务器·ansible
晚安,cheems14 分钟前
linux的权限
linux·运维·服务器
路溪非溪15 分钟前
Linux加载一个应用程序的过程总结
linux·运维·服务器
QT.qtqtqtqtqt1 小时前
攻防世界easyphp
linux·运维·服务器
菜鸟康2 小时前
Linux系统编程——系统内核中的信号
linux·运维·服务器
CSND7403 小时前
Ubuntu vi(vim)编辑器配置一键补全main函数
linux·c语言·ubuntu·编辑器·vim
Crazy learner5 小时前
C 和 C++ 动态库的跨语言调用原理
c语言·c++
シ風箏7 小时前
Neo4j【环境部署 02】图形数据库Neo4j在Linux系统ARM架构下的安装使用
linux·数据库·arm·neo4j
Cachel wood9 小时前
Vue.js前端框架教程8:Vue消息提示ElMessage和ElMessageBox
linux·前端·javascript·vue.js·前端框架·ecmascript
人才程序员11 小时前
QML z轴(z-order)前后层级
c语言·前端·c++·qt·软件工程·用户界面·界面