Skip to content

Commit 0932d69

Browse files
committed
Update backend of xnu kernel, and macho_ctx_kit
1 parent 0527af1 commit 0932d69

24 files changed

Lines changed: 123 additions & 563 deletions

CMakeLists.txt

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,7 @@ include_directories(
9090
./builtin-plugin
9191
)
9292

93-
if (SYSTEM.Darwin AND DOBBY_BUILD_KERNEL_MODE)
94-
include_directories(
95-
source/Backend/KernelMode
96-
)
97-
else ()
93+
if (SYSTEM.Darwin AND (NOT DOBBY_BUILD_KERNEL_MODE))
9894
include_directories(
9995
source/Backend/UserMode
10096
)
@@ -177,53 +173,32 @@ set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
177173
source/InterceptEntry.cpp
178174
)
179175

180-
if (SYSTEM.Darwin AND DOBBY_BUILD_KERNEL_MODE)
181-
set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
182-
# platform util
183-
source/Backend/KernelMode/PlatformUtil/Darwin/ProcessRuntimeUtility.cc
184-
185-
# kernel mode - platform interface
186-
source/Backend/KernelMode/UnifiedInterface/platform-darwin.cc
187-
source/Backend/KernelMode/UnifiedInterface/exec_mem_placeholder.asm
188176

189-
# kernel mode - executable memory
190-
source/Backend/KernelMode/ExecMemory/code-patch-tool-darwin.cc
191-
source/Backend/KernelMode/ExecMemory/clear-cache-tool-all.c
192-
)
193-
elseif (SYSTEM.Darwin)
177+
if (SYSTEM.Darwin AND NOT DOBBY_BUILD_KERNEL_MODE)
194178
set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
195-
# platform util
196179
source/Backend/UserMode/PlatformUtil/Darwin/ProcessRuntimeUtility.cc
197180

198-
# user mode - platform interface
199181
source/Backend/UserMode/UnifiedInterface/platform-posix.cc
200182

201-
# user mode - executable memory
202183
source/Backend/UserMode/ExecMemory/code-patch-tool-darwin.cc
203184
source/Backend/UserMode/ExecMemory/clear-cache-tool-all.c
204185
)
205186

206187
elseif (SYSTEM.Linux OR SYSTEM.Android)
207188
set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
208-
# platform util
209189
source/Backend/UserMode/PlatformUtil/Linux/ProcessRuntimeUtility.cc
210190

211-
# user mode - platform interface
212191
source/Backend/UserMode/UnifiedInterface/platform-posix.cc
213192

214-
# user mode - executable memory
215193
source/Backend/UserMode/ExecMemory/code-patch-tool-posix.cc
216194
source/Backend/UserMode/ExecMemory/clear-cache-tool-all.c
217195
)
218196
elseif (SYSTEM.Windows)
219197
set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
220-
# platform util
221198
source/Backend/UserMode/PlatformUtil/Windows/ProcessRuntimeUtility.cc
222199

223-
# user mode - platform interface
224200
source/Backend/UserMode/UnifiedInterface/platform-windows.cc
225201

226-
# user mode - executable memory
227202
source/Backend/UserMode/ExecMemory/code-patch-tool-windows.cc
228203
source/Backend/UserMode/ExecMemory/clear-cache-tool-all.c
229204
)
@@ -314,26 +289,28 @@ message(STATUS "[Dobby] ${DOBBY_BUILD_VERSION}")
314289

315290
# ---
316291

317-
add_library(dobby SHARED
292+
set(SOURCE_FILE_LIST
318293
${dobby.HEADER_FILE_LIST}
319294
${dobby.SOURCE_FILE_LIST}
320295
${logging.SOURCE_FILE_LIST}
321-
${misc_helper.SOURCE_FILE_LIST}
322296
${dobby.plugin.SOURCE_FILE_LIST}
323297
)
324298

299+
get_absolute_path_list(SOURCE_FILE_LIST SOURCE_FILE_LIST_)
300+
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST_})
301+
302+
add_library(dobby SHARED
303+
${SOURCE_FILE_LIST}
304+
)
305+
325306
target_include_directories(dobby PUBLIC
326307
include
327308
)
328309

329310
# ---
330311

