• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 
3 #ifndef _GNU_SOURCE
4 #define _GNU_SOURCE
5 #endif
6 
7 #ifdef HAVE_LIBELF
8 #include <libelf.h>
9 #include <gelf.h>
10 #endif
11 
12 #include <fcntl.h>
13 #include <linux/kernel.h>
14 
15 #include "libbpf_internal.h"
16 #include "str_error.h"
17 
18 
19 
20 #define STRERR_BUFSIZE  128
21 
22 /* A SHT_GNU_versym section holds 16-bit words. This bit is set if
23  * the symbol is hidden and can only be seen when referenced using an
24  * explicit version number. This is a GNU extension.
25  */
26 #define VERSYM_HIDDEN	0x8000
27 
28 /* This is the mask for the rest of the data in a word read from a
29  * SHT_GNU_versym section.
30  */
31 #define VERSYM_VERSION	0x7fff
32 
33 
34 #ifdef  HAVE_LIBELF
elf_open(const char * binary_path,struct elf_fd * elf_fd)35 int elf_open(const char *binary_path, struct elf_fd *elf_fd)
36 {
37 	char errmsg[STRERR_BUFSIZE];
38 	int fd, ret;
39 	Elf *elf;
40 
41 	if (elf_version(EV_CURRENT) == EV_NONE) {
42 		pr_warn("elf: failed to init libelf for %s\n", binary_path);
43 		return -LIBBPF_ERRNO__LIBELF;
44 	}
45 	fd = open(binary_path, O_RDONLY | O_CLOEXEC);
46 	if (fd < 0) {
47 		ret = -errno;
48 		pr_warn("elf: failed to open %s: %s\n", binary_path,
49 			libbpf_strerror_r(ret, errmsg, sizeof(errmsg)));
50 		return ret;
51 	}
52 	elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
53 	if (!elf) {
54 		pr_warn("elf: could not read elf from %s: %s\n", binary_path, elf_errmsg(-1));
55 		close(fd);
56 		return -LIBBPF_ERRNO__FORMAT;
57 	}
58 	elf_fd->fd = fd;
59 	elf_fd->elf = elf;
60 	return 0;
61 }
62 #elif HAVE_ELFIO
elf_open(const char * binary_path,struct elf_fd * elf_fd)63 int elf_open(const char *binary_path, struct elf_fd *elf_fd)
64 {
65 	pelfio_t pelfio = elfio_new();
66 	bool ret = false;
67 	ret = elfio_load(pelfio, binary_path);
68 	if (!ret) {
69 		pr_warn("elf: could not read elf from %s: %s\n", binary_path, elf_errmsg(-1));
70 		return -LIBBPF_ERRNO__FORMAT;
71 	}
72 	elf_fd->elf = pelfio;
73 	elf_fd->fd = -1;
74 	return 0;
75 }
76 #endif
77 
78 
elf_close(struct elf_fd * elf_fd)79 void elf_close(struct elf_fd *elf_fd)
80 {
81 	if (!elf_fd)
82 		return;
83 #ifdef HAVE_LIBELF
84 	elf_end(elf_fd->elf);
85 	close(elf_fd->fd);
86 #elif HAVE_ELFIO
87 	elfio_delete(elf_fd->elf);
88 #endif
89 }
90 
91 /* Return next ELF section of sh_type after scn, or first of that type if scn is NULL. */
92 #ifdef  HAVE_LIBELF
elf_find_next_scn_by_type(Elf * elf,int sh_type,Elf_Scn * scn)93 static Elf_Scn *elf_find_next_scn_by_type(Elf *elf, int sh_type, Elf_Scn *scn)
94 {
95 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
96 		GElf_Shdr sh;
97 
98 		if (!gelf_getshdr(scn, &sh))
99 			continue;
100 		if (sh.sh_type == sh_type)
101 			return scn;
102 	}
103 	return NULL;
104 }
105 #elif HAVE_ELFIO
elf_find_next_scn_by_type(pelfio_t pelfio,int sh_type,psection_t pscn)106 static psection_t elf_find_next_scn_by_type(pelfio_t pelfio, int sh_type, psection_t pscn)
107 {
108     int secno = elfio_get_sections_num(pelfio);
109     int j = 0;
110     if (pscn != NULL) {
111         for (int i = 0; i < secno; i++) {
112             psection_t psection = elfio_get_section_by_index(pelfio, i);
113             if (psection == pscn) {
114                 j = i;
115             }
116         }
117     }
118     for (; j < secno; j++) {
119         psection_t psection = elfio_get_section_by_index(pelfio, j);
120         Elf_Word sec_type = elfio_section_get_type(psection);
121         if (sec_type == sh_type) {
122             return psection;
123         }
124     }
125     return NULL;
126 }
127 #endif
128 
129 struct elf_sym {
130 	const char *name;
131 	GElf_Sym sym;
132 	GElf_Shdr sh;
133 	int ver;
134 	bool hidden;
135 };
136 
137 struct elf_sym_iter {
138 #ifdef  HAVE_LIBELF
139 	Elf *elf;
140 #elif HAVE_ELFIO
141 	pelfio_t elf;
142 	psection_t symsSec;
143 #endif
144 	Elf_Data *syms;
145 	Elf_Data *versyms;
146 	Elf_Data *verdefs;
147 	size_t nr_syms;
148 	size_t strtabidx;
149 	size_t verdef_strtabidx;
150 	size_t next_sym_idx;
151 	struct elf_sym sym;
152 	int st_type;
153 };
154 
155 #ifdef  HAVE_LIBELF
elf_sym_iter_new(struct elf_sym_iter * iter,Elf * elf,const char * binary_path,int sh_type,int st_type)156 static int elf_sym_iter_new(struct elf_sym_iter *iter,
157 			    Elf *elf, const char *binary_path,
158 			    int sh_type, int st_type)
159 {
160 	Elf_Scn *scn = NULL;
161 	GElf_Ehdr ehdr;
162 	GElf_Shdr sh;
163 
164 	memset(iter, 0, sizeof(*iter));
165 
166 	if (!gelf_getehdr(elf, &ehdr)) {
167 		pr_warn("elf: failed to get ehdr from %s: %s\n", binary_path, elf_errmsg(-1));
168 		return -EINVAL;
169 	}
170 
171 	scn = elf_find_next_scn_by_type(elf, sh_type, NULL);
172 	if (!scn) {
173 		pr_debug("elf: failed to find symbol table ELF sections in '%s'\n",
174 			 binary_path);
175 		return -ENOENT;
176 	}
177 
178 	if (!gelf_getshdr(scn, &sh))
179 		return -EINVAL;
180 
181 	iter->strtabidx = sh.sh_link;
182 	iter->syms = elf_getdata(scn, 0);
183 	if (!iter->syms) {
184 		pr_warn("elf: failed to get symbols for symtab section in '%s': %s\n",
185 			binary_path, elf_errmsg(-1));
186 		return -EINVAL;
187 	}
188 	iter->nr_syms = iter->syms->d_size / sh.sh_entsize;
189 	iter->elf = elf;
190 	iter->st_type = st_type;
191 
192 	/* Version symbol table is meaningful to dynsym only */
193 	if (sh_type != SHT_DYNSYM)
194 		return 0;
195 
196 	scn = elf_find_next_scn_by_type(elf, SHT_GNU_versym, NULL);
197 	if (!scn)
198 		return 0;
199 	iter->versyms = elf_getdata(scn, 0);
200 
201 	scn = elf_find_next_scn_by_type(elf, SHT_GNU_verdef, NULL);
202 	if (!scn)
203 		return 0;
204 
205 	iter->verdefs = elf_getdata(scn, 0);
206 	if (!iter->verdefs || !gelf_getshdr(scn, &sh)) {
207 		pr_warn("elf: failed to get verdef ELF section in '%s'\n", binary_path);
208 		return -EINVAL;
209 	}
210 	iter->verdef_strtabidx = sh.sh_link;
211 
212 	return 0;
213 }
214 #elif HAVE_ELFIO
elf_sym_iter_new(struct elf_sym_iter * iter,pelfio_t elf,const char * binary_path,int sh_type,int st_type)215 static int elf_sym_iter_new(struct elf_sym_iter *iter,pelfio_t elf, const char *binary_path, int sh_type, int st_type)
216 {
217 	psection_t pSec = NULL;
218 	memset(iter, 0, sizeof(*iter));
219 	pSec = elf_find_next_scn_by_type(elf, sh_type, NULL);
220 	iter->strtabidx = elfio_section_get_link(pSec);
221 	iter->syms->d_buf = (void*)elfio_section_get_data(pSec);
222 	iter->syms->d_size = elfio_section_get_size(pSec);
223 	if (!iter->syms->d_buf) {
224 		pr_warn("elf: failed to get symbols for symtab section in '%s': %s\n",
225 			binary_path, elf_errmsg(-1));
226 		return -EINVAL;
227 	}
228 	iter->nr_syms = iter->syms->d_size / elfio_section_get_entry_size(pSec);
229 	iter->symsSec = pSec;
230 	iter->elf = elf;
231 	iter->st_type = st_type;
232 	/* Version symbol table is meaningful to dynsym only */
233 	if (sh_type != SHT_DYNSYM)
234 		return 0;
235 	pSec = elf_find_next_scn_by_type(elf, SHT_GNU_versym, NULL);
236 	if (!pSec) {
237 		return 0;
238 	}
239 	iter->versyms->d_buf = (void*)elfio_section_get_data(pSec);
240 	iter->versyms->d_size = elfio_section_get_size(pSec);
241 	pSec = elf_find_next_scn_by_type(elf, SHT_GNU_verdef, NULL);
242 	if (!pSec) {
243 		return 0;
244 	}
245 	iter->verdefs->d_buf = (void*)elfio_section_get_data(pSec);
246 	iter->verdefs->d_size = elfio_section_get_size(pSec);
247 	if (!iter->verdefs->d_buf) {
248 		pr_warn("elf: failed to get verdef ELF section in '%s'\n", binary_path);
249 		return -EINVAL;
250 	}
251 	iter->verdef_strtabidx = elfio_section_get_link(pSec);
252 	return 0;
253 }
254 #endif
255 
256 #ifdef  HAVA_ELFIO
elf_sec_hdr_by_idx(const pelfio_t elf,size_t idx,GElf_Shdr * sheader)257 static GElf_Shdr *elf_sec_hdr_by_idx(const pelfio_t elf, size_t idx, GElf_Shdr *sheader)
258 {
259 	psection_t psection = elfio_get_section_by_index(elf, idx);
260 	sheader->sh_name = elfio_section_get_name_string_offset(psection);
261 	sheader->sh_type = elfio_section_get_type(psection);
262 	sheader->sh_flags = elfio_section_get_flags(psection);
263 	sheader->sh_addr = elfio_section_get_address(psection);
264 	sheader->sh_offset = elfio_section_get_offset(psection);
265 	sheader->sh_size = elfio_section_get_size(psection);
266 	sheader->sh_link = elfio_section_get_link(psection);
267 	sheader->sh_info = elfio_section_get_info(psection);
268 	sheader->sh_addralign = elfio_section_get_addr_align(psection);
269 	sheader->sh_entsize = elfio_section_get_entry_size(psection);
270 	return sheader;
271 }
272 #endif  //HAVA_ELFIO
273 
274 
elf_sym_iter_next(struct elf_sym_iter * iter)275 static struct elf_sym *elf_sym_iter_next(struct elf_sym_iter *iter)
276 {
277 	struct elf_sym *ret = &iter->sym;
278 	GElf_Sym *sym = &ret->sym;
279 	GElf_Versym versym;
280 #ifdef  HAVA_LIBELF
281 	Elf_Scn *sym_scn;
282 #elif HAVE_ELFIO
283 	psection_t sym_scn;
284 #endif
285 	const char *name = NULL;
286 	size_t idx;
287 	for (idx = iter->next_sym_idx; idx < iter->nr_syms; idx++) {
288 #ifdef  HAVA_LIBELF
289 		if (!gelf_getsym(iter->syms, idx, sym))
290 			continue;
291 		if (GELF_ST_TYPE(sym->st_info) != iter->st_type)
292 			continue;
293 		name = elf_strptr(iter->elf, iter->strtabidx, sym->st_name);
294 		if (!name)
295 			continue;
296 		sym_scn = elf_getscn(iter->elf, sym->st_shndx);
297 		if (!sym_scn)
298 			continue;
299 		if (!gelf_getshdr(sym_scn, &ret->sh))
300 			continue;
301 #elif HAVA_ELFIO
302 		if(memcpy(sym,iter->sysms->d_buf + idx,sizeof(GElf_Sym) == NULL) {
303 			continue;
304 		}
305 		if(((sym->st_info) & 0xf) != iter->st_type) {
306 			continue;
307 		}
308 		psection_t psection = elfio_get_section_by_index(iter->elf, iter->strtabidx);
309 		if (!psection)
310 			return -LIBBPF_ERRNO__FORMAT;
311 		pstring_t strstring = elfio_string_section_accessor_new(psection);
312 		name = elfio_string_get_string(strstring, sym->st_name);
313 		if(!name) {
314 			continue;
315 		}
316 		if(!elf_sec_hdr_by_idx(iter->elf, sym->st_shndx, &ret->sh)) {
317 			continue;
318 		}
319 #endif
320 		iter->next_sym_idx = idx + 1;
321 		ret->name = name;
322 		ret->ver = 0;
323 		ret->hidden = false;
324 		if (iter->versyms) {
325 #ifdef  HAVA_LIBELF
326 	if (!gelf_getversym(iter->versyms, idx, &versym))
327 				continue;
328 #elif HAVA_ELFIO
329 		versym = (GElf_Versym)iter->versysm->d_buf[idx];
330 #endif
331 		ret->ver = versym & VERSYM_VERSION;
332 		ret->hidden = versym & VERSYM_HIDDEN;
333 		}
334 		return ret;
335 	}
336 	return NULL;
337 }
338 
elf_get_vername(struct elf_sym_iter * iter,int ver)339 static const char *elf_get_vername(struct elf_sym_iter *iter, int ver)
340 {
341 	GElf_Verdaux verdaux;
342 	GElf_Verdef verdef;
343 	int offset;
344 	if (!iter->verdefs)
345 		return NULL;
346 	offset = 0;
347 #ifdef  HAVE_LIBELF
348 	while (gelf_getverdef(iter->verdefs, offset, &verdef)) {
349 		if (verdef.vd_ndx != ver) {
350 			if (!verdef.vd_next)
351 				break;
352 			offset += verdef.vd_next;
353 			continue;
354 		}
355 	 if (!gelf_getverdaux(iter->verdefs, offset + verdef.vd_aux, &verdaux))
356 			break;
357 		return elf_strptr(iter->elf, iter->verdef_strtabidx, verdaux.vda_name);
358 	}
359 #elif HAVE_ELFIO
360 	while (memcpy(&verdef, (void *)iter->verdefs->d_buf + offset, sizeof(GElf_Verdef)) != NULL) {
361 		if (verdef.vd_ndx != ver) {
362 			if (!verdef.vd_next)
363 				break;
364 			offset += verdef.vd_next;
365 			continue;
366 		}
367 		if(memcpy(&verdaux, (void *)iter->verdefs->d_buf + offset + verdef.vd_aux, sizeof(GElf_Verdaux)) == NULL) {
368 			break;
369 		}
370 		psection_t psection = elfio_get_section_by_index(iter->elf, iter->verdef_strtabidx);
371 		if (!psection)
372 			return NULL;
373 		pstring_t strstring = elfio_string_section_accessor_new(psection);
374 		return elfio_string_get_string(strstring, verdaux.vda_name);
375 	}
376 #endif
377 return NULL;
378 }
379 
symbol_match(struct elf_sym_iter * iter,int sh_type,struct elf_sym * sym,const char * name,size_t name_len,const char * lib_ver)380 static bool symbol_match(struct elf_sym_iter *iter, int sh_type, struct elf_sym *sym,
381 			 const char *name, size_t name_len, const char *lib_ver)
382 {
383 	const char *ver_name;
384 
385 	/* Symbols are in forms of func, func@LIB_VER or func@@LIB_VER
386 	 * make sure the func part matches the user specified name
387 	 */
388 	if (strncmp(sym->name, name, name_len) != 0)
389 		return false;
390 
391 	/* ...but we don't want a search for "foo" to match 'foo2" also, so any
392 	 * additional characters in sname should be of the form "@@LIB".
393 	 */
394 	if (sym->name[name_len] != '\0' && sym->name[name_len] != '@')
395 		return false;
396 
397 	/* If user does not specify symbol version, then we got a match */
398 	if (!lib_ver)
399 		return true;
400 
401 	/* If user specifies symbol version, for dynamic symbols,
402 	 * get version name from ELF verdef section for comparison.
403 	 */
404 	if (sh_type == SHT_DYNSYM) {
405 		ver_name = elf_get_vername(iter, sym->ver);
406 		if (!ver_name)
407 			return false;
408 		return strcmp(ver_name, lib_ver) == 0;
409 	}
410 
411 	/* For normal symbols, it is already in form of func@LIB_VER */
412 	return strcmp(sym->name, name) == 0;
413 }
414 
415 /* Transform symbol's virtual address (absolute for binaries and relative
416  * for shared libs) into file offset, which is what kernel is expecting
417  * for uprobe/uretprobe attachment.
418  * See Documentation/trace/uprobetracer.rst for more details. This is done
419  * by looking up symbol's containing section's header and using iter's virtual
420  * address (sh_addr) and corresponding file offset (sh_offset) to transform
421  * sym.st_value (virtual address) into desired final file offset.
422  */
elf_sym_offset(struct elf_sym * sym)423 static unsigned long elf_sym_offset(struct elf_sym *sym)
424 {
425 	return sym->sym.st_value - sym->sh.sh_addr + sym->sh.sh_offset;
426 }
427 
428 /* Find offset of function name in the provided ELF object. "binary_path" is
429  * the path to the ELF binary represented by "elf", and only used for error
430  * reporting matters. "name" matches symbol name or name@@LIB for library
431  * functions.
432  */
433 #ifdef HAVE_LIBELF
elf_find_func_offset(Elf * elf,const char * binary_path,const char * name)434 long elf_find_func_offset(Elf *elf, const char *binary_path, const char *name)
435 #elif HAVE_ELFIO
436 long elf_find_func_offset(pelfio_t elf, const char *binary_path, const char *name)
437 #endif
438 {
439 	int i, sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB };
440 	const char *at_symbol, *lib_ver;
441 	bool is_shared_lib;
442 	long ret = -ENOENT;
443 	size_t name_len;
444 #ifdef  HAVE_LIBELF
445 	GElf_Ehdr ehdr;
446 
447 	if (!gelf_getehdr(elf, &ehdr)) {
448 		pr_warn("elf: failed to get ehdr from %s: %s\n", binary_path, elf_errmsg(-1));
449 		ret = -LIBBPF_ERRNO__FORMAT;
450 		goto out;
451 	}
452 
453 	/* for shared lib case, we do not need to calculate relative offset */
454 	is_shared_lib = ehdr.e_type == ET_DYN;
455 #elif HAVA_ELFIO
456 	is_shared_lib = (ET_DYN == elfio_get_type(pelfio));
457 #endif
458 	/* Does name specify "@@LIB_VER" or "@LIB_VER" ? */
459 	at_symbol = strchr(name, '@');
460 	if (at_symbol) {
461 		name_len = at_symbol - name;
462 		/* skip second @ if it's @@LIB_VER case */
463 		if (at_symbol[1] == '@')
464 			at_symbol++;
465 		lib_ver = at_symbol + 1;
466 	} else {
467 		name_len = strlen(name);
468 		lib_ver = NULL;
469 	}
470 
471 	/* Search SHT_DYNSYM, SHT_SYMTAB for symbol. This search order is used because if
472 	 * a binary is stripped, it may only have SHT_DYNSYM, and a fully-statically
473 	 * linked binary may not have SHT_DYMSYM, so absence of a section should not be
474 	 * reported as a warning/error.
475 	 */
476 	for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
477 		struct elf_sym_iter iter;
478 		struct elf_sym *sym;
479 		int last_bind = -1;
480 		int cur_bind;
481 
482 		ret = elf_sym_iter_new(&iter, elf, binary_path, sh_types[i], STT_FUNC);
483 		if (ret == -ENOENT)
484 			continue;
485 		if (ret)
486 			goto out;
487 
488 		while ((sym = elf_sym_iter_next(&iter))) {
489 			if (!symbol_match(&iter, sh_types[i], sym, name, name_len, lib_ver))
490 				continue;
491 
492 			cur_bind = GELF_ST_BIND(sym->sym.st_info);
493 
494 			if (ret > 0) {
495 				/* handle multiple matches */
496 				if (elf_sym_offset(sym) == ret) {
497 					/* same offset, no problem */
498 					continue;
499 				} else if (last_bind != STB_WEAK && cur_bind != STB_WEAK) {
500 					/* Only accept one non-weak bind. */
501 					pr_warn("elf: ambiguous match for '%s', '%s' in '%s'\n",
502 						sym->name, name, binary_path);
503 					ret = -LIBBPF_ERRNO__FORMAT;
504 					goto out;
505 				} else if (cur_bind == STB_WEAK) {
506 					/* already have a non-weak bind, and
507 					 * this is a weak bind, so ignore.
508 					 */
509 					continue;
510 				}
511 			}
512 
513 			ret = elf_sym_offset(sym);
514 			last_bind = cur_bind;
515 		}
516 		if (ret > 0)
517 			break;
518 	}
519 
520 	if (ret > 0) {
521 		pr_debug("elf: symbol address match for '%s' in '%s': 0x%lx\n", name, binary_path,
522 			 ret);
523 	} else {
524 		if (ret == 0) {
525 			pr_warn("elf: '%s' is 0 in symtab for '%s': %s\n", name, binary_path,
526 				is_shared_lib ? "should not be 0 in a shared library" :
527 						"try using shared library path instead");
528 			ret = -ENOENT;
529 		} else {
530 			pr_warn("elf: failed to find symbol '%s' in '%s'\n", name, binary_path);
531 		}
532 	}
533 out:
534 	return ret;
535 }
536 
537 /* Find offset of function name in ELF object specified by path. "name" matches
538  * symbol name or name@@LIB for library functions.
539  */
elf_find_func_offset_from_file(const char * binary_path,const char * name)540 long elf_find_func_offset_from_file(const char *binary_path, const char *name)
541 {
542 	struct elf_fd elf_fd;
543 	long ret = -ENOENT;
544 
545 	ret = elf_open(binary_path, &elf_fd);
546 	if (ret)
547 		return ret;
548 	ret = elf_find_func_offset(elf_fd.elf, binary_path, name);
549 	elf_close(&elf_fd);
550 	return ret;
551 }
552 
553 struct symbol {
554 	const char *name;
555 	int bind;
556 	int idx;
557 };
558 
symbol_cmp(const void * a,const void * b)559 static int symbol_cmp(const void *a, const void *b)
560 {
561 	const struct symbol *sym_a = a;
562 	const struct symbol *sym_b = b;
563 
564 	return strcmp(sym_a->name, sym_b->name);
565 }
566 
567 /*
568  * Return offsets in @poffsets for symbols specified in @syms array argument.
569  * On success returns 0 and offsets are returned in allocated array with @cnt
570  * size, that needs to be released by the caller.
571  */
elf_resolve_syms_offsets(const char * binary_path,int cnt,const char ** syms,unsigned long ** poffsets)572 int elf_resolve_syms_offsets(const char *binary_path, int cnt,
573 			     const char **syms, unsigned long **poffsets)
574 {
575 	int sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB };
576 	int err = 0, i, cnt_done = 0;
577 	unsigned long *offsets;
578 	struct symbol *symbols;
579 	struct elf_fd elf_fd;
580 
581 	err = elf_open(binary_path, &elf_fd);
582 	if (err)
583 		return err;
584 
585 	offsets = calloc(cnt, sizeof(*offsets));
586 	symbols = calloc(cnt, sizeof(*symbols));
587 
588 	if (!offsets || !symbols) {
589 		err = -ENOMEM;
590 		goto out;
591 	}
592 
593 	for (i = 0; i < cnt; i++) {
594 		symbols[i].name = syms[i];
595 		symbols[i].idx = i;
596 	}
597 
598 	qsort(symbols, cnt, sizeof(*symbols), symbol_cmp);
599 
600 	for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
601 		struct elf_sym_iter iter;
602 		struct elf_sym *sym;
603 
604 		err = elf_sym_iter_new(&iter, elf_fd.elf, binary_path, sh_types[i], STT_FUNC);
605 		if (err == -ENOENT)
606 			continue;
607 		if (err)
608 			goto out;
609 
610 		while ((sym = elf_sym_iter_next(&iter))) {
611 			unsigned long sym_offset = elf_sym_offset(sym);
612 			int bind = GELF_ST_BIND(sym->sym.st_info);
613 			struct symbol *found, tmp = {
614 				.name = sym->name,
615 			};
616 			unsigned long *offset;
617 
618 			found = bsearch(&tmp, symbols, cnt, sizeof(*symbols), symbol_cmp);
619 			if (!found)
620 				continue;
621 
622 			offset = &offsets[found->idx];
623 			if (*offset > 0) {
624 				/* same offset, no problem */
625 				if (*offset == sym_offset)
626 					continue;
627 				/* handle multiple matches */
628 				if (found->bind != STB_WEAK && bind != STB_WEAK) {
629 					/* Only accept one non-weak bind. */
630 					pr_warn("elf: ambiguous match found '%s@%lu' in '%s' previous offset %lu\n",
631 						sym->name, sym_offset, binary_path, *offset);
632 					err = -ESRCH;
633 					goto out;
634 				} else if (bind == STB_WEAK) {
635 					/* already have a non-weak bind, and
636 					 * this is a weak bind, so ignore.
637 					 */
638 					continue;
639 				}
640 			} else {
641 				cnt_done++;
642 			}
643 			*offset = sym_offset;
644 			found->bind = bind;
645 		}
646 	}
647 
648 	if (cnt != cnt_done) {
649 		err = -ENOENT;
650 		goto out;
651 	}
652 
653 	*poffsets = offsets;
654 
655 out:
656 	free(symbols);
657 	if (err)
658 		free(offsets);
659 	elf_close(&elf_fd);
660 	return err;
661 }
662 
663 /*
664  * Return offsets in @poffsets for symbols specified by @pattern argument.
665  * On success returns 0 and offsets are returned in allocated @poffsets
666  * array with the @pctn size, that needs to be released by the caller.
667  */
elf_resolve_pattern_offsets(const char * binary_path,const char * pattern,unsigned long ** poffsets,size_t * pcnt)668 int elf_resolve_pattern_offsets(const char *binary_path, const char *pattern,
669 				unsigned long **poffsets, size_t *pcnt)
670 {
671 	int sh_types[2] = { SHT_SYMTAB, SHT_DYNSYM };
672 	unsigned long *offsets = NULL;
673 	size_t cap = 0, cnt = 0;
674 	struct elf_fd elf_fd;
675 	int err = 0, i;
676 
677 	err = elf_open(binary_path, &elf_fd);
678 	if (err)
679 		return err;
680 
681 	for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
682 		struct elf_sym_iter iter;
683 		struct elf_sym *sym;
684 
685 		err = elf_sym_iter_new(&iter, elf_fd.elf, binary_path, sh_types[i], STT_FUNC);
686 		if (err == -ENOENT)
687 			continue;
688 		if (err)
689 			goto out;
690 
691 		while ((sym = elf_sym_iter_next(&iter))) {
692 			if (!glob_match(sym->name, pattern))
693 				continue;
694 
695 			err = libbpf_ensure_mem((void **) &offsets, &cap, sizeof(*offsets),
696 						cnt + 1);
697 			if (err)
698 				goto out;
699 
700 			offsets[cnt++] = elf_sym_offset(sym);
701 		}
702 
703 		/* If we found anything in the first symbol section,
704 		 * do not search others to avoid duplicates.
705 		 */
706 		if (cnt)
707 			break;
708 	}
709 
710 	if (cnt) {
711 		*poffsets = offsets;
712 		*pcnt = cnt;
713 	} else {
714 		err = -ENOENT;
715 	}
716 
717 out:
718 	if (err)
719 		free(offsets);
720 	elf_close(&elf_fd);
721 	return err;
722 }
723