Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp112616pxm; Tue, 22 Feb 2022 18:09:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJyiT6dl4Pkq7Z1z2BpPZQf9KYXivMdCOJEt1qyTyMavK4dyCxj6gG/M3ozmwbJpLihwDz+u X-Received: by 2002:a17:906:564d:b0:6cf:54ec:72ca with SMTP id v13-20020a170906564d00b006cf54ec72camr20883481ejr.110.1645582157128; Tue, 22 Feb 2022 18:09:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645582157; cv=none; d=google.com; s=arc-20160816; b=TVX9e766OrI1P9HrPHpvGPFrCRnR2+RCmXdKHMU7j3aFDPYacYU9VMikk0TGZd+PAO 0CXFRNzRsRmj3W/9ZuqtRAPJsiUyqF8wCIrpNWO99LBNCvICpCYnMB6Lk7up/xXtuEdn 58hV8hTBJa+o2o9tv3FLO19FM+OBGygX+j8DqhbgBpYWkIuFhBdg907Nyp/mPHvu3EuL rN+R8aqPmWBQeK5wuG2/f2pHCSZ+YrYvH2Ya4VF3jGF+SNelsb7jp9w+5Q9bgD/FXla3 vRtF4CiyFOvh7hFSrZTB9TT6sKchKSFldDlgCvquZeEWNvbrie6JjjyhotVty/l/aqob VsOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=essXx35OKQ3VdwyHYqjCi7T7cGBHwj9F0+c8Nnuym1w=; b=wOr4dnrAdtkQ/f44L5kqcD5jSZPE4jJkdj/eYSkuLmh1XbzL8g2aSWUvXE0xIIKE5z w/b0NDWgw5+PfTzt+9q3mLoxy0lZS18hJK96jsA08WnbLMRVsR0rl47615A+CQT9hxNS 9LoRpahdJL+oW5ytIGaTqXL0+vClI/M2Ih6IM+/K1x2ZyMbNJjlo7U0AoZpEIz5X09tX 7nIaqN32iDiG6FVdY9GDBx/EjsnZzoQh/s+I3C6o49xvcuRdo/XERaC39rKeIOLtEHID tpTqh6RwpY/VFelhlAZQZmX1zafpjwevk4lSOpm92E5PDGcIXvtajf4q/2gvxMN8n/yn zuOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lRLmw5qF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j19si10261544ejy.636.2022.02.22.18.08.54; Tue, 22 Feb 2022 18:09:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lRLmw5qF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230102AbiBVRIJ (ORCPT + 99 others); Tue, 22 Feb 2022 12:08:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234628AbiBVRH6 (ORCPT ); Tue, 22 Feb 2022 12:07:58 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5445F3AA56; Tue, 22 Feb 2022 09:07:31 -0800 (PST) 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 dfw.source.kernel.org (Postfix) with ESMTPS id D497A60F89; Tue, 22 Feb 2022 17:07:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CEA5BC340E8; Tue, 22 Feb 2022 17:07:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1645549650; bh=buDCVi7z40Yberf/fZYcIZI7mBYRvC9QlP2EJJMj2Qo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lRLmw5qFksGZASk6ANSXw3UavHitGE7enOt7ZOAgM889Og9SzDmsuZv8O+Wsa5Y7a uKTlha1SMCVuFVMoGbhgr2u9ptS0ebm/n2lTOESk8XKLSH+tvZCHpi5oVpvTEx+c10 Er8HIUS0XlxQlREstiCflI60CmudigTX7IuSJRws5C5330qa4xmph/mwF14wUePlSj OWt27yD/hnr2tjOoeYjAUZ17Qv3+xGxe5JdDSAP9BY5h4+QLV2KNe/NbOEErcmA6tq rVUo89W9sGI0XuxbepQAcc9YMOpRcmQ7hcsVNtyMQGRE8/ug+VHuGLTxRbVz6XjB+U D/81GPN6eYhuw== From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt Subject: [PATCH 06/10] libbpf: Add libbpf_kallsyms_parse function Date: Tue, 22 Feb 2022 18:05:56 +0100 Message-Id: <20220222170600.611515-7-jolsa@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220222170600.611515-1-jolsa@kernel.org> References: <20220222170600.611515-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Move the kallsyms parsing in internal libbpf_kallsyms_parse function, so it can be used from other places. It will be used in following changes. Signed-off-by: Jiri Olsa --- tools/lib/bpf/libbpf.c | 62 ++++++++++++++++++++------------- tools/lib/bpf/libbpf_internal.h | 5 +++ 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index ad43b6ce825e..fb530b004a0d 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -7172,12 +7172,10 @@ static int bpf_object__sanitize_maps(struct bpf_object *obj) return 0; } -static int bpf_object__read_kallsyms_file(struct bpf_object *obj) +int libbpf_kallsyms_parse(kallsyms_cb_t cb, void *ctx) { char sym_type, sym_name[500]; unsigned long long sym_addr; - const struct btf_type *t; - struct extern_desc *ext; int ret, err = 0; FILE *f; @@ -7196,35 +7194,51 @@ static int bpf_object__read_kallsyms_file(struct bpf_object *obj) if (ret != 3) { pr_warn("failed to read kallsyms entry: %d\n", ret); err = -EINVAL; - goto out; + break; } - ext = find_extern_by_name(obj, sym_name); - if (!ext || ext->type != EXT_KSYM) - continue; - - t = btf__type_by_id(obj->btf, ext->btf_id); - if (!btf_is_var(t)) - continue; - - if (ext->is_set && ext->ksym.addr != sym_addr) { - pr_warn("extern (ksym) '%s' resolution is ambiguous: 0x%llx or 0x%llx\n", - sym_name, ext->ksym.addr, sym_addr); - err = -EINVAL; - goto out; - } - if (!ext->is_set) { - ext->is_set = true; - ext->ksym.addr = sym_addr; - pr_debug("extern (ksym) %s=0x%llx\n", sym_name, sym_addr); - } + err = cb(sym_addr, sym_type, sym_name, ctx); + if (err) + break; } -out: fclose(f); return err; } +static int kallsyms_cb(unsigned long long sym_addr, char sym_type, + const char *sym_name, void *ctx) +{ + struct bpf_object *obj = ctx; + const struct btf_type *t; + struct extern_desc *ext; + + ext = find_extern_by_name(obj, sym_name); + if (!ext || ext->type != EXT_KSYM) + return 0; + + t = btf__type_by_id(obj->btf, ext->btf_id); + if (!btf_is_var(t)) + return 0; + + if (ext->is_set && ext->ksym.addr != sym_addr) { + pr_warn("extern (ksym) '%s' resolution is ambiguous: 0x%llx or 0x%llx\n", + sym_name, ext->ksym.addr, sym_addr); + return -EINVAL; + } + if (!ext->is_set) { + ext->is_set = true; + ext->ksym.addr = sym_addr; + pr_debug("extern (ksym) %s=0x%llx\n", sym_name, sym_addr); + } + return 0; +} + +static int bpf_object__read_kallsyms_file(struct bpf_object *obj) +{ + return libbpf_kallsyms_parse(kallsyms_cb, obj); +} + static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name, __u16 kind, struct btf **res_btf, struct module_btf **res_mod_btf) diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index 4fda8bdf0a0d..b6247dc7f8eb 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -449,6 +449,11 @@ __s32 btf__find_by_name_kind_own(const struct btf *btf, const char *type_name, extern enum libbpf_strict_mode libbpf_mode; +typedef int (*kallsyms_cb_t)(unsigned long long sym_addr, char sym_type, + const char *sym_name, void *ctx); + +int libbpf_kallsyms_parse(kallsyms_cb_t cb, void *arg); + /* handle direct returned errors */ static inline int libbpf_err(int ret) { -- 2.35.1