Skip to content

Commit 023eafb

Browse files
committed
Add MSVC support
1 parent 0c2950e commit 023eafb

126 files changed

Lines changed: 5394 additions & 3573 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.clang-format

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ AlignAfterOpenBracket: Align
55
AlignArrayOfStructures: Left
66
AlignConsecutiveAssignments:
77
Enabled: true
8-
AcrossEmptyLines: true
8+
AcrossEmptyLines: false
99
AcrossComments: true
1010
AlignCompound: false
1111
PadOperators: true
@@ -17,7 +17,7 @@ AlignConsecutiveBitFields:
1717
PadOperators: false
1818
AlignConsecutiveDeclarations:
1919
Enabled: true
20-
AcrossEmptyLines: true
20+
AcrossEmptyLines: false
2121
AcrossComments: true
2222
AlignCompound: false
2323
PadOperators: false

CMakeLists.txt

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,83 @@
11
cmake_minimum_required(VERSION 3.20.0)
2-
project(readpe)
2+
3+
set(CMAKE_DISABLE_SOURCE_CHANGES ON)
4+
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
5+
6+
project(readpe LANGUAGES C VERSION 1.0.0)
37

48
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
9+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
510

611
option(BUILD_DISASSEMBLER "Build with basic disassembler support (deprecated)" OFF)
7-
option(BUILD_LEGACY "Build support for legacy executables by link name" ON)
8-
option(BUILD_STANDALONE "Build separate executables" OFF)
9-
option(BUILD_STATIC "Build static executable" Off)
1012
option(ENABLE_TESTS "Enable testing" On)
1113

