apcs-gnu ABI中的结构布局

对于此代码 :

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(各种版本),我得到断言失败。 事实certificate结构的大小是4

我可以通过使用__attribute__((packed))解决这个问题; 但我的问题是:

  • 使这个结构大小为4的理由是什么?
  • 是否有任何文档指定此ABI中结构的布局?

在ARM网站上,我找到了aapcs (EABI第5版)的文档,它确实将此结构指定为大小为2; 但我找不到关于apcs-gnu任何信息。

这是GCC特定的决策,用于权衡业绩规模。 它可以用-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= 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