]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/RvdPeCoffExtraActionLib/RvdPeCoffExtraActionLib.c
Added new PeCoffGetEntryPoint lib function to get size of PE/COFF header. This is...
[mirror_edk2.git] / ArmPkg / Library / RvdPeCoffExtraActionLib / RvdPeCoffExtraActionLib.c
1 /**@file
2
3 Copyright (c) 2006 - 2009, Intel Corporation
4 Portions copyright (c) 2008-2010 Apple Inc. All rights reserved.
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <PiDxe.h>
16 #include <Library/PeCoffLib.h>
17
18 #include <Library/BaseLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/PeCoffExtraActionLib.h>
22 #include <Library/SemihostLib.h>
23 #include <Library/PrintLib.h>
24
25 /**
26 Append string to debugger script file, create file if needed.
27
28 This library can show up in mulitple places so we need to append the file every time we write to it.
29 For example Sec can use this to load the DXE core, and the DXE core would use this to load all the
30 other modules. So we have two instances of the library in the system.
31
32 @param Buffer Buffer to write to file.
33 @param Length Length of Buffer in bytes.
34 **/
35 VOID
36 WriteStringToFile (
37 IN VOID *Buffer,
38 IN UINT32 Length
39 )
40 {
41 // Working around and issue with the code that is commented out. For now send it to the console.
42 // You can copy the console into a file and source the file as a script and you get symbols.
43 // This gets you all the symbols except for SEC. To get SEC symbols you need to copy the
44 // debug print in the SEC into the debugger manually
45 SemihostWriteString (Buffer);
46 /*
47 I'm currently having issues with this code crashing the debugger. Seems like it should work.
48
49 UINT32 SemihostHandle;
50 UINT32 SemihostMode = SEMIHOST_FILE_MODE_WRITE | SEMIHOST_FILE_MODE_BINARY | SEMIHOST_FILE_MODE_CREATE;
51
52 SemihostFileOpen ("c:\rvi_symbols.inc", SemihostMode, &SemihostHandle);
53 SemihostFileWrite (SemihostHandle, &Length, Buffer);
54 SemihostFileClose (SemihostHandle);
55 */
56 }
57
58
59 /**
60 If the build is done on cygwin the paths are cygpaths.
61 /cygdrive/c/tmp.txt vs c:\tmp.txt so we need to convert
62 them to work with RVD commands
63
64 @param Name Path to convert if needed
65
66 **/
67 CHAR8 *
68 DeCygwinPathIfNeeded (
69 IN CHAR8 *Name
70 )
71 {
72 CHAR8 *Ptr;
73 UINTN Index;
74 UINTN Len;
75
76 Ptr = AsciiStrStr (Name, "/cygdrive/");
77 if (Ptr == NULL) {
78 return Name;
79 }
80
81 Len = AsciiStrLen (Ptr);
82
83 // convert "/cygdrive" to spaces
84 for (Index = 0; Index < 9; Index++) {
85 Ptr[Index] = ' ';
86 }
87
88 // convert /c to c:
89 Ptr[9] = Ptr[10];
90 Ptr[10] = ':';
91
92 // switch path seperators
93 for (Index = 11; Index < Len; Index++) {
94 if (Ptr[Index] == '/') {
95 Ptr[Index] = '\\' ;
96 }
97 }
98
99 return Name;
100 }
101
102
103 /**
104 Performs additional actions after a PE/COFF image has been loaded and relocated.
105
106 If ImageContext is NULL, then ASSERT().
107
108 @param ImageContext Pointer to the image context structure that describes the
109 PE/COFF image that has already been loaded and relocated.
110
111 **/
112 VOID
113 EFIAPI
114 PeCoffLoaderRelocateImageExtraAction (
115 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
116 )
117 {
118 CHAR8 Buffer[256];
119
120 AsciiSPrint (Buffer, sizeof(Buffer), "load /a /ni /np %a &0x%08x\n", ImageContext->PdbPointer, (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders));
121 DeCygwinPathIfNeeded (&Buffer[16]);
122
123 WriteStringToFile (Buffer, AsciiStrSize (Buffer));
124 }
125
126
127
128 /**
129 Performs additional actions just before a PE/COFF image is unloaded. Any resources
130 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
131
132 If ImageContext is NULL, then ASSERT().
133
134 @param ImageContext Pointer to the image context structure that describes the
135 PE/COFF image that is being unloaded.
136
137 **/
138 VOID
139 EFIAPI
140 PeCoffLoaderUnloadImageExtraAction (
141 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
142 )
143 {
144 CHAR8 Buffer[256];
145
146 AsciiSPrint (Buffer, sizeof(Buffer), "unload symbols_only %a", ImageContext->PdbPointer);
147 DeCygwinPathIfNeeded (Buffer);
148
149 WriteStringToFile (Buffer, AsciiStrSize (Buffer));
150 }