Skip to content

Commit 831916f

Browse files
committed
Merge branch 'libbpf: Support symbol versioning for uprobe'
Hengqi Chen says: ==================== Dynamic symbols in shared library may have the same name, for example: $ nm -D /lib/x86_64-linux-gnu/libc.so.6 | grep rwlock_wrlock 000000000009b1a0 T __pthread_rwlock_wrlock@GLIBC_2.2.5 000000000009b1a0 T pthread_rwlock_wrlock@@GLIBC_2.34 000000000009b1a0 T pthread_rwlock_wrlock@GLIBC_2.2.5 $ readelf -W --dyn-syms /lib/x86_64-linux-gnu/libc.so.6 | grep rwlock_wrlock 706: 000000000009b1a0 878 FUNC GLOBAL DEFAULT 15 __pthread_rwlock_wrlock@GLIBC_2.2.5 2568: 000000000009b1a0 878 FUNC GLOBAL DEFAULT 15 pthread_rwlock_wrlock@@GLIBC_2.34 2571: 000000000009b1a0 878 FUNC GLOBAL DEFAULT 15 pthread_rwlock_wrlock@GLIBC_2.2.5 There are two pthread_rwlock_wrlock symbols in libc.so .dynsym section. The one with @@ is the default version, the other is hidden. Note that the version info is stored in .gnu.version and .gnu.version_d sections of libc and the two symbols are at the _same_ offset. Currently, specify `pthread_rwlock_wrlock`, `pthread_rwlock_wrlock@@GLIBC_2.34` or `pthread_rwlock_wrlock@GLIBC_2.2.5` in bpf_uprobe_opts::func_name won't work. Because there are two `pthread_rwlock_wrlock` in .dynsym sections without the version suffix and both are global bind. We could solve this by introducing symbol versioning ([0]). So that users can specify func, func@LIB_VERSION or func@@LIB_VERSION to attach a uprobe. This patchset resolves symbol conflicts and add symbol versioning for uprobe. - Patch 1 resolves symbol conflicts at the same offset - Patch 2 adds symbol versioning for dynsym - Patch 3 adds selftests for the above changes Changes from v3: - Address comments from Andrii Changes from v2: - Add uretprobe selfttest (Alan) - Check symbol exact match (Alan) - Fix typo (Jiri) Changes from v1: - Address comments from Alan and Jiri - Add selftests (Someone reminds me that there is an attempt at [1] and part of the selftest code from Andrii is taken from there) [0]: https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html [1]: https://lore.kernel.org/lkml/CAEf4BzZTrjjyyOm3ak9JsssPSh6T_ZmGd677a2rt5e5rBLUrpQ@mail.gmail.com/ ==================== Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
2 parents ac0691c + 7089f85 commit 831916f

8 files changed

Lines changed: 337 additions & 17 deletions

File tree

tools/lib/bpf/elf.c

Lines changed: 127 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
22

3+
#ifndef _GNU_SOURCE
4+
#define _GNU_SOURCE
5+
#endif
36
#include <libelf.h>
47
#include <gelf.h>
58
#include <fcntl.h>
@@ -10,6 +13,17 @@
1013

1114
#define STRERR_BUFSIZE 128
1215

