Skip to content

Commit

Permalink
Fix crash when parsing .symtab in .gnu_debugdata. Improve license for…
Browse files Browse the repository at this point in the history
…mat. Upgrade NDK to 23.1.
  • Loading branch information
caikelun committed Dec 9, 2021
1 parent 7b1c277 commit e327d28
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 58 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -61,7 +61,7 @@ android {
}
dependencies {
implementation 'io.hexhacking:xdl:1.1.1'
implementation 'io.hexhacking:xdl:1.1.2'
}
```

Expand Down
12 changes: 6 additions & 6 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 |


## 使用
Expand All @@ -61,7 +61,7 @@ android {
}
dependencies {
implementation 'io.hexhacking:xdl:1.1.1'
implementation 'io.hexhacking:xdl:1.1.2'
}
```

Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand All @@ -26,15 +26,15 @@ 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
dependencyOnLocalLibrary = true

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."
Expand Down
4 changes: 2 additions & 2 deletions xdl/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
4 changes: 2 additions & 2 deletions xdl/src/main/cpp/include/xdl.h
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
68 changes: 39 additions & 29 deletions xdl/src/main/cpp/xdl.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion xdl/src/main/cpp/xdl_iterate.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion xdl/src/main/cpp/xdl_iterate.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion xdl/src/main/cpp/xdl_linker.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion xdl/src/main/cpp/xdl_linker.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion xdl/src/main/cpp/xdl_lzma.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion xdl/src/main/cpp/xdl_lzma.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion xdl/src/main/cpp/xdl_util.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion xdl/src/main/cpp/xdl_util.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion xdl_sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down

0 comments on commit e327d28

Please sign in to comment.