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.

801 lines
24 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. ldrreloc.c
  5. Abstract:
  6. This module contains the code to relocate an image when
  7. the preferred base isn't available. This is called by the
  8. boot loader, device driver loader, and system loader.
  9. Author:
  10. Mike O'Leary (mikeol) 03-Feb-1992
  11. Revision History:
  12. --*/
  13. #include "ntrtlp.h"
  14. #if 0 // These are not actually used.
  15. // See also ntrtl.h, RtlUshortByteSwap, RtlUlongByteSwap, RtlUlonglongByteSwap.
  16. //
  17. // byte swapping macros (LE/BE) used for IA64 relocations
  18. // source != destination
  19. //
  20. //#define SWAP_SHORT(_dst,_src) \
  21. // ((((unsigned char *)_dst)[1] = ((unsigned char *)_src)[0]), \
  22. // (((unsigned char *)_dst)[0] = ((unsigned char *)_src)[1]))
  23. //#define SWAP_INT(_dst,_src) \
  24. // ((((unsigned char *)_dst)[3] = ((unsigned char *)_src)[0]), \
  25. // (((unsigned char *)_dst)[2] = ((unsigned char *)_src)[1]), \
  26. // (((unsigned char *)_dst)[1] = ((unsigned char *)_src)[2]), \
  27. // (((unsigned char *)_dst)[0] = ((unsigned char *)_src)[3]))
  28. //#define SWAP_LONG_LONG(_dst,_src) \
  29. // ((((unsigned char *)_dst)[7] = ((unsigned char *)_src)[0]), \
  30. // (((unsigned char *)_dst)[6] = ((unsigned char *)_src)[1]), \
  31. // (((unsigned char *)_dst)[5] = ((unsigned char *)_src)[2]), \
  32. // (((unsigned char *)_dst)[4] = ((unsigned char *)_src)[3]), \
  33. // (((unsigned char *)_dst)[3] = ((unsigned char *)_src)[4]), \
  34. // (((unsigned char *)_dst)[2] = ((unsigned char *)_src)[5]), \
  35. // (((unsigned char *)_dst)[1] = ((unsigned char *)_src)[6]), \
  36. // (((unsigned char *)_dst)[0] = ((unsigned char *)_src)[7]))
  37. #endif
  38. //
  39. // Mark a HIGHADJ entry as needing an increment if reprocessing.
  40. //
  41. #define LDRP_RELOCATION_INCREMENT 0x1
  42. //
  43. // Mark a HIGHADJ entry as not suitable for reprocessing.
  44. //
  45. #define LDRP_RELOCATION_FINAL 0x2
  46. PIMAGE_BASE_RELOCATION
  47. LdrProcessRelocationBlockLongLong(
  48. IN ULONG_PTR VA,
  49. IN ULONG SizeOfBlock,
  50. IN PUSHORT NextOffset,
  51. IN LONGLONG Diff
  52. );
  53. #if defined(NTOS_KERNEL_RUNTIME)
  54. #if defined(ALLOC_PRAGMA)
  55. #pragma alloc_text(PAGE,LdrRelocateImage)
  56. #pragma alloc_text(PAGE,LdrRelocateImageWithBias)
  57. #pragma alloc_text(PAGE,LdrProcessRelocationBlock)
  58. #pragma alloc_text(PAGE,LdrProcessRelocationBlockLongLong)
  59. #endif // ALLOC_PRAGMA
  60. #if defined(_ALPHA_)
  61. PIMAGE_BASE_RELOCATION
  62. LdrpProcessVolatileRelocationBlock(
  63. IN ULONG_PTR VA,
  64. IN ULONG SizeOfBlock,
  65. IN PUSHORT NextOffset,
  66. IN LONG_PTR Diff,
  67. IN LONG_PTR OldDiff,
  68. IN ULONG_PTR OldBase
  69. );
  70. #if defined(ALLOC_PRAGMA)
  71. #pragma alloc_text(INIT,LdrDoubleRelocateImage)
  72. #pragma alloc_text(INIT,LdrpProcessVolatileRelocationBlock)
  73. #endif // ALLOC_PRAGMA
  74. #endif // _ALPHA_
  75. #endif // NTOS_KERNEL_RUNTIME
  76. #if defined(BLDR_KERNEL_RUNTIME)
  77. typedef LDR_RELOCATE_IMAGE_RETURN_TYPE ARC_STATUS;
  78. #else
  79. typedef LDR_RELOCATE_IMAGE_RETURN_TYPE NTSTATUS;
  80. #endif
  81. LDR_RELOCATE_IMAGE_RETURN_TYPE
  82. LdrRelocateImage (
  83. IN PVOID NewBase,
  84. IN CONST CHAR* LoaderName,
  85. IN LDR_RELOCATE_IMAGE_RETURN_TYPE Success,
  86. IN LDR_RELOCATE_IMAGE_RETURN_TYPE Conflict,
  87. IN LDR_RELOCATE_IMAGE_RETURN_TYPE Invalid
  88. )
  89. /*++
  90. Routine Description:
  91. This routine relocates an image file that was not loaded into memory
  92. at the preferred address.
  93. Arguments:
  94. NewBase - Supplies a pointer to the image base.
  95. LoaderName - Indicates which loader routine is being called from.
  96. Success - Value to return if relocation successful.
  97. Conflict - Value to return if can't relocate.
  98. Invalid - Value to return if relocations are invalid.
  99. Return Value:
  100. Success if image is relocated.
  101. Conflict if image can't be relocated.
  102. Invalid if image contains invalid fixups.
  103. --*/
  104. {
  105. //
  106. // Just call LdrRelocateImageWithBias() with a zero bias.
  107. //
  108. return LdrRelocateImageWithBias( NewBase,
  109. 0,
  110. LoaderName,
  111. Success,
  112. Conflict,
  113. Invalid );
  114. }
  115. LDR_RELOCATE_IMAGE_RETURN_TYPE
  116. LdrRelocateImageWithBias (
  117. IN PVOID NewBase,
  118. IN LONGLONG AdditionalBias,
  119. IN CONST CHAR* LoaderName,
  120. IN LDR_RELOCATE_IMAGE_RETURN_TYPE Success,
  121. IN LDR_RELOCATE_IMAGE_RETURN_TYPE Conflict,
  122. IN LDR_RELOCATE_IMAGE_RETURN_TYPE Invalid
  123. )
  124. /*++
  125. Routine Description:
  126. This routine relocates an image file that was not loaded into memory
  127. at the preferred address.
  128. Arguments:
  129. NewBase - Supplies a pointer to the image base.
  130. AdditionalBias - An additional quantity to add to all fixups. The
  131. 32-bit X86 loader uses this when loading 64-bit images
  132. to specify a NewBase that is actually a 64-bit value.
  133. LoaderName - Indicates which loader routine is being called from.
  134. Success - Value to return if relocation successful.
  135. Conflict - Value to return if can't relocate.
  136. Invalid - Value to return if relocations are invalid.
  137. Return Value:
  138. Success if image is relocated.
  139. Conflict if image can't be relocated.
  140. Invalid if image contains invalid fixups.
  141. --*/
  142. {
  143. LONGLONG Diff;
  144. ULONG TotalCountBytes = 0;
  145. ULONG_PTR VA;
  146. ULONG_PTR OldBase;
  147. ULONG SizeOfBlock;
  148. PUCHAR FixupVA;
  149. USHORT Offset;
  150. PUSHORT NextOffset = NULL;
  151. PIMAGE_NT_HEADERS NtHeaders;
  152. PIMAGE_BASE_RELOCATION NextBlock;
  153. LDR_RELOCATE_IMAGE_RETURN_TYPE Status;
  154. RTL_PAGED_CODE();
  155. NtHeaders = RtlImageNtHeader( NewBase );
  156. if ( NtHeaders ) {
  157. OldBase = NtHeaders->OptionalHeader.ImageBase;
  158. }
  159. else {
  160. Status = Invalid;
  161. goto Exit;
  162. }
  163. //
  164. // Locate the relocation section.
  165. //
  166. NextBlock = (PIMAGE_BASE_RELOCATION)RtlImageDirectoryEntryToData(
  167. NewBase, TRUE, IMAGE_DIRECTORY_ENTRY_BASERELOC, &TotalCountBytes);
  168. //
  169. // It is possible for a file to have no relocations, but the relocations
  170. // must not have been stripped.
  171. //
  172. if (!NextBlock || !TotalCountBytes) {
  173. if (NtHeaders->FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
  174. #if DBG
  175. DbgPrint("%s: Image can't be relocated, no fixup information.\n", LoaderName);
  176. #endif // DBG
  177. Status = Conflict;
  178. } else {
  179. Status = Success;
  180. }
  181. goto Exit;
  182. }
  183. //
  184. // If the image has a relocation table, then apply the specified fixup
  185. // information to the image.
  186. //
  187. Diff = (PCHAR)NewBase - (PCHAR)OldBase + AdditionalBias;
  188. while (TotalCountBytes) {
  189. SizeOfBlock = NextBlock->SizeOfBlock;
  190. TotalCountBytes -= SizeOfBlock;
  191. SizeOfBlock -= sizeof(IMAGE_BASE_RELOCATION);
  192. SizeOfBlock /= sizeof(USHORT);
  193. NextOffset = (PUSHORT)((PCHAR)NextBlock + sizeof(IMAGE_BASE_RELOCATION));
  194. VA = (ULONG_PTR)NewBase + NextBlock->VirtualAddress;
  195. if ( !(NextBlock = LdrProcessRelocationBlockLongLong( VA,
  196. SizeOfBlock,
  197. NextOffset,
  198. Diff)) ) {
  199. #if DBG
  200. DbgPrint("%s: Unknown base relocation type\n", LoaderName);
  201. #endif
  202. Status = Invalid;
  203. goto Exit;
  204. }
  205. }
  206. Status = Success;
  207. Exit:
  208. if (Status != Success) {
  209. DbgPrint(
  210. "%s: %s() failed 0x%lx\n"
  211. "%s: OldBase : %p\n"
  212. "%s: NewBase : %p\n"
  213. "%s: Diff : 0x%I64x\n"
  214. "%s: NextOffset : %p\n"
  215. "%s: *NextOffset : 0x%x\n"
  216. "%s: SizeOfBlock : 0x%lx\n",
  217. LoaderName, __FUNCTION__, Status,
  218. LoaderName, OldBase,
  219. LoaderName, NewBase,
  220. LoaderName, Diff,
  221. LoaderName, NextOffset,
  222. LoaderName, (NextOffset != NULL) ? *NextOffset : 0,
  223. LoaderName, SizeOfBlock
  224. );
  225. #if DBG
  226. DbgBreakPoint();
  227. #endif
  228. }
  229. return Status;
  230. }
  231. PIMAGE_BASE_RELOCATION
  232. LdrProcessRelocationBlock(
  233. IN ULONG_PTR VA,
  234. IN ULONG SizeOfBlock,
  235. IN PUSHORT NextOffset,
  236. IN LONG_PTR Diff
  237. )
  238. {
  239. PIMAGE_BASE_RELOCATION baseRelocation;
  240. baseRelocation = LdrProcessRelocationBlockLongLong( VA,
  241. SizeOfBlock,
  242. NextOffset,
  243. (LONGLONG)Diff );
  244. return baseRelocation;
  245. }
  246. PIMAGE_BASE_RELOCATION
  247. LdrProcessRelocationBlockLongLong(
  248. IN ULONG_PTR VA,
  249. IN ULONG SizeOfBlock,
  250. IN PUSHORT NextOffset,
  251. IN LONGLONG Diff
  252. )
  253. {
  254. PUCHAR FixupVA;
  255. USHORT Offset;
  256. LONG Temp;
  257. LONG TempOrig;
  258. ULONG Temp32;
  259. ULONGLONG Value64;
  260. LONGLONG Temp64;
  261. LONG_PTR ActualDiff;
  262. RTL_PAGED_CODE();
  263. while (SizeOfBlock--) {
  264. Offset = *NextOffset & (USHORT)0xfff;
  265. FixupVA = (PUCHAR)(VA + Offset);
  266. //
  267. // Apply the fixups.
  268. //
  269. switch ((*NextOffset) >> 12) {
  270. case IMAGE_REL_BASED_HIGHLOW :
  271. //
  272. // HighLow - (32-bits) relocate the high and low half
  273. // of an address.
  274. //
  275. *(LONG UNALIGNED *)FixupVA += (ULONG) Diff;
  276. break;
  277. case IMAGE_REL_BASED_HIGH :
  278. //
  279. // High - (16-bits) relocate the high half of an address.
  280. //
  281. Temp = *(PUSHORT)FixupVA << 16;
  282. Temp += (ULONG) Diff;
  283. *(PUSHORT)FixupVA = (USHORT)(Temp >> 16);
  284. break;
  285. case IMAGE_REL_BASED_HIGHADJ :
  286. //
  287. // Adjust high - (16-bits) relocate the high half of an
  288. // address and adjust for sign extension of low half.
  289. //
  290. #if defined(NTOS_KERNEL_RUNTIME)
  291. //
  292. // If the address has already been relocated then don't
  293. // process it again now or information will be lost.
  294. //
  295. if (Offset & LDRP_RELOCATION_FINAL) {
  296. ++NextOffset;
  297. --SizeOfBlock;
  298. break;
  299. }
  300. #endif
  301. Temp = *(PUSHORT)FixupVA << 16;
  302. #if defined(BLDR_KERNEL_RUNTIME)
  303. TempOrig = Temp;
  304. #endif
  305. ++NextOffset;
  306. --SizeOfBlock;
  307. Temp += (LONG)(*(PSHORT)NextOffset);
  308. Temp += (ULONG) Diff;
  309. Temp += 0x8000;
  310. *(PUSHORT)FixupVA = (USHORT)(Temp >> 16);
  311. #if defined(BLDR_KERNEL_RUNTIME)
  312. ActualDiff = ((((ULONG_PTR)(Temp - TempOrig)) >> 16) -
  313. (((ULONG_PTR)Diff) >> 16 ));
  314. if (ActualDiff == 1) {
  315. //
  316. // Mark the relocation as needing an increment if it is
  317. // relocated again.
  318. //
  319. *(NextOffset - 1) |= LDRP_RELOCATION_INCREMENT;
  320. }
  321. else if (ActualDiff != 0) {
  322. //
  323. // Mark the relocation as cannot be reprocessed.
  324. //
  325. *(NextOffset - 1) |= LDRP_RELOCATION_FINAL;
  326. }
  327. #endif
  328. break;
  329. case IMAGE_REL_BASED_LOW :
  330. //
  331. // Low - (16-bit) relocate the low half of an address.
  332. //
  333. Temp = *(PSHORT)FixupVA;
  334. Temp += (ULONG) Diff;
  335. *(PUSHORT)FixupVA = (USHORT)Temp;
  336. break;
  337. case IMAGE_REL_BASED_IA64_IMM64:
  338. //
  339. // Align it to bundle address before fixing up the
  340. // 64-bit immediate value of the movl instruction.
  341. //
  342. FixupVA = (PUCHAR)((ULONG_PTR)FixupVA & ~(15));
  343. Value64 = (ULONGLONG)0;
  344. //
  345. // Extract the lower 32 bits of IMM64 from bundle
  346. //
  347. EXT_IMM64(Value64,
  348. (PULONG)FixupVA + EMARCH_ENC_I17_IMM7B_INST_WORD_X,
  349. EMARCH_ENC_I17_IMM7B_SIZE_X,
  350. EMARCH_ENC_I17_IMM7B_INST_WORD_POS_X,
  351. EMARCH_ENC_I17_IMM7B_VAL_POS_X);
  352. EXT_IMM64(Value64,
  353. (PULONG)FixupVA + EMARCH_ENC_I17_IMM9D_INST_WORD_X,
  354. EMARCH_ENC_I17_IMM9D_SIZE_X,
  355. EMARCH_ENC_I17_IMM9D_INST_WORD_POS_X,
  356. EMARCH_ENC_I17_IMM9D_VAL_POS_X);
  357. EXT_IMM64(Value64,
  358. (PULONG)FixupVA + EMARCH_ENC_I17_IMM5C_INST_WORD_X,
  359. EMARCH_ENC_I17_IMM5C_SIZE_X,
  360. EMARCH_ENC_I17_IMM5C_INST_WORD_POS_X,
  361. EMARCH_ENC_I17_IMM5C_VAL_POS_X);
  362. EXT_IMM64(Value64,
  363. (PULONG)FixupVA + EMARCH_ENC_I17_IC_INST_WORD_X,
  364. EMARCH_ENC_I17_IC_SIZE_X,
  365. EMARCH_ENC_I17_IC_INST_WORD_POS_X,
  366. EMARCH_ENC_I17_IC_VAL_POS_X);
  367. EXT_IMM64(Value64,
  368. (PULONG)FixupVA + EMARCH_ENC_I17_IMM41a_INST_WORD_X,
  369. EMARCH_ENC_I17_IMM41a_SIZE_X,
  370. EMARCH_ENC_I17_IMM41a_INST_WORD_POS_X,
  371. EMARCH_ENC_I17_IMM41a_VAL_POS_X);
  372. EXT_IMM64(Value64,
  373. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM41b_INST_WORD_X),
  374. EMARCH_ENC_I17_IMM41b_SIZE_X,
  375. EMARCH_ENC_I17_IMM41b_INST_WORD_POS_X,
  376. EMARCH_ENC_I17_IMM41b_VAL_POS_X);
  377. EXT_IMM64(Value64,
  378. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM41c_INST_WORD_X),
  379. EMARCH_ENC_I17_IMM41c_SIZE_X,
  380. EMARCH_ENC_I17_IMM41c_INST_WORD_POS_X,
  381. EMARCH_ENC_I17_IMM41c_VAL_POS_X);
  382. EXT_IMM64(Value64,
  383. ((PULONG)FixupVA + EMARCH_ENC_I17_SIGN_INST_WORD_X),
  384. EMARCH_ENC_I17_SIGN_SIZE_X,
  385. EMARCH_ENC_I17_SIGN_INST_WORD_POS_X,
  386. EMARCH_ENC_I17_SIGN_VAL_POS_X);
  387. //
  388. // Update 64-bit address
  389. //
  390. Value64+=Diff;
  391. //
  392. // Insert IMM64 into bundle
  393. //
  394. INS_IMM64(Value64,
  395. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM7B_INST_WORD_X),
  396. EMARCH_ENC_I17_IMM7B_SIZE_X,
  397. EMARCH_ENC_I17_IMM7B_INST_WORD_POS_X,
  398. EMARCH_ENC_I17_IMM7B_VAL_POS_X);
  399. INS_IMM64(Value64,
  400. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM9D_INST_WORD_X),
  401. EMARCH_ENC_I17_IMM9D_SIZE_X,
  402. EMARCH_ENC_I17_IMM9D_INST_WORD_POS_X,
  403. EMARCH_ENC_I17_IMM9D_VAL_POS_X);
  404. INS_IMM64(Value64,
  405. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM5C_INST_WORD_X),
  406. EMARCH_ENC_I17_IMM5C_SIZE_X,
  407. EMARCH_ENC_I17_IMM5C_INST_WORD_POS_X,
  408. EMARCH_ENC_I17_IMM5C_VAL_POS_X);
  409. INS_IMM64(Value64,
  410. ((PULONG)FixupVA + EMARCH_ENC_I17_IC_INST_WORD_X),
  411. EMARCH_ENC_I17_IC_SIZE_X,
  412. EMARCH_ENC_I17_IC_INST_WORD_POS_X,
  413. EMARCH_ENC_I17_IC_VAL_POS_X);
  414. INS_IMM64(Value64,
  415. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM41a_INST_WORD_X),
  416. EMARCH_ENC_I17_IMM41a_SIZE_X,
  417. EMARCH_ENC_I17_IMM41a_INST_WORD_POS_X,
  418. EMARCH_ENC_I17_IMM41a_VAL_POS_X);
  419. INS_IMM64(Value64,
  420. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM41b_INST_WORD_X),
  421. EMARCH_ENC_I17_IMM41b_SIZE_X,
  422. EMARCH_ENC_I17_IMM41b_INST_WORD_POS_X,
  423. EMARCH_ENC_I17_IMM41b_VAL_POS_X);
  424. INS_IMM64(Value64,
  425. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM41c_INST_WORD_X),
  426. EMARCH_ENC_I17_IMM41c_SIZE_X,
  427. EMARCH_ENC_I17_IMM41c_INST_WORD_POS_X,
  428. EMARCH_ENC_I17_IMM41c_VAL_POS_X);
  429. INS_IMM64(Value64,
  430. ((PULONG)FixupVA + EMARCH_ENC_I17_SIGN_INST_WORD_X),
  431. EMARCH_ENC_I17_SIGN_SIZE_X,
  432. EMARCH_ENC_I17_SIGN_INST_WORD_POS_X,
  433. EMARCH_ENC_I17_SIGN_VAL_POS_X);
  434. break;
  435. case IMAGE_REL_BASED_DIR64:
  436. *(ULONGLONG UNALIGNED *)FixupVA += Diff;
  437. break;
  438. case IMAGE_REL_BASED_MIPS_JMPADDR :
  439. //
  440. // JumpAddress - (32-bits) relocate a MIPS jump address.
  441. //
  442. Temp = (*(PULONG)FixupVA & 0x3ffffff) << 2;
  443. Temp += (ULONG) Diff;
  444. *(PULONG)FixupVA = (*(PULONG)FixupVA & ~0x3ffffff) |
  445. ((Temp >> 2) & 0x3ffffff);
  446. break;
  447. case IMAGE_REL_BASED_ABSOLUTE :
  448. //
  449. // Absolute - no fixup required.
  450. //
  451. break;
  452. case IMAGE_REL_BASED_SECTION :
  453. //
  454. // Section Relative reloc. Ignore for now.
  455. //
  456. break;
  457. case IMAGE_REL_BASED_REL32 :
  458. //
  459. // Relative intrasection. Ignore for now.
  460. //
  461. break;
  462. default :
  463. //
  464. // Illegal - illegal relocation type.
  465. //
  466. return (PIMAGE_BASE_RELOCATION)NULL;
  467. }
  468. ++NextOffset;
  469. }
  470. return (PIMAGE_BASE_RELOCATION)NextOffset;
  471. }
  472. #if defined(NTOS_KERNEL_RUNTIME) && defined(_ALPHA_)
  473. NTSTATUS
  474. LdrDoubleRelocateImage (
  475. IN PVOID NewBase,
  476. IN PVOID CurrentBase,
  477. IN CONST CHAR* LoaderName,
  478. IN NTSTATUS Success,
  479. IN NTSTATUS Conflict,
  480. IN NTSTATUS Invalid
  481. )
  482. /*++
  483. Routine Description:
  484. This routine handles the volatile relocations that cannot be easily repeated
  485. on an image file that has already been relocated at least once.
  486. Since this only needs to be done once (at kernel startup time), the
  487. decision was made to split this into a separate routine so as not to
  488. impact the mainline code.
  489. N.B. This function is for use by memory management ONLY.
  490. Arguments:
  491. NewBase - Supplies a pointer to the new (second relocated) image base.
  492. CurrentBase - Supplies a pointer to the first relocated image base.
  493. LoaderName - Indicates which loader routine is being called from.
  494. Success - Value to return if relocation successful.
  495. Conflict - Value to return if can't relocate.
  496. Invalid - Value to return if relocations are invalid.
  497. Return Value:
  498. Success if image is relocated.
  499. Conflict if image can't be relocated.
  500. Invalid if image contains invalid fixups.
  501. --*/
  502. {
  503. LONG_PTR Diff;
  504. LONG_PTR OldDiff;
  505. ULONG TotalCountBytes;
  506. ULONG_PTR VA;
  507. ULONG_PTR OldBase;
  508. ULONG SizeOfBlock;
  509. PUCHAR FixupVA;
  510. USHORT Offset;
  511. PUSHORT NextOffset;
  512. PIMAGE_NT_HEADERS NtHeaders;
  513. PIMAGE_BASE_RELOCATION NextBlock;
  514. RTL_PAGED_CODE();
  515. NtHeaders = RtlImageNtHeader( NewBase );
  516. OldBase = NtHeaders->OptionalHeader.ImageBase;
  517. OldDiff = (PCHAR)CurrentBase - (PCHAR)OldBase;
  518. //
  519. // Locate the relocation section.
  520. //
  521. NextBlock = (PIMAGE_BASE_RELOCATION)RtlImageDirectoryEntryToData(
  522. NewBase, TRUE, IMAGE_DIRECTORY_ENTRY_BASERELOC, &TotalCountBytes);
  523. if (!NextBlock || !TotalCountBytes) {
  524. //
  525. // The image does not contain a relocation table, and therefore
  526. // cannot be relocated.
  527. //
  528. #if DBG
  529. DbgPrint("%s: Image can't be relocated, no fixup information.\n", LoaderName);
  530. #endif // DBG
  531. return Conflict;
  532. }
  533. //
  534. // If the image has a relocation table, then apply the specified fixup
  535. // information to the image.
  536. //
  537. Diff = (PCHAR)NewBase - (PCHAR)OldBase;
  538. while (TotalCountBytes) {
  539. SizeOfBlock = NextBlock->SizeOfBlock;
  540. TotalCountBytes -= SizeOfBlock;
  541. SizeOfBlock -= sizeof(IMAGE_BASE_RELOCATION);
  542. SizeOfBlock /= sizeof(USHORT);
  543. NextOffset = (PUSHORT)((PCHAR)NextBlock + sizeof(IMAGE_BASE_RELOCATION));
  544. VA = (ULONG_PTR)NewBase + NextBlock->VirtualAddress;
  545. if ( !(NextBlock = LdrpProcessVolatileRelocationBlock(VA,SizeOfBlock,NextOffset,Diff, OldDiff, OldBase)) ) {
  546. #if DBG
  547. DbgPrint("%s: Unknown base relocation type\n", LoaderName);
  548. #endif
  549. return Invalid;
  550. }
  551. }
  552. return Success;
  553. }
  554. PIMAGE_BASE_RELOCATION
  555. LdrpProcessVolatileRelocationBlock(
  556. IN ULONG_PTR VA,
  557. IN ULONG SizeOfBlock,
  558. IN PUSHORT NextOffset,
  559. IN LONG_PTR Diff,
  560. IN LONG_PTR OldDiff,
  561. IN ULONG_PTR OldBase
  562. )
  563. /*++
  564. Routine Description:
  565. This routine handles the volatile relocations that cannot be easily repeated
  566. on an image file that has already been relocated at least once.
  567. Since this only needs to be done once (at kernel startup time), the
  568. decision was made to split this into a separate routine so as not to
  569. impact the mainline code.
  570. N.B. This function is for use by memory management ONLY.
  571. Arguments:
  572. TBD.
  573. Return Value:
  574. Next relocation entry to process.
  575. --*/
  576. {
  577. PUCHAR FixupVA;
  578. USHORT Offset;
  579. LONG Temp;
  580. ULONG Temp32;
  581. USHORT TempShort1;
  582. USHORT TempShort2;
  583. ULONGLONG Value64;
  584. LONGLONG Temp64;
  585. USHORT RelocationType;
  586. IN PVOID CurrentBase;
  587. RTL_PAGED_CODE();
  588. CurrentBase = (PVOID)((ULONG_PTR)OldDiff + OldBase);
  589. while (SizeOfBlock--) {
  590. Offset = *NextOffset & (USHORT)0xfff;
  591. FixupVA = (PUCHAR)(VA + Offset);
  592. //
  593. // Apply the fixups.
  594. //
  595. switch ((*NextOffset) >> 12) {
  596. case IMAGE_REL_BASED_HIGHADJ :
  597. //
  598. // Adjust high - (16-bits) relocate the high half of an
  599. // address and adjust for sign extension of low half.
  600. //
  601. //
  602. // Return the relocation to its original state, checking for
  603. // whether the entry was sign extended the 1st time it was
  604. // relocated.
  605. //
  606. FixupVA = (PUCHAR)((LONG_PTR)FixupVA & (LONG_PTR)~(LDRP_RELOCATION_FINAL | LDRP_RELOCATION_INCREMENT));
  607. Temp = *(PUSHORT)(FixupVA) << 16;
  608. ++NextOffset;
  609. --SizeOfBlock;
  610. // remove the carry bit from the low word
  611. Temp -= ((LONG)(*(PSHORT)NextOffset) + (USHORT)OldDiff + 0x8000) & ~0xFFFF;
  612. Temp -= (LONG)(OldDiff & ~0xffff);
  613. Temp += (LONG)(*(PSHORT)NextOffset);
  614. Temp += (ULONG) Diff;
  615. Temp += 0x8000;
  616. *(PUSHORT)FixupVA = (USHORT)(Temp >> 16);
  617. //
  618. // Mark the relocation as needing no further reprocessing.
  619. //
  620. *(NextOffset - 1) |= LDRP_RELOCATION_FINAL;
  621. break;
  622. default :
  623. break;
  624. }
  625. ++NextOffset;
  626. }
  627. return (PIMAGE_BASE_RELOCATION)NextOffset;
  628. }
  629. #endif // NTOS_KERNEL_RUNTIME && _ALPHA_