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