From patchwork Wed Feb 17 11:08:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giuliano Procida X-Patchwork-Id: 12091291 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 X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B1C09C433E0 for ; Wed, 17 Feb 2021 11:10:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8738264E24 for ; Wed, 17 Feb 2021 11:10:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230416AbhBQLJ2 (ORCPT ); Wed, 17 Feb 2021 06:09:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230486AbhBQLI5 (ORCPT ); Wed, 17 Feb 2021 06:08:57 -0500 Received: from mail-qt1-x84a.google.com (mail-qt1-x84a.google.com [IPv6:2607:f8b0:4864:20::84a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BF4BC0613D6 for ; Wed, 17 Feb 2021 03:08:17 -0800 (PST) Received: by mail-qt1-x84a.google.com with SMTP id l1so9864189qtv.2 for ; Wed, 17 Feb 2021 03:08:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=ajZkQUpdD91YaKxYZS+6hb11vUCM5tx8VfmWTzbq5CE=; b=tPeKGEJsRm7bag5ChoB4+RYn4gD3GJjqyfimARLITA5CzqOfCuAQ1uSrwW9cWm9JyQ KTLMAIamnPujMoVKCoD9UoCkdnUV0wJY5Ujdx+XejD65wKFo86/uNJdwvztoewwxx5QJ RWJF6Fa6yuH7Efa3ZZEQgULYaI4zvgDt7/aB/IYv2CJ7Dn1Y1TLJ3oxFCL8JdS026WV4 swjU4QXn1xcduIkxzQtSphug/aarIqdg3uppjYi5xOr26nzLzIhWUNWh4wO8SOh/+Epy JvMCM3b2DAdTI3F2tRonQCb4o8f91mWrdgERX2wDHRcTv2urtI4URqiQZR9KOfP2YJzx Ob2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ajZkQUpdD91YaKxYZS+6hb11vUCM5tx8VfmWTzbq5CE=; b=uDkDD/TMvJ75ZgzO0RNwpfHMvlqHQkIyhE+l+GZzpMsfAnaQTkEqNIOgiJxoZrukRU NUpQDRJz8SEYvHhb6l5dT00676/o0cYGi5QNHU2xa03yfZiPwat13KPLDh3XuIVZyjzH CbEGXiboBakBRZwKRBs0EnEzsTgL+EYRHBC/lyl/tk/QFksTglv61yRcn91zSQGS/WVw PziNZ68TZzQKIRwEVBk6JPb8FoIHrJUYQBWb8SHX/07xxdQkPWzn+J8f5i9UTzp1G+1u onRCMG3wMo9SryKMaEdQHNFThc1QOGnNQ3dagHjuuFWx1s7YrjhRFG/D9v6J3KpR7zN0 yxwQ== X-Gm-Message-State: AOAM530V4JCfYOU+8zk+INV8PmYYU0GjarU2d2ChbQeP+6YY0TrktllH u4I4HRJh1Ov7Ecw8WYm90Ym/8WU/JLGaSw== X-Google-Smtp-Source: ABdhPJzg1aMAb13eTxUh3bqLKkQVWxGXNaAaDk1GlCH9Rd+XiSyWwrc3ipEuNbzCtt3z/liffkc0HhKA31nxXA== Sender: "gprocida via sendgmr" X-Received: from tef.lon.corp.google.com ([2a00:79e0:d:110:61b3:1cb2:c180:c3f]) (user=gprocida job=sendgmr) by 2002:a0c:fc44:: with SMTP id w4mr22445659qvp.55.1613560096470; Wed, 17 Feb 2021 03:08:16 -0800 (PST) Date: Wed, 17 Feb 2021 11:08:00 +0000 In-Reply-To: <20210217110804.75923-1-gprocida@google.com> Message-Id: <20210217110804.75923-2-gprocida@google.com> Mime-Version: 1.0 References: <20210205134221.2953163-1-gprocida@google.com> <20210217110804.75923-1-gprocida@google.com> X-Mailer: git-send-email 2.30.0.478.g8a0d178c01-goog Subject: [PATCH dwarves v4 1/5] btf_encoder: Funnel ELF error reporting through a macro From: Giuliano Procida To: dwarves@vger.kernel.org, acme@kernel.org Cc: andrii@kernel.org, ast@kernel.org, gprocida@google.com, maennich@google.com, kernel-team@android.com, kernel-team@fb.com, bpf@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org This adds elf_error which prepends error messages with the function and appends a readable ELF error status. Also capitalise ELF consistently in error messages. Acked-by: Andrii Nakryiko Signed-off-by: Giuliano Procida --- libbtf.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/libbtf.c b/libbtf.c index 9f76283..7bc49ba 100644 --- a/libbtf.c +++ b/libbtf.c @@ -27,6 +27,16 @@ #include "dwarves.h" #include "elf_symtab.h" +/* + * This depends on the GNU extension to eliminate the stray comma in the zero + * arguments case. + * + * The difference between elf_errmsg(-1) and elf_errmsg(elf_errno()) is that the + * latter clears the current error. + */ +#define elf_error(fmt, ...) \ + fprintf(stderr, "%s: " fmt ": %s.\n", __func__, ##__VA_ARGS__, elf_errmsg(-1)) + struct btf *base_btf; uint8_t btf_elf__verbose; uint8_t btf_elf__force; @@ -103,15 +113,13 @@ try_as_raw_btf: goto errout; if (elf_version(EV_CURRENT) == EV_NONE) { - fprintf(stderr, "%s: cannot set libelf version.\n", - __func__); + elf_error("cannot set libelf version"); goto errout; } btfe->elf = elf_begin(btfe->in_fd, ELF_C_READ_MMAP, NULL); if (!btfe->elf) { - fprintf(stderr, "%s: cannot read %s ELF file: %s.\n", - __func__, filename, elf_errmsg(elf_errno())); + elf_error("cannot read %s ELF file", filename); goto errout; } } @@ -127,7 +135,7 @@ try_as_raw_btf: goto try_as_raw_btf; } if (btf_elf__verbose) - fprintf(stderr, "%s: cannot get elf header.\n", __func__); + elf_error("cannot get ELF header"); goto errout; } @@ -141,7 +149,7 @@ try_as_raw_btf: btf__set_endianness(btfe->btf, BTF_BIG_ENDIAN); break; default: - fprintf(stderr, "%s: unknown elf endianness.\n", __func__); + fprintf(stderr, "%s: unknown ELF endianness.\n", __func__); goto errout; } @@ -707,15 +715,13 @@ static int btf_elf__write(const char *filename, struct btf *btf) } if (elf_version(EV_CURRENT) == EV_NONE) { - fprintf(stderr, "Cannot set libelf version: %s.\n", - elf_errmsg(elf_errno())); + elf_error("Cannot set libelf version"); goto out; } elf = elf_begin(fd, ELF_C_RDWR, NULL); if (elf == NULL) { - fprintf(stderr, "Cannot update ELF file: %s.\n", - elf_errmsg(elf_errno())); + elf_error("Cannot update ELF file"); goto out; } @@ -723,8 +729,7 @@ static int btf_elf__write(const char *filename, struct btf *btf) ehdr = gelf_getehdr(elf, &ehdr_mem); if (ehdr == NULL) { - fprintf(stderr, "%s: elf_getehdr failed: %s.\n", __func__, - elf_errmsg(elf_errno())); + elf_error("elf_getehdr failed"); goto out; } @@ -736,7 +741,7 @@ static int btf_elf__write(const char *filename, struct btf *btf) btf__set_endianness(btf, BTF_BIG_ENDIAN); break; default: - fprintf(stderr, "%s: unknown elf endianness.\n", __func__); + fprintf(stderr, "%s: unknown ELF endianness.\n", __func__); goto out; } @@ -768,8 +773,7 @@ static int btf_elf__write(const char *filename, struct btf *btf) elf_update(elf, ELF_C_WRITE) >= 0) err = 0; else - fprintf(stderr, "%s: elf_update failed: %s.\n", - __func__, elf_errmsg(elf_errno())); + elf_error("elf_update failed"); } else { const char *llvm_objcopy; char tmp_fn[PATH_MAX]; From patchwork Wed Feb 17 11:08:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giuliano Procida X-Patchwork-Id: 12091295 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 X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DB847C433E6 for ; Wed, 17 Feb 2021 11:10:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B354264E79 for ; Wed, 17 Feb 2021 11:10:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230470AbhBQLJa (ORCPT ); Wed, 17 Feb 2021 06:09:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231181AbhBQLJA (ORCPT ); Wed, 17 Feb 2021 06:09:00 -0500 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BD9EC06178A for ; Wed, 17 Feb 2021 03:08:20 -0800 (PST) Received: by mail-wm1-x34a.google.com with SMTP id b201so2064689wmb.9 for ; Wed, 17 Feb 2021 03:08:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=CtyPUVbXhc5CDRZLtdHVem+8lAgiUhAKNAjbcL0rPXE=; b=PoCIn0uyIl6lXycffqtFFJqz/msRd6zXJMEsOtAkL16xrfSuU9+ZDpyV+ggWH0OtgA XTAw5Ji/YiGGohzvGFcypyRxBytlHzM+oNqHyJsoN9WVPa3JH7nNQIaxy5UJBJczU+Pa xg2qksgjRVQIKqxEGacDNN1IyL9/T+xeb424GKaD9Ff8VcL4BW75zZl1GrGxKT7QM4EJ fjdrPXG10sqbEwHfvM6cG9XVX1dub0A71yBb5aD/rRKkI4bGaPRJwHxFUaKH0fE7rEgp vJf1vNlWSNiKRLN9vIxN9bwU+RPxjt2o5j9zBoLQyctVpwpzFMhsylEINxnR+NQw1NSP 5nxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=CtyPUVbXhc5CDRZLtdHVem+8lAgiUhAKNAjbcL0rPXE=; b=FBZyBe3r6YpXcjElctyNASspQiVfa2dg55HmCK3OX3roYmhfmm06KfiFw2p9wfmM+R aRc3IJaDxnkE3mqmqv31UvZEYsUPIElhLPWunh598SwIBMflkrV1ScZ0TJmt2NP1VlxT QyWaKUGaPC5ScOZnLkiO1dj5+YA9UkSyyHdBxw5Ebk5ASyoZF6EilLEq7JQPal8ETiYe zZKcqZomLofDQ+b9UqeJubxqG27wKsDxL10GDvihDOVmfLhAZ4V7xANuq5IAEjlpdy7H qKxiE8Qt2et1I8jUm62BTPPZc+PU0lm357NSX1RygaqVxIYUZjfjcmtvqEvIdaKQdHUx /tow== X-Gm-Message-State: AOAM533o2ckLNIQew1u1PPn+ymxbIJI67zmNmIjv2WSjl9EqguFKs2JK W5d38SU+jqqymV9SVyAvDtyFQ5KUUY+5BA== X-Google-Smtp-Source: ABdhPJym3p+zIhDoQ+3jBbV+a0GOC2AQpwjftZY+ZDQpPFM7fGDhIg3x/c2hdrCQkQttCGyodvElvcISkRuMUA== Sender: "gprocida via sendgmr" X-Received: from tef.lon.corp.google.com ([2a00:79e0:d:110:61b3:1cb2:c180:c3f]) (user=gprocida job=sendgmr) by 2002:a05:600c:2d44:: with SMTP id a4mr6563054wmg.95.1613560098890; Wed, 17 Feb 2021 03:08:18 -0800 (PST) Date: Wed, 17 Feb 2021 11:08:01 +0000 In-Reply-To: <20210217110804.75923-1-gprocida@google.com> Message-Id: <20210217110804.75923-3-gprocida@google.com> Mime-Version: 1.0 References: <20210205134221.2953163-1-gprocida@google.com> <20210217110804.75923-1-gprocida@google.com> X-Mailer: git-send-email 2.30.0.478.g8a0d178c01-goog Subject: [PATCH dwarves v4 2/5] btf_encoder: Do not use both structs and pointers for the same data From: Giuliano Procida To: dwarves@vger.kernel.org, acme@kernel.org Cc: andrii@kernel.org, ast@kernel.org, gprocida@google.com, maennich@google.com, kernel-team@android.com, kernel-team@fb.com, bpf@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Many operations in the libelf API return a pointer to a user-provided struct (on success) or NULL (on failure). There are a couple of places in btf_elf__write where both structs and pointers to the same structs are used. Holding on to the pointers raises ownership and lifetime issues unnecessarily and the code is cleaner with only a single access path for these data. The code now treats the returned pointers as booleans. Acked-by: Andrii Nakryiko Signed-off-by: Giuliano Procida --- libbtf.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/libbtf.c b/libbtf.c index 7bc49ba..ace8896 100644 --- a/libbtf.c +++ b/libbtf.c @@ -698,8 +698,7 @@ int32_t btf_elf__add_datasec_type(struct btf_elf *btfe, const char *section_name static int btf_elf__write(const char *filename, struct btf *btf) { - GElf_Shdr shdr_mem, *shdr; - GElf_Ehdr ehdr_mem, *ehdr; + GElf_Ehdr ehdr; Elf_Data *btf_data = NULL; Elf_Scn *scn = NULL; Elf *elf = NULL; @@ -727,13 +726,12 @@ static int btf_elf__write(const char *filename, struct btf *btf) elf_flagelf(elf, ELF_C_SET, ELF_F_DIRTY); - ehdr = gelf_getehdr(elf, &ehdr_mem); - if (ehdr == NULL) { + if (!gelf_getehdr(elf, &ehdr)) { elf_error("elf_getehdr failed"); goto out; } - switch (ehdr_mem.e_ident[EI_DATA]) { + switch (ehdr.e_ident[EI_DATA]) { case ELFDATA2LSB: btf__set_endianness(btf, BTF_LITTLE_ENDIAN); break; @@ -751,10 +749,10 @@ static int btf_elf__write(const char *filename, struct btf *btf) elf_getshdrstrndx(elf, &strndx); while ((scn = elf_nextscn(elf, scn)) != NULL) { - shdr = gelf_getshdr(scn, &shdr_mem); - if (shdr == NULL) + GElf_Shdr shdr; + if (!gelf_getshdr(scn, &shdr)) continue; - char *secname = elf_strptr(elf, strndx, shdr->sh_name); + char *secname = elf_strptr(elf, strndx, shdr.sh_name); if (strcmp(secname, ".BTF") == 0) { btf_data = elf_getdata(scn, btf_data); break; From patchwork Wed Feb 17 11:08:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giuliano Procida X-Patchwork-Id: 12091293 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 X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE360C43381 for ; Wed, 17 Feb 2021 11:10:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C80DC64EAE for ; Wed, 17 Feb 2021 11:10:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231181AbhBQLJe (ORCPT ); Wed, 17 Feb 2021 06:09:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231466AbhBQLJC (ORCPT ); Wed, 17 Feb 2021 06:09:02 -0500 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E59F7C06178C for ; Wed, 17 Feb 2021 03:08:21 -0800 (PST) Received: by mail-qv1-xf4a.google.com with SMTP id y16so9715396qvs.12 for ; Wed, 17 Feb 2021 03:08:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=IfrsKiM9qTPCm/mu8IvDda9E8qIyarKKAm3lIwYPM20=; b=inbroXb8rNUuFuFw3kMiTs2hMpy6Pj1G6RKGaHjP7A9qVkb/BIEEMVUBll3DeGv+oL 65IavyDeNsBiWjbjbiZGi3BdHZEs38HEaVioWRqUM93C00FL7UcCeQvgtoLo4yGtH+AB Gk+9l7rwQ1ZazlNx3ElOaBHn8hD6idoHjb+Ew5NcsfMIC53Fe3kgXP7swcrvBfw76jEQ +ybbbNnNjvxkYXvHUFxA5ypZt3YP4hPH+YFSMhv0HTO+l/JGwQPHeaAx8jkGGPHw+tY9 UUSGcKK1MaUEAgfAiDddInmT0BBFGvSGvQyUfOYOj9J/cG0963g9pGCkOHU09h8GAmpY 26OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=IfrsKiM9qTPCm/mu8IvDda9E8qIyarKKAm3lIwYPM20=; b=KejZMJXV9zhEFirphNW3bItp9FuyqAsRcccNLBXFcOql0MgmGj4RtZAvyH7WOAlDmv 4WFRp//D9zeaoPIq4dRHqIWeYEhrbjtV1YFkYh7Tw+EKffmcYDaBJq8VufHqV3Y97pLi /2PYRAvv/irodw2kDKkSuegtxvHHaEAC5SAgm43oyrMlXKfgJsfFDok+c01OjL4yoIlQ GXJG0QroPBJHO8uQ8/nOSPQI2wmgfT5ZdwDCgAgI56gnT6UQkzK+r4bk/D0heby8skst YX2cqbujVsKym5j/QLbrtP/Nlp/S46AIqy5t+VuU2NY68GZT3OAYQsj14Gok/BnM3poG LE8Q== X-Gm-Message-State: AOAM531A/9aRqt89eHZY6u1xgcY90QSIyfJsoTPc/M+gMCdQYcC0LyTB /COR0MZ9X7y84DGxvHaWcHWpd5yGjbHjGw== X-Google-Smtp-Source: ABdhPJxZIxtFwhvPKVtmI3xcKyLpDdQKBhyOFBS5FG4Qyf3pD4m41kvSNZBEpagTy5xgzrfoQaCwblzdYBSHPA== Sender: "gprocida via sendgmr" X-Received: from tef.lon.corp.google.com ([2a00:79e0:d:110:61b3:1cb2:c180:c3f]) (user=gprocida job=sendgmr) by 2002:ad4:4e14:: with SMTP id dl20mr1895136qvb.51.1613560101098; Wed, 17 Feb 2021 03:08:21 -0800 (PST) Date: Wed, 17 Feb 2021 11:08:02 +0000 In-Reply-To: <20210217110804.75923-1-gprocida@google.com> Message-Id: <20210217110804.75923-4-gprocida@google.com> Mime-Version: 1.0 References: <20210205134221.2953163-1-gprocida@google.com> <20210217110804.75923-1-gprocida@google.com> X-Mailer: git-send-email 2.30.0.478.g8a0d178c01-goog Subject: [PATCH dwarves v4 3/5] btf_encoder: Traverse sections using a for-loop From: Giuliano Procida To: dwarves@vger.kernel.org, acme@kernel.org Cc: andrii@kernel.org, ast@kernel.org, gprocida@google.com, maennich@google.com, kernel-team@android.com, kernel-team@fb.com, bpf@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org The pointer (iterator) scn can be made local to the loop and a more general while-loop is not needed. Signed-off-by: Giuliano Procida --- libbtf.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libbtf.c b/libbtf.c index ace8896..4ae7150 100644 --- a/libbtf.c +++ b/libbtf.c @@ -700,7 +700,6 @@ static int btf_elf__write(const char *filename, struct btf *btf) { GElf_Ehdr ehdr; Elf_Data *btf_data = NULL; - Elf_Scn *scn = NULL; Elf *elf = NULL; const void *raw_btf_data; uint32_t raw_btf_size; @@ -748,7 +747,7 @@ static int btf_elf__write(const char *filename, struct btf *btf) */ elf_getshdrstrndx(elf, &strndx); - while ((scn = elf_nextscn(elf, scn)) != NULL) { + for (Elf_Scn *scn = elf_nextscn(elf, NULL); scn; scn = elf_nextscn(elf, scn)) { GElf_Shdr shdr; if (!gelf_getshdr(scn, &shdr)) continue; From patchwork Wed Feb 17 11:08:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giuliano Procida X-Patchwork-Id: 12091297 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 X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18E5DC433E9 for ; Wed, 17 Feb 2021 11:10:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0143564DEC for ; Wed, 17 Feb 2021 11:10:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231491AbhBQLJo (ORCPT ); Wed, 17 Feb 2021 06:09:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231496AbhBQLJS (ORCPT ); Wed, 17 Feb 2021 06:09:18 -0500 Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 55481C061794 for ; Wed, 17 Feb 2021 03:08:24 -0800 (PST) Received: by mail-wr1-x44a.google.com with SMTP id d10so16821598wrq.17 for ; Wed, 17 Feb 2021 03:08:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=/jlrrw//5EBw0HxItehu19/zul9u1dKvK5phINfjwEk=; b=s13SxwqbF6VEmAgB0MOMVRv/Qo0IFrsZCbK/vRUoK1110OmI0lfLOl87udU+/ECgHi 2TZTX9Y1VzOMZJRXiCuo503jWsCvZvfeZ94iYkfNIwVW5giC5/V2Uos5NFneM+FjJfn2 yUfzuTvs1/wAyUQ8FIedv1v4Q4uJyLz+u5j2eHAnPIdsLJF436zqRHE/T0QAWNgZbypU G22K9d++X/SUaMT8ghhdxRBvtGjQuCerHzB3Sg/kNR0WNubRXUxpKSL1sEvAxpufW8Jl oPq234NoTsmcjUBzmcMmvxiT0EaF2odqXzGzh8pJ6fMy8IUL61MVZtgmK7UCVa+Sr2s+ 0elw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=/jlrrw//5EBw0HxItehu19/zul9u1dKvK5phINfjwEk=; b=t+Tlz2L94D1zq+wjk0aaU/FUoUX/P2RJJ6OVIK9tp4lbnC6wPwAve/+DeBuu6yu2b9 ePxqOR8NH05WCPQviOb0kYPseyzaXSZ+UsH6rV3IaOCyQ4M/JZLlxH6YC90/gm1flLZ0 vZOP2hx1XW/e3YdytEvk68Eb2JRtjt8ijU1jteE9U8Bm0e6NYzM27MM6qlfydKX6mgU4 VsgO9tbpCTaKAHV3MJeNI9AYFu56FBejktOMOieZxDwTZd0AAf5NU4hBNgzPnPT7JMgj SQkcIjLKOt2ENmpYgs14Fy34QM5Ie38JHQrg+vRpuNcf0Op/GJP7FG+1VVDdRENvnd17 QcKg== X-Gm-Message-State: AOAM530NSE2kqUgseMOMUp2tqJu9tpZoKkweZfFv24YGsU/Ag2kanAvc oDRDst3vNPktD8L75HCrC19S9TPnFNn3gg== X-Google-Smtp-Source: ABdhPJx1JLkbNWtWhwyTxH9S+q24GjVOq5lYO+EjG/IBH4jQH5Xb+2PfqDArpk3BaNPsDuZHEEXnCfAdmtcMYQ== Sender: "gprocida via sendgmr" X-Received: from tef.lon.corp.google.com ([2a00:79e0:d:110:61b3:1cb2:c180:c3f]) (user=gprocida job=sendgmr) by 2002:a7b:ce12:: with SMTP id m18mr6895587wmc.148.1613560103057; Wed, 17 Feb 2021 03:08:23 -0800 (PST) Date: Wed, 17 Feb 2021 11:08:03 +0000 In-Reply-To: <20210217110804.75923-1-gprocida@google.com> Message-Id: <20210217110804.75923-5-gprocida@google.com> Mime-Version: 1.0 References: <20210205134221.2953163-1-gprocida@google.com> <20210217110804.75923-1-gprocida@google.com> X-Mailer: git-send-email 2.30.0.478.g8a0d178c01-goog Subject: [PATCH dwarves v4 4/5] btf_encoder: Add .BTF section using libelf From: Giuliano Procida To: dwarves@vger.kernel.org, acme@kernel.org Cc: andrii@kernel.org, ast@kernel.org, gprocida@google.com, maennich@google.com, kernel-team@android.com, kernel-team@fb.com, bpf@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org pahole -J uses libelf directly when updating a .BTF section. However, it uses llvm-objcopy to add .BTF sections. This commit switches to using libelf for both cases. This eliminates pahole's dependency on llvm-objcopy. One unfortunate side-effect is that vmlinux actually increases in size. It seems that llvm-objcopy modifies the .strtab section, discarding many strings. I speculate that is it discarding strings not referenced from .symtab and updating the references therein. Layout is left completely up to libelf and existing section offsets are likely to change. Acked-by: Andrii Nakryiko Signed-off-by: Giuliano Procida --- libbtf.c | 127 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 81 insertions(+), 46 deletions(-) diff --git a/libbtf.c b/libbtf.c index 4ae7150..9ff03ca 100644 --- a/libbtf.c +++ b/libbtf.c @@ -698,6 +698,7 @@ int32_t btf_elf__add_datasec_type(struct btf_elf *btfe, const char *section_name static int btf_elf__write(const char *filename, struct btf *btf) { + const char BTF_SEC_NAME[] = ".BTF"; GElf_Ehdr ehdr; Elf_Data *btf_data = NULL; Elf *elf = NULL; @@ -705,6 +706,7 @@ static int btf_elf__write(const char *filename, struct btf *btf) uint32_t raw_btf_size; int fd, err = -1; size_t strndx; + void *str_table = NULL; fd = open(filename, O_RDWR); if (fd < 0) { @@ -743,73 +745,106 @@ static int btf_elf__write(const char *filename, struct btf *btf) } /* - * First we look if there was already a .BTF section to overwrite. + * First we check if there is already a .BTF section present. */ - elf_getshdrstrndx(elf, &strndx); + Elf_Scn *btf_scn = NULL; for (Elf_Scn *scn = elf_nextscn(elf, NULL); scn; scn = elf_nextscn(elf, scn)) { GElf_Shdr shdr; if (!gelf_getshdr(scn, &shdr)) continue; char *secname = elf_strptr(elf, strndx, shdr.sh_name); - if (strcmp(secname, ".BTF") == 0) { - btf_data = elf_getdata(scn, btf_data); + if (strcmp(secname, BTF_SEC_NAME) == 0) { + btf_scn = scn; break; } } - raw_btf_data = btf__get_raw_data(btf, &raw_btf_size); - - if (btf_data) { - /* Exisiting .BTF section found */ - btf_data->d_buf = (void *)raw_btf_data; - btf_data->d_size = raw_btf_size; - elf_flagdata(btf_data, ELF_C_SET, ELF_F_DIRTY); + Elf_Scn *str_scn = elf_getscn(elf, strndx); + if (!str_scn) { + elf_error("elf_getscn(strndx) failed"); + goto out; + } - if (elf_update(elf, ELF_C_NULL) >= 0 && - elf_update(elf, ELF_C_WRITE) >= 0) - err = 0; - else - elf_error("elf_update failed"); + size_t dot_btf_offset = 0; + if (btf_scn) { + /* Existing .BTF section found */ + btf_data = elf_getdata(btf_scn, NULL); + if (!btf_data) { + elf_error("elf_getdata failed"); + goto out; + } } else { - const char *llvm_objcopy; - char tmp_fn[PATH_MAX]; - char cmd[PATH_MAX * 2]; - - llvm_objcopy = getenv("LLVM_OBJCOPY"); - if (!llvm_objcopy) - llvm_objcopy = "llvm-objcopy"; - - /* Use objcopy to add a .BTF section */ - snprintf(tmp_fn, sizeof(tmp_fn), "%s.btf", filename); - close(fd); - fd = creat(tmp_fn, S_IRUSR | S_IWUSR); - if (fd == -1) { - fprintf(stderr, "%s: open(%s) failed!\n", __func__, - tmp_fn); + /* Add ".BTF" to the section name string table */ + Elf_Data *str_data = elf_getdata(str_scn, NULL); + if (!str_data) { + elf_error("elf_getdata(str_scn) failed"); goto out; } - - if (write(fd, raw_btf_data, raw_btf_size) != raw_btf_size) { - fprintf(stderr, "%s: write of %d bytes to '%s' failed: %d!\n", - __func__, raw_btf_size, tmp_fn, errno); - goto unlink; + dot_btf_offset = str_data->d_size; + size_t new_str_size = dot_btf_offset + sizeof(BTF_SEC_NAME); + str_table = malloc(new_str_size); + if (!str_table) { + fprintf(stderr, "%s: malloc (strtab) failed\n", __func__); + goto out; } - - snprintf(cmd, sizeof(cmd), "%s --add-section .BTF=%s %s", - llvm_objcopy, tmp_fn, filename); - if (system(cmd)) { - fprintf(stderr, "%s: failed to add .BTF section to '%s': %d!\n", - __func__, filename, errno); - goto unlink; + memcpy(str_table, str_data->d_buf, dot_btf_offset); + memcpy(str_table + dot_btf_offset, BTF_SEC_NAME, sizeof(BTF_SEC_NAME)); + str_data->d_buf = str_table; + str_data->d_size = new_str_size; + elf_flagdata(str_data, ELF_C_SET, ELF_F_DIRTY); + + /* Create a new section */ + btf_scn = elf_newscn(elf); + if (!btf_scn) { + elf_error("elf_newscn failed"); + goto out; + } + btf_data = elf_newdata(btf_scn); + if (!btf_data) { + elf_error("elf_newdata failed"); + goto out; } + } + + /* (Re)populate the BTF section data */ + raw_btf_data = btf__get_raw_data(btf, &raw_btf_size); + btf_data->d_buf = (void *)raw_btf_data; + btf_data->d_size = raw_btf_size; + btf_data->d_type = ELF_T_BYTE; + btf_data->d_version = EV_CURRENT; + elf_flagdata(btf_data, ELF_C_SET, ELF_F_DIRTY); + + /* Update .BTF section in the SHT */ + GElf_Shdr btf_shdr; + if (!gelf_getshdr(btf_scn, &btf_shdr)) { + elf_error("elf_getshdr(btf_scn) failed"); + goto out; + } + btf_shdr.sh_entsize = 0; + btf_shdr.sh_flags = 0; + if (dot_btf_offset) + btf_shdr.sh_name = dot_btf_offset; + btf_shdr.sh_type = SHT_PROGBITS; + if (!gelf_update_shdr(btf_scn, &btf_shdr)) { + elf_error("gelf_update_shdr failed"); + goto out; + } - err = 0; - unlink: - unlink(tmp_fn); + if (elf_update(elf, ELF_C_NULL) < 0) { + elf_error("elf_update (layout) failed"); + goto out; + } + + if (elf_update(elf, ELF_C_WRITE) < 0) { + elf_error("elf_update (write) failed"); + goto out; } + err = 0; out: + if (str_table) + free(str_table); if (fd != -1) close(fd); if (elf) From patchwork Wed Feb 17 11:08:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giuliano Procida X-Patchwork-Id: 12091299 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 X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 81ACDC433E0 for ; Wed, 17 Feb 2021 11:10:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5D89264DCF for ; Wed, 17 Feb 2021 11:10:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231578AbhBQLKK (ORCPT ); Wed, 17 Feb 2021 06:10:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231544AbhBQLJz (ORCPT ); Wed, 17 Feb 2021 06:09:55 -0500 Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F256C0617A7 for ; Wed, 17 Feb 2021 03:08:26 -0800 (PST) Received: by mail-qv1-xf49.google.com with SMTP id h10so9704211qvf.19 for ; Wed, 17 Feb 2021 03:08:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=ICJenGYVVB2UMBJ+iIKGIow1eUvegxgyz46lhETpOEo=; b=PEhEAThsEfNyW5iaB/O495UGeIjmkOQhOja4zRTQcigkQrN0VXhnDEqu5yCsVz8hJt C3RSWAwyrkRcLgJ250YHpCYOhR/d7SbD87sVPf+kQaADqe18hrMxJwgHC3vC3e1r/5hM i6N0SqLKpqT8Yx55U6WIOJhX3XeVVnpUHv4bAJyrcc7RgXMWl7TFjX7EWybymD1i0rE9 fjBEAdbQQpasahkwXL5h00bGF9Z8Z4C9h7a3YEYQMxNDoH1G2jx7Jc6+u1aQqbMwWw85 jHjpHOC+RgJSJPx5UhjMsOnAiVNqt8qFkWUBGOllF96E7qFkPTIe5Pr/lpNKxSCUoeon LYTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ICJenGYVVB2UMBJ+iIKGIow1eUvegxgyz46lhETpOEo=; b=GdrkUBuBbr+qKZiOQ+qNDqqTeG/9l/kCMtlji3e8FY2yE12VpwxT8g7j5xxm6Mrxdi D7f3y1ZSspSMGpg+57cLr8y58xDNI+/EkeJL1rqSGEZbUnIG7xlAkUlztg6YM92cbsiS C7pCvfwzqJIbbpka7x1cas4Khiysj3Jt6ImtZCFfjjwfUx5FYCUuJBdRdb/smo1UBwhd vj2Pasu4rC9FceTX9EdIEFXcWm5pgERCB/jy/TOg96VRME9xhfV7LqnJjuQE83HiWY4B 0YZL3EW4ynqN3RLj1ThxvV9yn1bDO3Dlc8T+/wytAnZGikv/usErbBE8fI6G25cg73nW aZzg== X-Gm-Message-State: AOAM531CksTcjFzzMSjPGYrozUimK0HIKAS9gKqqo2kpkQNg46tf2PCk 8VBkdEdeI2YGMjCnZ5kuRwpPvgcJHeDtig== X-Google-Smtp-Source: ABdhPJziMOSsIQGNdtJaRQPRS9whDjRhhEU+hGOvIG0l8uCjwtm9jd+lI0E/ouyizbEYfP8GW+SMxFAcBfbP4Q== Sender: "gprocida via sendgmr" X-Received: from tef.lon.corp.google.com ([2a00:79e0:d:110:61b3:1cb2:c180:c3f]) (user=gprocida job=sendgmr) by 2002:a05:6214:16cf:: with SMTP id d15mr23910944qvz.32.1613560105235; Wed, 17 Feb 2021 03:08:25 -0800 (PST) Date: Wed, 17 Feb 2021 11:08:04 +0000 In-Reply-To: <20210217110804.75923-1-gprocida@google.com> Message-Id: <20210217110804.75923-6-gprocida@google.com> Mime-Version: 1.0 References: <20210205134221.2953163-1-gprocida@google.com> <20210217110804.75923-1-gprocida@google.com> X-Mailer: git-send-email 2.30.0.478.g8a0d178c01-goog Subject: [PATCH dwarves v4 5/5] btf_encoder: Align .BTF section to 8 bytes From: Giuliano Procida To: dwarves@vger.kernel.org, acme@kernel.org Cc: andrii@kernel.org, ast@kernel.org, gprocida@google.com, maennich@google.com, kernel-team@android.com, kernel-team@fb.com, bpf@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org This is to avoid misaligned access to BTF type structs when memory-mapping ELF objects. Acked-by: Andrii Nakryiko Signed-off-by: Giuliano Procida --- libbtf.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libbtf.c b/libbtf.c index 9ff03ca..ee677fa 100644 --- a/libbtf.c +++ b/libbtf.c @@ -744,6 +744,14 @@ static int btf_elf__write(const char *filename, struct btf *btf) goto out; } + /* + * We'll align .BTF to 8 bytes to cater for all architectures. It'd be + * nice if we could fetch this value from somewhere. The BTF + * specification does not discuss alignment and its trailing string + * table is not currently padded to any particular alignment. + */ + const size_t btf_alignment = 8; + /* * First we check if there is already a .BTF section present. */ @@ -821,6 +829,7 @@ static int btf_elf__write(const char *filename, struct btf *btf) elf_error("elf_getshdr(btf_scn) failed"); goto out; } + btf_shdr.sh_addralign = btf_alignment; btf_shdr.sh_entsize = 0; btf_shdr.sh_flags = 0; if (dot_btf_offset)