| 1 | /* $Id: ldr.h,v 1.7 2000-12-11 06:44:35 bird Exp $ | 
|---|
| 2 | * | 
|---|
| 3 | * ldr - Our loader "subsystem" public header file. | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright (c) 2000 knut st. osmundsen (knut.stange.osmundsen@mynd.no) | 
|---|
| 6 | * | 
|---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 8 | * | 
|---|
| 9 | */ | 
|---|
| 10 |  | 
|---|
| 11 |  | 
|---|
| 12 | #ifndef _ldr_h_ | 
|---|
| 13 | #define _ldr_h_ | 
|---|
| 14 |  | 
|---|
| 15 | #ifdef __cplusplus | 
|---|
| 16 | extern "C" { | 
|---|
| 17 | #endif | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 | #ifndef LDR_INCL_INITONLY | 
|---|
| 21 |  | 
|---|
| 22 | /* | 
|---|
| 23 | * Fail if dependent header files is missing | 
|---|
| 24 | */ | 
|---|
| 25 | #ifndef _AVL_H_ | 
|---|
| 26 | #error "You'll have to include avl.h before ldr.h!" | 
|---|
| 27 | #endif | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | /** @design Loader State. | 
|---|
| 31 | * | 
|---|
| 32 | * Used to determin behaviour in different cases. | 
|---|
| 33 | * Use the isLdrState<State> macros to query current state. | 
|---|
| 34 | * IMPORTANT! Don't change this variable if you don't really mean it! | 
|---|
| 35 | *            And only change it thru the setLdrState* macros! | 
|---|
| 36 | * | 
|---|
| 37 | * The state is changing as follows: | 
|---|
| 38 | *    1) Load a new program | 
|---|
| 39 | *      mytkExecPgm will set the state to LDRSTATE_TKEXECPGM on successful overloading. | 
|---|
| 40 | *          myldrOpenPath will set the type part of the loaderbits. (EXE,DLL or UNSUPPORTED) | 
|---|
| 41 | *          (NB! myldrOpenPath is called several times. First for the EXE then for imported modules.) | 
|---|
| 42 | *              IF executable THEN myLdrOpen might set the LDRSTATE_OUR flag. | 
|---|
| 43 | *          myldrOpenPath will reset the type part of the loaderbits upon return. | 
|---|
| 44 | *      mytkExecPgm resets the state to LDRSTATE_UNKNOWN upon return. | 
|---|
| 45 | * | 
|---|
| 46 | *    2) Query program type. | 
|---|
| 47 | *      myLDRQAppType will set the state to LDRSTATE_LDRQAPPTYPE on entry. | 
|---|
| 48 | *          myldrOpenPath will set the type part of the loaderbits. (EXE,DLL or UNSUPPORTED) | 
|---|
| 49 | *          (NB! myldrOpenPath may be called several times.) | 
|---|
| 50 | *              IF executable THEN myLdrOpen might set the LDRSTATE_OUR flag. | 
|---|
| 51 | *          myldrOpenPath will reset the type part of the loaderbits upon return. | 
|---|
| 52 | *      myLDRQAppType resets the state to LDRSTATE_UNKNOWN upon return. | 
|---|
| 53 | * | 
|---|
| 54 | *    3) Unknown invocation - probably DosLoadModule. Base state is LDRSTATE_UNKNOWN. | 
|---|
| 55 | *          myldrOpenPath will set the type part of the loaderbits. (EXE,DLL or UNSUPPORTED) | 
|---|
| 56 | *          (NB! myldrOpenPath is probably called several times. Import modules.) | 
|---|
| 57 | *          myldrOpenPath will reset the type part of the loaderbits upon return. | 
|---|
| 58 | * | 
|---|
| 59 | */ | 
|---|
| 60 | extern ULONG    ulLdrState; | 
|---|
| 61 |  | 
|---|
| 62 | #define LDRSTATE_UNKNOWN        0       /* Default state - undertermined. */ | 
|---|
| 63 | #define LDRSTATE_TKEXECPGM      1       /* A program is being loaded. */ | 
|---|
| 64 | #define LDRSTATE_LDRQAPPTYPE    2       /* Loader called from LDRQAPPTYPE */ | 
|---|
| 65 | /*#define LDRSTATE_LOADMODULE     3 */  /* A module is being loaded by DosLoadModule. Not implemented! */ | 
|---|
| 66 | #define LDRSTATE_MASK           0x00FF  /* State mask. */ | 
|---|
| 67 |  | 
|---|
| 68 | /* | 
|---|
| 69 | * The following flags are only valid when myldrOpenPath is on the stack!, ie. in ldrOpen. | 
|---|
| 70 | * These flags is the "loading-bits". | 
|---|
| 71 | */ | 
|---|
| 72 | #define LDRSTATE_EXE            0x0100  /* Flags telling that an executable is being opened. */ | 
|---|
| 73 | #define LDRSTATE_DLL            0x0200  /* Flags telling that an dll is being opened. */ | 
|---|
| 74 | #define LDRSTATE_UNSUPPORTED    0x0300  /* Flags telling to not override this open call. */ | 
|---|
| 75 | #define LDRSTATE_TYPE_MASK      0x0f00  /* Load Type Mask. */ | 
|---|
| 76 | /* The following flag will tell us if the executable which is loading is ours or not. */ | 
|---|
| 77 | #define LDRSTATE_OUREXE         0x1000 | 
|---|
| 78 | #define LDRSTATE_LOADERBITS     0xff00  /* Mask */ | 
|---|
| 79 |  | 
|---|
| 80 |  | 
|---|
| 81 | /* | 
|---|
| 82 | * Query macros. | 
|---|
| 83 | */ | 
|---|
| 84 | #define isLdrStateUnknown()             ((ulLdrState & LDRSTATE_MASK) == LDRSTATE_UNKNOWN) | 
|---|
| 85 | #define isLdrStateExecPgm()             ((ulLdrState & LDRSTATE_MASK) == LDRSTATE_TKEXECPGM) | 
|---|
| 86 | #define isLdrStateQAppType()            ((ulLdrState & LDRSTATE_MASK) == LDRSTATE_LDRQAPPTYPE) | 
|---|
| 87 | /*#define isLdrStateLoadModule()        ((ulLdrState & LDRSTATE_MASK) == LDRSTATE_LOADMODULE)*/ | 
|---|
| 88 |  | 
|---|
| 89 | #define isLdrStateLoadingEXE()          ((ulLdrState & LDRSTATE_TYPE_MASK) == LDRSTATE_EXE) | 
|---|
| 90 | #define isLdrStateLoadingDLL()          ((ulLdrState & LDRSTATE_TYPE_MASK) == LDRSTATE_DLL) | 
|---|
| 91 | #define isLdrStateLoadingUnsupported()  ((ulLdrState & LDRSTATE_TYPE_MASK) == LDRSTATE_UNSUPPORTED) | 
|---|
| 92 |  | 
|---|
| 93 | #define isLdrStateLoadingOurEXE()       (ulLdrState & LDRSTATE_OUREXE) | 
|---|
| 94 |  | 
|---|
| 95 |  | 
|---|
| 96 | /* | 
|---|
| 97 | * Set macros. | 
|---|
| 98 | */ | 
|---|
| 99 | #define setLdrStateQAppType()           ulLdrState = LDRSTATE_LDRQAPPTYPE | 
|---|
| 100 | #define setLdrStateUnknown()            ulLdrState = LDRSTATE_UNKNOWN | 
|---|
| 101 | /*      setLdrStateExecPgm() isn't needed as this is in assembly source! */ | 
|---|
| 102 | /*#define setLdrStateLoadModule()         ulLdrState = LDRSTATE_LOADMODULE */ | 
|---|
| 103 |  | 
|---|
| 104 | #define setLdrStateLoadingEXE()         ulLdrState = (ulLdrState & (ULONG)(~LDRSTATE_TYPE_MASK)) | LDRSTATE_EXE | 
|---|
| 105 | #define setLdrStateLoadingDLL()         ulLdrState = (ulLdrState & (ULONG)(~LDRSTATE_TYPE_MASK)) | LDRSTATE_DLL | 
|---|
| 106 | #define setLdrStateLoadingUnsupported() ulLdrState = (ulLdrState & (ULONG)(~LDRSTATE_TYPE_MASK)) | LDRSTATE_UNSUPPORTED | 
|---|
| 107 | #define setLdrStateClearLoadingType()   ulLdrState &= (ULONG)(~LDRSTATE_TYPE_MASK) | 
|---|
| 108 |  | 
|---|
| 109 | #define setLdrStateLoadingOurEXE()      ulLdrState |= LDRSTATE_OUREXE | 
|---|
| 110 |  | 
|---|
| 111 |  | 
|---|
| 112 | /* | 
|---|
| 113 | * Loader State assert macros. | 
|---|
| 114 | */ | 
|---|
| 115 | #define ASSERT_LdrStateUnknown(fn)      ASSERT_LdrState(fn, LDRSTATE_UNKNOWN) | 
|---|
| 116 | #define ASSERT_LdrStateExecPgm(fn)      ASSERT_LdrState(fn, LDRSTATE_TKEXECPGM) | 
|---|
| 117 | #define ASSERT_LdrStateQAppType(fn)     ASSERT_LdrState(fn, LDRSTATE_LDRQAPPTYPE) | 
|---|
| 118 |  | 
|---|
| 119 | #define ASSERT_LdrState(fn, state) \ | 
|---|
| 120 | {                              \ | 
|---|
| 121 | if ((ulLdrState & LDRSTATE_MASK) != (state)) \ | 
|---|
| 122 | {                          \ | 
|---|
| 123 | kprintf((fn ": assertion incorrect loader state. ulLdrState (%d) != " #state "(%d)", \ | 
|---|
| 124 | ulLdrState, state)); \ | 
|---|
| 125 | }                          \ | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 |  | 
|---|
| 129 | /* | 
|---|
| 130 | * This flag is used by myldrOpenPath to communicate to myldrOpen that | 
|---|
| 131 | * the .DLL extention should be removed before opening the file. | 
|---|
| 132 | */ | 
|---|
| 133 | extern BOOL fldrOpenExtentionFix; | 
|---|
| 134 |  | 
|---|
| 135 |  | 
|---|
| 136 | /* | 
|---|
| 137 | * handle state - Array of handle states. Eight state per byte! | 
|---|
| 138 | */ | 
|---|
| 139 | #define MAX_FILE_HANDLES 0x10000 | 
|---|
| 140 |  | 
|---|
| 141 | extern unsigned char achHandleStates[MAX_FILE_HANDLES/8]; | 
|---|
| 142 |  | 
|---|
| 143 | #define HSTATE_UNUSED       0x00    /* Handle not used (or OS/2). */ | 
|---|
| 144 | #define HSTATE_OS2          0x00    /* OS/2 module filehandle. */ | 
|---|
| 145 | #define HSTATE_OUR          0x01    /* Our module filehandle. */ | 
|---|
| 146 | #define HSTATE_MASK         0xFE | 
|---|
| 147 | #define HSTATE_UMASK        0x01 | 
|---|
| 148 |  | 
|---|
| 149 | #define GetState(a)         (HSTATE_UMASK & (achHandleStates[(a)/8] >> ((a)%8))) | 
|---|
| 150 | #define SetState(a,b)       (achHandleStates[(a)/8] = (achHandleStates[(a)/8] & (HSTATE_MASK << ((a)%8) | HSTATE_MASK >> 8-((a)%8)) | ((b) & 0x1) << ((a)%8))) | 
|---|
| 151 |  | 
|---|
| 152 |  | 
|---|
| 153 | /* | 
|---|
| 154 | * Declare the module classes used below in case they aren't declared yet. | 
|---|
| 155 | */ | 
|---|
| 156 | #ifdef __cplusplus | 
|---|
| 157 | class ModuleBase; | 
|---|
| 158 | class Pe2Lx; | 
|---|
| 159 | class Elf2Lx; | 
|---|
| 160 | #else | 
|---|
| 161 | typedef char ModuleBase; | 
|---|
| 162 | typedef char Pe2Lx; | 
|---|
| 163 | typedef char Elf2Lx; | 
|---|
| 164 | #endif | 
|---|
| 165 |  | 
|---|
| 166 |  | 
|---|
| 167 | /* | 
|---|
| 168 | * Module struct. | 
|---|
| 169 | */ | 
|---|
| 170 | typedef struct _Module | 
|---|
| 171 | { | 
|---|
| 172 | AVLNODECORE     coreKey;    /* Key is hFile. */ | 
|---|
| 173 | AVLNODECORE     coreMTE;    /* Key is pMTE. */ | 
|---|
| 174 |  | 
|---|
| 175 | SFN             hFile;      /* System file number or file handle if you prefer that. */ | 
|---|
| 176 | PMTE            pMTE;       /* Pointer to MTE if we got one - NULL is allowed. */ | 
|---|
| 177 |  | 
|---|
| 178 | ULONG           fFlags;     /* Flags. Flags if coreMte is in use and what Data contains. */ | 
|---|
| 179 | union | 
|---|
| 180 | { | 
|---|
| 181 | ModuleBase *pModule;    /* Pointer to base module. */ | 
|---|
| 182 | Pe2Lx *     pPe2Lx;     /* Pointer to a Pe2Lx object. (Win32 executables) */ | 
|---|
| 183 | Elf2Lx *    pElf2Lx;    /* Pointer to a Elf2Lx object. (ELF executables) */ | 
|---|
| 184 | #if 0 | 
|---|
| 185 | Script *    pScript;    /* Pointer to a Script object. (Shell scripts) */ | 
|---|
| 186 | Pe *        pPe;        /* Pointer to a Pe object. (Ring3 loader) */ | 
|---|
| 187 | #endif | 
|---|
| 188 | } Data;                     /* Pointer to data. Currently it's allways a Pe2Lx object! */ | 
|---|
| 189 | } MODULE, *PMODULE; | 
|---|
| 190 |  | 
|---|
| 191 | #define MOD_FLAGS_IN_MTETREE    0x00000010UL /* The node is present in the MTE-tree. */ | 
|---|
| 192 | #define MOD_TYPE_MASK           0x0000000FUL /* Type mask. */ | 
|---|
| 193 | #define MOD_TYPE_PE2LX          0x00000001UL /* Pe2Lx module. */ | 
|---|
| 194 | #define MOD_TYPE_ELF2LX         0x00000002UL /* Elf2Lx module. */ | 
|---|
| 195 | #define MOD_TYPE_SCRIPT         0x00000003UL /* Script module. */ | 
|---|
| 196 | #define MOD_TYPE_PE             0x00000004UL /* Pe module. */ | 
|---|
| 197 |  | 
|---|
| 198 |  | 
|---|
| 199 | /* | 
|---|
| 200 | * Pointer to the currently loading executable module. | 
|---|
| 201 | * Available at tkExecPgm time when loading a converted module. | 
|---|
| 202 | */ | 
|---|
| 203 | extern PMODULE pExeModule; | 
|---|
| 204 |  | 
|---|
| 205 |  | 
|---|
| 206 | /* | 
|---|
| 207 | * Modules operations. | 
|---|
| 208 | */ | 
|---|
| 209 | PMODULE     getModuleBySFN(SFN hFile); | 
|---|
| 210 | PMODULE     getModuleByMTE(PMTE pMTE); | 
|---|
| 211 | PMODULE     getModuleByhMTE(HMTE hMTE); | 
|---|
| 212 | PMODULE     getModuleByFilename(PCSZ pszFilename); | 
|---|
| 213 |  | 
|---|
| 214 | ULONG       addModule(SFN hFile, PMTE pMTE, ULONG fFlags, ModuleBase *pModObj); | 
|---|
| 215 | ULONG       removeModule(SFN hFile); | 
|---|
| 216 |  | 
|---|
| 217 |  | 
|---|
| 218 |  | 
|---|
| 219 | /* | 
|---|
| 220 | * mytkExecPgm variables and functions | 
|---|
| 221 | * | 
|---|
| 222 | * (See ldr\mytkExecPgm.asm for further info on these variabels and functions.) | 
|---|
| 223 | */ | 
|---|
| 224 | #define CCHFILENAME     261             /* This is defined in mytkExecPgm.asm too. */ | 
|---|
| 225 | #define CCHARGUMENTS   1536             /* This is defined in mytkExecPgm.asm too. */ | 
|---|
| 226 | extern const char   fTkExecPgm; | 
|---|
| 227 | extern char         achTkExecPgmFilename[CCHFILENAME]; | 
|---|
| 228 | extern char         achTkExecPgmArguments[CCHARGUMENTS]; | 
|---|
| 229 | #endif | 
|---|
| 230 | ULONG _Optlink  tkExecPgmEnvLength(void); | 
|---|
| 231 | ULONG _Optlink  tkExecPgmCopyEnv(char *pachBuffer, unsigned cchBuffer); | 
|---|
| 232 |  | 
|---|
| 233 |  | 
|---|
| 234 |  | 
|---|
| 235 | /* | 
|---|
| 236 | * functions | 
|---|
| 237 | */ | 
|---|
| 238 | PSZ         ldrGetExePath(PSZ pszPath, BOOL fExecChild); | 
|---|
| 239 | ULONG       ldrInit(void); | 
|---|
| 240 |  | 
|---|
| 241 | #ifdef __cplusplus | 
|---|
| 242 | } | 
|---|
| 243 | #endif | 
|---|
| 244 |  | 
|---|
| 245 | #pragma pack() | 
|---|
| 246 |  | 
|---|
| 247 | #endif | 
|---|