2
0

MDDefScopeElems.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * PERWAPI - An API for Reading and Writing PE Files
  3. *
  4. * Copyright (c) Diane Corney, Queensland University of Technology, 2004.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the PERWAPI Copyright as included with this
  8. * distribution in the file PERWAPIcopyright.rtf.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY as is explained in the copyright notice.
  12. *
  13. * The author may be contacted at d.corney@qut.edu.au
  14. *
  15. * Version Date: 26/01/07
  16. */
  17. using System;
  18. using System.IO;
  19. using System.Collections;
  20. using System.Security.Cryptography;
  21. namespace QUT.PERWAPI
  22. {
  23. /**************************************************************************/
  24. /// <summary>
  25. /// A scope for definitions
  26. /// </summary>
  27. public abstract class DefiningScope : ResolutionScope
  28. {
  29. /*-------------------- Constructors ---------------------------------*/
  30. internal DefiningScope(string name)
  31. : base(name)
  32. {
  33. readAsDef = true;
  34. }
  35. internal override void AddToClassList(Class aClass)
  36. {
  37. ((ClassDef)aClass).SetScope((PEFile)this);
  38. classes.Add(aClass);
  39. }
  40. }
  41. /**************************************************************************/
  42. /// <summary>
  43. /// Descriptor for an assembly (.assembly)
  44. /// </summary>
  45. public class Assembly : DefiningScope
  46. {
  47. //internal static Hashtable Assemblies = new Hashtable();
  48. ushort majorVer, minorVer, buildNo, revisionNo;
  49. uint flags;
  50. HashAlgorithmType hashAlgId = HashAlgorithmType.None;
  51. uint keyIx = 0, cultIx = 0;
  52. byte[] publicKey;
  53. string culture;
  54. internal AssemblyRef refOf;
  55. ArrayList security;
  56. internal PEFile pefile;
  57. /*-------------------- Constructors ---------------------------------*/
  58. internal Assembly(string name, PEFile pefile)
  59. : base(name)
  60. {
  61. this.pefile = pefile;
  62. tabIx = MDTable.Assembly;
  63. }
  64. internal Assembly(string name, HashAlgorithmType hashAlgId, ushort majVer,
  65. ushort minVer, ushort bldNo, ushort revNo, uint flags, byte[] pKey,
  66. string cult, PEFile pefile)
  67. : base(name)
  68. {
  69. this.hashAlgId = hashAlgId;
  70. this.majorVer = majVer;
  71. this.minorVer = minVer;
  72. this.buildNo = bldNo;
  73. this.revisionNo = revNo;
  74. this.flags = flags;
  75. this.publicKey = pKey;
  76. this.culture = cult;
  77. tabIx = MDTable.Assembly;
  78. }
  79. internal static AssemblyRef ReadAssemblyRef(PEReader buff)
  80. {
  81. buff.SetElementPosition(MDTable.Assembly, 1);
  82. HashAlgorithmType hAlg = (HashAlgorithmType)buff.ReadUInt32();
  83. ushort majVer = buff.ReadUInt16();
  84. ushort minVer = buff.ReadUInt16();
  85. ushort bldNo = buff.ReadUInt16();
  86. ushort revNo = buff.ReadUInt16();
  87. uint flags = buff.ReadUInt32();
  88. byte[] pKey = buff.GetBlob();
  89. string name = buff.GetString();
  90. string cult = buff.GetString();
  91. AssemblyRef assemRef = null;
  92. if (name.ToLower() == "mscorlib")
  93. {
  94. assemRef = MSCorLib.mscorlib;
  95. assemRef.AddVersionInfo(majVer, minVer, bldNo, revNo);
  96. if (pKey.Length > 8) assemRef.AddKey(pKey);
  97. else assemRef.AddKeyToken(pKey);
  98. assemRef.AddCulture(cult);
  99. assemRef.SetFlags(flags);
  100. }
  101. else
  102. {
  103. assemRef = new AssemblyRef(name, majVer, minVer, bldNo, revNo, flags, pKey, cult, null);
  104. }
  105. //AssemblyRef assemRef = new AssemblyRef(name,majVer,minVer,bldNo,revNo,flags,pKey,cult,null);
  106. assemRef.ReadAsDef();
  107. return assemRef;
  108. }
  109. internal static void Read(PEReader buff, TableRow[] table, PEFile pefile)
  110. {
  111. for (int i = 0; i < table.Length; i++)
  112. {
  113. HashAlgorithmType hAlg = (HashAlgorithmType)buff.ReadUInt32();
  114. ushort majVer = buff.ReadUInt16();
  115. ushort minVer = buff.ReadUInt16();
  116. ushort bldNo = buff.ReadUInt16();
  117. ushort revNo = buff.ReadUInt16();
  118. uint flags = buff.ReadUInt32();
  119. byte[] pKey = buff.GetBlob();
  120. string name = buff.GetString();
  121. string cult = buff.GetString();
  122. table[i] = new Assembly(name, hAlg, majVer, minVer, bldNo, revNo, flags, pKey, cult, pefile);
  123. }
  124. }
  125. /*------------------------- public set and get methods --------------------------*/
  126. /// <summary>
  127. /// Add details about an assembly
  128. /// </summary>
  129. /// <param name="majVer">Major Version</param>
  130. /// <param name="minVer">Minor Version</param>
  131. /// <param name="bldNo">Build Number</param>
  132. /// <param name="revNo">Revision Number</param>
  133. /// <param name="key">Hash Key</param>
  134. /// <param name="hash">Hash Algorithm</param>
  135. /// <param name="cult">Culture</param>
  136. public void AddAssemblyInfo(int majVer, int minVer, int bldNo, int revNo,
  137. byte[] key, HashAlgorithmType hash, string cult)
  138. {
  139. majorVer = (ushort)majVer;
  140. minorVer = (ushort)minVer;
  141. buildNo = (ushort)bldNo;
  142. revisionNo = (ushort)revNo;
  143. hashAlgId = hash;
  144. publicKey = key;
  145. culture = cult;
  146. }
  147. /// <summary>
  148. /// Get the major version number for this Assembly
  149. /// </summary>
  150. /// <returns>major version number</returns>
  151. public int MajorVersion() { return majorVer; }
  152. /// <summary>
  153. /// Get the minor version number for this Assembly
  154. /// </summary>
  155. /// <returns>minor version number</returns>
  156. public int MinorVersion() { return minorVer; }
  157. /// <summary>
  158. /// Get the build number for this Assembly
  159. /// </summary>
  160. /// <returns>build number</returns>
  161. public int BuildNumber() { return buildNo; }
  162. /// <summary>
  163. /// Get the revision number for this Assembly
  164. /// </summary>
  165. /// <returns>revision number</returns>
  166. public int RevisionNumber() { return revisionNo; }
  167. /// <summary>
  168. /// Get the public key for this Assembly
  169. /// </summary>
  170. /// <returns>public key bytes</returns>
  171. public byte[] Key() { return publicKey; }
  172. /// <summary>
  173. /// Get the public key token for this assembly
  174. /// or null if the assembly is not signed
  175. /// </summary>
  176. /// <returns>Key token or null</returns>
  177. public byte[] KeyTokenBytes()
  178. {
  179. if (this.publicKey != null &&
  180. this.publicKey.Length != 0 &&
  181. this.hashAlgId == HashAlgorithmType.SHA1)
  182. {
  183. int ix, ofst;
  184. byte[] token = new byte[8];
  185. HashAlgorithm sha = new SHA1CryptoServiceProvider();
  186. byte[] hash = sha.ComputeHash(publicKey);
  187. for (ix = 0, ofst = hash.Length - 8; ix < 8; ix++)
  188. token[ix] = hash[ix + ofst];
  189. return token;
  190. }
  191. else
  192. return null;
  193. }
  194. /// <summary>
  195. /// Returns Public Key Token as Int64
  196. /// </summary>
  197. /// <returns></returns>
  198. public long KeyTokenAsLong()
  199. {
  200. byte[] token = KeyTokenBytes();
  201. return (token == null ? 0 : BitConverter.ToInt64(token, 0));
  202. }
  203. /// <summary>
  204. /// Get the type of the hash algorithm for this Assembly
  205. /// </summary>
  206. /// <returns>hash algorithm type</returns>
  207. public HashAlgorithmType HashAlgorithm() { return hashAlgId; }
  208. /// <summary>
  209. /// Get the culture information for this Assembly
  210. /// </summary>
  211. /// <returns>culture string</returns>
  212. public string Culture() { return culture; }
  213. /// <summary>
  214. /// Add some security action(s) to this Assembly
  215. /// </summary>
  216. public void AddSecurity(SecurityAction act, byte[] permissionSet)
  217. {
  218. AddSecurity(new DeclSecurity(this, act, permissionSet));
  219. // securityActions = permissionSet;
  220. }
  221. /// <summary>
  222. /// Get the security information for this assembly
  223. /// </summary>
  224. /// <returns>security information</returns>
  225. public DeclSecurity[] GetSecurity()
  226. {
  227. if (security == null) return null;
  228. return (DeclSecurity[])security.ToArray(typeof(DeclSecurity));
  229. }
  230. /// <summary>
  231. /// Check if this assembly has security information
  232. /// </summary>
  233. public bool HasSecurity() { return security != null; }
  234. /// <summary>
  235. /// Set the attributes for this assembly
  236. /// </summary>
  237. /// <param name="aa">assembly attribute</param>
  238. public void SetAssemblyAttr(AssemAttr aa)
  239. {
  240. flags = (uint)aa;
  241. }
  242. /// <summary>
  243. /// Add an attribute for this assembly
  244. /// </summary>
  245. /// <param name="aa">assembly attribute</param>
  246. public void AddAssemblyAttr(AssemAttr aa)
  247. {
  248. flags |= (uint)aa;
  249. }
  250. /// <summary>
  251. /// Get the attributes of this assembly
  252. /// </summary>
  253. /// <returns>assembly attributes</returns>
  254. public AssemAttr AssemblyAttributes()
  255. {
  256. return (AssemAttr)flags;
  257. }
  258. /// <summary>
  259. /// Make an AssemblyRef descriptor for this Assembly
  260. /// </summary>
  261. /// <returns>AssemblyRef descriptor for this Assembly</returns>
  262. public AssemblyRef MakeRefOf()
  263. {
  264. if (refOf == null)
  265. {
  266. refOf = new AssemblyRef(name, majorVer, minorVer, buildNo, revisionNo,
  267. flags, publicKey, culture, null);
  268. }
  269. return refOf;
  270. }
  271. /*------------------------ internal functions ----------------------------*/
  272. internal void AddSecurity(DeclSecurity sec)
  273. {
  274. if (security == null) security = new ArrayList();
  275. security.Add(sec);
  276. }
  277. internal static uint Size(MetaData md)
  278. {
  279. return 16 + md.BlobIndexSize() + 2 * md.StringsIndexSize();
  280. }
  281. internal sealed override void BuildTables(MetaDataOut md)
  282. {
  283. md.AddToTable(MDTable.Assembly, this);
  284. nameIx = md.AddToStringsHeap(name);
  285. cultIx = md.AddToStringsHeap(culture);
  286. keyIx = md.AddToBlobHeap(publicKey);
  287. if (security != null)
  288. {
  289. for (int i = 0; i < security.Count; i++)
  290. {
  291. ((DeclSecurity)security[i]).BuildMDTables(md);
  292. }
  293. }
  294. }
  295. internal sealed override void Write(PEWriter output)
  296. {
  297. //Console.WriteLine("Writing assembly element with nameIx of " + nameIx + " at file offset " + output.Seek(0,SeekOrigin.Current));
  298. output.Write((uint)hashAlgId);
  299. output.Write(majorVer);
  300. output.Write(minorVer);
  301. output.Write(buildNo);
  302. output.Write(revisionNo);
  303. output.Write(flags);
  304. output.BlobIndex(keyIx);
  305. output.StringsIndex(nameIx);
  306. output.StringsIndex(cultIx);
  307. }
  308. internal override void Write(CILWriter output)
  309. {
  310. output.WriteLine(".assembly " + name + " { }");
  311. }
  312. internal sealed override uint GetCodedIx(CIx code)
  313. {
  314. switch (code)
  315. {
  316. case (CIx.HasCustomAttr): return 14;
  317. case (CIx.HasDeclSecurity): return 2;
  318. }
  319. return 0;
  320. }
  321. }
  322. /**************************************************************************/
  323. /// <summary>
  324. /// Descriptor for a module
  325. /// </summary>
  326. public abstract class Module : DefiningScope
  327. {
  328. Guid mvid;
  329. uint mvidIx = 0;
  330. internal ModuleRef refOf;
  331. /// <summary>
  332. /// The default class "Module" for globals
  333. /// </summary>
  334. protected ClassDef defaultClass;
  335. /// <summary>
  336. /// Is this module a .dll or .exe
  337. /// </summary>
  338. //protected bool isDLL;
  339. /// <summary>
  340. /// Is this module mscorlib.dll
  341. /// </summary>
  342. protected bool ismscorlib = false;
  343. /// <summary>
  344. /// Managed resources for this module
  345. /// </summary>
  346. protected ArrayList resources = new ArrayList();
  347. /*-------------------- Constructors ---------------------------------*/
  348. internal Module(string mName)
  349. : base(GetBaseName(mName))
  350. {
  351. mvid = Guid.NewGuid();
  352. //isDLL = name.EndsWith(".dll") || name.EndsWith(".DLL");
  353. defaultClass = new ClassDef((PEFile)this, TypeAttr.Private, "", "<Module>");
  354. defaultClass.MakeSpecial();
  355. tabIx = MDTable.Module;
  356. ismscorlib = name.ToLower() == "mscorlib.dll";
  357. if (Diag.DiagOn) Console.WriteLine("Module name = " + name);
  358. }
  359. internal void Read(PEReader buff)
  360. {
  361. buff.ReadZeros(2);
  362. name = buff.GetString();
  363. mvid = buff.GetGUID();
  364. uint junk = buff.GetGUIDIx();
  365. junk = buff.GetGUIDIx();
  366. if (Diag.DiagOn) Console.WriteLine("Reading module with name " + name + " and Mvid = " + mvid);
  367. ismscorlib = name.ToLower() == "mscorlib.dll";
  368. }
  369. internal static ModuleRef ReadModuleRef(PEReader buff)
  370. {
  371. buff.ReadZeros(2);
  372. string name = buff.GetString();
  373. uint junk = buff.GetGUIDIx();
  374. junk = buff.GetGUIDIx();
  375. junk = buff.GetGUIDIx();
  376. ModuleRef mRef = new ModuleRef(new ModuleFile(name, null));
  377. mRef.ReadAsDef();
  378. return mRef;
  379. }
  380. /*------------------------- public set and get methods --------------------------*/
  381. /// <summary>
  382. /// Add a class to this Module
  383. /// If this class already exists, throw an exception
  384. /// </summary>
  385. /// <param name="attrSet">attributes of this class</param>
  386. /// <param name="nsName">name space name</param>
  387. /// <param name="name">class name</param>
  388. /// <returns>a descriptor for this new class</returns>
  389. public ClassDef AddClass(TypeAttr attrSet, string nsName, string name)
  390. {
  391. ClassDef aClass = GetClass(nsName, name);
  392. if (aClass != null)
  393. throw new DescriptorException("Class " + aClass.NameString());
  394. aClass = new ClassDef((PEFile)this, attrSet, nsName, name);
  395. classes.Add(aClass);
  396. return aClass;
  397. }
  398. /// <summary>
  399. /// Add a class which extends System.ValueType to this Module
  400. /// If this class already exists, throw an exception
  401. /// </summary>
  402. /// <param name="attrSet">attributes of this class</param>
  403. /// <param name="nsName">name space name</param>
  404. /// <param name="name">class name</param>
  405. /// <returns>a descriptor for this new class</returns>
  406. public ClassDef AddValueClass(TypeAttr attrSet, string nsName, string name)
  407. {
  408. ClassDef aClass = AddClass(attrSet, nsName, name);
  409. aClass.SuperType = MSCorLib.mscorlib.ValueType();
  410. aClass.MakeValueClass();
  411. return aClass;
  412. }
  413. /// <summary>
  414. /// Add a class to this PE File
  415. /// </summary>
  416. /// <param name="attrSet">attributes of this class</param>
  417. /// <param name="nsName">name space name</param>
  418. /// <param name="name">class name</param>
  419. /// <param name="superType">super type of this class (extends)</param>
  420. /// <returns>a descriptor for this new class</returns>
  421. public ClassDef AddClass(TypeAttr attrSet, string nsName, string name, Class superType)
  422. {
  423. ClassDef aClass = AddClass(attrSet, nsName, name);
  424. aClass.SuperType = superType;
  425. return aClass;
  426. }
  427. /// <summary>
  428. /// Add a class to this module
  429. /// If this class already exists, throw an exception
  430. /// </summary>
  431. /// <param name="aClass">The class to be added</param>
  432. public void AddClass(ClassDef aClass)
  433. {
  434. ClassDef eClass = GetClass(aClass.NameSpace(), aClass.Name());
  435. if (eClass != null)
  436. throw new DescriptorException("Class " + aClass.NameString());
  437. classes.Add(aClass);
  438. // MERGE change Refs to Defs here, fix this
  439. aClass.SetScope((PEFile)this);
  440. }
  441. /// <summary>
  442. /// Get a class of this module, if no class exists, return null
  443. /// </summary>
  444. /// <param name="name">The name of the class to get</param>
  445. /// <returns>ClassDef for name or null</returns>
  446. public ClassDef GetClass(string name)
  447. {
  448. return (ClassDef)GetClass(null, name, false);
  449. }
  450. /// <summary>
  451. /// Get a class of this module, if no class exists, return null
  452. /// </summary>
  453. /// <param name="nsName">The namespace of the class</param>
  454. /// <param name="name">The name of the class to get</param>
  455. /// <returns>ClassDef for nsName.name or null</returns>
  456. public ClassDef GetClass(string nsName, string name)
  457. {
  458. return (ClassDef)GetClass(nsName, name, true);
  459. }
  460. /// <summary>
  461. /// Get all the classes of this module
  462. /// </summary>
  463. /// <returns>An array containing a ClassDef for each class of this module</returns>
  464. public ClassDef[] GetClasses()
  465. {
  466. return (ClassDef[])classes.ToArray(typeof(ClassDef));
  467. }
  468. /// <summary>
  469. /// Add a "global" method to this module
  470. /// </summary>
  471. /// <param name="name">method name</param>
  472. /// <param name="retType">return type</param>
  473. /// <param name="pars">method parameters</param>
  474. /// <returns>a descriptor for this new "global" method</returns>
  475. public MethodDef AddMethod(string name, Type retType, Param[] pars)
  476. {
  477. MethodDef newMeth = defaultClass.AddMethod(name, retType, pars);
  478. return newMeth;
  479. }
  480. /// <summary>
  481. /// Add a "global" method to this module
  482. /// </summary>
  483. /// <param name="name">method name</param>
  484. /// <param name="genPars">generic parameters</param>
  485. /// <param name="retType">return type</param>
  486. /// <param name="pars">method parameters</param>
  487. /// <returns>a descriptor for this new "global" method</returns>
  488. public MethodDef AddMethod(string name, GenericParam[] genPars, Type retType, Param[] pars)
  489. {
  490. MethodDef newMeth = defaultClass.AddMethod(name, genPars, retType, pars);
  491. return newMeth;
  492. }
  493. /// <summary>
  494. /// Add a "global" method to this module
  495. /// </summary>
  496. /// <param name="mAtts">method attributes</param>
  497. /// <param name="iAtts">method implementation attributes</param>
  498. /// <param name="name">method name</param>
  499. /// <param name="retType">return type</param>
  500. /// <param name="pars">method parameters</param>
  501. /// <returns>a descriptor for this new "global" method</returns>
  502. public MethodDef AddMethod(MethAttr mAtts, ImplAttr iAtts, string name, Type retType, Param[] pars)
  503. {
  504. MethodDef newMeth = defaultClass.AddMethod(mAtts, iAtts, name, retType, pars);
  505. return newMeth;
  506. }
  507. /// <summary>
  508. /// Add a "global" method to this module
  509. /// </summary>
  510. /// <param name="mAtts">method attributes</param>
  511. /// <param name="iAtts">method implementation attributes</param>
  512. /// <param name="name">method name</param>
  513. /// <param name="genPars">generic parameters</param>
  514. /// <param name="retType">return type</param>
  515. /// <param name="pars">method parameters</param>
  516. /// <returns>a descriptor for this new "global" method</returns>
  517. public MethodDef AddMethod(MethAttr mAtts, ImplAttr iAtts, string name, GenericParam[] genPars, Type retType, Param[] pars)
  518. {
  519. MethodDef newMeth = defaultClass.AddMethod(mAtts, iAtts, name, genPars, retType, pars);
  520. return newMeth;
  521. }
  522. /// <summary>
  523. /// Add a "global" method to this module
  524. /// </summary>
  525. /// <param name="meth">The method to be added</param>
  526. public void AddMethod(MethodDef meth)
  527. {
  528. defaultClass.AddMethod(meth);
  529. }
  530. /// <summary>
  531. /// Get a method of this module, if it exists
  532. /// </summary>
  533. /// <param name="name">The name of the method to get</param>
  534. /// <returns>MethodDef for name, or null if one does not exist</returns>
  535. public MethodDef GetMethod(string name)
  536. {
  537. return defaultClass.GetMethod(name);
  538. }
  539. /// <summary>
  540. /// Get all the methods of this module with a specified name
  541. /// </summary>
  542. /// <param name="name">The name of the method(s)</param>
  543. /// <returns>An array of all the methods of this module called "name" </returns>
  544. public MethodDef[] GetMethods(string name)
  545. {
  546. return defaultClass.GetMethods(name);
  547. }
  548. /// <summary>
  549. /// Get a method of this module, if it exists
  550. /// </summary>
  551. /// <param name="name">The name of the method to get</param>
  552. /// <param name="parTypes">The signature of the method</param>
  553. /// <returns>MethodDef for name(parTypes), or null if one does not exist</returns>
  554. public MethodDef GetMethod(string name, Type[] parTypes)
  555. {
  556. return defaultClass.GetMethod(name, parTypes);
  557. }
  558. /// <summary>
  559. /// Get all the methods of this module
  560. /// </summary>
  561. /// <returns>An array of all the methods of this module</returns>
  562. public MethodDef[] GetMethods()
  563. {
  564. return defaultClass.GetMethods();
  565. }
  566. /// <summary>
  567. /// Delete a method from this module
  568. /// </summary>
  569. /// <param name="meth">The method to be deleted</param>
  570. public void RemoveMethod(MethodDef meth)
  571. {
  572. defaultClass.RemoveMethod(meth);
  573. }
  574. /// <summary>
  575. /// Delete a method from this module
  576. /// </summary>
  577. /// <param name="name">The name of the method to be deleted</param>
  578. public void RemoveMethod(string name)
  579. {
  580. defaultClass.RemoveMethod(name);
  581. }
  582. /// <summary>
  583. /// Delete a method from this module
  584. /// </summary>
  585. /// <param name="name">The name of the method to be deleted</param>
  586. /// <param name="parTypes">The signature of the method to be deleted</param>
  587. public void RemoveMethod(string name, Type[] parTypes)
  588. {
  589. defaultClass.RemoveMethod(name, parTypes);
  590. }
  591. /// <summary>
  592. /// Delete a method from this module
  593. /// </summary>
  594. /// <param name="ix">The index of the method (in the method array
  595. /// returned by GetMethods()) to be deleted</param>
  596. public void RemoveMethod(int ix)
  597. {
  598. defaultClass.RemoveMethod(ix);
  599. }
  600. /// <summary>
  601. /// Add a "global" field to this module
  602. /// </summary>
  603. /// <param name="name">field name</param>
  604. /// <param name="fType">field type</param>
  605. /// <returns>a descriptor for this new "global" field</returns>
  606. public FieldDef AddField(string name, Type fType)
  607. {
  608. FieldDef newField = defaultClass.AddField(name, fType);
  609. return newField;
  610. }
  611. /// <summary>
  612. /// Add a "global" field to this module
  613. /// </summary>
  614. /// <param name="attrSet">attributes of this field</param>
  615. /// <param name="name">field name</param>
  616. /// <param name="fType">field type</param>
  617. /// <returns>a descriptor for this new "global" field</returns>
  618. public FieldDef AddField(FieldAttr attrSet, string name, Type fType)
  619. {
  620. FieldDef newField = defaultClass.AddField(attrSet, name, fType);
  621. return newField;
  622. }
  623. /// <summary>
  624. /// Add a "global" field to this module
  625. /// </summary>
  626. /// <param name="fld">The field to be added</param>
  627. public void AddField(FieldDef fld)
  628. {
  629. defaultClass.AddField(fld);
  630. }
  631. /// <summary>
  632. /// Get a field of this module, if it exists
  633. /// </summary>
  634. /// <param name="name">The name of the field</param>
  635. /// <returns>FieldDef for "name", or null if one doesn't exist</returns>
  636. public FieldDef GetField(string name)
  637. {
  638. return defaultClass.GetField(name);
  639. }
  640. /// <summary>
  641. /// Get all the fields of this module
  642. /// </summary>
  643. /// <returns>An array of all the fields of this module</returns>
  644. public FieldDef[] GetFields()
  645. {
  646. return defaultClass.GetFields();
  647. }
  648. /// <summary>
  649. /// Make a ModuleRef for this Module.
  650. /// </summary>
  651. /// <returns>ModuleRef for this Module</returns>
  652. public ModuleRef MakeRefOf(/*bool hasEntryPoint, byte[] hashValue*/)
  653. {
  654. if (refOf == null)
  655. {
  656. refOf = new ModuleRef(name/*,hasEntryPoint,hashValue*/);
  657. refOf.defOf = this;
  658. }/* else { // fix this
  659. if (hasEntryPoint)
  660. refOf.SetEntryPoint();
  661. refOf.SetHash(hashValue);
  662. }*/
  663. return refOf;
  664. }
  665. /// <summary>
  666. /// Set the name for this module
  667. /// </summary>
  668. /// <param name="newName">New module name</param>
  669. public void SetName(string newName)
  670. {
  671. name = newName;
  672. //isDLL = name.EndsWith(".dll") || name.EndsWith(".DLL");
  673. }
  674. public void SetMVid(Guid guid)
  675. {
  676. mvid = guid;
  677. }
  678. public Guid GetMVid()
  679. {
  680. return mvid;
  681. }
  682. /*------------------------- internal functions --------------------------*/
  683. internal bool isMSCorLib() { return ismscorlib; }
  684. internal bool isDefaultClass(ClassDef aClass) { return aClass == defaultClass; }
  685. private static string GetBaseName(string name)
  686. {
  687. // more to this??
  688. if (name.IndexOf("\\") != -1)
  689. name = name.Substring(name.LastIndexOf("\\") + 1);
  690. return name;
  691. }
  692. internal void SetDefaultClass(ClassDef dClass)
  693. {
  694. defaultClass = dClass;
  695. }
  696. internal sealed override void BuildTables(MetaDataOut md)
  697. {
  698. md.AddToTable(MDTable.Module, this);
  699. nameIx = md.AddToStringsHeap(name);
  700. mvidIx = md.AddToGUIDHeap(mvid);
  701. defaultClass.BuildTables(md);
  702. for (int i = 0; i < classes.Count; i++)
  703. {
  704. ((Class)classes[i]).BuildMDTables(md);
  705. }
  706. for (int i = 0; i < resources.Count; i++)
  707. {
  708. ((ManifestResource)resources[i]).BuildMDTables(md);
  709. }
  710. }
  711. internal static uint Size(MetaData md)
  712. {
  713. return 2 + md.StringsIndexSize() + 3 * md.GUIDIndexSize();
  714. }
  715. internal sealed override void Write(PEWriter output)
  716. {
  717. output.Write((short)0);
  718. output.StringsIndex(nameIx);
  719. output.GUIDIndex(mvidIx);
  720. output.GUIDIndex(0);
  721. output.GUIDIndex(0);
  722. }
  723. internal sealed override uint GetCodedIx(CIx code)
  724. {
  725. switch (code)
  726. {
  727. case (CIx.HasCustomAttr): return 7;
  728. case (CIx.ResolutionScope): return 0;
  729. }
  730. return 0;
  731. }
  732. }
  733. }