在 Perl 中从 ELF 文件中读取数据

Reading data from ELF file in Perl

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

如何在 Perl 中做到这一点。我也试过 Parse::Readelf 但没有用。 提前致谢。

您可以使用 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/;
    }
}

对于临时脚本你可以简单地做

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