]> git.proxmox.com Git - mirror_edk2.git/blame - Nt32Pkg/Library/Nt32PeiPeCoffGetEntryPointLib/PeCoffGetEntryPoint.c
Nt32Pkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / Nt32Pkg / Library / Nt32PeiPeCoffGetEntryPointLib / PeCoffGetEntryPoint.c
CommitLineData
b06f85ae 1/**@file\r
2\r
f66a43b2 3Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
9d2eedba 4SPDX-License-Identifier: BSD-2-Clause-Patent\r
b06f85ae 5\r
6Module Name:\r
7\r
8 PeCoffGetEntryPoint.c\r
9\r
10Abstract:\r
11\r
12 Tiano PE/COFF loader\r
13\r
14Revision History\r
15\r
16**/\r
17\r
18#include <PiPei.h>\r
19#include <IndustryStandard/PeImage.h>\r
20#include <WinNtPeim.h>\r
21#include <Ppi/NtPeiLoadFile.h>\r
22#include <Library/PeCoffGetEntryPointLib.h>\r
23#include <Library/PeiServicesLib.h>\r
24#include <Library/DebugLib.h>\r
25\r
26\r
27RETURN_STATUS\r
28EFIAPI\r
29PeCoffLoaderGetEntryPoint (\r
30 IN VOID *Pe32Data,\r
31 IN OUT VOID **EntryPoint\r
32 )\r
33/*++\r
34\r
35Routine Description:\r
36\r
37 Loads a PE/COFF image into memory, this is not follow the original purpose of \r
38 PeCoffGetEntryPoint library class. But it's ok that Unix package not run on a real \r
39 platform and this is for source level debug.\r
40\r
41Arguments:\r
42\r
43 Pe32Data - Pointer to a PE/COFF Image\r
44\r
45 EntryPoint - Pointer to the entry point of the PE/COFF image\r
46\r
47Returns:\r
48\r
49 EFI_SUCCESS if the EntryPoint was returned\r
50 EFI_INVALID_PARAMETER if the EntryPoint could not be found from Pe32Data\r
51\r
52--*/\r
53{\r
54 EFI_STATUS Status;\r
55 EFI_PEI_PPI_DESCRIPTOR *PpiDescriptor;\r
56 NT_PEI_LOAD_FILE_PPI *PeiNtService;\r
57 EFI_PHYSICAL_ADDRESS ImageAddress;\r
58 UINT64 ImageSize;\r
59 EFI_PHYSICAL_ADDRESS ImageEntryPoint;\r
60\r
61 ASSERT (Pe32Data != NULL);\r
62 ASSERT (EntryPoint != NULL);\r
63\r
64 Status = PeiServicesLocatePpi (\r
65 &gNtPeiLoadFilePpiGuid,\r
66 0,\r
67 &PpiDescriptor,\r
68 (VOID**)&PeiNtService\r
69 );\r
70 ASSERT_EFI_ERROR (Status);\r
71\r
72 Status = PeiNtService->PeiLoadFileService (\r
73 Pe32Data,\r
74 &ImageAddress,\r
75 &ImageSize,\r
76 &ImageEntryPoint\r
77 );\r
78 if (EFI_ERROR (Status)) {\r
79 return Status;\r
80 }\r
81\r
82 *EntryPoint = (VOID*)(UINTN)ImageEntryPoint;\r
83 return Status;\r
84}\r
85\r
86/**\r
87 Returns the machine type of PE/COFF image. \r
88 This is copied from MDE BasePeCoffGetEntryPointLib, the code should be sync with it.\r
89 The reason is NT32 package needs to load the image to memory to support source\r
90 level debug.\r
91 \r
92\r
93 @param Pe32Data Pointer to a PE/COFF header\r
94\r
95 @return Machine type or zero if not a valid iamge\r
96\r
97**/\r
98UINT16\r
99EFIAPI\r
100PeCoffLoaderGetMachineType (\r
101 IN VOID *Pe32Data\r
102 )\r
103{ \r
104 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
105 EFI_IMAGE_DOS_HEADER *DosHdr;\r
106\r
107 ASSERT (Pe32Data != NULL);\r
108\r
109 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
110 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
111 //\r
112 // DOS image header is present, so read the PE header after the DOS image header.\r
113 //\r
114 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r
115 } else {\r
116 //\r
117 // DOS image header is not present, so PE header is at the image base.\r
118 //\r
119 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
120 }\r
121\r
122 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
123 return Hdr.Te->Machine;\r
124 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
125 return Hdr.Pe32->FileHeader.Machine;\r
126 }\r
127\r
128 return 0x0000;\r
129}\r
130\r
131/**\r
132 Returns a pointer to the PDB file name for a PE/COFF image that has been\r
133 loaded into system memory with the PE/COFF Loader Library functions.\r
134\r
135 Returns the PDB file name for the PE/COFF image specified by Pe32Data. If\r
136 the PE/COFF image specified by Pe32Data is not a valid, then NULL is\r
137 returned. If the PE/COFF image specified by Pe32Data does not contain a\r
138 debug directory entry, then NULL is returned. If the debug directory entry\r
139 in the PE/COFF image specified by Pe32Data does not contain a PDB file name,\r
140 then NULL is returned.\r
141 If Pe32Data is NULL, then ASSERT().\r
142\r
143 @param Pe32Data Pointer to the PE/COFF image that is loaded in system\r
144 memory.\r
145\r
146 @return The PDB file name for the PE/COFF image specified by Pe32Data or NULL\r
147 if it cannot be retrieved.\r
148\r
149**/\r
150VOID *\r
151EFIAPI\r
152PeCoffLoaderGetPdbPointer (\r
153 IN VOID *Pe32Data\r
154 )\r
155{\r
156 EFI_IMAGE_DOS_HEADER *DosHdr;\r
157 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
158 EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;\r
159 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry;\r
160 UINTN DirCount;\r
161 VOID *CodeViewEntryPointer;\r
162 INTN TEImageAdjust;\r
163 UINT32 NumberOfRvaAndSizes;\r
164 UINT16 Magic;\r
165\r
166 ASSERT (Pe32Data != NULL);\r
167\r
168 TEImageAdjust = 0;\r
169 DirectoryEntry = NULL;\r
170 DebugEntry = NULL;\r
171 NumberOfRvaAndSizes = 0;\r
172\r
173 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
174 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
175 //\r
176 // DOS image header is present, so read the PE header after the DOS image header.\r
177 //\r
178 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r
179 } else {\r
180 //\r
181 // DOS image header is not present, so PE header is at the image base.\r
182 //\r
183 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
184 }\r
185\r
186 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
187 if (Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress != 0) {\r
188 DirectoryEntry = &Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG];\r
189 TEImageAdjust = sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize;\r
190 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)((UINTN) Hdr.Te +\r
191 Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress +\r
192 TEImageAdjust);\r
193 }\r
194 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
195 //\r
196 // NOTE: We use Machine field to identify PE32/PE32+, instead of Magic.\r
197 // It is due to backward-compatibility, for some system might\r
198 // generate PE32+ image with PE32 Magic.\r
199 //\r
200 switch (Hdr.Pe32->FileHeader.Machine) {\r
201 case IMAGE_FILE_MACHINE_I386:\r
202 //\r
203 // Assume PE32 image with IA32 Machine field.\r
204 //\r
205 Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;\r
206 break;\r
207 case IMAGE_FILE_MACHINE_X64:\r
208 case IMAGE_FILE_MACHINE_IA64:\r
209 //\r
210 // Assume PE32+ image with X64 or IA64 Machine field\r
211 //\r
212 Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
213 break;\r
214 default:\r
215 //\r
216 // For unknow Machine field, use Magic in optional Header\r
217 //\r
218 Magic = Hdr.Pe32->OptionalHeader.Magic;\r
219 }\r
220\r
221 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
222 //\r
223 // Use PE32 offset get Debug Directory Entry\r
224 //\r
225 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
226 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r
227 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) ((UINTN) Pe32Data + DirectoryEntry->VirtualAddress);\r
228 } else if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r
229 //\r
230 // Use PE32+ offset get Debug Directory Entry\r
231 //\r
232 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
233 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r
234 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) ((UINTN) Pe32Data + DirectoryEntry->VirtualAddress);\r
235 }\r
236\r
237 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {\r
238 DirectoryEntry = NULL;\r
239 DebugEntry = NULL;\r
240 }\r
241 } else {\r
242 return NULL;\r
243 }\r
244\r
245 if (DebugEntry == NULL || DirectoryEntry == NULL) {\r
246 return NULL;\r
247 }\r
248\r
249 for (DirCount = 0; DirCount < DirectoryEntry->Size; DirCount += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY), DebugEntry++) {\r
250 if (DebugEntry->Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {\r
251 if (DebugEntry->SizeOfData > 0) {\r
252 CodeViewEntryPointer = (VOID *) ((UINTN) DebugEntry->RVA + ((UINTN)Pe32Data) + (UINTN)TEImageAdjust);\r
253 switch (* (UINT32 *) CodeViewEntryPointer) {\r
254 case CODEVIEW_SIGNATURE_NB10:\r
255 return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY));\r
256 case CODEVIEW_SIGNATURE_RSDS:\r
257 return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY));\r
258 case CODEVIEW_SIGNATURE_MTOC: \r
259 return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY));\r
260 break;\r
261 default:\r
262 break;\r
263 }\r
264 }\r
265 }\r
266 }\r
267\r
268 return NULL;\r
269}\r
225290eb
A
270\r
271/**\r
272 Returns the size of the PE/COFF headers\r
273\r
274 Returns the size of the PE/COFF header specified by Pe32Data.\r
275 If Pe32Data is NULL, then ASSERT().\r
276\r
277 @param Pe32Data Pointer to the PE/COFF image that is loaded in system\r
278 memory.\r
279\r
a3deae0d 280 @return Size of PE/COFF header in bytes or zero if not a valid image.\r
225290eb
A
281\r
282**/\r
a3deae0d 283UINT32\r
284EFIAPI\r
285PeCoffGetSizeOfHeaders (\r
286 IN VOID *Pe32Data\r
287 )\r
288{\r
289 EFI_IMAGE_DOS_HEADER *DosHdr;\r
290 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
291 UINTN SizeOfHeaders;\r
292\r
225290eb 293 ASSERT (Pe32Data != NULL);\r
a3deae0d 294 \r
295 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
296 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
297 //\r
298 // DOS image header is present, so read the PE header after the DOS image header.\r
299 //\r
300 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r
301 } else {\r
302 //\r
303 // DOS image header is not present, so PE header is at the image base.\r
304 //\r
305 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
306 }\r
307\r
308 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
309 SizeOfHeaders = sizeof (EFI_TE_IMAGE_HEADER) + (UINTN)Hdr.Te->BaseOfCode - (UINTN)Hdr.Te->StrippedSize;\r
310 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
311 SizeOfHeaders = Hdr.Pe32->OptionalHeader.SizeOfHeaders;\r
312 } else {\r
313 SizeOfHeaders = 0;\r
314 }\r
315\r
f66a43b2 316 return (UINT32) SizeOfHeaders;\r
225290eb
A
317}\r
318\r