-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
134 lines (104 loc) · 5.7 KB
/
Copy pathCMakeLists.txt
File metadata and controls
134 lines (104 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
cmake_minimum_required(VERSION 3.15)
project(libload C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(APPLE)
enable_language(OBJC)
else()
enable_language(ASM)
endif()
# Compiler flags
add_compile_options(-Wall -Wextra -Werror -O2)
option(LIBLOAD_DEBUG "Enable debug logging to stderr" OFF)
if(LIBLOAD_DEBUG)
add_compile_definitions(LIBLOAD_DEBUG)
endif()
# ─── Platform source selection ────────────────────────────────────
if(APPLE)
set(LIBLOAD_SRC src/macho.c src/inject.c)
elseif(UNIX)
set(LIBLOAD_SRC src/elf.c src/elf_inject.c
src/entry_x86_64.S src/entry_aarch64.S
src/entry_x86.S src/entry_arm.S
src/entry_mips.S src/entry_sparc.S)
else()
message(FATAL_ERROR "Unsupported platform")
endif()
# ─── Shared library ──────────────────────────────────────────────
add_library(libload_shared SHARED ${LIBLOAD_SRC})
target_include_directories(libload_shared PUBLIC include)
set_target_properties(libload_shared PROPERTIES OUTPUT_NAME load)
if(UNIX AND NOT APPLE)
target_link_libraries(libload_shared PRIVATE dl)
endif()
# ─── Static library ──────────────────────────────────────────────
add_library(libload_static STATIC ${LIBLOAD_SRC})
target_include_directories(libload_static PUBLIC include)
set_target_properties(libload_static PROPERTIES OUTPUT_NAME load)
# ─── llpack tool ─────────────────────────────────────────────────
add_executable(llpack tools/llpack.c)
target_include_directories(llpack PRIVATE include)
# ─── Common examples ─────────────────────────────────────────────
add_executable(testexec examples/common/testexec.c)
add_executable(testexec_edge examples/common/testexec_edge.c)
add_executable(test_exec examples/common/test_exec.c)
target_link_libraries(test_exec PRIVATE libload_static)
add_executable(test_llbin examples/common/test_llbin.c)
target_link_libraries(test_llbin PRIVATE libload_static)
add_executable(test_open examples/common/test_open.c)
target_link_libraries(test_open PRIVATE libload_static)
add_executable(inject_target examples/common/inject_target.c)
add_library(inject_payload SHARED examples/common/inject_payload.c)
add_library(test_lib SHARED examples/common/test_lib.c)
# ─── Install ─────────────────────────────────────────────────────
install(TARGETS libload_shared libload_static llpack
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin)
install(DIRECTORY include/ DESTINATION include)
# ─── macOS examples ──────────────────────────────────────────────
if(APPLE)
set_target_properties(inject_payload PROPERTIES
PREFIX "" SUFFIX ".dylib" OUTPUT_NAME "inject_payload")
set_target_properties(test_lib PROPERTIES
PREFIX "" SUFFIX ".dylib" OUTPUT_NAME "test_lib")
add_executable(testexec_objc examples/macos/testexec_objc.m)
target_link_libraries(testexec_objc PRIVATE objc)
target_compile_options(testexec_objc PRIVATE -Wno-unused-parameter)
add_executable(testexec_objc_class examples/macos/testexec_objc_class.m)
target_link_libraries(testexec_objc_class PRIVATE objc)
target_compile_options(testexec_objc_class PRIVATE -Wno-unused-parameter)
add_executable(testexec_tlv examples/macos/testexec_tlv.c)
add_executable(test_inject examples/macos/test_inject.c)
target_link_libraries(test_inject PRIVATE libload_static)
add_executable(test_inject_dylib examples/macos/test_inject_dylib.c)
target_link_libraries(test_inject_dylib PRIVATE libload_static)
add_executable(test_inject_spawn examples/macos/test_inject_spawn.c)
target_link_libraries(test_inject_spawn PRIVATE libload_static)
endif()
# ─── Linux examples ──────────────────────────────────────────────
if(UNIX AND NOT APPLE)
set_target_properties(inject_payload PROPERTIES
PREFIX "" SUFFIX ".so" OUTPUT_NAME "inject_payload")
set_target_properties(test_lib PROPERTIES
PREFIX "" SUFFIX ".so" OUTPUT_NAME "test_lib")
add_executable(test_inject examples/linux/test_inject.c)
target_link_libraries(test_inject PRIVATE libload_static)
add_executable(test_inject_dylib examples/linux/test_inject_dylib.c)
target_link_libraries(test_inject_dylib PRIVATE libload_static dl)
add_executable(test_inject_spawn examples/linux/test_inject_spawn.c)
target_link_libraries(test_inject_spawn PRIVATE libload_static)
add_executable(test_bin examples/linux/test_bin.c)
target_compile_options(test_bin PRIVATE -Wno-unused-parameter)
# Architecture-specific assembly bin loaders
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64")
add_executable(test_bin_asm
examples/linux/test_bin_aarch64.S)
target_link_options(test_bin_asm PRIVATE -nostdlib -static)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64")
add_executable(test_bin_asm
examples/linux/test_bin_x86_64.S)
target_link_options(test_bin_asm PRIVATE -nostdlib -static)
endif()
endif()