MDFieldElems.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. /// Descriptor for a field of a class
  26. /// </summary>
  27. public abstract class Field : Member
  28. {
  29. internal static readonly byte FieldTag = 0x6;
  30. protected Type type;
  31. /*-------------------- Constructors ---------------------------------*/
  32. internal Field(string pfName, Type pfType, Class paren)
  33. : base(pfName, paren)
  34. {
  35. type = pfType;
  36. }
  37. internal override void Resolve(PEReader buff)
  38. {
  39. if (type == null)
  40. {
  41. buff.currentClassScope = parent;
  42. type = buff.GetFieldType(sigIx);
  43. buff.currentClassScope = null;
  44. }
  45. }
  46. /*------------------------- public set and get methods --------------------------*/
  47. /// <summary>
  48. /// Get the type of this field
  49. /// </summary>
  50. /// <returns>Type descriptor for this field</returns>
  51. public Type GetFieldType() { return type; }
  52. /// <summary>
  53. /// Set the type of this field
  54. /// </summary>
  55. /// <param name="ty">The type of the field</param>
  56. public void SetFieldType(Type ty) { type = ty; }
  57. /*----------------------------- internal functions ------------------------------*/
  58. internal sealed override void BuildSignatures(MetaDataOut md)
  59. {
  60. MemoryStream sig = new MemoryStream();
  61. sig.WriteByte(FieldTag);
  62. type.TypeSig(sig);
  63. sigIx = md.AddToBlobHeap(sig.ToArray());
  64. done = false;
  65. }
  66. internal override string NameString()
  67. {
  68. return parent.NameString() + "." + name;
  69. }
  70. internal override void WriteType(CILWriter output)
  71. {
  72. type.WriteType(output);
  73. output.Write(" ");
  74. parent.WriteName(output);
  75. output.Write("::" + name);
  76. }
  77. }
  78. /**************************************************************************/
  79. /// <summary>
  80. /// Descriptor for a field defined in a class of an assembly/module
  81. /// </summary>
  82. public class FieldDef : Field
  83. {
  84. //private static readonly uint PInvokeImpl = 0x2000;
  85. private static readonly ushort HasFieldMarshal = 0x1000;
  86. private static readonly ushort HasFieldRVA = 0x100;
  87. private static readonly ushort HasDefault = 0x8000;
  88. private static readonly ushort NoFieldMarshal = 0xEFFF;
  89. private static readonly ushort NoFieldRVA = 0xFEFF;
  90. private static readonly ushort NoDefault = 0x7FFF;
  91. internal FieldRef refOf;
  92. DataConstant initVal;
  93. Constant constVal;
  94. NativeType marshalType;
  95. ushort flags;
  96. bool hasOffset = false;
  97. uint offset;
  98. /*-------------------- Constructors ---------------------------------*/
  99. internal FieldDef(string name, Type fType, ClassDef paren)
  100. : base(name, fType, paren)
  101. {
  102. tabIx = MDTable.Field;
  103. }
  104. internal FieldDef(FieldAttr attrSet, string name, Type fType, ClassDef paren)
  105. : base(name, fType, paren)
  106. {
  107. flags = (ushort)attrSet;
  108. tabIx = MDTable.Field;
  109. }
  110. internal FieldDef(FieldAttr attrSet, string name, Type fType, ClassSpec paren)
  111. : base(name, fType, paren)
  112. {
  113. flags = (ushort)attrSet;
  114. tabIx = MDTable.Field;
  115. }
  116. internal FieldDef(PEReader buff)
  117. : base(null, null, null)
  118. {
  119. flags = buff.ReadUInt16();
  120. name = buff.GetString();
  121. sigIx = buff.GetBlobIx();
  122. tabIx = MDTable.Field;
  123. }
  124. internal static void Read(PEReader buff, TableRow[] fields)
  125. {
  126. for (int i = 0; i < fields.Length; i++)
  127. fields[i] = new FieldDef(buff);
  128. }
  129. internal static void GetFieldRefs(PEReader buff, uint num, ClassRef parent)
  130. {
  131. for (int i = 0; i < num; i++)
  132. {
  133. uint flags = buff.ReadUInt16();
  134. string name = buff.GetString();
  135. uint sigIx = buff.GetBlobIx();
  136. if ((flags & (uint)FieldAttr.Public) == (uint)FieldAttr.Public)
  137. {
  138. if (parent.GetField(name) == null)
  139. {
  140. //Console.WriteLine(parent.NameString());
  141. buff.currentClassScope = parent;
  142. FieldRef fRef = new FieldRef(parent, name, buff.GetFieldType(sigIx));
  143. buff.currentClassScope = null;
  144. parent.AddToFieldList(fRef);
  145. }
  146. }
  147. }
  148. }
  149. internal void Resolve(PEReader buff, uint fIx)
  150. {
  151. /*
  152. if ((flags & HasFieldMarshal) != 0)
  153. marshalType = FieldMarshal.FindMarshalType(buff,this,
  154. buff.MakeCodedIndex(CIx.HasFieldMarshal,MDTable.Field,fIx));
  155. if ((flags & HasFieldRVA) != 0)
  156. initVal = FieldRVA.FindValue(buff,this,fIx);
  157. if ((flags & HasDefault) != 0)
  158. constVal = ConstantElem.FindConst(buff,this,
  159. buff.MakeCodedIndex(CIx.HasConstant,MDTable.Field,fIx));
  160. long offs = FieldLayout.FindLayout(buff,this,fIx);
  161. if (offs > -1){
  162. hasOffset = true;
  163. offset = (uint)offs;
  164. }
  165. */
  166. buff.currentClassScope = parent;
  167. type = buff.GetFieldType(sigIx);
  168. buff.currentClassScope = null;
  169. }
  170. /*------------------------- public set and get methods --------------------------*/
  171. /// <summary>
  172. /// Add an attribute(s) to this field
  173. /// </summary>
  174. /// <param name="fa">the attribute(s) to be added</param>
  175. public void AddFieldAttr(FieldAttr fa)
  176. {
  177. flags |= (ushort)fa;
  178. }
  179. public void SetFieldAttr(FieldAttr fa)
  180. {
  181. flags = (ushort)fa;
  182. }
  183. public FieldAttr GetFieldAttr()
  184. {
  185. return (FieldAttr)flags;
  186. }
  187. /// <summary>
  188. /// Add a value for this field
  189. /// </summary>
  190. /// <param name="val">the value for the field</param>
  191. public void AddValue(Constant val)
  192. {
  193. flags |= HasDefault;
  194. constVal = val;
  195. }
  196. /// <summary>
  197. /// Retrieve the initial value for this field
  198. /// </summary>
  199. /// <returns>initial value</returns>
  200. public Constant GetValue() { return constVal; }
  201. /// <summary>
  202. /// Remove the initial value from this field
  203. /// </summary>
  204. public void RemoveValue()
  205. {
  206. constVal = null;
  207. flags &= NoDefault;
  208. }
  209. /// <summary>
  210. /// Add an initial value for this field (at dataLabel) (.data)
  211. /// </summary>
  212. /// <param name="val">the value for the field</param>
  213. public void AddDataValue(DataConstant val)
  214. {
  215. flags |= HasFieldRVA;
  216. initVal = val;
  217. }
  218. /// <summary>
  219. /// Get the value for this data constant
  220. /// </summary>
  221. /// <returns></returns>
  222. public DataConstant GetDataValue()
  223. {
  224. return initVal;
  225. }
  226. /// <summary>
  227. /// Delete the value of this data constant
  228. /// </summary>
  229. public void RemoveDataValue()
  230. {
  231. initVal = null;
  232. flags &= NoFieldRVA;
  233. }
  234. /// <summary>
  235. /// Set the offset of the field. Used for sequential or explicit classes.
  236. /// (.field [offs])
  237. /// </summary>
  238. /// <param name="offs">field offset</param>
  239. public void SetOffset(uint offs)
  240. {
  241. offset = offs;
  242. hasOffset = true;
  243. }
  244. /// <summary>
  245. /// Return the offset for this data constant
  246. /// </summary>
  247. /// <returns></returns>
  248. public uint GetOffset() { return offset; }
  249. /// <summary>
  250. /// Delete the offset of this data constant
  251. /// </summary>
  252. public void RemoveOffset() { hasOffset = false; }
  253. /// <summary>
  254. /// Does this data constant have an offset?
  255. /// </summary>
  256. public bool HasOffset() { return hasOffset; }
  257. /// <summary>
  258. /// Set the marshalling info for a field
  259. /// </summary>
  260. /// <param name="mType"></param>
  261. public void SetMarshalType(NativeType mType)
  262. {
  263. flags |= HasFieldMarshal;
  264. marshalType = mType;
  265. }
  266. public NativeType GetMarshalType() { return marshalType; }
  267. public void RemoveMarshalType() { marshalType = null; flags &= NoFieldMarshal; }
  268. /// <summary>
  269. /// Get the FieldRef equivalent to this FieldDef. Assumes that
  270. /// one already exists.
  271. /// </summary>
  272. /// <returns>FieldRef for this FieldDef</returns>
  273. public FieldRef RefOf() { return refOf; }
  274. /// <summary>
  275. /// Create the FieldRef equivalent to this FieldDef. If one does not
  276. /// exist then create it.
  277. /// </summary>
  278. /// <returns>FieldRef for this FieldDef</returns>
  279. public FieldRef MakeRefOf()
  280. {
  281. if (refOf != null) return refOf;
  282. ClassRef parRef = ((ClassDef)parent).MakeRefOf();
  283. refOf = parRef.GetField(name);
  284. if (refOf == null)
  285. {
  286. Type refType;
  287. if (type is ClassDef)
  288. {
  289. refType = ((ClassDef)type).MakeRefOf();
  290. }
  291. else
  292. {
  293. refType = type;
  294. }
  295. refOf = new FieldRef(parRef, name, refType);
  296. refOf.defOf = this;
  297. }
  298. return refOf;
  299. }
  300. /*------------------------- internal functions --------------------------*/
  301. internal PEFile GetScope()
  302. {
  303. return ((ClassDef)parent).GetScope();
  304. }
  305. internal void ChangeRefsToDefs(ClassDef newPar, ClassDef[] oldTypes)
  306. {
  307. parent = newPar;
  308. bool changeType = false;
  309. for (int i = 0; i < oldTypes.Length && !changeType; i++)
  310. {
  311. if (type == oldTypes[i])
  312. type = newPar;
  313. }
  314. }
  315. internal override bool isDef() { return true; }
  316. internal void SetParent(ClassDef paren) { parent = paren; }
  317. internal sealed override void BuildTables(MetaDataOut md)
  318. {
  319. md.AddToTable(MDTable.Field, this);
  320. nameIx = md.AddToStringsHeap(name);
  321. if (!type.isDef()) type.BuildMDTables(md);
  322. if (initVal != null)
  323. {
  324. FieldRVA rva = new FieldRVA(this, initVal);
  325. rva.BuildMDTables(md);
  326. }
  327. if (constVal != null)
  328. {
  329. ConstantElem constElem = new ConstantElem(this, constVal);
  330. constElem.BuildMDTables(md);
  331. }
  332. if (hasOffset)
  333. {
  334. FieldLayout layout = new FieldLayout(this, offset);
  335. layout.BuildMDTables(md);
  336. }
  337. if (marshalType != null)
  338. {
  339. FieldMarshal marshalInfo = new FieldMarshal(this, marshalType);
  340. marshalInfo.BuildMDTables(md);
  341. }
  342. }
  343. internal sealed override void BuildCILInfo(CILWriter output)
  344. {
  345. if (!type.isDef()) type.BuildCILInfo(output);
  346. }
  347. internal static uint Size(MetaData md)
  348. {
  349. return 2 + md.StringsIndexSize() + md.BlobIndexSize();
  350. }
  351. internal sealed override void Write(PEWriter output)
  352. {
  353. output.Write(flags);
  354. output.StringsIndex(nameIx);
  355. output.BlobIndex(sigIx);
  356. }
  357. internal override void Write(CILWriter output)
  358. {
  359. output.Write(" .field ");
  360. if (hasOffset)
  361. {
  362. output.Write("[ {0} ] ", offset);
  363. }
  364. WriteFlags(output, flags);
  365. if (marshalType != null)
  366. {
  367. output.Write("marshal ");
  368. marshalType.Write(output);
  369. }
  370. type.WriteType(output);
  371. output.Write(" " + name);
  372. if (initVal != null)
  373. {
  374. initVal.Write(output);
  375. }
  376. else if (constVal != null)
  377. {
  378. constVal.Write(output);
  379. }
  380. output.WriteLine();
  381. }
  382. internal sealed override uint GetCodedIx(CIx code)
  383. {
  384. switch (code)
  385. {
  386. case (CIx.HasConstant): return 0;
  387. case (CIx.HasCustomAttr): return 1;
  388. case (CIx.HasFieldMarshal): return 0;
  389. case (CIx.MemberForwarded): return 0;
  390. }
  391. return 0;
  392. }
  393. }
  394. /**************************************************************************/
  395. /// <summary>
  396. /// Descriptor for a field of a class defined in another assembly/module
  397. /// </summary>
  398. public class FieldRef : Field
  399. {
  400. internal FieldDef defOf;
  401. /*-------------------- Constructors ---------------------------------*/
  402. internal FieldRef(Class paren, string name, Type fType)
  403. : base(name, fType, paren)
  404. {
  405. parent = paren;
  406. }
  407. internal FieldRef(uint parenIx, string name, uint sigIx)
  408. : base(name, null, null)
  409. {
  410. parentIx = parenIx;
  411. this.name = name;
  412. this.sigIx = sigIx;
  413. }
  414. internal override Member ResolveParent(PEReader buff)
  415. {
  416. if (parent != null) return this;
  417. MetaDataElement paren = buff.GetCodedElement(CIx.MemberRefParent, parentIx);
  418. //Console.WriteLine("parentIx = " + parentIx);
  419. //Console.WriteLine("paren = " + paren);
  420. if (paren is ClassDef)
  421. return ((ClassDef)paren).GetField(this.name);
  422. //if (paren is ClassSpec)
  423. // paren = ((ClassSpec)paren).GetParent();
  424. if (paren is ReferenceScope)
  425. parent = ((ReferenceScope)paren).GetDefaultClass();
  426. if (paren is TypeSpec)
  427. parent = new ConstructedTypeSpec((TypeSpec)paren);
  428. else
  429. parent = (Class)paren;
  430. if (parent != null)
  431. {
  432. Field existing = (Field)((Class)parent).GetFieldDesc(name);
  433. if (existing != null)
  434. {
  435. return existing;
  436. }
  437. }
  438. parent.AddToFieldList(this);
  439. return this;
  440. }
  441. /*------------------------- internal functions --------------------------*/
  442. internal sealed override void BuildTables(MetaDataOut md)
  443. {
  444. md.AddToTable(tabIx, this);
  445. nameIx = md.AddToStringsHeap(name);
  446. if (type is ClassSpec) md.AddToTable(MDTable.TypeSpec, type);
  447. if (!type.isDef())
  448. type.BuildMDTables(md);
  449. if (parent != null)
  450. {
  451. if (parent is ClassSpec) md.AddToTable(MDTable.TypeSpec, parent);
  452. parent.BuildMDTables(md);
  453. }
  454. }
  455. internal override void BuildCILInfo(CILWriter output)
  456. {
  457. parent.BuildCILInfo(output);
  458. }
  459. internal static uint Size(MetaData md)
  460. {
  461. return md.CodedIndexSize(CIx.MemberRefParent) + md.StringsIndexSize() + md.BlobIndexSize();
  462. }
  463. internal sealed override void Write(PEWriter output)
  464. {
  465. output.WriteCodedIndex(CIx.MemberRefParent, parent);
  466. output.StringsIndex(nameIx);
  467. output.BlobIndex(sigIx);
  468. }
  469. internal sealed override uint GetCodedIx(CIx code) { return 6; }
  470. }
  471. /**************************************************************************/
  472. /// <summary>
  473. /// Descriptor for layout information for a field
  474. /// </summary>
  475. public class FieldLayout : MetaDataElement
  476. {
  477. FieldDef field;
  478. uint offset, fieldIx = 0;
  479. /*-------------------- Constructors ---------------------------------*/
  480. internal FieldLayout(FieldDef field, uint offset)
  481. {
  482. this.field = field;
  483. this.offset = offset;
  484. tabIx = MDTable.FieldLayout;
  485. }
  486. internal FieldLayout(PEReader buff)
  487. {
  488. offset = buff.ReadUInt32();
  489. fieldIx = buff.GetIndex(MDTable.Field);
  490. tabIx = MDTable.FieldLayout;
  491. }
  492. internal static void Read(PEReader buff, TableRow[] layouts)
  493. {
  494. for (int i = 0; i < layouts.Length; i++)
  495. layouts[i] = new FieldLayout(buff);
  496. }
  497. internal sealed override void Resolve(PEReader buff)
  498. {
  499. field = (FieldDef)buff.GetElement(MDTable.Field, fieldIx);
  500. field.SetOffset(offset);
  501. }
  502. internal sealed override void BuildTables(MetaDataOut md)
  503. {
  504. md.AddToTable(MDTable.FieldLayout, this);
  505. }
  506. internal static uint Size(MetaData md)
  507. {
  508. return 4 + md.TableIndexSize(MDTable.Field);
  509. }
  510. internal sealed override void Write(PEWriter output)
  511. {
  512. output.Write(offset);
  513. output.WriteIndex(MDTable.Field, field.Row);
  514. }
  515. }
  516. }