Skip to content

Commit

Permalink
Fix bug where xdl_dsym could fail in Android 5.x.
Browse files Browse the repository at this point in the history
  • Loading branch information
caikelun committed Mar 21, 2022
1 parent 4770330 commit 3865f1d
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 22 deletions.
4 changes: 2 additions & 2 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.2-red.svg?style=flat)
![](https://img.shields.io/badge/release-1.1.3-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 @@ -61,7 +61,7 @@ android {
}
dependencies {
implementation 'io.hexhacking:xdl:1.1.2'
implementation 'io.hexhacking:xdl:1.1.3'
}
```

Expand Down
4 changes: 2 additions & 2 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.2-red.svg?style=flat)
![](https://img.shields.io/badge/release-1.1.3-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 @@ -61,7 +61,7 @@ android {
}
dependencies {
implementation 'io.hexhacking:xdl:1.1.2'
implementation 'io.hexhacking:xdl:1.1.3'
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ext {

POM_GROUP_ID = "io.hexhacking"
POM_ARTIFACT_ID = "xdl"
POM_VERSION_NAME = "1.1.2"
POM_VERSION_NAME = "1.1.3"

POM_NAME = "xDL Android Lib"
POM_DESCRIPTION = "xDL is an enhanced implementation of the Android DL series functions."
Expand Down
2 changes: 1 addition & 1 deletion xdl/src/main/cpp/include/xdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// Created by caikelun on 2020-10-04.

//
// xDL version: 1.1.2
// xDL version: 1.1.3
//
// xDL is an enhanced implementation of the Android DL series functions.
// For more information, documentation, and the latest version please check:
Expand Down
24 changes: 23 additions & 1 deletion xdl/src/main/cpp/xdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
#include "xdl_lzma.h"
#include "xdl_util.h"

#ifndef __LP64__
#define XDL_LIB_PATH "/system/lib"
#else
#define XDL_LIB_PATH "/system/lib64"
#endif

#define XDL_DYNSYM_IS_EXPORT_SYM(shndx) (SHN_UNDEF != (shndx))
#define XDL_SYMTAB_IS_EXPORT_SYM(shndx) \
(SHN_UNDEF != (shndx) && !((shndx) >= SHN_LORESERVE && (shndx) <= SHN_HIRESERVE))
Expand Down Expand Up @@ -282,6 +288,8 @@ static int xdl_symtab_load_from_debugdata(xdl_t *self, int file_fd, size_t file_

// load from disk and memory
static int xdl_symtab_load(xdl_t *self) {
if ('[' == self->pathname[0]) return -1;

int r = -1;
ElfW(Shdr) *shdrs = NULL;
char *shstrtab = NULL;
Expand All @@ -298,7 +306,21 @@ static int xdl_symtab_load(xdl_t *self) {
self->base = self->load_bias + vaddr_min;

// open file
int file_fd = open(self->pathname, O_RDONLY | O_CLOEXEC);
int flags = O_RDONLY | O_CLOEXEC;
int file_fd;
if ('/' == self->pathname[0]) {
file_fd = open(self->pathname, flags);
} else {
char full_pathname[1024];
// try the fast method
snprintf(full_pathname, sizeof(full_pathname), "%s/%s", XDL_LIB_PATH, self->pathname);
file_fd = open(full_pathname, flags);
if (file_fd < 0) {
// try the slow method
if (0 != xdl_iterate_get_full_pathname(self->base, full_pathname, sizeof(full_pathname))) return -1;
file_fd = open(full_pathname, flags);
}
}
if (file_fd < 0) return -1;
struct stat st;
if (0 != fstat(file_fd, &st)) goto end;
Expand Down
33 changes: 19 additions & 14 deletions xdl/src/main/cpp/xdl_iterate.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,9 @@ static int xdl_iterate_open_or_rewind_maps(FILE **maps) {
return 0;
}

static uintptr_t xdl_iterate_get_pathname_from_maps(struct dl_phdr_info *info, char *buf, size_t buf_len,
FILE **maps) {
// get base address
uintptr_t min_vaddr = xdl_iterate_get_min_vaddr(info);
if (UINTPTR_MAX == min_vaddr) return 0; // failed
uintptr_t base = (uintptr_t)(info->dlpi_addr + min_vaddr);

static int xdl_iterate_get_pathname_from_maps(uintptr_t base, char *buf, size_t buf_len, FILE **maps) {
// open or rewind maps-file
if (0 != xdl_iterate_open_or_rewind_maps(maps)) return 0; // failed
if (0 != xdl_iterate_open_or_rewind_maps(maps)) return -1; // failed

char line[1024];
while (fgets(line, sizeof(line), *maps)) {
Expand All @@ -106,10 +100,10 @@ static uintptr_t xdl_iterate_get_pathname_from_maps(struct dl_phdr_info *info, c

// found it
strlcpy(buf, pathname, buf_len);
return (uintptr_t)buf; // OK
return 0; // OK
}

return 0; // failed
return -1; // failed
}

static int xdl_iterate_by_linker_cb(struct dl_phdr_info *info, size_t size, void *arg) {
Expand Down Expand Up @@ -142,11 +136,15 @@ static int xdl_iterate_by_linker_cb(struct dl_phdr_info *info, size_t size, void

// fix dlpi_name (from /proc/self/maps)
if ('/' != info->dlpi_name[0] && '[' != info->dlpi_name[0] && (0 != (flags & XDL_FULL_PATHNAME))) {
char buf[512];
uintptr_t pathname = xdl_iterate_get_pathname_from_maps(info, buf, sizeof(buf), maps);
if (0 == pathname) return 0; // ignore this ELF
// get base address
uintptr_t min_vaddr = xdl_iterate_get_min_vaddr(info);
if (UINTPTR_MAX == min_vaddr) return 0; // ignore this ELF
uintptr_t base = (uintptr_t)(info->dlpi_addr + min_vaddr);

char buf[1024];
if (0 != xdl_iterate_get_pathname_from_maps(base, buf, sizeof(buf), maps)) return 0; // ignore this ELF

info->dlpi_name = (const char *)pathname;
info->dlpi_name = (const char *)buf;
}

// callback
Expand Down Expand Up @@ -245,3 +243,10 @@ int xdl_iterate_phdr_impl(xdl_iterate_phdr_cb_t cb, void *cb_arg, int flags) {
// iterate by dl_iterate_phdr()
return xdl_iterate_by_linker(cb, cb_arg, flags);
}

int xdl_iterate_get_full_pathname(uintptr_t base, char *buf, size_t buf_len) {
FILE *maps = NULL;
int r = xdl_iterate_get_pathname_from_maps(base, buf, buf_len, &maps);
if (NULL != maps) fclose(maps);
return r;
}
2 changes: 2 additions & 0 deletions xdl/src/main/cpp/xdl_iterate.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ extern "C" {
typedef int (*xdl_iterate_phdr_cb_t)(struct dl_phdr_info *info, size_t size, void *arg);
int xdl_iterate_phdr_impl(xdl_iterate_phdr_cb_t cb, void *cb_arg, int flags);

int xdl_iterate_get_full_pathname(uintptr_t base, char *buf, size_t buf_len);

#ifdef __cplusplus
}
#endif
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.2'
implementation 'io.hexhacking:xdl:1.1.3'
}
}

Expand Down

0 comments on commit 3865f1d

Please sign in to comment.