.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
rv64/
asm/
image/
shared/
tests/
catalog.rad
11.6 KiB
tests.rad
22.7 KiB
tests/
asm.rad
32.4 KiB
atomics.rad
5.1 KiB
decode.rad
27.1 KiB
emit.rad
34.6 KiB
encode.rad
30.9 KiB
image.rad
6.1 KiB
isel.rad
138.3 KiB
printer.rad
19.2 KiB
shared.rad
21.1 KiB
tests.rad
73.3 KiB
vector.rad
59.4 KiB
rv64.rad
22.4 KiB
bits/
char/
collections/
fs/
hash/
lang/
mem/
sys/
arch.rad
68 B
bits.rad
1.6 KiB
char.rad
1.5 KiB
collections.rad
39 B
fmt.rad
11.6 KiB
fs.rad
11.0 KiB
hash.rad
41 B
intrinsics.rad
467 B
io.rad
1.7 KiB
lang.rad
335 B
mem.rad
12.0 KiB
mem.ras
919 B
process.rad
6.2 KiB
std.manifest
4.9 KiB
std.rad
349 B
sys.rad
1.0 KiB
testing.rad
2.4 KiB
tests.rad
26.6 KiB
vec.rad
3.2 KiB
scripts/
seed/
sublime/
test/
vim/
.gitignore
333 B
.gitsigners
224 B
AGENTS.md
8.3 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
2.5 KiB
README
3.4 KiB
STYLE
3.2 KiB
std.lib
2.1 KiB
std.lib.test
1.1 KiB
lib/std/arch/rv64/shared/catalog.rad
raw
| 1 | //! Persistent native boot catalogs for the RV64 record ABI. |
| 2 | //! |
| 3 | //! The kernel reads a catalog in place as [`Entry`] records, so each record is |
| 4 | //! written at the byte offsets of the compiler's native layout. The constants |
| 5 | //! below name those offsets, and `shared/tests.rad` compares every one of them |
| 6 | //! with the layout that the compiler gives the records, so that a change of |
| 7 | //! the layout fails a test. All integers are little-endian. |
| 8 | |
| 9 | use std::arch::rv64::shared; |
| 10 | use std::lang::il::binary; |
| 11 | use std::mem; |
| 12 | |
| 13 | /// Binary package identity and its resident native descriptor. |
| 14 | export record Entry: Copy { |
| 15 | /// Complete immutable binary RIL bytes. |
| 16 | source: *[u8], |
| 17 | /// Shared code, exports, and private-state template. |
| 18 | package: shared::Package, |
| 19 | } |
| 20 | |
| 21 | /// Size of a native slice: data address, length, and capacity. |
| 22 | export constant SLICE_SIZE: u32 = 16; |
| 23 | /// Alignment of a native slice. |
| 24 | export constant SLICE_ALIGN: u32 = 8; |
| 25 | /// Offset of the length in a native slice. The capacity follows it. |
| 26 | export constant SLICE_LEN: u32 = 8; |
| 27 | /// Offset of the capacity in a native slice. |
| 28 | export constant SLICE_CAP: u32 = 12; |
| 29 | |
| 30 | /// Size of a native [`Entry`]. |
| 31 | export constant ENTRY_SIZE: u32 = 240; |
| 32 | /// Alignment of a native [`Entry`]. |
| 33 | export constant ENTRY_ALIGN: u32 = 8; |
| 34 | /// Offset of `Entry.source`. |
| 35 | export constant ENTRY_SOURCE: u32 = 0; |
| 36 | /// Offset of `Entry.package.name`. |
| 37 | export constant ENTRY_NAME: u32 = 16; |
| 38 | /// Offset of `Entry.package.dependencies`. |
| 39 | export constant ENTRY_DEPENDENCIES: u32 = 32; |
| 40 | /// Offset of `Entry.package.slot`. |
| 41 | export constant ENTRY_SLOT: u32 = 48; |
| 42 | /// Offset of `Entry.package.codeAddress`. |
| 43 | export constant ENTRY_CODE_ADDRESS: u32 = 56; |
| 44 | /// Offset of `Entry.package.code`. |
| 45 | export constant ENTRY_CODE: u32 = 64; |
| 46 | /// Offset of `Entry.package.exports`. |
| 47 | export constant ENTRY_EXPORTS: u32 = 80; |
| 48 | /// Offset of the one-byte presence tag of `Entry.package.entry`: one when |
| 49 | /// present. The padding up to the value is zero. |
| 50 | export constant ENTRY_ENTRY_TAG: u32 = 96; |
| 51 | /// Offset of the value of `Entry.package.entry`. |
| 52 | export constant ENTRY_ENTRY_VALUE: u32 = 104; |
| 53 | /// Offset of `Entry.package.template`. |
| 54 | export constant ENTRY_TEMPLATE: u32 = 112; |
| 55 | /// Offset of `Entry.package.memory`. |
| 56 | export constant ENTRY_MEMORY: u32 = 128; |
| 57 | /// Offset of `Entry.package.alignment`. |
| 58 | export constant ENTRY_ALIGNMENT: u32 = 132; |
| 59 | /// Offset of `Entry.package.relocations`. |
| 60 | export constant ENTRY_RELOCATIONS: u32 = 136; |
| 61 | |
| 62 | /// Size of a native [`shared::Symbol`]. |
| 63 | export constant SYMBOL_SIZE: u32 = 40; |
| 64 | /// Alignment of a native [`shared::Symbol`]. |
| 65 | export constant SYMBOL_ALIGN: u32 = 8; |
| 66 | /// Offset of `Symbol.name`. |
| 67 | export constant SYMBOL_NAME: u32 = 0; |
| 68 | /// Offset of the one-byte variant tag of `Symbol.target`: zero for a function, |
| 69 | /// one for data. The padding up to the payload is zero. |
| 70 | export constant SYMBOL_TAG: u32 = 16; |
| 71 | /// Offset of the payload of `Symbol.target`: the address of a function, or the |
| 72 | /// slot and the offset of a data reference as two 32-bit integers. |
| 73 | export constant SYMBOL_VALUE: u32 = 24; |
| 74 | |
| 75 | /// Offset of the exact dependency pin slice in the native package contract. |
| 76 | export constant ENTRY_PINS: u32 = 152; |
| 77 | /// Offset of the verification policy tag of the native package contract. |
| 78 | export constant ENTRY_VERIFICATION: u32 = 201; |
| 79 | /// Offset of the optional exact image identity tag. |
| 80 | export constant ENTRY_IMAGE_TAG: u32 = 168; |
| 81 | /// Offset of the 32 identity bytes when the identity is present. |
| 82 | export constant ENTRY_IMAGE_VALUE: u32 = 169; |
| 83 | /// Offset of the package compiler metadata hash. |
| 84 | export constant ENTRY_METADATA: u32 = 202; |
| 85 | /// Size in bytes of one 256-bit dependency identity. |
| 86 | export constant PIN_SIZE: u32 = 32; |
| 87 | |
| 88 | /// Size of a native [`shared::Relocation`]. |
| 89 | export constant RELOCATION_SIZE: u32 = 16; |
| 90 | /// Alignment of a native [`shared::Relocation`]. |
| 91 | export constant RELOCATION_ALIGN: u32 = 4; |
| 92 | /// Offset of `Relocation.offset`. |
| 93 | export constant RELOCATION_OFFSET: u32 = 0; |
| 94 | /// Offset of `Relocation.count`. |
| 95 | export constant RELOCATION_COUNT: u32 = 4; |
| 96 | /// Offset of `Relocation.target.slot`. |
| 97 | export constant RELOCATION_SLOT: u32 = 8; |
| 98 | /// Offset of `Relocation.target.offset`. |
| 99 | export constant RELOCATION_TARGET_OFFSET: u32 = 12; |
| 100 | |
| 101 | /// Offset of the verified context-entry classification of a public symbol. |
| 102 | export constant SYMBOL_STARTABLE: u32 = 32; |
| 103 | |
| 104 | /// Reserve aligned bytes in the native catalog output. |
| 105 | fn reserve(output: &mut [u8], used: &mut u32, size: u32, alignment: u32) -> u32 |
| 106 | throws (shared::Error) |
| 107 | { |
| 108 | let padding = (alignment - (*used & (alignment - 1))) & (alignment - 1); |
| 109 | if padding > output.len - *used { |
| 110 | throw shared::Error::Capacity; |
| 111 | } |
| 112 | let offset = *used + padding; |
| 113 | if size > output.len - offset { |
| 114 | throw shared::Error::Capacity; |
| 115 | } |
| 116 | for i in *used .. offset { |
| 117 | set output[i] = 0; |
| 118 | } |
| 119 | set *used = offset + size; |
| 120 | return offset; |
| 121 | } |
| 122 | |
| 123 | /// Write one little-endian integer into a reserved native field. |
| 124 | fn integer(output: &mut [u8], offset: u32, value: u64, width: u32) { |
| 125 | for i in 0 .. width { |
| 126 | set output[offset + i] = (value >> (i as u64 * 8)) as u8; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /// Write the RV64 pointer, length, and capacity of a native immutable slice. |
| 131 | fn slice(output: &mut [u8], offset: u32, address: u64, length: u32) { |
| 132 | integer(output, offset, address, 8); |
| 133 | integer(output, offset + SLICE_LEN, length as u64, 4); |
| 134 | integer(output, offset + SLICE_CAP, length as u64, 4); |
| 135 | } |
| 136 | |
| 137 | /// Copy bytes into the catalog and return their target-relative offset. |
| 138 | fn bytes(input: *[u8], output: &mut [u8], used: &mut u32) -> u32 throws (shared::Error) { |
| 139 | let offset = try reserve(output, used, input.len, 1); |
| 140 | try! mem::copy(&mut output[offset .. offset + input.len], input); |
| 141 | return offset; |
| 142 | } |
| 143 | |
| 144 | /// Copy a dependency list and each name into native catalog storage. |
| 145 | fn dependencies(input: *[*[u8]], base: u64, output: &mut [u8], used: &mut u32) -> u32 |
| 146 | throws (shared::Error) |
| 147 | { |
| 148 | if input.len > output.len / SLICE_SIZE { |
| 149 | throw shared::Error::Capacity; |
| 150 | } |
| 151 | let offset = try reserve(output, used, input.len * SLICE_SIZE, SLICE_ALIGN); |
| 152 | for name, i in input { |
| 153 | let at = try bytes(name, output, used); |
| 154 | slice(output, offset + i * SLICE_SIZE, base + at as u64, name.len); |
| 155 | } |
| 156 | return offset; |
| 157 | } |
| 158 | |
| 159 | /// Copy dependency identities into immutable native metadata. |
| 160 | fn pins(input: *[binary::Id], output: &mut [u8], used: &mut u32) -> u32 |
| 161 | throws (shared::Error) |
| 162 | { |
| 163 | if input.len > output.len / PIN_SIZE { |
| 164 | throw shared::Error::Capacity; |
| 165 | } |
| 166 | let offset = try reserve(output, used, input.len * PIN_SIZE, 1); |
| 167 | for pin, i in input { |
| 168 | let at = offset + i * PIN_SIZE; |
| 169 | try! mem::copy(&mut output[at .. at + PIN_SIZE], &pin.bytes[..]); |
| 170 | } |
| 171 | return offset; |
| 172 | } |
| 173 | |
| 174 | /// Copy public symbols and their names into native catalog storage. |
| 175 | fn symbols(input: *[shared::Symbol], base: u64, output: &mut [u8], used: &mut u32) -> u32 |
| 176 | throws (shared::Error) |
| 177 | { |
| 178 | if input.len > output.len / SYMBOL_SIZE { |
| 179 | throw shared::Error::Capacity; |
| 180 | } |
| 181 | let offset = try reserve(output, used, input.len * SYMBOL_SIZE, SYMBOL_ALIGN); |
| 182 | for symbol, i in input { |
| 183 | let at = offset + i * SYMBOL_SIZE; |
| 184 | let name = try bytes(symbol.name, output, used); |
| 185 | slice(output, at + SYMBOL_NAME, base + name as u64, symbol.name.len); |
| 186 | integer(output, at + SYMBOL_STARTABLE, 1 if symbol.startable else 0, 1); |
| 187 | match symbol.target { |
| 188 | case shared::Target::Function(address) => { |
| 189 | integer(output, at + SYMBOL_TAG, 0, 8); |
| 190 | integer(output, at + SYMBOL_VALUE, address, 8); |
| 191 | } |
| 192 | case shared::Target::Data(location) => { |
| 193 | integer(output, at + SYMBOL_TAG, 1, 8); |
| 194 | integer(output, at + SYMBOL_VALUE, location.slot as u64, 4); |
| 195 | integer(output, at + SYMBOL_VALUE + 4, location.offset as u64, 4); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | return offset; |
| 200 | } |
| 201 | |
| 202 | /// Copy private-state relocation records into native catalog storage. |
| 203 | fn relocations(input: *[shared::Relocation], output: &mut [u8], used: &mut u32) -> u32 |
| 204 | throws (shared::Error) |
| 205 | { |
| 206 | if input.len > output.len / RELOCATION_SIZE { |
| 207 | throw shared::Error::Capacity; |
| 208 | } |
| 209 | let offset = try reserve(output, used, input.len * RELOCATION_SIZE, RELOCATION_ALIGN); |
| 210 | for fixup, i in input { |
| 211 | let at = offset + i * RELOCATION_SIZE; |
| 212 | integer(output, at + RELOCATION_OFFSET, fixup.offset as u64, 4); |
| 213 | integer(output, at + RELOCATION_COUNT, fixup.count as u64, 4); |
| 214 | integer(output, at + RELOCATION_SLOT, fixup.target.slot as u64, 4); |
| 215 | integer(output, at + RELOCATION_TARGET_OFFSET, fixup.target.offset as u64, 4); |
| 216 | } |
| 217 | return offset; |
| 218 | } |
| 219 | |
| 220 | /// Pack native Entry records followed by all referenced metadata and source bytes. |
| 221 | /// Code is placed separately at each package's codeAddress. The target base must |
| 222 | /// have Entry alignment. Return the used extent. Failure leaves partial output. |
| 223 | export fn pack(entries: &[Entry], base: u64, output: &mut [u8]) -> u32 throws (shared::Error) { |
| 224 | if (base & (ENTRY_ALIGN as u64 - 1)) <> 0 { |
| 225 | throw shared::Error::Alignment; |
| 226 | } |
| 227 | if base > mem::U64_MAX - output.len as u64 { |
| 228 | throw shared::Error::Range; |
| 229 | } |
| 230 | if entries.len > output.len / ENTRY_SIZE { |
| 231 | throw shared::Error::Capacity; |
| 232 | } |
| 233 | let mut used: u32 = 0; |
| 234 | try reserve(output, &mut used, entries.len * ENTRY_SIZE, ENTRY_ALIGN); |
| 235 | for entry, i in entries { |
| 236 | let at = i * ENTRY_SIZE; |
| 237 | for k in 0 .. ENTRY_SIZE { |
| 238 | set output[at + k] = 0; |
| 239 | } |
| 240 | let package = entry.package; |
| 241 | let source = try bytes(entry.source, output, &mut used); |
| 242 | let name = try bytes(package.name, output, &mut used); |
| 243 | let required = try dependencies(package.dependencies, base, output, &mut used); |
| 244 | let exports = try symbols(package.exports, base, output, &mut used); |
| 245 | let template = try bytes(package.template, output, &mut used); |
| 246 | let fixups = try relocations(package.relocations, output, &mut used); |
| 247 | let exact = try pins(package.contract.pins, output, &mut used); |
| 248 | slice(output, at + ENTRY_SOURCE, base + source as u64, entry.source.len); |
| 249 | slice(output, at + ENTRY_NAME, base + name as u64, package.name.len); |
| 250 | slice(output, at + ENTRY_DEPENDENCIES, base + required as u64, package.dependencies.len); |
| 251 | integer(output, at + ENTRY_SLOT, package.slot as u64, 4); |
| 252 | integer(output, at + ENTRY_CODE_ADDRESS, package.codeAddress, 8); |
| 253 | slice(output, at + ENTRY_CODE, package.codeAddress, package.code.len); |
| 254 | slice(output, at + ENTRY_EXPORTS, base + exports as u64, package.exports.len); |
| 255 | if let address = package.entry { |
| 256 | integer(output, at + ENTRY_ENTRY_TAG, 1, 8); |
| 257 | integer(output, at + ENTRY_ENTRY_VALUE, address, 8); |
| 258 | } |
| 259 | slice(output, at + ENTRY_TEMPLATE, base + template as u64, package.template.len); |
| 260 | integer(output, at + ENTRY_MEMORY, package.memory as u64, 4); |
| 261 | integer(output, at + ENTRY_ALIGNMENT, package.alignment as u64, 4); |
| 262 | slice(output, at + ENTRY_RELOCATIONS, base + fixups as u64, package.relocations.len); |
| 263 | slice(output, at + ENTRY_PINS, base + exact as u64, package.contract.pins.len); |
| 264 | if let identity = package.contract.image { |
| 265 | integer(output, at + ENTRY_IMAGE_TAG, 1, 1); |
| 266 | try! mem::copy( |
| 267 | &mut output[at + ENTRY_IMAGE_VALUE .. at + ENTRY_IMAGE_VALUE + 32], |
| 268 | &identity.bytes[..] |
| 269 | ); |
| 270 | } |
| 271 | try! mem::copy( |
| 272 | &mut output[at + ENTRY_METADATA .. at + ENTRY_METADATA + PIN_SIZE], |
| 273 | &package.contract.metadata.bytes[..] |
| 274 | ); |
| 275 | integer( |
| 276 | output, |
| 277 | at + ENTRY_VERIFICATION, |
| 278 | 1 if package.contract.verification == shared::Verification::Verified else 0, |
| 279 | 1 |
| 280 | ); |
| 281 | } |
| 282 | return used; |
| 283 | } |