从内核模块的结构中删除填充

发布于 2024-08-31 17:16:17 字数 2648 浏览 5 评论 0原文

我正在使用标准命令编译一个内核模块,其中包含大小为 34 的结构。

make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules

sizeof(some_struct) 是 36 而不是 34,即编译器正在填充结构。

如何删除此填充?

运行 make V=1 显示 gcc 编译器选项传递为

make -I../inc -C /lib/modules/2.6.29.4-167.fc11.i686.PAE/build M=/home/vishal/20100426_eth_vishal/organised_eth/src modules

make[1]: Entering directory `/usr/src/kernels/2.6.29.4-167.fc11.i686.PAE'
test -e include/linux/autoconf.h -a -e include/config/auto.conf || (  \
 echo;        \
 echo "  ERROR: Kernel configuration is invalid.";  \
 echo "         include/linux/autoconf.h or include/config/auto.conf are missing."; \
 echo "         Run 'make oldconfig && make prepare' on kernel src to fix it."; \
 echo;        \
 /bin/false)

mkdir -p /home/vishal/20100426_eth_vishal/organised_eth/src/.tmp_versions ; rm -f /home/vishal/20100426_eth_vishal/organised_eth/src/.tmp_versions/*

make -f scripts/Makefile.build obj=/home/vishal/20100426_eth_vishal/organised_eth/src

  gcc -Wp,-MD,/home/vishal/20100426_eth_vishal/organised_eth/src/.eth_main.o.d  -nostdinc -isystem /usr/lib/gcc/i586-redhat-linux/4.4.0/include -Iinclude  -I/usr/src/kernels/2.6.29.4-167.fc11.i686.PAE/arch/x86/include -include include/linux/autoconf.h -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Os -m32 -msoft-float -mregparm=3 -freg-struct-return -mpreferred-stack-boundary=2 -march=i686 -mtune=generic -Wa,-mtune=generic32 -ffreestanding -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -Iarch/x86/include/asm/mach-generic -Iarch/x86/include/asm/mach-default -Wframe-larger-than=1024 -fno-stack-protector -fno-omit-frame-pointer -fno-optimize-sibling-calls -g -pg -Wdeclaration-after-statement -Wno-pointer-sign -fwrapv -fno-dwarf2-cfi-asm -DTX_DESCRIPTOR_IN_SYSTEM_MEMORY -DRX_DESCRIPTOR_IN_SYSTEM_MEMORY -DTX_BUFFER_IN_SYSTEM_MEMORY -DRX_BUFFER_IN_SYSTEM_MEMORY -DALTERNATE_DESCRIPTORS -DEXT_8_BYTE_DESCRIPTOR -O0 -Wall -DT_ETH_1588_051 -DALTERNATE_DESCRIPTORS -DEXT_8_BYTE_DESCRIPTOR -DNETHERNET_INTERRUPTS -DETH_IEEE1588_TESTS -DSNAPTYPSEL_TMSTRENA_TEVENTENA_TESTS -DT_ETH_1588_140_147 -DLOW_DEBUG_PRINTS -DMEDIUM_DEBUG_PRINTS -DHIGH_DEBUG_PRINTS -DMODULE -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(eth_main)"  -D"KBUILD_MODNAME=KBUILD_STR(conxt_eth)"  -c -o /home/vishal/20100426_eth_vishal/organised_eth/src/eth_main.o /home/vishal/20100426_eth_vishal/organised_eth/src/eth_main.c 

I am compiling a kernel module, containing a structure of size 34, using the standard command.

make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules

The sizeof(some_structure) is coming as 36 instead of 34 i.e. the compiler is padding the structure.

How do I remove this padding?

Running make V=1 shows the gcc compiler options passed as

make -I../inc -C /lib/modules/2.6.29.4-167.fc11.i686.PAE/build M=/home/vishal/20100426_eth_vishal/organised_eth/src modules

make[1]: Entering directory `/usr/src/kernels/2.6.29.4-167.fc11.i686.PAE'
test -e include/linux/autoconf.h -a -e include/config/auto.conf || (  \
 echo;        \
 echo "  ERROR: Kernel configuration is invalid.";  \
 echo "         include/linux/autoconf.h or include/config/auto.conf are missing."; \
 echo "         Run 'make oldconfig && make prepare' on kernel src to fix it."; \
 echo;        \
 /bin/false)

