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.

270 lines
8.9 KiB

  1. #include <private.h>
  2. #include <globals.h>
  3. // Random debug info modification functions for imagehlp
  4. BOOL
  5. IMAGEAPI
  6. UpdateDebugInfoFile(
  7. LPSTR ImageFileName,
  8. LPSTR SymbolPath,
  9. LPSTR DebugFilePath,
  10. PIMAGE_NT_HEADERS32 NtHeaders
  11. )
  12. {
  13. return UpdateDebugInfoFileEx(
  14. ImageFileName,
  15. SymbolPath,
  16. DebugFilePath,
  17. NtHeaders,
  18. NtHeaders->OptionalHeader.CheckSum);
  19. }
  20. BOOL
  21. IMAGEAPI
  22. UpdateDebugInfoFileEx(
  23. LPSTR ImageFileName,
  24. LPSTR SymbolPath,
  25. LPSTR DebugFilePath,
  26. PIMAGE_NT_HEADERS32 NtHeaders,
  27. DWORD OldCheckSum
  28. )
  29. {
  30. // UnSafe...
  31. HANDLE hDebugFile, hMappedFile;
  32. PVOID MappedAddress;
  33. PIMAGE_SEPARATE_DEBUG_HEADER DbgFileHeader;
  34. DWORD dwError = ERROR_SUCCESS;
  35. #ifdef _BUILDING_UPDDBG_
  36. OSVERSIONINFO OSVerInfo;
  37. OSVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  38. GetVersionEx(&OSVerInfo);
  39. #endif
  40. hDebugFile = FindDebugInfoFile(
  41. ImageFileName,
  42. SymbolPath,
  43. DebugFilePath
  44. );
  45. if ( hDebugFile == NULL ) {
  46. return FALSE;
  47. }
  48. CloseHandle(hDebugFile);
  49. hDebugFile = CreateFile( DebugFilePath,
  50. GENERIC_READ | GENERIC_WRITE,
  51. g.OSVerInfo.dwPlatformId == VER_PLATFORM_WIN32_NT ? (FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE) : (FILE_SHARE_READ | FILE_SHARE_WRITE),
  52. NULL,
  53. OPEN_EXISTING,
  54. 0,
  55. NULL
  56. );
  57. if ( hDebugFile == INVALID_HANDLE_VALUE ) {
  58. return FALSE;
  59. }
  60. hMappedFile = CreateFileMapping(
  61. hDebugFile,
  62. NULL,
  63. PAGE_READWRITE,
  64. 0,
  65. 0,
  66. NULL
  67. );
  68. if ( !hMappedFile ) {
  69. CloseHandle(hDebugFile);
  70. return FALSE;
  71. }
  72. MappedAddress = MapViewOfFile(hMappedFile,
  73. FILE_MAP_WRITE,
  74. 0,
  75. 0,
  76. 0
  77. );
  78. CloseHandle(hMappedFile);
  79. if ( !MappedAddress ) {
  80. CloseHandle(hDebugFile);
  81. return FALSE;
  82. }
  83. DbgFileHeader = (PIMAGE_SEPARATE_DEBUG_HEADER)MappedAddress;
  84. if (DbgFileHeader->ImageBase != NtHeaders->OptionalHeader.ImageBase ||
  85. DbgFileHeader->CheckSum != NtHeaders->OptionalHeader.CheckSum ||
  86. DbgFileHeader->SizeOfImage != NtHeaders->OptionalHeader.SizeOfImage
  87. ) {
  88. DbgFileHeader->ImageBase = NtHeaders->OptionalHeader.ImageBase;
  89. if (OldCheckSum != DbgFileHeader->CheckSum) {
  90. DbgFileHeader->Flags |= IMAGE_SEPARATE_DEBUG_MISMATCH;
  91. dwError = ERROR_INVALID_DATA;
  92. }
  93. DbgFileHeader->CheckSum = NtHeaders->OptionalHeader.CheckSum;
  94. DbgFileHeader->TimeDateStamp = NtHeaders->FileHeader.TimeDateStamp;
  95. DbgFileHeader->SizeOfImage = NtHeaders->OptionalHeader.SizeOfImage;
  96. FlushViewOfFile(MappedAddress,0);
  97. UnmapViewOfFile(MappedAddress);
  98. TouchFileTimes(hDebugFile,NULL);
  99. CloseHandle(hDebugFile);
  100. SetLastError(dwError);
  101. return TRUE;
  102. } else {
  103. FlushViewOfFile(MappedAddress,0);
  104. UnmapViewOfFile(MappedAddress);
  105. CloseHandle(hDebugFile);
  106. return FALSE;
  107. }
  108. }
  109. #ifndef _BUILDING_UPDDBG_
  110. BOOL
  111. IMAGEAPI
  112. RemovePrivateCvSymbolic(
  113. PCHAR DebugData,
  114. PCHAR * NewDebugData,
  115. ULONG * NewDebugSize
  116. )
  117. {
  118. BOOL rc;
  119. rc = RemovePrivateCvSymbolicEx(DebugData, 0, NewDebugData, NewDebugSize);
  120. if (rc && (*NewDebugSize == 0) && (*NewDebugData == DebugData)) {
  121. *NewDebugData = NULL;
  122. }
  123. return(rc);
  124. }
  125. BOOL
  126. IMAGEAPI
  127. RemovePrivateCvSymbolicEx(
  128. PCHAR DebugData,
  129. ULONG DebugSize,
  130. PCHAR * NewDebugData,
  131. ULONG * NewDebugSize
  132. )
  133. {
  134. OMFSignature *CvDebugData, *NewStartCvSig, *NewEndCvSig;
  135. OMFDirEntry *CvDebugDirEntry;
  136. OMFDirHeader *CvDebugDirHead;
  137. unsigned int i, j;
  138. PCHAR NewCvData;
  139. ULONG NewCvSize = 0, NewCvOffset;
  140. BOOL RC = FALSE;
  141. __try {
  142. CvDebugDirHead = NULL;
  143. CvDebugDirEntry = NULL;
  144. CvDebugData = (OMFSignature *)DebugData;
  145. if ((((*(PULONG)(CvDebugData->Signature)) == '90BN') ||
  146. ((*(PULONG)(CvDebugData->Signature)) == '80BN') ||
  147. ((*(PULONG)(CvDebugData->Signature)) == '11BN')) &&
  148. ((CvDebugDirHead = (OMFDirHeader *)((PUCHAR) CvDebugData + CvDebugData->filepos)) != NULL) &&
  149. ((CvDebugDirEntry = (OMFDirEntry *)((PUCHAR) CvDebugDirHead + CvDebugDirHead->cbDirHeader)) != NULL)) {
  150. // Walk the directory. Keep what we want, zero out the rest.
  151. for (i=0, j=0; i < CvDebugDirHead->cDir; i++) {
  152. switch (CvDebugDirEntry[i].SubSection) {
  153. case sstSegMap:
  154. case sstSegName:
  155. case sstOffsetMap16:
  156. case sstOffsetMap32:
  157. case sstModule:
  158. case SSTMODULE:
  159. case SSTPUBLIC:
  160. case sstPublic:
  161. case sstPublicSym:
  162. case sstGlobalPub:
  163. CvDebugDirEntry[j] = CvDebugDirEntry[i];
  164. NewCvSize += CvDebugDirEntry[j].cb;
  165. NewCvSize = (NewCvSize + 3) & ~3;
  166. if (i != j++) {
  167. // Clear the old entry.
  168. RtlZeroMemory(&CvDebugDirEntry[i], CvDebugDirHead->cbDirEntry);
  169. }
  170. break;
  171. default:
  172. RC = TRUE;
  173. RtlZeroMemory(CvDebugDirEntry[i].lfo + (PUCHAR) CvDebugData, CvDebugDirEntry[i].cb);
  174. RtlZeroMemory(&CvDebugDirEntry[i], CvDebugDirHead->cbDirEntry);
  175. break;
  176. }
  177. }
  178. // Now, allocate the new cv data.
  179. CvDebugDirHead->cDir = j;
  180. NewCvSize += (j * CvDebugDirHead->cbDirEntry) + // The directory itself
  181. CvDebugDirHead->cbDirHeader + // The directory header
  182. (sizeof(OMFSignature) * 2); // The signature/offset pairs at each end.
  183. NewCvData = (PCHAR) MemAlloc( NewCvSize );
  184. // And move the stuff we kept into the new section.
  185. NewCvOffset = sizeof(OMFSignature);
  186. RtlCopyMemory(NewCvData + NewCvOffset, CvDebugDirHead, CvDebugDirHead->cbDirHeader);
  187. CvDebugDirHead = (OMFDirHeader *) (NewCvData + NewCvOffset);
  188. NewCvOffset += CvDebugDirHead->cbDirHeader;
  189. RtlCopyMemory(NewCvData + NewCvOffset,
  190. CvDebugDirEntry,
  191. CvDebugDirHead->cDir * CvDebugDirHead->cbDirEntry);
  192. CvDebugDirEntry = (OMFDirEntry *)(NewCvData + NewCvOffset);
  193. NewCvOffset += (CvDebugDirHead->cbDirEntry * CvDebugDirHead->cDir);
  194. for (i=0; i < CvDebugDirHead->cDir; i++) {
  195. RtlCopyMemory(NewCvData + NewCvOffset,
  196. CvDebugDirEntry[i].lfo + (PCHAR) CvDebugData,
  197. CvDebugDirEntry[i].cb);
  198. CvDebugDirEntry[i].lfo = NewCvOffset;
  199. NewCvOffset += (CvDebugDirEntry[i].cb + 3) & ~3;
  200. }
  201. // Re-do the start/end signatures
  202. NewStartCvSig = (OMFSignature *) NewCvData;
  203. NewEndCvSig = (OMFSignature *) ((PCHAR)NewCvData + NewCvOffset);
  204. *(PULONG)(NewStartCvSig->Signature) = *(PULONG)(CvDebugData->Signature);
  205. NewStartCvSig->filepos = (int)((PCHAR)CvDebugDirHead - (PCHAR)NewStartCvSig);
  206. *(PULONG)(NewEndCvSig->Signature) = *(PULONG)(CvDebugData->Signature);
  207. NewCvOffset += sizeof(OMFSignature);
  208. NewEndCvSig->filepos = (LONG)NewCvOffset;
  209. // Set the return values appropriately
  210. *NewDebugData = NewCvData;
  211. *NewDebugSize = NewCvSize;
  212. } else {
  213. if (*(PULONG)(CvDebugData->Signature) == '01BN') {
  214. *NewDebugData = DebugData;
  215. *NewDebugSize = DebugSize;
  216. RC = TRUE;
  217. } else {
  218. // Not NB10, NB09 or NB08. Forget we ever heard of it.
  219. *NewDebugData = DebugData;
  220. *NewDebugSize = 0;
  221. RC = TRUE;
  222. }
  223. }
  224. } __except(EXCEPTION_EXECUTE_HANDLER) {
  225. RC = FALSE;
  226. }
  227. return(RC);
  228. }
  229. #include <copypdb.c>
  230. #endif