3 Copyright (c) 2006, 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
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.
14 PeiNt32PeCoffExtraActionLib.c
18 Provides services to perform additional actions to relocate and unload
19 PE/Coff image for NT32 environment specific purpose such as souce level debug.
20 This version only works for DXE phase
25 // The package level header files this module uses
27 #include <FrameworkDxe.h>
31 // The protocols, PPI and GUID defintions for this module
33 #include <Protocol/WinNtThunk.h>
35 #include <Library/PeCoffLib.h>
36 #include <Library/PeiServicesLib.h>
38 #include <Library/BaseLib.h>
39 #include <Library/DebugLib.h>
40 #include <Library/HobLib.h>
41 #include <Library/BaseMemoryLib.h>
42 #include <Library/PeCoffExtraActionLib.h>
44 #define MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE 0x100
49 } PDB_NAME_TO_MOD_HANDLE
;
53 // Cache of WinNtThunk protocol
55 EFI_WIN_NT_THUNK_PROTOCOL
*mWinNt
= NULL
;
58 // An Array to hold the ModHandle
60 PDB_NAME_TO_MOD_HANDLE
*mPdbNameModHandleArray
= NULL
;
61 UINTN mPdbNameModHandleArraySize
= 0;
65 The constructor function gets the pointer of the WinNT thunk functions
66 It will ASSERT() if NT thunk protocol is not installed.
68 @retval EFI_SUCCESS WinNT thunk protocol is found and cached.
73 DxeNt32PeCoffLibExtraActionConstructor (
74 IN EFI_HANDLE ImageHandle
,
75 IN EFI_SYSTEM_TABLE
*SystemTable
78 EFI_HOB_GUID_TYPE
*GuidHob
;
81 // Retrieve WinNtThunkProtocol from GUID'ed HOB
83 GuidHob
= GetFirstGuidHob (&gEfiWinNtThunkProtocolGuid
);
84 ASSERT (GuidHob
!= NULL
);
85 mWinNt
= (EFI_WIN_NT_THUNK_PROTOCOL
*)(*(UINTN
*)(GET_GUID_HOB_DATA (GuidHob
)));
86 ASSERT (mWinNt
!= NULL
);
93 Convert the passed in Ascii string to Unicode.
95 This function Convert the passed in Ascii string to Unicode.Optionally return
96 the length of the strings..
98 @param AsciiString Pointer to an AscII string
99 @param StrLen Length of string
101 @return Pointer to malloc'ed Unicode version of Ascii
107 IN UINTN
*StrLen OPTIONAL
114 // Allocate a buffer for unicode string
116 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++)
118 Unicode
= mWinNt
->HeapAlloc ( mWinNt
->GetProcessHeap (),
120 ((Index
+ 1) * sizeof (CHAR16
))
122 if (Unicode
== NULL
) {
126 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++) {
127 Unicode
[Index
] = (CHAR16
) Ascii
[Index
];
130 Unicode
[Index
] = '\0';
132 if (StrLen
!= NULL
) {
139 Store the ModHandle in an array indexed by the Pdb File name.
140 The ModHandle is needed to unload the image.
143 @param ImageContext - Input data returned from PE Laoder Library. Used to find the
144 .PDB file name of the PE Image.
145 @param ModHandle - Returned from LoadLibraryEx() and stored for call to
148 @return return EFI_SUCCESS when ModHandle was stored.
153 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
159 PDB_NAME_TO_MOD_HANDLE
*Array
;
161 PDB_NAME_TO_MOD_HANDLE
*TempArray
;
164 Array
= mPdbNameModHandleArray
;
165 for (Index
= 0; Index
< mPdbNameModHandleArraySize
; Index
++, Array
++) {
166 if (Array
->PdbPointer
== NULL
) {
168 // Make a copy of the stirng and store the ModHandle
170 Handle
= mWinNt
->GetProcessHeap ();
171 Array
->PdbPointer
= mWinNt
->HeapAlloc ( Handle
,
173 AsciiStrLen (ImageContext
->PdbPointer
) + 1
176 ASSERT (Array
->PdbPointer
!= NULL
);
178 AsciiStrCpy (Array
->PdbPointer
, ImageContext
->PdbPointer
);
179 Array
->ModHandle
= ModHandle
;
185 // No free space in mPdbNameModHandleArray so grow it by
186 // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires.
188 PreviousSize
= mPdbNameModHandleArraySize
* sizeof (PDB_NAME_TO_MOD_HANDLE
);
189 mPdbNameModHandleArraySize
+= MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE
;
191 // re-allocate a new buffer and copy the old values to the new locaiton.
193 TempArray
= mWinNt
->HeapAlloc ( mWinNt
->GetProcessHeap (),
195 mPdbNameModHandleArraySize
* sizeof (PDB_NAME_TO_MOD_HANDLE
)
198 CopyMem ((VOID
*) (UINTN
) TempArray
, (VOID
*) (UINTN
)mPdbNameModHandleArray
, PreviousSize
);
200 mWinNt
->HeapFree (mWinNt
->GetProcessHeap (), 0, mPdbNameModHandleArray
);
202 mPdbNameModHandleArray
= TempArray
;
203 if (mPdbNameModHandleArray
== NULL
) {
205 return EFI_OUT_OF_RESOURCES
;
209 return AddModHandle (ImageContext
, ModHandle
);
212 Return the ModHandle and delete the entry in the array.
215 @param ImageContext - Input data returned from PE Laoder Library. Used to find the
216 .PDB file name of the PE Image.
219 ModHandle - ModHandle assoicated with ImageContext is returned
220 NULL - No ModHandle associated with ImageContext
225 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
229 PDB_NAME_TO_MOD_HANDLE
*Array
;
231 if (ImageContext
->PdbPointer
== NULL
) {
233 // If no PDB pointer there is no ModHandle so return NULL
238 Array
= mPdbNameModHandleArray
;
239 for (Index
= 0; Index
< mPdbNameModHandleArraySize
; Index
++, Array
++) {
240 if ((Array
->PdbPointer
!= NULL
) && (AsciiStrCmp(Array
->PdbPointer
, ImageContext
->PdbPointer
) == 0)) {
242 // If you find a match return it and delete the entry
244 mWinNt
->HeapFree (mWinNt
->GetProcessHeap (), 0, Array
->PdbPointer
);
245 Array
->PdbPointer
= NULL
;
246 return Array
->ModHandle
;
254 Performs additional actions after a PE/COFF image has been loaded and relocated.
256 For NT32, this function load symbols to support source level debugging.
258 If ImageContext is NULL, then ASSERT().
260 @param ImageContext Pointer to the image context structure that describes the
261 PE/COFF image that has already been loaded and relocated.
266 PeCoffLoaderRelocateImageExtraAction (
267 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
275 ASSERT (ImageContext
!= NULL
);
278 // If we load our own PE COFF images the Windows debugger can not source
279 // level debug our code. If a valid PDB pointer exists usw it to load
280 // the *.dll file as a library using Windows* APIs. This allows
281 // source level debug. The image is still loaded and reloaced
282 // in the Framework memory space like on a real system (by the code above),
283 // but the entry point points into the DLL loaded by the code bellow.
286 DllEntryPoint
= NULL
;
289 // Load the DLL if it's not an EBC image.
291 if ((ImageContext
->PdbPointer
!= NULL
) &&
292 (ImageContext
->Machine
!= EFI_IMAGE_MACHINE_EBC
)) {
294 // Convert filename from ASCII to Unicode
296 DllFileName
= AsciiToUnicode (ImageContext
->PdbPointer
, &Index
);
299 // Check that we have a valid filename
301 if (Index
< 5 || DllFileName
[Index
- 4] != '.') {
302 mWinNt
->HeapFree (mWinNt
->GetProcessHeap (), 0, DllFileName
);
305 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
306 // The image will run, but we just can't source level debug. If we
307 // return an error the image will not run.
312 // Replace .PDB with .DLL on the filename
314 DllFileName
[Index
- 3] = 'D';
315 DllFileName
[Index
- 2] = 'L';
316 DllFileName
[Index
- 1] = 'L';
319 // Load the .DLL file into the user process's address space for source
322 Library
= mWinNt
->LoadLibraryEx (DllFileName
, NULL
, DONT_RESOLVE_DLL_REFERENCES
);
323 if (Library
!= NULL
) {
325 // InitializeDriver is the entry point we put in all our EFI DLL's. The
326 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the
327 // normal DLL entry point of DllMain, and prevents other modules that are
328 // referenced in side the DllFileName from being loaded. There is no error
329 // checking as the we can point to the PE32 image loaded by Tiano. This
330 // step is only needed for source level debuging
332 DllEntryPoint
= (VOID
*) (UINTN
) mWinNt
->GetProcAddress (Library
, "InitializeDriver");
336 if ((Library
!= NULL
) && (DllEntryPoint
!= NULL
)) {
337 AddModHandle (ImageContext
, Library
);
338 ImageContext
->EntryPoint
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) DllEntryPoint
;
339 DEBUG ((EFI_D_INFO
, "LoadLibraryEx (%s,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName
));
341 DEBUG ((EFI_D_ERROR
, "WARNING: No source level debug %s. \n", DllFileName
));
344 mWinNt
->HeapFree (mWinNt
->GetProcessHeap (), 0, DllFileName
);
348 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
349 // The image will run, but we just can't source level debug. If we
350 // return an error the image will not run.
356 Performs additional actions just before a PE/COFF image is unloaded. Any resources
357 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
359 For NT32, this function unloads symbols for source level debugging.
361 If ImageContext is NULL, then ASSERT().
363 @param ImageContext Pointer to the image context structure that describes the
364 PE/COFF image that is being unloaded.
369 PeCoffLoaderUnloadImageExtraAction (
370 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
375 ASSERT (ImageContext
!= NULL
);
377 ModHandle
= RemoveModeHandle (ImageContext
);
378 if (ModHandle
!= NULL
) {
379 mWinNt
->FreeLibrary (ModHandle
);