ARM平台的对齐问题(有关_packed)

本文探讨了ARM架构下访问非对齐内存地址时出现的不可预料结果,并通过实例对比了ARM与IA-32的不同表现。介绍了ARM编译器为解决此问题所做的努力。

前言

ARM流行已久,做嵌入式开发的不知道ARM不大可能。鉴于其所具备的较低功耗下的较高性能,也就成了大多数嵌入式设备的首选了。

不过对于刚上手的人来说,有可能会遇到一些稀奇古怪的问题。毕竟大部分人都习惯了IA-32下的程序设计,虽然两者都是32位的处理器,但是体系架构完全不同,于是也导致了一些隐含的问题。这里想描述一下一个有点蛊惑的问题,即在ARM上访问非对齐地址内容,会出现所谓“不可预料”结果的问题。

ARM内存访问的对齐问题

按照ARM文档上的描述,其访问规则如下:

1. 一次访问4字节内容,该内容的起始地址必须是4字节对齐的位置上;

2. 一次访问2字节内容,该内容的起始地址必须是2字节对齐的位置上;

(单字节的没有这个问题,就不用考虑啦。 )

好,既然规则如此,那应该遵守。不过么,不安分的人往往喜欢破坏规则,喜欢看看不遵守规则会有什么结果;另外么,即便遵规蹈距的人,有时也难免考虑不周,犯个错也是正常现象。好,那么让我们来看看犯错的结果吧。例如下面的代码:

char        buff[8] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xab, 0xbc, 0xcd};

int          v32, *p32;

short       v16, *p16;

p32 = (int*)&( buff[1] );       //unalignment

p16 = (short*)&( buff[1] );      //unalignment

v32 = *p32;      //what’s the result?

v16 = *p16;      //what’s the result?

如果上面这段代码在IA-32上运行,那么结果应该如下:

v32 = 0x9a785634

v16 = 0x5634

即便非对齐地址上访问,IA-32也就是牺牲一点性能,但是结果保证是正确的。恩,这也是我们所期望的……

可是…… 换到ARM上呢?我们来看看在ADS1.2编译后,执行的结果如下:

v32 = 0x12785634

v16 = 0x1234

这个结果有点奇怪了吧。照理说指向0x34,那么如果是Big-Endian的话,v32应该是0x3456789a,如果是Little-Endian的话,就是前面IA-32的结果。可现在的结果呢?两者都不是,莫名地把更低地址的0x12给凑进来了…… 而如果看看编译生成的汇编code的话,这两个赋值很简单,分别用了ldr和ldrsh指令,指令没有问题,分别用于读取32位和16位数据,都是最基本的指令。嗯,嗯,这就是我们所要描述的访问非对齐地址的问题了。

问题的缘由(个人猜测,非官方资料……)

个人感觉呢,这是ARM体系架构实现的问题,或者说这本来就是By Design的。这样做简化了处理器的实现,IA-32实现的时候肯定会对读取地址是否对齐进行判断,然后转换为相应的操作 ,而ARM呢?没有做这个事情,默认认为大家都按照规矩办事,你要是胆敢破坏,俺就给你好看~~~

那有没有办法解决呢?

这个问题其实ARM自己也知道,所以呢,它在编译器里面,已经添加了部分支持。不过有人会问,那上面那个情况呢?为什么结果还是不对呢?好像没有添加什么支持嘛……

嗯,其实ARM是做了一定的努力的,只是这个情况它没办法解决…… 它做的事情就是:在编译器能够的得知的情况下,尽量保证访问内容的正确。这句话有点笼统,那么把具体情况一个个来看看吧。

编译器的努力(1)—— 所有局部/全局/静态等变量都放在4字节对齐的地址上

其实这个努力很常见,由于在32位平台上,一次访问4字节是效率最高的,所以大多数32平台的编译器都如此处理,ARM的ADS也不例外。

编译器的努力(2)—— 填充、填充、再填充

这个事情么,其实也是常见的。各类编译器上,对于某些结构定义中会产生不对齐的情况,自动填充,以提高访问效率(例如IA-32上访问非对齐的,会加1个周期的)。而ARM的编译器也一样操作,不过感觉这里不单单是为了提高效率,也能够顺带解决这个不对齐的问题。

编译器的努力(3)—— 产生特殊代码

嗯,这个就是关键了,也是ARM编译器的与众不同之处。先来看一段代码:

__packed typedef struct _test
{

        char a;

        short c;
        int d;
} test;

char        buff[8] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xab, 0xbc, 0xcd};

test        *p = (test *)buff;

v32 =      p->d;       //这里的v32借用上面的定义;

貌似多了个限定为__packed的struct,以此来造成不对齐的状况,看不出多大区别嘛。可是运行一下的话,就会发现这里的结果是正确的。我们来看看ADS生成的汇编代码吧。

        v32 = q->d;
[0xe2890003]       add          r0,r9,#3
[0xeb000088]       bl           __rt_uread4
[0xe1a05000]       mov          r5,r0

