BlCvParser.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #include "BlCvParser.h"
  2. #include "BlContext.h"
  3. #include "BlCodeView.h"
  4. #include "codeview/cvinfo.h"
  5. #include "../COFFData.h"
  6. #include "BeefySysLib/util/PerfTimer.h"
  7. USING_NS_BF;
  8. #define CV_BLOCK_SIZE 0x1000
  9. #define MSF_SIGNATURE_700 "Microsoft C/C++ MSF 7.00\r\n\032DS\0\0"
  10. #define GET(T) *((T*)(data += sizeof(T)) - 1)
  11. #define PTR_ALIGN(ptr, origPtr, alignSize) ptr = ( (origPtr)+( ((ptr - origPtr) + (alignSize - 1)) & ~(alignSize - 1) ) )
  12. BlCvParser::BlCvParser(BlContext* context)
  13. {
  14. mContext = context;
  15. mCodeView = context->mCodeView;
  16. mCurModule = NULL;
  17. mSyms = NULL;
  18. }
  19. void BlCvParser::NotImpl()
  20. {
  21. }
  22. void BlCvParser::Fail(const StringImpl& err)
  23. {
  24. mContext->Fail(StrFormat("%s in %s", err.c_str(), mContext->ObjectDataIdxToString(mCurModule->mObjectData->mIdx).c_str()));
  25. }
  26. int64 BlCvParser::CvParseConstant(uint16 constVal, uint8*& data)
  27. {
  28. if (constVal < LF_NUMERIC) // 0x8000
  29. return constVal;
  30. switch (constVal)
  31. {
  32. case LF_CHAR:
  33. return GET(int8);
  34. case LF_SHORT:
  35. return GET(int16);
  36. case LF_USHORT:
  37. return GET(uint16);
  38. case LF_LONG:
  39. return GET(int32);
  40. case LF_ULONG:
  41. return GET(uint32);
  42. case LF_QUADWORD:
  43. return GET(int64);
  44. case LF_UQUADWORD:
  45. return (int64)GET(uint64);
  46. default:
  47. BF_FATAL("Not handled");
  48. }
  49. return 0;
  50. }
  51. int64 BlCvParser::CvParseConstant(uint8*& data)
  52. {
  53. uint16 val = GET(uint16);
  54. return CvParseConstant(val, data);
  55. }
  56. const char* BlCvParser::CvParseString(uint8*& data)
  57. {
  58. const char* strStart = (const char*)data;
  59. int strLen = (int)strlen((const char*)data);
  60. data += strLen + 1;
  61. if (strLen == 0)
  62. return NULL;
  63. return strStart;
  64. }
  65. bool BlCvParser::MapAddress(void* symbolData, void* cvLoc, BlReloc& outReloc, COFFRelocation*& nextReloc)
  66. {
  67. int posOfs = (int)((uint8*)cvLoc - (uint8*)symbolData);
  68. int posSect = (int)((uint8*)cvLoc + 4 - (uint8*)symbolData);
  69. if (nextReloc->mVirtualAddress != posOfs)
  70. {
  71. outReloc.mFlags = (BeRelocFlags)0;
  72. outReloc.mSymName = NULL;
  73. return false;
  74. }
  75. auto sym = &(*mSyms)[nextReloc->mSymbolTableIndex];
  76. if (sym->mSym != NULL)
  77. {
  78. outReloc.mFlags = BeRelocFlags_Sym;
  79. outReloc.mSym = sym->mSym;
  80. }
  81. else
  82. {
  83. outReloc.mFlags = BeRelocFlags_Loc;
  84. outReloc.mSegmentIdx = sym->mSegmentIdx;
  85. outReloc.mSegmentOffset = sym->mSegmentOffset;
  86. }
  87. nextReloc++;
  88. if (nextReloc->mVirtualAddress != posSect)
  89. return false;
  90. // It MUST be the same sym
  91. nextReloc++;
  92. return true;
  93. }
  94. bool BlCvParser::TryReloc(void* symbolData, void* cvLoc, int32* outVal, COFFRelocation*& nextReloc)
  95. {
  96. int posOfs = (int)((uint8*)cvLoc - (uint8*)symbolData);
  97. if (nextReloc->mVirtualAddress != posOfs)
  98. return true;
  99. return false;
  100. }
  101. void BlCvParser::AddModule(BlObjectData* objectData, const char* strTab)
  102. {
  103. //const char* invalidStrs[] = { "delete_scalar", "new_scalar", "throw_bad_alloc", "std_type_info_static", "delete_scalar_size", "main2" };
  104. //const char* invalidStrs[] = { "delete_scalar", "new_scalar" };
  105. /*const char* invalidStrs[] = { "delete_scalar" };
  106. for (const char* invalidStr : invalidStrs)
  107. {
  108. //TODO:
  109. if (strstr(objectData->mName, invalidStr) != NULL)
  110. {
  111. return;
  112. }
  113. }
  114. if (strstr(objectData->mName, "new_scalar") != NULL)
  115. {
  116. mTEMP_Testing = true;
  117. }*/
  118. mCurModule = mCodeView->mModuleInfo.Alloc();
  119. mCurModule->mObjectData = objectData;
  120. mCurModule->mIdx = (int)mCodeView->mModuleInfo.size() - 1;
  121. mCurModule->mStrTab = strTab;
  122. }
  123. void BlCvParser::AddTypeData(PESectionHeader* sect)
  124. {
  125. mTypeSects.push_back(sect);
  126. }
  127. void BlCvParser::AddSymbolData(PESectionHeader* sect)
  128. {
  129. mSymSects.push_back(sect);
  130. }
  131. void BlCvParser::ParseTypeData(void* typeData, int size)
  132. {
  133. uint8* data = (uint8*)typeData;
  134. uint8* dataEnd = data + size;
  135. int sig = GET(int32);
  136. if (sig != CV_SIGNATURE_C13)
  137. {
  138. Fail("Invalid debug signature");
  139. return;
  140. }
  141. bool useWorkList = true;
  142. if (*(int16*)(data + 2) == LF_TYPESERVER2)
  143. {
  144. lfTypeServer2& typeServer = *(lfTypeServer2*)(data + 2);
  145. String filePath = (char*)typeServer.name;
  146. String fixedFilePath = FixPathAndCase(filePath);
  147. auto itr = mCodeView->mTypeServerFiles.find(fixedFilePath);
  148. if (itr == mCodeView->mTypeServerFiles.end())
  149. {
  150. auto itrPair = mCodeView->mTypeServerFiles.insert(std::make_pair(fixedFilePath, (BlCvTypeSource*)NULL));
  151. BlPdbParser* pdbParser = new BlPdbParser();
  152. pdbParser->mTypeSource = new BlCvTypeSource;
  153. itrPair.first->second = pdbParser->mTypeSource;
  154. pdbParser->mTypeSource->mTypeServerLib = pdbParser;
  155. pdbParser->mTypeSource->Init(mCodeView);
  156. if (FileExists(filePath))
  157. {
  158. pdbParser->mFileName = filePath;
  159. }
  160. else
  161. {
  162. String fileName = GetFileName(filePath);
  163. String checkFilePath;
  164. if (mCurModule->mObjectData->mLib != NULL)
  165. checkFilePath = GetFileDir(mCurModule->mObjectData->mLib->mFileName);
  166. else
  167. checkFilePath = GetFileDir(mCurModule->mObjectData->mName);
  168. checkFilePath += fileName;
  169. pdbParser->mFileName = checkFilePath;
  170. }
  171. if (useWorkList)
  172. {
  173. mCodeView->mTypeWorkThread.Add(pdbParser->mTypeSource);
  174. }
  175. else
  176. {
  177. pdbParser->Load(pdbParser->mFileName);
  178. }
  179. mCurModule->mTypeSource = pdbParser->mTypeSource;
  180. }
  181. else
  182. {
  183. mCurModule->mTypeSource = itr->second;
  184. }
  185. return;
  186. }
  187. auto typeSource = new BlCvTypeSource();
  188. typeSource->Init(mCodeView);
  189. mCurModule->mTypeSource = typeSource;
  190. typeSource->mTPI.mSectionData = data;
  191. typeSource->mTPI.mSectionSize = size - 4;
  192. typeSource->mObjectData = mCurModule->mObjectData;
  193. if (useWorkList)
  194. {
  195. mCodeView->mTypeWorkThread.Add(typeSource);
  196. }
  197. else
  198. {
  199. typeSource->mTPI.ScanTypeData();
  200. typeSource->mTPI.ParseTypeData();
  201. }
  202. }
  203. void BlCvParser::ParseSymbolData(void* symbolData, int size, void* relocData, int numRelocs)
  204. {
  205. // static int itrCount = 0;
  206. // itrCount++;
  207. //
  208. // if (itrCount == 0x32)
  209. // {
  210. // NOP;
  211. // }
  212. uint8* data = (uint8*)symbolData;
  213. uint8* symDataEnd = data + size;
  214. int sig = GET(int32);
  215. if (sig != CV_SIGNATURE_C13)
  216. {
  217. Fail("Invalid debug signature");
  218. return;
  219. }
  220. bool relocFailed = false;
  221. uint8* fileChecksumStart = NULL;
  222. uint8* fileChecksumEnd = NULL;
  223. COFFRelocation* nextReloc = (COFFRelocation*)relocData;
  224. COFFRelocation* relocEnd = nextReloc + numRelocs;
  225. const char* strTable = NULL;
  226. while (data < symDataEnd)
  227. {
  228. int sectionNum = GET(int32);
  229. int sectionLen = GET(int32);
  230. uint8* sectionStart = data;
  231. uint8* sectionEnd = data + sectionLen;
  232. if (sectionNum == DEBUG_S_STRINGTABLE)
  233. {
  234. //BF_ASSERT(mCurModule->mStrTab.size() == 0);
  235. //mCurModule->mStrTab.insert(mCurModule->mStrTab.begin(), (char*)sectionStart, (char*)sectionEnd);
  236. strTable = (char*)sectionStart;
  237. }
  238. else if (sectionNum == DEBUG_S_FILECHKSMS)
  239. {
  240. fileChecksumStart = sectionStart;
  241. fileChecksumEnd = sectionEnd;
  242. }
  243. data = sectionEnd;
  244. PTR_ALIGN(data, (uint8*)symbolData, 4);
  245. }
  246. //int bytesPerFile = 24;
  247. // Handle DEBUG_S_FILECHKSMS
  248. if (fileChecksumStart != NULL)
  249. {
  250. data = fileChecksumStart;
  251. uint8* sectionStart = data;
  252. uint8* sectionEnd = fileChecksumEnd;
  253. // Always at least 8 bytes per file info
  254. mChksumOfsToFileIdx.resize((sectionEnd - data) / 8);
  255. mCurModule->mFileInfos.reserve((sectionEnd - data) / 24);
  256. while (data < sectionEnd)
  257. {
  258. int dataOfs = (int)(data - sectionStart);
  259. uint32 fileTableOfs = GET(uint32);
  260. const char* fileName = strTable + fileTableOfs;
  261. uint8 hashLen = GET(uint8);
  262. uint8 hashType = GET(uint8);
  263. BlCvFileInfo fileInfo;
  264. fileInfo.mChksumOfs = dataOfs;
  265. fileInfo.mStrTableIdx = mCodeView->AddToStringTable(fileName);
  266. if (hashLen > 0)
  267. {
  268. BF_ASSERT(hashType == 1);
  269. fileInfo.mHashType = 1;
  270. memcpy(fileInfo.mHash, data, 16);
  271. data += hashLen;
  272. }
  273. else
  274. {
  275. fileInfo.mHashType = 0;
  276. }
  277. mChksumOfsToFileIdx[dataOfs / 8] = (int)mCurModule->mFileInfos.size();
  278. mCurModule->mFileInfos.push_back(fileInfo);
  279. PTR_ALIGN(data, sectionStart, 4);
  280. }
  281. }
  282. data = (uint8*)symbolData + 4;
  283. while (data < symDataEnd)
  284. {
  285. int sectionNum = GET(int32);
  286. int sectionLen = GET(int32);
  287. uint8* sectionStart = data;
  288. uint8* sectionEnd = data + sectionLen;
  289. if ((sectionNum == DEBUG_S_SYMBOLS) || (sectionNum == DEBUG_S_INLINEELINES))
  290. {
  291. //BL_AUTOPERF("DEBUG_S_SYMBOLS");
  292. BlCvSymDataBlock dataBlock;
  293. dataBlock.mSize = (int)(sectionEnd - sectionStart);
  294. dataBlock.mData = sectionStart;
  295. dataBlock.mSourceSectOffset = (int)((uint8*)sectionStart - (uint8*)symbolData);
  296. dataBlock.mRelocStart = nextReloc;
  297. dataBlock.mOutSize = -1;
  298. WIN32_MEMORY_RANGE_ENTRY vAddrs = { dataBlock.mData, (SIZE_T)dataBlock.mSize };
  299. //TODO: This was causing link errors on Win7
  300. #if 0
  301. PrefetchVirtualMemory(GetCurrentProcess(), 1, &vAddrs, 0);
  302. #endif
  303. data = sectionEnd;
  304. while ((nextReloc < relocEnd) && ((int)nextReloc->mVirtualAddress < (int)(sectionEnd - (uint8*)symbolData)))
  305. nextReloc++;
  306. dataBlock.mRelocEnd = nextReloc;
  307. if (sectionNum == DEBUG_S_SYMBOLS)
  308. mCurModule->mSymData.push_back(dataBlock);
  309. else
  310. mCurModule->mInlineData.push_back(dataBlock);
  311. PTR_ALIGN(data, (uint8*)symbolData, 4);
  312. continue;
  313. }
  314. if ((sectionNum & DEBUG_S_IGNORE) != 0)
  315. {
  316. data = sectionEnd;
  317. while ((nextReloc < relocEnd) && ((int)nextReloc->mVirtualAddress < (int)(sectionEnd - (uint8*)symbolData)))
  318. nextReloc++;
  319. PTR_ALIGN(data, (uint8*)symbolData, 4);
  320. continue;
  321. }
  322. switch (sectionNum)
  323. {
  324. case DEBUG_S_LINES:
  325. {
  326. //BL_AUTOPERF("DEBUG_S_LINES");
  327. CV_DebugSLinesHeader_t& lineSec = GET(CV_DebugSLinesHeader_t);
  328. BlReloc reloc;
  329. MapAddress(symbolData, &lineSec.offCon, reloc, nextReloc);
  330. auto lineInfo = mCurModule->mLineInfo.Alloc();
  331. /*if ((reloc.mFlags & BeRelocFlags_Sym) != 0)
  332. {
  333. auto sym = mContext->ProcessSymbol(reloc.mSym);
  334. if (sym->mSegmentIdx >= 0)
  335. {
  336. lineInfo->mStartSegmentIdx = sym->mSegmentIdx;
  337. lineInfo->mStartSegmentOffset = sym->mSegmentOffset;
  338. }
  339. else
  340. relocFailed = true;
  341. }
  342. else*/ if ((reloc.mFlags & BeRelocFlags_Loc) != 0)
  343. {
  344. lineInfo->mStartSegmentIdx = reloc.mSegmentIdx;
  345. lineInfo->mStartSegmentOffset = reloc.mSegmentOffset;
  346. }
  347. else
  348. {
  349. relocFailed = true;
  350. }
  351. lineInfo->mContribBytes = lineSec.cbCon;
  352. while (data < sectionEnd)
  353. {
  354. CV_DebugSLinesFileBlockHeader_t& linesFileHeader = GET(CV_DebugSLinesFileBlockHeader_t);
  355. lineInfo->mLineInfoBlocks.emplace_back(BlCvLineInfoBlock());
  356. auto& lineInfoBlock = lineInfo->mLineInfoBlocks.back();
  357. lineInfoBlock.mLines.reserve(linesFileHeader.nLines);
  358. if ((lineSec.flags & CV_LINES_HAVE_COLUMNS) != 0)
  359. lineInfoBlock.mColumns.reserve(linesFileHeader.nLines);
  360. //BF_ASSERT(linesFileHeader.offFile % bytesPerFile == 0);
  361. //lineInfoBlock.mFileInfoIdx = linesFileHeader.offFile / bytesPerFile;
  362. lineInfoBlock.mFileInfoIdx = mChksumOfsToFileIdx[linesFileHeader.offFile / 8];
  363. for (int lineIdx = 0; lineIdx < linesFileHeader.nLines; lineIdx++)
  364. {
  365. CV_Line_t& srcLineData = GET(CV_Line_t);
  366. BlCvLine cvLine;
  367. cvLine.mLineNumStart = srcLineData.linenumStart;
  368. cvLine.mOffset = srcLineData.offset;
  369. lineInfoBlock.mLines.push_back(cvLine);
  370. }
  371. if ((lineSec.flags & CV_LINES_HAVE_COLUMNS) != 0)
  372. {
  373. for (int lineIdx = 0; lineIdx < linesFileHeader.nLines; lineIdx++)
  374. {
  375. CV_Column_t& srcColumnData = GET(CV_Column_t);
  376. lineInfoBlock.mColumns.push_back(srcColumnData.offColumnStart);
  377. }
  378. }
  379. }
  380. }
  381. break;
  382. case DEBUG_S_INLINEELINES:
  383. {
  384. // Already handled
  385. }
  386. break;
  387. case DEBUG_S_STRINGTABLE:
  388. {
  389. // Already handled
  390. }
  391. break;
  392. case DEBUG_S_FILECHKSMS:
  393. {
  394. // Already handled
  395. }
  396. break;
  397. default:
  398. NotImpl();
  399. }
  400. data = sectionEnd;
  401. PTR_ALIGN(data, (uint8*)symbolData, 4);
  402. }
  403. if (nextReloc != (COFFRelocation*)relocData + numRelocs)
  404. relocFailed = true;
  405. if (relocFailed)
  406. Fail("Failed to apply relocations to debug symbol data");
  407. }
  408. void BlCvParser::AddContribution(int blSectionIdx, int blSectionOfs, int size, int characteristics)
  409. {
  410. if (mCurModule == NULL)
  411. return;
  412. if ((characteristics & IMAGE_SCN_MEM_EXECUTE) != 0)
  413. {
  414. if (mCurModule->mContrib.mCharacteristics == 0)
  415. {
  416. mCurModule->mContrib.mBlSectionIdx = blSectionIdx;
  417. mCurModule->mContrib.mBlSectionOfs = blSectionOfs;
  418. mCurModule->mContrib.mSize = size;
  419. mCurModule->mContrib.mCharacteristics = characteristics;
  420. }
  421. }
  422. BlCvContrib contrib;
  423. contrib.mModuleIdx = mCurModule->mIdx;
  424. contrib.mBlSectionOfs = blSectionOfs;
  425. contrib.mSize = size;
  426. contrib.mCharacteristics = characteristics;
  427. while (blSectionIdx >= (int)mCodeView->mContribMap.mSegments.size())
  428. mCodeView->mContribMap.mSegments.Alloc();
  429. auto contribMapSeg = mCodeView->mContribMap.mSegments[blSectionIdx];
  430. contribMapSeg->mContribs.push_back(contrib);
  431. }
  432. void BlCvParser::FinishModule(PESectionHeader* sectHeaderArr, const BfSizedVectorRef<BlObjectDataSectInfo>& sectInfos, PE_SymInfo* objSyms, const BfSizedVectorRef<BlObjectDataSymInfo>& syms)
  433. {
  434. if (mCurModule == NULL)
  435. return;
  436. mCurModule->mObjSyms = objSyms;
  437. if (!syms.empty())
  438. mCurModule->mSymInfo.insert(mCurModule->mSymInfo.begin(), syms.begin(), syms.end());
  439. for (auto sectInfo : sectInfos)
  440. mCurModule->mSectInfos.push_back(sectInfo);
  441. mSyms = &syms;
  442. //
  443. {
  444. BL_AUTOPERF("BlCvParser::FinishModule ParseTypeData");
  445. for (auto sect : mTypeSects)
  446. {
  447. ParseTypeData((uint8*)mCurModule->mObjectData->mData + sect->mPointerToRawData, sect->mSizeOfRawData);
  448. }
  449. }
  450. //
  451. {
  452. BL_AUTOPERF("BlCvParser::FinishModule ParseSymbolData");
  453. for (auto sect : mSymSects)
  454. {
  455. ParseSymbolData(
  456. (uint8*)mCurModule->mObjectData->mData + sect->mPointerToRawData, sect->mSizeOfRawData,
  457. (uint8*)mCurModule->mObjectData->mData + sect->mPointerToRelocations, sect->mNumberOfRelocations);
  458. }
  459. }
  460. mCodeView->mModuleWorkThread.Add(mCurModule);
  461. }