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

kLdr.h

Go to the documentation of this file.
/* $Id: kLdr.h 29 2009-07-01 20:30:29Z bird $ */
/** @file
 * kLdr - The Dynamic Loader.
 */

/*
 * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef ___k_kLdr_h___
#define ___k_kLdr_h___

#ifdef __cplusplus
extern "C" {
#endif

/*
 * Include the base typedefs and macros.
 */
#include <k/kDefs.h>
#include <k/kTypes.h>
#include <k/kCpus.h>


/** @defgroup grp_kLdrBasic     kLdr Basic Types
 * @{ */

/** The kLdr address type. */
00050 typedef KU64 KLDRADDR;
/** Pointer to a kLdr address. */
00052 typedef KLDRADDR *PKLDRADDR;
/** Pointer to a const kLdr address. */
00054 typedef const KLDRADDR *PCKLDRADDR;

/** NIL address. */
00057 #define NIL_KLDRADDR    (~(KU64)0)

/** @def PRI_KLDRADDR
 * printf format type. */
#ifdef _MSC_VER
# define PRI_KLDRADDR    "I64x"
#else
00064 # define PRI_KLDRADDR    "llx"
#endif

/** Align a KSIZE value. */
00068 #define KLDR_ALIGN_ADDR(val, align) ( ((val) + ((align) - 1)) & ~(KLDRADDR)((align) - 1) )


/** The kLdr size type. */
00072 typedef KU64 KLDRSIZE;
/** Pointer to a kLdr size. */
00074 typedef KLDRSIZE *PKLDRSIZE;
/** Pointer to a const kLdr size. */
00076 typedef const KLDRSIZE *PCKLDRSIZE;

/** @def PRI_KLDRSIZE
 * printf format type. */
#ifdef _MSC_VER
# define PRI_KLDRSIZE    "I64x"
#else
00083 # define PRI_KLDRSIZE    "llx"
#endif


/** The kLdr file offset type. */
00088 typedef long KLDRFOFF;
/** Pointer to a kLdr file offset type. */
00090 typedef KLDRFOFF *PKLDRFOFF;
/** Pointer to a const kLdr file offset type. */
00092 typedef const KLDRFOFF *PCKLDRFOFF;

/** @def PRI_KLDRFOFF
 * printf format type. */
00096 #define PRI_KLDRFOFF     "lx"


/**
 * Union of all the integer types.
 */
00102 typedef union KLDRU
{
00104     KI8             i8;     /**< KI8 view. */
00105     KU8             u8;     /**< KU8 view. */
00106     KI16            i16;    /**< KI16 view. */
00107     KU16            u16;    /**< KU16 view. */
00108     KI32            i32;    /**< KI32 view. */
00109     KU32            u32;    /**< KU32 view. */
00110     KI64            i64;    /**< KI64 view. */
00111     KU64            u64;    /**< KU64 view. */

00113     KI8             ai8[8]; /**< KI8 array view . */
00114     KU8             au8[8]; /**< KU8 array view. */
00115     KI16            ai16[4];/**< KI16 array view . */
00116     KU16            au16[4];/**< KU16 array view. */
00117     KI32            ai32[2];/**< KI32 array view . */
00118     KU32            au32[2];/**< KU32 array view. */

00120     signed char     ch;     /**< signed char view. */
00121     unsigned char   uch;    /**< unsigned char view. */
00122     signed short    s;      /**< signed short view. */
00123     unsigned short  us;     /**< unsigned short view. */
00124     signed int      i;      /**< signed int view. */
00125     unsigned int    u;      /**< unsigned int view. */
00126     signed long     l;      /**< signed long view. */
00127     unsigned long   ul;     /**< unsigned long view. */
00128     void           *pv;     /**< void pointer view. */

00130     KLDRADDR        Addr;   /**< kLdr address view. */
00131     KLDRSIZE        Size;   /**< kLdr size view. */
} KLDRU;
/** Pointer to an integer union. */
00134 typedef KLDRU *PKLDRU;
/** Pointer to a const integer union. */
00136 typedef const KLDRU *PCKLDRU;


/**
 * Union of pointers to all the integer types.
 */
00142 typedef union KLDRPU
{
00144     KI8            *pi8;    /**< KI8 view. */
00145     KU8            *pu8;    /**< KU8 view. */
00146     KI16           *pi16;   /**< KI16 view. */
00147     KU16           *pu16;   /**< KU16 view. */
00148     KI32           *pi32;   /**< KI32 view. */
00149     KU32           *pu32;   /**< KU32 view. */
00150     KI64           *pi64;   /**< KI64 view. */
00151     KU64           *pu64;   /**< KU64 view. */

00153     signed char    *pch;    /**< signed char view. */
00154     unsigned char  *puch;   /**< unsigned char view. */
00155     signed short   *ps;     /**< signed short view. */
00156     unsigned short *pus;    /**< unsigned short view. */
00157     signed int     *pi;     /**< signed int view. */
00158     unsigned int   *pu;     /**< unsigned int view. */
00159     signed long    *pl;     /**< signed long view. */
00160     unsigned long  *pul;    /**< unsigned long view. */
00161     void           *pv;     /**< void pointer view. */
} KLDRPU;
/** Pointer to an integer pointer union. */
00164 typedef KLDRPU *PKLDRPU;
/** Pointer to a const integer pointer union. */
00166 typedef const KLDRPU *PCKLDRPU;

/** @} */


/** @defgroup grp_kLdrMod   kLdrMod - The executable image intepreter
 * @{ */

