aboutsummaryrefslogtreecommitdiffhomepage
path: root/development/ldso/deepbind/main.c
blob: 6d935208763054c6c6e89cd00d73525bf558e462 (plain) (blame)
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
38
39
40
#include <dlfcn.h>
#include <stdio.h>

int main() {
  puts("-- deep --");
  // Load library into its own LOCAL scope with DEEPBINDING, meaning that
  // symbols will first resolve to the library + its dependencies first before
  // considering global symbols.
  void* h1 = dlopen("./libdeep.so", RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);
  if (h1) {
    // Lookup symbol in the libraries LOCAL scope (library + its own
    // dependencies).
    void (*test_fn)() = dlsym(h1, "test");
    test_fn();

    // Lookup non-existing symbol in the libraries LOCAL scope (library + its
    // own dependencies).
    (void)dlsym(h1, "libdeep_main");
  }

  puts("-- nodp --");
  // Load library into its own LOCAL scope.
  void* h2 = dlopen("./libnodp.so", RTLD_LOCAL | RTLD_LAZY);
  if (h2) {
    // Lookup symbol in the libraries LOCAL scope (library + its own
    // dependencies).
    void (*test_fn)() = dlsym(h2, "test");
    test_fn();

    // Lookup non-existing symbol in the libraries LOCAL scope (library + its
    // own dependencies).
    (void)dlsym(h2, "libnodp_main");
  }

  puts("-- main --");
  // Lookup non-existing symbol in the GLOBAL scope.
  (void)dlsym(RTLD_DEFAULT, "default_main");

  return 0;
}