Bug 23598

Summary: Merge libgcc_r with libgcc
Product: Base System Reporter: yakisoba <yakisoba>
Component: gnuAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.2-STABLE   
Hardware: Any   
OS: Any   

Description yakisoba 2000-12-17 17:00:01 UTC
Building "shared" libraries with pthread, we always need "-pthread" flag.

To avoid the flag "-pthread" ...

 1. Create stubs and link it. This stubs for the library and "libgcc_r".
   I thought "libgcc_r-stubs.c" needs for all "shared" libraries with pthread.

   Stub, stub, stub ... I hate this way.

 2. Add weak directive for the library, but cannot for "libgcc_r" without
   hack FreeBSD source tree.

 I found this problem, building libstdc++-v3 on MY make tree.
On merging GCC 3.0 I think this problem will happen.

Fix: 

Checking ...

 1. 2.2.0-RELEASE or later has weak directive.

 2. "gthr-posix.h" is ready to make weak symbols.
    If "ASM_WEAKEN_LABEL" is defined in Target Macro file, 
   "SUPPORTS_WEAK 1" is defined in "default.h".
   Then "GTHREAD_USE_WEAK 1" is defined in "gthr.h".
   So "gthr-posix.h" make weak symbols.

 So we can merge "libgcc_r" with "libgcc". I think we should do that
like "gcc/Makefile" generated by configure script.

 1. Add the next line into "config/freebsd.h".

      #ifdef IN_LIBGCC2
      #ifndef __GTHREAD_MUTEX_INIT_FUNCTION
      #define __GTHREAD_MUTEX_INIT_FUNCTION(m) \
	do {                           \
	  pthread_mutex_init (m, NULL) \
        } while (0)
      #endif
      #endif /* IN_LIBGCC2 */

 2. Improve "gnu/usr.bin/cc/cc_tools/freebsd-native.h"

      #define LIBGCC_SPEC "libgcc%{shared:_pic}.a%s"

 3. Create "gthr-default.h" in "gnu/usr.bin/cc/cc_tools" directory.

      gthr-default.h:
      #include "gthr-posix.h"

    This header file is generated by configure script.
  
 4. Add the next line into "gnu/usr.bin/lib/libgcc/Makefile".

      CFLAGS += -DHAVE_GTHR_DEFAULT
      CFLAGS += -DIN_LIBGCC2

    This is same as "gcc/Makefile" generated by configure script.
   
 5. Remove "libgcc_r" from "Makefile.inc1" and "gnu/lib/Makefile".

1 for "Native" and "Ports"
2-5 for "Native" only

P.S.
  No need of my PR/23002 because of this PR. Please remove it.
  See my PR/19327 for a.out and I hope to commit it together.
Comment 1 yakisoba 2000-12-19 06:39:46 UTC
>  Sorry, missing "pthread_mutex_init" weak symbol.
> If we need "__GTHREAD_MUTEX_INIT_FUNCTION", hacking "gcc/gthr-posix.h".
> So new Fix 1+ instead of previous Fix 1, and new Fix 1.1.
> 
>  1+. Add the next line into "config/freebsd.h".
> 
>       #ifdef IN_LIBGCC2
>       #ifndef __GTHREAD_MUTEX_INIT_FUNCTION
>       #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
>       #endif
>       #endif /* IN_LIBGCC2 */
> 
>  1.1. Improve "contrib/gcc/gthr-posix.h".
> 
>       *** gcc/gthr-posix.h.orig
>       --- gcc/gthr-posix.h
>       ***************
>       *** 56,61 ****
>       --- 56,65 ----
>         #pragma weak pthread_mutex_trylock 
>         #pragma weak pthread_mutex_unlock 
>         
>       + #ifdef	__GTHREAD_MUTEX_INIT_FUNCTION
>       + #pragma weak pthread_mutex_init
>       + #endif
>       + 
>         static void *__gthread_active_ptr = &pthread_create;
>         
>         static inline int
>       ***************
>       *** 142,147 ****
>       --- 146,157 ----
>             return pthread_mutex_unlock (mutex);
>           else
>             return 0;
>       + }
>       + 
>       + static inline void
>       + __gthread_mutex_init_function (__gthread_mutex_t *mutex)
>       + {
>       +   pthread_mutex_init (mutex, NULL);
>         }
>         
>         #endif /* not __gthr_posix_h */
Comment 2 yakisoba 2000-12-21 17:50:33 UTC
 So sorry, building in the GCC original make tree is fine,