mkdir -p /home/vishal/20100426_eth_vishal/organised_eth/src/.tmp_versions ; rm -f /home/vishal/20100426_eth_vishal/organised_eth/src/.tmp_versions/*

make -f scripts/Makefile.build obj=/home/vishal/20100426_eth_vishal/organised_eth/src

  gcc -Wp,-MD,/home/vishal/20100426_eth_vishal/organised_eth/src/.eth_main.o.d  -nostdinc -isystem /usr/lib/gcc/i586-redhat-linux/4.4.0/include -Iinclude  -I/usr/src/kernels/2.6.29.4-167.fc11.i686.PAE/arch/x86/include -include include/linux/autoconf.h -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Os -m32 -msoft-float -mregparm=3 -freg-struct-return -mpreferred-stack-boundary=2 -march=i686 -mtune=generic -Wa,-mtune=generic32 -ffreestanding -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -Iarch/x86/include/asm/mach-generic -Iarch/x86/include/asm/mach-default -Wframe-larger-than=1024 -fno-stack-protector -fno-omit-frame-pointer -fno-optimize-sibling-calls -g -pg -Wdeclaration-after-statement -Wno-pointer-sign -fwrapv -fno-dwarf2-cfi-asm -DTX_DESCRIPTOR_IN_SYSTEM_MEMORY -DRX_DESCRIPTOR_IN_SYSTEM_MEMORY -DTX_BUFFER_IN_SYSTEM_MEMORY -DRX_BUFFER_IN_SYSTEM_MEMORY -DALTERNATE_DESCRIPTORS -DEXT_8_BYTE_DESCRIPTOR -O0 -Wall -DT_ETH_1588_051 -DALTERNATE_DESCRIPTORS -DEXT_8_BYTE_DESCRIPTOR -DNETHERNET_INTERRUPTS -DETH_IEEE1588_TESTS -DSNAPTYPSEL_TMSTRENA_TEVENTENA_TESTS -DT_ETH_1588_140_147 -DLOW_DEBUG_PRINTS -DMEDIUM_DEBUG_PRINTS -DHIGH_DEBUG_PRINTS -DMODULE -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(eth_main)"  -D"KBUILD_MODNAME=KBUILD_STR(conxt_eth)"  -c -o /home/vishal/20100426_eth_vishal/organised_eth/src/eth_main.o /home/vishal/20100426_eth_vishal/organised_eth/src/eth_main.c 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青巷忧颜 2024-09-07 17:16:17

如果使用 GCC,您可以在结构上使用 packed 属性来防止填充:

struct foo
{
    void * bar;
}
__attribute__( ( packed ) );

If using GCC, you can use the packed attribute on your structure to prevent padding:

struct foo
{
    void * bar;
}
__attribute__( ( packed ) );
痞味浪人 2024-09-07 17:16:17

#pragma pack 可能有效

#pragma pack might work

傻比既视感 2024-09-07 17:16:17

我怀疑 GCC 强制整个结构对齐到 32 位边界,因此它的大小是 4 的倍数

。想象一下以下情况。
结构体foo
{

无效 * 栏 ;
其他一些东西......
};

结构 foo my_foo_array[10];

那么如果 sizeof(struct foo) 不是 4 的倍数那么。
my_foo_array[0].bar 与 my_foo_array[1].bar 具有不同的内存对齐方式。处理器需要执行 2 次 32 位内存访问才能访问 my_foo_array[1].bar 的所有四个字节。 x86 处理器将对未对齐的 32 位值进行重新组装,但大多数其他处理器将抛出某种形式的总线错误异常,这是不好的。

打包属性表示结构的元素如何相互打包,但在正常操作中,结构的开头需要放置在 32 位对齐的地址上。

我希望这能更好地解释事情。

I suspect that GCC is forcing the total structure to be aligned onto a 32 bit boundary, so its size is a multiple of 4.

Imagine the following.
struct foo
{

void * bar ;
some other stuff .....
};

struct foo my_foo_array[10];

Then if the sizeof(struct foo) is not a multiple of 4 then.
my_foo_array[0].bar has a different memory alignment to my_foo_array[1].bar. The processor would need to perform 2 32 bit memory accesses in order to access all four bytes of my_foo_array[1].bar. x86 processors will do this reassembly of misaligned 32 bit values but most other processors will throw some form of bus error exception which is not good.

The packed attribute signals how the elements of the structure are packed with respect to each other, but in normal operation the start of structure needs to placed on a 32 bit aligned address.

I hope this explains things a little better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文