Exemplo n.º 1
0
 def test_get_elf_files_to_patch(self):
     elf_files = elf.get_elf_files(
         self.fake_elf.root_path,
         {'libc.so.6', 'libssl.so.1.0.0', 'fake_elf-shared-object',
          'fake_elf-2.26'})
     to_patch = elf.get_elf_files_to_patch(elf_files)
     self.assertThat({os.path.basename(e.path) for e in to_patch},
                     Equals({'fake_elf-shared-object', 'fake_elf-2.26'}))
Exemplo n.º 2
0
    def _patch(self, dynamic_linker: str) -> None:
        preferred_patchelf_path = self._get_preferred_patchelf_path()

        elf_patcher = elf.Patcher(
            dynamic_linker=dynamic_linker,
            root_path=self._primedir,
            preferred_patchelf_path=preferred_patchelf_path)
        files_to_patch = elf.get_elf_files_to_patch(self._elf_files)
        for elf_file in files_to_patch:
            try:
                elf_patcher.patch(elf_file=elf_file)
            except errors.PatcherError as patch_error:
                logger.warning(
                    'An attempt to patch {!r} so that it would work '
                    'correctly in diverse environments was made and failed. '
                    'To disable this behavior set '
                    '`build-attributes: [no-patchelf]` for the part.'.format(
                        elf_file.path))
                if not self._is_go_based_plugin:
                    raise patch_error
Exemplo n.º 3
0
    def prime(self, force=False) -> None:  # noqa: C901
        self.makedirs()
        self.notify_part_progress('Priming')
        snap_files, snap_dirs = self.migratable_fileset_for('prime')
        _migrate_files(snap_files, snap_dirs, self.stagedir, self.primedir)

        elf_files = elf.get_elf_files(self.primedir, snap_files)
        all_dependencies = set()
        # TODO: base snap support
        core_path = common.get_core_path()

        # Clear the cache of all libs that aren't already in the primedir
        self._soname_cache.reset_except_root(self.primedir)
        for elf_file in elf_files:
            all_dependencies.update(
                elf_file.load_dependencies(root_path=self.primedir,
                                           core_base_path=core_path,
                                           soname_cache=self._soname_cache))

        dependency_paths = self._handle_dependencies(all_dependencies)

        if not self._build_attributes.keep_execstack():
            clear_execstack(elf_files=elf_files)

        # TODO revisit if we need to support variations and permutations
        #  of this
        staged_patchelf_path = os.path.join(self.stagedir, 'bin', 'patchelf')
        if not os.path.exists(staged_patchelf_path):
            staged_patchelf_path = None
        # We need to verify now that the GLIBC version would be compatible
        # with that of the base.
        # TODO the linker version depends on the chosen base, but that
        # base may not be installed so we cannot depend on
        # get_core_dynamic_linker to resolve the final path for which
        # we resort to our only working base 16, ld-2.23.so.
        linker_incompat = dict()  # type: Dict[str, str]
        for elf_file in elf_files:
            if not elf_file.is_linker_compatible(linker='ld-2.23.so'):
                linker_incompat[elf_file.path] = elf_file.get_required_glibc()
        # If libc6 is staged, to avoid symbol mixups we will resort to
        # glibc mangling.
        libc6_staged = 'libc6' in self._part_properties.get(
            'stage-packages', [])
        is_classic = self._confinement == 'classic'
        # classic confined snaps built on anything but a host supporting the
        # the target base will require glibc mangling.
        classic_mangling_needed = (
            is_classic
            and not self._project_options.is_host_compatible_with_base)
        if linker_incompat:
            formatted_items = [
                '- {} (requires GLIBC {})'.format(k, v)
                for k, v in linker_incompat.items()
            ]
            logger.warning(
                'The GLIBC version of the targeted core is 2.23. A newer '
                'libc will be required for the following files:\n{}'.format(
                    '\n'.join(formatted_items)))

        dynamic_linker = None
        if linker_incompat or libc6_staged or classic_mangling_needed:
            if not libc6_staged:
                raise errors.StagePackageMissingError(package='libc6')
            dynamic_linker = elf.find_linker(
                root_path=self.primedir, snap_base_path=self._snap_base_path)
        elif is_classic:
            dynamic_linker = self._project_options.get_core_dynamic_linker()

        if dynamic_linker and not self._build_attributes.no_patchelf():
            logger.warning(
                'Files in this part are going to be patched to execute '
                'correctly on diverse environments.\n'
                'To disable this behavior set '
                '`build-attributes: [no-patchelf]` for the part.')
            elf_patcher = elf.Patcher(
                dynamic_linker=dynamic_linker,
                root_path=self.primedir,
                preferred_patchelf_path=staged_patchelf_path)
            files_to_patch = elf.get_elf_files_to_patch(elf_files)
            for elf_file in files_to_patch:
                elf_patcher.patch(elf_file=elf_file)
        elif dynamic_linker and self._build_attributes.no_patchelf():
            logger.warning(
                'The following files are not going to be patched to work '
                'correctly in the environment as '
                '`build-attributes: [no-patchelf]` is set for the '
                'part:\n{}'.format(''.join(
                    ['- {}\n'.format(e.path) for e in elf_files])))

        self.mark_prime_done(snap_files, snap_dirs, dependency_paths)