sbsv1_os/e32toolp/platform/sym_lkup_util.pl
changeset 0 83f4b4db085c
child 1 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 # Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 #
       
    11 # Contributors:
       
    12 #
       
    13 # Description:
       
    14 #
       
    15 
       
    16 use FindBin;		# for FindBin::Bin
       
    17 use Getopt::Long;
       
    18 
       
    19 BEGIN {
       
    20 # check user has a version of perl that will cope
       
    21 	require 5.005_03;
       
    22 # establish the path to the Perl libraries: currently the same directory as this script
       
    23 	$PerlLibPath = $FindBin::Bin;	# X:/epoc32/tools
       
    24 	$PerlLibPath =~ s/\//\\/g;	# X:\epoc32\tools
       
    25 	$PerlLibPath .= "\\";
       
    26 }
       
    27 
       
    28 use lib $PerlLibPath;
       
    29 
       
    30 my $IgnoreExportDir = 0;
       
    31 my $ImportSymFile;
       
    32 my $CppFile;
       
    33 my $mapFile;
       
    34 my $SegmentContentSz = 0;
       
    35 my $nSyms = 0;
       
    36 my $nImports = 0;
       
    37 my %Symbols = ();
       
    38 my @Imports = ();
       
    39 
       
    40 {
       
    41 	unless (GetOptions(\%Options, 'sym=s', 'o=s', 'map=s', 'ignore_export_dir')) {
       
    42 		exit 1;
       
    43 	}
       
    44 	$ImportSymFile = $Options{sym};
       
    45 	$CppFile = $Options{o};
       
    46 	$mapFile = $Options{map};
       
    47 	$IgnoreExportDir = $Options{ignore_export_dir};
       
    48 
       
    49 	ReadSymFile() if $ImportSymFile;
       
    50 	ReadMapFile() if $mapFile;
       
    51 	
       
    52 	GenNamedSegment();
       
    53 }
       
    54 
       
    55 sub ReadMapFile() {
       
    56 	open FILE, $mapFile or die "Error :SymLookup: Cannot open map file $mapFile\n";
       
    57 
       
    58 	my $GlbSyms = 0;
       
    59 	while(<FILE>) {
       
    60 		if($_ =~ /Public Symbols/) {
       
    61 			$GlbSyms = 1;
       
    62 		}
       
    63 		elsif( !$GlbSyms ) {
       
    64 			next;
       
    65 		}
       
    66 		if($_ =~ /([0-9a-fA-F]{8})\s+(\S+)\s+_?(\S+)\s/){
       
    67 			$addr = $1;
       
    68 			$module = $2;
       
    69 			$name = $3;
       
    70 			if( defined $Symbols{$name} ) {
       
    71 				$Symbols{$name} = $addr;
       
    72 			}
       
    73 		}
       
    74 	}
       
    75 #	Remove symbols not found in map file
       
    76 	foreach my $sym (keys %Symbols){
       
    77 		if( !$Symbols{$sym} ){
       
    78 			delete $Symbols{$sym};
       
    79 		}
       
    80 	}
       
    81 
       
    82 }
       
    83 
       
    84 sub ReadSymFile() {
       
    85 	open FILE, $ImportSymFile or die "Error :SymLookup: Cannot open file $ImportSymFile\n";
       
    86 	
       
    87 	my $ImportDirSeen = 0;
       
    88 	my $ExportDirSeen = 0;
       
    89 	my $ExportNameTblSeen = 0;
       
    90 	my $numOfExportNames = 0;
       
    91 	my $nameCount = 0;
       
    92 	while (<FILE>) {
       
    93 # Ignore export table(s) if the flag '$IgnoreExportDir' is set. This flag is set for stddlls, as symbol listing 
       
    94 # is not required for them. The windows API GetProcAddr can be used directly. While for stdexe, the symbol names
       
    95 # are filtered out and then looked up in the map file for their addresses.
       
    96 
       
    97 		if($_ =~ /\*\*\* EXPORT DIRECTORY \*\*\*/){
       
    98 			next if($IgnoreExportDir);
       
    99 			$ExportDirSeen = 1;
       
   100 		}
       
   101 		elsif($_ =~ /\*\*\* Export Name Pointer Table \*\*\*/){
       
   102 			next if($IgnoreExportDir);
       
   103 			$ExportNameTblSeen = 1;
       
   104 		}
       
   105 		elsif($_ =~ /\*\*\* IMPORT DIRECTORY \*\*\*/) {
       
   106 			$ImportDirSeen = 1;
       
   107 		}
       
   108 
       
   109 		if($ExportDirSeen){
       
   110 			if($_ =~ /numberofnames\s+=\s+0x(\S+)/){
       
   111 				$numOfExportNames = hex($1);
       
   112 # Reset the flag once done with the export table
       
   113 				$ExportDirSeen = 0;
       
   114 			}
       
   115 		}
       
   116 		elsif($ExportNameTblSeen && $numOfExportNames){
       
   117 			if($_ =~ /\d+\s+0x[0-9a-fA-F]+\s+(\S+)/){
       
   118 				$Symbols{$1}=0;
       
   119 # Keep track of the symbols seen in "Export Name Pointer Table"
       
   120 				$nameCount++;
       
   121 			}
       
   122 			if($nameCount == $numOfExportNames){
       
   123 # Reset the flag once done with the name table
       
   124 				$ExportNameTblSeen = 0;
       
   125 			}
       
   126 		}
       
   127 		elsif($ImportDirSeen) {
       
   128 			if($_ =~ /^DLL name\s+=\s+\S+\s+\((\S+)\)/) {
       
   129 				my $dllname = $1;
       
   130 				push @Imports, $dllname;
       
   131 				$ImportDirSeen = 0;
       
   132 			}
       
   133 		}
       
   134 	}
       
   135 }
       
   136 
       
   137 sub GenNamedSegment() {
       
   138 
       
   139 	my $SegContents = "";
       
   140 	
       
   141 	&Header(\$SegContents);
       
   142 
       
   143 	&SymAddrTbl(\$SegContents);
       
   144 
       
   145 	&SymNames(\$SegContents);
       
   146 
       
   147 	&Footer(\$SegContents);
       
   148 
       
   149 	open OUTFILE, ">$CppFile" or die "Error :SymLookup:Cannot open file $CppFile\n";
       
   150 	print OUTFILE $SegContents;
       
   151 }
       
   152 
       
   153 sub Header(){
       
   154 	my $SegContentsRef = shift @_;
       
   155 	
       
   156 	$$SegContentsRef .= "\/\* $CppFile\n";
       
   157 	$$SegContentsRef .= " \* Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).  All rights reserved.\n";
       
   158 	$$SegContentsRef .= " \* Makmake-generated source file for named symbol lookup\n";
       
   159 	$$SegContentsRef .= " \*\/\n";
       
   160 	$$SegContentsRef .= "#pragma data_seg(\".expdata\")";
       
   161 
       
   162 	$$SegContentsRef .= "\n\n";
       
   163 }
       
   164 
       
   165 sub Footer() {
       
   166 	my $SegContentsRef = shift @_;
       
   167 	$$SegContentsRef .= "\n#pragma data_seg()\n";
       
   168 	$$SegContentsRef .= "\n";
       
   169 }
       
   170 
       
   171 sub SymAddrTbl(){
       
   172 	my $SegContentsRef = shift @_;
       
   173 	$nSyms = keys %Symbols;
       
   174 	$nImports = @Imports;
       
   175 	$$SegContentsRef .= "int NSymbols = $nSyms;\n";
       
   176 	$$SegContentsRef .= "int NImports = $nImports;\n";
       
   177 
       
   178 	if(!$nSyms) {
       
   179 		return;
       
   180 	}
       
   181 	$$SegContentsRef .= "int addresses[] = {\n";
       
   182 	
       
   183 	foreach $key (sort keys %Symbols) {
       
   184 		if($Symbols{$key}){
       
   185 			$$SegContentsRef .= "\t0x".$Symbols{$key}.",\n";
       
   186 		}
       
   187 	}
       
   188 	$$SegContentsRef .= "};\n";
       
   189 }
       
   190 
       
   191 sub SymNames() {
       
   192 	if(!$nImports && !$nSyms){
       
   193 		return;
       
   194 	}
       
   195 	my $SegContentsRef = shift @_;
       
   196 	$$SegContentsRef .= "\nchar data[] = {\n";
       
   197 
       
   198 	my $symnames ;
       
   199 	my $colCnt ;
       
   200 	foreach $symnames (sort keys %Symbols) {
       
   201 		next if( $Symbols{$symnames} == 0);
       
   202 		my @chars = split(//,$symnames);
       
   203 		$$SegContentsRef .= "\t";
       
   204 		$colCnt =0;
       
   205 		foreach $aChar (@chars) {
       
   206 			if($colCnt >= 80) {
       
   207 				$$SegContentsRef .= "\\\n\t";
       
   208 				$colCnt = 0;
       
   209 			}
       
   210 			$$SegContentsRef .= "\'$aChar\',";
       
   211 			$colCnt += 4;
       
   212 		}
       
   213 		$$SegContentsRef .= "0,\n";
       
   214 	}
       
   215 
       
   216 	foreach my $dll (@Imports) {
       
   217 		my @chars = split(//,$dll);
       
   218 		$$SegContentsRef .= "\t";
       
   219 		$colCnt =0;
       
   220 		foreach $aChar (@chars) {
       
   221 			if($colCnt >= 80) {
       
   222 				$$SegContentsRef .= "\\\n\t";
       
   223 				$colCnt = 0;
       
   224 			}
       
   225 			$$SegContentsRef .= "\'$aChar\',";
       
   226 			$colCnt += 4;
       
   227 		}
       
   228 		$$SegContentsRef .= "0,\n";
       
   229 	}
       
   230 	$$SegContentsRef .= "\n};\n";
       
   231 }
       
   232