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.

418 lines
10 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. blbind.c
  5. Abstract:
  6. This module contains the code that implements the funtions required
  7. to relocate an image and bind DLL entry points.
  8. Author:
  9. David N. Cutler (davec) 21-May-1991
  10. Revision History:
  11. --*/
  12. #include "bldr.h"
  13. #include "ctype.h"
  14. #include "string.h"
  15. //
  16. // Define local procedure prototypes.
  17. //
  18. BOOLEAN
  19. BlpCompareDllName (
  20. IN PCHAR Name,
  21. IN PUNICODE_STRING UnicodeString
  22. );
  23. #if defined(_X86AMD64_)
  24. //
  25. // For this version of the loader there are several routines that are needed
  26. // in both a 32-bit and 64-bit flavor. The code for these routines exists
  27. // in blbindt.c.
  28. //
  29. // First, set up various definitions to cause 32-bit functions to be
  30. // generated, then include blbindt.c.
  31. //
  32. #define BlAllocateDataTableEntry BlAllocateDataTableEntry32
  33. #define BlAllocateFirmwareTableEntry BlAllocateFirmwareTableEntry32
  34. #define BlpBindImportName BlpBindImportName32
  35. #define BlpScanImportAddressTable BlpScanImportAddressTable32
  36. #define BlScanImportDescriptorTable BlScanImportDescriptorTable32
  37. #define BlScanOsloaderBoundImportTable BlScanOsloaderBoundImportTable32
  38. #undef IMAGE_DEFINITIONS
  39. #define IMAGE_DEFINITIONS 32
  40. #include <ximagdef.h>
  41. #include "amd64\amd64prv.h"
  42. #include "blbindt.c"
  43. #undef BlAllocateDataTableEntry
  44. #undef BlAllocateFirmwareTableEntry
  45. #undef BlpBindImportName
  46. #undef BlpScanImportAddressTable
  47. #undef BlScanImportDescriptorTable
  48. #undef BlScanOsloaderBoundImportTable
  49. //
  50. // Now, change those definitions in order to generate 64-bit versions of
  51. // those same functions.
  52. //
  53. #define BlAllocateDataTableEntry BlAllocateDataTableEntry64
  54. #define BlAllocateFirmwareTableEntry BlAllocateFirmwareTableEntry64
  55. #define BlpBindImportName BlpBindImportName64
  56. #define BlpScanImportAddressTable BlpScanImportAddressTable64
  57. #define BlScanImportDescriptorTable BlScanImportDescriptorTable64
  58. #define BlScanOsloaderBoundImportTable BlScanOsloaderBoundImportTable64
  59. #undef IMAGE_DEFINITIONS
  60. #define IMAGE_DEFINITIONS 64
  61. #include <ximagdef.h>
  62. #include "amd64\amd64prv.h"
  63. #include "blbindt.c"
  64. #undef BlAllocateDataTableEntry
  65. #undef BlAllocateFirmwareTableEntry
  66. #undef BlpBindImportName
  67. #undef BlpScanImportAddressTable
  68. #undef BlScanImportDescriptorTable
  69. #undef BlScanOsloaderBoundImportTable
  70. #else // _X86AMD64_
  71. #define IMAGE_DEFINITIONS 32
  72. #define IMAGE_NT_HEADER(x) RtlImageNtHeader(x)
  73. #include "blbindt.c"
  74. #endif // _X86AMD64_
  75. BOOLEAN
  76. BlCheckForLoadedDll (
  77. IN PCHAR DllName,
  78. OUT PKLDR_DATA_TABLE_ENTRY *FoundEntry
  79. )
  80. /*++
  81. Routine Description:
  82. This routine scans the loaded DLL list to determine if the specified
  83. DLL has already been loaded. If the DLL has already been loaded, then
  84. its reference count is incremented.
  85. Arguments:
  86. DllName - Supplies a pointer to a null terminated DLL name.
  87. FoundEntry - Supplies a pointer to a variable that receives a pointer
  88. to the matching data table entry.
  89. Return Value:
  90. If the specified DLL has already been loaded, then TRUE is returned.
  91. Otherwise, FALSE is returned.
  92. --*/
  93. {
  94. PKLDR_DATA_TABLE_ENTRY DataTableEntry;
  95. PLIST_ENTRY NextEntry;
  96. //
  97. // Scan the loaded data table list to determine if the specified DLL
  98. // has already been loaded.
  99. //
  100. NextEntry = BlLoaderBlock->LoadOrderListHead.Flink;
  101. while (NextEntry != &BlLoaderBlock->LoadOrderListHead) {
  102. DataTableEntry = CONTAINING_RECORD(NextEntry,
  103. KLDR_DATA_TABLE_ENTRY,
  104. InLoadOrderLinks);
  105. if (BlpCompareDllName(DllName, &DataTableEntry->BaseDllName) != FALSE) {
  106. *FoundEntry = DataTableEntry;
  107. DataTableEntry->LoadCount += 1;
  108. return TRUE;
  109. }
  110. NextEntry = NextEntry->Flink;
  111. }
  112. return FALSE;
  113. }
  114. BOOLEAN
  115. BlpCompareDllName (
  116. IN PCHAR DllName,
  117. IN PUNICODE_STRING UnicodeString
  118. )
  119. /*++
  120. Routine Description:
  121. This routine compares a zero terminated character string with a unicode
  122. string. The UnicodeString's extension is ignored.
  123. Arguments:
  124. DllName - Supplies a pointer to a null terminated DLL name.
  125. UnicodeString - Supplies a pointer to a Unicode string descriptor.
  126. Return Value:
  127. If the specified name matches the Unicode name, then TRUE is returned.
  128. Otherwise, FALSE is returned.
  129. --*/
  130. {
  131. PWSTR Buffer;
  132. ULONG Index;
  133. ULONG Length;
  134. //
  135. // Compute the length of the DLL Name and compare with the length of
  136. // the Unicode name. If the DLL Name is longer, the strings are not
  137. // equal.
  138. //
  139. Length = (ULONG)strlen(DllName);
  140. if ((Length * sizeof(WCHAR)) > UnicodeString->Length) {
  141. return FALSE;
  142. }
  143. //
  144. // Compare the two strings case insensitive, ignoring the Unicode
  145. // string's extension.
  146. //
  147. Buffer = UnicodeString->Buffer;
  148. for (Index = 0; Index < Length; Index += 1) {
  149. if (toupper(*DllName) != toupper((CHAR)*Buffer)) {
  150. return FALSE;
  151. }
  152. DllName += 1;
  153. Buffer += 1;
  154. }
  155. if ((UnicodeString->Length == Length * sizeof(WCHAR)) ||
  156. (*Buffer == L'.')) {
  157. //
  158. // Strings match exactly or match up until the UnicodeString's extension.
  159. //
  160. return(TRUE);
  161. }
  162. return FALSE;
  163. }
  164. #if defined(_X86AMD64_)
  165. ARC_STATUS
  166. BlScanImportDescriptorTable(
  167. IN PPATH_SET PathSet,
  168. IN PKLDR_DATA_TABLE_ENTRY ScanEntry,
  169. IN TYPE_OF_MEMORY MemoryType
  170. )
  171. /*++
  172. Routine Description:
  173. This routine scans the import descriptor table for the specified image
  174. file and loads each DLL that is referenced.
  175. Arguments:
  176. PathSet - Supplies a pointer to a set of paths to scan when searching
  177. for DLL's.
  178. ScanEntry - Supplies a pointer to the data table entry for the
  179. image whose import table is to be scanned.
  180. MemoryType - Supplies the type of memory to to be assigned to any DLL's
  181. referenced.
  182. Return Value:
  183. ESUCCESS is returned in the scan is successful. Otherwise, return an
  184. unsuccessful status.
  185. --*/
  186. {
  187. ARC_STATUS status;
  188. if (BlAmd64UseLongMode != FALSE) {
  189. status = BlScanImportDescriptorTable64( PathSet,
  190. ScanEntry,
  191. MemoryType );
  192. } else {
  193. status = BlScanImportDescriptorTable32( PathSet,
  194. ScanEntry,
  195. MemoryType );
  196. }
  197. return status;
  198. }
  199. ARC_STATUS
  200. BlScanOsloaderBoundImportTable (
  201. IN PKLDR_DATA_TABLE_ENTRY ScanEntry
  202. )
  203. /*++
  204. Routine Description:
  205. This routine scans the import descriptor table for the specified image
  206. file and loads each DLL that is referenced.
  207. Arguments:
  208. DataTableEntry - Supplies a pointer to the data table entry for the
  209. image whose import table is to be scanned.
  210. Return Value:
  211. ESUCCESS is returned in the scan is successful. Otherwise, return an
  212. unsuccessful status.
  213. --*/
  214. {
  215. ARC_STATUS status;
  216. if (BlAmd64UseLongMode != FALSE) {
  217. status = BlScanOsloaderBoundImportTable64( ScanEntry );
  218. } else {
  219. status = BlScanOsloaderBoundImportTable32( ScanEntry );
  220. }
  221. return status;
  222. }
  223. ARC_STATUS
  224. BlAllocateDataTableEntry (
  225. IN PCHAR BaseDllName,
  226. IN PCHAR FullDllName,
  227. IN PVOID Base,
  228. OUT PKLDR_DATA_TABLE_ENTRY *AllocatedEntry
  229. )
  230. /*++
  231. Routine Description:
  232. This routine allocates a data table entry for the specified image
  233. and inserts the entry in the loaded module list.
  234. Arguments:
  235. BaseDllName - Supplies a pointer to a zero terminated base DLL name.
  236. FullDllName - Supplies a pointer to a zero terminated full DLL name.
  237. Base - Supplies a pointer to the base of the DLL image.
  238. AllocatedEntry - Supplies a pointer to a variable that receives a
  239. pointer to the allocated data table entry.
  240. Return Value:
  241. ESUCCESS is returned if a data table entry is allocated. Otherwise,
  242. return a unsuccessful status.
  243. --*/
  244. {
  245. ARC_STATUS status;
  246. if (BlAmd64UseLongMode != FALSE) {
  247. status = BlAllocateDataTableEntry64( BaseDllName,
  248. FullDllName,
  249. Base,
  250. AllocatedEntry );
  251. } else {
  252. status = BlAllocateDataTableEntry32( BaseDllName,
  253. FullDllName,
  254. Base,
  255. AllocatedEntry );
  256. }
  257. return status;
  258. }
  259. ARC_STATUS
  260. BlAllocateFirmwareTableEntry (
  261. IN PCHAR BaseDllName,
  262. IN PCHAR FullDllName,
  263. IN PVOID Base,
  264. IN ULONG Size,
  265. OUT PKLDR_DATA_TABLE_ENTRY *AllocatedEntry
  266. )
  267. /*++
  268. Routine Description:
  269. This routine allocates a firmware table entry for the specified image
  270. and inserts the entry in the loaded module list.
  271. Arguments:
  272. BaseDllName - Supplies a pointer to a zero terminated base DLL name.
  273. FullDllName - Supplies a pointer to a zero terminated full DLL name.
  274. Base - Supplies a pointer to the base of the DLL image.
  275. Size - Supplies how big the image is.
  276. AllocatedEntry - Supplies a pointer to a variable that receives a
  277. pointer to the allocated data table entry.
  278. Return Value:
  279. ESUCCESS is returned if a data table entry is allocated. Otherwise,
  280. return a unsuccessful status.
  281. --*/
  282. {
  283. ARC_STATUS status;
  284. if (BlAmd64UseLongMode != FALSE) {
  285. status = BlAllocateFirmwareTableEntry64( BaseDllName,
  286. FullDllName,
  287. Base,
  288. Size,
  289. AllocatedEntry );
  290. } else {
  291. status = BlAllocateFirmwareTableEntry32( BaseDllName,
  292. FullDllName,
  293. Base,
  294. Size,
  295. AllocatedEntry );
  296. }
  297. return status;
  298. }
  299. #endif // _X86AMD64_