MDFieldElems.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. //if ((flags & HasFieldMarshal) != 0)
  152. // marshalType = FieldMarshal.FindMarshalType(buff,this,
  153. // buff.MakeCodedIndex(CIx.HasFieldMarshal,MDTable.Field,fIx));
  154. //if ((flags & HasFieldRVA) != 0) {
  155. //initVal = FieldRVA.FindValue(buff, this, fIx);
  156. //}
  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. buff.currentClassScope = parent;
  166. type = buff.GetFieldType(sigIx);
  167. buff.currentClassScope = null;
  168. }
  169. /*------------------------- public set and get methods --------------------------*/
  170. /// <summary>
  171. /// Add an attribute(s) to this field
  172. /// </summary>
  173. /// <param name="fa">the attribute(s) to be added</param>
  174. public void AddFieldAttr(FieldAttr fa)
  175. {
  176. flags |= (ushort)fa;
  177. }
  178. public void SetFieldAttr(FieldAttr fa)
  179. {
  180. flags = (ushort)fa;
  181. }
  182. public FieldAttr GetFieldAttr()
  183. {
  184. return (FieldAttr)flags;
  185. }
  186. /// <summary>
  187. /// Add a value for this field
  188. /// </summary>
  189. /// <param name="val">the value for the field</param>
  190. public void AddValue(Constant val)
  191. {
  192. flags |= HasDefault;
  193. constVal = val;
  194. }
  195. /// <summary>
  196. /// Retrieve the initial value for this field
  197. /// </summary>
  198. /// <returns>initial value</returns>
  199. public Constant GetValue() { return constVal; }
  200. /// <summary>
  201. /// Remove the initial value from this field
  202. /// </summary>
  203. public void RemoveValue()
  204. {
  205. constVal = null;
  206. flags &= NoDefault;
  207. }
  208. /// <summary>
  209. /// Add an initial value for this field (at dataLabel) (.data)
  210. /// </summary>
  211. /// <param name="val">the value for the field</param>
  212. public void AddDataValue(DataConstant val)
  213. {
  214. flags |= HasFieldRVA;
  215. initVal = val;
  216. }
  217. /// <summary>
  218. /// Get the value for this data constant
  219. /// </summary>
  220. /// <returns></returns>
  221. public DataConstant GetDataValue()
  222. {
  223. return initVal;
  224. }
  225. /// <summary>
  226. /// Delete the value of this data constant
  227. /// </summary>
  228. public void RemoveDataValue()
  229. {
  230. initVal = null;
  231. flags &= NoFieldRVA;
  232. }
  233. /// <summary>
  234. /// Set the offset of the field. Used for sequential or explicit classes.
  235. /// (.field [offs])
  236. /// </summary>
  237. /// <param name="offs">field offset</param>
  238. public void SetOffset(uint offs)
  239. {
  240. offset = offs;
  241. hasOffset = true;
  242. }
  243. /// <summary>
  244. /// Return the offset for this data constant
  245. /// </summary>
  246. /// <returns></returns>
  247. public uint GetOffset() { return offset; }
  248. /// <summary>
  249. /// Delete the offset of this data constant
  250. /// </summary>
  251. public void RemoveOffset() { hasOffset = false; }
  252. /// <summary>
  253. /// Does this data constant have an offset?
  254. /// </summary>
  255. public bool HasOffset() { return hasOffset; }
  256. /// <summary>
  257. /// Set the marshalling info for a field
  258. /// </summary>
  259. /// <param name="mType"></param>
  260. public void SetMarshalType(NativeType mType)
  261. {
  262. flags |= HasFieldMarshal;
  263. marshalType = mType;
  264. }
  265. public NativeType GetMarshalType() { return marshalType; }
  266. public void RemoveMarshalType() { marshalType = null; flags &= NoFieldMarshal; }
  267. /// <summary>
  268. /// Get the FieldRef equivalent to this FieldDef. Assumes that
  269. /// one already exists.
  270. /// </summary>
  271. /// <returns>FieldRef for this FieldDef</returns>
  272. public FieldRef RefOf() { return refOf; }
  273. /// <summary>
  274. /// Create the FieldRef equivalent to this FieldDef. If one does not
  275. /// exist then create it.
  276. /// </summary>
  277. /// <returns>FieldRef for this FieldDef</returns>
  278. public FieldRef MakeRefOf()
  279. {
  280. if (refOf != null) return refOf;
  281. ClassRef parRef = ((ClassDef)parent).MakeRefOf();
  282. refOf = parRef.GetField(name);
  283. if (refOf == null)
  284. {
  285. Type refType;
  286. if (type is ClassDef)
  287. {
  288. refType = ((ClassDef)type).MakeRefOf();
  289. }
  290. else
  291. {
  292. refType = type;
  293. }
  294. refOf = new FieldRef(parRef, name, refType);
  295. refOf.defOf = this;
  296. }
  297. return refOf;
  298. }
  299. /*------------------------- internal functions --------------------------*/
  300. internal PEFile GetScope()
  301. {
  302. return ((ClassDef)parent).GetScope();
  303. }
  304. internal void ChangeRefsToDefs(ClassDef newPar, ClassDef[] oldTypes)
  305. {
  306. parent = newPar;
  307. bool changeType = false;
  308. for (int i = 0; i < oldTypes.Length && !changeType; i++)
  309. {
  310. if (type == oldTypes[i])
  311. type = newPar;
  312. }
  313. }
  314. internal override bool isDef() { return true; }
  315. internal void SetParent(ClassDef paren) { parent = paren; }
  316. internal sealed override void BuildTables(MetaDataOut md)
  317. {
  318. md.AddToTable(MDTable.Field, this);
  319. nameIx = md.AddToStringsHeap(name);
  320. if (!type.isDef()) type.BuildMDTables(md);
  321. if (initVal != null)
  322. {
  323. FieldRVA rva = new FieldRVA(this, initVal);
  324. rva.BuildMDTables(md);
  325. }
  326. if (constVal != null)
  327. {
  328. ConstantElem constElem = new ConstantElem(this, constVal);
  329. constElem.BuildMDTables(md);
  330. }
  331. if (hasOffset)
  332. {
  333. FieldLayout layout = new FieldLayout(this, offset);
  334. layout.BuildMDTables(md);
  335. }
  336. if (marshalType != null)
  337. {
  338. FieldMarshal marshalInfo = new FieldMarshal(this, marshalType);
  339. marshalInfo.BuildMDTables(md);
  340. }
  341. }
  342. internal sealed override void BuildCILInfo(CILWriter output)
  343. {
  344. if (!type.isDef()) type.BuildCILInfo(output);
  345. }
  346. internal static uint Size(MetaData md)
  347. {
  348. return 2 + md.StringsIndexSize() + md.BlobIndexSize();
  349. }
  350. internal sealed override void Write(PEWriter output)
  351. {
  352. output.Write(flags);
  353. output.StringsIndex(nameIx);
  354. output.BlobIndex(sigIx);
  355. }
  356. internal override void Write(CILWriter output)
  357. {
  358. output.Write(" .field ");
  359. if (hasOffset)
  360. {
  361. output.Write("[ {0} ] ", offset);
  362. }
  363. WriteFlags(output, flags);
  364. if (marshalType != null)
  365. {
  366. output.Write("marshal ");
  367. marshalType.Write(output);
  368. }
  369. type.WriteType(output);
  370. output.Write(" " + name);
  371. if (initVal != null)
  372. {
  373. initVal.Write(output);
  374. }
  375. else if (constVal != null)
  376. {
  377. constVal.Write(output);
  378. }
  379. output.WriteLine();
  380. }
  381. internal sealed override uint GetCodedIx(CIx code)
  382. {
  383. switch (code)
  384. {
  385. case (CIx.HasConstant): return 0;
  386. case (CIx.HasCustomAttr): return 1;
  387. case (CIx.HasFieldMarshal): return 0;
  388. case (CIx.MemberForwarded): return 0;
  389. }
  390. return 0;
  391. }
  392. }
  393. /**************************************************************************/
  394. /// <summary>
  395. /// Descriptor for a field of a class defined in another assembly/module
  396. /// </summary>
  397. public class FieldRef : Field
  398. {
  399. internal FieldDef defOf;
  400. /*-------------------- Constructors ---------------------------------*/
  401. internal FieldRef(Class paren, string name, Type fType)
  402. : base(name, fType, paren)
  403. {
  404. parent = paren;
  405. }
  406. internal FieldRef(uint parenIx, string name, uint sigIx)
  407. : base(name, null, null)
  408. {
  409. parentIx = parenIx;
  410. this.name = name;
  411. this.sigIx = sigIx;
  412. }
  413. internal override Member ResolveParent(PEReader buff)
  414. {
  415. if (parent != null) return this;
  416. MetaDataElement paren = buff.GetCodedElement(CIx.MemberRefParent, parentIx);
  417. //Console.WriteLine("parentIx = " + parentIx);
  418. //Console.WriteLine("paren = " + paren);
  419. if (paren is ClassDef)
  420. return ((ClassDef)paren).GetField(this.name);
  421. //if (paren is ClassSpec)
  422. // paren = ((ClassSpec)paren).GetParent();
  423. if (paren is ReferenceScope)
  424. parent = ((ReferenceScope)paren).GetDefaultClass();
  425. if (paren is TypeSpec)
  426. parent = new ConstructedTypeSpec((TypeSpec)paren);
  427. else
  428. parent = (Class)paren;
  429. if (parent != null)
  430. {
  431. Field existing = (Field)((Class)parent).GetFieldDesc(name);
  432. if (existing != null)
  433. {
  434. return existing;
  435. }
  436. }
  437. parent.AddToFieldList(this);
  438. return this;
  439. }
  440. /*------------------------- internal functions --------------------------*/
  441. internal sealed override void BuildTables(MetaDataOut md)
  442. {
  443. md.AddToTable(tabIx, this);
  444. nameIx = md.AddToStringsHeap(name);
  445. if (type is ClassSpec) md.ConditionalAddTypeSpec(type);
  446. if (!type.isDef())
  447. type.BuildMDTables(md);
  448. if (parent != null)
  449. {
  450. if (parent is ClassSpec) md.ConditionalAddTypeSpec(parent);
  451. parent.BuildMDTables(md);
  452. }
  453. }
  454. internal override void BuildCILInfo(CILWriter output)
  455. {
  456. parent.BuildCILInfo(output);
  457. }
  458. internal static uint Size(MetaData md)
  459. {
  460. return md.CodedIndexSize(CIx.MemberRefParent) + md.StringsIndexSize() + md.BlobIndexSize();
  461. }
  462. internal sealed override void Write(PEWriter output)
  463. {
  464. output.WriteCodedIndex(CIx.MemberRefParent, parent);
  465. output.StringsIndex(nameIx);
  466. output.BlobIndex(sigIx);
  467. }
  468. internal sealed override uint GetCodedIx(CIx code) { return 6; }
  469. }
  470. /**************************************************************************/
  471. /// <summary>
  472. /// Descriptor for layout information for a field
  473. /// </summary>
  474. public class FieldLayout : MetaDataElement
  475. {
  476. FieldDef field;
  477. uint offset, fieldIx = 0;
  478. /*-------------------- Constructors ---------------------------------*/
  479. internal FieldLayout(FieldDef field, uint offset)
  480. {
  481. this.field = field;
  482. this.offset = offset;
  483. tabIx = MDTable.FieldLayout;
  484. }
  485. internal FieldLayout(PEReader buff)
  486. {
  487. offset = buff.ReadUInt32();
  488. fieldIx = buff.GetIndex(MDTable.Field);
  489. tabIx = MDTable.FieldLayout;
  490. }
  491. internal static void Read(PEReader buff, TableRow[] layouts)
  492. {
  493. for (int i = 0; i < layouts.Length; i++)
  494. layouts[i] = new FieldLayout(buff);
  495. }
  496. internal sealed override void Resolve(PEReader buff)
  497. {
  498. field = (FieldDef)buff.GetElement(MDTable.Field, fieldIx);
  499. field.SetOffset(offset);
  500. }
  501. internal sealed override void BuildTables(MetaDataOut md)
  502. {
  503. md.AddToTable(MDTable.FieldLayout, this);
  504. }
  505. internal static uint Size(MetaData md)
  506. {
  507. return 4 + md.TableIndexSize(MDTable.Field);
  508. }
  509. internal sealed override void Write(PEWriter output)
  510. {
  511. output.Write(offset);
  512. output.WriteIndex(MDTable.Field, field.Row);
  513. }
  514. }
  515. }