interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / tools / zero-legacy / elfclose.om

1301 lines · 19804 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note elfclose: ELF and process closure inventory (aien-architecture-mv #316 item 2, DRA-178).
3note Seeded Omega program, built only by the pinned seed compiler in omega.pin.
4note usage: elfclose elf PATH... one row per file
5note elfclose proc [PROCROOT] every process under PROCROOT (default /proc)
6note An ELF row is ELF path class machine type link interp needed producer verdict (tabs).
7note class ELF64LE, or UNSUPPORTED (32-bit or big-endian), MALFORMED (a header, program
8note header, PT_LOAD, dynamic or section field points outside the file or breaks a rule,
9note or the interpreter or a DT_NEEDED name holds a control byte (below 0x20, 0x7f),
10note or a DT_NEEDED name holds a comma), NOT_ELF, UNREADABLE (cannot be opened, or not
11note a regular file: FIFO, directory, device, socket; opened without blocking).
12note link static (no PT_INTERP, no PT_DYNAMIC), dynamic (PT_INTERP), static-pie
13note (PT_DYNAMIC without PT_INTERP).
14note needed the DT_NEEDED names, comma separated, read through DT_STRTAB mapped by PT_LOAD.
15note producer the toolchain marks found, joined by +, or none: gcc, clang, rustc, lld (in
16note .comment), go (.go.buildinfo or .note.go.buildid), gnu (PT_GNU_* program headers,
17note .gnu.* or .note.gnu.* sections), crt (.note.ABI-tag), note (PT_NOTE).
18note verdict FOREIGN when a toolchain mark is present or the file links dynamically
19note (dynamic or static-pie); UNMARKED when an ELF64LE file carries neither (seeded
20note omc output: PT_LOAD only, no sections; that is not proof of its source);
21note UNKNOWN for every other class.
22note A process row is PROC pid owner exe-link-target class ... verdict, classified by reading
23note PROCROOT/pid/exe (the running image); owner is self when PROCROOT/pid belongs to the
24note scanning uid, else that uid. MAP pid path rows list the file paths of its executable
25note mappings in PROCROOT/pid/maps, dropping a path equal to the one on the MAP row just before
26note (consecutive duplicates), not every repeat. Processes whose exe link does not exist
27note (kernel threads, exited) are counted as NOEXE; other link errors give an UNREADABLE row.
28note Paths, exe link targets and MAP paths are written with every control byte and backslash
29note as \xHH, so no file name can end a row or add a field. PROCROOT is at most 4027 bytes.
30note Capacities: 64 program headers, 1024 sections, a 64 KiB section-name table, the first 512
31note dynamic entries and sonames up to 255 bytes; beyond them a file is MALFORMED (dynamic
32note entries past 512 are not read). Only the first 4 KiB of .comment is searched.
33note Summary lines: # ELF, # FOREIGN, # UNMARKED, # UNKNOWN, # PROC, # MAP, # NOEXE,
34note # MAPTRUNC (maps over 960 KiB), each with a count. Exit 0 done, 64 usage.
35
36text tuse "usage: elfclose elf PATH... | elfclose proc [PROCROOT]\n"
37text telf "elf"
38text tprc "proc"
39text tdpr "/proc"
40text texe "/exe"
41text tmps "/maps"
42text tnl "\n"
43text ttab "\t"
44text tdash "-"
45text tplus "+"
46text tcom ","
47text thash "# "
48text tsp " "
49text trELF "ELF\t"
50text trPRC "PROC\t"
51text trMAP "MAP\t"
52text tcls "ELF64LE\nUNSUPPORTED\nMALFORMED\nNOT_ELF\nUNREADABLE\n"
53text tlnk "static\ndynamic\nstatic-pie\n"
54text tprd "gcc\nclang\nrustc\ngo\nlld\ngnu\ncrt\nnote\n"
55text tvrd "FOREIGN\nUNMARKED\nUNKNOWN\n"
56text tcnt "ELF\nFOREIGN\nUNMARKED\nUNKNOWN\nPROC\nMAP\nNOEXE\nMAPTRUNC\n"
57text tsn ".comment\n.go.buildinfo\n.note.go.buildid\n.note.ABI-tag\n.gnu.\n.note.gnu.\n"
58text tnd "GCC:\nclang version\nrustc version\nLinker: LLD\n"
59text tmach "aarch64"
60text tx86 "x86_64"
61text tem "em"
62text tet "et"
63text ttyp "rel\nexec\ndyn\ncore\n"
64text tnone "none"
65text tself "self"
66
67global hb uchar 64
68global ph uchar 3584
69global dy uchar 8192
70global st uchar 4096
71global sh uchar 65536
72global ss uchar 65536
73global ssn ulong 1
74global cm uchar 4096
75global ib uchar 4096
76global ibn ulong 1
77global nd uchar 8192
78global ndn ulong 1
79global fsz ulong 1
80global fdg long 1
81global res ulong 8
82global pv ulong 8
83global path uchar 4096
84global pthn ulong 1
85global lnk uchar 4096
86global lnkn ulong 1
87global db uchar 65536
88global dfd long 1
89global root uchar 4096
90global rootn ulong 1
91global pid uchar 64
92global pidn ulong 1
93global mp uchar 983040
94global prv uchar 4096
95global prvn ulong 1
96global cnt ulong 8
97global nb uchar 24
98global arg uchar 16
99global stb uchar 128
100
101note ---- little-endian readers, one per buffer (a global is never an argument) ----
102
103fn h16 o ulong to ulong
104ret add ulong get hb o mul 256 ulong get hb add o 1
105end
106
107fn h64 o ulong to ulong
108v = ulong 0
109for i ulong 8
110v = or shl v 8 ulong get hb add o sub 7 i
111end
112ret v
113end
114
115fn p32 o ulong to ulong
116v = ulong 0
117for i ulong 4
118v = or shl v 8 ulong get ph add o sub 3 i
119end
120ret v
121end
122
123fn p64 o ulong to ulong
124v = ulong 0
125for i ulong 8
126v = or shl v 8 ulong get ph add o sub 7 i
127end
128ret v
129end
130
131fn d64 o ulong to ulong
132v = ulong 0
133for i ulong 8
134v = or shl v 8 ulong get dy add o sub 7 i
135end
136ret v
137end
138
139fn s32 o ulong to ulong
140v = ulong 0
141for i ulong 4
142v = or shl v 8 ulong get sh add o sub 3 i
143end
144ret v
145end
146
147fn s64 o ulong to ulong
148v = ulong 0
149for i ulong 8
150v = or shl v 8 ulong get sh add o sub 7 i
151end
152ret v
153end
154
155note ---- text tables: entry e of a newline-separated table ----
156
157fn tst e ulong k ulong to ulong
158s = ulong 0
159j = ulong 0
160while lt j e bound 4096
161if eq k 0
162if eq get tcls s 10
163j = add j 1
164end
165end
166if eq k 1
167if eq get tlnk s 10
168j = add j 1
169end
170end
171if eq k 2
172if eq get tprd s 10
173j = add j 1
174end
175end
176if eq k 3
177if eq get tvrd s 10
178j = add j 1
179end
180end
181if eq k 4
182if eq get tcnt s 10
183j = add j 1
184end
185end
186if eq k 5
187if eq get tsn s 10
188j = add j 1
189end
190end
191if eq k 6
192if eq get tnd s 10
193j = add j 1
194end
195end
196if eq k 7
197if eq get ttyp s 10
198j = add j 1
199end
200end
201s = add s 1
202end
203ret s
204end
205
206fn tch k ulong i ulong to uchar
207if eq k 0
208ret get tcls i
209end
210if eq k 1
211ret get tlnk i
212end
213if eq k 2
214ret get tprd i
215end
216if eq k 3
217ret get tvrd i
218end
219if eq k 4
220ret get tcnt i
221end
222if eq k 5
223ret get tsn i
224end
225if eq k 6
226ret get tnd i
227end
228ret get ttyp i
229end
230
231fn tln e ulong k ulong to ulong
232s = tst e k
233n = ulong 0
234while ne tch k add s n 10 bound 64
235n = add n 1
236end
237ret n
238end
239
240fn wtbl e ulong k ulong to void
241s = tst e k
242n = tln e k
243for i ulong n
244c = tch k add s i
245put nb 0 c
246r = write 1 nb 0 1
247end
248end
249
250note ---- output ----
251
252fn wnl to void
253r = write 1 tnl 0 1
254end
255
256note byte i of buffer k (0 path, 1 lnk, 2 mp): a global is never an argument
257fn ebyte k ulong i ulong to uchar
258if eq k 0
259ret get path i
260end
261if eq k 1
262ret get lnk i
263end
264ret get mp i
265end
266
267fn wrun k ulong s ulong n ulong to void
268if eq n 0
269ret
270end
271if eq k 0
272r = write 1 path s n
273ret
274end
275if eq k 1
276r = write 1 lnk s n
277ret
278end
279r = write 1 mp s n
280end
281
282fn hexd v uchar to uchar
283if lt v 10
284ret add v 48
285end
286ret add v 87
287end
288
289note write buffer k bytes [o, o+n) into a report row: a control byte or a backslash is written as
290note \xHH (two lowercase hex digits), so a path or link target can never end a row or add a field
291fn wesc k ulong o ulong n ulong to void
292s = o
293e = add o n
294i = o
295while lt i e bound 983041
296c = ebyte k i
297esc = ctlb c
298if eq c 92
299esc = true
300end
301if esc
302wrun k s sub i s
303put nb 0 92
304put nb 1 120
305put nb 2 hexd shr c 4
306put nb 3 hexd and c 15
307r = write 1 nb 0 4
308s = add i 1
309end
310i = add i 1
311end
312wrun k s sub e s
313end
314
315fn wtab to void
316r = write 1 ttab 0 1
317end
318
319fn wdash to void
320r = write 1 tdash 0 1
321end
322
323fn wnum v ulong to void
324n = ulong 0
325if eq v 0
326put nb 23 48
327n = 1
328end
329x = v
330while gt x 0 bound 20
331put nb sub 23 n uchar add 48 rem x 10
332x = div x 10
333n = add n 1
334end
335r = write 1 nb sub 24 n n
336end
337
338note ---- file ranges ----
339
340fn inrange o ulong n ulong to bool
341f = get fsz 0
342if gt o f
343ret false
344end
345ret ge sub f o n
346end
347
348note ---- program headers ----
349
350fn interp o ulong to ulong
351off = p64 add o 8
352fz = p64 add o 32
353if eq fz 0
354ret 2
355end
356if gt fz 4095
357ret 2
358end
359if not inrange off fz
360ret 2
361end
362k = pread int get fdg 0 ib 0 fz long off
363if ne k long fz
364ret 2
365end
366n = ulong 0
367while lt n fz bound 4096
368if eq get ib n 0
369break
370end
371n = add n 1
372end
373if eq n 0
374ret 2
375end
376for i ulong n
377if ctlb get ib i
378ret 2
379end
380end
381put ibn 0 n
382ret 0
383end
384
385note a control byte (below 0x20, or 0x7f): never valid in an interpreter path or a soname, and
386note never written raw into a report row (it could end the row or forge a field)
387fn ctlb c uchar to bool
388if lt c 32
389ret true
390end
391ret eq c 127
392end
393
394note file offset of virtual address v through the PT_LOAD headers, or 2^64-1 when unmapped
395fn vtoo v ulong pn ulong to ulong
396for i ulong pn
397b = mul i 56
398if eq p32 b 1
399va = p64 add b 16
400fz = p64 add b 32
401if ge v va
402if lt sub v va fz
403ret add p64 add b 8 sub v va
404end
405end
406end
407end
408ret sub ulong 0 1
409end
410
411fn phscan pn ulong to ulong
412put pv 0 0
413put pv 1 0
414for i ulong pn
415b = mul i 56
416t = p32 b
417if eq t 1
418note a PT_LOAD whose file bytes leave the file is MALFORMED, so vtoo never wraps an offset
419if not inrange p64 add b 8 p64 add b 32
420ret 2
421end
422end
423if eq t 3
424if eq get pv 0 1
425ret 2
426end
427put pv 0 1
428r = interp b
429if ne r 0
430ret r
431end
432end
433if eq t 2
434put pv 1 1
435put pv 2 p64 add b 8
436put pv 3 p64 add b 32
437end
438if eq t 4
439put res 4 or get res 4 128
440end
441if ge t 1685382480
442if ge 1685382483 t
443note PT_GNU_EH_FRAME, PT_GNU_STACK, PT_GNU_RELRO, PT_GNU_PROPERTY
444put res 4 or get res 4 32
445end
446end
447end
448ret 0
449end
450
451note append the NUL-terminated name at strtab offset o (strsz z, strtab at file offset pv 7)
452fn addneed o ulong z ulong to ulong
453if ge o z
454ret 2
455end
456w = sub z o
457if gt w 256
458w = 256
459end
460k = pread int get fdg 0 st 0 w long add get pv 7 o
461if ne k long w
462ret 2
463end
464n = ulong 0
465while lt n w bound 4096
466if eq get st n 0
467break
468end
469n = add n 1
470end
471if eq n w
472ret 2
473end
474if eq n 0
475ret 2
476end
477for i ulong n
478c = get st i
479if ctlb c
480ret 2
481end
482if eq c 44
483ret 2
484end
485end
486m = get ndn 0
487if gt add add m n 1 8192
488ret 2
489end
490if gt m 0
491put nd m 44
492m = add m 1
493end
494for i ulong n
495put nd add m i get st i
496end
497put ndn 0 add m n
498ret 0
499end
500
501fn dynamic pn ulong to ulong
502z = get pv 3
503if gt z 8192
504z = 8192
505end
506z = mul div z 16 16
507off = get pv 2
508if not inrange off z
509ret 2
510end
511k = pread int get fdg 0 dy 0 z long off
512if ne k long z
513ret 2
514end
515nent = div z 16
516put pv 4 0
517put pv 5 0
518put pv 6 0
519for i ulong nent
520t = d64 mul i 16
521if eq t 0
522break
523end
524if eq t 5
525put pv 4 d64 add mul i 16 8
526put pv 6 1
527end
528if eq t 10
529put pv 5 d64 add mul i 16 8
530end
531end
532ret needs nent pn
533end
534
535fn needs nent ulong pn ulong to ulong
536sz = get pv 5
537if eq get pv 6 0
538for i ulong nent
539t = d64 mul i 16
540if eq t 0
541break
542end
543if eq t 1
544ret 2
545end
546end
547ret 0
548end
549so = vtoo get pv 4 pn
550if not inrange so sz
551ret 2
552end
553put pv 7 so
554for i ulong nent
555t = d64 mul i 16
556if eq t 0
557break
558end
559if eq t 1
560r = addneed d64 add mul i 16 8 sz
561if ne r 0
562ret r
563end
564end
565end
566ret 0
567end
568
569fn phdrs to ulong
570put res 3 0
571po = h64 32
572pe = h16 54
573pn = h16 56
574if eq pn 0
575ret 0
576end
577if ne pe 56
578ret 2
579end
580if gt pn 64
581ret 2
582end
583sz = mul pn 56
584if not inrange po sz
585ret 2
586end
587k = pread int get fdg 0 ph 0 sz long po
588if ne k long sz
589ret 2
590end
591r = phscan pn
592if ne r 0
593ret r
594end
595if eq get pv 1 1
596r = dynamic pn
597if ne r 0
598ret r
599end
600end
601if eq get pv 0 1
602put res 3 1
603ret 0
604end
605if eq get pv 1 1
606put res 3 2
607end
608ret 0
609end
610
611note ---- sections ----
612
613note does the shstrtab name at ss[o] equal entry e of tsn (with its terminating NUL)
614fn ssis o ulong e ulong to bool
615s = tst e 5
616n = tln e 5
617if gt add add o n 1 get ssn 0
618ret false
619end
620for i ulong n
621if ne get ss add o i get tsn add s i
622ret false
623end
624end
625ret eq get ss add o n 0
626end
627
628note does the shstrtab name at ss[o] start with entry e of tsn
629fn sspre o ulong e ulong to bool
630s = tst e 5
631n = tln e 5
632if gt add o n get ssn 0
633ret false
634end
635for i ulong n
636if ne get ss add o i get tsn add s i
637ret false
638end
639end
640ret true
641end
642
643note does needle (tnd from s, m bytes) occur at cm[i]
644fn cmat i ulong s ulong m ulong to bool
645for j ulong m
646c = get cm add i j
647if ne c get tnd add s j
648ret false
649end
650end
651ret true
652end
653
654fn cmhas e ulong n ulong to bool
655s = tst e 6
656m = tln e 6
657if lt n m
658ret false
659end
660lim = sub n m
661i = ulong 0
662while le i lim bound 8192
663if cmat i s m
664ret true
665end
666i = add i 1
667end
668ret false
669end
670
671fn comment b ulong to ulong
672off = s64 add b 24
673z = s64 add b 32
674if gt z 4096
675z = 4096
676end
677if not inrange off z
678ret 2
679end
680k = pread int get fdg 0 cm 0 z long off
681if ne k long z
682ret 2
683end
684p = get res 4
685if cmhas 0 z
686p = or p 1
687end
688if cmhas 1 z
689p = or p 2
690end
691if cmhas 2 z
692p = or p 4
693end
694if cmhas 3 z
695p = or p 16
696end
697put res 4 p
698ret 0
699end
700
701fn shstr sn ulong sx ulong to ulong
702if ge sx sn
703ret 2
704end
705b = mul sx 64
706xo = s64 add b 24
707xz = s64 add b 32
708if gt xz 65536
709ret 2
710end
711if not inrange xo xz
712ret 2
713end
714k = pread int get fdg 0 ss 0 xz long xo
715if ne k long xz
716ret 2
717end
718put ssn 0 xz
719ret 0
720end
721
722fn secscan sn ulong to ulong
723for i ulong sn
724b = mul i 64
725nm = s32 b
726if ge nm get ssn 0
727ret 2
728end
729if ssis nm 0
730r = comment b
731if ne r 0
732ret r
733end
734end
735go = ssis nm 1
736if not go
737go = ssis nm 2
738end
739if go
740put res 4 or get res 4 8
741end
742if ssis nm 3
743put res 4 or get res 4 64
744end
745gn = sspre nm 4
746if not gn
747gn = sspre nm 5
748end
749if gn
750put res 4 or get res 4 32
751end
752end
753ret 0
754end
755
756fn sects to ulong
757so = h64 40
758se = h16 58
759sn = h16 60
760if eq so 0
761ret 0
762end
763if eq sn 0
764ret 0
765end
766if ne se 64
767ret 2
768end
769if gt sn 1024
770ret 2
771end
772sz = mul sn 64
773if not inrange so sz
774ret 2
775end
776k = pread int get fdg 0 sh 0 sz long so
777if ne k long sz
778ret 2
779end
780r = shstr sn h16 62
781if ne r 0
782ret r
783end
784ret secscan sn
785end
786
787note ---- one file ----
788
789fn parse to ulong
790k = pread int get fdg 0 hb 0 64 0
791if lt k 4
792ret 3
793end
794if ne get hb 0 127
795ret 3
796end
797if ne get hb 1 69
798ret 3
799end
800if ne get hb 2 76
801ret 3
802end
803if ne get hb 3 70
804ret 3
805end
806if lt k 64
807ret 2
808end
809if ne get hb 4 2
810ret 1
811end
812if ne get hb 5 1
813ret 1
814end
815put res 1 h16 18
816put res 2 h16 16
817r = phdrs
818if ne r 0
819ret r
820end
821ret sects
822end
823
824note classify the file named by path; results in res, ib, nd
825fn classify to void
826put res 0 4
827put res 3 0
828put res 4 0
829put ndn 0 0
830put ibn 0 0
831put ssn 0 0
832note open without blocking (a FIFO would block a plain open) and accept only a regular file:
833note anything else (FIFO, directory, device, socket) is UNREADABLE
834fd = fopen path 0 get pthn 0 2048 0
835if lt fd 0
836ret
837end
838s = fstat int fd stb 0 128
839if lt s 0
840r = fclose int fd
841ret
842end
843m = or ulong get stb 16 shl ulong get stb 17 8
844if ne and m 61440 32768
845r = fclose int fd
846ret
847end
848put fdg 0 fd
849e = fseek int fd 0 2
850if lt e 0
851r = fclose int fd
852ret
853end
854put fsz 0 ulong e
855c = parse
856put res 0 c
857r = fclose int fd
858end
859
860fn wmach m ulong to void
861if eq m 183
862r = write 1 tmach 0 len tmach
863ret
864end
865if eq m 62
866r = write 1 tx86 0 len tx86
867ret
868end
869r = write 1 tem 0 len tem
870wnum m
871end
872
873fn wtype t ulong to void
874if ge t 1
875if ge 4 t
876wtbl sub t 1 7
877ret
878end
879end
880r = write 1 tet 0 len tet
881wnum t
882end
883
884fn wprod to void
885p = get res 4
886if eq p 0
887r = write 1 tnone 0 len tnone
888ret
889end
890first = true
891for i ulong 8
892if ne and shr p i 1 0
893if not first
894r = write 1 tplus 0 1
895end
896first = false
897wtbl i 2
898end
899end
900end
901
902note write class onward for the classified file, count it
903fn wtail to void
904c = get res 0
905wtbl c 0
906wtab
907if ne c 0
908for i ulong 5
909wdash
910wtab
911end
912wdash
913wtab
914wtbl 2 3
915wnl
916put cnt 3 add get cnt 3 1
917ret
918end
919wmach get res 1
920wtab
921wtype get res 2
922wtab
923wtbl get res 3 1
924wtab
925if eq get ibn 0 0
926wdash
927else
928r = write 1 ib 0 get ibn 0
929end
930wtab
931if eq get ndn 0 0
932wdash
933else
934r = write 1 nd 0 get ndn 0
935end
936wtab
937wprod
938wtab
939fx = ne get res 4 0
940if ne get res 3 0
941fx = true
942end
943if not fx
944wtbl 1 3
945put cnt 2 add get cnt 2 1
946else
947wtbl 0 3
948put cnt 1 add get cnt 1 1
949end
950wnl
951end
952
953note ---- elf mode ----
954
955fn elfmode to void
956for a ulong sub ulong argcnt 2
957n = argread add a 2 path 0 4095
958if gt n 0
959put pthn 0 ulong n
960classify
961r = write 1 trELF 0 len trELF
962wesc 0 0 ulong n
963wtab
964wtail
965put cnt 0 add get cnt 0 1
966end
967end
968end
969
970note ---- proc mode ----
971
972note path = root / pid / suffix (0 exe, 1 maps)
973fn mkpath sfx ulong to void
974n = get rootn 0
975for i ulong n
976put path i get root i
977end
978put path n 47
979m = get pidn 0
980for i ulong m
981put path add add n 1 i get pid i
982end
983k = add add n 1 m
984if eq sfx 2
985put pthn 0 k
986ret
987end
988if eq sfx 0
989for i ulong len texe
990put path add k i get texe i
991end
992put pthn 0 add k len texe
993ret
994end
995for i ulong len tmps
996put path add k i get tmps i
997end
998put pthn 0 add k len tmps
999end
1000
1001fn wpid to void
1002r = write 1 pid 0 get pidn 0
1003end
1004
1005note one maps line mp[s, e): print MAP for an executable mapping of a file path
1006fn mapline s ulong e ulong to void
1007if not mapx s e
1008ret
1009end
1010note skip five fields (range perms offset dev inode) to the path
1011i = s
1012f = ulong 0
1013while lt i e bound 4096
1014if eq get mp i 32
1015f = add f 1
1016while lt i e bound 4096
1017if ne get mp i 32
1018break
1019end
1020i = add i 1
1021end
1022if eq f 5
1023break
1024end
1025else
1026i = add i 1
1027end
1028end
1029if ge i e
1030ret
1031end
1032if ne get mp i 47
1033ret
1034end
1035n = sub e i
1036if gt n 4095
1037ret
1038end
1039if eq n get prvn 0
1040same = true
1041for j ulong n
1042if ne get prv j get mp add i j
1043same = false
1044break
1045end
1046end
1047if same
1048ret
1049end
1050end
1051for j ulong n
1052put prv j get mp add i j
1053end
1054put prvn 0 n
1055r = write 1 trMAP 0 len trMAP
1056wpid
1057wtab
1058wesc 2 i n
1059wnl
1060put cnt 5 add get cnt 5 1
1061end
1062
1063note the perms field of the maps line at mp[s, e) has x as its third byte
1064fn mapx s ulong e ulong to bool
1065i = s
1066while lt i e bound 4096
1067if eq get mp i 32
1068break
1069end
1070i = add i 1
1071end
1072if gt add i 4 e
1073ret false
1074end
1075ret eq get mp add i 3 120
1076end
1077
1078fn maps to void
1079mkpath 1
1080fd = fopen path 0 get pthn 0 0 0
1081if lt fd 0
1082ret
1083end
1084n = ulong 0
1085c = true
1086while c bound 983041
1087r = read int fd mp n sub 983040 n
1088if le r 0
1089c = false
1090else
1091n = add n ulong r
1092if eq n 983040
1093c = false
1094put cnt 7 add get cnt 7 1
1095end
1096end
1097end
1098r = fclose int fd
1099put prvn 0 0
1100s = ulong 0
1101i = ulong 0
1102while lt i n bound 983041
1103if eq get mp i 10
1104if ge 4000 sub i s
1105mapline s i
1106end
1107s = add i 1
1108end
1109i = add i 1
1110end
1111end
1112
1113note the owner uid of PROCROOT/pid, or - when stat fails
1114fn wuid to void
1115mkpath 2
1116r = stat path 0 get pthn 0 stb 0 128
1117if lt r 0
1118wdash
1119ret
1120end
1121u = ulong 0
1122for i ulong 4
1123u = or shl u 8 ulong get stb add 24 sub 3 i
1124end
1125if eq u ulong getuid
1126r = write 1 tself 0 len tself
1127ret
1128end
1129wnum u
1130end
1131
1132fn onepid to void
1133mkpath 0
1134k = readlnk path 0 get pthn 0 lnk 0 4096
1135if eq k -2
1136put cnt 6 add get cnt 6 1
1137ret
1138end
1139r = write 1 trPRC 0 len trPRC
1140wpid
1141wtab
1142wuid
1143wtab
1144mkpath 0
1145put cnt 4 add get cnt 4 1
1146if lt k 0
1147wdash
1148wtab
1149put res 0 4
1150wtail
1151ret
1152end
1153wesc 1 0 ulong k
1154wtab
1155classify
1156wtail
1157maps
1158end
1159
1160fn isnum o ulong m ulong to bool
1161if eq m 0
1162ret false
1163end
1164if gt m 63
1165ret false
1166end
1167for i ulong m
1168c = get db add o i
1169if lt c 48
1170ret false
1171end
1172if gt c 57
1173ret false
1174end
1175end
1176ret true
1177end
1178
1179fn dent o ulong to void
1180k = add o 19
1181m = ulong 0
1182while ne get db add k m 0 bound 256
1183m = add m 1
1184end
1185if not isnum k m
1186ret
1187end
1188for i ulong m
1189put pid i get db add k i
1190end
1191put pidn 0 m
1192onepid
1193end
1194
1195fn procmode to void
1196n = get rootn 0
1197for i ulong n
1198put path i get root i
1199end
1200fd = fopen path 0 n 16384 0
1201if lt fd 0
1202ret
1203end
1204while true bound 100000
1205r = getdnts int fd db 0 len db
1206if le r 0
1207break
1208end
1209o = ulong 0
1210while lt o ulong r bound 65536
1211lo = ulong get db add o 16
1212hi = ulong get db add o 17
1213dent o
1214o = add o add lo mul 256 hi
1215end
1216end
1217r = fclose int fd
1218end
1219
1220note ---- main ----
1221
1222fn summary to void
1223for k ulong 8
1224r = write 1 thash 0 2
1225wtbl k 4
1226r = write 1 tsp 0 1
1227wnum get cnt k
1228wnl
1229end
1230end
1231
1232fn mode to ulong
1233n = argread 1 arg 0 15
1234if eq n long len telf
1235ok = true
1236for i ulong len telf
1237if ne get arg i get telf i
1238ok = false
1239end
1240end
1241if ok
1242ret 1
1243end
1244end
1245if eq n long len tprc
1246ok = true
1247for i ulong len tprc
1248if ne get arg i get tprc i
1249ok = false
1250end
1251end
1252if ok
1253ret 2
1254end
1255end
1256ret 0
1257end
1258
1259fn main to int
1260if lt argcnt 2
1261r = write 2 tuse 0 len tuse
1262ret 64
1263end
1264m = mode
1265if eq m 0
1266r = write 2 tuse 0 len tuse
1267ret 64
1268end
1269if eq m 1
1270elfmode
1271summary
1272ret 0
1273end
1274n = long len tdpr
1275if ge argcnt 3
1276n = argread 2 root 0 4095
1277if lt n 1
1278r = write 2 tuse 0 len tuse
1279ret 64
1280end
1281else
1282for i ulong len tdpr
1283put root i get tdpr i
1284end
1285end
1286while gt n 1 bound 4095
1287if ne get root sub ulong n 1 47
1288break
1289end
1290n = sub n 1
1291end
1292note PROCROOT + / + a pid of up to 63 digits + /maps must fit the 4096-byte path buffer
1293if gt n 4027
1294r = write 2 tuse 0 len tuse
1295ret 64
1296end
1297put rootn 0 ulong n
1298procmode
1299summary
1300ret 0
1301end