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

kLdr.h

Go to the documentation of this file.
/* $Id: kLdr.h 2 2007-11-16 16:07:14Z bird $ */
/** @file
 *
 * kLdr - The Dynamic Loader.
 *
 * Copyright (c) 2006 knut st. osmundsen <bird@anduin.net>
 *
 *
 * This file is part of kLdr.
 *
 * kLdr is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * kLdr 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with kLdr; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#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. */
00046 typedef KU64 KLDRADDR;
/** Pointer to a kLdr address. */
00048 typedef KLDRADDR *PKLDRADDR;
/** Pointer to a const kLdr address. */
00050 typedef const KLDRADDR *PCKLDRADDR;

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

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

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


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

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


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

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


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

00109     KI8             ai8[8]; /**< KI8 array view . */
00110     KU8             au8[8]; /**< KU8 array view. */
00111     KI16            ai16[4];/**< KI16 array view . */
00112     KU16            au16[4];/**< KU16 array view. */
00113     KI32            ai32[2];/**< KI32 array view . */
00114     KU32            au32[2];/**< KU32 array view. */

00116     signed char     ch;     /**< signed char view. */
00117     unsigned char   uch;    /**< unsigned char view. */
00118     signed short    s;      /**< signed short view. */
00119     unsigned short  us;     /**< unsigned short view. */
00120     signed int      i;      /**< signed int view. */
00121     unsigned int    u;      /**< unsigned int view. */
00122     signed long     l;      /**< signed long view. */
00123     unsigned long   ul;     /**< unsigned long view. */
00124     void           *pv;     /**< void pointer view. */

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


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

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

/** @} */


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

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


/**
 * Stack information.
 */
00201 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. */
00205     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. */
00208     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. */
00212     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. */
00215     KLDRSIZE        cbStackThread;
} KLDRSTACKINFO;
/** Pointer to stack information. */
00218 typedef KLDRSTACKINFO *PKLDRSTACKINFO;
/** Pointer to const stack information. */
00220 typedef const KLDRSTACKINFO *PCKLDRSTACKINFO;


/**
 * Loader segment.
 */
00226 typedef struct KLDRSEG
{
    /** Variable free to use for the kLdr user. */
00229     void           *pvUser;
    /** The segment name. (Might not be zero terminated!) */
00231     const char     *pchName;
    /** The length of the segment name. */
00233     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. */
00236     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. */
00239     KU16            Sel16bit;
    /** Segment flags. */
00241     KU32            fFlags;
    /** The segment protection. */
00243     KPROT        enmProt;
    /** The size of the segment. */
00245     KLDRSIZE        cb;
    /** The required segment alignment.
     * The to 0 if the segment isn't supposed to be mapped. */
00248     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. */
00252     KLDRADDR        LinkAddress;
    /** File offset of the segment.
     * Set to -1 if no file backing (like BSS). */
00255     KLDRFOFF        offFile;
    /** Size of the file bits of the segment.
     * Set to -1 if no file backing (like BSS). */
00258     KLDRFOFF        cbFile;
    /** The relative virtual address when mapped.
     * Set to NIL_KLDRADDR if the segment isn't supposed to be mapped. */
00261     KLDRADDR        RVA;
    /** The size of the segment including the alignment gap up to the next segment when mapped. */
00263     KSIZE           cbMapped;
    /** The address the segment was mapped at by kLdrModMap().
     * Set to 0 if not mapped. */
00266     KUPTR           MapAddress;
} KLDRSEG;


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


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


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


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


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

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

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


/** Special base address value alias for the link address. */
00412 #define KLDRMOD_BASEADDRESS_LINK            (~(KLDRADDR)1)
/** Special base address value alias for the actual load address (must be mapped). */
00414 #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. */
00418 #define NIL_KLDRMOD_IMPORT                  (~(KU32)0)

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


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

/** @name kLdrModEnumSymbols flags.
 * @{ */
/** Returns ALL kinds of symbols. The default is to only return public/exported symbols. */
00462 #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.
 */
00482 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. */
00485 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().
 */
00505 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. */
00508 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.
 */
00529 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. */
00532 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.
 */
00550 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. */
00553 typedef FNKLDRENUMRSRC *PFNKLDRENUMRSRC;

