M7350/system/extras/tests/bionic/libc/common/test_dlopen_null.c

38 lines
817 B
C
Raw Normal View History

2024-09-09 08:57:42 +00:00
#include <dlfcn.h>
#include <stddef.h>
#include <stdio.h>
extern int foo(void)
{
return 42;
}
int (*func_ptr)(void) = foo;
int main(void)
{
void* lib = dlopen(NULL, RTLD_NOW | RTLD_GLOBAL);
void* symbol;
#if 0
/* The Gold linker will garbage-collect unused global functions
* even if --Wl,--export-dynamic is used. So use a dummy global
* variable reference here to prevent this.
*/
if (foo() != 42)
return 3;
#endif
if (lib == NULL) {
fprintf(stderr, "Could not open self-executable with dlopen(NULL) !!: %s\n", dlerror());
return 1;
}
symbol = dlsym(lib, "foo");
if (symbol == NULL) {
fprintf(stderr, "Could not lookup symbol inside executable !!: %s\n", dlerror());
return 2;
}
dlclose(lib);
return 0;
}