From e327d288e0caf1a8c296d7f67e64408893e8796d Mon Sep 17 00:00:00 2001 From: CAI Kelun Date: Thu, 9 Dec 2021 20:59:04 +0800 Subject: [PATCH] Fix crash when parsing .symtab in .gnu_debugdata. Improve license format. Upgrade NDK to 23.1. --- LICENSE | 2 +- README.md | 12 +++--- README.zh-CN.md | 12 +++--- build.gradle | 6 +-- xdl/src/main/cpp/CMakeLists.txt | 4 +- xdl/src/main/cpp/include/xdl.h | 4 +- xdl/src/main/cpp/xdl.c | 68 +++++++++++++++++++-------------- xdl/src/main/cpp/xdl_iterate.c | 2 +- xdl/src/main/cpp/xdl_iterate.h | 2 +- xdl/src/main/cpp/xdl_linker.c | 2 +- xdl/src/main/cpp/xdl_linker.h | 2 +- xdl/src/main/cpp/xdl_lzma.c | 2 +- xdl/src/main/cpp/xdl_lzma.h | 2 +- xdl/src/main/cpp/xdl_util.c | 2 +- xdl/src/main/cpp/xdl_util.h | 2 +- xdl_sample/build.gradle | 2 +- 16 files changed, 68 insertions(+), 58 deletions(-) diff --git a/LICENSE b/LICENSE index 9d8b7e6..ea858f5 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020-present, HexHacking Team. All rights reserved. +Copyright (c) 2020-2021 HexHacking Team Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index bbcf275..167dc57 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat) ![](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat) -![](https://img.shields.io/badge/release-1.1.1-red.svg?style=flat) +![](https://img.shields.io/badge/release-1.1.2-red.svg?style=flat) ![](https://img.shields.io/badge/Android-4.1%20--%2012-blue.svg?style=flat) ![](https://img.shields.io/badge/arch-armeabi--v7a%20%7C%20arm64--v8a%20%7C%20x86%20%7C%20x86__64-blue.svg?style=flat) @@ -33,10 +33,10 @@ If xDL is compiled into an independent dynamic library: | ABI | Compressed (KB) | Uncompressed (KB) | | :---------- | --------------: | ----------------: | -| armeabi-v7a | 6.8 | 12.0 | -| arm64-v8a | 7.6 | 16.2 | -| x86 | 7.8 | 15.1 | -| x86_64 | 7.8 | 16.6 | +| armeabi-v7a | 7.0 | 13 | +| arm64-v8a | 7.7 | 17 | +| x86 | 7.9 | 16 | +| x86_64 | 7.9 | 17 | ## Usage @@ -61,7 +61,7 @@ android { } dependencies { - implementation 'io.hexhacking:xdl:1.1.1' + implementation 'io.hexhacking:xdl:1.1.2' } ``` diff --git a/README.zh-CN.md b/README.zh-CN.md index 2eec56c..95e14a7 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -2,7 +2,7 @@ ![](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat) ![](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat) -![](https://img.shields.io/badge/release-1.1.1-red.svg?style=flat) +![](https://img.shields.io/badge/release-1.1.2-red.svg?style=flat) ![](https://img.shields.io/badge/Android-4.1%20--%2012-blue.svg?style=flat) ![](https://img.shields.io/badge/arch-armeabi--v7a%20%7C%20arm64--v8a%20%7C%20x86%20%7C%20x86__64-blue.svg?style=flat) @@ -33,10 +33,10 @@ xDL 是 Android DL 系列函数的增强实现。 | ABI | 压缩后 (KB) | 未压缩 (KB) | | :---------- | ---------: | ---------: | -| armeabi-v7a | 6.8 | 12.0 | -| arm64-v8a | 7.6 | 16.2 | -| x86 | 7.8 | 15.1 | -| x86_64 | 7.8 | 16.6 | +| armeabi-v7a | 7.0 | 13 | +| arm64-v8a | 7.7 | 17 | +| x86 | 7.9 | 16 | +| x86_64 | 7.9 | 17 | ## 使用 @@ -61,7 +61,7 @@ android { } dependencies { - implementation 'io.hexhacking:xdl:1.1.1' + implementation 'io.hexhacking:xdl:1.1.2' } ``` diff --git a/build.gradle b/build.gradle index 74ef598..6f4c9bb 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:7.0.0' + classpath 'com.android.tools.build:gradle:7.0.3' } } @@ -26,7 +26,7 @@ ext { targetSdkVersion = 31 buildToolsVersion = "31.0.0" javaVersion = JavaVersion.VERSION_1_7 - ndkVersion = "22.1.7171670" + ndkVersion = "23.1.7779620" cmakeVersion = "3.18.1" abiFilters = "armeabi-v7a,arm64-v8a,x86,x86_64" useASAN = false @@ -34,7 +34,7 @@ ext { POM_GROUP_ID = "io.hexhacking" POM_ARTIFACT_ID = "xdl" - POM_VERSION_NAME = "1.1.1" + POM_VERSION_NAME = "1.1.2" POM_NAME = "xDL Android Lib" POM_DESCRIPTION = "xDL is an enhanced implementation of the Android DL series functions." diff --git a/xdl/src/main/cpp/CMakeLists.txt b/xdl/src/main/cpp/CMakeLists.txt index 37e03a8..ee0921b 100644 --- a/xdl/src/main/cpp/CMakeLists.txt +++ b/xdl/src/main/cpp/CMakeLists.txt @@ -12,6 +12,6 @@ if(USEASAN) target_compile_options(xdl PUBLIC -fsanitize=address -fno-omit-frame-pointer) target_link_options(xdl PUBLIC -fsanitize=address) else() -target_compile_options(xdl PUBLIC -Oz -flto -ffunction-sections -fdata-sections) -target_link_options(xdl PUBLIC -O3 -flto -Wl,--exclude-libs,ALL -Wl,--gc-sections -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/xdl.map.txt) +target_compile_options(xdl PUBLIC -Oz -flto -faddrsig -ffunction-sections -fdata-sections) +target_link_options(xdl PUBLIC -Oz -flto -Wl,--icf=all -Wl,-mllvm,--enable-machine-outliner=always -Wl,--exclude-libs,ALL -Wl,--gc-sections -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/xdl.map.txt) endif() diff --git a/xdl/src/main/cpp/include/xdl.h b/xdl/src/main/cpp/include/xdl.h index 418d7c5..3fa24c0 100644 --- a/xdl/src/main/cpp/include/xdl.h +++ b/xdl/src/main/cpp/include/xdl.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020-present, HexHacking Team. All rights reserved. +// Copyright (c) 2020-2021 HexHacking Team // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -22,7 +22,7 @@ // Created by caikelun on 2020-10-04. // -// xDL version: 1.1.1 +// xDL version: 1.1.2 // // xDL is an enhanced implementation of the Android DL series functions. // For more information, documentation, and the latest version please check: diff --git a/xdl/src/main/cpp/xdl.c b/xdl/src/main/cpp/xdl.c index 834a6f4..f49a9dd 100644 --- a/xdl/src/main/cpp/xdl.c +++ b/xdl/src/main/cpp/xdl.c @@ -1,4 +1,4 @@ -// Copyright (c) 2020-present, HexHacking Team. All rights reserved. +// Copyright (c) 2020-2021 HexHacking Team // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -98,8 +98,6 @@ typedef struct xdl bool symtab_try_load; uintptr_t base; - void *debugdata; // decompressed .gnu_debugdata - ElfW(Sym) *symtab; // .symtab size_t symtab_cnt; char *strtab; // .strtab @@ -195,7 +193,25 @@ static void *xdl_read_file_to_heap_by_section(int file_fd, size_t file_sz, ElfW( return xdl_read_file_to_heap(file_fd, file_sz, (size_t)shdr->sh_offset, shdr->sh_size); } -static void *xdl_read_memory(void *mem, size_t mem_sz, size_t data_offset, size_t data_len) +static void *xdl_read_memory_to_heap(void *mem, size_t mem_sz, size_t data_offset, size_t data_len) +{ + if(0 == data_len) return NULL; + if(data_offset >= mem_sz) return NULL; + if(data_offset + data_len > mem_sz) return NULL; + + void *data = malloc(data_len); + if(NULL == data) return NULL; + + memcpy(data, (void *)((uintptr_t)mem + data_offset), data_len); + return data; +} + +static void *xdl_read_memory_to_heap_by_section(void *mem, size_t mem_sz, ElfW(Shdr) *shdr) +{ + return xdl_read_memory_to_heap(mem, mem_sz, (size_t)shdr->sh_offset, shdr->sh_size); +} + +static void *xdl_get_memory(void *mem, size_t mem_sz, size_t data_offset, size_t data_len) { if(0 == data_len) return NULL; if(data_offset >= mem_sz) return NULL; @@ -204,14 +220,16 @@ static void *xdl_read_memory(void *mem, size_t mem_sz, size_t data_offset, size_ return (void *)((uintptr_t)mem + data_offset); } -static void *xdl_read_memory_by_section(void *mem, size_t mem_sz, ElfW(Shdr) *shdr) +static void *xdl_get_memory_by_section(void *mem, size_t mem_sz, ElfW(Shdr) *shdr) { - return xdl_read_memory(mem, mem_sz, (size_t)shdr->sh_offset, shdr->sh_size); + return xdl_get_memory(mem, mem_sz, (size_t)shdr->sh_offset, shdr->sh_size); } // load from disk and memory static int xdl_symtab_load_from_debugdata(xdl_t *self, int file_fd, size_t file_sz, ElfW(Shdr) *shdr_debugdata) { + void *debugdata = NULL; + ElfW(Shdr) *shdrs = NULL; int r = -1; // get zipped .gnu_debugdata @@ -220,19 +238,19 @@ static int xdl_symtab_load_from_debugdata(xdl_t *self, int file_fd, size_t file_ // get unzipped .gnu_debugdata size_t debugdata_sz; - if(0 != xdl_lzma_decompress(debugdata_zip, shdr_debugdata->sh_size, (uint8_t **)(&(self->debugdata)), &debugdata_sz)) goto end; + if(0 != xdl_lzma_decompress(debugdata_zip, shdr_debugdata->sh_size, (uint8_t **)&debugdata, &debugdata_sz)) goto end; // get ELF header - ElfW(Ehdr) *ehdr = (ElfW(Ehdr) *)self->debugdata; + ElfW(Ehdr) *ehdr = (ElfW(Ehdr) *)debugdata; if(0 == ehdr->e_shnum || ehdr->e_shentsize != sizeof(ElfW(Shdr))) goto end; // get section headers - ElfW(Shdr) *shdrs = (ElfW(Shdr) *)xdl_read_memory(self->debugdata, debugdata_sz, (size_t)ehdr->e_shoff, ehdr->e_shentsize * ehdr->e_shnum); + shdrs = (ElfW(Shdr) *)xdl_read_memory_to_heap(debugdata, debugdata_sz, (size_t)ehdr->e_shoff, ehdr->e_shentsize * ehdr->e_shnum); if(NULL == shdrs) goto end; // get .shstrtab if(SHN_UNDEF == ehdr->e_shstrndx || ehdr->e_shstrndx >= ehdr->e_shnum) goto end; - char *shstrtab = (char *)xdl_read_memory_by_section(self->debugdata, debugdata_sz, shdrs + ehdr->e_shstrndx); + char *shstrtab = (char *)xdl_get_memory_by_section(debugdata, debugdata_sz, shdrs + ehdr->e_shstrndx); if(NULL == shstrtab) goto end; // find .symtab & .strtab @@ -248,10 +266,14 @@ static int xdl_symtab_load_from_debugdata(xdl_t *self, int file_fd, size_t file_ if(SHT_STRTAB != shdr_strtab->sh_type) continue; // get .symtab & .strtab - ElfW(Sym) *symtab = (ElfW(Sym) *)xdl_read_memory_by_section(self->debugdata, debugdata_sz, shdr); + ElfW(Sym) *symtab = (ElfW(Sym) *)xdl_read_memory_to_heap_by_section(debugdata, debugdata_sz, shdr); if(NULL == symtab) continue; - char *strtab = (char *)xdl_read_memory_by_section(self->debugdata, debugdata_sz, shdr_strtab); - if(NULL == strtab) continue; + char *strtab = (char *)xdl_read_memory_to_heap_by_section(debugdata, debugdata_sz, shdr_strtab); + if(NULL == strtab) + { + free(symtab); + continue; + } // OK self->symtab = symtab; @@ -265,11 +287,8 @@ static int xdl_symtab_load_from_debugdata(xdl_t *self, int file_fd, size_t file_ end: free(debugdata_zip); - if(0 != r && NULL != self->debugdata) - { - free(self->debugdata); - self->debugdata = NULL; - } + if(NULL != debugdata) free(debugdata); + if(NULL != shdrs) free(shdrs); return r; } @@ -549,17 +568,8 @@ void *xdl_close(void *handle) xdl_t *self = (xdl_t *)handle; if(NULL != self->pathname) free(self->pathname); - if(NULL != self->debugdata) - { - // free unzipped .gnu_debugdata - // self->symtab and self->strtab points to self->debugdata - free(self->debugdata); - } - else - { - if(NULL != self->symtab) free(self->symtab); - if(NULL != self->strtab) free(self->strtab); - } + if(NULL != self->symtab) free(self->symtab); + if(NULL != self->strtab) free(self->strtab); void *linker_handle = self->linker_handle; free(self); diff --git a/xdl/src/main/cpp/xdl_iterate.c b/xdl/src/main/cpp/xdl_iterate.c index 67fe437..e71acfd 100644 --- a/xdl/src/main/cpp/xdl_iterate.c +++ b/xdl/src/main/cpp/xdl_iterate.c @@ -1,4 +1,4 @@ -// Copyright (c) 2020-present, HexHacking Team. All rights reserved. +// Copyright (c) 2020-2021 HexHacking Team // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/xdl/src/main/cpp/xdl_iterate.h b/xdl/src/main/cpp/xdl_iterate.h index 28cb9ee..e04e699 100644 --- a/xdl/src/main/cpp/xdl_iterate.h +++ b/xdl/src/main/cpp/xdl_iterate.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020-present, HexHacking Team. All rights reserved. +// Copyright (c) 2020-2021 HexHacking Team // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/xdl/src/main/cpp/xdl_linker.c b/xdl/src/main/cpp/xdl_linker.c index 0c11bc1..f90ff11 100644 --- a/xdl/src/main/cpp/xdl_linker.c +++ b/xdl/src/main/cpp/xdl_linker.c @@ -1,4 +1,4 @@ -// Copyright (c) 2020-present, HexHacking Team. All rights reserved. +// Copyright (c) 2020-2021 HexHacking Team // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/xdl/src/main/cpp/xdl_linker.h b/xdl/src/main/cpp/xdl_linker.h index a6eb7a8..4067de4 100644 --- a/xdl/src/main/cpp/xdl_linker.h +++ b/xdl/src/main/cpp/xdl_linker.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020-present, HexHacking Team. All rights reserved. +// Copyright (c) 2020-2021 HexHacking Team // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/xdl/src/main/cpp/xdl_lzma.c b/xdl/src/main/cpp/xdl_lzma.c index 3608f5b..29072f6 100644 --- a/xdl/src/main/cpp/xdl_lzma.c +++ b/xdl/src/main/cpp/xdl_lzma.c @@ -1,4 +1,4 @@ -// Copyright (c) 2020-present, HexHacking Team. All rights reserved. +// Copyright (c) 2020-2021 HexHacking Team // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/xdl/src/main/cpp/xdl_lzma.h b/xdl/src/main/cpp/xdl_lzma.h index 58d4dc5..bd12ac0 100644 --- a/xdl/src/main/cpp/xdl_lzma.h +++ b/xdl/src/main/cpp/xdl_lzma.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020-present, HexHacking Team. All rights reserved. +// Copyright (c) 2020-2021 HexHacking Team // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/xdl/src/main/cpp/xdl_util.c b/xdl/src/main/cpp/xdl_util.c index b1e6b4d..cee54ee 100644 --- a/xdl/src/main/cpp/xdl_util.c +++ b/xdl/src/main/cpp/xdl_util.c @@ -1,4 +1,4 @@ -// Copyright (c) 2020-present, HexHacking Team. All rights reserved. +// Copyright (c) 2020-2021 HexHacking Team // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/xdl/src/main/cpp/xdl_util.h b/xdl/src/main/cpp/xdl_util.h index 79c2e0a..26b3cb2 100644 --- a/xdl/src/main/cpp/xdl_util.h +++ b/xdl/src/main/cpp/xdl_util.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020-present, HexHacking Team. All rights reserved. +// Copyright (c) 2020-2021 HexHacking Team // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/xdl_sample/build.gradle b/xdl_sample/build.gradle index 9d8f965..4e42daf 100644 --- a/xdl_sample/build.gradle +++ b/xdl_sample/build.gradle @@ -66,7 +66,7 @@ dependencies { if (rootProject.ext.dependencyOnLocalLibrary) { implementation project(':xdl') } else { - implementation 'io.hexhacking:xdl:1.1.1' + implementation 'io.hexhacking:xdl:1.1.2' } }