Is it possible to solve the align problem with compiler options ?

I am involved again with problems related to alignment. For instance, consider the small code below, where a frame received from ethernet is formatted using a casting operation: ? wBufferSize = recvfrom((char*)bBuffer,MAXCONF_SIZE, saddr_in,sddr_len); SSEConfMarshal psSEConfMarshal = (SSEConfMarshal) bBuffer; switch(psSEConfMarshal->wMethodID) ? In this case, SSEConfMarshal is defined as: typedef STRUCT SEConfMarshal_str { BYTE bVersion; WORD wMethodID; /* WORD = 2 bytes */ BYTE bRevision; BYTE bParameters; } SSEConfMarshal; The code generated by compiler is: SSEConfMarshal psSEConfMarshal = (SSEConfMarshal) bBuffer; e1a02007 MOV R2, R7 switch(psSEConfMarshal->wMethodID) e1d220b2 LDRH R2,[R2,2] Suppose the buffer contents is ?00 ea 03 00 f1 fb 09?. The instruction generated by the compiler (LDRH R2,[R2,2]) will load 0x0300 and not 0xea03 since the offset is 2 ([R2,2]) and not 1 ! Moreover, I imagine if compiler generates ([R2,1]), a misaligned access will take place. What I can I do to solve this problem ? Is it possible to solve the alignment problem with compiler options ?

A c-compiler normally pads the elements in structures to the next “useful” address. Some compilers have a qualifier __packed, then he will not insert a padding. But i don’t know what the compiler does with the unaligned word access. typedef __packed STRUCT SEConfMarshal_str { BYTE bVersion; WORD wMethodID; /* WORD = 2 bytes */ BYTE bRevision; BYTE bParameters; } SSEConfMarshal;

Hi,> typedef __packed STRUCT SEConfMarshal_str I found a driver level directive called pack (-pack=1) in GHS compiler that does this thing, but for all code and not only for a specific structure. The problem is that we have libs provided by NS and GHS compiled without ?pack=1. So, NetArm 50 generates a data abort exception when a not aligned address is used inside these libs. I will check if GHS compiler has this kind of directive that can be applied to structures. Thanks ! Marcelo Barros

Hi,I found a very handy trick pragma in GHS C/C++ Manual to specify the alignment: #pragma pack (1) The default alignment can be restored with: #pragma pack () So, for my example: #pragma pack (1) typedef STRUCT SEConfMarshal_str { BYTE bVersion; WORD wMethodID; BYTE bRevision; BYTE bParameters; } SSEConfMarshal; /* … other structures … */ #pragma pack () Bye Marcelo

I’ve attached a document describing alignment and packing with the GHS tools; it should answer most of your questions.