From ec4d182d4ad675e79ae635d3f612ed6ab98bbfcd Mon Sep 17 00:00:00 2001 From: ad Date: Tue, 13 Nov 2007 15:21:19 +0000 Subject: [PATCH] - stack_protector.c doesn't really belong under sys/. Add a new directory for misc support routines and put it there. - Add a libc constructor. Use this to initialize threading and the stack protector stuff. libpthread cannot be initialized safely using its own constructor because libc and libpthread are deeply intertwined. PR bin/37347 --- lib/libc/Makefile | 3 +- lib/libc/misc/Makefile.inc | 10 +++++ lib/libc/misc/initfini.c | 53 ++++++++++++++++++++++++ lib/libc/{sys => misc}/stack_protector.c | 9 ++-- lib/libc/sys/Makefile.inc | 5 +-- lib/libc/thread-stub/thread-stub.c | 18 +++++++- 6 files changed, 86 insertions(+), 12 deletions(-) create mode 100644 lib/libc/misc/Makefile.inc create mode 100644 lib/libc/misc/initfini.c rename lib/libc/{sys => misc}/stack_protector.c (92%) diff --git a/lib/libc/Makefile b/lib/libc/Makefile index d56108610475..663cfcd7caaa 100644 --- a/lib/libc/Makefile +++ b/lib/libc/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.131 2007/05/30 01:13:15 tls Exp $ +# $NetBSD: Makefile,v 1.132 2007/11/13 15:21:19 ad Exp $ # @(#)Makefile 8.2 (Berkeley) 2/3/94 # # All library objects contain sccsid strings by default; they may be @@ -65,6 +65,7 @@ COMPATDIR=${.CURDIR}/compat .include "${.CURDIR}/isc/Makefile.inc" .include "${.CURDIR}/locale/Makefile.inc" .include "${.CURDIR}/md/Makefile.inc" +.include "${.CURDIR}/misc/Makefile.inc" .include "${.CURDIR}/net/Makefile.inc" .include "${.CURDIR}/nameser/Makefile.inc" .include "${.CURDIR}/nls/Makefile.inc" diff --git a/lib/libc/misc/Makefile.inc b/lib/libc/misc/Makefile.inc new file mode 100644 index 000000000000..d5cb9ef8739c --- /dev/null +++ b/lib/libc/misc/Makefile.inc @@ -0,0 +1,10 @@ +# $NetBSD: Makefile.inc,v 1.1 2007/11/13 15:21:20 ad Exp $ +# @(#)Makefile.inc 8.3 (Berkeley) 10/24/94 + +.PATH: ${.CURDIR}/misc + +# constructor +SRCS+= initfini.c + +# for -fstack-protector +SRCS+= stack_protector.c diff --git a/lib/libc/misc/initfini.c b/lib/libc/misc/initfini.c new file mode 100644 index 000000000000..fa7767a5aed5 --- /dev/null +++ b/lib/libc/misc/initfini.c @@ -0,0 +1,53 @@ +/* $NetBSD: initfini.c,v 1.1 2007/11/13 15:21:20 ad Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Andrew Doran. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +static void __libc_init(void) __attribute__((__constructor__, __used__)); + +void __guard_setup(void); +void __libc_thr_init(void); + +static void +__libc_init(void) +{ + + /* For -fstack-protector */ + __guard_setup(); + + /* Threads */ + __libc_thr_init(); +} diff --git a/lib/libc/sys/stack_protector.c b/lib/libc/misc/stack_protector.c similarity index 92% rename from lib/libc/sys/stack_protector.c rename to lib/libc/misc/stack_protector.c index c1dca69ddbce..a404bfae4631 100644 --- a/lib/libc/sys/stack_protector.c +++ b/lib/libc/misc/stack_protector.c @@ -1,4 +1,4 @@ -/* $NetBSD: stack_protector.c,v 1.5 2007/09/27 17:53:05 christos Exp $ */ +/* $NetBSD: stack_protector.c,v 1.1 2007/11/13 15:21:20 ad Exp $ */ /* $OpenBSD: stack_protector.c,v 1.10 2006/03/31 05:34:44 deraadt Exp $ */ /* @@ -28,7 +28,7 @@ * */ #include -__RCSID("$NetBSD: stack_protector.c,v 1.5 2007/09/27 17:53:05 christos Exp $"); +__RCSID("$NetBSD: stack_protector.c,v 1.1 2007/11/13 15:21:20 ad Exp $"); #ifdef _LIBC #include "namespace.h" @@ -49,14 +49,13 @@ extern int xprintf(const char *fmt, ...); #endif long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0}; -static void __guard_setup(void) __attribute__((__constructor__, __used__)); static void __fail(const char *); void __stack_chk_fail(void); void __chk_fail(void); void __stack_chk_fail_local(void); +void __guard_setup(void); -/*LINTED used*/ -static void +void __guard_setup(void) { int mib[2]; diff --git a/lib/libc/sys/Makefile.inc b/lib/libc/sys/Makefile.inc index 2111d729481b..52d809e48b3a 100644 --- a/lib/libc/sys/Makefile.inc +++ b/lib/libc/sys/Makefile.inc @@ -1,4 +1,4 @@ -# $NetBSD: Makefile.inc,v 1.179 2007/11/12 23:11:58 ad Exp $ +# $NetBSD: Makefile.inc,v 1.180 2007/11/13 15:21:19 ad Exp $ # @(#)Makefile.inc 8.3 (Berkeley) 10/24/94 # sys sources @@ -22,9 +22,6 @@ DPSRCS+= ${_LSRC:MLintSys*.c} CLEANFILES+= ${_LSRC:MLintSys*.c} .endif -# for -fstack-protector -SRCS+= stack_protector.c - # glue to offer userland wrappers for some syscalls SRCS+= sigtimedwait.c sigwait.c sigwaitinfo.c statvfs.c diff --git a/lib/libc/thread-stub/thread-stub.c b/lib/libc/thread-stub/thread-stub.c index b395afb983e9..587d6790eda4 100644 --- a/lib/libc/thread-stub/thread-stub.c +++ b/lib/libc/thread-stub/thread-stub.c @@ -1,4 +1,4 @@ -/* $NetBSD: thread-stub.c,v 1.14 2005/11/29 03:12:00 christos Exp $ */ +/* $NetBSD: thread-stub.c,v 1.15 2007/11/13 15:21:20 ad Exp $ */ /*- * Copyright (c) 2003 The NetBSD Foundation, Inc. @@ -38,7 +38,7 @@ #include #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: thread-stub.c,v 1.14 2005/11/29 03:12:00 christos Exp $"); +__RCSID("$NetBSD: thread-stub.c,v 1.15 2007/11/13 15:21:20 ad Exp $"); #endif /* LIBC_SCCS and not lint */ /* @@ -74,6 +74,20 @@ do { \ #define CHECK_NOT_THREADED() /* nothing */ #endif +/* libpthread init */ + +void __libc_thr_init(void); +void __libc_thr_init_stub(void); + +__weak_alias(__libc_thr_init,__libc_thr_init_stub) + +void +__libc_thr_init_stub(void) +{ + + /* nothing, may be overridden by libpthread */ +} + /* mutexes */ int __libc_mutex_init_stub(mutex_t *, const mutexattr_t *);