]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePeCoffLib/Ipf/PeCoffLoaderEx.c
Update IPF version BasePeCoffLib only supports IFP and EBC PeImage, doesn't support...
[mirror_edk2.git] / MdePkg / Library / BasePeCoffLib / Ipf / PeCoffLoaderEx.c
1 /** @file
2 Fixes Intel Itanium(TM) specific relocation types.
3
4 Copyright (c) 2006 - 2008, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "BasePeCoffLibInternals.h"
16
17
18
19 #define EXT_IMM64(Value, Address, Size, InstPos, ValPos) \
20 Value |= (((UINT64)((*(Address) >> InstPos) & (((UINT64)1 << Size) - 1))) << ValPos)
21
22 #define INS_IMM64(Value, Address, Size, InstPos, ValPos) \
23 *(UINT32*)Address = (*(UINT32*)Address & ~(((1 << Size) - 1) << InstPos)) | \
24 ((UINT32)((((UINT64)Value >> ValPos) & (((UINT64)1 << Size) - 1))) << InstPos)
25
26 #define IMM64_IMM7B_INST_WORD_X 3
27 #define IMM64_IMM7B_SIZE_X 7
28 #define IMM64_IMM7B_INST_WORD_POS_X 4
29 #define IMM64_IMM7B_VAL_POS_X 0
30
31 #define IMM64_IMM9D_INST_WORD_X 3
32 #define IMM64_IMM9D_SIZE_X 9
33 #define IMM64_IMM9D_INST_WORD_POS_X 18
34 #define IMM64_IMM9D_VAL_POS_X 7
35
36 #define IMM64_IMM5C_INST_WORD_X 3
37 #define IMM64_IMM5C_SIZE_X 5
38 #define IMM64_IMM5C_INST_WORD_POS_X 13
39 #define IMM64_IMM5C_VAL_POS_X 16
40
41 #define IMM64_IC_INST_WORD_X 3
42 #define IMM64_IC_SIZE_X 1
43 #define IMM64_IC_INST_WORD_POS_X 12
44 #define IMM64_IC_VAL_POS_X 21
45
46 #define IMM64_IMM41A_INST_WORD_X 1
47 #define IMM64_IMM41A_SIZE_X 10
48 #define IMM64_IMM41A_INST_WORD_POS_X 14
49 #define IMM64_IMM41A_VAL_POS_X 22
50
51 #define IMM64_IMM41B_INST_WORD_X 1
52 #define IMM64_IMM41B_SIZE_X 8
53 #define IMM64_IMM41B_INST_WORD_POS_X 24
54 #define IMM64_IMM41B_VAL_POS_X 32
55
56 #define IMM64_IMM41C_INST_WORD_X 2
57 #define IMM64_IMM41C_SIZE_X 23
58 #define IMM64_IMM41C_INST_WORD_POS_X 0
59 #define IMM64_IMM41C_VAL_POS_X 40
60
61 #define IMM64_SIGN_INST_WORD_X 3
62 #define IMM64_SIGN_SIZE_X 1
63 #define IMM64_SIGN_INST_WORD_POS_X 27
64 #define IMM64_SIGN_VAL_POS_X 63
65
66 /**
67 Performs an Itanium-based specific relocation fixup.
68
69 @param Reloc Pointer to the relocation record.
70 @param Fixup Pointer to the address to fix up.
71 @param FixupData Pointer to a buffer to log the fixups.
72 @param Adjust The offset to adjust the fixup.
73
74 @retval RETURN_SUCCESS Succeed to fix the relocation entry.
75 @retval RETURN_UNSUPPOTED Unrecoganized relocation entry.
76
77 **/
78 RETURN_STATUS
79 PeCoffLoaderRelocateImageEx (
80 IN UINT16 *Reloc,
81 IN OUT CHAR8 *Fixup,
82 IN OUT CHAR8 **FixupData,
83 IN UINT64 Adjust
84 )
85 {
86 UINT64 *Fixup64;
87 UINT64 FixupVal;
88
89 switch ((*Reloc) >> 12) {
90 case EFI_IMAGE_REL_BASED_IA64_IMM64:
91
92 //
93 // Align it to bundle address before fixing up the
94 // 64-bit immediate value of the movl instruction.
95 //
96
97 Fixup = (CHAR8 *)((UINTN) Fixup & (UINTN) ~(15));
98 FixupVal = (UINT64)0;
99
100 //
101 // Extract the lower 32 bits of IMM64 from bundle
102 //
103 EXT_IMM64(FixupVal,
104 (UINT32 *)Fixup + IMM64_IMM7B_INST_WORD_X,
105 IMM64_IMM7B_SIZE_X,
106 IMM64_IMM7B_INST_WORD_POS_X,
107 IMM64_IMM7B_VAL_POS_X
108 );
109
110 EXT_IMM64(FixupVal,
111 (UINT32 *)Fixup + IMM64_IMM9D_INST_WORD_X,
112 IMM64_IMM9D_SIZE_X,
113 IMM64_IMM9D_INST_WORD_POS_X,
114 IMM64_IMM9D_VAL_POS_X
115 );
116
117 EXT_IMM64(FixupVal,
118 (UINT32 *)Fixup + IMM64_IMM5C_INST_WORD_X,
119 IMM64_IMM5C_SIZE_X,
120 IMM64_IMM5C_INST_WORD_POS_X,
121 IMM64_IMM5C_VAL_POS_X
122 );
123
124 EXT_IMM64(FixupVal,
125 (UINT32 *)Fixup + IMM64_IC_INST_WORD_X,
126 IMM64_IC_SIZE_X,
127 IMM64_IC_INST_WORD_POS_X,
128 IMM64_IC_VAL_POS_X
129 );
130
131 EXT_IMM64(FixupVal,
132 (UINT32 *)Fixup + IMM64_IMM41A_INST_WORD_X,
133 IMM64_IMM41A_SIZE_X,
134 IMM64_IMM41A_INST_WORD_POS_X,
135 IMM64_IMM41A_VAL_POS_X
136 );
137
138 //
139 // Update 64-bit address
140 //
141 FixupVal += Adjust;
142
143 //
144 // Insert IMM64 into bundle
145 //
146 INS_IMM64(FixupVal,
147 ((UINT32 *)Fixup + IMM64_IMM7B_INST_WORD_X),
148 IMM64_IMM7B_SIZE_X,
149 IMM64_IMM7B_INST_WORD_POS_X,
150 IMM64_IMM7B_VAL_POS_X
151 );
152
153 INS_IMM64(FixupVal,
154 ((UINT32 *)Fixup + IMM64_IMM9D_INST_WORD_X),
155 IMM64_IMM9D_SIZE_X,
156 IMM64_IMM9D_INST_WORD_POS_X,
157 IMM64_IMM9D_VAL_POS_X
158 );
159
160 INS_IMM64(FixupVal,
161 ((UINT32 *)Fixup + IMM64_IMM5C_INST_WORD_X),
162 IMM64_IMM5C_SIZE_X,
163 IMM64_IMM5C_INST_WORD_POS_X,
164 IMM64_IMM5C_VAL_POS_X
165 );
166
167 INS_IMM64(FixupVal,
168 ((UINT32 *)Fixup + IMM64_IC_INST_WORD_X),
169 IMM64_IC_SIZE_X,
170 IMM64_IC_INST_WORD_POS_X,
171 IMM64_IC_VAL_POS_X
172 );
173
174 INS_IMM64(FixupVal,
175 ((UINT32 *)Fixup + IMM64_IMM41A_INST_WORD_X),
176 IMM64_IMM41A_SIZE_X,
177 IMM64_IMM41A_INST_WORD_POS_X,
178 IMM64_IMM41A_VAL_POS_X
179 );
180
181 INS_IMM64(FixupVal,
182 ((UINT32 *)Fixup + IMM64_IMM41B_INST_WORD_X),
183 IMM64_IMM41B_SIZE_X,
184 IMM64_IMM41B_INST_WORD_POS_X,
185 IMM64_IMM41B_VAL_POS_X
186 );
187
188 INS_IMM64(FixupVal,
189 ((UINT32 *)Fixup + IMM64_IMM41C_INST_WORD_X),
190 IMM64_IMM41C_SIZE_X,
191 IMM64_IMM41C_INST_WORD_POS_X,
192 IMM64_IMM41C_VAL_POS_X
193 );
194
195 INS_IMM64(FixupVal,
196 ((UINT32 *)Fixup + IMM64_SIGN_INST_WORD_X),
197 IMM64_SIGN_SIZE_X,
198 IMM64_SIGN_INST_WORD_POS_X,
199 IMM64_SIGN_VAL_POS_X
200 );
201
202 Fixup64 = (UINT64 *) Fixup;
203 if (*FixupData != NULL) {
204 *FixupData = ALIGN_POINTER(*FixupData, sizeof(UINT64));
205 *(UINT64 *)(*FixupData) = *Fixup64;
206 *FixupData = *FixupData + sizeof(UINT64);
207 }
208 break;
209
210 default:
211 return RETURN_UNSUPPORTED;
212 }
213
214 return RETURN_SUCCESS;
215 }
216
217 /**
218 Returns TRUE if the machine type of PE/COFF image is supported. Supported
219 does not mean the image can be executed it means the PE/COFF loader supports
220 loading and relocating of the image type. It's up to the caller to support
221 the entry point.
222
223 This function implies the basic PE/COFF loader/relocator supports IA32, EBC,
224 & x64 images. Calling the entry point in a correct mannor is up to the
225 consumer of this library. This version also supports the special relocations
226 for Itanium.
227
228 @param Machine Machine type from the PE Header.
229
230 @return TRUE if this PE/COFF loader can load the image
231 @return FALSE unrecoganized machine type of image.
232
233 **/
234 BOOLEAN
235 PeCoffLoaderImageFormatSupported (
236 IN UINT16 Machine
237 )
238 {
239 if ((Machine == IMAGE_FILE_MACHINE_IA64) || (Machine == IMAGE_FILE_MACHINE_EBC)) {
240 return TRUE;
241 }
242
243 return FALSE;
244 }
245
246
247 /**
248 ImageRead function that operates on a memory buffer whos base is passed into
249 FileHandle.
250
251 @param Reloc Ponter to baes of the input stream
252 @param Fixup Offset to the start of the buffer
253 @param FixupData Number of bytes to copy into the buffer
254 @param Adjust Location to place results of read
255
256 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
257 the buffer.
258 @retval RETURN_UNSUPPORTED Un-recoganized relocation entry
259 type.
260 **/
261 RETURN_STATUS
262 PeHotRelocateImageEx (
263 IN UINT16 *Reloc,
264 IN OUT CHAR8 *Fixup,
265 IN OUT CHAR8 **FixupData,
266 IN UINT64 Adjust
267 )
268 {
269 UINT64 *Fixup64;
270 UINT64 FixupVal;
271
272 switch ((*Reloc) >> 12) {
273 case EFI_IMAGE_REL_BASED_DIR64:
274 Fixup64 = (UINT64 *) Fixup;
275 *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT64));
276 if (*(UINT64 *) (*FixupData) == *Fixup64) {
277 *Fixup64 = *Fixup64 + (UINT64) Adjust;
278 }
279
280 *FixupData = *FixupData + sizeof (UINT64);
281 break;
282
283 case EFI_IMAGE_REL_BASED_IA64_IMM64:
284 Fixup64 = (UINT64 *) Fixup;
285 *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT64));
286 if (*(UINT64 *) (*FixupData) == *Fixup64) {
287 //
288 // Align it to bundle address before fixing up the
289 // 64-bit immediate value of the movl instruction.
290 //
291 //
292 Fixup = (CHAR8 *) ((UINT64) Fixup & (UINT64)~(15));
293 FixupVal = (UINT64) 0;
294
295 //
296 // Extract the lower 32 bits of IMM64 from bundle
297 //
298 EXT_IMM64 (
299 FixupVal,
300 (UINT32 *) Fixup + IMM64_IMM7B_INST_WORD_X,
301 IMM64_IMM7B_SIZE_X,
302 IMM64_IMM7B_INST_WORD_POS_X,
303 IMM64_IMM7B_VAL_POS_X
304 );
305
306 EXT_IMM64 (
307 FixupVal,
308 (UINT32 *) Fixup + IMM64_IMM9D_INST_WORD_X,
309 IMM64_IMM9D_SIZE_X,
310 IMM64_IMM9D_INST_WORD_POS_X,
311 IMM64_IMM9D_VAL_POS_X
312 );
313
314 EXT_IMM64 (
315 FixupVal,
316 (UINT32 *) Fixup + IMM64_IMM5C_INST_WORD_X,
317 IMM64_IMM5C_SIZE_X,
318 IMM64_IMM5C_INST_WORD_POS_X,
319 IMM64_IMM5C_VAL_POS_X
320 );
321
322 EXT_IMM64 (
323 FixupVal,
324 (UINT32 *) Fixup + IMM64_IC_INST_WORD_X,
325 IMM64_IC_SIZE_X,
326 IMM64_IC_INST_WORD_POS_X,
327 IMM64_IC_VAL_POS_X
328 );
329
330 EXT_IMM64 (
331 FixupVal,
332 (UINT32 *) Fixup + IMM64_IMM41A_INST_WORD_X,
333 IMM64_IMM41A_SIZE_X,
334 IMM64_IMM41A_INST_WORD_POS_X,
335 IMM64_IMM41A_VAL_POS_X
336 );
337
338 //
339 // Update 64-bit address
340 //
341 FixupVal += Adjust;
342
343 //
344 // Insert IMM64 into bundle
345 //
346 INS_IMM64 (
347 FixupVal,
348 ((UINT32 *) Fixup + IMM64_IMM7B_INST_WORD_X),
349 IMM64_IMM7B_SIZE_X,
350 IMM64_IMM7B_INST_WORD_POS_X,
351 IMM64_IMM7B_VAL_POS_X
352 );
353
354 INS_IMM64 (
355 FixupVal,
356 ((UINT32 *) Fixup + IMM64_IMM9D_INST_WORD_X),
357 IMM64_IMM9D_SIZE_X,
358 IMM64_IMM9D_INST_WORD_POS_X,
359 IMM64_IMM9D_VAL_POS_X
360 );
361
362 INS_IMM64 (
363 FixupVal,
364 ((UINT32 *) Fixup + IMM64_IMM5C_INST_WORD_X),
365 IMM64_IMM5C_SIZE_X,
366 IMM64_IMM5C_INST_WORD_POS_X,
367 IMM64_IMM5C_VAL_POS_X
368 );
369
370 INS_IMM64 (
371 FixupVal,
372 ((UINT32 *) Fixup + IMM64_IC_INST_WORD_X),
373 IMM64_IC_SIZE_X,
374 IMM64_IC_INST_WORD_POS_X,
375 IMM64_IC_VAL_POS_X
376 );
377
378 INS_IMM64 (
379 FixupVal,
380 ((UINT32 *) Fixup + IMM64_IMM41A_INST_WORD_X),
381 IMM64_IMM41A_SIZE_X,
382 IMM64_IMM41A_INST_WORD_POS_X,
383 IMM64_IMM41A_VAL_POS_X
384 );
385
386 INS_IMM64 (
387 FixupVal,
388 ((UINT32 *) Fixup + IMM64_IMM41B_INST_WORD_X),
389 IMM64_IMM41B_SIZE_X,
390 IMM64_IMM41B_INST_WORD_POS_X,
391 IMM64_IMM41B_VAL_POS_X
392 );
393
394 INS_IMM64 (
395 FixupVal,
396 ((UINT32 *) Fixup + IMM64_IMM41C_INST_WORD_X),
397 IMM64_IMM41C_SIZE_X,
398 IMM64_IMM41C_INST_WORD_POS_X,
399 IMM64_IMM41C_VAL_POS_X
400 );
401
402 INS_IMM64 (
403 FixupVal,
404 ((UINT32 *) Fixup + IMM64_SIGN_INST_WORD_X),
405 IMM64_SIGN_SIZE_X,
406 IMM64_SIGN_INST_WORD_POS_X,
407 IMM64_SIGN_VAL_POS_X
408 );
409
410 *(UINT64 *) (*FixupData) = *Fixup64;
411 }
412
413 *FixupData = *FixupData + sizeof (UINT64);
414 break;
415
416 default:
417 DEBUG ((EFI_D_ERROR, "PeHotRelocateEx:unknown fixed type\n"));
418 return RETURN_UNSUPPORTED;
419 }
420
421 return RETURN_SUCCESS;
422 }
423
424
425