diff options
Diffstat (limited to 'misc/ldd/ldd.py')
-rwxr-xr-x | misc/ldd/ldd.py | 62 |
1 files changed, 44 insertions, 18 deletions
diff --git a/misc/ldd/ldd.py b/misc/ldd/ldd.py index afb03b6..4d6f500 100755 --- a/misc/ldd/ldd.py +++ b/misc/ldd/ldd.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -import sys +import os, sys import re, glob from optparse import OptionParser @@ -9,6 +9,7 @@ from elftools.common.exceptions import ELFError from elftools.common.py3compat import bytes2str from elftools.elf.elffile import ELFFile from elftools.elf.dynamic import DynamicSection +from elftools.elf.descriptions import describe_ei_class class ReadElf(object): def __init__(self, file): @@ -16,19 +17,31 @@ class ReadElf(object): """ self.elffile = ELFFile(file) - def display_dynamic_dt_needed(self): - """ Display the dynamic DT_NEEDED contained in the file + + def elf_class(self): + """ Return the ELF Class + """ + header = self.elffile.header + e_ident = header['e_ident'] + return describe_ei_class(e_ident['EI_CLASS']) + + def dynamic_dt_needed(self): + """ Return a list of the DT_NEEDED """ + dt_needed = [] for section in self.elffile.iter_sections(): if not isinstance(section, DynamicSection): continue for tag in section.iter_tags(): if tag.entry.d_tag == 'DT_NEEDED': - sys.stdout.write('\t%s\n' % bytes2str(tag.needed) ) + dt_needed.append(bytes2str(tag.needed)) + #sys.stdout.write('\t%s\n' % bytes2str(tag.needed) ) + + return dt_needed -def ldpath(ld_so_conf='/etc/ld.so.conf'): +def ldpaths(ld_so_conf='/etc/ld.so.conf'): """ Generate paths to search for libraries from ld.so.conf. Recursively parse included files. We assume correct syntax and the ld.so.cache is in sync with ld.so.conf. @@ -38,25 +51,33 @@ def ldpath(ld_so_conf='/etc/ld.so.conf'): lines = re.sub('#.*', '', lines) # kill comments lines = list(re.split(':+|\s+|\t+|\n+|,+', lines)) # man 8 ldconfig + paths = [] include_globs = [] for l in lines: if l == '': - lines.remove('') + continue if l == 'include': f = lines[lines.index(l) + 1] - lines.remove(l) - lines.remove(f) include_globs.append(f) + continue + if l not in include_globs: + paths.append(os.path.realpath(l)) include_files = [] for g in include_globs: include_files = include_files + glob.glob('/etc/' + g) for c in include_files: - lines = lines + ldpath(c) + paths = paths + ldpaths(os.path.realpath(c)) - return list(set(lines)) + return list(set(paths)) +def dynamic_dt_needed_paths( dt_needed, eclass, paths): + for n in dt_needed: + for p in paths: + print('%s' % p + '/' + n) + return + SCRIPT_DESCRIPTION = 'Print shared library dependencies' VERSION_STRING = '%%prog: based on pyelftools %s' % __version__ @@ -72,9 +93,13 @@ def main(): help='Display this information') options, args = optparser.parse_args() - #if options.help or len(args) == 0: - #optparser.print_help() - #sys.exit(0) + if options.help or len(args) == 0: + optparser.print_help() + sys.exit(0) + + paths = ldpaths() + print(paths) + sys.exit(0) for f in args: with open(f, 'rb') as file: @@ -82,14 +107,15 @@ def main(): readelf = ReadElf(file) if len(args) > 1: sys.stdout.write('%s : \n' % f) - readelf.display_dynamic_dt_needed() + eclass = readelf.elf_class() + #sys.stdout.write('\t%s\n' % eclass) + dt_needed = readelf.dynamic_dt_needed() + dt_needed_paths = dynamic_dt_needed_paths( dt_needed, eclass, paths) + for n in dt_needed: + sys.stdout.write('\t%s\n' % n ) except ELFError as ex: sys.stderr.write('ELF error: %s\n' % ex) sys.exit(1) - lines = ldpath() - print(lines) - - if __name__ == '__main__': main() |