]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePeCoffGetEntryPointLib/PeCoffGetEntryPoint.c
29a623fd8c3a6a7d8d82f51cb9a2bae322bdbc72
[mirror_edk2.git] / MdePkg / Library / BasePeCoffGetEntryPointLib / PeCoffGetEntryPoint.c
1 /** @file
2 Tiano PE/COFF loader.
3
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
9
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.
12
13 **/
14
15 //
16 // The package level header files this module uses
17 //
18 #include <Base.h>
19 //
20 // The protocols, PPI and GUID defintions for this module
21 //
22 //
23 // The Library classes this module consumes
24 //
25 #include <Library/PeCoffGetEntryPointLib.h>
26 #include <Library/DebugLib.h>
27
28 #include <IndustryStandard/PeImage.h>
29
30 /**
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.
33
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().
39
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.
42
43 @retval RETURN_SUCCESS EntryPoint was returned.
44 @retval RETURN_INVALID_PARAMETER The entry point could not be found in the PE/COFF image.
45
46 **/
47 RETURN_STATUS
48 EFIAPI
49 PeCoffLoaderGetEntryPoint (
50 IN VOID *Pe32Data,
51 OUT VOID **EntryPoint
52 )
53 {
54 EFI_IMAGE_DOS_HEADER *DosHeader;
55 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Header;
56
57 ASSERT (Pe32Data != NULL);
58 ASSERT (EntryPoint != NULL);
59
60 DosHeader = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
61 if (DosHeader->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
62 //
63 // DOS image header is present, so read the PE header after the DOS image header.
64 //
65 Header.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHeader->e_lfanew) & 0x0ffff));
66 } else {
67 //
68 // DOS image header is not present, so PE header is at the image base.
69 //
70 Header.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
71 }
72
73 //
74 // Calculate the entry point relative to the start of the image.
75 // AddressOfEntryPoint is common for PE32 & PE32+
76 //
77 *EntryPoint = (VOID *)((UINTN)Pe32Data + (UINTN)(Header.Pe32->OptionalHeader.AddressOfEntryPoint & 0x0ffffffff));
78 return RETURN_SUCCESS;
79 }
80
81
82 /**
83 Returns the machine type of a PE/COFF image.
84
85 Returns the machine type from the PE/COFF image specified by Pe32Data.
86 If Pe32Data is NULL, then ASSERT().
87
88 @param Pe32Data Pointer to the PE/COFF image that is loaded in system
89 memory.
90
91 @return Machine type or zero if not a valid iamge.
92
93 **/
94 UINT16
95 EFIAPI
96 PeCoffLoaderGetMachineType (
97 IN VOID *Pe32Data
98 )
99 {
100 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
101 EFI_IMAGE_DOS_HEADER *DosHdr;
102
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);
106 } else {
107 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data);
108 }
109
110 if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
111 return Hdr.Pe32->FileHeader.Machine;
112 }
113
114 return 0x0000;
115 }
116
117 /**
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.
120
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().
128
129 @param Pe32Data Pointer to the PE/COFF image that is loaded in system
130 memory.
131
132 @return The PDB file name for the PE/COFF image specified by Pe32Data or NULL
133 if it cannot be retrieved.
134
135 **/
136 VOID *
137 EFIAPI
138 PeCoffLoaderGetPdbPointer (
139 IN VOID *Pe32Data
140 )
141 {
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;
146 UINTN DirCount;
147 VOID *CodeViewEntryPointer;
148 INTN TEImageAdjust;
149 UINT32 NumberOfRvaAndSizes;
150 UINT16 Magic;
151
152 ASSERT (Pe32Data != NULL);
153
154 TEImageAdjust = 0;
155 DirectoryEntry = NULL;
156 DebugEntry = NULL;
157 NumberOfRvaAndSizes = 0;
158
159 DosHeader = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
160 if (DosHeader->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
161 //
162 // DOS image header is present, so read the PE header after the DOS image header.
163 //
164 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHeader->e_lfanew) & 0x0ffff));
165 } else {
166 //
167 // DOS image header is not present, so PE header is at the image base.
168 //
169 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
170 }
171
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 +
178 TEImageAdjust);
179 }
180 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
181 //
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.
185 //
186 switch (Hdr.Pe32->FileHeader.Machine) {
187 case EFI_IMAGE_MACHINE_IA32:
188 //
189 // Assume PE32 image with IA32 Machine field.
190 //
191 Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;
192 break;
193 case EFI_IMAGE_MACHINE_X64:
194 case EFI_IMAGE_MACHINE_IPF:
195 //
196 // Assume PE32+ image with X64 or IPF Machine field
197 //
198 Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
199 break;
200 default:
201 //
202 // For unknow Machine field, use Magic in optional Header
203 //
204 Magic = Hdr.Pe32->OptionalHeader.Magic;
205 }
206
207 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
208 //
209 // Use PE32 offset get Debug Directory Entry
210 //
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) {
215 //
216 // Use PE32+ offset get Debug Directory Entry
217 //
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);
221 }
222
223 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {
224 DirectoryEntry = NULL;
225 DebugEntry = NULL;
226 }
227 } else {
228 return NULL;
229 }
230
231 if (DebugEntry == NULL || DirectoryEntry == NULL) {
232 return NULL;
233 }
234
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));
244 default:
245 break;
246 }
247 }
248 }
249 }
250
251 return NULL;
252 }
253
254