R2RDump - Non-Windows machine code (dotnet/coreclr#19638)
authorAmy <amycmyu@gmail.com>
Sun, 26 Aug 2018 03:55:56 +0000 (20:55 -0700)
committerGitHub <noreply@github.com>
Sun, 26 Aug 2018 03:55:56 +0000 (20:55 -0700)
* Get machine architecture for non-Windows images

* Update readme

* Throw exception if machine is invalid, output OS

* Capitalization

Commit migrated from https://github.com/dotnet/coreclr/commit/5d2ae2fc7f97f31fa2730fadfc56b832299fd9e0

src/coreclr/src/tools/r2rdump/R2RReader.cs
src/coreclr/src/tools/r2rdump/TextDumper.cs

index 549415f..d73fdc8 100644 (file)
@@ -43,6 +43,19 @@ namespace R2RDump
         }
     }
 
+    /// <summary>
+    /// based on <a href="https://github.com/dotnet/coreclr/blob/master/src/inc/pedecoder.h">src/inc/pedecoder.h</a> IMAGE_FILE_MACHINE_NATIVE_OS_OVERRIDE
+    /// </summary>
+    public enum OperatingSystem
+    {
+        Apple = 0x4644,
+        FreeBSD = 0xADC4,
+        Linux = 0x7B79,
+        NetBSD = 0x1993,
+        Windows = 0,
+        Unknown = -1
+    }
+
     public class R2RReader
     {
         /// <summary>
@@ -76,6 +89,8 @@ namespace R2RDump
         /// </summary>
         public Machine Machine { get; set; }
 
+        public OperatingSystem OS { get; set; }
+
         /// <summary>
         /// The preferred address of the first byte of image when loaded into memory; 
         /// must be a multiple of 64K.
@@ -127,11 +142,20 @@ namespace R2RDump
                     throw new BadImageFormatException("The file is not a ReadyToRun image");
                 }
 
-                Machine = PEReader.PEHeaders.CoffHeader.Machine;
-                               if (!Machine.IsDefined(typeof(Machine), Machine))
+                uint machine = (uint)PEReader.PEHeaders.CoffHeader.Machine;
+                OS = OperatingSystem.Unknown;
+                foreach(OperatingSystem os in Enum.GetValues(typeof(OperatingSystem)))
+                {
+                    Machine = (Machine)(machine ^ (uint)os);
+                    if (Enum.IsDefined(typeof(Machine), Machine))
+                    {
+                        OS = os;
+                        break;
+                    }
+                }
+                if (OS == OperatingSystem.Unknown)
                 {
-                    R2RDump.WriteWarning($"Invalid Machine: {Machine}");
-                    Machine = Machine.Amd64;
+                    throw new BadImageFormatException($"Invalid Machine: {machine}");
                 }
                 ImageBase = PEReader.PEHeaders.PEHeader.ImageBase;
 
index 1406fd2..e7b249e 100644 (file)
@@ -26,6 +26,7 @@ namespace R2RDump
         internal override void Begin()
         {
             _writer.WriteLine($"Filename: {_r2r.Filename}");
+            _writer.WriteLine($"OS: {_r2r.OS}");
             _writer.WriteLine($"Machine: {_r2r.Machine}");
             _writer.WriteLine($"ImageBase: 0x{_r2r.ImageBase:X8}");
             SkipLine();