Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.8 KiB

  1. //
  2. // DelayImp.h
  3. //
  4. // define structures and prototypes necessary for delay loading of imports
  5. //
  6. #if !defined(_delayimp_h)
  7. #define _delayimp_h
  8. #if _MSC_VER > 1000
  9. #pragma once
  10. #endif
  11. #ifndef DELAYLOAD_VERSION
  12. #ifdef _WIN64
  13. #define DELAYLOAD_VERSION 0x200
  14. #else
  15. #define DELAYLOAD_VERSION 0x100
  16. #endif
  17. #endif
  18. #if defined(__cplusplus)
  19. #define ExternC extern "C"
  20. #else
  21. #define ExternC
  22. #endif
  23. typedef IMAGE_THUNK_DATA * PImgThunkData;
  24. typedef const IMAGE_THUNK_DATA * PCImgThunkData;
  25. typedef DWORD RVA;
  26. typedef struct ImgDelayDescrV2 {
  27. DWORD grAttrs; // attributes
  28. RVA rvaDLLName; // RVA to dll name
  29. RVA rvaHmod; // RVA of module handle
  30. RVA rvaIAT; // RVA of the IAT
  31. RVA rvaINT; // RVA of the INT
  32. RVA rvaBoundIAT; // RVA of the optional bound IAT
  33. RVA rvaUnloadIAT; // RVA of optional copy of original IAT
  34. DWORD dwTimeStamp; // 0 if not bound,
  35. // O.W. date/time stamp of DLL bound to (Old BIND)
  36. } ImgDelayDescrV2, * PImgDelayDescrV2;
  37. typedef struct ImgDelayDescrV1 {
  38. DWORD grAttrs; // attributes
  39. LPCSTR szName; // pointer to dll name
  40. HMODULE * phmod; // address of module handle
  41. PImgThunkData pIAT; // address of the IAT
  42. PCImgThunkData pINT; // address of the INT
  43. PCImgThunkData pBoundIAT; // address of the optional bound IAT
  44. PCImgThunkData pUnloadIAT; // address of optional copy of original IAT
  45. DWORD dwTimeStamp; // 0 if not bound,
  46. // O.W. date/time stamp of DLL bound to (Old BIND)
  47. } ImgDelayDescrV1, * PImgDelayDescrV1;
  48. #if DELAYLOAD_VERSION >= 0x0200
  49. typedef ImgDelayDescrV2 ImgDelayDescr;
  50. typedef PImgDelayDescrV2 PImgDelayDescr;
  51. #else
  52. typedef ImgDelayDescrV1 ImgDelayDescr;
  53. typedef PImgDelayDescrV1 PImgDelayDescr;
  54. #endif
  55. typedef const ImgDelayDescr * PCImgDelayDescr;
  56. enum DLAttr { // Delay Load Attributes
  57. dlattrRva = 0x1, // RVAs are used instead of pointers
  58. };
  59. //
  60. // Delay load import hook notifications
  61. //
  62. enum {
  63. dliStartProcessing, // used to bypass or note helper only
  64. dliNotePreLoadLibrary, // called just before LoadLibrary, can
  65. // override w/ new HMODULE return val
  66. dliNotePreGetProcAddress, // called just before GetProcAddress, can
  67. // override w/ new FARPROC return value
  68. dliFailLoadLib, // failed to load library, fix it by
  69. // returning a valid HMODULE
  70. dliFailGetProc, // failed to get proc address, fix it by
  71. // returning a valid FARPROC
  72. dliNoteEndProcessing, // called after all processing is done, no
  73. // no bypass possible at this point except
  74. // by longjmp()/throw()/RaiseException.
  75. };
  76. typedef struct DelayLoadProc {
  77. BOOL fImportByName;
  78. union {
  79. LPCSTR szProcName;
  80. DWORD dwOrdinal;
  81. };
  82. } DelayLoadProc;
  83. typedef struct DelayLoadInfo {
  84. DWORD cb; // size of structure
  85. PCImgDelayDescr pidd; // raw form of data (everything is there)
  86. FARPROC * ppfn; // points to address of function to load
  87. LPCSTR szDll; // name of dll
  88. DelayLoadProc dlp; // name or ordinal of procedure
  89. HMODULE hmodCur; // the hInstance of the library we have loaded
  90. FARPROC pfnCur; // the actual function that will be called
  91. DWORD dwLastError;// error received (if an error notification)
  92. } DelayLoadInfo, * PDelayLoadInfo;
  93. typedef FARPROC (WINAPI *PfnDliHook)(
  94. unsigned dliNotify,
  95. PDelayLoadInfo pdli
  96. );
  97. // utility function for calculating the index of the current import
  98. // for all the tables (INT, BIAT, UIAT, and IAT).
  99. __inline unsigned
  100. IndexFromPImgThunkData(PCImgThunkData pitdCur, PCImgThunkData pitdBase) {
  101. return (unsigned)(pitdCur - pitdBase);
  102. }
  103. // C++ template utility function for converting RVAs to pointers
  104. //
  105. #if defined(_WIN64) && defined(_M_IA64)
  106. #pragma section(".base", long, read, write)
  107. ExternC
  108. __declspec(allocate(".base"))
  109. extern
  110. IMAGE_DOS_HEADER __ImageBase;
  111. #else
  112. ExternC
  113. extern
  114. IMAGE_DOS_HEADER __ImageBase;
  115. #endif
  116. #if defined(__cplusplus)
  117. template <class X>
  118. X * PFromRva(RVA rva, const X *) {
  119. return (X*)(PBYTE(&__ImageBase) + rva);
  120. }
  121. #else
  122. __inline
  123. void *
  124. WINAPI
  125. PFromRva(RVA rva, void *unused) {
  126. return (PVOID)(((PBYTE)&__ImageBase) + rva);
  127. }
  128. #endif
  129. //
  130. // Unload support
  131. //
  132. // routine definition; takes a pointer to a name to unload
  133. //
  134. #if DELAYLOAD_VERSION >= 0x0200
  135. ExternC
  136. BOOL WINAPI
  137. __FUnloadDelayLoadedDLL2(LPCSTR szDll);
  138. #else
  139. ExternC
  140. BOOL WINAPI
  141. __FUnloadDelayLoadedDLL(LPCSTR szDll);
  142. #endif
  143. // structure definitions for the list of unload records
  144. typedef struct UnloadInfo * PUnloadInfo;
  145. typedef struct UnloadInfo {
  146. PUnloadInfo puiNext;
  147. PCImgDelayDescr pidd;
  148. } UnloadInfo;
  149. // the default delay load helper places the unloadinfo records in the list
  150. // headed by the following pointer.
  151. ExternC
  152. extern
  153. PUnloadInfo __puiHead;
  154. //
  155. // Exception information
  156. //
  157. #define FACILITY_VISUALCPP ((LONG)0x6d)
  158. #define VcppException(sev,err) ((sev) | (FACILITY_VISUALCPP<<16) | err)
  159. // utility function for calculating the count of imports given the base
  160. // of the IAT. NB: this only works on a valid IAT!
  161. __inline unsigned
  162. CountOfImports(PCImgThunkData pitdBase) {
  163. unsigned cRet = 0;
  164. PCImgThunkData pitd = pitdBase;
  165. while (pitd->u1.Function) {
  166. pitd++;
  167. cRet++;
  168. }
  169. return cRet;
  170. }
  171. //
  172. // Hook pointers
  173. //
  174. // The "notify hook" gets called for every call to the
  175. // delay load helper. This allows a user to hook every call and
  176. // skip the delay load helper entirely.
  177. //
  178. // dliNotify == {
  179. // dliStartProcessing |
  180. // dliPreLoadLibrary |
  181. // dliPreGetProc |
  182. // dliNoteEndProcessing}
  183. // on this call.
  184. //
  185. ExternC
  186. extern
  187. PfnDliHook __pfnDliNotifyHook;
  188. ExternC
  189. extern
  190. PfnDliHook __pfnDliNotifyHook2;
  191. // This is the failure hook, dliNotify = {dliFailLoadLib|dliFailGetProc}
  192. ExternC
  193. extern
  194. PfnDliHook __pfnDliFailureHook;
  195. ExternC
  196. extern
  197. PfnDliHook __pfnDliFailureHook2;
  198. #if DELAYLOAD_VERSION >= 0x0200
  199. #define __pfnDliFailureHook __pfnDliFailureHook2
  200. #define __pfnDliNotifyHook __pfnDliNotifyHook2
  201. #endif
  202. #endif