Skip to content

Commit

Permalink
Fixup logging, remove stl container, cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
jmpews committed Mar 28, 2023
1 parent 6f065cf commit 687ff82
Show file tree
Hide file tree
Showing 30 changed files with 461 additions and 492 deletions.
97 changes: 60 additions & 37 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set(CMAKE_C_STANDARD 11)

auto_source_group("." "auto-source-group" "\\.(cc|cpp|c|h)$")

# ===== handle option =====
# --- options

option(DOBBY_DEBUG "Enable debug logging" OFF)

Expand All @@ -28,45 +28,54 @@ option(Plugin.ImportTableReplace "Enable import table replace " OFF)

option(Plugin.Android.BionicLinkerUtil "Enable android bionic linker util" OFF)

option(BUILD_EXAMPLE "Build example" OFF)
option(DOBBY_BUILD_EXAMPLE "Build example" OFF)

option(BUILD_TEST "Build test" OFF)
option(DOBBY_BUILD_TEST "Build test" OFF)

# private
option(Obfuscation "Enable llvm obfuscation" OFF)
# --- private
option(DOBBY_BUILD_KERNEL_MODE "Build xnu kernel mode" OFF)

# private
option(BUILD_KERNEL_MODE "Build xnu kernel mode" OFF)
option(Private.Obfuscation "Enable llvm obfuscation" OFF)

# Enable debug will log more information
if ((NOT DEFINED CMAKE_BUILD_TYPE) OR (CMAKE_BUILD_TYPE STREQUAL "Debug"))
set(DOBBY_DEBUG ON)
endif ()

if (DOBBY_DEBUG)
add_definitions(-DDOBBY_DEBUG)
add_definitions(-DLOGGING_DEBUG)
message(STATUS "[Dobby] Enable debug logging")
endif ()

# Enable full floating point register pack
set(compile_definitions "")

# for arm64, allow access q8 - q31
if (FullFloatingPointRegisterPack)
add_definitions(-DFULL_FLOATING_POINT_REGISTER_PACK)
message(STATUS "[Dobby] Save and pack all floating-point registers")
set(compile_definitions "${compile_definitions} -DFULL_FLOATING_POINT_REGISTER_PACK")
endif ()

if (BUILD_KERNEL_MODE)
set(BUILDING_KERNEL ON)
add_definitions(-DBUILDING_KERNEL)
message(STATUS "[Dobby] Build xnu kernel mode")
if (DOBBY_BUILD_KERNEL_MODE)
set(compile_definitions "${compile_definitions} -DBUILDING_KERNEL")
endif ()

if (DOBBY_DEBUG)
set(compile_definitions "${compile_definitions} -DDOBBY_DEBUG")
else ()
set(compile_definitions "${compile_definitions} -DDOBBY_LOGGING_DISABLE")
endif ()

if (CMAKE_GENERATOR STREQUAL Xcode)
endif ()

include(cmake/compiler_and_linker.cmake)

message(STATUS "[Dobby] CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
message(STATUS "[Dobby] DOBBY_DEBUG: ${DOBBY_DEBUG}")
message(STATUS "[Dobby] NearBranch: ${NearBranch}")
message(STATUS "[Dobby] FullFloatingPointRegisterPack: ${FullFloatingPointRegisterPack}")
message(STATUS "[Dobby] Plugin.SymbolResolver: ${Plugin.SymbolResolver}")
message(STATUS "[Dobby] Plugin.ImportTableReplace: ${Plugin.ImportTableReplace}")
message(STATUS "[Dobby] Plugin.Android.BionicLinkerUtil: ${Plugin.Android.BionicLinkerUtil}")
message(STATUS "[Dobby] DOBBY_BUILD_EXAMPLE: ${DOBBY_BUILD_EXAMPLE}")
message(STATUS "[Dobby] DOBBY_BUILD_TEST: ${DOBBY_BUILD_TEST}")
message(STATUS "[Dobby] DOBBY_BUILD_KERNEL_MODE: ${DOBBY_BUILD_KERNEL_MODE}")
message(STATUS "[Dobby] Private.Obfuscation: ${Private.Obfuscation}")

# ---

include_directories(
Expand All @@ -81,7 +90,7 @@ include_directories(
./builtin-plugin
)

if (SYSTEM.Darwin AND BUILDING_KERNEL)
if (SYSTEM.Darwin AND DOBBY_BUILD_KERNEL_MODE)
include_directories(
source/Backend/KernelMode
)
Expand Down Expand Up @@ -168,7 +177,7 @@ set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
source/InterceptEntry.cpp
)

if (SYSTEM.Darwin AND BUILDING_KERNEL)
if (SYSTEM.Darwin AND DOBBY_BUILD_KERNEL_MODE)
set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
# platform util
source/Backend/KernelMode/PlatformUtil/Darwin/ProcessRuntimeUtility.cc
Expand Down Expand Up @@ -226,11 +235,11 @@ endif ()

# ---

if (0 AND SYSTEM.iOS AND (NOT BUILDING_KERNEL))
if (0 AND SYSTEM.iOS AND (NOT DOBBY_BUILD_KERNEL_MODE))
include_directories(
source/Backend/UserMode/ExecMemory/substrated
)
add_definitions(-DCODE_PATCH_WITH_SUBSTRATED)
set(compile_definitions "${compile_definitions} -DCODE_PATCH_WITH_SUBSTRATED")
set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
source/Backend/UserMode/ExecMemory/substrated/mach_interface_support
)
Expand All @@ -251,7 +260,6 @@ endif ()
# ---

