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 PEI phase
25 // The package level header files this module uses
27 #include <FrameworkPei.h>
28 #include <FrameworkModulePei.h>
29 #include <WinNtPeim.h>
32 // The protocols, PPI and GUID defintions for this module
34 #include <Ppi/NtThunk.h>
37 #include <Library/PeCoffLib.h>
38 #include <Library/PeiServicesLib.h>
39 #include <Library/DebugLib.h>
40 #include <Library/BaseLib.h>
41 #include <Library/PeCoffExtraActionLib.h>
44 // Cache of WinNtThunk protocol
46 EFI_WIN_NT_THUNK_PROTOCOL
*mWinNt
= NULL
;
49 The function caches the pointer of the WinNT thunk functions
50 It will ASSERT() if NT thunk ppi is not installed.
52 @retval EFI_SUCCESS WinNT thunk protocol is found and cached.
57 Nt32PeCoffGetWinNtThunkStucture (
60 PEI_NT_THUNK_PPI
*NtThunkPpi
;
65 // Locate NtThunkPpi for retrieving standard output handle
67 Status
= PeiServicesLocatePpi (
74 ASSERT_EFI_ERROR (Status
);
76 mWinNt
= (EFI_WIN_NT_THUNK_PROTOCOL
*) NtThunkPpi
->NtThunk ();
82 Convert the passed in Ascii string to Unicode.
84 This function Convert the passed in Ascii string to Unicode.Optionally return
85 the length of the strings..
87 @param AsciiString Pointer to an AscII string
88 @param StrLen Length of string
90 @return Pointer to malloc'ed Unicode version of Ascii
96 IN UINTN
*StrLen OPTIONAL
103 // Allocate a buffer for unicode string
105 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++)
107 Unicode
= mWinNt
->HeapAlloc ( mWinNt
->GetProcessHeap (),
109 ((Index
+ 1) * sizeof (CHAR16
))
111 if (Unicode
== NULL
) {
115 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++) {
116 Unicode
[Index
] = (CHAR16
) Ascii
[Index
];
119 Unicode
[Index
] = '\0';
121 if (StrLen
!= NULL
) {
129 Performs additional actions after a PE/COFF image has been loaded and relocated.
131 For NT32, this function load symbols to support source level debugging.
133 If ImageContext is NULL, then ASSERT().
135 @param ImageContext Pointer to the image context structure that describes the
136 PE/COFF image that has already been loaded and relocated.
141 PeCoffLoaderRelocateImageExtraAction (
142 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
150 ASSERT (ImageContext
!= NULL
);
152 if (mWinNt
== NULL
) {
153 Nt32PeCoffGetWinNtThunkStucture ();
156 // If we load our own PE COFF images the Windows debugger can not source
157 // level debug our code. If a valid PDB pointer exists usw it to load
158 // the *.dll file as a library using Windows* APIs. This allows
159 // source level debug. The image is still loaded and reloaced
160 // in the Framework memory space like on a real system (by the code above),
161 // but the entry point points into the DLL loaded by the code bellow.
164 DllEntryPoint
= NULL
;
167 // Load the DLL if it's not an EBC image.
169 if ((ImageContext
->PdbPointer
!= NULL
) &&
170 (ImageContext
->Machine
!= EFI_IMAGE_MACHINE_EBC
)) {
172 // Convert filename from ASCII to Unicode
174 DllFileName
= AsciiToUnicode (ImageContext
->PdbPointer
, &Index
);
177 // Check that we have a valid filename
179 if (Index
< 5 || DllFileName
[Index
- 4] != '.') {
180 mWinNt
->HeapFree (mWinNt
->GetProcessHeap (), 0, DllFileName
);
183 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
184 // The image will run, but we just can't source level debug. If we
185 // return an error the image will not run.
190 // Replace .PDB with .DLL on the filename
192 DllFileName
[Index
- 3] = 'D';
193 DllFileName
[Index
- 2] = 'L';
194 DllFileName
[Index
- 1] = 'L';
197 // Load the .DLL file into the user process's address space for source
200 Library
= mWinNt
->LoadLibraryEx (DllFileName
, NULL
, DONT_RESOLVE_DLL_REFERENCES
);
201 if (Library
!= NULL
) {
203 // InitializeDriver is the entry point we put in all our EFI DLL's. The
204 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the
205 // normal DLL entry point of DllMain, and prevents other modules that are
206 // referenced in side the DllFileName from being loaded. There is no error
207 // checking as the we can point to the PE32 image loaded by Tiano. This
208 // step is only needed for source level debuging
210 DllEntryPoint
= (VOID
*) (UINTN
) mWinNt
->GetProcAddress (Library
, "InitializeDriver");
214 if ((Library
!= NULL
) && (DllEntryPoint
!= NULL
)) {
215 ImageContext
->EntryPoint
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) DllEntryPoint
;
216 DEBUG ((EFI_D_INFO
, "LoadLibraryEx (%s,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName
));
218 DEBUG ((EFI_D_ERROR
, "WARNING: No source level debug %s. \n", DllFileName
));
221 mWinNt
->HeapFree (mWinNt
->GetProcessHeap (), 0, DllFileName
);
225 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
226 // The image will run, but we just can't source level debug. If we
227 // return an error the image will not run.
233 Performs additional actions just before a PE/COFF image is unloaded. Any resources
234 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
236 For NT32, this function unloads symbols for source level debugging.
238 If ImageContext is NULL, then ASSERT().
240 @param ImageContext Pointer to the image context structure that describes the
241 PE/COFF image that is being unloaded.
246 PeCoffLoaderUnloadImageExtraAction (
247 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
250 ASSERT (ImageContext
!= NULL
);