为什么 GCC 会为 ARM Cortex-A9 产生非法的未对齐访问 [英] Why does GCC produce illegal unalign accesses for ARM Cortex-A9

查看:31
本文介绍了为什么 GCC 会为 ARM Cortex-A9 产生非法的未对齐访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:ARM Cortex-A9
海湾合作委员会版本:4.9.2

Target: ARM Cortex-A9
GCC Version: 4.9.2

大家好,

我在 GCC 生成核心时遇到问题,该问题会由于未对齐的访问而导致数据中止.我隔离了一段显示问题的代码.我不知道如何让 GCC 正确处理这个问题.帮助将不胜感激!

I have a problem with GCC producing core that causes data aborts because of unaligned accesses. I isolated a piece of code that shows the problem. I don't know how to make GCC to handle this correctly. Help would be appreciated!

struct B
{
    char c1;
    char c2;
    char c3;
    char c4;
};

struct A
{
    char c;
    struct B b;
    char c2;
};

int main(int argc, char** argv)
{
    struct A a;
    a.c2 = 42;    // Works fine
    a.b.c1 = 42   // Works fine, too
    struct B b;
    b = a.b;      // Crashes because of unaligned access
    return 0;
}

内存布局如下:

a           struct A    48  S:0x3FFFFFE0    R/W
  c         char        8   S:0x3FFFFFE0    R/W
  b         struct B    32  S:0x3FFFFFE1    R/W
    c1      char        8   S:0x3FFFFFE1    R/W
    c2      char        8   S:0x3FFFFFE2    R/W
    c3      char        8   S:0x3FFFFFE3    R/W
    c4      char        8   S:0x3FFFFFE4    R/W
  c2        char        8   S:0x3FFFFFE5    R/W

现在是前两个赋值命令,如

Now for the first two assignments commands like

a.c2 = 42;     : STRB     r3,[r11,#-7]       with r11 = 0x3FFFFFEC

已生产并且运行良好.

然而,对于最后一个作业,

However, for the last assignment,

b = a.b;       : LDR      r0,[r2,#0]         with r2 = 0x3FFFFFE1

产生的结果是由于未对齐的访问而导致数据中止.GCC 不会发出任何警告或任何相关内容.

is produced what results in a data abort because of unaligned access. GCC issues no warnings or anything about that.

有人知道怎么解决吗?

顺便说一句,所有结构声明中的对齐属性在我的项目中不是一个选项.

Btw., alignment attributes in all struct declarations is not an option in my project.

推荐答案

如果你使用 -mno-unaligned-access 你会得到 正确结果.

If you use -mno-unaligned-access you get the correct result.

令我惊讶的是,默认情况下它没有这样做,因为在 arm 上进行未对齐的内存访问通常是错误的.

It was a surprise to me that it didn't do this by default as it's so oftenly wrong to do unaligned memory access on arm.

这篇关于为什么 GCC 会为 ARM Cortex-A9 产生非法的未对齐访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