//! Native image segment placement and wire headers. @test export mod tests; use std::mem; /// Largest positive AUIPC/ADDI displacement. export constant DISPLACEMENT_MAX: u32 = 0x7ffff7ff; /// Largest negative AUIPC/ADDI displacement magnitude. constant DISPLACEMENT_MIN_MAGNITUDE: u64 = 0x80000000; /// Native image signature: the ASCII bytes "RADIANCE" as a little-endian u64. export constant MAGIC: u64 = 0x45434E4149444152; /// Fixed header size: signature, code and rodata records of address and size, /// and the rwdata record of address, size, and zero-filled tail. export constant HEADER_SIZE: u32 = 48; /// Offset of the code record. constant CODE_OFFSET: u32 = 8; /// Offset of the rodata record. constant RO_DATA_OFFSET: u32 = 20; /// Offset of the rwdata record. constant RW_DATA_OFFSET: u32 = 32; /// Size of a segment address in the header. constant ADDRESS_SIZE: u32 = 8; /// Size of a segment size or tail in the header. constant SIZE_SIZE: u32 = 4; /// Invalid native image placement. export union Error: Copy { /// A size or end address cannot be represented. Overflow, /// A segment violates its alignment requirement. Alignment, /// Segment memory intervals overlap. Overlap, /// Emitted data exceeds the extent laid out for it. Size, /// A PC-relative address load exceeds its instruction range. Relocation, /// The input is shorter than a header. Truncated, /// The input does not start with the native image signature. Signature, } /// Address and stored size of one image segment. export record Segment: Copy { /// Physical start address. address: u64, /// Number of bytes stored in the image file. size: u32, } /// Explicit native image layout. File payload order is code, rodata, rwdata. /// Execution starts at the first code byte. export record Layout: Copy { /// Executable instructions. code: Segment, /// Read-only data. roData: Segment, /// Writable data. rwData: Segment, /// Zero-filled bytes that follow the stored writable data in memory. rwTail: u32, } /// Check one segment's memory extent and required base alignment. /// `tail` is the number of zero-filled bytes after the stored bytes. fn segment(item: Segment, tail: u32, alignment: u64) throws (Error) { if item.size > mem::U32_MAX - tail { throw Error::Overflow; } let memory = item.size as u64 + tail as u64; if item.address > mem::U64_MAX - memory { throw Error::Overflow; } if memory > 0 and (item.address & (alignment - 1)) <> 0 { throw Error::Alignment; } } /// Check that two nonempty memory intervals are disjoint. fn disjoint(a: Segment, aMemory: u64, b: Segment, bMemory: u64) throws (Error) { if aMemory > 0 and bMemory > 0 and a.address < b.address + bMemory and b.address < a.address + aMemory { throw Error::Overlap; } } /// Validate segment extents, alignment, and overlap. export fn validate(layout: Layout) throws (Error) { try segment(layout.code, 0, 4); try segment(layout.roData, 0, 8); try segment(layout.rwData, layout.rwTail, 8); if (layout.code.size & 3) <> 0 { throw Error::Alignment; } let rwMemory = layout.rwData.size as u64 + layout.rwTail as u64; try disjoint(layout.code, layout.code.size as u64, layout.roData, layout.roData.size as u64); try disjoint(layout.code, layout.code.size as u64, layout.rwData, rwMemory); try disjoint(layout.roData, layout.roData.size as u64, layout.rwData, rwMemory); } /// Encode a validated native header in little-endian order. export fn header(layout: Layout) -> [u8; HEADER_SIZE] throws (Error) { try validate(layout); let mut bytes: [u8; HEADER_SIZE] = [0; HEADER_SIZE]; mem::writeLe(&mut bytes[..], 0, MAGIC, ADDRESS_SIZE); let segments = [layout.code, layout.roData, layout.rwData]; let offsets = [CODE_OFFSET, RO_DATA_OFFSET, RW_DATA_OFFSET]; for item, i in &segments[..] { mem::writeLe(&mut bytes[..], offsets[i], item.address, ADDRESS_SIZE); mem::writeLe(&mut bytes[..], offsets[i] + ADDRESS_SIZE, item.size as u64, SIZE_SIZE); } mem::writeLe( &mut bytes[..], RW_DATA_OFFSET + ADDRESS_SIZE + SIZE_SIZE, layout.rwTail as u64, SIZE_SIZE ); return bytes; } /// Decode and validate the header at the start of `bytes`. It is the inverse /// of [`header`]. Later bytes are not examined. export fn parse(bytes: &[u8]) -> Layout throws (Error) { if bytes.len < HEADER_SIZE { throw Error::Truncated; } if mem::readLe(bytes, 0, ADDRESS_SIZE) <> MAGIC { throw Error::Signature; } let code = segmentAt(bytes, CODE_OFFSET); let roData = segmentAt(bytes, RO_DATA_OFFSET); let rwData = segmentAt(bytes, RW_DATA_OFFSET); let rwTail = mem::readLe(bytes, RW_DATA_OFFSET + ADDRESS_SIZE + SIZE_SIZE, SIZE_SIZE); let layout = Layout { code, roData, rwData, rwTail: rwTail as u32 }; try validate(layout); return layout; } /// Decode the segment record at `offset` of a header. fn segmentAt(bytes: &[u8], offset: u32) -> Segment { return Segment { address: mem::readLe(bytes, offset, ADDRESS_SIZE), size: mem::readLe(bytes, offset + ADDRESS_SIZE, SIZE_SIZE) as u32, }; } /// Address policy for generated code and data. export union Placement: Copy { /// Hosted layout with code after read-only data. Hosted, /// Fixed physical segment addresses. Execution starts at `code`. Physical { /// Executable segment base. code: u64, /// Read-only segment base. roData: u64, /// Writable segment base. rwData: u64, }, } /// Compute a signed displacement for a two-instruction AUIPC/ADDI load. export fn displacement(source: u64, target: u64) -> ?i32 { if target >= source { let distance = target - source; // AUIPC sign-extends its upper immediate before ADDI applies the low part. if distance > DISPLACEMENT_MAX as u64 { return nil; } return distance as i32; } let distance = source - target; if distance > DISPLACEMENT_MIN_MAGNITUDE { return nil; } return -(distance as i64) as i32; }