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.

214 lines
8.4 KiB

  1. #include "SymCommon.h"
  2. #include <strsafe.h>
  3. IMAGE_DEBUG_DIRECTORY UNALIGNED *
  4. SymCommonGetDebugDirectoryInExe(PIMAGE_DOS_HEADER pDosHeader, DWORD* NumberOfDebugDirectories) {
  5. /* Exe is already mapped and a pointer to the base is
  6. passed in. Find a pointer to the Debug Directories
  7. */
  8. ULONG size;
  9. IMAGE_DEBUG_DIRECTORY UNALIGNED *pDebugDirectory = NULL;
  10. ULONG DebugDirectorySize;
  11. PIMAGE_SECTION_HEADER pSectionHeader;
  12. size = sizeof(IMAGE_DEBUG_DIRECTORY);
  13. pDebugDirectory = (PIMAGE_DEBUG_DIRECTORY)
  14. ImageDirectoryEntryToDataEx (
  15. (PVOID)pDosHeader,
  16. FALSE,
  17. IMAGE_DIRECTORY_ENTRY_DEBUG,
  18. &DebugDirectorySize,
  19. &pSectionHeader );
  20. if (pDebugDirectory) {
  21. (*NumberOfDebugDirectories) = DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
  22. return (pDebugDirectory);
  23. } else {
  24. (*NumberOfDebugDirectories) = 0;
  25. return(NULL);
  26. }
  27. }
  28. ///////////////////////////////////////////////////////////////////////////////
  29. //
  30. // Returns true if the image is a resource only dll.
  31. //
  32. // Return values:
  33. // TRUE, FALSE
  34. //
  35. // Parameters:
  36. // PVOID pImageBase (IN)
  37. // BOOOLEAN bMapedAsImage (IN)
  38. //
  39. // [ copied from original SymChk.exe ]
  40. //
  41. BOOL SymCommonResourceOnlyDll(PVOID pImageBase) {
  42. BOOLEAN bMappedAsImage = FALSE;
  43. BOOL fResourceOnlyDll = TRUE;
  44. PVOID pExports,
  45. pImports,
  46. pResources;
  47. DWORD dwExportSize,
  48. dwImportSize,
  49. dwResourceSize;
  50. pExports = ImageDirectoryEntryToData(pImageBase,
  51. bMappedAsImage,
  52. IMAGE_DIRECTORY_ENTRY_EXPORT,
  53. &dwExportSize);
  54. pImports = ImageDirectoryEntryToData(pImageBase,
  55. bMappedAsImage,
  56. IMAGE_DIRECTORY_ENTRY_IMPORT,
  57. &dwImportSize);
  58. pResources= ImageDirectoryEntryToData(pImageBase,
  59. bMappedAsImage,
  60. IMAGE_DIRECTORY_ENTRY_RESOURCE,
  61. &dwResourceSize);
  62. // if resources are found, but imports and exports are not,
  63. // then this is a resource only DLL
  64. if ( (pResources != NULL) &&
  65. (dwResourceSize != 0 ) &&
  66. (pImports == NULL) &&
  67. (dwImportSize == 0 ) && // this check may not be needed
  68. (pExports == NULL) &&
  69. (dwExportSize == 0 ) ) { // this check may not be needed
  70. fResourceOnlyDll = TRUE;
  71. } else {
  72. fResourceOnlyDll = FALSE;
  73. }
  74. return(fResourceOnlyDll);
  75. }
  76. ///////////////////////////////////////////////////////////////////////////////
  77. //
  78. // Returns true if the image is a managed dll built from tlbimp.
  79. //
  80. // Return values:
  81. // TRUE, FALSE
  82. //
  83. // Parameters:
  84. // PVOID pImageBase (IN)
  85. // pointer to image mapping
  86. // PIMAGE_NT_HEADER pNtHEader (IN)
  87. // pointer to image's NT headers
  88. //
  89. // [ copied from original SymChk.exe ]
  90. //
  91. BOOL SymCommonTlbImpManagedDll(PVOID pImageBase, PIMAGE_NT_HEADERS pNtHeader) {
  92. // tlbimp generated binaries have no data, no exports, and only import _CorDllMain from mscoree.dll.
  93. // if this is true, let it through.
  94. BOOL retVal = TRUE;
  95. PVOID pData;
  96. DWORD dwDataSize;
  97. PCHAR pImportModule;
  98. PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor = NULL;
  99. PIMAGE_IMPORT_BY_NAME pImportName = NULL;
  100. pData = ImageDirectoryEntryToData(pImageBase,
  101. FALSE,
  102. IMAGE_DIRECTORY_ENTRY_EXPORT,
  103. &dwDataSize);
  104. if (pData || dwDataSize) {
  105. // exports exist - not a tlbimp output file
  106. retVal = FALSE;
  107. } else {
  108. pData = ImageDirectoryEntryToData(pImageBase,
  109. FALSE,
  110. IMAGE_DIRECTORY_ENTRY_IMPORT,
  111. &dwDataSize);
  112. if (!pData || !dwDataSize) {
  113. // no imports - not a tlbimp output file
  114. retVal = FALSE;
  115. } else {
  116. pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)pData;
  117. if (!pImportDescriptor->Name ||
  118. !pImportDescriptor->OriginalFirstThunk ||
  119. pImportDescriptor->ForwarderChain ||
  120. (pImportDescriptor+1)->Name) {
  121. // Empty/malformed import table or more than just one dll imported.
  122. retVal = FALSE;
  123. } else {
  124. pImportModule = (PCHAR) ImageRvaToVa(pNtHeader, pImageBase, pImportDescriptor->Name, NULL);
  125. if (_memicmp(pImportModule, "mscoree.dll", sizeof("mcsoree.dll"))) {
  126. // Import dll name is not mscoree.dll - not what we're looking for.
  127. retVal = FALSE;
  128. }
  129. }
  130. }
  131. }
  132. // if we haven't invalidated the image yet, keep checking
  133. if (retVal) {
  134. if (pNtHeader->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
  135. // 32bit image
  136. PIMAGE_THUNK_DATA32 pThunkData = (PIMAGE_THUNK_DATA32)ImageRvaToVa(pNtHeader,
  137. pImageBase,
  138. pImportDescriptor->OriginalFirstThunk,
  139. NULL);
  140. if (IMAGE_SNAP_BY_ORDINAL32(pThunkData->u1.Ordinal)) {
  141. // We're looking for a name - not this one.
  142. retVal = FALSE;
  143. } else {
  144. if ((pThunkData+1)->u1.AddressOfData) {
  145. // There's another import after this - that's an error too.
  146. retVal = FALSE;
  147. } else {
  148. // set pImportName for comparison below
  149. pImportName = (PIMAGE_IMPORT_BY_NAME)ImageRvaToVa(pNtHeader,
  150. pImageBase,
  151. pThunkData->u1.AddressOfData,
  152. NULL);
  153. }
  154. }
  155. } else if (pNtHeader->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
  156. // 64-bit image
  157. PIMAGE_THUNK_DATA64 pThunkData = (PIMAGE_THUNK_DATA64)ImageRvaToVa(pNtHeader,
  158. pImageBase,
  159. pImportDescriptor->OriginalFirstThunk,
  160. NULL);
  161. if (IMAGE_SNAP_BY_ORDINAL64(pThunkData->u1.Ordinal)) {
  162. // We're looking for a name - not this one.
  163. retVal = FALSE;
  164. } else {
  165. if ((pThunkData+1)->u1.AddressOfData) {
  166. // There's another import after this - that's an error too.
  167. retVal = FALSE;
  168. } else {
  169. pImportName = (PIMAGE_IMPORT_BY_NAME)ImageRvaToVa(pNtHeader,
  170. pImageBase,
  171. (ULONG)(pThunkData->u1.AddressOfData),
  172. NULL);
  173. }
  174. }
  175. } else {
  176. // unknown image - not what we're looking for
  177. retVal = FALSE;
  178. }
  179. }
  180. // still valid- do the last check
  181. if (retVal) {
  182. if (memcmp(pImportName->Name, "_CorDllMain", sizeof("_CorDllMain"))) {
  183. // The import from mscoree isn't _CorDllMain.
  184. retVal = FALSE;
  185. }
  186. }
  187. return(retVal);
  188. }