2
2
using PESupport ;
3
3
using SharpPdb . Native ;
4
4
using System ;
5
+ using System . Threading . Tasks ;
5
6
using System . Collections . Generic ;
7
+ using System . Data . SQLite ;
6
8
using System . IO ;
7
9
using System . Linq ;
8
10
using System . Runtime . InteropServices ;
@@ -70,7 +72,8 @@ private static void DeprecatedMain(DeprecatedOptions options) {
70
72
new Config . PrefixFilter ( "?$TSS" ) ,
71
73
new Config . RegexFilter ( "std@@[QU]" ) ,
72
74
} ) ;
73
- var cfg = new Config {
75
+ var cfg = new Config
76
+ {
74
77
InputFile = options . InputFile ,
75
78
OutputFile = options . OutputFile ,
76
79
PdbFile = options . PdbFile ,
@@ -90,21 +93,63 @@ private static void RealMain(Config config, bool verbose) {
90
93
using var exp = File . CreateText ( config . DefFile ) ;
91
94
using var action = new PEAction ( file ) ;
92
95
using var pdb = new PdbFileReader ( config . PdbFile ) ;
96
+ using var db = new SQLiteConnection ( "" + new SQLiteConnectionStringBuilder
97
+ {
98
+ DataSource = config . FilterOutDatabase ,
99
+ SyncMode = SynchronizationModes . Off ,
100
+ NoSharedFlags = true ,
101
+ FailIfMissing = false
102
+ } ) ;
103
+ db . Open ( ) ;
104
+ var filterout = new SortedDictionary < string , uint > ( StringComparer . Ordinal ) ;
93
105
var symdb = new SymbolDatabase ( ) ;
94
106
Console . WriteLine ( "filtering ({0})" , pdb . PublicSymbols . Length ) ;
95
107
96
108
// Collect all symbols
97
- foreach ( var item in pdb . PublicSymbols . Where ( item => config . Filter ? . Filter ( item . IsCode , item . Name ) ?? true ) )
98
- symdb . Add ( ( uint ) item . RelativeVirtualAddress , item . Name ) ;
109
+ Parallel . ForEach ( pdb . PublicSymbols , ( item ) => {
110
+ if ( config . Filter ? . Filter ( item . IsCode , item . Name ) ?? true ) {
111
+ lock ( symdb ) { symdb . Add ( ( uint ) item . RelativeVirtualAddress , item . Name ) ; }
112
+ } else {
113
+ lock ( filterout ) { filterout . Add ( item . Name , ( uint ) item . RelativeVirtualAddress ) ; }
114
+ }
115
+ } ) ;
99
116
100
- Console . WriteLine ( "symbols collected: {0}" , symdb . Count ( ) ) ;
117
+ Console . WriteLine ( "symbols collected: {0}, filtered: {1} " , symdb . Count ( ) , filterout . Count ( ) ) ;
101
118
102
119
// Exclude imported symbols
103
120
foreach ( var sym in action . GetImportSymbols ( ) )
104
121
if ( symdb . RemoveName ( sym ) && verbose )
105
122
Console . WriteLine ( "Removed {0}" , sym ) ;
123
+ else if ( filterout . Remove ( sym ) && verbose )
124
+ Console . WriteLine ( "Removed {0} (filtered)" , sym ) ;
106
125
107
- Console . WriteLine ( "symbols filtered: {0}" , symdb . Count ( ) ) ;
126
+ Console . WriteLine ( "symbols fixed: {0}, filtered fixed: {1}" , symdb . Count ( ) , filterout . Count ( ) ) ;
127
+
128
+ // Write to filter out db
129
+ using ( var transition = db . BeginTransaction ( ) ) {
130
+ using ( var dropCommand = db . CreateCommand ( ) ) {
131
+ dropCommand . CommandText = "DROP TABLE IF EXISTS symbols" ;
132
+ dropCommand . Transaction = transition ;
133
+ dropCommand . ExecuteNonQuery ( ) ;
134
+ }
135
+ using ( var createCommand = db . CreateCommand ( ) ) {
136
+ createCommand . CommandText = "CREATE TABLE symbols(symbol TEXT PRIMARY KEY, rva INTEGER) WITHOUT ROWID" ;
137
+ createCommand . Transaction = transition ;
138
+ createCommand . ExecuteNonQuery ( ) ;
139
+ }
140
+ using ( var insertCommand = db . CreateCommand ( ) ) {
141
+ insertCommand . CommandText = "INSERT INTO symbols VALUES ($symbol, $rva)" ;
142
+ insertCommand . Transaction = transition ;
143
+ foreach ( var item in filterout ) {
144
+ insertCommand . Parameters . AddWithValue ( "$symbol" , item . Key ) ;
145
+ insertCommand . Parameters . AddWithValue ( "$rva" , item . Value ) ;
146
+ insertCommand . ExecuteNonQuery ( ) ;
147
+ insertCommand . Parameters . Clear ( ) ;
148
+ insertCommand . Reset ( ) ;
149
+ }
150
+ }
151
+ transition . Commit ( ) ;
152
+ }
108
153
109
154
// Build cache
110
155
var cache = symdb . ToArray ( ) ;
@@ -120,8 +165,8 @@ private static void RealMain(Config config, bool verbose) {
120
165
121
166
// Calculate append length
122
167
uint length = 0 ;
123
- var expdirlength = ( uint ) Marshal . SizeOf < ExportDir > ( ) ;
124
- var dllnamelength = ( uint ) ( config . DllName . Length + 1 ) ;
168
+ var expdirlength = ( uint ) Marshal . SizeOf < ExportDir > ( ) ;
169
+ var dllnamelength = ( uint ) ( config . DllName . Length + 1 ) ;
125
170
var NumberOfFunctions = ( uint ) cache . Count ( ) ;
126
171
var functionslength = NumberOfFunctions * 4 ;
127
172
var NumberOfNames = ( uint ) cache . Select ( item => item . Value . Count ) . Aggregate ( 0 , ( a , b ) => a + b ) ;
@@ -141,11 +186,11 @@ private static void RealMain(Config config, bool verbose) {
141
186
142
187
// Start modify header
143
188
var VirtualEnd = action . GetEnding ( ) ;
144
- var OriginalSize = ( uint ) file . Length ;
189
+ var OriginalSize = ( uint ) file . Length ;
145
190
action . PatchNtHeader ( GetAlign ( length ) ) ;
146
191
action . PatchDir ( 0 , VirtualEnd , length ) ;
147
192
{
148
- var header = new SectionHeader { } ;
193
+ var header = new SectionHeader { } ;
149
194
header . SetName ( ".hacked" ) ;
150
195
header . Misc . VirtualSize = length ;
151
196
header . VirtualAddress = VirtualEnd ;
@@ -158,7 +203,7 @@ private static void RealMain(Config config, bool verbose) {
158
203
// Write export table
159
204
file . SetLength ( OriginalSize + GetAlign ( length ) ) ;
160
205
{
161
- var expdir = new ExportDir { } ;
206
+ var expdir = new ExportDir { } ;
162
207
expdir . Name = VirtualEnd + expdirlength ;
163
208
expdir . Base = 1 ;
164
209
expdir . NumberOfFunctions = NumberOfFunctions ;
@@ -175,7 +220,7 @@ private static void RealMain(Config config, bool verbose) {
175
220
action . Writer . WriteStruct ( new Ordinal { Value = idx } ) ;
176
221
{
177
222
var strscache = new List < uint > ( ) ;
178
- var baseoff = ( uint ) file . Position ;
223
+ var baseoff = ( uint ) file . Position ;
179
224
var baserva = VirtualEnd + expdirlength + dllnamelength + functionslength + ordinalslength ;
180
225
foreach ( var ( name , _) in sorted ) {
181
226
strscache . Add ( ( uint ) file . Position - baseoff + baserva ) ;
0 commit comments