Logo Search packages:      
Sourcecode: virtualbox-ose version File versions  Download package

kLdrMod.c

Go to the documentation of this file.
/* $Id: kLdrMod.c 2 2007-11-16 16:07:14Z bird $ */
/** @file
 * kLdr - The Module Interpreter.
 */

/*
 * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
 *
 * This file is part of kStuff.
 *
 * kStuff is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * In addition to the permissions in the GNU Lesser General Public
 * License, you are granted unlimited permission to link the compiled
 * version of this file into combinations with other programs, and to
 * distribute those combinations without any restriction coming from
 * the use of this file.
 *
 * kStuff is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with kStuff; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 */

/*******************************************************************************
*   Header Files                                                               *
*******************************************************************************/
#include <k/kLdr.h>
#include "kLdrInternal.h"
#include <k/kCpu.h>
#include <k/kLdrFmts/mz.h>
#if 1 /* testing headers */
# include <k/kLdrFmts/pe.h>
# include <k/kLdrFmts/lx.h>
# include <k/kLdrFmts/elf32.h>
# include <k/kLdrFmts/elf64.h>
# include <k/kLdrFmts/mach-o.h>
#endif


/*******************************************************************************
*   Defined Constants And Macros                                               *
*******************************************************************************/
/** @def KLDRMOD_STRICT
 * Define KLDRMOD_STRICT to enabled strict checks in KLDRMOD. */
00054 #define KLDRMOD_STRICT 1

/** @def KLDRMOD_ASSERT
 * Assert that an expression is true when KLDR_STRICT is defined.
 */
#ifdef KLDRMOD_STRICT
00060 # define KLDRMOD_ASSERT(expr)  kHlpAssert(expr)
#else
# define KLDRMOD_ASSERT(expr)  do {} while (0)
#endif

/** Return / crash validation of a module argument. */
00066 #define KLDRMOD_VALIDATE_EX(pMod, rc) \
    do  { \
        if (    (pMod)->u32Magic != KLDRMOD_MAGIC \
            ||  (pMod)->pOps == NULL \
           )\
        { \
            return (rc); \
        } \
    } while (0)

/** Return / crash validation of a module argument. */
00077 #define KLDRMOD_VALIDATE(pMod) \
    KLDRMOD_VALIDATE_EX(pMod, KERR_INVALID_PARAMETER)

/** Return / crash validation of a module argument. */
00081 #define KLDRMOD_VALIDATE_VOID(pMod) \
    do  { \
        if (    (pMod)->u32Magic != KLDRMOD_MAGIC \
            ||  (pMod)->pOps == NULL \
           )\
        { \
            return; \
        } \
    } while (0)


/*******************************************************************************
*   Global Variables                                                           *
*******************************************************************************/
/** The list of module interpreters. */
00096 static PCKLDRMODOPS g_pModInterpreterHead = NULL;



/*******************************************************************************
*   Internal Functions                                                         *
*******************************************************************************/



/**
 * Open a executable image by file name.
 *
 * @returns 0 on success and *ppMod pointing to a module instance.
 *          On failure, a non-zero OS specific error code is returned.
 * @param   pszFilename     The filename to open.
 * @param   ppMod           Where to store the module handle.
 */
00114 int kLdrModOpen(const char *pszFilename, PPKLDRMOD ppMod)
{
    /*
     * Open the file using a bit provider.
     */
    PKRDR pRdr;
    int rc = kRdrOpen(&pRdr, pszFilename);
    if (!rc)
    {
        rc = kLdrModOpenFromRdr(pRdr, ppMod);
        if (!rc)
            return 0;
       kRdrClose(pRdr);
    }
    return rc;
}


/**
 * Open a executable image from a file provider instance.
 *
 * @returns 0 on success and *ppMod pointing to a module instance.
 *          On failure, a non-zero OS specific error code is returned.
 * @param   pRdr            The file provider instance to use.
 *                          On success, the ownership of the instance is taken by the
 *                          module and the caller must not ever touch it again.
 *                          (The instance is not closed on failure, the call has to do that.)
 * @param   ppMod           Where to store the module handle.
 */
