http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46563
Summary: link with -lgcc when creating a shared lib Product: gcc Version: 4.5.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: driver AssignedTo: unassig...@gcc.gnu.org ReportedBy: christophe.l...@st.com Created attachment 22457 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=22457 main prog C++ source As discussed in http://gcc.gnu.org/ml/gcc-help/2010-11/msg00198.html, the G++ driver does not link with -lgcc when creating a shared lib. This is a problem when the shared lib references some symbols which are only present in libgcc.a (such as __sync_fetch_and_add and all __sync*). This is the case for instance on ARM. I have attached atomic.cxx (shared lib) and atomain.cxx (main prog) to be compiled with: $ arm-linux-g++ atomic.cxx -fPIC -shared -o libatomic.so $ arm-linux-g++ atomain.cxx -o atomain -L. -latomic .../ld: atomain: hidden symbol `__sync_fetch_and_add_4' in /.../libgcc.a(linux-atomic.o) is referenced by DSO As a fix I can suggest to patch gcc.c:init_gcc_specs() and to add static_name next to shared_name when expanding the specs. diff -up gcc.c gcc.c.patch --- gcc.c 2010-02-25 17:20:51.831150000 +0100 +++ gcc.c.patch 2010-11-19 17:02:37.141674000 +0100 @@ -1722,7 +1722,7 @@ init_gcc_specs (struct obstack *obstack, static_name, " --as-needed ", shared_name, " --no-as-needed" "}" "%{shared-libgcc:", - shared_name, "%{!shared: ", static_name, "}" + shared_name, " ", static_name, "%{!shared: ", static_name, "}" "}" #else "%{!shared:" @@ -1731,11 +1731,11 @@ init_gcc_specs (struct obstack *obstack, "}" #ifdef LINK_EH_SPEC "%{shared:" - "%{shared-libgcc:", shared_name, "}" + "%{shared-libgcc:", shared_name, " ", static_name"}" "%{!shared-libgcc:", static_name, "}" "}" #else - "%{shared:", shared_name, "}" + "%{shared:", shared_name, " ", static_name"}" #endif #endif "}}", NULL);