1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
//
// Mono.ILASM.CodeGen.cs
//
// Author(s):
// Sergey Chaban (serge@wildwestsoftware.com)
// Jackson Harper (Jackson@LatitudeGeo.com)
//
// (C) Sergey Chaban
// (C) 2003 Jackson Harper, All rights reserved
//
using PEAPI;
using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using System.Security;
namespace Mono.ILASM {
public class CodeGen {
private PEFile pefile;
private string assembly_name;
private ExternAssembly current_assemblyref;
private ExternModule current_moduleref;
private string current_namespace;
private TypeDef current_typedef;
private MethodDef current_methoddef;
private ArrayList typedef_stack;
private int typedef_stack_top;
private SymbolWriter symwriter;
private ICustomAttrTarget current_customattrtarget;
private IDeclSecurityTarget current_declsectarget;
private PEAPI.NativeType current_field_native_type;
private byte [] assembly_public_key;
private int assembly_major_version;
private int assembly_minor_version;
private int assembly_build_version;
private int assembly_revision_version;
private string assembly_locale;
private int assembly_hash_algorithm;
private ArrayList assembly_custom_attributes;
private DeclSecurity assembly_declsec;
private TypeManager type_manager;
private ExternTable extern_table;
private Hashtable global_field_table;
private Hashtable global_method_table;
private Hashtable global_methodref_table;
private Hashtable global_fieldref_table;
private ArrayList data_list;
private FileRef file_ref;
private ArrayList manifestResources;
private Hashtable typeref_table;
private ArrayList defcont_list;
private int sub_system;
private int cor_flags;
private long image_base;
private long stack_reserve;
private string output_file;
private string debug_file;
private bool is_dll;
private bool is_assembly;
private bool entry_point;
private Module this_module;
public CodeGen (string output_file, bool is_dll, bool is_assembly,
bool debugging_info)
{
this.output_file = output_file;
this.is_dll = is_dll;
this.is_assembly = is_assembly;
if (debugging_info)
symwriter = new SymbolWriter (CreateDebugFile (output_file));
type_manager = new TypeManager (this);
extern_table = new ExternTable ();
typedef_stack = new ArrayList ();
typedef_stack_top = 0;
global_field_table = new Hashtable ();
global_method_table = new Hashtable ();
data_list = new ArrayList ();
defcont_list = new ArrayList ();
sub_system = -1;
cor_flags = -1;
image_base = -1;
stack_reserve = -1;
entry_point = false;
this_module = null;
}
private string CreateDebugFile (string output_file)
{
int ext_index = output_file.LastIndexOf ('.');
if (ext_index == -1)
ext_index = output_file.Length;
return String.Format ("{0}.{1}", output_file.Substring (0, ext_index),
"mdb");
}
public PEFile PEFile {
get { return pefile; }
}
public SymbolWriter SymbolWriter {
get { return symwriter; }
}
public string CurrentNameSpace {
get { return current_namespace; }
set { current_namespace = value; }
}
public TypeDef CurrentTypeDef {
get { return current_typedef; }
}
public MethodDef CurrentMethodDef {
get { return current_methoddef; }
}
public ExternAssembly CurrentAssemblyRef {
get { return current_assemblyref; }
}
public ExternModule CurrentModuleRef {
get { return current_moduleref; }
}
public ICustomAttrTarget CurrentCustomAttrTarget {
get { return current_customattrtarget; }
set { current_customattrtarget = value; }
}
public IDeclSecurityTarget CurrentDeclSecurityTarget {
get { return current_declsectarget; }
set { current_declsectarget = value; }
}
public ExternTable ExternTable {
get { return extern_table; }
}
public TypeManager TypeManager {
get { return type_manager; }
}
public bool HasEntryPoint {
get { return entry_point; }
set {
/* if (!value) error: unsetting entrypoint ? */
if (entry_point)
Report.Error ("Multiple .entrypoint declarations.");
entry_point = value;
}
}
public TypeRef GetTypeRef (string name)
{
TypeRef tr = null;
if (typeref_table == null)
typeref_table = new Hashtable ();
else
tr = typeref_table [name] as TypeRef;
if (tr == null) {
tr = new TypeRef (name, false, null);
typeref_table [name] = tr;
}
return tr;
}
public GlobalMethodRef GetGlobalMethodRef (BaseTypeRef ret_type, PEAPI.CallConv call_conv,
string name, BaseTypeRef[] param, int gen_param_count)
{
string key = MethodDef.CreateSignature (ret_type, name, param, gen_param_count);
GlobalMethodRef methref = null;
if (global_methodref_table == null)
global_methodref_table = new Hashtable ();
else
methref = (GlobalMethodRef) global_methodref_table [key];
if (methref == null) {
methref = new GlobalMethodRef (ret_type, call_conv, name, param, gen_param_count);
global_methodref_table [key] = methref;
}
return methref;
}
public GlobalFieldRef GetGlobalFieldRef (BaseTypeRef ret_type, string name)
{
string key = ret_type.FullName + name;
GlobalFieldRef fieldref = null;
if (global_fieldref_table == null)
global_fieldref_table = new Hashtable ();
else
fieldref = (GlobalFieldRef) global_fieldref_table [key];
if (fieldref == null) {
fieldref = new GlobalFieldRef (ret_type, name);
global_fieldref_table [key] = fieldref;
}
return fieldref;
}
public void SetSubSystem (int sub_system)
{
this.sub_system = sub_system;
}
public void SetCorFlags (int cor_flags)
{
this.cor_flags = cor_flags;
}
public void SetImageBase (long image_base)
{
this.image_base = image_base;
}
public void SetStackReserve (long stack_reserve)
{
this.stack_reserve = stack_reserve;
}
public void SetAssemblyName (string name)
{
if (assembly_name != null && assembly_name != name)
Report.Error ("Multiple assembly declarations");
assembly_name = name;
if (assembly_name != "mscorlib")
ExternTable.AddCorlib ();
}
public void SetModuleName (string module_name)
{
this_module = new Module (module_name);
CurrentCustomAttrTarget = this_module;
}
public void SetFileRef (FileRef file_ref)
{
this.file_ref = file_ref;
}
public bool IsThisAssembly (string name)
{
return (name == assembly_name);
}
public Module ThisModule {
get { return this_module; }
}
public bool IsThisModule (string name)
{
return (this_module != null && name == this_module.Name);
}
public void BeginSourceFile (string name)
{
if (symwriter != null)
symwriter.BeginSourceFile (name);
}
public void EndSourceFile ()
{
if (symwriter != null)
symwriter.EndSourceFile ();
}
public void BeginTypeDef (TypeAttr attr, string name, BaseClassRef parent,
ArrayList impl_list, Location location, GenericParameters gen_params)
{
TypeDef outer = null;
string cache_name = CacheName (name);
if (typedef_stack_top > 0) {
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < typedef_stack_top; i++){
outer = (TypeDef) typedef_stack [i];
if (i == 0)
/* Use FullName for outermost class to get the
namespace also */
sb.Append (outer.FullName);
else
sb.Append (outer.Name);
sb.Append ("/");
}
sb.Append (name);
cache_name = sb.ToString ();
}
TypeDef typedef = type_manager[cache_name];
if (typedef != null) {
// Class head is allready defined, we are just reopening the class
current_customattrtarget = current_typedef = typedef;
current_declsectarget = typedef;
typedef_stack.Add (current_typedef);
typedef_stack_top++;
return;
}
typedef = new TypeDef (attr, current_namespace,
name, parent, impl_list, location, gen_params, outer);
type_manager[cache_name] = typedef;
current_customattrtarget = current_typedef = typedef;
current_declsectarget = typedef;
typedef_stack.Add (typedef);
typedef_stack_top++;
}
public void AddFieldMarshalInfo (PEAPI.NativeType native_type)
{
current_field_native_type = native_type;
}
public void AddFieldDef (FieldDef fielddef)
{
if (current_field_native_type != null) {
fielddef.AddMarshalInfo (current_field_native_type);
current_field_native_type = null;
}
if (current_typedef != null) {
current_typedef.AddFieldDef (fielddef);
} else {
global_field_table.Add (
new DictionaryEntry (fielddef.Name, fielddef.Type.FullName),
fielddef);
}
}
public void AddDataDef (DataDef datadef)
{
data_list.Add (datadef);
}
public void AddManifestResource (ManifestResource mr)
{
if (manifestResources == null)
manifestResources = new ArrayList ();
manifestResources.Add (mr);
}
public PEAPI.DataConstant GetDataConst (string name)
{
foreach (DataDef def in data_list) {
if (def.Name == name)
return (DataConstant) def.PeapiConstant;
}
return null;
}
public void BeginMethodDef (MethodDef methoddef)
{
if (current_typedef != null) {
current_typedef.AddMethodDef (methoddef);
} else {
global_method_table.Add (methoddef.Signature,
methoddef);
}
current_customattrtarget = current_methoddef = methoddef;
current_declsectarget = methoddef;
}
public void EndMethodDef (Location location)
{
if (symwriter != null)
symwriter.EndMethod (location);
current_methoddef = null;
}
public void EndTypeDef ()
{
typedef_stack_top--;
typedef_stack.RemoveAt (typedef_stack_top);
if (typedef_stack_top > 0)
current_typedef = (TypeDef) typedef_stack [typedef_stack_top-1];
else
current_typedef = null;
}
public void BeginAssemblyRef (string name, AssemblyName asmb_name)
{
current_customattrtarget = current_assemblyref = ExternTable.AddAssembly (name, asmb_name);
current_declsectarget = current_assemblyref;
}
public void EndAssemblyRef ()
{
current_assemblyref = null;
current_customattrtarget = null;
current_declsectarget = null;
}
public void AddToDefineContentsList (TypeDef typedef)
{
defcont_list.Add (typedef);
}
public void SetAssemblyPublicKey (byte [] public_key)
{
assembly_public_key = public_key;
}
public void SetAssemblyVersion (int major, int minor, int build, int revision)
{
assembly_major_version = major;
assembly_minor_version = minor;
assembly_build_version = build;
assembly_revision_version = revision;
}
public void SetAssemblyLocale (string locale)
{
assembly_locale = locale;
}
public void SetAssemblyHashAlgorithm (int algorithm)
{
assembly_hash_algorithm = algorithm;
}
public void AddAssemblyCustomAttribute (CustomAttr attribute)
{
if (assembly_custom_attributes == null)
assembly_custom_attributes = new ArrayList ();
assembly_custom_attributes.Add (attribute);
}
public void AddAssemblyPermission (PEAPI.SecurityAction sec_action, object perm)
{
if (assembly_declsec == null)
assembly_declsec = new DeclSecurity ();
PermissionSet ps = perm as PermissionSet;
if (ps == null)
assembly_declsec.AddPermission (sec_action, (IPermission) perm);
else
assembly_declsec.AddPermissionSet (sec_action, ps);
}
public void AddPermission (PEAPI.SecurityAction sec_action, object perm)
{
if (CurrentDeclSecurityTarget == null)
return;
PermissionSet ps = perm as PermissionSet;
if (ps == null)
CurrentDeclSecurityTarget.AddPermission (sec_action, (IPermission) perm);
else
CurrentDeclSecurityTarget.AddPermissionSet (sec_action, ps);
}
public void Write ()
{
FileStream out_stream = null;
try {
if (ThisModule == null)
this_module = new Module (Path.GetFileName (output_file));
out_stream = new FileStream (output_file, FileMode.Create, FileAccess.Write);
pefile = new PEFile (assembly_name, ThisModule.Name, is_dll, is_assembly, null, out_stream);
PEAPI.Assembly asmb = pefile.GetThisAssembly ();
ThisModule.PeapiModule = pefile.GetThisModule ();
if (file_ref != null)
file_ref.Resolve (this);
extern_table.Resolve (this);
type_manager.DefineAll ();
if (manifestResources != null) {
foreach (ManifestResource mr in manifestResources)
pefile.AddManifestResource (mr);
}
foreach (FieldDef fielddef in global_field_table.Values) {
fielddef.Define (this);
}
foreach (MethodDef methoddef in global_method_table.Values) {
methoddef.Define (this);
}
foreach (TypeDef typedef in defcont_list) {
typedef.DefineContents (this);
}
if (assembly_custom_attributes != null) {
foreach (CustomAttr cattr in assembly_custom_attributes)
cattr.AddTo (this, asmb);
}
ThisModule.Resolve (this, pefile.GetThisModule ());
if (assembly_declsec != null)
assembly_declsec.AddTo (this, asmb);
if (sub_system != -1)
pefile.SetSubSystem ((PEAPI.SubSystem) sub_system);
if (cor_flags != -1)
pefile.SetCorFlags (cor_flags);
if (stack_reserve != -1)
pefile.SetStackReserve (stack_reserve);
asmb.AddAssemblyInfo(assembly_major_version,
assembly_minor_version, assembly_build_version,
assembly_revision_version, assembly_public_key,
(uint) assembly_hash_algorithm, assembly_locale);
pefile.WritePEFile ();
if (symwriter != null) {
Guid guid = pefile.GetThisModule ().Guid;
symwriter.Write (guid);
}
} finally {
if (out_stream != null)
out_stream.Close ();
}
}
public PEAPI.Method ResolveMethod (string signature)
{
MethodDef methoddef = (MethodDef) global_method_table[signature];
if (methoddef == null)
Report.Error ("Unable to resolve global method : " + signature);
return methoddef.Resolve (this);
}
public PEAPI.Method ResolveVarargMethod (string signature,
CodeGen code_gen, PEAPI.Type[] opt)
{
MethodDef methoddef = (MethodDef) global_method_table[signature];
methoddef.Resolve (code_gen);
return methoddef.GetVarargSig (opt);
}
public PEAPI.Field ResolveField (string name, string type_name)
{
FieldDef fielddef = (FieldDef) global_field_table[new DictionaryEntry (name, type_name)];
if (fielddef == null)
Report.Error (String.Format ("Unable to resolve global field : {0} {1}", type_name, name));
return fielddef.Resolve (this);
}
private string CacheName (string name)
{
if (current_namespace == null ||
current_namespace == String.Empty)
return name;
return current_namespace + "." + name;
}
}
}
|