Skip to content

Commit

Permalink
Merge pull request #36 from Vipon/issue_35
Browse files Browse the repository at this point in the history
Update binParser tool.
  • Loading branch information
Vipon authored Jul 6, 2023
2 parents 4ea8860 + a285c81 commit 789b3d5
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@
"fatmacho64dynmod.h": "c",
"binparse.h": "c",
"elf32dynmod.h": "c",
"arch.h": "c"
"arch.h": "c",
"pe64parse.h": "c"
},
"C_Cpp.errorSquiggles": "enabled",

Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ git clone -c core.symlinks=true https://github.com/Vipon/viponTools
## Setup environment
### Windows
Scripts bellow will automatically download and install at least:
* python3, ninja, vscode, cmake, ccache, visual studio
```
cd batch
setup.bat
```

Expand Down
26 changes: 18 additions & 8 deletions cTools/libs/binPrinter/binPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "binParse.h"
#include "binPrinter.h"
#include "pe64Printer.h"
#include "macho64Printer.h"
#include "fatMacho64Printer.h"

Expand All @@ -35,13 +36,21 @@ BinPrinter binPrinter = {};
binPrinter.printSections = (BinPrintSections)&(type ## PrintSections); \
binPrinter.printSegments = (BinPrintSegments)&(type ## PrintSegments);

#define INIT_MACHO_PRINT_FUN(type) \
#define INIT_MACHO_PRINT_FUNC(type) \
binPrinter.macho.printFuncStarts = (BinPrintFuncStarts)&(type ## PrintFuncStarts); \
binPrinter.macho.printLComs = (BinPrintLComs)&(type ## PrintLComs);

#define INIT_FAT_MACHO_PRINT_FUN(type) \
#define INIT_FAT_MACHO_PRINT_FUNC(type) \
binPrinter.fatMacho.printFatHeader = (BinPrintFatHeader)&(type ## PrintHeader);

#define INIT_PE_PRINT_FUNC(type) \
binPrinter.pe.printDosHeader = (BinPrintDosHeader)&(type ## PrintDosHeader); \
binPrinter.pe.printFileHeader = (BinPrintFileHeader)&(type ## PrintFileHeader); \
binPrinter.pe.printOptHeader = (BinPrintOptHeader)&(type ## PrintOptHeader); \
binPrinter.pe.printImports = (BinPrintImports)&(type ## PrintImports); \
binPrinter.pe.printDelayImports = (BinPrintOptHeader)&(type ## PrintDelayImports); \
binPrinter.pe.printExports = (BinPrintExports)&(type ## PrintExports);

int initBinPrinter(const char *fn)
{
if (initBinParser(fn)) {
Expand All @@ -51,20 +60,21 @@ int initBinPrinter(const char *fn)
switch(binParser.type) {
case MACHO64:
INIT_BIN_PRINTER(macho64);
INIT_MACHO_PRINT_FUN(macho64);
INIT_MACHO_PRINT_FUNC(macho64);
break;
case FATMACHO64:
INIT_BIN_PRINTER(fatMacho64);
INIT_MACHO_PRINT_FUN(fatMacho64);
INIT_FAT_MACHO_PRINT_FUN(fatMacho64);
INIT_MACHO_PRINT_FUNC(fatMacho64);
INIT_FAT_MACHO_PRINT_FUNC(fatMacho64);
break;
case PE64:
INIT_BIN_PRINTER(pe64);
INIT_PE_PRINT_FUNC(pe64);
break;
/*
case ELF64:
INIT_BIN_PRINTER(elf64);
break;
case PE64:
INIT_BIN_PRINTER(pe64);
break;
case ELF32:
INIT_BIN_PRINTER(elf32);
break;
Expand Down
14 changes: 14 additions & 0 deletions cTools/libs/binPrinter/binPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ typedef void (*BinPrintSegments)(const BinFilePtr bin);
typedef void (*BinPrintFuncStarts)(const BinFilePtr bin);
typedef void (*BinPrintLComs)(const BinFilePtr bin);
typedef void (*BinPrintFatHeader)(const BinFilePtr bin);
typedef void (*BinPrintDosHeader)(const BinFilePtr bin);
typedef void (*BinPrintFileHeader)(const BinFilePtr bin);
typedef void (*BinPrintOptHeader)(const BinFilePtr bin);
typedef void (*BinPrintImports)(const BinFilePtr bin);
typedef void (*BinPrintDelayImports)(const BinFilePtr bin);
typedef void (*BinPrintExports)(const BinFilePtr bin);

typedef struct {
BinPrintHeader printHeader;
Expand All @@ -46,6 +52,14 @@ typedef struct {
struct {
BinPrintFatHeader printFatHeader;
} fatMacho;
struct {
BinPrintDosHeader printDosHeader;
BinPrintFileHeader printFileHeader;
BinPrintOptHeader printOptHeader;
BinPrintImports printImports;
BinPrintDelayImports printDelayImports;
BinPrintExports printExports;
} pe;
} BinPrinter;

#ifdef BIN_PRINTER_SHARED_LIB
Expand Down
1 change: 1 addition & 0 deletions cTools/libs/binPrinter/pePrinter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(PE64_PRINTER_SOURSES
pe64PrinterSymbols.c
pe64PrinterImports.c
pe64PrinterDelayImports.c
pe64PrinterExports.c
)

set(PE64_PRINTER_HEADERS
Expand Down
18 changes: 16 additions & 2 deletions cTools/libs/binPrinter/pePrinter/pe64Printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#ifndef __PE64_PRINTER_H
#define __PE64_PRINTER_H

#include "comdef.h"
#include "pe64Parse.h"

EXPORT_FUNC
Expand All @@ -38,6 +39,11 @@ void pe64PrintOptHeader(const PE64File *pe);

EXPORT_FUNC
void pe64PrintNtHeader(const PE64File *pe);
static INLINE
void pe64PrintHeader(const PE64File *pe)
{
pe64PrintNtHeader(pe);
}

EXPORT_FUNC
void pe64PrintDataDir(const DataDir *dataDir);
Expand All @@ -50,22 +56,30 @@ void pe64PrintSection(const PE64File *pe, const PESection *sect);

EXPORT_FUNC
void pe64PrintSections(const PE64File *pe);
static INLINE
void pe64PrintSegments(const PE64File *pe)
{
pe64PrintSections(pe);
}

/***
* Import Name Table
*/
EXPORT_FUNC
void pe64PrintINT(const PE64File *pe, ThunkData64 *INT);

EXPORT_FUNC
void pe64PrintImport(const PE64File *pe, const PEImport* import);

EXPORT_FUNC
void pe64PrintImports(const PE64File *pe);

EXPORT_FUNC
void pe64PrintDelayImport(const PE64File *pe, const PEDelimp *delimp);

EXPORT_FUNC
void pe64PrintDelayImports(const PE64File *pe);

EXPORT_FUNC
void pe64PrintExport(const PE64File *pe, const PEExport *exp);
EXPORT_FUNC
void pe64PrintExports(const PE64File *pe);

Expand Down
3 changes: 2 additions & 1 deletion cTools/libs/binPrinter/pePrinter/pe64PrinterHeaders.c
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,8 @@ void pe64PrintNtHeader(const PE64File *pe)
pe64PrintNTMagic(pe->ntHeader);
NEW_LINE;
pe64PrintFileHeader(pe);
pe64PrintOptHeader(pe);
if (pe->type != PE64_OBJ)
pe64PrintOptHeader(pe);

NEW_LINE;
}
Expand Down
2 changes: 0 additions & 2 deletions cTools/libs/binPrinter/pePrinter/pe64PrinterImports.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ void pe64PrintImport(const PE64File *pe, const PEImport *import)
uint64_t AddressOfData = INT->u1.AddressOfData;

if (AddressOfData) {


pe64PrintINT(pe, INT);
NEW_LINE;
} else {
Expand Down
27 changes: 16 additions & 11 deletions cTools/tools/binParser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ The followed table shows support platforms and binary formats. macho files forma
| | MacOsX | Win | Linux |
|-------|:------:|:---:|:-----:|
|macho64| X | X | X |
|elf64 | X | X | X |
|elf64 | - | - | - |
|PE64 | X | X | X |

## Command Line Arguments
| Short Arg | Long Arg | Description |
|----------:|:--------------|:------------|
| -h | --header | print all headers |
| -s | --symbols | print all symbols |
| -S | --sections | print all section |
| | --segments | print all segments |
| | --func-starts | macho: print info about function starts |
| -l | --lcom | macho: print load commands |
| | --fat-header | macho: print fat header information if it's |
| -m | --mcpu | set up cpu type for parser, used for fat binaries |
| Short Arg | Long Arg | Description |
|----------:|:----------------|:------------|
| -h | --header | print all headers |
| -s | --symbols | print all symbols |
| -S | --sections | print all section |
| | --segments | print all segments |
| | --func-starts | macho: print info about function starts |
| -l | --lcom | macho: print load commands |
| | --fat-header | macho: print fat header information if it's |
| -m | --mcpu | set up cpu type for parser, used for fat binaries |
| | --dos-header | pe: print dos header |
| -d | --delay-imports | pe: print delay imports |
| -e | --exports | pe: print exports |
| | --file-header | pe: print file header |
| | --opt-header | pe: print opt header |

## Examples
### Print aarch64 symbols of fat macho64
Expand Down
96 changes: 96 additions & 0 deletions cTools/tools/binParser/binParser.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ typedef enum {
FAT_HEADER,
FUNC_STARTS,
LCOMS,
DOS_HEADER,
FILE_HEADER,
OPT_HEADER,
IMPORTS,
DELAY_IMPORTS,
EXPORTS,
NUM_FLAGS
} BinParserOpt;

Expand Down Expand Up @@ -100,6 +106,48 @@ void printLComs(const char *arg)
flags[LCOMS] = true;
}

static
void printDosHeader(const char *arg)
{
UNUSED(arg);
flags[DOS_HEADER] = true;
}

static
void printFileHeader(const char *arg)
{
UNUSED(arg);
flags[FILE_HEADER] = true;
}

static
void printOptHeader(const char *arg)
{
UNUSED(arg);
flags[OPT_HEADER] = true;
}

static
void printImports(const char *arg)
{
UNUSED(arg);
flags[IMPORTS] = true;
}

static
void printDelayImports(const char *arg)
{
UNUSED(arg);
flags[DELAY_IMPORTS] = true;
}

static
void printExports(const char *arg)
{
UNUSED(arg);
flags[EXPORTS] = true;
}

static
Arch getArchByName(const char* arch)
{
Expand Down Expand Up @@ -180,6 +228,36 @@ int main(int argc, char *argv[])
, .flags = OPTION_ARG_OPTIONAL
, .doc = "set up cpu type for parser"
);
ADD_ARG(printDosHeader, .name = "dos-header"
, .key = 153
, .flags = OPTION_ARG_OPTIONAL
, .doc = "pe: print dos header"
);
ADD_ARG(printFileHeader, .name = "file-header"
, .key = 154
, .flags = OPTION_ARG_OPTIONAL
, .doc = "pe: print file header"
);
ADD_ARG(printOptHeader, .name = "opt-header"
, .key = 155
, .flags = OPTION_ARG_OPTIONAL
, .doc = "pe: print opt header"
);
ADD_ARG(printImports, .name = "imports"
, .key = 'i'
, .flags = OPTION_ARG_OPTIONAL
, .doc = "pe: print imports"
);
ADD_ARG(printDelayImports, .name = "delay-imports"
, .key = 'd'
, .flags = OPTION_ARG_OPTIONAL
, .doc = "pe: print delay imports"
);
ADD_ARG(printExports, .name = "exports"
, .key = 'e'
, .flags = OPTION_ARG_OPTIONAL
, .doc = "pe: print exports"
);

ARG_PARSE(argc, argv);
setupBinPrinterArch(binParserArch);
Expand All @@ -204,6 +282,24 @@ int main(int argc, char *argv[])
if (flags[LCOMS]) {
binPrinter.macho.printLComs(binParser.bin);
}
if (flags[DOS_HEADER]) {
binPrinter.pe.printDosHeader(binParser.bin);
}
if (flags[FILE_HEADER]) {
binPrinter.pe.printFileHeader(binParser.bin);
}
if (flags[OPT_HEADER]) {
binPrinter.pe.printOptHeader(binParser.bin);
}
if (flags[IMPORTS]) {
binPrinter.pe.printImports(binParser.bin);
}
if (flags[DELAY_IMPORTS]) {
binPrinter.pe.printDelayImports(binParser.bin);
}
if (flags[EXPORTS]) {
binPrinter.pe.printExports(binParser.bin);
}

finiBinPrinter();
}
Expand Down

0 comments on commit 789b3d5

Please sign in to comment.