Skip to content

Commit

Permalink
Cleanup code, and fix build script
Browse files Browse the repository at this point in the history
  • Loading branch information
jmpews committed Mar 3, 2023
1 parent 0aac493 commit 52cc028
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 59 deletions.
10 changes: 7 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ 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 symbol_resolver SOURCES)
get_target_property(symbol_resolver.SOURCE_FILE_LIST dobby_symbol_resolver SOURCES)
set(dobby.plugin.SOURCE_FILE_LIST ${dobby.plugin.SOURCE_FILE_LIST}
${symbol_resolver.SOURCE_FILE_LIST}
)
Expand Down Expand Up @@ -314,6 +314,10 @@ add_library(dobby SHARED
${dobby.plugin.SOURCE_FILE_LIST}
)

target_include_directories(dobby PUBLIC include)

# ---

add_library(dobby_static STATIC
${dobby.HEADER_FILE_LIST}
${dobby.SOURCE_FILE_LIST}
Expand All @@ -322,10 +326,10 @@ add_library(dobby_static STATIC
${dobby.plugin.SOURCE_FILE_LIST}
)

target_include_directories(dobby PUBLIC include)

target_include_directories(dobby_static PUBLIC include)

set_target_properties(dobby_static PROPERTIES OUTPUT_NAME "dobby")

# ---

if (Obfuscation)
Expand Down
2 changes: 1 addition & 1 deletion builtin-plugin/ImportTableReplace/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
add_library(import_table_repalce INTERFACE
add_library(dobby_import_replace INTERFACE
dobby_import_replace.cc
)
2 changes: 1 addition & 1 deletion builtin-plugin/ObjcRuntimeReplace/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_library(objc_runtime_replace
objc_runtime_replace.mm
dobby_objc_runtime_replace.mm
)

target_link_libraries(objc_runtime_replace
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ObjcRuntimeReplace/objc_runtime_repalce.h"
#include "dobby_objc_runtime_repalce.h"

#include <stdio.h>
#include <objc/runtime.h>
Expand Down
32 changes: 19 additions & 13 deletions builtin-plugin/SymbolResolver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
set(SOURCE_FILE_LIST )
set(SOURCE_FILE_LIST)

if(NOT DEFINED DOBBY_DIR)
if (NOT DEFINED DOBBY_DIR)
message(FATAL_ERROR "DOBBY_DIR must be set!")
endif()
endif ()

if(SYSTEM.Darwin AND (NOT BUILDING_KERNEL))
if (SYSTEM.Darwin AND (NOT BUILDING_KERNEL))
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST}
${CMAKE_CURRENT_SOURCE_DIR}/macho/macho_ctx.cc
${CMAKE_CURRENT_SOURCE_DIR}/macho/dyld_shared_cache_symbol_table_iterator.cc
${CMAKE_CURRENT_SOURCE_DIR}/macho/dobby_symbol_resolver.cc

${DOBBY_DIR}/source/Backend/UserMode/PlatformUtil/Darwin/ProcessRuntimeUtility.cc
)
endif()
if(SYSTEM.Darwin AND BUILDING_KERNEL)
endif ()
if (SYSTEM.Darwin AND BUILDING_KERNEL)
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST}
${CMAKE_CURRENT_SOURCE_DIR}/macho/dobby_symbol_resolver.cc

${DOBBY_DIR}/source/Backend/KernelMode/PlatformUtil/Darwin/ProcessRuntimeUtility.cc
)
endif()
if(SYSTEM.Linux OR SYSTEM.Android)
endif ()
if (SYSTEM.Linux OR SYSTEM.Android)
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST}
${CMAKE_CURRENT_SOURCE_DIR}/elf/dobby_symbol_resolver.cc

