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