Imported Upstream version 5.0.0.42

Former-commit-id: fd56571888259555122d8a0f58c68838229cea2b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2017-04-10 11:41:01 +00:00
parent 1190d13a04
commit 6bdd276d05
19939 changed files with 3099680 additions and 93811 deletions

View File

@@ -0,0 +1,47 @@
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2015 Jb Evain
// Copyright (c) 2008 - 2011 Novell, Inc.
//
// Licensed under the MIT/X11 license.
//
using System;
using System.IO;
namespace Mono.Cecil.PE {
class BinaryStreamReader : BinaryReader {
public int Position {
get { return (int) BaseStream.Position; }
set { BaseStream.Position = value; }
}
public int Length {
get { return (int) BaseStream.Length; }
}
public BinaryStreamReader (Stream stream)
: base (stream)
{
}
public void Advance (int bytes)
{
BaseStream.Seek (bytes, SeekOrigin.Current);
}
public void MoveTo (uint position)
{
BaseStream.Seek (position, SeekOrigin.Begin);
}
public DataDirectory ReadDataDirectory ()
{
return new DataDirectory (ReadUInt32 (), ReadUInt32 ());
}
}
}

View File

@@ -0,0 +1,78 @@
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2015 Jb Evain
// Copyright (c) 2008 - 2011 Novell, Inc.
//
// Licensed under the MIT/X11 license.
//
using System;
using System.IO;
#if !READ_ONLY
namespace Mono.Cecil.PE {
class BinaryStreamWriter : BinaryWriter {
public BinaryStreamWriter (Stream stream)
: base (stream)
{
}
public void WriteByte (byte value)
{
Write (value);
}
public void WriteUInt16 (ushort value)
{
Write (value);
}
public void WriteInt16 (short value)
{
Write (value);
}
public void WriteUInt32 (uint value)
{
Write (value);
}
public void WriteInt32 (int value)
{
Write (value);
}
public void WriteUInt64 (ulong value)
{
Write (value);
}
public void WriteBytes (byte [] bytes)
{
Write (bytes);
}
public void WriteDataDirectory (DataDirectory directory)
{
Write (directory.VirtualAddress);
Write (directory.Size);
}
public void WriteBuffer (ByteBuffer buffer)
{
Write (buffer.buffer, 0, buffer.length);
}
protected void Advance (int bytes)
{
BaseStream.Seek (bytes, SeekOrigin.Current);
}
}
}
#endif

View File

