<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Copyright В© 2007-2010 RaphaГ«l Hertzog &lt;hertzog@debian.org&gt;
# Copyright В© 2007-2009,2012-2015,2017-2018 Guillem Jover &lt;guillem@debian.org&gt;
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see &lt;https://www.gnu.org/licenses/&gt;.

package Dpkg::Shlibs::Objdump;

use strict;
use warnings;
use feature qw(state);

our $VERSION = '0.01';

use Dpkg::Gettext;
use Dpkg::ErrorHandling;
use Dpkg::IPC;

sub new {
    my $this = shift;
    my $class = ref($this) || $this;
    my $self = { objects =&gt; {} };
    bless $self, $class;
    return $self;
}

sub add_object {
    my ($self, $obj) = @_;
    my $id = $obj-&gt;get_id;
    if ($id) {
	$self-&gt;{objects}{$id} = $obj;
    }
    return $id;
}

sub analyze {
    my ($self, $file) = @_;
    my $obj = Dpkg::Shlibs::Objdump::Object-&gt;new($file);

    return $self-&gt;add_object($obj);
}

sub locate_symbol {
    my ($self, $name) = @_;
    foreach my $obj (values %{$self-&gt;{objects}}) {
	my $sym = $obj-&gt;get_symbol($name);
	if (defined($sym) &amp;&amp; $sym-&gt;{defined}) {
	    return $sym;
	}
    }
    return;
}

sub get_object {
    my ($self, $objid) = @_;
    if ($self-&gt;has_object($objid)) {
	return $self-&gt;{objects}{$objid};
    }
    return;
}

sub has_object {
    my ($self, $objid) = @_;
    return exists $self-&gt;{objects}{$objid};
}

sub is_armhf {
    my ($file) = @_;
    my ($output, %opts, $pid, $res);
    my $hf = 0;
    my $sf = 0;
    $pid = spawn(exec =&gt; [ "readelf", "-h", "--", $file ],
		 env =&gt; { "LC_ALL" =&gt; "C" },
		 to_pipe =&gt; \$output, %opts);
    while (&lt;$output&gt;) {
	chomp;
	if (/0x500040./) {
	    $hf = 1;
	    last;
	}
	if (/0x500020./) {
	    $sf = 1;
	    last;
	}
    }
    close($output);
    wait_child($pid, nocheck =&gt; 1);
    if ($?) {
	subprocerr("readelf");
    }
    if(($hf) || ($sf)) {
	return $hf;
    }
    $hf = 0;
    undef $output;
    $pid = spawn(exec =&gt; [ "readelf", "-A", "--", $file ],
		 env =&gt; { "LC_ALL" =&gt; "C" },
		 to_pipe =&gt; \$output, %opts);
    while (&lt;$output&gt;) {
	chomp;
	if (/Tag_ABI_VFP_args: VFP registers/) {
	    $hf = 1;
	    last;
	}
    }
    close($output);
    wait_child($pid, nocheck =&gt; 1);
    if ($?) {
	subprocerr("readelf");
    }
    return $hf;
}

