public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Question about Darwin GCC and weak symbol tagging policy
@ 2011-01-12 19:46 nchaumont
  2011-01-12 20:15 ` Jonathan Wakely
  2011-01-12 22:57 ` Ian Lance Taylor
  0 siblings, 2 replies; 7+ messages in thread
From: nchaumont @ 2011-01-12 19:46 UTC (permalink / raw)
  To: gcc-help


Hello,

I am trying to use dynamic_cast to upcast object pointers generated by
object factories. Each factory belong to a separate dynamic shared object
(OSX bundles in this case), and GCC fails the cast in few cases (and
succeeds in the others). 

In order to understand the problem, I've created a factory that creates an
empty object (only a CTOR and a  virtual DTOR). When the destructor is
inline, everything works fine (the CTOR's definition is in a .cpp). However,
as soon as I move the destructor's definition to the .cpp, the dynamic_cast
fails.

I looked at the symbols in each case (nm -m <bundle-file>) and found that
the only differences were that the symbols associated with the empty class
are declared weak extern in the binary where the destructor is inline and
extern in the other binary.

Here is my understanding of the problem: dynamic_cast can only succeeds in
GCC if both object's typeinfo have the same address. If there are several
definitions of the same class in different DSOs, then some of the duplicated
symbols associated to those definitions have to be marked as weak in order
to be coalesced to enforce the one definition rule (ODR).

This is exactly what doesn't happen when my empty class has its constructor
defined in the .cpp. I've stumbled on a 
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15428#c0 bug report  relating to
GCC on Darwin which adjusted GCC's behavior regarding vtables to compensate
a restriction from the linker on Darwin.

Because of this change, each duplicated symbol related to an empty object is
marked as non-weak and will have its own typeinfo object (and its own
typeinfo pointer), which will cause dynamic_cast to fail on GCC.

In order to revert this behavior, I need to have a 'key function' in my
class.

So here are my two questions:

What qualifies as a key function (I've tried several dummy function
prototypes, but none solved my problem)?

Where can I find some guidelines about the way I should code my classes so
that gcc marks symbols as weak on Darwin?


I've looked for answers for both questions for several days with no success.
Your help will be greatly appreciated.
Thank you very much in advance,

Nicolas
-- 
View this message in context: http://old.nabble.com/Question-about-Darwin-GCC-and-weak-symbol-tagging-policy-tp30654360p30654360.html
Sent from the gcc - Help mailing list archive at Nabble.com.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Question about Darwin GCC and weak symbol tagging policy
  2011-01-12 19:46 Question about Darwin GCC and weak symbol tagging policy nchaumont
@ 2011-01-12 20:15 ` Jonathan Wakely
  2011-01-12 20:57   ` Jonathan Wakely
  2011-01-12 22:57 ` Ian Lance Taylor
  1 sibling, 1 reply; 7+ messages in thread
From: Jonathan Wakely @ 2011-01-12 20:15 UTC (permalink / raw)
  To: nchaumont; +Cc: gcc-help

On 12 January 2011 19:46, nchaumont wrote:
>
> What qualifies as a key function (I've tried several dummy function
> prototypes, but none solved my problem)?

The first non-pure virtual function declared in the class.

The vtables and typeinfo should be emitted in the object file that
contains the definition of the key function.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Question about Darwin GCC and weak symbol tagging policy
  2011-01-12 20:15 ` Jonathan Wakely
@ 2011-01-12 20:57   ` Jonathan Wakely
  0 siblings, 0 replies; 7+ messages in thread
From: Jonathan Wakely @ 2011-01-12 20:57 UTC (permalink / raw)
  To: nchaumont; +Cc: gcc-help

On 12 January 2011 20:15, Jonathan Wakely wrote:
>
> The first non-pure virtual function declared in the class.

... that is not inline at the point of class definition

(forgot that bit)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Question about Darwin GCC and weak symbol tagging policy
  2011-01-12 19:46 Question about Darwin GCC and weak symbol tagging policy nchaumont
  2011-01-12 20:15 ` Jonathan Wakely
@ 2011-01-12 22:57 ` Ian Lance Taylor
  2011-01-13 14:57   ` nchaumont
  1 sibling, 1 reply; 7+ messages in thread
From: Ian Lance Taylor @ 2011-01-12 22:57 UTC (permalink / raw)
  To: nchaumont; +Cc: gcc-help

nchaumont <nicolas.chaumont@gmail.com> writes:

> Here is my understanding of the problem: dynamic_cast can only succeeds in
> GCC if both object's typeinfo have the same address.

Which version of gcc are you using?  That was changed in gcc 4.3 to
avoid the kinds of problems you are encountering.

Ian

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Question about Darwin GCC and weak symbol tagging policy
  2011-01-12 22:57 ` Ian Lance Taylor
@ 2011-01-13 14:57   ` nchaumont
  0 siblings, 0 replies; 7+ messages in thread
From: nchaumont @ 2011-01-13 14:57 UTC (permalink / raw)
  To: gcc-help


Hello Ian,

I'm using GCC 4.2, which is the one shipped with all the versions of OSX
from 2008 onwards. I wanted to make my application work with this version of
GCC for two reasons:

1) It's the default version shipped with OSX, and if the en user can get my
program to work out of the box, that would be the best. Installing another
version of GCC on OSX takes approximately a gigabyte, unless it is possible
to narrow the installation down to those components that are absolutely
necessary, which most probably requires some advanced knowledge.

2) Apple is not going to switch to later versions of GCC, since the later
ones are under the GPLv3 license, which conflicts with Apple's licensing
policies. That's probably one of the reasons why Apple decided to ditch GCC
altogether and use LLVM for the upcoming Xcode 4.

I'll give a try with a newer version of GCC and try LLVM as well.

