-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Add R2RDump support for Mach-O composite R2R images #121566
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c83a999
Add R2RDump support for Mach-O images
elinor-fung 57dde51
Move shared files under tools/Common/MachO
elinor-fung 37a1764
Simplify getting Machine, store MachHeader
elinor-fung 9d07517
Combine getting composite or regular header into one function
elinor-fung 7ab47c2
Move getting r2r manifest / standalone assembly metadata to image rea…
elinor-fung 3e51420
Handle DumpImageInformation and GetSections for Mach-O
elinor-fung dd169c8
Copilot comments
elinor-fung ff0656d
Use AsSpan instead of Substring
elinor-fung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...NET.HostModel/MachO/Enums/MachFileType.cs → .../tools/Common/MachO/Enums/MachFileType.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...tModel/MachO/Enums/MachLoadCommandType.cs → ...Common/MachO/Enums/MachLoadCommandType.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...ft.NET.HostModel/MachO/Enums/MachMagic.cs → ...clr/tools/Common/MachO/Enums/MachMagic.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
|
|
||
| #if HOST_MODEL | ||
| namespace Microsoft.NET.HostModel.MachO; | ||
| #else | ||
| namespace ILCompiler.Reflection.ReadyToRun.MachO; | ||
| #endif | ||
|
|
||
| /// <summary> | ||
| /// A managed object containing relevant information for AdHoc signing a Mach-O file. | ||
| /// The object is created from a memory mapped file, and a signature can be calculated from the memory mapped file. | ||
| /// However, since a memory mapped file cannot be extended, the signature is written to a file stream. | ||
| /// </summary> | ||
| internal unsafe partial class MachObjectFile | ||
| { | ||
| public static bool IsMachOImage(string filePath) | ||
| { | ||
| using (BinaryReader reader = new BinaryReader(File.OpenRead(filePath))) | ||
| { | ||
| if (reader.BaseStream.Length < 256) // Header size | ||
| { | ||
| return false; | ||
| } | ||
| uint magic = reader.ReadUInt32(); | ||
| return Enum.IsDefined(typeof(MachMagic), magic); | ||
| } | ||
| } | ||
| } |
6 changes: 1 addition & 5 deletions
6
src/coreclr/tools/aot/ILCompiler.ReadyToRun/CodeGen/ReadyToRunContainerFormat.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/IBinaryImageReader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Collections.Immutable; | ||
| using System.IO; | ||
| using System.Reflection.PortableExecutable; | ||
|
|
||
| namespace ILCompiler.Reflection.ReadyToRun | ||
| { | ||
| /// <summary> | ||
| /// Interface for abstracting binary image reading across different formats (PE, MachO) | ||
| /// </summary> | ||
| public interface IBinaryImageReader | ||
| { | ||
| /// <summary> | ||
| /// Gets the machine type of the binary image | ||
| /// </summary> | ||
| Machine Machine { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the operating system of the binary image | ||
| /// </summary> | ||
| OperatingSystem OperatingSystem { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the image base address | ||
| /// </summary> | ||
| ulong ImageBase { get; } | ||
|
|
||
| /// <summary> | ||
| /// Get the entire image content | ||
| /// </summary> | ||
| ImmutableArray<byte> GetEntireImage(); | ||
|
|
||
| /// <summary> | ||
| /// Get the index in the image byte array corresponding to the RVA | ||
| /// </summary> | ||
| /// <param name="rva">The relative virtual address</param> | ||
| int GetOffset(int rva); | ||
|
|
||
| /// <summary> | ||
| /// Try to get the ReadyToRun header RVA for this image. | ||
| /// </summary> | ||
| /// <param name="rva">RVA of the ReadyToRun header if available, 0 when not</param> | ||
| /// <param name="isComposite">true when the reader represents a composite ReadyToRun image, false for regular R2R</param> | ||
| /// <returns>true when the reader represents a ReadyToRun image (composite or regular), false otherwise</returns> | ||
| bool TryGetReadyToRunHeader(out int rva, out bool isComposite); | ||
|
|
||
| /// <summary> | ||
| /// Creates standalone assembly metadata from the image's embedded metadata. | ||
| /// </summary> | ||
| /// <returns>Assembly metadata, or null if the image has no embedded metadata</returns> | ||
| IAssemblyMetadata GetStandaloneAssemblyMetadata(); | ||
|
|
||
| /// <summary> | ||
| /// Creates manifest assembly metadata from the R2R manifest | ||
| /// </summary> | ||
| /// <param name="manifestReader">Manifest metadata reader</param> | ||
| /// <returns>Manifest assembly metadata</returns> | ||
| IAssemblyMetadata GetManifestAssemblyMetadata(System.Reflection.Metadata.MetadataReader manifestReader); | ||
|
|
||
| /// <summary> | ||
| /// Write out image information using the specified writer | ||
| /// </summary> | ||
| /// <param name="writer">The writer to use</param> | ||
| void DumpImageInformation(TextWriter writer); | ||
|
|
||
| /// <summary> | ||
| /// Gets the sections (name and size) of the binary image | ||
| /// </summary> | ||
| Dictionary<string, int> GetSections(); | ||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/MachO/BinaryFormat/NList64.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace ILCompiler.Reflection.ReadyToRun.MachO; | ||
|
|
||
| /// <summary> | ||
| /// Represents a 64-bit symbol table entry. | ||
| /// See https://github.com/apple-oss-distributions/cctools/blob/7a5450708479bbff61527d5e0c32a3f7b7e4c1d0/include/mach-o/nlist.h#L92 for reference. | ||
| /// </summary> | ||
| [StructLayout(LayoutKind.Sequential)] | ||
| internal readonly struct NList64 | ||
| { | ||
| private readonly uint _stringTableIndex; | ||
| private readonly byte _type; | ||
| private readonly byte _section; | ||
| private readonly ushort _description; | ||
| private readonly ulong _value; | ||
|
|
||
| public uint GetStringTableIndex(MachHeader header) => header.ConvertValue(_stringTableIndex); | ||
| public ulong GetValue(MachHeader header) => header.ConvertValue(_value); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.