interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / bridge / shlib / shlib.om

2414 lines · 61473 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0
3note bridge/shlib/shlib.om: writes an AArch64 Linux shared library (ELF64 ET_DYN) from a table and the native
4note object omc emits for an OM-2 unit with `export fn` entries, and a probe executable that loads it through the
5note system's dynamic loader and calls it. No C compiler, assembler or linker is involved.
6note Build: seed/out/omc < bridge/shlib/shlib.om > shlib
7note shlib lib TABLE OBJECT OUT the library: DT_SONAME from TABLE, one global function symbol per export row
8note shlib call TABLE OUT the probe: an executable that needs the TABLE's SONAME (searched next to the
9note probe, DT_RUNPATH $ORIGIN, bound at load), makes each call row's call in order
10note and exits 0 when every result matches, 1+k (100 for k >= 99) when call k does
11note not, 250 when x19..x27, x29, d8..d15 or the stack pointer did not come back
12note unchanged
13note OBJECT is omc's output for the line "object" followed by the unit (OMOBJ001, spec/omegese-modules.md).
14note TABLE: one row per line, fields separated by one tab; an empty line or one starting with # is skipped.
15note soname NAME the library's DT_SONAME (exactly one row)
16note needed NAME a DT_NEEDED entry: a library the dynamic linker loads first (any number, in order)
17note runpath PATH the library's DT_RUNPATH, where its needed libraries are searched (at most one;
18note $ORIGIN is the library's own directory)
19note state NAME BYTES a zero-filled writable region of BYTES bytes (1..4294967296), 64-byte aligned,
20note in the library's data segment (one copy per process); a state row comes before
21note every row that names it
22note lock STATE LOCK UNLOCK every frame export (below) calls the C function LOCK with STATE's address first
23note and UNLOCK with it on both its return and its trap path (at most one row; LOCK
24note and UNLOCK, pthread_mutex_lock and pthread_mutex_unlock for example, are imports
25note of the library); LOCK answering nonzero returns TRAP without running the body
26note import NAME MAP... how the C arguments of the object's import NAME are made from omc's parameter
27note slots: up to 8 tokens sK (slot K) or sK+N (slot K plus the byte offset N); without
28note a row, C argument k is slot k
29note export NAME TRAP NAME, an `export fn` of the object, becomes a global function symbol; a trap
30note inside it returns TRAP (0..4294967295) to the C caller
31note export NAME TRAP frame STATE... the same, as a frame export: omc's first parameter is a sized view or
32note edit of ulong (count 1..9 + states) holding the C argument registers x0..x7 as
33note they came (fr 0..7), then the address of every state row in table order (fr 8
34note on), then the address of the caller's stack arguments (fr 8 + states: AAPCS64
35note argument 9 on, 8 bytes each, a narrow one in its low bytes with the rest
36note unspecified); each further parameter, one per STATE named (at most 5), is a sized
37note view or edit that the wrapper points at that state, at most its size
38note call NAME EXPECT ARG... the probe calls NAME with up to 10 arguments (a decimal 64-bit value, buf for
39note a pointer to one writable buffer, every byte 0xA5 at load and shared by every
40note call, so a later call reads what an earlier one wrote, buf+N for the pointer
41note N bytes into it, or bufword for the u64 that buffer holds at offset 0;
42note arguments 9 and 10 go on the stack, as AAPCS64
43note passes them) and compares the low 32 bits of the result with EXPECT (any:
44note not compared, for an address)
45note bufeq OFF VALUE the probe compares the u64 the buffer holds at byte offset OFF with VALUE, all
46note 64 bits, after the rows before it (at least one call row)
47note bufset OFF VALUE the probe stores the u64 VALUE at byte offset OFF of the buffer, after the rows
48note before it (at least one call row), so a later call reads it
49note reteq OFF VALUE the probe compares the u64 at byte OFF from the address the last call returned
50note with VALUE (at least one call row before it)
51note The buffer reaches 4096 bytes past the largest offset buf+N, bufeq or bufset names.
52note From the C calling convention (AAPCS64) to omc's function ABI, per export: each C argument register is one
53note omc parameter slot, in order (a scalar or a sized array: one slot; an unsized view or edit: a pointer slot
54note then a ulong count slot). Narrow integer arguments are sign- or zero-extended as omc expects. x7 points at a
55note per-call context on the caller's stack whose byte offset 16 ([x7, #16]) holds the trap entry; x19..x30 and d8..d15 are saved
56note there and restored on both the return and the trap path. The result is returned in x0.
57note Imports (an `extern fn` of the object): each becomes an undefined global function symbol bound at load
58note through a GOT slot (R_AARCH64_GLOB_DAT), and omc's BL to it goes to a veneer in .text that saves x7 and x30
59note (omc's context and return; C may change x7), makes the C arguments (the import row), calls through the
60note GOT, extends a narrow result as omc expects (C leaves the upper bits undefined) and returns. omc saves its
61note own live registers around the BL. An import's stack need counts as 0: the C callee runs on the caller's
62note stack below omc's frames, as any C call does.
63note Refused (exit 1, nothing written), each with its own message: an object that is not an OMOBJ001 version 1
64note AArch64 object (header, trailing bytes, records, padding, names); an import with an address or a stack
65note need; a function address
66note outside the code, unaligned or out of order, or an export's inside the entry slots; an entry slot that does not branch to
67note its function; a BL with no call record or two; a call record whose offset, source or cost does not fit its
68note BL, or whose destination is not the BL's target (an import's BL is the unlinked word 0x94000000); a
69note base-register use of x7 other than the trap-entry load
70note [x7, #16] (a pattern check over the load/store group); a stack need above the 2^48-byte address space or a
71note call cycle; a float scalar parameter or result (omc passes them in x registers, C in v registers); more than 6
72note parameter slots; a table export that is not an export of the object; a repeated or malformed name; a
73note malformed row or number; no soname or no export; a call of a name that is no export of the table; an
74note import row naming no import of the object, or a slot past the import's slots; a frame export whose
75note parameters do not match its row; a lock with an export that is not a frame export.
76note Exit 0 written, 1 refused, 2 usage, 3 a file unreadable or unwritable or memory refused.
77note Every buffer is a run-time pool sized from its input. The wrappers, entry slots and probe reach their
78note targets with ADR and LDR literal (1 MiB) and B and BL (128 MiB) because each is one word, the data segment
79note with ADRP (4 GiB); an output past their reach is refused (e_reach), never truncated. That is a choice of
80note instruction forms, not a machine limit: a larger output needs longer sequences and veneers for far branches.
81note A frame export's frame (72 + 8 per state bytes, 16-aligned) is reached with 12-bit immediates, and so is the
82note caller's stack 192 bytes above it (e_reach when frame + 192 passes 4080).
83global ib uchar *
84global eb uchar *
85global ds uchar *
86global ex ulong *
87global cl ulong *
88global cd ulong *
89global fr ulong *
90global ck ulong *
91global st ulong *
92global im ulong *
93global mp ulong *
94global nd ulong *
95global ag uchar 4096
96global fo ulong 16
97global fl ulong 16
98global nv ulong 64
99global ly ulong 32
100note nv: 0 table end (object start), 1 object end, 2 dynstr length, 3 exports, 4 calls, 5 soname offset in ib,
101note 6 soname length, 7 functions, 8 code end, 9 fields in the current row, 10 output length, 13 soname in
102note dynstr, 14 runpath in dynstr, 15 section header offset, 16 address minus offset of the current section,
103note 17 object call records, 18 a stack need changed in this pass, 19 the previous function address,
104note 20 and 21 the slot and extension counts of exts, 22 the object's exported functions, 23 states, 24 import
105note symbols, 25 import rows, 26 needed rows, 27 the runpath's offset in ib plus 1 (0 none), 28 its length,
106note 29 the lock state plus 1 (0 none), 30 and 31 the LOCK name in ib, 32 and 33 the UNLOCK name, 34 and 35
107note their import symbols, 36 the state area's size, 37 .dynamic bytes, 38 the shared return's bytes
108note ck, per object call record (4): source function, destination function, stack cost, instruction offset
109note SMAX: the most stack a call may need, the AArch64 Linux user address space (48-bit virtual addresses,
110note 2^48 bytes): no stack can be larger. Every settled need and cost is at most SMAX, so need + cost stays
111note below 2^64; the probe's page count (at most 2^36) takes MOVZ and two MOVKs.
112const SMAX ulong 281474976710656
113note ex, per export (EXW): 0 name offset in ib, 1 name length, 2 trap value, 3 function index, 4 dynstr offset,
114note 5 wrapper offset in .text, 6 wrapper bytes, 7 extensions, 8 1 for a frame export, 9 its states, 10..14
115note those states, 15 its frame bytes
116const EXW ulong 16
117note st, per state (4): 0 name offset in ib, 1 name length, 2 bytes, 3 offset in the state area
118note im, per import symbol (8): 0 name offset in ib, 1 name length, 2 dynstr offset, 3 object function (the
119note function count when only the lock calls it), 4 import row (the row count when none), 5 veneer offset in
120note .text, 6 veneer bytes
121note mp, per import row (20): 0 name offset in ib, 1 name length, 2 tokens, 3..10 their slots, 11..18 their
122note offsets, 19 1 once an import uses the row
123note nd, per needed row (3): 0 name offset in ib, 1 name length, 2 dynstr offset
124note cl, per call row (CLW): 0 name offset, 1 name length, 2 expect, 3 arguments, 4..13 values, 14..23 1 for buf,
125note 2 for bufword, 24 1 for a bufeq row (its offset in 4, its value in 5; its name the call row's before it)
126const CLW ulong 25
127note cd, per call: the dynstr offset of its name
128note fr, per object function (8): 0 flags, 1 address, 2 result type, 3 parameters, 4 parameter records offset
129note in ib, 5 name offset in ib, 6 name length
130text m_lib "lib"
131text m_call "call"
132text k_son "soname"
133text k_exp "export"
134text k_call "call"
135text k_buf "buf"
136text k_bufw "bufword"
137text k_bufeq "bufeq"
138text k_bufp "buf+"
139text k_bufset "bufset"
140text k_reteq "reteq"
141text k_any "any"
142text k_need "needed"
143text k_rpath "runpath"
144text k_state "state"
145text k_lock "lock"
146text k_imp "import"
147text k_frame "frame"
148text k_obj "OMOBJ001"
149text t_interp "/lib/ld-linux-aarch64.so.1"
150text t_origin "$ORIGIN"
151note the section names, each space written as a zero: .hash 1, .dynsym 7, .dynstr 15, .text 23, .dynamic 29,
152note .rela.dyn 38, .got 48, .bss 53, .shstrtab 58
153text t_shs " .hash .dynsym .dynstr .text .dynamic .rela.dyn .got .bss .shstrtab "
154text e_usage "usage: shlib lib TABLE OBJECT OUT | shlib call TABLE OUT\n"
155text e_read "shlib: cannot read an input file\n"
156text e_write "shlib: cannot write the output file\n"
157text e_mem "shlib: memory refused\n"
158text e_row "shlib: malformed table row\n"
159text e_num "shlib: malformed number in the table\n"
160text e_name "shlib: a name outside A-Z a-z 0-9 _ (1..63 bytes; a soname also . + -, any length) or repeated\n"
161text e_son "shlib: the table needs exactly one soname row\n"
162text e_noexp "shlib: the table names no export\n"
163text e_trap "shlib: a trap value above 4294967295\n"
164text e_obj "shlib: the object is not an OMOBJ001 version 1 AArch64 object\n"
165text e_imp "shlib: an import with an address or a stack need\n"
166text e_improw "shlib: an import row names no import of the object, or a slot past the import's slots, or more than 8\n"
167text e_frame "shlib: a frame export's parameters do not match its row (a sized ulong frame of 1..9 + states, then one sized view or edit per state, within its size)\n"
168text e_state "shlib: a state row is malformed, repeated, or named before it is declared\n"
169text e_lock "shlib: a lock row is malformed or repeated, or an export of a locked library is not a frame export\n"
170text e_rt "shlib: the code loads a runtime entry other than the trap entry\n"
171text e_rtw "shlib: the code reads or writes through the per-call context (x7) other than the trap-entry load\n"
172text e_addr "shlib: a function address is outside the code, unaligned, out of order or inside the entry slots\n"
173text e_slotb "shlib: an export's entry slot does not branch to its function\n"
174text e_bl "shlib: a BL word has no call record, or two records name one BL\n"
175text e_call "shlib: a call record's offset, source or cost does not fit its BL\n"
176text e_cdst "shlib: a call record's destination is not its BL's target\n"
177text e_miss "shlib: a table export is not an export of the object\n"
178text e_flt "shlib: a float parameter or result; refused\n"
179text e_slot "shlib: more than 6 parameter slots\n"
180text e_ncall "shlib: a call names no export of the table\n"
181text e_reach "shlib: output past the AArch64 branch or literal reach\n"
182text e_stk "shlib: a function needs more stack than the 2^48-byte address space, or its calls form a cycle\n"
183note ---- small helpers
184fn die t view uchar * code int to void
185r = write 2 t 0 len t
186exit code
187end
188fn al8 x ulong to ulong
189ret and add x 7 not ulong 7
190end
191fn al16 x ulong to ulong
192ret and add x 15 not ulong 15
193end
194fn w8 o ulong v ulong to void
195put eb o uchar and v 255
196end
197fn w16 o ulong v ulong to void
198w8 o v
199w8 add o 1 shr v 8
200end
201fn w32 o ulong v ulong to void
202w16 o and v 65535
203w16 add o 2 shr v 16
204end
205fn w64 o ulong v ulong to void
206w32 o and v 4294967295
207w32 add o 4 shr v 32
208end
209note the u32 of eb at o
210fn r32 o ulong to ulong
211v = ulong 0
212for i ulong 4
213v = or v shl ulong get eb add o i mul i 8
214end
215ret v
216end
217note the u64 of ib at o, inside the object
218fn q o ulong to ulong
219if gt add o 8 get nv 1
220die e_obj 1
221end
222v = ulong 0
223for i ulong 8
224v = or v shl ulong get ib add o i mul i 8
225end
226ret v
227end
228note the u64 at object offset o
229fn oq o ulong to ulong
230ret q add get nv 0 o
231end
232note argument ai into ag; its length
233fn ap ai ulong to ulong
234r = argread ai ag 0 4095
235if le r 0
236die e_usage 2
237end
238ret ulong r
239end
240fn argis ai ulong t view uchar * to bool
241r = argread ai ag 0 4095
242if ne r long len t
243ret false
244end
245for i ulong len t bound 4096
246if ne get ag i get t i
247ret false
248end
249end
250ret true
251end
252note append argument ai's file to ib from offset at; the new end
253fn ldf ai ulong from ulong to ulong
254m = ap ai
255fd = fopen ag 0 m int 524288 int 0
256if lt fd 0
257die e_read 3
258end
259n = from
260while true bound 9223372036854775807
261if lt sub len ib n 65536
262r = enlarge ib add add n 65536 len ib
263if lt r 0
264die e_mem 3
265end
266end
267x = read int fd ib n sub len ib n
268if lt x 0
269die e_read 3
270end
271if eq x 0
272break
273end
274n = add n ulong x
275end
276r = fclose int fd
277ret n
278end
279note write eb[0, nv 10) to argument ai's path with mode md
280fn save ai ulong md int to void
281m = ap ai
282r = unlink ag 0 m
283fd = fopen ag 0 m int 524865 md
284if lt fd 0
285die e_write 3
286end
287n = get nv 10
288o = ulong 0
289while lt o n bound 9223372036854775807
290x = write int fd eb o sub n o
291if le x 0
292die e_write 3
293end
294o = add o ulong x
295end
296r = fclose int fd
297if lt r 0
298die e_write 3
299end
300end
301note eb grown to n bytes, all zero
302fn ebnew n ulong to void
303if gt n len eb
304r = enlarge eb n
305if lt r 0
306die e_mem 3
307end
308end
309for i ulong n bound 9223372036854775807
310put eb i 0
311end
312put nv 10 n
313end
314note ---- table rows: the fields of one line into fo/fl, their count into nv 9; returns the next line
315fn row o ulong e ulong to ulong
316put nv 9 0
317s = o
318p = o
319while lt p e bound 9223372036854775807
320c = get ib p
321if eq c 10
322break
323end
324if eq c 9
325k = get nv 9
326if ge k 15
327die e_row 1
328end
329put fo k s
330put fl k sub p s
331put nv 9 add k 1
332s = add p 1
333end
334p = add p 1
335end
336k = get nv 9
337put fo k s
338put fl k sub p s
339put nv 9 add k 1
340if lt p e
341p = add p 1
342end
343ret p
344end
345fn ft i ulong t view uchar * to bool
346if ne get fl i len t
347ret false
348end
349o = get fo i
350for j ulong len t bound 4096
351if ne get ib add o j get t j
352ret false
353end
354end
355ret true
356end
357note field i starts with text t and is longer
358fn fpre i ulong t view uchar * to bool
359if le get fl i len t
360ret false
361end
362o = get fo i
363for j ulong len t bound 4096
364if ne get ib add o j get t j
365ret false
366end
367end
368ret true
369end
370note field i as a decimal u64
371fn fnum i ulong to ulong
372ret dnum get fo i get fl i
373end
374note ib[o, o+n) as a decimal u64
375fn dnum o ulong n ulong to ulong
376if or eq n 0 gt n 20
377die e_num 1
378end
379v = ulong 0
380for j ulong n bound 20
381c = get ib add o j
382if or lt c 48 gt c 57
383die e_num 1
384end
385d = ulong sub c 48
386if gt v div sub 18446744073709551615 d 10
387die e_num 1
388end
389v = add mul v 10 d
390end
391ret v
392end
393note ib[a, a+n) equals ib[b, b+n)
394fn same a ulong b ulong n ulong to bool
395for j ulong n bound 9223372036854775807
396if ne get ib add a j get ib add b j
397ret false
398end
399end
400ret true
401end
402note field i is a name: 1..63 bytes of A-Z a-z 0-9 _ (omc's own name limit), or when so (a soname) at least
403note one byte of those and . + -
404fn fname i ulong so bool to void
405n = get fl i
406if or eq n 0 and not so gt n 63
407die e_name 1
408end
409o = get fo i
410for j ulong n bound 9223372036854775807
411c = get ib add o j
412ok = or and ge c 48 le c 57 eq c 95
413ok = or ok or and ge c 65 le c 90 and ge c 97 le c 122
414if so
415ok = or ok or or eq c 46 eq c 43 eq c 45
416end
417if not ok
418die e_name 1
419end
420end
421end
422note the export named ib[a, a+n), or the export count when none
423fn exfind a ulong n ulong to ulong
424nx = get nv 3
425for k ulong nx bound 9223372036854775807
426b = mul k EXW
427if eq get ex add b 1 n
428if same get ex b a n
429ret k
430end
431end
432end
433ret nx
434end
435fn exrow to void
436nf = get nv 9
437if lt nf 3
438die e_row 1
439end
440nb = ulong 0
441if gt nf 3
442if not ft 3 k_frame
443die e_row 1
444end
445nb = sub nf 4
446if gt nb 5
447die e_frame 1
448end
449end
450fname 1 false
451nx = get nv 3
452if ne exfind get fo 1 get fl 1 nx
453die e_name 1
454end
455t = fnum 2
456if gt t 4294967295
457die e_trap 1
458end
459r = enlarge ex mul add nx 1 EXW
460if lt r 0
461die e_mem 3
462end
463b = mul nx EXW
464for j ulong EXW
465put ex add b j 0
466end
467put ex b get fo 1
468put ex add b 1 get fl 1
469put ex add b 2 t
470if gt nf 3
471put ex add b 8 1
472end
473put ex add b 9 nb
474for j ulong nb bound 5
475s = stfind add j 4
476if eq s get nv 23
477die e_state 1
478end
479put ex add add b 10 j s
480end
481put nv 3 add nx 1
482end
483note the state named by field i, or the state count when none
484fn stfind i ulong to ulong
485n = get nv 23
486for k ulong n bound 9223372036854775807
487b = mul k 4
488if eq get st add b 1 get fl i
489if same get st b get fo i get fl i
490ret k
491end
492end
493end
494ret n
495end
496fn strow to void
497if ne get nv 9 3
498die e_state 1
499end
500fname 1 false
501n = get nv 23
502if ne stfind 1 n
503die e_state 1
504end
505z = fnum 2
506if or eq z 0 gt z 4294967296
507die e_state 1
508end
509r = enlarge st mul add n 1 4
510if lt r 0
511die e_mem 3
512end
513b = mul n 4
514put st b get fo 1
515put st add b 1 get fl 1
516put st add b 2 z
517put st add b 3 get nv 36
518put nv 36 add get nv 36 and add z 63 not ulong 63
519put nv 23 add n 1
520end
521fn needrow to void
522if ne get nv 9 2
523die e_row 1
524end
525fname 1 true
526n = get nv 26
527r = enlarge nd mul add n 1 3
528if lt r 0
529die e_mem 3
530end
531b = mul n 3
532put nd b get fo 1
533put nd add b 1 get fl 1
534put nd add b 2 0
535put nv 26 add n 1
536end
537fn rprow to void
538if or ne get nv 9 2 ne get nv 27 0
539die e_row 1
540end
541n = get fl 1
542if eq n 0
543die e_row 1
544end
545o = get fo 1
546for j ulong n bound 9223372036854775807
547if eq get ib add o j 0
548die e_row 1
549end
550end
551put nv 27 add o 1
552put nv 28 n
553end
554fn lockrow to void
555if or ne get nv 9 4 ne get nv 29 0
556die e_lock 1
557end
558k = stfind 1
559if eq k get nv 23
560die e_state 1
561end
562fname 2 false
563fname 3 false
564put nv 29 add k 1
565put nv 30 get fo 2
566put nv 31 get fl 2
567put nv 32 get fo 3
568put nv 33 get fl 3
569end
570note the import row named ib[a, a+n), or the row count when none
571fn mpfind a ulong n ulong to ulong
572m = get nv 25
573for k ulong m bound 9223372036854775807
574b = mul k 20
575if eq get mp add b 1 n
576if same get mp b a n
577ret k
578end
579end
580end
581ret m
582end
583fn improw to void
584nf = get nv 9
585if or lt nf 2 gt nf 10
586die e_improw 1
587end
588fname 1 false
589m = get nv 25
590if ne mpfind get fo 1 get fl 1 m
591die e_name 1
592end
593r = enlarge mp mul add m 1 20
594if lt r 0
595die e_mem 3
596end
597b = mul m 20
598put mp b get fo 1
599put mp add b 1 get fl 1
600nt = sub nf 2
601put mp add b 2 nt
602put mp add b 19 0
603for j ulong nt bound 8
604mtok add j 2 add b j
605end
606put nv 25 add m 1
607end
608note field i, a token sK or sK+N, into mp at slot word w + 3 and offset word w + 11
609fn mtok i ulong w ulong to void
610o = get fo i
611n = get fl i
612if or lt n 2 ne get ib o 115
613die e_improw 1
614end
615p = ulong 1
616while lt p n bound 9223372036854775807
617if eq get ib add o p 43
618break
619end
620p = add p 1
621end
622put mp add w 3 dnum add o 1 sub p 1
623v = ulong 0
624if lt p n
625v = dnum add add o p 1 sub sub n p 1
626end
627put mp add w 11 v
628end
629note one table row after soname
630fn rowx to void
631if ft 0 k_exp
632exrow
633ret
634end
635if ft 0 k_call
636callrow
637ret
638end
639if ft 0 k_bufeq
640eqrow 1
641ret
642end
643if ft 0 k_bufset
644eqrow 2
645ret
646end
647if ft 0 k_reteq
648eqrow 3
649ret
650end
651if ft 0 k_need
652needrow
653ret
654end
655if ft 0 k_rpath
656rprow
657ret
658end
659if ft 0 k_state
660strow
661ret
662end
663if ft 0 k_lock
664lockrow
665ret
666end
667if ft 0 k_imp
668improw
669ret
670end
671die e_row 1
672end
673fn callrow to void
674nf = get nv 9
675if or lt nf 3 gt nf 13
676die e_row 1
677end
678fname 1 false
679b = clnew
680put cl b get fo 1
681put cl add b 1 get fl 1
682e = ulong 4294967296
683if not ft 2 k_any
684e = fnum 2
685end
686if and gt e 4294967295 not ft 2 k_any
687die e_num 1
688end
689put cl add b 2 e
690na = sub nf 3
691put cl add b 3 na
692for j ulong na bound 10
693f = add j 3
694if ft f k_buf
695put cl add add b 14 j 1
696else
697if ft f k_bufw
698put cl add add b 14 j 2
699else
700if fpre f k_bufp
701put cl add add b 14 j 1
702u = dnum add get fo f 4 sub get fl f 4
703bufref u
704put cl add add b 4 j u
705else
706put cl add add b 4 j fnum f
707end
708end
709end
710end
711end
712note a bufeq (kind 1), bufset (2) or reteq (3) row after a call row, whose name it takes (its GOT slot is never
713note called)
714fn eqrow kind ulong to void
715nc = get nv 4
716if or ne get nv 9 3 eq nc 0
717die e_row 1
718end
719o = fnum 1
720if ne kind 3
721bufref o
722end
723v = fnum 2
724a = mul sub nc 1 CLW
725b = clnew
726put cl b get cl a
727put cl add b 1 get cl add a 1
728put cl add b 4 o
729put cl add b 5 v
730put cl add b 24 kind
731end
732note the buffer reaches 4096 bytes past the largest offset a row names (nv 39 holds that offset + 1)
733fn bufref o ulong to void
734if gt o sub SMAX 1
735die e_num 1
736end
737if ge o get nv 39
738put nv 39 add o 1
739end
740end
741note a new zeroed call row; its first word
742fn clnew to ulong
743nc = get nv 4
744r = enlarge cl mul add nc 1 CLW
745if lt r 0
746die e_mem 3
747end
748b = mul nc CLW
749for i ulong CLW
750put cl add b i 0
751end
752put nv 4 add nc 1
753ret b
754end
755fn parse to void
756p = ulong 0
757e = get nv 0
758ns = ulong 0
759while lt p e bound 9223372036854775807
760p = row p e
761skip = and eq get nv 9 1 eq get fl 0 0
762if gt get fl 0 0
763if eq get ib get fo 0 35
764skip = true
765end
766end
767if not skip
768if ft 0 k_son
769if ne get nv 9 2
770die e_row 1
771end
772fname 1 true
773put nv 5 get fo 1
774put nv 6 get fl 1
775ns = add ns 1
776else
777rowx
778end
779end
780end
781if ne ns 1
782die e_son 1
783end
784if eq get nv 3 0
785die e_noexp 1
786end
787for k ulong get nv 4 bound 9223372036854775807
788b = mul k CLW
789if eq exfind get cl b get cl add b 1 get nv 3
790die e_ncall 1
791end
792end
793if ne get nv 29 0
794for k ulong get nv 3 bound 9223372036854775807
795if eq get ex add mul k EXW 8 0
796die e_lock 1
797end
798end
799end
800end
801note ---- dynstr
802fn dsgrow n ulong to void
803if gt n len ds
804r = enlarge ds add n 4096
805if lt r 0
806die e_mem 3
807end
808end
809end
810note append ib[a, a+n) and a zero; its offset
811fn dsib a ulong n ulong to ulong
812s = get nv 2
813dsgrow add add s n 1
814for j ulong n bound 9223372036854775807
815put ds add s j get ib add a j
816end
817put ds add s n 0
818put nv 2 add add s n 1
819ret s
820end
821fn dstx t view uchar * to ulong
822s = get nv 2
823dsgrow add add s len t 1
824for j ulong len t bound 4096
825put ds add s j get t j
826end
827put ds add s len t 0
828put nv 2 add add s len t 1
829ret s
830end
831fn dsinit to void
832dsgrow 1
833put ds 0 0
834put nv 2 1
835end
836note SysV ELF hash of the dynstr string at o
837fn ehash o ulong to ulong
838h = ulong 0
839p = o
840while ne get ds p 0 bound 9223372036854775807
841h = and add shl h 4 ulong get ds p 4294967295
842g = and h 4026531840
843if ne g 0
844h = xor h shr g 24
845end
846h = and h not g
847p = add p 1
848end
849ret h
850end
851note the dynstr offset of symbol k + 1 (library: ex 4, then the imports' im 2; probe: cd)
852fn symname k ulong lib bool to ulong
853if lib
854nx = get nv 3
855if lt k nx
856ret get ex add mul k EXW 4
857end
858ret get im add mul sub k nx 8 2
859end
860ret get cd k
861end
862note .hash at off for n symbols after the null one: n buckets, n + 1 chain entries
863fn nbk n ulong to ulong
864if eq n 0
865ret 1
866end
867ret n
868end
869fn wrhash off ulong n ulong lib bool to void
870nb = nbk n
871w32 off nb
872w32 add off 4 add n 1
873cb = add off 8
874ch = add cb mul nb 4
875for k ulong n bound 9223372036854775807
876s = add k 1
877bo = add cb mul rem ehash symname k lib nb 4
878w32 add ch mul s 4 r32 bo
879w32 bo s
880end
881end
882note copy ds to eb at off
883fn wrstr off ulong to void
884for j ulong get nv 2 bound 9223372036854775807
885put eb add off j get ds j
886end
887end
888fn wrbytes off ulong t view uchar * to void
889for j ulong len t bound 4096
890c = get t j
891if eq c 32
892c = 0
893end
894put eb add off j c
895end
896end
897note ELF64 header: type, entry, program headers, section header offset, sections
898fn wrehdr ty ulong entry ulong nph ulong shoff ulong nsh ulong to void
899w32 0 1179403647
900w8 4 2
901w8 5 1
902w8 6 1
903w16 16 ty
904w16 18 183
905w32 20 1
906w64 24 entry
907w64 32 64
908w64 40 shoff
909w16 52 64
910w16 54 56
911w16 56 nph
912if ne nsh 0
913w16 58 64
914w16 60 nsh
915w16 62 sub nsh 1
916end
917end
918note program header i; a PT_LOAD is aligned to 64 KiB, anything else to 8
919fn wrph i ulong ty ulong fg ulong off ulong va ulong sz ulong to void
920o = add 64 mul i 56
921w32 o ty
922w32 add o 4 fg
923w64 add o 8 off
924w64 add o 16 va
925w64 add o 24 va
926w64 add o 32 sz
927w64 add o 40 sz
928if eq ty 1
929w64 add o 48 65536
930else
931w64 add o 48 8
932end
933end
934note section header i (address = offset + nv 16 when allocated)
935fn wrsh i ulong nm ulong ty ulong fg ulong off ulong sz ulong to void
936o = add get nv 15 mul i 64
937w32 o nm
938w32 add o 4 ty
939w64 add o 8 fg
940if ne and fg 2 0
941w64 add o 16 add off get nv 16
942end
943w64 add o 24 off
944w64 add o 32 sz
945end
946fn wrshl i ulong lk ulong inf ulong al ulong es ulong to void
947o = add get nv 15 mul i 64
948w32 add o 40 lk
949w32 add o 44 inf
950w64 add o 48 al
951w64 add o 56 es
952end
953fn dyn o ulong tag ulong v ulong to ulong
954w64 o tag
955w64 add o 8 v
956ret add o 16
957end
958note a B or BL (op) at offset p to offset t, both in the same address space
959fn wrb p ulong t ulong op ulong to void
960d = sub t p
961if ne and d 3 0
962die e_obj 1
963end
964if and ge d 134217728 lt d sub ulong 0 134217728
965die e_reach 1
966end
967w32 p or op and shr d 2 67108863
968end
969note ADR rd at p to t
970fn wradr p ulong t ulong rd ulong to void
971d = sub t p
972if and ge d 1048576 lt d sub ulong 0 1048576
973die e_reach 1
974end
975w32 p or or 268435456 rd or shl and d 3 29 shl and shr d 2 524287 5
976end
977note LDR X16 (literal) at p from t
978fn wrldr p ulong t ulong to void
979d = sub t p
980if and ge d 1048576 lt d sub ulong 0 1048576
981die e_reach 1
982end
983w32 p or 1476395024 shl and shr d 2 524287 5
984end
985note MOVZ (k 0) or MOVK (k 1..3, shift 16k) of the 16 bits v into rd
986fn wrmov p ulong k ulong v ulong rd ulong to void
987op = ulong 3531603968
988if ne k 0
989op = or 4068474880 shl k 21
990end
991w32 p or or op rd shl and v 65535 5
992end
993note ---- the object
994fn objparse to void
995b = get nv 0
996e = get nv 1
997if lt sub e b 176
998die e_obj 1
999end
1000for j ulong 8
1001if ne get ib add b j get k_obj j
1002die e_obj 1
1003end
1004end
1005if or ne oq 8 1 or ne oq 16 1 or gt oq 24 1 ne oq 32 176
1006die e_obj 1
1007end
1008for j ulong 13
1009if ne oq add 72 mul j 8 0
1010die e_obj 1
1011end
1012end
1013c = oq 40
1014if or gt c sub e b lt c 176
1015die e_obj 1
1016end
1017if ne and c 3 0
1018die e_obj 1
1019end
1020put nv 8 c
1021nf = oq 48
1022nc = oq 56
1023note a function record takes at least 48 bytes and a call record 32: counts past that are malformed, and
1024note checking them first keeps every size product below 2^64
1025if or gt nf div sub e b 48 gt nc div sub e b 32
1026die e_obj 1
1027end
1028put nv 7 nf
1029put nv 17 nc
1030r = enlarge fr mul add nf 1 8
1031if lt r 0
1032die e_mem 3
1033end
1034r = enlarge ck mul add nc 1 4
1035if lt r 0
1036die e_mem 3
1037end
1038o = c
1039for i ulong nf bound 9223372036854775807
1040o = frec i o
1041end
1042if ne get nv 22 oq 64
1043die e_obj 1
1044end
1045for k ulong nc bound 9223372036854775807
1046o = crec k o
1047end
1048if ne o sub e b
1049die e_obj 1
1050end
1051slots
1052uniq
1053blscan
1054needs
1055end
1056note the B slot of the k-th export (record order) at 176 + 4k branches to that export's own address
1057fn slots to void
1058k = ulong 0
1059for i ulong get nv 7 bound 9223372036854775807
1060if eq get fr mul i 8 1
1061w = add 176 mul k 4
1062bslot w get fr add mul i 8 1
1063k = add k 1
1064end
1065end
1066end
1067note the word at object offset w is a B to object offset a
1068fn bslot w ulong a ulong to void
1069if ne shr ib32 add get nv 0 w 26 5
1070die e_slotb 1
1071end
1072if ne brt w a
1073die e_slotb 1
1074end
1075if lt a add 176 mul oq 64 4
1076die e_addr 1
1077end
1078end
1079note the target of the B or BL word at object offset w
1080fn brt w ulong to ulong
1081m = and ib32 add get nv 0 w 67108863
1082t = add w mul m 4
1083if ne and m 33554432 0
1084t = sub t 268435456
1085end
1086ret t
1087end
1088note no two non-imported function names are equal
1089fn uniq to void
1090nf = get nv 7
1091for i ulong nf bound 9223372036854775807
1092for j ulong i bound 9223372036854775807
1093if dupn i j
1094die e_obj 1
1095end
1096end
1097end
1098end
1099fn dupn i ulong j ulong to bool
1100a = mul i 8
1101b = mul j 8
1102if ne get fr add a 6 get fr add b 6
1103ret false
1104end
1105ret same get fr add a 5 get fr add b 5 get fr add a 6
1106end
1107note function i's checks (spec/omegese-modules.md): flags 0, 1 or 2; a private or exported function's address
1108note aligned, inside the code and above the previous one; a legal result type; zero name padding; legal
1109note parameter records. nv 19 holds the previous address, nv 22 counts exports.
1110fn fchk i ulong o ulong to void
1111f = mul i 8
1112fg = get fr f
1113if gt fg 2
1114die e_obj 1
1115end
1116if eq fg 1
1117put nv 22 add get nv 22 1
1118end
1119a = get fr add f 1
1120if lt fg 2
1121if or lt a 176 or ge a get nv 8 or ne and a 3 0 le a get nv 19
1122die e_addr 1
1123end
1124put nv 19 a
1125end
1126rt = get fr add f 2
1127if or eq rt 0 gt rt 13
1128die e_obj 1
1129end
1130s = add add get nv 0 o 48
1131padz add s get fr add f 6 get fr add f 4
1132precs f
1133end
1134note function record f's parameter records
1135fn precs f ulong to void
1136for p ulong get fr add f 3 bound 6
1137prec add get fr add f 4 mul p 40
1138end
1139end
1140note the bytes ib[s, e) are zero (a name's padding)
1141fn padz s ulong e ulong to void
1142for j ulong sub e s bound 8
1143if ne get ib add s j 0
1144die e_obj 1
1145end
1146end
1147end
1148note parameter record at ib offset p: type 1..13 but not 12 (void), kind 0, 2 or 3, layout 0, fresh 0 or 1
1149fn prec p ulong to void
1150t = q p
1151k = q add p 8
1152if or or eq t 0 gt t 13 eq t 12
1153die e_obj 1
1154end
1155if and ne k 0 and ne k 2 ne k 3
1156die e_obj 1
1157end
1158if or ne q add p 24 0 gt q add p 32 1
1159die e_obj 1
1160end
1161if and eq k 0 ne q add p 16 0
1162die e_obj 1
1163end
1164end
1165note the end of function s's code: the lowest higher address of a private or exported function, else the
1166note code end
1167fn fend s ulong to ulong
1168a = get fr add mul s 8 1
1169e = get nv 8
1170for i ulong get nv 7 bound 9223372036854775807
1171x = get fr add mul i 8 1
1172ok = lt get fr mul i 8 2
1173if and ok and gt x a lt x e
1174e = x
1175end
1176end
1177ret e
1178end
1179note every BL word of the code [176, C) has exactly one call record (spec: "Every native BL word must have a
1180note record"), so the stack needs settle over every call
1181fn blscan to void
1182b = get nv 0
1183n = ulong 0
1184for i ulong div sub get nv 8 176 4 bound 9223372036854775807
1185if eq shr ib32 add add b 176 mul i 4 26 37
1186n = add n 1
1187end
1188end
1189if ne n get nv 17
1190die e_bl 1
1191end
1192for k ulong get nv 17 bound 9223372036854775807
1193w = get ck add mul k 4 3
1194for j ulong k bound 9223372036854775807
1195if eq get ck add mul j 4 3 w
1196die e_bl 1
1197end
1198end
1199end
1200end
1201note function record i at object offset o into fr (0 flags, 1 address, 2 result type, 3 parameters, 4 their
1202note records in ib, 5 name in ib, 6 name length, 7 stack need, first its own frame); the next record's offset
1203fn frec i ulong o ulong to ulong
1204b = get nv 0
1205if gt add o 48 sub get nv 1 b
1206die e_obj 1
1207end
1208f = mul i 8
1209put fr f oq o
1210put fr add f 1 oq add o 8
1211put fr add f 2 oq add o 16
1212np = oq add o 24
1213if gt np 6
1214die e_slot 1
1215end
1216put fr add f 3 np
1217put fr add f 7 oq add o 32
1218if gt get fr add f 7 SMAX
1219die e_stk 1
1220end
1221nl = oq add o 40
1222if gt nl 63
1223die e_obj 1
1224end
1225put fr add f 5 add b add o 48
1226put fr add f 6 nl
1227n = al8 add add o 48 nl
1228put fr add f 4 add b n
1229n = add n mul np 40
1230if gt n sub get nv 1 b
1231die e_obj 1
1232end
1233impchk f
1234fchk i o
1235ret n
1236end
1237note function record word f: an import (flags 2) has address 0 and needs no stack
1238fn impchk f ulong to void
1239if eq get fr f 2
1240if or ne get fr add f 1 0 ne get fr add f 7 0
1241die e_imp 1
1242end
1243end
1244end
1245note call record k at object offset o into ck (source, destination, stack cost, instruction offset): the
1246note instruction is a BL inside the source function; the next record's offset
1247fn crec k ulong o ulong to ulong
1248if gt add o 32 sub get nv 1 get nv 0
1249die e_obj 1
1250end
1251nf = get nv 7
1252s = oq add o 8
1253d = oq add o 16
1254if or ge s nf ge d nf
1255die e_obj 1
1256end
1257w = oq o
1258csite s w
1259if eq get fr mul d 8 2
1260if ne ib32 add get nv 0 w 2483027968
1261die e_cdst 1
1262end
1263else
1264if ne brt w get fr add mul d 8 1
1265die e_cdst 1
1266end
1267end
1268if eq oq add o 24 0
1269die e_call 1
1270end
1271x = mul k 4
1272put ck x s
1273put ck add x 1 d
1274put ck add x 2 oq add o 24
1275put ck add x 3 w
1276if gt get ck add x 2 SMAX
1277die e_stk 1
1278end
1279ret add o 32
1280end
1281note the call instruction at object offset w: aligned, inside function s and a BL
1282fn csite s ulong w ulong to void
1283if or lt w get fr add mul s 8 1 ne and w 3 0
1284die e_call 1
1285end
1286if ge w fend s
1287die e_call 1
1288end
1289if ne shr ib32 add get nv 0 w 26 37
1290die e_call 1
1291end
1292end
1293note the stack each function needs, settled as the linker does: need(f) = max(local(f), cost(call) +
1294note need(callee) for each call of f). Each pass lengthens every settled path by one call, so nf + 1 passes
1295note settle an acyclic graph; a change in the last pass is a cycle. Values stay at or below SMAX (refused past it).
1296fn needs to void
1297nf = get nv 7
1298for p ulong add nf 1 bound 9223372036854775807
1299put nv 18 0
1300for k ulong get nv 17 bound 9223372036854775807
1301nstep k
1302end
1303if eq get nv 18 0
1304ret
1305end
1306end
1307die e_stk 1
1308end
1309fn nstep k ulong to void
1310x = mul k 4
1311d = mul get ck add x 1 8
1312v = add get ck add x 2 get fr add d 7
1313s = mul get ck x 8
1314if gt v SMAX
1315die e_stk 1
1316end
1317if gt v get fr add s 7
1318put fr add s 7 v
1319put nv 18 1
1320end
1321end
1322note the u32 of ib at o
1323fn ib32 o ulong to ulong
1324v = ulong 0
1325for j ulong 4
1326v = or v shl ulong get ib add o j mul j 8
1327end
1328ret v
1329end
1330note no use of x7 as a base register except the trap-entry load LDR Xt, [X7, #16]: every instruction of the
1331note load/store group (literal loads aside, which have no base register) whose Rn is x7 is refused; another
1332note LDR (immediate, 64-bit) offset is e_rt, any other form e_rtw. The wrapper's saved registers sit in the frame
1333note x7 points at.
1334fn rtscan to void
1335b = add get nv 0 176
1336n = div sub get nv 8 176 4
1337for i ulong n bound 9223372036854775807
1338w = ib32 add b mul i 4
1339if eq and w 4290773984 4181721312
1340if ne and shr w 10 4095 2
1341die e_rt 1
1342end
1343else
1344if lsx7 w
1345die e_rtw 1
1346end
1347end
1348end
1349end
1350note w is in the load/store group (op0 x1x0), is not a literal load (011x 0 0), and has base register x7
1351fn lsx7 w ulong to bool
1352if ne and w 167772160 134217728
1353ret false
1354end
1355if eq and w 989855744 402653184
1356ret false
1357end
1358ret eq and shr w 5 31 7
1359end
1360fn exbind k ulong to void
1361x = mul k EXW
1362a = get ex x
1363n = get ex add x 1
1364for i ulong get nv 7 bound 9223372036854775807
1365if fnis i a n
1366put ex add x 3 i
1367if ne get ex add x 8 0
1368frchk k
1369end
1370ret
1371end
1372end
1373die e_miss 1
1374end
1375note the bytes of one element of type ty (a float or integer type: 1..11 but not 12)
1376fn esz ty ulong to ulong
1377if or le ty 2 eq ty 6
1378ret 1
1379end
1380if or eq ty 3 eq ty 7
1381ret 2
1382end
1383if or or eq ty 4 eq ty 8 eq ty 10
1384ret 4
1385end
1386ret 8
1387end
1388note frame export k: a non-float result; 1 + its states parameters; the first a sized view or edit of ulong
1389note (count 1..9 + states), each further one sized, within its state; ex 15 its frame bytes, which ADD X16, SP,
1390note #frame + 192 must reach with its 12-bit immediate
1391fn frchk k ulong to void
1392x = mul k EXW
1393f = mul get ex add x 3 8
1394if isflt get fr add f 2
1395die e_flt 1
1396end
1397nb = get ex add x 9
1398if ne get fr add f 3 add nb 1
1399die e_frame 1
1400end
1401pr = get fr add f 4
1402if or or ne q pr 5 eq q add pr 8 0 eq q add pr 16 0
1403die e_frame 1
1404end
1405if gt q add pr 16 add 9 get nv 23
1406die e_frame 1
1407end
1408for j ulong nb bound 5
1409frpar x j pr
1410end
1411fb = al16 mul 8 add 9 get nv 23
1412if gt add fb 192 4080
1413die e_reach 1
1414end
1415put ex add x 15 fb
1416end
1417note frame export at ex word x: its parameter j + 1 (records from pr) is sized and fits its state
1418fn frpar x ulong j ulong pr ulong to void
1419o = add pr mul add j 1 40
1420c = q add o 16
1421ty = q o
1422if or or eq q add o 8 0 eq c 0 gt ty 11
1423die e_frame 1
1424end
1425s = get ex add add x 10 j
1426z = get st add mul s 4 2
1427e = esz ty
1428if gt c div z e
1429die e_frame 1
1430end
1431end
1432note ---- imports
1433note append an import symbol named ib[a, a+n) for object function fi; its index
1434fn imadd a ulong n ulong fi ulong to ulong
1435i = get nv 24
1436r = enlarge im mul add i 1 8
1437if lt r 0
1438die e_mem 3
1439end
1440x = mul i 8
1441put im x a
1442put im add x 1 n
1443put im add x 2 0
1444put im add x 3 fi
1445m = mpfind a n
1446put im add x 4 m
1447if lt m get nv 25
1448put mp add mul m 20 19 1
1449end
1450put im add x 5 0
1451put im add x 6 0
1452put im add x 7 0
1453put nv 24 add i 1
1454ret i
1455end
1456note the import symbol named ib[a, a+n), added for the lock when the object does not import it
1457fn imsym a ulong n ulong to ulong
1458for i ulong get nv 24 bound 9223372036854775807
1459x = mul i 8
1460if eq get im add x 1 n
1461if same get im x a n
1462ret i
1463end
1464end
1465end
1466ret imadd a n get nv 7
1467end
1468note the import symbol of object function d
1469fn imof d ulong to ulong
1470for i ulong get nv 24 bound 9223372036854775807
1471if eq get im add mul i 8 3 d
1472ret i
1473end
1474end
1475die e_obj 1
1476ret 0
1477end
1478note the object's imports in function order, then the lock's; every import row used; each import's slots
1479fn imps to void
1480nf = get nv 7
1481for i ulong nf bound 9223372036854775807
1482f = mul i 8
1483if eq get fr f 2
1484r = imadd get fr add f 5 get fr add f 6 i
1485imchk r
1486end
1487end
1488if ne get nv 29 0
1489put nv 34 imsym get nv 30 get nv 31
1490put nv 35 imsym get nv 32 get nv 33
1491end
1492for k ulong get nv 25 bound 9223372036854775807
1493if eq get mp add mul k 20 19 0
1494die e_improw 1
1495end
1496end
1497end
1498note import i: no float result or scalar, at most 6 slots, its row's tokens within them
1499fn imchk i ulong to void
1500x = mul i 8
1501f = mul get im add x 3 8
1502if isflt get fr add f 2
1503die e_flt 1
1504end
1505put nv 20 0
1506pr = get fr add f 4
1507for j ulong get fr add f 3 bound 6
1508w = pext add pr mul j 40
1509end
1510ns = get nv 20
1511if gt ns 6
1512die e_slot 1
1513end
1514put im add x 7 ns
1515rowchk get im add x 4 ns
1516end
1517note import row r (none when r is the row count): every token's slot below ns
1518fn rowchk r ulong ns ulong to void
1519if ge r get nv 25
1520ret
1521end
1522y = mul r 20
1523for c ulong get mp add y 2 bound 8
1524t = get mp add add y 3 c
1525if ge t ns
1526die e_improw 1
1527end
1528end
1529end
1530note import i's veneer bytes (0 for one only the lock calls): STP, the argument moves of its row, ADRP, LDR,
1531note BLR, the result's extension, LDP, RET
1532fn vsz i ulong to ulong
1533x = mul i 8
1534fi = get im add x 3
1535if eq fi get nv 7
1536ret 0
1537end
1538n = add 6 rowwds get im add x 4 get im add x 7
1539if ne extw get fr add mul fi 8 2 0 0
1540n = add n 1
1541end
1542ret mul n 4
1543end
1544note the argument-move words of import row r for an import of ns slots (0 when r is the row count)
1545fn rowwds r ulong ns ulong to ulong
1546if ge r get nv 25
1547ret 0
1548end
1549y = mul r 20
1550n = ns
1551for c ulong get mp add y 2 bound 8
1552t = get mp add add y 11 c
1553if eq t 0
1554n = add n 1
1555else
1556n = add n 5
1557end
1558end
1559ret n
1560end
1561note the GOT slot address of import i, and the address of state s
1562fn gotva i ulong to ulong
1563ret add add get ly 8 get nv 37 mul i 8
1564end
1565fn stva s ulong to ulong
1566ret add add get ly 8 get ly 15 get st add mul s 4 3
1567end
1568note ADRP rd at p to t's 4 KiB page; ADD rd, rs, #(t mod 4096); LDR rd, [rs, #(t mod 4096)] (t 8-aligned)
1569fn wradrp p ulong t ulong rd ulong to void
1570g = sub shr t 12 shr p 12
1571if and ge g 1048576 lt g sub ulong 0 1048576
1572die e_reach 1
1573end
1574w32 p or or 2415919104 rd or shl and g 3 29 shl and shr g 2 524287 5
1575end
1576fn wraddlo p ulong t ulong rs ulong rd ulong to void
1577w32 p or or 2432696320 rd or shl and t 4095 10 shl rs 5
1578end
1579fn wrldrlo p ulong t ulong rs ulong rd ulong to void
1580w32 p or or 4181721088 rd or shl div and t 4095 8 10 shl rs 5
1581end
1582note import i's veneer at im 5 (the header's imports)
1583fn wrven i ulong to void
1584x = mul i 8
1585p = get im add x 5
1586f = mul get im add x 3 8
1587w32 p 2847898599
1588p = add p 4
1589p = wrvmv p get im add x 4 get im add x 7
1590g = gotva i
1591wradrp p g 16
1592wrldrlo add p 4 g 16 16
1593w32 add p 8 3594453504
1594p = add p 12
1595e = extw get fr add f 2 0
1596if ne e 0
1597w32 p e
1598p = add p 4
1599end
1600w32 p 2831252455
1601w32 add p 4 3596551104
1602end
1603note the argument moves at p of import row r for ns slots: MOV x9+k, xk; then per token c, MOV xc, x9+K or
1604note MOVZ/MOVK X16, N; ADD xc, x9+K, X16 (nothing when r is the row count); the next offset
1605fn wrvmv p ulong r ulong ns ulong to ulong
1606if ge r get nv 25
1607ret p
1608end
1609for k ulong ns bound 6
1610w32 p or 2852127712 or shl k 16 add 9 k
1611p = add p 4
1612end
1613y = mul r 20
1614for c ulong get mp add y 2 bound 8
1615p = wrvtok p y c
1616end
1617ret p
1618end
1619note token c of the import row at mp word y, at p; the next offset
1620fn wrvtok p ulong y ulong c ulong to ulong
1621a = add add y 3 c
1622sl = add 9 get mp a
1623off = get mp add a 8
1624if eq off 0
1625w32 p or 2852127712 or shl sl 16 c
1626ret add p 4
1627end
1628for h ulong 4
1629wrmov add p mul h 4 h shr off mul h 16 16
1630end
1631w32 add p 16 or 2332033024 or 1048576 or shl sl 5 c
1632ret add p 20
1633end
1634note object function i is exported under the name ib[a, a+n)
1635fn fnis i ulong a ulong n ulong to bool
1636f = mul i 8
1637if or ne get fr f 1 ne get fr add f 6 n
1638ret false
1639end
1640ret same get fr add f 5 a n
1641end
1642note the extension of a scalar of type ty in register s: UBFX/SBFX X, X, #0, #width (0 none)
1643fn extw ty ulong s ulong to ulong
1644d = or shl s 5 s
1645if le ty 2
1646ret or 3544194048 d
1647end
1648if eq ty 3
1649ret or 3544202240 d
1650end
1651if eq ty 4
1652ret or 3544218624 d
1653end
1654if eq ty 6
1655ret or 2470452224 d
1656end
1657if eq ty 7
1658ret or 2470460416 d
1659end
1660if eq ty 8
1661ret or 2470476800 d
1662end
1663ret 0
1664end
1665fn isflt ty ulong to bool
1666ret or eq ty 10 eq ty 11
1667end
1668note parameter record o: advances the slot count nv 20; the extension word (0 none). A float scalar is refused;
1669note a float view or edit is a pointer in an x register like any other.
1670fn pext o ulong to ulong
1671ty = q o
1672if and eq q add o 8 0 isflt ty
1673die e_flt 1
1674end
1675s = get nv 20
1676if eq q add o 8 0
1677put nv 20 add s 1
1678ret extw ty s
1679end
1680if eq q add o 16 0
1681put nv 20 add s 2
1682else
1683put nv 20 add s 1
1684end
1685ret 0
1686end
1687note the extensions of export k, written from eb offset wa when wr; their count
1688fn exts k ulong wr bool wa ulong to ulong
1689f = mul get ex add mul k EXW 3 8
1690if isflt get fr add f 2
1691die e_flt 1
1692end
1693put nv 20 0
1694put nv 21 0
1695pr = get fr add f 4
1696for j ulong get fr add f 3 bound 6
1697w = pext add pr mul j 40
1698if ne w 0
1699n = get nv 21
1700if wr
1701w32 add wa mul n 4 w
1702end
1703put nv 21 add n 1
1704end
1705end
1706if gt get nv 20 6
1707die e_slot 1
1708end
1709ret get nv 21
1710end
1711note STP or LDP (op) of registers r and r + 1 at [sp, #8 * ix]
1712fn pw op ulong r ulong ix ulong to ulong
1713a = or shl ix 15 shl add r 1 10
1714ret or or op a or 992 r
1715end
1716note the register save area at sp + 32: STP (LDP when back) of x19..x30 and d8..d15, 10 words from p
1717fn saves p ulong back bool to void
1718xo = ulong 2835349504
1719vo = ulong 1828716544
1720if back
1721xo = ulong 2839543808
1722vo = ulong 1832910848
1723end
1724for i ulong 6
1725v = pw xo add 19 mul i 2 add 4 mul i 2
1726w32 add p mul i 4 v
1727end
1728for i ulong 4
1729v = pw vo add 8 mul i 2 add 16 mul i 2
1730w32 add p mul add i 6 4 v
1731end
1732end
1733note ---- the library
1734note export k's wrapper: tx is the trap entry (the shared return follows it at tx + 8), cb the object's code
1735fn wrwrap k ulong tx ulong cb ulong to void
1736b = mul k EXW
1737if ne get ex add b 8 0
1738wrframe k tx cb
1739ret
1740end
1741p = get ex add b 5
1742w32 p 3506635775
1743saves add p 4 false
1744wradr add p 44 tx 16
1745w32 add p 48 4177529840
1746t = get ex add b 2
1747wrmov add p 52 0 t 16
1748wrmov add p 56 1 shr t 16 16
1749w32 add p 60 4177530864
1750n = exts k true wrprobe k add p 64
1751a = add wrprobe k add p 64 mul n 4
1752w32 a 2432697319
1753f = mul get ex add b 3 8
1754wrb add a 4 add cb sub get fr add f 1 176 2483027968
1755wrb add a 8 add tx 8 335544320
1756end
1757note frame export k's wrapper: the 16 words above (to the trap value's store), STR XZR, [SP, #8] (the lock
1758note flag) when locked, SUB SP, SP, #frame; STP of x0..x7 at [sp]; per state ADRP, ADD, STR X16 at
1759note [sp, #64 + 8j]; when locked, the LOCK call (ADRP X0, ADD X0, ADRP X16, LDR X16 from its GOT slot, BLR
1760note X16; CBZ W0, +16; LDR X0, [SP, #frame + 24]; ADD SP, SP, #frame; B shared return; MOVZ X16, #1; STR X16,
1761note [SP, #frame + 8]); the stack probe; ADD X0, SP, #0 (the frame); per bound state ADRP, ADD into x1..x5;
1762note ADD X7, SP, #frame; BL body; ADD SP, SP, #frame; B shared return
1763fn wrframe k ulong tx ulong cb ulong to void
1764b = mul k EXW
1765p = get ex add b 5
1766fb = get ex add b 15
1767w32 p 3506635775
1768saves add p 4 false
1769wradr add p 44 tx 16
1770w32 add p 48 4177529840
1771t = get ex add b 2
1772wrmov add p 52 0 t 16
1773wrmov add p 56 1 shr t 16 16
1774w32 add p 60 4177530864
1775p = wrfsav add p 64 fb
1776p = wrflk p fb tx
1777p = wrprobe k p
1778p = wrfbind p b
1779w32 p or 2432697319 shl fb 10
1780f = mul get ex add b 3 8
1781wrb add p 4 add cb sub get fr add f 1 176 2483027968
1782w32 add p 8 or 2432697343 shl fb 10
1783wrb add p 12 add tx 8 335544320
1784end
1785note at p: the lock flag's clear when locked, SUB SP, the four STPs and the state stores; the next offset
1786fn wrfsav p ulong fb ulong to ulong
1787if ne get nv 29 0
1788w32 p 4177528831
1789p = add p 4
1790end
1791w32 p or 3506439167 shl fb 10
1792for i ulong 4
1793v = pw 2835349504 mul i 2 mul i 2
1794w32 add add p 4 mul i 4 v
1795end
1796p = add p 20
1797w32 p or 2432697328 shl add fb 192 10
1798w32 add p 4 or 4177527776 or shl add 8 get nv 23 10 16
1799p = add p 8
1800for j ulong get nv 23 bound 9223372036854775807
1801a = stva j
1802wradrp p a 16
1803wraddlo add p 4 a 16 16
1804w32 add p 8 or 4177527776 or shl add 8 j 10 16
1805p = add p 12
1806end
1807ret p
1808end
1809note at p, when locked: the LOCK call and its refusal path; the next offset
1810fn wrflk p ulong fb ulong tx ulong to ulong
1811if eq get nv 29 0
1812ret p
1813end
1814a = stva sub get nv 29 1
1815wradrp p a 0
1816wraddlo add p 4 a 0 0
1817g = gotva get nv 34
1818wradrp add p 8 g 16
1819wrldrlo add p 12 g 16 16
1820w32 add p 16 3594453504
1821w32 add p 20 872415360
1822w32 add p 24 or 4181722080 shl div add fb 24 8 10
1823w32 add p 28 or 2432697343 shl fb 10
1824wrb add p 32 add tx 8 335544320
1825wrmov add p 36 0 1 16
1826w32 add p 40 or 4177527776 or shl div add fb 8 8 10 16
1827ret add p 44
1828end
1829note at p: ADD X0, SP, #0, then the bound states of the export at ex word b into x1..x5; the next offset
1830fn wrfbind p ulong b ulong to ulong
1831w32 p 2432697312
1832p = add p 4
1833for j ulong get ex add b 9 bound 5
1834a = stva get ex add add b 10 j
1835r = add j 1
1836wradrp p a r
1837wraddlo add p 4 a r r
1838p = add p 8
1839end
1840ret p
1841end
1842note frame export k's wrapper words (wrframe)
1843fn fwords k ulong to ulong
1844b = mul k EXW
1845n = add add 28 mul 3 get nv 23 add pwds k mul 2 get ex add b 9
1846if ne get nv 29 0
1847n = add n 12
1848end
1849ret n
1850end
1851note the stack probe at p when export k's body needs stack: touch one word in each 4 KiB page below the
1852note wrapper's lowest store [sp, #16], down past the body's need, top first, so no two touches are more than
1853note 4096 bytes apart and a stack too small for the body faults on a 4 KiB guard page before the body runs:
1854note ADD X16, SP, #16; MOVZ/MOVK X17, pages; loop: SUB X16, X16, #1, LSL #12; LDR XZR, [X16];
1855note SUBS X17, X17, #1; B.NE loop. The next offset.
1856fn wrprobe k ulong p ulong to ulong
1857g = pgs k
1858if eq g 0
1859ret p
1860end
1861w32 p 2432713712
1862wrmov add p 4 0 g 17
1863wrmov add p 8 1 shr g 16 17
1864wrmov add p 12 2 shr g 32 17
1865w32 add p 16 3510634000
1866w32 add p 20 4181721631
1867w32 add p 24 4043310641
1868w32 add p 28 1426063265
1869ret add p 32
1870end
1871note the 4 KiB pages export k's probe touches: 0 when its function needs no stack, else the settled need plus
1872note the 16 bytes from sp to the wrapper's lowest store, rounded up, so the last touch is at or below sp - need
1873fn pgs k ulong to ulong
1874f = mul get ex add mul k EXW 3 8
1875n = get fr add f 7
1876if eq n 0
1877ret 0
1878end
1879ret div add n 4111 4096
1880end
1881note the probe's words for export k: 8, or 0 when the body needs no stack
1882fn pwds k ulong to ulong
1883if eq pgs k 0
1884ret 0
1885end
1886ret 8
1887end
1888note the library layout in ly: 0 .hash, 1 its bytes, 2 .dynsym, 3 .dynstr, 4 .text (the trap entry), 5 the
1889note object's code, 6 the end of the RX segment, 7 the RW segment's file offset (.dynamic, then .got), 8 its
1890note address, 9 .shstrtab, 10 the section headers, 11 symbols, 12 .rela.dyn, 13 the veneers, 14 .got's file
1891note offset, 15 the state area's offset in the RW segment (64-aligned, after .got), 16 the RW segment's size
1892note in memory
1893fn liblay to void
1894nx = get nv 3
1895imps
1896dsinit
1897put nv 13 dsib get nv 5 get nv 6
1898for j ulong get nv 26 bound 9223372036854775807
1899b = mul j 3
1900put nd add b 2 dsib get nd b get nd add b 1
1901end
1902if ne get nv 27 0
1903put nv 14 dsib sub get nv 27 1 get nv 28
1904end
1905for k ulong nx bound 9223372036854775807
1906b = mul k EXW
1907put ex add b 4 dsib get ex b get ex add b 1
1908exbind k
1909if ne get ex add b 8 0
1910put ex add b 6 mul fwords k 4
1911else
1912put ex add b 7 exts k false 0
1913w = add 19 pwds k
1914put ex add b 6 mul add w get ex add b 7 4
1915end
1916end
1917ni = get nv 24
1918for i ulong ni bound 9223372036854775807
1919x = mul i 8
1920put im add x 2 dsib get im x get im add x 1
1921end
1922rtscan
1923ns = add add nx ni 1
1924put ly 11 ns
1925put ly 0 288
1926put ly 1 mul add nbk add nx ni add ns 2 4
1927put ly 2 al8 add get ly 0 get ly 1
1928put ly 3 add get ly 2 mul ns 24
1929put ly 12 al8 add get ly 3 get nv 2
1930put ly 4 al16 add get ly 12 mul ni 24
1931note .text: the trap entry and the shared return (14 words, 23 with the lock's UNLOCK call), the wrappers, the
1932note object's code, then the veneers
1933put nv 38 56
1934if ne get nv 29 0
1935put nv 38 92
1936end
1937wo = add get ly 4 get nv 38
1938for k ulong nx bound 9223372036854775807
1939b = mul k EXW
1940put ex add b 5 wo
1941wo = add wo get ex add b 6
1942end
1943note the object's code keeps its 16-byte alignment (it starts at 176 in the object)
1944put ly 5 al16 wo
1945put ly 13 add get ly 5 sub get nv 8 176
1946vo = get ly 13
1947for i ulong ni bound 9223372036854775807
1948x = mul i 8
1949put im add x 5 vo
1950put im add x 6 vsz i
1951vo = add vo get im add x 6
1952end
1953put ly 6 vo
1954note the RW segment at a 64-byte boundary (its address is its offset + 64 KiB), so each state, at a multiple of
1955note 64 from it, is 64-byte aligned as the state row promises
1956put ly 7 and add get ly 6 63 not ulong 63
1957put ly 8 add get ly 7 65536
1958de = add get nv 26 11
1959if ne get nv 27 0
1960de = add de 1
1961end
1962put nv 37 mul de 16
1963put ly 14 add get ly 7 get nv 37
1964put ly 15 and add add get nv 37 mul ni 8 63 not ulong 63
1965put ly 16 add get ly 15 get nv 36
1966put ly 9 add get ly 14 mul ni 8
1967put ly 10 al8 add get ly 9 len t_shs
1968put nv 15 get ly 10
1969end
1970fn libsyms to void
1971nx = get nv 3
1972ni = get nv 24
1973wrhash get ly 0 add nx ni true
1974for k ulong nx bound 9223372036854775807
1975b = mul k EXW
1976o = add get ly 2 mul add k 1 24
1977w32 o get ex add b 4
1978w8 add o 4 18
1979w16 add o 6 4
1980w64 add o 8 get ex add b 5
1981w64 add o 16 get ex add b 6
1982end
1983note each import: an undefined global function symbol, and its R_AARCH64_GLOB_DAT (1025) at its GOT slot
1984for i ulong ni bound 9223372036854775807
1985s = add add nx i 1
1986o = add get ly 2 mul s 24
1987w32 o get im add mul i 8 2
1988w8 add o 4 18
1989r = add get ly 12 mul i 24
1990w64 r gotva i
1991w64 add r 8 or shl s 32 1025
1992end
1993wrstr get ly 3
1994end
1995fn libtext to void
1996tx = get ly 4
1997cb = get ly 5
1998note trap entry: MOV SP, X0; LDR X0, [SP, #24]; the shared return: when locked, LDR X16, [SP, #8] (the lock
1999note flag); CBZ X16, +32; STR X0, [SP]; ADRP X0, ADD X0 (the lock state); ADRP X16, LDR X16 (UNLOCK's GOT
2000note slot); BLR X16; LDR X0, [SP]; then restore, ADD SP, SP, #192; RET
2001w32 tx 2432696351
2002w32 add tx 4 4181725152
2003r = add tx 8
2004if ne get nv 29 0
2005m = sub get nv 29 1
2006w32 r 4181723120
2007w32 add r 4 3019899152
2008w32 add r 8 4177527776
2009wradrp add r 12 stva m 0
2010wraddlo add r 16 stva m 0 0
2011g = gotva get nv 35
2012wradrp add r 20 g 16
2013wrldrlo add r 24 g 16 16
2014w32 add r 28 3594453504
2015w32 add r 32 4181722080
2016r = add r 36
2017end
2018saves r true
2019w32 add r 40 2432893951
2020w32 add r 44 3596551104
2021for k ulong get nv 3 bound 9223372036854775807
2022wrwrap k tx cb
2023end
2024b = add get nv 0 176
2025for i ulong sub get nv 8 176 bound 9223372036854775807
2026put eb add cb i get ib add b i
2027end
2028libven cb
2029end
2030note each BL to an import goes to its veneer (code at cb); the veneers
2031fn libven cb ulong to void
2032for k ulong get nv 17 bound 9223372036854775807
2033x = mul k 4
2034d = get ck add x 1
2035if eq get fr mul d 8 2
2036v = get im add mul imof d 8 5
2037w = add cb sub get ck add x 3 176
2038wrb w v 2483027968
2039end
2040end
2041for i ulong get nv 24 bound 9223372036854775807
2042if ne get im add mul i 8 6 0
2043wrven i
2044end
2045end
2046end
2047fn libsecs to void
2048ni = get nv 24
2049o = get ly 7
2050for j ulong get nv 26 bound 9223372036854775807
2051o = dyn o 1 get nd add mul j 3 2
2052end
2053if ne get nv 27 0
2054o = dyn o 29 get nv 14
2055end
2056o = dyn o 4 get ly 0
2057o = dyn o 5 get ly 3
2058o = dyn o 6 get ly 2
2059o = dyn o 10 get nv 2
2060o = dyn o 11 24
2061o = dyn o 14 get nv 13
2062o = dyn o 7 get ly 12
2063o = dyn o 8 mul ni 24
2064o = dyn o 9 24
2065o = dyn o 0 0
2066wrbytes get ly 9 t_shs
2067put nv 16 0
2068wrsh 1 1 5 2 get ly 0 get ly 1
2069wrshl 1 2 0 8 4
2070wrsh 2 7 11 2 get ly 2 mul get ly 11 24
2071wrshl 2 3 1 8 24
2072wrsh 3 15 3 2 get ly 3 get nv 2
2073wrshl 3 0 0 1 0
2074wrsh 4 23 1 6 get ly 4 sub get ly 6 get ly 4
2075wrshl 4 0 0 16 0
2076put nv 16 65536
2077wrsh 5 29 6 3 get ly 7 get nv 37
2078wrshl 5 3 0 8 16
2079put nv 16 0
2080wrsh 6 38 4 2 get ly 12 mul ni 24
2081wrshl 6 2 0 8 24
2082put nv 16 65536
2083wrsh 7 48 1 3 get ly 14 mul ni 8
2084wrshl 7 0 0 8 8
2085wrsh 8 53 8 3 add get ly 7 get ly 15 get nv 36
2086wrshl 8 0 0 64 0
2087put nv 16 0
2088wrsh 9 58 3 0 get ly 9 len t_shs
2089wrshl 9 0 0 1 0
2090end
2091fn wrlib to void
2092liblay
2093sh = get ly 10
2094ebnew add sh 640
2095wrehdr 3 0 4 sh 10
2096wrph 0 1 5 0 0 get ly 6
2097wrph 1 1 6 get ly 7 get ly 8 add get nv 37 mul get nv 24 8
2098note the RW segment's size in memory (offset 64 + 56 + 40): .dynamic, .got and the state area
2099w64 160 get ly 16
2100wrph 2 2 6 get ly 7 get ly 8 get nv 37
2101wrph 3 1685382481 6 0 0 0
2102note PT_GNU_STACK's alignment (offset 64 + 3 * 56 + 48)
2103w64 280 16
2104libsyms
2105libtext
2106libsecs
2107end
2108note ---- the probe executable (ET_EXEC at 4 MiB)
2109note bytes of call row k's code, at most: a bufeq, bufset or reteq row 13 words (it uses 11, 7 or 13); a call row
2110note its arguments (buf 2 words, bufword 3, a value 4; a stack argument 1 more for its STR, and SUB SP and ADD SP
2111note when there is one) and 8
2112fn callsz k ulong to ulong
2113b = mul k CLW
2114if ne get cl add b 24 0
2115ret 52
2116end
2117na = get cl add b 3
2118n = ulong 0
2119for j ulong na bound 10
2120c = get cl add add b 14 j
2121if eq c 1
2122n = add n 2
2123else
2124if eq c 2
2125n = add n 3
2126else
2127n = add n 4
2128end
2129end
2130if ge j 8
2131n = add n 1
2132end
2133end
2134if gt na 8
2135n = add n 2
2136end
2137ret mul add n 8 4
2138end
2139note the probe layout in ly: 0 .hash, 1 its bytes, 2 .dynsym, 3 .dynstr, 4 the relocations, 5 the exit block
2140note (code), 6 the end of the RX segment, 7 the RW segment's file offset, 8 its address minus the base, 9 the
2141note file's length, 17 the buffer's size; the base address is 4 MiB; RW holds .dynamic (192 bytes), the GOT, then
2142note the buffer
2143const PBASE ulong 4194304
2144note the arguments of call row k from offset p (buf: ADRP and ADD of the buffer; bufword: then LDR of its first
2145note word): arguments 1..8 into x0..x7; arguments 9 and 10 into x17, then STR X17, [SP, #0 or #8] below SUB SP,
2146note SP, #16; the next offset
2147fn wrargs k ulong p ulong to ulong
2148b = mul k CLW
2149na = get cl add b 3
2150if gt na 8
2151w32 p 3506455551
2152p = add p 4
2153end
2154for j ulong na bound 10
2155r = j
2156if ge j 8
2157r = 17
2158end
2159p = wrarg p b j r
2160if ge j 8
2161w32 p or 4177527793 shl sub j 8 10
2162p = add p 4
2163end
2164end
2165ret p
2166end
2167note argument j of the call row at cl word b into xr at p; the next offset
2168fn wrarg p ulong b ulong j ulong r ulong to ulong
2169bf = add add get ly 8 192 mul get nv 4 8
2170c = get cl add add b 14 j
2171if ne c 0
2172ret wrbuf p add bf get cl add add b 4 j r c
2173end
2174ret wrval p get cl add add b 4 j r
2175end
2176note MOVZ/MOVK of v into xj at p; the next offset
2177fn wrval p ulong v ulong j ulong to ulong
2178for h ulong 4
2179u = shr v mul h 16
2180wrmov add p mul h 4 h u j
2181end
2182ret add p 16
2183end
2184note argument j as the buffer's address + N (c 1) or its first word (c 2) at p (ADRP; ADD; LDR); the next offset
2185fn wrbuf p ulong bf ulong j ulong c ulong to ulong
2186wradrp p bf j
2187wraddlo add p 4 bf j j
2188if eq c 2
2189w32 add p 8 or 4181721088 or shl j 5 j
2190ret add p 12
2191end
2192ret add p 8
2193end
2194note call row k at offset p (LDR X16, GOT[k]; BLR X16; ADD SP, SP, #16 after stack arguments; X16 = expect; CMP
2195note W0, W16; B.EQ +12; MOVZ X0, #k+1; B exit), its bufeq (X16 = buffer + OFF; LDR X16, [X16]; X17 = VALUE; CMP
2196note X16, X17; B.EQ +12; MOVZ X0, #k+1; B exit), bufset (X16 = buffer + OFF; X17 = VALUE; STR X17, [X16]) or
2197note reteq (X17 = OFF; LDR X16, [X0, X17]; X17 = VALUE; CMP and exit as bufeq); the next offset
2198fn wrcall1 k ulong p ulong to ulong
2199tx = get ly 5
2200b = mul k CLW
2201kd = get cl add b 24
2202if ne kd 0
2203if eq kd 3
2204p = wrval p get cl add b 4 17
2205w32 p 4168181776
2206p = add p 4
2207else
2208bf = add add add get ly 8 192 mul get nv 4 8 get cl add b 4
2209p = wrbuf p bf 16 1
2210end
2211if eq kd 2
2212p = wrval p get cl add b 5 17
2213w32 p 4177527313
2214ret add p 4
2215end
2216if eq kd 1
2217w32 p 4181721616
2218p = add p 4
2219end
2220p = wrval p get cl add b 5 17
2221w32 p 3943760415
2222ret wrfail k add p 4
2223end
2224p = wrargs k p
2225wrldr p add add get ly 8 192 mul k 8
2226w32 add p 4 3594453504
2227p = add p 8
2228if gt get cl add b 3 8
2229w32 p 2432713727
2230p = add p 4
2231end
2232e = get cl add b 2
2233if gt e 4294967295
2234ret p
2235end
2236wrmov p 0 e 16
2237wrmov add p 4 1 shr e 16 16
2238w32 add p 8 1796210719
2239ret wrfail k add p 12
2240end
2241note B.EQ +12; MOVZ X0, #k+1 (at most 100); B exit at p; the next offset
2242fn wrfail k ulong p ulong to ulong
2243w32 p 1409286240
2244x = add k 1
2245if gt x 100
2246x = 100
2247end
2248wrmov add p 4 0 x 0
2249wrb add p 8 get ly 5 335544320
2250ret add p 12
2251end
2252note CMP-equal check then exit 250 (B.EQ +12; MOVZ X0, #250; B exit) at p; the next offset
2253fn chk250 p ulong cmp ulong to ulong
2254w32 p cmp
2255w32 add p 4 1409286240
2256w32 add p 8 3531611968
2257wrb add p 12 get ly 5 335544320
2258ret add p 16
2259end
2260fn calllay to void
2261nc = get nv 4
2262dsinit
2263put nv 13 dsib get nv 5 get nv 6
2264put nv 14 dstx t_origin
2265r = enlarge cd add nc 1
2266if lt r 0
2267die e_mem 3
2268end
2269for k ulong nc bound 9223372036854775807
2270b = mul k CLW
2271put cd k dsib get cl b get cl add b 1
2272end
2273ns = add nc 1
2274note .hash after the interpreter path and its NUL at 344, aligned to 8
2275put ly 0 al8 add 345 len t_interp
2276put ly 1 mul add nbk nc add ns 2 4
2277put ly 2 al8 add get ly 0 get ly 1
2278put ly 3 add get ly 2 mul ns 24
2279put ly 4 al8 add get ly 3 get nv 2
2280put ly 5 al16 add get ly 4 mul nc 24
2281note exit block 8, register setup 76, the calls, register checks 10 * 16 + 8 * 20 + 4 + 16, exit 0 8
2282cs = ulong 432
2283for k ulong nc bound 9223372036854775807
2284cs = add cs callsz k
2285end
2286put ly 6 add get ly 5 cs
2287put ly 7 al16 get ly 6
2288put ly 8 add get ly 7 65536
2289note ly 17 the buffer's size: 4096 bytes past the largest offset a row names, a multiple of 16
2290put ly 17 al16 add get nv 39 4096
2291put ly 9 add add add get ly 7 192 mul nc 8 get ly 17
2292end
2293fn callsyms to void
2294nc = get nv 4
2295wrhash get ly 0 nc false
2296for k ulong nc bound 9223372036854775807
2297o = add get ly 2 mul add k 1 24
2298w32 o get cd k
2299w8 add o 4 18
2300end
2301wrstr get ly 3
2302for k ulong nc bound 9223372036854775807
2303o = add get ly 4 mul k 24
2304w64 o add PBASE add add get ly 8 192 mul k 8
2305w64 add o 8 or shl add k 1 32 1025
2306end
2307note the buffer is in the file, every byte 0xA5, so a write of zeros or of a value past an output shows
2308bo = add add get ly 7 192 mul nc 8
2309for i ulong get ly 17 bound 9223372036854775807
2310w8 add bo i 165
2311end
2312end
2313fn calltext to void
2314tx = get ly 5
2315note exit: MOVZ X8, #94; SVC #0 (exit_group with X0)
2316w32 tx 3531606984
2317w32 add tx 4 3556769793
2318note entry: MOVZ X19..X27 with their numbers; MOV X28, SP; MOVZ X29, #29; FMOV D8..D15 from X19..X26
2319p = add tx 8
2320for i ulong 9
2321r = add 19 i
2322wrmov add p mul i 4 0 r r
2323end
2324w32 add p 36 2432697340
2325wrmov add p 40 0 29 29
2326for i ulong 8
2327w32 add add p 44 mul i 4 or 2657550336 or shl add 19 i 5 add 8 i
2328end
2329p = add p 76
2330for k ulong get nv 4 bound 9223372036854775807
2331p = wrcall1 k p
2332end
2333note CMP Xr, #r for r 19..27 and 29; FMOV X16, Dd and CMP X16, #(d + 11) for d 8..15; MOV X16, SP;
2334note CMP X16, X28; MOVZ X0, #0; B exit
2335for i ulong 9
2336r = add 19 i
2337c = or 4043309087 or shl r 10 shl r 5
2338p = chk250 p c
2339end
2340r = ulong 29
2341p = chk250 p or 4043309087 or shl r 10 shl r 5
2342for i ulong 8
2343w32 p or 2657484816 shl add 8 i 5
2344p = chk250 add p 4 or 4043309599 shl add 19 i 10
2345end
2346w32 p 2432697328
2347p = chk250 add p 4 3944481311
2348w32 p 3531603968
2349wrb add p 4 tx 335544320
2350end
2351fn calldyn to void
2352nc = get nv 4
2353o = dyn get ly 7 1 get nv 13
2354o = dyn o 29 get nv 14
2355o = dyn o 4 add PBASE get ly 0
2356o = dyn o 5 add PBASE get ly 3
2357o = dyn o 6 add PBASE get ly 2
2358o = dyn o 10 get nv 2
2359o = dyn o 11 24
2360o = dyn o 7 add PBASE get ly 4
2361o = dyn o 8 mul nc 24
2362o = dyn o 9 24
2363o = dyn o 30 8
2364o = dyn o 0 0
2365end
2366fn wrcall to void
2367calllay
2368tot = get ly 9
2369ebnew tot
2370dof = get ly 7
2371dva = add PBASE get ly 8
2372wrehdr 2 add add PBASE get ly 5 8 5 0 0
2373wrph 0 3 4 344 add PBASE 344 27
2374wrph 1 1 5 0 PBASE get ly 6
2375wrph 2 1 6 dof dva sub tot dof
2376wrph 3 2 6 dof dva 192
2377wrph 4 1685382481 6 0 0 0
2378note PT_GNU_STACK's alignment (offset 64 + 4 * 56 + 48)
2379w64 336 16
2380wrbytes 344 t_interp
2381callsyms
2382calltext
2383calldyn
2384end
2385fn main to int
2386if lt argcnt 2
2387die e_usage 2
2388end
2389if argis 1 m_lib
2390if ne argcnt 5
2391die e_usage 2
2392end
2393put nv 0 ldf 2 0
2394put nv 1 ldf 3 get nv 0
2395parse
2396objparse
2397wrlib
2398save 4 int 420
2399ret 0
2400end
2401if argis 1 m_call
2402if ne argcnt 4
2403die e_usage 2
2404end
2405put nv 0 ldf 2 0
2406put nv 1 get nv 0
2407parse
2408wrcall
2409save 3 int 493
2410ret 0
2411end
2412die e_usage 2
2413ret 2
2414end