Skip to content
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

Roslyn wrote incorrect timedatestime to PE Coff Header of a .Net 8.0 program file #74200

Open
CuteLeon opened this issue Jun 28, 2024 · 2 comments

Comments

@CuteLeon
Copy link

Version Used:
How can I get compiler's version?
Steps to Reproduce:

  1. Create a new .Net 8.0 WinFrom application
  2. Add below method in above application
    public static DateTime? GetBuildDateTime(this Assembly assembly)
    {
        var location = assembly.Location;
        var buildDateTime = default(DateTime?);
        try
        {
            using var fileStream = new FileStream(location, FileMode.Open, FileAccess.Read);
            using var peReader = new PEReader(fileStream);
            buildDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)
                .AddSeconds(peReader.PEHeaders.CoffHeader.TimeDateStamp)
                .ToLocalTime();
        }
        finally
        {
        }
        return buildDateTime;
    }
  1. Call above method and pass 'Assembly.GetEntryAssembly()' in

Diagnostic Id:

Expected Behavior:
I hope this method will read a correct compile time from PEHeaders.CoffHeader.TimeDateStamp;

Actual Behavior:
I got a negative integer from PEHeaders.CoffHeader.TimeDateStamp, and incorrect compile time;

Some details:
Bytes wrotten in binary program file:
image
Hex value readed from PEReader:
image
Got build date time:
image

@CuteLeon CuteLeon changed the title Roslyn wrote icorrect timedatestime to PE Coff Header of a .Net 8.0 program file Roslyn wrote incorrect timedatestime to PE Coff Header of a .Net 8.0 program file Jun 28, 2024
@CuteLeon
Copy link
Author

I found below content in https://github.com/dotnet/runtime/blob/main/docs/design/specs/PE-COFF.md#deterministic-pecoff-file, think TimeDateStamp doesn't mean compile date time any more, what will be the substitute?

The value of field TimeDateStamp in COFF File Header of a deterministic PE/COFF file does not indicate the date and time when the file was produced and should not be interpreted that way. Instead the value of the field is derived from a hash of the file content.

@CuteLeon
Copy link
Author

CuteLeon commented Jul 1, 2024

I implement another solution is that: Create a new AssemblyCompileDateTimeAttribute class and a SourceGenerator project to inject AssemblyCompileDateTimeAttribute declaretion into each project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant