Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp3541886pxb; Fri, 4 Feb 2022 10:40:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJzzCkwLNfiHKK2mXhRhEL3I5ySBFQgm+0raeqxrTtW6oGW61FaZaPK2GV9zlNBJiISLlEsR X-Received: by 2002:a63:5b58:: with SMTP id l24mr283571pgm.418.1644000030246; Fri, 04 Feb 2022 10:40:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644000030; cv=none; d=google.com; s=arc-20160816; b=a5qlTcBAIW9cLxGm6uac3Fip1tN27r/ZBqcY00814w9lfa7ZZXdWteqLlFt9JeF5pq hM15PjzzNquH2VaWHPX3mETgMQJUW9GBNdZJEuMZ6TFzfUNdEMVzEyGkpzWZ5VD08uDa DWe5IrF5DRsa0DtPQ00V0klHT8//StXgMgE3LT6tCd8uUGLN8oN9Ka5K2UyRa/a4Wz7H JB8TLhqASVijPFMszf1b5iZgFpBZyx+8h5GivLWBx+r4GHaBwvvhBOrFaa6AzTiKIOpD zgU068WE/ZD9PxQ1ED2vkpQqwenh74dOZwwA+NMisGZ5BsoZqVQUr0+PvOyNZ51Rminv BeIw== 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=jBnKFQ30kOdAO0O49lD07YWI/2GEebyH6rvvven/zqI=; b=aGis8XrShGtpTyJnfMFrZGOWXVziTbWqBbT47Kn/zGGij5gioQynzlv/oCRtnZUdUl EN0xArFdEV/ZJShrUHMPB4weZP0A1AMRP0rKAYvqTNGo6Fv/kYuVcSeNz7UbcUI5oSEa Y57I12nRCR2HRYYcTZzOBetgsKNif4sph7DFMI+qM/rttl+sdgfEOAHph83pVXW942uo ViCdtqj31/mP5gVRbrkTYqcmuKv0CvXg4pctIVB7OXbeNK7Rknjd6u9rpVg4XwSeWp8C PdUy+vrtPWsH/MSO0WSS5qBm8voHAowzGbXIJArFYsb5JEIVgxXpxzOdvPypR31d5Ylm yx7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=BW5Qoziw; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x186si2559297pgb.69.2022.02.04.10.40.17; Fri, 04 Feb 2022 10:40:30 -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=@redhat.com header.s=mimecast20190719 header.b=BW5Qoziw; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230167AbiBBNyQ (ORCPT + 99 others); Wed, 2 Feb 2022 08:54:16 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:30581 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344616AbiBBNyE (ORCPT ); Wed, 2 Feb 2022 08:54:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643810044; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jBnKFQ30kOdAO0O49lD07YWI/2GEebyH6rvvven/zqI=; b=BW5QoziwiXa/flqH8owlwPDHDyuClNMlLpnrYvkVtcMTzx78cgKd5RxLIBJK3Aps0BTo5g VisC80HJZaFoo1FuN2mEGBEy3Lw8sAbkdKa7/o9VNviAcqLmM2xve85sySuLGNaLYwp1wJ PIJDo83uuyU1EQCvEJpBYp/tXZCCAPE= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-426-JvUGRzKzOLuIEG9Iy0TQYg-1; Wed, 02 Feb 2022 08:54:01 -0500 X-MC-Unique: JvUGRzKzOLuIEG9Iy0TQYg-1 Received: by mail-ej1-f69.google.com with SMTP id 13-20020a170906328d00b006982d0888a4so8103664ejw.9 for ; Wed, 02 Feb 2022 05:54:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jBnKFQ30kOdAO0O49lD07YWI/2GEebyH6rvvven/zqI=; b=YUAY0f+OP9Ozcd6W8mP3sEd6mzuW4wX21mbXuXggd3LJdUDwSdFSWrSw309Z+4Qdmy slUTdQXhS3rJaFZKbLmUw/VFiPv/NszjnEREgbOd8gTTQvcpxRdsUvp1FiUNadskBi63 RgG26NnQsA5txR/XU2A6S4/mpePWU3ciulke6efoeHNw7AYrs0jW5jDbVsKAlO3eNwJm whE7Wfd8HZ0A0kxdNa2F6ft6Y46C2OXPcchjst8S786UgK63Z0CfWef7ErQto2Vni2s+ QXoZ0uq1fHL5wD3JSmcTQyOVf+N0ec7JTna/v5wRuN+Wfm3Cx485zuEEnMnrlmh+WCeg PAyw== X-Gm-Message-State: AOAM531wr8Hzx047TEh/JQF52UagtT+bwFfNTXjlzwPh2/zad2fmD1Fh yEllpUYGEzz7bGcM5RcOBSjVjlCb0tL94MBmop9yoAA3/RYehfCY2q7ILqfE9NstsToxX+5GigI Gui7WoJbswvpwxbmfLDb9F1jD X-Received: by 2002:a50:bb0a:: with SMTP id y10mr8988219ede.441.1643810039919; Wed, 02 Feb 2022 05:53:59 -0800 (PST) X-Received: by 2002:a50:bb0a:: with SMTP id y10mr8988201ede.441.1643810039727; Wed, 02 Feb 2022 05:53:59 -0800 (PST) Received: from krava.redhat.com (nat-pool-brq-u.redhat.com. [213.175.37.12]) by smtp.gmail.com with ESMTPSA id f4sm15819702ejh.93.2022.02.02.05.53.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 05:53:59 -0800 (PST) From: Jiri Olsa X-Google-Original-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 , Jiri Olsa Subject: [PATCH 4/8] libbpf: Add libbpf__kallsyms_parse function Date: Wed, 2 Feb 2022 14:53:29 +0100 Message-Id: <20220202135333.190761-5-jolsa@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220202135333.190761-1-jolsa@kernel.org> References: <20220202135333.190761-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 1b0936b016d9..7d595cfd03bc 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -7165,12 +7165,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(void *arg, kallsyms_cb_t cb) { 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; @@ -7189,35 +7187,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(arg, sym_addr, sym_type, sym_name); + if (err) + break; } -out: fclose(f); return err; } +static int kallsyms_cb(void *arg, unsigned long long sym_addr, + char sym_type, const char *sym_name) +{ + struct bpf_object *obj = arg; + 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(obj, kallsyms_cb); +} + 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 bc86b82e90d1..fb3b07d401df 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)(void *arg, unsigned long long sym_addr, + char sym_type, const char *sym_name); + +int libbpf__kallsyms_parse(void *arg, kallsyms_cb_t cb); + /* handle direct returned errors */ static inline int libbpf_err(int ret) { -- 2.34.1