[cfe-dev] RTLD_LAZY does not work if compiled with clang

Tom Yan via cfe-dev cfe-dev at lists.llvm.org
Fri Jun 8 04:10:43 PDT 2018


Hi all!

So I have the following files:

$ cat main.c
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>

typedef void (*plugin_function_t)();

int main() {
        void* plugin_handle = dlopen("./libplugin.so", RTLD_LAZY);
        if (plugin_handle == NULL) {
                printf("dlopen failed: %s\n", dlerror());
                return 1;
        }
        plugin_function_t plugin_function = dlsym(plugin_handle,
"plugin_function");
        if (plugin_function == NULL) {
                printf("plugin_function not found\n");
                return 1;
        }

        void* shared_handle = dlopen("./libshared.so", RTLD_GLOBAL | RTLD_NOW);
        if (shared_handle == NULL) {
                printf("dlopen failed: %s\n", dlerror());
                return 1;
        }

        plugin_function();

        return 0;
}

$ cat libshared.c
int libshared_get_value() { return 42; }

$ cat libplugin.c
#include <stdio.h>

extern int libshared_get_value();

void plugin_function() {
        printf("plugin: value = %d\n", libshared_get_value());
}

If they are compiled with gcc, RTLD_LAZY works as expected and hence
the program:

$ make
cc -shared libshared.c -o libshared.so
cc -shared libplugin.c -o libplugin.so
cc main.c -ldl -o executable
$ ./executable
plugin: value = 42

But if they are compiled with clang, the program fails as if I used RTLD_NOW:

$ make CC=clang
clang -shared libshared.c -o libshared.so
clang -shared libplugin.c -o libplugin.so
clang main.c -ldl -o executable
$ ./executable
dlopen failed: ./libplugin.so: undefined symbol: libshared_get_value

Am I missing something here or is it a bug of clang? I am using clang
6.0.0 on Arch Linux ARM (aarch64).



More information about the cfe-dev mailing list