Ejemplo n.º 1
0
    def test_sorting_by_all_attributes(self) -> None:
        # pylint: disable=line-too-long
        with tests.MockLdd(out=textwrap.dedent('''\
                            \tlinux-vdso.so.1 (0x00007ffd66ce2000)
                            \tlibselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f72b88fc000)
                            \tlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f72b850b000)
                            \tlibpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f72b8299000)
                            \tlibdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f72b8095000)
                            \t/lib64/ld-linux-x86-64.so.2 (0x00007f72b8d46000)
                            \tlibpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f72b7e76000)\n'''
                                               ),
                           out_unused=''):
            # pylint: enable=line-too-long

            for attr in lddwrap.DEPENDENCY_ATTRIBUTES:
                deps = lddwrap.list_dependencies(path=pathlib.Path("/bin/dir"),
                                                 unused=True)

                # pylint: disable=protected-access
                lddwrap._sort_dependencies_in_place(deps=deps, sort_by=attr)

                previous = getattr(deps[0], attr)
                previous = '' if previous is None else str(previous)

                for i in range(1, len(deps)):
                    current = getattr(deps[i], attr)
                    current = '' if current is None else str(current)

                    self.assertLessEqual(
                        previous, current,
                        ("The dependencies must be sorted according to "
                         "attribute {!r}: {!r}").format(attr, deps))
Ejemplo n.º 2
0
    def test_with_fantasy_unused(self):
        """Test against a fantasy executable with fantasy unused."""
        # pylint: disable=line-too-long
        with tests.MockLdd(out=textwrap.dedent('''\
                            \tlinux-vdso.so.1 (0x00007ffd66ce2000)
                            \tlibm.so.6 => /lib64/libm.so.6 (0x00007f72b7e76000)\n'''
                                               ),
                           out_unused=textwrap.dedent('''\
                    Unused direct dependencies:
                    \t/lib64/libm.so.6\n''')):
            # pylint: enable=line-too-long
            deps = lddwrap.list_dependencies(path=pathlib.Path("/bin/dir"),
                                             unused=True)

            unused = [dep for dep in deps if dep.unused]

            expected_unused = [
                lddwrap.Dependency(soname="libm.so.6",
                                   path=pathlib.Path("/lib64/libm.so.6"),
                                   found=True,
                                   mem_address="0x00007f72b7e76000",
                                   unused=True)
            ]

            self.assertEqual(len(expected_unused), len(unused))

            for i, (dep, exp_dep) in enumerate(zip(unused, expected_unused)):
                self.assertListEqual(
                    [], diff_dependencies(ours=dep, theirs=exp_dep),
                    "Mismatch at the unused dependency {}".format(i))
Ejemplo n.º 3
0
def linux_executable_copy_actions(
        source_path: pathlib.Path,
        reference_path: pathlib.Path,
) -> typing.Set[FileCopyAction]:
    actions = {
        FileCopyAction.from_path(
            source=source_path,
            root=reference_path,
        ),
        *(
            FileCopyAction.from_path(
                source=path,
                root=reference_path,
            )
            for path in filtered_relative_to(
                base=reference_path,
                paths=(
                    dependency.path
                    for dependency in lddwrap.list_dependencies(
                        path=source_path,
                    )
                    if dependency.path is not None
                ),
            )
        ),
    }

    return actions
Ejemplo n.º 4
0
    def test_pwd(self):
        path = pathlib.Path("/bin/pwd")
        deps = lddwrap.list_dependencies(path=path)

        expected_deps = [
            lddwrap.Dependency(soname="linux-vdso.so.1",
                               path=None,
                               found=True,
                               mem_address="",
                               unused=None),
            lddwrap.Dependency(
                soname="libc.so.6",
                path=pathlib.Path("/lib/x86_64-linux-gnu/libc.so.6"),
                found=True,
                mem_address="",
                unused=None),
            lddwrap.Dependency(
                soname=None,
                path=pathlib.Path("/lib64/ld-linux-x86-64.so.2"),
                found=True,
                mem_address="",
                unused=None)
        ]

        for dep in deps:
            for exp_dep in expected_deps:
                if exp_dep.soname == dep.soname:
                    self.assertTrue(
                        dependencies_equal(dep, exp_dep),
                        "Incorrect dependency, expected {}\ngot {}".format(
                            exp_dep, dep))
                    break
Ejemplo n.º 5
0
def setup_fips(**kwargs):
    deps = lddwrap.list_dependencies(path=Path(ssl._ssl.__file__))
    libcrypto = [
        x.soname for x in deps if x.soname and "libcrypto" in x.soname
    ]
    libcrypto = ctypes.CDLL(libcrypto[0])
    if not libcrypto.FIPS_mode_set(1):
        raise ssl.SSLError("FIPS mode failed to initialize")