/**
 * Debug info type (from the loader point of view).
 */
00177 typedef enum KLDRDBGINFOTYPE
{
    /** The usual invalid enum value. */
00180     KLDRDBGINFOTYPE_INVALID = 0,
    /** Unknown debug info format. */
00182     KLDRDBGINFOTYPE_UNKNOWN,
    /** Stabs. */
00184     KLDRDBGINFOTYPE_STABS,
    /** Debug With Arbitrary Record Format (DWARF). */
00186     KLDRDBGINFOTYPE_DWARF,
    /** Microsoft Codeview debug info. */
00188     KLDRDBGINFOTYPE_CODEVIEW,
    /** Watcom debug info. */
00190     KLDRDBGINFOTYPE_WATCOM,
    /** IBM High Level Language debug info.. */
00192     KLDRDBGINFOTYPE_HLL,
    /** The end of the valid debug info values (exclusive). */
00194     KLDRDBGINFOTYPE_END,
    /** Blow the type up to 32-bit. */
00196     KLDRDBGINFOTYPE_32BIT_HACK = 0x7fffffff
} KLDRDBGINFOTYPE;
/** Pointer to a kLdr debug info type. */
00199 typedef KLDRDBGINFOTYPE *PKLDRDBGINFOTYPE;


/**
 * Stack information.
 */
00205 typedef struct KLDRSTACKINFO
{
    /** The base address of the stack (sub) segment.
     * Set this to NIL_KLDRADDR if the module doesn't include any stack segment. */
00209     KLDRADDR        Address;
    /** The base address of the stack (sub) segment, link address.
     * Set this to NIL_KLDRADDR if the module doesn't include any stack (sub)segment. */
00212     KLDRADDR        LinkAddress;
    /** The stack size of the main thread.
     * If no stack (sub)segment in the module, this is the stack size of the main thread.
     * If the module doesn't contain this kind of information this field will be set to 0. */
00216     KLDRSIZE        cbStack;
    /** The stack size of non-main threads.
     * If the module doesn't contain this kind of information this field will be set to 0. */
00219     KLDRSIZE        cbStackThread;
} KLDRSTACKINFO;
/** Pointer to stack information. */
00222 typedef KLDRSTACKINFO *PKLDRSTACKINFO;
/** Pointer to const stack information. */
00224 typedef const KLDRSTACKINFO *PCKLDRSTACKINFO;


/**
 * Loader segment.
 */
00230 typedef struct KLDRSEG
{
    /** Variable free to use for the kLdr user. */
00233     void           *pvUser;
    /** The segment name. (Might not be zero terminated!) */
00235     const char     *pchName;
    /** The length of the segment name. */
00237     KU32            cchName;
    /** The flat selector to use for the segment (i.e. data/code).
     * Primarily a way for the user to specify selectors for the LX/LE and NE interpreters. */
00240     KU16            SelFlat;
    /** The 16-bit selector to use for the segment.
     * Primarily a way for the user to specify selectors for the LX/LE and NE interpreters. */
00243     KU16            Sel16bit;
    /** Segment flags. */
00245     KU32            fFlags;
    /** The segment protection. */
00247     KPROT           enmProt;
    /** The size of the segment. */
00249     KLDRSIZE        cb;
    /** The required segment alignment.
     * The to 0 if the segment isn't supposed to be mapped. */
00252     KLDRADDR        Alignment;
    /** The link address.
     * Set to NIL_KLDRADDR if the segment isn't supposed to be
     * mapped or if the image doesn't have link addresses. */
00256     KLDRADDR        LinkAddress;
    /** File offset of the segment.
     * Set to -1 if no file backing (like BSS). */
00259     KLDRFOFF        offFile;
    /** Size of the file bits of the segment.
     * Set to -1 if no file backing (like BSS). */
00262     KLDRFOFF        cbFile;
    /** The relative virtual address when mapped.
     * Set to NIL_KLDRADDR if the segment isn't supposed to be mapped. */
00265     KLDRADDR        RVA;
    /** The size of the segment including the alignment gap up to the next segment when mapped. */
00267     KSIZE           cbMapped;
    /** The address the segment was mapped at by kLdrModMap().
     * Set to 0 if not mapped. */
00270     KUPTR           MapAddress;
} KLDRSEG;


/** @name Segment flags
 * @{ */
/** The segment is 16-bit. When not set the default of the target architecture is assumed. */
00277 #define KLDRSEG_FLAG_16BIT          1
/** The segment requires a 16-bit selector alias. (OS/2) */
00279 #define KLDRSEG_FLAG_OS2_ALIAS16    2
/** Conforming segment (x86 weirdness). (OS/2) */
00281 #define KLDRSEG_FLAG_OS2_CONFORM    4
/** IOPL (ring-2) segment. (OS/2) */
00283 #define KLDRSEG_FLAG_OS2_IOPL       8
/** @} */


/**
 * Loader module format.
 */
00290 typedef enum KLDRFMT
{
    /** The usual invalid 0 format. */
00293     KLDRFMT_INVALID = 0,
    /** The native OS loader. */
00295     KLDRFMT_NATIVE,
    /** The AOUT loader. */
00297     KLDRFMT_AOUT,
    /** The ELF loader. */
00299     KLDRFMT_ELF,
    /** The LX loader. */
00301     KLDRFMT_LX,
    /** The Mach-O loader. */
00303     KLDRFMT_MACHO,
    /** The PE loader. */
00305     KLDRFMT_PE,
    /** The end of the valid format values (exclusive). */
00307     KLDRFMT_END,
    /** Hack to blow the type up to 32-bit. */
00309     KLDRFMT_32BIT_HACK = 0x7fffffff
} KLDRFMT;


