Global variable in static library - double free or corruption error

classic Classic list List threaded Threaded
24 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: Global variable in static library - double free or corruption error

Alexey Skidanov-3




----- Original Message ----

> From: Andrew Haley <[hidden email]>
> To: [hidden email]
> Cc: Alexey Skidanov <[hidden email]>; Alexey Skidanov
><[hidden email]>; [hidden email]
> Sent: Fri, October 22, 2010 3:02:13 PM
> Subject: Re: Global variable in static library - double free or corruption
>error
>
> On 10/22/2010 01:22 PM, Jeffrey Walton wrote:
> > On Fri, Oct 22, 2010 at 4:32 AM, Andrew Haley <[hidden email]> wrote:
> >> Please stop top-posting.  It makes it very hard to reply to you.
> >>
> >> On 10/21/2010 09:01 PM, Alexey Skidanov wrote:
> >>> Actually, you claim that if some static library defines some global
>variable,
> >>> then it should NOT be linked with more then one shared library.
> >>
> >> In the same process, yes.  That's absolutely true.
> >>
> >>> That is, if you
> >>> have the dependencies tree like in my example then you have a problem.
>Think
> >>> about such static library is third party library where I can't change the
>code.
> >>>
> >>> Regarding the ODR. You are absolutly right. But what about a MULTIPLE
>variable
> >>> initialization. Is it correct behaviour according to standard?
> >>
> >> I don't know what you mean by a "MULTIPLE variable initialization".
> > I believe he means the constructor running multiple times (the
> > antithesis of the destructor running multiple times).
>
> I see, thanks.
>
> Well, unless there's a bug you can't get that unless you break the
> ODR.

Do you mean, once I break the ODR the further behaviour is unexpected?

You are right. I break it. And I would like to get linker/loader error/warning
as I get linking two object files with the same global variable defined in each
file ( "Symbol zzz already defined" or something like that).


>
> Andrew.
>



Reply | Threaded
Open this post in threaded view
|

Re: Global variable in static library - double free or corruption error

Marc Glisse-6
On Fri, 22 Oct 2010, Alexey Skidanov wrote:

> You are right. I break it. And I would like to get linker/loader error/warning
> as I get linking two object files with the same global variable defined in each
> file ( "Symbol zzz already defined" or something like that).

GNU ld has --warn-common, but it doesn't seem to look at shared objects,
only plain objects and archives. And you may have to grep through false
positives.

--
Marc Glisse
Reply | Threaded
Open this post in threaded view
|

Re: Global variable in static library - double free or corruption error

Andrew Haley
In reply to this post by Alexey Skidanov-3
On 10/22/2010 04:46 PM, Alexey Skidanov wrote:
>
> Do you mean, once I break the ODR the further behaviour is unexpected?

Certainly not.  Once you break the ODR you have undefined behaviour, so
there is no behaviour that is really unexpected.

> You are right. I break it. And I would like to get linker/loader error/warning
> as I get linking two object files with the same global variable defined in each
> file ( "Symbol zzz already defined" or something like that).

Mmm, but you'd get false positives for places it's done deliberately.
Some thread libraries, for example, contain thread-safe versions of
system library functions.

Andrew.
Reply | Threaded
Open this post in threaded view
|

Re: Global variable in static library - double free or corruption error

Ian Lance Taylor-3
In reply to this post by Alexey Skidanov-3
Alexey Skidanov <[hidden email]> writes:

> Do you mean, once I break the ODR the further behaviour is unexpected?
>
> You are right. I break it. And I would like to get linker/loader error/warning
> as I get linking two object files with the same global variable defined in each
> file ( "Symbol zzz already defined" or something like that).

You will already get an error for a multiple definition.

What is happening here is that you have two different instances of a
variable or function with vague linkage which originated in two
different sources.  Because g++ uses the Borland linking model by
default, in which symbols with vague linkage are duplicated in each .o
file which uses them, the linker can not easily determine that in this
case the symbols are an ODR violation rather than an ordinary
duplication.

The gold linker does implement a --detect-odr-violations option.
Whenever it sees a symbol with vague linkage appearing more than once,
such that the two instances have different sizes or symbol types, it
checks the debug info for the symbols.  If they originate from different
files, it issues a warning.  This approach works fairly well if the
object files are compiled with debugging information and without
optimization.  When optimization is used, there are false positives due
to confusion due to inlined functions.

Ian
12