1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
9 #include <linux/kernel.h>
11 #include "libbpf_internal.h"
12 #include "str_error.h"
14 /* A SHT_GNU_versym section holds 16-bit words. This bit is set if
15 * the symbol is hidden and can only be seen when referenced using an
16 * explicit version number. This is a GNU extension.
18 #define VERSYM_HIDDEN 0x8000
20 /* This is the mask for the rest of the data in a word read from a
21 * SHT_GNU_versym section.
23 #define VERSYM_VERSION 0x7fff
25 int elf_open(const char *binary_path, struct elf_fd *elf_fd)
33 if (elf_version(EV_CURRENT) == EV_NONE) {
34 pr_warn("elf: failed to init libelf for %s\n", binary_path);
35 return -LIBBPF_ERRNO__LIBELF;
37 fd = open(binary_path, O_RDONLY | O_CLOEXEC);
40 pr_warn("elf: failed to open %s: %s\n", binary_path, errstr(ret));
43 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
45 pr_warn("elf: could not read elf from %s: %s\n", binary_path, elf_errmsg(-1));
47 return -LIBBPF_ERRNO__FORMAT;
54 void elf_close(struct elf_fd *elf_fd)
62 /* Return next ELF section of sh_type after scn, or first of that type if scn is NULL. */
63 static Elf_Scn *elf_find_next_scn_by_type(Elf *elf, int sh_type, Elf_Scn *scn)
65 while ((scn = elf_nextscn(elf, scn)) != NULL) {
68 if (!gelf_getshdr(scn, &sh))
70 if (sh.sh_type == sh_type)
91 size_t verdef_strtabidx;
97 static int elf_sym_iter_new(struct elf_sym_iter *iter,
98 Elf *elf, const char *binary_path,
99 int sh_type, int st_type)
105 memset(iter, 0, sizeof(*iter));
107 if (!gelf_getehdr(elf, &ehdr)) {
108 pr_warn("elf: failed to get ehdr from %s: %s\n", binary_path, elf_errmsg(-1));
112 scn = elf_find_next_scn_by_type(elf, sh_type, NULL);
114 pr_debug("elf: failed to find symbol table ELF sections in '%s'\n",
119 if (!gelf_getshdr(scn, &sh))
122 iter->strtabidx = sh.sh_link;
123 iter->syms = elf_getdata(scn, 0);
125 pr_warn("elf: failed to get symbols for symtab section in '%s': %s\n",
126 binary_path, elf_errmsg(-1));
129 iter->nr_syms = iter->syms->d_size / sh.sh_entsize;
131 iter->st_type = st_type;
133 /* Version symbol table is meaningful to dynsym only */
134 if (sh_type != SHT_DYNSYM)
137 scn = elf_find_next_scn_by_type(elf, SHT_GNU_versym, NULL);
140 iter->versyms = elf_getdata(scn, 0);
142 scn = elf_find_next_scn_by_type(elf, SHT_GNU_verdef, NULL);
146 iter->verdefs = elf_getdata(scn, 0);
147 if (!iter->verdefs || !gelf_getshdr(scn, &sh)) {
148 pr_warn("elf: failed to get verdef ELF section in '%s'\n", binary_path);
151 iter->verdef_strtabidx = sh.sh_link;
156 static struct elf_sym *elf_sym_iter_next(struct elf_sym_iter *iter)
158 struct elf_sym *ret = &iter->sym;
159 GElf_Sym *sym = &ret->sym;
160 const char *name = NULL;
165 for (idx = iter->next_sym_idx; idx < iter->nr_syms; idx++) {
166 if (!gelf_getsym(iter->syms, idx, sym))
168 if (GELF_ST_TYPE(sym->st_info) != iter->st_type)
170 name = elf_strptr(iter->elf, iter->strtabidx, sym->st_name);
173 sym_scn = elf_getscn(iter->elf, sym->st_shndx);
176 if (!gelf_getshdr(sym_scn, &ret->sh))
179 iter->next_sym_idx = idx + 1;
185 if (!gelf_getversym(iter->versyms, idx, &versym))
187 ret->ver = versym & VERSYM_VERSION;
188 ret->hidden = versym & VERSYM_HIDDEN;
196 static const char *elf_get_vername(struct elf_sym_iter *iter, int ver)
198 GElf_Verdaux verdaux;
206 while (gelf_getverdef(iter->verdefs, offset, &verdef)) {
207 if (verdef.vd_ndx != ver) {
211 offset += verdef.vd_next;
215 if (!gelf_getverdaux(iter->verdefs, offset + verdef.vd_aux, &verdaux))
218 return elf_strptr(iter->elf, iter->verdef_strtabidx, verdaux.vda_name);
224 static bool symbol_match(struct elf_sym_iter *iter, int sh_type, struct elf_sym *sym,
225 const char *name, size_t name_len, const char *lib_ver)
227 const char *ver_name;
229 /* Symbols are in forms of func, func@LIB_VER or func@@LIB_VER
230 * make sure the func part matches the user specified name
232 if (strncmp(sym->name, name, name_len) != 0)
235 /* ...but we don't want a search for "foo" to match 'foo2" also, so any
236 * additional characters in sname should be of the form "@@LIB".
238 if (sym->name[name_len] != '\0' && sym->name[name_len] != '@')
241 /* If user does not specify symbol version, then we got a match */
245 /* If user specifies symbol version, for dynamic symbols,
246 * get version name from ELF verdef section for comparison.
248 if (sh_type == SHT_DYNSYM) {
249 ver_name = elf_get_vername(iter, sym->ver);
252 return strcmp(ver_name, lib_ver) == 0;
255 /* For normal symbols, it is already in form of func@LIB_VER */
256 return strcmp(sym->name, name) == 0;
259 /* Transform symbol's virtual address (absolute for binaries and relative
260 * for shared libs) into file offset, which is what kernel is expecting
261 * for uprobe/uretprobe attachment.
262 * See Documentation/trace/uprobetracer.rst for more details. This is done
263 * by looking up symbol's containing section's header and using iter's virtual
264 * address (sh_addr) and corresponding file offset (sh_offset) to transform
265 * sym.st_value (virtual address) into desired final file offset.
267 static unsigned long elf_sym_offset(struct elf_sym *sym)
269 return sym->sym.st_value - sym->sh.sh_addr + sym->sh.sh_offset;
272 /* Find offset of function name in the provided ELF object. "binary_path" is
273 * the path to the ELF binary represented by "elf", and only used for error
274 * reporting matters. "name" matches symbol name or name@@LIB for library
277 long elf_find_func_offset(Elf *elf, const char *binary_path, const char *name)
279 int i, sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB };
280 const char *at_symbol, *lib_ver;
286 if (!gelf_getehdr(elf, &ehdr)) {
287 pr_warn("elf: failed to get ehdr from %s: %s\n", binary_path, elf_errmsg(-1));
288 ret = -LIBBPF_ERRNO__FORMAT;
291 /* for shared lib case, we do not need to calculate relative offset */
292 is_shared_lib = ehdr.e_type == ET_DYN;
294 /* Does name specify "@@LIB_VER" or "@LIB_VER" ? */
295 at_symbol = strchr(name, '@');
297 name_len = at_symbol - name;
298 /* skip second @ if it's @@LIB_VER case */
299 if (at_symbol[1] == '@')
301 lib_ver = at_symbol + 1;
303 name_len = strlen(name);
307 /* Search SHT_DYNSYM, SHT_SYMTAB for symbol. This search order is used because if
308 * a binary is stripped, it may only have SHT_DYNSYM, and a fully-statically
309 * linked binary may not have SHT_DYMSYM, so absence of a section should not be
310 * reported as a warning/error.
312 for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
313 struct elf_sym_iter iter;
318 ret = elf_sym_iter_new(&iter, elf, binary_path, sh_types[i], STT_FUNC);
324 while ((sym = elf_sym_iter_next(&iter))) {
325 if (!symbol_match(&iter, sh_types[i], sym, name, name_len, lib_ver))
328 cur_bind = GELF_ST_BIND(sym->sym.st_info);
331 /* handle multiple matches */
332 if (elf_sym_offset(sym) == ret) {
333 /* same offset, no problem */
335 } else if (last_bind != STB_WEAK && cur_bind != STB_WEAK) {
336 /* Only accept one non-weak bind. */
337 pr_warn("elf: ambiguous match for '%s', '%s' in '%s'\n",
338 sym->name, name, binary_path);
339 ret = -LIBBPF_ERRNO__FORMAT;
341 } else if (cur_bind == STB_WEAK) {
342 /* already have a non-weak bind, and
343 * this is a weak bind, so ignore.
349 ret = elf_sym_offset(sym);
350 last_bind = cur_bind;
357 pr_debug("elf: symbol address match for '%s' in '%s': 0x%lx\n", name, binary_path,
361 pr_warn("elf: '%s' is 0 in symtab for '%s': %s\n", name, binary_path,
362 is_shared_lib ? "should not be 0 in a shared library" :
363 "try using shared library path instead");
366 pr_warn("elf: failed to find symbol '%s' in '%s'\n", name, binary_path);
373 /* Find offset of function name in ELF object specified by path. "name" matches
374 * symbol name or name@@LIB for library functions.
376 long elf_find_func_offset_from_file(const char *binary_path, const char *name)
378 struct elf_fd elf_fd;
381 ret = elf_open(binary_path, &elf_fd);
384 ret = elf_find_func_offset(elf_fd.elf, binary_path, name);
395 static int symbol_cmp(const void *a, const void *b)
397 const struct symbol *sym_a = a;
398 const struct symbol *sym_b = b;
400 return strcmp(sym_a->name, sym_b->name);
404 * Return offsets in @poffsets for symbols specified in @syms array argument.
405 * On success returns 0 and offsets are returned in allocated array with @cnt
406 * size, that needs to be released by the caller.
408 int elf_resolve_syms_offsets(const char *binary_path, int cnt,
409 const char **syms, unsigned long **poffsets,
412 int sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB };
413 int err = 0, i, cnt_done = 0;
414 unsigned long *offsets;
415 struct symbol *symbols;
416 struct elf_fd elf_fd;
418 err = elf_open(binary_path, &elf_fd);
422 offsets = calloc(cnt, sizeof(*offsets));
423 symbols = calloc(cnt, sizeof(*symbols));
425 if (!offsets || !symbols) {
430 for (i = 0; i < cnt; i++) {
431 symbols[i].name = syms[i];
435 qsort(symbols, cnt, sizeof(*symbols), symbol_cmp);
437 for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
438 struct elf_sym_iter iter;
441 err = elf_sym_iter_new(&iter, elf_fd.elf, binary_path, sh_types[i], st_type);
447 while ((sym = elf_sym_iter_next(&iter))) {
448 unsigned long sym_offset = elf_sym_offset(sym);
449 int bind = GELF_ST_BIND(sym->sym.st_info);
450 struct symbol *found, tmp = {
453 unsigned long *offset;
455 found = bsearch(&tmp, symbols, cnt, sizeof(*symbols), symbol_cmp);
459 offset = &offsets[found->idx];
461 /* same offset, no problem */
462 if (*offset == sym_offset)
464 /* handle multiple matches */
465 if (found->bind != STB_WEAK && bind != STB_WEAK) {
466 /* Only accept one non-weak bind. */
467 pr_warn("elf: ambiguous match found '%s@%lu' in '%s' previous offset %lu\n",
468 sym->name, sym_offset, binary_path, *offset);
471 } else if (bind == STB_WEAK) {
472 /* already have a non-weak bind, and
473 * this is a weak bind, so ignore.
480 *offset = sym_offset;
485 if (cnt != cnt_done) {
501 * Return offsets in @poffsets for symbols specified by @pattern argument.
502 * On success returns 0 and offsets are returned in allocated @poffsets
503 * array with the @pctn size, that needs to be released by the caller.
505 int elf_resolve_pattern_offsets(const char *binary_path, const char *pattern,
506 unsigned long **poffsets, size_t *pcnt)
508 int sh_types[2] = { SHT_SYMTAB, SHT_DYNSYM };
509 unsigned long *offsets = NULL;
510 size_t cap = 0, cnt = 0;
511 struct elf_fd elf_fd;
514 err = elf_open(binary_path, &elf_fd);
518 for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
519 struct elf_sym_iter iter;
522 err = elf_sym_iter_new(&iter, elf_fd.elf, binary_path, sh_types[i], STT_FUNC);
528 while ((sym = elf_sym_iter_next(&iter))) {
529 if (!glob_match(sym->name, pattern))
532 err = libbpf_ensure_mem((void **) &offsets, &cap, sizeof(*offsets),
537 offsets[cnt++] = elf_sym_offset(sym);
540 /* If we found anything in the first symbol section,
541 * do not search others to avoid duplicates.