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.
5 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
6 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include <Library/PeCoffGetEntryPointLib.h>
15 #include <Library/DebugLib.h>
17 #include <IndustryStandard/PeImage.h>
19 #define PE_COFF_IMAGE_ALIGN_SIZE 4
22 Retrieves and returns a pointer to the entry point to a PE/COFF image that has been loaded
23 into system memory with the PE/COFF Loader Library functions.
25 Retrieves the entry point to the PE/COFF image specified by Pe32Data and returns this entry
26 point in EntryPoint. If the entry point could not be retrieved from the PE/COFF image, then
27 return RETURN_INVALID_PARAMETER. Otherwise return RETURN_SUCCESS.
28 If Pe32Data is NULL, then ASSERT().
29 If EntryPoint is NULL, then ASSERT().
31 @param Pe32Data The pointer to the PE/COFF image that is loaded in system memory.
32 @param EntryPoint The pointer to entry point to the PE/COFF image to return.
34 @retval RETURN_SUCCESS EntryPoint was returned.
35 @retval RETURN_INVALID_PARAMETER The entry point could not be found in the PE/COFF image.
40 PeCoffLoaderGetEntryPoint (
45 EFI_IMAGE_DOS_HEADER
*DosHdr
;
46 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
48 ASSERT (Pe32Data
!= NULL
);
49 ASSERT (EntryPoint
!= NULL
);
51 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
52 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
54 // DOS image header is present, so read the PE header after the DOS image header.
56 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
59 // DOS image header is not present, so PE header is at the image base.
61 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
65 // Calculate the entry point relative to the start of the image.
66 // AddressOfEntryPoint is common for PE32 & PE32+
68 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
69 *EntryPoint
= (VOID
*)((UINTN
)Pe32Data
+ (UINTN
)(Hdr
.Te
->AddressOfEntryPoint
& 0x0ffffffff) + sizeof(EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
);
70 return RETURN_SUCCESS
;
71 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
72 *EntryPoint
= (VOID
*)((UINTN
)Pe32Data
+ (UINTN
)(Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
& 0x0ffffffff));
73 return RETURN_SUCCESS
;
76 return RETURN_UNSUPPORTED
;
81 Returns the machine type of a PE/COFF image.
83 Returns the machine type from the PE/COFF image specified by Pe32Data.
84 If Pe32Data is NULL, then ASSERT().
86 @param Pe32Data The pointer to the PE/COFF image that is loaded in system
89 @return Machine type or zero if not a valid image.
94 PeCoffLoaderGetMachineType (
98 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
99 EFI_IMAGE_DOS_HEADER
*DosHdr
;
101 ASSERT (Pe32Data
!= NULL
);
103 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
104 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
106 // DOS image header is present, so read the PE header after the DOS image header.
108 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
111 // DOS image header is not present, so PE header is at the image base.
113 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
116 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
117 return Hdr
.Te
->Machine
;
118 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
119 return Hdr
.Pe32
->FileHeader
.Machine
;
126 Returns a pointer to the PDB file name for a PE/COFF image that has been
127 loaded into system memory with the PE/COFF Loader Library functions.
129 Returns the PDB file name for the PE/COFF image specified by Pe32Data. If
130 the PE/COFF image specified by Pe32Data is not a valid, then NULL is
131 returned. If the PE/COFF image specified by Pe32Data does not contain a
132 debug directory entry, then NULL is returned. If the debug directory entry
133 in the PE/COFF image specified by Pe32Data does not contain a PDB file name,
134 then NULL is returned.
135 If Pe32Data is NULL, then ASSERT().
137 @param Pe32Data The pointer to the PE/COFF image that is loaded in system
140 @return The PDB file name for the PE/COFF image specified by Pe32Data or NULL
141 if it cannot be retrieved.
146 PeCoffLoaderGetPdbPointer (
150 EFI_IMAGE_DOS_HEADER
*DosHdr
;
151 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
152 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
153 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
155 VOID
*CodeViewEntryPointer
;
157 UINT32 NumberOfRvaAndSizes
;
160 ASSERT (Pe32Data
!= NULL
);
163 DirectoryEntry
= NULL
;
165 NumberOfRvaAndSizes
= 0;
167 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
168 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
170 // DOS image header is present, so read the PE header after the DOS image header.
172 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
175 // DOS image header is not present, so PE header is at the image base.
177 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
180 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
181 if (Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
!= 0) {
182 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
];
183 TEImageAdjust
= sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
;
184 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)((UINTN
) Hdr
.Te
+
185 Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
+
188 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
190 // NOTE: We use Machine field to identify PE32/PE32+, instead of Magic.
191 // It is due to backward-compatibility, for some system might
192 // generate PE32+ image with PE32 Magic.
194 switch (Hdr
.Pe32
->FileHeader
.Machine
) {
195 case IMAGE_FILE_MACHINE_I386
:
197 // Assume PE32 image with IA32 Machine field.
199 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
201 case IMAGE_FILE_MACHINE_X64
:
202 case IMAGE_FILE_MACHINE_IA64
:
204 // Assume PE32+ image with x64 or IA64 Machine field
206 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
210 // For unknow Machine field, use Magic in optional Header
212 Magic
= Hdr
.Pe32
->OptionalHeader
.Magic
;
215 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
217 // Use PE32 offset get Debug Directory Entry
219 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
220 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
221 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*) ((UINTN
) Pe32Data
+ DirectoryEntry
->VirtualAddress
);
222 } else if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
224 // Use PE32+ offset get Debug Directory Entry
226 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
227 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
228 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*) ((UINTN
) Pe32Data
+ DirectoryEntry
->VirtualAddress
);
231 if (NumberOfRvaAndSizes
<= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
232 DirectoryEntry
= NULL
;
239 if (DebugEntry
== NULL
|| DirectoryEntry
== NULL
) {
244 // Scan the directory to find the debug entry.
246 for (DirCount
= 0; DirCount
< DirectoryEntry
->Size
; DirCount
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
), DebugEntry
++) {
247 if (DebugEntry
->Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
248 if (DebugEntry
->SizeOfData
> 0) {
249 CodeViewEntryPointer
= (VOID
*) ((UINTN
) DebugEntry
->RVA
+ ((UINTN
)Pe32Data
) + (UINTN
)TEImageAdjust
);
250 switch (* (UINT32
*) CodeViewEntryPointer
) {
251 case CODEVIEW_SIGNATURE_NB10
:
252 return (VOID
*) ((CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
));
253 case CODEVIEW_SIGNATURE_RSDS
:
254 return (VOID
*) ((CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
));
255 case CODEVIEW_SIGNATURE_MTOC
:
256 return (VOID
*) ((CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY
));
268 Returns the size of the PE/COFF headers
270 Returns the size of the PE/COFF header specified by Pe32Data.
271 If Pe32Data is NULL, then ASSERT().
273 @param Pe32Data The pointer to the PE/COFF image that is loaded in system
276 @return Size of PE/COFF header in bytes or zero if not a valid image.
281 PeCoffGetSizeOfHeaders (
285 EFI_IMAGE_DOS_HEADER
*DosHdr
;
286 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
289 ASSERT (Pe32Data
!= NULL
);
291 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
292 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
294 // DOS image header is present, so read the PE header after the DOS image header.
296 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
299 // DOS image header is not present, so PE header is at the image base.
301 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
304 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
305 SizeOfHeaders
= sizeof (EFI_TE_IMAGE_HEADER
) + (UINTN
)Hdr
.Te
->BaseOfCode
- (UINTN
)Hdr
.Te
->StrippedSize
;
306 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
307 SizeOfHeaders
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
312 return (UINT32
) SizeOfHeaders
;
316 Returns PE/COFF image base is loaded in system memory where the input address is in.
318 On DEBUG build, searches the PE/COFF image base forward the input address and
321 @param Address Address located in one PE/COFF image.
323 @retval 0 RELEASE build or cannot find the PE/COFF image base.
324 @retval others PE/COFF image base found.
329 PeCoffSearchImageBase (
338 EFI_IMAGE_DOS_HEADER
*DosHdr
;
339 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
344 Pe32Data
= Address
& ~(PE_COFF_IMAGE_ALIGN_SIZE
- 1);
345 while (Pe32Data
!= 0) {
346 DosHdr
= (EFI_IMAGE_DOS_HEADER
*) Pe32Data
;
347 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
349 // DOS image header is present, so read the PE header after the DOS image header.
351 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
353 // Make sure PE header address does not overflow and is less than the initial address.
355 if (((UINTN
)Hdr
.Pe32
> Pe32Data
) && ((UINTN
)Hdr
.Pe32
< Address
)) {
356 if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
362 // DOS image header is not present, TE header is at the image base.
364 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
365 if ((Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) &&
366 ((Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_I386
) || (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_IA64
) ||
367 (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_EBC
) || (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_X64
) ||
368 (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_ARM64
) || (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_ARMTHUMB_MIXED
))
375 // Not found the image base, check the previous aligned address
377 Pe32Data
-= PE_COFF_IMAGE_ALIGN_SIZE
;