File/Readelf.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« File/Read.pm ↑ Summary File/Text.pm »
1 #
2 # $Id$
3 #
4 # file::readelf Brik
5 #
6 package Metabrik::File::Readelf;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::Shell::Command Metabrik::System::Package);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 datadir => [ qw(datadir) ],
20 input => [ qw(file) ],
21 },
22 attributes_default => {
23 },
24 commands => {
25 install => [ ], # Inherited
26 program_headers => [ qw(file|OPTIONAL) ],
27 },
28 require_modules => {
29 'Metabrik::File::Type' => [ ],
30 },
31 require_binaries => {
32 readelf => [ ],
33 },
34 need_packages => {
35 ubuntu => [ qw(binutils) ],
36 debian => [ qw(binutils) ],
37 kali => [ qw(binutils) ],
38 },
39 };
40 }
41  
42 sub brik_use_properties {
43 my $self = shift;
44  
45 return {
46 attributes_default => {
47 },
48 };
49 }
50  
51 sub program_headers {
52 my $self = shift;
53 my ($file) = @_;
54  
55 $file ||= $self->input;
56 $self->brik_help_run_undef_arg('program_headers', $file) or return;
57  
58 my $ft = Metabrik::File::Type->new_from_brik_init($self) or return;
59 my $magic = $ft->get_magic_type($file) or return;
60  
61 if ($magic !~ /^ELF /) {
62 return $self->log->error("program_headers: file [$file] is not ELF: [$magic]");
63 }
64  
65 my $cmd = "readelf --program-headers \"$file\"";
66 my $r = $self->capture($cmd) or return;
67  
68 my $entry_point = 0;
69 my $starting_offset = 0;
70 my $start = 0;
71 my $first = 1;
72 my $section = {};
73 my @sections = ();
74 for my $line (@$r) {
75 next if $line =~ /^\s*$/;
76 next if $line =~ /^\s*Type\s+Offset/; # Skip header line
77 next if $line =~ /^\s*FileSiz\s+MemSiz/; # Skip header line
78 $line =~ s/^\s*//;
79 $line =~ s/\s*$//;
80 if ($line =~ /^\s*Entry point (\S+)/) {
81 $entry_point = $1;
82 next;
83 }
84 if ($line =~ /starting at offset (\d+)/) {
85 $starting_offset = $1;
86 next;
87 }
88  
89 # Skip until we find program headers section
90 if ($line =~ /^\s*Program Headers/) {
91 $start++;
92 next;
93 }
94  
95 # Parse the section
96 # LINE[ Type Offset VirtAddr PhysAddr]
97 # LINE[ FileSiz MemSiz Flags Align]
98 # LINE[ NOTE 0x0000000000000318 0x0000000000000000 0x0000000000000000]
99 # LINE[ 0x0000000000000480 0x0000000000000480 R 0]
100  
101 if ($start) {
102 if ($first) { # First line out of two, they are wrapped
103 my @toks = split(/\s+/, $line);
104 $section->{type} = $toks[0];
105 $section->{offset} = $toks[1];
106 $section->{virtaddr} = $toks[2];
107 $section->{physaddr} = $toks[3];
108 $first = 0; # Prepare for next round
109 }
110 else {
111 my @toks = split(/\s+/, $line);
112 $section->{filesiz} = $toks[0];
113 $section->{memsiz} = $toks[1];
114 $section->{flags} = $toks[2];
115 $section->{align} = $toks[3];
116 push @sections, $section;
117 $section = {};
118 $first = 1; # Prepare for next round
119 }
120 }
121 }
122  
123 my $headers = {
124 entry_point => $entry_point,
125 starting_offset => $starting_offset,
126 sections => \@sections,
127 };
128  
129 return $headers;
130 }
131  
132 1;
133  
134 __END__
« File/Read.pm ↑ Summary File/Text.pm »