]>
Commit | Line | Data |
---|---|---|
8733430b | 1 | //****************************************************************************\r |
2 | //**\r | |
a3ab2a47 | 3 | //** Copyright (C) 2006 Intel Corporation. All rights reserved.\r |
8733430b | 4 | //**\r |
a3ab2a47 | 5 | //** The information and source code contained herein is the exclusive\r |
8733430b | 6 | //** property of Intel Corporation and may not be disclosed, examined\r |
a3ab2a47 | 7 | //** or reproduced in whole or in part without explicit written authorization\r |
8733430b | 8 | //** from the company.\r |
9 | //**\r | |
10 | //****************************************************************************\r | |
aaed63a9 | 11 | #include <cstdio>\r |
327bd141 | 12 | #include <iostream>\r |
8733430b | 13 | #include <fstream>\r |
aaed63a9 | 14 | #include <iomanip>\r |
15 | #include <stdexcept>\r | |
16 | #include <set>\r | |
8733430b | 17 | #include <string>\r |
aaed63a9 | 18 | #include <sstream>\r |
19 | #include <vector>\r | |
20 | #include <map>\r | |
8733430b | 21 | #include <algorithm>\r |
8733430b | 22 | using namespace std;\r |
23 | \r | |
aaed63a9 | 24 | #include "ProcessorBind.h"\r |
1b266b0c | 25 | \r |
aaed63a9 | 26 | class putUINT64\r |
8733430b | 27 | {\r |
28 | public:\r | |
aaed63a9 | 29 | putUINT64(UINT64 ullVal) : m_ull(ullVal) {}\r |
30 | putUINT64(const putUINT64& r) : m_ull(r.m_ull) {}\r | |
31 | \r | |
32 | template <class _E, class _Tr>\r | |
33 | friend basic_ostream<_E, _Tr>& operator << (basic_ostream<_E, _Tr>&, putUINT64);\r | |
8733430b | 34 | \r |
35 | private:\r | |
aaed63a9 | 36 | UINT64 m_ull;\r |
37 | };\r | |
38 | \r | |
39 | template <class _E, class _Tr>\r | |
40 | basic_ostream<_E, _Tr>& operator << (basic_ostream<_E, _Tr>& os, putUINT64 ull)\r | |
41 | {\r | |
42 | static const char cDigits[] = "0123456789abcdef";\r | |
43 | \r | |
44 | UINT64 base = 10;\r | |
45 | if (os.flags() & ios_base::hex)\r | |
46 | base = 16;\r | |
47 | else if (os.flags() & ios_base::oct)\r | |
48 | base = 8;\r | |
49 | \r | |
50 | ostringstream ostr;\r | |
51 | UINT64 ullVal = ull.m_ull;\r | |
52 | while (ullVal != 0)\r | |
8733430b | 53 | {\r |
aaed63a9 | 54 | ostr << cDigits[ullVal % base];\r |
55 | ullVal /= base;\r | |
8733430b | 56 | }\r |
57 | \r | |
aaed63a9 | 58 | string s1(ostr.str());\r |
59 | string s2(s1.rbegin(), s1.rend());\r | |
60 | return os << s2;\r | |
61 | }\r | |
62 | \r | |
63 | class getUINT64\r | |
64 | {\r | |
65 | public:\r | |
66 | getUINT64(UINT64& ullVal) : m_ull(ullVal) {}\r | |
67 | getUINT64(const getUINT64& r) : m_ull(r.m_ull) {}\r | |
68 | \r | |
69 | template <class _E, class _Tr>\r | |
70 | friend basic_istream<_E, _Tr>& operator >> (basic_istream<_E, _Tr>&, getUINT64);\r | |
71 | \r | |
72 | private:\r | |
73 | UINT64& m_ull;\r | |
74 | \r | |
75 | private:\r | |
76 | getUINT64& operator = (const getUINT64&);\r | |
8733430b | 77 | };\r |
78 | \r | |
aaed63a9 | 79 | template <class _E, class _Tr>\r |
80 | basic_istream<_E, _Tr>& operator >> (basic_istream<_E, _Tr>& is, getUINT64 ull)\r | |
8733430b | 81 | {\r |
aaed63a9 | 82 | string strBuf;\r |
83 | is >> strBuf;\r | |
84 | \r | |
85 | UINT64 base = 10;\r | |
86 | if (is.flags() & ios_base::hex)\r | |
87 | base = 16;\r | |
88 | else if (is.flags() & ios_base::oct)\r | |
89 | base = 8;\r | |
90 | \r | |
91 | UINT64 ullVal = 0;\r | |
92 | for (string::iterator i = strBuf.begin(); i != strBuf.end(); i++)\r | |
93 | {\r | |
94 | if (*i <= '9' && *i >= '0')\r | |
95 | *i -= '0';\r | |
96 | else if (*i <= 'F' && *i >= 'A')\r | |
97 | *i -= 'A' - '\x0a';\r | |
98 | else if (*i <= 'f' && *i >= 'a')\r | |
99 | *i -= 'a' - '\x0a';\r | |
100 | else throw runtime_error("Invalid number format");\r | |
101 | \r | |
102 | ullVal = ullVal * base + *i;\r | |
103 | }\r | |
104 | ull.m_ull = ullVal;\r | |
105 | return is;\r | |
8733430b | 106 | }\r |
107 | \r | |
aaed63a9 | 108 | class EMemoryLeak : public logic_error\r |
8733430b | 109 | {\r |
aaed63a9 | 110 | public:\r |
111 | EMemoryLeak() : logic_error("Memory leak detected") {}\r | |
112 | };\r | |
8733430b | 113 | \r |
aaed63a9 | 114 | class EInvalidGuidString : public invalid_argument\r |
115 | {\r | |
116 | public:\r | |
117 | EInvalidGuidString() : invalid_argument("Unexpected format of GUID string") {}\r | |
118 | };\r | |
119 | \r | |
120 | class ELogFileError : public logic_error\r | |
121 | {\r | |
122 | public:\r | |
123 | ELogFileError(const string& strMsg) : logic_error(strMsg) {}\r | |
124 | };\r | |
125 | \r | |
126 | class EDuplicatedFfsFile : public ELogFileError\r | |
127 | {\r | |
128 | public:\r | |
129 | EDuplicatedFfsFile() : ELogFileError("Duplicated FFS found in LOG file") {}\r | |
130 | };\r | |
131 | \r | |
132 | class EUnexpectedLogFileToken : public ELogFileError\r | |
133 | {\r | |
134 | public:\r | |
135 | EUnexpectedLogFileToken() : ELogFileError("Unexpected LOG file token") {}\r | |
136 | };\r | |
137 | \r | |
138 | class EFileNotFound : public invalid_argument\r | |
139 | {\r | |
140 | public:\r | |
141 | EFileNotFound(const string& strFName) : invalid_argument("File not found - " + strFName) {}\r | |
142 | };\r | |
143 | \r | |
144 | class EUnexpectedMapFile : public logic_error\r | |
145 | {\r | |
146 | public:\r | |
147 | EUnexpectedMapFile(const string& strKeyWord) : logic_error("Unexpected map file format - " + strKeyWord) {}\r | |
148 | };\r | |
149 | \r | |
150 | class EUsage : public invalid_argument\r | |
151 | {\r | |
152 | public:\r | |
153 | EUsage() : invalid_argument("Usage: GenFvMap <FV.LOG> <FV.INF> <FV.MAP>") {}\r | |
154 | };\r | |
155 | \r | |
156 | template <class T>\r | |
157 | class CMemoryLeakChecker : public set<T*>\r | |
8733430b | 158 | {\r |
159 | protected:\r | |
aaed63a9 | 160 | CMemoryLeakChecker()\r |
161 | {\r | |
162 | }\r | |
163 | \r | |
164 | public:\r | |
165 | virtual ~CMemoryLeakChecker();\r | |
166 | static CMemoryLeakChecker<T>& GetInstance();\r | |
167 | \r | |
168 | private:\r | |
169 | CMemoryLeakChecker(const CMemoryLeakChecker<T>&);\r | |
8733430b | 170 | };\r |
171 | \r | |
aaed63a9 | 172 | template <class T>\r |
173 | CMemoryLeakChecker<T>::~CMemoryLeakChecker()\r | |
8733430b | 174 | {\r |
aaed63a9 | 175 | if (!CMemoryLeakChecker<T>::empty())\r |
176 | throw EMemoryLeak();\r | |
8733430b | 177 | }\r |
178 | \r | |
aaed63a9 | 179 | template <class T>\r |
180 | CMemoryLeakChecker<T>& CMemoryLeakChecker<T>::GetInstance()\r | |
8733430b | 181 | {\r |
aaed63a9 | 182 | static CMemoryLeakChecker<T> s_instance;\r |
183 | return s_instance;\r | |
8733430b | 184 | }\r |
185 | \r | |
aaed63a9 | 186 | class CObjRoot\r |
187 | {\r | |
188 | protected:\r | |
189 | CObjRoot()\r | |
190 | {\r | |
191 | #ifdef _CHK_MEM_LEAK\r | |
192 | CMemoryLeakChecker<CObjRoot>::GetInstance().insert(this);\r | |
193 | #endif\r | |
194 | }\r | |
195 | \r | |
196 | public:\r | |
197 | virtual ~CObjRoot()\r | |
198 | {\r | |
199 | #ifdef _CHK_MEM_LEAK\r | |
200 | CMemoryLeakChecker<CObjRoot>::GetInstance().erase(this);\r | |
201 | #endif\r | |
202 | }\r | |
203 | \r | |
204 | private:\r | |
205 | CObjRoot(const CObjRoot&);\r | |
206 | };\r | |
207 | \r | |
8733430b | 208 | class CIdentity : public CObjRoot\r |
209 | {\r | |
210 | public:\r | |
8733430b | 211 | CIdentity(const string&);\r |
aaed63a9 | 212 | operator string (void) const;\r |
213 | \r | |
214 | bool operator < (const CIdentity& id) const\r | |
215 | {\r | |
216 | return memcmp(this, &id, sizeof(*this)) < 0;\r | |
217 | }\r | |
218 | \r | |
219 | CIdentity() : ulD1(0), wD2(0), wD3(0), wD4(0), ullD5(0)\r | |
220 | {\r | |
221 | }\r | |
8733430b | 222 | \r |
aaed63a9 | 223 | CIdentity(const CIdentity& r) : ulD1(r.ulD1), wD2(r.wD2), wD3(r.wD3), wD4(r.wD4), ullD5(r.ullD5)\r |
224 | {\r | |
225 | }\r | |
8733430b | 226 | \r |
aaed63a9 | 227 | template <class _E, class _Tr>\r |
228 | basic_istream<_E, _Tr>& ReadId(basic_istream<_E, _Tr>&);\r | |
229 | template <class _E, class _Tr>\r | |
230 | basic_ostream<_E, _Tr>& WriteId(basic_ostream<_E, _Tr>&);\r | |
8733430b | 231 | \r |
aaed63a9 | 232 | template <class _E, class _Tr>\r |
233 | friend basic_istream<_E, _Tr>& operator >> (basic_istream<_E, _Tr>&, CIdentity&);\r | |
234 | template <class _E, class _Tr>\r | |
235 | friend basic_ostream<_E, _Tr>& operator << (basic_ostream<_E, _Tr>&, CIdentity);\r | |
8733430b | 236 | \r |
aaed63a9 | 237 | private:\r |
238 | UINT32 ulD1;\r | |
239 | UINT16 wD2, wD3, wD4;\r | |
240 | UINT64 ullD5;\r | |
241 | };\r | |
8733430b | 242 | \r |
aaed63a9 | 243 | CIdentity::CIdentity(const string& strGuid)\r |
8733430b | 244 | {\r |
aaed63a9 | 245 | try\r |
246 | {\r | |
247 | string str(strGuid);\r | |
248 | str.erase(0, str.find_first_not_of(" {"));\r | |
249 | str.resize(str.find_last_not_of(" }") + 1);\r | |
250 | str[str.find('-')] = ' ';\r | |
251 | str[str.find('-')] = ' ';\r | |
252 | str[str.find('-')] = ' ';\r | |
253 | str[str.find('-')] = ' ';\r | |
254 | \r | |
255 | istringstream is(str);\r | |
256 | is >> hex >> ulD1 >> wD2 >> wD3 >> wD4 >> getUINT64(ullD5);\r | |
257 | }\r | |
258 | catch (const exception&)\r | |
259 | {\r | |
260 | throw EInvalidGuidString();\r | |
261 | }\r | |
8733430b | 262 | }\r |
263 | \r | |
aaed63a9 | 264 | CIdentity::operator string(void) const\r |
8733430b | 265 | {\r |
aaed63a9 | 266 | ostringstream os;\r |
267 | os << hex << setfill('0')\r | |
268 | << setw(8) << ulD1 << '-'\r | |
269 | << setw(4) << wD2 << '-'\r | |
270 | << setw(4) << wD3 << '-'\r | |
271 | << setw(4) << wD4 << '-'\r | |
272 | << setw(12) << putUINT64(ullD5);\r | |
273 | return os.str();\r | |
8733430b | 274 | }\r |
275 | \r | |
aaed63a9 | 276 | template <class _E, class _Tr>\r |
277 | basic_istream<_E, _Tr>& CIdentity::ReadId(basic_istream<_E, _Tr>& is)\r | |
8733430b | 278 | {\r |
aaed63a9 | 279 | string str;\r |
280 | if (!!(is >> str))\r | |
281 | *this = CIdentity(str);\r | |
282 | return is;\r | |
8733430b | 283 | }\r |
284 | \r | |
aaed63a9 | 285 | template <class _E, class _Tr>\r |
286 | basic_ostream<_E, _Tr>& CIdentity::WriteId(basic_ostream<_E, _Tr>& os)\r | |
8733430b | 287 | {\r |
aaed63a9 | 288 | return os << (string)(*this);\r |
8733430b | 289 | }\r |
290 | \r | |
aaed63a9 | 291 | template <class _E, class _Tr>\r |
292 | basic_istream<_E, _Tr>& operator >> (basic_istream<_E, _Tr>& is, CIdentity& id)\r | |
8733430b | 293 | {\r |
aaed63a9 | 294 | return id.ReadId(is);\r |
8733430b | 295 | }\r |
296 | \r | |
aaed63a9 | 297 | template <class _E, class _Tr>\r |
298 | basic_ostream<_E, _Tr>& operator << (basic_ostream<_E, _Tr>& os, CIdentity id)\r | |
a3ab2a47 | 299 | {\r |
aaed63a9 | 300 | return id.WriteId(os);\r |
a3ab2a47 | 301 | }\r |
302 | \r | |
aaed63a9 | 303 | template <class T>\r |
304 | class IVectorContainerByReference : virtual public CObjRoot, public vector<T*>\r | |
8733430b | 305 | {\r |
aaed63a9 | 306 | };\r |
8733430b | 307 | \r |
aaed63a9 | 308 | template <class T>\r |
309 | class IMapContainer : virtual public CObjRoot, public map<CIdentity, T>\r | |
310 | {\r | |
311 | };\r | |
8733430b | 312 | \r |
aaed63a9 | 313 | struct ISymbol : virtual public CObjRoot\r |
314 | {\r | |
315 | string strAddress;\r | |
316 | string strName;\r | |
317 | string strFrom;\r | |
318 | UINT64 ullRva;\r | |
319 | bool bStatic;\r | |
320 | bool bFunction;\r | |
321 | virtual void Relocate(UINT64)=0;\r | |
322 | };\r | |
8733430b | 323 | \r |
aaed63a9 | 324 | class IModule : public IVectorContainerByReference<ISymbol>\r |
325 | {\r | |
326 | public:\r | |
327 | string strName;\r | |
328 | CIdentity id;\r | |
329 | virtual UINT64 BaseAddress(void) const=0;\r | |
330 | virtual UINT64 BaseAddress(UINT64)=0;\r | |
331 | virtual const ISymbol *EntryPoint(void) const=0;\r | |
8733430b | 332 | };\r |
333 | \r | |
aaed63a9 | 334 | class IFirmwareVolume : public IVectorContainerByReference<IModule>\r |
8733430b | 335 | {\r |
aaed63a9 | 336 | };\r |
8733430b | 337 | \r |
aaed63a9 | 338 | class IMapFileSet : public IMapContainer<istream*>\r |
8733430b | 339 | {\r |
aaed63a9 | 340 | };\r |
8733430b | 341 | \r |
aaed63a9 | 342 | class IFfsSet : public IMapContainer<UINT64>\r |
8733430b | 343 | {\r |
aaed63a9 | 344 | };\r |
8733430b | 345 | \r |
aaed63a9 | 346 | class CFfsSetFromLogFile : public IFfsSet\r |
8733430b | 347 | {\r |
348 | public:\r | |
aaed63a9 | 349 | CFfsSetFromLogFile(const string&);\r |
8733430b | 350 | };\r |
351 | \r | |
aaed63a9 | 352 | CFfsSetFromLogFile::CFfsSetFromLogFile(const string& strFName)\r |
8733430b | 353 | {\r |
aaed63a9 | 354 | ifstream ifs(strFName.c_str());\r |
355 | if (!ifs)\r | |
356 | throw EFileNotFound(strFName);\r | |
357 | \r | |
358 | CIdentity ffsId;\r | |
359 | while (!!ffsId.ReadId(ifs))\r | |
360 | {\r | |
361 | UINT64 ullBase;\r | |
362 | if (!(ifs >> hex >> getUINT64(ullBase)))\r | |
363 | throw EUnexpectedLogFileToken();\r | |
364 | if (!insert(value_type(ffsId, ullBase)).second)\r | |
365 | throw EDuplicatedFfsFile();\r | |
366 | }\r | |
8733430b | 367 | }\r |
368 | \r | |
aaed63a9 | 369 | class CMapFileSetFromInfFile : public IMapFileSet\r |
8733430b | 370 | {\r |
371 | public:\r | |
aaed63a9 | 372 | CMapFileSetFromInfFile(const string&);\r |
373 | ~CMapFileSetFromInfFile();\r | |
374 | };\r | |
375 | \r | |
376 | CMapFileSetFromInfFile::CMapFileSetFromInfFile(const string& strFName)\r | |
377 | {\r | |
378 | static const char cszEfiFileName[] = "EFI_FILE_NAME";\r | |
379 | \r | |
380 | ifstream ifs(strFName.c_str());\r | |
381 | if (!ifs)\r | |
382 | throw EFileNotFound(strFName);\r | |
383 | \r | |
384 | string strFile;\r | |
385 | getline(ifs, strFile, ifstream::traits_type::to_char_type(ifstream::traits_type::eof()));\r | |
386 | strFile.erase(0, strFile.find("[files]"));\r | |
387 | \r | |
388 | istringstream is(strFile);\r | |
389 | string strTmp;\r | |
390 | while (!!getline(is, strTmp))\r | |
8733430b | 391 | {\r |
aaed63a9 | 392 | string::size_type pos = strTmp.find(cszEfiFileName);\r |
393 | if (pos == string::npos)\r | |
394 | continue;\r | |
395 | \r | |
396 | strTmp.erase(0, strTmp.find_first_not_of(" =", pos + sizeof(cszEfiFileName) - 1));\r | |
397 | pos = strTmp.find_last_of("\\/");\r | |
398 | string strId(\r | |
399 | strTmp.begin() + pos + 1,\r | |
400 | strTmp.begin() + strTmp.find('-', strTmp.find('-', strTmp.find('-', strTmp.find('-', strTmp.find('-') + 1) + 1) + 1) + 1)\r | |
401 | );\r | |
402 | strTmp.erase(pos + 1, strId.length() + 1);\r | |
403 | strTmp.replace(strTmp.rfind('.'), string::npos, ".map");\r | |
404 | \r | |
405 | istream *ifmaps = new ifstream(strTmp.c_str());\r | |
406 | if (ifmaps && !!*ifmaps &&\r | |
407 | !insert(value_type(CIdentity(strId), ifmaps)).second)\r | |
408 | throw EDuplicatedFfsFile();\r | |
8733430b | 409 | }\r |
aaed63a9 | 410 | }\r |
411 | \r | |
412 | CMapFileSetFromInfFile::~CMapFileSetFromInfFile()\r | |
413 | {\r | |
414 | for (iterator i = begin(); i != end(); i++)\r | |
415 | delete i->second;\r | |
416 | }\r | |
417 | \r | |
418 | class CSymbolFromString : public ISymbol\r | |
419 | {\r | |
420 | public:\r | |
421 | CSymbolFromString(const string&, bool = false);\r | |
422 | void Relocate(UINT64);\r | |
8733430b | 423 | };\r |
424 | \r | |
aaed63a9 | 425 | CSymbolFromString::CSymbolFromString(const string& strSymbol, bool b)\r |
8733430b | 426 | {\r |
aaed63a9 | 427 | bStatic = b;\r |
8733430b | 428 | \r |
aaed63a9 | 429 | istringstream is(strSymbol);\r |
df13ceba | 430 | is >> strAddress >> strName >> hex >> getUINT64(ullRva) >> strFrom;\r |
aaed63a9 | 431 | if (strFrom == "f")\r |
8733430b | 432 | {\r |
aaed63a9 | 433 | bFunction = true;\r |
434 | is >> strFrom;\r | |
435 | }\r | |
436 | else bFunction = false;\r | |
437 | if (!is)\r | |
438 | throw EUnexpectedMapFile("Symbol line format");\r | |
8733430b | 439 | }\r |
440 | \r | |
aaed63a9 | 441 | void CSymbolFromString::Relocate(UINT64 ullDelta)\r |
8733430b | 442 | {\r |
aaed63a9 | 443 | if (ullRva > 0)\r |
444 | ullRva += ullDelta;\r | |
8733430b | 445 | }\r |
446 | \r | |
aaed63a9 | 447 | class CModuleFromMap : public IModule\r |
8733430b | 448 | {\r |
449 | public:\r | |
aaed63a9 | 450 | CModuleFromMap(istream&);\r |
451 | ~CModuleFromMap();\r | |
452 | \r | |
453 | UINT64 BaseAddress() const;\r | |
454 | UINT64 BaseAddress(UINT64);\r | |
455 | const ISymbol *EntryPoint() const;\r | |
8733430b | 456 | \r |
aaed63a9 | 457 | private:\r |
458 | UINT64 m_ullLoadAddress;\r | |
459 | iterator m_iEntryPoint;\r | |
8733430b | 460 | \r |
aaed63a9 | 461 | static pair<string, string::size_type> FindToken(istream&, const string&);\r |
8733430b | 462 | };\r |
463 | \r | |
aaed63a9 | 464 | pair<string, string::size_type> CModuleFromMap::FindToken(istream& is, const string& strToken)\r |
465 | {\r | |
466 | for (string strTmp; !!getline(is, strTmp);)\r | |
467 | {\r | |
468 | string::size_type pos = strTmp.find(strToken);\r | |
469 | if (pos != string::npos)\r | |
470 | return pair<string, string::size_type>(strTmp, pos);\r | |
471 | }\r | |
472 | throw EUnexpectedMapFile(strToken);\r | |
473 | }\r | |
474 | \r | |
475 | CModuleFromMap::CModuleFromMap(istream& imaps)\r | |
8733430b | 476 | {\r |
477 | static const char cszLoadAddr[] = "Preferred load address is";\r | |
478 | static const char cszGlobal[] = "Address";\r | |
479 | static const char cszEntryPoint[] = "entry point at";\r | |
480 | static const char cszStatic[] = "Static symbols";\r | |
481 | \r | |
aaed63a9 | 482 | pair<string, string::size_type> pairTmp;\r |
483 | istringstream iss;\r | |
484 | \r | |
485 | getline(imaps, strName);\r | |
486 | strName.erase(0, strName.find_first_not_of(' '));\r | |
487 | \r | |
488 | pairTmp = FindToken(imaps, cszLoadAddr);\r | |
489 | iss.str(pairTmp.first.substr(pairTmp.second + sizeof(cszLoadAddr) - 1));\r | |
490 | iss >> getUINT64(m_ullLoadAddress);\r | |
491 | \r | |
492 | pairTmp = FindToken(imaps, cszGlobal);\r | |
493 | while (!!getline(imaps, pairTmp.first) &&\r | |
494 | pairTmp.first.find(cszEntryPoint) == string::npos)\r | |
495 | if (pairTmp.first.find_first_not_of(' ') != string::npos)\r | |
496 | push_back(new CSymbolFromString(pairTmp.first));\r | |
497 | \r | |
498 | iss.str(pairTmp.first.substr(pairTmp.first.find(cszEntryPoint) + sizeof(cszEntryPoint) - 1));\r | |
499 | iss.clear();\r | |
500 | string strEntryPoint;\r | |
501 | iss >> strEntryPoint;\r | |
502 | \r | |
503 | pairTmp = FindToken(imaps, cszStatic);\r | |
504 | if (pairTmp.second)\r | |
505 | while (!!getline(imaps, pairTmp.first))\r | |
506 | if (pairTmp.first.find_first_not_of(' ') != string::npos)\r | |
507 | push_back(new CSymbolFromString(pairTmp.first, true));\r | |
508 | \r | |
509 | for (m_iEntryPoint = begin();\r | |
510 | m_iEntryPoint != end() && (*m_iEntryPoint)->strAddress != strEntryPoint;\r | |
511 | m_iEntryPoint++);\r | |
512 | if (m_iEntryPoint == end())\r | |
513 | throw EUnexpectedMapFile("Entry point not found");\r | |
514 | }\r | |
8733430b | 515 | \r |
aaed63a9 | 516 | CModuleFromMap::~CModuleFromMap()\r |
517 | {\r | |
518 | for (iterator i = begin(); i != end(); i++)\r | |
519 | delete *i;\r | |
520 | }\r | |
8733430b | 521 | \r |
aaed63a9 | 522 | UINT64 CModuleFromMap::BaseAddress(void) const\r |
523 | {\r | |
524 | return m_ullLoadAddress;\r | |
8733430b | 525 | }\r |
526 | \r | |
aaed63a9 | 527 | UINT64 CModuleFromMap::BaseAddress(UINT64 ullNewBase)\r |
8733430b | 528 | {\r |
aaed63a9 | 529 | ullNewBase -= m_ullLoadAddress;\r |
8733430b | 530 | for (iterator i = begin(); i != end(); i++)\r |
aaed63a9 | 531 | (*i)->Relocate(ullNewBase);\r |
532 | m_ullLoadAddress += ullNewBase;\r | |
533 | return m_ullLoadAddress - ullNewBase;\r | |
8733430b | 534 | }\r |
535 | \r | |
aaed63a9 | 536 | const ISymbol *CModuleFromMap::EntryPoint(void) const\r |
8733430b | 537 | {\r |
aaed63a9 | 538 | return *m_iEntryPoint;\r |
539 | }\r | |
8733430b | 540 | \r |
aaed63a9 | 541 | class CFvMap : public IFirmwareVolume\r |
8733430b | 542 | {\r |
543 | public:\r | |
aaed63a9 | 544 | CFvMap(IFfsSet*, IMapFileSet*);\r |
545 | ~CFvMap();\r | |
8733430b | 546 | \r |
547 | private:\r | |
aaed63a9 | 548 | CFvMap(const CFvMap&);\r |
8733430b | 549 | };\r |
550 | \r | |
aaed63a9 | 551 | CFvMap::CFvMap(IFfsSet *pFfsSet, IMapFileSet *pMapSet)\r |
8733430b | 552 | {\r |
aaed63a9 | 553 | for (IFfsSet::iterator i = pFfsSet->begin(); i != pFfsSet->end(); i++)\r |
8733430b | 554 | {\r |
aaed63a9 | 555 | IMapFileSet::iterator j = pMapSet->find(i->first);\r |
556 | if (j != pMapSet->end())\r | |
60ad3479 | 557 | {\r |
aaed63a9 | 558 | IModule *pModule = new CModuleFromMap(*j->second);\r |
559 | pModule->id = i->first;\r | |
560 | pModule->BaseAddress(i->second);\r | |
561 | push_back(pModule);\r | |
60ad3479 | 562 | }\r |
8733430b | 563 | }\r |
564 | }\r | |
565 | \r | |
aaed63a9 | 566 | CFvMap::~CFvMap()\r |
8733430b | 567 | {\r |
568 | for (iterator i = begin(); i != end(); i++)\r | |
aaed63a9 | 569 | delete *i;\r |
8733430b | 570 | }\r |
571 | \r | |
df13ceba | 572 | class CFvMapGenerator : public CObjRoot\r |
93d16c69 | 573 | {\r |
aaed63a9 | 574 | public:\r |
df13ceba | 575 | CFvMapGenerator(const IFirmwareVolume *pFv) : m_pFv(pFv) {}\r |
576 | CFvMapGenerator(const CFvMapGenerator& r) : m_pFv(r.m_pFv) {}\r | |
93d16c69 | 577 | \r |
aaed63a9 | 578 | template <class _E, class _Tr>\r |
df13ceba | 579 | friend basic_ostream<_E, _Tr>& operator << (basic_ostream<_E, _Tr>&, CFvMapGenerator);\r |
93d16c69 | 580 | \r |
aaed63a9 | 581 | private:\r |
582 | static bool Less(const IModule*, const IModule*);\r | |
583 | \r | |
584 | private:\r | |
585 | const IFirmwareVolume *m_pFv;\r | |
586 | };\r | |
587 | \r | |
588 | template <class _E, class _Tr>\r | |
df13ceba | 589 | basic_ostream<_E, _Tr>& operator << (basic_ostream<_E, _Tr>& os, CFvMapGenerator fvMapFmt)\r |
aaed63a9 | 590 | {\r |
591 | vector<IModule*> rgMods(fvMapFmt.m_pFv->begin(), fvMapFmt.m_pFv->end());\r | |
df13ceba | 592 | sort(rgMods.begin(), rgMods.end(), CFvMapGenerator::Less);\r |
aaed63a9 | 593 | for (vector<IModule*>::iterator i = rgMods.begin(); i != rgMods.end(); i++)\r |
a3ab2a47 | 594 | {\r |
aaed63a9 | 595 | os << (*i)->strName << hex << " (BaseAddress=" << putUINT64((*i)->BaseAddress());\r |
596 | os << ", EntryPoint=" << hex << putUINT64((*i)->EntryPoint()->ullRva);\r | |
597 | os << ", GUID=";\r | |
598 | (*i)->id.WriteId(os);\r | |
599 | os << ")" << endl << endl;\r | |
600 | \r | |
601 | for (IModule::iterator j = (*i)->begin(); j != (*i)->end(); j++)\r | |
602 | {\r | |
603 | os << hex << " " << setw(16) << setfill('0') << putUINT64((*j)->ullRva);\r | |
604 | os << ((*j)->bFunction ? " F" : " ")\r | |
605 | << ((*j)->bStatic ? "S " : " ")\r | |
606 | << (*j)->strName << endl;\r | |
607 | }\r | |
a3ab2a47 | 608 | \r |
609 | os << endl << endl;\r | |
610 | }\r | |
8733430b | 611 | return os;\r |
612 | }\r | |
613 | \r | |
df13ceba | 614 | bool CFvMapGenerator::Less(const IModule *pModL, const IModule *pModR)\r |
8733430b | 615 | {\r |
aaed63a9 | 616 | return pModL->BaseAddress() < pModR->BaseAddress();\r |
617 | }\r | |
8733430b | 618 | \r |
aaed63a9 | 619 | class CApplication : public CObjRoot\r |
8733430b | 620 | {\r |
621 | public:\r | |
aaed63a9 | 622 | CApplication(int, char**);\r |
8733430b | 623 | int Run(void);\r |
624 | \r | |
625 | private:\r | |
aaed63a9 | 626 | char **m_ppszArg;\r |
627 | private:\r | |
628 | CApplication(const CApplication&);\r | |
8733430b | 629 | };\r |
630 | \r | |
aaed63a9 | 631 | CApplication::CApplication(int cArg, char *ppszArg[])\r |
632 | : m_ppszArg(ppszArg)\r | |
8733430b | 633 | {\r |
aaed63a9 | 634 | if (cArg != 4)\r |
635 | throw EUsage();\r | |
8733430b | 636 | }\r |
637 | \r | |
aaed63a9 | 638 | int CApplication::Run(void)\r |
8733430b | 639 | {\r |
aaed63a9 | 640 | CFfsSetFromLogFile ffsSet(m_ppszArg[1]);\r |
641 | CMapFileSetFromInfFile mapSet(m_ppszArg[2]);\r | |
642 | ofstream ofs(m_ppszArg[3]);\r | |
643 | CFvMap fvMap(&ffsSet, &mapSet);\r | |
df13ceba | 644 | ofs << CFvMapGenerator(&fvMap);\r |
8733430b | 645 | return 0;\r |
646 | }\r | |
647 | \r | |
648 | int main(int argc, char *argv[])\r | |
649 | {\r | |
650 | try\r | |
651 | {\r | |
aaed63a9 | 652 | CApplication app(argc, argv);\r |
8733430b | 653 | return app.Run();\r |
654 | }\r | |
655 | catch (const exception& e)\r | |
656 | {\r | |
657 | cerr << e.what() << endl;\r | |
658 | return -1;\r | |
659 | }\r | |
660 | }\r | |
aaed63a9 | 661 | \r |
662 | #ifdef _DDK3790x1830_WORKAROUND\r | |
663 | extern "C" void __fastcall __security_check_cookie(int)\r | |
664 | {\r | |
665 | }\r | |
666 | #endif\r |