2 Specific relocation fixups for ARM architecture.
4 Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "BasePeCoffLibInternals.h"
17 #include <Library/BaseLib.h>
21 Pass in a pointer to an ARM MOVT or MOVW immediate instruciton and
22 return the immediate data encoded in the instruction.
24 @param Instruction Pointer to ARM MOVT or MOVW immediate instruction
26 @return Immediate address encoded in the instruction
30 ThumbMovtImmediateAddress (
31 IN UINT16
*Instruction
37 // Thumb2 is two 16-bit instructions working together. Not a single 32-bit instruction
38 // Example MOVT R0, #0 is 0x0000f2c0 or 0xf2c0 0x0000
39 Movt
= (*Instruction
<< 16) | (*(Instruction
+ 1));
41 // imm16 = imm4:i:imm3:imm8
42 // imm4 -> Bit19:Bit16
44 // imm3 -> Bit14:Bit12
46 Address
= (UINT16
)(Movt
& 0x000000ff); // imm8
47 Address
|= (UINT16
)((Movt
>> 4) & 0x0000f700); // imm4 imm3
48 Address
|= (((Movt
& BIT26
) != 0) ? BIT11
: 0); // i
54 Update an ARM MOVT or MOVW immediate instruction immediate data.
56 @param Instruction Pointer to ARM MOVT or MOVW immediate instruction
57 @param Address New addres to patch into the instruction
60 ThumbMovtImmediatePatch (
61 IN OUT UINT16
*Instruction
,
67 // First 16-bit chunk of instruciton
68 Patch
= ((Address
>> 12) & 0x000f); // imm4
69 Patch
|= (((Address
& BIT11
) != 0) ? BIT10
: 0); // i
70 // Mask out instruction bits and or in address
71 *(Instruction
) = (*Instruction
& ~0x040f) | Patch
;
73 // Second 16-bit chunk of instruction
74 Patch
= Address
& 0x000000ff; // imm8
75 Patch
|= ((Address
<< 4) & 0x00007000); // imm3
76 // Mask out instruction bits and or in address
78 *Instruction
= (*Instruction
& ~0x70ff) | Patch
;
84 Pass in a pointer to an ARM MOVW/MOVT instruciton pair and
85 return the immediate data encoded in the two` instruction.
87 @param Instructions Pointer to ARM MOVW/MOVT insturction pair
89 @return Immediate address encoded in the instructions
93 ThumbMovwMovtImmediateAddress (
94 IN UINT16
*Instructions
100 Word
= Instructions
; // MOVW
101 Top
= Word
+ 2; // MOVT
103 return (ThumbMovtImmediateAddress (Top
) << 16) + ThumbMovtImmediateAddress (Word
);
108 Update an ARM MOVW/MOVT immediate instruction instruction pair.
110 @param Instructions Pointer to ARM MOVW/MOVT instruction pair
111 @param Address New addres to patch into the instructions
114 ThumbMovwMovtImmediatePatch (
115 IN OUT UINT16
*Instructions
,
122 Word
= Instructions
; // MOVW
123 Top
= Word
+ 2; // MOVT
125 ThumbMovtImmediatePatch (Word
, (UINT16
)(Address
& 0xffff));
126 ThumbMovtImmediatePatch (Top
, (UINT16
)(Address
>> 16));
132 Performs an ARM-based specific relocation fixup and is a no-op on other
135 @param Reloc The pointer to the relocation record.
136 @param Fixup The pointer to the address to fix up.
137 @param FixupData The pointer to a buffer to log the fixups.
138 @param Adjust The offset to adjust the fixup.
144 PeCoffLoaderRelocateImageEx (
147 IN OUT CHAR8
**FixupData
,
154 Fixup16
= (UINT16
*) Fixup
;
156 switch ((*Reloc
) >> 12) {
158 case EFI_IMAGE_REL_BASED_ARM_MOV32T
:
159 FixupVal
= ThumbMovwMovtImmediateAddress (Fixup16
) + (UINT32
)Adjust
;
160 ThumbMovwMovtImmediatePatch (Fixup16
, FixupVal
);
162 if (*FixupData
!= NULL
) {
163 *FixupData
= ALIGN_POINTER(*FixupData
, sizeof(UINT64
));
164 // Fixup16 is not aligned so we must copy it. Thumb instructions are streams of 16 bytes.
165 CopyMem (*FixupData
, Fixup16
, sizeof (UINT64
));
166 *FixupData
= *FixupData
+ sizeof(UINT64
);
170 case EFI_IMAGE_REL_BASED_ARM_MOV32A
:
172 // break omitted - ARM instruction encoding not implemented
174 return RETURN_UNSUPPORTED
;
177 return RETURN_SUCCESS
;
181 Returns TRUE if the machine type of PE/COFF image is supported. Supported
182 does not mean the image can be executed it means the PE/COFF loader supports
183 loading and relocating of the image type. It's up to the caller to support
186 @param Machine Machine type from the PE Header.
188 @return TRUE if this PE/COFF loader can load the image
192 PeCoffLoaderImageFormatSupported (
196 if ((Machine
== IMAGE_FILE_MACHINE_ARMTHUMB_MIXED
) || (Machine
== IMAGE_FILE_MACHINE_EBC
)) {
204 Performs an ARM-based specific re-relocation fixup and is a no-op on other
205 instruction sets. This is used to re-relocated the image into the EFI virtual
206 space for runtime calls.
208 @param Reloc The pointer to the relocation record.
209 @param Fixup The pointer to the address to fix up.
210 @param FixupData The pointer to a buffer to log the fixups.
211 @param Adjust The offset to adjust the fixup.
217 PeHotRelocateImageEx (
220 IN OUT CHAR8
**FixupData
,
227 Fixup16
= (UINT16
*)Fixup
;
229 switch ((*Reloc
) >> 12) {
231 case EFI_IMAGE_REL_BASED_ARM_MOV32T
:
232 *FixupData
= ALIGN_POINTER (*FixupData
, sizeof (UINT64
));
233 if (*(UINT64
*) (*FixupData
) == ReadUnaligned64 ((UINT64
*)Fixup16
)) {
234 FixupVal
= ThumbMovwMovtImmediateAddress (Fixup16
) + (UINT32
)Adjust
;
235 ThumbMovwMovtImmediatePatch (Fixup16
, FixupVal
);
237 *FixupData
= *FixupData
+ sizeof(UINT64
);
240 case EFI_IMAGE_REL_BASED_ARM_MOV32A
:
242 // break omitted - ARM instruction encoding not implemented
244 DEBUG ((EFI_D_ERROR
, "PeHotRelocateEx:unknown fixed type\n"));
245 return RETURN_UNSUPPORTED
;
248 return RETURN_SUCCESS
;