331312
add_library(dobby_static STATIC
332-
${dobby.HEADER_FILE_LIST}
333-
${dobby.SOURCE_FILE_LIST}
334-
${logging.SOURCE_FILE_LIST}
335-
${misc_helper.SOURCE_FILE_LIST}
336-
${dobby.plugin.SOURCE_FILE_LIST}
313+
${SOURCE_FILE_LIST}
337314
)
338315

339316
target_include_directories(dobby_static PUBLIC
Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,55 @@
11
set(SOURCE_FILE_LIST)
22

3+
include_directories(
4+
.
5+
)
6+
37
if (NOT DEFINED DOBBY_DIR)
48
message(FATAL_ERROR "DOBBY_DIR must be set!")
59
endif ()
610

7-
if (SYSTEM.Darwin AND (NOT DOBBY_BUILD_KERNEL_MODE))
8-
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST}
11+
if (SYSTEM.Darwin)
12+
add_library(macho_ctx_kit
13+
${CMAKE_CURRENT_SOURCE_DIR}/macho/macho_ctx.h
914
${CMAKE_CURRENT_SOURCE_DIR}/macho/macho_ctx.cc
10-
${CMAKE_CURRENT_SOURCE_DIR}/macho/shared_cache_ctx.cpp
11-
${CMAKE_CURRENT_SOURCE_DIR}/macho/dobby_symbol_resolver.cc
12-
${CMAKE_CURRENT_SOURCE_DIR}/macho/macho_file_symbol_resolver.cpp
15+
)
1316

14-
${DOBBY_DIR}/source/Backend/UserMode/PlatformUtil/Darwin/ProcessRuntimeUtility.cc
17+
add_library(shared_cache_ctx_kit
18+
${CMAKE_CURRENT_SOURCE_DIR}/macho/shared_cache_ctx.h
19+
${CMAKE_CURRENT_SOURCE_DIR}/macho/shared_cache_ctx.cpp
1520
)
16-
endif ()
17-
if (SYSTEM.Darwin AND DOBBY_BUILD_KERNEL_MODE)
18-
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST}
19-
${CMAKE_CURRENT_SOURCE_DIR}/macho/dobby_symbol_resolver.cc
2021

21-
${DOBBY_DIR}/source/Backend/KernelMode/PlatformUtil/Darwin/ProcessRuntimeUtility.cc
22+
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST}
23+
macho/macho_ctx.cc
24+
macho/dobby_symbol_resolver.cc
2225
)
23-
endif ()
24-
if (SYSTEM.Linux OR SYSTEM.Android)
26+
27+
if (NOT DOBBY_BUILD_KERNEL_MODE)
28+
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST}
29+
macho/macho_file_symbol_resolver.cpp
30+
macho/shared_cache_ctx.cpp
31+
${DOBBY_DIR}/source/Backend/UserMode/PlatformUtil/Darwin/ProcessRuntimeUtility.cc
32+
)
33+
endif ()
34+
elseif (SYSTEM.Linux OR SYSTEM.Android)
2535
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST}
26-
${CMAKE_CURRENT_SOURCE_DIR}/elf/dobby_symbol_resolver.cc
36+
elf/dobby_symbol_resolver.cc
2737

2838
${DOBBY_DIR}/source/Backend/UserMode/PlatformUtil/Linux/ProcessRuntimeUtility.cc
2939
)
30-
endif ()
31-
if (SYSTEM.Windows)
40+
elseif (SYSTEM.Windows)
3241
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST}
33-
${CMAKE_CURRENT_SOURCE_DIR}/pe/dobby_symbol_resolver.cc
42+
pe/dobby_symbol_resolver.cc
3443

3544
${DOBBY_DIR}/source/Backend/UserMode/PlatformUtil/Windows/ProcessRuntimeUtility.cc
3645
)
3746
endif ()
3847

