]>
Commit | Line | Data |
---|---|---|
30fdf114 | 1 | /** @file\r |
8daa4278 | 2 | IA32 and X64 Specific relocation fixups\r |
30fdf114 | 3 | \r |
f7496d71 | 4 | Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>\r |
4afd3d04 | 5 | Portions Copyright (c) 2011 - 2013, ARM Ltd. All rights reserved.<BR>\r |
2e351cbe | 6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
30fdf114 | 7 | \r |
30fdf114 LG |
8 | --*/\r |
9 | \r | |
10 | #include <Common/UefiBaseTypes.h>\r | |
11 | #include <IndustryStandard/PeImage.h>\r | |
12 | #include "PeCoffLib.h"\r | |
da92f276 | 13 | #include "CommonLib.h"\r |
4afd3d04 | 14 | #include "EfiUtilityMsgs.h"\r |
da92f276 | 15 | \r |
30fdf114 LG |
16 | \r |
17 | #define EXT_IMM64(Value, Address, Size, InstPos, ValPos) \\r | |
18 | Value |= (((UINT64)((*(Address) >> InstPos) & (((UINT64)1 << Size) - 1))) << ValPos)\r | |
19 | \r | |
20 | #define INS_IMM64(Value, Address, Size, InstPos, ValPos) \\r | |
21 | *(UINT32*)Address = (*(UINT32*)Address & ~(((1 << Size) - 1) << InstPos)) | \\r | |
22 | ((UINT32)((((UINT64)Value >> ValPos) & (((UINT64)1 << Size) - 1))) << InstPos)\r | |
23 | \r | |
f7496d71 LG |
24 | #define IMM64_IMM7B_INST_WORD_X 3\r |
25 | #define IMM64_IMM7B_SIZE_X 7\r | |
26 | #define IMM64_IMM7B_INST_WORD_POS_X 4\r | |
27 | #define IMM64_IMM7B_VAL_POS_X 0\r | |
28 | \r | |
29 | #define IMM64_IMM9D_INST_WORD_X 3\r | |
30 | #define IMM64_IMM9D_SIZE_X 9\r | |
31 | #define IMM64_IMM9D_INST_WORD_POS_X 18\r | |
32 | #define IMM64_IMM9D_VAL_POS_X 7\r | |
33 | \r | |
34 | #define IMM64_IMM5C_INST_WORD_X 3\r | |
35 | #define IMM64_IMM5C_SIZE_X 5\r | |
36 | #define IMM64_IMM5C_INST_WORD_POS_X 13\r | |
37 | #define IMM64_IMM5C_VAL_POS_X 16\r | |
38 | \r | |
39 | #define IMM64_IC_INST_WORD_X 3\r | |
40 | #define IMM64_IC_SIZE_X 1\r | |
41 | #define IMM64_IC_INST_WORD_POS_X 12\r | |
42 | #define IMM64_IC_VAL_POS_X 21\r | |
43 | \r | |
44 | #define IMM64_IMM41a_INST_WORD_X 1\r | |
45 | #define IMM64_IMM41a_SIZE_X 10\r | |
46 | #define IMM64_IMM41a_INST_WORD_POS_X 14\r | |
47 | #define IMM64_IMM41a_VAL_POS_X 22\r | |
48 | \r | |
49 | #define IMM64_IMM41b_INST_WORD_X 1\r | |
50 | #define IMM64_IMM41b_SIZE_X 8\r | |
51 | #define IMM64_IMM41b_INST_WORD_POS_X 24\r | |
52 | #define IMM64_IMM41b_VAL_POS_X 32\r | |
53 | \r | |
54 | #define IMM64_IMM41c_INST_WORD_X 2\r | |
55 | #define IMM64_IMM41c_SIZE_X 23\r | |
56 | #define IMM64_IMM41c_INST_WORD_POS_X 0\r | |
57 | #define IMM64_IMM41c_VAL_POS_X 40\r | |
58 | \r | |
59 | #define IMM64_SIGN_INST_WORD_X 3\r | |
60 | #define IMM64_SIGN_SIZE_X 1\r | |
61 | #define IMM64_SIGN_INST_WORD_POS_X 27\r | |
62 | #define IMM64_SIGN_VAL_POS_X 63\r | |
30fdf114 LG |
63 | \r |
64 | RETURN_STATUS\r | |
65 | PeCoffLoaderRelocateIa32Image (\r | |
66 | IN UINT16 *Reloc,\r | |
67 | IN OUT CHAR8 *Fixup,\r | |
68 | IN OUT CHAR8 **FixupData,\r | |
69 | IN UINT64 Adjust\r | |
70 | )\r | |
71 | /*++\r | |
72 | \r | |
73 | Routine Description:\r | |
74 | \r | |
75 | Performs an IA-32 specific relocation fixup\r | |
76 | \r | |
77 | Arguments:\r | |
78 | \r | |
79 | Reloc - Pointer to the relocation record\r | |
80 | \r | |
81 | Fixup - Pointer to the address to fix up\r | |
82 | \r | |
83 | FixupData - Pointer to a buffer to log the fixups\r | |
84 | \r | |
85 | Adjust - The offset to adjust the fixup\r | |
86 | \r | |
87 | Returns:\r | |
88 | \r | |
89 | EFI_UNSUPPORTED - Unsupported now\r | |
90 | \r | |
91 | --*/\r | |
92 | {\r | |
93 | return RETURN_UNSUPPORTED;\r | |
94 | }\r | |
95 | \r | |
30fdf114 | 96 | \r |
40d841f6 | 97 | /**\r |
fb0b35e0 | 98 | Pass in a pointer to an ARM MOVT or MOVW immediate instruction and\r |
40d841f6 LG |
99 | return the immediate data encoded in the instruction\r |
100 | \r | |
101 | @param Instruction Pointer to ARM MOVT or MOVW immediate instruction\r | |
102 | \r | |
103 | @return Immediate address encoded in the instruction\r | |
104 | \r | |
105 | **/\r | |
106 | UINT16\r | |
107 | ThumbMovtImmediateAddress (\r | |
108 | IN UINT16 *Instruction\r | |
109 | )\r | |
110 | {\r | |
111 | UINT32 Movt;\r | |
112 | UINT16 Address;\r | |
113 | \r | |
114 | // Thumb2 is two 16-bit instructions working together. Not a single 32-bit instruction\r | |
115 | // Example MOVT R0, #0 is 0x0000f2c0 or 0xf2c0 0x0000\r | |
f7496d71 | 116 | Movt = (*Instruction << 16) | (*(Instruction + 1));\r |
40d841f6 LG |
117 | \r |
118 | // imm16 = imm4:i:imm3:imm8\r | |
119 | // imm4 -> Bit19:Bit16\r | |
120 | // i -> Bit26\r | |
121 | // imm3 -> Bit14:Bit12\r | |
122 | // imm8 -> Bit7:Bit0\r | |
123 | Address = (UINT16)(Movt & 0x000000ff); // imm8\r | |
124 | Address |= (UINT16)((Movt >> 4) & 0x0000f700); // imm4 imm3\r | |
125 | Address |= (((Movt & BIT26) != 0) ? BIT11 : 0); // i\r | |
126 | return Address;\r | |
127 | }\r | |
128 | \r | |
129 | \r | |
130 | /**\r | |
131 | Update an ARM MOVT or MOVW immediate instruction immediate data.\r | |
132 | \r | |
133 | @param Instruction Pointer to ARM MOVT or MOVW immediate instruction\r | |
fb0b35e0 | 134 | @param Address New address to patch into the instruction\r |
40d841f6 LG |
135 | **/\r |
136 | VOID\r | |
137 | ThumbMovtImmediatePatch (\r | |
138 | IN OUT UINT16 *Instruction,\r | |
139 | IN UINT16 Address\r | |
140 | )\r | |
141 | {\r | |
142 | UINT16 Patch;\r | |
143 | \r | |
fb0b35e0 | 144 | // First 16-bit chunk of instruction\r |
f7496d71 | 145 | Patch = ((Address >> 12) & 0x000f); // imm4\r |
40d841f6 LG |
146 | Patch |= (((Address & BIT11) != 0) ? BIT10 : 0); // i\r |
147 | *Instruction = (*Instruction & ~0x040f) | Patch;\r | |
148 | \r | |
149 | // Second 16-bit chunk of instruction\r | |
150 | Patch = Address & 0x000000ff; // imm8\r | |
151 | Patch |= ((Address << 4) & 0x00007000); // imm3\r | |
152 | Instruction++;\r | |
153 | *Instruction = (*Instruction & ~0x70ff) | Patch;\r | |
154 | }\r | |
155 | \r | |
da92f276 | 156 | /**\r |
fb0b35e0 | 157 | Pass in a pointer to an ARM MOVW/MOVT instruction pair and\r |
da92f276 LG |
158 | return the immediate data encoded in the two` instruction\r |
159 | \r | |
fb0b35e0 | 160 | @param Instructions Pointer to ARM MOVW/MOVT instruction pair\r |
da92f276 LG |
161 | \r |
162 | @return Immediate address encoded in the instructions\r | |
163 | \r | |
164 | **/\r | |
165 | UINT32\r | |
166 | EFIAPI\r | |
167 | ThumbMovwMovtImmediateAddress (\r | |
168 | IN UINT16 *Instructions\r | |
169 | )\r | |
170 | {\r | |
171 | UINT16 *Word;\r | |
172 | UINT16 *Top;\r | |
f7496d71 | 173 | \r |
da92f276 LG |
174 | Word = Instructions; // MOVW\r |
175 | Top = Word + 2; // MOVT\r | |
f7496d71 | 176 | \r |
da92f276 LG |
177 | return (ThumbMovtImmediateAddress (Top) << 16) + ThumbMovtImmediateAddress (Word);\r |
178 | }\r | |
179 | \r | |
180 | \r | |
181 | /**\r | |
182 | Update an ARM MOVW/MOVT immediate instruction instruction pair.\r | |
183 | \r | |
184 | @param Instructions Pointer to ARM MOVW/MOVT instruction pair\r | |
fb0b35e0 | 185 | @param Address New address to patch into the instructions\r |
da92f276 LG |
186 | **/\r |
187 | VOID\r | |
188 | EFIAPI\r | |
189 | ThumbMovwMovtImmediatePatch (\r | |
190 | IN OUT UINT16 *Instructions,\r | |
191 | IN UINT32 Address\r | |
192 | )\r | |
193 | {\r | |
194 | UINT16 *Word;\r | |
195 | UINT16 *Top;\r | |
f7496d71 | 196 | \r |
da92f276 LG |
197 | Word = (UINT16 *)Instructions; // MOVW\r |
198 | Top = Word + 2; // MOVT\r | |
199 | \r | |
200 | ThumbMovtImmediatePatch (Word, (UINT16)(Address & 0xffff));\r | |
201 | ThumbMovtImmediatePatch (Top, (UINT16)(Address >> 16));\r | |
202 | }\r | |
203 | \r | |
204 | \r | |
40d841f6 LG |
205 | /**\r |
206 | Performs an ARM-based specific relocation fixup and is a no-op on other\r | |
207 | instruction sets.\r | |
208 | \r | |
209 | @param Reloc Pointer to the relocation record.\r | |
210 | @param Fixup Pointer to the address to fix up.\r | |
211 | @param FixupData Pointer to a buffer to log the fixups.\r | |
212 | @param Adjust The offset to adjust the fixup.\r | |
213 | \r | |
214 | @return Status code.\r | |
215 | \r | |
216 | **/\r | |
217 | RETURN_STATUS\r | |
218 | PeCoffLoaderRelocateArmImage (\r | |
219 | IN UINT16 **Reloc,\r | |
220 | IN OUT CHAR8 *Fixup,\r | |
221 | IN OUT CHAR8 **FixupData,\r | |
222 | IN UINT64 Adjust\r | |
223 | )\r | |
224 | {\r | |
225 | UINT16 *Fixup16;\r | |
da92f276 | 226 | UINT32 FixupVal;\r |
40d841f6 | 227 | \r |
da92f276 | 228 | Fixup16 = (UINT16 *) Fixup;\r |
40d841f6 LG |
229 | \r |
230 | switch ((**Reloc) >> 12) {\r | |
f7496d71 | 231 | \r |
da92f276 LG |
232 | case EFI_IMAGE_REL_BASED_ARM_MOV32T:\r |
233 | FixupVal = ThumbMovwMovtImmediateAddress (Fixup16) + (UINT32)Adjust;\r | |
234 | ThumbMovwMovtImmediatePatch (Fixup16, FixupVal);\r | |
f7496d71 LG |
235 | \r |
236 | \r | |
40d841f6 | 237 | if (*FixupData != NULL) {\r |
da92f276 | 238 | *FixupData = ALIGN_POINTER(*FixupData, sizeof(UINT64));\r |
da92f276 | 239 | CopyMem (*FixupData, Fixup16, sizeof (UINT64));\r |
94762dde | 240 | *FixupData = *FixupData + sizeof(UINT64);\r |
40d841f6 LG |
241 | }\r |
242 | break;\r | |
f7496d71 | 243 | \r |
da92f276 LG |
244 | case EFI_IMAGE_REL_BASED_ARM_MOV32A:\r |
245 | // break omitted - ARM instruction encoding not implemented\r | |
40d841f6 LG |
246 | default:\r |
247 | return RETURN_UNSUPPORTED;\r | |
248 | }\r | |
249 | \r | |
250 | return RETURN_SUCCESS;\r | |
251 | }\r |