00143 int kLdrModOpenFromRdr(PKRDR pRdr, PPKLDRMOD ppMod)
{
    union
    {
        KU32        u32;
        KU16        u16;
        KU16        au16[2];
        KU8         au8[4];
    }           u;
    KLDRFOFF    offHdr = 0;
    int         rc;

    /*
     * Try figure out what kind of image this is.
     * Always read the 'new header' if we encounter MZ.
     */
    rc = kRdrRead(pRdr, &u, sizeof(u), 0);
    if (rc)
        return rc;
    if (    u.u16 == IMAGE_DOS_SIGNATURE
        &&  kRdrSize(pRdr) > sizeof(IMAGE_DOS_HEADER))
    {
        rc = kRdrRead(pRdr, &u, sizeof(u.u32), K_OFFSETOF(IMAGE_DOS_HEADER, e_lfanew));
        if (rc)
            return rc;
        if ((KLDRFOFF)u.u32 < kRdrSize(pRdr))
        {
            offHdr = u.u32;
            rc = kRdrRead(pRdr, &u, sizeof(u.u32), offHdr);
            if (rc)
                return rc;
        }
        else
            u.u16 = IMAGE_DOS_SIGNATURE;
    }

    /*
     * Use the magic to select the appropriate image interpreter head on.
     */
    if (u.u16 == IMAGE_DOS_SIGNATURE)
        rc = KLDR_ERR_MZ_NOT_SUPPORTED;
    else if (u.u16 == IMAGE_NE_SIGNATURE)
        rc = KLDR_ERR_NE_NOT_SUPPORTED;
    else if (u.u16 == IMAGE_LX_SIGNATURE)
        rc = g_kLdrModLXOps.pfnCreate(&g_kLdrModLXOps, pRdr, offHdr, ppMod);
    else if (u.u16 == IMAGE_LE_SIGNATURE)
        rc = KLDR_ERR_LE_NOT_SUPPORTED;
    else if (u.u32 == IMAGE_NT_SIGNATURE)
        rc = g_kLdrModPEOps.pfnCreate(&g_kLdrModPEOps, pRdr, offHdr, ppMod);
    else if (   u.u32 == IMAGE_MACHO32_SIGNATURE
             || u.u32 == IMAGE_MACHO32_SIGNATURE_OE
             || u.u32 == IMAGE_MACHO64_SIGNATURE
             || u.u32 == IMAGE_MACHO64_SIGNATURE_OE)
        rc = g_kLdrModMachOOps.pfnCreate(&g_kLdrModMachOOps, pRdr, offHdr, ppMod);
    else if (u.u32 == IMAGE_ELF_SIGNATURE)
        rc = KLDR_ERR_ELF_NOT_SUPPORTED;
    else if (   u.u32 == IMAGE_FAT_SIGNATURE
             || u.u32 == IMAGE_FAT_SIGNATURE_OE)
        rc = KLDR_ERR_FAT_NOT_SUPPORTED;
    else
        rc = KLDR_ERR_UNKNOWN_FORMAT;

    /*
     * If no head on hit, let each interpreter have a go.
     */
    if (rc)
    {
        PCKLDRMODOPS pOps;
        for (pOps = g_pModInterpreterHead; pOps; pOps = pOps->pNext)
        {
            int rc2 = pOps->pfnCreate(pOps, pRdr, offHdr, ppMod);
            if (!rc2)
                return rc;
        }
        *ppMod = NULL;
    }
    return rc;
}


/**
 * Closes an open module.
 *
 * The caller is responsible for calling kLdrModUnmap() and kLdrFreeTLS()
 * before closing the module.
 *
 * @returns 0 on success, non-zero on failure. The module instance state
 *          is unknown on failure, it's best not to touch it.
 * @param   pMod    The module.
 */
00233 int     kLdrModClose(PKLDRMOD pMod)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnDestroy(pMod);
}


