[ARM] unexpected sizeof() of a complex packed type

Dmitry Antipov dmantipov@yandex.ru
Thu Nov 16 14:09:59 GMT 2023


(The following sample is taken from my LKML post at https://lkml.org/lkml/2023/11/15/213)

$ cat t-build-bug.c

struct vring_tx_mac {
	unsigned int d[3];
	unsigned int ucode_cmd;
} __attribute__((packed));

struct vring_rx_mac {
	unsigned int d0;
	unsigned int d1;
	unsigned short w4;
	union { struct { unsigned short pn_15_0; unsigned int pn_47_16; } __attribute__((packed));
		struct { unsigned short pn_15_0; unsigned int pn_47_16; } __attribute__((packed)) pn;
	};
} __attribute__((packed));

struct wil_ring_dma_addr {
	unsigned int addr_low;
	unsigned short addr_high;
} __attribute__((packed));

struct vring_tx_dma {
	unsigned int d0;
	struct wil_ring_dma_addr addr;
	unsigned char ip_length;
	unsigned char b11;
	unsigned char error;
	unsigned char status;
	unsigned short length;
} __attribute__((packed));

struct vring_tx_desc {
	struct vring_tx_mac mac;
	struct vring_tx_dma dma;
} __attribute__((packed));

struct wil_ring_tx_enhanced_mac {
	unsigned int d[3];
	unsigned short tso_mss;
	unsigned short scratchpad;
} __attribute__((packed));

struct wil_ring_tx_enhanced_dma {
	unsigned char l4_hdr_len;
	unsigned char cmd;
	unsigned short w1;
	struct wil_ring_dma_addr addr;
	unsigned char ip_length;
	unsigned char b11;
	unsigned short addr_high_high;
	unsigned short length;
} __attribute__((packed));

struct wil_tx_enhanced_desc {
	struct wil_ring_tx_enhanced_mac mac;
	struct wil_ring_tx_enhanced_dma dma;
} __attribute__((packed));

union wil_tx_desc {
	struct vring_tx_desc legacy;
	struct wil_tx_enhanced_desc enhanced;
} __attribute__((packed));

struct vring_rx_dma {
	unsigned int d0;
	struct wil_ring_dma_addr addr;
	unsigned char ip_length;
	unsigned char b11;
	unsigned char error;
	unsigned char status;
	unsigned short length;
} __attribute__((packed));

struct vring_rx_desc {
	struct vring_rx_mac mac;
	struct vring_rx_dma dma;
} __attribute__((packed));

struct wil_ring_rx_enhanced_mac {
	unsigned int d[3];
	unsigned short buff_id;
	unsigned short reserved;
} __attribute((packed));

struct wil_ring_rx_enhanced_dma {
	unsigned int d0;
	struct wil_ring_dma_addr addr;
	unsigned short w5;
	unsigned short addr_high_high;
	unsigned short length;
} __attribute((packed));

struct wil_rx_enhanced_desc {
	struct wil_ring_rx_enhanced_mac mac;
	struct wil_ring_rx_enhanced_dma dma;
} __attribute((packed));

union wil_rx_desc {
	struct vring_rx_desc legacy;
	struct wil_rx_enhanced_desc enhanced;
} __attribute__((packed));

union wil_ring_desc {
	union wil_tx_desc tx;
	union wil_rx_desc rx;
} __attribute__((packed));

int f (void) {
	return sizeof(union wil_ring_desc);
}

$ arm-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=arm-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/arm-linux-gnueabi/13/lto-wrapper
Target: arm-linux-gnueabi
Configured with: ../gcc-13.2.1-20230728/configure --bindir=/usr/bin --build=x86_64-redhat-linux-gnu --datadir=/usr/share --disable-decimal-float --disable-dependency-tracking --disable-gold 
--disable-libgcj --disable-libgomp --disable-libmpx --disable-libquadmath --disable-libssp --disable-libunwind-exceptions --disable-shared --disable-silent-rules --disable-sjlj-exceptions 
--disable-threads --with-ld=/usr/bin/arm-linux-gnu-ld --enable-__cxa_atexit --enable-checking=release --enable-gnu-unique-object --enable-initfini-array --enable-languages=c,c++ 
--enable-linker-build-id --enable-lto --enable-nls --enable-obsolete --enable-plugin --enable-targets=all --exec-prefix=/usr --host=x86_64-redhat-linux-gnu --includedir=/usr/include 
--infodir=/usr/share/info --libexecdir=/usr/libexec --localstatedir=/var --mandir=/usr/share/man --prefix=/usr --program-prefix=arm-linux-gnu- --sbindir=/usr/sbin --sharedstatedir=/var/lib 
--sysconfdir=/etc --target=arm-linux-gnueabi --with-bugurl=http://bugzilla.redhat.com/bugzilla/ --with-gcc-major-version-only --with-isl --with-newlib --with-plugin-ld=/usr/bin/arm-linux-gnu-ld 
--with-sysroot=/usr/arm-linux-gnu/sys-root --with-system-libunwind --with-system-zlib --without-headers --with-tune=generic-armv7-a --with-arch=armv7-a --with-float=hard --with-fpu=vfpv3-d16 
--with-abi=aapcs-linux --enable-gnu-indirect-function --with-linker-hash-style=gnu
Thread model: single
Supported LTO compression algorithms: zlib zstd
gcc version 13.2.1 20230728 (Red Hat Cross 13.2.1-1) (GCC)

$ arm-linux-gnu-gcc -Os -c t-build-bug.c
$ arm-linux-gnu-objdump -j .text -D t-build-bug.o

t-build-bug.o:     file format elf32-littlearm

Disassembly of section .text:

00000000 <f>:
    0:	e3a00020 	mov	r0, #32                         ;; As expected
    4:	e12fff1e 	bx	lr

;; Now with a lot of weird things taken from the kernel's CFLAGS...

$ arm-linux-gnu-gcc -mlittle-endian -std=gnu11 -fshort-wchar -funsigned-char -fno-common -fno-PIE -fno-strict-aliasing -fno-dwarf2-cfi-asm -mno-fdpic -fno-omit-frame-pointer -mapcs -mno-sched-prolog 
-fno-ipa-sra -mabi=apcs-gnu -mno-thumb-interwork -marm -Wa,-mno-warn-deprecated -march=armv4 -mtune=xscale -msoft-float -Uarm -fno-delete-null-pointer-checks -Os -fno-allow-store-data-races 
-fno-reorder-blocks -fno-ipa-cp-clone -fno-partial-inlining -fno-stack-protector -fno-omit-frame-pointer -fno-optimize-sibling-calls -ftrivial-auto-var-init=zero -fno-stack-clash-protection -pg 
-fno-inline-functions-called-once -fstrict-flex-arrays=3 -fno-strict-overflow -fno-stack-check -fconserve-stack -fsanitize=kernel-address -fasan-shadow-offset=0x1f000000 --param asan-globals=1 --param 
asan-instrumentation-with-call-threshold=10000 --param asan-instrument-allocas=1 --param asan-stack=1 --param asan-kernel-mem-intrinsic-prefix=1 -c t-build-bug.c
$ arm-linux-gnu-objdump -j .text -D t-build-bug.o

t-build-bug.o:     file format elf32-littlearm

Disassembly of section .text:

00000000 <f>:
    0:	e1a0c00d 	mov	ip, sp
    4:	e92dd800 	push	{fp, ip, lr, pc}
    8:	e24cb004 	sub	fp, ip, #4
    c:	e52de004 	push	{lr}		@ (str lr, [sp, #-4]!)
   10:	ebfffffe 	bl	0 <__gnu_mcount_nc>
   14:	e3a00022 	mov	r0, #34	@ 0x22                  ;; Hmmm... sizeof(xxx) changed?
   18:	e89da800 	ldm	sp, {fp, sp, pc}

Dmitry


More information about the Gcc mailing list