Linux 内核模块符号信息以及strip命令

前言

最近学习了Linux内核模块的符号信息和 strip 命令,总结一下。

一、strip

1.1 GNU Binutils

GNU Binutils 是二进制工具的集合。 主要有:

ld - the GNU linker.
as - the GNU assembler.

其他常用的一些工具:

ar - A utility for creating, modifying and extracting from archives.
nm - Lists symbols from object files.
objcopy - Copies and translates object files.
objdump - Displays information from object files.
readelf - Displays information from any ELF format object file.
size - Lists the section sizes of an object or archive file.
strings - Lists printable strings from files.
strip - Discards symbols.
......

binutils 已被移植到大多数主要的 Unix variants 中,是为GNU系统(以及GNU/Linux)提供编译和链接程序的工具。

本文主要介绍strip命令的使用。

1.2 strip

(1)

strip - Discard symbols from object files.

Discard 目标文件 objfile 中的符号。

(2)

 -s
 --strip-all
      Remove all symbols.

 -g
 -S
 -d
 --strip-debug
     Remove debugging symbols only.

(3)

 --info
     Display a list showing all architectures and object formats available.

在这里插入图片描述

二、使用步骤

2.1 demo

我以一个简单的内核模块为例子:

#include <linux/kernel.h>
#include <linux/module.h>

//内核模块初始化函数
static int __init hello_init(void)
{
	printk(KERN_EMERG "Hello World\n");
	return 0;
}