/**
 * Loader module type.
 */
00316 typedef enum KLDRTYPE
{
    /** The usual invalid 0 type. */
00319     KLDRTYPE_INVALID = 0,
    /** Object file. */
00321     KLDRTYPE_OBJECT,
    /** Executable module, fixed load address. */
00323     KLDRTYPE_EXECUTABLE_FIXED,
    /** Executable module, relocatable, non-fixed load address. */
00325     KLDRTYPE_EXECUTABLE_RELOCATABLE,
    /** Executable module, position independent code, non-fixed load address. */
00327     KLDRTYPE_EXECUTABLE_PIC,
    /** Shared library, fixed load address.
     * Typically a system library. */
00330     KLDRTYPE_SHARED_LIBRARY_FIXED,
    /** Shared library, relocatable, non-fixed load address. */
00332     KLDRTYPE_SHARED_LIBRARY_RELOCATABLE,
    /** Shared library, position independent code, non-fixed load address. */
00334     KLDRTYPE_SHARED_LIBRARY_PIC,
    /** DLL that contains no code or data only imports and exports. (Chiefly OS/2.) */
00336     KLDRTYPE_FORWARDER_DLL,
    /** Core or dump. */
00338     KLDRTYPE_CORE,
    /** The end of the valid types values (exclusive). */
00340     KLDRTYPE_END,
    /** Hack to blow the type up to 32-bit. */
00342     KLDRTYPE_32BIT_HACK = 0x7fffffff
} KLDRTYPE;


/**
 * Loader endian indicator.
 */
00349 typedef enum KLDRENDIAN
{
    /** The usual invalid endian. */
00352     KLDRENDIAN_INVALID,
    /** Little endian. */
00354     KLDRENDIAN_LITTLE,
    /** Bit endian. */
00356     KLDRENDIAN_BIG,
    /** Endianness doesn't have a meaning in the context. */
00358     KLDRENDIAN_NA,
    /** The end of the valid endian values (exclusive). */
00360     KLDRENDIAN_END,
    /** Hack to blow the type up to 32-bit. */
00362     KLDRENDIAN_32BIT_HACK = 0x7fffffff
} KLDRENDIAN;


/** Pointer to a module interpreter method table. */
00367 typedef struct KLDRMODOPS *PKLDRMODOPS;
/** Pointer to const module interpreter methods table. */
00369 typedef const struct KLDRMODOPS *PCKLDRMODOPS;

/**
 * Module interpreter instance.
 * All members are read only unless you're kLdrMod or the module interpreter.
 */
00375 typedef struct KLDRMOD
{
    /** Magic number (KLDRMOD_MAGIC). */
00378     KU32                u32Magic;
    /** The format of this module. */
00380     KLDRFMT             enmFmt;
    /** The type of module. */
00382     KLDRTYPE            enmType;
    /** The CPU architecture this module was built for. */
00384     KCPUARCH            enmArch;
    /** The minium cpu this module was built for.
     * This might not be accurate, so use kLdrModCanExecuteOn() to check. */
00387     KCPU                enmCpu;
    /** The endian used by the module. */
00389     KLDRENDIAN          enmEndian;
    /** The filename length (bytes). */
00391     KU32                cchFilename;
    /** The filename. */
00393     const char         *pszFilename;
    /** The module name. */
00395     const char         *pszName;
    /** The module name length (bytes). */
00397     KU32                cchName;
    /** The number of segments in the module. */
00399     KU32                cSegments;
    /** Pointer to the loader methods.
     * Not meant for calling directly thru! */
00402     PCKLDRMODOPS        pOps;
    /** Pointer to the read instance. (Can be NULL after kLdrModDone().)*/
00404     PKRDR               pRdr;
    /** The module data. */
00406     void               *pvData;
    /** Segments. (variable size, can be zero) */
00408     KLDRSEG             aSegments[1];
} KLDRMOD, *PKLDRMOD, **PPKLDRMOD;

/** The magic for KLDRMOD::u32Magic. (Kosuke Fujishima) */
00412 #define KLDRMOD_MAGIC   0x19640707


/** Special base address value alias for the link address. */
00416 #define KLDRMOD_BASEADDRESS_LINK            (~(KLDRADDR)1)
/** Special base address value alias for the actual load address (must be mapped). */
00418 #define KLDRMOD_BASEADDRESS_MAP             (~(KLDRADDR)2)

/** Special import module ordinal value used to indicate that there is no
 * specific module associated with the requested symbol. */
00422 #define NIL_KLDRMOD_IMPORT                  (~(KU32)0)

/** Special symbol ordinal value used to indicate that the symbol
 * only has a string name. */
00426 #define NIL_KLDRMOD_SYM_ORDINAL             (~(KU32)0)


/** @name Load symbol kind flags.
 * @{ */
