| 1 | package Sys::Export::ELF; | ||
| 2 | |||
| 3 | # ABSTRACT: Unpack various data structures of an ELF binary | ||
| 4 | our $VERSION = '0.006'; # VERSION | ||
| 5 | |||
| 6 | |||
| 7 | use v5.26; | ||
| 8 | use warnings; | ||
| 9 | use experimental qw( signatures ); | ||
| 10 | use Carp; | ||
| 11 | use Scalar::Util 'dualvar'; | ||
| 12 | use Sys::Export '_unpack'; | ||
| 13 | |||
| 14 | sub _make_enum { | ||
| 15 | my $i= 0; | ||
| 16 | map +(defined? dualvar($i++, $_) : undef), @_; | ||
| 17 | } | ||
| 18 | |||
| 19 | our $elf_common_header_packstr= 'a4 C C C C C a7'; | ||
| 20 | our @elf_common_header_fields= qw( magic class data header_version osabi osabi_version padding ); | ||
| 21 | |||
| 22 | our @elf_header_fields= qw( type machine version entry_point segment_table_ofs section_table_ofs | ||
| 23 | flags elf_header_len segment_header_elem_len segment_count | ||
| 24 | section_header_elem_len section_count section_name_string_table_idx ); | ||
| 25 | our @elf_header_packstr= ( | ||
| 26 | 'a16 v v V V V V V v v v v v v', # 32-bit LE | ||
| 27 | 'a16 n n N N N N N n n n n n n', # 32-bit BE | ||
| 28 | 'a16 v v V Q< Q< Q< V v v v v v v', # 64-bit LE | ||
| 29 | 'a16 n n N Q> Q> Q> N n n n n n n', # 64-bit BE | ||
| 30 | ); | ||
| 31 | our @segment_header_len= ( 32, 32, 56, 56 ); | ||
| 32 | # 'flags' moves depending on 32 vs 64 bit, so changing the pack string isn't enough | ||
| 33 | our @segment_header_fields= ( | ||
| 34 | [ qw( type offset virt_addr phys_addr filesize memsize flags align ) ], | ||
| 35 | [ qw( type offset virt_addr phys_addr filesize memsize flags align ) ], | ||
| 36 | [ qw( type flags offset virt_addr phys_addr filesize memsize align ) ], | ||
| 37 | [ qw( type flags offset virt_addr phys_addr filesize memsize align ) ], | ||
| 38 | ); | ||
| 39 | our @segment_header_packstr= ( | ||
| 40 | 'V V V V V V V V', | ||
| 41 | 'N N N N N N N N', | ||
| 42 | 'V V Q< Q< Q< Q< Q< Q<', | ||
| 43 | 'N N Q> Q> Q> Q> Q> Q>', | ||
| 44 | ); | ||
| 45 | our @segment_header_type= _make_enum qw( NULL LOAD DYNAMIC INTERP NOTE SHLIB PHDR TLS ); | ||
| 46 | |||
| 47 | our @section_header_len= ( 40, 40, 64, 64 ); | ||
| 48 | our @section_header_fields= qw( name type flags addr offset size link info align entry_size ); | ||
| 49 | our @section_header_packstr= ( | ||
| 50 | 'V V V V V V V V V V', | ||
| 51 | 'N N N N N N N N N N', | ||
| 52 | 'V V Q< Q< Q< Q< V V Q< Q<', | ||
| 53 | 'N N Q> Q> Q> Q> N N Q> Q>', | ||
| 54 | ); | ||
| 55 | |||
| 56 | our @dynamic_link_entry_len= ( 8, 8, 16, 16 ); | ||
| 57 | our @dynamic_link_entry_fields= qw( tag val ); | ||
| 58 | our @dynamic_link_entry_packstr= ( | ||
| 59 | 'V V', | ||
| 60 | 'N N', | ||
| 61 | 'Q< Q<', | ||
| 62 | 'Q> Q>', | ||
| 63 | ); | ||
| 64 | our @dynamic_link_entry_tag= _make_enum qw( NULL NEEDED PLTRELSZ PLTGOT HASH STRTAB SYMTAB RELA | ||
| 65 | RELASZ RELAENT STRSZ SYMENT INIT FINI SONAME RPATH SYMBOLIC REL RELSZ RELENT PLTREL DEBUG | ||
| 66 | TEXTREL JMPREL BIND_NOW INIT_ARRAY FINI_ARRAY INIT_ARRAYSZ FINI_ARRAYSZ RUNPATH FLAGS | ||
| 67 | ENCODING PREINIT_ARRAY PREINIT_ARRAYSZ SYMTAB_SHNDX RELRSZ RELR RELRENT NUM ); | ||
| 68 | |||
| 69 | our @symbol_table_fields= ( | ||
| 70 | [qw( name value size info other shndx )], | ||
| 71 | [qw( name value size info other shndx )], | ||
| 72 | [qw( name info other shndx value size )], | ||
| 73 | [qw( name info other shndx value size )], | ||
| 74 | ); | ||
| 75 | our @symbol_table_packstr= ( | ||
| 76 | 'V V V C C v', | ||
| 77 | 'N N N C C n', | ||
| 78 | 'V C C v Q< Q<', | ||
| 79 | 'N C C n Q> Q>', | ||
| 80 | ); | ||
| 81 | |||
| 82 | our @relocation_fields= qw( offset info ); | ||
| 83 | our @relocation_packstr= ( | ||
| 84 | 'V V', | ||
| 85 | 'N N', | ||
| 86 | 'Q< Q<', | ||
| 87 | 'Q> Q>', | ||
| 88 | ); | ||
| 89 | |||
| 90 | our @relocation_addend_fields= qw( offset info addend ); | ||
| 91 | our @relocation_addend_packstr= ( | ||
| 92 | 'V V V', | ||
| 93 | 'N N N', | ||
| 94 | 'Q< Q< Q<', | ||
| 95 | 'Q> Q> Q>', | ||
| 96 | ); | ||
| 97 | |||
| 98 | sub _strz_from_offset { | ||
| 99 | return undef unless 0 <= $_[1] && $_[1] < length $_[0]; | ||
| 100 | pos $_[0] = $_[1]; | ||
| 101 | $_[0] =~ /\G([^\0]*)/? $1 : undef; | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | sub unpack { | ||
| 106 | # This doesn't copy $_[0] into a variable because it might be a memory-map | ||
| 107 | my %elf; | ||
| 108 | |||
| 109 | # Start with the encoding-independent fields | ||
| 110 | @elf{@elf_common_header_fields}= _unpack($elf_common_header_packstr, $_[0]); | ||
| 111 | $elf{magic} eq "\x7FELF" or return undef; | ||
| 112 | die "Unsupported 'class'" unless 1 <= $elf{class} && $elf{class} <= 2; | ||
| 113 | die "Unsupported 'data'" unless 1 <= $elf{data} && $elf{data} <= 2; | ||
| 114 | my $encoding_idx= ($elf{class}-1)*2 + ($elf{data}-1); | ||
| 115 | |||
| 116 | # Now decode the endian and bit-length-varying fields | ||
| 117 | (undef, @elf{@elf_header_fields})= _unpack $elf_header_packstr[$encoding_idx], $_[0]; | ||
| 118 | my $lim= length $_[0]; | ||
| 119 | |||
| 120 | # parse segments | ||
| 121 | my @segments; | ||
| 122 | # sanity check on table size | ||
| 123 | my $elem_len= $elf{segment_header_elem_len}; | ||
| 124 | if ($elf{segment_count} > 0) { | ||
| 125 | $elf{segment_table_ofs} < $lim | ||
| 126 | or croak "Segment table beyond end of file"; | ||
| 127 | $elem_len >= $segment_header_len[$encoding_idx] | ||
| 128 | or croak "Segment records are shorter than expected"; | ||
| 129 | $elf{segment_count} <= (($lim - $elf{segment_table_ofs}) / $elem_len) | ||
| 130 | or croak "Segment table extends past end of file"; | ||
| 131 | |||
| 132 | for (my $i= 0; $i < $elf{segment_count}; $i++) { | ||
| 133 | my $ofs= $elf{segment_table_ofs} + $i * $elem_len; | ||
| 134 | my %segment; | ||
| 135 | @segment{@{$segment_header_fields[$encoding_idx]}} | ||
| 136 | = _unpack $segment_header_packstr[$encoding_idx], | ||
| 137 | substr($_[0], $ofs, $elem_len); | ||
| 138 | $segment{type}= $segment_header_type[$segment{type}] | ||
| 139 | if $segment{type} > 0 && $segment{type} < @segment_header_type; | ||
| 140 | push @segments, \%segment; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | $elf{segments}= \@segments; | ||
| 144 | |||
| 145 | $elem_len= $elf{section_header_elem_len}; | ||
| 146 | my @sections; | ||
| 147 | my $dynamic_section; | ||
| 148 | if ($elf{section_count} > 0) { | ||
| 149 | $elf{section_table_ofs} < $lim | ||
| 150 | or croak "Section table beyond end of file"; | ||
| 151 | $elem_len >= $section_header_len[$encoding_idx] | ||
| 152 | or croak "Section records are shorter than expected"; | ||
| 153 | $elf{section_count} <= (($lim - $elf{section_table_ofs}) / $elem_len) | ||
| 154 | or croak "Section table extends past end of file"; | ||
| 155 | |||
| 156 | for (my $i= 0; $i < $elf{segment_count}; $i++) { | ||
| 157 | my $ofs= $elf{section_table_ofs} + $i * $elem_len; | ||
| 158 | my %section; | ||
| 159 | @section{@section_header_fields} | ||
| 160 | = _unpack $section_header_packstr[$encoding_idx], | ||
| 161 | substr($_[0], $ofs, $elem_len); | ||
| 162 | push @sections, \%section; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | $elf{sections}= \@sections; | ||
| 166 | |||
| 167 | for my $seg (@segments) { | ||
| 168 | if ($seg->{type} eq 'DYNAMIC') { | ||
| 169 | $elf{dynamic} and croak "Found multiple PT_DYNAMIC sections?"; | ||
| 170 | $seg->{offset} >= 0 && $seg->{offset} + $seg->{filesize} < $lim | ||
| 171 | or croak "Dynamic section extends beyond end of file"; | ||
| 172 | my @dynamic_entries; | ||
| 173 | for (my $ofs= 0; $ofs < $seg->{filesize}; $ofs += $dynamic_link_entry_len[$encoding_idx]) { | ||
| 174 | my %dynamic; | ||
| 175 | @dynamic{@dynamic_link_entry_fields} | ||
| 176 | = _unpack $dynamic_link_entry_packstr[$encoding_idx], | ||
| 177 | substr($_[0], $seg->{offset}+$ofs, $dynamic_link_entry_len[$encoding_idx]); | ||
| 178 | $dynamic{tag}= $dynamic_link_entry_tag[$dynamic{tag}] | ||
| 179 | if $dynamic{tag} > 0 && $dynamic{tag} < @dynamic_link_entry_tag; | ||
| 180 | |||
| 181 | last if $dynamic{tag} == 0; | ||
| 182 | |||
| 183 | push @dynamic_entries, \%dynamic; | ||
| 184 | if ($dynamic{tag} eq 'STRTAB') { | ||
| 185 | defined $elf{string_table_offset} | ||
| 186 | and croak "Found multiple STRTAB?"; | ||
| 187 | $elf{string_table_offset}= $dynamic{val}; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | if (defined $elf{string_table_offset}) { | ||
| 191 | # now that string table is known, decode the needed_libraries | ||
| 192 | my @needed; | ||
| 193 | for (@dynamic_entries) { | ||
| 194 | if ($_->{tag} eq 'NEEDED') { | ||
| 195 | # Read a NUL-terminated string from this offset in the string table | ||
| 196 | my $str= _strz_from_offset($_[0], $elf{string_table_offset} + $_->{val}); | ||
| 197 | push @needed, $str if length $str; | ||
| 198 | } | ||
| 199 | elsif ($_->{tag} eq 'RPATH' || $_->{tag} eq 'RUNPATH') { | ||
| 200 | my $str= _strz_from_offset($_[0], $elf{string_table_offset} + $_->{val}); | ||
| 201 | $elf{rpath}= $str if length $str; | ||
| 202 | } | ||
| 203 | elsif ($_->{tag} eq 'SONAME') { | ||
| 204 | my $str= _strz_from_offset($_[0], $elf{string_table_offset} + $_->{val}); | ||
| 205 | $elf{soname}= $str if length $str; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | $elf{needed_libraries}= \@needed; | ||
| 209 | } | ||
| 210 | $elf{dynamic}= \@dynamic_entries; | ||
| 211 | } | ||
| 212 | elsif ($seg->{type} eq 'INTERP') { | ||
| 213 | my $str= _strz_from_offset($_[0], $seg->{offset}); | ||
| 214 | $elf{interpreter}= $str if length $str; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | \%elf; | ||
| 219 | } | ||
| 220 | |||
| 221 | # Avoiding dependency on namespace::clean | ||
| 222 | delete @{Sys::Export::ELF::}{qw( croak carp confess dualvar _unpack )}; | ||
| 223 | 1; | ||
| 224 | |||
| 225 | __END__ |