From patchwork Sun Sep 11 20:14:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12973053 X-Patchwork-Delegate: bpf@iogearbox.net 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 06B8AC6FA83 for ; Sun, 11 Sep 2022 20:15:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229457AbiIKUPC (ORCPT ); Sun, 11 Sep 2022 16:15:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229536AbiIKUPB (ORCPT ); Sun, 11 Sep 2022 16:15:01 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45ADE13F82 for ; Sun, 11 Sep 2022 13:14:59 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id t14so12296845wrx.8 for ; Sun, 11 Sep 2022 13:14:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=DJc2WTeQ79aN7lw9j3ViprJPP9RDB1+5PaPrZJopgzo=; b=tfi0xMhI4A09iIjDRDfwyBGtW76n2eghEso5u138wSFwdlRG8oH1igv4gKvZO4eamO yssKtgCvqLQKroPGgLYVUksvwK2Pwv/uGxPTi7WE1NaFnpmIg/0yI0e9yb2UOSBUuZ+d NTDIh/HbyvwF+G33e7BZFAYwojKNax7M50Mxlz+sTCqDZcpH/r+vf4d36eHnCCR6WL1z YsCLfXxG2CkvcsxSPESJvzcDrUu6lW6vg4TaK9rZxlDhS00VHvIy7pHKpcQhlSHBH2ph LSus2B9AID3dyWfIs+8zncWemtzqFeBDvxKkSwWZBLbEC7cTkv6HUcGexBK6Fu9Yk4rV nJhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=DJc2WTeQ79aN7lw9j3ViprJPP9RDB1+5PaPrZJopgzo=; b=kyGIoncKZ3+u0Yc7cR3eCYUinDMkYOL6Vyr/lrLcODNsTcyhxvYO72qOG4EQFh5aTF fBIsxX7HcynrtsnimngEQri+lzcQaN3+P5v7RDj0eRixs0m8GxrA7gVZUzQFEWGia94u yzCQ0yEzyU5PoFJU/0/crFsop9BOzWEJODHYuAg3EqgXKXWOAFlvzf661ZdFksjxCTqc TLwDUyQqI4QYAM1WKNnq8X8T7VorcirEO3hzK/yeYAbO0SEUUkoOFk2J8ofTuEO0Ih5s rWP6bsxxMtkMnF38jbcUhdMmHtzm9s01BC2tGfk3DFsV4ra/pU8pFJtV0/tzHgZNkzoB gB3Q== X-Gm-Message-State: ACgBeo2ICOJlia7xSXYh2XeNuT78JGPNN5R0NwC2ygkHf8ZNoD5rDKc8 pfuoAJuJJ6o/wgoOZxvjy5WrNA== X-Google-Smtp-Source: AA6agR6pNIJy9SyyCH8BRvO5LomTTaBxXFf8wvXJzC//3v3Ao5LszVI/WEwZGV1ljjcpcr0mqma2vQ== X-Received: by 2002:adf:fc83:0:b0:226:d2d4:bc27 with SMTP id g3-20020adffc83000000b00226d2d4bc27mr12450847wrr.606.1662927297848; Sun, 11 Sep 2022 13:14:57 -0700 (PDT) Received: from harfang.access.network ([185.122.133.20]) by smtp.gmail.com with ESMTPSA id bh16-20020a05600c3d1000b003a60ff7c082sm7603789wmb.15.2022.09.11.13.14.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Sep 2022 13:14:56 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet , Song Liu Subject: [PATCH bpf-next v2 1/8] bpftool: Define _GNU_SOURCE only once Date: Sun, 11 Sep 2022 21:14:44 +0100 Message-Id: <20220911201451.12368-2-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220911201451.12368-1-quentin@isovalent.com> References: <20220911201451.12368-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net _GNU_SOURCE is defined in several source files for bpftool, but only one of them takes the precaution of checking whether the value is already defined. Add #ifndef for other occurrences too. This is in preparation for the support of disassembling JIT-ed programs with LLVM, with $(llvm-config --cflags) passing -D_GNU_SOURCE as a compilation argument. Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Song Liu --- tools/bpf/bpftool/common.c | 2 ++ tools/bpf/bpftool/iter.c | 2 ++ tools/bpf/bpftool/jit_disasm.c | 2 ++ tools/bpf/bpftool/net.c | 2 ++ tools/bpf/bpftool/perf.c | 2 ++ tools/bpf/bpftool/prog.c | 2 ++ tools/bpf/bpftool/xlated_dumper.c | 2 ++ 7 files changed, 14 insertions(+) diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c index 8727765add88..4c2e909a2d67 100644 --- a/tools/bpf/bpftool/common.c +++ b/tools/bpf/bpftool/common.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include diff --git a/tools/bpf/bpftool/iter.c b/tools/bpf/bpftool/iter.c index f88fdc820d23..a3e6b167153d 100644 --- a/tools/bpf/bpftool/iter.c +++ b/tools/bpf/bpftool/iter.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) // Copyright (C) 2020 Facebook +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include diff --git a/tools/bpf/bpftool/jit_disasm.c b/tools/bpf/bpftool/jit_disasm.c index aaf99a0168c9..71cb258ab0ee 100644 --- a/tools/bpf/bpftool/jit_disasm.c +++ b/tools/bpf/bpftool/jit_disasm.c @@ -11,7 +11,9 @@ * Licensed under the GNU General Public License, version 2.0 (GPLv2) */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c index 526a332c48e6..c40e44c938ae 100644 --- a/tools/bpf/bpftool/net.c +++ b/tools/bpf/bpftool/net.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) // Copyright (C) 2018 Facebook +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include diff --git a/tools/bpf/bpftool/perf.c b/tools/bpf/bpftool/perf.c index 226ec2c39052..91743445e4c7 100644 --- a/tools/bpf/bpftool/perf.c +++ b/tools/bpf/bpftool/perf.c @@ -2,7 +2,9 @@ // Copyright (C) 2018 Facebook // Author: Yonghong Song +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index c81362a001ba..a31ae9f0c307 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c index 2d9cd6a7b3c8..6fe3134ae45d 100644 --- a/tools/bpf/bpftool/xlated_dumper.c +++ b/tools/bpf/bpftool/xlated_dumper.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) /* Copyright (C) 2018 Netronome Systems, Inc. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include From patchwork Sun Sep 11 20:14:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12973054 X-Patchwork-Delegate: bpf@iogearbox.net 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 D8F0CECAAD3 for ; Sun, 11 Sep 2022 20:15:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229643AbiIKUPJ (ORCPT ); Sun, 11 Sep 2022 16:15:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229583AbiIKUPF (ORCPT ); Sun, 11 Sep 2022 16:15:05 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAA7B193EA for ; Sun, 11 Sep 2022 13:15:03 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id bj14so12252094wrb.12 for ; Sun, 11 Sep 2022 13:15:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=KdmLbSWGqiOLb/m/H92/rXiw90GcfXHeM589SNO2Oa0=; b=WYEznBBRZ82Fnv9eYDT1gLDxE3qNgbVEgeJLn8Lc/xei1Km9vGLqM4R7JYn7dVolga NLYjSRc6B+HKxBHJ2Far8ucX08GOFrhDO4k15hyhb3V7cTiWBfO0R4YSAaLhLGpB497M xItxlIYmklJkY3uv64w6cqpYpzfe2oT2FAg1K8xjUar/3JYoH0/MjVYkq1LViQv14eul 3lHWo0mrAZO8BGy+T5INpkJaiLnfcDTL56GIC7EqBwSnOdwnaxaAs6FKHGmDIeLAUF+S GN/XXouCdi2vPc1xmUH0wkzK4gMCD6zOO8AzOSfoGC/thPQoo5BHRdXMDpEW+WROAcrS 8CCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=KdmLbSWGqiOLb/m/H92/rXiw90GcfXHeM589SNO2Oa0=; b=XyduMtc7pipY/ZA3FTtfMPporh5VahwsX2U+ut90Zg9R5XanyYiQt/ShFNpeStaQkX GLzn9BRP1Fr6wDH9+qRhZTtv5P7lKu1J395PNbYTp9wJcrtK6jJ9cs/2yyRSMc7uWV1z LnImZKszogofoCZvYfDbJGAhrJugKh+oRkIl6gFBDJGiNAi9PqTvkwynaPvxMxasUNMt ZZK/OVET99vfUgzd0QO2X7hd39M+C2c8bLdUFfLh4uy10abzAAVqWtWNt0jN3I26ALUF ClxbwDaDtxgn/SYI8tf5fwQLtgfoTO2q+smRq6zeBLFs6P6fWcvfnDYhC/0E+EpZFo7+ gwlQ== X-Gm-Message-State: ACgBeo3Kmwu4OljxrYR3bJ42PTrVnAjst+xI51AVhQkCtXuHI1WVT2Gn BIHDaLm3IrjC2kbgNwD0/sFuaw== X-Google-Smtp-Source: AA6agR5zFo61+YyP0TuVZfx6eoQopKPYkTGQYVRf+1htSOwdOzqNZ5xDTY11ikg69CvCmmS83V3iHQ== X-Received: by 2002:a5d:6802:0:b0:228:d6e9:6dd8 with SMTP id w2-20020a5d6802000000b00228d6e96dd8mr14170538wru.710.1662927302400; Sun, 11 Sep 2022 13:15:02 -0700 (PDT) Received: from harfang.access.network ([185.122.133.20]) by smtp.gmail.com with ESMTPSA id bh16-20020a05600c3d1000b003a60ff7c082sm7603789wmb.15.2022.09.11.13.14.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Sep 2022 13:14:58 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet , Song Liu Subject: [PATCH bpf-next v2 2/8] bpftool: Remove asserts from JIT disassembler Date: Sun, 11 Sep 2022 21:14:45 +0100 Message-Id: <20220911201451.12368-3-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220911201451.12368-1-quentin@isovalent.com> References: <20220911201451.12368-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net The JIT disassembler in bpftool is the only components (with the JSON writer) using asserts to check the return values of functions. But it does not do so in a consistent way, and diasm_print_insn() returns no value, although sometimes the operation failed. Remove the asserts, and instead check the return values, print messages on errors, and propagate the error to the caller from prog.c. Remove the inclusion of assert.h from jit_disasm.c, and also from map.c where it is unused. Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Song Liu --- tools/bpf/bpftool/jit_disasm.c | 51 +++++++++++++++++++++++----------- tools/bpf/bpftool/main.h | 25 +++++++++-------- tools/bpf/bpftool/map.c | 1 - tools/bpf/bpftool/prog.c | 15 ++++++---- 4 files changed, 57 insertions(+), 35 deletions(-) diff --git a/tools/bpf/bpftool/jit_disasm.c b/tools/bpf/bpftool/jit_disasm.c index 71cb258ab0ee..723a9b799a0c 100644 --- a/tools/bpf/bpftool/jit_disasm.c +++ b/tools/bpf/bpftool/jit_disasm.c @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -31,14 +30,18 @@ #include "json_writer.h" #include "main.h" -static void get_exec_path(char *tpath, size_t size) +static int get_exec_path(char *tpath, size_t size) { const char *path = "/proc/self/exe"; ssize_t len; len = readlink(path, tpath, size - 1); - assert(len > 0); + if (len <= 0) + return -1; + tpath[len] = 0; + + return 0; } static int oper_count; @@ -99,30 +102,39 @@ static int fprintf_json_styled(void *out, return r; } -void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, - const char *arch, const char *disassembler_options, - const struct btf *btf, - const struct bpf_prog_linfo *prog_linfo, - __u64 func_ksym, unsigned int func_idx, - bool linum) +int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, + const char *arch, const char *disassembler_options, + const struct btf *btf, + const struct bpf_prog_linfo *prog_linfo, + __u64 func_ksym, unsigned int func_idx, + bool linum) { const struct bpf_line_info *linfo = NULL; disassembler_ftype disassemble; + int count, i, pc = 0, err = -1; struct disassemble_info info; unsigned int nr_skip = 0; - int count, i, pc = 0; char tpath[PATH_MAX]; bfd *bfdf; if (!len) - return; + return -1; memset(tpath, 0, sizeof(tpath)); - get_exec_path(tpath, sizeof(tpath)); + if (get_exec_path(tpath, sizeof(tpath))) { + p_err("failed to create disasembler (get_exec_path)"); + return -1; + } bfdf = bfd_openr(tpath, NULL); - assert(bfdf); - assert(bfd_check_format(bfdf, bfd_object)); + if (!bfdf) { + p_err("failed to create disassembler (bfd_openr)"); + return -1; + } + if (!bfd_check_format(bfdf, bfd_object)) { + p_err("failed to create disassembler (bfd_check_format)"); + goto exit_close; + } if (json_output) init_disassemble_info_compat(&info, stdout, @@ -141,7 +153,7 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, bfdf->arch_info = inf; } else { p_err("No libbfd support for %s", arch); - return; + goto exit_close; } } @@ -162,7 +174,10 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, #else disassemble = disassembler(bfdf); #endif - assert(disassemble); + if (!disassemble) { + p_err("failed to create disassembler"); + goto exit_close; + } if (json_output) jsonw_start_array(json_wtr); @@ -226,7 +241,11 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, if (json_output) jsonw_end_array(json_wtr); + err = 0; + +exit_close: bfd_close(bfdf); + return err; } int disasm_init(void) diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index 5e5060c2ac04..c9e171082cf6 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -173,22 +173,23 @@ int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len); struct bpf_prog_linfo; #ifdef HAVE_LIBBFD_SUPPORT -void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, - const char *arch, const char *disassembler_options, - const struct btf *btf, - const struct bpf_prog_linfo *prog_linfo, - __u64 func_ksym, unsigned int func_idx, - bool linum); +int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, + const char *arch, const char *disassembler_options, + const struct btf *btf, + const struct bpf_prog_linfo *prog_linfo, + __u64 func_ksym, unsigned int func_idx, + bool linum); int disasm_init(void); #else static inline -void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, - const char *arch, const char *disassembler_options, - const struct btf *btf, - const struct bpf_prog_linfo *prog_linfo, - __u64 func_ksym, unsigned int func_idx, - bool linum) +int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, + const char *arch, const char *disassembler_options, + const struct btf *btf, + const struct bpf_prog_linfo *prog_linfo, + __u64 func_ksym, unsigned int func_idx, + bool linum) { + return 0; } static inline int disasm_init(void) { diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 38b6bc9c26c3..a0e573589811 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -1,7 +1,6 @@ // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ -#include #include #include #include diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index a31ae9f0c307..345dca656a34 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -822,10 +822,11 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, printf("%s:\n", sym_name); } - disasm_print_insn(img, lens[i], opcodes, - name, disasm_opt, btf, - prog_linfo, ksyms[i], i, - linum); + if (disasm_print_insn(img, lens[i], opcodes, + name, disasm_opt, btf, + prog_linfo, ksyms[i], i, + linum)) + goto exit_free; img += lens[i]; @@ -838,8 +839,10 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, if (json_output) jsonw_end_array(json_wtr); } else { - disasm_print_insn(buf, member_len, opcodes, name, - disasm_opt, btf, NULL, 0, 0, false); + if (disasm_print_insn(buf, member_len, opcodes, name, + disasm_opt, btf, NULL, 0, 0, + false)) + goto exit_free; } } else if (visual) { if (json_output) From patchwork Sun Sep 11 20:14:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12973055 X-Patchwork-Delegate: bpf@iogearbox.net 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 9AD30C6FA86 for ; Sun, 11 Sep 2022 20:15:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229510AbiIKUPL (ORCPT ); Sun, 11 Sep 2022 16:15:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229522AbiIKUPI (ORCPT ); Sun, 11 Sep 2022 16:15:08 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3D531A817 for ; Sun, 11 Sep 2022 13:15:05 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id bj14so12252179wrb.12 for ; Sun, 11 Sep 2022 13:15:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=ixQHMERYdgRb7FUM1/1Io76g7S2tfmEKG93IxR6PYMU=; b=wkJdJFZkR0v25pHG3qJEVRrlgKFSoACgyrOZS+S32uO1OxMERcJXgi3KWeZnFh7KJu 11/S6DG5rw+9y1bz+PTn+n3O2vk7JSaednJKaGq21v2zKTECv1bxH7ZfIiqa0Iuc0nJw jH0Za3r89FKnsfLwF4swsodKkHQPwFiHf25oTFSECq01/YhznJVDXU1w6o/zl+d2U9ZJ N3Ou44JJlOb0KueO76jKS3eyfE2BZ1jRfCiTKV0uzNB1ubSvWUYC05x3h14sRakWVvqZ /tiKfXaMZmzXo1QHRSH5dLqcaS2/UjykVEZ0dqVAhboIHsl1/cKXY/jevwYUWIydifCi dXyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=ixQHMERYdgRb7FUM1/1Io76g7S2tfmEKG93IxR6PYMU=; b=5NZabnH3tx4tbQnBTb2jTYM3ST4ddm1fOcJ2JTk+GSC2EnZxKCkwsDQf7vgwGV/lgn iS14bXrQCUrkGftCe+L8YQBAowervzAMpj+JV5VFSZdLmJ8EtFJ0y5eZYNwVpjORsH5G PGC71h/0vWqGvlRRtsP9jvv2sgAHERUmFGnUviRryKImGrDX0iOJuQ7bT45tVoCDOslC FpYbNFFDnpZ+0k8ZbaEXYdndTrKf/zyX0LjwbzNBHw3k509GtOV9MdXsI/5ewLdMhR/o K4rVawssjx4qb2dKzK41Lmyx7CL+QcMWcgjHxTaJAjhNlBTbRoQq69/LYyt0JMskHv5f tNAg== X-Gm-Message-State: ACgBeo0QT9uuS12po6cg3yaHcLAQ6GeTSazGoEFMdlK6yd66WbY0tkqS wsBhDt7ZUgXOxQEIYSNpsY38Kw== X-Google-Smtp-Source: AA6agR7wIncRH+kS9Za/sTkPpPPqlp1hINZlv0AyJPvzSYk2YWDn4W4RRuF+7PAXevo/1tg9HoQxiw== X-Received: by 2002:a5d:45c4:0:b0:228:9248:867d with SMTP id b4-20020a5d45c4000000b002289248867dmr13163772wrs.474.1662927304477; Sun, 11 Sep 2022 13:15:04 -0700 (PDT) Received: from harfang.access.network ([185.122.133.20]) by smtp.gmail.com with ESMTPSA id bh16-20020a05600c3d1000b003a60ff7c082sm7603789wmb.15.2022.09.11.13.15.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Sep 2022 13:15:03 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet , Andres Freund , Song Liu Subject: [PATCH bpf-next v2 3/8] bpftool: Split FEATURE_TESTS/FEATURE_DISPLAY definitions in Makefile Date: Sun, 11 Sep 2022 21:14:46 +0100 Message-Id: <20220911201451.12368-4-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220911201451.12368-1-quentin@isovalent.com> References: <20220911201451.12368-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Make FEATURE_TESTS and FEATURE_DISPLAY easier to read and less likely to be subject to conflicts on updates by having one feature per line. Suggested-by: Andres Freund Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Song Liu --- tools/bpf/bpftool/Makefile | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile index 4a95c017ad4c..0218d6a1cae7 100644 --- a/tools/bpf/bpftool/Makefile +++ b/tools/bpf/bpftool/Makefile @@ -93,11 +93,20 @@ INSTALL ?= install RM ?= rm -f FEATURE_USER = .bpftool -FEATURE_TESTS = libbfd libbfd-liberty libbfd-liberty-z \ - disassembler-four-args disassembler-init-styled libcap \ - clang-bpf-co-re -FEATURE_DISPLAY = libbfd libbfd-liberty libbfd-liberty-z \ - libcap clang-bpf-co-re + +FEATURE_TESTS := clang-bpf-co-re +FEATURE_TESTS += libcap +FEATURE_TESTS += libbfd +FEATURE_TESTS += libbfd-liberty +FEATURE_TESTS += libbfd-liberty-z +FEATURE_TESTS += disassembler-four-args +FEATURE_TESTS += disassembler-init-styled + +FEATURE_DISPLAY := clang-bpf-co-re +FEATURE_DISPLAY += libcap +FEATURE_DISPLAY += libbfd +FEATURE_DISPLAY += libbfd-liberty +FEATURE_DISPLAY += libbfd-liberty-z check_feat := 1 NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall From patchwork Sun Sep 11 20:14:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12973056 X-Patchwork-Delegate: bpf@iogearbox.net 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 62E57C6FA83 for ; Sun, 11 Sep 2022 20:15:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229522AbiIKUPM (ORCPT ); Sun, 11 Sep 2022 16:15:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229583AbiIKUPK (ORCPT ); Sun, 11 Sep 2022 16:15:10 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 175561AD82 for ; Sun, 11 Sep 2022 13:15:09 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id t14so12297300wrx.8 for ; Sun, 11 Sep 2022 13:15:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=pwBcXEdZ0bwNQEQkHCPZ0SYXvbbBEOobN7rGFCnE0t8=; b=Fb/pQovZwD5nekkQUFlWKh+RXfFhFiB/8X/SA2Tu1nO4LYvQGWO1AtYA7XHheQqByW PFz5sPK6ycU7MmxDksIi59hBcY7qi1kUbujtYyj5jcthl7mih2zCIN5zU3cRONydDJkp ZdiGgN2GSYxNmZUiZykLTt3lWSAtdTaxaD5tKq/chaMXf5gRbz9goZYlDES1cB427Mn0 bf5+TIsx4OszF6g+aEsup9C1ZMcpJ5LG3AgSTA7IvFHpsp8+xyzaxHdkWt+Gysac9tu6 VIiUyqhHvnF9/+Dvha1gC5Eij9K7t5UZ1OdZy8zqdBSPc1E/AFUdS4DQB9OEdiD8mdas C6DQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=pwBcXEdZ0bwNQEQkHCPZ0SYXvbbBEOobN7rGFCnE0t8=; b=2j9OitbM82pl+b5FXCaqwwk0Qwkbcc6j0Rivhgq1Rsa4vcRfPXJm7s05O8yeRcbCWU RXsL7B9xjrAry6tJXGtg+6vnmHOq22xw/LEqvTPl91SqFKLrP+xOc1xQrRpu9KrF0vW+ tz7kvBEeQ5KB9OKzGQgBdjqeTaSfMY1SpWvvMBW+a/6aQqnPu9vtwd2SnnZQ22b2oWMi 2Xm2HaUlSEwi5cdHAXb+VA6oEDHixYy7Xo5CILcpM6pn33mYGbJnsoxARE+yPZeRWzrj de0nvtDwJ4YRDvb8/Ncey+fq8MbpjQTUExwjtUNfaDMeQxDNP3JUDzcydZQOSJ29DDzv CnMQ== X-Gm-Message-State: ACgBeo31LB1xDH40Or35FFZSrPy9Whvke9+gN2hWjzOInYOsirK2c9FE z5pf1zN84NXn/LN1d9zGNRaD7g== X-Google-Smtp-Source: AA6agR5SemsBV/iDMr5/ITBVVbGPLT04T7jwbFjp6QqxSPXEoHFAwIFrSU3Az29lycHLI9HHqAmueQ== X-Received: by 2002:adf:f911:0:b0:21e:c0f6:fd26 with SMTP id b17-20020adff911000000b0021ec0f6fd26mr13214403wrr.361.1662927308599; Sun, 11 Sep 2022 13:15:08 -0700 (PDT) Received: from harfang.access.network ([185.122.133.20]) by smtp.gmail.com with ESMTPSA id bh16-20020a05600c3d1000b003a60ff7c082sm7603789wmb.15.2022.09.11.13.15.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Sep 2022 13:15:06 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet , Song Liu Subject: [PATCH bpf-next v2 4/8] bpftool: Group libbfd defs in Makefile, only pass them if we use libbfd Date: Sun, 11 Sep 2022 21:14:47 +0100 Message-Id: <20220911201451.12368-5-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220911201451.12368-1-quentin@isovalent.com> References: <20220911201451.12368-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Bpftool uses libbfd for disassembling JIT-ed programs. But the feature is optional, and the tool can be compiled without libbfd support. The Makefile sets the relevant variables accordingly. It also sets variables related to libbfd's interface, given that it has changed over time. Group all those libbfd-related definitions so that it's easier to understand what we are testing for, and only use variables related to libbfd's interface if we need libbfd in the first place. In addition to make the Makefile clearer, grouping the definitions related to disassembling JIT-ed programs will help support alternatives to libbfd. Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Song Liu --- tools/bpf/bpftool/Makefile | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile index 0218d6a1cae7..1c81f4d514bb 100644 --- a/tools/bpf/bpftool/Makefile +++ b/tools/bpf/bpftool/Makefile @@ -124,13 +124,6 @@ include $(FEATURES_DUMP) endif endif -ifeq ($(feature-disassembler-four-args), 1) -CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE -endif -ifeq ($(feature-disassembler-init-styled), 1) - CFLAGS += -DDISASM_INIT_STYLED -endif - LIBS = $(LIBBPF) -lelf -lz LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz ifeq ($(feature-libcap), 1) @@ -142,9 +135,7 @@ include $(wildcard $(OUTPUT)*.d) all: $(OUTPUT)bpftool -BFD_SRCS = jit_disasm.c - -SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c)) +SRCS := $(wildcard *.c) ifeq ($(feature-libbfd),1) LIBS += -lbfd -ldl -lopcodes @@ -154,9 +145,21 @@ else ifeq ($(feature-libbfd-liberty-z),1) LIBS += -lbfd -ldl -lopcodes -liberty -lz endif +# If one of the above feature combinations is set, we support libbfd ifneq ($(filter -lbfd,$(LIBS)),) -CFLAGS += -DHAVE_LIBBFD_SUPPORT -SRCS += $(BFD_SRCS) + CFLAGS += -DHAVE_LIBBFD_SUPPORT + + # Libbfd interface changed over time, figure out what we need + ifeq ($(feature-disassembler-four-args), 1) + CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE + endif + ifeq ($(feature-disassembler-init-styled), 1) + CFLAGS += -DDISASM_INIT_STYLED + endif +endif +ifeq ($(filter -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),) + # No support for JIT disassembly + SRCS := $(filter-out jit_disasm.c,$(SRCS)) endif HOST_CFLAGS = $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\ From patchwork Sun Sep 11 20:14:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12973057 X-Patchwork-Delegate: bpf@iogearbox.net 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 089C0C6FA83 for ; Sun, 11 Sep 2022 20:15:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229616AbiIKUPR (ORCPT ); Sun, 11 Sep 2022 16:15:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229575AbiIKUPQ (ORCPT ); Sun, 11 Sep 2022 16:15:16 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5790B1B7B3 for ; Sun, 11 Sep 2022 13:15:14 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id m17-20020a7bce11000000b003a5bedec07bso9622934wmc.0 for ; Sun, 11 Sep 2022 13:15:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=RA9eacgGZiA4SnV5uyL526OIKeHyinf6gpH6AIvYlko=; b=5PqFripS0UuDAy86R9gDOLHe/Qq4cGn5fRKrvCU+nxoCDRh3W2N/KabVdS/rdIXR64 zpWfhUjHEo/SJKMarDUF76pCeh6JNCC57DmORM2noK+9H2TiQqzDHVtwYZDw1tk6cjUI bnvTXMnzi6Fik9l/ZJ0tN9dVdj4CplZB4P+RlEOiuADJTy/ZTratK7wN61g9zk6jbJvz IRB1pwcWFfb1hG6JyXUa5k/6Y+UT3/O2mfxKn6+2uu5SFDwxf1kPHDxIeKvE46UcYE/N j0BeMC3Ap2io3RXEwW1Y7tv9zp6jUCh6D2fDM3x5pPOjvJngln0SiLKXDwN6GCjirxVL R2IA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=RA9eacgGZiA4SnV5uyL526OIKeHyinf6gpH6AIvYlko=; b=v0Dy2gfK8Rd31AFAzwfP7prKXPOtqcyhicxY4cPn2V61lwsuXJnMrvhLaK2Kptc+bI mj0lM8AVM4kYEub+vbfcUXRkqsqPYwR7aMfZdDZNuUvBOI9j2FsnstZIXtNX55UwLGYw HkL6PE9S1l/SKmKsJ3UGv9mj+uZL3SiEG05DmAhznKR3Jpl3XVPAYfTW5oiD4qJjLSoV yed5QFCrmaqx0cU0igguV66W+ELZeFtt8bD1HMnnHSrwusdJ3wBAR3Gsp8rF2IjTcQEN XtnzJElBe51TbEiMhjXiPU/HxbfOQ+9FDnf2we7zYJzvdPHzrKQf/edM0Sdp2EIbmYjJ /30Q== X-Gm-Message-State: ACgBeo0WnuaaV004gmt/d1Nz2YTlJQLDsZe09wDSNaY4sgPwrzKsrWk0 Bv1uupvk/lAEEtY7dc2y8fiSMrknVeMi1JLoHHg= X-Google-Smtp-Source: AA6agR7JD/35BUrsgnquyPDxEyyIMAvFI9C7gq5NS1exLg0USnTV/vGk8FPoQreEej8mbzmzN+vivQ== X-Received: by 2002:a05:600c:3482:b0:3a6:e09:1ebf with SMTP id a2-20020a05600c348200b003a60e091ebfmr11498534wmq.173.1662927312884; Sun, 11 Sep 2022 13:15:12 -0700 (PDT) Received: from harfang.access.network ([185.122.133.20]) by smtp.gmail.com with ESMTPSA id bh16-20020a05600c3d1000b003a60ff7c082sm7603789wmb.15.2022.09.11.13.15.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Sep 2022 13:15:10 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet , Song Liu Subject: [PATCH bpf-next v2 5/8] bpftool: Refactor disassembler for JIT-ed programs Date: Sun, 11 Sep 2022 21:14:48 +0100 Message-Id: <20220911201451.12368-6-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220911201451.12368-1-quentin@isovalent.com> References: <20220911201451.12368-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Refactor disasm_print_insn() to extract the code specific to libbfd and move it to dedicated functions. There is no functional change. This is in preparation for supporting an alternative library for disassembling the instructions. Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Song Liu --- tools/bpf/bpftool/jit_disasm.c | 133 ++++++++++++++++++++++----------- 1 file changed, 88 insertions(+), 45 deletions(-) diff --git a/tools/bpf/bpftool/jit_disasm.c b/tools/bpf/bpftool/jit_disasm.c index 723a9b799a0c..e31ad3950fd6 100644 --- a/tools/bpf/bpftool/jit_disasm.c +++ b/tools/bpf/bpftool/jit_disasm.c @@ -30,6 +30,14 @@ #include "json_writer.h" #include "main.h" +static int oper_count; + +typedef struct { + struct disassemble_info *info; + disassembler_ftype disassemble; + bfd *bfdf; +} disasm_ctx_t; + static int get_exec_path(char *tpath, size_t size) { const char *path = "/proc/self/exe"; @@ -44,7 +52,6 @@ static int get_exec_path(char *tpath, size_t size) return 0; } -static int oper_count; static int printf_json(void *out, const char *fmt, va_list ap) { char *s; @@ -102,46 +109,44 @@ static int fprintf_json_styled(void *out, return r; } -int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, - const char *arch, const char *disassembler_options, - const struct btf *btf, - const struct bpf_prog_linfo *prog_linfo, - __u64 func_ksym, unsigned int func_idx, - bool linum) +static int init_context(disasm_ctx_t *ctx, const char *arch, + const char *disassembler_options, + unsigned char *image, ssize_t len) { - const struct bpf_line_info *linfo = NULL; - disassembler_ftype disassemble; - int count, i, pc = 0, err = -1; - struct disassemble_info info; - unsigned int nr_skip = 0; + struct disassemble_info *info; char tpath[PATH_MAX]; bfd *bfdf; - if (!len) - return -1; - memset(tpath, 0, sizeof(tpath)); if (get_exec_path(tpath, sizeof(tpath))) { p_err("failed to create disasembler (get_exec_path)"); return -1; } - bfdf = bfd_openr(tpath, NULL); - if (!bfdf) { + ctx->bfdf = bfd_openr(tpath, NULL); + if (!ctx->bfdf) { p_err("failed to create disassembler (bfd_openr)"); return -1; } - if (!bfd_check_format(bfdf, bfd_object)) { + if (!bfd_check_format(ctx->bfdf, bfd_object)) { p_err("failed to create disassembler (bfd_check_format)"); - goto exit_close; + goto err_close; } + bfdf = ctx->bfdf; + + ctx->info = malloc(sizeof(struct disassemble_info)); + if (!ctx->info) { + p_err("mem alloc failed"); + goto err_close; + } + info = ctx->info; if (json_output) - init_disassemble_info_compat(&info, stdout, + init_disassemble_info_compat(info, stdout, (fprintf_ftype) fprintf_json, fprintf_json_styled); else - init_disassemble_info_compat(&info, stdout, + init_disassemble_info_compat(info, stdout, (fprintf_ftype) fprintf, fprintf_styled); @@ -153,31 +158,76 @@ int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, bfdf->arch_info = inf; } else { p_err("No libbfd support for %s", arch); - goto exit_close; + goto err_free; } } - info.arch = bfd_get_arch(bfdf); - info.mach = bfd_get_mach(bfdf); + info->arch = bfd_get_arch(bfdf); + info->mach = bfd_get_mach(bfdf); if (disassembler_options) - info.disassembler_options = disassembler_options; - info.buffer = image; - info.buffer_length = len; + info->disassembler_options = disassembler_options; + info->buffer = image; + info->buffer_length = len; - disassemble_init_for_target(&info); + disassemble_init_for_target(info); #ifdef DISASM_FOUR_ARGS_SIGNATURE - disassemble = disassembler(info.arch, - bfd_big_endian(bfdf), - info.mach, - bfdf); + ctx->disassemble = disassembler(info->arch, + bfd_big_endian(bfdf), + info->mach, + bfdf); #else - disassemble = disassembler(bfdf); + ctx->disassemble = disassembler(bfdf); #endif - if (!disassemble) { + if (!ctx->disassemble) { p_err("failed to create disassembler"); - goto exit_close; + goto err_free; } + return 0; + +err_free: + free(info); +err_close: + bfd_close(ctx->bfdf); + return -1; +} + +static void destroy_context(disasm_ctx_t *ctx) +{ + free(ctx->info); + bfd_close(ctx->bfdf); +} + +static int +disassemble_insn(disasm_ctx_t *ctx, __maybe_unused unsigned char *image, + __maybe_unused ssize_t len, int pc) +{ + return ctx->disassemble(pc, ctx->info); +} + +int disasm_init(void) +{ + bfd_init(); + return 0; +} + +int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, + const char *arch, const char *disassembler_options, + const struct btf *btf, + const struct bpf_prog_linfo *prog_linfo, + __u64 func_ksym, unsigned int func_idx, + bool linum) +{ + const struct bpf_line_info *linfo = NULL; + unsigned int nr_skip = 0; + int count, i, pc = 0; + disasm_ctx_t ctx; + + if (!len) + return -1; + + if (init_context(&ctx, arch, disassembler_options, image, len)) + return -1; if (json_output) jsonw_start_array(json_wtr); @@ -205,7 +255,8 @@ int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, printf("%4x:\t", pc); } - count = disassemble(pc, &info); + count = disassemble_insn(&ctx, image, len, pc); + if (json_output) { /* Operand array, was started in fprintf_json. Before * that, make sure we have a _null_ value if no operand @@ -241,15 +292,7 @@ int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, if (json_output) jsonw_end_array(json_wtr); - err = 0; + destroy_context(&ctx); -exit_close: - bfd_close(bfdf); - return err; -} - -int disasm_init(void) -{ - bfd_init(); return 0; } From patchwork Sun Sep 11 20:14:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12973058 X-Patchwork-Delegate: bpf@iogearbox.net 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 104F4C6FA83 for ; Sun, 11 Sep 2022 20:15:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229536AbiIKUPX (ORCPT ); Sun, 11 Sep 2022 16:15:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229575AbiIKUPU (ORCPT ); Sun, 11 Sep 2022 16:15:20 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52B231B7B3 for ; Sun, 11 Sep 2022 13:15:18 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id cc5so2469247wrb.6 for ; Sun, 11 Sep 2022 13:15:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=sdeh3UJa4UHuyyibCnhct+Nz4AM+vpV8iV5JJ+Tg1CQ=; b=lJS0gzQ+pG5IsqsT+55XUqH9e3KBs0PVFHrSKdDfWtW5pX0qhgtKfEe49smF/2H9tW c1MYCZzj/iC4upiAmkd9PukxMJqnXSVnOvI/D+CxVpktcYOD37nzyfhbfuwQOGJtQ6Jv 5jJ56Y2v7eQ4L28YGHt+zL1yXUQqyjuAcRqUPTfClktJJU/MeX6Qin6Wq2JIAHpiGa0H UFhjMakVDcrc4wrjXB95HtKbvlwqooIz5aREu6rz+vrcmrCfldClkXprRsTYq74iqqY8 /XeL4i+YEQe3Dniuru/xwpjRQshI1EYYKcwavvjtZM5xJiR7ybRob93OuZrEzPtlkL/X Klvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=sdeh3UJa4UHuyyibCnhct+Nz4AM+vpV8iV5JJ+Tg1CQ=; b=chSXcJfjBuzyshslAsfhmcZp9TvrEgbUClTOsF3vbRd7U9vSgDwdUZKAqBB5rwKPx+ 1dSR5wbmO8oVSN6bA1KW0F1wpAtburHP4O+hreF7hmUPq8giYaLnYXHkscjUAlFmZ1SC YI/5VLTh6tVH/Mp96Qkb1HKJ326L4JqJW9Xw1mrlxUc+pG67ZzTMchLSv0FnZQh9v52t roB5t8I3To9VsrkU7kJbPKmureEETvesRc/IwEf+0R/3SAHWrz68vFUAyH4PhwFYg/vO WDOC4AEzbkIET2RlTOc5mW6YA/IbvOGbMoGEg1M+seY8R/i/J5dsICrRT2A83LJjU9m6 2wpQ== X-Gm-Message-State: ACgBeo1f3O+lYTMGZidxN632INdBcTmqtwy/VG8BySA98++C+AKqpiZj oeUe1z88hHkxQttP/dLyYGo12w== X-Google-Smtp-Source: AA6agR7g3FWSDDSAIia0HVdX7X4NnljZF/9i+ofBiXAQoZCOldzPJHkIXzp0yNeCU00wUqQRTwXoTg== X-Received: by 2002:a5d:65c2:0:b0:228:68b7:e7b2 with SMTP id e2-20020a5d65c2000000b0022868b7e7b2mr13129055wrw.440.1662927316801; Sun, 11 Sep 2022 13:15:16 -0700 (PDT) Received: from harfang.access.network ([185.122.133.20]) by smtp.gmail.com with ESMTPSA id bh16-20020a05600c3d1000b003a60ff7c082sm7603789wmb.15.2022.09.11.13.15.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Sep 2022 13:15:14 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet Subject: [PATCH bpf-next v2 6/8] bpftool: Add LLVM as default library for disassembling JIT-ed programs Date: Sun, 11 Sep 2022 21:14:49 +0100 Message-Id: <20220911201451.12368-7-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220911201451.12368-1-quentin@isovalent.com> References: <20220911201451.12368-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net To disassemble instructions for JIT-ed programs, bpftool has relied on the libbfd library. This has been problematic in the past: libbfd's interface is not meant to be stable and has changed several times. For building bpftool, we have to detect how the libbfd version on the system behaves, which is why we have to handle features disassembler-four-args and disassembler-init-styled in the Makefile. When it comes to shipping bpftool, this has also caused issues with several distribution maintainers unwilling to support the feature (see for example Debian's page for binutils-dev, which ships libbfd: "Note that building Debian packages which depend on the shared libbfd is Not Allowed." [0]). For these reasons, we add support for LLVM as an alternative to libbfd for disassembling instructions of JIT-ed programs. Thanks to the preparation work in the previous commits, it's easy to add the library by passing the relevant compilation options in the Makefile, and by adding the functions for setting up the LLVM disassembler in file jit_disasm.c. Naturally, the display of disassembled instructions comes with a few minor differences. Here is a sample output with libbfd (already supported before this patch): # bpftool prog dump jited id 56 bpf_prog_6deef7357e7b4530: 0: nopl 0x0(%rax,%rax,1) 5: xchg %ax,%ax 7: push %rbp 8: mov %rsp,%rbp b: push %rbx c: push %r13 e: push %r14 10: mov %rdi,%rbx 13: movzwq 0xb4(%rbx),%r13 1b: xor %r14d,%r14d 1e: or $0x2,%r14d 22: mov $0x1,%eax 27: cmp $0x2,%r14 2b: jne 0x000000000000002f 2d: xor %eax,%eax 2f: pop %r14 31: pop %r13 33: pop %rbx 34: leave 35: ret LLVM supports several variants that we could set when initialising the disassembler, for example with: LLVMSetDisasmOptions(*ctx, LLVMDisassembler_Option_AsmPrinterVariant); but the default printer is used for now. Here is the output with LLVM: # bpftool prog dump jited id 56 bpf_prog_6deef7357e7b4530: 0: nopl (%rax,%rax) 5: nop 7: pushq %rbp 8: movq %rsp, %rbp b: pushq %rbx c: pushq %r13 e: pushq %r14 10: movq %rdi, %rbx 13: movzwq 180(%rbx), %r13 1b: xorl %r14d, %r14d 1e: orl $2, %r14d 22: movl $1, %eax 27: cmpq $2, %r14 2b: jne 0x2f 2d: xorl %eax, %eax 2f: popq %r14 31: popq %r13 33: popq %rbx 34: leave 35: retq The LLVM disassembler comes as the default choice, with libbfd as a fall-back. Of course, we could replace libbfd entirely and avoid supporting two different libraries. One reason for keeping libbfd is that, right now, it works well, we have all we need in terms of features detection in the Makefile, so it provides a fallback for disassembling JIT-ed programs if libbfd is installed but LLVM is not. The other motivation is that libbfd supports nfp instruction for Netronome's SmartNICs and can be used to disassemble offloaded programs, something that LLVM cannot do. If libbfd's interface breaks again in the future, we might reconsider keeping support for it. [0] https://packages.debian.org/buster/binutils-dev Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Yonghong Song --- tools/bpf/bpftool/Makefile | 46 +++++++++----- tools/bpf/bpftool/jit_disasm.c | 112 ++++++++++++++++++++++++++++++++- tools/bpf/bpftool/main.h | 4 +- 3 files changed, 140 insertions(+), 22 deletions(-) diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile index 1c81f4d514bb..787b857d3fb5 100644 --- a/tools/bpf/bpftool/Makefile +++ b/tools/bpf/bpftool/Makefile @@ -95,6 +95,7 @@ RM ?= rm -f FEATURE_USER = .bpftool FEATURE_TESTS := clang-bpf-co-re +FEATURE_TESTS += llvm FEATURE_TESTS += libcap FEATURE_TESTS += libbfd FEATURE_TESTS += libbfd-liberty @@ -103,6 +104,7 @@ FEATURE_TESTS += disassembler-four-args FEATURE_TESTS += disassembler-init-styled FEATURE_DISPLAY := clang-bpf-co-re +FEATURE_DISPLAY += llvm FEATURE_DISPLAY += libcap FEATURE_DISPLAY += libbfd FEATURE_DISPLAY += libbfd-liberty @@ -137,27 +139,37 @@ all: $(OUTPUT)bpftool SRCS := $(wildcard *.c) -ifeq ($(feature-libbfd),1) - LIBS += -lbfd -ldl -lopcodes -else ifeq ($(feature-libbfd-liberty),1) - LIBS += -lbfd -ldl -lopcodes -liberty -else ifeq ($(feature-libbfd-liberty-z),1) - LIBS += -lbfd -ldl -lopcodes -liberty -lz -endif +ifeq ($(feature-llvm),1) + # If LLVM is available, use it for JIT disassembly + CFLAGS += -DHAVE_LLVM_SUPPORT + LLVM_CONFIG_LIB_COMPONENTS := mcdisassembler all-targets + CFLAGS += $(shell $(LLVM_CONFIG) --cflags --libs $(LLVM_CONFIG_LIB_COMPONENTS)) + LIBS += $(shell $(LLVM_CONFIG) --libs $(LLVM_CONFIG_LIB_COMPONENTS)) + LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags) +else + # Fall back on libbfd + ifeq ($(feature-libbfd),1) + LIBS += -lbfd -ldl -lopcodes + else ifeq ($(feature-libbfd-liberty),1) + LIBS += -lbfd -ldl -lopcodes -liberty + else ifeq ($(feature-libbfd-liberty-z),1) + LIBS += -lbfd -ldl -lopcodes -liberty -lz + endif -# If one of the above feature combinations is set, we support libbfd -ifneq ($(filter -lbfd,$(LIBS)),) - CFLAGS += -DHAVE_LIBBFD_SUPPORT + # If one of the above feature combinations is set, we support libbfd + ifneq ($(filter -lbfd,$(LIBS)),) + CFLAGS += -DHAVE_LIBBFD_SUPPORT - # Libbfd interface changed over time, figure out what we need - ifeq ($(feature-disassembler-four-args), 1) - CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE - endif - ifeq ($(feature-disassembler-init-styled), 1) - CFLAGS += -DDISASM_INIT_STYLED + # Libbfd interface changed over time, figure out what we need + ifeq ($(feature-disassembler-four-args), 1) + CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE + endif + ifeq ($(feature-disassembler-init-styled), 1) + CFLAGS += -DDISASM_INIT_STYLED + endif endif endif -ifeq ($(filter -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),) +ifeq ($(filter -DHAVE_LLVM_SUPPORT -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),) # No support for JIT disassembly SRCS := $(filter-out jit_disasm.c,$(SRCS)) endif diff --git a/tools/bpf/bpftool/jit_disasm.c b/tools/bpf/bpftool/jit_disasm.c index e31ad3950fd6..c28b21f90cb9 100644 --- a/tools/bpf/bpftool/jit_disasm.c +++ b/tools/bpf/bpftool/jit_disasm.c @@ -20,18 +20,123 @@ #include #include #include -#include -#include #include #include #include + +#ifdef HAVE_LLVM_SUPPORT +#include +#include +#include +#include +#endif + +#ifdef HAVE_LIBBFD_SUPPORT +#include +#include #include +#endif #include "json_writer.h" #include "main.h" static int oper_count; +#ifdef HAVE_LLVM_SUPPORT +#define DISASM_SPACER + +typedef LLVMDisasmContextRef disasm_ctx_t; + +static int printf_json(char *s) +{ + s = strtok(s, " \t"); + jsonw_string_field(json_wtr, "operation", s); + + jsonw_name(json_wtr, "operands"); + jsonw_start_array(json_wtr); + oper_count = 1; + + while ((s = strtok(NULL, " \t,()")) != 0) { + jsonw_string(json_wtr, s); + oper_count++; + } + return 0; +} + +/* This callback to set the ref_type is necessary to have the LLVM disassembler + * print PC-relative addresses instead of byte offsets for branch instruction + * targets. + */ +static const char * +symbol_lookup_callback(__maybe_unused void *disasm_info, + __maybe_unused uint64_t ref_value, + uint64_t *ref_type, __maybe_unused uint64_t ref_PC, + __maybe_unused const char **ref_name) +{ + *ref_type = LLVMDisassembler_ReferenceType_InOut_None; + return NULL; +} + +static int +init_context(disasm_ctx_t *ctx, const char *arch, + __maybe_unused const char *disassembler_options, + __maybe_unused unsigned char *image, __maybe_unused ssize_t len) +{ + char *triple; + + if (arch) { + p_err("Architecture %s not supported", arch); + return -1; + } + + triple = LLVMGetDefaultTargetTriple(); + if (!triple) { + p_err("Failed to retrieve triple"); + return -1; + } + *ctx = LLVMCreateDisasm(triple, NULL, 0, NULL, symbol_lookup_callback); + LLVMDisposeMessage(triple); + + if (!*ctx) { + p_err("Failed to create disassembler"); + return -1; + } + + return 0; +} + +static void destroy_context(disasm_ctx_t *ctx) +{ + LLVMDisposeMessage(*ctx); +} + +static int +disassemble_insn(disasm_ctx_t *ctx, unsigned char *image, ssize_t len, int pc) +{ + char buf[256]; + int count; + + count = LLVMDisasmInstruction(*ctx, image + pc, len - pc, pc, + buf, sizeof(buf)); + if (json_output) + printf_json(buf); + else + printf("%s", buf); + + return count; +} + +int disasm_init(void) +{ + LLVMInitializeNativeTarget(); + LLVMInitializeNativeDisassembler(); + return 0; +} +#endif /* HAVE_LLVM_SUPPORT */ + +#ifdef HAVE_LIBBFD_SUPPORT +#define DISASM_SPACER "\t" + typedef struct { struct disassemble_info *info; disassembler_ftype disassemble; @@ -210,6 +315,7 @@ int disasm_init(void) bfd_init(); return 0; } +#endif /* HAVE_LIBBPFD_SUPPORT */ int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, const char *arch, const char *disassembler_options, @@ -252,7 +358,7 @@ int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, if (linfo) btf_dump_linfo_plain(btf, linfo, "; ", linum); - printf("%4x:\t", pc); + printf("%4x:" DISASM_SPACER, pc); } count = disassemble_insn(&ctx, image, len, pc); diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index c9e171082cf6..9a149c67aa5d 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -172,7 +172,7 @@ int map_parse_fds(int *argc, char ***argv, int **fds); int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len); struct bpf_prog_linfo; -#ifdef HAVE_LIBBFD_SUPPORT +#if defined(HAVE_LLVM_SUPPORT) || defined(HAVE_LIBBFD_SUPPORT) int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, const char *arch, const char *disassembler_options, const struct btf *btf, @@ -193,7 +193,7 @@ int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, } static inline int disasm_init(void) { - p_err("No libbfd support"); + p_err("No JIT disassembly support"); return -1; } #endif From patchwork Sun Sep 11 20:14:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12973059 X-Patchwork-Delegate: bpf@iogearbox.net 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 435C8ECAAD3 for ; Sun, 11 Sep 2022 20:15:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229597AbiIKUP1 (ORCPT ); Sun, 11 Sep 2022 16:15:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229583AbiIKUPZ (ORCPT ); Sun, 11 Sep 2022 16:15:25 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1129F1BE96 for ; Sun, 11 Sep 2022 13:15:24 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id b5so12277364wrr.5 for ; Sun, 11 Sep 2022 13:15:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=7Udp2ZXgLhv3fTpUhsQtFyg4gt4TLG5RtdOt3QsPbyo=; b=wi8ZCgkvHjXhT9RD2yMZYVTMATOSa7HGNdBatCo3rg8pegm90Zpq0rKXQEMn9qLfdK Fg+EaZVfJURjlWiODI/Z3oUxmd1S5Yra2UaiYxUnw0Jm62jCUrWS6L84r0dh+iS1+W5s mWhKnuZu4+1g/cWPckLkRBrik0dksM2LffXmFa0yQx5Y58vDUicjJlqjAFTl0d0s/JNG q0wo88WyhOk27pibp/07wezKZgOmcsTcRMmp8sM7mDyL1z9LD0L5DOdzu8jdJRa3oMvu s8EJgEBBjmdLnevIGMw1eRR81C3/JloSdQtaU6Lwmg9YH1Bvny4w+hBx5ueboWfC9P4P DNYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=7Udp2ZXgLhv3fTpUhsQtFyg4gt4TLG5RtdOt3QsPbyo=; b=A8Q82Z6br2GJJmmQfP5vj79ZcgOXfMg/gQsEtto0J0+oQWeT+TUd37+sK90+VrzZcc k+aMmySfD9htihLch1lce2sbqAMxb1KBQu1OM3qjuy1LOyQIVd/xPiJMWFSKIXIrC8zh TvlPtwDJYPpYOtcapTGS/mjvCgZCODJuE8y1UU47VObAKx4aep7g+jemOpyiwmKzGYrO riP8UUQns0iV3qjNNz6u943IygVclcFxdv0cZMFpqykgrqLR2ZGnhRBLkl1ZsBiZ06Jg cx6zFRS8aVcqQbGVLs4wGNRoTb72hmeJd3SD/nbD5xyCHxuS0+CVHp7oHo6RcOfr2UkZ 2tuQ== X-Gm-Message-State: ACgBeo0AhGPpHxi2wUKECW7HZaq1400z5EWz8l6T9mLT5vtwa8GAfudi /HhhxMWdhKq90OJreTkDpflMMw== X-Google-Smtp-Source: AA6agR76a3yhnJdyXmf5KZjfeG4cF2+oAo7PMd7qkZzJu0/xbXhoLZEtoeS0Gb9YvknPVq8Sj2ByVg== X-Received: by 2002:a5d:618a:0:b0:228:a5a6:fb4a with SMTP id j10-20020a5d618a000000b00228a5a6fb4amr12930177wru.234.1662927322598; Sun, 11 Sep 2022 13:15:22 -0700 (PDT) Received: from harfang.access.network ([185.122.133.20]) by smtp.gmail.com with ESMTPSA id bh16-20020a05600c3d1000b003a60ff7c082sm7603789wmb.15.2022.09.11.13.15.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Sep 2022 13:15:19 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet , Song Liu Subject: [PATCH bpf-next v2 7/8] bpftool: Support setting alternative arch for JIT disasm with LLVM Date: Sun, 11 Sep 2022 21:14:50 +0100 Message-Id: <20220911201451.12368-8-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220911201451.12368-1-quentin@isovalent.com> References: <20220911201451.12368-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net For offloaded BPF programs, instead of failing to create the LLVM disassembler without even looking for a triple at all, do run the function that attempts to retrieve a valid architecture name for the device. It will still fail for the LLVM disassembler, because currently we have no valid triple to return (NFP disassembly is not supported by LLVM). But failing in that function is more logical than to assume in jit_disasm.c that passing an "arch" name is simply not supported. Suggested-by: Song Liu Signed-off-by: Quentin Monnet --- tools/bpf/bpftool/common.c | 10 ++++++---- tools/bpf/bpftool/jit_disasm.c | 15 +++++++-------- tools/bpf/bpftool/main.h | 3 +-- tools/bpf/bpftool/prog.c | 6 ++---- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c index 4c2e909a2d67..e4d33bc8bbbf 100644 --- a/tools/bpf/bpftool/common.c +++ b/tools/bpf/bpftool/common.c @@ -627,12 +627,11 @@ static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name) } const char * -ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, - const char **opt) +ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt) { + __maybe_unused int device_id; char devname[IF_NAMESIZE]; int vendor_id; - int device_id; if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) { p_err("Can't get net device name for ifindex %d: %s", ifindex, @@ -647,6 +646,7 @@ ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, } switch (vendor_id) { +#ifdef HAVE_LIBBFD_SUPPORT case 0x19ee: device_id = read_sysfs_netdev_hex_int(devname, "device"); if (device_id != 0x4000 && @@ -655,8 +655,10 @@ ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch"); *opt = "ctx4"; return "NFP-6xxx"; +#endif /* HAVE_LIBBFD_SUPPORT */ + /* No NFP support in LLVM, we have no valid triple to return. */ default: - p_err("Can't get bfd arch name for device vendor id 0x%04x", + p_err("Can't get arch name for device vendor id 0x%04x", vendor_id); return NULL; } diff --git a/tools/bpf/bpftool/jit_disasm.c b/tools/bpf/bpftool/jit_disasm.c index c28b21f90cb9..58a5017034a2 100644 --- a/tools/bpf/bpftool/jit_disasm.c +++ b/tools/bpf/bpftool/jit_disasm.c @@ -84,12 +84,10 @@ init_context(disasm_ctx_t *ctx, const char *arch, { char *triple; - if (arch) { - p_err("Architecture %s not supported", arch); - return -1; - } - - triple = LLVMGetDefaultTargetTriple(); + if (arch) + triple = LLVMNormalizeTargetTriple(arch); + else + triple = LLVMGetDefaultTargetTriple(); if (!triple) { p_err("Failed to retrieve triple"); return -1; @@ -128,8 +126,9 @@ disassemble_insn(disasm_ctx_t *ctx, unsigned char *image, ssize_t len, int pc) int disasm_init(void) { - LLVMInitializeNativeTarget(); - LLVMInitializeNativeDisassembler(); + LLVMInitializeAllTargetInfos(); + LLVMInitializeAllTargetMCs(); + LLVMInitializeAllDisassemblers(); return 0; } #endif /* HAVE_LLVM_SUPPORT */ diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index 9a149c67aa5d..467d8472df0c 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -203,8 +203,7 @@ void print_hex_data_json(uint8_t *data, size_t len); unsigned int get_page_size(void); unsigned int get_possible_cpus(void); const char * -ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, - const char **opt); +ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt); struct btf_dumper { const struct btf *btf; diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index 345dca656a34..aa041ae8ec32 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -764,10 +764,8 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, const char *name = NULL; if (info->ifindex) { - name = ifindex_to_bfd_params(info->ifindex, - info->netns_dev, - info->netns_ino, - &disasm_opt); + name = ifindex_to_arch(info->ifindex, info->netns_dev, + info->netns_ino, &disasm_opt); if (!name) goto exit_free; } From patchwork Sun Sep 11 20:14:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12973060 X-Patchwork-Delegate: bpf@iogearbox.net 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 0D0BFECAAD3 for ; Sun, 11 Sep 2022 20:15:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229583AbiIKUP3 (ORCPT ); Sun, 11 Sep 2022 16:15:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229539AbiIKUP3 (ORCPT ); Sun, 11 Sep 2022 16:15:29 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04EDD1C12D for ; Sun, 11 Sep 2022 13:15:27 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id t7so12257299wrm.10 for ; Sun, 11 Sep 2022 13:15:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=3svs1veSlQXEKs+OzbLdgeJamu/CII4Jy/jB0glIdS8=; b=IYANcLHPfz+UrUk2tl44OKL4nZyE5ZuZShjPqWEfwVhVy7OBUTvX+/1UWaEO8C1XfR MIg7roMfkx83d9QxuOXNG+Qecudpr8/McmleUgwg4pOlSstQNcW9dErj8253laX7uwrb eM+ewyrhkhKGYgNBWA3Qq8ReFsQ/blACsUSu9xaXJgckzBVqkn6kCESkgCe0uIkFNHFe GgtbF0WVmqBl67TuyQy8rZDrPKwad/GqfRgNVxtAB3IX0Kr4OiRtNULCtMcJ0Xo9OqXD xxTfn8T4kTsmdBBrMOX1hEVrULd0xC/F+W+G1g3EtG2uBt1Joz8yavIWM4I44kJtNQMv imug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=3svs1veSlQXEKs+OzbLdgeJamu/CII4Jy/jB0glIdS8=; b=TSxgUNl7xtvJpzssOyStBUA5mIo7K4EE7DsMg8fztIx2+y8K1U7LdwVXxV2imSSpOF 6txMBXFi0c6JOx1Uecr6dRZNmCKkdLuecktDZ2IsJgmbfF3jfJfI2KJeNVjpSrJcqRfn qYgr6sEICcAc5aBVzeg08bisRkiR93zSS5XJTAlACHKN2UkB0TJOLi/IDGIDwgnkQQyE Ia+FclaGNyEJBrUx5/oK1vuZocznxmtJTCD+PeMrFXh+W0DVXq9hQoa/2SDjIpd4CHgN Q+bzq4WZIbd6Ul6/S+ZT0rIuUD0Riy2YlxgTGVeSiWpRlWA575IjlVlSrBZUHTDnoxxN IGBw== X-Gm-Message-State: ACgBeo2jk+A5/jnGh7lAmT6dWZeujpFBevg6Ssxc7aEF0ma0lV1fOSgf bxDxuvz2aL/ZDMOKc7NrA1u02w== X-Google-Smtp-Source: AA6agR61bljDVqCAVvY6mMslz2eYM9NtUQbCi0KfvJY5eoWqmtRVeLXdIetqgeEZjUqxoPwMzY8F2w== X-Received: by 2002:a5d:6da7:0:b0:226:e081:941a with SMTP id u7-20020a5d6da7000000b00226e081941amr13053639wrs.642.1662927325486; Sun, 11 Sep 2022 13:15:25 -0700 (PDT) Received: from harfang.access.network ([185.122.133.20]) by smtp.gmail.com with ESMTPSA id bh16-20020a05600c3d1000b003a60ff7c082sm7603789wmb.15.2022.09.11.13.15.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Sep 2022 13:15:23 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet Subject: [PATCH bpf-next v2 8/8] bpftool: Add llvm feature to "bpftool version" Date: Sun, 11 Sep 2022 21:14:51 +0100 Message-Id: <20220911201451.12368-9-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220911201451.12368-1-quentin@isovalent.com> References: <20220911201451.12368-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Similarly to "libbfd", add a "llvm" feature to the output of command "bpftool version" to indicate that LLVM is used for disassembling JIT-ed programs. This feature is mutually exclusive with "libbfd". Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Yonghong Song --- tools/bpf/bpftool/Documentation/common_options.rst | 8 ++++---- tools/bpf/bpftool/main.c | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/tools/bpf/bpftool/Documentation/common_options.rst b/tools/bpf/bpftool/Documentation/common_options.rst index 4107a586b68b..05350a1aadf9 100644 --- a/tools/bpf/bpftool/Documentation/common_options.rst +++ b/tools/bpf/bpftool/Documentation/common_options.rst @@ -7,10 +7,10 @@ Print bpftool's version number (similar to **bpftool version**), the number of the libbpf version in use, and optional features that were included when bpftool was compiled. Optional features include linking - against libbfd to provide the disassembler for JIT-ted programs - (**bpftool prog dump jited**) and usage of BPF skeletons (some - features like **bpftool prog profile** or showing pids associated to - BPF objects may rely on it). + against LLVM or libbfd to provide the disassembler for JIT-ted + programs (**bpftool prog dump jited**) and usage of BPF skeletons + (some features like **bpftool prog profile** or showing pids + associated to BPF objects may rely on it). -j, --json Generate JSON output. For commands that cannot produce JSON, this diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c index ccd7457f92bf..7e06ca2c5d42 100644 --- a/tools/bpf/bpftool/main.c +++ b/tools/bpf/bpftool/main.c @@ -89,6 +89,11 @@ static int do_version(int argc, char **argv) #else const bool has_libbfd = false; #endif +#ifdef HAVE_LLVM_SUPPORT + const bool has_llvm = true; +#else + const bool has_llvm = false; +#endif #ifdef BPFTOOL_WITHOUT_SKELETONS const bool has_skeletons = false; #else @@ -112,6 +117,7 @@ static int do_version(int argc, char **argv) jsonw_name(json_wtr, "features"); jsonw_start_object(json_wtr); /* features */ jsonw_bool_field(json_wtr, "libbfd", has_libbfd); + jsonw_bool_field(json_wtr, "llvm", has_llvm); jsonw_bool_field(json_wtr, "libbpf_strict", !legacy_libbpf); jsonw_bool_field(json_wtr, "skeletons", has_skeletons); jsonw_end_object(json_wtr); /* features */ @@ -132,6 +138,10 @@ static int do_version(int argc, char **argv) printf(" libbfd"); nb_features++; } + if (has_llvm) { + printf(" llvm"); + nb_features++; + } if (!legacy_libbpf) { printf("%s libbpf_strict", nb_features++ ? "," : ""); nb_features++;