/** The bitness doesn't matter. */
00432 #define KLDRSYMKIND_NO_BIT                  0x00000000
/** 16-bit symbol. */
00434 #define KLDRSYMKIND_16BIT                   0x00000001
/** 32-bit symbol. */
00436 #define KLDRSYMKIND_32BIT                   0x00000002
/** 64-bit symbol. */
00438 #define KLDRSYMKIND_64BIT                   0x00000003
/** Mask out the bit.*/
00440 #define KLDRSYMKIND_BIT_MASK                0x00000003
/** We don't know the type of symbol. */
00442 #define KLDRSYMKIND_NO_TYPE                 0x00000000
/** The symbol is a code object (method/function/procedure/whateveryouwannacallit). */
00444 #define KLDRSYMKIND_CODE                    0x00000010
/** The symbol is a data object. */
00446 #define KLDRSYMKIND_DATA                    0x00000020
/** Mask out the symbol type. */
00448 #define KLDRSYMKIND_TYPE_MASK               0x00000030
/** Valid symbol kind mask. */
00450 #define KLDRSYMKIND_MASK                    0x00000033
/** Weak symbol. */
00452 #define KLDRSYMKIND_WEAK                    0x00000100
/** Forwarder symbol. */
00454 #define KLDRSYMKIND_FORWARDER               0x00000200
/** Request a flat symbol address. */
00456 #define KLDRSYMKIND_REQ_FLAT                0x00000000
/** Request a segmented symbol address. */
00458 #define KLDRSYMKIND_REQ_SEGMENTED           0x40000000
/** Request type mask. */
00460 #define KLDRSYMKIND_REQ_TYPE_MASK           0x40000000
/** @} */

/** @name kLdrModEnumSymbols flags.
 * @{ */
/** Returns ALL kinds of symbols. The default is to only return public/exported symbols. */
00466 #define KLDRMOD_ENUM_SYMS_FLAGS_ALL         0x00000001
/** @} */


/**
 * Callback for resolving imported symbols when applying fixups.
 *
 * @returns 0 on success and *pValue and *pfKind filled.
 * @returns Non-zero OS specific or kLdr status code on failure.
 *
 * @param   pMod        The module which fixups are begin applied.
 * @param   iImport     The import module ordinal number or NIL_KLDRMOD_IMPORT.
 * @param   iSymbol     The symbol ordinal number or NIL_KLDRMOD_SYM_ORDINAL.
 * @param   pchSymbol   The symbol name. Can be NULL if iSymbol isn't nil. Doesn't have to be null-terminated.
 * @param   cchSymbol   The length of the symbol.
 * @param   pszVersion  The symbol version. NULL if not versioned.
 * @param   puValue     Where to store the symbol value.
 * @param   pfKind      Where to store the symbol kind flags.
 * @param   pvUser      The user parameter specified to the relocation function.
 */
00486 typedef int FNKLDRMODGETIMPORT(PKLDRMOD pMod, KU32 iImport, KU32 iSymbol, const char *pchSymbol, KSIZE cchSymbol,
                               const char *pszVersion, PKLDRADDR puValue, KU32 *pfKind, void *pvUser);
/** Pointer to a import callback. */
00489 typedef FNKLDRMODGETIMPORT *PFNKLDRMODGETIMPORT;

/**
 * Symbol enumerator callback.
 *
 * @returns 0 if enumeration should continue.
 * @returns non-zero if the enumeration should stop. This status code will then be returned by kLdrModEnumSymbols().
 *
 * @param   pMod        The module which symbols are being enumerated.s
 * @param   iSymbol     The symbol ordinal number or NIL_KLDRMOD_SYM_ORDINAL.
 * @param   pchSymbol   The symbol name. This can be NULL if there is a symbol ordinal.
 *                      This can also be an empty string if the symbol doesn't have a name
 *                      or it's name has been stripped.
 *                      Important, this doesn't have to be a null-terminated string.
 * @param   cchSymbol   The length of the symbol.
 * @param   pszVersion  The symbol version. NULL if not versioned.
 * @param   uValue      The symbol value.
 * @param   fKind       The symbol kind flags.
 * @param   pvUser      The user parameter specified to kLdrModEnumSymbols().
 */
00509 typedef int FNKLDRMODENUMSYMS(PKLDRMOD pMod, KU32 iSymbol, const char *pchSymbol, KSIZE cchSymbol, const char *pszVersion,
                              KLDRADDR uValue, KU32 fKind, void *pvUser);
/** Pointer to a symbol enumerator callback. */
00512 typedef FNKLDRMODENUMSYMS *PFNKLDRMODENUMSYMS;

/**
 * Debug info enumerator callback.
 *
 * @returns 0 to continue the enumeration.
 * @returns non-zero if the enumeration should stop. This status code will then be returned by kLdrModEnumDbgInfo().
 *
 * @param   pMod        The module.
 * @param   iDbgInfo    The debug info ordinal number / id.
 * @param   enmType     The debug info type.
 * @param   iMajorVer   The major version number of the debug info format. -1 if unknow - implies invalid iMinorVer.
 * @param   iMinorVer   The minor version number of the debug info format. -1 when iMajorVer is -1.
 * @param   offFile     The file offset *if* this type has one specific location in the executable image file.
 *                      This is -1 if there isn't any specific file location.
 * @param   LinkAddress The link address of the debug info if it's loadable. NIL_KLDRADDR if not loadable.
 * @param   cb          The size of the debug information. -1 is used if this isn't applicable.
 * @param   pszExtFile  This points to the name of an external file containing the debug info.
 *                      This is NULL if there isn't any external file.
 * @param   pvUser      The user parameter specified to kLdrModEnumDbgInfo.
 */
00533 typedef int FNKLDRENUMDBG(PKLDRMOD pMod, KU32 iDbgInfo, KLDRDBGINFOTYPE enmType, KI16 iMajorVer, KI16 iMinorVer,
                          KLDRFOFF offFile, KLDRADDR LinkAddress, KLDRSIZE cb, const char *pszExtFile, void *pvUser);
/** Pointer to a debug info enumerator callback. */
00536 typedef FNKLDRENUMDBG *PFNKLDRENUMDBG;

