Aligning static arrays in the .bss section of the linker file
I have a function:
void testfunction() {
static char_t theChar1 = 1;
static unsigned char smallArray[1];
static unsigned char largeArray[135];
...
}
and the linker file:
. = ALIGN(4);
_edata = . ;
PROVIDE (edata = .);
.bss (NOLOAD) :
{
__bss_start = . ;
__bss_start__ = . ;
*(.bss)
*(.bss.*)
*(COMMON)
. = ALIGN(4);
} > ramEXT
. = ALIGN(4);
__bss_end__ = . ;
PROVIDE (__bss_end = .);
I need static arrays (.bss data) to align on 4-byte boundaries, but the arrays seem to refuse to do this. Structures and primitive types are aligned exactly (see Fill Lines), but arrays all end up. Here is my map file:
.data.firstTimeFlag.7295
0xa000098c 0x4 output/file1.o
.data.theChar1.5869
0xa0000990 0x1 output/file2.o
*fill* 0xa0000991 0x3 00
.data.debounce
0xa0000994 0x270 output/file3.o
...
.bss.initialized.5826
0xa000812c 0x1 output/file2.o
*fill* 0xa000812d 0x3 00
.bss.allocator.5825
0xa0008130 0x34 output/file2.o
.bss.largeArray.5869
0xa0008164 0x87 output/file2.o
.bss.smallArray.5868
0xa00081eb 0x1 output/file2.o
.bss.initialized.5897
0xa00081ec 0x1 output/file2.o
*fill* 0xa00081ed 0x3 00
.bss.allocator.5896
Does anyone know how to align arrays?
source to share
I'm not sure if you can do this with a linker script, and I'm puzzled by your purpose; the alignment attribute attached to each assembly declaration is presumably ABI and machine compliant. Are you trying to increase the cache hit rate? Compensate for the inappropriate ping type in the source code?
One thing you can "easily" do is add the gnu alignment extension to the C source.
static unsigned char smallArray[1] __attribute__ ((aligned (4)));
Update: Hmm, a large third party macro library that generates clearly inappropriate code? (If it was standard-compatible code, it would seem to work fine. :-) Ok, this is a terrible kludge, but I can almost guarantee that it "works", FSDO "works" and doesn't require debugging lib macros .. You can post-process the compiler's assembly language output. Local static bss symbols are not layout specific and are usually declared in a single directive, .comm
or .lcomm
for which the last parameter is likely to be the alignment amount.
This parameter changes __attribute__
. You can change them at build time with a script ...
source to share
On systems I've worked on, linker options can be controlled to set the alignment of the beginning of each section, but not the alignment of individual variables in that section. The alignment of individual variables was determined by the compiler according to the type of the variable.
You might be able to use some kind of compiler pragma, of course for the platform.
An alternative is to define your array as an array of whatever type you have to bind to, like uint32_t.
I'm curious to know what you are trying to achieve - it seems like you are doing something unusual and platform specific if you have this alignment requirement. Your best bet is to create platform independent code, if at all possible, of course.
source to share