From patchwork Fri Feb 5 13:42:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giuliano Procida X-Patchwork-Id: 12071207 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 162CEC4332D for ; Fri, 5 Feb 2021 22:44:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DEA3564FFE for ; Fri, 5 Feb 2021 22:44:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230436AbhBEWoB (ORCPT ); Fri, 5 Feb 2021 17:44:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231282AbhBENnU (ORCPT ); Fri, 5 Feb 2021 08:43:20 -0500 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93539C0617A7 for ; Fri, 5 Feb 2021 05:42:39 -0800 (PST) Received: by mail-qk1-x749.google.com with SMTP id c63so5847266qkd.1 for ; Fri, 05 Feb 2021 05:42:39 -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=ex0vuOuT0A88stBA/se/KcPkQGfalK3n6k9EL0PLw04=; b=jbN5L4FF0ns0Fu9f3MT0Q9pY9If7HN+erYJFO6t5R8hl2N9ODJ80KRQHotFVQ57R0y az7oD86L1IL0Wgo3BxhYoDqMVgGYq05pqfMBIeRu+LzzhvtI3Opdnc/6v7WDkK9GFb1e 9zCH9pPU8tsIg016I0vp+LLrt74A5np9YzRxB3b8GvMST99MsRP6hxnzc5eqtznVHXEG 0MQvU2zgnETJV4ht264THFpeM0Ah0CZpEvttR/0VgQyFgJJJQ+RqmGY+8FrSUg1W5g3f 9uChM7Jz22zHhSI5hU7ObY5VZgEmbCr0fn8SxhTu3MzUdKZnTGoCexGjPZ8A5FCQBosX 3Duw== 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=ex0vuOuT0A88stBA/se/KcPkQGfalK3n6k9EL0PLw04=; b=Tq/rjvQqEVbb8FdqVrUZ+/EhuhnWwUoG3aYjsoGGNUscnE+veAcvGutWUFD7/pMOjQ AItQtMRGxCCtEnA6Ukts4pTe7kHEKFzHjKjwAd5z/esIxzZbyQV7peHyuhf0iya07Pd5 hsab5+An++fcaRBH7ZDa96IoDENwe2HHZpFbAS5t3hErKqt1Sev9ZMXlxiwtUMhEa//7 /lYHppyadGP2rE+CQM72EEHpa3fW1oCvvh0CMWEqItCm7/BLyAQ9Snza/yvFf8TfiT11 DVCWoteQFWF7EjRJPwWrS+IucSNiE3wzAXPPGhdK2e+6Vwc6VJqucg3hdM33ubS4X1SJ /kHQ== X-Gm-Message-State: AOAM531Se5gY1PQQOZVfFSI3WBS9D64egfCo0E0oIfjK/C+sbInepu8m PKpXk9MJGgod8LH8Rle0F0cWFOczuCH64g== X-Google-Smtp-Source: ABdhPJyedtu2hOD+NDzfyRsR16vbD8etc3H9cWiVxAdaB43HGxmS5EukGG6WB+TDjaPii7ElTsri9d9dNuEsyg== Sender: "gprocida via sendgmr" X-Received: from tef.lon.corp.google.com ([2a00:79e0:d:110:656b:9716:1ea:3de6]) (user=gprocida job=sendgmr) by 2002:a05:6214:1144:: with SMTP id b4mr4269505qvt.12.1612532558809; Fri, 05 Feb 2021 05:42:38 -0800 (PST) Date: Fri, 5 Feb 2021 13:42:17 +0000 In-Reply-To: <20210205134221.2953163-1-gprocida@google.com> Message-Id: <20210205134221.2953163-2-gprocida@google.com> Mime-Version: 1.0 References: <20210201172530.1141087-1-gprocida@google.com> <20210205134221.2953163-1-gprocida@google.com> X-Mailer: git-send-email 2.30.0.478.g8a0d178c01-goog Subject: [PATCH dwarves v3 1/5] btf_encoder: Funnel ELF error reporting through a macro From: Giuliano Procida To: dwarves@vger.kernel.org Cc: acme@kernel.org, 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. Signed-off-by: Giuliano Procida Acked-by: Andrii Nakryiko --- 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 Fri Feb 5 13:42:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giuliano Procida X-Patchwork-Id: 12071211 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 868E5C433E0 for ; Fri, 5 Feb 2021 22:44:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5BCCD64FF5 for ; Fri, 5 Feb 2021 22:44:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232498AbhBEWoI (ORCPT ); Fri, 5 Feb 2021 17:44:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231352AbhBENnX (ORCPT ); Fri, 5 Feb 2021 08:43:23 -0500 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9827BC06121C for ; Fri, 5 Feb 2021 05:42:41 -0800 (PST) Received: by mail-qk1-x74a.google.com with SMTP id v130so5800020qkb.14 for ; Fri, 05 Feb 2021 05:42:41 -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=Jm9Rl/O23yAE3HZ62rmeV5YrYLNhsE5EvtDbKOWTIow=; b=ITUIwpQ8q9tGG4CIbJ0ci2ur/ZJSsEfgdPWAsLObON4aNaUKWMRH5VuROapZFGrYus 5fa4bI8iLDZEm6l3nYLFzlHxdrDFx1EUQaNjXPKHot4OYnaoGLNYA1h6JiSpo7jIlsRk 9kGukaEg//ID1Nn1O0irdtWgsIy+PqYgGTCvcp4DYU/jZ0z6ALvjbt6mBliklwtDJrP9 2bdtZhULisqdwWNw2HrnTW6zNKNpRYi/LKfkYQ2eH5/6iRYGGXS+nw4oYVROKHHzq8+u GBJ+Wo6dJDdLsur/AV9T2DNq+iNqolHUVKv3hNLa9huQZxg88b2FUgzoTsihgXyKgR04 HXrw== 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=Jm9Rl/O23yAE3HZ62rmeV5YrYLNhsE5EvtDbKOWTIow=; b=P7vpKllDSqca/i3nOjYEHh0jTDjCyMkT1bkb9OcLijpE4nUbEDgv1UUdEol26+Ehra Wlr0ZO8lbxOewSwprUYtHO9fBvXUVybxHATUycNxdUHxs4e5gx6RQN3xtfZQ1jxhHikS 8c04al6v7gZDwCQzTtAx7a9TtDdrQybGiK8y/tPvfX8TbWDisx2phR644SSzSz0PpzGS lDhn4SBeTb4LCxRCcxhffhjrH3/FbckfcC5CFru9OdG9XZuqWVJ74937B2EBy67ZAwkT hA1K7Vy/lFVIWRpaCS1OXKgN1yTXBlvTcmEMb+F1lN0QHb+pvJvd2szMwgxTii3Dw7SL o7Ng== X-Gm-Message-State: AOAM531MQ03pqDg5S5nZgFqNXR3Edo4pTzlEGh2uxiRRzPX9n8R7JO+C E8dBSzHRyB7/P+CCtvmhOgrEUxj9gpANHQ== X-Google-Smtp-Source: ABdhPJzIz42Mwp/Z6N9/StY2UCNjQr9VPyc9b4cxNS9Y9mkf9ti9vhjfcwqC/1Op+ISWE/P5kp6cce7rWt8WIw== Sender: "gprocida via sendgmr" X-Received: from tef.lon.corp.google.com ([2a00:79e0:d:110:656b:9716:1ea:3de6]) (user=gprocida job=sendgmr) by 2002:a05:6214:906:: with SMTP id dj6mr4375856qvb.28.1612532560829; Fri, 05 Feb 2021 05:42:40 -0800 (PST) Date: Fri, 5 Feb 2021 13:42:18 +0000 In-Reply-To: <20210205134221.2953163-1-gprocida@google.com> Message-Id: <20210205134221.2953163-3-gprocida@google.com> Mime-Version: 1.0 References: <20210201172530.1141087-1-gprocida@google.com> <20210205134221.2953163-1-gprocida@google.com> X-Mailer: git-send-email 2.30.0.478.g8a0d178c01-goog Subject: [PATCH dwarves v3 2/5] btf_encoder: Do not use both structs and pointers for the same data From: Giuliano Procida To: dwarves@vger.kernel.org Cc: acme@kernel.org, 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 unncessarily and the code is cleaner with only a single access path for these data. The code now treats the returned pointers as booleans. Signed-off-by: Giuliano Procida Acked-by: Andrii Nakryiko --- 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 Fri Feb 5 13:42:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giuliano Procida X-Patchwork-Id: 12071205 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 9ABDBC433E9 for ; Fri, 5 Feb 2021 22:44:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 62E3365007 for ; Fri, 5 Feb 2021 22:44:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232462AbhBEWn0 (ORCPT ); Fri, 5 Feb 2021 17:43:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231335AbhBENnL (ORCPT ); Fri, 5 Feb 2021 08:43:11 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD876C06121E for ; Fri, 5 Feb 2021 05:42:43 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 134so7094145ybd.3 for ; Fri, 05 Feb 2021 05:42:43 -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=kw2tnKgzpXRLq/FqFTSuKJnVTUwBFoe6uff7J2jbvTcndve02E6mDJ8rCruNugyAts rlS8Kvjxzr6gfSq1DDmlFxih48bdx0gJhhwGihjxmfB1Q5YxUPbFzCFxjFX2rCuhzUaw WNGY8moTkd6Nhlq313HeCqdYi/orDKDKiQgJo558nRTYgkCjdOOF/TRrJ7rp519idd9e yyS855aB5dsK2wBQl8jaqrqJvGjIsXVFrHvKdDbbw3BUW9pdT4LQ2b6iLx2m9lIA19Vq Gylnftm4KHwIJsQHEWQtTyI4lq4eUmQN21aLMT7Cg3D+O4KZCxipK2Z2wktGENEIZusN ieag== 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=s3pbQ4PywhIAaZalWEktMgKzcJvxpvcySTE9YZq+D9dJFwWvWh3T2JcqvIJ6gah5/F ji1QPBIZORswvb/EiDxkrq2VpPtOaf5zjY0jQFXA3J+Tcb98fMX0MwRnIq9aCah0atH7 /GNFEolVhzdw0p9YIpGIXQFu9HSSBdslMfjIOsbbVCRcZy2NHzGFmzLFo0uCCMgqyoaZ djR/Ch1j8MyljdUxcp13C1ZBvxp4SfvDxhyhKwQzmoarzysgmRhHCDkkt3tt5PTxwN6R PccPpR1D79+PSoLg5LEWaGTBmEsr3PcIq5bBZEBj0HfpYXxH22ngFZcq6BsrjwcJsvGt 4sOA== X-Gm-Message-State: AOAM533TMhCzgxolFuSDaq+3qvBZ/N4q+SpLw4E4DglFcYIFXe7BrKks +z8pG2aJl3Uu4nOVK5RXC1yU2LxwN/2CHA== X-Google-Smtp-Source: ABdhPJyI1NCzzeWtf0SgohHDK9KQ1fbnKF8fZb+IoqC/+CQBb5mfhvDAQak9cwHQ8DFF/jncptcNKBvQNRZEkA== Sender: "gprocida via sendgmr" X-Received: from tef.lon.corp.google.com ([2a00:79e0:d:110:656b:9716:1ea:3de6]) (user=gprocida job=sendgmr) by 2002:a25:3610:: with SMTP id d16mr6573081yba.454.1612532563011; Fri, 05 Feb 2021 05:42:43 -0800 (PST) Date: Fri, 5 Feb 2021 13:42:19 +0000 In-Reply-To: <20210205134221.2953163-1-gprocida@google.com> Message-Id: <20210205134221.2953163-4-gprocida@google.com> Mime-Version: 1.0 References: <20210201172530.1141087-1-gprocida@google.com> <20210205134221.2953163-1-gprocida@google.com> X-Mailer: git-send-email 2.30.0.478.g8a0d178c01-goog Subject: [PATCH dwarves v3 3/5] btf_encoder: Traverse sections using a for-loop From: Giuliano Procida To: dwarves@vger.kernel.org Cc: acme@kernel.org, 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 Fri Feb 5 13:42:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giuliano Procida X-Patchwork-Id: 12071203 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 772BEC433E6 for ; Fri, 5 Feb 2021 22:44:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4572264FF5 for ; Fri, 5 Feb 2021 22:44:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232506AbhBEWng (ORCPT ); Fri, 5 Feb 2021 17:43:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231336AbhBENnM (ORCPT ); Fri, 5 Feb 2021 08:43:12 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 376DFC061221 for ; Fri, 5 Feb 2021 05:42:48 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id x20so3837614wmc.0 for ; Fri, 05 Feb 2021 05:42:48 -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=bhWfXx9Qgpn0E0Ol053PNCp6i4yy+5CeY3XooaJfTt8=; b=O0/3dIv+NX7O4GuQZ8vFvL45ODVXKFXA38Ur0roS5Cy2+wODqwrmN3BeoNnIVtitfn tOwVGgRrMlNQtWhEmXOoM45N8UD7HIpfnl4XMDJEuuFutlWGsXbcyC/e3ANBAK8tFk1B eLoGBq7KnVQwWlcqIz099cOYWDzsIW6LLMBCc5hT465poxUojcDl7YUSdiykSN3LZ2AF Twj72dUHH5bgLHwAkcSfrEQdB/C1C3oj8yyNWETdJlLnHQrBR+epa/t0bZCkGDnUW1hS D0pXwIMvv3W4KSiHofeX7wIXRfNUxPF8t7afYmGPhhI59aTTQI3PUxskX4Cf0orZWJVW ejtg== 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=bhWfXx9Qgpn0E0Ol053PNCp6i4yy+5CeY3XooaJfTt8=; b=s0BQgDb0s0sFH1sBoppiVIMkMqtqYyR6chpCbAw8jC78DeSkWNj5XfmR8zaeXRuNc3 D7DNpaLW2ERtIjPReSZ4P3R5s1GjCIHZu5yln8IYwvztGOF9FkY3YMCz/6wsRxokbW7L 4GhNubgS+s6GQNXtISz2Nz2M83izzJQbRqaFRVKVsYul/DaDTleP8xEfXEDKos333pE0 9sUuoO68BSe73YMjXB6dfIm/Y9ymlstDI4eYhvbB5N0l7IJm+7JUJYZ5AoukTIv/dKd7 jJITzGq+IIsGt3+mDMszLQjU/kl2K3zpgD4FPLmHy/mJHuBAvnBRaSmFzX4roTeLcTjR r0JQ== X-Gm-Message-State: AOAM532PR1G2KvHgKlksa0fCbw5F/bRCj+0PY7DF9gNPylxn9he+EX8B QF7Q5vfn18nOu8WVNVdUsvOd7honp/kE3w== X-Google-Smtp-Source: ABdhPJx7SUfQq5sqjd7T84qgQzY7enqhVF5EUx7LVx002Wfk2U31hjpfIKjEHtRD1tkeQ5+QM2tqV92Da6ZW6Q== Sender: "gprocida via sendgmr" X-Received: from tef.lon.corp.google.com ([2a00:79e0:d:110:656b:9716:1ea:3de6]) (user=gprocida job=sendgmr) by 2002:a05:600c:35c9:: with SMTP id r9mr447046wmq.0.1612532565173; Fri, 05 Feb 2021 05:42:45 -0800 (PST) Date: Fri, 5 Feb 2021 13:42:20 +0000 In-Reply-To: <20210205134221.2953163-1-gprocida@google.com> Message-Id: <20210205134221.2953163-5-gprocida@google.com> Mime-Version: 1.0 References: <20210201172530.1141087-1-gprocida@google.com> <20210205134221.2953163-1-gprocida@google.com> X-Mailer: git-send-email 2.30.0.478.g8a0d178c01-goog Subject: [PATCH dwarves v3 4/5] btf_encoder: Add .BTF section using libelf From: Giuliano Procida To: dwarves@vger.kernel.org Cc: acme@kernel.org, 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. Signed-off-by: Giuliano Procida Acked-by: Andrii Nakryiko --- libbtf.c | 127 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 81 insertions(+), 46 deletions(-) diff --git a/libbtf.c b/libbtf.c index 4ae7150..9f4abb3 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 dot_BTF[] = ".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, dot_BTF) == 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(dot_BTF); + 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, dot_BTF, sizeof(dot_BTF)); + 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 Fri Feb 5 13:42:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giuliano Procida X-Patchwork-Id: 12071213 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 76BDDC433E0 for ; Fri, 5 Feb 2021 22:44:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 428976500E for ; Fri, 5 Feb 2021 22:44:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231189AbhBEWoM (ORCPT ); Fri, 5 Feb 2021 17:44:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231373AbhBENnf (ORCPT ); Fri, 5 Feb 2021 08:43:35 -0500 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3B1AC061223 for ; Fri, 5 Feb 2021 05:42:48 -0800 (PST) Received: by mail-qk1-x74a.google.com with SMTP id s66so5798913qkh.10 for ; Fri, 05 Feb 2021 05:42:48 -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=tAOVQ3G2eN9dGCWyxwwne5gBaM7loqyQjU9wOYpr/6I=; b=OH+gSvlZu8rebHPUVoqKDmTFHlv+/7sQvcchXxOZT1Jn21iONeHO5qh0iOgXDZWX7q jeC17g6FkVEOtLw2JujRlsXjCBYN14uQ/OGcRwPWafs4nX0OA8kWZjC42AjACQRY3E+O 6K/exEZWEsXQ7RObfSoLk6W2LbPGLqRruEHRwzRRiJvTCBgd82+1G8ivxl2sOdOftuJg m2WBkw65oW3/j1LRV1+a1nZkqeU/kLZ7Q1IpGzztRAi0qeh1hjeMjoO8ZWfgx29MYJIa TUwZpYnUIxODrp3LiKHFcqxHFa/CINvN3it27Eev4QLEzWMsmWlQfsjBnm/+Twr7/tdz Qe0w== 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=tAOVQ3G2eN9dGCWyxwwne5gBaM7loqyQjU9wOYpr/6I=; b=XVFd1WA4SQhlKNzp7ur6qoVmQKUCLBgiC1b3uEUmk8LKKy1TSZT7RMI1jMXhxxz6c4 mYH2nUf3nwiHoyJw1t/8YatzKFysjzMewe1p3ctQXMnKFiRANOknvRGBB0cDCNZC7JRU MWbklzTH5QtJj+sylyHnDPmVwyGa1hueqIALFT2yravF3hC+lZBt6P5Hj6Na9VGlgx2T t1SbW7r94p3PMRnwZbo92MCb2XXb/k6yUwhvrZSX4gBseIEvtPn2a8LXBcR3dT3wrydX GeNZA1dIpMU9YNXFfonCaAPBwQTwhWIGk1PWqjCB9e/8Ck/hPV3rhbcdjcwAfZv3qJ2c s5xA== X-Gm-Message-State: AOAM533IRUaG2Xvi5vitdENyv6/5Llv38ya0BLAbLsT+PdewEYgNSQb3 jUIxCdX6zpIO5urQguj4vtQ8kWWGRu/crg== X-Google-Smtp-Source: ABdhPJz5wihZxlw8hgR5CE0fmzuZmzIXYPXLcOoH3i+RKhajR348e6cGEOGc6mhYA8VGyVScObuVOOKwC0tm4w== Sender: "gprocida via sendgmr" X-Received: from tef.lon.corp.google.com ([2a00:79e0:d:110:656b:9716:1ea:3de6]) (user=gprocida job=sendgmr) by 2002:a05:6214:a54:: with SMTP id ee20mr4390536qvb.16.1612532567845; Fri, 05 Feb 2021 05:42:47 -0800 (PST) Date: Fri, 5 Feb 2021 13:42:21 +0000 In-Reply-To: <20210205134221.2953163-1-gprocida@google.com> Message-Id: <20210205134221.2953163-6-gprocida@google.com> Mime-Version: 1.0 References: <20210201172530.1141087-1-gprocida@google.com> <20210205134221.2953163-1-gprocida@google.com> X-Mailer: git-send-email 2.30.0.478.g8a0d178c01-goog Subject: [PATCH dwarves v3 5/5] btf_encoder: Align .BTF section to 8 bytes From: Giuliano Procida To: dwarves@vger.kernel.org Cc: acme@kernel.org, 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. Signed-off-by: Giuliano Procida Acked-by: Andrii Nakryiko --- libbtf.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libbtf.c b/libbtf.c index 9f4abb3..6754a17 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)