Declare the constructor/destructor linker sets as extern rather than

common.  Add one do-nothing element to each set.  This ensures that
the linker realizes that they are linker sets rather than simple
commons, and makes it possible to link c++rt0.o into every shared
library regardless of whether it is a C++ library or not.  Without
this change, the constructors and destructors in the main program
could be executed multiple times.

This change is going to make it possible to get rid of the
CPLUSPLUSLIB makefile variable once and for all.  It is a piece of
the solution to PR gnu/3505 (gcc -shared).  Finally, it fixes a
heretofore unreported bug:  If CPLUSPLUSLIB was set in a makefile
for a C++ shared library that had no static constructors or
destructors in it, then the main program's constructors and
destructors would be executed multiple times.
This commit is contained in:
John Polstra 1997-04-09 19:14:31 +00:00
parent 405eecfbb1
commit f3112eb32a
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=24756

View file

@ -27,7 +27,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
* $Id: c++rt0.c,v 1.7 1997/02/22 14:57:42 peter Exp $
*/
/*
@ -38,8 +38,8 @@
* number of pointers in each.
* The tables are also null-terminated.
*/
void (*__CTOR_LIST__[2])(void);
void (*__DTOR_LIST__[2])(void);
extern void (*__CTOR_LIST__[])(void);
extern void (*__DTOR_LIST__[])(void);
static void
__dtors(void)
@ -84,3 +84,24 @@ __fini(void)
{
__dtors();
}
/*
* Make sure there is at least one constructor and one destructor in the
* shared library. Otherwise, the linker does not realize that the
* constructor and destructor lists are linker sets. It treats them as
* commons and resolves them to the lists from the main program. That
* causes multiple invocations of the main program's static constructors
* and destructors, which is very bad.
*/
static void
do_nothing(void)
{
}
/* Linker magic to add an element to a constructor or destructor list. */
#define TEXT_SET(set, sym) \
asm(".stabs \"_" #set "\", 23, 0, 0, _" #sym)
TEXT_SET(__CTOR_LIST__, do_nothing);
TEXT_SET(__DTOR_LIST__, do_nothing);