UefiCpuPkg/MpLib.c: Set AP state after X2APIC mode enabled
[mirror_edk2.git] / MdePkg / Library / BasePeCoffGetEntryPointLib / PeCoffGetEntryPoint.c
1 /** @file
2 Provides the services to get the entry point to a PE/COFF image that has either been
3 loaded into memory or is executing at it's linked address.
4
5 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
6 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17
18 #include <Base.h>
19
20 #include <Library/PeCoffGetEntryPointLib.h>
21 #include <Library/DebugLib.h>
22
23 #include <IndustryStandard/PeImage.h>
24
25 #define PE_COFF_IMAGE_ALIGN_SIZE 4
26
27 /**
28 Retrieves and returns a pointer to the entry point to a PE/COFF image that has been loaded
29 into system memory with the PE/COFF Loader Library functions.
30
31 Retrieves the entry point to the PE/COFF image specified by Pe32Data and returns this entry
32 point in EntryPoint. If the entry point could not be retrieved from the PE/COFF image, then
33 return RETURN_INVALID_PARAMETER. Otherwise return RETURN_SUCCESS.
34 If Pe32Data is NULL, then ASSERT().
35 If EntryPoint is NULL, then ASSERT().
36
37 @param Pe32Data The pointer to the PE/COFF image that is loaded in system memory.
38 @param EntryPoint The pointer to entry point to the PE/COFF image to return.
39
40 @retval RETURN_SUCCESS EntryPoint was returned.
41 @retval RETURN_INVALID_PARAMETER The entry point could not be found in the PE/COFF image.
42
43 **/
44 RETURN_STATUS
45 EFIAPI
46 PeCoffLoaderGetEntryPoint (
47 IN VOID *Pe32Data,
48 OUT VOID **EntryPoint
49 )
50 {
51 EFI_IMAGE_DOS_HEADER *DosHdr;
52 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
53
54 ASSERT (Pe32Data != NULL);
55 ASSERT (EntryPoint != NULL);
56
57 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
58 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
59 //
60 // DOS image header is present, so read the PE header after the DOS image header.
61 //
62 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));
63 } else {
64 //
65 // DOS image header is not present, so PE header is at the image base.
66 //
67 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
68 }
69
70 //
71 // Calculate the entry point relative to the start of the image.
72 // AddressOfEntryPoint is common for PE32 & PE32+
73 //
74 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
75 *EntryPoint = (VOID *)((UINTN)Pe32Data + (UINTN)(Hdr.Te->AddressOfEntryPoint & 0x0ffffffff) + sizeof(EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize);
76 return RETURN_SUCCESS;
77 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
78 *EntryPoint = (VOID *)((UINTN)Pe32Data + (UINTN)(Hdr.Pe32->OptionalHeader.AddressOfEntryPoint & 0x0ffffffff));
79 return RETURN_SUCCESS;
80 }
81
82 return RETURN_UNSUPPORTED;
83 }
84
85
86 /**
87 Returns the machine type of a PE/COFF image.
88
89 Returns the machine type from the PE/COFF image specified by Pe32Data.
90 If Pe32Data is NULL, then ASSERT().
91
92 @param Pe32Data The pointer to the PE/COFF image that is loaded in system
93 memory.
94
95 @return Machine type or zero if not a valid image.
96
97 **/
98 UINT16
99 EFIAPI
100 PeCoffLoaderGetMachineType (
101 IN VOID *Pe32Data
102 )
103 {
104 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
105 EFI_IMAGE_DOS_HEADER *DosHdr;
106
107 ASSERT (Pe32Data != NULL);
108
109 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
110 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
111 //
112 // DOS image header is present, so read the PE header after the DOS image header.
113 //
114 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));
115 } else {
116 //
117 // DOS image header is not present, so PE header is at the image base.
118 //
119 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
120 }
121
122 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
123 return Hdr.Te->Machine;
124 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
125 return Hdr.Pe32->FileHeader.Machine;
126 }
127
128 return 0x0000;
129 }
130
131 /**
132 Returns a pointer to the PDB file name for a PE/COFF image that has been
133 loaded into system memory with the PE/COFF Loader Library functions.
134
135 Returns the PDB file name for the PE/COFF image specified by Pe32Data. If
136 the PE/COFF image specified by Pe32Data is not a valid, then NULL is
137 returned. If the PE/COFF image specified by Pe32Data does not contain a
138 debug directory entry, then NULL is returned. If the debug directory entry
139 in the PE/COFF image specified by Pe32Data does not contain a PDB file name,
140 then NULL is returned.
141 If Pe32Data is NULL, then ASSERT().
142
143 @param Pe32Data The pointer to the PE/COFF image that is loaded in system
144 memory.
145
146 @return The PDB file name for the PE/COFF image specified by Pe32Data or NULL
147 if it cannot be retrieved.
148
149 **/
150 VOID *
151 EFIAPI
152 PeCoffLoaderGetPdbPointer (
153 IN VOID *Pe32Data
154 )
155 {
156 EFI_IMAGE_DOS_HEADER *DosHdr;
157 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
158 EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;
159 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry;
160 UINTN DirCount;
161 VOID *CodeViewEntryPointer;
162 INTN TEImageAdjust;
163 UINT32 NumberOfRvaAndSizes;
164 UINT16 Magic;
165
166 ASSERT (Pe32Data != NULL);
167
168 TEImageAdjust = 0;
169 DirectoryEntry = NULL;
170 DebugEntry = NULL;
171 NumberOfRvaAndSizes = 0;
172
173 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
174 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
175 //
176 // DOS image header is present, so read the PE header after the DOS image header.
177 //
178 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));
179 } else {
180 //
181 // DOS image header is not present, so PE header is at the image base.
182 //
183 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
184 }
185
186 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
187 if (Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress != 0) {
188 DirectoryEntry = &Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG];
189 TEImageAdjust = sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize;
190 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)((UINTN) Hdr.Te +
191 Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress +
192 TEImageAdjust);
193 }
194 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
195 //
196 // NOTE: We use Machine field to identify PE32/PE32+, instead of Magic.
197 // It is due to backward-compatibility, for some system might
198 // generate PE32+ image with PE32 Magic.
199 //
200 switch (Hdr.Pe32->FileHeader.Machine) {
201 case IMAGE_FILE_MACHINE_I386:
202 //
203 // Assume PE32 image with IA32 Machine field.
204 //
205 Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;
206 break;
207 case IMAGE_FILE_MACHINE_X64:
208 case IMAGE_FILE_MACHINE_IA64:
209 //
210 // Assume PE32+ image with x64 or IA64 Machine field
211 //
212 Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
213 break;
214 default:
215 //
216 // For unknow Machine field, use Magic in optional Header
217 //
218 Magic = Hdr.Pe32->OptionalHeader.Magic;
219 }
220
221 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
222 //
223 // Use PE32 offset get Debug Directory Entry
224 //
225 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
226 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
227 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) ((UINTN) Pe32Data + DirectoryEntry->VirtualAddress);
228 } else if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
229 //
230 // Use PE32+ offset get Debug Directory Entry
231 //
232 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
233 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
234 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) ((UINTN) Pe32Data + DirectoryEntry->VirtualAddress);
235 }
236
237 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {
238 DirectoryEntry = NULL;
239 DebugEntry = NULL;
240 }
241 } else {
242 return NULL;
243 }
244
245 if (DebugEntry == NULL || DirectoryEntry == NULL) {
246 return NULL;
247 }
248
249 //
250 // Scan the directory to find the debug entry.
251 //
252 for (DirCount = 0; DirCount < DirectoryEntry->Size; DirCount += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY), DebugEntry++) {
253 if (DebugEntry->Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {
254 if (DebugEntry->SizeOfData > 0) {
255 CodeViewEntryPointer = (VOID *) ((UINTN) DebugEntry->RVA + ((UINTN)Pe32Data) + (UINTN)TEImageAdjust);
256 switch (* (UINT32 *) CodeViewEntryPointer) {
257 case CODEVIEW_SIGNATURE_NB10:
258 return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY));
259 case CODEVIEW_SIGNATURE_RSDS:
260 return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY));
261 case CODEVIEW_SIGNATURE_MTOC:
262 return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY));
263 default:
264 break;
265 }
266 }
267 }
268 }
269
270 return NULL;
271 }
272
273 /**
274 Returns the size of the PE/COFF headers
275
276 Returns the size of the PE/COFF header specified by Pe32Data.
277 If Pe32Data is NULL, then ASSERT().
278
279 @param Pe32Data The pointer to the PE/COFF image that is loaded in system
280 memory.
281
282 @return Size of PE/COFF header in bytes or zero if not a valid image.
283
284 **/
285 UINT32
286 EFIAPI
287 PeCoffGetSizeOfHeaders (
288 IN VOID *Pe32Data
289 )
290 {
291 EFI_IMAGE_DOS_HEADER *DosHdr;
292 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
293 UINTN SizeOfHeaders;
294
295 ASSERT (Pe32Data != NULL);
296
297 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
298 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
299 //
300 // DOS image header is present, so read the PE header after the DOS image header.
301 //
302 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));
303 } else {
304 //
305 // DOS image header is not present, so PE header is at the image base.
306 //
307 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
308 }
309
310 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
311 SizeOfHeaders = sizeof (EFI_TE_IMAGE_HEADER) + (UINTN)Hdr.Te->BaseOfCode - (UINTN)Hdr.Te->StrippedSize;
312 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
313 SizeOfHeaders = Hdr.Pe32->OptionalHeader.SizeOfHeaders;
314 } else {
315 SizeOfHeaders = 0;
316 }
317
318 return (UINT32) SizeOfHeaders;
319 }
320
321 /**
322 Returns PE/COFF image base is loaded in system memory where the input address is in.
323
324 On DEBUG build, searches the PE/COFF image base forward the input address and
325 returns it.
326
327 @param Address Address located in one PE/COFF image.
328
329 @retval 0 RELEASE build or cannot find the PE/COFF image base.
330 @retval others PE/COFF image base found.
331
332 **/
333 UINTN
334 EFIAPI
335 PeCoffSerachImageBase (
336 IN UINTN Address
337 )
338 {
339 UINTN Pe32Data;
340
341 Pe32Data = 0;
342
343 DEBUG_CODE (
344 EFI_IMAGE_DOS_HEADER *DosHdr;
345 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
346
347 //
348 // Find Image Base
349 //
350 Pe32Data = Address & ~(PE_COFF_IMAGE_ALIGN_SIZE - 1);
351 while (Pe32Data != 0) {
352 DosHdr = (EFI_IMAGE_DOS_HEADER *) Pe32Data;
353 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
354 //
355 // DOS image header is present, so read the PE header after the DOS image header.
356 //
357 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));
358 //
359 // Make sure PE header address does not overflow and is less than the initial address.
360 //
361 if (((UINTN)Hdr.Pe32 > Pe32Data) && ((UINTN)Hdr.Pe32 < Address)) {
362 if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
363 break;
364 }
365 }
366 } else {
367 //
368 // DOS image header is not present, TE header is at the image base.
369 //
370 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
371 if ((Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) &&
372 ((Hdr.Te->Machine == IMAGE_FILE_MACHINE_I386) || (Hdr.Te->Machine == IMAGE_FILE_MACHINE_IA64) ||
373 (Hdr.Te->Machine == IMAGE_FILE_MACHINE_EBC) || (Hdr.Te->Machine == IMAGE_FILE_MACHINE_X64) ||
374 (Hdr.Te->Machine == IMAGE_FILE_MACHINE_ARM64) || (Hdr.Te->Machine == IMAGE_FILE_MACHINE_ARMTHUMB_MIXED))
375 ) {
376 break;
377 }
378 }
379
380 //
381 // Not found the image base, check the previous aligned address
382 //
383 Pe32Data -= PE_COFF_IMAGE_ALIGN_SIZE;
384 }
385 );
386
387 return Pe32Data;
388 }