Re: Subtleties of __attribute__((packed))

From: Phil Endecott
Date: Wed Dec 06 2006 - 10:22:59 EST


Jan Blunck wrote:
On 12/6/06, Phil Endecott <phil_arcwk_endecott@xxxxxxxxxxxx> wrote:
I used to think that this:

struct foo {
int a __attribute__((packed));
char b __attribute__((packed));
... more fields, all packed ...
};

was exactly the same as this:

struct foo {
int a;
char b;
... more fields ...
} __attribute__((packed));

but it is not, in a subtle way.


The same code is generated. [...]

I don't think so. Example:

struct test {
int a __attribute__((packed));
int b __attribute__((packed));
};

char c = 1;
struct test t = { .a=2, .b=3 };

$ arm-linux-gnu-gcc -O2 -S -W -Wall test1.c

.file "test2.c"
.global c
.data
.type c, %object
.size c, 1
c:
.byte 1
.global t
.align 2 <<<<<<<<===== t is aligned
.type t, %object
.size t, 8
t:
.word 2
.word 3
.ident "GCC: (GNU) 4.1.2 20061028 (prerelease) (Debian 4.1.1-19)"


Compare with:

struct test {
int a;
int b;
} __attribute__((packed));

char c = 1;
struct test t = { .a=2, .b=3 };

$ arm-linux-gnu-gcc -O2 -S -W -Wall test2.c

.file "test1.c"
.global c
.data
.type c, %object
.size c, 1
c:
.byte 1
.global t <<<<<< "align" has gone, t is unaligned
.type t, %object
.size t, 8
t:
.4byte 2
.4byte 3
.ident "GCC: (GNU) 4.1.2 20061028 (prerelease) (Debian 4.1.1-19)"



Phil.





-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/