Skip to content

Commit 3683bd5

Browse files
authored
Add initial doc for future R2R platform-native image (Mach-O, composite only) (#120584)
Create a doc with a tentative plan for the platform-native R2R support we are intending to add (Mach-O, composite only). We can update this as we go.
1 parent c01656e commit 3683bd5

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

docs/design/coreclr/botr/readytorun-format.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ The COR header and ECMA 335 metadata pointed to by the COM descriptor data direc
3030
in the COFF header represent a full copy of the input IL and MSIL metadata it was generated from.
3131

3232
**Composite R2R files** currently conform to Windows PE executable file format as the
33-
native envelope. Moving forward we plan to gradually add support for platform-native
34-
executable formats (ELF on Linux, MachO on OSX) as the native envelopes. There is a
33+
native envelope. Moving forward we [plan to gradually add support for platform-native
34+
executable formats](./readytorun-platform-native-envelope.md) (ELF on Linux, MachO on OSX) as the native envelopes. There is a
3535
global CLI / COR header in the file, but it only exists to facilitate pdb generation, and does
3636
not participate in any usages by the CoreCLR runtime. The ReadyToRun header structure is pointed to
3737
by the well-known export symbol `RTR_HEADER` and has the `READYTORUN_FLAG_COMPOSITE` flag set.

docs/design/coreclr/botr/readytorun-overview.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Some of this information can be omitted or stored in more efficient form, e.g.:
6060
- The garbage collection information can be omitted for environments with conservative garbage collection, such as IL2CPP.
6161
- The full metadata information is not strictly required for 'private' methods or types so it is possible to strip it from the CLI image.
6262
- The metadata can be stored in more efficient form, such as the .NET Native metadata format.
63-
- The platform native executable format (ELF, Mach-O) can be used as envelope instead of PE to take advantage of platform OS loader.
63+
- As of .NET 10, the PE format is used on all platforms. A [future improvement](#platform-native-envelope-support) could be support for the platform native executable format (ELF, Mach-O) to take advantage of platform OS loader.
6464

6565

6666
## Definition of Version Compatibility for Native Code
@@ -332,4 +332,8 @@ Another important observation is that `MethodTable` contains other very frequent
332332

333333
# Current State
334334

335-
The design and implementation is a work in progress under code name ReadyToRun (`FEATURE_READYTORUN`). RyuJIT is used as the code generator to produce the ReadyToRun images currently.
335+
As of .NET 6, the .NET SDK can publish applications as ReadyToRun using the `crossgen2` tool. The tool lives under `src/coreclr/aot/crossgen2`. Support in the coreclr runtime is under `FEATURE_READYTORUN`. RyuJIT is used as the code generator to produce the ReadyToRun images currently.
336+
337+
## Platform-Native Envelope Support
338+
339+
Through .NET 10, ReadyToRun uses the PE format on all platforms. In .NET 11, we plan to start adding support for other formats, with Mach-O being the first target. For more details, see [ReadyToRun Platform-Native Envelope](./readytorun-platform-native-envelope.md).
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# ReadyToRun Platform Native Envelope
2+
3+
Up through .NET 10, ReadyToRun (R2R) uses the Windows PE format as the native envelope on every platform. Non‑Windows platforms therefore load a PE file with the .NET loader performing the required fixups and code activation.
4+
5+
In .NET 11, we plan to start adding support beyond the PE format. We will target support for:
6+
- Composite R2R only
7+
- Mach-O object files emitted by `crossgen2`
8+
- Runtime using a composite R2R image that is a Mach-O shared library
9+
- Linking the object files into a shared library is expected to be handled by the SDK and is not covered in this document.
10+
11+
The tentative high-level design is outlined below. As we implement this support, this document should be updated with more details and the [ReadyToRun overview](./readytorun-overview.md) and [ReadyToRun format](./readytorun-format.md) should be updated to reflect the changes.
12+
13+
## crossgen2: producing Mach-O object files
14+
15+
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:
16+
- `--obj-format macho`
17+
18+
`crossgen2` will:
19+
- Produce a Mach-O object file as the composite R2R image with the `RTR_HEADER` export for the `READYTORUN_HEADER`.
20+
- Mark each input IL assembly as a component R2R assembly: `READYTORUN_FLAG_COMPONENT`.
21+
- 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`
22+
23+
`crossgen2` does not produce the final shared library. A separate SDK / build linking step must preserve the `RTR_HEADER` export in the final `dylib`.
24+
25+
## Runtime: consuming a platform-native R2R image
26+
27+
The runtime will be updated to handle platform-native R2R images during assembly load.
28+
29+
1. Load IL assembly and determine if it is a R2R assembly.
30+
2. If it is not a component R2R assembly, proceed with existing R2R load logic.
31+
- We will not have platform-native support for this scenario
32+
3. If it is a component R2R assembly with the new `READYTORUN_FLAG_PLATFORM_NATIVE_IMAGE` flag set:
33+
a. Read `OwnerCompositeExecutable` value.
34+
b. Invoke host callback with component assembly path and owner composite name.
35+
c. On success, obtain pointer to composite `READYTORUN_HEADER` and use it for native method lookup / fixups.
36+
d. On failure, fall back to IL/JIT path.
37+
4. If the platform-native flag is not set, proceed with existing R2R load logic (PE assembly lookup and load).
38+
39+
### Host callback
40+
41+
The [`host_runtime_contract`](/src/native/corehost/host_runtime_contract.h) will be updated with a new callback for getting native code information.
42+
43+
```c
44+
struct native_code_context
45+
{
46+
size_t size; // size of this struct
47+
const char* assembly_path; // component assembly path
48+
const char* owner_composite_name; // name from component R2R header
49+
};
50+
51+
struct native_code_data
52+
{
53+
size_t size; // size of this struct
54+
void* r2r_header_ptr; // ReadyToRun header
55+
size_t image_size; // size of the image
56+
void* image_base; // base address where the image was loaded
57+
};
58+
59+
bool get_native_code_data(
60+
const struct native_code_context* context,
61+
/*out*/ struct native_code_data* data
62+
);
63+
```
64+
65+
This leaves it to the host to do the actual load (for example, `dlopen` of a shared library, using something statically linked into the host itself) of the platform-native image. It is also responsible for any caching desired.

0 commit comments

Comments
 (0)