/**
 * Queries a symbol by name or ordinal number.
 *
 * @returns 0 and *puValue and *pfKind on success.
 *          KLDR_ERR_SYMBOL_NOT_FOUND is returned if the symbol wasn't found.
 *          Other failures could stem from bad executable format failures,
 *          read failure in case pvBits isn't specified and no mapping should be used.
 * @param   pMod            The module.
 * @param   pvBits          Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
 *                          This can be used by some module interpreters to reduce memory consumption.
 * @param   BaseAddress     The module base address to use when calculating the symbol value.
 *                          There are two special values that can be used:
 *                              KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
 * @param   iSymbol         The symbol ordinal. (optional)
 * @param   pchSymbol       The symbol name. (optional)
 *                          Important, this doesn't have to be a null-terminated string.
 * @param   cchSymbol       The length of the symbol name.
 * @param   pszVersion      The symbol version. NULL if not versioned.
 * @param   pfnGetForwarder The callback to use when resolving a forwarder symbol. This is optional
 *                          and if not specified KLDR_ERR_FORWARDER is returned instead.
 * @param   pvUser          The user argument for the pfnGetForwarder callback.
 * @param   puValue         Where to store the symbol value. (optional)
 * @param   pfKind          On input one of the KLDRSYMKIND_REQ_* #defines.
 *                          On output the symbol kind. (optional)
 */
00265 int     kLdrModQuerySymbol(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 iSymbol,
                           const char *pchSymbol, KSIZE cchSymbol, const char *pszVersion,
                           PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind)
{
    KLDRMOD_VALIDATE(pMod);
    if (!puValue && !pfKind)
        return KERR_INVALID_PARAMETER;
    if (puValue)
        *puValue = 0;
    if (pfKind)
        K_VALIDATE_FLAGS(*pfKind, KLDRSYMKIND_REQ_SEGMENTED);
    return pMod->pOps->pfnQuerySymbol(pMod, pvBits, BaseAddress, iSymbol, pchSymbol, cchSymbol, pszVersion,
                                      pfnGetForwarder, pvUser, puValue, pfKind);
}


/**
 * Enumerate the symbols in the module.
 *
 * @returns 0 on success and non-zero a status code on failure.
 * @param   pMod            The module which symbols should be enumerated.
 * @param   pvBits          Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
 *                          This can be used by some module interpreters to reduce memory consumption.
 * @param   BaseAddress     The module base address to use when calculating the symbol values.
 *                          There are two special values that could be can:
 *                              KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
 * @param   fFlags          The enumeration flags. A combination of the KLDRMOD_ENUM_SYMS_FLAGS_* \#defines.
 * @param   pfnCallback     The enumeration callback function.
 * @param   pvUser          The user argument to the callback function.
 */
00295 int     kLdrModEnumSymbols(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 fFlags,
                           PFNKLDRMODENUMSYMS pfnCallback, void *pvUser)
{
    KLDRMOD_VALIDATE(pMod);
    K_VALIDATE_FLAGS(fFlags, KLDRMOD_ENUM_SYMS_FLAGS_ALL);
    return pMod->pOps->pfnEnumSymbols(pMod, pvBits, BaseAddress, fFlags, pfnCallback, pvUser);
}


/**
 * Get the name of an import module by ordinal number.
 *
 * @returns 0 and name in pszName on success.
 *          On buffer overruns KERR_BUFFER_OVERFLOW will be returned.
 *          On other failures and appropriate error code is returned.
 * @param   pMod            The module.
 * @param   pvBits          Optional pointer to bits returned by kLdrModGetBits().
 *                          This can be used by some module interpreters to reduce memory consumption.
 * @param   iImport         The import module ordinal number.
 * @param   pszName         Where to store the name.
 * @param   cchName         The size of the name buffer.
 */
00317 int     kLdrModGetImport(PKLDRMOD pMod, const void *pvBits, KU32 iImport, char *pszName, KSIZE cchName)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnGetImport(pMod, pvBits, iImport, pszName, cchName);
}


/**
 * Get the number of import modules.
 *
 * @returns The number of import modules. -1 if something really bad happens.
 * @param   pMod            The module.
 * @param   pvBits          Optional pointer to bits returned by kLdrModGetBits().
 *                          This can be used by some module interpreters to reduce memory consumption.
 */
00332 KI32 kLdrModNumberOfImports(PKLDRMOD pMod, const void *pvBits)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnNumberOfImports(pMod, pvBits);
}


/**
 * Checks if this module can be executed by the specified arch+cpu.
 *
 * @returns 0 if it can, KCPU_ERR_ARCH_CPU_NOT_COMPATIBLE if it can't.
 *          Other failures may occur and cause other return values.
 * @param   pMod            The module.
 * @param   pvBits          Optional pointer to bits returned by kLdrModGetBits().
 *                          This can be used by some module interpreters to reduce memory consumption.
 */