16+
/* A SHT_GNU_versym section holds 16-bit words. This bit is set if
17+
* the symbol is hidden and can only be seen when referenced using an
18+
* explicit version number. This is a GNU extension.
19+
*/
20+
#define VERSYM_HIDDEN 0x8000
21+
22+
/* This is the mask for the rest of the data in a word read from a
23+
* SHT_GNU_versym section.
24+
*/
25+
#define VERSYM_VERSION 0x7fff
26+
1327
int elf_open(const char *binary_path, struct elf_fd *elf_fd)
1428
{
1529
char errmsg[STRERR_BUFSIZE];
@@ -64,13 +78,18 @@ struct elf_sym {
6478
const char *name;
6579
GElf_Sym sym;
6680
GElf_Shdr sh;
81+
int ver;
82+
bool hidden;
6783
};
6884

6985
struct elf_sym_iter {
7086
Elf *elf;
7187
Elf_Data *syms;
88+
Elf_Data *versyms;
89+
Elf_Data *verdefs;
7290
size_t nr_syms;
7391
size_t strtabidx;
92+
size_t verdef_strtabidx;
7493
size_t next_sym_idx;
7594
struct elf_sym sym;
7695
int st_type;
@@ -111,6 +130,26 @@ static int elf_sym_iter_new(struct elf_sym_iter *iter,
111130
iter->nr_syms = iter->syms->d_size / sh.sh_entsize;
112131
iter->elf = elf;
113132
iter->st_type = st_type;
133+
134+
/* Version symbol table is meaningful to dynsym only */
135+
if (sh_type != SHT_DYNSYM)
136+
return 0;
137+
138+
scn = elf_find_next_scn_by_type(elf, SHT_GNU_versym, NULL);
139+
if (!scn)
140+
return 0;
141+
iter->versyms = elf_getdata(scn, 0);
142+
143+
scn = elf_find_next_scn_by_type(elf, SHT_GNU_verdef, NULL);
144+
if (!scn) {
145+
pr_debug("elf: failed to find verdef ELF sections in '%s'\n", binary_path);
146+
return -ENOENT;
147+
}
148+
if (!gelf_getshdr(scn, &sh))
149+
return -EINVAL;
150+
iter->verdef_strtabidx = sh.sh_link;
151+
iter->verdefs = elf_getdata(scn, 0);
152+
114153
return 0;
115154
}
116155

@@ -119,6 +158,7 @@ static struct elf_sym *elf_sym_iter_next(struct elf_sym_iter *iter)
119158
struct elf_sym *ret = &iter->sym;
120159
GElf_Sym *sym = &ret->sym;
121160
const char *name = NULL;
161+
GElf_Versym versym;
122162
Elf_Scn *sym_scn;
123163
size_t idx;
124164

@@ -138,12 +178,80 @@ static struct elf_sym *elf_sym_iter_next(struct elf_sym_iter *iter)
138178

139179
iter->next_sym_idx = idx + 1;
140180
ret->name = name;
181+
ret->ver = 0;
182+
ret->hidden = false;
183+
184+
if (iter->versyms) {
185+
if (!gelf_getversym(iter->versyms, idx, &versym))
186+
continue;
187+
ret->ver = versym & VERSYM_VERSION;
188+
ret->hidden = versym & VERSYM_HIDDEN;
189+
}
141190
return ret;
142191
}
143192

144193
return NULL;
145194
}
146195

196+
static const char *elf_get_vername(struct elf_sym_iter *iter, int ver)
197+
{
198+
GElf_Verdaux verdaux;
199+
GElf_Verdef verdef;
200+
int offset;
201+
202+
offset = 0;
203+
while (gelf_getverdef(iter->verdefs, offset, &verdef)) {
204+
if (verdef.vd_ndx != ver) {
205+
if (!verdef.vd_next)
206+
break;
207+
208+
offset += verdef.vd_next;
209+
continue;
210+
}
211+
212+
if (!gelf_getverdaux(iter->verdefs, offset + verdef.vd_aux, &verdaux))
213+
break;
214+
215+
return elf_strptr(iter->elf, iter->verdef_strtabidx, verdaux.vda_name);
216+
217+
}
218+
return NULL;
219+
}
220+
221+
static bool symbol_match(struct elf_sym_iter *iter, int sh_type, struct elf_sym *sym,
222+
const char *name, size_t name_len, const char *lib_ver)
223+
{
224+
const char *ver_name;
225+
226+
/* Symbols are in forms of func, func@LIB_VER or func@@LIB_VER
227+
* make sure the func part matches the user specified name
228+
*/
229+
if (strncmp(sym->name, name, name_len) != 0)
230+
return false;
231+
232+
/* ...but we don't want a search for "foo" to match 'foo2" also, so any
233+
* additional characters in sname should be of the form "@@LIB".
234+
*/
235+
if (sym->name[name_len] != '\0' && sym->name[name_len] != '@')
236+
return false;
237+
238+
/* If user does not specify symbol version, then we got a match */
239+
if (!lib_ver)
240+
return true;
241+
242+
/* If user specifies symbol version, for dynamic symbols,
243+
* get version name from ELF verdef section for comparison.
244+
*/
245+
if (sh_type == SHT_DYNSYM) {
246+
ver_name = elf_get_vername(iter, sym->ver);
247+
if (!ver_name)
248+
return false;
249+
return strcmp(ver_name, lib_ver) == 0;
250+
}
251+
252+
/* For normal symbols, it is already in form of func@LIB_VER */
253+
return strcmp(sym->name, name) == 0;
254+
}
147255