if (NearBranch)
message(STATUS "[Dobby] Enable near branch trampoline")
set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
source/InterceptRouting/RoutingPlugin/NearBranchTrampoline/near_trampoline_arm64.cc
source/InterceptRouting/RoutingPlugin/NearBranchTrampoline/NearBranchTrampoline.cc
Expand All @@ -266,10 +274,10 @@ get_target_property(logging.SOURCE_FILE_LIST logging SOURCES)

# add osbase library
add_subdirectory(external/osbase)

# ---

if (Plugin.SymbolResolver)
message(STATUS "[Dobby] Enable symbol resolver")
include_directories(builtin-plugin/SymbolResolver)
add_subdirectory(builtin-plugin/SymbolResolver)
get_target_property(symbol_resolver.SOURCE_FILE_LIST dobby_symbol_resolver SOURCES)
Expand Down Expand Up @@ -301,7 +309,7 @@ if (EXISTS "${CMAKE_SOURCE_DIR}/.git")
endif ()
endif ()
set(DOBBY_BUILD_VERSION "Dobby${VERSION_REVISION}")
add_definitions(-D__DOBBY_BUILD_VERSION__="${DOBBY_BUILD_VERSION}")
set(compile_definitions "${compile_definitions} -D__DOBBY_BUILD_VERSION__=\"${DOBBY_BUILD_VERSION}\"")
message(STATUS "[Dobby] ${DOBBY_BUILD_VERSION}")

# ---
Expand All @@ -314,7 +322,9 @@ add_library(dobby SHARED
${dobby.plugin.SOURCE_FILE_LIST}
)

target_include_directories(dobby PUBLIC include)
target_include_directories(dobby PUBLIC
include
)

# ---

Expand All @@ -326,16 +336,16 @@ add_library(dobby_static STATIC
${dobby.plugin.SOURCE_FILE_LIST}
)

target_include_directories(dobby_static PUBLIC include)
target_include_directories(dobby_static PUBLIC
include
)

set_target_properties(dobby_static PROPERTIES OUTPUT_NAME "dobby")
set_target_properties(dobby_static
PROPERTIES OUTPUT_NAME "dobby"
)

# ---

if (Obfuscation)
set(linker_flags "${linker_flags} -Wl,-mllvm -Wl,-obfuscator-conf=all")
endif ()

