File: | Dpkg/Shlibs/Objdump.pm |
Coverage: | 17.7% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
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 - 30 | =head1 NAME Dpkg::Shlibs::Objdump - symbol support via objdump =head1 DESCRIPTION This module provides a class that wraps objdump to handle symbols and their attributes from a shared object. B<Note>: This is a private module, its API can change at any time. =cut | ||||||
31 | |||||||
32 | package Dpkg::Shlibs::Objdump 0.01; | ||||||
33 | |||||||
34 | 6 6 6 | 100 54 146 | use strict; | ||||
35 | 6 6 6 | 12 2 244 | use warnings; | ||||
36 | 6 6 6 | 16 6 272 | use feature qw(state); | ||||
37 | |||||||
38 | 6 6 6 | 8 6 236 | use Dpkg::Gettext; | ||||
39 | 6 6 6 | 20 4 204 | use Dpkg::ErrorHandling; | ||||
40 | 6 6 6 | 5018 10 1772 | use Dpkg::Shlibs::Objdump::Object; | ||||
41 | |||||||
42 | sub new { | ||||||
43 | 0 | 0 | my $this = shift; | ||||
44 | 0 | my $class = ref($this) || $this; | |||||
45 | 0 | my $self = { objects => {} }; | |||||
46 | 0 | bless $self, $class; | |||||
47 | 0 | return $self; | |||||
48 | } | ||||||
49 | |||||||
50 | sub add_object { | ||||||
51 | 0 | 0 | my ($self, $obj) = @_; | ||||
52 | 0 | my $id = $obj->get_id; | |||||
53 | 0 | if ($id) { | |||||
54 | 0 | $self->{objects}{$id} = $obj; | |||||
55 | } | ||||||
56 | 0 | return $id; | |||||
57 | } | ||||||
58 | |||||||
59 | sub analyze { | ||||||
60 | 0 | 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 | sub locate_symbol { | ||||||
67 | 0 | 0 | my ($self, $name) = @_; | ||||
68 | 0 0 | foreach my $obj (values %{$self->{objects}}) { | |||||
69 | 0 | my $sym = $obj->get_symbol($name); | |||||
70 | 0 | if (defined($sym) && $sym->{defined}) { | |||||
71 | 0 | return $sym; | |||||
72 | } | ||||||
73 | } | ||||||
74 | 0 | return; | |||||
75 | } | ||||||
76 | |||||||
77 | sub get_object { | ||||||
78 | 0 | 0 | my ($self, $objid) = @_; | ||||
79 | 0 | if ($self->has_object($objid)) { | |||||
80 | 0 | return $self->{objects}{$objid}; | |||||
81 | } | ||||||
82 | 0 | return; | |||||
83 | } | ||||||
84 | |||||||
85 | sub has_object { | ||||||
86 | 0 | 0 | my ($self, $objid) = @_; | ||||
87 | 0 | return exists $self->{objects}{$objid}; | |||||
88 | } | ||||||
89 | |||||||
90 | use constant { | ||||||
91 | # ELF Class. | ||||||
92 | 6 | 3738 | 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 | 6 6 | 30 6 | }; | ||||
168 | |||||||
169 | # These map machine IDs to their name. | ||||||
170 | 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 | 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 | 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 | sub get_format { | ||||||
228 | 0 | 0 | my ($file) = @_; | ||||
229 | 0 | state %format; | |||||
230 | |||||||
231 | 0 | return $format{$file} if exists $format{$file}; | |||||
232 | |||||||
233 | 0 | my $header; | |||||
234 | |||||||
235 | 0 | open my $fh, '<', $file or syserr(g_('cannot read %s'), $file); | |||||
236 | 0 | my $rc = read $fh, $header, 64; | |||||
237 | 0 | if (not defined $rc) { | |||||
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"; | |||||
250 | 0 | return unless $elf{vertype} == EV_CURRENT; | |||||
251 | |||||||
252 | 0 | my %abi; | |||||
253 | 0 | my ($elf_word, $elf_endian); | |||||
254 | 0 | if ($elf{bits} == ELF_BITS_32) { | |||||
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) { | |||||
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}; | |||||
279 | 0 | $abi{mach} = $elf_mach_name{$elf{mach}} // $elf{mach}; | |||||
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); | |||||
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 | sub is_elf { | ||||||
292 | 0 | 0 | my $file = shift; | ||||
293 | 0 | open(my $file_fh, '<', $file) or syserr(g_('cannot read %s'), $file); | |||||
294 | 0 | my ($header, $result) = ('', 0); | |||||
295 | 0 | if (read($file_fh, $header, 4) == 4) { | |||||
296 | 0 | $result = 1 if ($header =~ /^\177ELF$/); | |||||
297 | } | ||||||
298 | 0 | close($file_fh); | |||||
299 | 0 | return $result; | |||||
300 | } | ||||||
301 | |||||||
302 - 308 | =head1 CHANGES =head2 Version 0.xx This is a private module. =cut | ||||||
309 | |||||||
310 | 1; |