148256
/* Transform symbol's virtual address (absolute for binaries and relative
149257
* for shared libs) into file offset, which is what kernel is expecting
@@ -166,7 +274,8 @@ static unsigned long elf_sym_offset(struct elf_sym *sym)
166274
long elf_find_func_offset(Elf *elf, const char *binary_path, const char *name)
167275
{
168276
int i, sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB };
169-
bool is_shared_lib, is_name_qualified;
277+
const char *at_symbol, *lib_ver;
278+
bool is_shared_lib;
170279
long ret = -ENOENT;
171280
size_t name_len;
172281
GElf_Ehdr ehdr;
@@ -179,9 +288,18 @@ long elf_find_func_offset(Elf *elf, const char *binary_path, const char *name)
179288
/* for shared lib case, we do not need to calculate relative offset */
180289
is_shared_lib = ehdr.e_type == ET_DYN;
181290

182-
name_len = strlen(name);
183-
/* Does name specify "@@LIB"? */
184-
is_name_qualified = strstr(name, "@@") != NULL;
291+
/* Does name specify "@@LIB_VER" or "@LIB_VER" ? */
292+
at_symbol = strchr(name, '@');
293+
if (at_symbol) {
294+
name_len = at_symbol - name;
295+
/* skip second @ if it's @@LIB_VER case */
296+
if (at_symbol[1] == '@')
297+
at_symbol++;
298+
lib_ver = at_symbol + 1;
299+
} else {
300+
name_len = strlen(name);
301+
lib_ver = NULL;
302+
}
185303

186304
/* Search SHT_DYNSYM, SHT_SYMTAB for symbol. This search order is used because if
187305
* a binary is stripped, it may only have SHT_DYNSYM, and a fully-statically
@@ -201,20 +319,17 @@ long elf_find_func_offset(Elf *elf, const char *binary_path, const char *name)
201319
goto out;
202320

203321
while ((sym = elf_sym_iter_next(&iter))) {
204-
/* User can specify func, func@@LIB or func@@LIB_VERSION. */
205-
if (strncmp(sym->name, name, name_len) != 0)
206-
continue;
207-
/* ...but we don't want a search for "foo" to match 'foo2" also, so any
208-
* additional characters in sname should be of the form "@@LIB".
209-
*/
210-
if (!is_name_qualified && sym->name[name_len] != '\0' && sym->name[name_len] != '@')
322+
if (!symbol_match(&iter, sh_types[i], sym, name, name_len, lib_ver))
211323
continue;
212324

213325
cur_bind = GELF_ST_BIND(sym->sym.st_info);
214326

