PIMAGE_SECTION_HEADER CPeFile::GetFirstSectionHeader() { PIMAGE_NT_HEADERS pNtH=NULL; PIMAGE_SECTION_HEADER pSH=NULL; pNtH=GetNtHeaders(); pSH=IMAGE_FIRST_SECTION(pNtH); return pSH; }
// // main // int main(int argc, char **argv) { dbCursor<LibM> cursor1; if (argc != 4) { printf("Usage: %s target lib_root_path full:0|1\n", argv[0]); return 0; } lib_root_path = argv[2]; bFull = argv[3][0] == '1'; TCHAR *filename = argv[1]; TCHAR filename2[MAX_PATH]; int i; DeleteFile("LibM.fdb"); if (!db.open("LibM")) { return 0; } printf("Built on " __DATE__ " " __TIME__ "\nInit db..."); build_instruction_db(); printf("total:%d OK\n", lib_count); //getch(); InitializeCriticalSection(&cs); while (1) { printf("Loading...\n"); if (!LoadFileR(filename, &stMapFile)) { cerr << "ERROR FILE!" << endl; //continue; break; } if (!IsPEFile(stMapFile.ImageBase)) { cerr << "NOT PE FILE!" << endl; UnLoadFile(&stMapFile); //continue; break; } pOH = GetOptionalHeader(stMapFile.ImageBase); pImageNtH = GetNtHeaders(stMapFile.ImageBase); int c0 = 0, c1 = 0, c2 = 0, c3 = 0, c4 = 0, count = 0; // read target function info wsprintf(filename2, _T("%s.txt"), filename); ifstream is2(filename2, ios::binary); if (!is2.is_open()) { break; } int tt; db.detach(); f_info.clear(); while (!is2.eof()) { int startEA, endEA; is2.read((char *)&startEA, sizeof(int)); is2.read((char *)&endEA, sizeof(int)); if (endEA - startEA < MIN_INS_LENGTH) { continue; } function_info fi; memset(&fi, 0, sizeof(fi)); fi.startEA = startEA; fi.len = endEA - startEA; fi.lib_name[0] = 0; f_info.push_back(fi); } total_function_len = f_info.size(); //sort(f_info.begin(), f_info.end(), myfunction); printf("OK\n"); tt = GetTickCount(); int thread_num = bMultiThread ? THREAD_NUM : 1; if (show_dump) { thread_num = 1; } HANDLE *thread = new HANDLE[thread_num]; for (i = 0; i < thread_num; i++) { thread[i] = (HANDLE)_beginthreadex(NULL, 0, bFull ? (&MatchThreadForFull) : (&MatchThread), (void *)i, 0, NULL); } WaitForMultipleObjects(thread_num, thread, TRUE, INFINITE); tt = GetTickCount() - tt; delete[] thread; db.attach(); UnLoadFile(&stMapFile); if (cursor1.select() > 0) { do { delete (ControlFlowGraph *)cursor1->cfg; } while (cursor1.next()); } DWORD s = 0; DWORD _r0 = 0, _r1 = 0, _r34 = 0, _r2 = 0, _r5 = 0, _fn = 0; for (i = 0; i < THREAD_NUM; i++) { s += instruction_count[i]; _r0 += r0[i]; _r1 += r1[i]; _r2 += r2[i]; _r34 += r34[i]; _r5 += r5[i]; _fn += fn[i]; } printf("#fn:%d #instr:%d t:%d found:%d\n", _fn, s, tt, found_c); printf("r1:\t%d\n", _r1); printf("r2:\t%d\n", _r2); printf("r34:\t%d\n", _r34); printf("r5:\t%d\n", _r5); printf("r0:\t%d\n", _r0); printf("fulled:\t%d\n", fulled); printf("check failed:\t%d\n", check_failed); printf("END.\n"); break; } DeleteCriticalSection(&cs); db.detach(); #ifdef _DEBUG cout << "Press ENTER..." << endl; char c; c = cin.get(); #endif return 0; }
bool GetNtHeaders(NtHeaders* nth) { return GetNtHeaders(GetBase(), nth); }