Files
ziran/cmd/compiler_machine_elf.zi
T
waoziandClaude Opus 5.5 931fed4c86 Run ZX natively by default and integrate the finished assembly lanes
One commit, because the parts below only build together.

Allocator: an interval needed at the current instruction is never
evicted, and the allocated-program verifier checks that every operand sits
where the allocation says.

AArch64 encoder: compiler_aarch64.zi encodes allocated machine IR with the
shared ARM byte helpers, maps the function-address relocation,
sign-extends signed 8- and 16-bit loads and rejects what it cannot encode
with a named diagnostic. It has no AOT ELF writer yet.

Native default with checked pointers and no fallback: on linux-x86_64,
`ziran run app.zx`, `zi2zx run`, the embedding API's Auto and the Kryon
players run a bundle's verified program as native code in W^X memory.
Stray and freed pointers, double free, bounds and read-only stores trap. A
native gap is a named preflight error, nothing falls back to the engine,
and `--backend engine` picks it explicitly. zx2native uses the same
preflight. Native code exchanges module state for hot reload, checks the
stack, zeroes fresh locals, spends fuel at the engine's checkpoints and
returns records, arrays and strings from host calls. `zi2zx --debug` source
origins name lines in native traps and `zx2native --emit map`. The host
value API drops its VM prefix (HostValue, HOST_*). A call with a record
result loads the hidden result address into rax only after its stack
arguments are stored, and an indirect target stays in r10: rax could hold
a stack argument (Kryon's text-field clip guard arrived as an address) or
the target (indirect calls returning records crashed).

Self-hosting ports: compiler_backend_process, compiler_backend_cache,
compiler_native_names and compiler_runtime_emit replace the zir_c_build,
zir_c_cache and zir_c_process C, with checked bootstrap C and Makefile
rules. zir_c.toolchain again reports "compiling OUT failed:" with the head
of the compiler output and a truncation marker. The parallel scheduler no
longer stalls on a held compiler after reaping a finished job, and poll or
read errors now fail the build. A fresh BUILD_DIR (Kryon's runtime build)
builds: generated files have pattern rules, objects wait for the headers
they include, and engine flags are per object instead of leaking into zi2c.

Legacy removals F1-F10 and N1-N4: the duplicate LirX64 encoder and its
spec test, zir_c_*, zir_runtime and zir_native, nine zero-reference spec
tests, vec_capacity, the zx_metadata and native_cache_names benchmarks,
build-android-host.sh and two shell wrappers are deleted; zx2native is
machine-only; `zi2zx` rejects `--opt` on ZX input; the bootstrap is
regenerated. ASSEMBLY docs, the legacy-removal plan, the milestones and the
remaining-work plan record the current position.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 14:04:48 -03:00

747 lines
38 KiB
Ziran

using Allocated :: #import "compiler_machine_verify_allocated"
using Image :: #import "compiler_machine_image"
using Machine :: #import "compiler_machine_ir"
using X64 :: #import "compiler_x86_64"
// Deterministic, sectionless Linux x86-64 ELF image for allocated machine IR.
// The first PT_LOAD contains the ELF headers, a Ziran-owned process entry stub,
// the encoded machine text and the hard-trap stub, and is read/execute only.
// The second PT_LOAD contains zero-initialized globals, the fuel counter, the
// last trap code, the stack limit and the pointer runtime: its header, the
// pointer table (slot 0 and the globals' slots in the file, the rest zero
// fill) and the frame arena. It is read/write but never executable.
//
// Process convention:
// * a void entry exits zero; a bool/integer entry exits with its low byte;
// * fuel starts at the requested budget, by default MachineElfInitialFuel,
// the same unbounded budget a bundle run starts with, and every
// MachineOpcode.Fuel debits it;
// * a hard trap stores its u32 MachineX64Trap* code and exits status 125;
// * _start sets the stack limit MachineElfStackBytes below its own stack
// pointer, inside the default 8 MiB process stack, so runaway recursion
// takes MachineX64TrapStack instead of faulting.
// No dynamic linker, runtime, assembler or foreign object participates.
MachineElfBase :: 0x400000
MachineElfPage :: 4096
MachineElfHeaderSize :: 64
MachineElfProgramHeaderSize :: 56
MachineElfProgramHeaders :: 2
MachineElfTextOffset :: 4096
// Stack limit setup (16 bytes), then the entry call and exit (14 bytes).
MachineElfStartupSize :: 30
MachineElfStackBytes :: 0x7c0000
MachineElfTrapSize :: 20
MachineElfRuntimeSize :: 1335
MachineElfWriteAllOffset :: 0
MachineElfUnsignedOffset :: 0x2d
MachineElfSignedOffset :: 0x6a
MachineElfBoolOffset :: 0xc3
MachineElfResizeOffset :: 0x10b
MachineElfFloat32Offset :: 0x4f7
MachineElfFloat64Offset :: 0x505
MachineElfInitialFuel :: 9223372036854775807
MachineElfTrapExitStatus :: 125
MachineElfResult :: struct {
diagnostic: MachineDiagnostic
size: s64
text_offset: s64
text_size: s64
data_offset: s64
data_size: s64
fuel_offset: s64
trap_status_offset: s64
pointer_offset: s64
}
#scope_file
DataLayout :: struct {
globals_size: s64
fuel: s64
trap_status: s64
stack_limit: s64
pointer_header: s64
table: s64
arena: s64
// Bytes in the file, and in memory with the zero fill.
size: s64
memory: s64
}
Layout :: struct {
startup_size: s64
text_size: s64
data_offset: s64
file_size: s64
trap_text: s64
runtime_text: s64
constants_text: s64
constants_size: s64
data: DataLayout
}
NoDiagnostic :: () -> MachineDiagnostic {
return .{problem = .None, function = MachineNoId, block = MachineNoId,
instruction = MachineNoId, operand = MachineNoId, virtual = MachineNoId}
}
Failure :: (problem: MachineProblem, instruction: s32 = MachineNoId) -> MachineElfResult {
return .{diagnostic = .{problem = problem, function = MachineNoId, block = MachineNoId,
instruction = instruction, operand = MachineNoId, virtual = MachineNoId}}
}
Align :: (value: s64, alignment: s64) -> s64 {
return (value + alignment - 1) & ~(alignment - 1)
}
ValidAlignment :: (alignment: s32) -> bool {
return alignment > 0 && alignment <= MachineElfPage && (alignment & (alignment - 1)) == 0
}
DataMeasure :: (program: MachineProgram, layout: *DataLayout) -> MachineDiagnostic {
at: s64 = 0
for global: program.globals {
if global.type < 0 || global.type >= program.types.count {
result := NoDiagnostic(); result.problem = .Unsupported; return result
}
if !global.writable { continue }
type := program.types[global.type]
if type.size <= 0 || type.size > 2147483647 || !ValidAlignment(type.alignment) {
result := NoDiagnostic(); result.problem = .Unsupported; return result
}
at = Align(at, type.alignment)
if at > 2147483647 - type.size {
result := NoDiagnostic(); result.problem = .Limit; return result
}
at += type.size
}
layout.globals_size = at
layout.fuel = Align(at, 8)
if layout.fuel > 2147483647 - 8 {
result := NoDiagnostic(); result.problem = .Limit; return result
}
layout.trap_status = Align(layout.fuel + 8, 4)
layout.stack_limit = Align(layout.trap_status + 4, 8)
layout.pointer_header = layout.stack_limit + 8
layout.table = Align(layout.pointer_header + MachinePointerHeaderBytes, MachinePointerEntryBytes)
slots := program.globals.count + 1
if slots > MachinePointerCapacity || layout.table > 2147483647 - slots * MachinePointerEntryBytes {
result := NoDiagnostic(); result.problem = .Limit; return result
}
layout.size = layout.table + slots * MachinePointerEntryBytes
layout.arena = Align(layout.table + (MachinePointerCapacity + 1) * MachinePointerEntryBytes, 16)
layout.memory = layout.arena + MachinePointerArenaBytes
return NoDiagnostic()
}
GlobalsMeasure :: (program: MachineProgram, writable: bool, size: *s64) -> MachineDiagnostic {
at: s64 = 0
for global: program.globals {
if global.type < 0 || global.type >= program.types.count {
result := NoDiagnostic(); result.problem = .Unsupported; return result
}
if global.writable != writable { continue }
type := program.types[global.type]
if type.size <= 0 || type.size > 2147483647 || !ValidAlignment(type.alignment) {
result := NoDiagnostic(); result.problem = .Unsupported; return result
}
at = Align(at, type.alignment)
if at > 2147483647 - type.size {
result := NoDiagnostic(); result.problem = .Limit; return result
}
at += type.size
}
<<size = at
return NoDiagnostic()
}
GlobalOffset :: (program: MachineProgram, symbol: s32, writable: bool, offset: *s64) -> bool {
if symbol < 0 || symbol >= program.globals.count { return false }
if program.globals[symbol].writable != writable { return false }
at: s64 = 0
for index: cast(s64)0..program.globals.count - 1 {
global := program.globals[index]
if global.type < 0 || global.type >= program.types.count { return false }
if global.writable != writable { continue }
type := program.types[global.type]
if type.size <= 0 || !ValidAlignment(type.alignment) { return false }
at = Align(at, type.alignment)
if index == symbol { <<offset = at; return true }
at += type.size
}
return false
}
Relative32 :: (target: u64, addend: s64, field: u64, value: *s64) -> bool {
// Every address in this image is bounded below 2^32 and relocation addends
// are restricted to i32, so this subtraction cannot overflow s64.
if target > 0xffffffff || field > 0xffffffff ||
addend < -2147483648 || addend > 2147483647 { return false }
displacement := cast(s64)target + addend - cast(s64)field
if displacement < -2147483648 || displacement > 2147483647 { return false }
<<value = displacement
return true
}
TrapOpcode :: (opcode: MachineOpcode) -> bool {
return opcode == .IntegerDivide || opcode == .IntegerRemainder ||
opcode == .ShiftLeft || opcode == .ShiftRight || opcode == .AggregateExtract ||
opcode == .AggregateInsert || opcode == .ElementAddress ||
opcode == .Convert || opcode == .Fuel || opcode == .Trap || opcode == .Unreachable ||
PointerOpcode(opcode)
}
// Instructions whose sequences use the pointer runtime header.
PointerOpcode :: (opcode: MachineOpcode) -> bool {
return opcode == .PointerResolve || opcode == .PointerCreate || opcode == .ArenaAllocate ||
opcode == .Resize
}
FunctionNameEqual :: (program: MachineProgram, symbol: s32, expected: string) -> bool {
if symbol < 0 || symbol >= program.functions.count { return false }
function := program.functions[symbol]
if function.name_offset < 0 || function.name_length != expected.count ||
function.name_offset > program.text.count - function.name_length { return false }
for index: cast(s64)0..expected.count - 1 {
if program.text[function.name_offset + index] != cast(u8)expected[index] { return false }
}
return true
}
PlatformOffset :: (program: MachineProgram, symbol: s32, offset: *s64) -> bool {
if FunctionNameEqual(program, symbol, "ziran.platform.write_all") { <<offset = MachineElfWriteAllOffset; return true }
if FunctionNameEqual(program, symbol, "ziran.print.signed") { <<offset = MachineElfSignedOffset; return true }
if FunctionNameEqual(program, symbol, "ziran.print.unsigned") { <<offset = MachineElfUnsignedOffset; return true }
if FunctionNameEqual(program, symbol, "ziran.print.bool") { <<offset = MachineElfBoolOffset; return true }
if FunctionNameEqual(program, symbol, "ziran.print.float32") { <<offset = MachineElfFloat32Offset; return true }
if FunctionNameEqual(program, symbol, "ziran.print.float64") { <<offset = MachineElfFloat64Offset; return true }
return false
}
StartupSize :: (program: MachineProgram) -> s64 {
return MachineElfStartupSize + program.initializers.count * 5
}
ImageLayout :: (program: MachineProgram, image: MachineX64Image, layout: *Layout) -> MachineDiagnostic {
verified := MachineVerifyAllocated(program, MachineDefaultLimits())
if verified.problem != .None { return verified }
if program.target.arch != .X86_64 || program.target.os != .Linux || program.target.abi != .SysV {
result := NoDiagnostic(); result.problem = .UnsupportedTarget; return result
}
if program.entry < 0 || program.entry >= program.functions.count || image.entry != program.entry ||
image.code.count <= 0 || image.code.count > 2147483647 ||
image.functions.count != program.functions.count || image.blocks.count != program.blocks.count ||
image.instructions.count != program.instructions.count {
result := NoDiagnostic(); result.problem = .Relocation; return result
}
entry := program.functions[program.entry]
if entry.parameter_count != 0 || entry.result_type < 0 || entry.result_type >= program.types.count {
result := NoDiagnostic(); result.problem = .Unsupported; return result
}
result_type := program.types[entry.result_type]
if result_type.kind != .Void && result_type.kind != .Bool && result_type.kind != .Integer {
result := NoDiagnostic(); result.problem = .Unsupported; return result
}
for global: program.globals {
if !global.writable && global.constant < 0 {
result := NoDiagnostic(); result.problem = .Unsupported; return result
}
if global.constant >= 0 {
constant := program.constants[global.constant]
type := program.types[global.type]
if (constant.bytes_count == 0 && type.size > 8) ||
(constant.bytes_count != 0 && constant.bytes_count != type.size) {
result := NoDiagnostic(); result.problem = .Unsupported; return result
}
}
}
previous: s64 = 0
for function: image.functions {
if function.begin < previous || function.body < function.begin || function.end < function.body ||
function.end > image.code.count {
result := NoDiagnostic(); result.problem = .Relocation; return result
}
previous = function.end
}
for block: image.blocks {
if block < 0 || block > image.code.count {
result := NoDiagnostic(); result.problem = .Relocation; return result
}
}
for range: image.instructions {
if range.begin < 0 || range.end < range.begin || range.end > image.code.count {
result := NoDiagnostic(); result.problem = .Relocation; return result
}
}
data_diagnostic := DataMeasure(program, *layout.data)
if data_diagnostic.problem != .None { return data_diagnostic }
constants_diagnostic := GlobalsMeasure(program, false, *layout.constants_size)
if constants_diagnostic.problem != .None { return constants_diagnostic }
layout.startup_size = StartupSize(program)
if layout.startup_size > 2147483647 - image.code.count {
result := NoDiagnostic(); result.problem = .Limit; return result
}
layout.trap_text = layout.startup_size + image.code.count
// The runtime blob is position independent, but its embedded float section
// starts 523 bytes in and contains naturally aligned SSE constants. Start
// the runtime five bytes beyond the next text boundary so that section is
// 16-byte aligned without rewriting its internal relative references.
layout.runtime_text = Align(layout.trap_text + MachineElfTrapSize, 16) + 5
layout.constants_text = Align(layout.runtime_text + MachineElfRuntimeSize, 16)
layout.text_size = layout.constants_text + layout.constants_size
if layout.text_size > 2147483647 - MachineElfTextOffset {
result := NoDiagnostic(); result.problem = .Limit; return result
}
layout.data_offset = Align(MachineElfTextOffset + layout.text_size, MachineElfPage)
if layout.data_offset > 2147483647 - layout.data.size {
result := NoDiagnostic(); result.problem = .Limit; return result
}
layout.file_size = layout.data_offset + layout.data.size
// The encoder's own measuring pass must find exactly these relocations.
remeasured := MachineX64Measure(program, MachineDefaultLimits())
if remeasured.diagnostic.problem != .None || remeasured.relocations != image.relocations.count {
result := NoDiagnostic(); result.problem = .Relocation; return result
}
previous = -1
for relocation: image.relocations {
if relocation.instruction == MachineNoId {
// Only a checked prologue has relocations outside an instruction.
if relocation.offset <= previous || relocation.offset < 0 ||
relocation.offset > image.code.count - 4 ||
(relocation.kind != .StackLimit && relocation.kind != .TrapHandler &&
relocation.kind != .PointerRuntime) ||
relocation.symbol != 0 || relocation.addend != -4 ||
!InPrologue(program, image, relocation.offset) {
result := NoDiagnostic(); result.problem = .Relocation; return result
}
previous = relocation.offset
continue
}
if relocation.kind == .StackLimit ||
relocation.kind == .PointerRuntime &&
!PointerOpcode(program.instructions[relocation.instruction].opcode) &&
program.instructions[relocation.instruction].opcode != .Return ||
relocation.offset <= previous || relocation.offset < 0 ||
relocation.offset > image.code.count - 4 || relocation.instruction < 0 ||
relocation.instruction >= image.instructions.count ||
relocation.offset < image.instructions[relocation.instruction].begin ||
relocation.offset + 4 > image.instructions[relocation.instruction].end {
result := NoDiagnostic(); result.problem = .Relocation
result.instruction = relocation.instruction; return result
}
previous = relocation.offset
target: u64 = 0
if relocation.kind == .GlobalAddress || relocation.kind == .ConstantAddress {
readonly := relocation.kind == .ConstantAddress
if program.instructions[relocation.instruction].opcode != .GlobalAddress {
result := NoDiagnostic(); result.problem = .Relocation
result.instruction = relocation.instruction; return result
}
global_offset: s64 = 0
if !GlobalOffset(program, relocation.symbol, !readonly, *global_offset) {
result := NoDiagnostic(); result.problem = .Relocation
result.instruction = relocation.instruction; return result
}
if readonly {
target = cast(u64)(MachineElfBase + MachineElfTextOffset +
layout.constants_text + global_offset)
} else { target = cast(u64)(MachineElfBase + layout.data_offset + global_offset) }
} else if relocation.kind == .FuelCounter && relocation.symbol == 0 {
if program.instructions[relocation.instruction].opcode != .Fuel {
result := NoDiagnostic(); result.problem = .Relocation
result.instruction = relocation.instruction; return result
}
target = cast(u64)(MachineElfBase + layout.data_offset + layout.data.fuel)
} else if relocation.kind == .TrapHandler && relocation.symbol == 0 {
if !TrapOpcode(program.instructions[relocation.instruction].opcode) || relocation.addend != -4 {
result := NoDiagnostic(); result.problem = .Relocation
result.instruction = relocation.instruction; return result
}
target = cast(u64)(MachineElfBase + MachineElfTextOffset + layout.trap_text)
} else if relocation.kind == .PlatformService {
if program.instructions[relocation.instruction].opcode != .CallDirect {
result := NoDiagnostic(); result.problem = .Relocation
result.instruction = relocation.instruction; return result
}
service_offset: s64 = 0
if !PlatformOffset(program, relocation.symbol, *service_offset) {
result := NoDiagnostic(); result.problem = .Unsupported
result.instruction = relocation.instruction; return result
}
target = cast(u64)(MachineElfBase + MachineElfTextOffset + layout.runtime_text + service_offset)
} else if relocation.kind == .PointerRuntime && relocation.symbol == 0 {
target = cast(u64)(MachineElfBase + layout.data_offset + layout.data.pointer_header)
} else if relocation.kind == .RuntimeResize && relocation.symbol == 0 {
if program.instructions[relocation.instruction].opcode != .Resize {
result := NoDiagnostic(); result.problem = .Relocation
result.instruction = relocation.instruction; return result
}
target = cast(u64)(MachineElfBase + MachineElfTextOffset + layout.runtime_text + MachineElfResizeOffset)
} else {
result := NoDiagnostic(); result.problem = .Relocation
result.instruction = relocation.instruction; return result
}
field := cast(u64)(MachineElfBase + MachineElfTextOffset + layout.startup_size + relocation.offset)
displacement: s64 = 0
if !Relative32(target, relocation.addend, field, *displacement) {
result := NoDiagnostic(); result.problem = .Relocation
result.instruction = relocation.instruction; return result
}
}
return NoDiagnostic()
}
// Whether a field at OFFSET lies inside the prologue of a function that
// checks its stack or keeps checked frame storage.
InPrologue :: (program: MachineProgram, image: MachineX64Image, offset: s64) -> bool {
for function_id: cast(s64)0..image.functions.count - 1 {
function := image.functions[function_id]
if offset >= function.begin && offset + 4 <= function.body {
return MachineX64StackChecked(program, cast(s32)function_id) ||
program.functions[function_id].pointer_chain != 0
}
}
return false
}
Put8 :: (output: []u8, at: s64, value: u8) { output[at] = value }
PutLittle :: (output: []u8, at: s64, value: u64, count: s32) {
for index: 0..count - 1 {
output[at + index] = cast(u8)((value >> cast(u64)(index * 8)) & 255)
}
}
PutRelative :: (output: []u8, at: s64, target: u64, addend: s64, field: u64) -> bool {
value: s64 = 0
if !Relative32(target, addend, field, *value) { return false }
PutLittle(output, at, cast(u64)value, 4)
return true
}
WriteRuntime :: (output: []u8, at: s64) {
bytes: [523]u8 = .[
0x48,0x85,0xd2,0x74,0x27,0xb8,0x01,0x00,0x00,0x00,0x0f,0x05,
0x48,0x83,0xf8,0xfc,0x74,0xf3,0x48,0x85,0xc0,0x0f,0x88,0xe2,
0x00,0x00,0x00,0x48,0x85,0xc0,0x0f,0x84,0xd9,0x00,0x00,0x00,
0x48,0x01,0xc6,0x48,0x29,0xc2,0x75,0xd9,0xc3,0x48,0x83,0xec,
0x28,0x48,0x8d,0x74,0x24,0x20,0xb9,0x0a,0x00,0x00,0x00,0x48,
0x89,0xf8,0x31,0xd2,0x48,0xf7,0xf1,0x80,0xc2,0x30,0x48,0xff,
0xce,0x88,0x16,0x48,0x89,0xc7,0x48,0x85,0xc0,0x75,0xe8,0x48,
0x8d,0x54,0x24,0x20,0x48,0x29,0xf2,0xbf,0x01,0x00,0x00,0x00,
0xe8,0x00,0x00,0x00,0x00,0x48,0x83,0xc4,0x28,0xc3,0x48,0x83,
0xec,0x28,0x45,0x31,0xc0,0x48,0x85,0xff,0x79,0x09,0x48,0xf7,
0xdf,0x41,0xb8,0x01,0x00,0x00,0x00,0x48,0x8d,0x74,0x24,0x20,
0xb9,0x0a,0x00,0x00,0x00,0x48,0x89,0xf8,0x31,0xd2,0x48,0xf7,
0xf1,0x80,0xc2,0x30,0x48,0xff,0xce,0x88,0x16,0x48,0x89,0xc7,
0x48,0x85,0xc0,0x75,0xe8,0x45,0x85,0xc0,0x74,0x06,0x48,0xff,
0xce,0xc6,0x06,0x2d,0x48,0x8d,0x54,0x24,0x20,0x48,0x29,0xf2,
0xbf,0x01,0x00,0x00,0x00,0xe8,0x00,0x00,0x00,0x00,0x48,0x83,
0xc4,0x28,0xc3,0x48,0x83,0xec,0x18,0x40,0x84,0xff,0x74,0x0e,
0xc7,0x04,0x24,0x74,0x72,0x75,0x65,0xba,0x04,0x00,0x00,0x00,
0xeb,0x11,0xc7,0x04,0x24,0x66,0x61,0x6c,0x73,0xc6,0x44,0x24,
0x04,0x65,0xba,0x05,0x00,0x00,0x00,0xbf,0x01,0x00,0x00,0x00,
0x48,0x89,0xe6,0xe8,0x00,0x00,0x00,0x00,0x48,0x83,0xc4,0x18,
0xc3,0xbf,0x7d,0x00,0x00,0x00,0xb8,0xe7,0x00,0x00,0x00,0x0f,
0x05,0x0f,0x0b,0x53,0x41,0x54,0x41,0x55,0x41,0x56,0x41,0x57,
0x49,0x89,0xfc,0x49,0x89,0xf5,0x49,0x89,0xd6,0x49,0x89,0xcf,
0x4d,0x85,0xe4,0x75,0x07,0x4d,0x85,0xed,0x75,0xd3,0xeb,0x19,
0x49,0x81,0x7c,0x24,0xd0,0x58,0x5a,0x00,0x00,0x75,0xc6,0x4d,
0x39,0x6c,0x24,0xd8,0x75,0xbf,0x4d,0x39,0x7c,0x24,0xe0,0x75,
0xb8,0x4d,0x85,0xf6,0x75,0x22,0x4d,0x85,0xe4,0x74,0x16,0x49,
0x8d,0x7c,0x24,0xd0,0x49,0x8b,0x74,0x24,0xf8,0xb8,0x0b,0x00,
0x00,0x00,0x0f,0x05,0x48,0x85,0xc0,0x75,0x98,0x31,0xc0,0xe9,
0x95,0x00,0x00,0x00,0x4c,0x89,0xf0,0x49,0xf7,0xe7,0x48,0x85,
0xd2,0x0f,0x85,0x84,0x00,0x00,0x00,0x48,0x89,0xc3,0x48,0x83,
0xc0,0x30,0x72,0x7b,0x48,0x89,0xc6,0x31,0xff,0xba,0x03,0x00,
0x00,0x00,0x41,0xba,0x22,0x00,0x00,0x00,0x49,0xc7,0xc0,0xff,
0xff,0xff,0xff,0x45,0x31,0xc9,0xb8,0x09,0x00,0x00,0x00,0x0f,
0x05,0x48,0x85,0xc0,0x78,0x55,0x48,0xc7,0x00,0x58,0x5a,0x00,
0x00,0x4c,0x89,0x70,0x08,0x4c,0x89,0x78,0x10,0x48,0x89,0x58,
0x18,0x48,0x89,0x70,0x28,0x48,0x8d,0x58,0x30,0x4d,0x85,0xe4,
0x74,0x30,0x4c,0x89,0xe9,0x4c,0x39,0xf1,0x49,0x0f,0x47,0xce,
0x49,0x0f,0xaf,0xcf,0x4c,0x89,0xe6,0x48,0x89,0xdf,0xf3,0xa4,
0x49,0x8d,0x7c,0x24,0xd0,0x49,0x8b,0x74,0x24,0xf8,0xb8,0x0b,
0x00,0x00,0x00,0x0f,0x05,0x48,0x85,0xc0,0x0f,0x85,0x03,0xff,
0xff,0xff,0x48,0x89,0xd8,0xeb,0x02,0x31,0xc0,0x41,0x5f,0x41,
0x5e,0x41,0x5d,0x41,0x5c,0x5b,0xc3]
for index: cast(s64)0..bytes.count - 1 { output[at + index] = bytes[index] }
}
WriteFloatRuntime :: (output: []u8, at: s64) {
first: [406]u8 = .[
0x49,0x89,0xf8,0x48,0x89,0xf2,0xbf,0x01,0x00,0x00,0x00,0x48,
0x85,0xd2,0x74,0x1b,0x48,0x89,0xf8,0x4c,0x89,0xc6,0x0f,0x05,
0x48,0x83,0xf8,0xfc,0x74,0xf2,0x48,0x85,0xc0,0x7e,0x08,0x49,
0x01,0xc0,0x48,0x29,0xc2,0xeb,0xe0,0xc3,0x53,0x66,0x48,0x0f,
0x7e,0xc2,0x41,0x89,0xfb,0x31,0xc9,0x48,0x81,0xec,0xc0,0x01,
0x00,0x00,0x48,0x85,0xd2,0x79,0x11,0xc6,0x44,0x24,0x30,0x2d,
0x0f,0x57,0x05,0xc1,0x02,0x00,0x00,0xb9,0x01,0x00,0x00,0x00,
0xbe,0xff,0x07,0x00,0x00,0x48,0x89,0xd0,0x8d,0x79,0x01,0x48,
0xc1,0xe6,0x34,0x48,0xf7,0xd0,0x48,0x85,0xf0,0x75,0x3f,0x49,
0xb8,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0x00,0x8d,0x41,0x02,
0x8d,0x71,0x03,0x4c,0x85,0xc2,0x75,0x0a,0x41,0xb1,0x69,0x41,
0xb0,0x6e,0xb2,0x66,0xeb,0x08,0x41,0xb1,0x6e,0x41,0xb0,0x61,
0xb2,0x6e,0x48,0x63,0xff,0x48,0x98,0x44,0x88,0x4c,0x0c,0x30,
0x48,0x63,0xf6,0x44,0x88,0x44,0x3c,0x30,0x88,0x54,0x04,0x30,
0xeb,0x13,0x0f,0x57,0xc9,0x66,0x0f,0x2e,0xc1,0x7a,0x14,0x75,
0x12,0xc6,0x44,0x0c,0x30,0x30,0x48,0x63,0xf7,0x48,0x8d,0x7c,
0x24,0x30,0xe9,0x17,0x02,0x00,0x00,0xf2,0x0f,0x11,0x04,0x24,
0xdd,0x04,0x24,0x45,0x31,0xc0,0xd9,0x05,0x48,0x02,0x00,0x00,
0xd9,0xc9,0xdb,0xf1,0xd9,0xc1,0x72,0x09,0xdd,0xd8,0xde,0xf1,
0x41,0xff,0xc0,0xeb,0xe9,0xdd,0xda,0xd9,0xc9,0xd9,0xe8,0xdb,
0xf2,0x76,0x0b,0xd9,0xca,0xd8,0xc9,0xd9,0xca,0x41,0xff,0xc8,
0xeb,0xf1,0xdd,0xd8,0xbe,0x11,0x00,0x00,0x00,0x45,0x85,0xdb,
0x74,0x05,0xbe,0x09,0x00,0x00,0x00,0xd9,0x7c,0x24,0x0e,0x41,
0xb9,0x01,0x00,0x00,0x00,0x41,0xba,0x01,0x00,0x00,0x00,0xbb,
0x0a,0x00,0x00,0x00,0x66,0x8b,0x44,0x24,0x0e,0x80,0xcc,0x0c,
0x66,0x89,0x44,0x24,0x0c,0x4c,0x89,0x14,0x24,0xdf,0x2c,0x24,
0x4d,0x85,0xd2,0x79,0x06,0xd8,0x05,0xe1,0x01,0x00,0x00,0xd9,
0xc0,0xd8,0xcb,0xd8,0x05,0xdb,0x01,0x00,0x00,0xd9,0x6c,0x24,
0x0c,0xdf,0x3c,0x24,0xd9,0x6c,0x24,0x0e,0x4d,0x6b,0xd2,0x0a,
0x48,0x8b,0x04,0x24,0x4c,0x39,0xd0,0x72,0x08,0x31,0xd2,0x41,
0xff,0xc0,0x48,0xf7,0xf3,0x48,0x89,0x04,0x24,0xdf,0x2c,0x24,
0x48,0x85,0xc0,0x79,0x06,0xd8,0x05,0xa5,0x01,0x00,0x00,0xde,
0xf1,0x45,0x85,0xc0,0x7e,0x0d,0x31,0xd2,0xff,0xc2,0xd8,0xc9,
0x41,0x39,0xd0,0x75,0xf7,0xeb,0x0d,0x74,0x0b,0x31]
second: [406]u8 = .[
0xd2,0xd8,0xf1,0xff,0xca,0x41,0x39,0xd0,0x75,0xf7,0x45,0x85,
0xdb,0x74,0x11,0xd9,0x1c,0x24,0xf3,0x0f,0x10,0x14,0x24,0xf2,
0x0f,0x5a,0xc8,0x0f,0x2e,0xd1,0xeb,0x0c,0xdd,0x1c,0x24,0xf2,
0x0f,0x10,0x0c,0x24,0x66,0x0f,0x2e,0xc8,0x7a,0x02,0x74,0x12,
0x41,0xff,0xc1,0x44,0x39,0xce,0x0f,0x8d,0x5f,0xff,0xff,0xff,
0xdd,0xd8,0xdd,0xd8,0xeb,0x04,0xdd,0xd8,0xdd,0xd8,0x44,0x39,
0xce,0x41,0xbb,0x0a,0x00,0x00,0x00,0x41,0x0f,0x4f,0xf1,0x4c,
0x8d,0x4c,0x24,0x18,0x44,0x8d,0x56,0xff,0x4d,0x63,0xd2,0x31,
0xd2,0x49,0xff,0xca,0x49,0xf7,0xf3,0x83,0xc2,0x30,0x43,0x88,
0x54,0x0a,0x01,0x41,0x83,0xfa,0xff,0x75,0xea,0x48,0x63,0xc6,
0x83,0xf8,0x01,0x74,0x11,0x41,0x80,0x7c,0x01,0xff,0x30,0x48,
0x8d,0x50,0xff,0x75,0x05,0x48,0x89,0xd0,0xeb,0xea,0x31,0xf6,
0x45,0x85,0xc0,0x41,0x89,0xc3,0x48,0x63,0xd1,0x4c,0x8d,0x54,
0x24,0x30,0x78,0x05,0x4c,0x01,0xd2,0xeb,0x54,0x48,0x63,0xff,
0xc6,0x44,0x14,0x30,0x30,0x83,0xc1,0x02,0x31,0xf6,0xc6,0x44,
0x3c,0x30,0x2e,0x44,0x89,0xc7,0x4c,0x01,0xd2,0xf7,0xdf,0x48,
0xff,0xc6,0x39,0xf7,0x7e,0x07,0xc6,0x44,0x32,0x01,0x30,0xeb,
0xf2,0x44,0x29,0xc1,0x31,0xd2,0xff,0xc9,0x48,0x89,0xcf,0x4c,
0x01,0xd1,0x41,0x8a,0x34,0x11,0x40,0x88,0x34,0x11,0x48,0xff,
0xc2,0x39,0xd0,0x7f,0xf1,0x44,0x01,0xdf,0xeb,0x54,0x88,0x1c,
0x32,0x48,0xff,0xc6,0x41,0x39,0xf0,0x7c,0x0c,0xb3,0x30,0x39,
0xf0,0x7e,0xef,0x41,0x8a,0x1c,0x31,0xeb,0xe9,0x41,0x8d,0x50,
0x01,0x44,0x01,0xc7,0x39,0xc2,0x7d,0x32,0x48,0x63,0xff,0x44,
0x01,0xc1,0x48,0x63,0xd2,0xc6,0x44,0x3c,0x30,0x2e,0x8d,0x71,
0x02,0x4c,0x01,0xd1,0x48,0xff,0xc1,0x39,0xd0,0x7e,0x0d,0x41,
0x8a,0x3c,0x11,0x48,0xff,0xc2,0x40,0x88,0x79,0x01,0xeb,0xec,
0x89,0xf0,0x44,0x29,0xc0,0x41,0x8d,0x7c,0x03,0xff,0x48,0x63,
0xf7,0x4c,0x89,0xd7,0xe8,0x1d,0xfd,0xff,0xff,0x48,0x81,0xc4,
0xc0,0x01,0x00,0x00,0x5b,0xc3,0xbf,0x01,0x00,0x00,0x00,0xf3,
0x0f,0x5a,0xc0,0xe9,0x32,0xfd,0xff,0xff,0x31,0xff,0xe9,0x2b,
0xfd,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x20,0x41,0x00,0x00,0x80,0x5f,0x00,0x00,0x00,0x3f]
for index: cast(s64)0..first.count - 1 { output[at + index] = first[index] }
for index: cast(s64)0..second.count - 1 { output[at + first.count + index] = second[index] }
}
WriteGlobal :: (program: MachineProgram, symbol: s32, at: s64, output: []u8) -> bool {
global := program.globals[symbol]
if global.constant < 0 { return global.writable }
constant := program.constants[global.constant]
type := program.types[global.type]
if constant.bytes_count == 0 {
if type.size > 8 { return false }
PutLittle(output, at, constant.bits, cast(s32)type.size)
return true
}
if constant.bytes_count != type.size { return false }
for index: cast(s64)0..constant.bytes_count - 1 {
output[at + index] = program.data[constant.bytes_begin + index]
}
return true
}
Write :: (program: MachineProgram, image: MachineX64Image, layout: Layout, output: []u8, fuel: s64) -> bool {
for index: cast(s64)0..output.count - 1 { output[index] = 0 }
// ELF64 little-endian ET_EXEC, x86-64, two program headers, no sections.
Put8(output, 0, 0x7f); Put8(output, 1, 69); Put8(output, 2, 76); Put8(output, 3, 70)
Put8(output, 4, 2); Put8(output, 5, 1); Put8(output, 6, 1)
PutLittle(output, 16, 2, 2); PutLittle(output, 18, 62, 2); PutLittle(output, 20, 1, 4)
PutLittle(output, 24, cast(u64)(MachineElfBase + MachineElfTextOffset), 8)
PutLittle(output, 32, MachineElfHeaderSize, 8)
PutLittle(output, 52, MachineElfHeaderSize, 2)
PutLittle(output, 54, MachineElfProgramHeaderSize, 2)
PutLittle(output, 56, MachineElfProgramHeaders, 2)
// RX PT_LOAD includes the headers and text but ends before the RW page.
ph := cast(s64)MachineElfHeaderSize
PutLittle(output, ph, 1, 4); PutLittle(output, ph + 4, 5, 4)
PutLittle(output, ph + 16, MachineElfBase, 8); PutLittle(output, ph + 24, MachineElfBase, 8)
PutLittle(output, ph + 32, cast(u64)(MachineElfTextOffset + layout.text_size), 8)
PutLittle(output, ph + 40, cast(u64)(MachineElfTextOffset + layout.text_size), 8)
PutLittle(output, ph + 48, MachineElfPage, 8)
// RW PT_LOAD is congruent to its file offset and is never executable.
ph += MachineElfProgramHeaderSize
PutLittle(output, ph, 1, 4); PutLittle(output, ph + 4, 6, 4)
PutLittle(output, ph + 8, cast(u64)layout.data_offset, 8)
PutLittle(output, ph + 16, cast(u64)(MachineElfBase + layout.data_offset), 8)
PutLittle(output, ph + 24, cast(u64)(MachineElfBase + layout.data_offset), 8)
PutLittle(output, ph + 32, cast(u64)layout.data.size, 8)
PutLittle(output, ph + 40, cast(u64)layout.data.memory, 8)
PutLittle(output, ph + 48, MachineElfPage, 8)
// _start: set the stack limit (mov rax, rsp; sub rax, MachineElfStackBytes;
// mov [rip + limit], rax), call startup initializers in order, then machine
// entry; move (or clear) status; SYS_exit_group.
start := cast(s64)MachineElfTextOffset
Put8(output, start, 0x48); Put8(output, start + 1, 0x89); Put8(output, start + 2, 0xe0)
Put8(output, start + 3, 0x48); Put8(output, start + 4, 0x2d)
PutLittle(output, start + 5, MachineElfStackBytes, 4)
Put8(output, start + 9, 0x48); Put8(output, start + 10, 0x89); Put8(output, start + 11, 0x05)
stack_limit := cast(u64)(MachineElfBase + layout.data_offset + layout.data.stack_limit)
if !PutRelative(output, start + 12, stack_limit, -4, cast(u64)(MachineElfBase + start + 12)) { return false }
startup_at := start + 16
for initializer: program.initializers {
Put8(output, startup_at, 0xe8)
initializer_target := cast(u64)(MachineElfBase + MachineElfTextOffset + layout.startup_size +
image.functions[initializer].begin)
if !PutRelative(output, startup_at + 1, initializer_target, -4,
cast(u64)(MachineElfBase + startup_at + 1)) { return false }
startup_at += 5
}
Put8(output, startup_at, 0xe8)
entry_target := cast(u64)(MachineElfBase + MachineElfTextOffset + layout.startup_size +
image.functions[program.entry].begin)
if !PutRelative(output, startup_at + 1, entry_target, -4,
cast(u64)(MachineElfBase + startup_at + 1)) { return false }
result_type := program.types[program.functions[program.entry].result_type]
if result_type.kind == .Void { Put8(output, startup_at + 5, 0x31); Put8(output, startup_at + 6, 0xff) }
else { Put8(output, startup_at + 5, 0x89); Put8(output, startup_at + 6, 0xc7) }
Put8(output, startup_at + 7, 0xb8); PutLittle(output, startup_at + 8, 231, 4)
Put8(output, startup_at + 12, 0x0f); Put8(output, startup_at + 13, 0x05)
code_at := start + layout.startup_size
for index: cast(s64)0..image.code.count - 1 { output[code_at + index] = image.code[index] }
// trap_handler: store edi; exit_group(125); ud2 if the syscall unexpectedly returns.
trap := start + layout.trap_text
Put8(output, trap, 0x89); Put8(output, trap + 1, 0x3d)
trap_status := cast(u64)(MachineElfBase + layout.data_offset + layout.data.trap_status)
if !PutRelative(output, trap + 2, trap_status, -4, cast(u64)(MachineElfBase + trap + 2)) { return false }
Put8(output, trap + 6, 0xbf); PutLittle(output, trap + 7, MachineElfTrapExitStatus, 4)
Put8(output, trap + 11, 0xb8); PutLittle(output, trap + 12, 231, 4)
Put8(output, trap + 16, 0x0f); Put8(output, trap + 17, 0x05)
Put8(output, trap + 18, 0x0f); Put8(output, trap + 19, 0x0b)
runtime := start + layout.runtime_text
WriteRuntime(output, runtime)
WriteFloatRuntime(output, runtime + 523)
runtime_address := cast(u64)(MachineElfBase + runtime)
// The source blob's three formatter calls are ordinary rel32 calls to the
// write_all veneer. They stay position independent in the final image.
call_fields: [3]s64 = .[0x61, 0xba, 0xf4]
for field_offset: call_fields {
if !PutRelative(output, runtime + field_offset, runtime_address, -4,
cast(u64)(MachineElfBase + runtime + field_offset)) { return false }
}
for relocation: image.relocations {
target: u64 = 0
if relocation.kind == .GlobalAddress || relocation.kind == .ConstantAddress {
readonly := relocation.kind == .ConstantAddress
global_offset: s64 = 0
if !GlobalOffset(program, relocation.symbol, !readonly, *global_offset) { return false }
if readonly {
target = cast(u64)(MachineElfBase + MachineElfTextOffset +
layout.constants_text + global_offset)
} else { target = cast(u64)(MachineElfBase + layout.data_offset + global_offset) }
} else if relocation.kind == .FuelCounter {
target = cast(u64)(MachineElfBase + layout.data_offset + layout.data.fuel)
} else if relocation.kind == .TrapHandler {
target = cast(u64)(MachineElfBase + MachineElfTextOffset + layout.trap_text)
} else if relocation.kind == .PlatformService {
service_offset: s64 = 0
if !PlatformOffset(program, relocation.symbol, *service_offset) { return false }
target = cast(u64)(MachineElfBase + MachineElfTextOffset + layout.runtime_text + service_offset)
} else if relocation.kind == .RuntimeResize {
target = cast(u64)(MachineElfBase + MachineElfTextOffset + layout.runtime_text + MachineElfResizeOffset)
} else if relocation.kind == .StackLimit {
target = stack_limit
} else if relocation.kind == .PointerRuntime {
target = cast(u64)(MachineElfBase + layout.data_offset + layout.data.pointer_header)
} else { return false }
field_at := code_at + relocation.offset
field := cast(u64)(MachineElfBase + field_at)
if !PutRelative(output, field_at, target, relocation.addend, field) { return false }
}
for symbol: cast(s64)0..program.globals.count - 1 {
global := program.globals[symbol]
global_offset: s64 = 0
if !GlobalOffset(program, cast(s32)symbol, global.writable, *global_offset) { return false }
global_at := layout.data_offset + global_offset
if !global.writable {
global_at = MachineElfTextOffset + layout.constants_text + global_offset
}
if !WriteGlobal(program, cast(s32)symbol, global_at, output) { return false }
}
PutLittle(output, layout.data_offset + layout.data.fuel, cast(u64)fuel, 8)
// Pointer runtime: an empty free list, the next slot after the globals,
// the arena, and live entries for slot 0 (empty, at address 0) and each
// global.
data_base := cast(u64)(MachineElfBase + layout.data_offset)
header := layout.data_offset + layout.data.pointer_header
PutLittle(output, header + MachinePointerHeaderUnused, cast(u64)(program.globals.count + 1), 8)
PutLittle(output, header + MachinePointerHeaderCapacity, MachinePointerCapacity, 8)
PutLittle(output, header + MachinePointerHeaderArenaTop, data_base + cast(u64)layout.data.arena, 8)
PutLittle(output, header + MachinePointerHeaderArenaLimit,
data_base + cast(u64)(layout.data.arena + MachinePointerArenaBytes), 8)
PutLittle(output, header + MachinePointerHeaderTable, data_base + cast(u64)layout.data.table, 8)
table := layout.data_offset + layout.data.table
PutLittle(output, table + MachinePointerEntryFlags, MachinePointerLive, 2)
for symbol: cast(s64)0..program.globals.count - 1 {
global := program.globals[symbol]
global_offset: s64 = 0
if !GlobalOffset(program, cast(s32)symbol, global.writable, *global_offset) { return false }
address := cast(u64)(MachineElfBase + layout.data_offset + global_offset)
if !global.writable {
address = cast(u64)(MachineElfBase + MachineElfTextOffset + layout.constants_text + global_offset)
}
entry := table + (symbol + 1) * MachinePointerEntryBytes
PutLittle(output, entry + MachinePointerEntryBase, address, 8)
PutLittle(output, entry + MachinePointerEntrySize, cast(u64)program.types[global.type].size, 8)
PutLittle(output, entry + MachinePointerEntryFlags,
ifx global.writable then cast(u64)(MachinePointerLive | MachinePointerWritable) else MachinePointerLive, 2)
}
return true
}
#scope_export
// Bytes of the process entry stub before the encoded text.
MachineElfStartupBytes :: (program: MachineProgram) -> s64 { return StartupSize(program) }
// Empty OUTPUT measures. A non-empty OUTPUT must have exactly the measured
// length. Verification, complete relocation resolution and i32 range checks
// all finish before the first output byte is changed.
MachineElfEncode :: (program: MachineProgram, encoded: MachineX64Image, output: []u8,
fuel: s64 = MachineElfInitialFuel) -> MachineElfResult {
if fuel < 0 { return Failure(.Fuel) }
layout: Layout
diagnostic := ImageLayout(program, encoded, *layout)
if diagnostic.problem != .None { return .{diagnostic = diagnostic} }
result := MachineElfResult.{diagnostic = NoDiagnostic(), size = layout.file_size,
text_offset = MachineElfTextOffset, text_size = layout.text_size,
data_offset = layout.data_offset, data_size = layout.data.size,
fuel_offset = layout.data_offset + layout.data.fuel,
trap_status_offset = layout.data_offset + layout.data.trap_status,
pointer_offset = layout.data_offset + layout.data.pointer_header}
if output.count == 0 { return result }
if output.count != layout.file_size { return Failure(.Limit) }
if !Write(program, encoded, layout, output, fuel) { return Failure(.Relocation) }
return result
}