曹旭辉 发表于 2022-8-9 15:46:18

内核模块的strip以及依赖

  由于目前使用的内核模块ko文件太大, 一个驱动ko大约11M;
  于是直接strip xx.ko;然后insmod xx.ko
但是报错了:module has no symbols (stripped?)
Develop>insmod/opt/bin/test.ko
insmod: ERROR: could not insert module /opt/bin/test.ko: Invalid module format
Develop>dmesg
test: module has no symbols (stripped?) 难道不能strip; 肯定不可能,于是strip --help 看下
strip-h
Usage: strip <option(s)> in-file(s)
Removes symbols and sections from files
The options are:
-I --input-target=<bfdname>      Assume input file is in format <bfdname>
-O --output-target=<bfdname>   Create an output file in format <bfdname>
-F --target=<bfdname>            Set both input and output format to <bfdname>
-p --preserve-dates            Copy modified/access timestamps to the output
-D --enable-deterministic-archives
                                 Produce deterministic output when stripping archives
-U --disable-deterministic-archives
                                 Disable -D behavior (default)
-R --remove-section=<name>       Also remove section <name> from the output
-s --strip-all                   Remove all symbol and relocation information
-g -S -d --strip-debug         Remove all debugging symbols & sections
   --strip-dwo                   Remove all DWO sections
   --strip-unneeded            Remove all symbols not needed by relocations
   --only-keep-debug             Strip everything but the debug information
-N --strip-symbol=<name>         Do not copy symbol <name>
-K --keep-symbol=<name>          Do not strip symbol <name>
   --keep-file-symbols         Do not strip file symbol(s)
-w --wildcard                  Permit wildcard in symbol comparison
-x --discard-all               Remove all non-global symbols
-X --discard-locals            Remove any compiler-generated symbols
-v --verbose                     List all object files modified
-V --version                     Display this program's version number
-h --help                        Display this output
   --info                        List object formats & architectures supported
-o <file>                        Place stripped output into <file>   认为应该使用 -d or  --strip-debug 参数来strip 文件;在使用strip -d 后,insmod 加载成功;也就是 只能strip掉debug信息, 内核模块使用的信息较多, 不能瞎strip。
目前strip -d  xx 和 strip xx  两者执行后的文件大小也不是相差很大。但是都和源文件相差很大,所以以后使用strip -d 参数来strip吧, 然后以前使用的stip 是默认执行strip -d;
看样子不同版本 源码代码编译的strip 其默认参数是不一样的!!
  顺便说一个问题, 由于系统架构部那边提供的内核是带有netfiter的,但是由于业务需求,不需要netfilter,所以将netfilter编译成了ko模块;目前有业务人员想使用iptable 来查看丢包问题,于是问了一句这些netfilter中ko模块的依赖问题!----
  目前有个工具可以看到,就是modinfo;
_COMPILE:~# modinfo-h
Usage:
    modinfo filename
Options:
    -a, --author                Print only 'author'
    -d, --description         Print only 'description'
    -l, --license               Print only 'license'
    -p, --parameters            Print only 'parm'
    -n, --filename            Print only 'filename'
    -0, --null                  Use \0 instead of \n
    -F, --field=FIELD         Print only provided FIELD
    -k, --set-version=VERSION   Use VERSION instead of `uname -r`
    -b, --basedir=DIR         Use DIR as filesystem root for /lib/modules
    -V, --version               Show version
    -h, --help                  Show this helproot@COMPILE:~# lsmod|grep nf_nat_ipv4
nf_nat_ipv4            163841 iptable_nat
nf_nat               245763 nf_nat_ipv4,xt_nat,nf_nat_masquerade_ipv4
nf_conntrack          1064966 nf_nat,nf_nat_ipv4,xt_conntrack,nf_nat_masquerade_ipv4,nf_conntrack_netlink,nf_conntrack_ipv4
root@COMPILE:~# modinfo -F depends /lib/modules/4.4.0-148-generic/kernel/net/ipv4/netfilter/nf_nat_ipv4.ko
nf_nat,nf_conntrack 
 
  可以看到 modinfo -F depends xx 就可以看出依赖的模块!lsmod 是查看已经成功加载后,这些模块依赖关系;同时
  常用参数为:

[*]-a显示模块开发人员
[*]-p显示模块所支持的参数
 
modinfo /lib/modules/4.4.0-148-generic/kernel/net/ipv4/netfilter/nf_nat_ipv4.ko
filename:       /lib/modules/4.4.0-148-generic/kernel/net/ipv4/netfilter/nf_nat_ipv4.ko
alias:          nf-nat-2
license:      GPL
srcversion:   42CF3FCE9CB78E8B74CAB3D
depends:      nf_nat,nf_conntrack
retpoline:      Y
intree:         Y
vermagic:       4.4.0-148-generic SMP mod_unload modversions  
 这是直接查看模块的信息,包含了depends字段;这个modinfo -F depends 的原理其实直接读取查看modules.dep文件;
root@_COMPILE:~# cat/lib/modules/4.4.0-148-generic/modules.dep |grep nf_nat_ipv4
kernel/net/ipv4/netfilter/nf_nat_ipv4.ko: kernel/net/netfilter/nf_nat.ko kernel/net/netfilter/nf_conntrack.ko
kernel/net/ipv4/netfilter/nft_chain_nat_ipv4.ko: kernel/net/ipv4/netfilter/nf_nat_ipv4.ko kernel/net/netfilter/nf_tables.ko kernel/net/netfilter/nf_nat.ko kernel/net/netfilter/nfnetlink.ko kernel/net/netfilter/nf_conntrack.ko
kernel/net/ipv4/netfilter/iptable_nat.ko: kernel/net/ipv4/netfilter/ip_tables.ko kernel/net/ipv4/netfilter/nf_nat_ipv4.ko kernel/net/netfilter/nf_nat.ko kernel/net/netfilter/nf_conntrack.ko kernel/net/netfilter/x_tables.ko可以看到 也可查看依赖关系
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 内核模块的strip以及依赖