aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/development/ldso/deepbind/main.c
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2024-04-06 23:17:02 +0200
committerJohannes Stoelp <johannes.stoelp@gmail.com>2024-04-06 23:17:02 +0200
commit6d2292f2431ec9405b8789c28c81e8e711c15b2d (patch)
tree31cd471b7e3443af75fdddec619c51ad2db6c199 /src/development/ldso/deepbind/main.c
parent3de8cfbc28ff4f7619c20d5552e03ca96aab4b01 (diff)
downloadnotes-6d2292f2431ec9405b8789c28c81e8e711c15b2d.tar.gz
notes-6d2292f2431ec9405b8789c28c81e8e711c15b2d.zip
ld.so: add RTLD_LOCAL, RTLD_DEEPBIND notes + example
Diffstat (limited to 'src/development/ldso/deepbind/main.c')
-rw-r--r--src/development/ldso/deepbind/main.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/development/ldso/deepbind/main.c b/src/development/ldso/deepbind/main.c
new file mode 100644
index 0000000..6d93520
--- /dev/null
+++ b/src/development/ldso/deepbind/main.c
@@ -0,0 +1,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;
+}