/**
 * Resource enumerator callback.
 *
 * @returns 0 to continue the enumeration.
 * @returns non-zero if the enumeration should stop. This status code will then be returned by kLdrModEnumResources().
 *
 * @param   pMod        The module.
 * @param   idType      The resource type id. NIL_KLDRMOD_RSRC_TYPE_ID if no type id.
 * @param   pszType     The resource type name. NULL if no type name.
 * @param   idName      The resource id. NIL_KLDRMOD_RSRC_NAME_ID if no id.
 * @param   pszName     The resource name. NULL if no name.
 * @param   idLang      The language id.
 * @param   AddrRsrc    The address value for the resource.
 * @param   cbRsrc      The size of the resource.
 * @param   pvUser      The user parameter specified to kLdrModEnumDbgInfo.
 */
00554 typedef int FNKLDRENUMRSRC(PKLDRMOD pMod, KU32 idType, const char *pszType, KU32 idName, const char *pszName,
                           KU32 idLang, KLDRADDR AddrRsrc, KLDRSIZE cbRsrc, void *pvUser);
/** Pointer to a resource enumerator callback. */
00557 typedef FNKLDRENUMRSRC *PFNKLDRENUMRSRC;

/** NIL resource name ID. */
00560 #define NIL_KLDRMOD_RSRC_NAME_ID    ( ~(KU32)0 )
/** NIL resource type ID. */
00562 #define NIL_KLDRMOD_RSRC_TYPE_ID    ( ~(KU32)0 )
/** @name Language ID
 *
 * Except for the special IDs #defined here, the values are considered
 * format specific for now since it's only used by the PE resources.
 *
 * @{ */
/** NIL language ID. */
00570 #define NIL_KLDR_LANG_ID                ( ~(KU32)0 )
/** Special language id value for matching any language. */
00572 #define KLDR_LANG_ID_ANY                ( ~(KU32)1 )
/** Special language id value indicating language neutral. */
00574 #define KLDR_LANG_ID_NEUTRAL            ( ~(KU32)2 )
/** Special language id value indicating user default language. */
00576 #define KLDR_LANG_ID_USER_DEFAULT       ( ~(KU32)3 )
/** Special language id value indicating system default language. */
00578 #define KLDR_LANG_ID_SYS_DEFAULT        ( ~(KU32)4 )
/** Special language id value indicating default custom locale. */
00580 #define KLDR_LANG_ID_CUSTOM_DEFAULT     ( ~(KU32)5 )
/** Special language id value indicating unspecified custom locale. */
00582 #define KLDR_LANG_ID_CUSTOM_UNSPECIFIED ( ~(KU32)6 )
/** Special language id value indicating default custom MUI locale. */
00584 #define KLDR_LANG_ID_UI_CUSTOM_DEFAULT  ( ~(KU32)7 )
/** @} */


int     kLdrModOpen(const char *pszFilename, KU32 fFlags, KCPUARCH enmCpuArch, PPKLDRMOD ppMod);
int     kLdrModOpenFromRdr(PKRDR pRdr, KU32 fFlags, KCPUARCH enmCpuArch, PPKLDRMOD ppMod);
int     kLdrModOpenNative(const char *pszFilename, PPKLDRMOD ppMod);
int     kLdrModOpenNativeByHandle(KUPTR uHandle, PPKLDRMOD ppMod);
int     kLdrModClose(PKLDRMOD pMod);

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);
int     kLdrModEnumSymbols(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress,
                           KU32 fFlags, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser);
int     kLdrModGetImport(PKLDRMOD pMod, const void *pvBits, KU32 iImport, char *pszName, KSIZE cchName);
KI32    kLdrModNumberOfImports(PKLDRMOD pMod, const void *pvBits);
int     kLdrModCanExecuteOn(PKLDRMOD pMod, const void *pvBits, KCPUARCH enmArch, KCPU enmCpu);
int     kLdrModGetStackInfo(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo);
int     kLdrModQueryMainEntrypoint(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress);
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);
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);
int     kLdrModEnumDbgInfo(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser);
int     kLdrModHasDbgInfo(PKLDRMOD pMod, const void *pvBits);
int     kLdrModMostlyDone(PKLDRMOD pMod);


/** @name Operations On The Internally Managed Mapping
 * @{ */
