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