-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add .mvid section to PE and remove dependency on MetadataReader #19133
Changes from 5 commits
53a66be
67c31d3
51a3c3f
4de0d32
ec9b2d4
b1ac2aa
4c02a2d
7df9479
786dbe7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
|
||
namespace Microsoft.CodeAnalysis.BuildTasks | ||
{ | ||
public static class MvidReader | ||
{ | ||
public static Guid ReadAssemblyMvidOrEmpty(Stream stream) | ||
{ | ||
return ReadAssemblyMvidOrEmpty(new BinaryReader(stream)); | ||
} | ||
|
||
private static Guid ReadAssemblyMvidOrEmpty(BinaryReader reader) | ||
{ | ||
Guid empty = Guid.Empty; | ||
|
||
// DOS Header (64), DOS Stub (64), PE Signature (4), COFF Header (20), PE Header (224), 1x Section Header (40) | ||
if (reader.BaseStream.Length < 64 + 64 + 4 + 20 + 224 + 40) | ||
{ | ||
return empty; | ||
} | ||
|
||
// DOS Header: PE (2) | ||
if (reader.ReadUInt16() != 0x5a4d) | ||
{ | ||
return empty; | ||
} | ||
|
||
// DOS Header: Start (58) | ||
Skip(58, reader); | ||
|
||
// DOS Header: Address of PE Signature | ||
MoveTo(reader.ReadUInt32(), reader); | ||
|
||
// PE Signature ('P' 'E' null null) | ||
if (reader.ReadUInt32() != 0x00004550) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This looks like an attempt to read at an unknown location, which can throw if we are near the end of the stream. #Closed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already checked above. #Resolved |
||
{ | ||
return empty; | ||
} | ||
|
||
// COFF Header: Machine (2) | ||
Skip(2, reader); | ||
|
||
// COFF Header: NumberOfSections (2) | ||
ushort sections = reader.ReadUInt16(); | ||
|
||
// COFF Header: TimeDateStamp (4), PointerToSymbolTable (4), NumberOfSymbols (4) | ||
Skip(12, reader); | ||
|
||
// COFF Header: OptionalHeaderSize (2) | ||
ushort optionalHeaderSize = reader.ReadUInt16(); | ||
|
||
// COFF Header: Characteristics (2) | ||
Skip(2, reader); | ||
|
||
// Optional header | ||
Skip(optionalHeaderSize, reader); | ||
|
||
// Section headers | ||
return FindMvidInSections(sections, reader); | ||
} | ||
|
||
private static Guid FindMvidInSections(ushort count, BinaryReader reader) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, thanks #Resolved |
||
{ | ||
// .mvid section must be first, if it's there | ||
// Section: Name (8) | ||
byte[] name = reader.ReadBytes(8); | ||
if (name.Length == 8 && name[0] == '.' && | ||
name[1] == 'm' && name[2] == 'v' && name[3] == 'i' && name[4] == 'd' && name[5] == '\0') | ||
{ | ||
// Section: VirtualSize (4) | ||
uint virtualSize = reader.ReadUInt32(); | ||
|
||
// Section: VirtualAddress (4), SizeOfRawData (4) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tmat One thing I noticed is that SizeOfRawData is 500. I wonder if there is a way to reduce that overhead by tweaking FileAlignment? After all, this section only contains 16 bytes. #Resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sadly, the PE spec requires the value of FileAlignment to be
|
||
Skip(8, reader); | ||
|
||
// Section: PointerToRawData (4) | ||
uint pointerToRawData = reader.ReadUInt32(); | ||
|
||
// The .mvid section only stores a Guid | ||
if (virtualSize != 16) | ||
{ | ||
Debug.Assert(false); | ||
return Guid.Empty; | ||
} | ||
|
||
if (MoveTo(pointerToRawData, reader)) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: no need for else since the if-statement returns #Resolved |
||
byte[] guidBytes = new byte[16]; | ||
if (reader.BaseStream.Read(guidBytes, 0, 16) == 16) | ||
{ | ||
return new Guid(guidBytes); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
|
||
return Guid.Empty; | ||
} | ||
|
||
private static void Skip(int bytes, BinaryReader reader) | ||
{ | ||
reader.BaseStream.Seek(bytes, SeekOrigin.Current); | ||
} | ||
|
||
private static bool MoveTo(uint position, BinaryReader reader) | ||
{ | ||
if (reader.BaseStream.Length < position) | ||
{ | ||
return false; | ||
} | ||
|
||
reader.BaseStream.Seek(position, SeekOrigin.Begin); | ||
return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We probably shouldn't assume that Seek succeeds. Consider |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These methods can be |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we checking the mvids are non-default? #Resolved