int     kLdrModMap(PKLDRMOD pMod);
int     kLdrModUnmap(PKLDRMOD pMod);
int     kLdrModAllocTLS(PKLDRMOD pMod);
void    kLdrModFreeTLS(PKLDRMOD pMod);
int     kLdrModReload(PKLDRMOD pMod);
int     kLdrModFixupMapping(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
int     kLdrModCallInit(PKLDRMOD pMod, KUPTR uHandle);
int     kLdrModCallTerm(PKLDRMOD pMod, KUPTR uHandle);
int     kLdrModCallThread(PKLDRMOD pMod, KUPTR uHandle, unsigned fAttachingOrDetaching);
/** @} */

/** @name Operations On The Externally Managed Mappings
 * @{ */
KLDRADDR kLdrModSize(PKLDRMOD pMod);
int     kLdrModGetBits(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
int     kLdrModRelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
                            PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
/** @} */


/**
 * The loader module operation.
 */
00638 typedef struct KLDRMODOPS
{
    /** The name of this module interpreter. */
00641     const char         *pszName;
    /** Pointer to the next module interpreter. */
00643     PCKLDRMODOPS        pNext;

    /**
     * Create a loader module instance interpreting the executable image found
     * in the specified 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   pOps            Pointer to the registered method table.
     * @param   pRdr            The file provider instance to use.
     * @param   fFlags          Flags, MBZ.
     * @param   enmCpuArch      The desired CPU architecture. KCPUARCH_UNKNOWN means
     *                          anything goes, but with a preference for the current
     *                          host architecture.
     * @param   offNewHdr       The offset of the new header in MZ files. -1 if not found.
     * @param   ppMod           Where to store the module instance pointer.
     */
    int (* pfnCreate)(PCKLDRMODOPS pOps, PKRDR pRdr, KU32 fFlags, KCPUARCH enmCpuArch, KLDRFOFF offNewHdr, PPKLDRMOD ppMod);
    /**
     * Destroys an loader module instance.
     *
     * The caller is responsible for calling kLdrModUnmap() and kLdrFreeTLS() first.
     *
     * @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.
     */
    int (* pfnDestroy)(PKLDRMOD pMod);

    /** @copydoc kLdrModQuerySymbol */
    int (* pfnQuerySymbol)(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);
    /** @copydoc kLdrModEnumSymbols */
    int (* pfnEnumSymbols)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 fFlags,
                           PFNKLDRMODENUMSYMS pfnCallback, void *pvUser);
    /** @copydoc kLdrModGetImport */
    int (* pfnGetImport)(PKLDRMOD pMod, const void *pvBits, KU32 iImport, char *pszName, KSIZE cchName);
    /** @copydoc kLdrModNumberOfImports */
    KI32 (* pfnNumberOfImports)(PKLDRMOD pMod, const void *pvBits);
    /** @copydoc kLdrModCanExecuteOn */
    int (* pfnCanExecuteOn)(PKLDRMOD pMod, const void *pvBits, KCPUARCH enmArch, KCPU enmCpu);
    /** @copydoc kLdrModGetStackInfo */
    int (* pfnGetStackInfo)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo);
    /** @copydoc kLdrModQueryMainEntrypoint */
    int (* pfnQueryMainEntrypoint)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress);
    /** @copydoc kLdrModQueryResource */
    int (* pfnQueryResource)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 idType, const char *pszType,
                             KU32 idName, const char *pszName, KU32 idLang, PKLDRADDR pAddrRsrc, KSIZE *pcbRsrc);
    /** @copydoc kLdrModEnumResources */
    int (* pfnEnumResources)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 idType, const char *pszType,
                             KU32 idName, const char *pszName, KU32 idLang, PFNKLDRENUMRSRC pfnCallback, void *pvUser);
    /** @copydoc kLdrModEnumDbgInfo */
    int (* pfnEnumDbgInfo)(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser);
    /** @copydoc kLdrModHasDbgInfo */
    int (* pfnHasDbgInfo)(PKLDRMOD pMod, const void *pvBits);
    /** @copydoc kLdrModMap */
    int (* pfnMap)(PKLDRMOD pMod);
    /** @copydoc kLdrModUnmap */
    int (* pfnUnmap)(PKLDRMOD pMod);
    /** @copydoc kLdrModAllocTLS */
    int (* pfnAllocTLS)(PKLDRMOD pMod);
    /** @copydoc kLdrModFreeTLS */
    void (*pfnFreeTLS)(PKLDRMOD pMod);
    /** @copydoc kLdrModReload */
    int (* pfnReload)(PKLDRMOD pMod);
    /** @copydoc kLdrModFixupMapping */
    int (* pfnFixupMapping)(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
    /** @copydoc kLdrModCallInit */
    int (* pfnCallInit)(PKLDRMOD pMod, KUPTR uHandle);
    /** @copydoc kLdrModCallTerm */
    int (* pfnCallTerm)(PKLDRMOD pMod, KUPTR uHandle);
    /** @copydoc kLdrModCallThread */
    int (* pfnCallThread)(PKLDRMOD pMod, KUPTR uHandle, unsigned fAttachingOrDetaching);
    /** @copydoc kLdrModSize */
    KLDRADDR (* pfnSize)(PKLDRMOD pMod);
    /** @copydoc kLdrModGetBits */
    int (* pfnGetBits)(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
    /** @copydoc kLdrModRelocateBits */
    int (* pfnRelocateBits)(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
                            PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
    /** @copydoc kLdrModMostlyDone */
    int (* pfnMostlyDone)(PKLDRMOD pMod);
    /** Dummy which should be assigned a non-zero value. */
00727     KU32 uEndOfStructure;
} KLDRMODOPS;


/** @} */




/** @defgroup grp_kLdrDyld   kLdrDyld - The dynamic loader
 * @{ */

/** The handle to a dynamic loader module. */
00740 typedef struct KLDRDYLDMOD *HKLDRMOD;
/** Pointer to the handle to a dynamic loader module. */
00742 typedef HKLDRMOD *PHKLDRMOD;
/** NIL handle value. */
00744 #define NIL_HKLDRMOD    ((HKLDRMOD)0)


/**
 * File search method.
 *
 * In addition to it's own way of finding files, kLdr emulates
 * the methods employed by the most popular systems.
 */
00753 typedef enum KLDRDYLDSEARCH
{
    /** The usual invalid file search method. */
00756     KLDRDYLD_SEARCH_INVALID = 0,
    /** Uses the kLdr file search method.
     * @todo invent me. */
00759     KLDRDYLD_SEARCH_KLDR,
    /** Use the emulation closest to the host system. */
00761     KLDRDYLD_SEARCH_HOST,
    /** Emulate the OS/2 file search method.
     * On non-OS/2 systems, BEGINLIBPATH, LIBPATH, ENDLIBPATH and LIBPATHSTRICT are
     * taken form the environment. */
00765     KLDRDYLD_SEARCH_OS2,
    /** Emulate the standard window file search method. */
00767     KLDRDYLD_SEARCH_WINDOWS,
    /** Emulate the alternative window file search method. */
00769     KLDRDYLD_SEARCH_WINDOWS_ALTERED,
    /** Emulate the most common UNIX file search method. */
00771     KLDRDYLD_SEARCH_UNIX_COMMON,
    /** End of the valid file search method values. */
00773     KLDRDYLD_SEARCH_END,
    /** Hack to blow the type up to 32-bit. */
00775     KLDRDYLD_SEARCH_32BIT_HACK = 0x7fffffff
} KLDRDYLDSEARCH;

/** @name kLdrDyldLoad and kLdrDyldFindByName flags.
 * @{ */
/** The symbols in the module should be loaded into the global unix namespace.
 * If not specified, the symbols are local and can only be referenced directly. */
00782 #define KLDRYDLD_LOAD_FLAGS_GLOBAL_SYMBOLS      0x00000001
/** The symbols in the module should be loaded into the global unix namespace and
 * it's symbols should take precedence over all currently loaded modules.
 * This implies KLDRYDLD_LOAD_FLAGS_GLOBAL_SYMBOLS. */
00786 #define KLDRYDLD_LOAD_FLAGS_DEEP_SYMBOLS        0x00000002
/** The module shouldn't be found by a global module search.
 * If not specified, the module can be found by unspecified module searches,
 * typical used when loading import/dep modules. */
00790 #define KLDRYDLD_LOAD_FLAGS_SPECIFIC_MODULE     0x00000004
/** Do a recursive initialization calls instead of defering them to the outermost call. */
00792 #define KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT      0x00000008
/** We're loading the executable module.
 * @internal */
00795 #define KLDRDYLD_LOAD_FLAGS_EXECUTABLE          0x40000000
/** @} */


int     kLdrDyldLoad(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
                     unsigned fFlags, PHKLDRMOD phMod, char *pszErr, KSIZE cchErr);
int     kLdrDyldUnload(HKLDRMOD hMod);
int     kLdrDyldFindByName(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
                           unsigned fFlags, PHKLDRMOD phMod);
int     kLdrDyldFindByAddress(KUPTR Address, PHKLDRMOD phMod, KU32 *piSegment, KUPTR *poffSegment);
int     kLdrDyldGetName(HKLDRMOD hMod, char *pszName, KSIZE cchName);
int     kLdrDyldGetFilename(HKLDRMOD hMod, char *pszFilename, KSIZE cchFilename);
int     kLdrDyldQuerySymbol(HKLDRMOD hMod, KU32 uSymbolOrdinal, const char *pszSymbolName,
                            const char *pszSymbolVersion, KUPTR *pValue, KU32 *pfKind);
int     kLdrDyldQueryResource(HKLDRMOD hMod, KU32 idType, const char *pszType, KU32 idName,
                              const char *pszName, KU32 idLang, void **pvRsrc, KSIZE *pcbRsrc);
int     kLdrDyldEnumResources(HKLDRMOD hMod, KU32 idType, const char *pszType, KU32 idName,
                              const char *pszName, KU32 idLang, PFNKLDRENUMRSRC pfnCallback, void *pvUser);


/** @name OS/2 like API
 * @{ */
#if defined(__OS2__)
# define KLDROS2API _System
#else
# define KLDROS2API
#endif
int     kLdrDosLoadModule(char *pszObject, KSIZE cbObject, const char *pszModule, PHKLDRMOD phMod);
int     kLdrDosFreeModule(HKLDRMOD hMod);
int     kLdrDosQueryModuleHandle(const char *pszModname, PHKLDRMOD phMod);
int     kLdrDosQueryModuleName(HKLDRMOD hMod, KSIZE cchName, char *pszName);
int     kLdrDosQueryProcAddr(HKLDRMOD hMod, KU32 iOrdinal, const char *pszProcName, void **ppvProcAddr);
int     kLdrDosQueryProcType(HKLDRMOD hMod, KU32 iOrdinal, const char *pszProcName, KU32 *pfProcType);
int     kLdrDosQueryModFromEIP(PHKLDRMOD phMod, KU32 *piObject, KSIZE cbName, char *pszName, KUPTR *poffObject, KUPTR ulEIP);
int     kLdrDosReplaceModule(const char *pszOldModule, const char *pszNewModule, const char *pszBackupModule);
int     kLdrDosGetResource(HKLDRMOD hMod, KU32 idType, KU32 idName, void **pvResAddr);
int     kLdrDosQueryResourceSize(HKLDRMOD hMod, KU32 idType, KU32 idName, KU32 *pcb);
int     kLdrDosFreeResource(void *pvResAddr);
/** @} */

/** @name POSIX like API
 * @{ */
HKLDRMOD    kLdrDlOpen(const char *pszLibrary, int fFlags);
const char *kLdrDlError(void);
void *      kLdrDlSym(HKLDRMOD hMod, const char *pszSymbol);
int         kLdrDlClose(HKLDRMOD hMod);
/** @todo GNU extensions */
/** @} */

/** @name Win32 like API
 * @{ */
#if defined(_MSC_VER)
# define KLDRWINAPI __stdcall
#else
# define KLDRWINAPI
#endif
HKLDRMOD KLDRWINAPI kLdrWLoadLibrary(const char *pszFilename);
HKLDRMOD KLDRWINAPI kLdrWLoadLibraryEx(const char *pszFilename, void *hFileReserved, KU32 fFlags);
KU32     KLDRWINAPI kLdrWGetModuleFileName(HKLDRMOD hMod, char *pszModName, KSIZE cchModName);
HKLDRMOD KLDRWINAPI kLdrWGetModuleHandle(const char *pszFilename);
int      KLDRWINAPI kLdrWGetModuleHandleEx(KU32 fFlags, const char *pszFilename, HKLDRMOD hMod);
void *   KLDRWINAPI kLdrWGetProcAddress(HKLDRMOD hMod, const char *pszProcName);
KU32     KLDRWINAPI kLdrWGetDllDirectory(KSIZE cchDir, char *pszDir);
int      KLDRWINAPI kLdrWSetDllDirectory(const char *pszDir);
int      KLDRWINAPI kLdrWFreeLibrary(HKLDRMOD hMod);
int      KLDRWINAPI kLdrWDisableThreadLibraryCalls(HKLDRMOD hMod);

/** The handle to a resource that's been found. */
00863 typedef struct KLDRWRSRCFOUND *HKLDRWRSRCFOUND;
/** The handle to a loaded resource. */
00865 typedef struct KLDRWRSRCLOADED *HKLDRWRSRCLOADED;
HKLDRWRSRCFOUND  KLDRWINAPI kLdrWFindResource(HKLDRMOD hMod, const char *pszType, const char *pszName);
HKLDRWRSRCFOUND  KLDRWINAPI kLdrWFindResourceEx(HKLDRMOD hMod, const char *pszType, const char *pszName, KU16 idLang);
KU32             KLDRWINAPI kLdrWSizeofResource(HKLDRMOD hMod, HKLDRWRSRCFOUND hFoundRsrc);
HKLDRWRSRCLOADED KLDRWINAPI kLdrWLoadResource(HKLDRMOD hMod, HKLDRWRSRCFOUND hFoundRsrc);
void    *KLDRWINAPI kLdrWLockResource(HKLDRMOD hMod, HKLDRWRSRCLOADED hLoadedRsrc);
int      KLDRWINAPI kLdrWFreeResource(HKLDRMOD hMod, HKLDRWRSRCLOADED hLoadedRsrc);

typedef int (KLDRWINAPI *PFNKLDRWENUMRESTYPE)(HKLDRMOD hMod, const char *pszType, KUPTR uUser);
int      KLDRWINAPI kLdrWEnumResourceTypes(HKLDRMOD hMod, PFNKLDRWENUMRESTYPE pfnEnum, KUPTR uUser);
int      KLDRWINAPI kLdrWEnumResourceTypesEx(HKLDRMOD hMod, PFNKLDRWENUMRESTYPE pfnEnum, KUPTR uUser, KU32 fFlags, KU16 idLang);

typedef int (KLDRWINAPI *PFNKLDRWENUMRESNAME)(HKLDRMOD hMod, const char *pszType, char *pszName, KUPTR uUser);
int      KLDRWINAPI kLdrWEnumResourceNames(HKLDRMOD hMod, const char *pszType, PFNKLDRWENUMRESNAME pfnEnum, KUPTR uUser);
int      KLDRWINAPI kLdrWEnumResourceNamesEx(HKLDRMOD hMod, const char *pszType, PFNKLDRWENUMRESNAME pfnEnum, KUPTR uUser, KU32 fFlags, KU16 idLang);

typedef int (KLDRWINAPI *PFNKLDRWENUMRESLANG)(HKLDRMOD hMod, const char *pszType, const char *pszName, KU16 idLang, KUPTR uUser);
int      KLDRWINAPI kLdrWEnumResourceLanguages(HKLDRMOD hMod, const char *pszType, const char *pszName, PFNKLDRWENUMRESLANG pfnEnum, KUPTR uUser);
int      KLDRWINAPI kLdrWEnumResourceLanguagesEx(HKLDRMOD hMod, const char *pszType, const char *pszName,
                                                 PFNKLDRWENUMRESLANG pfnEnum, KUPTR uUser, KU32 fFlags, KU16 idLang);
/** @} */


/** @name Process Bootstrapping
 * @{ */

/**
 * Argument package from the stub.
 */
00894 typedef struct KLDREXEARGS
{
    /** Load & search flags, some which will become defaults. */
00897     KU32            fFlags;
    /** The default search method. */
00899     KLDRDYLDSEARCH  enmSearch;
    /** The executable file that the stub is supposed to load. */
00901     char            szExecutable[260];
    /** The default prefix used when searching for DLLs. */
00903     char            szDefPrefix[16];
    /** The default suffix used when searching for DLLs. */
00905     char            szDefSuffix[16];
    /** The LD_LIBRARY_PATH prefix for the process.. */
00907     char            szLibPath[4096 - sizeof(KU32) - sizeof(KLDRDYLDSEARCH) - 16 - 16 - 260];
} KLDREXEARGS, *PKLDREXEARGS;
/** Pointer to a const argument package from the stub. */
00910 typedef const KLDREXEARGS *PCKLDREXEARGS;

void kLdrLoadExe(PCKLDREXEARGS pArgs, void *pvOS); /** @todo fix this mess... */
void kLdrDyldLoadExe(PCKLDREXEARGS pArgs, void *pvOS);
/** @} */

/** @} */

/** @} */

#ifdef __cplusplus
}
#endif

#endif


Generated by  Doxygen 1.6.0   Back to index