看到这里的那条"bl           __rt_uread4"的指令了吧。对ARM指令有一定了解的都知道bl其实就是一个函数调用。所以,这里的代码其实是调用了ADS自己提供的__rt_uread4函数,该函数完成的操作就是读取四个字节。ADS提供了类似的一系列函数,针对signed/unsigned,以及4字节/2字节的读取/写入操作。

估计看到这里,大家会问,如果没有__packed限定符呢?猜对了,没有__packed限定符,那么编译器会对上面的情况pending,所以这个struct里面的d所在的位置是4字节对齐的(编译期信息,而非实际运行期信息)。所以就回到类似最初的例子了。

那么,还有一种情况,就是在有__packed的情况下,而struct里的字段都是符合对齐要求的,那么生成的代码会是怎么样的呢?从实际生成的代码来看,和上面的这段汇编代码,唯一的区别就是第一条指令把#3改成了#4,而后面仍旧调用__rt_uread4函数。嗯,这样结论就出来了:

编译器会在使用__packed的情况下,自动对其中的4字节/2字节访问添加特殊代码,以保证其结果的正确。

好了,这个关于这个问题描述得差不多了,可能的话,尽量倚赖编译器的这些功能,而对于编译器无能为力的部分,就要靠万分小心了……

p.s. 其实这里有很多事情可以来尽量预防此类问题,比如嵌入式项目往往喜欢自己管理内存分配,那么自己写的内存分配函数就保证返回的地址都是4字节对齐位置上的……

Build started: Project: SE71M *** Using Compiler 'V6.4', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin' Build target 'SE71M' warning: unknown warning option '-Wno-nonportable-include-path'; did you mean '-Wno-gnu-include-next'? [-Wunknown-warning-option] ../main/ex2_profile_hid_mouse.c(15): warning: In file included from... ../../../components/exble2/includes\ex2_api.h(18): warning: In file included from... ../../../components/exble2/includes/core_ble.h(18): warning: In file included from... ../../../components/exble2/includes/ble/ble_ll.h(85): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_header_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(85): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_header_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(98): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_data_header_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(98): error: expected ';' after top level declarator __PACKED_STRUCT _ble_data_header_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(117): error: use of '__PACKED' with tag type that does not match previous declaration struct __PACKED { ^~~~~~ union ../../../components/exble2/includes/ble/ble_ll.h(116): note: previous use is here typedef union __PACKED { ^ ../../../components/exble2/includes/ble/ble_ll.h(117): error: nested redefinition of '__PACKED' struct __PACKED { ^ ../../../components/exble2/includes/ble/ble_ll.h(116): note: previous definition is here typedef union __PACKED { ^ ../../../components/exble2/includes/ble/ble_ll.h(139): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(139): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(146): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_direct_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(146): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_direct_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(153): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_nonconn_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(153): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_nonconn_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(160): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_scan_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(160): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_scan_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(167): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_scan_req_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(167): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_scan_req_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(174): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_scan_rsp_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(174): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_scan_rsp_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(181): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_connect_ind_t { ^ fatal error: too many errors emitted, stopping now [-ferror-limit=] 1 warning and 20 errors generated. compiling ex2_profile_hid_mouse.c... warning: unknown warning option '-Wno-nonportable-include-path'; did you mean '-Wno-gnu-include-next'? [-Wunknown-warning-option] ../main/ex2_app_mouse.c(21): warning: In file included from... ../../../components/exble2/includes\ex2_api.h(18): warning: In file included from... ../../../components/exble2/includes/core_ble.h(18): warning: In file included from... ../../../components/exble2/includes/ble/ble_ll.h(85): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_header_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(85): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_header_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(98): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_data_header_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(98): error: expected ';' after top level declarator __PACKED_STRUCT _ble_data_header_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(117): error: use of '__PACKED' with tag type that does not match previous declaration struct __PACKED { ^~~~~~ union ../../../components/exble2/includes/ble/ble_ll.h(116): note: previous use is here typedef union __PACKED { ^ ../../../components/exble2/includes/ble/ble_ll.h(117): error: nested redefinition of '__PACKED' struct __PACKED { ^ ../../../components/exble2/includes/ble/ble_ll.h(116): note: previous definition is here typedef union __PACKED { ^ ../../../components/exble2/includes/ble/ble_ll.h(139): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(139): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(146): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_direct_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(146): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_direct_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(153): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_nonconn_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(153): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_nonconn_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(160): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_scan_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(160): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_scan_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(167): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_scan_req_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(167): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_scan_req_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(174): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_scan_rsp_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(174): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_scan_rsp_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(181): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_connect_ind_t { ^ fatal error: too many errors emitted, stopping now [-ferror-limit=] 1 warning and 20 errors generated. compiling ex2_app_mouse.c... ../../../hal/device/om6239/GCC/startup_om6239.S(27): error: attribute name not recognised: Tag_ABI_align_preserved .eabi_attribute Tag_ABI_align_preserved, 1 ^ assembling startup_om6239.S... ".\out\SE71M.axf" - 39 Error(s), 2 Warning(s). Target not created. Build Time Elapsed: 00:00:00
最新发布
08-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值