2
0

MDSignatureElems.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. /// Base descriptor for signature blobs
  26. /// </summary>
  27. public class Signature : MetaDataElement
  28. {
  29. protected uint sigIx;
  30. protected byte[] sigBytes;
  31. /*-------------------- Constructors ---------------------------------*/
  32. internal Signature()
  33. {
  34. tabIx = MDTable.StandAloneSig;
  35. }
  36. private Signature(uint sIx)
  37. {
  38. sigIx = sIx;
  39. }
  40. internal static void Read(PEReader buff, TableRow[] sigs)
  41. {
  42. for (int i = 0; i < sigs.Length; i++)
  43. {
  44. uint sigIx = buff.GetBlobIx();
  45. uint tag = buff.FirstBlobByte(sigIx);
  46. if (tag == LocalSig.LocalSigByte)
  47. sigs[i] = new LocalSig(sigIx);
  48. else if (tag == Field.FieldTag)
  49. sigs[i] = new Signature(sigIx);
  50. else
  51. sigs[i] = new CalliSig(sigIx);
  52. sigs[i].Row = (uint)i + 1;
  53. }
  54. }
  55. internal override void Resolve(PEReader buff)
  56. {
  57. Type sigType = buff.GetFieldType(sigIx);
  58. buff.ReplaceSig(this, sigType);
  59. }
  60. internal static uint Size(MetaData md)
  61. {
  62. return md.BlobIndexSize();
  63. }
  64. internal sealed override void Write(PEWriter output)
  65. {
  66. output.BlobIndex(sigIx);
  67. }
  68. internal sealed override uint GetCodedIx(CIx code) { return (uint)tabIx; }
  69. }
  70. /**************************************************************************/
  71. /// <summary>
  72. /// Signature for calli instruction
  73. /// </summary>
  74. public class CalliSig : Signature
  75. {
  76. CallConv callConv;
  77. Type retType;
  78. Type[] parTypes, optParTypes;
  79. uint numPars = 0, numOptPars = 0;
  80. /*-------------------- Constructors ---------------------------------*/
  81. /// <summary>
  82. /// Create a signature for a calli instruction
  83. /// </summary>
  84. /// <param name="cconv">calling conventions</param>
  85. /// <param name="retType">return type</param>
  86. /// <param name="pars">parameter types</param>
  87. public CalliSig(CallConv cconv, Type retType, Type[] pars)
  88. {
  89. callConv = cconv;
  90. this.retType = retType;
  91. parTypes = pars;
  92. if (pars != null) numPars = (uint)pars.Length;
  93. }
  94. internal CalliSig(uint sigIx)
  95. {
  96. this.sigIx = sigIx;
  97. }
  98. /// <summary>
  99. /// The return type of the method being called.
  100. /// </summary>
  101. public Type ReturnType { get { return retType; } }
  102. /// <summary>
  103. /// The number of parameters on the method being called.
  104. /// </summary>
  105. public uint NumPars { get { return numPars; } }
  106. /// <summary>
  107. /// The number of optional parameters on the method being called.
  108. /// </summary>
  109. public uint NumOptPars { get { return numOptPars; } }
  110. /// <summary>
  111. /// Check to see if the method signature has a particular calling convention.
  112. /// </summary>
  113. /// <param name="callCon">The convention to check to see if the method has.</param>
  114. /// <returns>Ture if the calling convention exists on the method.</returns>
  115. internal bool HasCallConv(CallConv callCon)
  116. {
  117. return ((callConv & callCon) == callCon);
  118. }
  119. internal sealed override void Resolve(PEReader buff)
  120. {
  121. MethSig mSig = buff.ReadMethSig(null, sigIx);
  122. callConv = mSig.callConv;
  123. retType = mSig.retType;
  124. parTypes = mSig.parTypes;
  125. if (parTypes != null) numPars = (uint)parTypes.Length;
  126. optParTypes = mSig.optParTypes;
  127. if (optParTypes != null) numOptPars = (uint)optParTypes.Length;
  128. }
  129. /// <summary>
  130. /// Add the optional parameters to a vararg method
  131. /// This method sets the vararg calling convention
  132. /// </summary>
  133. /// <param name="optPars">the optional pars for the vararg call</param>
  134. public void AddVarArgs(Type[] optPars)
  135. {
  136. optParTypes = optPars;
  137. if (optPars != null) numOptPars = (uint)optPars.Length;
  138. callConv |= CallConv.Vararg;
  139. }
  140. /// <summary>
  141. /// Add extra calling conventions to this callsite signature
  142. /// </summary>
  143. /// <param name="cconv"></param>
  144. public void AddCallingConv(CallConv cconv)
  145. {
  146. callConv |= cconv;
  147. }
  148. internal void ChangeRefsToDefs(ClassDef newType, ClassDef[] oldTypes)
  149. {
  150. for (int i = 0; i < oldTypes.Length; i++)
  151. {
  152. if (retType == oldTypes[i]) retType = newType;
  153. for (int j = 0; j < numPars; j++)
  154. {
  155. if (parTypes[j] == oldTypes[i])
  156. parTypes[j] = newType;
  157. }
  158. for (int j = 0; j < numOptPars; j++)
  159. {
  160. if (optParTypes[j] == oldTypes[i])
  161. optParTypes[j] = newType;
  162. }
  163. }
  164. }
  165. internal sealed override void BuildTables(MetaDataOut md)
  166. {
  167. md.AddToTable(MDTable.StandAloneSig, this);
  168. for (int i = 0; i < numPars; i++)
  169. {
  170. parTypes[i].BuildMDTables(md);
  171. }
  172. if (numOptPars > 0)
  173. {
  174. for (int i = 0; i < numOptPars; i++)
  175. {
  176. optParTypes[i].BuildMDTables(md);
  177. }
  178. }
  179. }
  180. internal sealed override void BuildSignatures(MetaDataOut md)
  181. {
  182. MemoryStream sig = new MemoryStream();
  183. sig.WriteByte((byte)callConv);
  184. MetaDataOut.CompressNum(BlobUtil.CompressUInt(numPars + numOptPars), sig);
  185. retType.TypeSig(sig);
  186. for (int i = 0; i < numPars; i++)
  187. {
  188. parTypes[i].TypeSig(sig);
  189. }
  190. if (numOptPars > 0)
  191. {
  192. sig.WriteByte((byte)ElementType.Sentinel);
  193. for (int i = 0; i < numOptPars; i++)
  194. {
  195. optParTypes[i].TypeSig(sig);
  196. }
  197. }
  198. sigIx = md.AddToBlobHeap(sig.ToArray());
  199. done = false;
  200. }
  201. }
  202. /**************************************************************************/
  203. /// <summary>
  204. /// Descriptor for the locals for a method
  205. /// </summary>
  206. public class LocalSig : Signature
  207. {
  208. internal static readonly byte LocalSigByte = 0x7;
  209. Local[] locals;
  210. bool resolved = true;
  211. /*-------------------- Constructors ---------------------------------*/
  212. public LocalSig(Local[] locals)
  213. {
  214. this.locals = locals;
  215. }
  216. internal LocalSig(uint sigIx)
  217. {
  218. resolved = false;
  219. this.sigIx = sigIx;
  220. }
  221. internal override void Resolve(PEReader buff)
  222. {
  223. }
  224. internal void Resolve(PEReader buff, MethodDef meth)
  225. {
  226. if (resolved) return;
  227. buff.currentMethodScope = meth;
  228. buff.currentClassScope = (Class)meth.GetParent();
  229. locals = buff.ReadLocalSig(sigIx);
  230. buff.currentMethodScope = null;
  231. buff.currentClassScope = null;
  232. }
  233. internal Local[] GetLocals()
  234. {
  235. return locals;
  236. }
  237. internal sealed override void BuildTables(MetaDataOut md)
  238. {
  239. md.AddToTable(tabIx, this);
  240. for (int i = 0; i < locals.Length; i++)
  241. {
  242. locals[i].BuildTables(md);
  243. }
  244. }
  245. internal byte[] SigBytes()
  246. {
  247. MemoryStream sig = new MemoryStream();
  248. sig.WriteByte(LocalSigByte);
  249. MetaDataOut.CompressNum(BlobUtil.CompressUInt((uint)locals.Length), sig);
  250. for (int i = 0; i < locals.Length; i++)
  251. {
  252. ((Local)locals[i]).TypeSig(sig);
  253. }
  254. return sig.ToArray();
  255. }
  256. internal sealed override void BuildSignatures(MetaDataOut md)
  257. {
  258. sigIx = md.AddToBlobHeap(SigBytes());
  259. done = false;
  260. }
  261. }
  262. /// <summary>
  263. /// Stores the signature for the debug info for a local variable.
  264. /// </summary>
  265. public class DebugLocalSig : Signature
  266. {
  267. internal static readonly byte LocalSigByte = 0x6;
  268. bool resolved = true;
  269. byte[] loc;
  270. /*-------------------- Constructors ---------------------------------*/
  271. internal DebugLocalSig(byte[] loc)
  272. {
  273. this.loc = loc;
  274. }
  275. internal DebugLocalSig(uint sigIx)
  276. {
  277. resolved = false;
  278. this.sigIx = sigIx;
  279. }
  280. internal override void Resolve(PEReader buff)
  281. {
  282. }
  283. internal void Resolve(PEReader buff, MethodDef meth)
  284. {
  285. if (resolved) return;
  286. }
  287. internal sealed override void BuildTables(MetaDataOut md)
  288. {
  289. md.AddToTable(tabIx, this);
  290. }
  291. internal byte[] SigBytes()
  292. {
  293. byte[] b = new byte[loc.Length + 1];
  294. b[0] = LocalSigByte;
  295. System.Array.Copy(loc, 0, b, 1, loc.Length);
  296. return b;
  297. }
  298. internal sealed override void BuildSignatures(MetaDataOut md)
  299. {
  300. sigIx = md.AddToBlobHeap(SigBytes());
  301. done = false;
  302. }
  303. }
  304. }