Don't fail nm calls silently

This commit is contained in:
2026-08-17 21:59:57 +02:00
parent 87d5d5b92d
commit 334bdbff76
+34 -4
View File
@@ -181,19 +181,49 @@ sub format_number_sep {
return $number; return $number;
} }
# Run nm once and cache the result instead of running 20x per query
my %nm_cache;
sub nm_lines {
my ( $elffile, @nm_args ) = @_;
my $command = join ' ', 'nm', @nm_args, shell_quote($elffile);
return @{ $nm_cache{$command} } if exists $nm_cache{$command};
die "ELF file does not exist: $elffile\n" unless -f $elffile;
my @lines = qx{$command 2>&1};
my $status = $?;
die "nm not found in PATH\n"
if $status == -1 || ( $status >> 8 ) == 127;
die "nm failed on $elffile (exit @{[ $status >> 8 ]}): @lines"
if $status != 0;
die "nm found no symbols in $elffile\n" unless @lines;
$nm_cache{$command} = \@lines;
return @lines;
}
sub elf_sym_addr { sub elf_sym_addr {
my ( $elffile, $sym ) = @_; my ( $elffile, $sym ) = @_;
my $line = qx{nm "$elffile" 2>/dev/null | grep " $sym\$"};
return undef unless $line =~ /^([0-9a-f]+)/i; for my $line ( nm_lines($elffile) ) {
# "0010eefe T aot_invoke_native"
# This is case-sensitive!
next unless $line =~ /^([0-9a-fA-F]+)\s+\S\s+\Q$sym\E$/;
return hex($1); return hex($1);
}
return undef;
} }
# Determine the address range of a function symbol # Determine the address range of a function symbol
sub elf_sym_range { sub elf_sym_range {
my ( $elffile, $sym ) = @_; my ( $elffile, $sym ) = @_;
my @lines = qx{nm -nS "$elffile" 2>/dev/null}; my @lines = nm_lines( $elffile, '-nS' );
return () unless @lines;
for my $i ( 0 .. $#lines ) { for my $i ( 0 .. $#lines ) {
my $line = $lines[$i]; my $line = $lines[$i];