39-
add_library(macho_ctx_kit
40-
${CMAKE_CURRENT_SOURCE_DIR}/macho/macho_ctx.h
41-
${CMAKE_CURRENT_SOURCE_DIR}/macho/macho_ctx.cc
42-
)
43-
44-
add_library(shared_cache_ctx_kit
45-
${CMAKE_CURRENT_SOURCE_DIR}/macho/shared_cache_ctx.h
46-
${CMAKE_CURRENT_SOURCE_DIR}/macho/shared_cache_ctx.cpp
47-
)
48+
get_absolute_path_list(SOURCE_FILE_LIST SOURCE_FILE_LIST_)
49+
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST_})
4850

4951
add_library(dobby_symbol_resolver
5052
${SOURCE_FILE_LIST}
5153
)
5254

5355

54-
include_directories(
55-
.
56-
)
57-

builtin-plugin/SymbolResolver/macho/dobby_symbol_resolver.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@
1212
#include "macho_ctx.h"
1313
#include "shared_cache_ctx.h"
1414

15-
#if defined(BUILDING_KERNEL)
16-
#else
17-
15+
#if !defined(BUILDING_KERNEL)
1816
#include <mach-o/dyld.h>
1917
#include <mach-o/dyld_images.h>
20-
2118
#endif
2219

2320
#undef LOG_TAG

builtin-plugin/SymbolResolver/macho/macho_ctx.cc

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,29 @@
88

99
#define ASSERT(x)
1010

