1 ! Copyright (C) 2017 Björn Lindqvist.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.strings assocs byte-arrays
4 classes.struct continuations io io.encodings.binary
5 io.encodings.string io.files kernel math math.bitwise sequences
10 ERROR: bad-magic got expected ;
12 : check-magic ( got expected -- )
13 2dup = [ 2drop ] [ bad-magic ] if ;
15 CONSTANT: HEADER_MAGIC_OLD "ld.so-1.7.0"
16 CONSTANT: HEADER_MAGIC_NEW "glibc-ld.so.cache1.1"
18 TUPLE: ldcache-entry elf? arch osversion hwcap key value ;
32 { stringslen uint32_t }
33 { unused uint32_t[5] } ;
39 { osversion uint32_t }
42 : check-ldcache-magic ( header expected -- )
43 [ magic>> ] dip [ >byte-array ] bi@ check-magic ;
45 : make-string ( string-table i -- str )
46 0 spin [ index-from ] 2keep swapd subseq
47 native-string-encoding decode ;
49 : string-offset ( header-new -- n )
50 nlibs>> EntryNew struct-size * HeaderNew struct-size + ;
52 : subtract-string-offset ( ofs entry-new -- entry-new )
53 over '[ _ - ] change-key swap '[ _ - ] change-value ;
55 : parse-new-entries ( header-new -- seq )
56 [ string-offset ] keep
57 nlibs>> [ EntryNew read-struct ] replicate
58 [ subtract-string-offset ] with map ;
60 : flag>arch ( flag -- arch )
67 : <ldcache-entry> ( string-table entry-new -- entry )
70 flags>> [ 1 mask? ] [ flag>arch ] bi
71 ] [ osversion>> ] [ hwcap>> ] tri
74 [ value>> make-string ] 2tri ldcache-entry boa ;
76 : parse ( -- entries )
77 ! Read the old header and jump past it.
80 [ HEADER_MAGIC_OLD check-ldcache-magic ]
81 [ nlibs>> EntryOld struct-size * seek-relative seek-input ] bi
82 ] [ 2drop HeaderOld struct-size neg seek-relative seek-input ] recover
84 [ HEADER_MAGIC_NEW check-ldcache-magic ] keep
86 [ stringslen>> read ] bi
87 swap [ <ldcache-entry> ] with map ;
89 : search ( entries namespec arch -- entry/f )
90 swap "lib" ".so" surround '[ [ arch>> _ = ] [ key>> _ head? ] bi and ] find nip ;
92 : find-so ( namespec -- so-name/f )
93 [ "/etc/ld.so.cache" binary [ parse ] with-file-reader ] dip
94 cpu search [ key>> ] [ f ] if* ;