14+
option(READPE_LEGACY "Build support for legacy executables by link name" ON)
15+
16+
# TODO:
17+
# option(READPE_STANDALONE "Build separate executables" OFF)
18+
19+
option(LIBPE_STATIC "Link libpe into readpe staticially" Off)
20+
21+
set(DEFAULT_LINK_SSDEEP OFF)
22+
if(LIBPE_STATIC)
23+
set(DEFAULT_LINK_SSDEEP ON)
24+
endif()
25+
26+
option(LIBPE_LINK_SSDEEP "LINK SSDEEP GPL2 code into libpe" ${DEFAULT_LINK_SSDEEP})
27+
28+
include(dependencies)
29+
30+
if(MSVC)
31+
# vcpkg on Linux has issues hijacking find_package for Windows packages
32+
# Maybe I can figure out how this works better at some point
33+
# It might come down to running vcpkg through wine also
34+
# Anyway this is a workaround for a niche case of a niche case
35+
if(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux")
36+
include(cmake/toolchains/msvc-wine.cmake)
37+
38+
# My wine installation of msvc does not like my CRT
39+
add_definitions(
40+
-D_CRT_SECURE_NO_WARNINGS
41+
)
42+
43+
set(OPENSSL_ROOT_DIR ${VCPKG_ROOT}/installed/x64-windows)
44+
endif()
45+
46+
add_definitions(
47+
-D_SLURP_EXECUTABLE
48+
)
49+
50+
# MSVC debug builds also require urctbased.dll and VCRUNTIME140D.dll
51+
# This does not affect RelWithDebInfo
52+
53+
# TODO: This might not work on windows
54+
install(PROGRAMS
55+
"${VCPKG_ROOT}/installed/x64-windows/bin/libcrypto-3-x64.dll"
56+
DESTINATION bin
57+
)
58+
endif()
59+
60+
if(CYGWIN)
61+
# TODO: These are probably not needed anymore
62+
## cp $(PEV_DIR)/userdb.txt $(ZIPDIR)
63+
## cp $(PEV_DIR)/windows/run.bat $(ZIPDIR)/
64+
install(PROGRAMS
65+
/usr/bin/cygwin1.dll
66+
/usr/bin/cygcrypto-3.dll
67+
/usr/bin/cygz.dll
68+
DESTINATION bin
69+
)
70+
endif()
71+
1272
if(BUILD_DISASSEMBLER)
13-
add_subdirectory(lib/libudis86/libudis86)
73+
add_subdirectory(lib/udis86/libudis86)
74+
endif()
75+
76+
if(LIBPE_LINK_SSDEEP)
77+
add_subdirectory(lib/fuzzy)
1478
endif()
1579

80+
add_subdirectory(lib/compat)
1681
add_subdirectory(lib/libpe)
1782
add_subdirectory(src/plugins)
1883
add_subdirectory(src)
@@ -21,28 +86,23 @@ if(ENABLE_TESTS)
2186
include("${CMAKE_SOURCE_DIR}/cmake/tests.cmake")
2287
endif()
2388

24-
set(
25-
CPACK_PACKAGE_NAME ${PROJECT_NAME}
26-
CACHE STRING "The resulting package name"
27-
)
28-
29-
30-
set(CPACK_STRIP_FILES YES)
31-
32-
89+
set(CPACK_PACKAGE_NAME ${PROJECT_NAME})
3390
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
3491
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
3592
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
93+
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE")
94+
set(CPACK_RESOURCE_FILE_README "${CMAKE_SOURCE_DIR}/README.md")
95+
set(CPACK_STRIP_FILES YES)
3696

37-
38-
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
39-
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
40-
97+
install(FILES
98+
"${CPACK_RESOURCE_FILE_LICENSE}"
99+
"${CPACK_RESOURCE_FILE_README}"
100+
TYPE DOC
101+
)
41102

42103
include(CPack)
43-
configure_file ("${PROJECT_SOURCE_DIR}/cmake/cpackopt.cmake.in"
44-
"${PROJECT_BINARY_DIR}/cpackopt.cmake"
45-
@ONLY)
46-
set (CPACK_PROJECT_CONFIG_FILE
47-
"${PROJECT_BINARY_DIR}/cpackopt.cmake")
104+
configure_file("${PROJECT_SOURCE_DIR}/cmake/cpackopt.cmake.in"
105+
"${PROJECT_BINARY_DIR}/cpackopt.cmake"
106+
@ONLY)
107+
set(CPACK_PROJECT_CONFIG_FILE "${PROJECT_BINARY_DIR}/cpackopt.cmake")
48108

Makefile

Lines changed: 0 additions & 27 deletions
This file was deleted.

README.md

Lines changed: 86 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,37 @@ and analyze PE (Portable Executables) binaries.
1010
## How to build on Linux
1111

1212
cd readpe
13-
make
13+
cmake -B build
14+
cmake --build build
1415

15-
**NOTE**: You may need to install OpenSSL using your package manager. Examples:
16+
**NOTE**: You may need to install CMake, and OpenSSL using your package manager. Examples:
1617

17-
apt install libssl-dev
18-
yum install openssl-devel
18+
apt install cmake libssl-dev
19+
yum install cmake3 openssl-devel
1920

2021
## How to install on Linux
2122

2223
cd readpe
23-
sudo make install
24+
sudo cmake --install build
2425
echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/libpe.conf
2526
sudo ldconfig
2627

2728
## How to build on macOS
2829

2930
cd readpe
30-
CFLAGS="-I/usr/local/opt/openssl/include/" LDFLAGS="-L/usr/local/opt/openssl/lib/" make
31+
CFLAGS="-I/usr/local/opt/openssl/include/" LDFLAGS="-L/usr/local/opt/openssl/lib/" cmake -B build
32+
cmake --build build
3133

32-
**NOTE**: You may need to install OpenSSL and PCRE via [Homebrew](https://brew.sh):
34+
**NOTE**: You may need to install CMake, OpenSSL and PCRE via [Homebrew](https://brew.sh):
3335

3436
brew update
35-
brew install openssl
37+
brew install cmake openssl
3638

37-
## How to build on Windows (via [Cygwin](https://cygwin.com))
39+
## How to build on Windows using [Cygwin](https://cygwin.com))
3840

3941
cd readpe
40-
make
41-
make zip
42+
cmake -B build
43+
cmake --build build
4244

4345
**NOTE**: The following packages must be installed along with your Cygwin:
4446

@@ -48,9 +50,15 @@ and analyze PE (Portable Executables) binaries.
4850
| Devel | binutils |
4951
| Devel | gcc-core |
5052
| Devel | git |
51-
| Devel | make |
53+
| Devel | cmake |
5254
| Net | libssl-devel |
5355

56+
## How to build on Windows using Visual Studio [EXPERIMENTAL]
57+
58+
Open CMakeLists.txt in Visual Studio.
59+
60+
This is still highly experimental and bug reports are highly appriciated.
61+
5462
## FAQ
5563

5664
### Is this pev? / What happened to pev?
@@ -84,4 +92,69 @@ This project is licensed under the GNU General Public License version 2
8492
with the exception of the contents of lib/libpe which are licensed under the
8593
GNU Lesser General Public License version 3.
8694

87-
A copy of these licenses can be found respectively in the project root and lib/libpe.
95+
A copy of these licenses can be found respectively
96+
in the project root and lib/libpe folders.
97+
98+
### Acknowledgments
99+
100+
Sources are given as acknowledgement for the original authors
101+
and do not represent an endorsement of this project by said authors.
102+
103+
#### lib/compat/asprintf.c
104+
105+
Code written by [Thomas Gamper](https://github.com/eiszapfen2000/asprintf)
106+
Licensed under [BSD-3-Clause License](https://opensource.org/license/bsd-3-clause)
107+
108+
#### lib/compat/strlcat.c
109+
110+
Code from the [OpenBSD Project](https://www.openbsd.org/)
111+
Licensed under [ISC License](https://opensource.org/license/isc)
112+
113+
#### lib/compat/getopt.c lib/compat/include/getopt.h
114+
115+
Code from the [GNU C Library](https://sourceware.org/glibc)
116+
Licensed under [LGPL-2.1+](https://opensource.org/license/lgpl-2-1)
117+
118+
#### lib/compat/include/unistd.h
119+
120+
Code from [win32ports/unistd_h](https://github.com/win32ports/unistd_h)
121+
Licensed under the [MIT License](https://opensource.org/license/mit)
122+
123+
#### lib/compat/include/sys/queue.h
124+
125+
Code from the [FreeBSD Project](https://www.freebsd.org/)
126+
Licensed under [BSD-3-Clause License](https://opensource.org/license/bsd-3-clause)
127+
128+
#### lib/fuzzy
129+
130+
Code from [SSDeep Project](https://ssdeep-project.github.io/ssdeep/index.html)
131+
Licensed under [GPL-2.0+](https://opensource.org/license/gpl-2.0)
132+
133+
#### lib/udis86 include/udis86.h
134+
135+
Code from [Udis86 library](https://sourceforge.net/projects/udis86/)
136+
Licensed under [BSD-2-Clause License](https://opensource.org/license/bsd-2-clause)
137+
138+
#### src/dylib.c src/dylib.h src/stack.h
139+
140+
Code written by Jardel Weyrich
141+
Licensed under [MIT License](https://opensource.org/license/mit)
142+
143+
### Static linked libraries
144+
145+
#### uthash
146+
147+
Project can be found [here](https://troydhanson.github.io/uthash/)
148+
Source code can be found [here](https://github.com/troydhanson/uthash)
149+
Licensed under [BSD-1-Clause](https://opensource.org/license/bsd-1-clause)
150+
151+
#### dirent
152+
153+
Project can be found [here](https://github.com/tronkko/dirent)
154+
Licensed under [MIT License](https://opensource.org/license/mit)
155+
156+
#### dlfcn-win32
157+
158+
Project can be found [here](https://github.com/dlfcn-win32/dlfcn-win32)
159+
Licensed under [MIT License](https://opensource.org/license/mit)
160+

build-msvc-wine.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env bash
2+
3+
ARCH=${ARCH:-"x64"}
4+
BIN=${BIN:-"/opt/msvc/bin/$ARCH"}
5+
6+
ARCH=$(. "$BIN/msvcenv.sh" && echo $ARCH)
7+
mkdir -p msvc-wine
8+
9+
export VCPKG_ROOT=/opt/msvc/VC/vcpkg
10+
11+
cat >msvc-wine/$ARCH-windows.cmake <<EOF
12+
set(VCPKG_TARGET_ARCHITECTURE $ARCH)
13+
set(VCPKG_CRT_LINKAGE dynamic)
14+
set(VCPKG_LIBRARY_LINKAGE dynamic)
15+
# set(VCPKG_BUILD_TYPE release)
16+
set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE \${VCPKG_ROOT_DIR}/scripts/toolchains/mingw.cmake)
17+
18+
set(VCPKG_POLICY_ALLOW_OBSOLETE_MSVCRT enabled)
19+
set(VCPKG_TARGET_IS_MINGW ON)
20+
set(ENV{CC} x86_64-w64-mingw32-gcc)
21+
set(ENV{CXX} x86_64-w64-mingw32-g++)
22+
23+
set(ENV{PATH} "$BIN:\$ENV{PATH}")
24+
EOF
25+
26+
#
27+
# Install dependencies with classic mode
28+
vcpkg install --vcpkg-root=$VCPKG_ROOT --triplet=x64-windows --overlay-triplets=./msvc-wine
29+
30+
cat >msvc-wine/$ARCH-windows.cmake <<EOF
31+
set(VCPKG_TARGET_ARCHITECTURE $ARCH)
32+
set(VCPKG_CRT_LINKAGE dynamic)
33+
set(VCPKG_LIBRARY_LINKAGE dynamic)
34+
# set(VCPKG_BUILD_TYPE release)
35+
set(VCPKG_DISABLE_COMPILER_TRACKING ON)
36+
set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE \${VCPKG_ROOT_DIR}/scripts/toolchains/windows.cmake)
37+
38+
set(ENV{CC} cl.exe)
39+
set(ENV{CXX} cl.exe)
40+
41+
set(ENV{PATH} "$BIN:\$ENV{PATH}")
42+
EOF
43+
44+
CMAKE_ARGS=(
45+
-G"Ninja Multi-Config"
46+
-DCMAKE_C_COMPILER=$BIN/cl
47+
-DCMAKE_CXX_COMPILER=$BIN/cl
48+
-DCMAKE_SYSTEM_NAME=Windows
49+
-DVCPKG_TARGET_TRIPLET=x64-windows
50+
-DVCPKG_OVERLAY_TRIPLETS=./msvc-wine
51+
-DVCPKG_MANIFEST_INSTALL=OFF
52+
-DVCPKG_MANIFEST_MODE=ON
53+
-DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
54+
-DOpenSSL_DIR=$VCPKG_ROOT/installed/x64-windows/share/openssl
55+
)
56+
57+
# Vcpkg uses pwsh and dumpbin to copy dependencies into the output directory for executables.
58+
if command -v pwsh &>/dev/null; then
59+
echo "Powershell installed!"
60+
export PATH=$BIN:$PATH
61+
else
62+
CMAKE_ARGS+=(
63+
-DVCPKG_APPLOCAL_DEPS=OFF
64+
)
65+
fi
66+
67+
case $OSTYPE in
68+
darwin*)
69+
CMAKE_ARGS+=(
70+
# No winbind package available on macOS.
71+
# https://github.com/mstorsjo/msvc-wine/issues/6
72+
-DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=Embedded
73+
) ;;
74+
esac
75+
76+
# CMAKE_CONFIG=Debug
77+
CMAKE_CONFIG=RelWithDebInfo
78+
# CMAKE_CONFIG=Release
79+
80+
cmake -B msvc-wine -DCMAKE_BUILD_TYPE=${CMAKE_CONFIG} "${CMAKE_ARGS[@]}"
81+
cmake --build msvc-wine --config ${CMAKE_CONFIG} -- -v
82+
83+
cd msvc-wine
84+
cpack -C ${CMAKE_CONFIG} -G 7Z
85+

0 commit comments

Comments
 (0)