@@ -0,0 +1,341 @@
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2015 Jb Evain
// Copyright (c) 2008 - 2011 Novell, Inc.
//
// Licensed under the MIT/X11 license.
//
using System;
namespace Mono.Cecil.PE {
class ByteBuffer {
internal byte [] buffer;
internal int length;
internal int position;
public ByteBuffer ()
{
this.buffer = Empty<byte>.Array;
}
public ByteBuffer (int length)
{
this.buffer = new byte [length];
}
public ByteBuffer (byte [] buffer)
{
this.buffer = buffer ?? Empty<byte>.Array;
this.length = this.buffer.Length;
}
public void Advance (int length)
{
position += length;
}
public byte ReadByte ()
{
return buffer [position++];
}
public sbyte ReadSByte ()
{
return (sbyte) ReadByte ();
}
public byte [] ReadBytes (int length)
{
var bytes = new byte [length];
Buffer.BlockCopy (buffer, position, bytes, 0, length);
position += length;
return bytes;
}
public ushort ReadUInt16 ()
{
ushort value = (ushort) (buffer [position]
| (buffer [position + 1] << 8));
position += 2;
return value;
}
public short ReadInt16 ()
{
return (short) ReadUInt16 ();
}
public uint ReadUInt32 ()
{
uint value = (uint) (buffer [position]
| (buffer [position + 1] << 8)
| (buffer [position + 2] << 16)
| (buffer [position + 3] << 24));
position += 4;
return value;
}
public int ReadInt32 ()
{
return (int) ReadUInt32 ();
}
public ulong ReadUInt64 ()
{
uint low = ReadUInt32 ();
uint high = ReadUInt32 ();
return (((ulong) high) << 32) | low;
}
public long ReadInt64 ()
{
return (long) ReadUInt64 ();
}
public uint ReadCompressedUInt32 ()
{
byte first = ReadByte ();
if ((first & 0x80) == 0)
return first;
if ((first & 0x40) == 0)
return ((uint) (first & ~0x80) << 8)
| ReadByte ();
return ((uint) (first & ~0xc0) << 24)
| (uint) ReadByte () << 16
| (uint) ReadByte () << 8
| ReadByte ();
}
public int ReadCompressedInt32 ()
{
var b = buffer [position];
var u = (int) ReadCompressedUInt32 ();
var v = u >> 1;
if ((u & 1) == 0)
return v;
switch (b & 0xc0)
{
case 0:
case 0x40:
return v - 0x40;
case 0x80:
return v - 0x2000;
default:
return v - 0x10000000;
}
}
public float ReadSingle ()
{
if (!BitConverter.IsLittleEndian) {
var bytes = ReadBytes (4);
Array.Reverse (bytes);
return BitConverter.ToSingle (bytes, 0);
}
float value = BitConverter.ToSingle (buffer, position);
position += 4;
return value;
}
public double ReadDouble ()
{
if (!BitConverter.IsLittleEndian) {
var bytes = ReadBytes (8);
Array.Reverse (bytes);
return BitConverter.ToDouble (bytes, 0);
}
double value = BitConverter.ToDouble (buffer, position);
position += 8;
return value;
}
#if !READ_ONLY
public void WriteByte (byte value)
{
if (position == buffer.Length)
Grow (1);
buffer [position++] = value;
if (position > length)
length = position;
}
public void WriteSByte (sbyte value)
{
WriteByte ((byte) value);
}
public void WriteUInt16 (ushort value)
{
if (position + 2 > buffer.Length)
Grow (2);
buffer [position++] = (byte) value;
buffer [position++] = (byte) (value >> 8);
if (position > length)
length = position;
}
public void WriteInt16 (short value)
{
WriteUInt16 ((ushort) value);
}
public void WriteUInt32 (uint value)
{
if (position + 4 > buffer.Length)
Grow (4);
buffer [position++] = (byte) value;
buffer [position++] = (byte) (value >> 8);
buffer [position++] = (byte) (value >> 16);
buffer [position++] = (byte) (value >> 24);
if (position > length)
length = position;
}
public void WriteInt32 (int value)
{
WriteUInt32 ((uint) value);
}
public void WriteUInt64 (ulong value)
{
if (position + 8 > buffer.Length)
Grow (8);
buffer [position++] = (byte) value;
buffer [position++] = (byte) (value >> 8);
buffer [position++] = (byte) (value >> 16);
buffer [position++] = (byte) (value >> 24);
buffer [position++] = (byte) (value >> 32);
buffer [position++] = (byte) (value >> 40);
buffer [position++] = (byte) (value >> 48);
buffer [position++] = (byte) (value >> 56);
if (position > length)
length = position;
}
public void WriteInt64 (long value)
{
WriteUInt64 ((ulong) value);
}
public void WriteCompressedUInt32 (uint value)
{
if (value < 0x80)
WriteByte ((byte) value);
else if (value < 0x4000) {
WriteByte ((byte) (0x80 | (value >> 8)));
WriteByte ((byte) (value & 0xff));
} else {
WriteByte ((byte) ((value >> 24) | 0xc0));
WriteByte ((byte) ((value >> 16) & 0xff));
WriteByte ((byte) ((value >> 8) & 0xff));
WriteByte ((byte) (value & 0xff));
}
}
public void WriteCompressedInt32 (int value)
{
if (value >= 0) {
WriteCompressedUInt32 ((uint) (value << 1));
return;
}
if (value > -0x40)
value = 0x40 + value;
else if (value >= -0x2000)
value = 0x2000 + value;
else if (value >= -0x20000000)
value = 0x20000000 + value;
WriteCompressedUInt32 ((uint) ((value << 1) | 1));
}
public void WriteBytes (byte [] bytes)
{
var length = bytes.Length;
if (position + length > buffer.Length)
Grow (length);
Buffer.BlockCopy (bytes, 0, buffer, position, length);
position += length;
if (position > this.length)
this.length = position;
}
public void WriteBytes (int length)
{
if (position + length > buffer.Length)
Grow (length);
position += length;
if (position > this.length)
this.length = position;
}
public void WriteBytes (ByteBuffer buffer)
{
if (position + buffer.length > this.buffer.Length)
Grow (buffer.length);
Buffer.BlockCopy (buffer.buffer, 0, this.buffer, position, buffer.length);
position += buffer.length;
if (position > this.length)
this.length = position;
}
public void WriteSingle (float value)
{
var bytes = BitConverter.GetBytes (value);
if (!BitConverter.IsLittleEndian)
Array.Reverse (bytes);
WriteBytes (bytes);
}
public void WriteDouble (double value)
{
var bytes = BitConverter.GetBytes (value);
if (!BitConverter.IsLittleEndian)
Array.Reverse (bytes);
WriteBytes (bytes);
}
void Grow (int desired)
{
var current = this.buffer;
var current_length = current.Length;
var buffer = new byte [System.Math.Max (current_length + desired, current_length * 2)];
Buffer.BlockCopy (current, 0, buffer, 0, current_length);
this.buffer = buffer;
}
#endif
}
}