11+
uintptr_t macho_iterate_symbol_table(char *symbol_name_pattern, nlist_t *symtab, uint32_t symtab_count, char *strtab) {
12+
for (uint32_t i = 0; i < symtab_count; i++) {
13+
if (symtab[i].n_value) {
14+
uint32_t strtab_offset = symtab[i].n_un.n_strx;
15+
char *symbol_name = strtab + strtab_offset;
16+
#if 0
17+
printf("> %s", symbol_name);
18+
#endif
19+
if (strcmp(symbol_name_pattern, symbol_name) == 0) {
20+
return symtab[i].n_value;
21+
}
22+
if (symbol_name[0] == '_') {
23+
if (strcmp(symbol_name_pattern, &symbol_name[1]) == 0) {
24+
return symtab[i].n_value;
25+
}
26+
}
27+
}
28+
}
29+
return 0;
30+
}
31+
32+
// ---
33+
1134
void macho_ctx_t::init(mach_header_t *header, bool is_runtime_mode) {
1235
memset(this, 0, sizeof(macho_ctx_t));
1336

@@ -62,6 +85,15 @@ void macho_ctx_t::init(mach_header_t *header, bool is_runtime_mode) {
6285
linkedit_base = (uintptr_t)slide + linkedit_segment_vmaddr - linkedit_segment->fileoff;
6386
}
6487

88+
vm_region_start = segments[0]->vmaddr;
89+
// skip __LINKEDIT
90+
if (strcmp(segments[0]->segname, "__LINKEDIT") == 0) {
91+
vm_region_start = segments[1]->vmaddr;
92+
}
93+
vm_region_end = segments[segments_count - 1]->vmaddr + segments[segments_count - 1]->vmsize;
94+
vmaddr = vm_region_start;
95+
vmsize = vm_region_end - vm_region_start;
96+
6597
this->text_seg = text_segment;
6698
this->text_exec_seg = text_exec_segment;
6799
this->data_seg = data_segment;
@@ -82,27 +114,6 @@ void macho_ctx_t::init(mach_header_t *header, bool is_runtime_mode) {
82114
this->indirect_symtab = (uint32_t *)(this->linkedit_base + this->dysymtab_cmd->indirectsymoff);
83115
}
84116

85-
uintptr_t macho_iterate_symbol_table(char *symbol_name_pattern, nlist_t *symtab, uint32_t symtab_count, char *strtab) {
86-
for (uint32_t i = 0; i < symtab_count; i++) {
87-
if (symtab[i].n_value) {
88-
uint32_t strtab_offset = symtab[i].n_un.n_strx;
89-
char *symbol_name = strtab + strtab_offset;
90-
#if 0
91-
printf("> %s", symbol_name);
92-
#endif
93-
if (strcmp(symbol_name_pattern, symbol_name) == 0) {
94-
return symtab[i].n_value;
95-
}
96-
if (symbol_name[0] == '_') {
97-
if (strcmp(symbol_name_pattern, &symbol_name[1]) == 0) {
98-
return symtab[i].n_value;
99-
}
100-
}
101-
}
102-
}
103-
return 0;
104-
}
105-
106117
uintptr_t macho_ctx_t::iterate_symbol_table(const char *symbol_name_pattern) {
107118
nlist_t *symtab = this->symtab;
108119
uint32_t symtab_count = this->symtab_cmd->nsyms;

builtin-plugin/SymbolResolver/macho/macho_ctx.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

3-
#include <stdint.h>
4-
#include <stdbool.h>
3+
#include <sys/types.h>
54
#include <mach-o/loader.h>
65
#include <mach-o/nlist.h>
76

@@ -34,6 +33,11 @@ struct macho_ctx_t {
3433

3534
mach_header_t *header;
3635

36+
uintptr_t vmaddr;
37+
size_t vmsize;
38+
uintptr_t vm_region_start;
39+
uintptr_t vm_region_end;
40+
3741
uintptr_t slide;
3842
uintptr_t linkedit_base;
3943

cmake/Util.cmake

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
# Check files list exist
22
function(check_files_exist CHECK_FILES)
3-
foreach(file ${CHECK_FILES})
4-
if(NOT EXISTS "${file}")
5-
message(FATAL_ERROR "${file} NOT EXISTS!")
6-
endif()
7-
endforeach()
3+
foreach (file ${CHECK_FILES})
4+
if (NOT EXISTS "${file}")
5+
message(FATAL_ERROR "${file} NOT EXISTS!")
6+
endif ()
7+
endforeach ()
88
endfunction(check_files_exist CHECK_FILES)
99

1010
# Search suffix files
1111
function(search_suffix_files suffix INPUT_VARIABLE OUTPUT_VARIABLE)
12-
set(ResultFiles )
13-
foreach(filePath ${${INPUT_VARIABLE}})
14-
# message(STATUS "[*] searching *.${suffix} from ${filePath}")
15-
file(GLOB files ${filePath}/*.${suffix})
16-
set(ResultFiles ${ResultFiles} ${files})
17-
endforeach()
18-
set(${OUTPUT_VARIABLE} ${ResultFiles} PARENT_SCOPE)
12+
set(ResultFiles)
13+
foreach (filePath ${${INPUT_VARIABLE}})
14+
# message(STATUS "[*] searching *.${suffix} from ${filePath}")
15+
file(GLOB files ${filePath}/*.${suffix})
16+
set(ResultFiles ${ResultFiles} ${files})
17+
endforeach ()
18+
set(${OUTPUT_VARIABLE} ${ResultFiles} PARENT_SCOPE)
1919
endfunction()
20+
21+
22+
function(get_absolute_path_list input_list output_list)
23+
set(absolute_list)
24+
foreach (file ${${input_list}})
25+
get_filename_component(absolute_file ${file} ABSOLUTE)
26+
list(APPEND absolute_list ${absolute_file})
27+
endforeach ()
28+
set(${output_list} ${absolute_list} PARENT_SCOPE)
29+
endfunction()

cmake/build_environment_check.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if(MSVC)
2626
endif()
2727

2828

29-
if(BUILDING_SILICON)
29+
if(DOBBY_BUILD_SILICON)
3030
set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_OSX_ARCHITECTURES})
3131
endif()
3232

external/logging/CMakeLists.txt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
include_directories(.)
22

3-
if(NOT DOBBY_BUILD_KERNEL_MODE)
4-
set(SOURCE_FILE_LIST
5-
${CMAKE_CURRENT_SOURCE_DIR}/logging.cc
3+
set(SOURCE_FILE_LIST
4+
logging.cc
65
)
7-
else()
6+
7+
if (DOBBY_BUILD_KERNEL_MODE)
88
set(SOURCE_FILE_LIST
9-
${CMAKE_CURRENT_SOURCE_DIR}/kernel_logging.cc
9+
logging_kern.cc
1010
)
11-
endif()
11+
endif ()
12+
13+
get_absolute_path_list(SOURCE_FILE_LIST SOURCE_FILE_LIST_)
14+
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST_})
15+
1216
add_library(logging
1317
${SOURCE_FILE_LIST}
14-
${SOURCE_HEADER_LIST}
15-
)
18+
)

0 commit comments

Comments
 (0)