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