Ejemplo n.º 6
0
def linuxdeployqt_substitute_list_source(
    target, ) -> typing.List[pathlib.Path]:
    paths = [
        dependency.path
        for dependency in lddwrap.list_dependencies(path=target, )
        if dependency.path is not None
    ]

    return paths
Ejemplo n.º 7
0
def linux_collect_dependencies(
    source_base: pathlib.Path,
    target: pathlib.Path,
) -> typing.Generator[pathlib.Path, None, None]:
    yield from filtered_relative_to(
        base=source_base,
        paths=(dependency.path.resolve()
               for dependency in lddwrap.list_dependencies(path=target)
               if dependency.path is not None),
    )
Ejemplo n.º 8
0
def _main(args: Args, stream: TextIO) -> int:
    """Execute the main routine."""
    # pylint: disable=protected-access
    deps = lddwrap.list_dependencies(path=args.path, unused=True)

    if args.format == 'verbose':
        lddwrap._output_verbose(deps=deps, stream=stream)
    elif args.format == 'json':
        lddwrap._output_json(deps=deps, stream=stream)
    else:
        raise NotImplementedError("Unhandled format: {}".format(args.format))
    stream.write('\n')

    return 0
Ejemplo n.º 9
0
def linuxdeployqt_substitute_list_source(
    target,
    translation_path,
) -> typing.List[pathlib.Path]:
    paths = [
        dependency.path
        for dependency in lddwrap.list_dependencies(path=target, )
        if dependency.path is not None
    ]

    if any('libicu' in path.name for path in paths):
        paths.extend(translation_path.glob('*.qm'))

    return paths
Ejemplo n.º 10
0
def linuxdeployqt_substitute_list_source(
    target, ) -> typing.List[pathlib.Path]:
    try:
        paths = [
            dependency.path
            for dependency in lddwrap.list_dependencies(path=target, )
            if dependency.path is not None
        ]
    except RuntimeError as e:
        if "Failed to ldd external" not in str(e):
            raise

        paths = []

    return paths
Ejemplo n.º 11
0
    def test_with_static_library(self) -> None:
        """Test against a fantasy static library."""
        with tempfile.TemporaryDirectory() as tmp_dir:
            lib_pth = pathlib.Path(tmp_dir) / "my_static_lib.so"
            lib_pth.write_text("totally static!")

            with tests.MockLdd(out=textwrap.dedent('''\
                                my_static_lib.so:
                                \tstatically linked\n'''),
                               out_unused=''):
                # pylint: enable=line-too-long
                deps = lddwrap.list_dependencies(path=lib_pth, unused=True)

                # The dependencies are empty since the library is
                # statically linked.
                self.assertListEqual([], deps)
Ejemplo n.º 12
0
def find_libc_ldd():
    """Find libc path with ldd utility"""
    logging.debug("Finding libc path: ldd")
    # first get ld path
    ld_path = shutil.which("ld")
    if not ld_path:
        raise FileNotFoundError("Failed to locate ld executable")
    # find libc
    libc_possibles = [
        dep.path for dep in lddwrap.list_dependencies(Path(ld_path))
        if dep.soname is not None and dep.soname.startswith("libc.so")
    ]
    if not libc_possibles:
        raise FileNotFoundError("Failed to find libc")
    if len(libc_possibles) > 1:
        raise FileNotFoundError("Found multiple libc")
    return libc_possibles[0]
def main(library_name):

    # first write the library for libabigail (or the main one)
    library = write_library(library_name)
    dependencies = {}

    # Get the dependencies for it
    path = pathlib.Path(library_name)
    deps = lddwrap.list_dependencies(path=path, env={"LD_LIBRARY_PATH": "/usr/local/lib"})
    for dep in deps:
        if not dep.path:
            continue
        dependencies[str(dep.path)] = write_library(str(dep.path))

    # Create list of what libabigail needs
    # probably a bug, some of these are strings
    needs = [x for x in library['function-decl'] if not isinstance(x, str) and ("abigail" not in x.get('@elf-symbol-id', "") and "abigail" not in x.get("@mangled-name", "") and "abigail" not in x.get("@filepath", ""))]
    mangled_names = [x['@mangled-name'] for x in needs]    
Ejemplo n.º 14
0
    def test_bin_dir_with_empty_unused(self):
        # pylint: disable=line-too-long
        with tests.MockLdd(out=textwrap.dedent('''\
                            \tlinux-vdso.so.1 (0x00007ffd66ce2000)
                            \tlibselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f72b88fc000)
                            \tlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f72b850b000)
                            \tlibpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f72b8299000)
                            \tlibdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f72b8095000)
                            \t/lib64/ld-linux-x86-64.so.2 (0x00007f72b8d46000)
                            \tlibpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f72b7e76000)\n'''
                                               ),
                           out_unused=''):
            # pylint: enable=line-too-long
            deps = lddwrap.list_dependencies(path=pathlib.Path("/bin/dir"),
                                             unused=True)

            unused = [dep for dep in deps if dep.unused]
            self.assertListEqual([], unused)
