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