在GCC下使用Cortex-M0进行可变对准

Jon*_*han 3 c linker gcc alignment cortex-m

我试图找到一种好方法来将变量与32位或16位的GCC对齐.

我正在研究一种不支持未对齐数据访问的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");
}
Run Code Online (Sandbox Code Playgroud)

当我编译它时,我得到一个非对齐的变量测试.

这是地图文件中的结果:

COMMON         0x20000000        0x6 ./SRC/main.o
               0x20000000                test1
               0x20000003                test
Run Code Online (Sandbox Code Playgroud)

所以现在我陷入了困境.如果我将此添加到vars:

volatile Struct_HdrPrefix test __attribute((aligned (4)));
volatile Struct_HdrPrefix test1 __attribute((aligned (4)));
Run Code Online (Sandbox Code Playgroud)

这是按预期工作的,因为test现在已经调整好了.

我不能在结构上使用align属性,因为这个结构也可能是另一个结构的一部分.

有没有办法告诉GCC或LD在32位边界上对齐打包变量?

问候

Val*_*ouk 5

如果你不介意:

typedef struct Struct_HdrPrefix{
    uint8_t Prefix;
    uint8_t dummy; // for alignment
    uint16_t Type;
}__attribute((packed))Struct_HdrPrefix;
Run Code Online (Sandbox Code Playgroud)

如果你想要打包结构和对齐字段,没有别的办法.您必须手动对齐数据.您唯一的选择是插入dummy(以及有多少).