Do weak variables waste mem?
Alan Modra
amodra@gmail.com
Thu Feb 26 05:24:00 GMT 2015
On Wed, Feb 25, 2015 at 02:22:43PM +0000, Thomas Schmid wrote:
> Alan Modra schrieb am 24.02.2015 23:33:
> > You have defined two copies of the variables. Without the
> > __attribute__((__weak__)) you also have two definitions of each
> > variable, but some compilers (including gcc) treat them specially - as
> > common variables. You should instead declare the variables as extern
>
> > in the header, and define them in one of the source files.
>
> When you say "two copies of the variables", you mean that each object uses its own variable?
> When i look at the symbol table, only one variable is used.
No, I didn't say anything about the linkage of the variables. I
merely pointed out that your source defined more than one copy.
> I agree that defining the variables explicitly is the better solution, but this declarations are generated and not easy to change.
>
> In reality these variables are also declared with __attribute__((__section__())) to put these special variables together. They are finally put back to .bss via linkerscript.
If you are able to modify the generator then a number of options are
available.
1) Emit code that does
#ifdef MAIN_PROGRAIN
variable definition
#else
variable declaration (with extern)
#endif
then define MAIN_PROGRAM in just one source file that includes your
headers.
2) Use linkonce or comdat group sections for the variables.
You might also like to investigate whether --gc-sections works for
you. Not all targets support this linker option.. If your target
doesn't then you could consider adding support to the linker.
--
Alan Modra
Australia Development Lab, IBM
More information about the Binutils
mailing list