00348 int     kLdrModCanExecuteOn(PKLDRMOD pMod, const void *pvBits, KCPUARCH enmArch, KCPU enmCpu)
{
    KLDRMOD_VALIDATE(pMod);
    if (pMod->pOps->pfnCanExecuteOn)
        return pMod->pOps->pfnCanExecuteOn(pMod, pvBits, enmArch, enmCpu);
    return kCpuCompare(pMod->enmArch, pMod->enmCpu, enmArch, enmCpu);
}


/**
 * Gets the image stack info.
 *
 * @returns 0 on success, non-zero on failure.
 * @param   pMod
 * @param   pvBits          Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
 *                          This can be used by some module interpreters to reduce memory consumption.
 * @param   BaseAddress     The module base address to use when calculating the stack address.
 *                          There are two special values that can be used:
 *                              KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
 * @param   pStackInfo      The stack information.
 */
00369 int     kLdrModGetStackInfo(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnGetStackInfo(pMod, pvBits, BaseAddress, pStackInfo);
}


/**
 * Queries the main entrypoint of the module.
 *
 * Only executable are supposed to have an main entrypoint, though some object and DLL
 * formats will also allow this.
 *
 * @returns 0 and *pMainEPAddress on success. Non-zero status code on failure.
 * @param   pMod            The module.
 * @param   pvBits          Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
 *                          This can be used by some module interpreters to reduce memory consumption.
 * @param   BaseAddress     The module base address to use when calculating the entrypoint address.
 *                          There are two special values that can be used:
 *                              KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
 * @param   pMainEPAddress  Where to store the entry point address.
 */
00391 int     kLdrModQueryMainEntrypoint(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress)
{
    KLDRMOD_VALIDATE(pMod);
    *pMainEPAddress = 0;
    return pMod->pOps->pfnQueryMainEntrypoint(pMod, pvBits, BaseAddress, pMainEPAddress);
}


/**
 * Queries info about a resource.
 *
 * If there are multiple resources matching the criteria, the best or
 * first match will be return.
 *
 *
 * @returns 0 on success.
 * @returns Whatever non-zero status returned by pfnCallback (enumeration was stopped).
 * @returns non-zero kLdr or native status code on failure.
 *
 * @param   pMod            The module.
 * @param   pvBits          Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
 *                          This can be used by some module interpreters to reduce memory consumption.
 * @param   BaseAddress     The module base address to use when calculating the resource addresses.
 *                          There are two special values that can be used:
 *                              KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
 * @param   idType          The resource type id to match if not NIL_KLDRMOD_RSRC_TYPE_ID.
 * @param   pszType         The resource type name to match if no NULL.
 * @param   idName          The resource name id to match if not NIL_KLDRMOD_RSRC_NAME_ID.
 * @param   pszName         The resource name to match if not NULL.
 * @param   idLang          The language id to match.
 * @param   pfnCallback     The callback function.
 * @param   pvUser          The user argument for the callback.
 */
00424 int     kLdrModQueryResource(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 idType, const char *pszType,
                             KU32 idName, const char *pszName, KU32 idLang, PKLDRADDR pAddrRsrc, KSIZE *pcbRsrc)
{
    KLDRMOD_VALIDATE(pMod);
    if (!pAddrRsrc && !pcbRsrc)
        return KERR_INVALID_PARAMETER;
    if (pAddrRsrc)
        *pAddrRsrc = NIL_KLDRADDR;
    if (pcbRsrc)
        *pcbRsrc = 0;
    return pMod->pOps->pfnQueryResource(pMod, pvBits, BaseAddress, idType, pszType, idName, pszName, idLang, pAddrRsrc, pcbRsrc);
}


/**
 * Enumerates the resources matching the specfied criteria.
 *
 *
 * @returns 0 on success.
 * @returns Whatever non-zero status returned by pfnCallback (enumeration was stopped).
 * @returns non-zero kLdr or native status code on failure.
 *
 * @param   pMod            The module.
 * @param   pvBits          Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
 *                          This can be used by some module interpreters to reduce memory consumption.
 * @param   BaseAddress     The module base address to use when calculating the resource addresses.
 *                          There are two special values that can be used:
 *                              KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
 * @param   idType          The resource type id to match if not NIL_KLDRMOD_RSRC_TYPE_ID.
 * @param   pszType         The resource type name to match if no NULL.
 * @param   idName          The resource name id to match if not NIL_KLDRMOD_RSRC_NAME_ID.
 * @param   pszName         The resource name to match if not NULL.
 * @param   idLang          The language id to match.
 * @param   pfnCallback     The callback function.
 * @param   pvUser          The user argument for the callback.
 */
