]> git.proxmox.com Git - mirror_edk2.git/blob - UnixPkg/Library/EdkUnixPeiPeCoffGetEntryPointLib/PeCoffGetEntryPoint.c
be9cba831c93c4e6c991efb217f01d4fd74e86c6
[mirror_edk2.git] / UnixPkg / Library / EdkUnixPeiPeCoffGetEntryPointLib / PeCoffGetEntryPoint.c
1 /*++
2
3 Copyright (c) 2006 - 2008, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 PeCoffGetEntryPoint.c
15
16 Abstract:
17
18 Tiano PE/COFF loader
19
20 Revision History
21
22 --*/
23 #include "PiPei.h"
24 #include <Library/PeCoffGetEntryPointLib.h>
25 #include <Library/PeiServicesLib.h>
26 #include <Ppi/UnixPeiLoadFile.h>
27 #include <IndustryStandard/PeImage.h>
28 #include <Library/DebugLib.h>
29
30 RETURN_STATUS
31 EFIAPI
32 PeCoffLoaderGetEntryPoint (
33 IN VOID *Pe32Data,
34 IN OUT VOID **EntryPoint
35 )
36 /*++
37
38 Routine Description:
39
40 Loads a PE/COFF image into memory, this is not follow the original purpose of
41 PeCoffGetEntryPoint library class. But it's ok that Unix package not run on a real
42 platform and this is for source level debug.
43
44 Arguments:
45
46 Pe32Data - Pointer to a PE/COFF Image
47
48 EntryPoint - Pointer to the entry point of the PE/COFF image
49
50 Returns:
51
52 EFI_SUCCESS if the EntryPoint was returned
53 EFI_INVALID_PARAMETER if the EntryPoint could not be found from Pe32Data
54
55 --*/
56 {
57 EFI_STATUS Status;
58 EFI_PEI_PPI_DESCRIPTOR *PpiDescriptor;
59 UNIX_PEI_LOAD_FILE_PPI *PeiUnixService;
60 EFI_PHYSICAL_ADDRESS ImageAddress;
61 UINT64 ImageSize;
62 EFI_PHYSICAL_ADDRESS ImageEntryPoint;
63
64 ASSERT (Pe32Data != NULL);
65 ASSERT (EntryPoint != NULL);
66
67 Status = PeiServicesLocatePpi (
68 &gUnixPeiLoadFilePpiGuid,
69 0,
70 &PpiDescriptor,
71 (void **)&PeiUnixService
72 );
73
74 ASSERT_EFI_ERROR (Status);
75
76 Status = PeiUnixService->PeiLoadFileService (
77 Pe32Data,
78 &ImageAddress,
79 &ImageSize,
80 &ImageEntryPoint
81 );
82
83 if (EFI_ERROR (Status)) {
84 return Status;
85 }
86
87 *EntryPoint = (VOID*)(UINTN)ImageEntryPoint;
88 return Status;
89 }
90
91 /**
92 Returns the machine type of PE/COFF image.
93 This is copied from MDE BasePeCoffGetEntryPointLib, the code should be sync with it.
94 The reason is Unix package needs to load the image to memory to support source
95 level debug.
96
97
98 @param Pe32Data Pointer to a PE/COFF header
99
100 @return Machine type or zero if not a valid iamge
101
102 **/
103 UINT16
104 EFIAPI
105 PeCoffLoaderGetMachineType (
106 IN VOID *Pe32Data
107 )
108 {
109 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
110 EFI_IMAGE_DOS_HEADER *DosHdr;
111
112 ASSERT (Pe32Data != NULL);
113
114 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
115 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
116 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));
117
118 } else {
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 EFI_IMAGE_MACHINE_IA32:
202 //
203 // Assume PE32 image with IA32 Machine field.
204 //
205 Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;
206 break;
207 case EFI_IMAGE_MACHINE_X64:
208 case EFI_IMAGE_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 default:
259 break;
260 }
261 }
262 }
263 }
264
265 return NULL;
266 }