summaryrefslogtreecommitdiff
path: root/tests/bionic/libc/common/test_dlopen_null.c
blob: 42b23dd38da9efe39ceefd62d412a664caef215d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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;
}