Changeset 2833 for trunk/kLdr/kLdrDy.c


Ignore:
Timestamp:
Oct 26, 2006, 2:08:09 AM (19 years ago)
Author:
bird
Message:

Wrote the kLdrDyld verification routines and made it build on win32.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kLdr/kLdrDy.c

    r2828 r2833  
    3939/** Pointer to the head module (the executable).
    4040 * (This is exported, so no prefix.) */
    41 PKLDRDY         kLdrDyHead = NULL;
     41PKLDRDYLDMOD    kLdrDyldHead = NULL;
    4242/** Pointer to the tail module.
    4343 * (This is exported, so no prefix.) */
    44 PKLDRDY         kLdrDyTail = NULL;
     44PKLDRDYLDMOD    kLdrDyldTail = NULL;
    4545/** The Library search path. */
    46 char            kLdrDyLibraryPath[4096];
     46char            kLdrDyldLibraryPath[4096];
    4747/** The executable flags. */
    48 uint32_t        kLdrDyFlags;
    49 /** Set if we've initialized the loader. */
    50 static int      fInitialized = 0;
     48uint32_t        kLdrDyldFlags;
    5149
    5250
     
    5452*   Internal Functions                                                         *
    5553*******************************************************************************/
    56 static int kldrDyInit(void);
    57 static int kldrDyTerm(void);
     54static int kldrDyldLoad(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch,
     55                        unsigned fFlags, PPKLDRDYLDMOD ppMod, char *pszErr, size_t cchErr);
     56static int kldrDyldUnload(PKLDRDYLDMOD pMod);
     57static int kldrDyldFindByName(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch, PPKLDRDYLDMOD ppMod);
     58static int kldrDyldFindByAddress(uintptr_t Address, PPKLDRDYLDMOD ppMod, uint32_t *piSegment, uintptr_t *poffSegment);
     59static int kldrDyldGetName(PKLDRDYLDMOD pMod, char *pszName, size_t cchName);
     60static int kldrDyldGetFilename(PKLDRDYLDMOD pMod, char *pszFilename, size_t cchFilename);
     61static int kldrDyldQuerySymbol(PKLDRDYLDMOD pMod, uint32_t uSymbolOrdinal, const char *pszSymbolName, uintptr_t *pValue, uint32_t *pfKind);
     62
     63
     64
     65/**
     66 * Initialize the dynamic loader.
     67 */
     68int kldrDyInit(void)
     69{
     70    kLdrDyldHead = NULL;
     71    kLdrDyldTail = NULL;
     72    kLdrDyldFlags = 0;
     73    return 0;
     74}
     75
     76
     77/**
     78 * Terminate the dynamic loader.
     79 */
     80void kldrDyTerm(void)
     81{
     82
     83}
     84
     85
     86/**
     87 * Loads a module into the current process.
     88 *
     89 * @returns 0 on success, non-zero native OS status code or kLdr status code on failure.
     90 * @param   pszDll          The name of the dll to open.
     91 * @param   pszDefPrefix    Prefix to use when searching.
     92 * @param   pszDefSuffix    Suffix to use when searching.
     93 * @param   enmSearch       Method to use when locating the module and any modules it may depend on.
     94 * @param   fFlags          Flags, a combintation of the KLDRYDLD_LOAD_FLAGS_* \#defines.
     95 * @param   phMod           Where to store the handle to the loaded module.
     96 * @param   pszErr          Where to store extended error information. (optional)
     97 * @param   cchErr          The size of the buffer pointed to by pszErr.
     98 */
     99int     kLdrDyldLoad(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch,
     100                     unsigned fFlags, PHKLDRMOD phMod, char *pszErr, size_t cchErr)
     101{
     102    int rc;
     103
     104    /* validate arguments and initialize return values. */
     105    if (pszErr && cchErr)
     106        *pszErr = '\0';
     107    *phMod = NIL_HKLDRMOD;
     108    KLDRHLP_VALIDATE_STRING(pszDll);
     109    KLDRHLP_VALIDATE_OPTIONAL_STRING(pszDefPrefix);
     110    KLDRHLP_VALIDATE_OPTIONAL_STRING(pszDefSuffix);
     111    KLDRHLP_VALIDATE_ENUM(enmSearch, KLDRDYLD_SEARCH);
     112    KLDRHLP_VALIDATE_OPTIONAL_BUFFER(pszErr, cchErr);
     113
     114    /* get the semaphore and do the job. */
     115    rc = kldrHlpSemRequest();
     116    if (!rc)
     117    {
     118        PKLDRDYLDMOD pMod = NULL;
     119        rc = kldrDyldLoad(pszDll, pszDefPrefix, pszDefSuffix, enmSearch, fFlags, phMod, pszErr, cchErr);
     120        kldrHlpSemRelease();
     121        *phMod = pMod;
     122    }
     123    return rc;
     124}
     125
     126
     127/**
     128 * Unloads a module loaded by kLdrDyldLoad.
     129 *
     130 * @returns 0 on success, non-zero native OS status code or kLdr status code on failure.
     131 * @param   hMod            Module handle.
     132 */
     133int     kLdrDyldUnload(HKLDRMOD hMod)
     134{
     135    int rc;
     136
     137    /* validate */
     138    KLDRDYLD_VALIDATE_HKLDRMOD(hMod);
     139
     140    /* get sem & do work */
     141    rc = kldrHlpSemRequest();
     142    if (!rc)
     143    {
     144        rc = kldrDyldUnload(hMod);
     145        kldrHlpSemRelease();
     146    }
     147    return rc;
     148}
     149
     150
     151/**
     152 * Finds a module by name or filename.
     153 *
     154 * This call does not increase any reference counters and must not be
     155 * paired with kLdrDyldUnload() like kLdrDyldLoad().
     156 *
     157 * @returns 0 on success.
     158 * @returns KLDR_ERR_MODULE_NOT_FOUND on failure.
     159 * @param   pszDll          The name of the dll to look for.
     160 * @param   pszDefPrefix    Prefix to use when searching.
     161 * @param   pszDefSuffix    Suffix to use when searching.
     162 * @param   enmSearch       Method to use when locating the module.
     163 * @param   phMod           Where to store the handle of the module on success.
     164 */
     165int     kLdrDyldFindByName(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch, PHKLDRMOD phMod)
     166{
     167    int rc;
     168
     169    /* validate & initialize */
     170    *phMod = NIL_HKLDRMOD;
     171    KLDRHLP_VALIDATE_STRING(pszDll);
     172
     173    /* get sem & do work */
     174    rc = kldrHlpSemRequest();
     175    if (!rc)
     176    {
     177        PKLDRDYLDMOD pMod = NULL;
     178        rc = kldrDyldFindByName(pszDll, pszDefPrefix, pszDefSuffix, enmSearch, phMod);
     179        kldrHlpSemRelease();
     180        *phMod = pMod;
     181    }
     182    return rc;
     183}
     184
     185
     186/**
     187 * Finds a module by address.
     188 *
     189 * This call does not increase any reference counters and must not be
     190 * paired with kLdrDyldUnload() like kLdrDyldLoad().
     191 *
     192 * @returns 0 on success.
     193 * @returns KLDR_ERR_MODULE_NOT_FOUND on failure.
     194 * @param   Address         The address believed to be within some module.
     195 * @param   phMod           Where to store the module handle on success.
     196 * @param   piSegment       Where to store the segment number. (optional)
     197 * @param   poffSegment     Where to store the offset into the segment. (optional)
     198 */
     199int     kLdrDyldFindByAddress(uintptr_t Address, PHKLDRMOD phMod, uint32_t *piSegment, uintptr_t *poffSegment)
     200{
     201    int rc;
     202
     203    /* validate & initialize */
     204    *phMod = NIL_HKLDRMOD;
     205    if (piSegment)
     206        *piSegment = ~(uint32_t)0;
     207    if (poffSegment)
     208        *poffSegment = ~(uintptr_t)0;
     209
     210    /* get sem & do work */
     211    rc = kldrHlpSemRequest();
     212    if (!rc)
     213    {
     214        PKLDRDYLDMOD pMod = NULL;
     215        rc = kldrDyldFindByAddress(Address, &pMod, piSegment, poffSegment);
     216        kldrHlpSemRelease();
     217        *phMod = pMod;
     218    }
     219    return rc;
     220}
     221
     222
     223/**
     224 * Gets the module name.
     225 *
     226 * @returns 0 on success and pszName filled with the name.
     227 * @returns KLDR_ERR_INVALID_HANDLE or KLDR_ERR_BUFFER_OVERFLOW on failure.
     228 * @param   hMod        The module handle.
     229 * @param   pszName     Where to put the name.
     230 * @param   cchName     The size of the name buffer.
     231 * @see kLdrDyldGetFilename
     232 */
     233int     kLdrDyldGetName(HKLDRMOD hMod, char *pszName, size_t cchName)
     234{
     235    int rc;
     236
     237    /* validate */
     238    if (pszName && cchName)
     239        *pszName = '\0';
     240    KLDRDYLD_VALIDATE_HKLDRMOD(hMod);
     241    KLDRHLP_VALIDATE_BUFFER(pszName, cchName);
     242
     243    /* get sem & do work */
     244    rc = kldrHlpSemRequest();
     245    if (!rc)
     246    {
     247        rc = kldrDyldGetName(hMod, pszName, cchName);
     248        kldrHlpSemRelease();
     249    }
     250    return rc;
     251}
     252
     253
     254/**
     255 * Gets the module filename.
     256 *
     257 * @returns 0 on success and pszFilename filled with the name.
     258 * @returns KLDR_ERR_INVALID_HANDLE or KLDR_ERR_BUFFER_OVERFLOW on failure.
     259 * @param   hMod            The module handle.
     260 * @param   pszFilename     Where to put the filename.
     261 * @param   cchFilename     The size of the filename buffer.
     262 * @see kLdrDyldGetName
     263 */
     264int     kLdrDyldGetFilename(HKLDRMOD hMod, char *pszFilename, size_t cchFilename)
     265{
     266    int rc;
     267
     268    /* validate & initialize */
     269    if  (pszFilename && cchFilename);
     270        *pszFilename = '\0';
     271    KLDRDYLD_VALIDATE_HKLDRMOD(hMod);
     272    KLDRHLP_VALIDATE_BUFFER(pszFilename, cchFilename);
     273
     274    /* get sem & do work */
     275    rc = kldrHlpSemRequest();
     276    if (!rc)
     277    {
     278        rc = kldrDyldGetFilename(hMod, pszFilename, cchFilename);
     279        kldrHlpSemRelease();
     280    }
     281    return rc;
     282}
     283
     284
     285/**
     286 * Queries the value and type of a symbol.
     287 *
     288 * @returns 0 on success and pValue and pfKind set.
     289 * @returns KLDR_ERR_INVALID_HANDLE or KLDR_ERR_SYMBOL_NOT_FOUND on failure.
     290 * @param   hMod            The module handle.
     291 * @param   uSymbolOrdinal  The symbol ordinal. This is ignored if pszSymbolName is non-zero.
     292 * @param   pszSymbolName   The symbol name.
     293 * @param   pValue          Where to put the symbol value. Optional if pfKind is non-zero.
     294 * @param   pfKind          Where to put the symbol kind flags. Optional if pValue is non-zero.
     295 */
     296int     kLdrDyldQuerySymbol(HKLDRMOD hMod, uint32_t uSymbolOrdinal, const char *pszSymbolName, uintptr_t *pValue, uint32_t *pfKind)
     297{
     298    int rc;
     299
     300    /* validate & initialize */
     301    if (pfKind)
     302        *pfKind = 0;
     303    if (pValue)
     304        *pValue = 0;
     305    if (!pfKind && !pValue)
     306        return KLDR_ERR_INVALID_PARAMETER;
     307    KLDRDYLD_VALIDATE_HKLDRMOD(hMod);
     308    KLDRHLP_VALIDATE_OPTIONAL_STRING(pszSymbolName);
     309
     310    /* get sem & do work */
     311    rc = kldrHlpSemRequest();
     312    if (!rc)
     313    {
     314        rc = kldrDyldQuerySymbol(hMod, uSymbolOrdinal, pszSymbolName, pValue, pfKind);
     315        kldrHlpSemRelease();
     316    }
     317    return rc;
     318}
     319
     320
     321
     322
     323
     324
     325/**
     326 * Worker for kldrDyldLoad().
     327 * @internal
     328 */
     329static int kldrDyldLoad(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch,
     330                        unsigned fFlags, PPKLDRDYLDMOD ppMod, char *pszErr, size_t cchErr)
     331{
     332    /*
     333     * Open the module.
     334     */
     335
     336
     337    return -1;
     338}
     339
     340
     341/**
     342 * Worker for kldrDyldUnload().
     343 * @internal
     344 */
     345static int kldrDyldUnload(HKLDRMOD hMod)
     346{
     347    return -1;
     348}
     349
     350
     351/**
     352 * Worker for kLdrDyldFindByName().
     353 * @internal
     354 */
     355static int kldrDyldFindByName(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch, PPKLDRDYLDMOD ppMod)
     356{
     357    return -1;
     358}
     359
     360
     361/**
     362 * Worker for kLdrDyldFindByAddress().
     363 * @internal
     364 */
     365static int kldrDyldFindByAddress(uintptr_t Address, PPKLDRDYLDMOD ppMod, uint32_t *piSegment, uintptr_t *poffSegment)
     366{
     367    return -1;
     368}
     369
     370
     371/**
     372 * Worker for kLdrDyldGetName().
     373 * @internal
     374 */
     375static int kldrDyldGetName(PKLDRDYLDMOD pMod, char *pszName, size_t cchName)
     376{
     377    return -1;
     378}
     379
     380
     381/**
     382 * Worker for kLdrDyldGetFilename().
     383 * @internal
     384 */
     385static int kldrDyldGetFilename(PKLDRDYLDMOD pMod, char *pszFilename, size_t cchFilename)
     386{
     387    return -1;
     388}
     389
     390
     391/**
     392 * Worker for kLdrDyldQuerySymbol().
     393 * @internal
     394 */
     395static int kldrDyldQuerySymbol(PKLDRDYLDMOD pMod, uint32_t uSymbolOrdinal, const char *pszSymbolName, uintptr_t *pValue, uint32_t *pfKind)
     396{
     397    return -1;
     398}
    58399
    59400
    60401#if 0
    61 /**
    62  * Initialize the loader.
    63  */
    64 int kldrDyInit(void)
    65 {
    66     if (fInitialized)
    67         return 0;
    68     /** @todo */
    69     return 0;
    70 }
    71 
    72 
    73 
    74402void kldrLoadExe(PKLDREXEARGS pArgs)
    75403{
     
    146474            pCur->enmState = KLDRSTATE_LOADED;
    147475
    148     kldrSemRelease();
     476    kldrHlpSemRelease();
    149477
    150478    /*
     
    153481    kldrOSStartExe(pLdrModuleHead, pvEntry);
    154482}
    155 
    156 
    157 int kLdrLoadDll(const char *pszFilename, unsigned fFlags, void *pvmod)
    158 {
    159 
    160     return -1;
    161 }
    162 
    163 
    164483
    165484
Note: See TracChangeset for help on using the changeset viewer.