libbpf: Support Debian in resolve_full_path()
authorIlya Leoshkevich <iii@linux.ibm.com>
Mon, 4 Apr 2022 22:50:20 +0000 (00:50 +0200)
committerAndrii Nakryiko <andrii@kernel.org>
Mon, 4 Apr 2022 23:47:16 +0000 (16:47 -0700)
attach_probe selftest fails on Debian-based distros with `failed to
resolve full path for 'libc.so.6'`. The reason is that these distros
embraced multiarch to the point where even for the "main" architecture
they store libc in /lib/<triple>.

This is configured in /etc/ld.so.conf and in theory it's possible to
replicate the loader's parsing and processing logic in libbpf, however
a much simpler solution is to just enumerate the known library paths.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220404225020.51029-1-iii@linux.ibm.com
tools/lib/bpf/libbpf.c

index 6d2be53e4ba9171efc1f768630907bb118a49b04..91ce94b61f7f47056baad85c47ccadae1653e2a4 100644 (file)
@@ -10707,15 +10707,53 @@ out:
        return ret;
 }
 
+static const char *arch_specific_lib_paths(void)
+{
+       /*
+        * Based on https://packages.debian.org/sid/libc6.
+        *
+        * Assume that the traced program is built for the same architecture
+        * as libbpf, which should cover the vast majority of cases.
+        */
+#if defined(__x86_64__)
+       return "/lib/x86_64-linux-gnu";
+#elif defined(__i386__)
+       return "/lib/i386-linux-gnu";
+#elif defined(__s390x__)
+       return "/lib/s390x-linux-gnu";
+#elif defined(__s390__)
+       return "/lib/s390-linux-gnu";
+#elif defined(__arm__) && defined(__SOFTFP__)
+       return "/lib/arm-linux-gnueabi";
+#elif defined(__arm__) && !defined(__SOFTFP__)
+       return "/lib/arm-linux-gnueabihf";
+#elif defined(__aarch64__)
+       return "/lib/aarch64-linux-gnu";
+#elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 64
+       return "/lib/mips64el-linux-gnuabi64";
+#elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 32
+       return "/lib/mipsel-linux-gnu";
+#elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+       return "/lib/powerpc64le-linux-gnu";
+#elif defined(__sparc__) && defined(__arch64__)
+       return "/lib/sparc64-linux-gnu";
+#elif defined(__riscv) && __riscv_xlen == 64
+       return "/lib/riscv64-linux-gnu";
+#else
+       return NULL;
+#endif
+}
+
 /* Get full path to program/shared library. */
 static int resolve_full_path(const char *file, char *result, size_t result_sz)
 {
-       const char *search_paths[2];
+       const char *search_paths[3] = {};
        int i;
 
        if (strstr(file, ".so")) {
                search_paths[0] = getenv("LD_LIBRARY_PATH");
                search_paths[1] = "/usr/lib64:/usr/lib";
+               search_paths[2] = arch_specific_lib_paths();
        } else {
                search_paths[0] = getenv("PATH");
                search_paths[1] = "/usr/bin:/usr/sbin";