Issue Details (XML | Word | Printable)

Key: PCC-332
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Anders Magnusson
Reporter: Iain Hibbert
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
pcc

#pragma pack not working "correctly"

Created: 24/Apr/11 07:40 PM   Updated: 25/Apr/11 04:54 PM
Component/s: i386 target
Affects Version/s: None
Fix Version/s: None

File Attachments: 1. File pack.c (0.2 kB)

Environment: NetBSD/i386


 Description  « Hide
Where "correct" is defined as "how gcc does it" or more importantly "how the win32 MIDL
compiler does it" since the pragma pack support in gcc is compatibility for that.. The
gcc manpage is not fully informative and I think the definitive reference might be at

  http://msdn.microsoft.com/en-us/library/ms253935.aspx

and the wikipedia entry at

  http://en.wikipedia.org/wiki/Data_structure_alignment#Default_packing_and_.23pragma_pack

points out that the default packing size can only be reduced. The attached source file pack.c
will show the problem if the assembler output is examined.. every item should not be packed,
only if the default (for that data type) is greater than the packing given. Thus, when pack(4)
is given, short and char types are left at their normal alignments..

I believe this affects running the ACPICA code, which uses carefully constructed
structures to traverse constant data arrays - currently, it crashes following invalid
pointers

 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Anders Magnusson added a comment - 25/Apr/11 12:08 PM
I checked with your example and compared gcc and pcc, and did get the same output...?

Iain Hibbert added a comment - 25/Apr/11 01:51 PM
Hm, with pcc-current and gcc-4.1.3 on i386, I see the following

% cat char.c
#pragma pack(4)

struct {
char a;
char b;
} foo = { 1, 2 };
% pcc -S char.c && cat char.s
.data
.align 4
.globl foo
.type foo,@object
.size foo,8
foo:
.byte 1
.zero 3
.byte 2
.zero 3
.ident "PCC: pcc 1.1.0.DEVEL [20110419] (netbsd)"
% gcc -S char.c && cat char.s
.file "char.c"
.globl foo
.data
.type foo, @object
.size foo, 2
foo:
.byte 1
.byte 2
.ident "GCC: (GNU) 4.1.3 20080704 prerelease (NetBSD nb2 20081120)"

ie pcc pads char types by 3 bytes..

% cat short.c
#pragma pack(4)

struct {
short a;
short b;
} foo = { 1, 2 };
% pcc -S short.c && cat short.s
.data
.align 4
.globl foo
.type foo,@object
.size foo,8
foo:
.short 0x1
.zero 2
.short 0x2
.zero 2
.ident "PCC: pcc 1.1.0.DEVEL [20110419] (netbsd)"
% gcc -S short.c && cat short.s
.file "short.c"
.globl foo
.data
.align 2
.type foo, @object
.size foo, 4
foo:
.value 1
.value 2
.ident "GCC: (GNU) 4.1.3 20080704 prerelease (NetBSD nb2 20081120)"

ie pcc pads short types by two bytes..

% cat int.c
#pragma pack(4)

struct {
int a;
int b;
} foo = { 1, 2 };
% pcc -S int.c && cat int.s
.data
.align 4
.globl foo
.type foo,@object
.size foo,8
foo:
.long 1
.long 2
.ident "PCC: pcc 1.1.0.DEVEL [20110419] (netbsd)"
% gcc -S int.c && cat int.s
.file "int.c"
.globl foo
.data
.align 4
.type foo, @object
.size foo, 8
foo:
.long 1
.long 2
.ident "GCC: (GNU) 4.1.3 20080704 prerelease (NetBSD nb2 20081120)"

ie pcc provides no padding for ints, with pack(4) which is the same as gcc, but changing that to pack(8)

% cat int_pack8.c
#pragma pack(8)

struct {
int a;
int b;
} foo = { 1, 2 };
% pcc -S int_pack8.c && cat int_pack8.s
.data
.align 8
.globl foo
.type foo,@object
.size foo,16
foo:
.long 1
.zero 4
.long 2
.zero 4
.ident "PCC: pcc 1.1.0.DEVEL [20110419] (netbsd)"
% gcc -S int_pack8.c && cat int_pack8.s
.file "int_pack8.c"
.globl foo
.data
.align 4
.type foo, @object
.size foo, 8
foo:
.long 1
.long 2
.ident "GCC: (GNU) 4.1.3 20080704 prerelease (NetBSD nb2 20081120)"

and pcc pads int types with 4 bytes.

Anders Magnusson added a comment - 25/Apr/11 04:54 PM
Ok, now I got it. Fixed now, thanks.