]> git.proxmox.com Git - mirror_edk2.git/blob - UnixPkg/Library/DxeUnixPeCoffExtraActionLib/DxeUnixPeCoffExtraActionLib.c
014390cf4fdb9b2d5b15bc5094ad73ad29687334
[mirror_edk2.git] / UnixPkg / Library / DxeUnixPeCoffExtraActionLib / DxeUnixPeCoffExtraActionLib.c
1 /**@file
2
3 Copyright (c) 2006 - 2009, 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
8
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.
11
12 Module Name:
13
14 PeiUnixPeCoffExtraActionLib.c
15
16 Abstract:
17
18 Provides services to perform additional actions to relocate and unload
19 PE/Coff image for Unix environment specific purpose such as souce level debug.
20 This version only works for DXE phase
21
22
23 **/
24
25 #include <FrameworkDxe.h>
26 #include <Guid/StatusCodeDataTypeId.h>
27 #include <UnixDxe.h>
28 #include <Library/PeCoffLib.h>
29
30
31 #include <Library/BaseLib.h>
32 #include <Library/DebugLib.h>
33 #include <Library/HobLib.h>
34 #include <Library/BaseMemoryLib.h>
35 #include <Library/PeCoffExtraActionLib.h>
36
37 //
38 // Cache of UnixThunk protocol
39 //
40 EFI_UNIX_THUNK_PROTOCOL *mUnix;
41
42
43 /**
44 The constructor function gets the pointer of the WinNT thunk functions
45 It will ASSERT() if Unix thunk protocol is not installed.
46
47 @retval EFI_SUCCESS Unix thunk protocol is found and cached.
48
49 **/
50 EFI_STATUS
51 EFIAPI
52 DxeUnixPeCoffLibExtraActionConstructor (
53 IN EFI_HANDLE ImageHandle,
54 IN EFI_SYSTEM_TABLE *SystemTable
55 )
56 {
57 EFI_HOB_GUID_TYPE *GuidHob;
58
59 //
60 // Retrieve UnixThunkProtocol from GUID'ed HOB
61 //
62 GuidHob = GetFirstGuidHob (&gEfiUnixThunkProtocolGuid);
63 ASSERT (GuidHob != NULL);
64 mUnix = (EFI_UNIX_THUNK_PROTOCOL *)(*(UINTN *)(GET_GUID_HOB_DATA (GuidHob)));
65 ASSERT (mUnix != NULL);
66
67 return EFI_SUCCESS;
68 }
69
70 /**
71 Performs additional actions after a PE/COFF image has been loaded and relocated.
72
73 If ImageContext is NULL, then ASSERT().
74
75 @param ImageContext Pointer to the image context structure that describes the
76 PE/COFF image that has already been loaded and relocated.
77
78 **/
79 VOID
80 EFIAPI
81 PeCoffLoaderRelocateImageExtraAction (
82 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
83 )
84 {
85 VOID * Handle;
86 VOID * Entry;
87
88 ASSERT (ImageContext != NULL);
89
90 Handle = NULL;
91 Entry = NULL;
92
93 DEBUG ((EFI_D_ERROR, "Loading %a 0x%08lx - entry point 0x%08lx\n",
94 ImageContext->PdbPointer,
95 (UINTN)ImageContext->ImageAddress,
96 (UINTN)ImageContext->EntryPoint));
97
98 Handle = mUnix->Dlopen(ImageContext->PdbPointer, RTLD_NOW);
99
100 if (Handle) {
101 Entry = mUnix->Dlsym(Handle, "_ModuleEntryPoint");
102 } else {
103 DEBUG ((EFI_D_ERROR, "%a\n", mUnix->Dlerror()));
104 }
105
106 if (Entry != NULL) {
107 ImageContext->EntryPoint = Entry;
108 DEBUG ((EFI_D_ERROR, "Change %a Entrypoint to :0x%08lx\n", ImageContext->PdbPointer, Entry));
109 }
110
111
112 return;
113 }
114
115 /**
116 Performs additional actions just before a PE/COFF image is unloaded. Any resources
117 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
118
119 If ImageContext is NULL, then ASSERT().
120
121 @param ImageContext Pointer to the image context structure that describes the
122 PE/COFF image that is being unloaded.
123
124 **/
125 VOID
126 EFIAPI
127 PeCoffLoaderUnloadImageExtraAction (
128 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
129 )
130 {
131 ASSERT (ImageContext != NULL);
132 }