but "make buildworld" in the FreeBSD make tree is not.
 I have to improve "gnu/lib/libgcc/Makefile". But I rewrite it
because of some reasons.

  * Use some header files in the "gnu/usr.bin/cc/cc_tools".
  * "make depend" work fine.
  * XCC -> CC for libgcc2, CC -> OLDCC for libgcc1 if need it.
  * Use <bsd.lib.mk> rules as we can.
  * -p, CXX, XCXX is confused.

--- BEGIN gnu/lib/libgcc/Makefile ---
# $FreeBSD$

GCCDIR=	${.CURDIR}/../../../contrib/gcc
XCCDIR=	../../usr.bin/cc

.PATH: ${GCCDIR}/cp ${GCCDIR}

LIB=	gcc

# Install libgcc_pic.a, since ld.so uses it.
INSTALL_PIC_ARCHIVE=  yes

#
# XXX This is a hack, but it seems to work.  libgcc1.a is supposed to be
# compiled by the native compiler, and libgcc2.a is meant to be compiled
# by *this* version of gcc.
#
# Normally, this does not make any difference, since we only have gcc, but
# when bootstrapping from gcc-2.6.3, we have to use the freshly built 2.7.2
# compiler for some of the libgcc2.c __attribute__ stuff.
#
OLDCC?=	${CC}
CCLIBFLAGS?= -O

# Members of libgcc1.a.
LIB1FUNCS= _mulsi3 _udivsi3 _divsi3 _umodsi3 _modsi3 \
	   _lshrsi3 _ashrsi3 _ashlsi3 \
	   _divdf3 _muldf3 _negdf2 _adddf3 _subdf3 \
	   _fixdfsi _fixsfsi _floatsidf _floatsisf _truncdfsf2 _extendsfdf2 \
	   _addsf3 _negsf2 _subsf3 _mulsf3 _divsf3 \
	   _eqdf2 _nedf2 _gtdf2 _gedf2 _ltdf2 _ledf2 \
	   _eqsf2 _nesf2 _gtsf2 _gesf2 _ltsf2 _lesf2

# Library members defined in libgcc2.c.
LIB2FUNCS= _muldi3 _divdi3 _moddi3 _udivdi3 _umoddi3 _negdi2 \
	   _lshrdi3 _ashldi3 _ashrdi3 _ffsdi2 \
	   _udiv_w_sdiv _udivmoddi4 _cmpdi2 _ucmpdi2 _floatdidf _floatdisf \
	   _fixunsdfsi _fixunssfsi _fixunsdfdi _fixdfdi _fixunssfdi _fixsfdi \
	   _fixxfdi _fixunsxfdi _floatdixf _fixunsxfsi \
	   _fixtfdi _fixunstfdi _floatditf \
	   __gcc_bcmp _varargs __dummy _eprintf \
	   _bb _shtab _clear_cache _trampoline __main _exit _ctors \
	   _eh _pure

# Library members defined in new1.cc.
NEW1FUNCS= _op_new _op_newnt 

# Library members defined in new2.cc.
NEW2FUNCS= _op_vnew _op_vnewnt _op_delete _op_delnt _op_vdel _op_vdelnt 

SRCS=	${LIB1FUNCS:S/$/.c/}
SRCS+=	${LIB2FUNCS:S/$/.c/}
SRCS+=	frame.c
.if !defined(NO_CXX)
SRCS+=	${NEW1FUNCS:S/$/.cc/}
SRCS+=	${NEW2FUNCS:S/$/.cc/}
SRCS+=	tinfo.cc tinfo2.cc new.cc exception.cc
.endif

