Skip to content

Commit bb7fa09

Browse files
chenhengqianakryiko
authored andcommitted
libbpf: Support symbol versioning for uprobe
In current implementation, we assume that symbol found in .dynsym section would have a version suffix and use it to compare with symbol user supplied. According to the spec ([0]), this assumption is incorrect, the version info of dynamic symbols are stored in .gnu.version and .gnu.version_d sections of ELF objects. 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 In this case, specify pthread_rwlock_wrlock@@GLIBC_2.34 or pthread_rwlock_wrlock@GLIBC_2.2.5 in bpf_uprobe_opts::func_name won't work. Because the qualified name does NOT match `pthread_rwlock_wrlock` (without version suffix) in .dynsym sections. This commit implements the symbol versioning for dynsym and allows user to specify symbol in the following forms: - func - func@LIB_VERSION - func@@LIB_VERSION In case of symbol conflicts, error out and users should resolve it by specifying a qualified name. [0]: https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Reviewed-by: Alan Maguire <alan.maguire@oracle.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Link: https://lore.kernel.org/bpf/20230918024813.237475-3-hengqi.chen@gmail.com
1 parent 7257cee commit bb7fa09

File tree

2 files changed

+124
-12
lines changed

2 files changed

+124
-12
lines changed

tools/lib/bpf/elf.c

Lines changed: 123 additions & 11 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,13 +319,7 @@ 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);

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:

0 commit comments

Comments
 (0)