/** NIL resource name ID. */
00556 #define NIL_KLDRMOD_RSRC_NAME_ID    ( ~(KU32)0 )
/** NIL resource type ID. */
00558 #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. */
00566 #define NIL_KLDR_LANG_ID                ( ~(KU32)0 )
/** Special language id value for matching any language. */
00568 #define KLDR_LANG_ID_ANY                ( ~(KU32)1 )
/** Special language id value indicating language neutral. */
00570 #define KLDR_LANG_ID_NEUTRAL            ( ~(KU32)2 )
/** Special language id value indicating user default language. */
00572 #define KLDR_LANG_ID_USER_DEFAULT       ( ~(KU32)3 )
/** Special language id value indicating system default language. */
00574 #define KLDR_LANG_ID_SYS_DEFAULT        ( ~(KU32)4 )
/** Special language id value indicating default custom locale. */
00576 #define KLDR_LANG_ID_CUSTOM_DEFAULT     ( ~(KU32)5 )
/** Special language id value indicating unspecified custom locale. */
00578 #define KLDR_LANG_ID_CUSTOM_UNSPECIFIED ( ~(KU32)6 )
/** Special language id value indicating default custom MUI locale. */
00580 #define KLDR_LANG_ID_UI_CUSTOM_DEFAULT  ( ~(KU32)7 )
/** @} */


int     kLdrModOpen(const char *pszFilename, PPKLDRMOD ppMod);
int     kLdrModOpenFromRdr(PKRDR pRdr, 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.
 */
00634 typedef struct KLDRMODOPS
{
    /** The name of this module interpreter. */
00637     const char         *pszName;
    /** Pointer to the next module interpreter. */
00639     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   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, 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. */
00719     KU32 uEndOfStructure;
} KLDRMODOPS;


/** @} */




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

/** The handle to a dynamic loader module. */
00732 typedef struct KLDRDYLDMOD *HKLDRMOD;
/** Pointer to the handle to a dynamic loader module. */
00734 typedef HKLDRMOD *PHKLDRMOD;
/** NIL handle value. */
00736 #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.
 */
00745 typedef enum KLDRDYLDSEARCH
{
    /** The usual invalid file search method. */
00748     KLDRDYLD_SEARCH_INVALID = 0,
    /** Uses the kLdr file search method.
     * @todo invent me. */
00751     KLDRDYLD_SEARCH_KLDR,
    /** Use the emulation closest to the host system. */
00753     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. */
00757     KLDRDYLD_SEARCH_OS2,
    /** Emulate the standard window file search method. */
00759     KLDRDYLD_SEARCH_WINDOWS,
    /** Emulate the alternative window file search method. */
00761     KLDRDYLD_SEARCH_WINDOWS_ALTERED,
    /** Emulate the most common UNIX file search method. */
00763     KLDRDYLD_SEARCH_UNIX_COMMON,
    /** End of the valid file search method values. */
00765     KLDRDYLD_SEARCH_END,
    /** Hack to blow the type up to 32-bit. */
00767     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. */
00774 #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. */
00778 #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. */
00782 #define KLDRYDLD_LOAD_FLAGS_SPECIFIC_MODULE     0x00000004
/** Do a recursive initialization calls instead of defering them to the outermost call. */
00784 #define KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT      0x00000008
/** We're loading the executable module.
 * @internal */
00787 #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. */
00855 typedef struct KLDRWRSRCFOUND *HKLDRWRSRCFOUND;
/** The handle to a loaded resource. */
00857 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.
 */
00886 typedef struct KLDREXEARGS
{
    /** Load & search flags, some which will become defaults. */
00889     KU32            fFlags;
    /** The default search method. */
00891     KLDRDYLDSEARCH  enmSearch;
    /** The executable file that the stub is supposed to load. */
00893     char            szExecutable[260];
    /** The default prefix used when searching for DLLs. */
00895     char            szDefPrefix[16];
    /** The default suffix used when searching for DLLs. */
00897     char            szDefSuffix[16];
    /** The LD_LIBRARY_PATH prefix for the process.. */
00899     char            szLibPath[4096 - sizeof(KU32) - sizeof(KLDRDYLDSEARCH) - 16 - 16 - 260];
} KLDREXEARGS, *PKLDREXEARGS;
/** Pointer to a const argument package from the stub. */
00902 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