Mui*_*uis 17 c gcc c99 padding memory-alignment
我知道为什么GCC默认不重新排序结构的成员,但我很少编写依赖于结构顺序的代码,所以有什么方法可以标记我的结构自动重新排序?
phu*_*clv 12
以前的GCC版本可以-fipa-struct-reorg选择允许在-fwhole-program+ -combine模式下进行结构重新排序.
-fipa-struct-reorg
执行结构重组优化,改变C类结构布局以更好地利用空间局部性.此转换对包含结构数组的程序有效.提供两种编译模式:基于配置文件(启用-fprofile-generate)或静态(使用内置启发式).需要-fipa-type-escape提供这种转变的安全性.它仅适用于整个程序模式,因此需要-fwhole-program并-combine启用它.'cold'这种转变所考虑的结构不受影响(参见参考资料--param struct-reorg-cold-struct-ratio=value).
由于发布说明中的以下原因,它自GCC 4.8.x后被删除
结构重组和矩阵重组优化(命令行选项
-fipa-struct-reorg和-fipa-matrix-reorg)已被删除.它们并不总是正常工作,也不适用于链接时优化(LTO),因此仅适用于由单个翻译单元组成的程序.
但是你仍然可以尝试struct-reorg-branch在GCC SVN在你自己的风险,因为它仍然在积极发展.
附带说明一下,Linux 内核实现了一个gcc 插件来引入一个名为 的属性randomize_layout。目标是在结构的定义中使用它,以使编译器随机化字段的顺序。Linux内核使用它是为了安全起见来对抗需要知道结构布局的攻击。例如,该结构在include/linux/cred.hcred中定义如下:
struct cred {
atomic_t usage;
#ifdef CONFIG_DEBUG_CREDENTIALS
atomic_t subscribers; /* number of processes subscribed */
void *put_addr;
[...]
struct user_struct *user; /* real user ID subscription */
struct user_namespace *user_ns; /* user_ns the caps and keyrings are relative to. */
struct group_info *group_info; /* supplementary groups for euid/fsgid */
/* RCU deletion */
union {
int non_rcu; /* Can we skip RCU deletion? */
struct rcu_head rcu; /* RCU deletion hook */
};
} __randomize_layout;
Run Code Online (Sandbox Code Playgroud)
该标签在Linux 源代码树的include/linux/compiler-gcc.h__randomize_layout中定义为:
#define __randomize_layout __attribute__((randomize_layout))
Run Code Online (Sandbox Code Playgroud)