215327
if (ret > 0) {
216328
/* handle multiple matches */
217-
if (last_bind != STB_WEAK && cur_bind != STB_WEAK) {
329+
if (elf_sym_offset(sym) == ret) {
330+
/* same offset, no problem */
331+
continue;
332+
} else if (last_bind != STB_WEAK && cur_bind != STB_WEAK) {
218333
/* Only accept one non-weak bind. */
219334
pr_warn("elf: ambiguous match for '%s', '%s' in '%s'\n",
220335
sym->name, name, binary_path);

tools/lib/bpf/libbpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11630,7 +11630,7 @@ static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf
1163011630

1163111631
*link = NULL;
1163211632

11633-
n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.]+%li",
11633+
n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.@]+%li",
1163411634
&probe_type, &binary_path, &func_name, &offset);
1163511635
switch (n) {
1163611636
case 1:

tools/testing/selftests/bpf/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,12 @@ endif
196196

197197
# Filter out -static for liburandom_read.so and its dependent targets so that static builds
198198
# do not fail. Static builds leave urandom_read relying on system-wide shared libraries.
199-
$(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c
199+
$(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom_read.map
200200
$(call msg,LIB,,$@)
201201
$(Q)$(CLANG) $(filter-out -static,$(CFLAGS) $(LDFLAGS)) \
202-
$^ $(filter-out -static,$(LDLIBS)) \
202+
$(filter %.c,$^) $(filter-out -static,$(LDLIBS)) \
203203
-fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
204+
-Wl,--version-script=liburandom_read.map \
204205
-fPIC -shared -o $@
205206

206207
$(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_read.so
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
LIBURANDOM_READ_1.0.0 {
2+
global:
3+
urandlib_api;
4+
urandlib_api_sameoffset;
5+
urandlib_read_without_sema;
6+
urandlib_read_with_sema;
7+
urandlib_read_with_sema_semaphore;
8+
local:
9+
*;
10+
};
11+
12+
LIBURANDOM_READ_2.0.0 {
13+
global:
14+
urandlib_api;
15+
} LIBURANDOM_READ_1.0.0;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2023 Hengqi Chen */
3+
4+
#include <test_progs.h>
5+
#include "test_uprobe.skel.h"
6+
7+
static FILE *urand_spawn(int *pid)
8+
{
9+
FILE *f;
10+
11+
/* urandom_read's stdout is wired into f */
12+
f = popen("./urandom_read 1 report-pid", "r");
13+
if (!f)
14+
return NULL;
15+
16+
if (fscanf(f, "%d", pid) != 1) {
17+
pclose(f);
18+
errno = EINVAL;
19+
return NULL;
20+
}
21+
22+
return f;
23+
}
24+
25+
static int urand_trigger(FILE **urand_pipe)
26+
{
27+
int exit_code;
28+
29+
/* pclose() waits for child process to exit and returns their exit code */
30+
exit_code = pclose(*urand_pipe);
31+
*urand_pipe = NULL;
32+
33+
return exit_code;
34+
}
35+
36+
void test_uprobe(void)
37+
{
38+
LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts);
39+
struct test_uprobe *skel;
40+
FILE *urand_pipe = NULL;
41+
int urand_pid = 0, err;
42+
43+
skel = test_uprobe__open_and_load();
44+
if (!ASSERT_OK_PTR(skel, "skel_open"))
45+
return;
46+
47+
urand_pipe = urand_spawn(&urand_pid);
48+
if (!ASSERT_OK_PTR(urand_pipe, "urand_spawn"))
49+
goto cleanup;
50+
51+
skel->bss->my_pid = urand_pid;
52+
53+
/* Manual attach uprobe to urandlib_api
54+
* There are two `urandlib_api` symbols in .dynsym section:
55+
* - urandlib_api@LIBURANDOM_READ_1.0.0
56+
* - urandlib_api@@LIBURANDOM_READ_2.0.0
57+
* Both are global bind and would cause a conflict if user
58+
* specify the symbol name without a version suffix
59+
*/
60+
uprobe_opts.func_name = "urandlib_api";
61+
skel->links.test4 = bpf_program__attach_uprobe_opts(skel->progs.test4,
62+
urand_pid,
63+
"./liburandom_read.so",
64+
0 /* offset */,
65+
&uprobe_opts);
66+
if (!ASSERT_ERR_PTR(skel->links.test4, "urandlib_api_attach_conflict"))
67+
goto cleanup;
68+
69+
uprobe_opts.func_name = "urandlib_api@LIBURANDOM_READ_1.0.0";
70+
skel->links.test4 = bpf_program__attach_uprobe_opts(skel->progs.test4,
71+
urand_pid,
72+
"./liburandom_read.so",
73+
0 /* offset */,
74+
&uprobe_opts);
75+
if (!ASSERT_OK_PTR(skel->links.test4, "urandlib_api_attach_ok"))
76+
goto cleanup;
77+
78+
/* Auto attach 3 u[ret]probes to urandlib_api_sameoffset */
79+
err = test_uprobe__attach(skel);
80+
if (!ASSERT_OK(err, "skel_attach"))
81+
goto cleanup;
82+
83+
/* trigger urandom_read */
84+
ASSERT_OK(urand_trigger(&urand_pipe), "urand_exit_code");
85+
86+
ASSERT_EQ(skel->bss->test1_result, 1, "urandlib_api_sameoffset");
87+
ASSERT_EQ(skel->bss->test2_result, 1, "urandlib_api_sameoffset@v1");
88+
ASSERT_EQ(skel->bss->test3_result, 3, "urandlib_api_sameoffset@@v2");
89+
ASSERT_EQ(skel->bss->test4_result, 1, "urandlib_api");
90+
91+
cleanup:
92+
if (urand_pipe)
93+
pclose(urand_pipe);
94+
test_uprobe__destroy(skel);
95+
}

0 commit comments

Comments
 (0)