diff options
author | johannst <johannst@users.noreply.github.com> | 2024-04-10 19:37:02 +0000 |
---|---|---|
committer | johannst <johannst@users.noreply.github.com> | 2024-04-10 19:37:02 +0000 |
commit | 269caa8ae18eb33e1286e4c21d434ea07228a619 (patch) | |
tree | b9c0ae7bf44a6873adc4c9da9204de2bdf4a1de1 /development/ldso/samename | |
parent | 92fc0e8e46352a593c3ce7f87292ee1c4ee4c4e4 (diff) | |
download | notes-269caa8ae18eb33e1286e4c21d434ea07228a619.tar.gz notes-269caa8ae18eb33e1286e4c21d434ea07228a619.zip |
deploy: ae498864bb638e597b589c52ff27237835625281
Diffstat (limited to 'development/ldso/samename')
-rw-r--r-- | development/ldso/samename/Makefile | 13 | ||||
-rw-r--r-- | development/ldso/samename/lib.c | 9 | ||||
-rw-r--r-- | development/ldso/samename/main.c | 36 |
3 files changed, 58 insertions, 0 deletions
diff --git a/development/ldso/samename/Makefile b/development/ldso/samename/Makefile new file mode 100644 index 0000000..f35a1e3 --- /dev/null +++ b/development/ldso/samename/Makefile @@ -0,0 +1,13 @@ +all: + mkdir -p foo bar + gcc -g -o foo/lib.so lib.c -shared -fPIC -DNAME=\"foo\" + gcc -g -o bar/lib.so lib.c -shared -fPIC -DNAME=\"bar\" + gcc -g -o main main.c -ldl + ./main foo/lib.so bar/lib.so + LD_LIBRARY_PATH=foo:bar ./main lib.so lib.so + LD_LIBRARY_PATH=bar:foo ./main lib.so lib.so + LD_LIBRARY_PATH=foo ./main lib.so foo/lib.so + LD_LIBRARY_PATH=foo ./main lib.so bar/lib.so + +clean: + $(RM) -r foo bar main diff --git a/development/ldso/samename/lib.c b/development/ldso/samename/lib.c new file mode 100644 index 0000000..d467d7f --- /dev/null +++ b/development/ldso/samename/lib.c @@ -0,0 +1,9 @@ +#include <stdio.h> + +#ifndef NAME +#define NAME __FILE_NAME__ +#endif + +void moose() { + puts(NAME " says moose"); +} diff --git a/development/ldso/samename/main.c b/development/ldso/samename/main.c new file mode 100644 index 0000000..6d6a8ea --- /dev/null +++ b/development/ldso/samename/main.c @@ -0,0 +1,36 @@ +#include <assert.h> +#include <dlfcn.h> +#include <stdio.h> +#include <string.h> + +void print_libso() { + FILE* m = fopen("/proc/self/maps", "r"); + assert(m); + + char line[256]; + while (fgets(line, sizeof(line), m)) { + if (strstr(line, "lib.so")) { + printf("%s", line); + } + } + fclose(m); +} + +int main(int argc, char* argv[]) { + for (int i = 1; i < argc; ++i) { + void* h = dlopen(argv[i], RTLD_LAZY | RTLD_GLOBAL); + if (!h) { + puts(dlerror()); + return 1; + } + + void (*next)() = dlsym(h, "moose"); + assert(next); + next(); + + // leak lib, we want to priint the mmap. + } + + print_libso(); + return 0; +} |