c - apcs-gnu ABI 中的结构布局

标签 c gcc struct arm elf

对于 this code :

struct S {     unsigned char ch[2]; };

int main(void)
{
    _Static_assert( sizeof(struct S) == 2, "size was not 2");
}

将 GCC(各种版本)用于 ARM 和 ABI apcs-gnu(又名 OABI,或 EABI 版本 0),我得到断言失败。结果该结构的大小为 4

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

  • 将此结构大小设置为 4 的基本原理是什么?
  • 是否有任何文档指定此 ABI 中的结构布局?

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

最佳答案

这是 GCC 特定的决定,以权衡大小换取性能。它可以用 -mstructure-size-boundary=8 覆盖。

摘自 source code :

/* 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

关于c - apcs-gnu ABI 中的结构布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43786747/

相关文章:

c - 在 C 中制作没有框架的 GUI

c++ - Reg 在 U-Boot 上执行 C++ 应用程序(无操作系统)

c - 让父进程等待并根据需要多次运行 linux 命令

c - 分配给类型 ‘typedef from type ‘uint8_t’ 时类型不兼容

go - 结构中的空字段会消耗内存吗?

c++ - 尝试传递结构类型的 vector 但没有控制台输出

c - 错误 C2084 : function 'int main()' already has a body

c - 动态阵列上的段错误(核心转储)错误

c - 如何引用变长结构

gcc - 如何在cygwin中将-pg传递给gcc