From 51b09fd36d2c8b3b5d95c5848360a57f67bc0704 Mon Sep 17 00:00:00 2001 From: "Konychev, Valerii" Date: Mon, 3 Jul 2023 19:07:30 +0200 Subject: [PATCH 1/3] Enable pePrintTest on all platforms. issue #33 --- .../libs/binPrinter/pePrinter/CMakeLists.txt | 4 +- .../pePrinter/pe64PrinterDelayImports.c | 28 +- .../binPrinter/pePrinter/pe64PrinterHeaders.c | 124 ++++---- .../binPrinter/pePrinter/pe64PrinterImports.c | 26 +- .../pePrinter/pe64PrinterSections.c | 26 +- .../binPrinter/pePrinter/pe64PrinterSymbols.c | 30 +- .../binPrinter/pePrinter/test/CMakeLists.txt | 27 +- .../pePrinter/test/pe64PrintExeTest.c | 28 +- .../pePrinter/test/pe64PrintExeTest_ref.exe | Bin 0 -> 22528 bytes .../pePrinter/test/pe64PrintExeTest_ref.txt | 266 ++++++++++++++++++ .../pePrinter/test/pe64PrintObjTest.c | 11 +- .../pePrinter/test/pe64PrintObjTest_ref.txt | 91 ++++++ .../binPrinter/pePrinter/test/testObjFile.o | Bin 0 -> 4226 bytes cTools/libs/comdef/comdef.h | 5 + 14 files changed, 526 insertions(+), 140 deletions(-) create mode 100644 cTools/libs/binPrinter/pePrinter/test/pe64PrintExeTest_ref.exe create mode 100644 cTools/libs/binPrinter/pePrinter/test/pe64PrintExeTest_ref.txt create mode 100644 cTools/libs/binPrinter/pePrinter/test/pe64PrintObjTest_ref.txt create mode 100644 cTools/libs/binPrinter/pePrinter/test/testObjFile.o 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..4b42338 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 @@ -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..a161822 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 @@ -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) @@ -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; } @@ -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 "); @@ -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..b4cc228 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 @@ -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..0014e2a 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: %.8lx ", "PhysAddr", sect->Misc.PhysicalAddress); break; case PE64_EXEC: case PE64_SHARED: default: - printf("VirtSize:\t%.8lx ", sect->Misc.VirtualSize); + printf("%13s: %.8lx ", "VirtSize", 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: %.8lx ", "VirtAddr", 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: %.8lx ", "raw data size", 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: %.8lx ", "raw data offs", 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: %.8lx ", "reloc offs", 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: %.8lx ", "line # offs",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: %.8lx ", "flags", 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..7c9e880 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 @@ -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: %.8lx", "Len", auxSym->Section.Length); + printf("%8s: %.4hx", "Relocs", auxSym->Section.NumberOfRelocations); + printf("%8s: %.4hx", "Lines", auxSym->Section.NumberOfLinenumbers); + printf("%10s: %.8lx", "CheckSum", 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..5a228f2 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,9 +43,10 @@ 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 @@ -56,7 +64,8 @@ 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 0000000000000000000000000000000000000000..4a10532a7e40f8e43f8c884762cf870b325ee971 GIT binary patch literal 22528 zcmeHv4|r77weOzHoMZ?I6M;xjgaZyW2*N<14TR_nWbh15Fou9sgZX2ocr%Gl!FPZ^Y1R{y*kcop z&+fMckI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l)>|)phcmd#Cq{o2Q1O5ZjlfV;zZz3H9 zF3w}@Ye>g{cL9Fsa>iZ+?gd9B%YJv9wzJ}Bc{3Ia#7O@?;7jQ3<0(>9fvZb&y@d0I|JAj7)TbIFxgabZ| z^eFHI;Hyaez)u41`Z8E+u?(e^U)BgJXt^33KckJJ{|DOF1J&<@{ l=mE4>kNr4+w-2u_{a=r=K4)J+Us0df=OOMF&;O$q_+PiG@Dl(4 literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..06c942acb66cf16144c2069d5418c30faf1d66de GIT binary patch literal 4226 zcmc&1U5^w+u!rT2<4`#;5JOPb6BSH)cK7bQ2)Q#CK#dA=2`8wTZ2V?ZXHxf)F@x=$@i@)N)L=z2I)jhjAd!RWIz)pImy1KePy1J^X zgOA6^dpExbzI=p`$B1pN=3QOD_@mI5oDu@SDp`Ej4VY%bXmW7_-2$m!ahqATf$X;7 z1*Gc^GaLh|JMdd@1K!MA2ha4FT-ZX$Cm?tGR*QKEejxW)LATVS`*S<+D`NwO z!~p{ku?~F>`Z#fRndR_t$((_ICManH`ggN-&X1UERXaS*gp$?9j9Yh~w>;2dm%P&F zf%Ij+Y?-he;G3$|Qta9z#1u7<9w4P002 zyRLR8*Nqo42-q@^=PjnHm9n^s9?rZ|!cSGELR~#8vYbWLGdd51@}}xjQ%D-}3=;>7 zytBtWdw@POnPy7my`9`F}q)r)b|^c)k&&TkwhGz?hCWavokdD>8g-{ zTFq#jb^OK=cY4~LZp=;{L24cgO;z(e!g0zY)}<_p%t%N-JAk1r1Y zg;ZgffLT(f%-8cu8kAHtRk;j=Y*C~(+<++4Y3^$U#zcZ;R--VNxV==P&Dp$>tXbOP zn)*o&E5QwPb_QAKT=D|h;u4HCddFzHnNB&=H1kO$x8&sPwY2Kf1fj2=P;5gq3nurd41rA^>P!YT=BI8hS zW;zd{Sfsi^t}7+Ga<=n|c4$$BU{<(bM;pQAJwqrBmG`PYE45i=Yz z7&ArCcqVF0*q|n!4IIz27cz=AX9EI_ifk>@93AnX#Csk@Sd<6mRNh}bPMyI$0jqnV z|NqKnI*LR8u63v$f>=82dFO08tG+ID23FNQ?^U~zIDks4ail)eZ~>kBsot!U@0xkG z!cuOM*k*ip*X(nX6YdLZ$XEAqU;HHL3pfXvDwl1kRwad;nB#4N4he0FdfD9R9@Sm zZ-Wl6BQg&C`g#=#G<;Z=TgAMB5K}R)j!^@ZpQBJVl8Vz?_v>qyvABd*eu`q%U9o60 zhc!C3@-q~x%wZZ^cb1=^SeaIwUhRLW{rx`kXaiY;=O@0$D$WHEfRD!d=hv1IgZda6 ze277Hi!}K7#P-+zi$5U-XiCq{Z7{Al$OHBG;HzWrBgVETi$7{Ith3c^A%r{Kt=Ez4 z)-B}FuayDgkSf>@*FBGr02*$MwHECGXB0F5n7ZF$kjB)@fUQEeeB2OfOBi9h39?$w b@T&sxHRwZCIS!VQZrQ5gVl}c{97pmGg=ZSR literal 0 HcmV?d00001 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'); \ From 59e3b2b72aa9fbd5593dad083ec58014cb86825f Mon Sep 17 00:00:00 2001 From: vipon Date: Mon, 3 Jul 2023 19:45:19 +0200 Subject: [PATCH 2/3] Fix DWORD size. Windows type DWORD should be 32 bits. --- cTools/include/Windows/WinDef.h | 6 ++++ cTools/include/Windows/WinNT.h | 6 ++++ .../pePrinter/pe64PrinterDelayImports.c | 10 +++--- .../binPrinter/pePrinter/pe64PrinterHeaders.c | 34 +++++++++---------- .../binPrinter/pePrinter/pe64PrinterImports.c | 8 ++--- .../pePrinter/pe64PrinterSections.c | 16 ++++----- .../binPrinter/pePrinter/pe64PrinterSymbols.c | 6 ++-- .../binPrinter/pePrinter/test/CMakeLists.txt | 18 +++++----- 8 files changed, 59 insertions(+), 45 deletions(-) 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/binPrinter/pePrinter/pe64PrinterDelayImports.c b/cTools/libs/binPrinter/pePrinter/pe64PrinterDelayImports.c index 4b42338..35cb7b3 100644 --- a/cTools/libs/binPrinter/pePrinter/pe64PrinterDelayImports.c +++ b/cTools/libs/binPrinter/pePrinter/pe64PrinterDelayImports.c @@ -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) diff --git a/cTools/libs/binPrinter/pePrinter/pe64PrinterHeaders.c b/cTools/libs/binPrinter/pePrinter/pe64PrinterHeaders.c index a161822..257860b 100644 --- a/cTools/libs/binPrinter/pePrinter/pe64PrinterHeaders.c +++ b/cTools/libs/binPrinter/pePrinter/pe64PrinterHeaders.c @@ -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) @@ -194,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) @@ -202,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) @@ -320,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) @@ -328,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) @@ -336,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) @@ -344,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) @@ -352,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) @@ -368,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) @@ -376,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) @@ -408,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) @@ -416,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) @@ -424,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) @@ -432,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) @@ -560,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) @@ -568,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) @@ -576,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) diff --git a/cTools/libs/binPrinter/pePrinter/pe64PrinterImports.c b/cTools/libs/binPrinter/pePrinter/pe64PrinterImports.c index b4cc228..5ee11ba 100644 --- a/cTools/libs/binPrinter/pePrinter/pe64PrinterImports.c +++ b/cTools/libs/binPrinter/pePrinter/pe64PrinterImports.c @@ -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) diff --git a/cTools/libs/binPrinter/pePrinter/pe64PrinterSections.c b/cTools/libs/binPrinter/pePrinter/pe64PrinterSections.c index 0014e2a..2d57ece 100644 --- a/cTools/libs/binPrinter/pePrinter/pe64PrinterSections.c +++ b/cTools/libs/binPrinter/pePrinter/pe64PrinterSections.c @@ -59,12 +59,12 @@ static void pe64PrintSectMisc(const PE64File *pe, const PESection *sect) switch (pe->type) { case PE64_OBJ: - printf("%13s: %.8lx ", "PhysAddr", sect->Misc.PhysicalAddress); + printf("%13s: %.8x ", "PhysAddr", (uint32_t)sect->Misc.PhysicalAddress); break; case PE64_EXEC: case PE64_SHARED: default: - printf("%13s: %.8lx ", "VirtSize", 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("%13s: %.8lx ", "VirtAddr", 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("%13s: %.8lx ", "raw data size", 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("%13s: %.8lx ", "raw data offs", 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("%13s: %.8lx ", "reloc offs", 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("%13s: %.8lx ", "line # offs",sect->PointerToLinenumbers); + printf("%13s: %.8x ", "line # offs", (uint32_t)sect->PointerToLinenumbers); } static void pe6464PrintNumberOfRelocations(const PESection *sect) @@ -130,7 +130,7 @@ static void pe6464PrintCharacteristics(const PESection *sect) if (sect == NULL) return; - printf("%13s: %.8lx ", "flags", 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 7c9e880..f32bd46 100644 --- a/cTools/libs/binPrinter/pePrinter/pe64PrinterSymbols.c +++ b/cTools/libs/binPrinter/pePrinter/pe64PrinterSymbols.c @@ -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) @@ -266,10 +266,10 @@ void pe64PrintAuxSymSect(const PE64File *pe, const PEAuxSymbol *auxSym) if (pe == NULL || auxSym == NULL) return; - printf("%8s: %.8lx", "Len", auxSym->Section.Length); + 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: %.8lx", "CheckSum", auxSym->Section.CheckSum); + printf("%10s: %.8x", "CheckSum", (uint32_t)auxSym->Section.CheckSum); printf("%10s: %.4hu", "AssocNum", auxSym->Section.Number); printf("%7s: %.2hhx", "Selec", auxSym->Section.Selection); } diff --git a/cTools/libs/binPrinter/pePrinter/test/CMakeLists.txt b/cTools/libs/binPrinter/pePrinter/test/CMakeLists.txt index 5a228f2..65f815b 100644 --- a/cTools/libs/binPrinter/pePrinter/test/CMakeLists.txt +++ b/cTools/libs/binPrinter/pePrinter/test/CMakeLists.txt @@ -49,16 +49,18 @@ add_vipon_test( 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 From 3cf5c473d4fca9613f3108a2a9daa3433c0a85d3 Mon Sep 17 00:00:00 2001 From: Vipon Date: Mon, 3 Jul 2023 21:26:03 +0300 Subject: [PATCH 3/3] Fix memory leaks. --- cTools/libs/binParse/peParse/pe64Parse.c | 26 +++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) 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)