Skip to content

Commit f8a0569

Browse files
anakryikoborkmann
authored andcommitted
libbpf: Work around kernel inconsistently stripping '.llvm.' suffix
Some versions of kernel were stripping out '.llvm.<hash>' suffix from kerne symbols (produced by Clang LTO compilation) from function names reported in available_filter_functions, while kallsyms reported full original name. This confuses libbpf's multi-kprobe logic of finding all matching kernel functions for specified user glob pattern by joining available_filter_functions and kallsyms contents, because joining by full symbol name won't work for symbols containing '.llvm.<hash>' suffix. This was eventually fixed by [0] in the kernel, but we'd like to not regress multi-kprobe experience and add a work around for this bug on libbpf side, stripping kallsym's name if it matches user pattern and contains '.llvm.' suffix. [0] fb6a421 ("kallsyms: Match symbols exactly with CONFIG_LTO_CLANG") Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent b53b63d commit f8a0569

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

tools/lib/bpf/libbpf.c

+25-1
Original file line numberDiff line numberDiff line change
@@ -11387,9 +11387,33 @@ static int avail_kallsyms_cb(unsigned long long sym_addr, char sym_type,
1138711387
struct kprobe_multi_resolve *res = data->res;
1138811388
int err;
1138911389

11390-
if (!bsearch(&sym_name, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp))
11390+
if (!glob_match(sym_name, res->pattern))
1139111391
return 0;
1139211392

11393+
if (!bsearch(&sym_name, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp)) {
11394+
/* Some versions of kernel strip out .llvm.<hash> suffix from
11395+
* function names reported in available_filter_functions, but
11396+
* don't do so for kallsyms. While this is clearly a kernel
11397+
* bug (fixed by [0]) we try to accommodate that in libbpf to
11398+
* make multi-kprobe usability a bit better: if no match is
11399+
* found, we will strip .llvm. suffix and try one more time.
11400+
*
11401+
* [0] fb6a421fb615 ("kallsyms: Match symbols exactly with CONFIG_LTO_CLANG")
11402+
*/
11403+
char sym_trim[256], *psym_trim = sym_trim, *sym_sfx;
11404+
11405+
if (!(sym_sfx = strstr(sym_name, ".llvm.")))
11406+
return 0;
11407+
11408+
/* psym_trim vs sym_trim dance is done to avoid pointer vs array
11409+
* coercion differences and get proper `const char **` pointer
11410+
* which avail_func_cmp() expects
11411+
*/
11412+
snprintf(sym_trim, sizeof(sym_trim), "%.*s", (int)(sym_sfx - sym_name), sym_name);
11413+
if (!bsearch(&psym_trim, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp))
11414+
return 0;
11415+
}
11416+
1139311417
err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, sizeof(*res->addrs), res->cnt + 1);
1139411418
if (err)
1139511419
return err;

0 commit comments

Comments
 (0)