Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a GCC keyword to allow structure-reordering?

Tags:

I know why GCC doesn't re-order members of a structure by default, but I seldom write code that relies on the order of the structure, so is there some way I can flag my structures to be automaticly reordered?

like image 409
Maestro Avatar asked Feb 03 '13 09:02

Maestro


People also ask

Can C compiler reorder struct members?

A Standard C compiler won't rearrange the members of a structure automatically to reduce the structure's size.

Can compiler reorder struct members?

Since the rules are fixed in the language, the compiler is able to figure out how the members were reordered, and react accordingly. As mentioned above, it will always be possible to prevent reordering in the cases where you want complete control.


2 Answers

Previous GCC versions have the -fipa-struct-reorg option to allow structure reordering in -fwhole-program + -combine mode.

  • -fipa-struct-reorg

    Perform structure reorganization optimization, that change C-like structures layout in order to better utilize spatial locality. This transformation is affective for programs containing arrays of structures. Available in two compilation modes: profile-based (enabled with -fprofile-generate) or static (which uses built-in heuristics). Require -fipa-type-escape to provide the safety of this transformation. It works only in whole program mode, so it requires -fwhole-program and -combine to be enabled. Structures considered 'cold' by this transformation are not affected (see --param struct-reorg-cold-struct-ratio=value).

It was removed since GCC 4.8.x due to the below reasons in the release note

The struct reorg and matrix reorg optimizations (command-line options -fipa-struct-reorg and -fipa-matrix-reorg) have been removed. They did not always work correctly, nor did they work with link-time optimization (LTO), hence were only applicable to programs consisting of a single translation unit.

However you can still try the struct-reorg-branch on GCC SVN or the github mirror out on your own risk as it's still in active development.

You can also reorder the fields with the clang-reorder-fields tool in clang-tools-extra

See also

  • Automated field re-ordering in C structs to avoid padding
like image 156
phuclv Avatar answered Oct 02 '22 19:10

phuclv


As a side note, the Linux kernel implements a gcc plugin to introduce an attribute named randomize_layout. The goal is to use it in the definition of the structures to make the compiler randomize the order of the fields. Linux kernel uses it for the sake of security to counter attacks that need to know the layout of structures. For example the cred structure is defined as follow in include/linux/cred.h:

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;

The __randomize_layout tag is defined in include/linux/compiler-gcc.h of the Linux source tree as:

#define __randomize_layout __attribute__((randomize_layout))
like image 25
Rachid K. Avatar answered Oct 02 '22 20:10

Rachid K.