From patchwork Thu Mar 16 17:01:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13178092 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79D96C6FD19 for ; Thu, 16 Mar 2023 17:03:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1C5EF940008; Thu, 16 Mar 2023 13:03:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1765E900002; Thu, 16 Mar 2023 13:03:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0658A940008; Thu, 16 Mar 2023 13:03:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id EC238900002 for ; Thu, 16 Mar 2023 13:03:05 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C319A1414CE for ; Thu, 16 Mar 2023 17:03:05 +0000 (UTC) X-FDA: 80575381530.17.F4C76EE Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf03.hostedemail.com (Postfix) with ESMTP id 6F2B520018 for ; Thu, 16 Mar 2023 17:03:03 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GGxc9yi2; spf=pass (imf03.hostedemail.com: domain of jolsa@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=jolsa@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678986183; a=rsa-sha256; cv=none; b=NFOgMiJBrq61NA6tSqaS0vPrs3NBX6Qj3iQJ7nvQkw5REBxTsGGMrO0MHdHCPXpO39rMv0 S4jCIYEP3yYkaceFwkmrKA5gl5IF5ZpOjhiFYkUe46oM87mt+W7yzMca2wC7dXJzrCzjOo k2g+Yhz6LKdIJW6zrV/CgmyXOutlf/E= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GGxc9yi2; spf=pass (imf03.hostedemail.com: domain of jolsa@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=jolsa@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678986183; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=6TycWHtNki/tCukF6ZcZrfhfnkxjrpC482gZ0vZgG4c=; b=OCokiBB9S1/LTR1WPqm8EDR2ZRZHzVPXL5GXrmvEeSGjzIdiXsNssvHz0luCvLI0RmzMv7 yRbdIIjZ8aOK2JwFuhG03sHf2aTYAXXF/rgQmVKyRXCpBIgN8Tpbgnb9MuDdb8xBlXdzvQ xaBwo0ZxhPRivluaY7PFyYFGzGUXbgM= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 1EE7CB820AD; Thu, 16 Mar 2023 17:03:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 84B9BC433EF; Thu, 16 Mar 2023 17:02:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678986180; bh=Rj6UdNWuOo8DkLviQii3I1+Tk2Kvc/67ARrHkQm/dfw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GGxc9yi28eFc9bY1ajwWe10NyIStMwt4YOzdhNM/wiX7yOjelaepeo5UXwJfAytWI lDik7EeGavF+ShDZ3r/gpO2e1n4/59BQSSW8iv6Z7JNAXcztJowDqIeD/qF/a5nuNf QtVkOWTQ+AKS6CyZBXVFQ7EsGqj70MeAYMnQIKquZwZBIZPDhvAblZJFyFBmKbUnJP rH/8mHFzO8LPJnWLCse6i6Z32V7YnpbZh4+vysz014gw+q2mzA7e0t0ec7lFSilRnj shtyTlk6ESVFdmTJGfIG5cKbon9GjIAevOi6p1Ee6Sq9SEUJ6lgRL/cKjUQpcFHlNO jrdFLxifXhH/A== From: Jiri Olsa To: Alexei Starovoitov , Andrii Nakryiko , Hao Luo , Andrew Morton , Alexander Viro , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Matthew Wilcox Cc: bpf@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-perf-users@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Daniel Borkmann , Namhyung Kim , Dave Chinner Subject: [PATCHv3 bpf-next 5/9] selftests/bpf: Add read_buildid function Date: Thu, 16 Mar 2023 18:01:45 +0100 Message-Id: <20230316170149.4106586-6-jolsa@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230316170149.4106586-1-jolsa@kernel.org> References: <20230316170149.4106586-1-jolsa@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 6F2B520018 X-Rspamd-Server: rspam01 X-Stat-Signature: 9ka7oq8ho4srzdddhcnizr3qbwgkzmeo X-HE-Tag: 1678986183-653790 X-HE-Meta: U2FsdGVkX19lXCJK/M2cqtVliwYpl/K43ETtLuvKdRMwU76QX/uWusFWH234m/aUVbxr9XLdIbYEdgudxJqTjH5aBIVNOr11f5/5rAxD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3Q4AIZ2jWwpxbG19PlR6IBbWUnSCrb9oNEeLeE/cm44TCXAoYh/cUWJxLmvW7//YNnmF1f9BEMnWFAE5ngX4QZghGRrj4gyz++eT8xf4iMiKNaC4331glINHcYJI2t1C6EB4fsOy4CCpPIMSA/aZc8qyy4a6huhst0+2XFtGqVi+VckCh+96YUwGBjP9oQ+sPu7ebe+50uHSq83EwqumFAgFxd0RI6pFrhcYIz8goEG1Jk6ARvjSSZYwuLukdf1T683LwJcfk5ZzCa7tvELLYRuxgJsF/5 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- tools/testing/selftests/bpf/trace_helpers.c | 86 +++++++++++++++++++++ tools/testing/selftests/bpf/trace_helpers.h | 5 ++ 2 files changed, 91 insertions(+) diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c index 934bf28fc888..72b38a41f574 100644 --- a/tools/testing/selftests/bpf/trace_helpers.c +++ b/tools/testing/selftests/bpf/trace_helpers.c @@ -11,6 +11,9 @@ #include #include #include "trace_helpers.h" +#include +#include +#include #define TRACEFS_PIPE "/sys/kernel/tracing/trace_pipe" #define DEBUGFS_PIPE "/sys/kernel/debug/tracing/trace_pipe" @@ -234,3 +237,86 @@ 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); + 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; + err = -EINVAL; + } + +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..bc3b92057033 100644 --- a/tools/testing/selftests/bpf/trace_helpers.h +++ b/tools/testing/selftests/bpf/trace_helpers.h @@ -4,6 +4,9 @@ #include +#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask)) +#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1) + 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