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 / si / architecture / tools / zero-legacy / fixtures / elfgen.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note elfgen: writes the synthetic ELF fixtures for the elfclose gate (#316 item 2, DRA-178). |
| 3 | note usage: elfgen DIR (DIR must exist; writes the files named in tnm, 1024 bytes or less each) |
| 4 | note Each file is a little ELF64 image with one property under test; see fixtures/elf.expected |
| 5 | note for the row elfclose must print for each. None of them is executable code. |
| 6 | |
| 7 | text tnm "dyn-gcc\ndyn-nomark\nbadneed\nneed-nostr\nstrtab-unmapped\nbad-shstrndx\nstatic-rust\nclang-lld\nstatic-pie-go\nbare\ngnu-phdr\ntrunc\nbadph\nelf32\nbigend\nnotelf\nneed-inject\nneed-comma\ninterp-tab\ninterp-del\nload-wrap\nesc\tname\\x\n" |
| 8 | text tsr "/lib/ld-linux-aarch64.so.1\nlibc.so.6\nlibm.so.6\nGCC: (Ubuntu 13.2.0) 13.2.0\nrustc version 1.98.1\nclang version 18.1.3\nLinker: LLD 18.1.3\n.comment\n.shstrtab\n.go.buildinfo\n#!/bin/sh\n" |
| 9 | text tuse "usage: elfgen DIR\n" |
| 10 | |
| 11 | global ob uchar 1024 |
| 12 | global dir uchar 4096 |
| 13 | global dirn ulong 1 |
| 14 | global path uchar 4096 |
| 15 | |
| 16 | fn clr to void |
| 17 | for i ulong 1024 |
| 18 | put ob i 0 |
| 19 | end |
| 20 | end |
| 21 | |
| 22 | fn w16 o ulong v ulong to void |
| 23 | put ob o uchar and v 255 |
| 24 | put ob add o 1 uchar and shr v 8 255 |
| 25 | end |
| 26 | |
| 27 | fn w32 o ulong v ulong to void |
| 28 | for i ulong 4 |
| 29 | put ob add o i uchar and shr v mul i 8 255 |
| 30 | end |
| 31 | end |
| 32 | |
| 33 | fn w64 o ulong v ulong to void |
| 34 | for i ulong 8 |
| 35 | put ob add o i uchar and shr v mul i 8 255 |
| 36 | end |
| 37 | end |
| 38 | |
| 39 | note start of entry k of tsr |
| 40 | fn sst k ulong to ulong |
| 41 | s = ulong 0 |
| 42 | j = ulong 0 |
| 43 | while lt j k bound 4096 |
| 44 | if eq get tsr s 10 |
| 45 | j = add j 1 |
| 46 | end |
| 47 | s = add s 1 |
| 48 | end |
| 49 | ret s |
| 50 | end |
| 51 | |
| 52 | note copy entry k of tsr to ob[o..], then a NUL; the entry length |
| 53 | fn wstr o ulong k ulong to ulong |
| 54 | s = sst k |
| 55 | n = ulong 0 |
| 56 | while ne get tsr add s n 10 bound 256 |
| 57 | put ob add o n get tsr add s n |
| 58 | n = add n 1 |
| 59 | end |
| 60 | put ob add o n 0 |
| 61 | ret n |
| 62 | end |
| 63 | |
| 64 | fn base ty ulong mach ulong phn ulong to void |
| 65 | clr |
| 66 | put ob 0 127 |
| 67 | put ob 1 69 |
| 68 | put ob 2 76 |
| 69 | put ob 3 70 |
| 70 | put ob 4 2 |
| 71 | put ob 5 1 |
| 72 | put ob 6 1 |
| 73 | w16 16 ty |
| 74 | w16 18 mach |
| 75 | w32 20 1 |
| 76 | w64 24 4096 |
| 77 | w64 32 64 |
| 78 | w16 52 64 |
| 79 | w16 54 56 |
| 80 | w16 56 phn |
| 81 | end |
| 82 | |
| 83 | fn phd i ulong t ulong off ulong va ulong fz ulong to void |
| 84 | b = add 64 mul i 56 |
| 85 | w32 b t |
| 86 | w32 add b 4 5 |
| 87 | w64 add b 8 off |
| 88 | w64 add b 16 va |
| 89 | w64 add b 24 va |
| 90 | w64 add b 32 fz |
| 91 | w64 add b 40 fz |
| 92 | w64 add b 48 8 |
| 93 | end |
| 94 | |
| 95 | fn dyn i ulong tag ulong v ulong to void |
| 96 | b = add 544 mul i 16 |
| 97 | w64 b tag |
| 98 | w64 add b 8 v |
| 99 | end |
| 100 | |
| 101 | fn shd i ulong nm ulong t ulong off ulong sz ulong to void |
| 102 | b = add 768 mul i 64 |
| 103 | w32 b nm |
| 104 | w32 add b 4 t |
| 105 | w64 add b 24 off |
| 106 | w64 add b 32 sz |
| 107 | w64 add b 48 1 |
| 108 | end |
| 109 | |
| 110 | note three sections: null, entry k of tsr at 672 (csz bytes), .shstrtab at 736 |
| 111 | fn secs3 k ulong csz ulong to void |
| 112 | put ob 736 0 |
| 113 | a = wstr 737 k |
| 114 | b = wstr add 738 a 8 |
| 115 | w64 40 768 |
| 116 | w16 58 64 |
| 117 | w16 60 3 |
| 118 | w16 62 2 |
| 119 | shd 1 1 1 672 csz |
| 120 | shd 2 add a 2 3 736 add add a b 3 |
| 121 | end |
| 122 | |
| 123 | note write n bytes of ob to DIR/name, name = entry k of tnm |
| 124 | fn save k ulong n ulong to bool |
| 125 | d = get dirn 0 |
| 126 | for i ulong d |
| 127 | put path i get dir i |
| 128 | end |
| 129 | put path d 47 |
| 130 | s = ulong 0 |
| 131 | j = ulong 0 |
| 132 | while lt j k bound 4096 |
| 133 | if eq get tnm s 10 |
| 134 | j = add j 1 |
| 135 | end |
| 136 | s = add s 1 |
| 137 | end |
| 138 | m = ulong 0 |
| 139 | while ne get tnm add s m 10 bound 64 |
| 140 | put path add add d 1 m get tnm add s m |
| 141 | m = add m 1 |
| 142 | end |
| 143 | fd = fopen path 0 add add d 1 m 577 420 |
| 144 | if lt fd 0 |
| 145 | ret false |
| 146 | end |
| 147 | r = write int fd ob 0 n |
| 148 | c = fclose int fd |
| 149 | ret eq r long n |
| 150 | end |
| 151 | |
| 152 | fn dyngcc to void |
| 153 | base 3 183 3 |
| 154 | phd 0 3 512 512 27 |
| 155 | a = wstr 512 0 |
| 156 | phd 1 1 0 65536 1024 |
| 157 | phd 2 2 544 544 80 |
| 158 | dyn 0 1 1 |
| 159 | dyn 1 1 11 |
| 160 | dyn 2 5 66160 |
| 161 | dyn 3 10 32 |
| 162 | dyn 4 0 0 |
| 163 | put ob 624 0 |
| 164 | b = wstr 625 1 |
| 165 | c = wstr 635 2 |
| 166 | d = wstr 672 3 |
| 167 | secs3 7 add d 1 |
| 168 | end |
| 169 | |
| 170 | fn bare to void |
| 171 | base 2 183 1 |
| 172 | phd 0 1 0 0 1024 |
| 173 | end |
| 174 | |
| 175 | fn main to int |
| 176 | if lt argcnt 2 |
| 177 | r = write 2 tuse 0 len tuse |
| 178 | ret 64 |
| 179 | end |
| 180 | n = argread 1 dir 0 4000 |
| 181 | if lt n 1 |
| 182 | ret 64 |
| 183 | end |
| 184 | put dirn 0 ulong n |
| 185 | ok = true |
| 186 | dyngcc |
| 187 | if not save 0 1024 |
| 188 | ok = false |
| 189 | end |
| 190 | note dyn-nomark: dyn-gcc without its section headers: no toolchain mark, but dynamic |
| 191 | dyngcc |
| 192 | w64 40 0 |
| 193 | w16 60 0 |
| 194 | w16 62 0 |
| 195 | if not save 1 1024 |
| 196 | ok = false |
| 197 | end |
| 198 | dyngcc |
| 199 | dyn 0 1 9999 |
| 200 | if not save 2 1024 |
| 201 | ok = false |
| 202 | end |
| 203 | dyngcc |
| 204 | dyn 2 0 0 |
| 205 | if not save 3 1024 |
| 206 | ok = false |
| 207 | end |
| 208 | dyngcc |
| 209 | dyn 2 5 999999 |
| 210 | if not save 4 1024 |
| 211 | ok = false |
| 212 | end |
| 213 | dyngcc |
| 214 | w16 62 3 |
| 215 | if not save 5 1024 |
| 216 | ok = false |
| 217 | end |
| 218 | base 2 62 1 |
| 219 | phd 0 1 0 4194304 1024 |
| 220 | a = wstr 672 4 |
| 221 | secs3 7 add a 1 |
| 222 | if not save 6 1024 |
| 223 | ok = false |
| 224 | end |
| 225 | base 2 183 1 |
| 226 | phd 0 1 0 0 1024 |
| 227 | a = wstr 672 5 |
| 228 | b = wstr add 673 a 6 |
| 229 | secs3 7 add add a b 2 |
| 230 | if not save 7 1024 |
| 231 | ok = false |
| 232 | end |
| 233 | base 3 183 2 |
| 234 | phd 0 1 0 0 1024 |
| 235 | phd 1 2 544 544 16 |
| 236 | dyn 0 0 0 |
| 237 | secs3 9 16 |
| 238 | if not save 8 1024 |
| 239 | ok = false |
| 240 | end |
| 241 | bare |
| 242 | if not save 9 1024 |
| 243 | ok = false |
| 244 | end |
| 245 | base 2 183 2 |
| 246 | phd 0 1 0 0 1024 |
| 247 | phd 1 1685382481 0 0 0 |
| 248 | if not save 10 1024 |
| 249 | ok = false |
| 250 | end |
| 251 | bare |
| 252 | if not save 11 40 |
| 253 | ok = false |
| 254 | end |
| 255 | bare |
| 256 | put ob 32 240 |
| 257 | for i ulong 7 |
| 258 | put ob add 33 i 255 |
| 259 | end |
| 260 | if not save 12 1024 |
| 261 | ok = false |
| 262 | end |
| 263 | bare |
| 264 | put ob 4 1 |
| 265 | if not save 13 1024 |
| 266 | ok = false |
| 267 | end |
| 268 | bare |
| 269 | put ob 5 2 |
| 270 | if not save 14 1024 |
| 271 | ok = false |
| 272 | end |
| 273 | clr |
| 274 | a = wstr 0 10 |
| 275 | put ob a 10 |
| 276 | if not save 15 add a 1 |
| 277 | ok = false |
| 278 | end |
| 279 | note need-inject: dyn-gcc with its first DT_NEEDED name "libc.so.6" replaced by the 9 bytes |
| 280 | note x, newline, E, L, F, tab, /, o, k: written raw it would end the row and forge an ELF row |
| 281 | dyngcc |
| 282 | put ob 625 120 |
| 283 | put ob 626 10 |
| 284 | put ob 627 69 |
| 285 | put ob 628 76 |
| 286 | put ob 629 70 |
| 287 | put ob 630 9 |
| 288 | put ob 631 47 |
| 289 | put ob 632 111 |
| 290 | put ob 633 107 |
| 291 | if not save 16 1024 |
| 292 | ok = false |
| 293 | end |
| 294 | note need-comma: "libc,so.6" would split into two names in the comma-separated field |
| 295 | dyngcc |
| 296 | put ob 629 44 |
| 297 | if not save 17 1024 |
| 298 | ok = false |
| 299 | end |
| 300 | note interp-tab and interp-del: a tab or a DEL inside the PT_INTERP path |
| 301 | dyngcc |
| 302 | put ob 516 9 |
| 303 | if not save 18 1024 |
| 304 | ok = false |
| 305 | end |
| 306 | dyngcc |
| 307 | put ob 516 127 |
| 308 | if not save 19 1024 |
| 309 | ok = false |
| 310 | end |
| 311 | note load-wrap: the PT_LOAD file offset is 2^64-4096 and DT_STRTAB is 4720 past its vaddr, so |
| 312 | note offset + (v - vaddr) wraps to 624, the real strtab; the PT_LOAD itself leaves the file |
| 313 | dyngcc |
| 314 | phd 1 1 sub ulong 0 4096 65536 8192 |
| 315 | dyn 2 5 70256 |
| 316 | if not save 20 1024 |
| 317 | ok = false |
| 318 | end |
| 319 | note esc<TAB>name\x: a clean ELF whose file name holds a tab and a backslash |
| 320 | bare |
| 321 | if not save 21 1024 |
| 322 | ok = false |
| 323 | end |
| 324 | if not ok |
| 325 | ret 1 |
| 326 | end |
| 327 | ret 0 |
| 328 | end |