Beispiel #1
0
    def _loadElfObjects(self, host_dir, target_dir, elf_error_handler):
        """Scans a host directory recursively and loads all ELF files in it.

        Args:
            host_dir: The host directory to scan.
            target_dir: The path from which host_dir is copied.
            elf_error_handler: A function that takes 2 arguments
                               (target_path, exception). It is called when
                               the parser fails to read an ELF file.

        Returns:
            List of ElfObject.
        """
        objs = []
        for root_dir, file_name in utils.iterate_files(host_dir):
            full_path = os.path.join(root_dir, file_name)
            rel_path = os.path.relpath(full_path, host_dir)
            target_path = path_utils.JoinTargetPath(
                target_dir, *rel_path.split(os.path.sep))
            try:
                elf = elf_parser.ElfParser(full_path)
            except elf_parser.ElfError:
                logging.debug("%s is not an ELF file", target_path)
                continue
            try:
                deps = elf.ListDependencies()
            except elf_parser.ElfError as e:
                elf_error_handler(target_path, e)
                continue
            finally:
                elf.Close()

            logging.info("%s depends on: %s", target_path, ", ".join(deps))
            objs.append(self.ElfObject(target_path, elf.bitness, deps))
        return objs
Beispiel #2
0
def ListGlobalSymbols(archive_path):
    """Lists global symbols in an ELF archive.

    Args:
        archive_path: The path to the archive file.

    Returns:
        A List of strings, the global symbols in the archive.

    Raises:
        ArError if fails to load the archive.
        elf_parser.ElfError if fails to load any library in the archive.
    """
    symbols = []
    for offset in _IterateArchive(archive_path):
        with elf_parser.ElfParser(archive_path, offset) as parser:
            symbols.extend(parser.ListGlobalSymbols())
    return symbols
    def _DiffSymbols(self, dump_path, lib_path):
        """Checks if a library includes all symbols in a dump.

        Args:
            dump_path: The path to the dump file containing list of symbols.
            lib_path: The path to the library.

        Returns:
            A list of strings, the global symbols that are in the dump but not
            in the library.

        Raises:
            IOError if fails to load the dump.
            elf_parser.ElfError if fails to load the library.
        """
        with open(dump_path, "r") as dump_file:
            dump_symbols = set(line.strip() for line in dump_file
                               if line.strip())
        parser = elf_parser.ElfParser(lib_path)
        try:
            lib_symbols = parser.ListGlobalDynamicSymbols(include_weak=True)
        finally:
            parser.Close()
        return sorted(dump_symbols.difference(lib_symbols))
Beispiel #4
0
 def setUp(self):
     """Creates an ElfParser."""
     dir_path = os.path.dirname(os.path.realpath(__file__))
     self.elf_file_path = os.path.join(dir_path, 'elf', 'testing',
                                       'libtest.so')
     self.elf_file = elf.ElfParser(self.elf_file_path)