]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePeCoffLib/Ipf/PeCoffLoaderEx.c
Removed MdePkg usage of ModuleName: in file headers
[mirror_edk2.git] / MdePkg / Library / BasePeCoffLib / Ipf / PeCoffLoaderEx.c
1 /** @file
2 Fixes Intel Itanium(TM) specific relocation types.
3
4 Copyright (c) 2006, 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 @return Status code.
75
76 **/
77 RETURN_STATUS
78 PeCoffLoaderRelocateImageEx (
79 IN UINT16 *Reloc,
80 IN OUT CHAR8 *Fixup,
81 IN OUT CHAR8 **FixupData,
82 IN UINT64 Adjust
83 )
84 {
85 UINT64 *F64;
86 UINT64 FixupVal;
87
88 switch ((*Reloc) >> 12) {
89 case EFI_IMAGE_REL_BASED_IA64_IMM64:
90
91 //
92 // Align it to bundle address before fixing up the
93 // 64-bit immediate value of the movl instruction.
94 //
95
96 Fixup = (CHAR8 *)((UINTN) Fixup & (UINTN) ~(15));
97 FixupVal = (UINT64)0;
98
99 //
100 // Extract the lower 32 bits of IMM64 from bundle
101 //
102 EXT_IMM64(FixupVal,
103 (UINT32 *)Fixup + IMM64_IMM7B_INST_WORD_X,
104 IMM64_IMM7B_SIZE_X,
105 IMM64_IMM7B_INST_WORD_POS_X,
106 IMM64_IMM7B_VAL_POS_X
107 );
108
109 EXT_IMM64(FixupVal,
110 (UINT32 *)Fixup + IMM64_IMM9D_INST_WORD_X,
111 IMM64_IMM9D_SIZE_X,
112 IMM64_IMM9D_INST_WORD_POS_X,
113 IMM64_IMM9D_VAL_POS_X
114 );
115
116 EXT_IMM64(FixupVal,
117 (UINT32 *)Fixup + IMM64_IMM5C_INST_WORD_X,
118 IMM64_IMM5C_SIZE_X,
119 IMM64_IMM5C_INST_WORD_POS_X,
120 IMM64_IMM5C_VAL_POS_X
121 );
122
123 EXT_IMM64(FixupVal,
124 (UINT32 *)Fixup + IMM64_IC_INST_WORD_X,
125 IMM64_IC_SIZE_X,
126 IMM64_IC_INST_WORD_POS_X,
127 IMM64_IC_VAL_POS_X
128 );
129
130 EXT_IMM64(FixupVal,
131 (UINT32 *)Fixup + IMM64_IMM41a_INST_WORD_X,
132 IMM64_IMM41a_SIZE_X,
133 IMM64_IMM41a_INST_WORD_POS_X,
134 IMM64_IMM41a_VAL_POS_X
135 );
136
137 //
138 // Update 64-bit address
139 //
140 FixupVal += Adjust;
141
142 //
143 // Insert IMM64 into bundle
144 //
145 INS_IMM64(FixupVal,
146 ((UINT32 *)Fixup + IMM64_IMM7B_INST_WORD_X),
147 IMM64_IMM7B_SIZE_X,
148 IMM64_IMM7B_INST_WORD_POS_X,
149 IMM64_IMM7B_VAL_POS_X
150 );
151
152 INS_IMM64(FixupVal,
153 ((UINT32 *)Fixup + IMM64_IMM9D_INST_WORD_X),
154 IMM64_IMM9D_SIZE_X,
155 IMM64_IMM9D_INST_WORD_POS_X,
156 IMM64_IMM9D_VAL_POS_X
157 );
158
159 INS_IMM64(FixupVal,
160 ((UINT32 *)Fixup + IMM64_IMM5C_INST_WORD_X),
161 IMM64_IMM5C_SIZE_X,
162 IMM64_IMM5C_INST_WORD_POS_X,
163 IMM64_IMM5C_VAL_POS_X
164 );
165
166 INS_IMM64(FixupVal,
167 ((UINT32 *)Fixup + IMM64_IC_INST_WORD_X),
168 IMM64_IC_SIZE_X,
169 IMM64_IC_INST_WORD_POS_X,
170 IMM64_IC_VAL_POS_X
171 );
172
173 INS_IMM64(FixupVal,
174 ((UINT32 *)Fixup + IMM64_IMM41a_INST_WORD_X),
175 IMM64_IMM41a_SIZE_X,
176 IMM64_IMM41a_INST_WORD_POS_X,
177 IMM64_IMM41a_VAL_POS_X
178 );
179
180 INS_IMM64(FixupVal,
181 ((UINT32 *)Fixup + IMM64_IMM41b_INST_WORD_X),
182 IMM64_IMM41b_SIZE_X,
183 IMM64_IMM41b_INST_WORD_POS_X,
184 IMM64_IMM41b_VAL_POS_X
185 );
186
187 INS_IMM64(FixupVal,
188 ((UINT32 *)Fixup + IMM64_IMM41c_INST_WORD_X),
189 IMM64_IMM41c_SIZE_X,
190 IMM64_IMM41c_INST_WORD_POS_X,
191 IMM64_IMM41c_VAL_POS_X
192 );
193
194 INS_IMM64(FixupVal,
195 ((UINT32 *)Fixup + IMM64_SIGN_INST_WORD_X),
196 IMM64_SIGN_SIZE_X,
197 IMM64_SIGN_INST_WORD_POS_X,
198 IMM64_SIGN_VAL_POS_X
199 );
200
201 F64 = (UINT64 *) Fixup;
202 if (*FixupData != NULL) {
203 *FixupData = ALIGN_POINTER(*FixupData, sizeof(UINT64));
204 *(UINT64 *)(*FixupData) = *F64;
205 *FixupData = *FixupData + sizeof(UINT64);
206 }
207 break;
208
209 default:
210 return RETURN_UNSUPPORTED;
211 }
212
213 return RETURN_SUCCESS;
214 }
215
216 /**
217 Returns TRUE if the machine type of PE/COFF image is supported. Supported
218 does not mean the image can be executed it means the PE/COFF loader supports
219 loading and relocating of the image type. It's up to the caller to support
220 the entry point.
221
222 This function implies the basic PE/COFF loader/relocator supports IA32, EBC,
223 & X64 images. Calling the entry point in a correct mannor is up to the
224 consumer of this library. This version also supports the special relocations
225 for Itanium.
226
227 @param Machine Machine type from the PE Header.
228
229 @return TRUE if this PE/COFF loader can load the image
230
231 **/
232 BOOLEAN
233 PeCoffLoaderImageFormatSupported (
234 IN UINT16 Machine
235 )
236 {
237 if ((Machine == EFI_IMAGE_MACHINE_IPF) || (Machine == EFI_IMAGE_MACHINE_IA32) ||
238 (Machine == EFI_IMAGE_MACHINE_EBC) || (Machine == EFI_IMAGE_MACHINE_X64)) {
239 return TRUE;
240 }
241
242 return FALSE;
243 }
244
245
246 /**
247 ImageRead function that operates on a memory buffer whos base is passed into
248 FileHandle.
249
250 @param Reloc Ponter to baes of the input stream
251 @param Fixup Offset to the start of the buffer
252 @param FixupData Number of bytes to copy into the buffer
253 @param Adjust Location to place results of read
254
255 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
256 the buffer.
257 **/
258 RETURN_STATUS
259 PeHotRelocateImageEx (
260 IN UINT16 *Reloc,
261 IN OUT CHAR8 *Fixup,
262 IN OUT CHAR8 **FixupData,
263 IN UINT64 Adjust
264 )
265 {
266 UINT64 *F64;
267 UINT64 FixupVal;
268
269 switch ((*Reloc) >> 12) {
270 case EFI_IMAGE_REL_BASED_DIR64:
271 F64 = (UINT64 *) Fixup;
272 *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT64));
273 if (*(UINT64 *) (*FixupData) == *F64) {
274 *F64 = *F64 + (UINT64) Adjust;
275 }
276
277 *FixupData = *FixupData + sizeof (UINT64);
278 break;
279
280 case EFI_IMAGE_REL_BASED_IA64_IMM64:
281 F64 = (UINT64 *) Fixup;
282 *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT64));
283 if (*(UINT64 *) (*FixupData) == *F64) {
284 //
285 // Align it to bundle address before fixing up the
286 // 64-bit immediate value of the movl instruction.
287 //
288 //
289 Fixup = (CHAR8 *) ((UINT64) Fixup & (UINT64)~(15));
290 FixupVal = (UINT64) 0;
291
292 //
293 // Extract the lower 32 bits of IMM64 from bundle
294 //
295 EXT_IMM64 (
296 FixupVal,
297 (UINT32 *) Fixup + IMM64_IMM7B_INST_WORD_X,
298 IMM64_IMM7B_SIZE_X,
299 IMM64_IMM7B_INST_WORD_POS_X,
300 IMM64_IMM7B_VAL_POS_X
301 );
302
303 EXT_IMM64 (
304 FixupVal,
305 (UINT32 *) Fixup + IMM64_IMM9D_INST_WORD_X,
306 IMM64_IMM9D_SIZE_X,
307 IMM64_IMM9D_INST_WORD_POS_X,
308 IMM64_IMM9D_VAL_POS_X
309 );
310
311 EXT_IMM64 (
312 FixupVal,
313 (UINT32 *) Fixup + IMM64_IMM5C_INST_WORD_X,
314 IMM64_IMM5C_SIZE_X,
315 IMM64_IMM5C_INST_WORD_POS_X,
316 IMM64_IMM5C_VAL_POS_X
317 );
318
319 EXT_IMM64 (
320 FixupVal,
321 (UINT32 *) Fixup + IMM64_IC_INST_WORD_X,
322 IMM64_IC_SIZE_X,
323 IMM64_IC_INST_WORD_POS_X,
324 IMM64_IC_VAL_POS_X
325 );
326
327 EXT_IMM64 (
328 FixupVal,
329 (UINT32 *) Fixup + IMM64_IMM41a_INST_WORD_X,
330 IMM64_IMM41a_SIZE_X,
331 IMM64_IMM41a_INST_WORD_POS_X,
332 IMM64_IMM41a_VAL_POS_X
333 );
334
335 //
336 // Update 64-bit address
337 //
338 FixupVal += Adjust;
339
340 //
341 // Insert IMM64 into bundle
342 //
343 INS_IMM64 (
344 FixupVal,
345 ((UINT32 *) Fixup + IMM64_IMM7B_INST_WORD_X),
346 IMM64_IMM7B_SIZE_X,
347 IMM64_IMM7B_INST_WORD_POS_X,
348 IMM64_IMM7B_VAL_POS_X
349 );
350
351 INS_IMM64 (
352 FixupVal,
353 ((UINT32 *) Fixup + IMM64_IMM9D_INST_WORD_X),
354 IMM64_IMM9D_SIZE_X,
355 IMM64_IMM9D_INST_WORD_POS_X,
356 IMM64_IMM9D_VAL_POS_X
357 );
358
359 INS_IMM64 (
360 FixupVal,
361 ((UINT32 *) Fixup + IMM64_IMM5C_INST_WORD_X),
362 IMM64_IMM5C_SIZE_X,
363 IMM64_IMM5C_INST_WORD_POS_X,
364 IMM64_IMM5C_VAL_POS_X
365 );
366
367 INS_IMM64 (
368 FixupVal,
369 ((UINT32 *) Fixup + IMM64_IC_INST_WORD_X),
370 IMM64_IC_SIZE_X,
371 IMM64_IC_INST_WORD_POS_X,
372 IMM64_IC_VAL_POS_X
373 );
374
375 INS_IMM64 (
376 FixupVal,
377 ((UINT32 *) Fixup + IMM64_IMM41a_INST_WORD_X),
378 IMM64_IMM41a_SIZE_X,
379 IMM64_IMM41a_INST_WORD_POS_X,
380 IMM64_IMM41a_VAL_POS_X
381 );
382
383 INS_IMM64 (
384 FixupVal,
385 ((UINT32 *) Fixup + IMM64_IMM41b_INST_WORD_X),
386 IMM64_IMM41b_SIZE_X,
387 IMM64_IMM41b_INST_WORD_POS_X,
388 IMM64_IMM41b_VAL_POS_X
389 );
390
391 INS_IMM64 (
392 FixupVal,
393 ((UINT32 *) Fixup + IMM64_IMM41c_INST_WORD_X),
394 IMM64_IMM41c_SIZE_X,
395 IMM64_IMM41c_INST_WORD_POS_X,
396 IMM64_IMM41c_VAL_POS_X
397 );
398
399 INS_IMM64 (
400 FixupVal,
401 ((UINT32 *) Fixup + IMM64_SIGN_INST_WORD_X),
402 IMM64_SIGN_SIZE_X,
403 IMM64_SIGN_INST_WORD_POS_X,
404 IMM64_SIGN_VAL_POS_X
405 );
406
407 *(UINT64 *) (*FixupData) = *F64;
408 }
409
410 *FixupData = *FixupData + sizeof (UINT64);
411 break;
412
413 default:
414 DEBUG ((EFI_D_ERROR, "PeHotRelocateEx:unknown fixed type\n"));
415 return RETURN_UNSUPPORTED;
416 }
417
418 return RETURN_SUCCESS;
419 }
420
421
422