Image.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // Image.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using Mono;
  30. using Mono.Cecil.Cil;
  31. using Mono.Cecil.Metadata;
  32. using RVA = System.UInt32;
  33. namespace Mono.Cecil.PE
  34. {
  35. sealed class Image
  36. {
  37. public ModuleKind Kind;
  38. public TargetRuntime Runtime;
  39. public TargetArchitecture Architecture;
  40. public ModuleCharacteristics Characteristics;
  41. public string FileName;
  42. public Section[] Sections;
  43. public Section MetadataSection;
  44. public uint EntryPointToken;
  45. public ModuleAttributes Attributes;
  46. public DataDirectory Debug;
  47. public DataDirectory Resources;
  48. public DataDirectory StrongName;
  49. public StringHeap StringHeap;
  50. public BlobHeap BlobHeap;
  51. public UserStringHeap UserStringHeap;
  52. public GuidHeap GuidHeap;
  53. public TableHeap TableHeap;
  54. readonly int[] coded_index_sizes = new int[13];
  55. readonly Func<Table, int> counter;
  56. public Image()
  57. {
  58. counter = GetTableLength;
  59. }
  60. public bool HasTable(Table table)
  61. {
  62. return GetTableLength(table) > 0;
  63. }
  64. public int GetTableLength(Table table)
  65. {
  66. return (int)TableHeap[table].Length;
  67. }
  68. public int GetTableIndexSize(Table table)
  69. {
  70. return GetTableLength(table) < 65536 ? 2 : 4;
  71. }
  72. public int GetCodedIndexSize(CodedIndex coded_index)
  73. {
  74. var index = (int)coded_index;
  75. var size = coded_index_sizes[index];
  76. if (size != 0)
  77. return size;
  78. return coded_index_sizes[index] = Mixin.GetSize(coded_index, counter);
  79. }
  80. public uint ResolveVirtualAddress(RVA rva)
  81. {
  82. var section = GetSectionAtVirtualAddress(rva);
  83. if (section == null)
  84. throw new ArgumentOutOfRangeException();
  85. return ResolveVirtualAddressInSection(rva, section);
  86. }
  87. public uint ResolveVirtualAddressInSection(RVA rva, Section section)
  88. {
  89. return rva + section.PointerToRawData - section.VirtualAddress;
  90. }
  91. public Section GetSection(string name)
  92. {
  93. var sections = this.Sections;
  94. for (int i = 0; i < sections.Length; i++)
  95. {
  96. var section = sections[i];
  97. if (section.Name == name)
  98. return section;
  99. }
  100. return null;
  101. }
  102. public Section GetSectionAtVirtualAddress(RVA rva)
  103. {
  104. var sections = this.Sections;
  105. for (int i = 0; i < sections.Length; i++)
  106. {
  107. var section = sections[i];
  108. if (rva >= section.VirtualAddress && rva < section.VirtualAddress + section.SizeOfRawData)
  109. return section;
  110. }
  111. return null;
  112. }
  113. public ImageDebugDirectory GetDebugHeader(out byte[] header)
  114. {
  115. var section = GetSectionAtVirtualAddress(Debug.VirtualAddress);
  116. var buffer = new ByteBuffer(section.Data);
  117. buffer.position = (int)(Debug.VirtualAddress - section.VirtualAddress);
  118. var directory = new ImageDebugDirectory
  119. {
  120. Characteristics = buffer.ReadInt32(),
  121. TimeDateStamp = buffer.ReadInt32(),
  122. MajorVersion = buffer.ReadInt16(),
  123. MinorVersion = buffer.ReadInt16(),
  124. Type = buffer.ReadInt32(),
  125. SizeOfData = buffer.ReadInt32(),
  126. AddressOfRawData = buffer.ReadInt32(),
  127. PointerToRawData = buffer.ReadInt32(),
  128. };
  129. buffer.position = (int)(directory.PointerToRawData - section.PointerToRawData);
  130. header = new byte[directory.SizeOfData];
  131. Buffer.BlockCopy(buffer.buffer, buffer.position, header, 0, header.Length);
  132. return directory;
  133. }
  134. }
  135. }