scripts/ldd.py: improve caching to avoid needless recursion