00460 int     kLdrModEnumResources(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 idType, const char *pszType,
                             KU32 idName, const char *pszName, KU32 idLang, PFNKLDRENUMRSRC pfnCallback, void *pvUser)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnEnumResources(pMod, pvBits, BaseAddress, idType, pszType, idName, pszName, idLang, pfnCallback, pvUser);
}


/**
 * Enumerate the debug info formats contained in the executable image.
 *
 * @returns 0 on success, non-zero OS or kLdr status code on failure, or non-zero callback status.
 * @param   pMod            The module.
 * @param   pvBits          Optional pointer to bits returned by kLdrModGetBits().
 *                          This can be used by some module interpreters to reduce memory consumption.
 * @param   pfnCallback     The callback function.
 * @param   pvUser          The user argument.
 * @see pg_kDbg for the debug info reader.
 */
00479 int     kLdrModEnumDbgInfo(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnEnumDbgInfo(pMod, pvBits, pfnCallback, pvUser);
}


/**
 * Checks if the module has debug info embedded or otherwise associated with it.
 *
 * @returns 0 if it has debug info, KLDR_ERR_NO_DEBUG_INFO if no debug info,
 *          and non-zero OS or kLdr status code on failure.
 * @param   pMod            The module.
 * @param   pvBits          Optional pointer to bits returned by kLdrModGetBits().
 *                          This can be used by some module interpreters to reduce memory consumption.
 */
00495 int     kLdrModHasDbgInfo(PKLDRMOD pMod, const void *pvBits)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnHasDbgInfo(pMod, pvBits);
}


/**
 * May free up some resources held by the module.
 *
 * @todo define exactly what it possible to do after this call.
 *
 * @returns 0 on success, KLDR_ERR_* on failure.
 * @param   pMod    The module.
 */
00510 int     kLdrModMostlyDone(PKLDRMOD pMod)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnMostlyDone(pMod);
}


/**
 * Maps the module into the memory of the caller.
 *
 * On success the actual addresses for the segments can be found in MapAddress
 * member of each segment in the segment array.
 *
 * @returns 0 on success, non-zero OS or kLdr status code on failure.
 * @param   pMod            The module to be mapped.
 * @remark  kLdr only supports one mapping at a time of a module.
 */
00527 int     kLdrModMap(PKLDRMOD pMod)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnMap(pMod);
}


/**
 * Unmaps a module previously mapped by kLdrModMap().
 *
 * @returns 0 on success, non-zero OS or kLdr status code on failure.
 * @param   pMod            The module to unmap.
 */
00540 int     kLdrModUnmap(PKLDRMOD pMod)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnUnmap(pMod);
}


/**
 * Allocates Thread Local Storage for module mapped by kLdrModMap().
 *
 * Calling kLdrModAllocTLS() more than once without calling kLdrModFreeTLS()
 * between each invocation is not supported.
 *
 * @returns 0 on success, non-zero OS or kLdr status code on failure.
 * @param   pMod            The module.
 */
00556 int     kLdrModAllocTLS(PKLDRMOD pMod)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnAllocTLS(pMod);
}


/**
 * Frees Thread Local Storage previously allocated by kLdrModAllocTLS().
 *
 * The caller is responsible for only calling kLdrModFreeTLS() once
 * after calling kLdrModAllocTLS().
 *
 * @returns 0 on success, non-zero OS or kLdr status code on failure.
 * @param   pMod            The module.
 */
00572 void    kLdrModFreeTLS(PKLDRMOD pMod)
{
    KLDRMOD_VALIDATE_VOID(pMod);
    pMod->pOps->pfnFreeTLS(pMod);
}


/**
 * Reloads all dirty pages in a module previously mapped by kLdrModMap().
 *
 * The module interpreter may omit code pages if it can safely apply code
 * fixups again in a subsequent kLdrModFixupMapping() call.
 *
 * The caller is responsible for freeing TLS before calling this function.
 *
 * @returns 0 on success, non-zero OS or kLdr status code on failure.
 * @param   pMod            The module.
 */