View File

@@ -0,0 +1,60 @@
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2015 Jb Evain
// Copyright (c) 2008 - 2011 Novell, Inc.
//
// Licensed under the MIT/X11 license.
//
using System;
using System.Collections.Generic;
namespace Mono.Cecil.PE {
sealed class ByteBufferEqualityComparer : IEqualityComparer<ByteBuffer> {
public bool Equals (ByteBuffer x, ByteBuffer y)
{
if (x.length != y.length)
return false;
var x_buffer = x.buffer;
var y_buffer = y.buffer;
for (int i = 0; i < x.length; i++)
if (x_buffer [i] != y_buffer [i])
return false;
return true;
}
public int GetHashCode (ByteBuffer buffer)
{
#if !BYTE_BUFFER_WELL_DISTRIBUTED_HASH
var hash = 0;
var bytes = buffer.buffer;
for (int i = 0; i < buffer.length; i++)
hash = (hash * 37) ^ bytes [i];
return hash;
#else
const uint p = 16777619;
uint hash = 2166136261;
var bytes = buffer.buffer;
for (int i = 0; i < buffer.length; i++)
hash = (hash ^ bytes [i]) * p;
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5;
return (int) hash;
#endif
}
}
}

View File

@@ -0,0 +1,32 @@
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2015 Jb Evain
// Copyright (c) 2008 - 2011 Novell, Inc.
//
// Licensed under the MIT/X11 license.
//
using System;
using RVA = System.UInt32;
namespace Mono.Cecil.PE {
struct DataDirectory {
public readonly RVA VirtualAddress;
public readonly uint Size;
public bool IsZero {
get { return VirtualAddress == 0 && Size == 0; }
}
public DataDirectory (RVA rva, uint size)
{
this.VirtualAddress = rva;
this.Size = size;
}
}
}

View File

