Source code of Windows XP (NT5)
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.

370 lines
10 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. BOOL
  11. Match(
  12. char *Pattern,
  13. char *Text
  14. )
  15. {
  16. switch (*Pattern) {
  17. case '\0':
  18. return *Text == '\0';
  19. case '?':
  20. return *Text != '\0' && Match( Pattern + 1, Text + 1 );
  21. case '*':
  22. do {
  23. if (Match( Pattern + 1, Text ))
  24. return TRUE;
  25. }
  26. while (*Text++);
  27. return FALSE;
  28. default:
  29. return toupper( *Text ) == toupper( *Pattern ) && Match( Pattern + 1, Text + 1 );
  30. }
  31. }
  32. BOOL
  33. AnyMatches(
  34. char *Name,
  35. int *NumList,
  36. int Length,
  37. char **StringList
  38. )
  39. {
  40. if (Length == 0) {
  41. return FALSE;
  42. }
  43. return (Match( StringList[ NumList[ 0 ] ], Name ) ||
  44. AnyMatches( Name, NumList + 1, Length - 1, StringList )
  45. );
  46. }
  47. BOOL
  48. BindStatusRoutine(
  49. IMAGEHLP_STATUS_REASON Reason,
  50. LPSTR ImageName,
  51. LPSTR DllName,
  52. PVOID Va,
  53. ULONG_PTR Parameter
  54. );
  55. #define BIND_ERR 99
  56. #define BIND_OK 0
  57. PCHAR SymbolPath;
  58. BOOL fVerbose;
  59. BOOL fNoUpdate = TRUE;
  60. BOOL fDisableNewImports;
  61. BOOL fNoCacheImportDlls;
  62. BOOL fBindSysImages;
  63. DWORD BindFlags;
  64. int __cdecl
  65. main(
  66. int argc,
  67. char *argv[],
  68. char *envp[]
  69. )
  70. {
  71. char c, *p;
  72. int ExcludeList[256];
  73. int ExcludeListLength = 0;
  74. BOOL fUsage = FALSE;
  75. LPSTR DllPath;
  76. LPSTR CurrentImageName;
  77. int ArgNumber = argc;
  78. char **ArgList = argv;
  79. envp;
  80. DllPath = NULL;
  81. CurrentImageName = NULL;
  82. if (argc < 2) {
  83. goto usage;
  84. }
  85. while (--argc) {
  86. p = *++argv;
  87. if (*p == '/' || *p == '-') {
  88. while (c = *++p)
  89. switch (toupper( c )) {
  90. case '?':
  91. fUsage = TRUE;
  92. break;
  93. case 'C':
  94. fNoCacheImportDlls = TRUE;
  95. break;
  96. case 'O':
  97. fDisableNewImports = TRUE;
  98. break;
  99. case 'P':
  100. if (--argc) {
  101. DllPath = *++argv;
  102. } else {
  103. fprintf( stderr, "BIND: Parameter missing for /%c\n", c );
  104. fUsage = TRUE;
  105. }
  106. break;
  107. case 'S':
  108. if (--argc) {
  109. SymbolPath = *++argv;
  110. } else {
  111. fprintf( stderr, "BIND: Parameter missing for /%c\n", c );
  112. fUsage = TRUE;
  113. }
  114. break;
  115. case 'U':
  116. fNoUpdate = FALSE;
  117. break;
  118. case 'V':
  119. fVerbose = TRUE;
  120. break;
  121. case 'X' :
  122. if (--argc) {
  123. ++argv;
  124. ExcludeList[ExcludeListLength] = ArgNumber - argc;
  125. ExcludeListLength++;
  126. } else {
  127. fprintf( stderr, "BIND: Parameter missing for /%c\n", c );
  128. fUsage = TRUE;
  129. }
  130. break;
  131. case 'Y':
  132. fBindSysImages = TRUE;
  133. break;
  134. default:
  135. fprintf( stderr, "BIND: Invalid switch - /%c\n", c );
  136. fUsage = TRUE;
  137. break;
  138. }
  139. if (fUsage) {
  140. usage:
  141. fputs("usage: BIND [switches] image-names... \n"
  142. " [-?] display this message\n"
  143. " [-c] no caching of import dlls\n"
  144. " [-o] disable new import descriptors\n"
  145. " [-p dll search path]\n"
  146. " [-s Symbol directory] update any associated .DBG file\n"
  147. " [-u] update the image\n"
  148. " [-v] verbose output\n"
  149. " [-x image name] exclude this image from binding\n"
  150. " [-y] allow binding on images located above 2G",
  151. stderr
  152. );
  153. return BIND_ERR;
  154. }
  155. } else {
  156. CurrentImageName = p;
  157. if (fVerbose) {
  158. fprintf( stdout,
  159. "BIND: binding %s using DllPath %s\n",
  160. CurrentImageName,
  161. DllPath ? DllPath : "Default"
  162. );
  163. }
  164. if (AnyMatches( CurrentImageName, ExcludeList, ExcludeListLength, ArgList )) {
  165. if (fVerbose) {
  166. fprintf( stdout, "BIND: skipping %s\n", CurrentImageName );
  167. }
  168. } else {
  169. BindFlags = 0;
  170. if (!fNoCacheImportDlls) {
  171. // Always cache across calls unless the user indicates otherwise.
  172. BindFlags |= BIND_CACHE_IMPORT_DLLS;
  173. }
  174. if (fNoUpdate) {
  175. BindFlags |= BIND_NO_UPDATE;
  176. }
  177. if (fDisableNewImports) {
  178. BindFlags |= BIND_NO_BOUND_IMPORTS;
  179. }
  180. if (fBindSysImages) {
  181. BindFlags |= BIND_ALL_IMAGES;
  182. }
  183. BindImageEx( BindFlags,
  184. CurrentImageName,
  185. DllPath,
  186. SymbolPath,
  187. (PIMAGEHLP_STATUS_ROUTINE)BindStatusRoutine
  188. );
  189. }
  190. }
  191. }
  192. return BIND_OK;
  193. }
  194. BOOL
  195. BindStatusRoutine(
  196. IMAGEHLP_STATUS_REASON Reason,
  197. LPSTR ImageName,
  198. LPSTR DllName,
  199. PVOID Va,
  200. ULONG_PTR Parameter
  201. )
  202. {
  203. PIMAGE_BOUND_IMPORT_DESCRIPTOR NewImports, NewImport;
  204. PIMAGE_BOUND_FORWARDER_REF NewForwarder;
  205. UINT i;
  206. switch( Reason ) {
  207. case BindOutOfMemory:
  208. fprintf( stderr, "BIND: Out of memory - needed %u bytes.\n", Parameter );
  209. ExitProcess( 1 );
  210. case BindRvaToVaFailed:
  211. fprintf( stderr, "BIND: %s contains invalid Rva - %p\n", ImageName, Va );
  212. break;
  213. case BindNoRoomInImage:
  214. fprintf( stderr,
  215. "BIND: Not enough room for new format import table. Defaulting to unbound image.\n"
  216. );
  217. break;
  218. case BindImportModuleFailed:
  219. fprintf( stderr,"BIND: %s - Unable to find %s\n", ImageName, DllName );
  220. break;
  221. case BindImportProcedureFailed:
  222. fprintf( stderr,
  223. "BIND: %s - %s entry point not found in %s\n",
  224. ImageName,
  225. Parameter,
  226. DllName
  227. );
  228. break;
  229. case BindImportModule:
  230. if (fVerbose) {
  231. fprintf( stderr,"BIND: %s - Imports from %s\n", ImageName, DllName );
  232. }
  233. break;
  234. case BindImportProcedure:
  235. if (fVerbose) {
  236. fprintf( stderr,
  237. "BIND: %s - %s Bound to %p\n",
  238. ImageName,
  239. Parameter,
  240. Va
  241. );
  242. }
  243. break;
  244. case BindForwarder:
  245. if (fVerbose) {
  246. fprintf( stderr, "BIND: %s - %s forwarded to %s [%p]\n",
  247. ImageName,
  248. DllName,
  249. Parameter,
  250. Va
  251. );
  252. }
  253. break;
  254. case BindForwarderNOT:
  255. if (fVerbose) {
  256. fprintf( stderr,
  257. "BIND: %s - Forwarder %s not snapped [%p]\n",
  258. ImageName,
  259. Parameter,
  260. Va
  261. );
  262. }
  263. break;
  264. case BindImageModified:
  265. fprintf( stdout, "BIND: binding %s\n", ImageName );
  266. break;
  267. case BindExpandFileHeaders:
  268. if (fVerbose) {
  269. fprintf( stderr,
  270. " Expanded %s file headers to %x\n",
  271. ImageName,
  272. Parameter
  273. );
  274. }
  275. break;
  276. case BindMismatchedSymbols:
  277. fprintf(stderr, "BIND: Warning: %s checksum did not match %s\n",
  278. ImageName,
  279. (LPSTR)Parameter);
  280. break;
  281. case BindSymbolsNotUpdated:
  282. fprintf(stderr, "BIND: Warning: symbol file %s not updated.\n",
  283. (LPSTR)Parameter);
  284. break;
  285. case BindImageComplete:
  286. if (fVerbose) {
  287. fprintf(stderr, "BIND: Details of binding of %s\n", ImageName );
  288. NewImports = (PIMAGE_BOUND_IMPORT_DESCRIPTOR)Va;
  289. NewImport = NewImports;
  290. while (NewImport->OffsetModuleName) {
  291. fprintf( stderr, " Import from %s [%x]",
  292. (LPSTR)NewImports + NewImport->OffsetModuleName,
  293. NewImport->TimeDateStamp
  294. );
  295. if (NewImport->NumberOfModuleForwarderRefs != 0) {
  296. fprintf( stderr, " with %u forwarders", NewImport->NumberOfModuleForwarderRefs );
  297. }
  298. fprintf( stderr, "\n" );
  299. NewForwarder = (PIMAGE_BOUND_FORWARDER_REF)(NewImport+1);
  300. for ( i=0; i<NewImport->NumberOfModuleForwarderRefs; i++ ) {
  301. fprintf( stderr, " Forward to %s [%x]\n",
  302. (LPSTR)NewImports + NewForwarder->OffsetModuleName,
  303. NewForwarder->TimeDateStamp
  304. );
  305. NewForwarder += 1;
  306. }
  307. NewImport = (PIMAGE_BOUND_IMPORT_DESCRIPTOR)NewForwarder;
  308. }
  309. }
  310. break;
  311. }
  312. return TRUE;
  313. }