问题描述
对于此代码:
struct S { unsigned char ch[2]; };
int main(void)
{
_Static_assert( sizeof(struct S) == 2, "size was not 2");
}
使用带有ABI apcs-gnu
(又名OABI或EABI版本0)的ARM的GCC(各种版本),我断言失败.事实证明,该结构的大小为4
.
using GCC (various versions) for ARM with the ABI apcs-gnu
(aka. OABI, or EABI version 0), I get the assertion fails. It turns out the size of the struct is 4
.
我可以使用__attribute__((packed))
解决此问题;但我的问题是:
I can work around this by using __attribute__((packed))
; but my questions are:
- 使此结构大小为
4
的理由是什么? - 是否有任何文档指定此ABI中结构的布局?
- What is the rationale for making this struct size
4
? - Is there any documentation specifying the layout of structs in this ABI?
在ARM网站上,我找到了aapcs
(EABI版本5)的文档,该文档确实将此结构指定为2;但是我找不到关于apcs-gnu
的任何信息.
On the ARM website I found documentation for aapcs
(EABI version 5) which does specify this struct as having a size of 2; but I could not find anything about apcs-gnu
.
推荐答案
这是特定于GCC的决定,需要权衡大小以提高性能.可以用-mstructure-size-boundary=8
覆盖.
This is a GCC-specific decision to trade-off size for performance. It can be overridden with -mstructure-size-boundary=8
.
摘录自源代码:
/* Setting STRUCTURE_SIZE_BOUNDARY to 32 produces more efficient code, but the
value set in previous versions of this toolchain was 8, which produces more
compact structures. The command line option -mstructure_size_boundary=<n>
can be used to change this value. For compatibility with the ARM SDK
however the value should be left at 32. ARM SDT Reference Manual (ARM DUI
0020D) page 2-20 says "Structures are aligned on word boundaries".
The AAPCS specifies a value of 8. */
#define STRUCTURE_SIZE_BOUNDARY arm_structure_size_boundary
这篇关于apcs-gnu ABI中的结构布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!