Order of variables in specific sections when enabling optimization in gcc

Segher Boessenkool segher@kernel.crashing.org
Thu Mar 7 13:00:00 GMT 2019


Hi Freddie,

[ putting the list back on cc: ]

On Thu, Mar 07, 2019 at 01:15:47PM +0100, Freddie Chopin wrote:
> On Thu, 2019-03-07 at 06:08 -0600, Segher Boessenkool wrote:
> > You probably should use a linker script for this.
> 
> You think of anything better than placing each variable in its own
> section and placing them manually in the linker script in the order I
> like? I would like to avoid any messing with linker script if that's

You can refer to separate variables in the linker script, you don't have
to put them each in their own section.

> possible... If there's nothing better I may end up using suffixes
> ("section.1", "section.2", "section.3", ...) and sort the sections in
> the linker script (`*(SORT(.section.*));`), but I would still prefer to
> solve that in the source file only.

You could try putting all those vars in a struct.  In principle the
compiler is free to mess that up, too, though, but this is less fragile,
and easier to work around should it break.

> > The compiler can optimise away (some of) those variables even, or not
> > always keep them in memory, etc., unless you use volatile on the
> > vars.
> 
> No worries, it won't optimize them away, there's also a header where
> these variables are declared with `extern ...` (omitted that in the
> example, as this doesn't change anything) and all of them will be used
> in the project. I'm just concerned about their order.

If you use LTO or some other whole-program optimisation, that won't
save you.  Hiding things from the compiler so it cannot assume anything
about it breaks as soon as the compiler gets more smarts (if it ever
worked).  Telling the compiler that something *is* accessed some way
that it cannot see: that is fool-proof and future-proof.


Segher



More information about the Gcc-help mailing list