Sys-Ebpf
view release on metacpan or search on metacpan
lib/Sys/Ebpf/Elf/Parser.pm view on Meta::CPAN
= $elf->{e_shoff} + $elf->{e_shstrndx} * $elf->{e_shentsize};
my $strtab_offset
= unpack( 'Q', substr( $data, $strtab_section_offset + 24, 8 ) );
# ã»ã¯ã·ã§ã³ãããã¨ã·ã³ãã«ãã¼ãã«ããã¼ã¹ããããã®è¿½å å¦ç
$elf->{sections}
= parse_sections( $data, $elf->{e_shoff}, $elf->{e_shnum},
$elf->{e_shentsize}, $strtab_offset );
$elf->{symbols}
= parse_symbols( $data, $elf->{sections}, $elf->{e_shstrndx} );
$elf->{relocations} = parse_relocations( $data, $elf->{sections} );
return $elf;
}
# ã»ã¯ã·ã§ã³ãããããã¼ã¹ãã
# args
# data: ELFãã¤ããªãã¼ã¿ã®æåå
# shoff: ã»ã¯ã·ã§ã³ããããã¼ãã«ã®ãªãã»ãã
# shnum: ã»ã¯ã·ã§ã³æ°
# shentsize: ã»ã¯ã·ã§ã³ãããã®ãµã¤ãº
# strtab_offset: ã»ã¯ã·ã§ã³åã®æååãã¼ãã«ã®ãªãã»ãã
lib/Sys/Ebpf/Elf/Parser.pm view on Meta::CPAN
}
return \@symbols;
}
# ãªãã±ã¼ã·ã§ã³ãã¼ãã«ããã¼ã¹ãã
# args
# data: ELFãã¤ããªãã¼ã¿ã®æåå
# sections: ã»ã¯ã·ã§ã³æ
å ±ã®é
å
# return
# relocations: ãªãã±ã¼ã·ã§ã³ãã¼ãã«ã®ããã·ã¥ã®ãªãã¡ã¬ã³ã¹
sub parse_relocations {
my ( $data, $sections ) = @_;
my %relocations;
for my $section (@$sections) {
unless ( $section->{sh_type} == Sys::Ebpf::Elf::Constants::SHT_REL
|| $section->{sh_type} == Sys::Ebpf::Elf::Constants::SHT_RELA )
{
next;
}
my @relocation;
my $sh_type = $section->{sh_type};
my $num_relocations = $section->{sh_size} / $section->{sh_entsize};
for my $i ( 0 .. $num_relocations - 1 ) {
my $offset = $section->{sh_offset} + $i * $section->{sh_entsize};
my ( $r_offset, $r_info, $r_addend );
# ãªãã±ã¼ã·ã§ã³ãã¼ãã«ã®ã¨ã³ããªããã¼ã¹
# 64ãããã®å ´åã¯Qã§8ãã¤ããèªã¿è¾¼ã(TODO: 32ãããã®å ´åã¯Lã§4ãã¤ããèªã¿è¾¼ã)
if ( $sh_type == Sys::Ebpf::Elf::Constants::SHT_REL ) {
( $r_offset, $r_info )
= unpack( 'Q<Q<',
substr( $data, $offset, $section->{sh_entsize} ) );
$r_addend = undef;
}
else { # SHT_RELA
( $r_offset, $r_info, $r_addend )
= unpack( 'Q<Q<Q<',
substr( $data, $offset, $section->{sh_entsize} ) );
}
push @relocation,
{
sh_type => $sh_type,
r_offset => $r_offset,
r_info => $r_info,
r_addend => $r_addend,
};
}
$relocations{ $section->{sh_name} } = \@relocation;
}
return \%relocations;
}
sub is_bpf_machine_type {
my ( $self, $e_machine ) = @_;
return $e_machine == Sys::Ebpf::Elf::Constants::EM_BPF;
}
sub find_section {
my ( $sections, $name ) = @_;
return ( grep { $_->{sh_name} eq $name } @$sections )[0];
lib/Sys/Ebpf/Loader.pm view on Meta::CPAN
# ãªãã±ã¼ã·ã§ã³ãé©ç¨
# args:
# prob_section: ããã°ã©ã ã»ã¯ã·ã§ã³
# reloc_sections: ãªãã±ã¼ã·ã§ã³ã»ã¯ã·ã§ã³
# elf: ELFãã¼ã¿
# map_hash_ref: ããããã¼ã¿ã®ãªãã¡ã¬ã³ã¹
# r_offsetã使ã£ã¦ãä¿®æ£ãã¹ãå½ä»¤ï¼ã¤ã³ã¹ãã©ã¯ã·ã§ã³ï¼ãkprobe/sys_execveã»ã¯ã·ã§ã³å
ããç¹å®ãã¾ãã
# r_infoããã·ã³ãã«ã¤ã³ããã¯ã¹ãåå¾ãããã®ã·ã³ãã«ã®ã¢ãã¬ã¹ãã·ã³ãã«ãã¼ãã«ï¼.symtabï¼ããåå¾ãã¾ãã
# ä¿®æ£ãã¹ãã¤ã³ã¹ãã©ã¯ã·ã§ã³ã«ãã·ã³ãã«ã®ã¢ãã¬ã¹ãé©ç¨ãã¦ãæ£ãããããã¸ã®åç
§ã«æ¸ãæãã¾ãã
sub apply_map_relocations {
my ( $self, $prob_section, $reloc_sections, $elf, $map_hash_ref ) = @_;
my $symbols_section = $elf->{symbols};
for my $reloc_section (@$reloc_sections) {
my $r_info = $reloc_section->{r_info};
my $r_offset
= $reloc_section->{r_offset} + $prob_section->{sh_offset};
my $sym_index = $r_info >> 32; # ã·ã³ãã«ã¤ã³ããã¯ã¹ãåå¾
my $reloc_type = $r_info & 0xFFFFFFFF; # ãªãã±ã¼ã·ã§ã³ã¿ã¤ããåå¾
# ã·ã³ãã«ãã¼ãã«ããrelocation対象ã«ãªãå¾ãã·ã³ãã«åãåå¾
my $symbol = $symbols_section->[$sym_index] // undef;
if ( !$symbol ) {
print "Symbol Table not found for index: $sym_index\n";
next;
}
my $sym_name = $symbol->{st_name};
if ( $symbol->{st_shndx} == 0 ) {
print "Symbol Name Table not found for index: $sym_index\n";
next;
}
lib/Sys/Ebpf/Loader.pm view on Meta::CPAN
my $map_name = $map->{map_name};
my $map_instance = Sys::Ebpf::Map->create($map);
my $map_fd = $map_instance->{map_fd};
if ( $map_fd < 0 ) {
die "Failed to load BPF map: $map_name (FD: $map_fd})\n";
}
$map_collection{$map_name} = $map_instance;
}
# ãªãã±ã¼ã·ã§ã³ãé©ç¨
my $reloc_section = $bpfelf->{relocations}{ ".rel" . $section_name };
my $prob_section
= find_symbol_table_from_name( $bpfelf->{sections}, $section_name );
if ( defined $reloc_section ) {
$self->apply_map_relocations( $prob_section, $reloc_section, $bpfelf,
\%map_collection );
}
# todo: bpfprobãè¤æ°ããã±ã¼ã¹ã«ã対å¿ãã
# BPF ããã°ã©ã ããã¼ã
my $prog_fd = $self->load_bpf_program_from_elf($section_name);
return ( \%map_collection, $prog_fd );
}
( run in 0.784 second using v1.01-cache-2.11-cpan-5511b514fd6 )