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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B7538C4332F for ; Tue, 4 Jan 2022 08:10:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234060AbiADIK4 (ORCPT ); Tue, 4 Jan 2022 03:10:56 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:42875 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234028AbiADIKm (ORCPT ); Tue, 4 Jan 2022 03:10:42 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1641283842; 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=k9zmsPfmvqNNuMjvxyy1I9bmPA8hKeQFPYbvFjf3I4A=; b=R43hMeZzKL4e30FXUnevUfBKIPldQiHG4pFgAr0sRcbidWBU6sjdCAw83Vfu4LKiBnOfAc ttSB359hSlh1xnavzEiSFkwgsdvQN8arqkmRqWbs5zcmbR3mEbVLX9XhMFueEwQZs+IqDG SJGKCkmN1fHMYuIFCjXSfeZLcHsOcIM= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-423-VEE5rPhyPKq23mVMmjfBHA-1; Tue, 04 Jan 2022 03:10:41 -0500 X-MC-Unique: VEE5rPhyPKq23mVMmjfBHA-1 Received: by mail-ed1-f69.google.com with SMTP id r8-20020a05640251c800b003f9a52daa3fso1871478edd.22 for ; Tue, 04 Jan 2022 00:10:40 -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=k9zmsPfmvqNNuMjvxyy1I9bmPA8hKeQFPYbvFjf3I4A=; b=lGfxDiJDu6qth6W5rQWZRlJNS/dihNVl3XgbRUU4lVhHyPH7OHU18milwfcOp7Q3Ab /A7BvS/N8nksJGdyCboz2yYnY+MKhEkuISYY5dDID7SazY7r/fi4/x6yGL4Nw7fwh+WR 59EvEtlJufFEEzB+6Hf1DCGP7Swx956bUkUz1YTuYrDXodAdOCDgta7Af13CTIjGlsiV 3HDKSlJa21ln4hIx6ISrgxCnrjOBvbYqi4aQWmbjfUeUa7WBHAyyGa6zeiWLkW1261Gq YjfERnQ6epTcmrKHWMPDm4Wxdy3ltOYK4vwWW84mzACjCj/5tR//iGakFE6H/KvlLjOr hvSw== X-Gm-Message-State: AOAM530CO8mnwKlymS7T5JYDJC8c3m8K+ewVJBWJOyFqXl1xo/pDZWnH WRlvMbpd/UH/tX18G27B/1fn96LIrPG3Fx43mDV4ehmqEwzEdcqBS+EMg5nN5o7HOlaiHYNJGbG nJj74yVUyGP6IsEEUwnXqo5bf X-Received: by 2002:a17:906:ced9:: with SMTP id si25mr41488679ejb.77.1641283839576; Tue, 04 Jan 2022 00:10:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJzi6A/ih8FmZYPaSWBAndlTnAjTuC5CIwYwH/5ES92eoOzDAHcM0nZAphH3RYwfLOv0tUGPwA== X-Received: by 2002:a17:906:ced9:: with SMTP id si25mr41488662ejb.77.1641283839376; Tue, 04 Jan 2022 00:10:39 -0800 (PST) Received: from krava.redhat.com (nat-pool-brq-u.redhat.com. [213.175.37.12]) by smtp.gmail.com with ESMTPSA id hd43sm1415724ejc.62.2022.01.04.00.10.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jan 2022 00:10:39 -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 , "Naveen N. Rao" , Anil S Keshavamurthy , "David S. Miller" Subject: [PATCH 09/13] libbpf: Add libbpf__kallsyms_parse function Date: Tue, 4 Jan 2022 09:09:39 +0100 Message-Id: <20220104080943.113249-10-jolsa@kernel.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220104080943.113249-1-jolsa@kernel.org> References: <20220104080943.113249-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 9cb99d1e2385..25512b4dbc8c 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -7146,12 +7146,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; @@ -7170,35 +7168,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 1565679eb432..dc544d239509 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -446,6 +446,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.33.1