INCLUDES=
.if exists(${.OBJDIR}/${XCCDIR}/cc_tools)
INCLUDES+=	-I${.OBJDIR}/${XCCDIR}/cc_tools
.endif
INCLUDES+=	-I${.CURDIR}/${XCCDIR}/cc_tools
INCLUDES+=	-I${GCCDIR}/config -I${GCCDIR}

CFLAGS+=	-fexceptions
CFLAGS+=	-DIN_GCC
CFLAGS+=	-DIN_LIBGCC2
CFLAGS+=	-DHAVE_GTHR_DEFAULT
.if !defined(NO_CXX)
CFLAGS+=	-nostdinc++
CFLAGS+=	-I${GCCDIR}/cp/inc
.endif
CFLAGS+=	${INCLUDES}

${LIB1FUNCS:S/$/.c/}: ${GCCDIR}/libgcc1.c
	@rm -f ${.TARGET}
	@echo "#define L${.PREFIX}" > ${.TARGET}
	@cat ${GCCDIR}/libgcc1.c >> ${.TARGET}

CLEANFILES +=	${LIB1FUNCS:S/$/.c/}

${LIB1FUNCS:S/$/.o/}:
	${OLDCC} ${CCLIBFLAGS} ${INCLUDES} -c ${.IMPSRC} -o ${.TARGET}
	@${LD} -o ${.TARGET}.tmp -x -r ${.TARGET}
	@mv ${.TARGET}.tmp ${.TARGET}

${LIB1FUNCS:S/$/.po/}:
	${OLDCC} -pg ${CCLIBFLAGS} ${INCLUDES} -c ${.IMPSRC} -o ${.TARGET}
	@${LD} -o ${.TARGET}.tmp -X -r ${.TARGET}
	@mv ${.TARGET}.tmp ${.TARGET}

${LIB1FUNCS:S/$/.So/}:
	${OLDCC} ${PICFLAG} -DPIC ${CCLIBFLAGS} ${INCLUDES} -c ${.IMPSRC} -o ${.TARGET}
	@${LD} -o ${.TARGET}.tmp -x -r ${.TARGET}
	@mv ${.TARGET}.tmp ${.TARGET}

${LIB2FUNCS:S/$/.c/}: ${GCCDIR}/libgcc2.c
	@rm -f ${.TARGET}
	@echo "#define L${.PREFIX}" > ${.TARGET}
	@cat ${GCCDIR}/libgcc2.c >> ${.TARGET}

CLEANFILES +=	${LIB2FUNCS:S/$/.c/}

${NEW1FUNCS:S/$/.cc/}: ${GCCDIR}/cp/new1.cc
	@rm -f ${.TARGET}
	@echo "#define L${.PREFIX}" > ${.TARGET}
	@cat ${GCCDIR}/cp/new1.cc >> ${.TARGET}

CLEANFILES +=	${NEW1FUNCS:S/$/.cc/}

${NEW2FUNCS:S/$/.cc/}: ${GCCDIR}/cp/new2.cc
	@rm -f ${.TARGET}
	@echo "#define L${.PREFIX}" > ${.TARGET}
	@cat ${GCCDIR}/cp/new2.cc >> ${.TARGET}

CLEANFILES +=	${NEW2FUNCS:S/$/.cc/}

.include <bsd.lib.mk>
--- END gnu/lib/libgcc/Makefile ---
Comment 3 Maxim Sobolev freebsd_committer freebsd_triage 2001-05-29 16:12:39 UTC
Responsible Changed
From-To: freebsd-bugs->obrien

Over to gcc maintainer.
Comment 4 David E. O'Brien freebsd_committer freebsd_triage 2001-09-10 17:32:58 UTC
Responsible Changed
From-To: obrien->freebsd-bugs
Comment 5 Gerald Pfeifer freebsd_committer freebsd_triage 2003-04-01 13:35:36 UTC
State Changed
From-To: open->closed

According to Alexander Kabaev <kan@freebsd.org>, who is one of the folks 
looking after our imported GCC these days, this is not applicable/relevant 
any longer in that we use weak references in libgcc_r for quite some time 
already.