Skip to content

Commit

Permalink
Merge pull request #34 from Vipon/pe_print_test_cross_platform
Browse files Browse the repository at this point in the history
Enable pePrintTest on all platforms.

issue #33
  • Loading branch information
Vipon authored Jul 3, 2023
2 parents cb3a53f + 3cf5c47 commit 4ea8860
Show file tree
Hide file tree
Showing 17 changed files with 594 additions and 182 deletions.
6 changes: 6 additions & 0 deletions cTools/include/Windows/WinDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#ifndef _WINDEF_
#define _WINDEF_

#include "os.h"

#ifndef WINVER
#define WINVER 0x0500
#endif
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions cTools/include/Windows/WinNT.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ typedef BYTE FCHAR;
typedef WORD FSHORT;
typedef DWORD FLONG;

// C standard headers
#include <assert.h>

static_assert(sizeof(WORD) == 2, "WORD 2 bytes");
static_assert(sizeof(DWORD) == 4, "WORD 4 bytes");

/*
* File formats definitions
*/
Expand Down
26 changes: 19 additions & 7 deletions cTools/libs/binParse/peParse/pe64Parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions cTools/libs/binPrinter/pePrinter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,5 @@ add_vipon_library(
INSTALL ON
)

if (WIN32)
add_subdirectory(test)
endif (WIN32)
add_subdirectory(test)

38 changes: 16 additions & 22 deletions cTools/libs/binPrinter/pePrinter/pe64PrinterDelayImports.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -65,39 +65,39 @@ 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)
{
if (delimp == NULL)
return;

printf("%lx", delimp->rvaIAT);
printf("%x", (uint32_t)delimp->rvaIAT);
}

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)
{
if (delimp == NULL)
return;

printf("%lx", delimp->rvaBoundIAT);
printf("%x", (uint32_t)delimp->rvaBoundIAT);
}

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)
Expand All @@ -119,44 +119,38 @@ 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(;;) {
ThunkData64 *INT = readFromFile(fd, (size_t*)&off, sizeof(ThunkData64));
uint64_t AddressOfData = INT->u1.AddressOfData;

if (AddressOfData) {
TAB;
TAB;
pe64PrintINT(pe, INT);
NEW_LINE;
} else {
Expand Down
Loading

0 comments on commit 4ea8860

Please sign in to comment.