Dpkg/Shlibs/Objdump.pm

filter: Statementstmt 20.9% 18 / 86 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 38.4% 5 / 13 Podpod 0.0% 0 / 8 totaltotal 14.2% 23 / 162 CC 23 SCAR 45.0
CC
23
Cov
14%
get_format
210.0
locate_symbol
20.0
is_elf
20.0
CRAP 89.9
« Dpkg/Source/Package/V2.pm ↑ Summary Dpkg/Source/Package/V3/Git.pm »
1 # Copyright © 2007-2010 Raphaël Hertzog <hertzog@debian.org>
2 # Copyright © 2007-2009,2012-2015,2017-2018 Guillem Jover <guillem@debian.org>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 Dpkg::Shlibs::Objdump - symbol support via objdump
22  
23 =head1 DESCRIPTION
24  
25 This module provides a class that wraps objdump to handle symbols and
26 their attributes from a shared object.
27  
28 B<Note>: This is a private module, its API can change at any time.
29  
30 =cut
31  
32 package Dpkg::Shlibs::Objdump 0.01;
33  
34 2 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 2 ▶use Dpkg::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use Dpkg::ErrorHandling;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use Dpkg::Shlibs::Objdump::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
41 0 my $this = shift;
42 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
43 0 my $self = {
44 objects => {},
45 };
46 0 bless $self, $class;
47 0 return $self;
48 }
49  
50 0 ▶sub add_object {
Subroutine add_object: not called CC=2 SCAR=17.9
Pod undocumented
51 0 my ($self, $obj) = @_;
52 0 my $id = $obj->get_id;
53 0 ▶ if ($id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 0 0 0
54 0 $self->{objects}{$id} = $obj;
55 }
56 0 return $id;
57 }
58  
59 0 ▶sub analyze {
Subroutine analyze: not called CC=1 SCAR=6.9
Pod undocumented
60 0 my ($self, $file) = @_;
61 0 my $obj = Dpkg::Shlibs::Objdump::Object->new($file);
62  
63 0 return $self->add_object($obj);
64 }
65  
66 0 ▶sub locate_symbol {
Subroutine locate_symbol: not called CC=4 SCAR=30.0
Pod undocumented
67 0 my ($self, $name) = @_;
68 0 foreach my $obj (values %{$self->{objects}}) {
69 0 my $sym = $obj->get_symbol($name);
70 0 ▶ if (defined($sym) && $sym->{defined}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sym and $sym->{"defined"}) 0 0 0
Condition0%0/3
defined $sym and $sym->{"defined"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sym and $sym->{"defined"}
defined $sym $sym->{"defined"}
Truth table0%0/3
A and B
A= defined $symB= $sym->{"defined"}
ABresult
0X0
100
111
71 0 return $sym;
72 }
73 }
74 0 return;
75 }
76  
77 0 ▶sub get_object {
Subroutine get_object: not called CC=2 SCAR=17.9
Pod undocumented
78 0 my ($self, $objid) = @_;
79 0 ▶ if ($self->has_object($objid)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_object($objid)) 0 0 0
80 0 return $self->{objects}{$objid};
81 }
82 0 return;
83 }
84  
85 0 ▶sub has_object {
Subroutine has_object: not called CC=1 SCAR=6.9
Pod undocumented
86 0 my ($self, $objid) = @_;
87 0 return exists $self->{objects}{$objid};
88 }
89  
90 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
91 # ELF Class.
92 2 ELF_BITS_NONE => 0,
93 ELF_BITS_32 => 1,
94 ELF_BITS_64 => 2,
95  
96 # ELF Data encoding.
97 ELF_ORDER_NONE => 0,
98 ELF_ORDER_2LSB => 1,
99 ELF_ORDER_2MSB => 2,
100  
101 # ELF Machine.
102 EM_NONE => 0,
103 EM_SPARC => 2,
104 EM_386 => 3,
105 EM_68K => 4,
106 EM_MIPS => 8,
107 EM_SPARC64_OLD => 11,
108 EM_PARISC => 15,
109 EM_SPARC32PLUS => 18,
110 EM_PPC => 20,
111 EM_PPC64 => 21,
112 EM_S390 => 22,
113 EM_ARM => 40,
114 EM_ALPHA_OLD => 41,
115 EM_SH => 42,
116 EM_SPARC64 => 43,
117 EM_IA64 => 50,
118 EM_X86_64 => 62,
119 EM_OR1K => 92,
120 EM_AARCH64 => 183,
121 EM_ARCV2 => 195,
122 EM_RISCV => 243,
123 EM_LOONGARCH => 258,
124 EM_OR1K_OLD => 0x8472,
125 EM_ALPHA => 0x9026,
126 EM_S390_OLD => 0xa390,
127 EM_NIOS32 => 0xfebb,
128  
129 # ELF Version.
130 EV_NONE => 0,
131 EV_CURRENT => 1,
132  
133 # ELF Flags (might influence the ABI).
134 EF_ARM_ALIGN8 => 0x00000040,
135 EF_ARM_NEW_ABI => 0x00000080,
136 EF_ARM_OLD_ABI => 0x00000100,
137 EF_ARM_SOFT_FLOAT => 0x00000200,
138 EF_ARM_HARD_FLOAT => 0x00000400,
139 EF_ARM_EABI_MASK => 0xff000000,
140  
141 EF_IA64_ABI64 => 0x00000010,
142  
143 EF_LOONGARCH_SOFT_FLOAT => 0x00000001,
144 EF_LOONGARCH_SINGLE_FLOAT => 0x00000002,
145 EF_LOONGARCH_DOUBLE_FLOAT => 0x00000003,
146 EF_LOONGARCH_ABI_MASK => 0x00000007,
147  
148 EF_MIPS_ABI2 => 0x00000020,
149 EF_MIPS_32BIT => 0x00000100,
150 EF_MIPS_FP64 => 0x00000200,
151 EF_MIPS_NAN2008 => 0x00000400,
152 EF_MIPS_ABI_MASK => 0x0000f000,
153 EF_MIPS_ARCH_MASK => 0xf0000000,
154  
155 EF_OR1K_NODELAY => 0x00000001,
156  
157 EF_PPC64_ABI64 => 0x00000003,
158  
159 EF_RISCV_FLOAT_ABI_SOFT => 0x0000,
160 EF_RISCV_FLOAT_ABI_SINGLE => 0x0002,
161 EF_RISCV_FLOAT_ABI_DOUBLE => 0x0004,
162 EF_RISCV_FLOAT_ABI_QUAD => 0x0006,
163 EF_RISCV_FLOAT_ABI_MASK => 0x0006,
164 EF_RISCV_RVE => 0x0008,
165  
166 EF_SH_MACH_MASK => 0x0000001f,
167 2 };
168  
169 # These map machine IDs to their name.
170 2 my %elf_mach_name = (
171 EM_NONE() => 'none',
172 EM_386() => 'i386',
173 EM_68K() => 'm68k',
174 EM_AARCH64() => 'arm64',
175 EM_ALPHA() => 'alpha',
176 EM_ARCV2() => 'arcv2',
177 EM_ARM() => 'arm',
178 EM_IA64() => 'ia64',
179 EM_LOONGARCH() => 'loong',
180 EM_MIPS() => 'mips',
181 EM_NIOS32() => 'nios2',
182 EM_OR1K() => 'or1k',
183 EM_PARISC() => 'hppa',
184 EM_PPC() => 'ppc',
185 EM_PPC64() => 'ppc64',
186 EM_RISCV() => 'riscv',
187 EM_S390() => 's390',
188 EM_SH() => 'sh',
189 EM_SPARC() => 'sparc',
190 EM_SPARC64() => 'sparc64',
191 EM_X86_64() => 'amd64',
192 );
193  
194 # These map alternative or old machine IDs to their canonical form.
195 2 my %elf_mach_map = (
196 EM_ALPHA_OLD() => EM_ALPHA,
197 EM_OR1K_OLD() => EM_OR1K,
198 EM_S390_OLD() => EM_S390,
199 EM_SPARC32PLUS() => EM_SPARC,
200 EM_SPARC64_OLD() => EM_SPARC64,
201 );
202  
203 # These masks will try to expose processor flags that are ABI incompatible,
204 # and as such are part of defining the architecture ABI. If uncertain it is
205 # always better to not mask a flag, because that preserves the historical
206 # behavior, and we do not drop dependencies.
207 2 my %elf_flags_mask = (
208 # XXX: The mask for ARM had to be disabled due to objects in the wild
209 # with EABIv4, while EABIv5 is the current one, and the soft and hard
210 # flags not always being set on armel and armhf respectively, although
211 # the Tag_ABI_VFP_args in the ARM attribute section should always be
212 # present on armhf, and there are even cases where both soft and hard
213 # float flags are set at the same time(!). Once these are confirmed to
214 # be fixed, we could reconsider enabling the below for a more strict
215 # ABI mismatch check. See #853793.
216 # EM_ARM() => EF_ARM_EABI_MASK |
217 # EF_ARM_NEW_ABI | EF_ARM_OLD_ABI |
218 # EF_ARM_SOFT_FLOAT | EF_ARM_HARD_FLOAT,
219 EM_IA64() => EF_IA64_ABI64,
220 EM_LOONGARCH() => EF_LOONGARCH_ABI_MASK,
221 EM_MIPS() => EF_MIPS_ABI_MASK | EF_MIPS_ABI2,
222 EM_OR1K() => EF_OR1K_NODELAY,
223 EM_PPC64() => EF_PPC64_ABI64,
224 EM_RISCV() => EF_RISCV_FLOAT_ABI_MASK | EF_RISCV_RVE,
225 );
226  
227 0 ▶sub get_format {
Subroutine get_format: not called CC=14 SCAR=53.5
Pod undocumented
228 0 my ($file) = @_;
229 0 state %format;
230  
231 0 ▶ return $format{$file} if exists $format{$file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $format{$file} 0 0 0
232  
233 0 my $header;
234  
235 0 ▶ open my $fh, '<', $file or syserr(g_('cannot read %s'), $file);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or syserr(g_("cannot read %s"), $file) 0 0 0
236 0 my $rc = read $fh, $header, 64;
237 0 ▶ if (not defined $rc) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $rc) { } 0 0 0
elsif ($rc != 64) { } 0 0 0
238 0 syserr(g_('cannot read %s'), $file);
239 } elsif ($rc != 64) {
240 0 return;
241 }
242 0 close $fh;
243  
244 0 my %elf;
245  
246 # Unpack the identifier field.
247 0 @elf{qw(magic bits endian vertype osabi verabi)} = unpack 'a4C5', $header;
248  
249 0 ▶ return unless $elf{magic} eq "\x7fELF";
Branch: 1 branch
BranchTrueFalseTotal
unless $elf{"magic"} eq "\177ELF" 0 0 0
250 0 ▶ return unless $elf{vertype} == EV_CURRENT;
Branch: 1 branch
BranchTrueFalseTotal
unless $elf{"vertype"} == 1 0 0 0
251  
252 0 my %abi;
253 0 my ($elf_word, $elf_endian);
254 0 ▶ if ($elf{bits} == ELF_BITS_32) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elf{'bits'} == 1) { } 0 0 0
elsif ($elf{'bits'} == 2) { } 0 0 0
255 0 $abi{bits} = 32;
256 0 $elf_word = 'L';
257 } elsif ($elf{bits} == ELF_BITS_64) {
258 0 $abi{bits} = 64;
259 0 $elf_word = 'Q';
260 } else {
261 0 return;
262 }
263 0 ▶ if ($elf{endian} == ELF_ORDER_2LSB) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elf{'endian'} == 1) { } 0 0 0
elsif ($elf{'endian'} == 2) { } 0 0 0
264 0 $abi{endian} = 'l';
265 0 $elf_endian = '<';
266 } elsif ($elf{endian} == ELF_ORDER_2MSB) {
267 0 $abi{endian} = 'b';
268 0 $elf_endian = '>';
269 } else {
270 0 return;
271 }
272  
273 # Unpack the endianness and size dependent fields.
274 0 my $tmpl = "x16(S2Lx[${elf_word}3]L)${elf_endian}";
275 0 @elf{qw(type mach version flags)} = unpack $tmpl, $header;
276  
277 # Canonicalize the machine ID.
278 0 ▶ $elf{mach} = $elf_mach_map{$elf{mach}} // $elf{mach};
Condition0%0/3
$elf_mach_map{$elf{"mach"}} // $elf{"mach"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$elf_mach_map{$elf{"mach"}} // $elf{"mach"}
$elf_mach_map{$elf{"mach"}} $elf{"mach"}
Truth table0%0/3
A // B
A= $elf_mach_map{$elf{"mach"}}B= $elf{"mach"}
ABresult
1X1
011
000
279 0 ▶ $abi{mach} = $elf_mach_name{$elf{mach}} // $elf{mach};
Condition0%0/3
$elf_mach_name{$elf{"mach"}} // $elf{"mach"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$elf_mach_name{$elf{"mach"}} // $elf{"mach"}
$elf_mach_name{$elf{"mach"}} $elf{"mach"}
Truth table0%0/3
A // B
A= $elf_mach_name{$elf{"mach"}}B= $elf{"mach"}
ABresult
1X1
011
000
280  
281 # Mask any processor flags that might not change the architecture ABI.
282 0 ▶ $abi{flags} = $elf{flags} & ($elf_flags_mask{$elf{mach}} // 0);
Condition0%0/2
$elf_flags_mask{$elf{"mach"}} // 0
l!l
00
MC/DC0%0/1
$elf_flags_mask{$elf{"mach"}} // 0
$elf_flags_mask{$elf{"mach"}}
Truth table0%0/2
A
A= $elf_flags_mask{$elf{"mach"}}
Aresult
11
00
283  
284 # Normalize into a colon-separated string for easy comparison, and easy
285 # debugging aid.
286 0 $format{$file} = join ':', 'ELF', @abi{qw(bits endian mach flags)};
287  
288 0 return $format{$file};
289 }
290  
291 0 ▶sub is_elf {
Subroutine is_elf: not called CC=4 SCAR=30.0
Pod undocumented
292 0 my $file = shift;
293 0 ▶ open(my $file_fh, '<', $file) or syserr(g_('cannot read %s'), $file);
Branch: 1 branch
BranchTrueFalseTotal
open my $file_fh, "<", $file or syserr(g_("cannot read %s"), $file) 0 0 0
294 0 my ($header, $result) = ('', 0);
295 0 ▶ if (read($file_fh, $header, 4) == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (read($file_fh, $header, 4) == 4) 0 0 0
296 0 ▶ $result = 1 if ($header =~ /^\177ELF$/);
Branch: 1 branch
BranchTrueFalseTotal
if $header =~ /^\177ELF$/u 0 0 0
297 }
298 0 close($file_fh);
299 0 return $result;
300 }
301  
302 =head1 CHANGES
303  
304 =head2 Version 0.xx
305  
306 This is a private module.
307  
308 =cut
309  
310 2 1;
« Dpkg/Source/Package/V2.pm ↑ Summary Dpkg/Source/Package/V3/Git.pm »