From 34c712de5d2f23a3c444dd4ca0ca965a93b23bea Mon Sep 17 00:00:00 2001 From: "Sergey Semyonov (Serge)" Date: Tue, 28 Aug 2007 10:36:38 +0000 Subject: [PATCH] update CLib startup code git-svn-id: svn://kolibrios.org@615 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../open watcom/trunk/clib/crt/atexit.c | 77 ++++++++ .../open watcom/trunk/clib/crt/cmdname.c | 71 +++++++ .../open watcom/trunk/clib/crt/extfunc.h | 106 +++++++++++ .../open watcom/trunk/clib/crt/ljmphdl.c | 43 +++++ .../open watcom/trunk/clib/crt/ljmphdl.h | 50 +++++ .../open watcom/trunk/clib/crt/seterrno.c | 76 ++++++++ .../open watcom/trunk/clib/crt/seterrno.h | 52 +++++ .../open watcom/trunk/clib/crt/stjmp386.asm | 177 ++++++++++++++++++ 8 files changed, 652 insertions(+) create mode 100644 programs/develop/open watcom/trunk/clib/crt/atexit.c create mode 100644 programs/develop/open watcom/trunk/clib/crt/cmdname.c create mode 100644 programs/develop/open watcom/trunk/clib/crt/extfunc.h create mode 100644 programs/develop/open watcom/trunk/clib/crt/ljmphdl.c create mode 100644 programs/develop/open watcom/trunk/clib/crt/ljmphdl.h create mode 100644 programs/develop/open watcom/trunk/clib/crt/seterrno.c create mode 100644 programs/develop/open watcom/trunk/clib/crt/seterrno.h create mode 100644 programs/develop/open watcom/trunk/clib/crt/stjmp386.asm diff --git a/programs/develop/open watcom/trunk/clib/crt/atexit.c b/programs/develop/open watcom/trunk/clib/crt/atexit.c new file mode 100644 index 0000000000..9c7ededf38 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/crt/atexit.c @@ -0,0 +1,77 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: Module for adding functions to the atexit() list, as well +* invoking the functions on library shutdown. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include "rtdata.h" +#include "extfunc.h" +#include "rtinit.h" + +#define EXIT_LIMIT 32 + +static void (* _HUGEDATA _ExitList[EXIT_LIMIT])( void ); +static int _ExitCount; + +int atexit( void (*func)( void ) ) +{ + if( _ExitCount < EXIT_LIMIT ) { + _ExitList[ _ExitCount++ ] = func; + return( 0 ); /* indicate added successfully */ + } + return( -1 ); /* indicate no room */ +} + +typedef void exit_fn( void ); +#if defined(_M_IX86) + #pragma aux (__outside_CLIB) exit_fn; +#endif + +static void _Full_at_exit_rtn( void ) +{ + int count; + exit_fn *func; + + count = _ExitCount; + if( count == ( EXIT_LIMIT + 1 ) ) { + return; /* already done once */ + } + _ExitCount = EXIT_LIMIT + 1; /* prevent others being registered */ + /* call functions in reverse order of their registration */ + while( count != 0 ) { + --count; + func = _ExitList[ count ]; + (*func)(); /* invoke user exit routine */ + } +} + +AYI( _Full_at_exit_rtn, INIT_PRIORITY_PROGRAM + 32 ); diff --git a/programs/develop/open watcom/trunk/clib/crt/cmdname.c b/programs/develop/open watcom/trunk/clib/crt/cmdname.c new file mode 100644 index 0000000000..47bcdc6cea --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/crt/cmdname.c @@ -0,0 +1,71 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: Implementation of _cmdname(). +* +****************************************************************************/ + + +#ifdef __WATCOMC__ + #include "variety.h" +#else + #define _WCRTLINK +#endif +#include +#include +#include +#include + +extern char **_argv; /* argument vector */ + +/* NOTE: This file isn't used for QNX. It's got its own version. */ + +#ifdef __LINUX__ + +_WCRTLINK char *_cmdname( char *name ) +{ + int save_errno = errno; + int result = readlink( "/proc/self/exe", name, PATH_MAX ); + + errno = save_errno; + + /* fall back to argv[0] if readlink doesn't work */ + if( result == -1 || result == PATH_MAX ) + return( strcpy( name, _argv[0] ) ); + + /* readlink does not add a NUL so we need to do it ourselves */ + name[result] = '\0'; + return( name ); +} + +#else + +_WCRTLINK char *_cmdname( char *name ) +{ + return( strcpy( name, _argv[0] ) ); +} + +#endif diff --git a/programs/develop/open watcom/trunk/clib/crt/extfunc.h b/programs/develop/open watcom/trunk/clib/crt/extfunc.h new file mode 100644 index 0000000000..a7e11fd93e --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/crt/extfunc.h @@ -0,0 +1,106 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: Special interface for calls to non-Watcom routines from +* inside clib (x86 specific). +* +****************************************************************************/ + + +#ifndef EXTFUNC_H_INCLUDED +#define EXTFUNC_H_INCLUDED + +#include "variety.h" + +#if defined(_M_IX86) + #if !defined(__WINDOWS__) + #if defined(__BIG_DATA__) + #define __DS ds + #endif + #endif + + #if !defined(__FLAT__) + #define __ES es + #endif + + #if defined(__386__) + #if defined(__WINDOWS__) || !defined(__FLAT__) + #define __FS fs + #endif + + #define __GS gs + #if defined(__SW_3S) + #define __AX eax + #define __BX ebx + #define __CX ecx + #define __DX edx + #endif + #endif + + #ifndef __AX + #define __AX + #endif + #ifndef __BX + #define __BX + #endif + #ifndef __CX + #define __CX + #endif + #ifndef __DX + #define __DX + #endif + #ifndef __DS + #define __DS + #endif + #ifndef __ES + #define __ES + #endif + #ifndef __FS + #define __FS + #endif + #ifndef __GS + #define __GS + #endif + + #pragma aux __outside_CLIB modify [__AX __BX __CX __DX __DS __ES __FS __GS]; + /* + use as follows: + + typedef void vfv( void ); + #pragma aux (__outside_CLIB) __vfv; + */ + + #undef __AX + #undef __BX + #undef __CX + #undef __DX + #undef __DS + #undef __ES + #undef __FS + #undef __GS +#endif + +#endif diff --git a/programs/develop/open watcom/trunk/clib/crt/ljmphdl.c b/programs/develop/open watcom/trunk/clib/crt/ljmphdl.c new file mode 100644 index 0000000000..9462593fd7 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/crt/ljmphdl.c @@ -0,0 +1,43 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: Default longjmp handler. +* +****************************************************************************/ + + +#include "variety.h" +#include "ljmphdl.h" + +static void default_handler( void _WCFAR *p ) +{ + p = p; +} +#if defined(_M_IX86) + #pragma aux (__arg_convention) default_handler; +#endif + +_WCRTLINKD pfun __longjmp_handler = &default_handler; diff --git a/programs/develop/open watcom/trunk/clib/crt/ljmphdl.h b/programs/develop/open watcom/trunk/clib/crt/ljmphdl.h new file mode 100644 index 0000000000..0a2baf1ecb --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/crt/ljmphdl.h @@ -0,0 +1,50 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE +* DESCRIBE IT HERE! +* +****************************************************************************/ + + +#ifndef _LJMPHDL_H_INCLUDED +#define _LJMPHDL_H_INCLUDED + +#include "variety.h" + +typedef void (*pfun)( void _WCFAR * ); + +#if defined(__386__) + #pragma aux __arg_convention parm caller [eax dx]; + #pragma aux (__arg_convention) pfun; +#elif defined(M_I86) + #pragma aux __arg_convention parm caller [ax dx]; + #pragma aux (__arg_convention) pfun; +#endif + +_WCRTLINKD extern pfun __longjmp_handler; + +#endif diff --git a/programs/develop/open watcom/trunk/clib/crt/seterrno.c b/programs/develop/open watcom/trunk/clib/crt/seterrno.c new file mode 100644 index 0000000000..ee15bb34eb --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/crt/seterrno.c @@ -0,0 +1,76 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: Implementation of __set_errno(). +* +****************************************************************************/ + + +#ifdef __WATCOMC__ + +#include "variety.h" +#include +#include +#include "rtdata.h" +#include "seterrno.h" + +_WCRTLINK void __set_errno( unsigned int err ) +{ + _RWD_errno = err; +} + +_WCRTLINK void __set_EDOM() +{ + __set_errno( EDOM ); +} + +_WCRTLINK void __set_ERANGE() +{ + __set_errno( ERANGE ); +} + +_WCRTLINK int __set_EINVAL() +{ + __set_errno( EINVAL ); + return( -1 ); +} + +#if !defined(__UNIX__) && !defined(__NETWARE__) +_WCRTLINK void __set_doserrno( unsigned int err ) +{ + _RWD_doserrno = err; +} +#endif + +#else + +#include +void __set_errno( unsigned int err ) +{ + errno = err; +} + +#endif diff --git a/programs/develop/open watcom/trunk/clib/crt/seterrno.h b/programs/develop/open watcom/trunk/clib/crt/seterrno.h new file mode 100644 index 0000000000..e313206667 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/crt/seterrno.h @@ -0,0 +1,52 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: errno related CLIB internal declarations. +* +****************************************************************************/ + +#ifndef _SETERNO_H_INCLUDED +#define _SETERNO_H_INCLUDED + +#include "variety.h" + +// defined in _dos\c\dosret.c +_WCRTLINK extern int __set_errno_dos( unsigned int ); +#if defined(__NT__) + _WCRTLINK extern int __set_errno_nt( void ); +#endif + +// defined in startup\c\seterrno.c +_WCRTLINK extern void __set_errno( unsigned int ); +_WCRTLINK extern int __set_EINVAL( void ); +_WCRTLINK extern void __set_EDOM( void ); +_WCRTLINK extern void __set_ERANGE( void ); +#if !defined(__UNIX__) + _WCRTLINK extern void __set_doserrno( unsigned int ); +#endif + +#endif + diff --git a/programs/develop/open watcom/trunk/clib/crt/stjmp386.asm b/programs/develop/open watcom/trunk/clib/crt/stjmp386.asm new file mode 100644 index 0000000000..c30e11dd99 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/crt/stjmp386.asm @@ -0,0 +1,177 @@ +;***************************************************************************** +;* +;* Open Watcom Project +;* +;* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +;* +;* ======================================================================== +;* +;* This file contains Original Code and/or Modifications of Original +;* Code as defined in and that are subject to the Sybase Open Watcom +;* Public License version 1.0 (the 'License'). You may not use this file +;* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +;* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +;* provided with the Original Code and Modifications, and is also +;* available at www.sybase.com/developer/opensource. +;* +;* The Original Code and all software distributed under the License are +;* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +;* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +;* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +;* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +;* NON-INFRINGEMENT. Please see the License for the specific language +;* governing rights and limitations under the License. +;* +;* ======================================================================== +;* +;* Description: C library setjmp/longjmp support for 386 processors +;* +;***************************************************************************** + +.386p +include mdef.inc +include struct.inc + + codeptr "C",__longjmp_handler + + modstart setjmp + +ifdef __386__ +ifdef __OS2__ + xref DosUnwindException +endif +endif +ifdef __NT__ + xref _RtlUnwind@16 +endif + + xdefp "C",_setjmp + defpe _setjmp + mov [eax],ebx ; save registers + mov 4[eax],ecx + mov 8[eax],edx + mov 12[eax],esi + mov 16[eax],edi + mov 20[eax],ebp + pop 24[eax] ; get return address + mov 28[eax],esp + push 24[eax] ; push return address back on stack + mov 32[eax],es ; save segment registers + mov 34[eax],ds + mov 36[eax],cs + mov 38[eax],fs + mov 40[eax],gs + mov 42[eax],ss +ifdef __NT__ + push fs:[0] ; touched this line to cause recompile + pop 44[eax] +endif +ifdef __386__ +ifdef __OS2__ + push fs:[0] ; get exception chain + pop 44[eax] ; ... +endif +endif + sub eax,eax ; return 0 + ret ; return +_setjmp endp + +; +; There is a pragma for longjmp, saying that it aborts, therefore +; the code generator does a jmp to here as opposed to a call. + + xdefp "C",longjmp + defpe longjmp +ifdef __STACK__ + pop eax ; get address of jmp_buf + pop edx ; get return code +endif + ; Warning, warning! + ; the profiler knows about the stack frame that longjmp generates. + ; do not change these 3 instructions without also changing the findLongJmpStack + ; pragma in profilog.c + ; + push edx + push eax ; save jmp_buf & retval in safe place + mov ebp,esp + ; + ; end warning + ; +ifdef __NT__ + push eax ; save address of jmp_buf + mov eax,[eax+44] + cmp eax,fs:[0] + jne dounwind + jmp short done_unwind +dounwind: + push 0 + push offset done_unwind + push eax ; unwind up to but not including SEH active + ; at setjmp() + call _RtlUnwind@16 ; trashes most registers except for ebp +done_unwind: + mov esp,ebp + mov eax,0[ebp] + mov edx,4[ebp] +endif +ifdef __386__ +ifdef __OS2__ + push eax ; save address of jmp_buf + push 0 +;; push offset unwind + mov eax, offset unwind + push eax + mov eax,8[esp] + push 44[eax] + call DosUnwindException +unwind: add esp,12 + pop eax ; restore address of jmp_buf +endif +endif + push eax ; save parm regs + push edx + mov dx,42[eax] ; setup old ss:esp as a parm + mov eax,28[eax] + call __longjmp_handler ; call handler + pop edx ; restore parm regs + pop eax + + mov ss,42[eax] ; load old ss:esp + mov esp,28[eax] ; ... + push 24[eax] ; push saved eip (our return address) + or edx,edx ; if return code is 0 + _if e ; then + inc edx ; - set it to 1 + _endif ; endif + push edx ; save return code + mov ebx,[eax] ; load up the saved registers + mov ecx,4[eax] + mov esi,12[eax] + mov edi,16[eax] + mov ebp,20[eax] + mov dx,32[eax] + verr dx ; verify segment + _if ne + sub edx,edx + _endif + mov es,dx + mov dx,38[eax] + verr dx ; verify segment + _if ne + sub edx,edx + _endif + mov fs,dx + mov dx,40[eax] + verr dx ; verify segment + _if ne + sub edx,edx + _endif + mov gs,dx + mov edx,8[eax] + mov ds,34[eax] + pop eax ; get return code + ret ; return to point following setjmp call +longjmp endp + +_TEXT ends + end