GCC __attribute___((aligned)) not aligning as expected

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
bitto
Posts: 16
Joined: Thu Jan 30, 2014 9:45 am

GCC __attribute___((aligned)) not aligning as expected

Post by bitto »

Hello,
I'm facing an alignment problem with problem with gcc 7 (the one contained in ChibiStudio preview 21) compiling code for ST NUCLEO-F466RE board.

In fact with this code:

Code: Select all

static GUARDEDMEMORYPOOL_DECL(memoryPool, TELEGRAM_SIZE, sizeof (stkalign_t));
static telegram_t mpTelegramWa[TELEGRAM_POOL_SIZE] __attribute__((aligned(sizeof(stkalign_t))));


gcc produces an array of [font=Courier New]telegram_t[/font] structs aligned on a 4 byte boundary:

  • element #0 address is 0x20001850
  • element #1 address is 0x20001864

even if [font=Courier New]sizeof (stkalign_t)[/font] correctly resovles to 8.

What am I missing?

Thanks.

Alberto
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: GCC __attribute___((aligned)) not aligning as expected

Post by Giovanni »

Hi,

I think GCC aligns the base of the array, not each element of the array. You need to pad the size of your structure.

Giovanni
bitto
Posts: 16
Joined: Thu Jan 30, 2014 9:45 am

Re: GCC __attribute___((aligned)) not aligning as expected

Post by bitto »

Hi,

think GCC aligns the base of the array, not each element of the array.


it makes sense.

Incidentally, is it necessary to align memory to 8 byte boundaries on STM32, or could 4 be sufficient, since it's a 32 bit machine?

Thanks

Alberto
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: GCC __attribute___((aligned)) not aligning as expected

Post by steved »

It is possible to define an alignment for a type; then if you have an array of that type they will be individually aligned.

https://gcc.gnu.org/onlinedocs/gcc-3.3/ ... butes.html
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: GCC __attribute___((aligned)) not aligning as expected

Post by Giovanni »

bitto wrote:Incidentally, is it necessary to align memory to 8 byte boundaries on STM32, or could 4 be sufficient, since it's a 32 bit machine?


4 is sufficient, only for stacks ARM EABI mandates an 8 bytes alignment (4 works but they decided to align to 8).

Giovanni
Afanador
Posts: 1
Joined: Sat Nov 16, 2019 10:28 am

Re: GCC __attribute___((aligned)) not aligning as expected

Post by Afanador »

What was the reason they went for 8 instead of 4, Giovanni?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: GCC __attribute___((aligned)) not aligning as expected

Post by Giovanni »

I think forward compatibility with 64 bits bus architectures, aligned accesses are faster.

Giovanni
Post Reply