]> git.proxmox.com Git - mirror_edk2.git/blame - Nt32Pkg/Library/DxeNt32PeCoffExtraActionLib/DxeNt32PeCoffExtraActionLib.c
Nt32Pkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / Nt32Pkg / Library / DxeNt32PeCoffExtraActionLib / DxeNt32PeCoffExtraActionLib.c
CommitLineData
ffdd18bb 1/**@file\r
2\r
8f2a5f80 3Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
9d2eedba 4SPDX-License-Identifier: BSD-2-Clause-Patent\r
ffdd18bb 5\r
6Module Name:\r
7\r
8 PeiNt32PeCoffExtraActionLib.c\r
9\r
10Abstract:\r
11\r
12 Provides services to perform additional actions to relocate and unload\r
13 PE/Coff image for NT32 environment specific purpose such as souce level debug.\r
14 This version only works for DXE phase \r
15\r
16\r
17**/\r
18//\r
19// The package level header files this module uses\r
20//\r
ffdd18bb 21#include <WinNtDxe.h>\r
22\r
23//\r
24// The protocols, PPI and GUID defintions for this module\r
25//\r
26#include <Protocol/WinNtThunk.h>\r
27\r
28#include <Library/PeCoffLib.h>\r
ffdd18bb 29\r
30#include <Library/BaseLib.h>\r
31#include <Library/DebugLib.h>\r
32#include <Library/HobLib.h>\r
33#include <Library/BaseMemoryLib.h>\r
34#include <Library/PeCoffExtraActionLib.h>\r
35\r
36#define MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE 0x100\r
37\r
38typedef struct {\r
39 CHAR8 *PdbPointer;\r
40 VOID *ModHandle;\r
41} PDB_NAME_TO_MOD_HANDLE;\r
42\r
43\r
44//\r
45// Cache of WinNtThunk protocol\r
46//\r
47EFI_WIN_NT_THUNK_PROTOCOL *mWinNt = NULL;\r
48\r
49//\r
50// An Array to hold the ModHandle\r
51//\r
52PDB_NAME_TO_MOD_HANDLE *mPdbNameModHandleArray = NULL;\r
53UINTN mPdbNameModHandleArraySize = 0;\r
54\r
55\r
56/**\r
57 The constructor function gets the pointer of the WinNT thunk functions\r
58 It will ASSERT() if NT thunk protocol is not installed.\r
59\r
60 @retval EFI_SUCCESS WinNT thunk protocol is found and cached.\r
61\r
62**/\r
63EFI_STATUS\r
64EFIAPI\r
1a2ae0f7
RN
65Nt32PeCoffGetWinNtThunkStucture (\r
66 VOID\r
ffdd18bb 67 )\r
68{\r
1a2ae0f7 69 EFI_HOB_GUID_TYPE *GuidHob;\r
ffdd18bb 70\r
71 //\r
72 // Retrieve WinNtThunkProtocol from GUID'ed HOB\r
73 //\r
74 GuidHob = GetFirstGuidHob (&gEfiWinNtThunkProtocolGuid);\r
75 ASSERT (GuidHob != NULL);\r
76 mWinNt = (EFI_WIN_NT_THUNK_PROTOCOL *)(*(UINTN *)(GET_GUID_HOB_DATA (GuidHob)));\r
77 ASSERT (mWinNt != NULL);\r
78\r
79\r
80 return EFI_SUCCESS;\r
81}\r
82\r
83/**\r
84 Convert the passed in Ascii string to Unicode.\r
85 \r
86 This function Convert the passed in Ascii string to Unicode.Optionally return\r
87 the length of the strings..\r
88\r
89 @param AsciiString Pointer to an AscII string\r
90 @param StrLen Length of string\r
91\r
92 @return Pointer to malloc'ed Unicode version of Ascii\r
93\r
94**/\r
95CHAR16 *\r
96AsciiToUnicode (\r
97 IN CHAR8 *Ascii,\r
98 IN UINTN *StrLen OPTIONAL\r
99 )\r
100{\r
101 UINTN Index;\r
102 CHAR16 *Unicode;\r
103\r
104 //\r
105 // Allocate a buffer for unicode string\r
106 //\r
107 for (Index = 0; Ascii[Index] != '\0'; Index++)\r
108 ;\r
109 Unicode = mWinNt->HeapAlloc ( mWinNt->GetProcessHeap (),\r
110 HEAP_ZERO_MEMORY,\r
111 ((Index + 1) * sizeof (CHAR16))\r
112 ); \r
113 if (Unicode == NULL) {\r
114 return NULL;\r
115 }\r
116\r
117 for (Index = 0; Ascii[Index] != '\0'; Index++) {\r
118 Unicode[Index] = (CHAR16) Ascii[Index];\r
119 }\r
120\r
121 Unicode[Index] = '\0';\r
122\r
123 if (StrLen != NULL) {\r
124 *StrLen = Index;\r
125 }\r
126\r
127 return Unicode;\r
128}\r
129/**\r
130 Store the ModHandle in an array indexed by the Pdb File name.\r
131 The ModHandle is needed to unload the image. \r
132\r
133\r
134 @param ImageContext - Input data returned from PE Laoder Library. Used to find the \r
135 .PDB file name of the PE Image.\r
136 @param ModHandle - Returned from LoadLibraryEx() and stored for call to \r
137 FreeLibrary().\r
138\r
139 @return return EFI_SUCCESS when ModHandle was stored. \r
140\r
141--*/\r
142EFI_STATUS\r
143AddModHandle (\r
144 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r
145 IN VOID *ModHandle\r
146 )\r
147\r
148{\r
149 UINTN Index;\r
150 PDB_NAME_TO_MOD_HANDLE *Array;\r
151 UINTN PreviousSize;\r
152 PDB_NAME_TO_MOD_HANDLE *TempArray;\r
a00ec39b 153 HANDLE Handle;\r
ffdd18bb 154\r
66352173 155 //\r
156 // Return EFI_ALREADY_STARTED if this DLL has already been loaded\r
157 //\r
158 Array = mPdbNameModHandleArray;\r
159 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
160 if (Array->PdbPointer != NULL && Array->ModHandle == ModHandle) {\r
161 return EFI_ALREADY_STARTED;\r
162 }\r
163 }\r
164 \r
ffdd18bb 165 Array = mPdbNameModHandleArray;\r
166 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
167 if (Array->PdbPointer == NULL) {\r
168 //\r
169 // Make a copy of the stirng and store the ModHandle\r
170 //\r
a00ec39b 171 Handle = mWinNt->GetProcessHeap ();\r
172 Array->PdbPointer = mWinNt->HeapAlloc ( Handle,\r
ffdd18bb 173 HEAP_ZERO_MEMORY,\r
174 AsciiStrLen (ImageContext->PdbPointer) + 1\r
175 ); \r
176 \r
177 ASSERT (Array->PdbPointer != NULL);\r
178\r
179 AsciiStrCpy (Array->PdbPointer, ImageContext->PdbPointer);\r
180 Array->ModHandle = ModHandle;\r
181 return EFI_SUCCESS;\r
182 }\r
183 }\r
184 \r
185 //\r
186 // No free space in mPdbNameModHandleArray so grow it by \r
187 // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires. \r
188 //\r
189 PreviousSize = mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE);\r
190 mPdbNameModHandleArraySize += MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE;\r
191 //\r
192 // re-allocate a new buffer and copy the old values to the new locaiton. \r
193 //\r
194 TempArray = mWinNt->HeapAlloc ( mWinNt->GetProcessHeap (),\r
195 HEAP_ZERO_MEMORY,\r
196 mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE)\r
197 ); \r
198 \r
199 CopyMem ((VOID *) (UINTN) TempArray, (VOID *) (UINTN)mPdbNameModHandleArray, PreviousSize);\r
200 \r
201 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, mPdbNameModHandleArray);\r
202 \r
203 mPdbNameModHandleArray = TempArray;\r
204 if (mPdbNameModHandleArray == NULL) {\r
205 ASSERT (FALSE);\r
206 return EFI_OUT_OF_RESOURCES;\r
207 }\r
208 \r
209 \r
210 return AddModHandle (ImageContext, ModHandle);\r
211}\r
212/**\r
213 Return the ModHandle and delete the entry in the array.\r
214\r
215\r
216 @param ImageContext - Input data returned from PE Laoder Library. Used to find the \r
217 .PDB file name of the PE Image.\r
218\r
219 @return \r
220 ModHandle - ModHandle assoicated with ImageContext is returned\r
221 NULL - No ModHandle associated with ImageContext\r
222\r
223**/\r
224VOID *\r
225RemoveModeHandle (\r
226 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
227 )\r
228{\r
229 UINTN Index;\r
230 PDB_NAME_TO_MOD_HANDLE *Array;\r
231\r
232 if (ImageContext->PdbPointer == NULL) {\r
233 //\r
234 // If no PDB pointer there is no ModHandle so return NULL\r
235 //\r
236 return NULL;\r
237 }\r
238\r
239 Array = mPdbNameModHandleArray;\r
240 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
241 if ((Array->PdbPointer != NULL) && (AsciiStrCmp(Array->PdbPointer, ImageContext->PdbPointer) == 0)) {\r
242 //\r
243 // If you find a match return it and delete the entry\r
244 //\r
245 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, Array->PdbPointer);\r
246 Array->PdbPointer = NULL;\r
247 return Array->ModHandle;\r
248 }\r
249 }\r
250\r
251 return NULL;\r
252}\r
253\r
254/**\r
51f1a2b5 255 Performs additional actions after a PE/COFF image has been loaded and relocated.\r
ffdd18bb 256\r
51f1a2b5 257 For NT32, this function load symbols to support source level debugging.\r
258\r
259 If ImageContext is NULL, then ASSERT().\r
260\r
261 @param ImageContext Pointer to the image context structure that describes the\r
262 PE/COFF image that has already been loaded and relocated.\r
ffdd18bb 263\r
264**/\r
265VOID\r
266EFIAPI\r
267PeCoffLoaderRelocateImageExtraAction (\r
268 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
269 )\r
270{\r
66352173 271 EFI_STATUS Status;\r
ffdd18bb 272 VOID *DllEntryPoint;\r
273 CHAR16 *DllFileName;\r
274 HMODULE Library;\r
275 UINTN Index;\r
51f1a2b5 276\r
277 ASSERT (ImageContext != NULL);\r
278\r
21ff163c 279 if (mWinNt == NULL) {\r
1a2ae0f7 280 Nt32PeCoffGetWinNtThunkStucture ();\r
21ff163c 281 }\r
282\r
1a2ae0f7 283 //\r
ffdd18bb 284 // If we load our own PE COFF images the Windows debugger can not source\r
285 // level debug our code. If a valid PDB pointer exists usw it to load\r
286 // the *.dll file as a library using Windows* APIs. This allows \r
8aa9d680 287 // source level debug. The image is still loaded and relocated\r
ffdd18bb 288 // in the Framework memory space like on a real system (by the code above),\r
289 // but the entry point points into the DLL loaded by the code bellow. \r
290 //\r
291\r
292 DllEntryPoint = NULL;\r
293\r
294 //\r
295 // Load the DLL if it's not an EBC image.\r
296 //\r
297 if ((ImageContext->PdbPointer != NULL) &&\r
298 (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC)) {\r
299 //\r
300 // Convert filename from ASCII to Unicode\r
301 //\r
302 DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);\r
303\r
304 //\r
305 // Check that we have a valid filename\r
306 //\r
307 if (Index < 5 || DllFileName[Index - 4] != '.') {\r
308 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, DllFileName);\r
309\r
310 //\r
311 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
312 // The image will run, but we just can't source level debug. If we\r
313 // return an error the image will not run.\r
314 //\r
315 return;\r
316 }\r
317 //\r
318 // Replace .PDB with .DLL on the filename\r
319 //\r
320 DllFileName[Index - 3] = 'D';\r
321 DllFileName[Index - 2] = 'L';\r
322 DllFileName[Index - 1] = 'L';\r
323\r
324 //\r
325 // Load the .DLL file into the user process's address space for source \r
326 // level debug\r
327 //\r
328 Library = mWinNt->LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);\r
329 if (Library != NULL) {\r
330 //\r
331 // InitializeDriver is the entry point we put in all our EFI DLL's. The\r
8aa9d680 332 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() suppresses the \r
ffdd18bb 333 // normal DLL entry point of DllMain, and prevents other modules that are\r
334 // referenced in side the DllFileName from being loaded. There is no error \r
335 // checking as the we can point to the PE32 image loaded by Tiano. This \r
8aa9d680 336 // step is only needed for source level debugging\r
ffdd18bb 337 //\r
338 DllEntryPoint = (VOID *) (UINTN) mWinNt->GetProcAddress (Library, "InitializeDriver");\r
339\r
340 }\r
341\r
342 if ((Library != NULL) && (DllEntryPoint != NULL)) {\r
66352173 343 Status = AddModHandle (ImageContext, Library);\r
344 if (Status == EFI_ALREADY_STARTED) {\r
345 //\r
346 // If the DLL has already been loaded before, then this instance of the DLL can not be debugged.\r
347 //\r
348 ImageContext->PdbPointer = NULL;\r
349 DEBUG ((EFI_D_ERROR, "WARNING: DLL already loaded. No source level debug %s. \n", DllFileName));\r
350 } else {\r
351 //\r
8aa9d680 352 // This DLL is not already loaded, so source level debugging is supported.\r
66352173 353 //\r
354 ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r
355 DEBUG ((EFI_D_INFO, "LoadLibraryEx (%s,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName));\r
356 }\r
ffdd18bb 357 } else {\r
66352173 358 //\r
359 // This DLL does not support source level debugging at all.\r
360 //\r
ffdd18bb 361 DEBUG ((EFI_D_ERROR, "WARNING: No source level debug %s. \n", DllFileName));\r
362 }\r
363\r
364 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, DllFileName);\r
365 }\r
366\r
367 //\r
368 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
369 // The image will run, but we just can't source level debug. If we\r
370 // return an error the image will not run.\r
371 //\r
372 return;\r
373} \r
374\r
375/**\r
51f1a2b5 376 Performs additional actions just before a PE/COFF image is unloaded. Any resources\r
377 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.\r
ffdd18bb 378 \r
51f1a2b5 379 For NT32, this function unloads symbols for source level debugging.\r
380\r
ffdd18bb 381 If ImageContext is NULL, then ASSERT().\r
382 \r
51f1a2b5 383 @param ImageContext Pointer to the image context structure that describes the\r
384 PE/COFF image that is being unloaded.\r
ffdd18bb 385\r
386**/\r
387VOID\r
388EFIAPI\r
389PeCoffLoaderUnloadImageExtraAction (\r
390 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
391 )\r
392{\r
1a2ae0f7 393 VOID *ModHandle;\r
ffdd18bb 394\r
51f1a2b5 395 ASSERT (ImageContext != NULL);\r
396\r
ffdd18bb 397 ModHandle = RemoveModeHandle (ImageContext);\r
398 if (ModHandle != NULL) {\r
399 mWinNt->FreeLibrary (ModHandle);\r
400 }\r
401 return;\r
ee31b443 402}\r