4 Copyright (c) 2006 - 2007, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 // The package level header files this module uses
20 // The protocols, PPI and GUID defintions for this module
23 // The Library classes this module consumes
25 #include <Library/PeCoffGetEntryPointLib.h>
26 #include <Library/DebugLib.h>
28 #include <IndustryStandard/PeImage.h>
31 Retrieves and returns a pointer to the entry point to a PE/COFF image that has been loaded
32 into system memory with the PE/COFF Loader Library functions.
34 Retrieves the entry point to the PE/COFF image specified by Pe32Data and returns this entry
35 point in EntryPoint. If the entry point could not be retrieved from the PE/COFF image, then
36 return RETURN_INVALID_PARAMETER. Otherwise return RETURN_SUCCESS.
37 If Pe32Data is NULL, then ASSERT().
38 If EntryPoint is NULL, then ASSERT().
40 @param Pe32Data Pointer to the PE/COFF image that is loaded in system memory.
41 @param EntryPoint Pointer to entry point to the PE/COFF image to return.
43 @retval RETURN_SUCCESS EntryPoint was returned.
44 @retval RETURN_INVALID_PARAMETER The entry point could not be found in the PE/COFF image.
49 PeCoffLoaderGetEntryPoint (
54 EFI_IMAGE_DOS_HEADER
*DosHeader
;
55 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Header
;
57 ASSERT (Pe32Data
!= NULL
);
58 ASSERT (EntryPoint
!= NULL
);
60 DosHeader
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
61 if (DosHeader
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
63 // DOS image header is present, so read the PE header after the DOS image header.
65 Header
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHeader
->e_lfanew
) & 0x0ffff));
68 // DOS image header is not present, so PE header is at the image base.
70 Header
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
74 // Calculate the entry point relative to the start of the image.
75 // AddressOfEntryPoint is common for PE32 & PE32+
77 *EntryPoint
= (VOID
*)((UINTN
)Pe32Data
+ (UINTN
)(Header
.Pe32
->OptionalHeader
.AddressOfEntryPoint
& 0x0ffffffff));
78 return RETURN_SUCCESS
;
83 Returns the machine type of a PE/COFF image.
85 Returns the machine type from the PE/COFF image specified by Pe32Data.
86 If Pe32Data is NULL, then ASSERT().
88 @param Pe32Data Pointer to the PE/COFF image that is loaded in system
91 @return Machine type or zero if not a valid iamge.
96 PeCoffLoaderGetMachineType (
100 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
101 EFI_IMAGE_DOS_HEADER
*DosHdr
;
103 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
104 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
105 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)Pe32Data
+ DosHdr
->e_lfanew
);
107 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)Pe32Data
);
110 if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
111 return Hdr
.Pe32
->FileHeader
.Machine
;
118 Returns a pointer to the PDB file name for a PE/COFF image that has been
119 loaded into system memory with the PE/COFF Loader Library functions.
121 Returns the PDB file name for the PE/COFF image specified by Pe32Data. If
122 the PE/COFF image specified by Pe32Data is not a valid, then NULL is
123 returned. If the PE/COFF image specified by Pe32Data does not contain a
124 debug directory entry, then NULL is returned. If the debug directory entry
125 in the PE/COFF image specified by Pe32Data does not contain a PDB file name,
126 then NULL is returned.
127 If Pe32Data is NULL, then ASSERT().
129 @param Pe32Data Pointer to the PE/COFF image that is loaded in system
132 @return The PDB file name for the PE/COFF image specified by Pe32Data or NULL
133 if it cannot be retrieved.
138 PeCoffLoaderGetPdbPointer (
142 EFI_IMAGE_DOS_HEADER
*DosHeader
;
143 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
144 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
145 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
147 VOID
*CodeViewEntryPointer
;
149 UINT32 NumberOfRvaAndSizes
;
152 ASSERT (Pe32Data
!= NULL
);
155 DirectoryEntry
= NULL
;
157 NumberOfRvaAndSizes
= 0;
159 DosHeader
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
160 if (DosHeader
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
162 // DOS image header is present, so read the PE header after the DOS image header.
164 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHeader
->e_lfanew
) & 0x0ffff));
167 // DOS image header is not present, so PE header is at the image base.
169 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
172 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
173 if (Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
!= 0) {
174 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
];
175 TEImageAdjust
= sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
;
176 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)((UINTN
) Hdr
.Te
+
177 Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
+
180 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
182 // NOTE: We use Machine field to identify PE32/PE32+, instead of Magic.
183 // It is due to backward-compatibility, for some system might
184 // generate PE32+ image with PE32 Magic.
186 switch (Hdr
.Pe32
->FileHeader
.Machine
) {
187 case EFI_IMAGE_MACHINE_IA32
:
189 // Assume PE32 image with IA32 Machine field.
191 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
193 case EFI_IMAGE_MACHINE_X64
:
194 case EFI_IMAGE_MACHINE_IPF
:
196 // Assume PE32+ image with X64 or IPF Machine field
198 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
202 // For unknow Machine field, use Magic in optional Header
204 Magic
= Hdr
.Pe32
->OptionalHeader
.Magic
;
207 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
209 // Use PE32 offset get Debug Directory Entry
211 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
212 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
213 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*) ((UINTN
) Pe32Data
+ DirectoryEntry
->VirtualAddress
);
214 } else if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
216 // Use PE32+ offset get Debug Directory Entry
218 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
219 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
220 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*) ((UINTN
) Pe32Data
+ DirectoryEntry
->VirtualAddress
);
223 if (NumberOfRvaAndSizes
<= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
224 DirectoryEntry
= NULL
;
231 if (DebugEntry
== NULL
|| DirectoryEntry
== NULL
) {
235 for (DirCount
= 0; DirCount
< DirectoryEntry
->Size
; DirCount
++, DebugEntry
++) {
236 if (DebugEntry
->Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
237 if (DebugEntry
->SizeOfData
> 0) {
238 CodeViewEntryPointer
= (VOID
*) ((UINTN
) DebugEntry
->RVA
+ ((UINTN
)Pe32Data
) + (UINTN
)TEImageAdjust
);
239 switch (* (UINT32
*) CodeViewEntryPointer
) {
240 case CODEVIEW_SIGNATURE_NB10
:
241 return (VOID
*) ((CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
));
242 case CODEVIEW_SIGNATURE_RSDS
:
243 return (VOID
*) ((CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
));