ELF::Extract::Sections::Scanner::Objdump - An C<objdump> based section scanner.


ELF-Extract-Sections documentation Contained in the ELF-Extract-Sections distribution.

Index


Code Index:

NAME

Top

ELF::Extract::Sections::Scanner::Objdump - An objdump based section scanner.

VERSION

Top

version 0.02071411

SYNOPSIS

Top

This module is a model implementation of a Naive and system reliant ELF Section detector. Its currently highly inefficient due to having to run the entire ELF through a disassembly process to determine the section positions and only guesses at section lengths by advertising that it can't compute sizes.

TO use this module, simply initialise ELF::Extract::Sections as so

    my $extractor  = ELF::Extract::Sections->new(
            file => "/path/to/file.so" ,
            scanner => "Objdump",
    );

IMPLEMENTS ROLES

Top

ELF::Extract::Sections::Meta::Scanner

ELF::Extract::Sections::Meta::Scanner

DEPENDS

Top

MooseX::Has::Sugar

Lots of keywords.

MooseX::Has::Sugar

MooseX::Types::Moose

Type Constraining Keywords.

MooseX::Types::Moose

MooseX::Types::Path::Class

File Type Constraints w/ Path::Class

MooseX::Types::Path::Class

PUBLIC METHODS

Top

-> open_file ( file => File ) : Bool ::Scanner

Opens the file and assigns our state to that file.

open_file in ELF::Extract::Sections::Meta::Scanner

-> next_section () : Bool ::Scanner

Advances our state to the next section.

next_section in ELF::Extract::Sections::Meta::Scanner

-> section_offset () : Int | Undef ::Scanner

Reports the offset of the currently open section

section_offset in ELF::Extract::Sections::Meta::Scanner

-> section_size () : Undef ::Scanner

Dies, because this module can't compute section sizes.

section_size in ELF::Extract::Sections::Meta::Scanner

-> section_name () : Str | Undef ::Scanner

Returns the name of the current section

section_name in ELF::Extract::Sections::Meta::Scanner

-> can_compute_size () : Bool ::Scanner

Returns false

can_compute_size in ELF::Extract::Sections::Meta::Scanner

PRIVATE ATTRIBUTES

Top

-> _header_regex : RegexpRef

A regular expression for identifying the

  <asdasdead>

Style tokens that denote objdump header names.

Note: This is not XML.

-> _offset_regex : RegexpRef

A regular expression for identifying offset blocks in objdump's output.

They look like this:

  File Offset: 0xdeadbeef

-> _section_header_identifier : RegexpRef

A regular expression for extracting Headers and Offsets together

  <headername> File Offset: 0xdeadbeef

-> _file : File

A Path::Class::File reference to a file somewhere on a system

clearer -> _clear_file

-> _filehandle : FileHandle

A perl FileHandle that points to the output of objdump for _file

clearer -> _clear_file_handle

-> _state : HashRef

Keeps track of what we're doing, and what the next header is to return.

predicate -> _has_state

clearer -> _clear_state

PRIVATE ATTRIBUTE BUILDERS

Top

-> _build__section_header_identifier : RegexpRef

Assembles _header_regex and _offset_regex

_section_header_identifier

PRIVATE METHODS

Top

-> _objdump : FileHandle | Undef

Calls the system objdump instance for the currently processing file.

AUTHOR

Top

Kent Fredric <kentnl@cpan.org>

COPYRIGHT AND LICENSE

Top


ELF-Extract-Sections documentation Contained in the ELF-Extract-Sections distribution.

use strict;
use warnings;

package ELF::Extract::Sections::Scanner::Objdump;
BEGIN {
  $ELF::Extract::Sections::Scanner::Objdump::VERSION = '0.02071411';
}

# ABSTRACT: An C<objdump> based section scanner.

# $Id:$
use MooseX::Declare;


class ELF::Extract::Sections::Scanner::Objdump
with ELF::Extract::Sections::Meta::Scanner {



    use MooseX::Has::Sugar 0.0300;


    use MooseX::Types::Moose (qw( Bool HashRef RegexpRef FileHandle Undef Str Int));


    use MooseX::Types::Path::Class ('File');



    method open_file ( File :$file! ) returns (Bool) {
        $self->log->debug("Opening $file");
        $self->_file($file);
        $self->_filehandle( $self->_objdump );
        return 1;
    };


    method next_section returns (Bool) {
        my $re = $self->_section_header_identifier;
        my $fh = $self->_filehandle;
        while ( my $line = <$fh> ) {
            next if $line !~ $re;
            my ( $header, $offset ) = ( $+{header}, $+{offset} );
            $self->_state( { header => $header, offset => $offset } );
            $self->log->info("objdump -D -F : Section $header at $offset");

            return 1;
        }
        $self->_clear_file;
        $self->_clear_filehandle;
        $self->_clear_state;
        return 0;
    };


    method section_offset returns (Int|Undef) {
        if ( not $self->_has_state ) {
            $self->log->logcroak('Invalid call to section_offset outside of file scan');
            return;
        }
        return hex( $self->_state->{offset} );
    };


    method section_size returns (Undef) {
        $self->log->logcroak('Can\'t perform section_size on this type of object.');
        return;
    };


    method section_name returns (Str|Undef) {
        if ( not $self->_has_state ) {
            $self->log->logcroak('Invalid call to section_name outside of file scan');
            return;
        }
        return $self->_state->{header};
    };


    method can_compute_size returns (Bool){
        return 0;
    };


    has _header_regex => ( isa => RegexpRef, ro, default => sub {
        return qr/<(?<header>[^>]+)>/;
    }, );


    has _offset_regex => ( isa => RegexpRef, ro, default => sub {
        ## no critic (RegularExpressions::ProhibitEnumeratedClasses)
        return qr/[(]File Offset:\s*(?<offset>0x[0-9a-f]+)[)]/;
    }, );


    has _section_header_identifier => ( isa => RegexpRef,  ro, lazy_build, );


    has _file                      => ( isa => File,       rw, clearer => '_clear_file', );


    has _filehandle                => ( isa => FileHandle, rw, clearer => '_clear_filehandle', );


    has _state                     => ( isa => HashRef,    rw,
      predicate => '_has_state', clearer => '_clear_state',
    );



    method _build__section_header_identifier returns (RegexpRef) {
        my $header = $self->_header_regex;
        my $offset = $self->_offset_regex;

        return qr/${header}\s*${offset}:/;
    };



    method _objdump returns (FileHandle|Undef){
        if ( open my $fh, q{-|}, q{objdump}, qw( -D -F ), $self->_file->cleanup->absolute ) {
            return $fh;
        }
        $self->log->logconfess(qq{An error occured requesting section data from objdump $^ $@ });
        return;
    };

};
1;




__END__