Changeset 2936


Ignore:
Timestamp:
Dec 31, 2006, 3:54:20 AM (19 years ago)
Author:
bird
Message:

hacking on init (3-4 link errors short of a libc07.dll!)

Location:
trunk/libc/src
Files:
3 added
1 deleted
11 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/libc/src/glibc/sysdeps/os2/__spawni.c

    r2935 r2936  
    7575    pRAction->fdSaved = -1;
    7676    pRAction->fSavedCoE = -1;
    77     if (!__libc_FH(fdToSave))
     77    if (__libc_Back_fhValidate(fdToSave))
    7878        return 0;                       /* no such handle. */
    7979
     
    219219            {
    220220                case spawn_do_close:
    221                     if (!__libc_FH(pAction->action.close_action.fd))
    222                         rc = -1;
     221                    rc = __libc_Back_fhValidate(pAction->action.close_action.fd);
    223222                    break;
    224223                case spawn_do_open:
     
    226225                    break;
    227226                case spawn_do_dup2:
    228                     if (!__libc_FH(pAction->action.dup2_action.fd))
    229                         rc = -1;
     227                    rc = __libc_Back_fhValidate(pAction->action.dup2_action.fd);
    230228                    if (pAction->action.dup2_action.newfd == -1)
    231229                        cOpens++;
     
    276274                continue;
    277275            /* if not closed or not being freed we cannot use it. */
    278             if (!fClosed && __libc_FH(fdMin))
     276            if (!fClosed && !__libc_Back_fhValidate(fdMin))
    279277                continue;
    280278            /* it's either closed or free */
  • trunk/libc/src/kNIX/Makefile.kmk

    r2935 r2936  
    3535libc_kNIX_INCS = $(PATH_LIBC_SRC)/kNIX
    3636libc_kNIX_SOURCES = \
     37    $(PATH_LIBC_SRC)/kNIX/b_initterm.c \
    3738    $(PATH_LIBC_SRC)/kNIX/b_fhImport.c \
    3839    $(PATH_LIBC_SRC)/kNIX/b_fhValidate.c \
     
    7980
    8081libc_kNIX_SOURCES.os2 = \
     82    $(PATH_LIBC_SRC)/kNIX/os2/b_initterm-os2.c \
     83    $(PATH_LIBC_SRC)/kNIX/os2/b_initDllLoadException.c \
     84    $(PATH_LIBC_SRC)/kNIX/os2/__libc_back_envInit.c \
     85    $(PATH_LIBC_SRC)/kNIX/os2/386/__libc_back_envInitAsm.s \
    8186    $(PATH_LIBC_SRC)/kNIX/os2/b_dir.c \
    8287    $(PATH_LIBC_SRC)/kNIX/os2/b_fsDirChangeRoot.c \
     
    168173    $(PATH_LIBC_SRC)/kNIX/os2/tcpipver43.c \
    169174    $(PATH_LIBC_SRC)/kNIX/os2/timebomb.c \
    170     $(PATH_LIBC_SRC)/kNIX/os2/386/appinit.s \
    171175    $(PATH_LIBC_SRC)/kNIX/os2/386/thunk0.s \
    172176    $(PATH_LIBC_SRC)/kNIX/os2/386/thunk1.s \
    173177    $(PATH_LIBC_SRC)/kNIX/os2/386/unwind.s \
    174     $(PATH_LIBC_SRC)/kNIX/os2/386/__init_environ.s \
    175178    $(PATH_LIBC_SRC)/kNIX/os2/_os2_bad.c \
    176179    $(PATH_LIBC_SRC)/kNIX/os2/__spawnve.c \
  • trunk/libc/src/kNIX/b_initterm.c

    r2929 r2936  
    1 /* $Id: $ */
     1/* $Id$ */
    22/** @file
    33 *
    4  * kNIX - Process init.
     4 * kNIX - Process Init/Term.
    55 *
    66 * Copyright (c) 2006 knut st. osmundsen <bird-src-spam@anduin.net>
     
    2525 */
    2626
    27 
    2827/*******************************************************************************
    2928*   Header Files                                                               *
    3029*******************************************************************************/
     30#include "kNIX.h"
    3131#define __LIBC_LOG_GROUP __LIBC_LOG_GRP_INITTERM
    32 #include "kNIX.h"
     32#include <klibc/logstrict.h>
    3333
     34/*******************************************************************************
     35*   Global Variables                                                           *
     36*******************************************************************************/
     37/** Number of backend users. */
     38static int32_t volatile g_ckNIXUsers = 0;
    3439
    3540
     
    4651 * @param   fFlags  Initialization flags, a combination of the __LIBC_INIT_FLAGS_* \#defines.
    4752 */
    48 static int __libc_init(uintptr_t hmod, void *pvOS, unsigned fFlags)
     53static int __libc_back_init(uintptr_t hmod, void *pvOS, unsigned fFlags)
    4954{
    50     LIBCLOG_ENTER("hmod=%tx pvOS=%p fFlags=%#x\n", hmod, pvOs, fFlags);
     55    LIBCLOG_ENTER("hmod=%tx pvOS=%p fFlags=%#x\n", hmod, pvOS, fFlags);
    5156    int rc;
    5257
    5358    /*
    54      * Do OS specific initialization of the process.
     59     * First up is the fast info blocks / cached process data.
    5560     */
    56 #ifdef __NT__
    57 #endif
    58 #ifdef __OS2__
    59     rc = __libc_spmInit();
    60     if (rc)
    61         LIBCLOG_ERROR_RETURN_INT(rc);
    62 #endif
    63 
    64     /*
    65      * Initialize the thread database entering this thread as the first one.
    66      */
    67     rc = __libc_back_threadInit()
     61    rc = __libc_back_fibInit(0);
    6862    if (!rc)
    6963    {
    7064        /*
    71          * Init the other
     65         * Do OS specific initialization of the process.
    7266         */
    73 
    74         rc = __libc_back_fsInit();
     67        rc = __libc_back_initOS(hmod, pvOS, fFlags);
    7568        if (!rc)
    7669        {
     70            /*
     71             * Initialize all backend mutexes residing in parts that doesn't have
     72             * special initializers.
     73             */
     74
     75
     76            if (!rc)
     77                rc = __libc_back_fsInit();
     78            if (!rc)
     79            {
     80                /*
     81                 * Init file handles.
     82                 */
     83                rc = __libc_fhInit();
     84                if (!rc)
     85                {
     86                    /*
     87                     * Init signals
     88                     */
     89                    rc = __libc_back_signalInit();
     90                    if (!rc)
     91                    {
     92                        /*
     93                         * Setup environment (org_environ and _STD(environ))
     94                         */
     95                        rc = __libc_back_envInit();
     96                        if (!rc)
     97                        {
     98                            /* more? */
     99                            LIBCLOG_RETURN_INT(0);
     100                        }
     101                        //__libc_back_signalTerm();
     102                    }
     103
     104                    //__libc_fhTerm();
     105                }
     106
     107                //__libc_back_fsTerm();
     108            }
     109            __libc_back_termOS(hmod, pvOS, fFlags);
    77110        }
    78 
    79111    }
    80112
    81 
    82     /*
    83      * Initialize all backend mutexes residing in parts that doesn't have
    84      * special initializers.
    85      */
    86 
    87 
    88     if (__libc_back_fsInit())
    89     {
    90         LIBC_ASSERTM_FAILED("__libc_back_fsInit() failed\n");
    91         return -1;
    92     }
    93 
    94     /*
    95      * Init file handles.
    96      */
    97     rc = __libc_fhInit();
    98     if (rc)
    99     {
    100         LIBC_ASSERTM_FAILED("__libc_fhInit() failed\n");
    101         return -1;
    102     }
    103 
    104     /*
    105      * Init signals
    106      */
    107     rc = __libc_back_signalInit();
    108     if (rc)
    109     {
    110         LIBC_ASSERTM_FAILED("__libc_back_signalInit() failed\n");
    111         return -1;
    112     }
    113 
    114     /*
    115      * Setup environment (org_environ and _STD(environ))
    116      */
    117     rc = _sys_init_environ(fibGetEnv());
    118     if (rc)
    119     {
    120         LIBC_ASSERTM_FAILED("_sys_init_environ() failed\n");
    121         return -1;
    122     }
    123 
    124 
    125     LIBCLOG_RETURN_INT(0);
     113    LIBCLOG_ERROR_RETURN_INT(rc);
    126114}
    127115
     
    135123 * @param   pvUser      OS specific argument.
    136124 */
    137 int __libc_Back_init(uintptr_t hmod, unsigned fFlags, void *pvUser)
     125int __libc_Back_init(uintptr_t hmod, void *pvOS, unsigned fFlags)
    138126{
    139     LIBCLOG_ENTER("hmod=%tx fFlags=%#x pvUser=%p\n", hmod, fFlags, pvUser);
     127    LIBCLOG_ENTER("hmod=%tx pvOS=%p fFlags=%#x\n", hmod, pvOS, fFlags);
    140128
    141 
    142 
    143     LIBCLOG_RETURN_INT(0);
     129    /*
     130     * Only initialize it once.
     131     *
     132     * We currently IGNORE the posibility that two thread might be racing here and
     133     * the loser begin able to call into LIBC while it's begin initialized.
     134     */
     135    const int32_t cRefs = __atomic_increment_s32(&g_ckNIXUsers);
     136    if (cRefs != 1)
     137        LIBCLOG_RETURN_MSG(0, "ret 0 (g_ckNIXUsers=%d)\n", cRefs);
     138    int rc = __libc_back_init(hmod, pvOS, fFlags);
     139    if (!rc)
     140        LIBCLOG_RETURN_INT(0);
     141    __atomic_decrement_s32(&g_ckNIXUsers);
     142    LIBCLOG_ERROR_RETURN_INT(rc);
    144143}
    145144
    146 
  • trunk/libc/src/kNIX/kNIX.h

    r2935 r2936  
    4343#endif
    4444#include <errno.h>
     45#define _osmajor __osminor
     46#define _osminor __osminor
     47#include <stdlib.h>
     48#undef _osmajor
     49#undef _osminor
    4550#include <io.h>
    4651#include <malloc.h>
     
    261266
    262267
    263 /** @group Init Functions
     268/** @group Init & Term Functions
    264269 * @{ */
    265 extern void             __init(int fFlags);
    266 extern int              __init_dll(int fFlags, unsigned long hmod);
    267 extern void /*volatile*/_sys_init_ret(void *stack) __attribute__((__noreturn__));
    268 extern int              _sys_init_environ(const char *pszEnv);
    269 extern void             _sys_init_largefileio(void);
    270 extern int              __libc_fhInit(void);
     270extern int  __libc_back_initOS(uintptr_t hmod, void *pvOS, unsigned fFlags);
     271extern void __libc_back_termOS(uintptr_t hmod, void *pvOS, unsigned fFlags);
     272extern int  __libc_fhInit(void);
     273extern int __libc_back_envInit(void);
    271274/** @} */
    272275
  • trunk/libc/src/kNIX/os2/386/__libc_back_envInitAsm.s

    r2929 r2936  
    44 *
    55 * Copyright (c) 2003 InnoTek Systemberatung GmbH
    6  * Author: knut st. osmundsen <bird-srcspam@anduin.net>
     6 * Author: knut st. osmundsen <bird-src-spam@anduin.net>
    77 *
    88 *
     
    3232 * Initialize __org_environ and _STD(environ).
    3333 *
    34  * @cproto  int __init_dll_env(const char *pszEnv);
     34 * @cproto  int __libc_back_envInitAsm(const char *pszzEnv);
    3535 * @sketch Assumes large amounts of stack so we can construct the a reverse array there.
    3636 *
    3737 */
    38     .globl __sys_init_environ
    39 __sys_init_environ:
     38    .globl ___libc_back_envInitAsm
     39___libc_back_envInitAsm:
    4040    pushl   %ebp
    4141    movl    %esp, %ebp
  • trunk/libc/src/kNIX/os2/kNIX-os2.h

    r2929 r2936  
    1 /* $Id: $ */
     1/* $Id$ */
    22/** @file
    33 *
  • trunk/libc/src/kNIX/os2/syscalls.h

    r2929 r2936  
    4646extern void             __init(int fFlags);
    4747extern int              __init_dll(int fFlags, unsigned long hmod);
    48 extern void /*volatile*/_sys_init_ret(void *stack) __attribute__((__noreturn__));
    49 extern int              _sys_init_environ(const char *pszEnv);
    50 extern void             _sys_init_largefileio(void);
    51 extern int              __libc_fhInit(void);
    5248/** @} */
    5349
  • trunk/libc/src/libc/libc.def

    r2935 r2936  
    249249    "___ibitmap" @262
    250250    ; dead "___imphandle" @263
    251     "___init" @264
    252     "___init_app" @265
    253     "___init_dll" @266
     251    ; dead "___init" @264
     252    ; dead "___init_app" @265
     253    ; dead "___init_dll" @266
    254254    ; dead "___ioctl1" @267
    255255    ; dead "___ioctl2" @268
     
    19581958    "__std_nanf" @1956
    19591959    "__std_nanl" @1957
     1960
    19601961    ; new stuff 0.7
    1961     "_gOS2FileOps" @1958
     1962    "__libc_back_envInitAsm" @1958
    19621963    "__std_adjtime" @1959
    19631964    "__std_getrusage" @1960
  • trunk/libc/src/libc/startup/Makefile.kmk

    r2901 r2936  
    3838    $(PATH_LIBC_SRC)/libc/startup/exit.c \
    3939    $(PATH_LIBC_SRC)/libc/startup/initterm.c \
     40    $(PATH_LIBC_SRC)/libc/startup/startup.c \
    4041    $(PATH_LIBC_SRC)/libc/startup/_exit.c
    4142
  • trunk/libc/src/libc/startup/initterm.c

    r2901 r2936  
    4444#include <klibc/backend.h>
    4545#include <klibc/atexit.h>
    46 #include <sys/builtin.h>
     46#include <klibc/umalloc.h>
    4747
    4848
     
    153153
    154154    /*
    155      * Only initialize it once.
     155     * Process the flags.
     156     */
     157    __libc_HeapVote(fFlags & __LIBC_INIT_FLAGS_MEM_HIGH);
     158    /*if (fFlags & __LIBC_INIT_FLAGS_NO_UNIX)
     159        __libc_gfNoUnix = 1;*/
     160
     161    /*
     162     * The remaining bits are only done once.
    156163     *
    157164     * We currently IGNORE the posibility that two thread might be racing here and
     
    175182         */
    176183
     184        /* weak initializers. */
     185        __ctordtorTerm1(&__crtinit1__);
    177186
    178187        LIBCLOG_RETURN_INT(0);
    179188    }
     189
     190    __atomic_decrement_s32(&g_cCrtUsers);
    180191    LIBCLOG_ERROR_RETURN_INT(rc);
    181192}
     
    212223     */
    213224
     225    /* weak terminators */
     226    __ctordtorTerm1(&__crtexit1__);
    214227
    215228    /*
  • trunk/libc/src/libc/startup/os2/x86/dll0.s

    r2806 r2936  
    222222
    223223    /*
    224      * Call __libc_InitDllException and let it decide what to do.
     224     * Call ___libc_Back_initDllLoadException and let it decide what to do.
    225225     * It returns 0 if we should continue search the exception handler chain.
    226226     * It returns -1 if we resume execution.
     
    231231    pushl   0x0c(%ebp)
    232232    pushl   0x08(%ebp)
    233     call    ___libc_InitDllException
     233    call    ___libc_Back_initDllLoadException
    234234    orl     %eax, %eax
    235235    jz      dll0_xcpt_return
  • trunk/libc/src/libc/startup/startup.c

    r2785 r2936  
    22/** @file
    33 *
    4  * kLIBC - CRT init and termination code.
     4 * kLIBC - VAC/MSC initialization and termination compatability.
    55 *
    6  * Copyright (c) 1990-1998 by Eberhard Mattes
    7  * Copyright (c) 2004-2006 knut st. osmundsen <bird@innotek.de>
     6 * Copyright (c) 2004-2006 knut st. osmundsen <bird-src-spam@anduin.net>
    87 *
    98 *
     
    2625 */
    2726
    28 
    29 
    30 /*******************************************************************************
    31 *   Header Files                                                               *
    32 *******************************************************************************/
     27#include <klibc/initterm.h>
    3328#include <errno.h>
    34 #include <sys/builtin.h>
    35 #include <emx/startup.h>
    36 #define __LIBC_LOG_GROUP    __LIBC_LOG_GRP_INITTERM
    37 #include <InnoTekLIBC/logstrict.h>
    38 
    39 
    40 /*******************************************************************************
    41 *   Global Variables                                                           *
    42 *******************************************************************************/
    43 /** The balance between _CRT_init and _CRT_term calls. */
    44 static volatile int32_t gcCRTReferences = 0;
     29#define __LIBC_LOG_GROUP __LIBC_LOG_GRP_INITTERM
     30#include <klibc/logstrict.h>
    4531
    4632
     
    4834 * Initializes the C runtime library.
    4935 *
    50  * A _CRT_init() call should be matched by a _CRT_term() call as we are
    51  * keeping a count of the number of C runtime users.
    52  *
    53  * This function is normally called from _DLL_InitTerm and crt0.s.
    54  *
    5536 * @returns 0 on success. -1 on failure.
     37 * @remark VAC/MSC compatability stub (OS/2).
    5638 */
    57 int _CRT_init (void)
     39int _CRT_init(void)
    5840{
    59   LIBCLOG_ENTER("\n");
    60 
    61     /*
    62      * On initialize once.
    63      */
    64     int32_t cRefs = __atomic_increment_s32(&gcCRTReferences);
    65     if (cRefs != 1)
    66         LIBCLOG_RETURN_MSG(0, "ret 0 (cRefs=%d)\n", cRefs);
    67 
    68     /*
    69      * Call the weak initializers.
    70      */
    71   __ctordtorInit1 (&__crtinit1__);
     41    LIBCLOG_ENTER("\n");
    7242
    7343    /*
     
    7747     * to zero before calling main().
    7848     */
    79   errno = 0;
    80   LIBCLOG_RETURN_INT(0);
     49    errno = 0;
     50
     51    LIBCLOG_RETURN_INT(0);
    8152}
    8253
     
    8556 * Terminates the C runtime library.
    8657 *
    87  * A _CRT_term() call must be paired with a _CRT_init() call as we're
    88  * counting the number of C runtime users.
    89  *
    90  * This function is normally called from _DLL_InitTerm and crt0.s.
     58 * @remark VAC/MSC compatability stub (OS/2).
    9159 */
    9260void _CRT_term(void)
    9361{
    9462    LIBCLOG_ENTER("\n");
    95     int32_t cRefs = __atomic_decrement_s32(&gcCRTReferences);
    96     if (cRefs == 0)
    97     {
    98         /*
    99          * Call the weak terminators.
    100          */
    101         __ctordtorTerm1(&__crtexit1__);
    102     }
    103     else if (cRefs < 0)
    104         LIBCLOG_ERROR_RETURN_MSG_VOID("cRefs=%d\n", cRefs);
    10563    LIBCLOG_RETURN_VOID();
    10664}
    10765
    108 
    109 /** @page Startup   Startup
    110  *
    111  * Quick description of how we startup a process which modules uses LIBC.
    112  *
    113  * The load first loads the DLLs of the process, calling their _DLL_InitTerm
    114  * entrypoints in order of dependencies. If you're using dynamic LIBC this
    115  * will be initiated first of the LIBC-based modules.
    116  *
    117  * LIBCxy.DLL:
    118  *      - dll0.s gets control and calls __init_dll in sys/__initdll.c
    119  *          - __init_dll calls __libc_HeapVote() to do the heap voting.
    120  *          - __init_dll initiates the _osminor and _osmajor globals.
    121  *          - __init_dll initiates _sys_gcbVirtualAddressLimit global.
    122  *          - __init_dll initiates _sys_pid and _sys_ppid globals.
    123  *          - __init_dll creates _sys_heap_fmutex, _sys_gmtxHimem and __libc_gmtxExec.
    124  *          - __init_dll then initiates __libc_gpTLS with an allocated TLS ULONG.
    125  *            The thread structure it self isn't initialized untill it's actually
    126  *            referenced, and when it is there is static structure for the first
    127  *            thread needing its per thread area.
    128  *          - __init_dll calls _sys_init_environ() which initializes environ and _org_environ.
    129  *              - _sys_init_environ() will call _hmalloc() thus initiating the high heap.
    130  *          - __init_dll calls __libc_spmSelf() which checks initiates SPM and gets
    131  *            any inherited properties.
    132  *          - __init_dll calls _sys_init_largefileio() which checks for LFN APIs.
    133  *          - __init_dll calls __libc_fhInit() which initiates filehandle table.
    134  *              - __libc_fhInit() will call _hmalloc() and a number of OS/2 APIs
    135  *                to figure out what handles was inherited.
    136  *          - __init_dll checks for LIBC_HOOK_DLLS and calls __libc_back_hooksInit()
    137  *                to process it if it is present in the environment.
    138  *          - __init_dll then intializes _sys_clock0_ms with the current MS count.
    139  *      - dll0.s calls _DLL_InitTerm in startup/dllinit.c.
    140  *          - _DLL_InitTerm calls _CRT_init() in startup/startup.c to initialize the CRT.
    141  *              - _CRT_init calls init_files() to initialize the file handle tables.
    142  *              - _CRT_init then call all the crt init functions in __crtinit1__.
    143  *          - _DLL_InitTerm calls __ctordtorInit() to initate any exception structures and
    144  *            construct static C++ objects.
    145  *      - dll0.s then returns to DOSCALL1.DLL and back to the kernel.
    146  *
    147  * Your.DLL:
    148  *      - dll0.s gets control and calls __init_dll() in sys/__initdll.c
    149  *          - __init_dll calls __libc_HeapVote() to do the heap voting.
    150  *          - __init_dll then returns since already done during LIBCxy.DLL init.
    151  *      - dll0.s calls _DLL_InitTerm in startup/dllinit.c.
    152  *          - _DLL_InitTerm calls _CRT_init() in startup/startup.c to initialize the CRT.
    153  *              - _CRT_init return at once since already done during LIBCxy.DLL init.
    154  *          - _DLL_InitTerm calls __ctordtorInit() to initate any exception structures and
    155  *            construct static C++ objects in _this_ DLL.
    156  *      - dll0.s then returns to DOSCALL1.DLL and back to the kernel.
    157  *
    158  * Your.exe:
    159  *      - crt0.s gets control and calls ___init_app in sys/386/appinit.s which forwards
    160  *        the call to __init() in sys/__init.c.
    161  *          - __init() calls __init_dll() to do common initiation.
    162  *              - __init_dll calls __libc_HeapVote() to do the heap voting.
    163  *              - __init_dll then returns since already done during LIBCxy.DLL init.
    164  *          - __init() parse the commandline to figure out how much stack to use for array
    165  *            and the argument copy.
    166  *          - __init() the allocates the stack space space, adding an exception handler struct
    167  *            and main() callframe.
    168  *          - __init() parse the commandline creating argv and it's strings using the
    169  *            allocated stack space.
    170  *          - __init() calls __libc_spmInheritFree() to release the inherit data (shared)
    171  *            associated with the current process.
    172  *          - __init() installs the exception handler.
    173  *          - __init() set the signal focus.
    174  *          - __init() then 'returns' thru the hack called ___init_ret in sys/386/appinit.s
    175  *      - crt0.s now gets control back with esp pointing to a complete callframe for main().
    176  *      - crt0.s calls _CRT_init() in startup/startup.c to initialize the CRT which returns
    177  *        immediately since since already done during LIBCxy.DLL init.
    178  *      - crt0.s the calls main().
    179  *      - crt0.s then calls exit() with the return value of main().
    180  *          - exit() will call all the registered at_exit functions.
    181  *          - exit() will call DosExit with the exit code and terminate the process.
    182  *
    183  * @todo update this with file handle changes!
    184  * @todo update this with fork and performance changes!
    185  */
    186 
Note: See TracChangeset for help on using the changeset viewer.