Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions docs/design/coreclr/botr/readytorun-platform-native-envelope.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ The tentative high-level design is outlined below. As we implement this support,
## crossgen2: producing Mach-O object files

Mach‑O support will only be supported for composite ReadyToRun when the target OS is macOS. It will be opt-in via a new `crossgen2` flag:

- `--obj-format macho`

`crossgen2` will:

- Produce a Mach-O object file as the composite R2R image with the `RTR_HEADER` export for the `READYTORUN_HEADER`.
- Mark each input IL assembly as a component R2R assembly: `READYTORUN_FLAG_COMPONENT`.
- Mark each input IL assembly with a new flag indicating that the associated composite image is in the platform-native format: `READYTORUN_FLAG_PLATFORM_NATIVE_IMAGE`
Expand All @@ -28,20 +30,14 @@ There's a few cases in the R2R format that are not natively represented in the M

#### Sections

Sections folded into `__TEXT,__text` that is in other sections in the PE envelope:

- CLR metadata: In the PE format, put in .cormeta, corresponds to the PE Header's "COR Header directory"
- Win32 Resources: In the PE format, put in .rsrc, corresponds to the PE Header's "Win32 Resources Header directory"
- Managed Unwind Info: In the Mach-O format, this section is expected to be in the Mach-O unwind format. The unwind info used by the runtime must be in the Windows unwind format.
- GC Info: Entries correspond to the unwind info.

Data moved out of `__TEXT,__text`:

- Precompiled managed code has been moved into `__TEXT,__managedcode`. `__TEXT,__text` gets special treatment by the linker and `__TEXT,__managedcode` matches NativeAOT.
- Read-only data such as jump tables, CLR metadata, Win32 Resources, managed unwind info, gc info, and the R2R headers are moved to `__TEXT,__const`

Data that stays in the corresponding locations as the PE envelope:

- Read-only data such as jump tables and the R2R headers: `__TEXT,__const`
- Read-write data, such as fixup tables: `__DATA,__data`
- Import thunks: `__TEXT,__text`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public ObjectNodeSection(string name, SectionType type, string comdatName)
public ObjectNodeSection(string name, SectionType type) : this(name, type, null)
{ }

public static readonly ObjectNodeSection XDataSection = new ObjectNodeSection("xdata", SectionType.ReadOnly);
public static readonly ObjectNodeSection DataSection = new ObjectNodeSection("data", SectionType.Writeable);
public static readonly ObjectNodeSection ReadOnlyDataSection = new ObjectNodeSection("rdata", SectionType.ReadOnly);
public static readonly ObjectNodeSection FoldableReadOnlyDataSection = new ObjectNodeSection("rdata", SectionType.ReadOnly);
Expand All @@ -51,10 +50,5 @@ public ObjectNodeSection(string name, SectionType type) : this(name, type, null)

public static readonly ObjectNodeSection ModulesWindowsContentSection = new ObjectNodeSection(".modules$I", SectionType.ReadOnly);
public static readonly ObjectNodeSection ModulesUnixContentSection = new ObjectNodeSection("__modules", SectionType.Writeable);

public static readonly ObjectNodeSection DebugDirectorySection = new ObjectNodeSection("debug", SectionType.Debug);
public static readonly ObjectNodeSection CorMetaSection = new ObjectNodeSection("cormeta", SectionType.ReadOnly);
public static readonly ObjectNodeSection Win32ResourcesSection = new ObjectNodeSection("rsrc", SectionType.ReadOnly);
public static readonly ObjectNodeSection PDataSection = new ObjectNodeSection("pdata", SectionType.ReadOnly);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public virtual int CompareToImpl(ISortableNode other, CompilerComparer comparer)
throw new NotImplementedException("Multiple nodes of this type are not supported");
}

protected enum ObjectNodePhase
protected internal enum ObjectNodePhase
{
/// <summary>
/// Nodes should only be placed in this phase if they have strict output ordering requirements that
Expand All @@ -49,7 +49,7 @@ protected enum ObjectNodePhase
Late,
}

