This is the mail archive of the crossgcc@sources.redhat.com mailing list for the crossgcc project.

See the CrossGCC FAQ for lots more infromation.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Structure alignment with GCC


Hello ppl,

I'm having trouble with structure alignment using gcc. The problem is
that if a structure contains only 1-byte fields, gcc will use 1-byte
alignment for the entire structure. Consider the following example:
_______________________________________________________
typedef struct { char a; short b; char c; } DataTypeA;
typedef struct { char a; char b; char c; } DataTypeB;

const char Data1 = 10;

const DataTypeB Data2 = { 5, 6, 7 };

const short MyAlign = __alignof__ (Data2);
_______________________________________________________

If I build as shown above Data1 will be put at (e.g.) 0x10000 followed
by Data2 at 0x10001. In this case, the alignment of Data2, MyAlign=1.

However, if I change Data2 to be of type DataTypeA (containing a short)
instead of DataTypeB (having all chars), then Data2 is placed at 0x10002
and MyAlign=2. Of course, it puts a padding byte (0x00) after Data1 and
before Data2.

I know I can use GNU's 'aligned' attribute to force the alignment of a
particular variable, as follows.
_______________________________________________________
typedef struct { char a; short b; char c; } DataTypeA;
typedef struct { char a; char b; char c; } DataTypeB;

const char Data1 = 10;

const DataTypeB Data2 __attribute__ ((aligned (2))) = { 5, 6, 7 };

const short MyAlign = __alignof__ (Data2);
const short MySize = sizeof(Data2);
_______________________________________________________

This works just fine (and is currently what I'm doing), but this is VERY
UNDESIRABLE. Here's why:

I'm porting a large project from MRI (Microtek Research) to GNU. The MRI
compiler apparently has a default structure alignment of 2, even for
structures that contain only 1-byte fields. Part of the project requires
that I build data tables whose format is identical to those built with
MRI, because in some cases we will have to have MRI-built code access
the tables that are being converted to GNU. The nature of the project
demands that we convert the table generation to GNU before the main
program. I *really* don't want to have to find all of these particular
situations (an odd-sized data element followed by a structure with
1-byte alignment). This would be time-consuming and the risk of missing
something (or something going wrong in the future) would be undesirable.
I'd also prefer not to add the aligned attribute to EVERY single
structure (although this would probably be most effective) -- note that
this would have to be done for each array or structure variable. In
either case the maintainability of the project is diminished because
each time a new variable is declared that meets the above criteria, the
maintainer will have to be aware of the problem and remember to insert
the 'aligned' attribute.

All of this is most undesirable and not at all elegant (as well as prone
to failure). I'd like some kind of command line option to specify the
default alignment of structures, but have searched the documentation (a
bit outdated) and found nothing. The only possible solution I've seen
mentioned so far is:
_______________________________________________________
Add a line to ./gcc-2.95.2/gcc/config/i386/linux.h:
   #define HANDLE_PRAGMA_PACK_PUSH_POP 1
Rebuild the compiler. Now you can use #pragma pack(push,<n>) and #pragma
pack(push) to change the maxiumum alignment (in bytes) of fields within
a structure.
_______________________________________________________

Based on the description, it does not sound like the use of #pragma
pack() will have the desired effect, but I suppose I'll try it (compiler
is building now). In the above case it seems the person was building an
i386 target on a linux host, so I don't know if it would even work for
me...

Here is my setup:
   Target=m68k-coff
   Windows NT 4.0 Workstation
   Cygnus Cygwin 1.1.2
   binutils-2.10
   gcc-2.95.2
   newlib-1.8.2

So, at the moment I'm stuck and really don't like the approach I've had
to take so far. If anyone can help (including confirming for me that
there is no better solution), I'd appreciate it.

Thanks
Chris
begin:vcard 
n:Bahns;Christopher
tel;home:812-342-4714
tel;work:812-342-4714
x-mozilla-html:FALSE
adr:;;;;;;
version:2.1
email;internet:chris@bahns.com
fn:Christopher Bahns
end:vcard

------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]