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.

290 lines
10 KiB

  1. /**
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. ldrreloc_rebase.c
  5. Abstract:
  6. Extract the LdrProcessRelocationBlock code from ldrreloc.c so rebase can use it
  7. w/o duplication of effort. This file is generated - don't edit by hand.
  8. Revision History:
  9. --*/
  10. #ifndef IMAGE_REL_BASED_SECTION
  11. #define IMAGE_REL_BASED_SECTION 6
  12. #endif
  13. #ifndef IMAGE_REL_BASED_REL32
  14. #define IMAGE_REL_BASED_REL32 7
  15. #endif
  16. #define RTL_PAGED_CODE() NOP_FUNCTION()
  17. #define LdrProcessRelocationBlockLongLong xxLdrProcessRelocationBlock64
  18. PIMAGE_BASE_RELOCATION
  19. LdrProcessRelocationBlockLongLong(
  20. IN ULONG_PTR VA,
  21. IN ULONG SizeOfBlock,
  22. IN PUSHORT NextOffset,
  23. IN LONGLONG Diff
  24. )
  25. {
  26. PUCHAR FixupVA;
  27. USHORT Offset;
  28. LONG Temp;
  29. #if defined(BLDR_KERNEL_RUNTIME)
  30. LONG TempOrig;
  31. LONG_PTR ActualDiff;
  32. #endif
  33. ULONG Temp32;
  34. ULONGLONG Value64;
  35. LONGLONG Temp64;
  36. RTL_PAGED_CODE();
  37. while (SizeOfBlock--) {
  38. Offset = *NextOffset & (USHORT)0xfff;
  39. FixupVA = (PUCHAR)(VA + Offset);
  40. //
  41. // Apply the fixups.
  42. //
  43. switch ((*NextOffset) >> 12) {
  44. case IMAGE_REL_BASED_HIGHLOW :
  45. //
  46. // HighLow - (32-bits) relocate the high and low half
  47. // of an address.
  48. //
  49. *(LONG UNALIGNED *)FixupVA += (ULONG) Diff;
  50. break;
  51. case IMAGE_REL_BASED_HIGH :
  52. //
  53. // High - (16-bits) relocate the high half of an address.
  54. //
  55. Temp = *(PUSHORT)FixupVA << 16;
  56. Temp += (ULONG) Diff;
  57. *(PUSHORT)FixupVA = (USHORT)(Temp >> 16);
  58. break;
  59. case IMAGE_REL_BASED_HIGHADJ :
  60. //
  61. // Adjust high - (16-bits) relocate the high half of an
  62. // address and adjust for sign extension of low half.
  63. //
  64. #if defined(NTOS_KERNEL_RUNTIME)
  65. //
  66. // If the address has already been relocated then don't
  67. // process it again now or information will be lost.
  68. //
  69. if (Offset & LDRP_RELOCATION_FINAL) {
  70. ++NextOffset;
  71. --SizeOfBlock;
  72. break;
  73. }
  74. #endif
  75. Temp = *(PUSHORT)FixupVA << 16;
  76. #if defined(BLDR_KERNEL_RUNTIME)
  77. TempOrig = Temp;
  78. #endif
  79. ++NextOffset;
  80. --SizeOfBlock;
  81. Temp += (LONG)(*(PSHORT)NextOffset);
  82. Temp += (ULONG) Diff;
  83. Temp += 0x8000;
  84. *(PUSHORT)FixupVA = (USHORT)(Temp >> 16);
  85. #if defined(BLDR_KERNEL_RUNTIME)
  86. ActualDiff = ((((ULONG_PTR)(Temp - TempOrig)) >> 16) -
  87. (((ULONG_PTR)Diff) >> 16 ));
  88. if (ActualDiff == 1) {
  89. //
  90. // Mark the relocation as needing an increment if it is
  91. // relocated again.
  92. //
  93. *(NextOffset - 1) |= LDRP_RELOCATION_INCREMENT;
  94. }
  95. else if (ActualDiff != 0) {
  96. //
  97. // Mark the relocation as cannot be reprocessed.
  98. //
  99. *(NextOffset - 1) |= LDRP_RELOCATION_FINAL;
  100. }
  101. #endif
  102. break;
  103. case IMAGE_REL_BASED_LOW :
  104. //
  105. // Low - (16-bit) relocate the low half of an address.
  106. //
  107. Temp = *(PSHORT)FixupVA;
  108. Temp += (ULONG) Diff;
  109. *(PUSHORT)FixupVA = (USHORT)Temp;
  110. break;
  111. case IMAGE_REL_BASED_IA64_IMM64:
  112. //
  113. // Align it to bundle address before fixing up the
  114. // 64-bit immediate value of the movl instruction.
  115. //
  116. FixupVA = (PUCHAR)((ULONG_PTR)FixupVA & ~(15));
  117. Value64 = (ULONGLONG)0;
  118. //
  119. // Extract the lower 32 bits of IMM64 from bundle
  120. //
  121. EXT_IMM64(Value64,
  122. (PULONG)FixupVA + EMARCH_ENC_I17_IMM7B_INST_WORD_X,
  123. EMARCH_ENC_I17_IMM7B_SIZE_X,
  124. EMARCH_ENC_I17_IMM7B_INST_WORD_POS_X,
  125. EMARCH_ENC_I17_IMM7B_VAL_POS_X);
  126. EXT_IMM64(Value64,
  127. (PULONG)FixupVA + EMARCH_ENC_I17_IMM9D_INST_WORD_X,
  128. EMARCH_ENC_I17_IMM9D_SIZE_X,
  129. EMARCH_ENC_I17_IMM9D_INST_WORD_POS_X,
  130. EMARCH_ENC_I17_IMM9D_VAL_POS_X);
  131. EXT_IMM64(Value64,
  132. (PULONG)FixupVA + EMARCH_ENC_I17_IMM5C_INST_WORD_X,
  133. EMARCH_ENC_I17_IMM5C_SIZE_X,
  134. EMARCH_ENC_I17_IMM5C_INST_WORD_POS_X,
  135. EMARCH_ENC_I17_IMM5C_VAL_POS_X);
  136. EXT_IMM64(Value64,
  137. (PULONG)FixupVA + EMARCH_ENC_I17_IC_INST_WORD_X,
  138. EMARCH_ENC_I17_IC_SIZE_X,
  139. EMARCH_ENC_I17_IC_INST_WORD_POS_X,
  140. EMARCH_ENC_I17_IC_VAL_POS_X);
  141. EXT_IMM64(Value64,
  142. (PULONG)FixupVA + EMARCH_ENC_I17_IMM41a_INST_WORD_X,
  143. EMARCH_ENC_I17_IMM41a_SIZE_X,
  144. EMARCH_ENC_I17_IMM41a_INST_WORD_POS_X,
  145. EMARCH_ENC_I17_IMM41a_VAL_POS_X);
  146. EXT_IMM64(Value64,
  147. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM41b_INST_WORD_X),
  148. EMARCH_ENC_I17_IMM41b_SIZE_X,
  149. EMARCH_ENC_I17_IMM41b_INST_WORD_POS_X,
  150. EMARCH_ENC_I17_IMM41b_VAL_POS_X);
  151. EXT_IMM64(Value64,
  152. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM41c_INST_WORD_X),
  153. EMARCH_ENC_I17_IMM41c_SIZE_X,
  154. EMARCH_ENC_I17_IMM41c_INST_WORD_POS_X,
  155. EMARCH_ENC_I17_IMM41c_VAL_POS_X);
  156. EXT_IMM64(Value64,
  157. ((PULONG)FixupVA + EMARCH_ENC_I17_SIGN_INST_WORD_X),
  158. EMARCH_ENC_I17_SIGN_SIZE_X,
  159. EMARCH_ENC_I17_SIGN_INST_WORD_POS_X,
  160. EMARCH_ENC_I17_SIGN_VAL_POS_X);
  161. //
  162. // Update 64-bit address
  163. //
  164. Value64+=Diff;
  165. //
  166. // Insert IMM64 into bundle
  167. //
  168. INS_IMM64(Value64,
  169. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM7B_INST_WORD_X),
  170. EMARCH_ENC_I17_IMM7B_SIZE_X,
  171. EMARCH_ENC_I17_IMM7B_INST_WORD_POS_X,
  172. EMARCH_ENC_I17_IMM7B_VAL_POS_X);
  173. INS_IMM64(Value64,
  174. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM9D_INST_WORD_X),
  175. EMARCH_ENC_I17_IMM9D_SIZE_X,
  176. EMARCH_ENC_I17_IMM9D_INST_WORD_POS_X,
  177. EMARCH_ENC_I17_IMM9D_VAL_POS_X);
  178. INS_IMM64(Value64,
  179. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM5C_INST_WORD_X),
  180. EMARCH_ENC_I17_IMM5C_SIZE_X,
  181. EMARCH_ENC_I17_IMM5C_INST_WORD_POS_X,
  182. EMARCH_ENC_I17_IMM5C_VAL_POS_X);
  183. INS_IMM64(Value64,
  184. ((PULONG)FixupVA + EMARCH_ENC_I17_IC_INST_WORD_X),
  185. EMARCH_ENC_I17_IC_SIZE_X,
  186. EMARCH_ENC_I17_IC_INST_WORD_POS_X,
  187. EMARCH_ENC_I17_IC_VAL_POS_X);
  188. INS_IMM64(Value64,
  189. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM41a_INST_WORD_X),
  190. EMARCH_ENC_I17_IMM41a_SIZE_X,
  191. EMARCH_ENC_I17_IMM41a_INST_WORD_POS_X,
  192. EMARCH_ENC_I17_IMM41a_VAL_POS_X);
  193. INS_IMM64(Value64,
  194. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM41b_INST_WORD_X),
  195. EMARCH_ENC_I17_IMM41b_SIZE_X,
  196. EMARCH_ENC_I17_IMM41b_INST_WORD_POS_X,
  197. EMARCH_ENC_I17_IMM41b_VAL_POS_X);
  198. INS_IMM64(Value64,
  199. ((PULONG)FixupVA + EMARCH_ENC_I17_IMM41c_INST_WORD_X),
  200. EMARCH_ENC_I17_IMM41c_SIZE_X,
  201. EMARCH_ENC_I17_IMM41c_INST_WORD_POS_X,
  202. EMARCH_ENC_I17_IMM41c_VAL_POS_X);
  203. INS_IMM64(Value64,
  204. ((PULONG)FixupVA + EMARCH_ENC_I17_SIGN_INST_WORD_X),
  205. EMARCH_ENC_I17_SIGN_SIZE_X,
  206. EMARCH_ENC_I17_SIGN_INST_WORD_POS_X,
  207. EMARCH_ENC_I17_SIGN_VAL_POS_X);
  208. break;
  209. case IMAGE_REL_BASED_DIR64:
  210. *(ULONGLONG UNALIGNED *)FixupVA += Diff;
  211. break;
  212. case IMAGE_REL_BASED_MIPS_JMPADDR :
  213. //
  214. // JumpAddress - (32-bits) relocate a MIPS jump address.
  215. //
  216. Temp = (*(PULONG)FixupVA & 0x3ffffff) << 2;
  217. Temp += (ULONG) Diff;
  218. *(PULONG)FixupVA = (*(PULONG)FixupVA & ~0x3ffffff) |
  219. ((Temp >> 2) & 0x3ffffff);
  220. break;
  221. case IMAGE_REL_BASED_ABSOLUTE :
  222. //
  223. // Absolute - no fixup required.
  224. //
  225. break;
  226. case IMAGE_REL_BASED_SECTION :
  227. //
  228. // Section Relative reloc. Ignore for now.
  229. //
  230. break;
  231. case IMAGE_REL_BASED_REL32 :
  232. //
  233. // Relative intrasection. Ignore for now.
  234. //
  235. break;
  236. default :
  237. //
  238. // Illegal - illegal relocation type.
  239. //
  240. return (PIMAGE_BASE_RELOCATION)NULL;
  241. }
  242. ++NextOffset;
  243. }
  244. return (PIMAGE_BASE_RELOCATION)NextOffset;
  245. }