lib/Sys/Ebpf/Elf/Parser.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Sys/Ebpf/Link/Perf/Constants/PerfEventIoctl.pm ↑ Summary lib/Sys/Ebpf/Reader.pm »
1 package Sys::Ebpf::Elf::Parser;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 our $VERSION = $Sys::Ebpf::VERSION;
8  
9 0 ▶use Sys::Ebpf::Elf::Constants;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 use Sys::Ebpf::Elf::MachineType ();
12  
13 # elf64形式はこの通り
14 # cf. https://refspecs.linuxfoundation.org/elf/gabi4+/ch4.eheader.html
15 # $cat /usr/include/elf.h | grep -B16 " Elf64_Ehdr;"
16 # typedef struct
17 # {
18 # unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
19 # Elf64_Half e_type; /* Object file type */
20 # Elf64_Half e_machine; /* Architecture */
21 # Elf64_Word e_version; /* Object file version */
22 # Elf64_Addr e_entry; /* Entry point virtual address */
23 # Elf64_Off e_phoff; /* Program header table file offset */
24 # Elf64_Off e_shoff; /* Section header table file offset */
25 # Elf64_Word e_flags; /* Processor-specific flags */
26 # Elf64_Half e_ehsize; /* ELF header size in bytes */
27 # Elf64_Half e_phentsize; /* Program header table entry size */
28 # Elf64_Half e_phnum; /* Program header table entry count */
29 # Elf64_Half e_shentsize; /* Section header table entry size */
30 # Elf64_Half e_shnum; /* Section header table entry count */
31 # Elf64_Half e_shstrndx; /* Section header string table index */
32 # } Elf64_Ehdr;
33 # typedef uint64_t Elf64_Addr;
34 # typedef uint16_t Elf64_Half;
35 # typedef uint64_t Elf64_Off;
36 # typedef int32_t Elf64_Sword;
37 # typedef int64_t Elf64_Sxword;
38 # typedef uint32_t Elf64_Word;
39 # typedef uint64_t Elf64_Lword;
40 # typedef uint64_t Elf64_Xword;
41  
42 # ebpf binaryを読み出して、elfをパースします
43 # コンストラクタにはファイルのバイナリデータが渡されます
44 sub new {
45 my ( $class, $data ) = @_;
46 my $self = { data => $data };
47 bless $self, $class;
48 return $self;
49 }
50  
51 # ELFヘッダをパースする
52 sub parse_elf {
53 my ($self) = @_;
54 my $elf = {};
55  
56 my $data = $self->{data};
57 my $byte_offset = 0;
58 my $byte_range = 16; # ELFヘッダは16バイト
59 # e_identをパース
60 my ( $magic, $class, $endian, $version, $abi, $abi_version )
61 = unpack( 'A4C3A5C2',
62 substr( $data, $byte_offset, $byte_offset + $byte_range ) );
63 $elf->{magic} = $magic;
64 $elf->{class} = $class == 1 ? 'ELF32' : 'ELF64';
65 $elf->{endian} = $endian == 1 ? 'little endian' : 'big endian';
66 $elf->{version} = $version;
67 $elf->{abi} = $abi;
68 $elf->{abi_version} = $abi_version;
69  
70 $byte_offset += $byte_range;
71 $byte_range = 32;
72  
73 # ELFファイルのサイズなどを取得
74 my ($e_type, $e_machine, $e_version, $e_entry, $e_phoff,
75 $e_shoff, $e_flags, $e_ehsize, $e_phentsize, $e_phnum,
76 $e_shentsize, $e_shnum, $e_shstrndx
77 )
78 = unpack( 'S S L Q Q Q L S S S S S S',
79 substr( $data, $byte_offset, $byte_offset + $byte_range ) );
80  
81 $elf->{e_type} = $e_type;
82 $elf->{e_machine} = $e_machine;
83 $elf->{e_machine_name}
84 = Sys::Ebpf::Elf::MachineType->get_machine_name($e_machine);
85 $elf->{e_version} = $e_version;
86 $elf->{e_entry} = $e_entry;
87 $elf->{e_phoff} = $e_phoff;
88 $elf->{e_shoff} = $e_shoff;
89 $elf->{e_flags} = $e_flags;
90 $elf->{e_ehsize} = $e_ehsize;
91 $elf->{e_phentsize} = $e_phentsize;
92 $elf->{e_phnum} = $e_phnum;
93 $elf->{e_shentsize} = $e_shentsize;
94 $elf->{e_shnum} = $e_shnum;
95 $elf->{e_shstrndx} = $e_shstrndx;
96  
97 # section tableのセクション名を取得するために文字列テーブルセクションを取得
98 my $strtab_section_offset
99 = $elf->{e_shoff} + $elf->{e_shstrndx} * $elf->{e_shentsize};
100 my $strtab_offset
101 = unpack( 'Q', substr( $data, $strtab_section_offset + 24, 8 ) );
102  
103 # セクションヘッダとシンボルテーブルをパースするための追加処理
104 $elf->{sections}
105 = parse_sections( $data, $elf->{e_shoff}, $elf->{e_shnum},
106 $elf->{e_shentsize}, $strtab_offset );
107 $elf->{symbols}
108 = parse_symbols( $data, $elf->{sections}, $elf->{e_shstrndx} );
109 $elf->{relocations} = parse_relocations( $data, $elf->{sections} );
110 return $elf;
111 }
112  
113 # セクションヘッダをパースする
114 # args
115 # data: ELFバイナリデータの文字列
116 # shoff: セクションヘッダテーブルのオフセット
117 # shnum: セクション数
118 # shentsize: セクションヘッダのサイズ
119 # strtab_offset: セクション名の文字列テーブルのオフセット
120 # return
121 # sections: セクション情報の配列
122 sub parse_sections {
123 my ( $data, $shoff, $shnum, $shentsize, $strtab_offset ) = @_;
124 my @sections;
125  
126 for my $i ( 0 .. $shnum - 1 ) {
127 my $offset = $shoff + $i * $shentsize;
128 my ($sh_name_offset, $sh_type, $sh_flags, $sh_addr,
129 $sh_offset, $sh_size, $sh_link, $sh_info,
130 $sh_addralign, $sh_entsize
131 )
132 = unpack( 'L L Q Q Q Q L L Q Q',
133 substr( $data, $offset, $shentsize ) );
134  
135 # セクション名を取得
136 my $name_offset = $strtab_offset + $sh_name_offset;
137 my $sh_name = unpack( 'Z*', substr( $data, $name_offset ) );
138  
139 push @sections,
140 {
141 sh_index => $i,
142 sh_name => $sh_name,
143 sh_type => $sh_type,
144 sh_flags => $sh_flags,
145 sh_addr => $sh_addr,
146 sh_offset => $sh_offset,
147 sh_size => $sh_size,
148 sh_link => $sh_link,
149 sh_info => $sh_info,
150 sh_addralign => $sh_addralign,
151 sh_entsize => $sh_entsize,
152 };
153 }
154  
155 return \@sections;
156 }
157  
158 # シンボルテーブルをパースする
159 # args
160 # data: ELFバイナリデータの文字列
161 # sections: セクション情報の配列
162 # strtab_idx: シンボルテーブルの文字列テーブルのインデックス
163 # return
164 # symbols: シンボルテーブルのハッシュのリファレンス
165 sub parse_symbols {
166 my ( $data, $sections, $strtab_idx ) = @_;
167 my @symbols;
168  
169 # get string table section
170 my $strtab_section = $sections->[$strtab_idx];
171 my $strtab_offset = $strtab_section->{sh_offset};
172 my $strtab_size = $strtab_section->{sh_size};
173  
174 # get symbol table section
175 my $symtab_section = find_section( $sections, '.symtab' );
176 my $num_symbols
177 = $symtab_section->{sh_size} / $symtab_section->{sh_entsize};
178  
179 for my $i ( 0 .. $num_symbols - 1 ) {
180 my $offset = $symtab_section->{sh_offset}
181 + $i * $symtab_section->{sh_entsize};
182 my ( $st_name, $st_info, $st_other, $st_shndx, $st_value, $st_size )
183 = unpack( 'L C C S Q Q',
184 substr( $data, $offset, $symtab_section->{sh_entsize} ) );
185  
186 my $name_offset = $strtab_offset + $st_name;
187 my $name = unpack(
188 'Z*',
189 substr(
190 $data, $name_offset,
191 $strtab_size - ( $name_offset - $strtab_offset )
192 )
193 );
194 my $symbol = {
195 st_name => $name,
196 st_info => $st_info,
197 st_other => $st_other,
198 st_shndx => $st_shndx,
199 st_value => $st_value,
200 st_size => $st_size,
201 st_type => $st_info & 0xf,
202 st_bind => $st_info >> 4,
203 };
204  
205 push @symbols, $symbol;
206 }
207  
208 return \@symbols;
209 }
210  
211 # リロケーションテーブルをパースする
212 # args
213 # data: ELFバイナリデータの文字列
214 # sections: セクション情報の配列
215 # return
216 # relocations: リロケーションテーブルのハッシュのリファレンス
217 sub parse_relocations {
218 my ( $data, $sections ) = @_;
219 my %relocations;
220  
221 for my $section (@$sections) {
222 unless ( $section->{sh_type} == Sys::Ebpf::Elf::Constants::SHT_REL
223 || $section->{sh_type} == Sys::Ebpf::Elf::Constants::SHT_RELA )
224 {
225 next;
226 }
227 my @relocation;
228 my $sh_type = $section->{sh_type};
229 my $num_relocations = $section->{sh_size} / $section->{sh_entsize};
230  
231 for my $i ( 0 .. $num_relocations - 1 ) {
232 my $offset = $section->{sh_offset} + $i * $section->{sh_entsize};
233 my ( $r_offset, $r_info, $r_addend );
234  
235 # リロケーションテーブルのエントリをパース
236 # 64ビットの場合はQで8バイトを読み込む(TODO: 32ビットの場合はLで4バイトを読み込む)
237 if ( $sh_type == Sys::Ebpf::Elf::Constants::SHT_REL ) {
238 ( $r_offset, $r_info )
239 = unpack( 'Q<Q<',
240 substr( $data, $offset, $section->{sh_entsize} ) );
241 $r_addend = undef;
242 }
243 else { # SHT_RELA
244 ( $r_offset, $r_info, $r_addend )
245 = unpack( 'Q<Q<Q<',
246 substr( $data, $offset, $section->{sh_entsize} ) );
247 }
248  
249 push @relocation,
250 {
251 sh_type => $sh_type,
252 r_offset => $r_offset,
253 r_info => $r_info,
254 r_addend => $r_addend,
255 };
256 }
257 $relocations{ $section->{sh_name} } = \@relocation;
258 }
259  
260 return \%relocations;
261 }
262  
263 sub is_bpf_machine_type {
264 my ( $self, $e_machine ) = @_;
265 return $e_machine == Sys::Ebpf::Elf::Constants::EM_BPF;
266 }
267  
268 sub find_section {
269 my ( $sections, $name ) = @_;
270 return ( grep { $_->{sh_name} eq $name } @$sections )[0];
271 }
272  
273 1;
« blib/lib/Sys/Ebpf/Link/Perf/Constants/PerfEventIoctl.pm ↑ Summary lib/Sys/Ebpf/Reader.pm »