use constant {
    ELF_BITS_NONE           =&gt; 0,
    ELF_BITS_32             =&gt; 1,
    ELF_BITS_64             =&gt; 2,

    ELF_ORDER_NONE          =&gt; 0,
    ELF_ORDER_2LSB          =&gt; 1,
    ELF_ORDER_2MSB          =&gt; 2,

    ELF_MACH_SPARC          =&gt; 2,
    ELF_MACH_MIPS           =&gt; 8,
    ELF_MACH_SPARC64_OLD    =&gt; 11,
    ELF_MACH_SPARC32PLUS    =&gt; 18,
    ELF_MACH_PPC64          =&gt; 21,
    ELF_MACH_S390           =&gt; 22,
    ELF_MACH_ARM            =&gt; 40,
    ELF_MACH_ALPHA_OLD      =&gt; 41,
    ELF_MACH_SH             =&gt; 42,
    ELF_MACH_SPARC64        =&gt; 43,
    ELF_MACH_IA64           =&gt; 50,
    ELF_MACH_AVR            =&gt; 83,
    ELF_MACH_M32R           =&gt; 88,
    ELF_MACH_MN10300        =&gt; 89,
    ELF_MACH_MN10200        =&gt; 90,
    ELF_MACH_OR1K           =&gt; 92,
    ELF_MACH_XTENSA         =&gt; 94,
    ELF_MACH_MICROBLAZE     =&gt; 189,
    ELF_MACH_AVR_OLD        =&gt; 0x1057,
    ELF_MACH_OR1K_OLD       =&gt; 0x8472,
    ELF_MACH_ALPHA          =&gt; 0x9026,
    ELF_MACH_M32R_CYGNUS    =&gt; 0x9041,
    ELF_MACH_S390_OLD       =&gt; 0xa390,
    ELF_MACH_XTENSA_OLD     =&gt; 0xabc7,
    ELF_MACH_MICROBLAZE_OLD =&gt; 0xbaab,
    ELF_MACH_MN10300_CYGNUS =&gt; 0xbeef,
    ELF_MACH_MN10200_CYGNUS =&gt; 0xdead,

    ELF_VERSION_NONE        =&gt; 0,
    ELF_VERSION_CURRENT     =&gt; 1,

    # List of processor flags that might influence the ABI.

    ELF_FLAG_ARM_ALIGN8     =&gt; 0x00000040,
    ELF_FLAG_ARM_NEW_ABI    =&gt; 0x00000080,
    ELF_FLAG_ARM_OLD_ABI    =&gt; 0x00000100,
    ELF_FLAG_ARM_SOFT_FLOAT =&gt; 0x00000200,
    ELF_FLAG_ARM_HARD_FLOAT =&gt; 0x00000400,
    ELF_FLAG_ARM_EABI_MASK  =&gt; 0xff000000,

    ELF_FLAG_IA64_ABI64     =&gt; 0x00000010,

    ELF_FLAG_MIPS_ABI2      =&gt; 0x00000020,
    ELF_FLAG_MIPS_32BIT     =&gt; 0x00000100,
    ELF_FLAG_MIPS_FP64      =&gt; 0x00000200,
    ELF_FLAG_MIPS_NAN2008   =&gt; 0x00000400,
    ELF_FLAG_MIPS_ABI_MASK  =&gt; 0x0000f000,
    ELF_FLAG_MIPS_ARCH_MASK =&gt; 0xf0000000,

    ELF_FLAG_PPC64_ABI64    =&gt; 0x00000003,

    ELF_FLAG_SH_MACH_MASK   =&gt; 0x0000001f,
};

# These map alternative or old machine IDs to their canonical form.
my %elf_mach_map = (
    ELF_MACH_ALPHA_OLD()        =&gt; ELF_MACH_ALPHA,
    ELF_MACH_AVR_OLD()          =&gt; ELF_MACH_AVR,
    ELF_MACH_M32R_CYGNUS()      =&gt; ELF_MACH_M32R,
    ELF_MACH_MICROBLAZE_OLD()   =&gt; ELF_MACH_MICROBLAZE,
    ELF_MACH_MN10200_CYGNUS()   =&gt; ELF_MACH_MN10200,
    ELF_MACH_MN10300_CYGNUS()   =&gt; ELF_MACH_MN10300,
    ELF_MACH_OR1K_OLD()         =&gt; ELF_MACH_OR1K,
    ELF_MACH_S390_OLD()         =&gt; ELF_MACH_S390,
    ELF_MACH_SPARC32PLUS()      =&gt; ELF_MACH_SPARC,
    ELF_MACH_SPARC64_OLD()      =&gt; ELF_MACH_SPARC64,
    ELF_MACH_XTENSA_OLD()       =&gt; ELF_MACH_XTENSA,
);

