• 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  HAVE_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  //HAVE_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  HAVE_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  HAVE_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 HAVE_ELFIO
302 		if(memcpy(sym,iter->syms->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 NULL;
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  HAVE_LIBELF
326 	if (!gelf_getversym(iter->versyms, idx, &versym))
327 				continue;
328 #elif HAVE_ELFIO
329 		if (memcpy(&versym, iter->versyms->d_buf + idx, sizeof(GElf_Versym)) == NULL) {
330 			continue;
331 		}
332 #endif
333 		ret->ver = versym & VERSYM_VERSION;
334 		ret->hidden = versym & VERSYM_HIDDEN;
335 		}
336 		return ret;
337 	}
338 	return NULL;
339 }
340 
elf_get_vername(struct elf_sym_iter * iter,int ver)341 static const char *elf_get_vername(struct elf_sym_iter *iter, int ver)
342 {
343 	GElf_Verdaux verdaux;
344 	GElf_Verdef verdef;
345 	int offset;
346 	if (!iter->verdefs)
347 		return NULL;
348 	offset = 0;
349 #ifdef  HAVE_LIBELF
350 	while (gelf_getverdef(iter->verdefs, offset, &verdef)) {
351 		if (verdef.vd_ndx != ver) {
352 			if (!verdef.vd_next)
353 				break;
354 			offset += verdef.vd_next;
355 			continue;
356 		}
357 	 if (!gelf_getverdaux(iter->verdefs, offset + verdef.vd_aux, &verdaux))
358 			break;
359 		return elf_strptr(iter->elf, iter->verdef_strtabidx, verdaux.vda_name);
360 	}
361 #elif HAVE_ELFIO
362 	while (memcpy(&verdef, (void *)iter->verdefs->d_buf + offset, sizeof(GElf_Verdef)) != NULL) {
363 		if (verdef.vd_ndx != ver) {
364 			if (!verdef.vd_next)
365 				break;
366 			offset += verdef.vd_next;
367 			continue;
368 		}
369 		if(memcpy(&verdaux, (void *)iter->verdefs->d_buf + offset + verdef.vd_aux, sizeof(GElf_Verdaux)) == NULL) {
370 			break;
371 		}
372 		psection_t psection = elfio_get_section_by_index(iter->elf, iter->verdef_strtabidx);
373 		if (!psection)
374 			return NULL;
375 		pstring_t strstring = elfio_string_section_accessor_new(psection);
376 		return elfio_string_get_string(strstring, verdaux.vda_name);
377 	}
378 #endif
379 return NULL;
380 }
381 
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)382 static bool symbol_match(struct elf_sym_iter *iter, int sh_type, struct elf_sym *sym,
383 			 const char *name, size_t name_len, const char *lib_ver)
384 {
385 	const char *ver_name;
386 
387 	/* Symbols are in forms of func, func@LIB_VER or func@@LIB_VER
388 	 * make sure the func part matches the user specified name
389 	 */
390 	if (strncmp(sym->name, name, name_len) != 0)
391 		return false;
392 
393 	/* ...but we don't want a search for "foo" to match 'foo2" also, so any
394 	 * additional characters in sname should be of the form "@@LIB".
395 	 */
396 	if (sym->name[name_len] != '\0' && sym->name[name_len] != '@')
397 		return false;
398 
399 	/* If user does not specify symbol version, then we got a match */
400 	if (!lib_ver)
401 		return true;
402 
403 	/* If user specifies symbol version, for dynamic symbols,
404 	 * get version name from ELF verdef section for comparison.
405 	 */
406 	if (sh_type == SHT_DYNSYM) {
407 		ver_name = elf_get_vername(iter, sym->ver);
408 		if (!ver_name)
409 			return false;
410 		return strcmp(ver_name, lib_ver) == 0;
411 	}
412 
413 	/* For normal symbols, it is already in form of func@LIB_VER */
414 	return strcmp(sym->name, name) == 0;
415 }
416 
417 /* Transform symbol's virtual address (absolute for binaries and relative
418  * for shared libs) into file offset, which is what kernel is expecting
419  * for uprobe/uretprobe attachment.
420  * See Documentation/trace/uprobetracer.rst for more details. This is done
421  * by looking up symbol's containing section's header and using iter's virtual
422  * address (sh_addr) and corresponding file offset (sh_offset) to transform
423  * sym.st_value (virtual address) into desired final file offset.
424  */
elf_sym_offset(struct elf_sym * sym)425 static unsigned long elf_sym_offset(struct elf_sym *sym)
426 {
427 	return sym->sym.st_value - sym->sh.sh_addr + sym->sh.sh_offset;
428 }
429 
430 /* Find offset of function name in the provided ELF object. "binary_path" is
431  * the path to the ELF binary represented by "elf", and only used for error
432  * reporting matters. "name" matches symbol name or name@@LIB for library
433  * functions.
434  */
435 #ifdef HAVE_LIBELF
elf_find_func_offset(Elf * elf,const char * binary_path,const char * name)436 long elf_find_func_offset(Elf *elf, const char *binary_path, const char *name)
437 #elif HAVE_ELFIO
438 long elf_find_func_offset(pelfio_t elf, const char *binary_path, const char *name)
439 #endif
440 {
441 	int i, sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB };
442 	const char *at_symbol, *lib_ver;
443 	bool is_shared_lib;
444 	long ret = -ENOENT;
445 	size_t name_len;
446 #ifdef  HAVE_LIBELF
447 	GElf_Ehdr ehdr;
448 
449 	if (!gelf_getehdr(elf, &ehdr)) {
450 		pr_warn("elf: failed to get ehdr from %s: %s\n", binary_path, elf_errmsg(-1));
451 		ret = -LIBBPF_ERRNO__FORMAT;
452 		goto out;
453 	}
454 
455 	/* for shared lib case, we do not need to calculate relative offset */
456 	is_shared_lib = ehdr.e_type == ET_DYN;
457 #elif HAVE_ELFIO
458 	is_shared_lib = (ET_DYN == elfio_get_type(elf));
459 #endif
460 	/* Does name specify "@@LIB_VER" or "@LIB_VER" ? */
461 	at_symbol = strchr(name, '@');
462 	if (at_symbol) {
463 		name_len = at_symbol - name;
464 		/* skip second @ if it's @@LIB_VER case */
465 		if (at_symbol[1] == '@')
466 			at_symbol++;
467 		lib_ver = at_symbol + 1;
468 	} else {
469 		name_len = strlen(name);
470 		lib_ver = NULL;
471 	}
472 
473 	/* Search SHT_DYNSYM, SHT_SYMTAB for symbol. This search order is used because if
474 	 * a binary is stripped, it may only have SHT_DYNSYM, and a fully-statically
475 	 * linked binary may not have SHT_DYMSYM, so absence of a section should not be
476 	 * reported as a warning/error.
477 	 */
478 	for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
479 		struct elf_sym_iter iter;
480 		struct elf_sym *sym;
481 		int last_bind = -1;
482 		int cur_bind;
483 
484 		ret = elf_sym_iter_new(&iter, elf, binary_path, sh_types[i], STT_FUNC);
485 		if (ret == -ENOENT)
486 			continue;
487 		if (ret)
488 			goto out;
489 
490 		while ((sym = elf_sym_iter_next(&iter))) {
491 			if (!symbol_match(&iter, sh_types[i], sym, name, name_len, lib_ver))
492 				continue;
493 
494 			cur_bind = GELF_ST_BIND(sym->sym.st_info);
495 
496 			if (ret > 0) {
497 				/* handle multiple matches */
498 				if (elf_sym_offset(sym) == ret) {
499 					/* same offset, no problem */
500 					continue;
501 				} else if (last_bind != STB_WEAK && cur_bind != STB_WEAK) {
502 					/* Only accept one non-weak bind. */
503 					pr_warn("elf: ambiguous match for '%s', '%s' in '%s'\n",
504 						sym->name, name, binary_path);
505 					ret = -LIBBPF_ERRNO__FORMAT;
506 					goto out;
507 				} else if (cur_bind == STB_WEAK) {
508 					/* already have a non-weak bind, and
509 					 * this is a weak bind, so ignore.
510 					 */
511 					continue;
512 				}
513 			}
514 
515 			ret = elf_sym_offset(sym);
516 			last_bind = cur_bind;
517 		}
518 		if (ret > 0)
519 			break;
520 	}
521 
522 	if (ret > 0) {
523 		pr_debug("elf: symbol address match for '%s' in '%s': 0x%lx\n", name, binary_path,
524 			 ret);
525 	} else {
526 		if (ret == 0) {
527 			pr_warn("elf: '%s' is 0 in symtab for '%s': %s\n", name, binary_path,
528 				is_shared_lib ? "should not be 0 in a shared library" :
529 						"try using shared library path instead");
530 			ret = -ENOENT;
531 		} else {
532 			pr_warn("elf: failed to find symbol '%s' in '%s'\n", name, binary_path);
533 		}
534 	}
535 out:
536 	return ret;
537 }
538 
539 /* Find offset of function name in ELF object specified by path. "name" matches
540  * symbol name or name@@LIB for library functions.
541  */
elf_find_func_offset_from_file(const char * binary_path,const char * name)542 long elf_find_func_offset_from_file(const char *binary_path, const char *name)
543 {
544 	struct elf_fd elf_fd;
545 	long ret = -ENOENT;
546 
547 	ret = elf_open(binary_path, &elf_fd);
548 	if (ret)
549 		return ret;
550 	ret = elf_find_func_offset(elf_fd.elf, binary_path, name);
551 	elf_close(&elf_fd);
552 	return ret;
553 }
554 
555 struct symbol {
556 	const char *name;
557 	int bind;
558 	int idx;
559 };
560 
symbol_cmp(const void * a,const void * b)561 static int symbol_cmp(const void *a, const void *b)
562 {
563 	const struct symbol *sym_a = a;
564 	const struct symbol *sym_b = b;
565 
566 	return strcmp(sym_a->name, sym_b->name);
567 }
568 
569 /*
570  * Return offsets in @poffsets for symbols specified in @syms array argument.
571  * On success returns 0 and offsets are returned in allocated array with @cnt
572  * size, that needs to be released by the caller.
573  */
elf_resolve_syms_offsets(const char * binary_path,int cnt,const char ** syms,unsigned long ** poffsets)574 int elf_resolve_syms_offsets(const char *binary_path, int cnt,
575 			     const char **syms, unsigned long **poffsets)
576 {
577 	int sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB };
578 	int err = 0, i, cnt_done = 0;
579 	unsigned long *offsets;
580 	struct symbol *symbols;
581 	struct elf_fd elf_fd;
582 
583 	err = elf_open(binary_path, &elf_fd);
584 	if (err)
585 		return err;
586 
587 	offsets = calloc(cnt, sizeof(*offsets));
588 	symbols = calloc(cnt, sizeof(*symbols));
589 
590 	if (!offsets || !symbols) {
591 		err = -ENOMEM;
592 		goto out;
593 	}
594 
595 	for (i = 0; i < cnt; i++) {
596 		symbols[i].name = syms[i];
597 		symbols[i].idx = i;
598 	}
599 
600 	qsort(symbols, cnt, sizeof(*symbols), symbol_cmp);
601 
602 	for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
603 		struct elf_sym_iter iter;
604 		struct elf_sym *sym;
605 
606 		err = elf_sym_iter_new(&iter, elf_fd.elf, binary_path, sh_types[i], STT_FUNC);
607 		if (err == -ENOENT)
608 			continue;
609 		if (err)
610 			goto out;
611 
612 		while ((sym = elf_sym_iter_next(&iter))) {
613 			unsigned long sym_offset = elf_sym_offset(sym);
614 			int bind = GELF_ST_BIND(sym->sym.st_info);
615 			struct symbol *found, tmp = {
616 				.name = sym->name,
617 			};
618 			unsigned long *offset;
619 
620 			found = bsearch(&tmp, symbols, cnt, sizeof(*symbols), symbol_cmp);
621 			if (!found)
622 				continue;
623 
624 			offset = &offsets[found->idx];
625 			if (*offset > 0) {
626 				/* same offset, no problem */
627 				if (*offset == sym_offset)
628 					continue;
629 				/* handle multiple matches */
630 				if (found->bind != STB_WEAK && bind != STB_WEAK) {
631 					/* Only accept one non-weak bind. */
632 					pr_warn("elf: ambiguous match found '%s@%lu' in '%s' previous offset %lu\n",
633 						sym->name, sym_offset, binary_path, *offset);
634 					err = -ESRCH;
635 					goto out;
636 				} else if (bind == STB_WEAK) {
637 					/* already have a non-weak bind, and
638 					 * this is a weak bind, so ignore.
639 					 */
640 					continue;
641 				}
642 			} else {
643 				cnt_done++;
644 			}
645 			*offset = sym_offset;
646 			found->bind = bind;
647 		}
648 	}
649 
650 	if (cnt != cnt_done) {
651 		err = -ENOENT;
652 		goto out;
653 	}
654 
655 	*poffsets = offsets;
656 
657 out:
658 	free(symbols);
659 	if (err)
660 		free(offsets);
661 	elf_close(&elf_fd);
662 	return err;
663 }
664 
665 /*
666  * Return offsets in @poffsets for symbols specified by @pattern argument.
667  * On success returns 0 and offsets are returned in allocated @poffsets
668  * array with the @pctn size, that needs to be released by the caller.
669  */
elf_resolve_pattern_offsets(const char * binary_path,const char * pattern,unsigned long ** poffsets,size_t * pcnt)670 int elf_resolve_pattern_offsets(const char *binary_path, const char *pattern,
671 				unsigned long **poffsets, size_t *pcnt)
672 {
673 	int sh_types[2] = { SHT_SYMTAB, SHT_DYNSYM };
674 	unsigned long *offsets = NULL;
675 	size_t cap = 0, cnt = 0;
676 	struct elf_fd elf_fd;
677 	int err = 0, i;
678 
679 	err = elf_open(binary_path, &elf_fd);
680 	if (err)
681 		return err;
682 
683 	for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
684 		struct elf_sym_iter iter;
685 		struct elf_sym *sym;
686 
687 		err = elf_sym_iter_new(&iter, elf_fd.elf, binary_path, sh_types[i], STT_FUNC);
688 		if (err == -ENOENT)
689 			continue;
690 		if (err)
691 			goto out;
692 
693 		while ((sym = elf_sym_iter_next(&iter))) {
694 			if (!glob_match(sym->name, pattern))
695 				continue;
696 
697 			err = libbpf_ensure_mem((void **) &offsets, &cap, sizeof(*offsets),
698 						cnt + 1);
699 			if (err)
700 				goto out;
701 
702 			offsets[cnt++] = elf_sym_offset(sym);
703 		}
704 
705 		/* If we found anything in the first symbol section,
706 		 * do not search others to avoid duplicates.
707 		 */
708 		if (cnt)
709 			break;
710 	}
711 
712 	if (cnt) {
713 		*poffsets = offsets;
714 		*pcnt = cnt;
715 	} else {
716 		err = -ENOENT;
717 	}
718 
719 out:
720 	if (err)
721 		free(offsets);
722 	elf_close(&elf_fd);
723 	return err;
724 }
725