Re: Order of variables in specific sections when enabling optimization in gcc

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Freddie Chopin <freddie_chopin@xxxxx> writes:

> Hello!
>
> Here is an extremely simple test case:
>
> -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 --
>
> int a __attribute__ ((section("section")));
> int b __attribute__ ((section("section")));
> int c __attribute__ ((section("section")));
>
> -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 --
>
> If I compile this file with GCC 8, with no optimizations and dump the
> object file I get the variables in the exact order as in the source
> file:
>
> -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 --
>
> $ gcc -c order.c
> $ nm -n order.o
> 0000000000000000 D a
> 0000000000000004 D b
> 0000000000000008 D c
>
> -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 --
>
> However if I enable the optimization (any level), the variables are
> reversed in memory:
>
> -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 --
>
> $ gcc -O -c order.c
> $ nm -n order.o
> 0000000000000000 D c
> 0000000000000004 D b
> 0000000000000008 D a
>
> -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 -- >8 --
>
> As I need to keep consistent addresses of various variables in specific
> memory section for different versions of the program, I decided to put
> all of these variables in the same source file. This is for an embedded
> project and the section of memory I'm using has backup battery power,
> so its contents are not lost when the device is powered down. However I
> also need the addresses of "old" objects to remain constant and I don't
> want them to change when I add some "new" objects.
>
> So I'm wondering whether this particular behavior of GCC is something
> which I could control with any compiler flag (other than disabling
> optimizations [; ) and whether this is something consistent across
> different versions of GCC? For example can I assume that in GCC 12, in
> year 2022, the order of the variables in the memory will still be
> reverse of the order in source file, or maybe someday the order will be
> different than today with GCC 8?

You could mark variables with no_reorder attribute:

int a __attribute__ ((section("section"),no_reorder));
int b __attribute__ ((section("section"),no_reorder));
int c __attribute__ ((section("section"),no_reorder));
 
>From docs:

 @cindex @code{no_reorder} function attribute
 Do not reorder functions or variables marked @code{no_reorder}
 against each other or top level assembler statements the executable.
 The actual order in the program will depend on the linker command
 line. Static variables marked like this are also not removed.
 This has a similar effect
 as the @option{-fno-toplevel-reorder} option, but only applies to the
 marked symbols.

>
> Thanks in advance!
>
> Regards,
> FCh



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux