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