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