# These masks will try to expose processor flags that are ABI incompatible,
# and as such are part of defining the architecture ABI. If uncertain it is
# always better to not mask a flag, because that preserves the historical
# behavior, and we do not drop dependencies.
my %elf_flags_mask = (
    ELF_MACH_IA64()     =&gt; ELF_FLAG_IA64_ABI64,
    ELF_MACH_MIPS()     =&gt; ELF_FLAG_MIPS_ABI_MASK | ELF_FLAG_MIPS_ABI2,
    ELF_MACH_PPC64()    =&gt; ELF_FLAG_PPC64_ABI64,
);

sub get_format {
    my ($file) = @_;
    state %format;

    return $format{$file} if exists $format{$file};

    my $header;

    open my $fh, '&lt;', $file or syserr(g_('cannot read %s'), $file);
    my $rc = read $fh, $header, 64;
    if (not defined $rc) {
        syserr(g_('cannot read %s'), $file);
    } elsif ($rc != 64) {
        return;
    }
    close $fh;

    my %elf;

    # Unpack the identifier field.
    @elf{qw(magic bits endian vertype osabi verabi)} = unpack 'a4C5', $header;

    return unless $elf{magic} eq "\x7fELF";
    return unless $elf{vertype} == ELF_VERSION_CURRENT;

    my ($elf_word, $elf_endian);
    if ($elf{bits} == ELF_BITS_32) {
        $elf_word = 'L';
    } elsif ($elf{bits} == ELF_BITS_64) {
        $elf_word = 'Q';
    } else {
        return;
    }
    if ($elf{endian} == ELF_ORDER_2LSB) {
        $elf_endian = '&lt;';
    } elsif ($elf{endian} == ELF_ORDER_2MSB) {
        $elf_endian = '&gt;';
    } else {
        return;
    }

    # Unpack the endianness and size dependent fields.
    my $tmpl = "x16(S2Lx[${elf_word}3]L)${elf_endian}";
    @elf{qw(type mach version flags)} = unpack $tmpl, $header;

    # Canonicalize the machine ID.
    $elf{mach} = $elf_mach_map{$elf{mach}} // $elf{mach};

    # Mask any processor flags that might not change the architecture ABI.
    $elf{flags} &amp;= $elf_flags_mask{$elf{mach}} // 0;

    if (($elf{mach} == ELF_MACH_ARM) &amp;&amp; ($elf{endian} == ELF_ORDER_2LSB)) {
        if (is_armhf($file)) {
            $elf{flags} |= ELF_FLAG_ARM_HARD_FLOAT;
        } else {
            $elf{flags} |= ELF_FLAG_ARM_SOFT_FLOAT;
        }
    }

    # Repack for easy comparison, as a big-endian byte stream, so that
    # unpacking for output gives meaningful results.
    $format{$file} = pack 'C2(SL)&gt;', @elf{qw(bits endian mach flags)};

    return $format{$file};
}

sub is_elf {
    my $file = shift;
    open(my $file_fh, '&lt;', $file) or syserr(g_('cannot read %s'), $file);
    my ($header, $result) = ('', 0);
    if (read($file_fh, $header, 4) == 4) {
	$result = 1 if ($header =~ /^\177ELF$/);
    }
    close($file_fh);
    return $result;
}

package Dpkg::Shlibs::Objdump::Object;

use strict;
use warnings;
use feature qw(state);

use Dpkg::Gettext;
use Dpkg::ErrorHandling;
use Dpkg::Path qw(find_command);
use Dpkg::Arch qw(debarch_to_gnutriplet get_build_arch get_host_arch);

sub new {
    my $this = shift;
    my $file = shift // '';
    my $class = ref($this) || $this;
    my $self = {};
    bless $self, $class;

    $self-&gt;reset;
    if ($file) {
	$self-&gt;analyze($file);
    }

    return $self;
}

