lib/ELF/Extract/Sections.pm

filter: Statementstmt 73.9% 91 / 123 Branchbran 50.0% 8 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 20 / 24 Podpod 50.0% 1 / 2 totaltotal 72.7% 120 / 165 CC 9 SCAR 7.3
CC
9
Cov
71%
sorted_sections
6.0
_scan_with_size
6.0
BUILD
2.1
CRAP 2.1
« lib/ELF/Extract/Sections/Scanner/Objdump.pm ↑ Summary lib/ELF/Extract/Sections/Meta/Scanner.pm »
1 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package ELF::Extract::Sections;
6  
7 # ABSTRACT: Extract Raw Chunks of data from identifiable ELF Sections
8  
9 2 our $VERSION = '1.001004'; # TRIAL
10  
11 2 our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12  
13 2 ▶use Moose qw( with has );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 with 'MooseX::Log::Log4perl';
16  
17 2 ▶use MooseX::Has::Sugar 0.0300;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use MooseX::Types::Moose ( ':all', );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use MooseX::Types::Path::Tiny ( 'File', );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use ELF::Extract::Sections::Meta::Types ( ':all', );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Module::Runtime ( 'require_module', );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use MooseX::Params::Validate (qw( validated_list pos_validated_list ));
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 require ELF::Extract::Sections::Section;
25  
26  
27  
28  
29  
30  
31  
32 2 has 'file' => ( isa => File, ro, required, coerce, );
33  
34  
35  
36  
37  
38  
39  
40 2 has 'sections' => ( isa => HashRef [ElfSection], ro, lazy_build, );
41  
42  
43  
44  
45  
46  
47  
48 2 has 'scanner' => ( isa => Str, ro, default => 'Objdump', );
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66 4 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.6
Pod undocumented
67 4 my ( $self, ) = @_;
68 4 ▶ if ( not $self->file->stat ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->file->stat) 0 4 4
69 0 $self->log->logconfess(q{File Specifed could not be found.});
70 }
71 4 return;
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 0 ▶sub sorted_sections {
Subroutine sorted_sections: not called CC=2 SCAR=17.9
Pod documented
107 0 my ( $self, $field, $descending ) = validated_list(
108 \@_,
109 'field' => { isa => FilterField, optional => 1 },
110 'descending' => { isa => Bool, optional => 1 },
111 );
112 0 my $m = 1;
113 0 ▶ $m = 0 - 1 if ($descending);
Branch: 1 branch
BranchTrueFalseTotal
if $descending 0 0 0
114 0 return [ sort { $m * ( $a->compare( other => $b, field => $field ) ) } values %{ $self->sections } ];
115 }
116  
117 4 ▶sub _build_sections {
Subroutine _build_sections: called CC=2 SCAR=7.4
118 4 my ($self) = @_;
119 4 $self->log->debug('Building Section List');
120 4 ▶ if ( $self->_scanner_instance->can_compute_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_scanner_instance->can_compute_size) { } 0 4 4
121 0 return $self->_scan_with_size;
122 }
123 else {
124 4 return $self->_scan_guess_size;
125 }
126 }
127  
128 2 has '_scanner_package' => ( isa => ClassName, ro, lazy_build, );
129  
130 2 has '_scanner_instance' => ( isa => Object, ro, lazy_build, );
131  
132 2 __PACKAGE__->meta->make_immutable;
133 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
134  
135 0 ▶sub _error_scanner_missing {
Subroutine _error_scanner_missing: not called CC=1 SCAR=6.9
136 0 my ( $self, @args ) = @_;
137 0 my ( $scanner, $package, $error ) = pos_validated_list(
138 \@args,
139 { isa => Str, }, #
140 { isa => Str, }, #
141 { isa => Str, }, #
142 );
143 0 my $message = sprintf qq[The Scanner %s could not be found as %s\n.], $scanner, $package;
144 0 $message .= '>' . $error;
145 0 $self->log->logconfess($message);
146 0 return;
147 }
148  
149 4 ▶sub _build__scanner_package {
Subroutine _build__scanner_package: called CC=2 SCAR=7.1
150 4 my ($self) = @_;
151 4 my $pkg = 'ELF::Extract::Sections::Scanner::' . $self->scanner;
152 4 local $@ = undef;
153 4 ▶ if ( not eval { require_module($pkg); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { &require_module($pkg); 1 } }) 0 4 4
154 0 return $self->_error_scanner_missing( $self->scanner, $pkg, $@ );
155 }
156 4 return $pkg;
157 }
158  
159 4 ▶sub _build__scanner_instance {
Subroutine _build__scanner_instance: called CC=1 SCAR=0.0
160 4 my ($self) = @_;
161 4 my $instance = $self->_scanner_package->new();
162 4 return $instance;
163 }
164  
165 0 ▶sub _warn_stash_collision {
Subroutine _warn_stash_collision: not called CC=1 SCAR=6.9
166 0 my ( $self, @args ) = @_;
167 0 my ( $stashname, $header, $offset ) = pos_validated_list(
168 \@args,
169 { isa => Str, }, #
170 { isa => Str, },
171 { isa => Str, },
172 );
173 0 my $message = q[Warning, duplicate file offset reported by scanner.];
174 0 $message .= sprintf q[<%s> and <%s> collide at <%s>.], $stashname, $header, $offset;
175 0 $message .= sprintf q[Assuming <%s> is empty and replacing it.], $stashname;
176 0 $self->log->warn($message);
177 0 return;
178 }
179  
180 3983 ▶sub _stash_record {
Subroutine _stash_record: called CC=2 SCAR=7.2
181 3983 my ( $self, @args ) = @_;
182 3983 my ( $stash, $header, $offset ) = pos_validated_list(
183 \@args,
184 { isa => HashRef, }, #
185 { isa => Str, },
186 { isa => Str, },
187 );
188 3983 ▶ if ( exists $stash->{$offset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $stash->{$offset}) 0 3983 3983
189 0 $self->_warn_stash_collision( $stash->{$offset}, $header, $offset );
190 }
191 3983 $stash->{$offset} = $header;
192 3983 return;
193 }
194  
195 3979 ▶sub _build_section_section {
Subroutine _build_section_section: called CC=1 SCAR=0.0
196 3979 my ( $self, @args ) = @_;
197 3979 my ( $stashName, $start, $stop, $file ) = pos_validated_list(
198 \@args,
199 { isa => Str, required => 1 },
200 { isa => Int, required => 1 },
201 { isa => Int, required => 1 },
202 { isa => File, required => 1 },
203 );
204 3979 $self->log->info(" Section ${stashName} , ${start} -> ${stop} ");
205 3979 return ELF::Extract::Sections::Section->new(
206 offset => $start,
207 size => $stop - $start,
208 name => $stashName,
209 source => $file,
210 );
211 }
212  
213 4 ▶sub _build_section_table {
Subroutine _build_section_table: called CC=2 SCAR=6.9
214 4 my ( $self, @args ) = @_;
215 4 my ($ob) = pos_validated_list(
216 \@args, #
217 { isa => HashRef },
218 );
219 4 my %datastash = ();
220 4 my @k = sort { $a <=> $b } keys %{$ob};
221 4 my $i = 0;
222 4 ▶ while ( $i < $#k ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#k) 3979 4 3983
223 3979 $datastash{ $ob->{ $k[$i] } } = $self->_build_section_section( $ob->{ $k[$i] }, $k[$i], $k[ $i + 1 ], $self->file );
224 3979 $i++;
225 }
226 4 return \%datastash;
227 }
228  
229 4 ▶sub _scan_guess_size {
Subroutine _scan_guess_size: called CC=2 SCAR=6.9
230 4 my ($self) = @_;
231  
232 # HACK: Temporary hack around rt#67210
233 4 scalar $self->_scanner_instance->open_file( file => $self->file );
234 4 my %offsets = ();
235 4 ▶ while ( $self->_scanner_instance->next_section() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_scanner_instance->next_section) 3983 4 3987
236 3983 my $name = $self->_scanner_instance->section_name;
237 3983 my $offset = $self->_scanner_instance->section_offset;
238 3983 $self->_stash_record( \%offsets, $name, $offset );
239 }
240 4 return $self->_build_section_table( \%offsets );
241 }
242  
243 0 ▶sub _scan_with_size {
Subroutine _scan_with_size: not called CC=2 SCAR=17.9
244 0 my ($self) = @_;
245 0 my %datastash = ();
246 0 $self->_scanner_instance->open_file( file => $self->file );
247 0 ▶ while ( $self->_scanner_instance->next_section() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_scanner_instance->next_section) 0 0 0
248 0 my $name = $self->_scanner_instance->section_name;
249 0 my $offset = $self->_scanner_instance->section_offset;
250 0 my $size = $self->_scanner_instance->section_size;
251 0 $datastash{$name} = $self->_build_section_section( $name, $offset, $offset + $size, $self->file );
252 }
253 0 return \%datastash;
254 }
255  
256 2 1;
257  
258 __END__
« lib/ELF/Extract/Sections/Scanner/Objdump.pm ↑ Summary lib/ELF/Extract/Sections/Meta/Scanner.pm »