hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

types.ha (23764B)


      1 // License: MPL-2.0
      2 // (c) 2021-2022 Alexey Yerin <yyp@disroot.org>
      3 // (c) 2021 Drew DeVault <sir@cmpwn.com>
      4 // (c) 2021 Mykyta Holubakha <hilobakho@gmail.com>
      5 
      6 // TODO:
      7 // - Flesh out ELF32 structures
      8 
      9 export def MAGIC: str = "\x7FELF";
     10 export def EI_MAG0: uint = 0;
     11 export def EI_MAG1: uint = 1;
     12 export def EI_MAG2: uint = 2;
     13 export def EI_MAG3: uint = 3;
     14 export def EI_CLASS: uint = 4;
     15 export def EI_DATA: uint = 5;
     16 export def EI_VERSION: uint = 6;
     17 export def EI_OSABI: uint = 7;
     18 export def EI_ABIVERSION: uint = 8;
     19 export def EI_PAD: uint = 9;
     20 export def EI_NIDENT: uint = 16;
     21 export def EV_CURRENT: u32 = 1;
     22 
     23 // ELF header for ELF64
     24 export type header64 = struct {
     25 	// ELF identification
     26 	e_ident: [EI_NIDENT]u8,
     27 	// Object file type
     28 	e_type: elf_type,
     29 	// Machine type
     30 	e_machine: elf_machine,
     31 	// Object file version ([EV_CURRENT])
     32 	e_version: u32,
     33 	// Entry point address
     34 	e_entry: u64,
     35 	// Program header offset
     36 	e_phoff: u64,
     37 	// Section header offset
     38 	e_shoff: u64,
     39 	// Processor-specific flags
     40 	e_flags: u32,
     41 	// ELF header size
     42 	e_ehsize: u16,
     43 	// Size of program header entry
     44 	e_phentsize: u16,
     45 	// Number of program header entries
     46 	e_phnum: u16,
     47 	// Size of section header entry
     48 	e_shentsize: u16,
     49 	// Number of section header entries
     50 	e_shnum: u16,
     51 	// Section name string table index, or [shn::UNDEF]
     52 	e_shstrndx: u16,
     53 };
     54 
     55 // Section header for ELF64
     56 export type section64 = struct {
     57 	// Section name
     58 	sh_name: u32,
     59 	// Section type
     60 	sh_type: u32,
     61 	// Section attributes
     62 	sh_flags: u64,
     63 	// Virtual address in memory
     64 	sh_addr: u64,
     65 	// Offset in file
     66 	sh_offset: u64,
     67 	// Size of section
     68 	sh_size: u64,
     69 	// Link to other section
     70 	sh_link: u32,
     71 	// Miscellaenous information
     72 	sh_info: u32,
     73 	// Address alignment boundary
     74 	sh_addralign: u64,
     75 	// Size of entries, if section has table
     76 	sh_entsize: u64,
     77 };
     78 
     79 // ELF file class
     80 export type ident_class = enum u8 {
     81 	// 32-bit objects
     82 	ELF32 = 1,
     83 	// 64-bit objects
     84 	ELF64 = 2,
     85 };
     86 
     87 // Byte ordering
     88 export type ident_data = enum u8 {
     89 	// Object file data structures are little-endian
     90 	LSB = 1,
     91 	// Object file data structures are big-endian
     92 	MSB = 2,
     93 };
     94 
     95 // Machine architecture
     96 export type elf_machine = enum u16 {
     97 	// Unknown machine
     98 	NONE = 0,
     99 	// AT&T WE32100
    100 	M32 = 1,
    101 	// Sun SPARC
    102 	SPARC = 2,
    103 	// Intel i386
    104 	I386 = 3,
    105 	// Motorola 68000
    106 	M68K = 4,
    107 	// Motorola 88000
    108 	M88K = 5,
    109 	// Intel i860
    110 	M860 = 7,
    111 	// MIPS R3000 Big-Endian only
    112 	MIPS = 8,
    113 	// IBM System/370
    114 	S370 = 9,
    115 	// MIPS R3000 Little-Endian
    116 	MIPS_RS3_LE = 10,
    117 	// HP PA-RISC
    118 	PARISC = 15,
    119 	// Fujitsu VPP500
    120 	VPP500 = 17,
    121 	// SPARC v8plus
    122 	SPARC32PLUS = 18,
    123 	// Intel 80960
    124 	I960 = 19,
    125 	// PowerPC 32-bit
    126 	PPC = 20,
    127 	// PowerPC 64-bit
    128 	PPC64 = 21,
    129 	// IBM System/390
    130 	S390 = 22,
    131 	// NEC V800
    132 	V800 = 36,
    133 	// Fujitsu FR20
    134 	FR20 = 37,
    135 	// TRW RH-32
    136 	RH32 = 38,
    137 	// Motorola RCE
    138 	RCE = 39,
    139 	// ARM
    140 	ARM = 40,
    141 	// Hitachi SH
    142 	SH = 42,
    143 	// SPARC v9 64-bit
    144 	SPARCV9 = 43,
    145 	// Siemens TriCore embedded processor
    146 	TRICORE = 44,
    147 	// Argonaut RISC Core
    148 	ARC = 45,
    149 	// Hitachi H8/300
    150 	H8_300 = 46,
    151 	// Hitachi H8/300H
    152 	H8_300H = 47,
    153 	// Hitachi H8S
    154 	H8S = 48,
    155 	// Hitachi H8/500
    156 	H8_500 = 49,
    157 	// Intel IA-64 Processor
    158 	IA_64 = 50,
    159 	// Stanford MIPS-X
    160 	MIPS_X = 51,
    161 	// Motorola ColdFire
    162 	COLDFIRE = 52,
    163 	// Motorola M68HC12
    164 	M68HC12 = 53,
    165 	// Fujitsu MMA
    166 	MMA = 54,
    167 	// Siemens PCP
    168 	PCP = 55,
    169 	// Sony nCPU
    170 	NCPU = 56,
    171 	// Denso NDR1 microprocessor
    172 	NDR1 = 57,
    173 	// Motorola Star*Core processor
    174 	STARCORE = 58,
    175 	// Toyota ME16 processor
    176 	ME16 = 59,
    177 	// STMicroelectronics ST100 processor
    178 	ST100 = 60,
    179 	// Advanced Logic Corp. TinyJ processor
    180 	TINYJ = 61,
    181 	// Advanced Micro Devices x86-64
    182 	X86_64 = 62,
    183 	// Sony DSP Processor
    184 	PDSP = 63,
    185 	// Digital Equipment Corp. PDP-10
    186 	PDP10 = 64,
    187 	// Digital Equipment Corp. PDP-11
    188 	PDP11 = 65,
    189 	// Siemens FX66 microcontroller
    190 	FX66 = 66,
    191 	// STMicroelectronics ST9+ 8/16 bit microcontroller
    192 	ST9PLUS = 67,
    193 	// STMicroelectronics ST7 8-bit microcontroller
    194 	ST7 = 68,
    195 	// Motorola MC68HC16 Microcontroller
    196 	M68HC16 = 69,
    197 	// Motorola MC68HC11 Microcontroller
    198 	M68HC11 = 70,
    199 	// Motorola MC68HC08 Microcontroller
    200 	M68HC08 = 71,
    201 	// Motorola MC68HC05 Microcontroller
    202 	M68HC05 = 72,
    203 	// Silicon Graphics SVx
    204 	SVX = 73,
    205 	// STMicroelectronics ST19 8-bit microcontroller
    206 	ST19 = 74,
    207 	// Digital VAX
    208 	VAX = 75,
    209 	// Axis Communications 32-bit embedded processor
    210 	CRIS = 76,
    211 	// Infineon Technologies 32-bit embedded processor
    212 	JAVELIN = 77,
    213 	// Element 14 64-bit DSP Processor
    214 	FIREPATH = 78,
    215 	// LSI Logic 16-bit DSP Processor
    216 	ZSP = 79,
    217 	// Donald Knuth's educational 64-bit processor
    218 	MMIX = 80,
    219 	// Harvard University machine-independent object files
    220 	HUANY = 81,
    221 	// SiTera Prism
    222 	PRISM = 82,
    223 	// Atmel AVR 8-bit microcontroller
    224 	AVR = 83,
    225 	// Fujitsu FR30
    226 	FR30 = 84,
    227 	// Mitsubishi D10V
    228 	D10V = 85,
    229 	// Mitsubishi D30V
    230 	D30V = 86,
    231 	// NEC v850
    232 	V850 = 87,
    233 	// Mitsubishi M32R
    234 	M32R = 88,
    235 	// Matsushita MN10300
    236 	MN10300 = 89,
    237 	// Matsushita MN10200
    238 	MN10200 = 90,
    239 	// picoJava
    240 	PJ = 91,
    241 	// OpenRISC 32-bit embedded processor
    242 	OPENRISC = 92,
    243 	// ARC International ARCompact processor
    244 	ARC_COMPACT = 93,
    245 	// Tensilica Xtensa Architecture
    246 	XTENSA = 94,
    247 	// Alphamosaic VideoCore processor
    248 	VIDEOCORE = 95,
    249 	// Thompson Multimedia General Purpose Processor
    250 	TMM_GPP = 96,
    251 	// National Semiconductor 32000 series
    252 	NS32K = 97,
    253 	// Tenor Network TPC processor
    254 	TPC = 98,
    255 	// Trebia SNP 1000 processor
    256 	SNP1K = 99,
    257 	// STMicroelectronics (www.st.com) ST200 microcontroller
    258 	ST200 = 100,
    259 	// Ubicom IP2xxx microcontroller family
    260 	IP2K = 101,
    261 	// MAX Processor
    262 	MAX = 102,
    263 	// National Semiconductor CompactRISC microprocessor
    264 	CR = 103,
    265 	// Fujitsu F2MC16
    266 	F2MC16 = 104,
    267 	// Texas Instruments embedded microcontroller msp430
    268 	MSP430 = 105,
    269 	// Analog Devices Blackfin (DSP) processor
    270 	BLACKFIN = 106,
    271 	// S1C33 Family of Seiko Epson processors
    272 	SE_C33 = 107,
    273 	// Sharp embedded microprocessor
    274 	SEP = 108,
    275 	// Arca RISC Microprocessor
    276 	ARCA = 109,
    277 	// Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University
    278 	UNICORE = 110,
    279 	// eXcess: 16/32/64-bit configurable embedded CPU
    280 	EXCESS = 111,
    281 	// Icera Semiconductor Inc. Deep Execution Processor
    282 	DXP = 112,
    283 	// Altera Nios II soft-core processor
    284 	ALTERA_NIOS2 = 113,
    285 	// National Semiconductor CompactRISC CRX microprocessor
    286 	CRX = 114,
    287 	// Motorola XGATE embedded processor
    288 	XGATE = 115,
    289 	// Infineon C16x/XC16x processor
    290 	C166 = 116,
    291 	// Renesas M16C series microprocessors
    292 	M16C = 117,
    293 	// Microchip Technology dsPIC30F Digital Signal Controller
    294 	DSPIC30F = 118,
    295 	// Freescale Communication Engine RISC core
    296 	CE = 119,
    297 	// Renesas M32C series microprocessors
    298 	M32C = 120,
    299 	// Altium TSK3000 core
    300 	TSK3000 = 131,
    301 	// Freescale RS08 embedded processor
    302 	RS08 = 132,
    303 	// Analog Devices SHARC family of 32-bit DSP processors
    304 	SHARC = 133,
    305 	// Cyan Technology eCOG2 microprocessor
    306 	ECOG2 = 134,
    307 	// Sunplus S+core7 RISC processor
    308 	SCORE7 = 135,
    309 	// New Japan Radio (NJR) 24-bit DSP Processor
    310 	DSP24 = 136,
    311 	// Broadcom VideoCore III processor
    312 	VIDEOCORE3 = 137,
    313 	// RISC processor for Lattice FPGA architecture
    314 	LATTICEMICO32 = 138,
    315 	// Seiko Epson C17 family
    316 	SE_C17 = 139,
    317 	// The Texas Instruments TMS320C6000 DSP family
    318 	TI_C6000 = 140,
    319 	// The Texas Instruments TMS320C2000 DSP family
    320 	TI_C2000 = 141,
    321 	// The Texas Instruments TMS320C55x DSP family
    322 	TI_C5500 = 142,
    323 	// Texas Instruments Application Specific RISC Processor, 32bit fetch
    324 	TI_ARP32 = 143,
    325 	// Texas Instruments Programmable Realtime Unit
    326 	TI_PRU = 144,
    327 	// STMicroelectronics 64bit VLIW Data Signal Processor
    328 	MMDSP_PLUS = 160,
    329 	// Cypress M8C microprocessor
    330 	CYPRESS_M8C = 161,
    331 	// Renesas R32C series microprocessors
    332 	R32C = 162,
    333 	// NXP Semiconductors TriMedia architecture family
    334 	TRIMEDIA = 163,
    335 	// QUALCOMM DSP6 Processor
    336 	QDSP6 = 164,
    337 	// Intel 8051 and variants
    338 	I8051 = 165,
    339 	// STMicroelectronics STxP7x family of configurable and extensible RISC processors
    340 	STXP7X = 166,
    341 	// Andes Technology compact code size embedded RISC processor family
    342 	NDS32 = 167,
    343 	// Cyan Technology eCOG1X family
    344 	ECOG1 = 168,
    345 	// Cyan Technology eCOG1X family
    346 	ECOG1X = 168,
    347 	// Dallas Semiconductor MAXQ30 Core Micro-controllers
    348 	MAXQ30 = 169,
    349 	// New Japan Radio (NJR) 16-bit DSP Processor
    350 	XIMO16 = 170,
    351 	// M2000 Reconfigurable RISC Microprocessor
    352 	MANIK = 171,
    353 	// Cray Inc. NV2 vector architecture
    354 	CRAYNV2 = 172,
    355 	// Renesas RX family
    356 	RX = 173,
    357 	// Imagination Technologies META processor architecture
    358 	METAG = 174,
    359 	// MCST Elbrus general purpose hardware architecture
    360 	MCST_ELBRUS = 175,
    361 	// Cyan Technology eCOG16 family
    362 	ECOG16 = 176,
    363 	// National Semiconductor CompactRISC CR16 16-bit microprocessor
    364 	CR16 = 177,
    365 	// Freescale Extended Time Processing Unit
    366 	ETPU = 178,
    367 	// Infineon Technologies SLE9X core
    368 	SLE9X = 179,
    369 	// Intel L10M
    370 	L10M = 180,
    371 	// Intel K10M
    372 	K10M = 181,
    373 	// ARM 64-bit Architecture (AArch64)
    374 	AARCH64 = 183,
    375 	// Atmel Corporation 32-bit microprocessor family
    376 	AVR32 = 185,
    377 	// STMicroeletronics STM8 8-bit microcontroller
    378 	STM8 = 186,
    379 	// Tilera TILE64 multicore architecture family
    380 	TILE64 = 187,
    381 	// Tilera TILEPro multicore architecture family
    382 	TILEPRO = 188,
    383 	// Xilinx MicroBlaze 32-bit RISC soft processor core
    384 	MICROBLAZE = 189,
    385 	// NVIDIA CUDA architecture
    386 	CUDA = 190,
    387 	// Tilera TILE-Gx multicore architecture family
    388 	TILEGX = 191,
    389 	// CloudShield architecture family
    390 	CLOUDSHIELD = 192,
    391 	// KIPO-KAIST Core-A 1st generation processor family
    392 	COREA_1ST = 193,
    393 	// KIPO-KAIST Core-A 2nd generation processor family
    394 	COREA_2ND = 194,
    395 	// Synopsys ARCompact V2
    396 	ARC_COMPACT2 = 195,
    397 	// Open8 8-bit RISC soft processor core
    398 	OPEN8 = 196,
    399 	// Renesas RL78 family
    400 	RL78 = 197,
    401 	// Broadcom VideoCore V processor
    402 	VIDEOCORE5 = 198,
    403 	// Renesas 78KOR family
    404 	R78KOR = 199,
    405 	// Freescale 56800EX Digital Signal Controller (DSC)
    406 	F56800EX = 200,
    407 	// Beyond BA1 CPU architecture
    408 	BA1 = 201,
    409 	// Beyond BA2 CPU architecture
    410 	BA2 = 202,
    411 	// XMOS xCORE processor family
    412 	XCORE = 203,
    413 	// Microchip 8-bit PIC(r) family
    414 	MCHP_PIC = 204,
    415 	// Reserved by Intel
    416 	INTEL205 = 205,
    417 	// Reserved by Intel
    418 	INTEL206 = 206,
    419 	// Reserved by Intel
    420 	INTEL207 = 207,
    421 	// Reserved by Intel
    422 	INTEL208 = 208,
    423 	// Reserved by Intel
    424 	INTEL209 = 209,
    425 	// KM211 KM32 32-bit processor
    426 	KM32 = 210,
    427 	// KM211 KMX32 32-bit processor
    428 	KMX32 = 211,
    429 	// KM211 KMX16 16-bit processor
    430 	KMX16 = 212,
    431 	// KM211 KMX8 8-bit processor
    432 	KMX8 = 213,
    433 	// KM211 KVARC processor
    434 	KVARC = 214,
    435 	// Paneve CDP architecture family
    436 	CDP = 215,
    437 	// Cognitive Smart Memory Processor
    438 	COGE = 216,
    439 	// Bluechip Systems CoolEngine
    440 	COOL = 217,
    441 	// Nanoradio Optimized RISC
    442 	NORC = 218,
    443 	// CSR Kalimba architecture family
    444 	CSR_KALIMBA = 219,
    445 	// Zilog Z80
    446 	Z80 = 220,
    447 	// Controls and Data Services VISIUMcore processor
    448 	VISIUM = 221,
    449 	// FTDI Chip FT32 high performance 32-bit RISC architecture
    450 	FT32 = 222,
    451 	// Moxie processor family
    452 	MOXIE = 223,
    453 	// AMD GPU architecture
    454 	AMDGPU = 224,
    455 	// RISC-V
    456 	RISCV = 243,
    457 	// Lanai 32-bit processor
    458 	LANAI = 244,
    459 	// Linux BPF – in-kernel virtual machine
    460 	BPF = 247,
    461 
    462 	// Intel i486 (deprecated)
    463 	I486 = 6,
    464 	// MIPS R4000 Big-Endian (deprecated)
    465 	MIPS_RS4_BE = 10,
    466 	// Digital Alpha (deprecated)
    467 	ALPHA_STD = 41,
    468 	// Alpha (deprecated)
    469 	ALPHA = 0x9026,
    470 };
    471 
    472 // ELF file type
    473 export type elf_type = enum u16 {
    474 	// No file type
    475 	NONE = 0,
    476 	// Relocatable object file
    477 	REL = 1,
    478 	// Executable file
    479 	EXEC = 2,
    480 	// Shared object file
    481 	DYN = 3,
    482 	// Core file
    483 	CORE = 4,
    484 	// Environment-specific use
    485 	LOOS = 0xFE00,
    486 	// Environment-specific use
    487 	HIOS = 0xFEFF,
    488 	// Processor-specific use
    489 	LOPROC = 0xFF00,
    490 	// Processor-specific use
    491 	HIPROC = 0xFFFF,
    492 };
    493 
    494 // Application binary interface
    495 export type ident_abi = enum u8 {
    496 	// System-V ABI
    497 	SYSV = 0,
    498 	// HP-UX operating system
    499 	HPUX = 1,
    500 	// Standalone (embedded) application
    501 	STANDALONE = 255,
    502 };
    503 
    504 // Special section indicies
    505 export type shn = enum u16 {
    506 	// Used to mark an undefined or meaningless section reference
    507 	UNDEF = 0,
    508 	// Processor-specific use
    509 	LOPROC = 0xFF00,
    510 	// Processor-specific use
    511 	HIPROC = 0xFF1F,
    512 	// Environment-specific-use
    513 	LOOS = 0xFF20,
    514 	// Environment-specific-use
    515 	HIOS = 0xFF3F,
    516 	// Indicates that the corresponding reference is an absolute value
    517 	ABS = 0xFFF1,
    518 	// Indicates a symbol that has been declared as a common block
    519 	COMMON = 0xFFF2,
    520 };
    521 
    522 // Section type
    523 export type sht = enum u32 {
    524 	// Marks an unused section header
    525 	NULL = 0,
    526 	// Contains information defined by the program
    527 	PROGBITS = 1,
    528 	// Contains a linker symbol table
    529 	SYMTAB = 2,
    530 	// Contains a string table
    531 	STRTAB = 3,
    532 	// Contains "Rela" type relocation entries
    533 	RELA = 4,
    534 	// Contains a symbol hash table
    535 	HASH = 5,
    536 	// Contains dynamic linking tables
    537 	DYNAMIC = 6,
    538 	// Contains note information
    539 	NOTE = 7,
    540 	// Contains uninitialized space; does not occupy any space in the file
    541 	NOBITS = 8,
    542 	// Contains "Rel" type relocation entries
    543 	REL = 9,
    544 	// Reserved
    545 	SHLIB = 10,
    546 	// Contains a dynamic loader symbol table
    547 	DYNSYM = 11,
    548 	// Environment-specific use
    549 	LOOS = 0x60000000,
    550 	// Environment-specific use
    551 	HIOS = 0x6FFFFFFF,
    552 	// Processor-specific use
    553 	LOPROC = 0x7000000,
    554 	// Processor-specific use
    555 	HIPROC = 0x7FFFFFF,
    556 };
    557 
    558 // Section flags
    559 export type shf = enum u32 {
    560 	// Section contains no data
    561 	NONE = 0,
    562 	// Section contains writable data
    563 	WRITE = 0x1,
    564 	// Section is allocated in memory image of program
    565 	ALLOC = 0x2,
    566 	// Section contains executable instructions
    567 	EXECINSTR = 0x4,
    568 	// Environment-specific use
    569 	MASKOS = 0x0F000000,
    570 	// Processor-specific use
    571 	MASKPROC = 0xF0000000,
    572 };
    573 
    574 // Symbol table entry
    575 export type sym64 = struct {
    576 	// Symbol name offset
    577 	st_name: u32,
    578 	// Type and binding attributes
    579 	st_info: u8,
    580 	// Reserved
    581 	st_other: u8,
    582 	// Section table index
    583 	st_shndx: u16,
    584 	// Symbol value
    585 	st_value: u64,
    586 	// Size of object
    587 	st_size: u64,
    588 };
    589 
    590 // Symbol bindings
    591 export type stb = enum u8 {
    592 	// Not visible outside the object file
    593 	LOCAL = 0,
    594 	// Global symbol, visible to all object files
    595 	GLOBAL = 1,
    596 	// Global scope, but with lower precedence than global symbols
    597 	WEAK = 2,
    598 	// Environment-specific use
    599 	LOOS = 10,
    600 	// Environment-specific use
    601 	HIOS = 12,
    602 	// Processor-specific use
    603 	LOPROC = 13,
    604 	// Processor-specific use
    605 	HIPROC = 15,
    606 };
    607 
    608 // Obtains the binding part of [sym64.st_info].
    609 //
    610 // Equivalent to the ELF64_ST_BIND macro.
    611 export fn st_bind(i: u8) stb = (i >> 4): stb;
    612 
    613 // Symbol types
    614 export type stt = enum u8 {
    615 	// No type specified (e.g. an absolute symbol)
    616 	NOTYPE = 0,
    617 	// Data object
    618 	OBJECT = 1,
    619 	// Function entry point
    620 	FUNC = 2,
    621 	// Symbol is associated with a section
    622 	SECTION = 3,
    623 	// Source file associated with the object
    624 	FILE = 4,
    625 	// Symbol is a common data object
    626 	COMMON = 5,
    627 	// Environment-specific use
    628 	LOOS = 10,
    629 	// Environment-specific use
    630 	HIOS = 12,
    631 	// Processor-specific use
    632 	LOPROC = 13,
    633 	// Processor-specific use
    634 	HIPROC = 15,
    635 };
    636 
    637 // Obtains the type part of [sym64.st_info].
    638 //
    639 // Equivalent to the ELF64_ST_TYPE macro.
    640 export fn st_type(i: u8) stt = (i & 0xF): stt;
    641 
    642 // Converts symbol bindings and type into [sym64.st_info].
    643 //
    644 // Equivalent to the ELF64_ST_INFO macro.
    645 export fn st_info(b: stb, t: stt) u8 = b: u8 << 4 + t: u8 & 0xF;
    646 
    647 // Relocation entry
    648 export type rel64 = struct {
    649 	// Address of reference
    650 	r_offset: u64,
    651 	// Symbol table index and type of relocation
    652 	r_info: u64,
    653 };
    654 
    655 // Relocation entry with explicit addend
    656 export type rela64 = struct {
    657 	// Address of reference
    658 	r_offset: u64,
    659 	// Symbol table index and type of relocation
    660 	r_info: u64,
    661 	// Constant part of expression
    662 	r_addend: i64,
    663 };
    664 
    665 // Obtains the symbol table index part of [rel64.r_info].
    666 //
    667 // Equivalent to the ELF64_R_SYM macro.
    668 export fn r64_sym(info: u64) u64 = info >> 32;
    669 
    670 // Obtains the relocation type part of [rel64.r_info].
    671 //
    672 // Equivalent to the ELF64_R_TYPE macro.
    673 export fn r64_type(info: u64) u64 = info & 0xFFFFFFFF;
    674 
    675 // Converts symbol table index and a relocation type into [rel64.r_info].
    676 //
    677 // Equivalent to the ELF64_R_INFO macro.
    678 export fn r64_info(sym: u64, stype: u64) u64 = sym << 32 | stype & 0xFFFFFFFF;
    679 
    680 // Program header table entry (segment)
    681 export type phdr64 = struct {
    682 	// Type of segment
    683 	p_type: pt,
    684 	// Segment attributes
    685 	p_flags: u32,
    686 	// Offset in file
    687 	p_offset: u64,
    688 	// Virtual address in memory
    689 	p_vaddr: u64,
    690 	// Reserved
    691 	p_paddr: u64,
    692 	// Size of segment in file
    693 	p_filesz: u64,
    694 	// Size of segment in memory
    695 	p_memsz: u64,
    696 	// Alignment of segment
    697 	p_align: u64,
    698 };
    699 
    700 // Segment types
    701 export type pt = enum u32 {
    702 	// Unused entry
    703 	NULL = 0,
    704 	// Loadable segment
    705 	LOAD = 1,
    706 	// Dynamic linking tables
    707 	DYNAMIC = 2,
    708 	// Program interpreter path name
    709 	INTERP = 3,
    710 	// Note sections
    711 	NOTE = 4,
    712 	// Reserved
    713 	SHLIB = 5,
    714 	// Program header table
    715 	PHDR = 6,
    716 	// Environment-specific use
    717 	LOOS = 0x60000000,
    718 	// Environment-specific use
    719 	HIOS = 0x6FFFFFFF,
    720 	// Processor-specific use
    721 	LOPROC = 0x70000000,
    722 	// Processor-specific use
    723 	HIPROC = 0x7FFFFFFF,
    724 };
    725 
    726 // Segment attributes
    727 export type pf = enum u32 {
    728 	// No permission
    729 	NONE = 0,
    730 	// Execute permission
    731 	X = 0x1,
    732 	// Write permission
    733 	W = 0x2,
    734 	// Read permission
    735 	R = 0x4,
    736 	// Reserved for environment-specific use
    737 	MASKOS = 0x00FF0000,
    738 	// Reserved for processor-specific use
    739 	MASKPROC = 0xFF000000,
    740 };
    741 
    742 // Dynamic table entry
    743 export type dyn64 = struct {
    744 	// The type of this entry
    745 	d_tag: dt,
    746 	// Additional data associated with this entry. The value which is valid
    747 	// is selected based on the entry type.
    748 	union {
    749 		d_val: u64,
    750 		d_ptr: u64,
    751 	},
    752 };
    753 
    754 // Dynamic table entry type
    755 export type dt = enum i64 {
    756 	// Marks the end of the dynamic array.
    757 	NULL = 0,
    758 	// The string table offset of the name of a needed library.
    759 	NEEDED = 1,
    760 	// Total size, in bytes, of the relocation entries associated with the
    761 	// procedure linkage table.
    762 	PLTRELSZ = 2,
    763 	// Contains an address associated with the linkage table. The specific
    764 	// meaning of this field is processor-dependent.
    765 	PLTGOT = 3,
    766 	// Address of the symbol hash table.
    767 	HASH = 4,
    768 	// Address of the dynamic string table.
    769 	STRTAB = 5,
    770 	// Address of the dynamic symbol table.
    771 	SYMTAB = 6,
    772 	// Address of a relocation table with rela64 entries.
    773 	RELA = 7,
    774 	// Total size, in bytes, of the RELA relocation table.
    775 	RELASZ = 8,
    776 	// Size, in bytes, of each RELA relocation entry.
    777 	RELAENT = 9,
    778 	// Total size, in bytes, of the string table.
    779 	STRSZ = 10,
    780 	// Size, in bytes, of each symbol table entry.
    781 	SYMENT = 11,
    782 	// Address of the initialization function.
    783 	INIT = 12,
    784 	// Address of the termination function.
    785 	FINI = 13,
    786 	// The string table offset of the name of this shared object.
    787 	SONAME = 14,
    788 	// The string table offset of a shared library search path string.
    789 	RPATH = 15,
    790 	// The presence of this dynamic table entry modifies the symbol
    791 	// resolution algorithm for references within the library. Symbols
    792 	// defined within the library are used to resolve references before the
    793 	// dynamic linker searches the usual search path.
    794 	SYMBOLIC = 16,
    795 	// Address of a relocation table with rel64 entries.
    796 	REL = 17,
    797 	// Total size, in bytes, of the REL relocation table.
    798 	RELSZ = 18,
    799 	// Size, in bytes, of each REL relocation entry.
    800 	RELENT = 19,
    801 	// Type of relocation entry used for the procedure linkage table. The
    802 	// d_val member contains either [dt::REL] or [dt::RELA].
    803 	PLTREL = 20,
    804 	// Reserved for debugger use.
    805 	DEBUG = 21,
    806 	// The presence of this dynamic table entry signals that the relocation
    807 	// table contains relocations for a non-writable segment.
    808 	TEXTREL = 22,
    809 	// Address of the relocations associated with the procedure linkage
    810 	// table.
    811 	JMPREL = 23,
    812 	// The presence of this dynamic table entry signals that the dynamic
    813 	// loader should process all relocations for this object before
    814 	// transferring control to the program.
    815 	BIND_NOW = 24,
    816 	// Pointer to an array of initialiation functions.
    817 	INIT_ARRAY = 25,
    818 	// Pointer to an array of termination functions.
    819 	FINI_ARRAY = 26,
    820 	// Size, in bytes, of the array of initialization functions.
    821 	INIT_ARRAYSZ = 27,
    822 	// Size, in bytes, of the array of termination functions.
    823 	FINI_ARRAYSZ = 28,
    824 	// Reserved for environment-specific use.
    825 	LOOS = 0x60000000,
    826 
    827 	// Symbol versioning entry types, GNU extension
    828 	// Version table records
    829 	// .gnu.version  section address
    830 	VERSYM = 0x6FFFFFF0,
    831 	// .gnu.version_d section address
    832 	VERDEF = 0x6FFFFFFC,
    833 	// Number of version definitions
    834 	VERDEFNUM = 0x6FFFFFFD,
    835 	// .gnu.version_r section address
    836 	VERNEED = 0x6FFFFFFE,
    837 	// Number of needed versions
    838 	VERNEEDNUM = 0x6FFFFFFF,
    839 
    840 	// Reserved for environment-specific use.
    841 	HIOS = 0x6FFFFFFF,
    842 	// Reserved for processor-specific use.
    843 	LOPROC = 0x70000000,
    844 	// Reserved for processor-specific use.
    845 	HIPROC = 0x7FFFFFFF,
    846 };
    847 
    848 // Auxiliary vector
    849 export type auxv64 = struct {
    850 	// Entry type
    851 	a_type: at,
    852 	union {
    853 		// Integer value
    854 		a_val: u64,
    855 		a_ptr: *void,
    856 		a_fnc: *fn() void,
    857 	}
    858 };
    859 
    860 // Legal auxiliary vector entry types
    861 export type at = enum u64 {
    862 	// End of vector
    863 	NULL = 0,
    864 	// Entry should be ignored
    865 	IGNORE = 1,
    866 	// File descriptor of program
    867 	EXECFD = 2,
    868 	// Program headers for program
    869 	PHDR = 3,
    870 	// Size of program header entry
    871 	PHENT = 4,
    872 	// Number of program headers
    873 	PHNUM = 5,
    874 	// System page size
    875 	PAGESZ = 6,
    876 	// Base address of interpreter
    877 	BASE = 7,
    878 	// Flags
    879 	FLAGS = 8,
    880 	// Entry point of program
    881 	ENTRY = 9,
    882 	// Program is not ELF
    883 	NOTELF = 10,
    884 	// Real uid
    885 	UID = 11,
    886 	// Effective uid
    887 	EUID = 12,
    888 	// Real gid
    889 	GID = 13,
    890 	// Effective gid
    891 	EGID = 14,
    892 	// Frequency of times()
    893 	CLKTCK = 17,
    894 
    895 	// String identifying platform.
    896 	PLATFORM = 15,
    897 	// Machine-dependent hints about processor capabilities.
    898 	HWCAP = 16,
    899 
    900 	// Used FPU control word.
    901 	FPUCW = 18,
    902 
    903 	// Data cache block size.
    904 	DCACHEBSIZE = 19,
    905 	// Instruction cache block size.
    906 	ICACHEBSIZE = 20,
    907 	// Unified cache block size.
    908 	UCACHEBSIZE = 21,
    909 
    910 	// A special ignored value for PPC, used by the kernel to control the
    911 	// interpretation of the AUXV. Must be > 16.
    912 	// Entry should be ignored.
    913 	IGNOREPPC = 22,
    914 	// Boolean, was exec setuid-like?
    915 	SECURE = 23,
    916 	// String identifying real platforms.
    917 	BASE_PLATFORM = 24,
    918 	// Address of 16 random bytes.
    919 	RANDOM = 25,
    920 	// More machine-dependent hints about processor capabilities.
    921 	HWCAP2 = 26,
    922 	// Filename of executable.
    923 	EXECFN = 31,
    924 
    925 	// Pointer to the global system page used for system calls and other
    926 	// nice things.
    927 	SYSINFO = 32,
    928 	SYSINFO_EHDR = 33,
    929 
    930 	// Shapes of the caches.  Bits 0-3 contains associativity, bits 4-7 contains
    931 	// log2 of line size, mask those to get cache size.
    932 	L1I_CACHESHAPE = 34,
    933 	L1D_CACHESHAPE = 35,
    934 	L2_CACHESHAPE = 36,
    935 	L3_CACHESHAPE = 37,
    936 
    937 	// Shapes of the caches, with more room to describe them.
    938 	// *GEOMETRY are comprised of cache line size in bytes in the bottom 16 bits
    939 	// and the cache associativity in the next 16 bits.
    940 	L1I_CACHESIZE = 40,
    941 	L1I_CACHEGEOMETRY = 41,
    942 	L1D_CACHESIZE = 42,
    943 	L1D_CACHEGEOMETRY = 43,
    944 	L2_CACHESIZE = 44,
    945 	L2_CACHEGEOMETRY = 45,
    946 	L3_CACHESIZE = 46,
    947 	L3_CACHEGEOMETRY = 47,
    948 
    949 	// Stack needed for signal delivery (AArch64).
    950 	MINSIGSTKSZ = 51,
    951 };
    952 
    953 // Version definition section
    954 export type verdef64 = struct {
    955 	// Version revision
    956 	vd_version: u16,
    957 	// Version information
    958 	vd_flags: u16,
    959 	// Version Index
    960 	vd_ndx: u16,
    961 	// Number of associated aux entries
    962 	vd_cnt: u16,
    963 	// Version name hash value
    964 	vd_hash: u32,
    965 	// Offset in bytes to verdaux array
    966 	vd_aux: u32,
    967 	// Offset in bytes to next verdef entry
    968 	vd_next: u32,
    969 };
    970 
    971 // Auxiliary version information
    972 export type verdaux64 = struct {
    973 	vda_name: u32,
    974 	vda_next: u32,
    975 };
    976 
    977 // Version revision values
    978 export type ver_def = enum u16 {
    979 	NONE = 0,
    980 	CURRENT = 1,
    981 	NUM = 2,
    982 };
    983 
    984 // Version information flags
    985 export type ver_flg = enum u16 {
    986 	BASE = 0x1,
    987 	WEAK = 0x2,
    988 };
    989 
    990 // Versym index values
    991 export type ver_ndx = enum u16 {
    992 	LOCAL = 0,
    993 	GLOBAL = 1,
    994 	LORESERVE = 0xff00,
    995 	ELIMINATE = 0xff01,
    996 };
    997 
    998 // DT_HASH section header
    999 export type hashhdr = struct {
   1000 	nbucket: u32,
   1001 	nchain: u32,
   1002 };