Skip to content

Commit

Permalink
Add support for uClibc
Browse files Browse the repository at this point in the history
uClibc doesn't implement the dlinfo() and dladdr1() dl functions.
The existing Android solution fits for the uClibc case as well.
  • Loading branch information
Idan Freiberg committed Mar 8, 2020
1 parent 81f5583 commit 1750e6d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
18 changes: 11 additions & 7 deletions plthook_elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
#include <link.h>
#include "plthook.h"

#if defined __UCLIBC__ && !defined RTLD_NOLOAD
#define RTLD_NOLOAD 0
#endif

#ifndef __GNUC__
#define __attribute__(arg)
#endif
Expand Down Expand Up @@ -200,7 +204,7 @@ static int check_elf_header(const Elf_Ehdr *ehdr);
#endif
static void set_errmsg(const char *fmt, ...) __attribute__((__format__ (__printf__, 1, 2)));

#if defined __ANDROID__
#if defined __ANDROID__ || defined __UCLIBC__
struct dl_iterate_data {
char* addr;
struct link_map lmap;
Expand Down Expand Up @@ -245,7 +249,7 @@ int plthook_open(plthook_t **plthook_out, const char *filename)

int plthook_open_by_handle(plthook_t **plthook_out, void *hndl)
{
#if defined __ANDROID__
#if defined __ANDROID__ || defined __UCLIBC__
char *addr;

if (hndl == NULL) {
Expand Down Expand Up @@ -276,7 +280,7 @@ int plthook_open_by_address(plthook_t **plthook_out, void *address)
{
#if defined __FreeBSD__
return PLTHOOK_NOT_IMPLEMENTED;
#elif defined __ANDROID__
#elif defined __ANDROID__ || defined __UCLIBC__
struct dl_iterate_data data = {0,};
data.addr = address;
dl_iterate_phdr(dl_iterate_cb, &data);
Expand All @@ -300,7 +304,7 @@ int plthook_open_by_address(plthook_t **plthook_out, void *address)

static int plthook_open_executable(plthook_t **plthook_out)
{
#if defined __ANDROID__
#if defined __ANDROID__ || defined __UCLIBC__
return plthook_open_shared_library(plthook_out, NULL);
#elif defined __linux__
return plthook_open_real(plthook_out, _r_debug.r_map);
Expand Down Expand Up @@ -339,7 +343,7 @@ static int plthook_open_executable(plthook_t **plthook_out)
static int plthook_open_shared_library(plthook_t **plthook_out, const char *filename)
{
void *hndl = dlopen(filename, RTLD_LAZY | RTLD_NOLOAD);
#ifdef __ANDROID__
#if defined __ANDROID__ || defined __UCLIBC__
int rv;
#else
struct link_map *lmap = NULL;
Expand All @@ -349,7 +353,7 @@ static int plthook_open_shared_library(plthook_t **plthook_out, const char *file
set_errmsg("dlopen error: %s", dlerror());
return PLTHOOK_FILE_NOT_FOUND;
}
#ifdef __ANDROID__
#if defined __ANDROID__ || defined __UCLIBC__
rv = plthook_open_by_handle(plthook_out, hndl);
dlclose(hndl);
return rv;
Expand Down Expand Up @@ -541,7 +545,7 @@ static int plthook_open_real(plthook_t **plthook_out, struct link_map *lmap)

#if defined __linux__
plthook.plt_addr_base = (char*)lmap->l_addr;
#if defined __ANDROID__
#if defined __ANDROID__ || defined __UCLIBC__
dyn_addr_base = (const char*)lmap->l_addr;
#endif
#elif defined __FreeBSD__ || defined __sun
Expand Down
5 changes: 5 additions & 0 deletions test/testprog.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <plthook.h>
#include <features.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Expand All @@ -10,6 +11,10 @@
#include <dlfcn.h>
#endif

#if defined __UCLIBC__ && !defined RTLD_NOLOAD
#define RTLD_NOLOAD 0
#endif

#define CHK_PH(func) do { \
if (func != 0) { \
fprintf(stderr, "%s error: %s\n", #func, plthook_error()); \
Expand Down

0 comments on commit 1750e6d

Please sign in to comment.