Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Thu, 21 May 2020 14:27:24 +0300
From: Alexander Scherbatiy <>
Subject: Shared library loading


I use Alpine Linux 3.11.6 with musl libc (x86_64) version 1.1.24.

I have two shared libraries "a" and "b" each of which is placed in its 
own directory and lib "b" depends on "a".
First, I use dlopen to load lib "a" with "RTLD_NOW | RTLD_GLOBAL" flags.
Second, I load lib "b" with  flag "RTLD_LAZY".

The "b" library loading works on my Ubuntu 19.10 and fails on Alpine 
3.11.6 with message:
   dlopen failed: Error loading shared library No such file or 
directory (needed by /root/load-lib-sample/bin/b/
Should it work on Alpine with musl libc as well?

Below is the source code which reproduces the library loading issue:

--- include/a.h ---
void test_a();
--- include/b.h ---
void test_b();
--- src/a.c ---
#include "a.h"
#include <stdio.h>

void test_a() {
     printf("test a\n");
--- src/b.c ---
#include "b.h"
#include "a.h"
#include <stdio.h>

void test_b() {
     printf("test b\n");
--- src/main.c ---
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

void *open_lib(const char* base_dir, const char* lib_dir, const char 
*lib_name,int flags) {
     char path_lib[256];
     sprintf(path_lib, "%s/%s/%s", base_dir, lib_dir, lib_name);
     printf("[load lib] %s\n", path_lib);

     void *handle = dlopen(path_lib, flags);

     if (!handle) {
         fprintf(stderr, "dlopen failed: %s\n", dlerror());
     return handle;

void call_func(void *handle, const char *func_name) {
     printf("[call func] %s\n", func_name);

     void (*func_t)(void);
     *(void **) (&func_t) = dlsym(handle, func_name);

     char *error = dlerror();
     if (error != NULL)  {
         fprintf(stderr, "%s\n", error);


int main(int argc, char* argv[]) {
     if (argc < 2) {
         printf("provide base dir.\n");

     void *handle_a = open_lib(argv[1], "a", "", RTLD_NOW | 
     call_func(handle_a, "test_a");

     void *handle_b = open_lib(argv[1], "b", "", RTLD_LAZY);
     call_func(handle_b, "test_b");

--- ---
rm -rf ./bin
mkdir -p bin bin/a bin/b

gcc -g -shared src/a.c -Wl, -Iinclude -o bin/a/
gcc -g -shared src/b.c -Wl, -Iinclude -la -Lbin/a -o 
gcc -g src/main.c -Iinclude -o bin/main -ldl

./bin/main $DIR_BIN
--- --- ---

Output on Ubuntu 19.10
[load lib] /home/user/load-lib-sample/bin/a/
[call func] test_a
test a
[load lib] /home/user/load-lib-sample/bin/b/
[call func] test_b
test b
test a

Output on Alpine 3.11.6
[load lib] /root/load-lib-sample/bin/a/
[call func] test_a
test a
[load lib] /root/load-lib-sample/bin/b/
dlopen failed: Error loading shared library No such file or 
directory (needed by /root/load-lib-sample/bin/b/


Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.