lib/std/arch/rv64/image.rad 6.1 KiB raw
1
//! Native image segment placement and wire headers.
2
3
@test export mod tests;
4
5
use std::mem;
6
7
/// Largest positive AUIPC/ADDI displacement.
8
export constant DISPLACEMENT_MAX: u32 = 0x7ffff7ff;
9
/// Largest negative AUIPC/ADDI displacement magnitude.
10
constant DISPLACEMENT_MIN_MAGNITUDE: u64 = 0x80000000;
11
12
/// Native image signature: the ASCII bytes "RADIANCE" as a little-endian u64.
13
export constant MAGIC: u64 = 0x45434E4149444152;
14
/// Fixed header size: signature, code and rodata records of address and size,
15
/// and the rwdata record of address, size, and zero-filled tail.
16
export constant HEADER_SIZE: u32 = 48;
17
/// Offset of the code record.
18
constant CODE_OFFSET: u32 = 8;
19
/// Offset of the rodata record.
20
constant RO_DATA_OFFSET: u32 = 20;
21
/// Offset of the rwdata record.
22
constant RW_DATA_OFFSET: u32 = 32;
23
/// Size of a segment address in the header.
24
constant ADDRESS_SIZE: u32 = 8;
25
/// Size of a segment size or tail in the header.
26
constant SIZE_SIZE: u32 = 4;
27
28
/// Invalid native image placement.
29
export union Error: Copy {
30
    /// A size or end address cannot be represented.
31
    Overflow,
32
    /// A segment violates its alignment requirement.
33
    Alignment,
34
    /// Segment memory intervals overlap.
35
    Overlap,
36
    /// Emitted data exceeds the extent laid out for it.
37
    Size,
38
    /// A PC-relative address load exceeds its instruction range.
39
    Relocation,
40
    /// The input is shorter than a header.
41
    Truncated,
42
    /// The input does not start with the native image signature.
43
    Signature,
44
}
45
46
/// Address and stored size of one image segment.
47
export record Segment: Copy {
48
    /// Physical start address.
49
    address: u64,
50
    /// Number of bytes stored in the image file.
51
    size: u32,
52
}
53
54
/// Explicit native image layout. File payload order is code, rodata, rwdata.
55
/// Execution starts at the first code byte.
56
export record Layout: Copy {
57
    /// Executable instructions.
58
    code: Segment,
59
    /// Read-only data.
60
    roData: Segment,
61
    /// Writable data.
62
    rwData: Segment,
63
    /// Zero-filled bytes that follow the stored writable data in memory.
64
    rwTail: u32,
65
}
66
67
/// Check one segment's memory extent and required base alignment.
68
/// `tail` is the number of zero-filled bytes after the stored bytes.
69
fn segment(item: Segment, tail: u32, alignment: u64) throws (Error) {
70
    if item.size > mem::U32_MAX - tail {
71
        throw Error::Overflow;
72
    }
73
    let memory = item.size as u64 + tail as u64;
74
    if item.address > mem::U64_MAX - memory {
75
        throw Error::Overflow;
76
    }
77
    if memory > 0 and (item.address & (alignment - 1)) <> 0 {
78
        throw Error::Alignment;
79
    }
80
}
81
82
/// Check that two nonempty memory intervals are disjoint.
83
fn disjoint(a: Segment, aMemory: u64, b: Segment, bMemory: u64) throws (Error) {
84
    if aMemory > 0
85
        and bMemory > 0
86
        and a.address < b.address + bMemory
87
        and b.address < a.address + aMemory
88
    {
89
        throw Error::Overlap;
90
    }
91
}
92
93
/// Validate segment extents, alignment, and overlap.
94
export fn validate(layout: Layout) throws (Error) {
95
    try segment(layout.code, 0, 4);
96
    try segment(layout.roData, 0, 8);
97
    try segment(layout.rwData, layout.rwTail, 8);
98
    if (layout.code.size & 3) <> 0 {
99
        throw Error::Alignment;
100
    }
101
    let rwMemory = layout.rwData.size as u64 + layout.rwTail as u64;
102
    try disjoint(layout.code, layout.code.size as u64, layout.roData, layout.roData.size as u64);
103
    try disjoint(layout.code, layout.code.size as u64, layout.rwData, rwMemory);
104
    try disjoint(layout.roData, layout.roData.size as u64, layout.rwData, rwMemory);
105
}
106
107
/// Encode a validated native header in little-endian order.
108
export fn header(layout: Layout) -> [u8; HEADER_SIZE] throws (Error) {
109
    try validate(layout);
110
    let mut bytes: [u8; HEADER_SIZE] = [0; HEADER_SIZE];
111
    mem::writeLe(&mut bytes[..], 0, MAGIC, ADDRESS_SIZE);
112
    let segments = [layout.code, layout.roData, layout.rwData];
113
    let offsets = [CODE_OFFSET, RO_DATA_OFFSET, RW_DATA_OFFSET];
114
    for item, i in &segments[..] {
115
        mem::writeLe(&mut bytes[..], offsets[i], item.address, ADDRESS_SIZE);
116
        mem::writeLe(&mut bytes[..], offsets[i] + ADDRESS_SIZE, item.size as u64, SIZE_SIZE);
117
    }
118
    mem::writeLe(
119
        &mut bytes[..], RW_DATA_OFFSET + ADDRESS_SIZE + SIZE_SIZE, layout.rwTail as u64, SIZE_SIZE
120
    );
121
    return bytes;
122
}
123
124
/// Decode and validate the header at the start of `bytes`. It is the inverse
125
/// of [`header`]. Later bytes are not examined.
126
export fn parse(bytes: &[u8]) -> Layout throws (Error) {
127
    if bytes.len < HEADER_SIZE {
128
        throw Error::Truncated;
129
    }
130
    if mem::readLe(bytes, 0, ADDRESS_SIZE) <> MAGIC {
131
        throw Error::Signature;
132
    }
133
    let code = segmentAt(bytes, CODE_OFFSET);
134
    let roData = segmentAt(bytes, RO_DATA_OFFSET);
135
    let rwData = segmentAt(bytes, RW_DATA_OFFSET);
136
    let rwTail = mem::readLe(bytes, RW_DATA_OFFSET + ADDRESS_SIZE + SIZE_SIZE, SIZE_SIZE);
137
    let layout = Layout { code, roData, rwData, rwTail: rwTail as u32 };
138
    try validate(layout);
139
    return layout;
140
}
141
142
/// Decode the segment record at `offset` of a header.
143
fn segmentAt(bytes: &[u8], offset: u32) -> Segment {
144
    return Segment {
145
        address: mem::readLe(bytes, offset, ADDRESS_SIZE),
146
        size: mem::readLe(bytes, offset + ADDRESS_SIZE, SIZE_SIZE) as u32,
147
    };
148
}
149
150
/// Address policy for generated code and data.
151
export union Placement: Copy {
152
    /// Hosted layout with code after read-only data.
153
    Hosted,
154
    /// Fixed physical segment addresses. Execution starts at `code`.
155
    Physical {
156
        /// Executable segment base.
157
        code: u64,
158
        /// Read-only segment base.
159
        roData: u64,
160
        /// Writable segment base.
161
        rwData: u64,
162
    },
163
}
164
165
/// Compute a signed displacement for a two-instruction AUIPC/ADDI load.
166
export fn displacement(source: u64, target: u64) -> ?i32 {
167
    if target >= source {
168
        let distance = target - source;
169
        // AUIPC sign-extends its upper immediate before ADDI applies the low part.
170
        if distance > DISPLACEMENT_MAX as u64 {
171
            return nil;
172
        }
173
        return distance as i32;
174
    }
175
    let distance = source - target;
176
    if distance > DISPLACEMENT_MIN_MAGNITUDE {
177
        return nil;
178
    }
179
    return -(distance as i64) as i32;
180
}