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