Hello. I am a maintainer of busybox project. One of our goals is to optimize our code for size. The following testcase was derived from busybox source:
struct client_config_t { char foreground; char quit_after_lease; char release_on_quit; char abort_if_no_lease; char *interface; char *script; char *clientid; char *fqdn; int retries; int timeout; char arp[6]; }; struct client_config_t udhcp_client_config1; struct client_config_t client_config2 = { /* Default options. */ .abort_if_no_lease = 0, .foreground = 0, .quit_after_lease = 0, .release_on_quit = 0, .interface = "eth0", .script = "/share/udhcpc/default.script", .clientid = 0, .fqdn = 0, .retries = 3, .timeout = 3, .arp = "\0\0\0\0\0\0", /* appease gcc-3.0 */ }; int udhcpc_main(int argc, char *argv[]) { udhcp_client_config1.interface = "eth0"; udhcp_client_config1.script = "/share/udhcpc/default.script"; udhcp_client_config1.retries = udhcp_client_config1.timeout = 3; return 0; } Even when compiled with optimisation for size (gcc -Os -S -fomit-frame-pointer dhcpc.c), both data- and bss-placed structures are aligned to 32 _bytes_. The structs are itself only _36 bytes_ large! When a lot of structures from different .o modules are combined into final executable, we waste a lot of space (on average, 15.5 bytes per structure): .file "dhcpc.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "eth0" .LC1: .string "/share/udhcpc/default.script" .text .globl udhcpc_main .type udhcpc_main, @function udhcpc_main: movl $.LC0, udhcp_client_config1+4 movl $.LC1, udhcp_client_config1+8 movl $3, udhcp_client_config1+24 movl $3, udhcp_client_config1+20 xorl %eax, %eax ret .size udhcpc_main, .-udhcpc_main .globl client_config2 .data .align 32 <---------------------HERE .type client_config2, @object .size client_config2, 36 client_config2: .byte 0 .byte 0 .byte 0 .byte 0 .long .LC0 .long .LC1 .long 0 .long 0 .long 3 .long 3 .string "" .string "" .string "" .string "" .string "" .string "" .zero 2 .comm udhcp_client_config1,36,32 <---- HERE .ident "GCC: (GNU) 4.1.1" .section .note.GNU-stack,"",@progbits Is it possible to instruct gcc to use smaller alignment? BTW, is there any progress on bug 22158 (also related to data alignment)? I have a patch there... -- Summary: overzealous alignment of structure - 32 bytes (not bits!) Product: gcc Version: 4.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: vda dot linux at googlemail dot com GCC build triplet: i386-pc-linux-gnu GCC host triplet: i386-pc-linux-gnu GCC target triplet: i386-pc-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30004