3 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
8 PeiNt32PeCoffExtraActionLib.c
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
19 // The package level header files this module uses
24 // The protocols, PPI and GUID defintions for this module
26 #include <Protocol/WinNtThunk.h>
28 #include <Library/PeCoffLib.h>
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>
36 #define MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE 0x100
41 } PDB_NAME_TO_MOD_HANDLE
;
45 // Cache of WinNtThunk protocol
47 EFI_WIN_NT_THUNK_PROTOCOL
*mWinNt
= NULL
;
50 // An Array to hold the ModHandle
52 PDB_NAME_TO_MOD_HANDLE
*mPdbNameModHandleArray
= NULL
;
53 UINTN mPdbNameModHandleArraySize
= 0;
57 The constructor function gets the pointer of the WinNT thunk functions
58 It will ASSERT() if NT thunk protocol is not installed.
60 @retval EFI_SUCCESS WinNT thunk protocol is found and cached.
65 Nt32PeCoffGetWinNtThunkStucture (
69 EFI_HOB_GUID_TYPE
*GuidHob
;
72 // Retrieve WinNtThunkProtocol from GUID'ed HOB
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
);
84 Convert the passed in Ascii string to Unicode.
86 This function Convert the passed in Ascii string to Unicode.Optionally return
87 the length of the strings..
89 @param AsciiString Pointer to an AscII string
90 @param StrLen Length of string
92 @return Pointer to malloc'ed Unicode version of Ascii
98 IN UINTN
*StrLen OPTIONAL
105 // Allocate a buffer for unicode string
107 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++)
109 Unicode
= mWinNt
->HeapAlloc ( mWinNt
->GetProcessHeap (),
111 ((Index
+ 1) * sizeof (CHAR16
))
113 if (Unicode
== NULL
) {
117 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++) {
118 Unicode
[Index
] = (CHAR16
) Ascii
[Index
];
121 Unicode
[Index
] = '\0';
123 if (StrLen
!= NULL
) {
130 Store the ModHandle in an array indexed by the Pdb File name.
131 The ModHandle is needed to unload the image.
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
139 @return return EFI_SUCCESS when ModHandle was stored.
144 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
150 PDB_NAME_TO_MOD_HANDLE
*Array
;
152 PDB_NAME_TO_MOD_HANDLE
*TempArray
;
156 // Return EFI_ALREADY_STARTED if this DLL has already been loaded
158 Array
= mPdbNameModHandleArray
;
159 for (Index
= 0; Index
< mPdbNameModHandleArraySize
; Index
++, Array
++) {
160 if (Array
->PdbPointer
!= NULL
&& Array
->ModHandle
== ModHandle
) {
161 return EFI_ALREADY_STARTED
;
165 Array
= mPdbNameModHandleArray
;
166 for (Index
= 0; Index
< mPdbNameModHandleArraySize
; Index
++, Array
++) {
167 if (Array
->PdbPointer
== NULL
) {
169 // Make a copy of the stirng and store the ModHandle
171 Handle
= mWinNt
->GetProcessHeap ();
172 Array
->PdbPointer
= mWinNt
->HeapAlloc ( Handle
,
174 AsciiStrLen (ImageContext
->PdbPointer
) + 1
177 ASSERT (Array
->PdbPointer
!= NULL
);
179 AsciiStrCpy (Array
->PdbPointer
, ImageContext
->PdbPointer
);
180 Array
->ModHandle
= ModHandle
;
186 // No free space in mPdbNameModHandleArray so grow it by
187 // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires.
189 PreviousSize
= mPdbNameModHandleArraySize
* sizeof (PDB_NAME_TO_MOD_HANDLE
);
190 mPdbNameModHandleArraySize
+= MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE
;
192 // re-allocate a new buffer and copy the old values to the new locaiton.
194 TempArray
= mWinNt
->HeapAlloc ( mWinNt
->GetProcessHeap (),
196 mPdbNameModHandleArraySize
* sizeof (PDB_NAME_TO_MOD_HANDLE
)
199 CopyMem ((VOID
*) (UINTN
) TempArray
, (VOID
*) (UINTN
)mPdbNameModHandleArray
, PreviousSize
);
201 mWinNt
->HeapFree (mWinNt
->GetProcessHeap (), 0, mPdbNameModHandleArray
);
203 mPdbNameModHandleArray
= TempArray
;
204 if (mPdbNameModHandleArray
== NULL
) {
206 return EFI_OUT_OF_RESOURCES
;
210 return AddModHandle (ImageContext
, ModHandle
);
213 Return the ModHandle and delete the entry in the array.
216 @param ImageContext - Input data returned from PE Laoder Library. Used to find the
217 .PDB file name of the PE Image.
220 ModHandle - ModHandle assoicated with ImageContext is returned
221 NULL - No ModHandle associated with ImageContext
226 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
230 PDB_NAME_TO_MOD_HANDLE
*Array
;
232 if (ImageContext
->PdbPointer
== NULL
) {
234 // If no PDB pointer there is no ModHandle so return NULL
239 Array
= mPdbNameModHandleArray
;
240 for (Index
= 0; Index
< mPdbNameModHandleArraySize
; Index
++, Array
++) {
241 if ((Array
->PdbPointer
!= NULL
) && (AsciiStrCmp(Array
->PdbPointer
, ImageContext
->PdbPointer
) == 0)) {
243 // If you find a match return it and delete the entry
245 mWinNt
->HeapFree (mWinNt
->GetProcessHeap (), 0, Array
->PdbPointer
);
246 Array
->PdbPointer
= NULL
;
247 return Array
->ModHandle
;
255 Performs additional actions after a PE/COFF image has been loaded and relocated.
257 For NT32, this function load symbols to support source level debugging.
259 If ImageContext is NULL, then ASSERT().
261 @param ImageContext Pointer to the image context structure that describes the
262 PE/COFF image that has already been loaded and relocated.
267 PeCoffLoaderRelocateImageExtraAction (
268 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
277 ASSERT (ImageContext
!= NULL
);
279 if (mWinNt
== NULL
) {
280 Nt32PeCoffGetWinNtThunkStucture ();
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.
292 DllEntryPoint
= NULL
;
295 // Load the DLL if it's not an EBC image.
297 if ((ImageContext
->PdbPointer
!= NULL
) &&
298 (ImageContext
->Machine
!= EFI_IMAGE_MACHINE_EBC
)) {
300 // Convert filename from ASCII to Unicode
302 DllFileName
= AsciiToUnicode (ImageContext
->PdbPointer
, &Index
);
305 // Check that we have a valid filename
307 if (Index
< 5 || DllFileName
[Index
- 4] != '.') {
308 mWinNt
->HeapFree (mWinNt
->GetProcessHeap (), 0, DllFileName
);
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.
318 // Replace .PDB with .DLL on the filename
320 DllFileName
[Index
- 3] = 'D';
321 DllFileName
[Index
- 2] = 'L';
322 DllFileName
[Index
- 1] = 'L';
325 // Load the .DLL file into the user process's address space for source
328 Library
= mWinNt
->LoadLibraryEx (DllFileName
, NULL
, DONT_RESOLVE_DLL_REFERENCES
);
329 if (Library
!= NULL
) {
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
338 DllEntryPoint
= (VOID
*) (UINTN
) mWinNt
->GetProcAddress (Library
, "InitializeDriver");
342 if ((Library
!= NULL
) && (DllEntryPoint
!= NULL
)) {
343 Status
= AddModHandle (ImageContext
, Library
);
344 if (Status
== EFI_ALREADY_STARTED
) {
346 // If the DLL has already been loaded before, then this instance of the DLL can not be debugged.
348 ImageContext
->PdbPointer
= NULL
;
349 DEBUG ((EFI_D_ERROR
, "WARNING: DLL already loaded. No source level debug %s. \n", DllFileName
));
352 // This DLL is not already loaded, so source level debugging is supported.
354 ImageContext
->EntryPoint
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) DllEntryPoint
;
355 DEBUG ((EFI_D_INFO
, "LoadLibraryEx (%s,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName
));
359 // This DLL does not support source level debugging at all.
361 DEBUG ((EFI_D_ERROR
, "WARNING: No source level debug %s. \n", DllFileName
));
364 mWinNt
->HeapFree (mWinNt
->GetProcessHeap (), 0, DllFileName
);
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.
376 Performs additional actions just before a PE/COFF image is unloaded. Any resources
377 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
379 For NT32, this function unloads symbols for source level debugging.
381 If ImageContext is NULL, then ASSERT().
383 @param ImageContext Pointer to the image context structure that describes the
384 PE/COFF image that is being unloaded.
389 PeCoffLoaderUnloadImageExtraAction (
390 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
395 ASSERT (ImageContext
!= NULL
);
397 ModHandle
= RemoveModeHandle (ImageContext
);
398 if (ModHandle
!= NULL
) {
399 mWinNt
->FreeLibrary (ModHandle
);