protected enum ObjectNodeOrder
protected internal enum ObjectNodeOrder
{
//
// The ordering of this sequence of nodes is deliberate and currently required for
Expand All @@ -66,7 +66,9 @@ protected enum ObjectNodeOrder
ImportSectionsTableNode,
ImportSectionNode,
MethodEntrypointTableNode,

DebugDirectoryNode,
RuntimeFunctionsGCInfoNode,
RuntimeFunctionsTableNode,

//
// NativeAOT Nodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ private protected override void CreateSection(ObjectNodeSection section, Utf8Str
{
SectionType.ReadOnly =>
SectionCharacteristics.MemRead | SectionCharacteristics.ContainsInitializedData,
SectionType.UnwindData =>
SectionCharacteristics.MemRead | SectionCharacteristics.ContainsInitializedData,
SectionType.Writeable =>
SectionCharacteristics.MemRead | SectionCharacteristics.MemWrite |
SectionCharacteristics.ContainsInitializedData,
Expand All @@ -102,7 +104,7 @@ private protected override void CreateSection(ObjectNodeSection section, Utf8Str
}
};

if (section == DebugTypesSection || section == ObjectNodeSection.DebugDirectorySection)
if (section == DebugTypesSection)
{
sectionHeader.SectionCharacteristics =
SectionCharacteristics.MemRead | SectionCharacteristics.ContainsInitializedData |
Expand Down
23 changes: 23 additions & 0 deletions src/coreclr/tools/Common/Compiler/ObjectWriter/ObjectWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ private protected ObjectWriter(NodeFactory factory, ObjectWritingOptions options

protected internal abstract void UpdateSectionAlignment(int sectionIndex, int alignment);

/// <summary>
/// Get the section in the image where nodes in the passed in section should actually be emitted.
/// </summary>
/// <param name="section">A node's requested section.</param>
/// <returns>The section to actually emit the node into.</returns>
/// <remarks>
/// Sections in an image can be very expensive, and unlike linkable formats,
/// sections cannot be merged after the fact.
/// This method allows formats that want to merge sections during emit to do so.
/// </remarks>
private protected virtual ObjectNodeSection GetEmitSection(ObjectNodeSection section) => section;

private protected SectionWriter GetOrCreateSection(ObjectNodeSection section)
=> GetOrCreateSection(section, default, default);

Expand All @@ -87,6 +99,8 @@ private protected SectionWriter GetOrCreateSection(ObjectNodeSection section, Ut
int sectionIndex;
SectionData sectionData;

section = GetEmitSection(section);

if (!comdatName.IsNull || !_sectionNameToSectionIndex.TryGetValue(section.Name, out sectionIndex))
{
sectionData = new SectionData(section.Type == SectionType.Executable ? _insPaddingByte : (byte)0);
Expand Down Expand Up @@ -426,6 +440,11 @@ public virtual void EmitObject(Stream outputFileStream, IReadOnlyCollection<Depe

_outputInfoBuilder?.AddSymbol(new OutputSymbol(sectionWriter.SectionIndex, (ulong)(sectionWriter.Position + n.Offset), alternateCName));
}

if (node.Phase == (int)SortableDependencyNode.ObjectNodePhase.Ordered)
{
RecordWellKnownSymbol(currentSymbolName, (SortableDependencyNode.ObjectNodeOrder)node.ClassCode);
}
}

if (nodeContents.Relocs is not null)
Expand Down Expand Up @@ -590,6 +609,10 @@ public virtual void EmitObject(Stream outputFileStream, IReadOnlyCollection<Depe
}
}

private protected virtual void RecordWellKnownSymbol(Utf8String currentSymbolName, SortableDependencyNode.ObjectNodeOrder classCode)
{
}

private protected virtual void EmitSymbolRangeDefinition(Utf8String rangeNodeName, Utf8String startNodeName, Utf8String endNodeName, SymbolDefinition endSymbol)
{
if (!_definedSymbols.TryGetValue(startNodeName, out var startSymbol))
Expand Down
Loading
Loading