MDDefScopeElems.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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.hashAlgId == HashAlgorithmType.SHA1)
  181. {
  182. int ix, ofst;
  183. byte[] token = new byte[8];
  184. HashAlgorithm sha = new SHA1CryptoServiceProvider();
  185. byte[] hash = sha.ComputeHash(publicKey);
  186. for (ix = 0, ofst = hash.Length - 8; ix < 8; ix++)
  187. token[ix] = hash[ix + ofst];
  188. return token;
  189. }
  190. else
  191. return null;
  192. }
  193. /// <summary>
  194. /// Returns Public Key Token as Int64
  195. /// </summary>
  196. /// <returns></returns>
  197. public long KeyTokenAsLong()
  198. {
  199. byte[] token = KeyTokenBytes();
  200. return (token == null ? 0 : BitConverter.ToInt64(token, 0));
  201. }
  202. /// <summary>
  203. /// Get the type of the hash algorithm for this Assembly
  204. /// </summary>
  205. /// <returns>hash algorithm type</returns>
  206. public HashAlgorithmType HashAlgorithm() { return hashAlgId; }
  207. /// <summary>
  208. /// Get the culture information for this Assembly
  209. /// </summary>
  210. /// <returns>culture string</returns>
  211. public string Culture() { return culture; }
  212. /// <summary>
  213. /// Add some security action(s) to this Assembly
  214. /// </summary>
  215. public void AddSecurity(SecurityAction act, byte[] permissionSet)
  216. {
  217. AddSecurity(new DeclSecurity(this, act, permissionSet));
  218. // securityActions = permissionSet;
  219. }
  220. /// <summary>
  221. /// Get the security information for this assembly
  222. /// </summary>
  223. /// <returns>security information</returns>
  224. public DeclSecurity[] GetSecurity()
  225. {
  226. if (security == null) return null;
  227. return (DeclSecurity[])security.ToArray(typeof(DeclSecurity));
  228. }
  229. /// <summary>
  230. /// Check if this assembly has security information
  231. /// </summary>
  232. public bool HasSecurity() { return security != null; }
  233. /// <summary>
  234. /// Set the attributes for this assembly
  235. /// </summary>
  236. /// <param name="aa">assembly attribute</param>
  237. public void SetAssemblyAttr(AssemAttr aa)
  238. {
  239. flags = (uint)aa;
  240. }
  241. /// <summary>
  242. /// Add an attribute for this assembly
  243. /// </summary>
  244. /// <param name="aa">assembly attribute</param>
  245. public void AddAssemblyAttr(AssemAttr aa)
  246. {
  247. flags |= (uint)aa;
  248. }
  249. /// <summary>
  250. /// Get the attributes of this assembly
  251. /// </summary>
  252. /// <returns>assembly attributes</returns>
  253. public AssemAttr AssemblyAttributes()
  254. {
  255. return (AssemAttr)flags;
  256. }
  257. /// <summary>
  258. /// Make an AssemblyRef descriptor for this Assembly
  259. /// </summary>
  260. /// <returns>AssemblyRef descriptor for this Assembly</returns>
  261. public AssemblyRef MakeRefOf()
  262. {
  263. if (refOf == null)
  264. {
  265. refOf = new AssemblyRef(name, majorVer, minorVer, buildNo, revisionNo,
  266. flags, publicKey, culture, null);
  267. }
  268. return refOf;
  269. }
  270. /*------------------------ internal functions ----------------------------*/
  271. internal void AddSecurity(DeclSecurity sec)
  272. {
  273. if (security == null) security = new ArrayList();
  274. security.Add(sec);
  275. }
  276. internal static uint Size(MetaData md)
  277. {
  278. return 16 + md.BlobIndexSize() + 2 * md.StringsIndexSize();
  279. }
  280. internal sealed override void BuildTables(MetaDataOut md)
  281. {
  282. md.AddToTable(MDTable.Assembly, this);
  283. nameIx = md.AddToStringsHeap(name);
  284. cultIx = md.AddToStringsHeap(culture);
  285. keyIx = md.AddToBlobHeap(publicKey);
  286. if (security != null)
  287. {
  288. for (int i = 0; i < security.Count; i++)
  289. {
  290. ((DeclSecurity)security[i]).BuildMDTables(md);
  291. }
  292. }
  293. }
  294. internal sealed override void Write(PEWriter output)
  295. {
  296. //Console.WriteLine("Writing assembly element with nameIx of " + nameIx + " at file offset " + output.Seek(0,SeekOrigin.Current));
  297. output.Write((uint)hashAlgId);
  298. output.Write(majorVer);
  299. output.Write(minorVer);
  300. output.Write(buildNo);
  301. output.Write(revisionNo);
  302. output.Write(flags);
  303. output.BlobIndex(keyIx);
  304. output.StringsIndex(nameIx);
  305. output.StringsIndex(cultIx);
  306. }
  307. internal override void Write(CILWriter output)
  308. {
  309. output.WriteLine(".assembly " + name + " { }");
  310. }
  311. internal sealed override uint GetCodedIx(CIx code)
  312. {
  313. switch (code)
  314. {
  315. case (CIx.HasCustomAttr): return 14;
  316. case (CIx.HasDeclSecurity): return 2;
  317. }
  318. return 0;
  319. }
  320. }
  321. /**************************************************************************/
  322. /// <summary>
  323. /// Descriptor for a module
  324. /// </summary>
  325. public abstract class Module : DefiningScope
  326. {
  327. Guid mvid;
  328. uint mvidIx = 0;
  329. internal ModuleRef refOf;
  330. /// <summary>
  331. /// The default class "Module" for globals
  332. /// </summary>
  333. protected ClassDef defaultClass;
  334. /// <summary>
  335. /// Is this module a .dll or .exe
  336. /// </summary>
  337. //protected bool isDLL;
  338. /// <summary>
  339. /// Is this module mscorlib.dll
  340. /// </summary>
  341. protected bool ismscorlib = false;
  342. /// <summary>
  343. /// Managed resources for this module
  344. /// </summary>
  345. protected ArrayList resources = new ArrayList();
  346. /*-------------------- Constructors ---------------------------------*/
  347. internal Module(string mName)
  348. : base(GetBaseName(mName))
  349. {
  350. mvid = Guid.NewGuid();
  351. //isDLL = name.EndsWith(".dll") || name.EndsWith(".DLL");
  352. defaultClass = new ClassDef((PEFile)this, TypeAttr.Private, "", "<Module>");
  353. defaultClass.MakeSpecial();
  354. tabIx = MDTable.Module;
  355. ismscorlib = name.ToLower() == "mscorlib.dll";
  356. if (Diag.DiagOn) Console.WriteLine("Module name = " + name);
  357. }
  358. internal void Read(PEReader buff)
  359. {
  360. buff.ReadZeros(2);
  361. name = buff.GetString();
  362. mvid = buff.GetGUID();
  363. uint junk = buff.GetGUIDIx();
  364. junk = buff.GetGUIDIx();
  365. if (Diag.DiagOn) Console.WriteLine("Reading module with name " + name + " and Mvid = " + mvid);
  366. ismscorlib = name.ToLower() == "mscorlib.dll";
  367. }
  368. internal static ModuleRef ReadModuleRef(PEReader buff)
  369. {
  370. buff.ReadZeros(2);
  371. string name = buff.GetString();
  372. uint junk = buff.GetGUIDIx();
  373. junk = buff.GetGUIDIx();
  374. junk = buff.GetGUIDIx();
  375. ModuleRef mRef = new ModuleRef(new ModuleFile(name, null));
  376. mRef.ReadAsDef();
  377. return mRef;
  378. }
  379. /*------------------------- public set and get methods --------------------------*/
  380. /// <summary>
  381. /// Add a class to this Module
  382. /// If this class already exists, throw an exception
  383. /// </summary>
  384. /// <param name="attrSet">attributes of this class</param>
  385. /// <param name="nsName">name space name</param>
  386. /// <param name="name">class name</param>
  387. /// <returns>a descriptor for this new class</returns>
  388. public ClassDef AddClass(TypeAttr attrSet, string nsName, string name)
  389. {
  390. ClassDef aClass = GetClass(nsName, name);
  391. if (aClass != null)
  392. throw new DescriptorException("Class " + aClass.NameString());
  393. aClass = new ClassDef((PEFile)this, attrSet, nsName, name);
  394. classes.Add(aClass);
  395. return aClass;
  396. }
  397. /// <summary>
  398. /// Add a class which extends System.ValueType to this Module
  399. /// If this class already exists, throw an exception
  400. /// </summary>
  401. /// <param name="attrSet">attributes of this class</param>
  402. /// <param name="nsName">name space name</param>
  403. /// <param name="name">class name</param>
  404. /// <returns>a descriptor for this new class</returns>
  405. public ClassDef AddValueClass(TypeAttr attrSet, string nsName, string name)
  406. {
  407. ClassDef aClass = AddClass(attrSet, nsName, name);
  408. aClass.SuperType = MSCorLib.mscorlib.ValueType();
  409. aClass.MakeValueClass();
  410. return aClass;
  411. }
  412. /// <summary>
  413. /// Add a class to this PE File
  414. /// </summary>
  415. /// <param name="attrSet">attributes of this class</param>
  416. /// <param name="nsName">name space name</param>
  417. /// <param name="name">class name</param>
  418. /// <param name="superType">super type of this class (extends)</param>
  419. /// <returns>a descriptor for this new class</returns>
  420. public ClassDef AddClass(TypeAttr attrSet, string nsName, string name, Class superType)
  421. {
  422. ClassDef aClass = AddClass(attrSet, nsName, name);
  423. aClass.SuperType = superType;
  424. return aClass;
  425. }
  426. /// <summary>
  427. /// Add a class to this module
  428. /// If this class already exists, throw an exception
  429. /// </summary>
  430. /// <param name="aClass">The class to be added</param>
  431. public void AddClass(ClassDef aClass)
  432. {
  433. ClassDef eClass = GetClass(aClass.NameSpace(), aClass.Name());
  434. if (eClass != null)
  435. throw new DescriptorException("Class " + aClass.NameString());
  436. classes.Add(aClass);
  437. // MERGE change Refs to Defs here, fix this
  438. aClass.SetScope((PEFile)this);
  439. }
  440. /// <summary>
  441. /// Get a class of this module, if no class exists, return null
  442. /// </summary>
  443. /// <param name="name">The name of the class to get</param>
  444. /// <returns>ClassDef for name or null</returns>
  445. public ClassDef GetClass(string name)
  446. {
  447. return (ClassDef)GetClass(null, name, false);
  448. }
  449. /// <summary>
  450. /// Get a class of this module, if no class exists, return null
  451. /// </summary>
  452. /// <param name="nsName">The namespace of the class</param>
  453. /// <param name="name">The name of the class to get</param>
  454. /// <returns>ClassDef for nsName.name or null</returns>
  455. public ClassDef GetClass(string nsName, string name)
  456. {
  457. return (ClassDef)GetClass(nsName, name, true);
  458. }
  459. /// <summary>
  460. /// Get all the classes of this module
  461. /// </summary>
  462. /// <returns>An array containing a ClassDef for each class of this module</returns>
  463. public ClassDef[] GetClasses()
  464. {
  465. return (ClassDef[])classes.ToArray(typeof(ClassDef));
  466. }
  467. /// <summary>
  468. /// Add a "global" method to this module
  469. /// </summary>
  470. /// <param name="name">method name</param>
  471. /// <param name="retType">return type</param>
  472. /// <param name="pars">method parameters</param>
  473. /// <returns>a descriptor for this new "global" method</returns>
  474. public MethodDef AddMethod(string name, Type retType, Param[] pars)
  475. {
  476. MethodDef newMeth = defaultClass.AddMethod(name, retType, pars);
  477. return newMeth;
  478. }
  479. /// <summary>
  480. /// Add a "global" method to this module
  481. /// </summary>
  482. /// <param name="name">method name</param>
  483. /// <param name="genPars">generic parameters</param>
  484. /// <param name="retType">return type</param>
  485. /// <param name="pars">method parameters</param>
  486. /// <returns>a descriptor for this new "global" method</returns>
  487. public MethodDef AddMethod(string name, GenericParam[] genPars, Type retType, Param[] pars)
  488. {
  489. MethodDef newMeth = defaultClass.AddMethod(name, genPars, retType, pars);
  490. return newMeth;
  491. }
  492. /// <summary>
  493. /// Add a "global" method to this module
  494. /// </summary>
  495. /// <param name="mAtts">method attributes</param>
  496. /// <param name="iAtts">method implementation attributes</param>
  497. /// <param name="name">method name</param>
  498. /// <param name="retType">return type</param>
  499. /// <param name="pars">method parameters</param>
  500. /// <returns>a descriptor for this new "global" method</returns>
  501. public MethodDef AddMethod(MethAttr mAtts, ImplAttr iAtts, string name, Type retType, Param[] pars)
  502. {
  503. MethodDef newMeth = defaultClass.AddMethod(mAtts, iAtts, name, retType, pars);
  504. return newMeth;
  505. }
  506. /// <summary>
  507. /// Add a "global" method to this module
  508. /// </summary>
  509. /// <param name="mAtts">method attributes</param>
  510. /// <param name="iAtts">method implementation attributes</param>
  511. /// <param name="name">method name</param>
  512. /// <param name="genPars">generic parameters</param>
  513. /// <param name="retType">return type</param>
  514. /// <param name="pars">method parameters</param>
  515. /// <returns>a descriptor for this new "global" method</returns>
  516. public MethodDef AddMethod(MethAttr mAtts, ImplAttr iAtts, string name, GenericParam[] genPars, Type retType, Param[] pars)
  517. {
  518. MethodDef newMeth = defaultClass.AddMethod(mAtts, iAtts, name, genPars, retType, pars);
  519. return newMeth;
  520. }
  521. /// <summary>
  522. /// Add a "global" method to this module
  523. /// </summary>
  524. /// <param name="meth">The method to be added</param>
  525. public void AddMethod(MethodDef meth)
  526. {
  527. defaultClass.AddMethod(meth);
  528. }
  529. /// <summary>
  530. /// Get a method of this module, if it exists
  531. /// </summary>
  532. /// <param name="name">The name of the method to get</param>
  533. /// <returns>MethodDef for name, or null if one does not exist</returns>
  534. public MethodDef GetMethod(string name)
  535. {
  536. return defaultClass.GetMethod(name);
  537. }
  538. /// <summary>
  539. /// Get all the methods of this module with a specified name
  540. /// </summary>
  541. /// <param name="name">The name of the method(s)</param>
  542. /// <returns>An array of all the methods of this module called "name" </returns>
  543. public MethodDef[] GetMethods(string name)
  544. {
  545. return defaultClass.GetMethods(name);
  546. }
  547. /// <summary>
  548. /// Get a method of this module, if it exists
  549. /// </summary>
  550. /// <param name="name">The name of the method to get</param>
  551. /// <param name="parTypes">The signature of the method</param>
  552. /// <returns>MethodDef for name(parTypes), or null if one does not exist</returns>
  553. public MethodDef GetMethod(string name, Type[] parTypes)
  554. {
  555. return defaultClass.GetMethod(name, parTypes);
  556. }
  557. /// <summary>
  558. /// Get all the methods of this module
  559. /// </summary>
  560. /// <returns>An array of all the methods of this module</returns>
  561. public MethodDef[] GetMethods()
  562. {
  563. return defaultClass.GetMethods();
  564. }
  565. /// <summary>
  566. /// Delete a method from this module
  567. /// </summary>
  568. /// <param name="meth">The method to be deleted</param>
  569. public void RemoveMethod(MethodDef meth)
  570. {
  571. defaultClass.RemoveMethod(meth);
  572. }
  573. /// <summary>
  574. /// Delete a method from this module
  575. /// </summary>
  576. /// <param name="name">The name of the method to be deleted</param>
  577. public void RemoveMethod(string name)
  578. {
  579. defaultClass.RemoveMethod(name);
  580. }
  581. /// <summary>
  582. /// Delete a method from this module
  583. /// </summary>
  584. /// <param name="name">The name of the method to be deleted</param>
  585. /// <param name="parTypes">The signature of the method to be deleted</param>
  586. public void RemoveMethod(string name, Type[] parTypes)
  587. {
  588. defaultClass.RemoveMethod(name, parTypes);
  589. }
  590. /// <summary>
  591. /// Delete a method from this module
  592. /// </summary>
  593. /// <param name="ix">The index of the method (in the method array
  594. /// returned by GetMethods()) to be deleted</param>
  595. public void RemoveMethod(int ix)
  596. {
  597. defaultClass.RemoveMethod(ix);
  598. }
  599. /// <summary>
  600. /// Add a "global" field to this module
  601. /// </summary>
  602. /// <param name="name">field name</param>
  603. /// <param name="fType">field type</param>
  604. /// <returns>a descriptor for this new "global" field</returns>
  605. public FieldDef AddField(string name, Type fType)
  606. {
  607. FieldDef newField = defaultClass.AddField(name, fType);
  608. return newField;
  609. }
  610. /// <summary>
  611. /// Add a "global" field to this module
  612. /// </summary>
  613. /// <param name="attrSet">attributes of this field</param>
  614. /// <param name="name">field name</param>
  615. /// <param name="fType">field type</param>
  616. /// <returns>a descriptor for this new "global" field</returns>
  617. public FieldDef AddField(FieldAttr attrSet, string name, Type fType)
  618. {
  619. FieldDef newField = defaultClass.AddField(attrSet, name, fType);
  620. return newField;
  621. }
  622. /// <summary>
  623. /// Add a "global" field to this module
  624. /// </summary>
  625. /// <param name="fld">The field to be added</param>
  626. public void AddField(FieldDef fld)
  627. {
  628. defaultClass.AddField(fld);
  629. }
  630. /// <summary>
  631. /// Get a field of this module, if it exists
  632. /// </summary>
  633. /// <param name="name">The name of the field</param>
  634. /// <returns>FieldDef for "name", or null if one doesn't exist</returns>
  635. public FieldDef GetField(string name)
  636. {
  637. return defaultClass.GetField(name);
  638. }
  639. /// <summary>
  640. /// Get all the fields of this module
  641. /// </summary>
  642. /// <returns>An array of all the fields of this module</returns>
  643. public FieldDef[] GetFields()
  644. {
  645. return defaultClass.GetFields();
  646. }
  647. /// <summary>
  648. /// Make a ModuleRef for this Module.
  649. /// </summary>
  650. /// <returns>ModuleRef for this Module</returns>
  651. public ModuleRef MakeRefOf(/*bool hasEntryPoint, byte[] hashValue*/)
  652. {
  653. if (refOf == null)
  654. {
  655. refOf = new ModuleRef(name/*,hasEntryPoint,hashValue*/);
  656. refOf.defOf = this;
  657. }/* else { // fix this
  658. if (hasEntryPoint)
  659. refOf.SetEntryPoint();
  660. refOf.SetHash(hashValue);
  661. }*/
  662. return refOf;
  663. }
  664. /// <summary>
  665. /// Set the name for this module
  666. /// </summary>
  667. /// <param name="newName">New module name</param>
  668. public void SetName(string newName)
  669. {
  670. name = newName;
  671. //isDLL = name.EndsWith(".dll") || name.EndsWith(".DLL");
  672. }
  673. public void SetMVid(Guid guid)
  674. {
  675. mvid = guid;
  676. }
  677. public Guid GetMVid()
  678. {
  679. return mvid;
  680. }
  681. /*------------------------- internal functions --------------------------*/
  682. internal bool isMSCorLib() { return ismscorlib; }
  683. internal bool isDefaultClass(ClassDef aClass) { return aClass == defaultClass; }
  684. private static string GetBaseName(string name)
  685. {
  686. // more to this??
  687. if (name.IndexOf("\\") != -1)
  688. name = name.Substring(name.LastIndexOf("\\") + 1);
  689. return name;
  690. }
  691. internal void SetDefaultClass(ClassDef dClass)
  692. {
  693. defaultClass = dClass;
  694. }
  695. internal sealed override void BuildTables(MetaDataOut md)
  696. {
  697. md.AddToTable(MDTable.Module, this);
  698. nameIx = md.AddToStringsHeap(name);
  699. mvidIx = md.AddToGUIDHeap(mvid);
  700. defaultClass.BuildTables(md);
  701. for (int i = 0; i < classes.Count; i++)
  702. {
  703. ((Class)classes[i]).BuildMDTables(md);
  704. }
  705. for (int i = 0; i < resources.Count; i++)
  706. {
  707. ((ManifestResource)resources[i]).BuildMDTables(md);
  708. }
  709. }
  710. internal static uint Size(MetaData md)
  711. {
  712. return 2 + md.StringsIndexSize() + 3 * md.GUIDIndexSize();
  713. }
  714. internal sealed override void Write(PEWriter output)
  715. {
  716. output.Write((short)0);
  717. output.StringsIndex(nameIx);
  718. output.GUIDIndex(mvidIx);
  719. output.GUIDIndex(0);
  720. output.GUIDIndex(0);
  721. }
  722. internal sealed override uint GetCodedIx(CIx code)
  723. {
  724. switch (code)
  725. {
  726. case (CIx.HasCustomAttr): return 7;
  727. case (CIx.ResolutionScope): return 0;
  728. }
  729. return 0;
  730. }
  731. }
  732. }