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
13 #include <Library/PeCoffGetEntryPointLib.h>
14 #include <Library/DebugLib.h>
16 #include <IndustryStandard/PeImage.h>
18 #define PE_COFF_IMAGE_ALIGN_SIZE 4
21 Retrieves and returns a pointer to the entry point to a PE/COFF image that has been loaded
22 into system memory with the PE/COFF Loader Library functions.
24 Retrieves the entry point to the PE/COFF image specified by Pe32Data and returns this entry
25 point in EntryPoint. If the entry point could not be retrieved from the PE/COFF image, then
26 return RETURN_INVALID_PARAMETER. Otherwise return RETURN_SUCCESS.
27 If Pe32Data is NULL, then ASSERT().
28 If EntryPoint is NULL, then ASSERT().
30 @param Pe32Data The pointer to the PE/COFF image that is loaded in system memory.
31 @param EntryPoint The pointer to entry point to the PE/COFF image to return.
33 @retval RETURN_SUCCESS EntryPoint was returned.
34 @retval RETURN_INVALID_PARAMETER The entry point could not be found in the PE/COFF image.
39 PeCoffLoaderGetEntryPoint (
44 EFI_IMAGE_DOS_HEADER
*DosHdr
;
45 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
47 ASSERT (Pe32Data
!= NULL
);
48 ASSERT (EntryPoint
!= NULL
);
50 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
51 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
53 // DOS image header is present, so read the PE header after the DOS image header.
55 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)Pe32Data
+ (UINTN
)((DosHdr
->e_lfanew
) & 0x0ffff));
58 // DOS image header is not present, so PE header is at the image base.
60 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
64 // Calculate the entry point relative to the start of the image.
65 // AddressOfEntryPoint is common for PE32 & PE32+
67 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
68 *EntryPoint
= (VOID
*)((UINTN
)Pe32Data
+ (UINTN
)(Hdr
.Te
->AddressOfEntryPoint
& 0x0ffffffff) + sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
);
69 return RETURN_SUCCESS
;
70 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
71 *EntryPoint
= (VOID
*)((UINTN
)Pe32Data
+ (UINTN
)(Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
& 0x0ffffffff));
72 return RETURN_SUCCESS
;
75 return RETURN_UNSUPPORTED
;
79 Returns the machine type of a PE/COFF image.
81 Returns the machine type from the PE/COFF image specified by Pe32Data.
82 If Pe32Data is NULL, then ASSERT().
84 @param Pe32Data The pointer to the PE/COFF image that is loaded in system
87 @return Machine type or zero if not a valid image.
92 PeCoffLoaderGetMachineType (
96 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
97 EFI_IMAGE_DOS_HEADER
*DosHdr
;
99 ASSERT (Pe32Data
!= NULL
);
101 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
102 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
104 // DOS image header is present, so read the PE header after the DOS image header.
106 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)Pe32Data
+ (UINTN
)((DosHdr
->e_lfanew
) & 0x0ffff));
109 // DOS image header is not present, so PE header is at the image base.
111 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
114 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
115 return Hdr
.Te
->Machine
;
116 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
117 return Hdr
.Pe32
->FileHeader
.Machine
;
124 Returns a pointer to the PDB file name for a PE/COFF image that has been
125 loaded into system memory with the PE/COFF Loader Library functions.
127 Returns the PDB file name for the PE/COFF image specified by Pe32Data. If
128 the PE/COFF image specified by Pe32Data is not a valid, then NULL is
129 returned. If the PE/COFF image specified by Pe32Data does not contain a
130 debug directory entry, then NULL is returned. If the debug directory entry
131 in the PE/COFF image specified by Pe32Data does not contain a PDB file name,
132 then NULL is returned.
133 If Pe32Data is NULL, then ASSERT().
135 @param Pe32Data The pointer to the PE/COFF image that is loaded in system
138 @return The PDB file name for the PE/COFF image specified by Pe32Data or NULL
139 if it cannot be retrieved.
144 PeCoffLoaderGetPdbPointer (
148 EFI_IMAGE_DOS_HEADER
*DosHdr
;
149 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
150 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
151 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
153 VOID
*CodeViewEntryPointer
;
155 UINT32 NumberOfRvaAndSizes
;
158 ASSERT (Pe32Data
!= NULL
);
161 DirectoryEntry
= NULL
;
163 NumberOfRvaAndSizes
= 0;
165 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
166 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
168 // DOS image header is present, so read the PE header after the DOS image header.
170 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)Pe32Data
+ (UINTN
)((DosHdr
->e_lfanew
) & 0x0ffff));
173 // DOS image header is not present, so PE header is at the image base.
175 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
178 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
179 if (Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
!= 0) {
180 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
];
181 TEImageAdjust
= sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
;
182 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)((UINTN
)Hdr
.Te
+
183 Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
+
186 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
188 // NOTE: We use Machine field to identify PE32/PE32+, instead of Magic.
189 // It is due to backward-compatibility, for some system might
190 // generate PE32+ image with PE32 Magic.
192 switch (Hdr
.Pe32
->FileHeader
.Machine
) {
193 case IMAGE_FILE_MACHINE_I386
:
195 // Assume PE32 image with IA32 Machine field.
197 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
199 case IMAGE_FILE_MACHINE_X64
:
200 case IMAGE_FILE_MACHINE_IA64
:
202 // Assume PE32+ image with x64 or IA64 Machine field
204 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
208 // For unknow Machine field, use Magic in optional Header
210 Magic
= Hdr
.Pe32
->OptionalHeader
.Magic
;
213 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
215 // Use PE32 offset get Debug Directory Entry
217 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
218 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
219 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)((UINTN
)Pe32Data
+ DirectoryEntry
->VirtualAddress
);
220 } else if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
222 // Use PE32+ offset get Debug Directory Entry
224 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
225 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
226 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)((UINTN
)Pe32Data
+ DirectoryEntry
->VirtualAddress
);
229 if (NumberOfRvaAndSizes
<= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
230 DirectoryEntry
= NULL
;
237 if ((DebugEntry
== NULL
) || (DirectoryEntry
== NULL
)) {
242 // Scan the directory to find the debug entry.
244 for (DirCount
= 0; DirCount
< DirectoryEntry
->Size
; DirCount
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
), DebugEntry
++) {
245 if (DebugEntry
->Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
246 if (DebugEntry
->SizeOfData
> 0) {
247 CodeViewEntryPointer
= (VOID
*)((UINTN
)DebugEntry
->RVA
+ ((UINTN
)Pe32Data
) + (UINTN
)TEImageAdjust
);
248 switch (*(UINT32
*)CodeViewEntryPointer
) {
249 case CODEVIEW_SIGNATURE_NB10
:
250 return (VOID
*)((CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
));
251 case CODEVIEW_SIGNATURE_RSDS
:
252 return (VOID
*)((CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
));
253 case CODEVIEW_SIGNATURE_MTOC
:
254 return (VOID
*)((CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY
));
266 Returns the size of the PE/COFF headers
268 Returns the size of the PE/COFF header specified by Pe32Data.
269 If Pe32Data is NULL, then ASSERT().
271 @param Pe32Data The pointer to the PE/COFF image that is loaded in system
274 @return Size of PE/COFF header in bytes or zero if not a valid image.
279 PeCoffGetSizeOfHeaders (
283 EFI_IMAGE_DOS_HEADER
*DosHdr
;
284 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
287 ASSERT (Pe32Data
!= NULL
);
289 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
290 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
292 // DOS image header is present, so read the PE header after the DOS image header.
294 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)Pe32Data
+ (UINTN
)((DosHdr
->e_lfanew
) & 0x0ffff));
297 // DOS image header is not present, so PE header is at the image base.
299 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
302 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
303 SizeOfHeaders
= sizeof (EFI_TE_IMAGE_HEADER
) + (UINTN
)Hdr
.Te
->BaseOfCode
- (UINTN
)Hdr
.Te
->StrippedSize
;
304 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
305 SizeOfHeaders
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
310 return (UINT32
)SizeOfHeaders
;
314 Returns PE/COFF image base is loaded in system memory where the input address is in.
316 On DEBUG build, searches the PE/COFF image base forward the input address and
319 @param Address Address located in one PE/COFF image.
321 @retval 0 RELEASE build or cannot find the PE/COFF image base.
322 @retval others PE/COFF image base found.
327 PeCoffSearchImageBase (
336 EFI_IMAGE_DOS_HEADER
*DosHdr
;
337 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
342 Pe32Data
= Address
& ~(PE_COFF_IMAGE_ALIGN_SIZE
- 1);
343 while (Pe32Data
!= 0) {
344 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
345 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
347 // DOS image header is present, so read the PE header after the DOS image header.
349 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(Pe32Data
+ (UINTN
)((DosHdr
->e_lfanew
) & 0x0ffff));
351 // Make sure PE header address does not overflow and is less than the initial address.
353 if (((UINTN
)Hdr
.Pe32
> Pe32Data
) && ((UINTN
)Hdr
.Pe32
< Address
)) {
354 if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
360 // DOS image header is not present, TE header is at the image base.
362 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
363 if ((Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) &&
364 ((Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_I386
) || (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_IA64
) ||
365 (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_EBC
) || (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_X64
) ||
366 (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_ARM64
) || (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_ARMTHUMB_MIXED
))
374 // Not found the image base, check the previous aligned address
376 Pe32Data
-= PE_COFF_IMAGE_ALIGN_SIZE
;