This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: Do weak variables waste mem?
- From: Alan Modra <amodra at gmail dot com>
- To: Thomas Schmid <scth2000 at yahoo dot de>
- Cc: "binutils at sourceware dot org" <binutils at sourceware dot org>
- Date: Thu, 26 Feb 2015 10:01:41 +1030
- Subject: Re: Do weak variables waste mem?
- Authentication-results: sourceware.org; auth=none
- References: <20150224223312 dot GC16384 at bubble dot grove dot modra dot org> <550769179 dot 11670999 dot 1424874164004 dot JavaMail dot yahoo at mail dot yahoo dot com>
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