sub reset {
    my $self = shift;

    $self-&gt;{file} = '';
    $self-&gt;{id} = '';
    $self-&gt;{HASH} = '';
    $self-&gt;{GNU_HASH} = '';
    $self-&gt;{INTERP} = 0;
    $self-&gt;{SONAME} = '';
    $self-&gt;{NEEDED} = [];
    $self-&gt;{RPATH} = [];
    $self-&gt;{dynsyms} = {};
    $self-&gt;{flags} = {};
    $self-&gt;{dynrelocs} = {};

    return $self;
}

sub _select_objdump {
    # Decide which objdump to call
    if (get_build_arch() ne get_host_arch()) {
        my $od = debarch_to_gnutriplet(get_host_arch()) . '-objdump';
        return $od if find_command($od);
    }
    return 'objdump';
}

sub analyze {
    my ($self, $file) = @_;

    $file ||= $self-&gt;{file};
    return unless $file;

    $self-&gt;reset;
    $self-&gt;{file} = $file;

    $self-&gt;{exec_abi} = Dpkg::Shlibs::Objdump::get_format($file);

    if (not defined $self-&gt;{exec_abi}) {
        warning(g_("unknown executable format in file '%s'"), $file);
        return;
    }

    state $OBJDUMP = _select_objdump();
    local $ENV{LC_ALL} = 'C';
    open(my $objdump, '-|', $OBJDUMP, '-w', '-f', '-p', '-T', '-R', $file)
        or syserr(g_('cannot fork for %s'), $OBJDUMP);
    my $ret = $self-&gt;parse_objdump_output($objdump);
    close($objdump);
    return $ret;
}

sub parse_objdump_output {
    my ($self, $fh) = @_;

    my $section = 'none';
    while (&lt;$fh&gt;) {
	s/\s*$//;
	next if length == 0;

	if (/^DYNAMIC SYMBOL TABLE:/) {
	    $section = 'dynsym';
	    next;
	} elsif (/^DYNAMIC RELOCATION RECORDS/) {
	    $section = 'dynreloc';
	    $_ = &lt;$fh&gt;; # Skip header
	    next;
	} elsif (/^Dynamic Section:/) {
	    $section = 'dyninfo';
	    next;
	} elsif (/^Program Header:/) {
	    $section = 'program';
	    next;
	} elsif (/^Version definitions:/) {
	    $section = 'verdef';
	    next;
	} elsif (/^Version References:/) {
	    $section = 'verref';
	    next;
	}

	if ($section eq 'dynsym') {
	    $self-&gt;parse_dynamic_symbol($_);
	} elsif ($section eq 'dynreloc') {
	    if (/^\S+\s+(\S+)\s+(.+)$/) {
		$self-&gt;{dynrelocs}{$2} = $1;
	    } else {
		warning(g_("couldn't parse dynamic relocation record: %s"), $_);
	    }
	} elsif ($section eq 'dyninfo') {
	    if (/^\s*NEEDED\s+(\S+)/) {
		push @{$self-&gt;{NEEDED}}, $1;
	    } elsif (/^\s*SONAME\s+(\S+)/) {
		$self-&gt;{SONAME} = $1;
	    } elsif (/^\s*HASH\s+(\S+)/) {
		$self-&gt;{HASH} = $1;
	    } elsif (/^\s*GNU_HASH\s+(\S+)/) {
		$self-&gt;{GNU_HASH} = $1;
	    } elsif (/^\s*RUNPATH\s+(\S+)/) {
                # RUNPATH takes precedence over RPATH but is
                # considered after LD_LIBRARY_PATH while RPATH
                # is considered before (if RUNPATH is not set).
                my $runpath = $1;
                $self-&gt;{RPATH} = [ split /:/, $runpath ];
	    } elsif (/^\s*RPATH\s+(\S+)/) {
                my $rpath = $1;
                unless (scalar(@{$self-&gt;{RPATH}})) {
                    $self-&gt;{RPATH} = [ split /:/, $rpath ];
                }
	    }
        } elsif ($section eq 'program') {
            if (/^\s*INTERP\s+/) {
                $self-&gt;{INTERP} = 1;
            }
	} elsif ($section eq 'none') {
	    if (/^\s*.+:\s*file\s+format\s+(\S+)$/) {
		$self-&gt;{format} = $1;
		if (($self-&gt;{format} eq "elf32-littlearm") &amp;&amp; $self-&gt;{file}) {
		    if (Dpkg::Shlibs::Objdump::is_armhf($self-&gt;{file})) {
			$self-&gt;{format} = "elf32-littlearm-hfabi";
		    } else {
			$self-&gt;{format} = "elf32-littlearm-sfabi";
		    }
		}
	    } elsif (/^architecture:\s*\S+,\s*flags\s*\S+:$/) {
		# Parse 2 lines of "-f"
		# architecture: i386, flags 0x00000112:
		# EXEC_P, HAS_SYMS, D_PAGED
		# start address 0x08049b50
		$_ = &lt;$fh&gt;;
		chomp;
		$self-&gt;{flags}{$_} = 1 foreach (split(/,\s*/));
	    }
	}
    }
    # Update status of dynamic symbols given the relocations that have
    # been parsed after the symbols...
    $self-&gt;apply_relocations();

    return $section ne 'none';
}

