Re: [PATCH RFC v2 bpf-next 5/9] selftests/bpf: Add read_buildid function

Andrii Nakryiko
2023-03-08T01:23:09Z

On Tue, Feb 28, 2023 at 1:33 AM Jiri Olsa <jolsa@kernel.org> wrote:
>
> Adding read_build_id function that parses out build id from
> specified binary.
>
> It will replace extract_build_id and also be used in following
> changes.
>
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> ---
> tools/testing/selftests/bpf/trace_helpers.c | 98 +++++++++++++++++++++
> tools/testing/selftests/bpf/trace_helpers.h | 5 ++
> 2 files changed, 103 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c
> index 09a16a77bae4..c10e16626cd3 100644
> --- a/tools/testing/selftests/bpf/trace_helpers.c
> +++ b/tools/testing/selftests/bpf/trace_helpers.c
> @@ -11,6 +11,9 @@
> #include <linux/perf_event.h>
> #include <sys/mman.h>
> #include "trace_helpers.h"
> +#include <linux/limits.h>
> +#include <libelf.h>
> +#include <gelf.h>
>
> #define DEBUGFS "/sys/kernel/debug/tracing/"
>
> @@ -230,3 +233,98 @@ ssize_t get_rel_offset(uintptr_t addr)
> fclose(f);
> return -EINVAL;
> }
> +
> +static int
> +parse_build_id_buf(const void *note_start, Elf32_Word note_size,
> + char *build_id)
> +{
> + Elf32_Word note_offs = 0, new_offs;
> +
> + while (note_offs + sizeof(Elf32_Nhdr) < note_size) {
> + Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs);
> +
> + if (nhdr->n_type == 3 &&
> + nhdr->n_namesz == sizeof("GNU") &&
> + !strcmp((char *)(nhdr + 1), "GNU") &&
> + nhdr->n_descsz > 0 &&
> + nhdr->n_descsz <= BPF_BUILD_ID_SIZE) {
> + memcpy(build_id, note_start + note_offs +
> + ALIGN(sizeof("GNU"), 4) + sizeof(Elf32_Nhdr),
> + nhdr->n_descsz);
> + memset(build_id + nhdr->n_descsz, 0,
> + BPF_BUILD_ID_SIZE - nhdr->n_descsz);

I won't count :) but if something fits within 100 characters, please
keep it on single line

> + return (int) nhdr->n_descsz;
> + }
> +
> + new_offs = note_offs + sizeof(Elf32_Nhdr) +
> + ALIGN(nhdr->n_namesz, 4) + ALIGN(nhdr->n_descsz, 4);
> +
> + if (new_offs >= note_size)
> + break;
> + note_offs = new_offs;
> + }
> +
> + return -EINVAL;
> +}
> +
> +/* Reads binary from *path* file and returns it in the *build_id*
> + * which is expected to be at least BPF_BUILD_ID_SIZE bytes.
> + * Returns size of build id on success. On error the error value
> + * is returned.
> + */
> +int read_build_id(const char *path, char *build_id)
> +{
> + int fd, err = -EINVAL;
> + Elf *elf = NULL;
> + GElf_Ehdr ehdr;
> + size_t max, i;
> +
> + fd = open(path, O_RDONLY | O_CLOEXEC);
> + if (fd < 0)
> + return -errno;
> +
> + (void)elf_version(EV_CURRENT);
> +
> + elf = elf_begin(fd, ELF_C_READ, NULL);
> + if (!elf)
> + goto out;
> +
> + if (elf_kind(elf) != ELF_K_ELF)
> + goto out;
> +
> + if (gelf_getehdr(elf, &ehdr) == NULL)
> + goto out;
> +
> + if (ehdr.e_ident[EI_CLASS] != ELFCLASS64)
> + goto out;
> +
> + for (i = 0; i < ehdr.e_phnum; i++) {
> + GElf_Phdr mem, *phdr;
> + char *data;
> +
> + phdr = gelf_getphdr(elf, i, &mem);
> + if (!phdr)
> + goto out;
> +
> + if (phdr->p_type != PT_NOTE)
> + continue;
> +
> + data = elf_rawfile(elf, &max);
> + if (!data)
> + goto out;
> +
> + if (phdr->p_offset >= max ||
> + (phdr->p_offset + phdr->p_memsz >= max))
> + goto out;
> +
> + err = parse_build_id_buf(data + phdr->p_offset, phdr->p_memsz, build_id);
> + if (err > 0)
> + goto out;
> + }
> +
> +out:
> + if (elf)
> + elf_end(elf);
> + close(fd);
> + return err;
> +}
> diff --git a/tools/testing/selftests/bpf/trace_helpers.h b/tools/testing/selftests/bpf/trace_helpers.h
> index 53efde0e2998..50b2cc498ba7 100644
> --- a/tools/testing/selftests/bpf/trace_helpers.h
> +++ b/tools/testing/selftests/bpf/trace_helpers.h
> @@ -4,6 +4,9 @@
>
> #include <bpf/libbpf.h>
>
> +#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
> +#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))

nit: I know these are macros, but why would you first use __ALIGN_MASK
and then #define it? swap them?

> +
> struct ksym {
> long addr;
> char *name;
> @@ -23,4 +26,6 @@ void read_trace_pipe(void);
> ssize_t get_uprobe_offset(const void *addr);
> ssize_t get_rel_offset(uintptr_t addr);
>
> +int read_build_id(const char *path, char *build_id);
> +
> #endif
> --
> 2.39.2
>