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.

344 lines
8.5 KiB

  1. #include <private.h>
  2. WCHAR **SymbolNames;
  3. typedef BOOL ( __cdecl *PPDBOPEN2W )(
  4. const wchar_t *,
  5. LNGNM_CONST char *,
  6. EC *,
  7. wchar_t *,
  8. size_t,
  9. PDB **
  10. );
  11. typedef BOOL ( __cdecl *PPDBCLOSE ) (
  12. PDB* ppdb
  13. );
  14. typedef BOOL (__cdecl *PPDBCOPYTOW2) (
  15. PDB *ppdb,
  16. const wchar_t *szTargetPdb,
  17. DWORD dwCopyFilter,
  18. PfnPDBCopyQueryCallback pfnCallBack,
  19. void * pvClientContext
  20. );
  21. PPDBOPEN2W pPDBOpen2W;
  22. PPDBCLOSE pPDBClose;
  23. PPDBCOPYTOW2 pPDBCopyToW2;
  24. wchar_t NewPdbName[_MAX_FNAME];
  25. CHAR SymbolName[2048];
  26. WCHAR SymbolNameW[2048];
  27. PWCHAR *SymbolsToRemove;
  28. ULONG SymbolCount;
  29. int
  30. __cdecl
  31. MyWcsCmp(const void*Ptr1, const void *Ptr2)
  32. {
  33. wchar_t*String1 = *(wchar_t**)Ptr1;
  34. wchar_t*String2 = *(wchar_t**)Ptr2;
  35. // printf("String1: %p - %ws\nString2: %p - %ws\n%d\n", String1, String1, String2, String2, wcscmp(String1, String2));
  36. return (wcscmp(String1, String2));
  37. }
  38. __cdecl
  39. MyWcsCmp2(const void*Ptr1, const void *Ptr2)
  40. {
  41. wchar_t*String1 = (wchar_t*)Ptr1;
  42. wchar_t*String2 = *(wchar_t**)Ptr2;
  43. // printf("String1: %p - %ws\nString2: %p - %ws\n%d\n", String1, String1, String2, String2, wcscmp(String1, String2));
  44. return (wcscmp(String1, String2));
  45. }
  46. BOOL
  47. LoadNamesFromSymbolFile(
  48. WCHAR *SymbolFileName
  49. )
  50. {
  51. FILE *SymbolFile;
  52. ULONG i;
  53. SymbolFile = _wfopen(SymbolFileName, L"rt");
  54. if (!SymbolFile)
  55. return FALSE;
  56. SymbolCount = 0;
  57. while (fgets(SymbolName,sizeof(SymbolName),SymbolFile)) {
  58. SymbolCount++;
  59. }
  60. if (fseek(SymbolFile, 0, SEEK_SET)) {
  61. fclose(SymbolFile);
  62. return FALSE;
  63. }
  64. SymbolsToRemove = (PWCHAR *) malloc(SymbolCount * sizeof(PWCHAR));
  65. if (!SymbolsToRemove) {
  66. fclose(SymbolFile);
  67. return FALSE;
  68. }
  69. SymbolCount = 0;
  70. while (fgets(SymbolName,sizeof(SymbolName),SymbolFile)) {
  71. // Remove trailing spaces
  72. if (strlen(SymbolName)) {
  73. PCHAR SymPtr = SymbolName + strlen(SymbolName) - 1;
  74. while ((*SymPtr == ' ') || (*SymPtr == '\r') || (*SymPtr == '\n')) {
  75. *SymPtr = '\0';
  76. SymPtr--;
  77. }
  78. }
  79. if (MultiByteToWideChar(CP_ACP,
  80. MB_PRECOMPOSED,
  81. SymbolName,
  82. -1,
  83. SymbolNameW,
  84. sizeof(SymbolNameW)/sizeof(SymbolNameW[0])) == 0) {
  85. fclose(SymbolFile);
  86. return FALSE;
  87. }
  88. SymbolsToRemove[SymbolCount] = malloc((wcslen(SymbolNameW) + 1) * sizeof(WCHAR));
  89. if (!SymbolsToRemove[SymbolCount]) {
  90. fclose(SymbolFile);
  91. return FALSE;
  92. }
  93. wcscpy(SymbolsToRemove[SymbolCount], SymbolNameW);
  94. SymbolCount++;
  95. }
  96. fclose(SymbolFile);
  97. qsort(&SymbolsToRemove[0], SymbolCount, sizeof(PWCHAR), MyWcsCmp);
  98. return TRUE;
  99. }
  100. BOOL PDBCALL PdbCopyFilterPublics (
  101. void * pvClientContext,
  102. DWORD dwFilterFlags,
  103. unsigned int offPublic,
  104. unsigned int sectPublic,
  105. unsigned int grfPublic,
  106. const wchar_t * szPublic,
  107. wchar_t ** pszNewPublic
  108. ) {
  109. static size_t cPubs = 0;
  110. static wchar_t wszNewName[2048];
  111. if (SymbolCount > 1) {
  112. if (bsearch(szPublic, &SymbolsToRemove[0], SymbolCount, sizeof(PWCHAR), MyWcsCmp2)) {
  113. return FALSE;
  114. }
  115. } else {
  116. if (!wcscmp(szPublic, *SymbolsToRemove)) {
  117. return FALSE;
  118. }
  119. }
  120. return TRUE;
  121. // if ((cPubs++ % 16) == 0) {
  122. // if ((cPubs % 32) == 1) {
  123. // wcscpy(wszNewName, szPublic);
  124. // wcscat(wszNewName, L"_BobsYerUncle");
  125. // *pszNewPublic = wszNewName;
  126. // }
  127. // return TRUE;
  128. // }
  129. // else {
  130. // return FALSE;
  131. // }
  132. }
  133. PDBCOPYCALLBACK PDBCALL PdbCopyQueryCallback(void * pv, PCC pccQuery) {
  134. switch (pccQuery) {
  135. case pccFilterPublics:
  136. return (PDBCOPYCALLBACK) PdbCopyFilterPublics;
  137. break;
  138. default:
  139. return NULL;
  140. }
  141. }
  142. void Usage(void)
  143. {
  144. _putws(L"Usage: removesym -d:<pdbdll path> -p:<pdbname> {-s:<symbol to remove> | -f:<file with symbols to remove>}");
  145. }
  146. const WCHAR szMsPdb60[] = L"\\mspdb60.dll";
  147. const WCHAR szMsPdb70[] = L"\\mspdb70.dll";
  148. int __cdecl wmain(int argc, wchar_t *argv[])
  149. {
  150. WCHAR *szPdbName = NULL;
  151. WCHAR *szNewPdbName = NULL;
  152. WCHAR *pSymbolName = NULL;
  153. WCHAR *pSymbolFileName = NULL;
  154. HINSTANCE hmodMsPdb;
  155. BOOL rc;
  156. LONG ErrorCode;
  157. WCHAR ErrorString[1024];
  158. PDB * pSrcPdb;
  159. int i;
  160. WCHAR PdbDllName[_MAX_PATH] = {0};
  161. WCHAR *pPdbDllPath = NULL;
  162. WCHAR const *pPdbDllName = szMsPdb60;
  163. // Grab the arguments
  164. if (argc < 4) {
  165. Usage();
  166. return 1;
  167. }
  168. i = 1;
  169. while (i < argc)
  170. {
  171. if (argv[i][0] == L'-' || argv[i][0] == L'/') {
  172. if (argv[i][1] == L'p' && argv[i][2] == ':') {
  173. // Pdb Name
  174. szPdbName = &argv[i][3];
  175. } else
  176. if (argv[i][1] == L's' && argv[i][2] == ':') {
  177. // Single Symbol name
  178. pSymbolName = &argv[i][3];
  179. } else
  180. if (argv[i][1] == L'f' && argv[i][2] == ':') {
  181. // File with symbol names
  182. pSymbolFileName = &argv[i][3];
  183. } else
  184. if (argv[i][1] == L'd' && argv[i][2] == ':') {
  185. // Single Symbol name
  186. pPdbDllPath = &argv[i][3];
  187. } else {
  188. Usage();
  189. return 1;
  190. }
  191. }
  192. i++;
  193. }
  194. if (!szPdbName) {
  195. _putws(L"Pdb name missing");
  196. Usage();
  197. return 1;
  198. }
  199. if (!pSymbolName && !pSymbolFileName) {
  200. _putws(L"Symbol name or file missing");
  201. Usage();
  202. return 1;
  203. }
  204. if (pSymbolName && pSymbolFileName) {
  205. _putws(L"Symbol name and symbol file specified - only one allowed");
  206. Usage();
  207. return 1;
  208. }
  209. if (!pPdbDllPath) {
  210. _putws(L"Pdb DllPath not specified");
  211. Usage();
  212. return 1;
  213. }
  214. // If there's a symfile, load it
  215. if (pSymbolFileName) {
  216. rc = LoadNamesFromSymbolFile(pSymbolFileName);
  217. if (!rc) {
  218. _putws(L"Unable do load names from symbol file");
  219. exit(1);
  220. }
  221. } else {
  222. SymbolsToRemove = &pSymbolName;
  223. SymbolCount = 1;
  224. }
  225. Retry:
  226. rc = FALSE;
  227. // Load mspdb60.dll and the necessar api's
  228. wcscpy(PdbDllName, pPdbDllPath);
  229. wcscat(PdbDllName, pPdbDllName);
  230. hmodMsPdb = LoadLibraryW(PdbDllName);
  231. if (!hmodMsPdb) {
  232. wprintf(L"Unable to loadlib %s\n", PdbDllName);
  233. goto Exit;
  234. }
  235. pPDBOpen2W = (PPDBOPEN2W) GetProcAddress(hmodMsPdb, "PDBOpen2W");
  236. if (!pPDBOpen2W) {
  237. wprintf(L"Unable to find PDBOpen2W in %s\n", PdbDllName);
  238. goto Exit;
  239. }
  240. pPDBClose = (PPDBCLOSE) GetProcAddress(hmodMsPdb, "PDBClose");
  241. if (!pPDBClose) {
  242. wprintf(L"Unable to find PDBClose in %s\n", PdbDllName);
  243. goto Exit;
  244. }
  245. pPDBCopyToW2 = (PPDBCOPYTOW2) GetProcAddress(hmodMsPdb, "PDBCopyToW2");
  246. if (!pPDBCopyToW2) {
  247. wprintf(L"Unable to find PDBCopyToW2 in %s\n", PdbDllName);
  248. goto Exit;
  249. }
  250. __try {
  251. rc = pPDBOpen2W(szPdbName, "r", &ErrorCode, ErrorString, sizeof(ErrorString) / sizeof(ErrorString[0]), &pSrcPdb);
  252. if (!rc) {
  253. wprintf(L"Unable to open pdb for changes with %s\n", PdbDllName);
  254. __leave;
  255. }
  256. szNewPdbName = _wtmpnam(NewPdbName);
  257. rc = pPDBCopyToW2(pSrcPdb, szNewPdbName, 1, PdbCopyQueryCallback, NULL);
  258. pPDBClose(pSrcPdb);
  259. if (!rc) {
  260. _putws(L"CopyTo operation failed");
  261. __leave;
  262. }
  263. rc = CopyFile(szNewPdbName, szPdbName, FALSE);
  264. if (!rc) {
  265. _putws(L"Unable to overwrite old pdb with new pdb");
  266. }
  267. DeleteFile(szNewPdbName);
  268. } __except (EXCEPTION_EXECUTE_HANDLER) {
  269. _putws(L"Exception occured in pdb api's");
  270. rc = FALSE;
  271. }
  272. Exit:
  273. if (hmodMsPdb) {
  274. FreeLibrary(hmodMsPdb);
  275. hmodMsPdb = NULL;
  276. }
  277. if (!rc) {
  278. if (pPdbDllName == szMsPdb60) {
  279. pPdbDllName = szMsPdb70;
  280. goto Retry;
  281. }
  282. }
  283. return(rc);
  284. }