Skip to content

Commit

Permalink
Optimize packaging and reduce module size
Browse files Browse the repository at this point in the history
  • Loading branch information
sanfengAndroid committed Jan 20, 2021
1 parent 87c9624 commit 9009d1a
Show file tree
Hide file tree
Showing 26 changed files with 2,939 additions and 192 deletions.
25 changes: 13 additions & 12 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ plugins {
id 'com.android.library'
}

def buildApi = rootProject.ext.buildApi
def buildApi = project.hasProperty("specialApi") ? specialApi: rootProject.ext.buildApi
def linkerModuleName = 'fake-linker'
def hookInstallModuleName = 'hook-install'
def mVersionCode = 1000
Expand All @@ -39,8 +39,8 @@ android {

externalNativeBuild {
cmake {
cppFlags ""
cFlags ""
cppFlags "-Werror"
cFlags "-Werror"
abiFilters 'armeabi-v7a', 'x86', 'arm64-v8a', 'x86_64'
arguments "-DANDROID_PLATFORM=${buildApi}", "-DLINKER_MODULE_NAME=${linkerModuleName}",
"-DHOOK_INSTALL_MODULE_NAME=${hookInstallModuleName}",
Expand All @@ -57,8 +57,15 @@ android {

buildTypes {
release {
minifyEnabled false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

externalNativeBuild {
cmake {
cppFlags "-fvisibility=hidden -fvisibility-inlines-hidden -Os -fomit-frame-pointer -fno-rtti -fno-exceptions"
cFlags "-fvisibility=hidden -fvisibility-inlines-hidden -Os -fomit-frame-pointer"
}
}
}
}
compileOptions {
Expand Down Expand Up @@ -88,20 +95,14 @@ tasks.whenTaskAdded { task ->
it.outputs.files.files.each { File dir ->
dir.eachFileRecurse {
if (it.name == "lib${linkerModuleName}-${buildApi}.so") {
println '找到当前正在编译的fake-linker模块版本,需要删除'
println 'delete current fake-linker version module'
it.delete()
}
}
}
}
}
}
dependencies {

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
testImplementation 'junit:junit:4.13.1'
implementation 'eu.chainfire:libsuperuser:1.0.0.201811281328'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
dependencies {
}
29 changes: 28 additions & 1 deletion proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,31 @@

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
#-renamesourcefileattribute SourceFile

-keep public class com.sanfengandroid.fakelinker.FakeLinker {
*;
}

-keep public class com.sanfengandroid.fakelinker.FileInstaller {
*;
}

-keep public class com.sanfengandroid.fakelinker.ErrorCode {
*;
}

-keep class eu.chainfire.libsuperuser.Shell {
public *;
}

-keep class eu.chainfire.libsuperuser.Debug {
public *;
}
-keep class eu.chainfire.libsuperuser.ShellNotClosedException {
*;
}

-keep class eu.chainfire.libsuperuser.ShellOnMainThreadException{
*;
}
Binary file modified src/main/assets/arm64-v8a/hook-install
Binary file not shown.
Binary file modified src/main/assets/armeabi-v7a/hook-install
Binary file not shown.
Binary file modified src/main/assets/x86/hook-install
Binary file not shown.
Binary file modified src/main/assets/x86_64/hook-install
Binary file not shown.
11 changes: 5 additions & 6 deletions src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ cmake_minimum_required(VERSION 3.4.1)
# Gradle automatically packages shared libraries with your APK.
string(TOLOWER ${CMAKE_BUILD_TYPE} THIS_BUILD_TYPE)

set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Os -Oz -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -Werror")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Os -Oz -fno-rtti -fno-exceptions -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -Werror")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Werror")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Werror")

if (${CMAKE_ANDROID_ARCH_ABI} STREQUAL "arm64-v8a" OR ${CMAKE_ANDROID_ARCH_ABI} STREQUAL "x86_64")
add_definitions(-DUSE_RELA)
else ()
Expand All @@ -32,7 +27,7 @@ include_directories(common)
include_directories(export)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../libs/${THIS_BUILD_TYPE}/${CMAKE_ANDROID_ARCH_ABI})
message(WARNING "当前版本:${ANDROID_PLATFORM},输出目录:${CMAKE_LIBRARY_OUTPUT_DIRECTORY}, 编译类型:${THIS_BUILD_TYPE},模块名:${LINKER_MODULE_NAME},安装模块${HOOK_INSTALL_MODULE_NAME}")
message(STATUS "current version:${ANDROID_PLATFORM}, output dir:${CMAKE_LIBRARY_OUTPUT_DIRECTORY}, build type:${THIS_BUILD_TYPE}, linker module name:${LINKER_MODULE_NAME}, install module name:${HOOK_INSTALL_MODULE_NAME}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/module_config.h.in" "${CMAKE_CURRENT_SOURCE_DIR}/common/module_config.h")

add_library( # Sets the name of the library.
Expand Down Expand Up @@ -97,6 +92,10 @@ set_target_properties(${LINKER_MODULE_NAME}-${ANDROID_PLATFORM} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}/../libs/${THIS_BUILD_TYPE}/${CMAKE_ANDROID_ARCH_ABI})

set_target_properties(${LINKER_MODULE_NAME}-${ANDROID_PLATFORM} PROPERTIES
LINK_FLAGS_RELEASE
"${LINK_FLAGS_RELEASE} -Wl,--gc-sections,-S,--version-script=\"${CMAKE_CURRENT_SOURCE_DIR}/symbol.map.txt\"")

target_link_libraries(
${HOOK_INSTALL_MODULE_NAME}
${log-lib})
Expand Down
6 changes: 3 additions & 3 deletions src/main/cpp/export/linker_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ enum SoinfoFunType {
kSFCallDlopen
};

//API_PUBLIC void *call_soinfo_function(SoinfoFunType fun_type, SoinfoParamType find_type, const void *find_param, SoinfoParamType param_type, const void *param, int *error_code);
//C_API API_PUBLIC void *call_soinfo_function(SoinfoFunType fun_type, SoinfoParamType find_type, const void *find_param, SoinfoParamType param_type, const void *param, int *error_code);

enum CommonFunType {
// 添加一个soinfo到全局库
Expand All @@ -164,7 +164,7 @@ enum CommonFunType {
kCFCallManualRelinks,
};

//API_PUBLIC void *call_common_function(CommonFunType fun_type, SoinfoParamType find_type, const void *find_param, SoinfoParamType param_type, const void *param, int *error_code);
//C_API API_PUBLIC void *call_common_function(CommonFunType fun_type, SoinfoParamType find_type, const void *find_param, SoinfoParamType param_type, const void *param, int *error_code);

#if __ANDROID_API__ >= __ANDROID_API_N__

Expand Down Expand Up @@ -231,7 +231,7 @@ enum NamespaceFunType {
* 调用相关命名空间函数
* @return 若返回值是多个集合,则都包装成VarLengthObject对象
* */
//API_PUBLIC void *
//C_API API_PUBLIC void *
//call_namespace_function(NamespaceFunType fun_type, NamespaceParamType find_type, const void *find_param, NamespaceParamType param_type, const void *param, int *error_code);

#endif
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/linker/fake_linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ static int ResolveInnerSymbolsAddress(const char *library_name, symbols_address
for (int i = 0; i < ret->len; ++i) {
if (ret->elements[i] != 0) {
ret->elements[i] += base;
LOGD("found symbol: %s, library: %s, address: %" PRIx64, symbols[i], library_name, ret->elements[i]);
LOGV("found symbol: %s, library: %s, address: %" PRIx64, symbols[i], library_name, ret->elements[i]);
}
}
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/linker/linker_common_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void Init() {
"__dl__ZL21g_namespace_allocator",
"__dl__ZL26g_namespace_list_allocator"));
if (symbols.data == nullptr) {
LOGD("find symbol g_soinfo_allocator/g_soinfo_links_allocator/g_namespace_allocator/g_namespace_list_allocator failed");
LOGE("find symbol g_soinfo_allocator/g_soinfo_links_allocator/g_namespace_allocator/g_namespace_list_allocator failed");
return;
}
g_soinfo_allocator = static_cast<LinkerTypeAllocator<struct soinfo> *>(GSIZE_TO_POINTER(symbols.data->elements[0]));
Expand Down
6 changes: 3 additions & 3 deletions src/main/cpp/linker/linker_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static soinfo *find_soinfo_by_type(SoinfoParamType type, const void *p, void *ca
return result;
}

API_PUBLIC void *call_soinfo_function(SoinfoFunType fun_type, SoinfoParamType find_type, const void *find_param,
C_API API_PUBLIC void *call_soinfo_function(SoinfoFunType fun_type, SoinfoParamType find_type, const void *find_param,
SoinfoParamType param_type, const void *param, int *error_code) {
void *result = nullptr;
soinfo *so;
Expand Down Expand Up @@ -178,7 +178,7 @@ API_PUBLIC void *call_soinfo_function(SoinfoFunType fun_type, SoinfoParamType fi
return result;
}

API_PUBLIC void *call_common_function(CommonFunType fun_type, SoinfoParamType find_type, const void *find_param,
C_API API_PUBLIC void *call_common_function(CommonFunType fun_type, SoinfoParamType find_type, const void *find_param,
SoinfoParamType param_type, const void *param, int *error_code) {
void *result = nullptr;
soinfo *so;
Expand Down Expand Up @@ -362,7 +362,7 @@ static soinfo *find_soinfo(NamespaceParamType type, const void *param, void *cal
return result;
}

API_PUBLIC void *call_namespace_function(NamespaceFunType fun_type, NamespaceParamType find_type, const void
C_API API_PUBLIC void *call_namespace_function(NamespaceFunType fun_type, NamespaceParamType find_type, const void
*find_param, NamespaceParamType param_type, const void *param, int *error_code) {
void *result = nullptr;
android_namespace_t *np = nullptr;
Expand Down
16 changes: 9 additions & 7 deletions src/main/cpp/linker/linker_globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ void *ProxyLinker::FindSymbolByDlsym(soinfo *si, const char *name) {
#else
result = CallDoDlsym(si, name);
#endif
LOGD("dlsym soinfo name: %s, symbol: %s, address: %p", si->get_soname() == nullptr ? "(null)" : si->get_soname(), name, result);
LOGV("dlsym soinfo name: %s, symbol: %s, address: %p", si->get_soname() == nullptr ? "(null)" : si->get_soname(), name, result);
return result;
}

Expand Down Expand Up @@ -724,6 +724,7 @@ bool ProxyLinker::ManualRelinkLibraries(soinfo *global, const VarLengthObject<co

if (rels.data == nullptr) {
LOGW("Function symbols not exported by the global library : %s", global->get_soname() == nullptr ? "(null)" : global->get_soname());
return false;
}
bool success = true;
for (int i = 0; i < vars->len; ++i) {
Expand Down Expand Up @@ -766,7 +767,9 @@ bool ProxyLinker::ManualRelinkLibrary(symbol_relocations *rels, soinfo *child) {
return child->again_process_relocation(rels);
}


/*
* 调用系统重定位会出现各种问题,废弃使用
* */
bool ProxyLinker::SystemRelinkLibrary(soinfo *so) {
bool success;
if (so == nullptr) {
Expand All @@ -780,7 +783,6 @@ bool ProxyLinker::SystemRelinkLibrary(soinfo *so) {
linker_block_protect_all(PROT_READ | PROT_WRITE);
so->set_unlinked();
std::string str = soinfo_to_string(so);
LOGD("system relink soinfo: %s", str.c_str());
// 重新dlopen出错,因为目前进程已经存在该so就不会在走ElfRead
// 5.0查找会修改linker的数据段,因此还要解保护linker
MapsUtil util(so->get_realpath());
Expand All @@ -797,7 +799,7 @@ bool ProxyLinker::SystemRelinkLibrary(soinfo *so) {
so->set_linked();
}
util.RecoveryPageProtect();
LOGD("The system relink library: %s, result: %s", so->get_soname(), success ? "true" : "false");
LOGV("The system relink library: %s, result: %s", so->get_soname(), success ? "true" : "false");
linker_block_protect_all(PROT_READ);
return success;
}
Expand Down Expand Up @@ -868,14 +870,14 @@ void ProxyLinker::Init() {

CHECK(g_default_namespace_ptr);
CHECK(g_soinfo_handles_map_ptr);
LOGD("find linker soinfo: %p, g_ld_debug_verbosity:%p, g_default_namespace: %p, g_soinfo_handles_map: %p, link_image: %p",
LOGV("find linker soinfo: %p, g_ld_debug_verbosity:%p, g_default_namespace: %p, g_soinfo_handles_map: %p, link_image: %p",
solist_ptr, g_ld_debug_verbosity_ptr, g_default_namespace_ptr, g_soinfo_handles_map_ptr, link_image_ptr);

#elif __ANDROID_API__ == __ANDROID_API_M__
#else
g_ld_preloads_ptr = (soinfo **) symbols.data->elements[5];
CHECK(g_ld_preloads_ptr);
LOGD("find soinfo: %p, g_ld_preloads: %p, link_image: %p", solist_ptr, g_ld_preloads_ptr, link_image_ptr);
LOGV("find soinfo: %p, g_ld_preloads: %p, link_image: %p", solist_ptr, g_ld_preloads_ptr, link_image_ptr);
#endif

#if __ANDROID_API__ >= __ANDROID_API_O__
Expand All @@ -886,7 +888,7 @@ void ProxyLinker::Init() {
dlsym_ptr = reinterpret_cast<void *(*)(void *, const char *, const void *)>(linker_soinfo_ptr->find_export_symbol_address("__loader_dlsym"));
create_namespace_ptr = reinterpret_cast<android_namespace_t *(*)(const char *, const char *, const char *, uint64_t, const char *, android_namespace_t *, const void *)>
(linker_soinfo_ptr->find_export_symbol_address("__loader_android_create_namespace"));
LOGD("linker __loader_android_dlopen_ext: %p, __loader_dlsym: %p, __loader_android_create_namespace: %p", dlopen_ptr, dlsym_ptr, create_namespace_ptr);
LOGV("linker __loader_android_dlopen_ext: %p, __loader_dlsym: %p, __loader_android_create_namespace: %p", dlopen_ptr, dlsym_ptr, create_namespace_ptr);
CHECK(create_namespace_ptr);
#else
linker_soinfo_ptr = ProxyLinker::FindSoinfoByName("libdl.so");
Expand Down
8 changes: 4 additions & 4 deletions src/main/cpp/linker/linker_soinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,20 +601,20 @@ static bool process_relocation(soinfo *so, const rel_t &reloc, symbol_relocation
if (Mode == RelocMode::JumpTable) {
if (r_type == R_GENERIC_JUMP_SLOT) {
*static_cast<ElfW(Addr) *>(rel_target) = sym_addr + get_addend_norel();
LOGD("Relocation symbol JumpTable: %s, original address: %p, new address: %p", sym_name, reinterpret_cast<void *>(orig),
LOGV("Relocation symbol JumpTable: %s, original address: %p, new address: %p", sym_name, reinterpret_cast<void *>(orig),
reinterpret_cast<void *>(*static_cast<ElfW(Addr) *>(rel_target)));
return true;
}
}
if (Mode == RelocMode::Typical) {
if (r_type == R_GENERIC_ABSOLUTE) {
*static_cast<ElfW(Addr) *>(rel_target) = sym_addr + get_addend_rel();
LOGD("Relocation symbol Typical ABSOLUTE: %s, original address: %16p, new address: %16p", sym_name, reinterpret_cast<void *>(orig),
LOGV("Relocation symbol Typical ABSOLUTE: %s, original address: %16p, new address: %16p", sym_name, reinterpret_cast<void *>(orig),
reinterpret_cast<void *>(*static_cast<ElfW(Addr) *>(rel_target)));
return true;
} else if (r_type == R_GENERIC_GLOB_DAT) {
*static_cast<ElfW(Addr) *>(rel_target) = sym_addr + get_addend_norel();
LOGD("Relocation symbol Typical GLOB_DAT: %s, original address: %16p, new address: %16p", sym_name, reinterpret_cast<void *>(orig),
LOGV("Relocation symbol Typical GLOB_DAT: %s, original address: %16p, new address: %16p", sym_name, reinterpret_cast<void *>(orig),
reinterpret_cast<void *>(*static_cast<ElfW(Addr) *>(rel_target)));
return true;
}
Expand Down Expand Up @@ -739,7 +739,7 @@ bool soinfo::again_process_relocation(symbol_relocations *rels) {
LOGE("cannot change soinfo: %s memory protect", get_soname() == nullptr ? "(null)" : get_soname());
return false;
}
LOGD("again relocation library: %s", get_soname());
LOGV("again relocation library: %s", get_soname());
#if defined(USE_RELA)
if (rela_ != nullptr) {
plain_relocate_impl<RelocMode::Typical>(this, rela_, rela_count_, rels);
Expand Down
Loading

0 comments on commit 9009d1a

Please sign in to comment.