From ad9e7cd5117c965222aae708f660e56d537914fc Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Mon, 6 Oct 2025 00:48:48 +0300 Subject: imported all of my Github gists from https://gist.github.com/rhaberkorn --- tests/relocatability-test.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/relocatability-test.c (limited to 'tests/relocatability-test.c') diff --git a/tests/relocatability-test.c b/tests/relocatability-test.c new file mode 100644 index 0000000..ef970bc --- /dev/null +++ b/tests/relocatability-test.c @@ -0,0 +1,28 @@ +/* + * This prints the location of the current binary as + * reported by the dynamic linker. + * It does not work on all UNIX-like platforms. + * For instance, it doesn't on Linux. + * + * Link with: cc -o relocatability-test relocatability-test.c -ldl + */ +#define _GNU_SOURCE +#include +#include + +int +main(int argc, char **argv) +{ + Dl_info info; + + printf("argv[0] = %s\n", argv[0]); + + if (!dladdr(main, &info)) { + fprintf(stderr, "Cannot query executable's path: %s\n", + dlerror()); + return 1; + } + + printf("dli_fname = %s\n", info.dli_fname); + return 0; +} -- cgit v1.2.3