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.

509 lines
14 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. bind.c
  5. Abstract:
  6. Author:
  7. Revision History:
  8. --*/
  9. #include <private.h>
  10. #define STANDALONE_BIND
  11. BOOL
  12. Match(
  13. char *Pattern,
  14. char *Text
  15. )
  16. {
  17. switch (*Pattern) {
  18. case '\0':
  19. return *Text == '\0';
  20. case '?':
  21. return *Text != '\0' && Match( Pattern + 1, Text + 1 );
  22. case '*':
  23. do {
  24. if (Match( Pattern + 1, Text ))
  25. return TRUE;
  26. }
  27. while (*Text++);
  28. return FALSE;
  29. default:
  30. return toupper( *Text ) == toupper( *Pattern ) && Match( Pattern + 1, Text + 1 );
  31. }
  32. }
  33. BOOL
  34. AnyMatches(
  35. char *Name,
  36. int *NumList,
  37. int Length,
  38. char **StringList
  39. )
  40. {
  41. if (Length == 0) {
  42. return FALSE;
  43. }
  44. return (Match( StringList[ NumList[ 0 ] ], Name ) ||
  45. AnyMatches( Name, NumList + 1, Length - 1, StringList )
  46. );
  47. }
  48. BOOL
  49. BindStatusRoutine(
  50. IMAGEHLP_STATUS_REASON Reason,
  51. LPSTR ImageName,
  52. LPSTR DllName,
  53. ULONG64 Va,
  54. ULONG_PTR Parameter
  55. );
  56. #define BIND_ERR 99
  57. #define BIND_OK 0
  58. PCHAR SymbolPath;
  59. BOOL fVerbose;
  60. BOOL fNoUpdate = TRUE;
  61. BOOL fDisableNewImports;
  62. BOOL fNoCacheImportDlls;
  63. BOOL fBindSysImages;
  64. DWORD BindFlags;
  65. #ifndef _WIN64
  66. BOOL
  67. BindStatusRoutine32(
  68. IMAGEHLP_STATUS_REASON Reason,
  69. LPSTR ImageName,
  70. LPSTR DllName,
  71. ULONG Va,
  72. ULONG Parameter
  73. )
  74. {
  75. return BindStatusRoutine(Reason, ImageName, DllName, Va, Parameter);
  76. }
  77. #endif
  78. int ExcludeList[256];
  79. int ExcludeListLength = 0;
  80. LPSTR DllPath;
  81. LPSTR CurrentImageName;
  82. char **ArgList;
  83. DWORD dwVersion;
  84. void DoBind(char *p);
  85. int __cdecl
  86. main(
  87. int argc,
  88. char *argv[]
  89. )
  90. {
  91. char c, *p;
  92. BOOL fUsage = FALSE;
  93. int ArgNumber = argc;
  94. ArgList = argv;
  95. DllPath = NULL;
  96. CurrentImageName = NULL;
  97. if (argc < 2) {
  98. goto usage;
  99. }
  100. setvbuf(stdout, NULL, _IONBF, 0);
  101. setvbuf(stderr, NULL, _IONBF, 0);
  102. while (--argc) {
  103. p = *++argv;
  104. if (*p == '/' || *p == '-') {
  105. while (c = *++p)
  106. switch (toupper( c )) {
  107. case '?':
  108. fUsage = TRUE;
  109. break;
  110. case 'C':
  111. fNoCacheImportDlls = TRUE;
  112. break;
  113. case 'O':
  114. fDisableNewImports = TRUE;
  115. break;
  116. case 'P':
  117. if (--argc) {
  118. DllPath = *++argv;
  119. } else {
  120. fprintf( stderr, "BIND: Parameter missing for /%c\n", c );
  121. fUsage = TRUE;
  122. }
  123. break;
  124. case 'S':
  125. if (--argc) {
  126. SymbolPath = *++argv;
  127. } else {
  128. fprintf( stderr, "BIND: Parameter missing for /%c\n", c );
  129. fUsage = TRUE;
  130. }
  131. break;
  132. case 'U':
  133. fNoUpdate = FALSE;
  134. break;
  135. case 'V':
  136. fVerbose = TRUE;
  137. break;
  138. case 'X' :
  139. if (--argc) {
  140. ++argv;
  141. ExcludeList[ExcludeListLength] = ArgNumber - argc;
  142. ExcludeListLength++;
  143. } else {
  144. fprintf( stderr, "BIND: Parameter missing for /%c\n", c );
  145. fUsage = TRUE;
  146. }
  147. break;
  148. case 'Y':
  149. fBindSysImages = TRUE;
  150. break;
  151. default:
  152. fprintf( stderr, "BIND: Invalid switch - /%c\n", c );
  153. fUsage = TRUE;
  154. break;
  155. }
  156. if (fUsage) {
  157. usage:
  158. fputs("usage: BIND [switches] image-names... \n"
  159. " [-?] display this message\n"
  160. " [-c] no caching of import dlls\n"
  161. " [-o] disable new import descriptors\n"
  162. " [-p dll search path]\n"
  163. " [-s Symbol directory] update any associated .DBG file\n"
  164. " [-u] update the image\n"
  165. " [-v] verbose output\n"
  166. " [-x image name] exclude this image from binding\n"
  167. " [-y] allow binding on images located above 2G",
  168. stderr
  169. );
  170. return BIND_ERR;
  171. }
  172. } else {
  173. BindFlags = 0;
  174. if (!fNoCacheImportDlls) {
  175. // Always cache across calls unless the user indicates otherwise.
  176. BindFlags |= BIND_CACHE_IMPORT_DLLS;
  177. }
  178. if (fNoUpdate) {
  179. BindFlags |= BIND_NO_UPDATE;
  180. }
  181. if (fDisableNewImports) {
  182. BindFlags |= BIND_NO_BOUND_IMPORTS;
  183. }
  184. if (fBindSysImages) {
  185. BindFlags |= BIND_ALL_IMAGES;
  186. }
  187. dwVersion = GetVersion();
  188. #if !defined(_WIN64) && !defined(STANDALONE_BIND)
  189. if ((HIWORD(dwVersion) & 0x3fff) > 3600) {
  190. // NT build > 3600 - supports 64-bit VA's on X86
  191. BindFlags |= BIND_REPORT_64BIT_VA;
  192. }
  193. #endif
  194. if (*p == '@') {
  195. FILE *hFiles;
  196. int ScanRet;
  197. CHAR pchFileName[_MAX_PATH];
  198. p++;
  199. hFiles=fopen(p, "rt");
  200. if (hFiles == NULL) {
  201. fprintf( stderr, "BIND: fopen %s failed %d\n", p, errno );
  202. ExitProcess( BIND_ERR );
  203. }
  204. ScanRet = fscanf( hFiles, "%s", pchFileName);
  205. while (ScanRet && ScanRet != EOF) {
  206. DoBind(pchFileName);
  207. ScanRet = fscanf( hFiles, "%s", pchFileName );
  208. }
  209. } else {
  210. DoBind(p);
  211. }
  212. }
  213. }
  214. return BIND_OK;
  215. }
  216. void
  217. DoBind(char *p)
  218. {
  219. CurrentImageName = p;
  220. if (fVerbose) {
  221. fprintf( stdout,
  222. "BIND: binding %s using DllPath %s\n",
  223. CurrentImageName,
  224. DllPath ? DllPath : "Default"
  225. );
  226. }
  227. if (AnyMatches( CurrentImageName, ExcludeList, ExcludeListLength, ArgList )) {
  228. if (fVerbose) {
  229. fprintf( stdout, "BIND: skipping %s\n", CurrentImageName );
  230. }
  231. } else {
  232. #if !defined(_WIN64) && !defined(STANDALONE_BIND)
  233. {
  234. if ((HIWORD(dwVersion) & 0x3fff) > 3600) {
  235. // NT build > 3600 - supports 64-bit VA's on X86
  236. BindImageEx( BindFlags,
  237. CurrentImageName,
  238. DllPath,
  239. SymbolPath,
  240. (PIMAGEHLP_STATUS_ROUTINE)BindStatusRoutine
  241. );
  242. } else {
  243. BindImageEx( BindFlags,
  244. CurrentImageName,
  245. DllPath,
  246. SymbolPath,
  247. (PIMAGEHLP_STATUS_ROUTINE)BindStatusRoutine32
  248. );
  249. }
  250. }
  251. #else
  252. BindFlags |= BIND_REPORT_64BIT_VA;
  253. BindImageEx( BindFlags,
  254. CurrentImageName,
  255. DllPath,
  256. SymbolPath,
  257. (PIMAGEHLP_STATUS_ROUTINE)BindStatusRoutine
  258. );
  259. #endif
  260. }
  261. }
  262. BOOL
  263. BindStatusRoutine(
  264. IMAGEHLP_STATUS_REASON Reason,
  265. LPSTR ImageName,
  266. LPSTR DllName,
  267. ULONG64 Va,
  268. ULONG_PTR Parameter
  269. )
  270. {
  271. PIMAGE_BOUND_IMPORT_DESCRIPTOR NewImports, NewImport;
  272. PIMAGE_BOUND_FORWARDER_REF NewForwarder;
  273. UINT i;
  274. switch( Reason ) {
  275. case BindOutOfMemory:
  276. fprintf( stderr, "BIND: Out of memory - needed %u bytes.\n", Parameter );
  277. ExitProcess( 1 );
  278. case BindRvaToVaFailed:
  279. fprintf( stderr, "BIND: %s contains invalid Rva - %08.8X\n", ImageName, (ULONG)Va );
  280. break;
  281. case BindNoRoomInImage:
  282. fprintf( stderr,
  283. "BIND: Not enough room for new format import table. Defaulting to unbound image.\n"
  284. );
  285. break;
  286. case BindImportModuleFailed:
  287. fprintf( stderr,"BIND: %s - Unable to find %s\n", ImageName, DllName );
  288. break;
  289. case BindImportProcedureFailed:
  290. fprintf( stderr,
  291. "BIND: %s - %s entry point not found in %s\n",
  292. ImageName,
  293. (char *)Parameter,
  294. DllName
  295. );
  296. break;
  297. case BindImportModule:
  298. if (fVerbose) {
  299. fprintf( stderr,"BIND: %s - Imports from %s\n", ImageName, DllName );
  300. }
  301. break;
  302. case BindImportProcedure64:
  303. #ifdef _WIN64
  304. case BindImportProcedure:
  305. #endif
  306. if (fVerbose) {
  307. fprintf( stderr,
  308. "BIND: %s - %s Bound to %16.16I64X\n",
  309. ImageName,
  310. (char *)Parameter,
  311. Va
  312. );
  313. }
  314. break;
  315. case BindImportProcedure32:
  316. #ifndef _WIN64
  317. case BindImportProcedure:
  318. #endif
  319. if (fVerbose) {
  320. fprintf( stderr,
  321. "BIND: %s - %s Bound to %08.8X\n",
  322. ImageName,
  323. (char *)Parameter,
  324. (ULONG)Va
  325. );
  326. }
  327. break;
  328. case BindForwarder64:
  329. #ifdef _WIN64
  330. case BindForwarder:
  331. #endif
  332. if (fVerbose) {
  333. fprintf( stderr, "BIND: %s - %s forwarded to %s [%16.16I64X]\n",
  334. ImageName,
  335. DllName,
  336. (char *)Parameter,
  337. Va
  338. );
  339. }
  340. break;
  341. case BindForwarder32:
  342. #ifndef _WIN64
  343. case BindForwarder:
  344. #endif
  345. if (fVerbose) {
  346. fprintf( stderr, "BIND: %s - %s forwarded to %s [%08.8X]\n",
  347. ImageName,
  348. DllName,
  349. (char *)Parameter,
  350. Va
  351. );
  352. }
  353. break;
  354. case BindForwarderNOT64:
  355. #ifdef _WIN64
  356. case BindForwarderNOT:
  357. #endif
  358. if (fVerbose) {
  359. fprintf( stderr,
  360. "BIND: %s - Forwarder %s not snapped [%16.16I64X]\n",
  361. ImageName,
  362. (char *)Parameter,
  363. Va
  364. );
  365. }
  366. break;
  367. case BindForwarderNOT32:
  368. #ifndef _WIN64
  369. case BindForwarderNOT:
  370. #endif
  371. if (fVerbose) {
  372. fprintf( stderr,
  373. "BIND: %s - Forwarder %s not snapped [%08.8X]\n",
  374. ImageName,
  375. (char *)Parameter,
  376. Va
  377. );
  378. }
  379. break;
  380. case BindImageModified:
  381. fprintf( stdout, "BIND: binding %s\n", ImageName );
  382. break;
  383. case BindExpandFileHeaders:
  384. if (fVerbose) {
  385. fprintf( stderr,
  386. " Expanded %s file headers to %x\n",
  387. ImageName,
  388. Parameter
  389. );
  390. }
  391. break;
  392. case BindMismatchedSymbols:
  393. fprintf(stderr, "BIND: Warning: %s checksum did not match %s\n",
  394. ImageName,
  395. (LPSTR)Parameter);
  396. break;
  397. case BindSymbolsNotUpdated:
  398. fprintf(stderr, "BIND: Warning: symbol file %s not updated.\n",
  399. (LPSTR)Parameter);
  400. break;
  401. case BindImageComplete:
  402. if (fVerbose) {
  403. fprintf(stderr, "BIND: Details of binding of %s\n", ImageName );
  404. NewImports = (PIMAGE_BOUND_IMPORT_DESCRIPTOR)Va;
  405. NewImport = NewImports;
  406. while (NewImport->OffsetModuleName) {
  407. fprintf( stderr, " Import from %s [%x]",
  408. (LPSTR)NewImports + NewImport->OffsetModuleName,
  409. NewImport->TimeDateStamp
  410. );
  411. if (NewImport->NumberOfModuleForwarderRefs != 0) {
  412. fprintf( stderr, " with %u forwarders", NewImport->NumberOfModuleForwarderRefs );
  413. }
  414. fprintf( stderr, "\n" );
  415. NewForwarder = (PIMAGE_BOUND_FORWARDER_REF)(NewImport+1);
  416. for ( i=0; i<NewImport->NumberOfModuleForwarderRefs; i++ ) {
  417. fprintf( stderr, " Forward to %s [%x]\n",
  418. (LPSTR)NewImports + NewForwarder->OffsetModuleName,
  419. NewForwarder->TimeDateStamp
  420. );
  421. NewForwarder += 1;
  422. }
  423. NewImport = (PIMAGE_BOUND_IMPORT_DESCRIPTOR)NewForwarder;
  424. }
  425. }
  426. break;
  427. default:
  428. break;
  429. }
  430. return TRUE;
  431. }
  432. #include <bindi.c>
  433. #define STANDALONE_MAP
  434. #include <mapi.c>