//内核模块退出函数
static void __exit hello_exit(void)
{
	printk(KERN_DEBUG "exit\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");

编译出来的内核模块:
在这里插入图片描述
可以看出内核模块带有debug_info信息,所以导致文件大小比较大。

readelf -S helloworld.ko 

在这里插入图片描述

有很多带debug的Section。

2.2 strip --strip-debug

我用 strip --strip-debug 命令去除内核模块的调试信息:

strip --strip-debug helloworld.ko

在这里插入图片描述
可见调试信息没有了,内核模块一下小了很多。

readelf -S helloworld.ko

在这里插入图片描述
没有带有debug的Section。

依然能够正常使用内核模块:
在这里插入图片描述

2.3 符号信息

2.3.1 查看模块的符号信息

readelf -s helloworld.ko
-s
  --symbols
   --syms
       Displays the entries in symbol table section of the file, if it has one.

在这里插入图片描述

nm helloworld.ko 

在这里插入图片描述

2.3.2 符号表

(1)
symtab 保存了二进制文件的符号表,符号表保存了查找程序符号、为符合赋值,重定位符号所需要的全部信息。有一个专门的section来保存符号表。符号表表项的格式由下列数据结构表示:

/* Symbol table entry.  */

typedef struct
{
  Elf64_Word	st_name;		/* Symbol name (string tbl index) */
  unsigned char	st_info;		/* Symbol type and binding */
  unsigned char st_other;		/* Symbol visibility */
  Elf64_Section	st_shndx;		/* Section index */
  Elf64_Addr	st_value;		/* Symbol value */
  Elf64_Xword	st_size;		/* Symbol size */
} Elf64_Sym;

符号的主要任务是将一个字符串和一个值关联起来。

(2)
一个符号的确切用途由st_info定义,由两部分组成:Symbol type 和 Symbol binding。

/* How to extract and insert information held in the st_info field.  */

#define ELF32_ST_BIND(val)		(((unsigned char) (val)) >> 4)
#define ELF32_ST_TYPE(val)		((val) & 0xf)
#define ELF32_ST_INFO(bind, type)	(((bind) << 4) + ((type) & 0xf))

/* Both Elf32_Sym and Elf64_Sym use the same one-byte st_info field.  */
#define ELF64_ST_BIND(val)		ELF32_ST_BIND (val)
#define ELF64_ST_TYPE(val)		ELF32_ST_TYPE (val)
#define ELF64_ST_INFO(bind, type)	ELF32_ST_INFO ((bind), (type))

(3)
Symbol type:

/* Legal values for ST_TYPE subfield of st_info (symbol type).  */

#define STT_NOTYPE	0		/* Symbol type is unspecified */
#define STT_OBJECT	1		/* Symbol is a data object */
#define STT_FUNC	2		/* Symbol is a code object */
#define STT_SECTION	3		/* Symbol associated with a section */
#define STT_FILE	4		/* Symbol's name is file name */
#define STT_COMMON	5		/* Symbol is a common data object */
#define STT_TLS		6		/* Symbol is thread-local data object*/
#define	STT_NUM		7		/* Number of defined types.  */
#define STT_LOOS	10		/* Start of OS-specific */
#define STT_GNU_IFUNC	10		/* Symbol is indirect code object */
#define STT_HIOS	12		/* End of OS-specific */
#define STT_LOPROC	13		/* Start of processor-specific */
#define STT_HIPROC	15		/* End of processor-specific */

主要说明三个:
STT_NOTYPE:表示符号的类型未指定,用于未定义引用。
STT_OBJECT:表示符号关联到一个数据对象,比如变量、数组或指针。
STT_FUNC:表示符号关联到一个代码对象,比如函数或过程。

(4)
Symbol binding:

/* Legal values for ST_BIND subfield of st_info (symbol binding).  */

#define STB_LOCAL	0		/* Local symbol */
#define STB_GLOBAL	1		/* Global symbol */
#define STB_WEAK	2		/* Weak symbol */
#define	STB_NUM		3		/* Number of defined types.  */
#define STB_LOOS	10		/* Start of OS-specific */
#define STB_GNU_UNIQUE	10		/* Unique symbol.  */
#define STB_HIOS	12		/* End of OS-specific */
#define STB_LOPROC	13		/* Start of processor-specific */
#define STB_HIPROC	15		/* End of processor-specific */

主要说明三个:
STB_LOCAL:局部符号,只在目标文件内部可见,在与其它程序的其它部分合并时,是不可见的。如果一个程序的几个目标文件都定义相同的符号名,也不会有问题。局部符号互不干扰。

STB_GLOBAL:全局符号,在定义的目标文件内部可见,也可以由构成的其他目标文件引用。每个全局符号在一个程序内部都只能定义一次,否则链接器将报告错误。
指向符号的未定义引用,将在重定位期间确定相关符号的位置。如果对全局符号的未定义引用无法解决,则拒绝程序执行或静态绑定。

STB_WEAK:整个程序内可见,但可以有多个定义。如果程序中一个全局符号和一个局部符号名字相同,全局符号优先处理。
即使一个弱符号未定义,程序也可以静态或动态链接,并将符号指定为0值。

2.3.3 strip -s

strip -s helloworld.ko

去除模块的所有符号:
在这里插入图片描述
二进制模块的符号信息全部被strip,如下所示:
在这里插入图片描述
去除所有模块后,内核模块不能正常加载:
在这里插入图片描述

三、Linux内核中的使用

内核头文件中的顶层Makefile根据 INSTALL_MOD_STRIP是否被定义来决定是否去除掉模块的debug信息。

// /usr/src/5.4.0-26-generic/linux-headers-5.4.0-26-generic/Makefile

# INSTALL_MOD_STRIP, if defined, will cause modules to be
# stripped after they are installed.  If INSTALL_MOD_STRIP is '1', then
# the default option --strip-debug will be used.  Otherwise,
# INSTALL_MOD_STRIP value will be used as the options to the strip command.

ifdef INSTALL_MOD_STRIP
ifeq ($(INSTALL_MOD_STRIP),1)
mod_strip_cmd = $(STRIP) --strip-debug
else
mod_strip_cmd = $(STRIP) $(INSTALL_MOD_STRIP)
endif # INSTALL_MOD_STRIP=1
else
mod_strip_cmd = true
endif # INSTALL_MOD_STRIP
export mod_strip_cmd

INSTALL_MOD_STRIP,如果已定义,将导致模块在安装后内核模块的debug信息被去除掉。 如果 INSTALL_MOD_STRIP 为 ‘1’,则将使用默认选项 --strip-debug。 否则,INSTALL_MOD_STRIP 值将用作 strip 命令的选项。

总结

本文是内核模块符号表的一些内容以及strip命令的使用。

参考资料

深入Linux内核架构
https://www.gnu.org/software/binutils/

### 升级Linux内核的安全方法 #### 准备工作 在升级之前,确保备份重要数据并安装必要的工具包。对于基于Arch Linux的系统,可以使用`pacman`或其他AUR辅助工具来获取最新的稳定版内核[^1]。 #### 下载新内核源码 如果手动下载内核源码,则可以通过官方镜像站点完成操作。例如,要升级到5.16.15版本,可执行以下命令: ```bash wget --no-check-certificate https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.16.15.tar.gz wget --no-check-certificate https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.16.15.tar.sign ``` 这些文件包含了完整的内核源代码及其签名验证信息[^2]。 #### 配置与编译 解压后进入目录配置环境变量以及设置参数选项。接着运行如下脚本进行构建过程,在此期间推荐加入多线程加速以提高效率。 ```bash make oldconfig make -j$(nproc) bzImage make -j$(nproc) modules make INSTALL_MOD_STRIP=1 modules_install > /dev/null ``` 上述指令中的`INSTALL_MOD_STRIP=1`用于移除模块内的调试符号从而节省存储空间;而`-j$(nproc)`则动态调整并发作业数匹配CPU核心数量[^4]。 #### 安装引导程序 完成后需更新GRUB或其它启动加载器以便识别新的kernel映像位置。通常只需简单调用grub-mkconfig即可自动刷新菜单列表项。 ```bash cp arch/x86/boot/bzImage /boot/vmlinuz-linux-custom update-initramfs -c -k $(make kernelrelease) grub-mkconfig -o /boot/grub/grub.cfg ``` #### 验证结果 最后一步也是至关重要的环节——重启计算机并通过`uname -r`核实当前正在使用的具体编号是否符合预期目标值。 ### 注意事项 在整个流程里务必小心谨慎对待每一步骤细节以免造成不可逆损害或者系统崩溃现象发生。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值