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