00590 int     kLdrModReload(PKLDRMOD pMod)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnReload(pMod);
}


/**
 * Fixup the mapping made by kLdrModMap().
 *
 * The caller is only responsible for not calling this function more than
 * once without doing kLDrModReload() inbetween.
 *
 * @returns 0 on success, non-zero OS or kLdr status code on failure.
 * @param   pMod            The module.
 * @param   pfnGetImport    The callback for resolving external (imported) symbols.
 * @param   pvUser          The callback user argument.
 */
00608 int     kLdrModFixupMapping(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnFixupMapping(pMod, pfnGetImport, pvUser);
}


/**
 * Call the module initializiation function of a mapped module (if any).
 *
 * @returns 0 on success or no init function, non-zero on init function failure or invalid pMod.
 * @param   pMod            The module.
 * @param   uHandle         The module handle to use if any of the init functions requires the module handle.
 */
00622 int     kLdrModCallInit(PKLDRMOD pMod, KUPTR uHandle)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnCallInit(pMod, uHandle);
}


/**
 * Call the module termination function of a mapped module (if any).
 *
 * @returns 0 on success or no term function, non-zero on invalid pMod.
 * @param   pMod            The module.
 * @param   uHandle         The module handle to use if any of the term functions requires the module handle.
 *
 * @remark  Termination function failure will be ignored by the module interpreter.
 */
00638 int     kLdrModCallTerm(PKLDRMOD pMod, KUPTR uHandle)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnCallTerm(pMod, uHandle);
}


/**
 * Call the thread attach or detach function of a mapped module (if any).
 *
 * Any per-thread TLS initialization/termination will have to be done at this time too.
 *
 * @returns 0 on success or no attach/detach function, non-zero on attach failure or invalid pMod.
 * @param   pMod            The module.
 * @param   uHandle         The module handle to use if any of the thread attach/detach functions
 *                          requires the module handle.
 *
 * @remark  Detach function failure will be ignored by the module interpreter.
 */
00657 int     kLdrModCallThread(PKLDRMOD pMod, KUPTR uHandle, unsigned fAttachingOrDetaching)
{
    KLDRMOD_VALIDATE(pMod);
    K_VALIDATE_FLAGS(fAttachingOrDetaching, 1);
    return pMod->pOps->pfnCallThread(pMod, uHandle, fAttachingOrDetaching);
}


/**
 * Get the size of the mapped module.
 *
 * @returns The size of the mapped module (in bytes).
 * @param   pMod            The module.
 */
00671 KLDRADDR kLdrModSize(PKLDRMOD pMod)
{
    KLDRMOD_VALIDATE_EX(pMod, 0);
    return pMod->pOps->pfnSize(pMod);
}


/**
 * Gets the module bits.
 *
 * The module interpreter will fill a mapping allocated by the caller with the
 * module bits reallocated to the specified address.
 *
 * @returns 0 on succes, non-zero OS or kLdr status code on failure.
 * @param   pMod            The module.
 * @param   pvBits          Where to put the bits.
 * @param   BaseAddress     The base address that should correspond to the first byte in pvBits
 *                          upon return.
 * @param   pfnGetImport    The callback ufor resolving external (imported) symbols.
 * @param   pvUser          The callback user argument.
 */
00692 int     kLdrModGetBits(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnGetBits(pMod, pvBits, BaseAddress, pfnGetImport, pvUser);
}


/**
 * Relocates the module bits previously obtained by kLdrModGetBits().
 *
 * @returns 0 on succes, non-zero OS or kLdr status code on failure.
 * @param   pMod            The module.
 * @param   pvBits          Where to put the bits.
 * @param   NewBaseAddress  The new base address.
 * @param   OldBaseAddress  The old base address (i.e. the one specified to kLdrModGetBits() or as
 *                          NewBaseAddressto the previous kLdrModRelocateBits() call).
 * @param   pfnGetImport    The callback ufor resolving external (imported) symbols.
 * @param   pvUser          The callback user argument.
 */
00711 int     kLdrModRelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
                            PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
{
    KLDRMOD_VALIDATE(pMod);
    return pMod->pOps->pfnRelocateBits(pMod, pvBits, NewBaseAddress, OldBaseAddress, pfnGetImport, pvUser);
}


Generated by  Doxygen 1.6.0   Back to index