# Output format of objdump -w -T
#
# /lib/libc.so.6:     file format elf32-i386
#
# DYNAMIC SYMBOL TABLE:
# 00056ef0 g    DF .text  000000db  GLIBC_2.2   getwchar
# 00000000 g    DO *ABS*  00000000  GCC_3.0     GCC_3.0
# 00069960  w   DF .text  0000001e  GLIBC_2.0   bcmp
# 00000000  w   D  *UND*  00000000              _pthread_cleanup_pop_restore
# 0000b788 g    DF .text  0000008e  Base        .protected xine_close
# 0000b788 g    DF .text  0000008e              .hidden IA__g_free
# |        ||||||| |      |         |           |
# |        ||||||| |      |         Version str (.visibility) + Symbol name
# |        ||||||| |      Alignment
# |        ||||||| Section name (or *UND* for an undefined symbol)
# |        ||||||F=Function,f=file,O=object
# |        |||||d=debugging,D=dynamic
# |        ||||I=Indirect
# |        |||W=warning
# |        ||C=constructor
# |        |w=weak
# |        g=global,l=local,!=both global/local
# Size of the symbol
#
# GLIBC_2.2 is the version string associated to the symbol
# (GLIBC_2.2) is the same but the symbol is hidden, a newer version of the
# symbol exist

my $vis_re = qr/(\.protected|\.hidden|\.internal|0x\S+)/;
my $dynsym_re = qr&lt;
    ^
    [0-9a-f]+                   # Symbol size
    \ (.{7})                    # Flags
    \s+(\S+)                    # Section name
    \s+[0-9a-f]+                # Alignment
    (?:\s+(\S+))?               # Version string
    (?:\s+$vis_re)?             # Visibility
    \s+(.+)                     # Symbol name
&gt;x;

