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.

345 lines
11 KiB

  1. /*
  2. This program stabilizes exports by creating stubs
  3. that jump to the actual exports, and makes these stubs the exports.
  4. It is assumed that the stubs will end up at the front of the image.
  5. This tool uses Vulcan, http://vulcan
  6. */
  7. #include "stdinc.h" /* from resourcetool */
  8. #include "yvals.h"
  9. #pragma warning(disable:4100) /* unused parameter */
  10. #pragma warning(disable:4663) /* warning in std headers about language change */
  11. #pragma warning(disable:4511) /* warning in std headers about inability to generate function */
  12. #pragma warning(disable:4512) /* warning in std headers about inability to generate function */
  13. #include <stdio.h>
  14. #include <limits.h>
  15. #include <string>
  16. #include <stdarg.h>
  17. #include "vulcanapi.h"
  18. #include "windows.h"
  19. #include "handle.h" /* from resourcetool */
  20. #define FormatError _snprintf
  21. #define NUMBER_OF(x) (sizeof(x)/sizeof((x)[0]))
  22. static const char File[] = __FILE__;
  23. class String : public std::string
  24. {
  25. typedef std::string Base;
  26. public:
  27. ~String() { }
  28. String() { }
  29. String(const char * s) : Base(s) { }
  30. String(const String & s) : Base(s) { }
  31. String(const Base & s) : Base(s) { }
  32. String(const_iterator i, const_iterator j) : Base(i, j) { }
  33. void operator=(const Base & s) { Base::operator=(s); }
  34. void operator=(const char * s) { Base::operator=(s); }
  35. operator const char * () const { return c_str(); }
  36. };
  37. void __cdecl Error(const char * format, ...)
  38. {
  39. va_list args;
  40. va_start(args, format);
  41. vfprintf(stderr, format, args);
  42. va_end(args);
  43. abort();
  44. }
  45. void ResourceToolAssertFailed(const char* Expression, const char* File, unsigned long Line)
  46. {
  47. fprintf(stderr, "ASSERTION FAILURE: File %s, Line %lu, Expression %s\n", File, Line, Expression);
  48. abort();
  49. }
  50. bool FileExists(const char * s)
  51. {
  52. DWORD dw;
  53. dw = GetFileAttributesA(s);
  54. if (dw == 0xFFFFFFFF)
  55. return false;
  56. if (dw & FILE_ATTRIBUTE_DIRECTORY)
  57. return false;
  58. return true;
  59. }
  60. bool IsDotOrDotDot(const wchar_t* s) { return (s[0] == '.' && ((s[1] == 0) || (s[1] == '.' && s[2] == 0))); }
  61. bool IsDotOrDotDot(const char * s) { return (s[0] == '.' && ((s[1] == 0) || (s[1] == '.' && s[2] == 0))); }
  62. String AppendPathElement(const String & s, const String & t)
  63. {
  64. return s + "\\" + t;
  65. }
  66. String GetLastPathElement(const String & s)
  67. {
  68. String::const_iterator LastSlash;
  69. int ch;
  70. const String::const_iterator begin = s.begin();
  71. const String::const_iterator end = s.end();
  72. for (LastSlash = end ; LastSlash != begin ; )
  73. {
  74. if ((ch = *--LastSlash) == '\\' || ch == '/')
  75. {
  76. return String(++LastSlash, end);
  77. }
  78. }
  79. return s;
  80. }
  81. String RemoveLastPathElement(const String & s)
  82. {
  83. String::const_iterator LastSlash;
  84. int ch;
  85. const String::const_iterator begin = s.begin();
  86. const String::const_iterator end = s.end();
  87. for (LastSlash = end ; LastSlash != begin ; )
  88. {
  89. if ((ch = *--LastSlash) == '\\' || ch == '/')
  90. {
  91. return String(begin, LastSlash);
  92. }
  93. }
  94. return String();
  95. }
  96. String GetBaseName(const String & s)
  97. {
  98. // basename is the part between the last slash and the last period
  99. // if there no period after the last slash, it's to end
  100. String::const_iterator LastSlash;
  101. String::const_iterator LastPeriod;
  102. int ch;
  103. const String::const_iterator begin = s.begin();
  104. const String::const_iterator end = s.end();
  105. for (LastSlash = end ; LastSlash != begin ; )
  106. {
  107. if ((ch = *--LastSlash) == '\\' || ch == '/')
  108. {
  109. ++LastSlash;
  110. break;
  111. }
  112. }
  113. for (LastPeriod = end ; LastPeriod != LastSlash ; )
  114. {
  115. if (*--LastPeriod == '.')
  116. {
  117. break;
  118. }
  119. }
  120. if (LastPeriod == LastSlash)
  121. LastPeriod = end;
  122. return String(LastSlash, LastPeriod);
  123. }
  124. String GetExtension(const String & s)
  125. {
  126. // extension is the part including and after the last period, unless the last
  127. // period is before a slash, in which case there is no extension
  128. String::const_iterator i;
  129. const String::const_iterator begin = s.begin();
  130. const String::const_iterator end = s.end();
  131. int ch;
  132. for (i = end ; i != begin ; )
  133. {
  134. if ((ch = *--i) == '\\' || ch == '/')
  135. {
  136. return String();
  137. }
  138. if (ch == '.')
  139. {
  140. return String(i, end);
  141. }
  142. }
  143. return String();
  144. }
  145. #define BUILD_STABILIZE_EXPORTS
  146. #include "clean.cpp"
  147. void RemoveDirectoryRecursive(const String & s)
  148. {
  149. char t[1U << 15];
  150. WIN32_FIND_DATAA FindData;
  151. strcpy(t, s.c_str());
  152. DeleteDirectory(t, strlen(t), &FindData);
  153. }
  154. /*
  155. UNDONE:
  156. put thunks at front of image
  157. identify code vs. data exports
  158. */
  159. bool StabilizeExports(const String & FileName)
  160. {
  161. bool Result = false;
  162. char ErrorBuffer[256];
  163. ErrorBuffer[0] = 0;
  164. VBlock * OriginalExportBlock = 0;
  165. VComp * Comp = 0;
  166. VExport * Export = 0;
  167. VBlock * NewBlock = 0;
  168. VProc * Proc = 0;
  169. VReloc * Reloc = 0;
  170. //char Name[sizeof(unsigned long) * CHAR_BIT];
  171. //unsigned long GenName = 0;
  172. unsigned long SizeOfPointer = 0;
  173. String FileNamePrestabilize;
  174. String PathLeaf;
  175. String FirstPdbName;
  176. String PdbName;
  177. String PdbNamePrestabilize;
  178. WIN32_FIND_DATAA FindData;
  179. String Directory;
  180. String PrestabilizeDirectory;
  181. unsigned long LastWin32Error = 0;
  182. ULONG PdbsFound = 0;
  183. CFindFile FindHandle;
  184. const BYTE x86Jmp = 0xE9;
  185. const BYTE x86Int3 = 0xCC;
  186. PlatformType Platform = platformtUnknown;
  187. BYTE protoTypeX86Thunk[8] = { x86Jmp, 0, 0, 0, 0, x86Int3, x86Int3, x86Int3 };
  188. try
  189. {
  190. Directory = RemoveLastPathElement(FileName);
  191. PrestabilizeDirectory = AppendPathElement(Directory, "prestabilize_exports");
  192. RemoveDirectoryRecursive(PrestabilizeDirectory);
  193. CreateDirectoryA(PrestabilizeDirectory, NULL);
  194. FileNamePrestabilize = AppendPathElement(PrestabilizeDirectory, GetLastPathElement(FileName));
  195. DeleteFileA(FileNamePrestabilize);
  196. if (!MoveFileA(FileName, FileNamePrestabilize))
  197. {
  198. LastWin32Error = GetLastError();
  199. FormatError(ErrorBuffer, NUMBER_OF(ErrorBuffer), "%s(%d):MoveFileA(%s, %s)\n", File, __LINE__, FileName.c_str(), FileNamePrestabilize.c_str(), LastWin32Error);
  200. goto Exit;
  201. }
  202. // move all .pdbs (vulcan won't give us the .pdb name..)
  203. if (FindHandle.Win32Create(AppendPathElement(Directory, "*.pdb"), &FindData))
  204. {
  205. do
  206. {
  207. if (IsDotOrDotDot(FindData.cFileName))
  208. continue;
  209. if ((FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
  210. {
  211. ++PdbsFound;
  212. PathLeaf = FindData.cFileName;
  213. PdbName = AppendPathElement(Directory, PathLeaf);
  214. if (FirstPdbName.empty())
  215. FirstPdbName = PdbName;
  216. PdbNamePrestabilize = AppendPathElement(PrestabilizeDirectory, PathLeaf);
  217. DeleteFileA(PdbNamePrestabilize);
  218. if (!MoveFileA(PdbName, PdbNamePrestabilize))
  219. {
  220. LastWin32Error = GetLastError();
  221. FormatError(ErrorBuffer, NUMBER_OF(ErrorBuffer), "%s(%d):MoveFileA(%s, %s):0x%lx\n", File, __LINE__, PdbName.c_str(), PdbNamePrestabilize.c_str(), LastWin32Error);
  222. goto Exit;
  223. }
  224. }
  225. } while (FindNextFileA(FindHandle, &FindData));
  226. FindHandle.Win32Close();
  227. }
  228. Comp = VComp::Open(FileNamePrestabilize, Open_FullLevel);
  229. if (Comp == NULL)
  230. {
  231. FormatError(ErrorBuffer, NUMBER_OF(ErrorBuffer), "%s(%d):VComp::Open(%s) failed\n", File, __LINE__, FileName);
  232. goto Exit;
  233. }
  234. {
  235. NewBlock = VBlock::CreateCodeBlock(Comp);
  236. switch (Platform = NewBlock->PlatformT())
  237. {
  238. default:
  239. SizeOfPointer = 4;
  240. break;
  241. case platformtX86:
  242. SizeOfPointer = 4;
  243. break;
  244. case platformtIA64:
  245. SizeOfPointer = 8;
  246. break;
  247. }
  248. NewBlock->Destroy();
  249. NewBlock = NULL;
  250. }
  251. for (Export = Comp->FirstExport() ; Export != NULL ; Export = Export->Next())
  252. {
  253. OriginalExportBlock = Export->Block();
  254. if (OriginalExportBlock == NULL)
  255. {
  256. // skip forwarders, they get bound to the target of the forwarder
  257. continue;
  258. }
  259. if (Platform == platformtX86)
  260. {
  261. NewBlock = VBlock::CreateDataBlock(Comp, protoTypeX86Thunk, sizeof(protoTypeX86Thunk));
  262. NewBlock->SetAlignmentSize(SizeOfPointer);
  263. Reloc = VReloc::Create(OriginalExportBlock, 0, 1, VReloc::Absolute);
  264. NewBlock->FirstReloc().AddFirst(Reloc);
  265. }
  266. else
  267. {
  268. NewBlock = VBlock::CreateCodeBlock(Comp);
  269. NewBlock->SetAlignmentSize(SizeOfPointer);
  270. NewBlock->InsertFirstInst(VInst::Create(COp::JMP, OriginalExportBlock));
  271. }
  272. //sprintf(Name, "%lu", ++GenName);
  273. //Proc = VProc::Create(Comp);
  274. //Proc->InsertFirstBlock(NewBlock);
  275. //Comp->FirstAllProc()->InsertPrev(Proc);
  276. //Comp->FirstImport()->Block()->InsertPrev(NewBlock);
  277. //Comp->FirstImport()->Block()->InsertPrev(NewBlock);
  278. Comp->FirstAllProc()->InsertFirstBlock(NewBlock);
  279. if (!Export->Redirect(NewBlock, Comp))
  280. {
  281. FormatError(ErrorBuffer, NUMBER_OF(ErrorBuffer), "Export->Redirect(%s) failed\n", Export->Name());
  282. goto Exit;
  283. }
  284. }
  285. Comp->Write(Write_Rereadable, FileName.c_str(), FirstPdbName.c_str(), static_cast<const char*>(NULL));
  286. Result = true;
  287. }
  288. catch (VErr & Err)
  289. {
  290. FormatError(ErrorBuffer, NUMBER_OF(ErrorBuffer), "caught VErr(%s)\n", Err.GetWhat());
  291. }
  292. Exit:
  293. if (Proc) Proc->Destroy();
  294. if (NewBlock) NewBlock->Destroy();
  295. if (OriginalExportBlock) OriginalExportBlock->Destroy();
  296. //if (Export) Export->Destroy();
  297. if (ErrorBuffer[0] != 0)
  298. {
  299. Error("%s", ErrorBuffer);
  300. }
  301. return Result;
  302. }
  303. #pragma warning(disable:4702) /* unreachable */
  304. int __cdecl main(int argc, char ** argv)
  305. {
  306. #if 0
  307. for ( ++argv ; *argv ; ++argv )
  308. {
  309. printf("base name(%s):%s\n", *argv, GetBaseName(*argv).c_str());
  310. printf("extension(%s):%s\n", *argv, GetExtension(*argv).c_str());
  311. printf("GetLastPathElement(%s):%s\n", *argv, GetLastPathElement(*argv).c_str());
  312. printf("RemoveLastPathElement(%s):%s\n", *argv, RemoveLastPathElement(*argv).c_str());
  313. }
  314. return 0;
  315. #endif
  316. StabilizeExports(argv[1]);
  317. return 0;
  318. }