Skip to content

Commit

Permalink
Merge pull request #1008 from Qwinci/dladdr1
Browse files Browse the repository at this point in the history
options/posix: Implement dladdr1
  • Loading branch information
Geertiebear authored Feb 24, 2024
2 parents dfc0aea + ffdd971 commit 147eb24
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
33 changes: 32 additions & 1 deletion options/posix/generic/dlfcn-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ struct __dlapi_symbol {
void *base;
const char *symbol;
void *address;
const void *elf_symbol;
void *link_map;
};

extern "C" const char *__dlapi_error();
Expand Down Expand Up @@ -45,6 +47,9 @@ void *dlvsym(void *__restrict handle, const char *__restrict string, const char
}

//gnu extensions

#if __MLIBC_GLIBC_OPTION

int dladdr(const void *ptr, Dl_info *out) {
__dlapi_symbol info;
if(__dlapi_reverse(ptr, &info))
Expand All @@ -57,8 +62,34 @@ int dladdr(const void *ptr, Dl_info *out) {
return 1;
}

int dlinfo(void *, int, void *) {
int dladdr1(const void *ptr, Dl_info *out, void **extra, int flags) {
__dlapi_symbol info;
if(__dlapi_reverse(ptr, &info)) {
return 0;
}

out->dli_fname = info.file;
out->dli_fbase = info.base;
out->dli_sname = info.symbol;
out->dli_saddr = info.address;

switch(flags) {
case RTLD_DL_SYMENT:
*const_cast<const void **>(extra) = info.elf_symbol;
break;
case RTLD_DL_LINKMAP:
*extra = info.link_map;
break;
default:
break;
}

return 1;
}

int dlinfo(void *__restrict, int, void *__restrict) {
__ensure(!"dlinfo() not implemented");
__builtin_unreachable();
}

#endif // __MLIBC_GLIBC_OPTION
14 changes: 12 additions & 2 deletions options/posix/include/dlfcn.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#ifndef _DLFCN_H
#define _DLFCN_H

#include <mlibc-config.h>

#define RTLD_LOCAL 0
#define RTLD_NOW 1
#define RTLD_GLOBAL 2
Expand All @@ -13,6 +15,9 @@
#define RTLD_NEXT ((void *)-1)
#define RTLD_DEFAULT ((void *)0)

#define RTLD_DL_SYMENT 1
#define RTLD_DL_LINKMAP 2

#define RTLD_DI_LINKMAP 2

#ifdef __cplusplus
Expand All @@ -29,6 +34,8 @@ void *dlvsym(void *__restrict, const char *__restrict, const char *__restrict);

#endif /* !__MLIBC_ABI_ONLY */

#if defined(_GNU_SOURCE) && __MLIBC_GLIBC_OPTION

//gnu extension
typedef struct {
const char *dli_fname;
Expand All @@ -39,11 +46,14 @@ typedef struct {

#ifndef __MLIBC_ABI_ONLY

int dladdr(const void *, Dl_info *);
int dlinfo(void *, int, void *);
int dladdr(const void *__ptr, Dl_info *__out);
int dladdr1(const void *__ptr, Dl_info *__out, void **__extra, int __flags);
int dlinfo(void *__restrict __handle, int __request, void *__restrict __info);

#endif /* !__MLIBC_ABI_ONLY */

#endif /* defined(_GNU_SOURCE) && __MLIBC_GLIBC_OPTION */

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions options/rtdl/generic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ struct __dlapi_symbol {
void *base;
const char *symbol;
void *address;
const void *elf_symbol;
void *link_map;
};

extern "C" [[ gnu::visibility("default") ]]
Expand Down Expand Up @@ -726,6 +728,8 @@ int __dlapi_reverse(const void *ptr, __dlapi_symbol *info) {
info->base = reinterpret_cast<void *>(object->baseAddress);
info->symbol = cand.getString();
info->address = reinterpret_cast<void *>(cand.virtualAddress());
info->elf_symbol = cand.symbol();
info->link_map = &object->linkMap;
return 0;
}
}
Expand All @@ -748,6 +752,8 @@ int __dlapi_reverse(const void *ptr, __dlapi_symbol *info) {
info->base = reinterpret_cast<void *>(object->baseAddress);
info->symbol = nullptr;
info->address = 0;
info->elf_symbol = nullptr;
info->link_map = &object->linkMap;
return 0;
}
}
Expand Down
5 changes: 1 addition & 4 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,9 @@ extra_cflags_test_cases = {

test_sources = []
test_link_args = []
test_c_args = []
test_c_args = ['-D_GNU_SOURCE']
use_pie = false

test_c_args = []
test_link_args = []

# Our ubsan implementation can't be used by the tests themselves,
# since it is internal to libc.so and ld.so.
test_override_options = ['b_sanitize=none']
Expand Down

0 comments on commit 147eb24

Please sign in to comment.