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 #define STRERR_BUFSIZE 128
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.
20 #define VERSYM_HIDDEN 0x8000
22 /* This is the mask for the rest of the data in a word read from a
23 * SHT_GNU_versym section.
25 #define VERSYM_VERSION 0x7fff
27 int elf_open(const char *binary_path, struct elf_fd *elf_fd)
29 char errmsg[STRERR_BUFSIZE];
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,
41 libbpf_strerror_r(ret, errmsg, sizeof(errmsg)));
44 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
46 pr_warn("elf: could not read elf from %s: %s\n", binary_path, elf_errmsg(-1));
48 return -LIBBPF_ERRNO__FORMAT;
55 void elf_close(struct elf_fd *elf_fd)
63 /* Return next ELF section of sh_type after scn, or first of that type if scn is NULL. */
64 static Elf_Scn *elf_find_next_scn_by_type(Elf *elf, int sh_type, Elf_Scn *scn)
66 while ((scn = elf_nextscn(elf, scn)) != NULL) {
69 if (!gelf_getshdr(scn, &sh))
71 if (sh.sh_type == sh_type)
92 size_t verdef_strtabidx;
98 static int elf_sym_iter_new(struct elf_sym_iter *iter,
99 Elf *elf, const char *binary_path,
100 int sh_type, int st_type)
106 memset(iter, 0, sizeof(*iter));
108 if (!gelf_getehdr(elf, &ehdr)) {
109 pr_warn("elf: failed to get ehdr from %s: %s\n", binary_path, elf_errmsg(-1));
113 scn = elf_find_next_scn_by_type(elf, sh_type, NULL);
115 pr_debug("elf: failed to find symbol table ELF sections in '%s'\n",
120 if (!gelf_getshdr(scn, &sh))
123 iter->strtabidx = sh.sh_link;
124 iter->syms = elf_getdata(scn, 0);
126 pr_warn("elf: failed to get symbols for symtab section in '%s': %s\n",
127 binary_path, elf_errmsg(-1));
130 iter->nr_syms = iter->syms->d_size / sh.sh_entsize;
132 iter->st_type = st_type;
134 /* Version symbol table is meaningful to dynsym only */
135 if (sh_type != SHT_DYNSYM)
138 scn = elf_find_next_scn_by_type(elf, SHT_GNU_versym, NULL);
141 iter->versyms = elf_getdata(scn, 0);
143 scn = elf_find_next_scn_by_type(elf, SHT_GNU_verdef, NULL);
147 iter->verdefs = elf_getdata(scn, 0);
148 if (!iter->verdefs || !gelf_getshdr(scn, &sh)) {
149 pr_warn("elf: failed to get verdef ELF section in '%s'\n", binary_path);
152 iter->verdef_strtabidx = sh.sh_link;
157 static struct elf_sym *elf_sym_iter_next(struct elf_sym_iter *iter)
159 struct elf_sym *ret = &iter->sym;
160 GElf_Sym *sym = &ret->sym;
161 const char *name = NULL;
166 for (idx = iter->next_sym_idx; idx < iter->nr_syms; idx++) {
167 if (!gelf_getsym(iter->syms, idx, sym))
169 if (GELF_ST_TYPE(sym->st_info) != iter->st_type)
171 name = elf_strptr(iter->elf, iter->strtabidx, sym->st_name);
174 sym_scn = elf_getscn(iter->elf, sym->st_shndx);
177 if (!gelf_getshdr(sym_scn, &ret->sh))
180 iter->next_sym_idx = idx + 1;
186 if (!gelf_getversym(iter->versyms, idx, &versym))
188 ret->ver = versym & VERSYM_VERSION;
189 ret->hidden = versym & VERSYM_HIDDEN;
197 static const char *elf_get_vername(struct elf_sym_iter *iter, int ver)
199 GElf_Verdaux verdaux;
207 while (gelf_getverdef(iter->verdefs, offset, &verdef)) {
208 if (verdef.vd_ndx != ver) {
212 offset += verdef.vd_next;
216 if (!gelf_getverdaux(iter->verdefs, offset + verdef.vd_aux, &verdaux))
219 return elf_strptr(iter->elf, iter->verdef_strtabidx, verdaux.vda_name);
225 static bool symbol_match(struct elf_sym_iter *iter, int sh_type, struct elf_sym *sym,
226 const char *name, size_t name_len, const char *lib_ver)
228 const char *ver_name;
230 /* Symbols are in forms of func, func@LIB_VER or func@@LIB_VER
231 * make sure the func part matches the user specified name
233 if (strncmp(sym->name, name, name_len) != 0)
236 /* ...but we don't want a search for "foo" to match 'foo2" also, so any
237 * additional characters in sname should be of the form "@@LIB".
239 if (sym->name[name_len] != '\0' && sym->name[name_len] != '@')
242 /* If user does not specify symbol version, then we got a match */
246 /* If user specifies symbol version, for dynamic symbols,
247 * get version name from ELF verdef section for comparison.
249 if (sh_type == SHT_DYNSYM) {
250 ver_name = elf_get_vername(iter, sym->ver);
253 return strcmp(ver_name, lib_ver) == 0;
256 /* For normal symbols, it is already in form of func@LIB_VER */
257 return strcmp(sym->name, name) == 0;
260 /* Transform symbol's virtual address (absolute for binaries and relative
261 * for shared libs) into file offset, which is what kernel is expecting
262 * for uprobe/uretprobe attachment.
263 * See Documentation/trace/uprobetracer.rst for more details. This is done
264 * by looking up symbol's containing section's header and using iter's virtual
265 * address (sh_addr) and corresponding file offset (sh_offset) to transform
266 * sym.st_value (virtual address) into desired final file offset.
268 static unsigned long elf_sym_offset(struct elf_sym *sym)
270 return sym->sym.st_value - sym->sh.sh_addr + sym->sh.sh_offset;
273 /* Find offset of function name in the provided ELF object. "binary_path" is
274 * the path to the ELF binary represented by "elf", and only used for error
275 * reporting matters. "name" matches symbol name or name@@LIB for library
278 long elf_find_func_offset(Elf *elf, const char *binary_path, const char *name)
280 int i, sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB };
281 const char *at_symbol, *lib_ver;
287 if (!gelf_getehdr(elf, &ehdr)) {
288 pr_warn("elf: failed to get ehdr from %s: %s\n", binary_path, elf_errmsg(-1));
289 ret = -LIBBPF_ERRNO__FORMAT;
292 /* for shared lib case, we do not need to calculate relative offset */
293 is_shared_lib = ehdr.e_type == ET_DYN;
295 /* Does name specify "@@LIB_VER" or "@LIB_VER" ? */
296 at_symbol = strchr(name, '@');
298 name_len = at_symbol - name;
299 /* skip second @ if it's @@LIB_VER case */
300 if (at_symbol[1] == '@')
302 lib_ver = at_symbol + 1;
304 name_len = strlen(name);
308 /* Search SHT_DYNSYM, SHT_SYMTAB for symbol. This search order is used because if
309 * a binary is stripped, it may only have SHT_DYNSYM, and a fully-statically
310 * linked binary may not have SHT_DYMSYM, so absence of a section should not be
311 * reported as a warning/error.
313 for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
314 struct elf_sym_iter iter;
319 ret = elf_sym_iter_new(&iter, elf, binary_path, sh_types[i], STT_FUNC);
325 while ((sym = elf_sym_iter_next(&iter))) {
326 if (!symbol_match(&iter, sh_types[i], sym, name, name_len, lib_ver))
329 cur_bind = GELF_ST_BIND(sym->sym.st_info);
332 /* handle multiple matches */
333 if (elf_sym_offset(sym) == ret) {
334 /* same offset, no problem */
336 } else if (last_bind != STB_WEAK && cur_bind != STB_WEAK) {
337 /* Only accept one non-weak bind. */
338 pr_warn("elf: ambiguous match for '%s', '%s' in '%s'\n",
339 sym->name, name, binary_path);
340 ret = -LIBBPF_ERRNO__FORMAT;
342 } else if (cur_bind == STB_WEAK) {
343 /* already have a non-weak bind, and
344 * this is a weak bind, so ignore.
350 ret = elf_sym_offset(sym);
351 last_bind = cur_bind;
358 pr_debug("elf: symbol address match for '%s' in '%s': 0x%lx\n", name, binary_path,
362 pr_warn("elf: '%s' is 0 in symtab for '%s': %s\n", name, binary_path,
363 is_shared_lib ? "should not be 0 in a shared library" :
364 "try using shared library path instead");
367 pr_warn("elf: failed to find symbol '%s' in '%s'\n", name, binary_path);
374 /* Find offset of function name in ELF object specified by path. "name" matches
375 * symbol name or name@@LIB for library functions.
377 long elf_find_func_offset_from_file(const char *binary_path, const char *name)
379 struct elf_fd elf_fd;
382 ret = elf_open(binary_path, &elf_fd);
385 ret = elf_find_func_offset(elf_fd.elf, binary_path, name);
396 static int symbol_cmp(const void *a, const void *b)
398 const struct symbol *sym_a = a;
399 const struct symbol *sym_b = b;
401 return strcmp(sym_a->name, sym_b->name);
405 * Return offsets in @poffsets for symbols specified in @syms array argument.
406 * On success returns 0 and offsets are returned in allocated array with @cnt
407 * size, that needs to be released by the caller.
409 int elf_resolve_syms_offsets(const char *binary_path, int cnt,
410 const char **syms, unsigned long **poffsets,
413 int sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB };
414 int err = 0, i, cnt_done = 0;
415 unsigned long *offsets;
416 struct symbol *symbols;
417 struct elf_fd elf_fd;
419 err = elf_open(binary_path, &elf_fd);
423 offsets = calloc(cnt, sizeof(*offsets));
424 symbols = calloc(cnt, sizeof(*symbols));
426 if (!offsets || !symbols) {
431 for (i = 0; i < cnt; i++) {
432 symbols[i].name = syms[i];
436 qsort(symbols, cnt, sizeof(*symbols), symbol_cmp);
438 for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
439 struct elf_sym_iter iter;
442 err = elf_sym_iter_new(&iter, elf_fd.elf, binary_path, sh_types[i], st_type);
448 while ((sym = elf_sym_iter_next(&iter))) {
449 unsigned long sym_offset = elf_sym_offset(sym);
450 int bind = GELF_ST_BIND(sym->sym.st_info);
451 struct symbol *found, tmp = {
454 unsigned long *offset;
456 found = bsearch(&tmp, symbols, cnt, sizeof(*symbols), symbol_cmp);
460 offset = &offsets[found->idx];
462 /* same offset, no problem */
463 if (*offset == sym_offset)
465 /* handle multiple matches */
466 if (found->bind != STB_WEAK && bind != STB_WEAK) {
467 /* Only accept one non-weak bind. */
468 pr_warn("elf: ambiguous match found '%s@%lu' in '%s' previous offset %lu\n",
469 sym->name, sym_offset, binary_path, *offset);
472 } else if (bind == STB_WEAK) {
473 /* already have a non-weak bind, and
474 * this is a weak bind, so ignore.
481 *offset = sym_offset;
486 if (cnt != cnt_done) {
502 * Return offsets in @poffsets for symbols specified by @pattern argument.
503 * On success returns 0 and offsets are returned in allocated @poffsets
504 * array with the @pctn size, that needs to be released by the caller.
506 int elf_resolve_pattern_offsets(const char *binary_path, const char *pattern,
507 unsigned long **poffsets, size_t *pcnt)
509 int sh_types[2] = { SHT_SYMTAB, SHT_DYNSYM };
510 unsigned long *offsets = NULL;
511 size_t cap = 0, cnt = 0;
512 struct elf_fd elf_fd;
515 err = elf_open(binary_path, &elf_fd);
519 for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
520 struct elf_sym_iter iter;
523 err = elf_sym_iter_new(&iter, elf_fd.elf, binary_path, sh_types[i], STT_FUNC);
529 while ((sym = elf_sym_iter_next(&iter))) {
530 if (!glob_match(sym->name, pattern))
533 err = libbpf_ensure_mem((void **) &offsets, &cap, sizeof(*offsets),
538 offsets[cnt++] = elf_sym_offset(sym);
541 /* If we found anything in the first symbol section,
542 * do not search others to avoid duplicates.