从Perl中的ELF文件读取数据

时间:2018-10-05 03:49:58

标签: regex perl elf

我需要从ELF文件或AXF文件中读取数据,并在该文件中搜索数据并返回其地址。 在Linux中,我尝试了objdump -t filename.axf | grep search_string并得到了结果。

如何在Perl中做到这一点。我也尝试了Parse :: Readelf,但是没有用。 预先感谢。

2 个答案:

答案 0 :(得分:2)

您可以使用Binutils::Objdump

hello.c

#include <stdio.h>

int main() {
    printf("Hello world!\n");
}

创建ELF

gcc -g hello.c -o hello

objdump.pl

use feature qw(say);
use strict;
use warnings;
use Binutils::Objdump (); # Seems like this module is missing Exporter interface!??

my $search_string = 'hello.c';

Binutils::Objdump::objdumpopt('-t');
Binutils::Objdump::objdumpwrap("SYMBOL TABLE" => sub { mysymtab( $search_string, @_ ) });
Binutils::Objdump::objdump('hello');

sub mysymtab {
    my ($search_string, @lines) = @_;

    for my $line (@lines) {
        say $line if $line =~ /\Q$search_string\E/;
    }
}

答案 1 :(得分:1)

对于临时脚本,您只需执行

my $search_str = 'minor';
for (grep {chop; s/.*(0x[0-9a-f]+\s+\S*$search_str\S*)$/$1/x} `objdump -t a.exe`) {
  my ($addr, $name) = split / +/;
  print "$name: $addr\n";
}