655
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 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 |
|
600
|
18 |
#include <boost/regex.hpp>
|
|
19 |
#define MAX_LINE 65535
|
|
20 |
#include "symbolgenerator.h"
|
|
21 |
#include "e32image.h"
|
|
22 |
|
|
23 |
#if defined(__LINUX__)
|
|
24 |
#define PATH_SEPARATOR '/'
|
|
25 |
#else
|
|
26 |
#define PATH_SEPARATOR '\\'
|
|
27 |
#endif
|
|
28 |
|
|
29 |
boost::mutex SymbolGenerator::iMutexSingleton;
|
|
30 |
SymbolGenerator* SymbolGenerator::iInst = NULL;
|
|
31 |
SymbolGenerator* SymbolGenerator::GetInstance(){
|
|
32 |
iMutexSingleton.lock();
|
|
33 |
if(iInst == NULL) {
|
|
34 |
iInst = new SymbolGenerator();
|
|
35 |
}
|
|
36 |
iMutexSingleton.unlock();
|
|
37 |
return iInst;
|
|
38 |
}
|
|
39 |
void SymbolGenerator::Release() {
|
|
40 |
iMutexSingleton.lock();
|
|
41 |
if(iInst != NULL) {
|
|
42 |
delete iInst;
|
|
43 |
iInst = NULL;
|
|
44 |
}
|
|
45 |
iMutexSingleton.unlock();
|
|
46 |
}
|
|
47 |
void SymbolGenerator::SetSymbolFileName( const string& fileName ){
|
|
48 |
if(iSymFile.is_open())
|
|
49 |
iSymFile.close();
|
|
50 |
string s = fileName.substr(0,fileName.rfind('.'))+".symbol";
|
|
51 |
printf("* Writing %s - ROFS symbol file\n", s.c_str());
|
|
52 |
iSymFile.open(s.c_str());
|
|
53 |
}
|
|
54 |
void SymbolGenerator::AddFile( const string& fileName, bool isExecutable ){
|
|
55 |
iMutex.lock();
|
|
56 |
iQueueFiles.push(TPlacedEntry(fileName,isExecutable));
|
|
57 |
iMutex.unlock();
|
|
58 |
iCond.notify_all();
|
|
59 |
}
|
|
60 |
void SymbolGenerator::ProcessExecutable( const string& fileName ){
|
|
61 |
char str[MAX_LINE];
|
|
62 |
string outString;
|
|
63 |
outString = "\nFrom ";
|
|
64 |
outString += fileName + "\n\n";
|
|
65 |
iSymFile.write(outString.c_str(),outString.length());
|
|
66 |
string mapFile2 = fileName+".map";
|
|
67 |
size_t dot = fileName.rfind('.');
|
|
68 |
string mapFile = fileName.substr(0,dot)+".map";
|
|
69 |
ifstream fMap;
|
|
70 |
fMap.open(mapFile2.c_str());
|
|
71 |
if(!fMap.is_open()) {
|
|
72 |
fMap.open(mapFile.c_str());
|
|
73 |
}
|
|
74 |
|
|
75 |
if(!fMap.is_open()) {
|
|
76 |
printf("%s\nWarning: Can't open \"%s\" or \"%s\"\n",fileName.c_str(),mapFile2.c_str(),mapFile.c_str());
|
|
77 |
int binSize = GetSizeFromBinFile(fileName);
|
|
78 |
memset(str,0,sizeof(str));
|
|
79 |
sprintf(str,"%04x", binSize);
|
|
80 |
outString = "00000000 ";
|
|
81 |
outString += str;
|
|
82 |
outString += " ";
|
|
83 |
outString += fileName.substr(fileName.rfind(PATH_SEPARATOR)+1)+"\n";
|
|
84 |
iSymFile.write(outString.c_str(),outString.length());
|
|
85 |
}
|
|
86 |
else {
|
|
87 |
if(!fMap.good()) fMap.clear();
|
|
88 |
boost::regex regARMV5("ARMV5", boost::regex::icase);
|
|
89 |
boost::regex regGCCEoARMV4("(GCCE|ARMV4)", boost::regex::icase);
|
|
90 |
boost::cmatch what;
|
655
|
91 |
if(regex_search(fileName.c_str(), what, regARMV5)) {
|
600
|
92 |
ProcessArmv5File(fileName, fMap);
|
|
93 |
}
|
655
|
94 |
else if(regex_search(fileName.c_str(), what, regGCCEoARMV4)) {
|
600
|
95 |
ProcessGcceOrArm4File(fileName, fMap);
|
|
96 |
}
|
|
97 |
else {
|
|
98 |
printf("\nWarning: cannot determine linker type used to create %s\n",fileName.c_str());
|
|
99 |
outString = "00000000 0000 ";
|
|
100 |
outString += fileName.substr(fileName.rfind(PATH_SEPARATOR)+1)+"\n";
|
|
101 |
iSymFile.write(outString.c_str(),outString.length());
|
|
102 |
}
|
|
103 |
}
|
|
104 |
}
|
|
105 |
void SymbolGenerator::ProcessDatafile( const string& fileName ){
|
|
106 |
string line = "\nFrom "+fileName+"\n\n00000000 0000 "+fileName.substr(fileName.rfind(PATH_SEPARATOR)+1)+"\n";
|
|
107 |
iSymFile.write(line.c_str(),line.length());
|
|
108 |
}
|
|
109 |
void SymbolGenerator::ProcessArmv5File( const string& fileName, ifstream& aMap ){
|
|
110 |
aMap.seekg (0, ios::beg);
|
|
111 |
char str[MAX_LINE];
|
|
112 |
string outString;
|
|
113 |
aMap.getline(str,MAX_LINE);
|
|
114 |
boost::cmatch what;
|
|
115 |
boost::regex reg("^ARM Linker");
|
|
116 |
if(!regex_search(str, what, reg)) {
|
|
117 |
printf("\nWarning: expecting %s to be generated by ARM linker\n", fileName.c_str());
|
|
118 |
outString = "00000000 0000 "+fileName.substr(fileName.rfind(PATH_SEPARATOR)+1)+"\n";
|
|
119 |
iSymFile.write(outString.c_str(),outString.length());
|
|
120 |
}
|
|
121 |
reg.assign("Global Symbols");
|
|
122 |
while(aMap.getline(str,MAX_LINE)) {
|
|
123 |
if(regex_search(str, what, reg)) {
|
|
124 |
break;
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
reg.assign("^\\s*(.+)\\s*0x(\\S+)\\s+[^\\d]*(\\d+)\\s+(.*)$");
|
|
129 |
string sSym,sTmp,sSection;
|
|
130 |
unsigned int addr,size,baseOffset = 0;
|
|
131 |
map<unsigned int,string> syms;
|
|
132 |
char symString[MAX_LINE];
|
|
133 |
while(aMap.getline(str,MAX_LINE)) {
|
|
134 |
if(regex_search(str, what, reg)) {
|
|
135 |
sSym.assign(what[1].first,what[1].second-what[1].first);
|
|
136 |
sTmp.assign(what[2].first,what[2].second-what[2].first);
|
|
137 |
addr = strtol(sTmp.c_str(), NULL, 16);
|
|
138 |
sTmp.assign(what[3].first,what[3].second-what[3].first);
|
|
139 |
size = strtol(sTmp.c_str(), NULL, 10);
|
|
140 |
sSection.assign(what[4].first,what[4].second-what[4].first);
|
|
141 |
if(sSection.find("(StubCode)") != string::npos)
|
|
142 |
size = 8;
|
|
143 |
if(addr > 0) {
|
|
144 |
memset(symString,0,sizeof(symString));
|
|
145 |
sprintf(symString,"%04x ",size);
|
|
146 |
outString = symString;
|
|
147 |
outString += sSym+" ";
|
|
148 |
outString += sSection;
|
|
149 |
if(baseOffset == 0)
|
|
150 |
baseOffset = addr;
|
|
151 |
unsigned int k = addr - baseOffset;
|
|
152 |
if( (syms.find(k) == syms.end()) || size != 0)
|
|
153 |
syms[k] = outString;
|
|
154 |
}
|
|
155 |
// end of addr>0
|
|
156 |
}
|
|
157 |
// end of regex_search
|
|
158 |
}
|
|
159 |
|
|
160 |
map<unsigned int,string>::iterator it;
|
|
161 |
for(it = syms.begin(); it != syms.end(); it++) {
|
|
162 |
memset(str,0,sizeof(str));
|
|
163 |
sprintf(str,"%08x",it->first);
|
|
164 |
outString = str;
|
|
165 |
outString += " ";
|
|
166 |
outString += it->second+"\n";
|
|
167 |
iSymFile.write(outString.c_str(),outString.length());
|
|
168 |
}
|
|
169 |
}
|
|
170 |
void SymbolGenerator::ProcessGcceOrArm4File( const string& fileName, ifstream& aMap ){
|
|
171 |
aMap.seekg (0, ios_base::beg);
|
|
172 |
char str[MAX_LINE];
|
|
173 |
aMap.getline(str,MAX_LINE);
|
|
174 |
boost::cmatch what;
|
|
175 |
boost::regex reg("^\\.text\\s+");
|
|
176 |
while(aMap.getline(str,MAX_LINE)) {
|
|
177 |
if(regex_search(str, what, reg)) {
|
|
178 |
break;
|
|
179 |
}
|
|
180 |
}
|
|
181 |
|
|
182 |
reg.assign("^\\.text\\s+(\\w+)\\s+\\w+");
|
|
183 |
if(!regex_search(str, what, reg)) {
|
|
184 |
printf("ERROR: Can't get .text section info for \"%s\"\n",fileName.c_str());
|
|
185 |
}
|
|
186 |
else {
|
|
187 |
string sTmp, sLibFile;
|
|
188 |
sTmp.assign(what[1].first,what[1].second-what[1].first);
|
|
189 |
unsigned int imgText = strtol(sTmp.c_str(), NULL, 16);
|
|
190 |
|
|
191 |
reg.assign("^LONG 0x.*", boost::regex::icase);
|
|
192 |
boost::cmatch what1;
|
|
193 |
boost::regex reg1("^\\s(\\.text)?\\s+(0x\\w+)\\s+(0x\\w+)\\s+(.*)$", boost::regex::icase);
|
|
194 |
boost::regex reg2("^\\s+(\\w+)\\s\\s+([a-zA-Z_].+)", boost::regex::icase);
|
|
195 |
boost::regex reg3(".*lib\\(.*d\\d*s_?\\d{5}.o\\)$", boost::regex::icase);
|
|
196 |
|
|
197 |
map<unsigned int,string> syms;
|
|
198 |
unsigned int addr, len, stubhex;
|
|
199 |
|
|
200 |
while(aMap.getline(str,MAX_LINE)) {
|
|
201 |
if(strlen(str) == 0)
|
|
202 |
break;
|
|
203 |
else if(regex_search(str, what, reg1)) {
|
|
204 |
sLibFile.assign(what[4].first,what[4].second-what[4].first);
|
655
|
205 |
if(!regex_search(sLibFile.c_str(), what1, reg)) {
|
600
|
206 |
sTmp.assign(what[2].first,what[2].second-what[2].first);
|
|
207 |
addr = strtol(sTmp.c_str(), NULL, 16);
|
|
208 |
sTmp.assign(what[3].first,what[3].second-what[3].first);
|
|
209 |
len = strtol(sTmp.c_str(), NULL, 16);
|
|
210 |
syms[addr+len] = "";
|
655
|
211 |
if(regex_search(sLibFile.c_str(), what, reg3)) {
|
600
|
212 |
stubhex = addr;
|
|
213 |
}
|
|
214 |
}
|
|
215 |
}
|
|
216 |
else if(regex_search(str, what, reg2)) {
|
|
217 |
sTmp.assign(what[1].first,what[1].second-what[1].first);
|
|
218 |
addr = strtol(sTmp.c_str(), NULL, 16);
|
|
219 |
sTmp.assign(what[2].first,what[2].second-what[2].first);
|
|
220 |
syms[addr] = (addr == stubhex)? ("stub "+sTmp) : sTmp;
|
|
221 |
}
|
|
222 |
}
|
|
223 |
|
|
224 |
map<unsigned int,string>::iterator it = syms.begin();
|
|
225 |
map<unsigned int,string>::iterator itp = it++;
|
|
226 |
string outString;
|
|
227 |
for(; it != syms.end(); itp = it++) {
|
|
228 |
if(itp->second != "") {
|
|
229 |
memset(str,0,sizeof(str));
|
|
230 |
sprintf(str,"%08x %04x ",(itp->first-imgText), (it->first-itp->first));
|
|
231 |
outString = str;
|
|
232 |
outString += it->second+"\n";
|
|
233 |
iSymFile.write(outString.c_str(),outString.length());
|
|
234 |
}
|
|
235 |
}
|
|
236 |
}
|
|
237 |
}
|
|
238 |
int SymbolGenerator::GetSizeFromBinFile( const string& fileName ){
|
|
239 |
TInt ret = 0;
|
|
240 |
ifstream aIf(fileName.c_str(), ios_base::binary);
|
|
241 |
if( !aIf.is_open() ) {
|
|
242 |
printf("Warning: Cannot open file \n");
|
|
243 |
}
|
|
244 |
else {
|
|
245 |
E32ImageFile e32Image;
|
|
246 |
TUint32 aSz;
|
|
247 |
|
|
248 |
aIf.seekg(0,ios_base::end);
|
|
249 |
aSz = aIf.tellg();
|
|
250 |
|
|
251 |
e32Image.Adjust(aSz);
|
|
252 |
e32Image.iFileSize = aSz;
|
|
253 |
|
|
254 |
aIf.seekg(0,ios_base::beg);
|
|
255 |
aIf >> e32Image;
|
|
256 |
ret = e32Image.iOrigHdr->iCodeSize;
|
|
257 |
}
|
|
258 |
return ret;
|
|
259 |
}
|
|
260 |
void SymbolGenerator::thrd_func(){
|
|
261 |
SymbolGenerator* me = GetInstance();
|
|
262 |
|
|
263 |
TPlacedEntry pe("",false);
|
|
264 |
while(1) {
|
|
265 |
if(1) {
|
|
266 |
//scope the code block with if(1) for lock
|
|
267 |
boost::mutex::scoped_lock lock(me->iMutex);
|
|
268 |
while(me->iQueueFiles.empty())
|
|
269 |
me->iCond.wait(lock);
|
|
270 |
/*
|
|
271 |
if(me->iQueueFiles.empty()) {
|
|
272 |
boost::this_thread::sleep(boost::posix_time::milliseconds(10));
|
|
273 |
continue;
|
|
274 |
}
|
|
275 |
*/
|
|
276 |
|
|
277 |
pe = me->iQueueFiles.front();
|
|
278 |
me->iQueueFiles.pop();
|
|
279 |
}
|
|
280 |
|
|
281 |
if(pe.iFileName == "")
|
|
282 |
break;
|
|
283 |
else if(pe.iExecutable)
|
|
284 |
me->ProcessExecutable(pe.iFileName);
|
|
285 |
else
|
|
286 |
me->ProcessDatafile(pe.iFileName);
|
|
287 |
}
|
|
288 |
}
|
|
289 |
SymbolGenerator::SymbolGenerator() : boost::thread(thrd_func) {
|
|
290 |
}
|
|
291 |
SymbolGenerator::~SymbolGenerator(){
|
|
292 |
if(joinable())
|
|
293 |
join();
|
|
294 |
iSymFile.flush();
|
|
295 |
iSymFile.close();
|
|
296 |
}
|