@@ -0,0 +1,189 @@
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2015 Jb Evain
// Copyright (c) 2008 - 2011 Novell, Inc.
//
// Licensed under the MIT/X11 license.
//
using System;
using System.IO;
using Mono.Cecil.Cil;
using Mono.Cecil.Metadata;
using RVA = System.UInt32;
namespace Mono.Cecil.PE {
sealed class Image : IDisposable {
public Disposable<Stream> Stream;
public string FileName;
public ModuleKind Kind;
public string RuntimeVersion;
public TargetArchitecture Architecture;
public ModuleCharacteristics Characteristics;
public Section [] Sections;
public Section MetadataSection;
public uint EntryPointToken;
public ModuleAttributes Attributes;
public DataDirectory Debug;
public DataDirectory Resources;
public DataDirectory StrongName;
public StringHeap StringHeap;
public BlobHeap BlobHeap;
public UserStringHeap UserStringHeap;
public GuidHeap GuidHeap;
public TableHeap TableHeap;
public PdbHeap PdbHeap;
readonly int [] coded_index_sizes = new int [14];
readonly Func<Table, int> counter;
public Image ()
{
counter = GetTableLength;
}
public bool HasTable (Table table)
{
return GetTableLength (table) > 0;
}
public int GetTableLength (Table table)
{
return (int) TableHeap [table].Length;
}
public int GetTableIndexSize (Table table)
{
return GetTableLength (table) < 65536 ? 2 : 4;
}
public int GetCodedIndexSize (CodedIndex coded_index)
{
var index = (int) coded_index;
var size = coded_index_sizes [index];
if (size != 0)
return size;
return coded_index_sizes [index] = coded_index.GetSize (counter);
}
public uint ResolveVirtualAddress (RVA rva)
{
var section = GetSectionAtVirtualAddress (rva);
if (section == null)
throw new ArgumentOutOfRangeException ();
return ResolveVirtualAddressInSection (rva, section);
}
public uint ResolveVirtualAddressInSection (RVA rva, Section section)
{
return rva + section.PointerToRawData - section.VirtualAddress;
}
public Section GetSection (string name)
{
var sections = this.Sections;
for (int i = 0; i < sections.Length; i++) {
var section = sections [i];
if (section.Name == name)
return section;
}
return null;
}
public Section GetSectionAtVirtualAddress (RVA rva)
{
var sections = this.Sections;
for (int i = 0; i < sections.Length; i++) {
var section = sections [i];
if (rva >= section.VirtualAddress && rva < section.VirtualAddress + section.SizeOfRawData)
return section;
}
return null;
}
BinaryStreamReader GetReaderAt (RVA rva)
{
var section = GetSectionAtVirtualAddress (rva);
if (section == null)
return null;
var reader = new BinaryStreamReader (Stream.value);
reader.MoveTo (ResolveVirtualAddressInSection (rva, section));
return reader;
}
public TRet GetReaderAt<TItem, TRet> (RVA rva, TItem item, Func<TItem, BinaryStreamReader, TRet> read) where TRet : class
{
var position = Stream.value.Position;
try {
var reader = GetReaderAt (rva);
if (reader == null)
return null;
return read (item, reader);
} finally {
Stream.value.Position = position;
}
}
public ImageDebugDirectory GetDebugHeader (out byte [] header)
{
var reader = GetReaderAt (Debug.VirtualAddress);
if (reader == null) {
header = Empty<byte>.Array;
return new ImageDebugDirectory ();
}
var directory = new ImageDebugDirectory {
Characteristics = reader.ReadInt32 (),
TimeDateStamp = reader.ReadInt32 (),
MajorVersion = reader.ReadInt16 (),
MinorVersion = reader.ReadInt16 (),
Type = reader.ReadInt32 (),
SizeOfData = reader.ReadInt32 (),
AddressOfRawData = reader.ReadInt32 (),
PointerToRawData = reader.ReadInt32 (),
};
reader = GetReaderAt ((uint) directory.AddressOfRawData);
header = reader != null
? reader.ReadBytes (directory.SizeOfData)
: Empty<byte>.Array;
return directory;
}
public bool HasDebugTables ()
{
return HasTable (Table.Document)
|| HasTable (Table.MethodDebugInformation)
|| HasTable (Table.LocalScope)
|| HasTable (Table.LocalVariable)
|| HasTable (Table.LocalConstant)
|| HasTable (Table.StateMachineMethod)
|| HasTable (Table.CustomDebugInformation);
}
public void Dispose ()
{
Stream.Dispose ();
}
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,24 @@
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2015 Jb Evain
// Copyright (c) 2008 - 2011 Novell, Inc.
//
// Licensed under the MIT/X11 license.
//
using System;
using RVA = System.UInt32;
namespace Mono.Cecil.PE {
sealed class Section {
public string Name;
public RVA VirtualAddress;
public uint VirtualSize;
public uint SizeOfRawData;
public uint PointerToRawData;
}
}

View File

@@ -0,0 +1,112 @@
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2015 Jb Evain
// Copyright (c) 2008 - 2011 Novell, Inc.
//
// Licensed under the MIT/X11 license.
//
using System;
#if !READ_ONLY
using RVA = System.UInt32;
namespace Mono.Cecil.PE {
enum TextSegment {
ImportAddressTable,
CLIHeader,
Code,
Resources,
Data,
StrongNameSignature,
// Metadata
MetadataHeader,
TableHeap,
StringHeap,
UserStringHeap,
GuidHeap,
BlobHeap,
PdbHeap,
// End Metadata
DebugDirectory,
ImportDirectory,
ImportHintNameTable,
StartupStub,
}
sealed class TextMap {
readonly Range [] map = new Range [17 /*Enum.GetValues (typeof (TextSegment)).Length*/];
public void AddMap (TextSegment segment, int length)
{
map [(int) segment] = new Range (GetStart (segment), (uint) length);
}
public void AddMap (TextSegment segment, int length, int align)
{
align--;
AddMap (segment, (length + align) & ~align);
}
public void AddMap (TextSegment segment, Range range)
{
map [(int) segment] = range;
}
public Range GetRange (TextSegment segment)
{
return map [(int) segment];
}
public DataDirectory GetDataDirectory (TextSegment segment)
{
var range = map [(int) segment];
return new DataDirectory (range.Length == 0 ? 0 : range.Start, range.Length);
}
public RVA GetRVA (TextSegment segment)
{
return map [(int) segment].Start;
}
public RVA GetNextRVA (TextSegment segment)
{
var i = (int) segment;
return map [i].Start + map [i].Length;
}
public int GetLength (TextSegment segment)
{
return (int) map [(int) segment].Length;
}
RVA GetStart (TextSegment segment)
{
var index = (int) segment;
return index == 0 ? ImageWriter.text_rva : ComputeStart (index);
}
RVA ComputeStart (int index)
{
index--;
return map [index].Start + map [index].Length;
}
public uint GetLength ()
{
var range = map [(int) TextSegment.StartupStub];
return range.Start - ImageWriter.text_rva + range.Length;
}
}
}
#endif