Ejemplo n.º 15
0
    def test_pwd(self):
        """Test parsing the captured output  of ``ldd`` on ``/bin/pwd``."""

        with tests.MockLdd(out=textwrap.dedent('''\
            \tlinux-vdso.so.1 (0x00007ffe0953f000)
            \tlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd548353000)
            \t/lib64/ld-linux-x86-64.so.2 (0x00007fd54894d000)\n'''),
                           out_unused=''):
            deps = lddwrap.list_dependencies(path=pathlib.Path('/bin/pwd'),
                                             unused=False)

            expected_deps = [
                lddwrap.Dependency(soname="linux-vdso.so.1",
                                   path=None,
                                   found=True,
                                   mem_address="0x00007ffe0953f000",
                                   unused=None),
                lddwrap.Dependency(
                    soname='libc.so.6',
                    path=pathlib.Path("/lib/x86_64-linux-gnu/libc.so.6"),
                    found=True,
                    mem_address="0x00007fd548353000",
                    unused=None),
                lddwrap.Dependency(
                    soname=None,
                    path=pathlib.Path("/lib64/ld-linux-x86-64.so.2"),
                    found=True,
                    mem_address="0x00007fd54894d000",
                    unused=None)
            ]

            self.assertEqual(len(expected_deps), len(deps))

            for i, (dep, expected_dep) in enumerate(zip(deps, expected_deps)):
                self.assertListEqual([],
                                     diff_dependencies(ours=dep,
                                                       theirs=expected_dep),
                                     "Mismatch at the dependency {}".format(i))
Ejemplo n.º 16
0
    def test_bin_dir(self):
        """Test parsing the captured output  of ``ldd`` on ``/bin/dir``."""

        # pylint: disable=line-too-long
        with tests.MockLdd(out=textwrap.dedent('''\
                    \tlinux-vdso.so.1 (0x00007ffd66ce2000)
                    \tlibselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f72b88fc000)
                    \tlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f72b850b000)
                    \tlibpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f72b8299000)
                    \tlibdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f72b8095000)
                    \t/lib64/ld-linux-x86-64.so.2 (0x00007f72b8d46000)
                    \tlibpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f72b7e76000)\n'''
                                               ),
                           out_unused=''):
            # pylint: enable=line-too-long
            deps = lddwrap.list_dependencies(path=pathlib.Path('/bin/dir'),
                                             unused=False)

            expected_deps = [
                lddwrap.Dependency(soname="linux-vdso.so.1",
                                   path=None,
                                   found=True,
                                   mem_address="0x00007ffd66ce2000",
                                   unused=None),
                lddwrap.Dependency(
                    soname="libselinux.so.1",
                    path=pathlib.Path("/lib/x86_64-linux-gnu/libselinux.so.1"),
                    found=True,
                    mem_address="0x00007f72b88fc000",
                    unused=None),
                lddwrap.Dependency(
                    soname="libc.so.6",
                    path=pathlib.Path("/lib/x86_64-linux-gnu/libc.so.6"),
                    found=True,
                    mem_address="0x00007f72b850b000",
                    unused=None),
                lddwrap.Dependency(
                    soname="libpcre.so.3",
                    path=pathlib.Path("/lib/x86_64-linux-gnu/libpcre.so.3"),
                    found=True,
                    mem_address="0x00007f72b8299000",
                    unused=None),
                lddwrap.Dependency(
                    soname="libdl.so.2",
                    path=pathlib.Path("/lib/x86_64-linux-gnu/libdl.so.2"),
                    found=True,
                    mem_address="0x00007f72b8095000",
                    unused=None),
                lddwrap.Dependency(
                    soname=None,
                    path=pathlib.Path("/lib64/ld-linux-x86-64.so.2"),
                    found=True,
                    mem_address="0x00007f72b8d46000",
                    unused=None),
                lddwrap.Dependency(
                    soname="libpthread.so.0",
                    path=pathlib.Path("/lib/x86_64-linux-gnu/libpthread.so.0"),
                    found=True,
                    mem_address="0x00007f72b7e76000",
                    unused=None),
            ]

            self.assertEqual(len(expected_deps), len(deps))

            for i, (dep, expected_dep) in enumerate(zip(deps, expected_deps)):
                self.assertListEqual([],
                                     diff_dependencies(ours=dep,
                                                       theirs=expected_dep),
                                     "Mismatch at the dependency {}".format(i))