2007年4月13日 星期五

How to generate the kernel module

1. compile 獨立的個別的檔案

gcc -Wp,-MD,net/ipv4/netfilter/.ip_conntrack_standalone.o.d -nostdinc -iwithprefix include -D__KERNEL__ -Iinclude -Wall -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Os -fomit-frame-pointer -pipe -msoft-float -mpreferred-stack-boundary=2 -march=i386 -Iinclude/asm-i386/mach-default -DMODULE -DKBUILD_BASENAME=ip_conntrack_standalone -DKBUILD_MODNAME=ip_conntrack -c -o net/ipv4/netfilter/ip_conntrack_standalone.o net/ipv4/netfilter/ip_conntrack_standalone.c

定義了__KERNEL__ definition
-D__KERNEL__

notice:
這裡有做了三個額外定義
-DMODULE
這個 .c file 要compile 成一個module
-DKBUILD_BASENAME=ip_conntrack_standalone
這個檔案的base name 為 ip_conntrack_standalone (也就是 .c 的名稱)
-DKBUILD_MODNAME=ip_conntrack
這個檔案被 compile 成為 module 後, 是成為哪一個 module (module name 為何)


2. 將屬於同一個module 的 .c 利用 ld 統一 compile 成一個單獨的檔案
ld -m elf_i386 -r -o net/ipv4/netfilter/ip_conntrack.o net/ipv4/netfilter/ip_conntrack_standalone.o net/ipv4/netfilter/ip_conntrack_core.o net/ipv4/netfilter/ip_conntrack_proto_generic.o net/ipv4/netfilter/ip_conntrack_proto_tcp.o net/ipv4/netfilter/ip_conntrack_proto_udp.o net/ipv4/netfilter/ip_conntrack_proto_icmp.o net/ipv4/netfilter/L7SessionMoniter.o net/ipv4/netfilter/skypesn.o net/ipv4/netfilter/skypefilehost.o


ld -m elf_i386 -r -o net/ipv4/netfilter/iptable_nat.o net/ipv4/netfilter/ip_nat_standalone.o net/ipv4/netfilter/ip_nat_rule.o net/ipv4/netfilter/ip_nat_core.o net/ipv4/netfilter/ip_nat_helper.o net/ipv4/netfilter/ip_nat_proto_unknown.o net/ipv4/netfilter/ip_nat_proto_tcp.o net/ipv4/netfilter/ip_nat_proto_udp.o net/ipv4/netfilter/ip_nat_proto_icmp.o

3. 利用 scripts/mod/modpost 來產生 module symbol version of vmlinux

scripts/mod/modpost -o /home3/vincent/development/Texas/CMF/kernel/linux-2.6.10/Module.symvers vmlinux net/ipv4/netfilter/ip_conntrack.o net/ipv4/netfilter/ip_conntrack_amanda.o net/ipv4/netfilter/ip_conntrack_ftp.o net/ipv4/netfilter/ip_conntrack_irc.o net/ipv4/netfilter/ip_conntrack_proto_sctp.o net/ipv4/netfilter/ip_conntrack_tftp.o net/ipv4/netfilter/ip_nat_amanda.o net/ipv4/netfilter/ip_nat_ftp.o net/ipv4/netfilter/ip_nat_irc.o net/ipv4/netfilter/ip_nat_snmp_basic.o net/ipv4/netfilter/ip_nat_tftp.o net/ipv4/netfilter/ipt_L7TARGET.o net/ipv4/netfilter/ipt_MASQUERADE.o net/ipv4/netfilter/ipt_NETMAP.o net/ipv4/netfilter/ipt_NOTRACK.o net/ipv4/netfilter/ipt_REDIRECT.o net/ipv4/netfilter/ipt_SAME.o net/ipv4/netfilter/ipt_TPROXY.o net/ipv4/netfilter/ipt_ULOG.o net/ipv4/netfilter/ipt_condition.o net/ipv4/netfilter/ipt_connmark.o net/ipv4/netfilter/ipt_conntrack.o net/ipv4/netfilter/ipt_dsize.o net/ipv4/netfilter/ipt_helper.o net/ipv4/netfilter/ipt_ipgroup.o net/ipv4/netfilter/ipt_l7check.o net/ipv4/netfilter/ipt_l7length.o net/ipv4/netfilter/ipt_l7state.o net/ipv4/netfilter/ipt_layer7.o net/ipv4/netfilter/ipt_pktbox.o net/ipv4/netfilter/ipt_pktseq.o net/ipv4/netfilter/ipt_pktseries.o net/ipv4/netfilter/ipt_puredsize.o net/ipv4/netfilter/ipt_skypeack.o net/ipv4/netfilter/ipt_skypehost.o net/ipv4/netfilter/ipt_skypelogin.o net/ipv4/netfilter/ipt_skypemark.o net/ipv4/netfilter/ipt_skypetcpfile.o net/ipv4/netfilter/ipt_skypeudpfile.o net/ipv4/netfilter/ipt_state.o net/ipv4/netfilter/ipt_tproxy.o net/ipv4/netfilter/iptable_nat.o net/ipv4/netfilter/iptable_tproxy.o
*** Warning: "skypesn_default_timeout_value" [net/ipv4/netfilter/ip_conntrack.ko] undefined!
*** Warning: "skypefilehost_default_timeout_value" [net/ipv4/netfilter/ip_conntrack.ko] undefined!