Thanks for the information!

Cheers,
Nicolas


Ian Lance Taylor-3 wrote:
> 
> nchaumont <nicolas.chaumont@gmail.com> writes:
> 
>> Here is my understanding of the problem: dynamic_cast can only succeeds
>> in
>> GCC if both object's typeinfo have the same address.
> 
> Which version of gcc are you using?  That was changed in gcc 4.3 to
> avoid the kinds of problems you are encountering.
> 
> Ian
> 
> 

-- 
View this message in context: http://old.nabble.com/Question-about-Darwin-GCC-and-weak-symbol-tagging-policy----second-attempt-tp30654360p30662867.html
Sent from the gcc - Help mailing list archive at Nabble.com.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Question about Darwin GCC and weak symbol tagging policy
@ 2011-01-12 19:49 nchaumont
  0 siblings, 0 replies; 7+ messages in thread
From: nchaumont @ 2011-01-12 19:49 UTC (permalink / raw)
  To: gcc-help


Hello,

I am trying to use dynamic_cast to upcast object pointers generated by
object factories. Each factory belong to a separate dynamic shared object
(OSX bundles in this case), and GCC fails the cast in few cases (and
succeeds in the others). 

In order to understand the problem, I've created a factory that creates an
empty object (only a CTOR and a  virtual DTOR). When the destructor is
inline, everything works fine (the CTOR's definition is in a .cpp). However,
as soon as I move the destructor's definition to the .cpp, the dynamic_cast
fails.

I looked at the symbols in each case (nm -m <bundle-file>) and found that
the only differences were that the symbols associated with the empty class
are declared weak extern in the binary where the destructor is inline and
extern in the other binary.

Here is my understanding of the problem: dynamic_cast can only succeeds in
GCC if both object's typeinfo have the same address. If there are several
definitions of the same class in different DSOs, then some of the duplicated
symbols associated to those definitions have to be marked as weak in order
to be coalesced to enforce the one definition rule (ODR).

This is exactly what doesn't happen when my empty class has its constructor
defined in the .cpp. I've stumbled on a 
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15428#c0 bug report  relating to
GCC on Darwin which adjusted GCC's behavior regarding vtables to compensate
a restriction from the linker on Darwin.

Because of this change, each duplicated symbol related to an empty object is
marked as non-weak and will have its own typeinfo object (and its own
typeinfo pointer), which will cause dynamic_cast to fail on GCC.

In order to revert this behavior, I need to have a 'key function' in my
class.

So here are my two questions:

What qualifies as a key function (I've tried several dummy function
prototypes, but none solved my problem)?

Where can I find some guidelines about the way I should code my classes so
that gcc marks symbols as weak on Darwin?


I've looked for answers for both questions for several days with no success.
Your help will be greatly appreciated.
Thank you very much in advance,

Nicolas
-- 
View this message in context: http://old.nabble.com/Question-about-Darwin-GCC-and-weak-symbol-tagging-policy-tp30654362p30654362.html
Sent from the gcc - Help mailing list archive at Nabble.com.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Question about Darwin GCC and weak symbol tagging policy
@ 2011-01-12 19:49 nchaumont
  0 siblings, 0 replies; 7+ messages in thread
From: nchaumont @ 2011-01-12 19:49 UTC (permalink / raw)
  To: gcc-help


Hello,

I am trying to use dynamic_cast to upcast object pointers generated by
object factories. Each factory belong to a separate dynamic shared object
(OSX bundles in this case), and GCC fails the cast in few cases (and
succeeds in the others). 

In order to understand the problem, I've created a factory that creates an
empty object (only a CTOR and a  virtual DTOR). When the destructor is
inline, everything works fine (the CTOR's definition is in a .cpp). However,
as soon as I move the destructor's definition to the .cpp, the dynamic_cast
fails.

I looked at the symbols in each case (nm -m <bundle-file>) and found that
the only differences were that the symbols associated with the empty class
are declared weak extern in the binary where the destructor is inline and
extern in the other binary.

Here is my understanding of the problem: dynamic_cast can only succeeds in
GCC if both object's typeinfo have the same address. If there are several
definitions of the same class in different DSOs, then some of the duplicated
symbols associated to those definitions have to be marked as weak in order
to be coalesced to enforce the one definition rule (ODR).

This is exactly what doesn't happen when my empty class has its constructor
defined in the .cpp. I've stumbled on a 
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15428#c0 bug report  relating to
GCC on Darwin which adjusted GCC's behavior regarding vtables to compensate
a restriction from the linker on Darwin.

Because of this change, each duplicated symbol related to an empty object is
marked as non-weak and will have its own typeinfo object (and its own
typeinfo pointer), which will cause dynamic_cast to fail on GCC.

In order to revert this behavior, I need to have a 'key function' in my
class.

So here are my two questions:

What qualifies as a key function (I've tried several dummy function
prototypes, but none solved my problem)?

Where can I find some guidelines about the way I should code my classes so
that gcc marks symbols as weak on Darwin?


I've looked for answers for both questions for several days with no success.
Your help will be greatly appreciated.
Thank you very much in advance,

Nicolas
-- 
View this message in context: http://old.nabble.com/Question-about-Darwin-GCC-and-weak-symbol-tagging-policy-tp30654361p30654361.html
Sent from the gcc - Help mailing list archive at Nabble.com.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-01-13 14:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-12 19:46 Question about Darwin GCC and weak symbol tagging policy nchaumont
2011-01-12 20:15 ` Jonathan Wakely
2011-01-12 20:57   ` Jonathan Wakely
2011-01-12 22:57 ` Ian Lance Taylor
2011-01-13 14:57   ` nchaumont
2011-01-12 19:49 nchaumont
2011-01-12 19:49 nchaumont

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).