bintools/elftools/elf2inf.pl
changeset 0 044383f39525
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 #
       
     2 # Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of the License "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 #
       
    16 
       
    17 use Getopt::Long;
       
    18 use strict;
       
    19 
       
    20 my $outputFileName = "default.inf";
       
    21 my $discard_noncallable_exports = 0;
       
    22 my @inputFiles;
       
    23 my @exports = ();
       
    24 my %symsSeen = ();
       
    25 
       
    26 #
       
    27 
       
    28 &args;
       
    29 &main;
       
    30 
       
    31 
       
    32 sub args ()
       
    33 {
       
    34     my %opts = ();
       
    35     my $result = GetOptions(\%opts, "output:s", "discard_noncallable_exports");
       
    36     $outputFileName = $opts{'output'} if $opts{'output'} ;
       
    37     $discard_noncallable_exports = 1 if $opts{'discard_noncallable_exports'};
       
    38     @inputFiles = @ARGV;
       
    39 }
       
    40 
       
    41 sub main ()
       
    42 {
       
    43 
       
    44     foreach my $file (@inputFiles){
       
    45 	ReadFromElf($file);
       
    46     }
       
    47     open( INFFILE, ">$outputFileName") or 
       
    48 	die "Error: can't open $outputFileName\n";
       
    49     foreach my $export (sort {$a cmp $b } @exports) {
       
    50 	print INFFILE $export;
       
    51     }
       
    52     close INFFILE;
       
    53 }
       
    54 
       
    55 sub ReadFromElf ($)
       
    56 {
       
    57     my ($filename) = @_;
       
    58     my $line;
       
    59     %symsSeen = ();
       
    60     open(FROMELF, "getexports $filename|")
       
    61 	or die "Error: can't open $filename\n";
       
    62 
       
    63     while ($_ = <FROMELF> ) 
       
    64     {
       
    65 	    if ($_ =~ /^\s*(\S+)\s+((DATA)\s+(\d+))?\s*$/) {
       
    66 		my $sym = $1;
       
    67 		my $data = "";
       
    68 		unless ($symsSeen{$sym}) { 
       
    69 		    $symsSeen{$sym} = $sym;
       
    70 			
       
    71 			if($2){
       
    72 #			Indicate if this is a data symbol along with its size.
       
    73 			if($4){
       
    74 				$data = " DATA $4";
       
    75 				}
       
    76 			}
       
    77 
       
    78 		    if ($sym =~ /^_ZTV/ && !$discard_noncallable_exports) { # allow vtables thru
       
    79 			push @exports, " _$sym (#<VT>#)\n";
       
    80 		    } elsif ($sym =~ /^_ZTI/ && !$discard_noncallable_exports) { # allow type info  thru: comment out if we don't want it
       
    81 			push @exports, " _$sym (#<TI>#)\n";
       
    82 		    } elsif ($sym =~ /^_ZThn\d/) { # allow thunk  thru
       
    83 			push @exports, " _$sym (#<thunk>#)\n";
       
    84 		    } elsif ($sym =~ /^_ZT/) { # punt all other type stuff: shouldn't be exported anyway
       
    85 			next;
       
    86 		    } elsif (C9Ctor($sym)) { #work around non-export of constructors
       
    87 			PushCtors($sym, $filename);
       
    88 		    } elsif (D9Dtor($sym)) { #work around non-export of constructors
       
    89 			PushDtors($sym, $filename);
       
    90 		    } else {
       
    91 			push @exports, " _$sym$data\n";
       
    92 		    }
       
    93 		}
       
    94 	    }
       
    95 	}
       
    96     close FROMELF;
       
    97 }
       
    98 
       
    99 sub PushSym($$)
       
   100 {
       
   101     my ($s, $f) = @_;
       
   102     unless ($symsSeen{$s}) {
       
   103 	$symsSeen{$s} = $s;
       
   104 	push @exports, " _$s\n";
       
   105     }
       
   106 }
       
   107 
       
   108 sub simpleC9Ctor($)
       
   109 {
       
   110     my ($sym) = @_;
       
   111     if ($sym =~ /_ZN(\d+)(.+)C9E.*$/) {
       
   112 	# Really check it
       
   113 	return 1 if ($1 == length($2));
       
   114     } 
       
   115     return 0;
       
   116 }
       
   117 
       
   118 sub simpleD9Ctor($)
       
   119 {
       
   120     my ($sym) = @_;
       
   121     if ($sym =~ /_ZN(\d+)(.+)D9E.*$/) {
       
   122 	# Really check it
       
   123 	return 1 if ($1 == length($2));
       
   124     } 
       
   125     return 0;
       
   126 }
       
   127 
       
   128 sub C9Ctor($)
       
   129 {
       
   130     my ($sym) = @_;
       
   131     return 1 if simpleC9Ctor($sym);
       
   132     if ($sym =~ /_ZN(\d+)(.+)C9E.*$/) {
       
   133 	my $l = $1;
       
   134 	my $s = $2;
       
   135 	my $t_params = substr $s, $l;
       
   136 	return 1 if ($t_params =~ /^I.*I$/); # not totally convincing check for templated ctor
       
   137     }
       
   138     return 0;
       
   139 }
       
   140 
       
   141 sub PushCtors($$)
       
   142 {
       
   143     my ($sym, $filename) = @_;
       
   144     return PushSimpleCtors($sym, $filename) if (simpleC9Ctor($sym));
       
   145 
       
   146     my ($sym, $filename) = @_;
       
   147     $sym =~ /(_ZN\d+.+IC)9E.*$/o;
       
   148     my $s = $1;
       
   149     my $i = length $s;
       
   150     my $e = substr $sym, $i + 1;
       
   151     my $C1 = "$s"."1"."$e";
       
   152     my $C2 = "$s"."2"."$e";
       
   153     push @exports, " _$sym\n";
       
   154     PushSym($C1, $filename);
       
   155     PushSym($C2, $filename);
       
   156 
       
   157 } 
       
   158 
       
   159 sub PushSimpleCtors($$)
       
   160 {
       
   161     my ($sym, $filename) = @_;
       
   162     $sym =~ /(_ZN\d+.+C)9E.*$/o;
       
   163     my $s = $1;
       
   164     my $i = length $s;
       
   165     my $e = substr $sym, $i + 1;
       
   166     my $C1 = "$s"."1"."$e";
       
   167     my $C2 = "$s"."2"."$e";
       
   168     push @exports, " _$sym\n";
       
   169     PushSym($C1, $filename);
       
   170     PushSym($C2, $filename);
       
   171 }
       
   172 
       
   173 sub D9Dtor($)
       
   174 {
       
   175     my ($sym) = @_;
       
   176     return 1 if simpleD9Ctor($sym);
       
   177     if ($sym =~ /_ZN(\d+)(.+)D9E.*$/) {
       
   178 	my $l = $1;
       
   179 	my $s = $2;
       
   180 	my $t_params = substr $s, $l;
       
   181 	return 1 if ($t_params =~ /^I.*I$/); # not totally convincing check for templated ctor
       
   182     }
       
   183     return 0;
       
   184 }
       
   185 
       
   186 sub PushDtors($$)
       
   187 {
       
   188     my ($sym, $filename) = @_;
       
   189     return PushSimpleDtors($sym, $filename) if (simpleD9Ctor($sym));
       
   190 
       
   191     my ($sym, $filename) = @_;
       
   192     $sym =~ /(_ZN\d+.+ID)9E.*$/o;
       
   193     my $s = $1;
       
   194     my $i = length $s;
       
   195     my $e = substr $sym, $i + 1;
       
   196     my $D0 = "$s"."0"."$e";
       
   197     my $D1 = "$s"."1"."$e";
       
   198     my $D2 = "$s"."2"."$e";
       
   199     push @exports, " _$sym\n";
       
   200     PushSym($D0, $filename);
       
   201     PushSym($D1, $filename);
       
   202     PushSym($D2, $filename);
       
   203 } 
       
   204 
       
   205 
       
   206 sub PushSimpleDtors($$)
       
   207 {
       
   208     my ($sym, $filename) = @_;
       
   209     $sym =~ /(_ZN\d+.+D)9E.*$/o;
       
   210     my $s = $1;
       
   211     my $i = length $s;
       
   212     my $e = substr $sym, $i + 1;
       
   213     my $D0 = "$s"."0"."$e";
       
   214     my $D1 = "$s"."1"."$e";
       
   215     my $D2 = "$s"."2"."$e";
       
   216     push @exports, " _$sym\n";
       
   217     PushSym($D0, $filename);
       
   218     PushSym($D1, $filename);
       
   219     PushSym($D2, $filename);
       
   220 }
       
   221