# SPDX-License-Identifier: LSL-1.0
# Copyright (c) 2026 Markus Maiwald
# Stewardship: Self Sovereign Society Foundation
#
# This file is part of the Nexus Sovereign Core.
# See legal/LICENSE_SOVEREIGN.md for license terms.

## Rumpk Layer 1: ELF Binary Parser

# MARKUS MAIWALD (ARCHITECT) | VOXIS FORGE (AI)
# ELF64 Header Definitions for Rumpk Summoning

type
  Elf64_Ehdr* {.packed.} = object
    e_ident*: array[16, uint8]
    e_type*: uint16
    e_machine*: uint16
    e_version*: uint32
    e_entry*: uint64
    e_phoff*: uint64
    e_shoff*: uint64
    e_flags*: uint32
    e_ehsize*: uint16
    e_phentsize*: uint16
    e_phnum*: uint16
    e_shentsize*: uint16
    e_shnum*: uint16
    e_shstrndx*: uint16

  Elf64_Phdr* {.packed.} = object
    p_type*: uint32
    p_flags*: uint32
    p_offset*: uint64
    p_vaddr*: uint64
    p_paddr*: uint64
    p_filesz*: uint64
    p_memsz*: uint64
    p_align*: uint64

  Elf64_Shdr* {.packed.} = object
    sh_name*: uint32
    sh_type*: uint32
    sh_flags*: uint64
    sh_addr*: uint64
    sh_offset*: uint64
    sh_size*: uint64
    sh_link*: uint32
    sh_info*: uint32
    sh_addralign*: uint64
    sh_entsize*: uint64

const
  PT_LOAD* = 1
  PT_NOTE* = 4
  PF_X* = 1
  PF_W* = 2
  PF_R* = 4

# SPEC-071: BKDL (Binary Manifest) types
const
  BKDL_MAGIC* = 0x4E585553'u32  # "NXUS" (little-endian)
  BKDL_VERSION* = 1'u16

type
  BkdlHeader* {.packed.} = object
    magic*: uint32
    version*: uint16
    flags*: uint16
    signature*: array[64, uint8]   # Ed25519 (unchecked in dev mode)
    pubkey_hash*: array[32, uint8] # SHA-256 of signing key
    cap_count*: uint16
    blob_size*: uint32
    entry_point*: uint64           # 0 = use ELF e_entry

  CapDescriptor* {.packed.} = object
    cap_type*: uint8      # CapType enum value
    perms*: uint8         # Permission bitmask
    reserved*: uint16     # Alignment padding
    resource_id*: uint64  # SipHash of resource name

  ManifestResult* = object
    header*: ptr BkdlHeader
    caps*: ptr UncheckedArray[CapDescriptor]
    count*: int
