【发布时间】:2013-04-07 21:33:26
【问题描述】:
我正在尝试找到一种使用 GCC 将 32 位或 16 位键上的变量对齐的好方法。
我正在开发不支持未对齐数据访问的 Cortex-M0。我在使用-Os 时遇到了这个问题。我使用 arm-none-eabi 版本 4.6.2。我有这个小测试用例:
#include "stdint.h"
typedef struct Struct_HdrPrefix{
uint8_t Prefix;
uint16_t Type;
}__attribute((packed))Struct_HdrPrefix;
volatile Struct_HdrPrefix test;
volatile Struct_HdrPrefix test1;
int main(void)
{
test.Type = 0;
while(1)
__asm("nop");
}
void HardFault_Handler(void)
{
while(1)
__asm("nop");
}
当我编译这个时,我得到一个非对齐的变量测试。
这是地图文件中的结果:
COMMON 0x20000000 0x6 ./SRC/main.o
0x20000000 test1
0x20000003 test
所以现在我遇到了硬故障。如果我将它添加到变量中:
volatile Struct_HdrPrefix test __attribute((aligned (4)));
volatile Struct_HdrPrefix test1 __attribute((aligned (4)));
由于test 现在已对齐,因此按预期工作。
我不能在结构上使用 align 属性,因为这个结构也可能是另一个包装的一部分。
有没有办法告诉 GCC 或 LD 在 32 位边界上对齐打包变量?
问候
【问题讨论】:
-
如果你告诉编译器打包一个结构,你就是在告诉它忽略对齐问题。你不能同时拥有它。
标签: c gcc linker alignment cortex-m