sub parse_dynamic_symbol {
    my ($self, $line) = @_;
    if ($line =~ $dynsym_re) {

	my ($flags, $sect, $ver, $vis, $name) = ($1, $2, $3, $4, $5);

	# Special case if version is missing but extra visibility
	# attribute replaces it in the match
	if (defined($ver) and $ver =~ /^$vis_re$/) {
	    $vis = $ver;
	    $ver = '';
	}

	# Cleanup visibility field
	$vis =~ s/^\.// if defined($vis);

	my $symbol = {
		name =&gt; $name,
		version =&gt; $ver // '',
		section =&gt; $sect,
		dynamic =&gt; substr($flags, 5, 1) eq 'D',
		debug =&gt; substr($flags, 5, 1) eq 'd',
		type =&gt; substr($flags, 6, 1),
		weak =&gt; substr($flags, 1, 1) eq 'w',
		local =&gt; substr($flags, 0, 1) eq 'l',
		global =&gt; substr($flags, 0, 1) eq 'g',
		visibility =&gt; $vis // '',
		hidden =&gt; '',
		defined =&gt; $sect ne '*UND*'
	    };

	# Handle hidden symbols
	if (defined($ver) and $ver =~ /^\((.*)\)$/) {
	    $ver = $1;
	    $symbol-&gt;{version} = $1;
	    $symbol-&gt;{hidden} = 1;
	}

	# Register symbol
	$self-&gt;add_dynamic_symbol($symbol);
    } elsif ($line =~ /^[0-9a-f]+ (.{7})\s+(\S+)\s+[0-9a-f]+/) {
	# Same start but no version and no symbol ... just ignore
    } elsif ($line =~ /^REG_G\d+\s+/) {
	# Ignore some s390-specific output like
	# REG_G6           g     R *UND*      0000000000000000              #scratch
    } else {
	warning(g_("couldn't parse dynamic symbol definition: %s"), $line);
    }
}

sub apply_relocations {
    my $self = shift;
    foreach my $sym (values %{$self-&gt;{dynsyms}}) {
	# We want to mark as undefined symbols those which are currently
	# defined but that depend on a copy relocation
	next if not $sym-&gt;{defined};
	next if not exists $self-&gt;{dynrelocs}{$sym-&gt;{name}};
	if ($self-&gt;{dynrelocs}{$sym-&gt;{name}} =~ /^R_.*_COPY$/) {
	    $sym-&gt;{defined} = 0;
	}
    }
}

sub add_dynamic_symbol {
    my ($self, $symbol) = @_;
    $symbol-&gt;{objid} = $symbol-&gt;{soname} = $self-&gt;get_id();
    $symbol-&gt;{soname} =~ s{^.*/}{} unless $self-&gt;{SONAME};
    if ($symbol-&gt;{version}) {
	$self-&gt;{dynsyms}{$symbol-&gt;{name} . '@' . $symbol-&gt;{version}} = $symbol;
    } else {
	$self-&gt;{dynsyms}{$symbol-&gt;{name} . '@Base'} = $symbol;
    }
}

sub get_id {
    my $self = shift;
    return $self-&gt;{SONAME} || $self-&gt;{file};
}

sub get_symbol {
    my ($self, $name) = @_;
    if (exists $self-&gt;{dynsyms}{$name}) {
	return $self-&gt;{dynsyms}{$name};
    }
    if ($name !~ /@/) {
        if (exists $self-&gt;{dynsyms}{$name . '@Base'}) {
            return $self-&gt;{dynsyms}{$name . '@Base'};
        }
    }
    return;
}

sub get_exported_dynamic_symbols {
    my $self = shift;
    return grep { $_-&gt;{defined} &amp;&amp; $_-&gt;{dynamic} &amp;&amp; !$_-&gt;{local} }
	    values %{$self-&gt;{dynsyms}};
}

sub get_undefined_dynamic_symbols {
    my $self = shift;
    return grep { (!$_-&gt;{defined}) &amp;&amp; $_-&gt;{dynamic} }
	    values %{$self-&gt;{dynsyms}};
}

sub get_needed_libraries {
    my $self = shift;
    return @{$self-&gt;{NEEDED}};
}

sub is_executable {
    my $self = shift;
    return (exists $self-&gt;{flags}{EXEC_P} &amp;&amp; $self-&gt;{flags}{EXEC_P}) ||
           (exists $self-&gt;{INTERP} &amp;&amp; $self-&gt;{INTERP});
}

sub is_public_library {
    my $self = shift;
    return exists $self-&gt;{flags}{DYNAMIC} &amp;&amp; $self-&gt;{flags}{DYNAMIC}
	&amp;&amp; exists $self-&gt;{SONAME} &amp;&amp; $self-&gt;{SONAME};
}

1;
</pre></body></html>