File: ELF32.pm

package info (click to toggle)
libdata-parsebinary-perl 0.31~dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 440 kB
  • sloc: perl: 4,386; makefile: 2
file content (155 lines) | stat: -rwxr-xr-x 3,799 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package Data::ParseBinary::Executable::ELF32;
use strict;
use warnings;
use Data::ParseBinary;

#"""
#Executable and Linkable Format (ELF), 32 bit, little endian
#Used on *nix systems as a replacement of the older a.out format
#"""

my $elf32_program_header = Struct("program_header",
    Enum(ULInt32("type"),
        NULL => 0,
        LOAD => 1,
        DYNAMIC => 2,
        INTERP => 3,
        NOTE => 4,
        SHLIB => 5,
        PHDR => 6,
        _default_ => $DefaultPass,
    ),
    ULInt32("offset"),
    ULInt32("vaddr"),
    ULInt32("paddr"),
    ULInt32("file_size"),
    ULInt32("mem_size"),
    ULInt32("flags"),
    ULInt32("align"),
);

my $elf32_section_header = Struct("section_header",
    ULInt32("name_offset"),
    Pointer(sub { $_->ctx(2)->{strtab_data_offset} + $_->ctx->{name_offset} },
        CString("name")
    ),
    Enum(ULInt32("type"), 
        NULL => 0,
        PROGBITS => 1,
        SYMTAB => 2,
        STRTAB => 3,
        RELA => 4,
        HASH => 5,
        DYNAMIC => 6,
        NOTE => 7,
        NOBITS => 8,
        REL => 9,
        SHLIB => 10,
        DYNSYM => 11,
        _default_ => $DefaultPass,
    ),
    ULInt32("flags"),
    ULInt32("addr"),
    ULInt32("offset"),
    ULInt32("size"),
    ULInt32("link"),
    ULInt32("info"),
    ULInt32("align"),
    ULInt32("entry_size"),
    Pointer(sub { $_->ctx->{offset} },
        Field("data", sub { $_->ctx->{size} })
    ),
);

our $elf32_parser = Struct("elf32_file",
    Struct("identifier",
        Const(Bytes("magic", 4), "\x7fELF"),
        Enum(Byte("file_class"),
            NONE => 0,
            CLASS32 => 1,
            CLASS64 => 2,
        ),
        Enum(Byte("encoding"),
            NONE => 0,
            LSB => 1,
            MSB => 2,            
        ),
        Byte("version"),
        Padding(9),
    ),
    Enum(ULInt16("type"),
        NONE => 0,
        RELOCATABLE => 1,
        EXECUTABLE => 2,
        SHARED => 3,
        CORE => 4,
    ),
    Enum(ULInt16("machine"),
        NONE => 0,
        M32 => 1,
        SPARC => 2,
        I386 => 3,
        Motorolla68K => 4,
        Motorolla88K => 5,
        Intel860 => 7,
        MIPS => 8,
    ),
    ULInt32("version"),
    ULInt32("entry"),
    ULInt32("ph_offset"),
    ULInt32("sh_offset"),
    ULInt32("flags"),
    ULInt16("header_size"),
    ULInt16("ph_entry_size"),
    ULInt16("ph_count"),
    ULInt16("sh_entry_size"),
    ULInt16("sh_count"),
    ULInt16("strtab_section_index"),
    
    # calculate the string table data offset (pointer arithmetics)
    # ugh... anyway, we need it in order to read the section names, later on
    Pointer(sub { $_->ctx->{sh_offset} + $_->ctx->{strtab_section_index} * $_->ctx->{sh_entry_size} + 16 },
        ULInt32("strtab_data_offset"),
    ),
    
    # program header table
    Pointer(sub { $_->ctx->{ph_offset} },
        Array(sub { $_->ctx->{ph_count} },
            $elf32_program_header
        )
    ),
    
    # section table
    Pointer(sub { $_->ctx->{sh_offset} },
        Array(sub { $_->ctx->{sh_count} },
            $elf32_section_header
        )
    ),
);

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw($elf32_parser);


1;

__END__

=head1 NAME

Data::ParseBinary::Executable::ELF32 - Parsing UNIX's SO files

=head1 SYNOPSIS

    use Data::ParseBinary::Executable::ELF32 qw{$elf32_parser};
    my $data = $elf32_parser->parse(CreateStreamReader(File => $fh));

Can parse and re-build UNIX "so" files.

This is a part of the Data::ParseBinary package, and is just one ready-made parser.
please go to the main page for additional usage info.

No known issues

=cut