From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1298) id ED93A3858418; Mon, 13 Dec 2021 18:03:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ED93A3858418 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Olivier Hainque To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-5939] Tigthen libc_internal and crtstuff for VxWorks shared objects X-Act-Checkin: gcc X-Git-Author: Frederic Konrad X-Git-Refname: refs/heads/master X-Git-Oldrev: 0515c95d5fe0a865f688f3ab89572b917e8f0185 X-Git-Newrev: 4099d6501e3526ca8d1d01e904f42b83c6824674 Message-Id: <20211213180358.ED93A3858418@sourceware.org> Date: Mon, 13 Dec 2021 18:03:58 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 13 Dec 2021 18:03:59 -0000 https://gcc.gnu.org/g:4099d6501e3526ca8d1d01e904f42b83c6824674 commit r12-5939-g4099d6501e3526ca8d1d01e904f42b83c6824674 Author: Frederic Konrad Date: Thu Nov 5 11:34:57 2020 +0100 Tigthen libc_internal and crtstuff for VxWorks shared objects This change tightens and documents the use of libc_internal, then strengthens the VxWorks crtstuff objects for the support of shared libraries. In particular: - Define __dso_handle, which libstdc++.so requires, - Provide _init and _fini functions to run through the init/fini arrays for shared libs in configurations which HAVE_INITFINI_ARRAY_SUPPORT. The init/fini functions are provided by libc_internal.a for static links but with slightly different names and we don't want to risk dragging other libc_internal contents in the closure accidentally so make sure we don't link with it. As for the !vxworks crtstuff, the new shared libs specific bits are conditioned by a CRTSTUFFS_O macro, for which we provide new Makefile fragment. The bits to actually use the fragment and the shared objects will be added by a forthcoming change, as part of a more general configury update for shared libs. The change also adds guards the eh table registration code in vxcrtstuff so the objects can be used for either init/fini or eh tables independently. 2021-12-07 Fred Konrad Olivier Hainque gcc/ * config/vxworks.h (VXWORKS_BASE_LIBS_RTP): Guard -lc_internal on !shared+!non-static and document. (VXWORKS_LIB_SPEC): Remove the bits intended to drag the init/fini functions from libc_internal in the shared lib case. (VX_CRTBEGIN_SPEC/VX_CRTEND_SPEC): Use vxcrtstuff objects also in configurations with shared lib and INITFINI_ARRAY support. libgcc/ * config/t-vxcrtstuffS: New Makefile fragment. * config/vxcrtstuff.c: Provide __dso_handle. Provide _init/_fini functions for INITFINI_ARRAY support in shared libs and guard the definition of eh table registration functions on conditions indicating they are needed. Diff: --- gcc/config/vxworks.h | 42 ++++++++++++++++--------- libgcc/config/t-vxcrtstuffS | 9 ++++++ libgcc/config/vxcrtstuff.c | 77 ++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 109 insertions(+), 19 deletions(-) diff --git a/gcc/config/vxworks.h b/gcc/config/vxworks.h index 96076e3c272..52d6aa1ae96 100644 --- a/gcc/config/vxworks.h +++ b/gcc/config/vxworks.h @@ -101,11 +101,23 @@ along with GCC; see the file COPYING3. If not see the default CPP spec for C++ as well. */ #undef CPLUSPLUS_CPP_SPEC -/* For VxWorks static rtps, the system provides libc_internal.a, a superset of - libgcc.a that we need to use e.g. to satisfy references to __init and - __fini. We still want our libgcc to prevail for symbols it would provide - (e.g. register save entry points), so re-place it here between libraries - that might reference it and libc_internal. +/* For VxWorks static rtps, the system provides libc_internal.a for a variety + of purposes. Care is needed to include it appropriately. + + - In some configurations, libc_internal fills in possible references from + the static libc that we don't wouldn't satisfy ourselves, say, with + libgcc. An example is the __aeabi_memcpy family of functions on arm, + which have very specific ABI allowances. + + - OTOH, in some configurations the library provides typical libgcc + services, for example register save/restore entry points on powerpc. We + want our libgcc to prevail for symbols it would provide, so place + -lc_internal after -lc -lgcc. + + - libc_internal also contains __init/__fini functions for + USE_INITFINI_ARRAY support. However, the system expects these in + every shared lib as well, with slightly different names, and it is + simpler for us to provide our own versions through vxcrtstuff. In addition, some versions of VxWorks rely on explicit extra libraries for system calls and the set of base network libraries of common use varies @@ -120,7 +132,8 @@ along with GCC; see the file COPYING3. If not see #define VXWORKS_NET_LIBS_RTP "-lnet -ldsi" #endif -#define VXWORKS_BASE_LIBS_RTP "-lc -lgcc -lc_internal" +#define VXWORKS_BASE_LIBS_RTP \ + "-lc -lgcc %{!shared:%{!non-static:-lc_internal}}" #define VXWORKS_EXTRA_LIBS_RTP @@ -161,10 +174,9 @@ along with GCC; see the file COPYING3. If not see #endif #undef VXWORKS_LIB_SPEC -#define VXWORKS_LIB_SPEC \ -"%{mrtp:%{shared:-u " USER_LABEL_PREFIX "__init -u " USER_LABEL_PREFIX "__fini} \ - %{!shared:%{non-static:-u " USER_LABEL_PREFIX "_STI__6__rtld -ldl} \ - " TLS_SYM " \ +#define VXWORKS_LIB_SPEC \ +"%{mrtp:%{!shared:%{non-static:-u " USER_LABEL_PREFIX "_STI__6__rtld -ldl} \ + " TLS_SYM " \ --start-group " VXWORKS_LIBS_RTP " --end-group}}" /* The no-op spec for "-shared" below is present because otherwise GCC @@ -196,11 +208,13 @@ along with GCC; see the file COPYING3. If not see #define VXWORKS_LIBGCC_SPEC "-lgcc" #endif -/* Setup the crtstuff begin/end we might need for dwarf EH registration. */ +/* Setup the crtstuff begin/end we might need for dwarf EH registration + and/or INITFINI_ARRAY support for shared libs. */ -#if !defined(CONFIG_SJLJ_EXCEPTIONS) && DWARF2_UNWIND_INFO -#define VX_CRTBEGIN_SPEC "vx_crtbegin.o%s" -#define VX_CRTEND_SPEC "-l:vx_crtend.o" +#if (HAVE_INITFINI_ARRAY_SUPPORT && defined(ENABLE_SHARED_LIBGCC)) \ + || (DWARF2_UNWIND_INFO && !defined(CONFIG_SJLJ_EXCEPTIONS)) +#define VX_CRTBEGIN_SPEC "%{!shared:vx_crtbegin.o%s;:vx_crtbeginS.o%s}" +#define VX_CRTEND_SPEC "%{!shared:vx_crtend.o%s;:vx_crtendS.o%s}" #else #define VX_CRTBEGIN_SPEC "" #define VX_CRTEND_SPEC "" diff --git a/libgcc/config/t-vxcrtstuffS b/libgcc/config/t-vxcrtstuffS new file mode 100644 index 00000000000..6b65b0005be --- /dev/null +++ b/libgcc/config/t-vxcrtstuffS @@ -0,0 +1,9 @@ +# Shared versions of vx_crt{begin,end}.o, those one must be compiled only +# when the shared libraries are available + +vx_crtbeginS.o: $(srcdir)/config/vxcrtstuff.c + $(crt_compile) $(CRTSTUFF_T_CFLAGS_S) -DCRT_BEGIN -c $< -DCRTSTUFFS_O +vx_crtendS.o: $(srcdir)/config/vxcrtstuff.c + $(crt_compile) $(CRTSTUFF_T_CFLAGS_S) -DCRT_END -c $< -DCRTSTUFFS_O + +EXTRA_PARTS += vx_crtbeginS.o vx_crtendS.o diff --git a/libgcc/config/vxcrtstuff.c b/libgcc/config/vxcrtstuff.c index c15e15e54e9..80f51f7399d 100644 --- a/libgcc/config/vxcrtstuff.c +++ b/libgcc/config/vxcrtstuff.c @@ -50,15 +50,25 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #define USE_CDTORS_SECTIONS #endif +#if DWARF2_UNWIND_INFO && !defined(__USING_SJLJ_EXCEPTIONS__) +#define USE_EH_FRAME_REGISTRY +#endif + /* ------------------------------ crtbegin ------------------------------- */ #ifdef CRT_BEGIN -/* Stick a label at the beginning of the frame unwind info so we can register - and deregister it with the exception handling library code. */ -static const char __EH_FRAME_BEGIN__[] -__attribute__((section(__LIBGCC_EH_FRAME_SECTION_NAME__), aligned(4))) - = { }; +#if DEFAULT_USE_CXA_ATEXIT && defined(__RTP__) +/* This mimics the crtstuff.c behavior. dso_handle should be NULL for the + main program (in vx_crtbegin.o) and a unique value for the shared libraries + (in vx_crtbeginS.o). */ +extern void *__dso_handle __attribute__ ((__visibility__ ("hidden"))); +#ifdef CRTSTUFFS_O +void *__dso_handle = &__dso_handle; +#else +void *__dso_handle = 0; +#endif +#endif /* DEFAULT_USE_CXA_ATEXIT */ /* Determine what names to use for the constructor/destructor functions. */ @@ -89,6 +99,53 @@ __attribute__((section(__LIBGCC_EH_FRAME_SECTION_NAME__), aligned(4))) #define EH_CTOR_ATTRIBUTE __attribute__((constructor (101))) #define EH_DTOR_ATTRIBUTE __attribute__((destructor (101))) +/* Provide the init/fini array support functions for shared libraries, + where we don't want to drag libc_internal contents blindly and which + provides functions with a slightly different name anyway. */ + +#if HAVE_INITFINI_ARRAY_SUPPORT && defined(CRTSTUFFS_O) + +/* Run through the .init_array, .fini_array sections. The linker script + *must* provide __init_array_start, __init_array_end, __fini_array_start, + __fini_array_end symbols. */ + +typedef void (*initfini_ptr) (void); +extern initfini_ptr __init_array_start[]; +extern initfini_ptr __init_array_end[]; +extern initfini_ptr __fini_array_start[]; +extern initfini_ptr __fini_array_end[]; + +/* Provide the actual code through static functions, which don't need + to be exposed in the shared lib interface. */ + +static void __exec_init_array(void) +{ + initfini_ptr *fn; + for (fn = __init_array_start; fn < __init_array_end; ++fn) + (*fn)(); +} + +static void __exec_fini_array(void) +{ + initfini_ptr *fn; + for (fn = __fini_array_end - 1; fn >= __fini_array_start; --fn) + (*fn)(); +} + +/* Reference the two above functions as the init / fini function. */ + +void __attribute__ ((__section__ (".init"))) _init() +{ + __exec_init_array(); +} + +void __attribute__ ((__section__ (".fini"))) _fini() +{ + __exec_fini_array(); +} + +#endif /* __CRTSTUFFS_O__ */ + #else /* !USE_INITFINI_ARRAY */ /* Note: Even in case of .ctors/.dtors sections, we can't use the attribute @@ -100,6 +157,13 @@ __attribute__((section(__LIBGCC_EH_FRAME_SECTION_NAME__), aligned(4))) #endif /* USE_INITFINI_ARRAY */ +#ifdef USE_EH_FRAME_REGISTRY +/* Stick a label at the beginning of the frame unwind info so we can register + and deregister it with the exception handling library code. */ +static const char __EH_FRAME_BEGIN__[] +__attribute__((section(__LIBGCC_EH_FRAME_SECTION_NAME__), aligned(4))) + = { }; + EH_LINKAGE EH_CTOR_ATTRIBUTE void EH_CTOR_NAME (void) { static struct object object; @@ -110,6 +174,7 @@ EH_LINKAGE EH_DTOR_ATTRIBUTE void EH_DTOR_NAME (void) { __deregister_frame_info (__EH_FRAME_BEGIN__); } +#endif /* USE_EH_FRAME_REGISTRY */ #ifdef USE_CDTORS_SECTIONS /* As explained above, we need to manually build the sections here as the @@ -126,6 +191,7 @@ static void (* volatile eh_registration_dtors[])() #elif defined (CRT_END) /* ! CRT_BEGIN */ +#ifdef USE_EH_FRAME_REGISTRY /* Terminate the frame unwind info section with a 4byte 0 as a sentinel; this would be the 'length' field in a real FDE. */ @@ -133,6 +199,7 @@ static const char __FRAME_END__[] __attribute__ ((used, section(__LIBGCC_EH_FRAME_SECTION_NAME__), aligned(4))) = { 0, 0, 0, 0 }; +#endif /* USE_EH_FRAME_REGISTRY */ #else /* ! CRT_BEGIN & ! CRT_END */