${DOBBY_DIR}/source/Backend/UserMode/PlatformUtil/Linux/ProcessRuntimeUtility.cc
)
endif()
if(SYSTEM.Windows)
endif ()
if (SYSTEM.Windows)
set(SOURCE_FILE_LIST ${SOURCE_FILE_LIST}
${CMAKE_CURRENT_SOURCE_DIR}/pe/dobby_symbol_resolver.cc

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

add_library(macho_ctx_kit
macho/macho_ctx.h
macho/macho_ctx.cc
)
endif()

add_library(symbol_resolver
add_library(dobby_symbol_resolver
${SOURCE_FILE_LIST}
)


include_directories(
.
)
Expand Down
82 changes: 42 additions & 40 deletions scripts/platform_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import argparse

platforms = {
"macos": ["x86_64", "arm64", "arm64e"],
"iphoneos": ["arm64", "arm64e"],
"linux": ["x86", "x86_64", "arm", "arm64"],
"android": ["x86", "x86_64", "armeabi-v7a", "arm64-v8a"]
"macos": ["x86_64", "arm64", "arm64e"],
"iphoneos": ["arm64", "arm64e"],
"linux": ["x86", "x86_64", "arm", "arm64"],
"android": ["x86", "x86_64", "armeabi-v7a", "arm64-v8a"]
}


Expand All @@ -26,7 +26,9 @@ class PlatformBuilder(object):

project_dir = ""
output_dir = ""
output_name = ""

shared_output_name = ""
static_output_name = ""

platform = ""
arch = ""
Expand Down Expand Up @@ -60,18 +62,21 @@ def setup_common_args(self):
self.cmake_args += ["-DCMAKE_BUILD_TYPE={}".format(self.cmake_build_type)]

if self.library_build_type == "shared":
self.cmake_args += ["-DDOBBY_GENERATE_SHARED=ON"]
pass
# self.cmake_args += ["-DDOBBY_GENERATE_SHARED=ON"]
elif self.library_build_type == "static":
self.cmake_args += ["-DDOBBY_GENERATE_SHARED=OFF"]
pass
# self.cmake_args += ["-DDOBBY_GENERATE_SHARED=OFF"]

def build(self):
subprocess.run(["mkdir", "-p", "{}".format(self.output_dir)], check=True)
self.cmake_generate_build_system()

subprocess.run(["make", "-j8", "dobby"], cwd=self.cmake_build_dir, check=True)
subprocess.run("cmake --build . --clean-first --target dobby --target dobby_static -- -j8", cwd=self.cmake_build_dir, shell=True, check=True)

os.system(f"mkdir -p {self.output_dir}")
os.system(f"cp {self.cmake_build_dir}/{self.output_name} {self.output_dir}")
os.system(f"cp {self.cmake_build_dir}/{self.shared_output_name} {self.output_dir}")
os.system(f"cp {self.cmake_build_dir}/{self.static_output_name} {self.output_dir}")


class WindowsPlatformBuilder(PlatformBuilder):
Expand All @@ -85,15 +90,15 @@ def __init__(self, project_dir, library_build_type, platform, arch):
self.output_name = "libdobby.lib"

triples = {
"x86": "i686-pc-windows-msvc",
"x64": "x86_64-pc-windows-msvc",
# "arm": "arm-pc-windows-msvc",
"arm64": "arm64-pc-windows-msvc",
"x86": "i686-pc-windows-msvc",
"x64": "x86_64-pc-windows-msvc",
# "arm": "arm-pc-windows-msvc",
"arm64": "arm64-pc-windows-msvc",
}

# self.cmake_args += ["--target {}".format(triples[arch])]
self.cmake_args += [
"-DCMAKE_SYSTEM_PROCESSOR={}".format(arch),
"-DCMAKE_SYSTEM_PROCESSOR={}".format(arch),
]


Expand All @@ -102,22 +107,20 @@ class LinuxPlatformBuilder(PlatformBuilder):
def __init__(self, project_dir, library_build_type, arch):
super().__init__(project_dir, library_build_type, "linux", arch)

if self.library_build_type == "shared":
self.output_name = "libdobby.so"
else:
self.output_name = "libdobby.a"
self.shared_output_name = "libdobby.so"
self.static_output_name = "libdobby.a"

targets = {
"x86": "i686-linux-gnu",
"x86_64": "x86_64-linux-gnu",
"arm": "arm-linux-gnueabi",
"aarch64": "aarch64-linux-gnu",
"x86": "i686-linux-gnu",
"x86_64": "x86_64-linux-gnu",
"arm": "arm-linux-gnueabi",
"aarch64": "aarch64-linux-gnu",
}

# self.cmake_args += ["--target={}".format(targets[arch])]
self.cmake_args += [
"-DCMAKE_SYSTEM_NAME=Linux",
"-DCMAKE_SYSTEM_PROCESSOR={}".format(arch),
"-DCMAKE_SYSTEM_NAME=Linux",
"-DCMAKE_SYSTEM_PROCESSOR={}".format(arch),
]


Expand All @@ -126,18 +129,16 @@ class AndroidPlatformBuilder(PlatformBuilder):
def __init__(self, android_nkd_dir, project_dir, library_build_type, arch):
super().__init__(project_dir, library_build_type, "android", arch)

if self.library_build_type == "shared":
self.output_name = "libdobby.so"
else:
self.output_name = "libdobby.a"
self.shared_output_name = "libdobby.so"
self.static_output_name = "libdobby.a"

android_api_level = 21
if arch == "armeabi-v7a" or arch == "x86":
android_api_level = 19

self.cmake_args += [
"-DCMAKE_SYSTEM_NAME=Android", f"-DCMAKE_ANDROID_NDK={android_nkd_dir}", f"-DCMAKE_ANDROID_ARCH_ABI={arch}",
f"-DCMAKE_SYSTEM_VERSION={android_api_level}"
"-DCMAKE_SYSTEM_NAME=Android", f"-DCMAKE_ANDROID_NDK={android_nkd_dir}", f"-DCMAKE_ANDROID_ARCH_ABI={arch}",
f"-DCMAKE_SYSTEM_VERSION={android_api_level}"
]


Expand All @@ -147,19 +148,17 @@ def __init__(self, project_dir, library_build_type, platform, arch):
super().__init__(project_dir, library_build_type, platform, arch)

self.cmake_args += [
"-DCMAKE_OSX_ARCHITECTURES={}".format(arch),
"-DCMAKE_SYSTEM_PROCESSOR={}".format(arch),
"-DCMAKE_OSX_ARCHITECTURES={}".format(arch),
"-DCMAKE_SYSTEM_PROCESSOR={}".format(arch),
]

if platform == "macos":
self.cmake_args += ["-DCMAKE_SYSTEM_NAME=Darwin"]
elif platform == "iphoneos":
self.cmake_args += ["-DCMAKE_SYSTEM_NAME=iOS", "-DCMAKE_OSX_DEPLOYMENT_TARGET=9.3"]

if self.library_build_type == "shared":
self.output_name = "libdobby.dylib"
else:
self.output_name = "libdobby.a"
self.shared_output_name = "libdobby.dylib"
self.static_output_name = "libdobby.a"

@classmethod
def lipo_create_fat(cls, project_dir, platform, output_name):
Expand All @@ -169,7 +168,9 @@ def lipo_create_fat(cls, project_dir, platform, output_name):
file = f"{project_dir}/build/{platform}/{arch}/{output_name}"
files.append(file)

cmd = ["lipo", "-create"] + files + ["-output", f"{project_dir}/build/{platform}/{output_name}"]
fat_output_dir = f"{project_dir}/build/{platform}/universal"
subprocess.run(["mkdir", "-p", "{}".format(fat_output_dir)], check=True)
cmd = ["lipo", "-create"] + files + ["-output", f"{fat_output_dir}/{output_name}"]
subprocess.run(cmd, check=True)


Expand Down Expand Up @@ -218,6 +219,7 @@ def lipo_create_fat(cls, project_dir, platform, output_name):
archs.append(arch)
logging.info("build platform: {}, archs: {}".format(platform, archs))

builder: PlatformBuilder = None
for arch_ in archs:
if platform == "macos":
builder = DarwinPlatformBuilder(project_dir, library_build_type, platform, arch_)
Expand All @@ -231,10 +233,10 @@ def lipo_create_fat(cls, project_dir, platform, output_name):
logging.error("invalid platform {}".format(platform))
sys.exit(-1)
logging.info(
f"build platform: {platform}, arch: {arch_}, cmake_build_dir: {builder.cmake_build_dir}, output_dir: {builder.output_dir}"
f"build platform: {platform}, arch: {arch_}, cmake_build_dir: {builder.cmake_build_dir}, output_dir: {builder.output_dir}"
)
builder.build()

if platform in ["iphoneos", "macos"] and arch == "all":
output_name = "libdobby.dylib" if library_build_type == "shared" else "libdobby.a"
DarwinPlatformBuilder.lipo_create_fat(project_dir, platform, output_name)
DarwinPlatformBuilder.lipo_create_fat(project_dir, platform, builder.shared_output_name)
DarwinPlatformBuilder.lipo_create_fat(project_dir, platform, builder.static_output_name)
3 changes: 3 additions & 0 deletions source/dobby/pac_kit.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#pragma once

#include <stdint.h>

#if defined(__arm64e__) && __has_feature(ptrauth_calls)
#include <ptrauth.h>
#endif

static inline void *pac_strip(void *addr) {
if (addr == NULL) {
Expand Down

0 comments on commit 52cc028

Please sign in to comment.