views:

769

answers:

3

I am trying to byte-align a function to 16-byte boundary using the 'aligned(16)' attribute. I did the following: void __attribute__((aligned(16))) function() { }

(Source: http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html)

But when I compile (gcc foo.c ; no makefiles or linker scripts used), I get the following error:

FOO.c:99: error: alignment may not be specified for 'function'

I tried aligning to 4,8,32, etc as well but the error remains the same. I need this to align an Interrupt Service Routine for a powerpc-based processor. What is the correct way of doing so ?

+6  A: 

Why don't you just pass the -falign-function=16 to gcc when compiling?

Gonzalo
That's too easy, isn't it?
Jonathan Leffler
Yes I can do that, but that would align all functions to 16byte, whereas I need only the ISR to get 16 byte aligned. But, if there is no other option I will have to do the same.
Sukanto
If you're not using GCC >=4.3, this is the way to go.
Gonzalo
A: 

Doesn't gcc automatically choose the right function/data alignment for platforms where it is needed (e.g. sparc/powerpc)?

Seeing as the OP is referring to a powerpc-**based** processor, I'd assume gcc doesn't automagically know the proper alignment.
Michael Foukarakis
Yes, gcc will know the alignment for instructions on an architecture (e.g. in my case I am using gcc which generates binaries for powerpc).But instructions in powerpc need to be just 4byte aligned whereas the ISR needs to be 16byte aligned.
Sukanto
+2  A: 

You are probably using an older version of gcc that does not support that attribute. The documentation link you provided is for the "current development" of gcc. Looking through the various releases, the attribute only appears in the documentation for gcc 4.3 and beyond.

R Samuel Klatchko
I guess that is possible. My compiler is gcc 4.0.0.
Sukanto