4. compile ip_conntrack.mod.c 成為 ip_conntrack.mod.o object file
gcc -Wp,-MD,net/ipv4/netfilter/.ip_conntrack.mod.o.d -nostdinc -iwithprefix include -D__KERNEL__ -Iinclude -Wall -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Os -fomit-frame-pointer -pipe -msoft-float -mpreferred-stack-boundary=2 -march=i386 -Iinclude/asm-i386/mach-default -DKBUILD_BASENAME=ip_conntrack -DKBUILD_MODNAME=ip_conntrack -DMODULE -c -o net/ipv4/netfilter/ip_conntrack.mod.o net/ipv4/netfilter/ip_conntrack.mod.c
// 再將 ip_conntrack.o + ip_conntrack.mod.o merge 成為 ip_conntrack.ko
ld -m elf_i386 -r -o net/ipv4/netfilter/ip_conntrack.ko net/ipv4/netfilter/ip_conntrack.o net/ipv4/netfilter/ip_conntrack.mod.o



gcc -Wp,-MD,net/ipv4/netfilter/.ip_conntrack.mod.o.d -nostdinc -iwithprefix include -D__KERNEL__ -Iinclude -Wall -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Os -fomit-frame-pointer -pipe -msoft-float -mpreferred-stack-boundary=2 -march=i386 -Iinclude/asm-i386/mach-default -DKBUILD_BASENAME=ip_conntrack -DKBUILD_MODNAME=ip_conntrack -DMODULE -c -o net/ipv4/netfilter/ip_conntrack.mod.o net/ipv4/netfilter/ip_conntrack.mod.c


ld -m elf_i386 -r -o net/ipv4/netfilter/ip_conntrack.ko net/ipv4/netfilter/ip_conntrack.o net/ipv4/netfilter/ip_conntrack.mod.o


** build-in kernel binary, 產生 built-in.o

ld -m elf_i386 -r -o net/ipv4/netfilter/built-in.o net/ipv4/netfilter/ip_tables.o net/ipv4/netfilter/iptable_filter.o net/ipv4/netfilter/iptable_mangle.o net/ipv4/netfilter/iptable_raw.o net/ipv4/netfilter/ipt_limit.o net/ipv4/netfilter/ipt_hashlimit.o net/ipv4/netfilter/ipt_sctp.o net/ipv4/netfilter/ipt_mark.o net/ipv4/netfilter/ipt_mac.o net/ipv4/netfilter/ipt_iprange.o net/ipv4/netfilter/ipt_pkttype.o net/ipv4/netfilter/ipt_multiport.o net/ipv4/netfilter/ipt_owner.o net/ipv4/netfilter/ipt_tos.o net/ipv4/netfilter/ipt_recent.o net/ipv4/netfilter/ipt_ecn.o net/ipv4/netfilter/ipt_dscp.o net/ipv4/netfilter/ipt_ah.o net/ipv4/netfilter/ipt_esp.o net/ipv4/netfilter/ipt_length.o net/ipv4/netfilter/ipt_ttl.o net/ipv4/netfilter/ipt_tcpmss.o net/ipv4/netfilter/ipt_realm.o net/ipv4/netfilter/ipt_addrtype.o net/ipv4/netfilter/ipt_physdev.o net/ipv4/netfilter/ipt_comment.o net/ipv4/netfilter/ipt_REJECT.o net/ipv4/netfilter/ipt_TOS.o net/ipv4/netfilter/ipt_ECN.o net/ipv4/netfilter/ipt_DSCP.o net/ipv4/netfilter/ipt_MARK.o net/ipv4/netfilter/ipt_CLASSIFY.o net/ipv4/netfilter/ipt_DLOG.o net/ipv4/netfilter/ipt_LOG.o net/ipv4/netfilter/ipt_CONNMARK.o net/ipv4/netfilter/ipt_TCPRST.o net/ipv4/netfilter/ipt_TCPMSS.o net/ipv4/netfilter/ipt_CLUSTERIP.o net/ipv4/netfilter/arp_tables.o net/ipv4/netfilter/arpt_mangle.o net/ipv4/netfilter/arptable_filter.o net/ipv4/netfilter/ip_queue.o net/ipv4/netfilter/netreport.o


ip_conntrack_standalone.c
ip_conntrack_core.c
ip_conntrack_proto_generic.c
ip_conntrack_proto_tcp.c
ip_conntrack_proto_udp.c
ip_conntrack_proto_icmp.c
L7SessionMoniter.c
skypesn.c
skypefilehost.c

ip_nat_standalone.c
ip_nat_rule.c
ip_nat_core.c
ip_nat_helper.c
ip_nat_proto_unknown.c
ip_nat_proto_tcp.c
ip_nat_proto_udp.c
ip_nat_proto_icmp.c

ip_conntrack_proto_sctp.c
ip_conntrack_amanda.c
ip_conntrack_tftp.c
ip_conntrack_ftp.c
ip_conntrack_irc.c

ip_nat_amanda.c
ip_nat_tftp.c
ip_nat_ftp.c
ip_nat_irc.c



iptable_tproxy.c
ipt_helper.c
ipt_ipgroup.c
ipt_l7length.c
ipt_dsize.c

沒有留言: