commit:     320b76139ae04eea3cbe74c424cda489bbf61882
Author:     Mike Frysinger <vapier <AT> chromium <DOT> org>
AuthorDate: Thu Jan 28 22:38:43 2016 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Thu Jan 28 22:38:43 2016 +0000
URL:        https://gitweb.gentoo.org/proj/pax-utils.git/commit/?id=320b7613

lddtree: handle exceptions thrown when parsing other ELFs

We have a top level exception handler for catching ELF errors, but if we
hit a problem with a dependency, we end up diagnosing it as a problem in
the first ELF.  e.g. If "foo" is linked against "libc.so", and "libc.so"
is a linker script, when we try to read the libc.so ELF, it fails, and
we end up saying:
  lddtree: warning: foo: Magic number does not match

This makes no sense because "foo" is a proper ELF with the right magic,
and we end up halting the whole parsing process.  Now we show:
  lddtree: warning: /usr/lib/libc.so: Magic number does not match
  foo (interpreter => /some/interp)
      libc.so => None

 lddtree.py | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/lddtree.py b/lddtree.py
index 100f475..e663d32 100755
--- a/lddtree.py
+++ b/lddtree.py
@@ -338,9 +338,12 @@ def FindLib(elf, lib, ldpaths, root='/', debug=False):
 
     if os.path.exists(target):
       with open(target, 'rb') as f:
-        libelf = ELFFile(f)
-        if CompatibleELFs(elf, libelf):
-          return (target, path)
+        try:
+          libelf = ELFFile(f)
+          if CompatibleELFs(elf, libelf):
+            return (target, path)
+        except exceptions.ELFError as e:
+          warn('%s: %s' % (target, e))
 
   return (None, None)
 
@@ -475,8 +478,11 @@ def ParseELF(path, root='/', prefix='', 
ldpaths={'conf':[], 'env':[], 'interp':[
           'needed': [],
       }
       if fullpath:
-        lret = ParseELF(realpath, root, prefix, ldpaths, display=fullpath,
-                        debug=debug, _first=False, _all_libs=_all_libs)
+        try:
+          lret = ParseELF(realpath, root, prefix, ldpaths, display=fullpath,
+                          debug=debug, _first=False, _all_libs=_all_libs)
+        except exceptions.ELFError as e:
+          warn('%s: %s' % (realpath, e))
         _all_libs[lib]['needed'] = lret['needed']
 
     del elf

Reply via email to