set_target_properties(dobby
PROPERTIES
LINK_FLAGS "${linker_flags}"
Expand All @@ -347,6 +357,19 @@ set_target_properties(dobby_static
COMPILE_FLAGS "${compiler_flags}"
)

target_compile_definitions(dobby PRIVATE
"COMPILE_DEFINITIONS ${compile_definitions}"
)
target_compile_definitions(dobby_static PRIVATE
"COMPILE_DEFINITIONS ${compile_definitions}"
)

# ---

if (Private.Obfuscation)
set(linker_flags "${linker_flags} -Wl,-mllvm -Wl,-obfuscator-conf=all")
endif ()

# ---

if (SYSTEM.Android)
Expand All @@ -369,16 +392,16 @@ endif ()

# ---

if (BUILD_EXAMPLE AND (NOT BUILDING_KERNEL))
if (DOBBY_BUILD_EXAMPLE AND (NOT DOBBY_BUILD_KERNEL_MODE))
add_subdirectory(examples)
endif ()

if (BUILD_TEST AND (NOT BUILDING_KERNEL))
if (DOBBY_BUILD_TEST AND (NOT DOBBY_BUILD_KERNEL_MODE))
add_subdirectory(tests)
endif ()

# ---

if (SYSTEM.Darwin AND (NOT BUILDING_KERNEL))
if (SYSTEM.Darwin AND (NOT DOBBY_BUILD_KERNEL_MODE))
include(cmake/platform/platform-darwin.cmake)
endif ()
4 changes: 2 additions & 2 deletions builtin-plugin/SymbolResolver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ if (NOT DEFINED DOBBY_DIR)
message(FATAL_ERROR "DOBBY_DIR must be set!")
endif ()

if (SYSTEM.Darwin AND (NOT BUILDING_KERNEL))
if (SYSTEM.Darwin AND (NOT DOBBY_BUILD_KERNEL_MODE))
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST}
${CMAKE_CURRENT_SOURCE_DIR}/macho/macho_ctx.cc
${CMAKE_CURRENT_SOURCE_DIR}/macho/shared_cache_ctx.cpp
Expand All @@ -14,7 +14,7 @@ if (SYSTEM.Darwin AND (NOT BUILDING_KERNEL))
${DOBBY_DIR}/source/Backend/UserMode/PlatformUtil/Darwin/ProcessRuntimeUtility.cc
)
endif ()
if (SYSTEM.Darwin AND BUILDING_KERNEL)
if (SYSTEM.Darwin AND DOBBY_BUILD_KERNEL_MODE)
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST}
${CMAKE_CURRENT_SOURCE_DIR}/macho/dobby_symbol_resolver.cc

Expand Down
13 changes: 10 additions & 3 deletions builtin-plugin/SymbolResolver/macho/dobby_symbol_resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@

PUBLIC void *DobbySymbolResolver(const char *image_name, const char *symbol_name_pattern) {
uintptr_t result = 0;
const std::vector<RuntimeModule> modules = ProcessRuntimeUtility::GetProcessModuleMap();
auto modules = ProcessRuntimeUtility::GetProcessModuleMap();

for (auto iter = modules.begin(); iter != modules.end(); iter++) {
auto module = *iter;
if (image_name != NULL && strnstr(module.path, image_name, strlen(module.path)) == NULL)

// image filter
if (image_name && !strstr(module.path, image_name))
continue;

// dyld in shared cached at new os version
// ignore dyld, as some functions as own implementation in dyld
if (!image_name && strstr(module.path, "dyld"))
continue;

auto header = (mach_header_t *)module.load_address;
Expand Down Expand Up @@ -102,7 +109,7 @@ PUBLIC void *DobbySymbolResolver(const char *image_name, const char *symbol_name
#endif

if (result == 0) {
LOG(LOG_LEVEL_DEBUG, "symbol resolver failed: %s", symbol_name_pattern);
DEBUG_LOG("symbol resolver failed: %s", symbol_name_pattern);
}

return (void *)result;
Expand Down
2 changes: 2 additions & 0 deletions cmake/compiler_and_linker.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ if (NOT DOBBY_DEBUG)
set(linker_flags "${linker_flags} -Wl,-x -Wl,-S")
endif ()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-rtti -fno-exceptions")

if (SYSTEM.Darwin)
# set(compiler_flags "${compiler_flags} -nostdinc++")
elseif (SYSTEM.Android)
Expand Down
2 changes: 1 addition & 1 deletion cmake/platform/platform-darwin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ set_target_properties(DobbyX PROPERTIES
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "Apple Development"
)

if ((SYSTEM.Darwin AND BUILDING_PLUGIN) AND (NOT BUILDING_KERNEL))
if ((SYSTEM.Darwin AND BUILDING_PLUGIN) AND (NOT DOBBY_BUILD_KERNEL_MODE))
add_subdirectory(builtin-plugin/Dyld2HideLibrary)
add_subdirectory(builtin-plugin/ObjcRuntimeHook)
if (PROCESSOR.AARCH64)
Expand Down
4 changes: 2 additions & 2 deletions docs/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ option(Plugin.ImportTableReplace "Enable import table replace " OFF)
option(Plugin.Android.BionicLinkerUtil "Enable android bionic linker util" OFF)
option(BUILD_EXAMPLE "Build example" OFF)
option(DOBBY_BUILD_EXAMPLE "Build example" OFF)
option(BUILD_TEST "Build test" OFF)
option(DOBBY_BUILD_TEST "Build test" OFF)
```

## Build with `scripts/platform_builder.py`
Expand Down
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_executable(socket_example

target_link_libraries(socket_example
dobby
logging
)


Expand Down
1 change: 1 addition & 0 deletions external/TINYSTL/algorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma once
Loading

0 comments on commit 687ff82

Please sign in to comment.