On Thu, Jun 22, 2023 at 05:33:33PM -0700, Andrii Nakryiko wrote: > On Tue, Jun 20, 2023 at 1:37 AM Jiri Olsa <jolsa@xxxxxxxxxx> wrote: > > > > Adding open_elf/close_elf functions and using it in > > elf_find_func_offset_from_file function. It will be > > used in following changes to save some code. > > > > Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx> > > --- > > tools/lib/bpf/libbpf.c | 62 ++++++++++++++++++++++++++++++------------ > > 1 file changed, 44 insertions(+), 18 deletions(-) > > > > we should definitely move all this into separate elf.c file right, also we could use this in usdt_manager_attach_usdt as well > > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > > index cdac368c7ce1..30d9e3b69114 100644 > > --- a/tools/lib/bpf/libbpf.c > > +++ b/tools/lib/bpf/libbpf.c > > @@ -10927,6 +10927,45 @@ static struct elf_symbol *elf_symbol_iter_next(struct elf_symbol_iter *iter) > > return ret; > > } > > > > +struct elf_fd { > > + Elf *elf; > > + int fd; > > +}; > > + > > +static int open_elf(const char *binary_path, struct elf_fd *elf_fd) > > +{ > > + char errmsg[STRERR_BUFSIZE]; > > + int fd, ret; > > + Elf *elf; > > + > > + if (elf_version(EV_CURRENT) == EV_NONE) { > > + pr_warn("failed to init libelf for %s\n", binary_path); > > + return -LIBBPF_ERRNO__LIBELF; > > + } > > + fd = open(binary_path, O_RDONLY | O_CLOEXEC); > > + if (fd < 0) { > > + ret = -errno; > > + pr_warn("failed to open %s: %s\n", binary_path, > > + libbpf_strerror_r(ret, errmsg, sizeof(errmsg))); > > let's add "elf: " prefix for consistency? ok jirka