File: Static.pm

package info (click to toggle)
lintian 2.129.0
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 71,680 kB
  • sloc: perl: 46,081; javascript: 9,151; makefile: 4,058; sh: 3,044; ansic: 714; xml: 518; python: 91; java: 15; cpp: 9; tcl: 4; lisp: 3
file content (121 lines) | stat: -rw-r--r-- 3,787 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# libraries/static -- lintian check script -*- perl -*-

# Copyright (C) 1998 Christian Schwarz and Richard Braakman
# Copyright (C) 2012 Kees Cook
# Copyright (C) 2017-2020 Chris Lamb <lamby@debian.org>
# Copyright (C) 2021 Felix Lechner
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, you can find it on the World Wide
# Web at https://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

package Lintian::Check::Libraries::Static;

use v5.20;
use warnings;
use utf8;

use Const::Fast;
use List::Compare;
use List::SomeUtils qw(any none uniq);

use Moo;
use namespace::clean;

with 'Lintian::Check';

const my $SPACE => q{ };
const my $LEFT_PARENTHESIS => q{(};
const my $RIGHT_PARENTHESIS => q{)};

sub visit_installed_files {
    my ($self, $item) = @_;

    return
      unless $item->is_file;

    return
      unless $item->file_type =~ m{ \b current [ ] ar [ ] archive \b }x;

    my @unstripped_members;
    my %stripped_sections_by_member;

    for my $member_name (keys %{$item->elf_by_member}) {

        my $member_elf = $item->elf_by_member->{$member_name};

        my @elf_sections = values %{$member_elf->{'SECTION-HEADERS'}};
        my @have_section_names = map { $_->name } @elf_sections;

        # These are the ones file(1) looks for.  The ".zdebug_info" being the
        # compressed version of .debug_info.
        # - Technically, file(1) also looks for .symtab, but that is apparently
        #   not strippable for static libs.  Accordingly, it is omitted below.
        my @KNOWN_DEBUG_SECTION_NAMES = qw{.debug_info .zdebug_info};
        my $lc_debug = List::Compare->new(\@have_section_names,
            \@KNOWN_DEBUG_SECTION_NAMES);

        my @have_debug_sections = $lc_debug->get_intersection;

        if (@have_debug_sections) {

            push(@unstripped_members, $member_name);
            next;
        }

        my @KNOWN_STRIPPED_SECTION_NAMES = qw{.note .comment};
        my $lc_stripped = List::Compare->new(\@have_section_names,
            \@KNOWN_STRIPPED_SECTION_NAMES);

        my @have_stripped_sections = $lc_stripped->get_intersection;

        $stripped_sections_by_member{$member_name} //= [];
        push(
            @{$stripped_sections_by_member{$member_name}},
            @have_stripped_sections
        );
    }

    $self->pointed_hint('unstripped-static-library', $item->pointer,
        $LEFT_PARENTHESIS
          . join($SPACE, sort +uniq @unstripped_members)
          . $RIGHT_PARENTHESIS)
      if @unstripped_members
      && $item->name !~ m{ _g [.]a $}x;

    # "libfoo_g.a" is usually a "debug" library, so ignore
    # unneeded sections in those.
    for my $member (keys %stripped_sections_by_member) {

        $self->pointed_hint(
            'static-library-has-unneeded-sections',
            $item->pointer,
            "($member)",
            join($SPACE, sort +uniq @{$stripped_sections_by_member{$member}})
          )
          if @{$stripped_sections_by_member{$member}}
          && $item->name !~ m{ _g [.]a $}x;
    }

    return;
}

1;

# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 sts=4 sr et