From ae498864bb638e597b589c52ff27237835625281 Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Wed, 10 Apr 2024 21:36:39 +0200 Subject: ld.so: load libs with same name experiments --- src/development/ld.so.md | 5 +++++ src/development/ldso/samename/Makefile | 13 ++++++++++++ src/development/ldso/samename/lib.c | 9 +++++++++ src/development/ldso/samename/main.c | 36 ++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 src/development/ldso/samename/Makefile create mode 100644 src/development/ldso/samename/lib.c create mode 100644 src/development/ldso/samename/main.c diff --git a/src/development/ld.so.md b/src/development/ld.so.md index 9b03c45..fd271bc 100644 --- a/src/development/ld.so.md +++ b/src/development/ld.so.md @@ -199,6 +199,10 @@ symbol=default_main; lookup in file=/lib64/ld-linux-x86-64.so.2 [0] ./main: error: symbol lookup error: undefined symbol: default_main (fatal) ``` +## Load lib with same name from different locations +The sources in [ldso/samename][src-samename] show some experiments, loading the +libs with the same name but potentially from different locations (paths). + ## Dynamic Linking (x86_64) Dynamic linking basically works via one indirect jump. It uses a combination of function trampolines (`.plt` section) and a function pointer table (`.got.plt` @@ -260,3 +264,4 @@ Using `radare2` we can analyze this in more detail: As we can see the offset from relocation at index `0` points to `GOT[3]`. [src-deepbind]: https://github.com/johannst/notes/tree/master/src/development/ldso/deepbind +[src-samename]: https://github.com/johannst/notes/tree/master/src/development/ldso/samename diff --git a/src/development/ldso/samename/Makefile b/src/development/ldso/samename/Makefile new file mode 100644 index 0000000..f35a1e3 --- /dev/null +++ b/src/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/src/development/ldso/samename/lib.c b/src/development/ldso/samename/lib.c new file mode 100644 index 0000000..d467d7f --- /dev/null +++ b/src/development/ldso/samename/lib.c @@ -0,0 +1,9 @@ +#include + +#ifndef NAME +#define NAME __FILE_NAME__ +#endif + +void moose() { + puts(NAME " says moose"); +} diff --git a/src/development/ldso/samename/main.c b/src/development/ldso/samename/main.c new file mode 100644 index 0000000..6d6a8ea --- /dev/null +++ b/src/development/ldso/samename/main.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +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; +} -- cgit v1.2.3