diff --git a/cTools/include/Windows/WinDef.h b/cTools/include/Windows/WinDef.h index 4cd74cc..2e35d7f 100644 --- a/cTools/include/Windows/WinDef.h +++ b/cTools/include/Windows/WinDef.h @@ -21,6 +21,8 @@ #ifndef _WINDEF_ #define _WINDEF_ +#include "os.h" + #ifndef WINVER #define WINVER 0x0500 #endif @@ -252,7 +254,11 @@ typedef unsigned int UINT, *PUINT; typedef float FLOAT, *PFLOAT; typedef char *PSZ; typedef long *LPLONG; +#ifdef __WIN__ typedef unsigned long DWORD, *PDWORD, *LPDWORD; +#else /* __WIN__ */ +typedef uint32_t DWORD, *PDWORD, *LPDWORD; +#endif /* __WIN__ */ typedef DWORD RVA; diff --git a/cTools/include/Windows/WinNT.h b/cTools/include/Windows/WinNT.h index 31aeb2e..0432e3e 100644 --- a/cTools/include/Windows/WinNT.h +++ b/cTools/include/Windows/WinNT.h @@ -168,6 +168,12 @@ typedef BYTE FCHAR; typedef WORD FSHORT; typedef DWORD FLONG; +// C standard headers +#include + +static_assert(sizeof(WORD) == 2, "WORD 2 bytes"); +static_assert(sizeof(DWORD) == 4, "WORD 4 bytes"); + /* * File formats definitions */ diff --git a/cTools/libs/binParse/peParse/pe64Parse.c b/cTools/libs/binParse/peParse/pe64Parse.c index a474d8b..5fcc98d 100644 --- a/cTools/libs/binParse/peParse/pe64Parse.c +++ b/cTools/libs/binParse/peParse/pe64Parse.c @@ -296,9 +296,11 @@ static PE64_ERROR pe64ParseMaybeObj(PE64File *pe) return PE64_INV_ARG; PE64File peCopy = *pe; - PE64_ERROR res = pe64ParseFileHeader(&peCopy); - if (res != PE64_OK) - return res; + PE64_ERROR err = pe64ParseFileHeader(&peCopy); + if (err != PE64_OK) { + Free(peCopy.fileHeader); + return err; + } switch (pe64GetMachineID(&peCopy)) { case IMAGE_FILE_MACHINE_UNKNOWN: @@ -333,24 +335,32 @@ static PE64_ERROR pe64ParseMaybeObj(PE64File *pe) case IMAGE_FILE_MACHINE_CEE: break; default: - return PE64_INV_MACHINE_TYPE; + err = PE64_INV_MACHINE_TYPE; + goto eexit; } uint64_t off = sizeof (FileHeader); char *firstSectName = readFromFile(peCopy.fd, (size_t*)&off, sizeof(TEXT_SECT_NAME) + 1); - if (firstSectName == NULL) - return PE64_NO_MEM; + if (firstSectName == NULL) { + err = PE64_NO_MEM; + goto eexit; + } if (strcmp(firstSectName, TEXT_SECT_NAME)) { Free(firstSectName); - return PE64_NO_OBJ; + err = PE64_NO_OBJ; + goto eexit; } Free(firstSectName); pe->fileHeader = peCopy.fileHeader; pe->type = PE64_OBJ; return PE64_OK; + +eexit: + Free(peCopy.fileHeader); + return err; } PE64File *pe64Parse(const char *fn) @@ -480,6 +490,8 @@ void pe64Free(PE64File *pe) pe->sortSymtab = (void*)-1; pe->symNum = (uint64_t)-1; pe->strtab = (void*)-1; + + Free(pe); } uint64_t pe64AddrToFileOff(const PE64File *pe, uint64_t addr) diff --git a/cTools/libs/binPrinter/pePrinter/CMakeLists.txt b/cTools/libs/binPrinter/pePrinter/CMakeLists.txt index e497387..d2c45f8 100644 --- a/cTools/libs/binPrinter/pePrinter/CMakeLists.txt +++ b/cTools/libs/binPrinter/pePrinter/CMakeLists.txt @@ -41,7 +41,5 @@ add_vipon_library( INSTALL ON ) -if (WIN32) - add_subdirectory(test) -endif (WIN32) +add_subdirectory(test) diff --git a/cTools/libs/binPrinter/pePrinter/pe64PrinterDelayImports.c b/cTools/libs/binPrinter/pePrinter/pe64PrinterDelayImports.c index 3cf713d..35cb7b3 100644 --- a/cTools/libs/binPrinter/pePrinter/pe64PrinterDelayImports.c +++ b/cTools/libs/binPrinter/pePrinter/pe64PrinterDelayImports.c @@ -1,7 +1,7 @@ /*** * MIT License * - * Copyright (c) 2021 Konychev Valerii + * Copyright (c) 2021-2023 Konychev Valerii * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -65,7 +65,7 @@ static void pe64PrintDelayImportHmod(const PEDelimp *delimp) if (delimp == NULL) return; - printf("%lx", delimp->rvaDLLName); + printf("%x", (uint32_t)delimp->rvaDLLName); } static void pe64PrintDelayImportIAT(const PEDelimp *delimp) @@ -73,7 +73,7 @@ static void pe64PrintDelayImportIAT(const PEDelimp *delimp) if (delimp == NULL) return; - printf("%lx", delimp->rvaIAT); + printf("%x", (uint32_t)delimp->rvaIAT); } static void pe64PrintDelayImportINT(const PEDelimp *delimp) @@ -81,7 +81,7 @@ static void pe64PrintDelayImportINT(const PEDelimp *delimp) if (delimp == NULL) return; - printf("%lx", delimp->rvaINT); + printf("%x", (uint32_t)delimp->rvaINT); } static void pe64PrintDelayImportBoundIAT(const PEDelimp *delimp) @@ -89,7 +89,7 @@ static void pe64PrintDelayImportBoundIAT(const PEDelimp *delimp) if (delimp == NULL) return; - printf("%lx", delimp->rvaBoundIAT); + printf("%x", (uint32_t)delimp->rvaBoundIAT); } static void pe64PrintDelayImportUnloadIAT(const PEDelimp *delimp) @@ -97,7 +97,7 @@ static void pe64PrintDelayImportUnloadIAT(const PEDelimp *delimp) if (delimp == NULL) return; - printf("%lx", delimp->rvaUnloadIAT); + printf("%x", (uint32_t)delimp->rvaUnloadIAT); } static void pe64PrintDelayImportTimeStamp(const PEDelimp *delimp) @@ -119,35 +119,31 @@ void pe64PrintDelayImport(const PE64File *pe, const PEDelimp *delimp) pe64PrintDelayImportName(pe, delimp); NEW_LINE; - TAB; - printf("type:\t"); + printf("%13s: ", "type"); pe64PrintDelayImportAttr(delimp); NEW_LINE; - TAB; - printf("handle:\t"); + printf("%13s: ", "handle"); pe64PrintDelayImportHmod(delimp); NEW_LINE; - TAB; - printf("IAT:\t"); + printf("%13s: ", "IAT"); pe64PrintDelayImportIAT(delimp); NEW_LINE; - TAB; - printf("INT:\t"); + printf("%13s: ", "INT"); pe64PrintDelayImportINT(delimp); NEW_LINE; - TAB; - printf("BoundIAT:\t"); + printf("%13s: ", "BoundIAT"); pe64PrintDelayImportBoundIAT(delimp); NEW_LINE; - TAB; - printf("UnloadIAT:\t"); + printf("%13s: ", "UnloadIAT"); pe64PrintDelayImportUnloadIAT(delimp); NEW_LINE; - TAB; - printf("TimeStamp:\t"); + printf("%13s: ", "TimeStamp"); pe64PrintDelayImportTimeStamp(delimp); NEW_LINE; + printf("%8sIndx Name\n", ""); + printf("%8s---- --------\n", ""); + FileD fd = pe->fd; uint64_t off = pe64AddrToFileOff(pe, delimp->rvaINT); for(;;) { @@ -155,8 +151,6 @@ void pe64PrintDelayImport(const PE64File *pe, const PEDelimp *delimp) uint64_t AddressOfData = INT->u1.AddressOfData; if (AddressOfData) { - TAB; - TAB; pe64PrintINT(pe, INT); NEW_LINE; } else { diff --git a/cTools/libs/binPrinter/pePrinter/pe64PrinterHeaders.c b/cTools/libs/binPrinter/pePrinter/pe64PrinterHeaders.c index 325a38e..257860b 100644 --- a/cTools/libs/binPrinter/pePrinter/pe64PrinterHeaders.c +++ b/cTools/libs/binPrinter/pePrinter/pe64PrinterHeaders.c @@ -1,7 +1,7 @@ /*** * MIT License * - * Copyright (c) 2021 Konychev Valerii + * Copyright (c) 2021-2023 Konychev Valerii * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -43,7 +43,7 @@ static void pePrintDosNtHeaderOff(const DosHeader *dosHeader) if (dosHeader == NULL) return; - printf("%.8lx", dosHeader->e_lfanew); + printf("%.8x", (uint32_t)dosHeader->e_lfanew); } void pe64PrintDosHeader(const PE64File *pe) @@ -52,12 +52,10 @@ void pe64PrintDosHeader(const PE64File *pe) return; printf("DOS Header:\n"); - TAB; - printf("Magic:\t\t\t"); + printf("%20s: ", "Magic"); pe64PrintDosMagic(pe->dosHeader); NEW_LINE - TAB; - printf("NT Header Offset:\t"); + printf("%20s: ", "NT Header Offset"); pePrintDosNtHeaderOff(pe->dosHeader); NEW_LINE; @@ -188,7 +186,7 @@ static void pe64PrintTimeStamp(const FileHeader *fileHeader) return; time_t time = (time_t)fileHeader->TimeDateStamp; - printf("%s", asctime(localtime(&time))); + printf("%s", asctime(gmtime(&time))); } static void pe64PrintSymTabPtr(const FileHeader *fileHeader) @@ -196,7 +194,7 @@ static void pe64PrintSymTabPtr(const FileHeader *fileHeader) if (fileHeader == NULL) return; - printf("%.8lx", fileHeader->PointerToSymbolTable); + printf("%.8x", (uint32_t)fileHeader->PointerToSymbolTable); } static void pe64PrintSymNum(const FileHeader *fileHeader) @@ -204,7 +202,7 @@ static void pe64PrintSymNum(const FileHeader *fileHeader) if (fileHeader == NULL) return; - printf("%.8lx", fileHeader->NumberOfSymbols); + printf("%.8x", (uint32_t)fileHeader->NumberOfSymbols); } static void pe64PrintOptHeaderSize(const FileHeader *fileHeader) @@ -224,10 +222,10 @@ static void pe64PrintCharacteristics(const FileHeader *fileHeader) printf("%.4hx\n", type); if (type) { - TAB; - TAB; - TAB; - TAB; + SPACEs(4); + SPACEs(4); + SPACEs(4); + SPACEs(4); } if (type & IMAGE_FILE_BYTES_REVERSED_HI) printf("REVERSED_HI "); @@ -267,24 +265,24 @@ void pe64PrintFileHeader(const PE64File *pe) return; printf("File Header:\n"); - printf("\tMachine:\t\t"); + printf("%20s: ", "Machine"); pe64PrintMachineId(pe->fileHeader); NEW_LINE; - printf("\tNumber of Sections:\t"); + printf("%20s: ", "Number of Sections"); pe64PrintSectNum(pe->fileHeader); NEW_LINE; - printf("\tTime stamp:\t\t"); + printf("%20s: ", "Time stamp"); pe64PrintTimeStamp(pe->fileHeader); - printf("\tSymTab Pointer:\t\t"); + printf("%20s: ", "SymTab Pointer"); pe64PrintSymTabPtr(pe->fileHeader); NEW_LINE; - printf("\tNumber of Symbols:\t"); + printf("%20s: ", "Number of Symbols"); pe64PrintSymNum(pe->fileHeader); NEW_LINE; - printf("\tSize of Opt Header:\t"); + printf("%20s: ", "Size of Opt Header"); pe64PrintOptHeaderSize(pe->fileHeader); NEW_LINE; - printf("\tCharacteristics:\t"); + printf("%20s: ", "Characteristics"); pe64PrintCharacteristics(pe->fileHeader); NEW_LINE; } @@ -322,7 +320,7 @@ static void pe64PrintCodeSize(const OptHeader64 *optHeader) if (optHeader == NULL) return; - printf("%.8lx", optHeader->SizeOfCode); + printf("%.8x", (uint32_t)optHeader->SizeOfCode); } static void pe64PrintInitializedDataSize(const OptHeader64 *optHeader) @@ -330,7 +328,7 @@ static void pe64PrintInitializedDataSize(const OptHeader64 *optHeader) if (optHeader == NULL) return; - printf("%.8lx", optHeader->SizeOfInitializedData); + printf("%.8x", (uint32_t)optHeader->SizeOfInitializedData); } static void pe64PrintUninitializedDataSize(const OptHeader64 *optHeader) @@ -338,7 +336,7 @@ static void pe64PrintUninitializedDataSize(const OptHeader64 *optHeader) if (optHeader == NULL) return; - printf("%.8lx", optHeader->SizeOfUninitializedData); + printf("%.8x", (uint32_t)optHeader->SizeOfUninitializedData); } static void pe64PrintEntryPoint(const OptHeader64 *optHeader) @@ -346,7 +344,7 @@ static void pe64PrintEntryPoint(const OptHeader64 *optHeader) if (optHeader == NULL) return; - printf("%.8lx", optHeader->AddressOfEntryPoint); + printf("%.8x", (uint32_t)optHeader->AddressOfEntryPoint); } static void pe64PrintCodeBase(const OptHeader64 *optHeader) @@ -354,7 +352,7 @@ static void pe64PrintCodeBase(const OptHeader64 *optHeader) if (optHeader == NULL) return; - printf("%.8lx", optHeader->BaseOfCode); + printf("%.8x", (uint32_t)optHeader->BaseOfCode); } static void pe64PrintImageBase(const OptHeader64 *optHeader) @@ -370,7 +368,7 @@ static void pe64PrintSectionAlignment(const OptHeader64 *optHeader) if (optHeader == NULL) return; - printf("%.8lx", optHeader->SectionAlignment); + printf("%.8x", (uint32_t)optHeader->SectionAlignment); } static void pe64PrintFileAlignment(const OptHeader64 *optHeader) @@ -378,7 +376,7 @@ static void pe64PrintFileAlignment(const OptHeader64 *optHeader) if (optHeader == NULL) return; - printf("%.8lx", optHeader->FileAlignment); + printf("%.8x", (uint32_t)optHeader->FileAlignment); } static void pe64PrintOSVersion(const OptHeader64 *optHeader) @@ -410,7 +408,7 @@ static void pe64PrintWin32VersionValue(const OptHeader64 *optHeader) if (optHeader == NULL) return; - printf("%.8lx", optHeader->Win32VersionValue); + printf("%.8x", (uint32_t)optHeader->Win32VersionValue); } static void pe64PrintImageSize(const OptHeader64 *optHeader) @@ -418,7 +416,7 @@ static void pe64PrintImageSize(const OptHeader64 *optHeader) if (optHeader == NULL) return; - printf("%.8lx", optHeader->SizeOfImage); + printf("%.8x", (uint32_t)optHeader->SizeOfImage); } static void pe64PrintHeadersSize(const OptHeader64 *optHeader) @@ -426,7 +424,7 @@ static void pe64PrintHeadersSize(const OptHeader64 *optHeader) if (optHeader == NULL) return; - printf("%.8lx", optHeader->SizeOfHeaders); + printf("%.8x", (uint32_t)optHeader->SizeOfHeaders); } static void pe64PrintCheckSum(const OptHeader64 *optHeader) @@ -434,7 +432,7 @@ static void pe64PrintCheckSum(const OptHeader64 *optHeader) if (optHeader == NULL) return; - printf("%.8lx", optHeader->CheckSum); + printf("%.8x", (uint32_t)optHeader->CheckSum); } static void pe64PrintSubsystem(const OptHeader64 *optHeader) @@ -499,9 +497,7 @@ static void pe64PrintDllCharacteristics(const OptHeader64 *optHeader) printf("%.4hx\n", DllCharacteristics); if (DllCharacteristics) { - TAB; - TAB; - TAB; + SPACEs(22); } if (DllCharacteristics & IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA) printf("HIGH_ENTROPY_VA "); @@ -564,7 +560,7 @@ static void pe64PrintLoaderFlags(const OptHeader64 *optHeader) if (optHeader == NULL) return; - printf("%.8lx", optHeader->LoaderFlags); + printf("%.8x", (uint32_t)optHeader->LoaderFlags); } static void pe64PrintNumOfRvaAndSizes(const OptHeader64 *optHeader) @@ -572,7 +568,7 @@ static void pe64PrintNumOfRvaAndSizes(const OptHeader64 *optHeader) if (optHeader == NULL) return; - printf("%.8lx", optHeader->NumberOfRvaAndSizes); + printf("%.8x", (uint32_t)optHeader->NumberOfRvaAndSizes); } void pe64PrintDataDir(const DataDir *dataDir) @@ -580,7 +576,7 @@ void pe64PrintDataDir(const DataDir *dataDir) if (dataDir == NULL) return; - printf("%.8lx %.8lx", dataDir->VirtualAddress, dataDir->Size); + printf("%.8x %.8x", (uint32_t)dataDir->VirtualAddress, (uint32_t)dataDir->Size); } void pe64PrintOptHeader(const PE64File *pe) @@ -589,135 +585,135 @@ void pe64PrintOptHeader(const PE64File *pe) return; printf("Optional Header:\n"); - printf("\tApplication Type:\t"); + printf("%20s: ", "Application Type"); pe64PrintOptHeaderMagic(pe->optHeader); NEW_LINE; - printf("\tLinker Version:\t\t"); + printf("%20s: ", "Linker Version"); pe64PrintLinkerVersion(pe->optHeader); NEW_LINE; - printf("\tCode Size:\t\t"); + printf("%20s: ", "Code Size"); pe64PrintCodeSize(pe->optHeader); NEW_LINE; - printf("\tInit Data Size:\t\t"); + printf("%20s: ", "Init Data Size"); pe64PrintInitializedDataSize(pe->optHeader); NEW_LINE; - printf("\tUninit Data Size:\t"); + printf("%20s: ", "Uninit Data Size"); pe64PrintUninitializedDataSize(pe->optHeader); NEW_LINE; - printf("\tEntry Point:\t\t"); + printf("%20s: ", "Entry Point"); pe64PrintEntryPoint(pe->optHeader); NEW_LINE; - printf("\tCode Base:\t\t"); + printf("%20s: ", "Code Base"); pe64PrintCodeBase(pe->optHeader); NEW_LINE; - printf("\tImage Base:\t\t"); + printf("%20s: ", "Image Base"); pe64PrintImageBase(pe->optHeader); NEW_LINE; - printf("\tSection Alignment:\t"); + printf("%20s: ", "Section Alignment"); pe64PrintSectionAlignment(pe->optHeader); NEW_LINE; - printf("\tFile Alignment:\t\t"); + printf("%20s: ", "File Alignment"); pe64PrintFileAlignment(pe->optHeader); NEW_LINE; - printf("\tOS Version:\t\t"); + printf("%20s: ", "OS Version"); pe64PrintOSVersion(pe->optHeader); NEW_LINE; - printf("\tImage Version:\t\t"); + printf("%20s: ", "Image Version"); pe64PrintImageVersion(pe->optHeader); NEW_LINE; - printf("\tSubsystem Version:\t"); + printf("%20s: ", "Subsystem Version"); pe64PrintSubsystemVersion(pe->optHeader); NEW_LINE; - printf("\tWin32 Version Val:\t"); + printf("%20s: ", "Win32 Version Val"); pe64PrintWin32VersionValue(pe->optHeader); NEW_LINE; - printf("\tImage Size:\t\t"); + printf("%20s: ", "Image Size"); pe64PrintImageSize(pe->optHeader); NEW_LINE; - printf("\tHeaders Size:\t\t"); + printf("%20s: ", "Headers Size"); pe64PrintHeadersSize(pe->optHeader); NEW_LINE; - printf("\tCheckSum:\t\t"); + printf("%20s: ", "CheckSum"); pe64PrintCheckSum(pe->optHeader); NEW_LINE; - printf("\tSubsystem:\t\t"); + printf("%20s: ", "Subsystem"); pe64PrintSubsystem(pe->optHeader); NEW_LINE; - printf("\tDllCharacteristics:\t"); + printf("%20s: ", "DllCharacteristics"); pe64PrintDllCharacteristics(pe->optHeader); NEW_LINE; - printf("\tStack Size Reserve:\t"); + printf("%20s: ", "Stack Size Reserve"); pe64PrintStackSizeReserve(pe->optHeader); NEW_LINE; - printf("\tStack Size Commit:\t"); + printf("%20s: ", "Stack Size Commit"); pe64PrintStackSizeCommit(pe->optHeader); NEW_LINE; - printf("\tHeap Size Reserve:\t"); + printf("%20s: ", "Heap Size Reserve"); pe64PrintHeapSizeReserve(pe->optHeader); NEW_LINE; - printf("\tHeap Size Commit:\t"); + printf("%20s: ", "Heap Size Commit"); pe64PrintHeapSizeCommit(pe->optHeader); NEW_LINE; - printf("\tLoader Flags:\t\t"); + printf("%20s: ", "Loader Flags"); pe64PrintLoaderFlags(pe->optHeader); NEW_LINE; - printf("\tNumberOfRvaAndSizes:\t"); + printf("%20s: ", "NumberOfRvaAndSizes"); pe64PrintNumOfRvaAndSizes(pe->optHeader); NEW_LINE; - printf("Data Directory:\tType\t\tAddr\t Size\n"); - printf("\t\t--------------- -------- --------\n"); + printf("%20s: %15s %8s %8s\n", "Data Directory", "Type", "Addr", "Size"); + printf("%21s %s\n", "", "--------------- -------- --------"); DWORD i = 0; DWORD NumberOfRvaAndSizes = pe->optHeader->NumberOfRvaAndSizes; for (i = 0; i < NumberOfRvaAndSizes; ++i) { switch (i) { case IMAGE_DIRECTORY_ENTRY_EXPORT: - printf("\t\tEXPORT:\t\t"); + printf("%36s: ", "EXPORT"); break; case IMAGE_DIRECTORY_ENTRY_IMPORT: - printf("\t\tIMPORT:\t\t"); + printf("%36s: ", "IMPORT"); break; case IMAGE_DIRECTORY_ENTRY_RESOURCE: - printf("\t\tRESOURCE:\t"); + printf("%36s: ", "RESOURCE"); break; case IMAGE_DIRECTORY_ENTRY_EXCEPTION: - printf("\t\tEXCEPTION:\t"); + printf("%36s: ", "EXCEPTION"); break; case IMAGE_DIRECTORY_ENTRY_SECURITY: - printf("\t\tSECURITY:\t"); + printf("%36s: ", "SECURITY"); break; case IMAGE_DIRECTORY_ENTRY_BASERELOC: - printf("\t\tBASERELOC:\t"); + printf("%36s: ", "BASERELOC"); break; case IMAGE_DIRECTORY_ENTRY_DEBUG: - printf("\t\tDEBUG:\t\t"); + printf("%36s: ", "DEBUG"); break; case IMAGE_DIRECTORY_ENTRY_ARCHITECTURE: - printf("\t\tARCHITECTURE:\t"); + printf("%36s: ", "ARCHITECTURE"); break; case IMAGE_DIRECTORY_ENTRY_GLOBALPTR: - printf("\t\tGLOBALPTR:\t"); + printf("%36s: ", "GLOBALPTR"); break; case IMAGE_DIRECTORY_ENTRY_TLS: - printf("\t\tTLS:\t\t"); + printf("%36s: ", "TLS"); break; case IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG: - printf("\t\tLOAD_CONFIG:\t"); + printf("%36s: ", "LOAD_CONFIG"); break; case IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT: - printf("\t\tBOUND_IMPORT:\t"); + printf("%36s: ", "BOUND_IMPORT"); break; case IMAGE_DIRECTORY_ENTRY_IAT: - printf("\t\tIAT:\t\t"); + printf("%36s: ", "IAT"); break; case IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT: - printf("\t\tDELAY_IMPORT:\t"); + printf("%36s: ", "DELAY_IMPORT"); break; case IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR: - printf("\t\tCOM_DESCRIPTOR:\t"); + printf("%36s: ", "COM_DESCRIPTOR"); break; default: - printf("\t\tRESERVED:\t"); + printf("%36s: ", "RESERVED"); break; } pe64PrintDataDir(pe->optHeader->DataDirectory + i); @@ -731,7 +727,7 @@ void pe64PrintNtHeader(const PE64File *pe) return; printf("NT Header:\n"); - printf("\tMagic:\t\t\t"); + printf("%20s: ", "Magic"); pe64PrintNTMagic(pe->ntHeader); NEW_LINE; pe64PrintFileHeader(pe); diff --git a/cTools/libs/binPrinter/pePrinter/pe64PrinterImports.c b/cTools/libs/binPrinter/pePrinter/pe64PrinterImports.c index de3f5fa..5ee11ba 100644 --- a/cTools/libs/binPrinter/pePrinter/pe64PrinterImports.c +++ b/cTools/libs/binPrinter/pePrinter/pe64PrinterImports.c @@ -1,7 +1,7 @@ /*** * MIT License * - * Copyright (c) 2021 Konychev Valerii + * Copyright (c) 2021-2023 Konychev Valerii * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -48,7 +48,7 @@ static void pe64PrintImportTimeStamp(const PE64File *pe, const PEImport *import) if (pe == NULL || import == NULL) return; - printf("%ld", import->TimeDateStamp); + printf("%d", (int32_t)import->TimeDateStamp); } static void pe64PrintImportForwardIndex(const PE64File *pe, const PEImport *import) @@ -56,7 +56,7 @@ static void pe64PrintImportForwardIndex(const PE64File *pe, const PEImport *impo if (pe == NULL || import == NULL) return; - printf("%ld", import->ForwarderChain); + printf("%d", (int32_t)import->ForwarderChain); } static void pe64PrintImporAddrTable(const PE64File *pe, const PEImport *import) @@ -64,7 +64,7 @@ static void pe64PrintImporAddrTable(const PE64File *pe, const PEImport *import) if (pe == NULL || import == NULL) return; - printf("%lx", import->FirstThunk); + printf("%x", (uint32_t)import->FirstThunk); } static void pe64PrintImporNameTable(const PE64File *pe, const PEImport *import) @@ -72,7 +72,7 @@ static void pe64PrintImporNameTable(const PE64File *pe, const PEImport *import) if (pe == NULL || import == NULL) return; - printf("%lx", import->OriginalFirstThunk); + printf("%x", (uint32_t)import->OriginalFirstThunk); } void pe64PrintINT(const PE64File *pe, ThunkData64 *INT) @@ -85,12 +85,12 @@ void pe64PrintINT(const PE64File *pe, ThunkData64 *INT) if (IS_BIT_SET(AddressOfData, 63)) { // Import by number - printf("%.4"PRIx64, (uint64_t)CLR_BIT(AddressOfData, 63)); + printf("%8s%.4"PRIx64, "", (uint64_t)CLR_BIT(AddressOfData, 63)); } else { // Import by name uint64_t importByNameOff = pe64AddrToFileOff(pe, AddressOfData); ImportByName *importByName = readFromFile(fd, (size_t*)&importByNameOff, 256); - printf("%.4hx %s", importByName->Hint, importByName->Name); + printf("%8s%.4hx %s", "", importByName->Hint, importByName->Name); Free(importByName); } } @@ -102,25 +102,21 @@ void pe64PrintImport(const PE64File *pe, const PEImport *import) pe64PrintImportName(pe, import); NEW_LINE; - TAB; - printf("addr table:\t\t"); + printf("%13s: ", "addr table"); pe64PrintImporAddrTable(pe, import); NEW_LINE; - TAB; - printf("name table:\t\t"); + printf("%13s: ", "name table"); pe64PrintImporNameTable(pe, import); NEW_LINE; - TAB; - printf("time stamp:\t\t"); + printf("%13s: ", "time stamp"); pe64PrintImportTimeStamp(pe, import); NEW_LINE; - TAB; - printf("forward index:\t"); + printf("%13s: ", "forward index"); pe64PrintImportForwardIndex(pe, import); NEW_LINE; - printf("\t\tIndx Name\n"); - printf("\t\t---- --------\n"); + printf("%8sIndx Name\n", ""); + printf("%8s---- --------\n", ""); if (import->TimeDateStamp == (DWORD)-1) { // Static bound @@ -134,8 +130,8 @@ void pe64PrintImport(const PE64File *pe, const PEImport *import) uint64_t AddressOfData = INT->u1.AddressOfData; if (AddressOfData) { - TAB; - TAB; + + pe64PrintINT(pe, INT); NEW_LINE; } else { diff --git a/cTools/libs/binPrinter/pePrinter/pe64PrinterSections.c b/cTools/libs/binPrinter/pePrinter/pe64PrinterSections.c index ee0e2dc..2d57ece 100644 --- a/cTools/libs/binPrinter/pePrinter/pe64PrinterSections.c +++ b/cTools/libs/binPrinter/pePrinter/pe64PrinterSections.c @@ -1,7 +1,7 @@ /*** * MIT License * - * Copyright (c) 2021 Konychev Valerii + * Copyright (c) 2021-2023 Konychev Valerii * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -41,9 +41,9 @@ void pe64PrintSectName(const PE64File *pe, const PESection *sect) break; case PE64_OBJ: if (sect->Name[0] == '/') - printf("%16s ", pe64GetLongSectName(pe, sect)); + printf("%s ", pe64GetLongSectName(pe, sect)); else - printf("%16.*s ", IMAGE_SIZEOF_SHORT_NAME, sect->Name); + printf("%.*s ", IMAGE_SIZEOF_SHORT_NAME, sect->Name); break; default: @@ -59,12 +59,12 @@ static void pe64PrintSectMisc(const PE64File *pe, const PESection *sect) switch (pe->type) { case PE64_OBJ: - printf("PhysAddr:\t%.8lx ", sect->Misc.PhysicalAddress); + printf("%13s: %.8x ", "PhysAddr", (uint32_t)sect->Misc.PhysicalAddress); break; case PE64_EXEC: case PE64_SHARED: default: - printf("VirtSize:\t%.8lx ", sect->Misc.VirtualSize); + printf("%13s: %.8x ", "VirtSize", (uint32_t)sect->Misc.VirtualSize); break; } } @@ -74,7 +74,7 @@ static void pe64PrintSectVirtAddr(const PESection *sect) if (sect == NULL) return; - printf("VirtAddr:\t\t%.8lx ", sect->VirtualAddress); + printf("%13s: %.8x ", "VirtAddr", (uint32_t)sect->VirtualAddress); } static void pe6464PrintSectSizeOfRawData(const PESection *sect) @@ -82,7 +82,7 @@ static void pe6464PrintSectSizeOfRawData(const PESection *sect) if (sect == NULL) return; - printf("raw data size:\t\t%.8lx ", sect->SizeOfRawData); + printf("%13s: %.8x ", "raw data size", (uint32_t)sect->SizeOfRawData); } static void pe6464PrintPointerToRawData(const PESection *sect) @@ -90,7 +90,7 @@ static void pe6464PrintPointerToRawData(const PESection *sect) if (sect == NULL) return; - printf("raw data offs:\t%.8lx ", sect->PointerToRawData); + printf("%13s: %.8x ", "raw data offs", (uint32_t)sect->PointerToRawData); } static void pe6464PrintPointerToRelocations(const PESection *sect) @@ -98,7 +98,7 @@ static void pe6464PrintPointerToRelocations(const PESection *sect) if (sect == NULL) return; - printf("reloc offs:\t%.8lx ", sect->PointerToRelocations); + printf("%13s: %.8x ", "reloc offs", (uint32_t)sect->PointerToRelocations); } static void pe6464PrintPointerToLinenumbers(const PESection *sect) @@ -106,7 +106,7 @@ static void pe6464PrintPointerToLinenumbers(const PESection *sect) if (sect == NULL) return; - printf("line # offs:\t%.8lx ", sect->PointerToLinenumbers); + printf("%13s: %.8x ", "line # offs", (uint32_t)sect->PointerToLinenumbers); } static void pe6464PrintNumberOfRelocations(const PESection *sect) @@ -114,7 +114,7 @@ static void pe6464PrintNumberOfRelocations(const PESection *sect) if (sect == NULL) return; - printf("relocations:\t\t%.8hx ", sect->NumberOfRelocations); + printf("%13s: %.8hx ", "relocations", sect->NumberOfRelocations); } static void pe6464PrintNumberOfLinenumbers(const PESection *sect) @@ -122,7 +122,7 @@ static void pe6464PrintNumberOfLinenumbers(const PESection *sect) if (sect == NULL) return; - printf("line #'s:\t\t%.8hx ", sect->NumberOfLinenumbers); + printf("%13s: %.8hx ", "line #'s", sect->NumberOfLinenumbers); } static void pe6464PrintCharacteristics(const PESection *sect) @@ -130,7 +130,7 @@ static void pe6464PrintCharacteristics(const PESection *sect) if (sect == NULL) return; - printf("flags: %.8lx ", sect->Characteristics); + printf("%13s: %.8x ", "flags", (uint32_t)sect->Characteristics); } void pe64PrintSection(const PE64File *pe, const PESection *sect) diff --git a/cTools/libs/binPrinter/pePrinter/pe64PrinterSymbols.c b/cTools/libs/binPrinter/pePrinter/pe64PrinterSymbols.c index 53b94ee..f32bd46 100644 --- a/cTools/libs/binPrinter/pePrinter/pe64PrinterSymbols.c +++ b/cTools/libs/binPrinter/pePrinter/pe64PrinterSymbols.c @@ -1,7 +1,7 @@ /*** * MIT License * - * Copyright (c) 2021 Konychev Valerii + * Copyright (c) 2021-2023 Konychev Valerii * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -44,7 +44,7 @@ static void pe64PrintSymValue(const PESymbol *sym) if (sym == NULL) return; - printf("%.16lx ", sym->Value); + printf("%.16x ", (uint32_t)sym->Value); } static void pe64PrintSymSection(const PE64File *pe, const PESymbol *sym) @@ -65,7 +65,7 @@ static void pe64PrintSymSection(const PE64File *pe, const PESymbol *sym) printf("%16s ", "N_DEBUG"); break; default: - pe64PrintSectName(pe, sect); + printf("%16s ", pe64GetSectName(pe, sect)); break; } } @@ -77,16 +77,16 @@ static void pe64PrintSymType(const PESymbol *sym) switch (sym->Type >> 8) { case IMAGE_SYM_DTYPE_NULL: - printf("%7s:", "DT_NON"); + printf("%s ", "DT_NON"); break; case IMAGE_SYM_DTYPE_POINTER: - printf("%7s:", "DT_PTR"); + printf("%s ", "DT_PTR"); break; case IMAGE_SYM_DTYPE_FUNCTION: - printf("%7s:", "DT_FCN"); + printf("%s ", "DT_FCN"); break; case IMAGE_SYM_DTYPE_ARRAY: - printf("%7s:", "DT_ARY"); + printf("%s ", "DT_ARY"); break; default: break; @@ -266,12 +266,12 @@ void pe64PrintAuxSymSect(const PE64File *pe, const PEAuxSymbol *auxSym) if (pe == NULL || auxSym == NULL) return; - printf("\t Len: %.8lx", auxSym->Section.Length); - printf("\t Relocs: %.4hx", auxSym->Section.NumberOfRelocations); - printf("\t Lines: %.4hx", auxSym->Section.NumberOfLinenumbers); - printf("\t CheckSum: %.8lx", auxSym->Section.CheckSum); - printf("\t AssocNum: %.4hu", auxSym->Section.Number); - printf("\t Selec: %.2hhx", auxSym->Section.Selection); + printf("%8s: %.8x", "Len",(uint32_t)auxSym->Section.Length); + printf("%8s: %.4hx", "Relocs", auxSym->Section.NumberOfRelocations); + printf("%8s: %.4hx", "Lines", auxSym->Section.NumberOfLinenumbers); + printf("%10s: %.8x", "CheckSum", (uint32_t)auxSym->Section.CheckSum); + printf("%10s: %.4hu", "AssocNum", auxSym->Section.Number); + printf("%7s: %.2hhx", "Selec", auxSym->Section.Selection); } void pe64PrintAuxSymFile(const PE64File *pe, const PEAuxSymbol *auxSym) @@ -279,7 +279,7 @@ void pe64PrintAuxSymFile(const PE64File *pe, const PEAuxSymbol *auxSym) if (pe == NULL || auxSym == NULL) return; - printf("\t %s", auxSym->File.Name); + printf("%5s%s", "", auxSym->File.Name); } void pe64PrintAuxSymbol(const PE64File *pe, const PESymbol *sym, const PEAuxSymbol *auxSym) @@ -310,14 +310,14 @@ void pe64PrintSymbols(const PE64File *pe) printf("%16s ", "Name"); printf("%16s ", "Value"); printf("%16s ", "Section"); - printf("%16s ", "Type"); + printf("%15s ", "Type"); printf("%8s ", "Storage"); printf("%8s\n", "Aux"); printf("-------- "); printf("---------------- "); printf("---------------- "); printf("---------------- "); - printf("---------------- "); + printf("--------------- "); printf("-------- "); printf("--------\n"); diff --git a/cTools/libs/binPrinter/pePrinter/test/CMakeLists.txt b/cTools/libs/binPrinter/pePrinter/test/CMakeLists.txt index 8b4040f..65f815b 100644 --- a/cTools/libs/binPrinter/pePrinter/test/CMakeLists.txt +++ b/cTools/libs/binPrinter/pePrinter/test/CMakeLists.txt @@ -20,13 +20,20 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +set(PE64_PRINTE_EXE_TEST_LINK_LIBS pe64Parse pe64Printer delayLib file mem string comdef) # Need for testing print of delay load libs -if (MSVC) - set(TEST_LINK_FLAGS /DELAYLOAD:delayLib.dll) -else () - set(TEST_LINK_FLAGS -Wl,-delayload:delayLib.dll) -endif () +if (WIN32) + # Delay load doesn't work without delayimp.lib + list(APPEND PE64_PRINTE_EXE_TEST_LINK_LIBS delayimp.lib) + if (MSVC) + set(TEST_LINK_FLAGS /DELAYLOAD:delayLib.dll) + else () + set(TEST_LINK_FLAGS -Wl,-delayload:delayLib.dll) + endif () +endif (WIN32) +# Actually delayLib isn't needed to build, because we have an ref exe file. +# Save it for future. add_vipon_library( NAME delayLib TYPE SHARED @@ -36,27 +43,31 @@ add_vipon_library( add_vipon_test( NAME pe64PrintExeTest SOURCES pe64PrintExeTest.c - # Delay load doesn't work without delayimp.lib - LINK_LIBS pe64Parse pe64Printer delayLib file mem string comdef delayimp.lib + LINK_LIBS ${PE64_PRINTE_EXE_TEST_LINK_LIBS} LINK_FLAGS ${TEST_LINK_FLAGS} + CMD_LINE pe64PrintExeTest_ref.exe pe64PrintExeTest_ref.txt + TEST_FILES pe64PrintExeTest_ref.exe pe64PrintExeTest_ref.txt ) -add_custom_target(testObjFile - COMMAND ${CMAKE_C_COMPILER} -c -g3 -ggdb -Wno-unknown-argument "${CMAKE_CURRENT_LIST_DIR}/testObjFile.c" - DEPENDS testObjFile.c -) +# Actually don't need to rebuild +# Save it for future. +#add_custom_target(testObjFile +# COMMAND ${CMAKE_C_COMPILER} -c -g3 -ggdb -Wno-unknown-argument "${CMAKE_CURRENT_LIST_DIR}/testObjFile.c" +# DEPENDS testObjFile.c +#) -if (MSVC) - set(TEST_FILE "${CMAKE_CURRENT_BINARY_DIR}/testObjFile.obj") -else () +#if (MSVC) +# set(TEST_FILE "${CMAKE_CURRENT_BINARY_DIR}/testObjFile.obj") +#else () set(TEST_FILE "${CMAKE_CURRENT_BINARY_DIR}/testObjFile.o") -endif () +#endif () add_vipon_test( NAME pe64PrintObjTest SOURCES pe64PrintObjTest.c LINK_LIBS pe64Parse pe64Printer file mem string comdef - CMD_LINE "${TEST_FILE}" - DEPENDS testObjFile + CMD_LINE "${TEST_FILE}" pe64PrintObjTest_ref.txt + #DEPENDS testObjFile + TEST_FILES pe64PrintObjTest_ref.txt testObjFile.o ) diff --git a/cTools/libs/binPrinter/pePrinter/test/pe64PrintExeTest.c b/cTools/libs/binPrinter/pePrinter/test/pe64PrintExeTest.c index 03ebb6a..5439660 100644 --- a/cTools/libs/binPrinter/pePrinter/test/pe64PrintExeTest.c +++ b/cTools/libs/binPrinter/pePrinter/test/pe64PrintExeTest.c @@ -1,7 +1,7 @@ /*** * MIT License * - * Copyright (c) 2021 Konychev Valerii + * Copyright (c) 2021-2023 Konychev Valerii * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,22 +22,40 @@ * SOFTWARE. */ +#include +#include #include "comdef.h" -#include "delayLib.h" // for test delay import +#ifdef __WIN__ +# include "delayLib.h" // for test delay import +#endif /* __WIN__ */ #include "pe64Parse.h" #include "pe64Printer.h" +#ifdef __WIN__ void dummy(void) __attribute__ ((section (".MY_SECTION123"))); void dummy(void) { } +#endif /* __WIN__ */ + +#include +static const char TESTOUT[] = "pe64PrintExeTest.txt"; int main(int argc, char *argv[]) { UNUSED(argc); +#ifdef __WIN__ delayLib(); // for test delay import - PE64File *pe = pe64Parse(argv[0]); +#endif /* __WIN__ */ + + PE64File *pe = pe64Parse(argv[1]); + if (pe == NULL) { + VT_ERROR("Cannot parse %s", argv[1]); + exit(EXIT_FAILURE); + } + + FILE *f = freopen(TESTOUT, "w", stdout); pe64PrintDosHeader(pe); pe64PrintNtHeader(pe); @@ -47,6 +65,10 @@ int main(int argc, char *argv[]) pe64PrintDelayImports(pe); pe64Free(pe); + + fclose(f); + EXPECT_FILE_EQ(TESTOUT, argv[2]); + return 0; } diff --git a/cTools/libs/binPrinter/pePrinter/test/pe64PrintExeTest_ref.exe b/cTools/libs/binPrinter/pePrinter/test/pe64PrintExeTest_ref.exe new file mode 100644 index 0000000..4a10532 Binary files /dev/null and b/cTools/libs/binPrinter/pePrinter/test/pe64PrintExeTest_ref.exe differ diff --git a/cTools/libs/binPrinter/pePrinter/test/pe64PrintExeTest_ref.txt b/cTools/libs/binPrinter/pePrinter/test/pe64PrintExeTest_ref.txt new file mode 100644 index 0000000..df26640 --- /dev/null +++ b/cTools/libs/binPrinter/pePrinter/test/pe64PrintExeTest_ref.txt @@ -0,0 +1,266 @@ +DOS Header: + Magic: MZ + NT Header Offset: 00000078 + +NT Header: + Magic: PE +File Header: + Machine: AMD64 + Number of Sections: 08 + Time stamp: Mon Jul 3 15:09:09 2023 + SymTab Pointer: 00000000 + Number of Symbols: 00000000 + Size of Opt Header: 00f0 + Characteristics: 0022 + LADDR EXE +Optional Header: + Application Type: 64-bit + Linker Version: 14.00 + Code Size: 00003000 + Init Data Size: 00002200 + Uninit Data Size: 00000000 + Entry Point: 00001560 + Code Base: 00001000 + Image Base: 0000000140000000 + Section Alignment: 00001000 + File Alignment: 00000200 + OS Version: 06.00 + Image Version: 00.00 + Subsystem Version: 06.00 + Win32 Version Val: 00000000 + Image Size: 0000c000 + Headers Size: 00000400 + CheckSum: 00000000 + Subsystem: 0003: WINDOWS_CUI + DllCharacteristics: 8160 + HIGH_ENTROPY_VA DYNAMIC_BASE NX_COMPAT TERMINAL_SERVER_AWARE + Stack Size Reserve: 0000000000100000 + Stack Size Commit: 0000000000001000 + Heap Size Reserve: 0000000000100000 + Heap Size Commit: 0000000000001000 + Loader Flags: 00000000 + NumberOfRvaAndSizes: 00000010 + Data Directory: Type Addr Size + --------------- -------- -------- + EXPORT: 00000000 00000000 + IMPORT: 00004561 000000a0 + RESOURCE: 0000a000 000001a8 + EXCEPTION: 00007000 00000420 + SECURITY: 00000000 00000000 + BASERELOC: 0000b000 00000044 + DEBUG: 00004400 0000001c + ARCHITECTURE: 00000000 00000000 + GLOBALPTR: 00000000 00000000 + TLS: 00000000 00000000 + LOAD_CONFIG: 00004140 00000140 + BOUND_IMPORT: 00000000 00000000 + IAT: 00004868 00000260 + DELAY_IMPORT: 000044f8 00000040 + COM_DESCRIPTOR: 00000000 00000000 + RESERVED: 00000000 00000000 + +01 .text + VirtSize: 00002ff5 VirtAddr: 00001000 +raw data offs: 00000400 raw data size: 00003000 + reloc offs: 00000000 relocations: 00000000 + line # offs: 00000000 line #'s: 00000000 + flags: 60000020 + +02 .rdata + VirtSize: 000013f4 VirtAddr: 00004000 +raw data offs: 00003400 raw data size: 00001400 + reloc offs: 00000000 relocations: 00000000 + line # offs: 00000000 line #'s: 00000000 + flags: 40000040 + +03 .data + VirtSize: 00000690 VirtAddr: 00006000 +raw data offs: 00004800 raw data size: 00000200 + reloc offs: 00000000 relocations: 00000000 + line # offs: 00000000 line #'s: 00000000 + flags: c0000040 + +04 .pdata + VirtSize: 00000420 VirtAddr: 00007000 +raw data offs: 00004a00 raw data size: 00000600 + reloc offs: 00000000 relocations: 00000000 + line # offs: 00000000 line #'s: 00000000 + flags: 40000040 + +05 .00cfg + VirtSize: 00000038 VirtAddr: 00008000 +raw data offs: 00005000 raw data size: 00000200 + reloc offs: 00000000 relocations: 00000000 + line # offs: 00000000 line #'s: 00000000 + flags: 40000040 + +06 .MY_SECT + VirtSize: 00000006 VirtAddr: 00009000 +raw data offs: 00005200 raw data size: 00000200 + reloc offs: 00000000 relocations: 00000000 + line # offs: 00000000 line #'s: 00000000 + flags: 60000020 + +07 .rsrc + VirtSize: 000001a8 VirtAddr: 0000a000 +raw data offs: 00005400 raw data size: 00000200 + reloc offs: 00000000 relocations: 00000000 + line # offs: 00000000 line #'s: 00000000 + flags: 40000040 + +08 .reloc + VirtSize: 00000044 VirtAddr: 0000b000 +raw data offs: 00005600 raw data size: 00000200 + reloc offs: 00000000 relocations: 00000000 + line # offs: 00000000 line #'s: 00000000 + flags: 42000040 + +Imports Table: +-------------------------------- +pe64Printer.dll + addr table: 4868 + name table: 4608 + time stamp: 0 +forward index: 0 + Indx Name + ---- -------- + 0000 pe64PrintDelayImports + 0000 pe64PrintDosHeader + 0000 pe64PrintImports + 0000 pe64PrintNtHeader + 0000 pe64PrintSections + 0000 pe64PrintSymbols + + +pe64Parse.dll + addr table: 48a0 + name table: 4640 + time stamp: 0 +forward index: 0 + Indx Name + ---- -------- + 0000 pe64Free + 0000 pe64Parse + + +file.dll + addr table: 48b8 + name table: 4658 + time stamp: 0 +forward index: 0 + Indx Name + ---- -------- + 0000 cmpFiles + + +KERNEL32.dll + addr table: 48c8 + name table: 4668 + time stamp: 0 +forward index: 0 + Indx Name + ---- -------- + 01b4 FreeLibrary + 0220 GetCurrentProcess + 0221 GetCurrentProcessId + 0225 GetCurrentThreadId + 026a GetLastError + 0281 GetModuleHandleW + 02b8 GetProcAddress + 02da GetStartupInfoW + 02ed GetSystemInfo + 02f3 GetSystemTimeAsFileTime + 036f InitializeSListHead + 0385 IsDebuggerPresent + 038c IsProcessorFeaturePresent + 03c9 LoadLibraryExA + 0452 QueryPerformanceCounter + 0468 RaiseException + 04d5 RtlCaptureContext + 04dc RtlLookupFunctionEntry + 04e3 RtlVirtualUnwind + 057f SetUnhandledExceptionFilter + 059e TerminateProcess + 05c0 UnhandledExceptionFilter + 05df VirtualProtect + 05e1 VirtualQuery + + +VCRUNTIME140D.dll + addr table: 4990 + name table: 4730 + time stamp: 0 +forward index: 0 + Indx Name + ---- -------- + 0008 __C_specific_handler + 001b __current_exception + 001c __current_exception_context + 0025 __std_type_info_destroy_list + + +ucrtbased.dll + addr table: 49b8 + name table: 4758 + time stamp: 0 +forward index: 0 + Indx Name + ---- -------- + 0035 __acrt_iob_func + 0049 __p___argc + 004a __p___argv + 004d __p__commode + 005b __setusermatherr + 005c __stdio_common_vfprintf + 009f _c_exit + 00a4 _cexit + 00b5 _configthreadlocale + 00b6 _configure_narrow_argv + 00c1 _crt_at_quick_exit + 00c2 _crt_atexit + 00e5 _execute_onexit_table + 00ea _exit + 013d _get_initial_narrow_environment + 0171 _initialize_narrow_environment + 0172 _initialize_onexit_table + 0174 _initterm + 0175 _initterm_e + 02b5 _register_onexit_function + 02b6 _register_thread_local_exe_atexit_callback + 02c2 _seh_filter_dll + 02c3 _seh_filter_exe + 02c6 _set_app_type + 02cb _set_fmode + 02ce _set_new_mode + 0450 exit + 045a fclose + 047e fputc + 0485 freopen + 054b terminate + + +string.dll + addr table: 4ab8 + name table: 4858 + time stamp: 0 +forward index: 0 + Indx Name + ---- -------- + 0000 memcpy + + +Delay Imports Table: +-------------------------------- +delayLib.dll + type: New + handle: 4554 + IAT: 6040 + INT: 4538 + BoundIAT: 0 + UnloadIAT: 0 + TimeStamp: 0 + Indx Name + ---- -------- + 0000 delayLib + + diff --git a/cTools/libs/binPrinter/pePrinter/test/pe64PrintObjTest.c b/cTools/libs/binPrinter/pePrinter/test/pe64PrintObjTest.c index bc42ac4..073beb7 100644 --- a/cTools/libs/binPrinter/pePrinter/test/pe64PrintObjTest.c +++ b/cTools/libs/binPrinter/pePrinter/test/pe64PrintObjTest.c @@ -1,7 +1,7 @@ /*** * MIT License * - * Copyright (c) 2021 Konychev Valerii + * Copyright (c) 2021-2023 Konychev Valerii * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,6 +26,8 @@ #include "pe64Parse.h" #include "pe64Printer.h" +static const char TESTOUT[] = "pe64PrintObjTest.txt"; + int main(int argc, char *argv[]) { if (argc < 2) { @@ -36,11 +38,18 @@ int main(int argc, char *argv[]) PE64File *pe = pe64Parse(argv[1]); EXPECT_VAL_NOT_EQ(pe, NULL, "Cannot parse pe64 file"); EXPECT_VAL_NOT_EQ(pe->symtab, NULL, "Cannot parse pe64 symbol table"); + + FILE *f = freopen(TESTOUT, "w", stdout); + pe64PrintFileHeader(pe); pe64PrintSections(pe); pe64PrintSymbols(pe); pe64Free(pe); + + fclose(f); + EXPECT_FILE_EQ(TESTOUT, argv[2]); + return 0; } diff --git a/cTools/libs/binPrinter/pePrinter/test/pe64PrintObjTest_ref.txt b/cTools/libs/binPrinter/pePrinter/test/pe64PrintObjTest_ref.txt new file mode 100644 index 0000000..7d24175 --- /dev/null +++ b/cTools/libs/binPrinter/pePrinter/test/pe64PrintObjTest_ref.txt @@ -0,0 +1,91 @@ +File Header: + Machine: AMD64 + Number of Sections: 08 + Time stamp: Mon Jul 3 16:59:29 2023 + SymTab Pointer: 00000ea3 + Number of Symbols: 00000018 + Size of Opt Header: 0000 + Characteristics: 0000 + +01 .text + PhysAddr: 00000000 VirtAddr: 00000000 +raw data offs: 00000154 raw data size: 0000002d + reloc offs: 00000000 relocations: 00000000 + line # offs: 00000000 line #'s: 00000000 + flags: 60500020 + +02 .data + PhysAddr: 00000000 VirtAddr: 00000000 +raw data offs: 00000181 raw data size: 00000000 + reloc offs: 00000000 relocations: 00000000 + line # offs: 00000000 line #'s: 00000000 + flags: c0300040 + +03 .bss + PhysAddr: 00000000 VirtAddr: 00000000 +raw data offs: 00000000 raw data size: 00000000 + reloc offs: 00000000 relocations: 00000000 + line # offs: 00000000 line #'s: 00000000 + flags: c0300080 + +04 .xdata + PhysAddr: 00000000 VirtAddr: 00000000 +raw data offs: 00000181 raw data size: 00000008 + reloc offs: 00000000 relocations: 00000000 + line # offs: 00000000 line #'s: 00000000 + flags: 40300040 + +05 .debug$S + PhysAddr: 00000000 VirtAddr: 00000000 +raw data offs: 00000189 raw data size: 00000280 + reloc offs: 00000409 relocations: 0000000c + line # offs: 00000000 line #'s: 00000000 + flags: 42300040 + +06 .debug$T + PhysAddr: 00000000 VirtAddr: 00000000 +raw data offs: 00000481 raw data size: 000009f8 + reloc offs: 00000000 relocations: 00000000 + line # offs: 00000000 line #'s: 00000000 + flags: 42300040 + +07 .pdata + PhysAddr: 00000000 VirtAddr: 00000000 +raw data offs: 00000e79 raw data size: 0000000c + reloc offs: 00000e85 relocations: 00000003 + line # offs: 00000000 line #'s: 00000000 + flags: 40300040 + +08 .llvm_addrsig + PhysAddr: 00000000 VirtAddr: 00000000 +raw data offs: 00000ea3 raw data size: 00000000 + reloc offs: 00000000 relocations: 00000000 + line # offs: 00000000 line #'s: 00000000 + flags: 00100800 + + Indx Name Value Section Type Storage Aux +-------- ---------------- ---------------- ---------------- --------------- -------- -------- +00000000 .text 0000000000000000 .text DT_NON T_NULL C_STAT 1 + Len: 0000002d Relocs: 0000 Lines: 0000 CheckSum: 90a14ad6 AssocNum: 0001 Selec: 00 +00000002 .data 0000000000000000 .data DT_NON T_NULL C_STAT 1 + Len: 00000000 Relocs: 0000 Lines: 0000 CheckSum: 00000000 AssocNum: 0002 Selec: 00 +00000004 .bss 0000000000000000 .bss DT_NON T_NULL C_STAT 1 + Len: 00000000 Relocs: 0000 Lines: 0000 CheckSum: 00000000 AssocNum: 0003 Selec: 00 +00000006 .xdata 0000000000000000 .xdata DT_NON T_NULL C_STAT 1 + Len: 00000008 Relocs: 0000 Lines: 0000 CheckSum: 1ab96b84 AssocNum: 0004 Selec: 00 +00000008 .debug$S 0000000000000000 .debug$S DT_NON T_NULL C_STAT 1 + Len: 00000280 Relocs: 000c Lines: 0000 CheckSum: 5fc89130 AssocNum: 0005 Selec: 00 +00000010 .debug$T 0000000000000000 .debug$T DT_NON T_NULL C_STAT 1 + Len: 000009f8 Relocs: 0000 Lines: 0000 CheckSum: cb811cfb AssocNum: 0006 Selec: 00 +00000012 .pdata 0000000000000000 .pdata DT_NON T_NULL C_STAT 1 + Len: 0000000c Relocs: 0003 Lines: 0000 CheckSum: 7a42ac7f AssocNum: 0007 Selec: 00 +00000014 .llvm_addrsig 0000000000000000 .llvm_addrsig DT_NON T_NULL C_STAT 1 + Len: 00000000 Relocs: 0000 Lines: 0000 CheckSum: 00000000 AssocNum: 0008 Selec: 00 +00000016 @feat.00 0000000000000000 N_ABS DT_NON T_NULL C_STAT 0 +00000017 shortNam 0000000000000000 .text DT_NON T_NULL C_EXT 0 +00000018 label 0000000000000000 .text DT_NON T_NULL C_STAT 0 +00000019 longNameTest 0000000000000010 .text DT_NON T_NULL C_EXT 0 +00000020 long_test_label 0000000000000010 .text DT_NON T_NULL C_STAT 0 +00000021 main 0000000000000020 .text DT_NON T_NULL C_EXT 0 +00000022 .file 0000000000000000 N_DEBUG DT_NON T_NULL C_FILE 1 + testObjFile.c diff --git a/cTools/libs/binPrinter/pePrinter/test/testObjFile.o b/cTools/libs/binPrinter/pePrinter/test/testObjFile.o new file mode 100644 index 0000000..06c942a Binary files /dev/null and b/cTools/libs/binPrinter/pePrinter/test/testObjFile.o differ diff --git a/cTools/libs/comdef/comdef.h b/cTools/libs/comdef/comdef.h index 30a624a..bd5d078 100644 --- a/cTools/libs/comdef/comdef.h +++ b/cTools/libs/comdef/comdef.h @@ -68,6 +68,11 @@ extern int VERBOSE; putchar(' '); \ ) + #define SPACEs(num) \ + DEF_GUARD( \ + printf("%*s", num, " "); \ + ) + #define TAB \ DEF_GUARD( \ putchar('\t'); \