Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp2623103rdb; Fri, 8 Dec 2023 13:52:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IFEp3JNt1Er+XG4s7hmFRW6BViVHEqlF53pRz4XkxOBL+mkYHiRD/LR19Row01rca3nDNZC X-Received: by 2002:a05:6a00:2d97:b0:6ce:7cba:3a6e with SMTP id fb23-20020a056a002d9700b006ce7cba3a6emr806323pfb.10.1702072330276; Fri, 08 Dec 2023 13:52:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702072330; cv=none; d=google.com; s=arc-20160816; b=Avx1dD3oZx5zY1FKJSdDpT3vjXH9TSuFlAxB/D6Jb7eIs4ziIcEPxZGKyjTSENS3Hq XHlVEeKOOfiWKDbD1JB4/5qr920MJ2UBSC8h5HR0v1o/QfI2X7u0BUKIUdRYDm6yQlB8 aAYp5DEaHLI4GadD31phaV4AsZwYxKlckMyqHVOaPHXeVwuRkS+aqQ5/H9vEUv3txZ76 GhmNL05IaGz2Ts4V2SoLLRKCUCesHZ70g/Em5uttcUsKcXtZiwDu3wLGurUviEqap0vo 6Uz65SfLp6AuBMsK2d9rqwaEMDvTSINxj5WCIZSRGpu6MUrUqLTNW7xoes6acJtX26Y6 2Wbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=aDn8NwTCQ+8/XvF68whD6iZE3c1B6URP/KepTuq9faE=; fh=TG4QY+uglcFIcCBRqVyuMaXJNwNvcxq29qhNG8lOAXY=; b=mC5xUyEtOXsGHfiOkOl44vSmDxC3/csq6sEedrtqPJtbMg6MlWulmSj+qWYtLhvBPF E/GzOlgh0xfFD62xTy+EEGJFjW1ymHENBaDoRbDXjyTpXQqayTIS0hsBk/1O21UCkCcV Xr11o+e2x6EKzGKEg4lrXw3wP5D7ZUVM88v1Pj9xxIIfoMx0vVryawVxcVFyrBD8BthE rTPxruQPWq4KUc5O2cOaqQ4z+4ykAhnWhSyyNO8Gv7qeF0AkF4l3Fw96s4gQsbKEEx2B IItDEmOesBox4yBi/S3lBh4GDO0O1ueOtrGt/z6MHa9rKHvzYMrMqVLwDv4MpDxef+8y AsSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=mEI3bgsc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id ka23-20020a056a00939700b006ce6a1cbc97si2157030pfb.68.2023.12.08.13.52.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 13:52:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=mEI3bgsc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id BB3248088564; Fri, 8 Dec 2023 13:51:38 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230004AbjLHVvW (ORCPT + 99 others); Fri, 8 Dec 2023 16:51:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229572AbjLHVvV (ORCPT ); Fri, 8 Dec 2023 16:51:21 -0500 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 3F5CCEA; Fri, 8 Dec 2023 13:51:27 -0800 (PST) Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-40c19467a63so28429865e9.3; Fri, 08 Dec 2023 13:51:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702072286; x=1702677086; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=aDn8NwTCQ+8/XvF68whD6iZE3c1B6URP/KepTuq9faE=; b=mEI3bgscaaakSSrpLb8xKYBWGo1GSzbBrw2fppCIbFiYwFrHF53frMSs9c6OR4Icsv HsjXYsNKyWQNRi5rikr5JcaeBm5+r3G6g0IFSnXzEoVNNUxGQxctwF/kWxEQ8HzUyCrQ 0Yh3cXaLAticszPodb4ZisCIS3FQxCEs/cWhkWkylsBd2aYETKo2Ke09q2dIQ1fQtQQh OGLrPCBlZGKQ/QBTXXmBZBiTqEvxYPXAo9Ef3k/ErGvun2hwRM3i2bb/SAXg4fHB94Ob jmsaGMMgw+Tjy9tHeYzk8UB/GWNT4YDZiTAvkDjTeYtnVptllmv+qQaOdEWv/qKyvY9k /+Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702072286; x=1702677086; 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:message-id:reply-to; bh=aDn8NwTCQ+8/XvF68whD6iZE3c1B6URP/KepTuq9faE=; b=FmGzmSmPdQuyLO0v0lRbPpb1VZPZMat1qyJiNcMXAI+7HxLy6a3r6mk2R8gO6mxfjG THAMXtc89gMtKU5pV77N0pSI2jVwPKnE769Eb0o57gWcuuU9NaEH3TvWMbcuRhxFeFrQ 4KcXGfCbhp17ei8tq6gMzJWd/VEsjUecOIpBjI5Zvpp2ki5eIJahkeJ52Wtf9idCHF02 5lUXnF04cS4LyM91v4eqp3PGrCWFpzAPM8fU61Gld9QujO7lfNiC5AaIvAFEgiYCdyaf JVREj9963pBnEJrVIQy+Z1Xum0W8MOkHZb+2ozCpEobKhCQDBQ+l4Nz0NdDruiseMsNF ragw== X-Gm-Message-State: AOJu0YwGboVXkv5ZR6W9kfBrOzy0TCMTXtBhZuKRCRBeJInHSWtxRsou jJcvOoE3ohqFURKG8DscIFs= X-Received: by 2002:a05:600c:2249:b0:40c:1ebd:11c4 with SMTP id a9-20020a05600c224900b0040c1ebd11c4mr328867wmm.15.1702072285316; Fri, 08 Dec 2023 13:51:25 -0800 (PST) Received: from nz.home ([2a00:23c8:a613:101:5d8:ec3b:9abe:2b3d]) by smtp.gmail.com with ESMTPSA id p14-20020a05600c358e00b0040c37c4c229sm1660581wmq.14.2023.12.08.13.51.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 13:51:24 -0800 (PST) Received: by nz.home (Postfix, from userid 1000) id 1E2A413AA9C8D3; Fri, 8 Dec 2023 21:51:24 +0000 (GMT) From: Sergei Trofimovich To: bpf@vger.kernel.org, Eduard Zingerman Cc: linux-kernel@vger.kernel.org, Sergei Trofimovich , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Subject: [PATCH v2] libbpf: add pr_warn() for EINVAL cases in linker_sanity_check_elf Date: Fri, 8 Dec 2023 21:51:00 +0000 Message-ID: <20231208215100.435876-1-slyich@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <159e94e7ce82e9432bd2bba0141c8feab0a9a2e6.camel@gmail.com> References: <159e94e7ce82e9432bd2bba0141c8feab0a9a2e6.camel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Fri, 08 Dec 2023 13:51:38 -0800 (PST) Before the change on `i686-linux` `systemd` build failed as: $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22) After the change it fails as: $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o libbpf: ELF section #9 has inconsistent alignment addr=8 != d=4 in src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22) Now it's slightly easier to figure out what is wrong with an ELF file. CC: Alexei Starovoitov CC: Daniel Borkmann CC: Andrii Nakryiko CC: Martin KaFai Lau CC: Song Liu CC: Yonghong Song CC: John Fastabend CC: KP Singh CC: Stanislav Fomichev CC: Hao Luo CC: Jiri Olsa CC: Eduard Zingerman CC: bpf@vger.kernel.org Signed-off-by: Sergei Trofimovich --- tools/lib/bpf/linker.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) Change since v1: Following Eduard's suggestion added one extra pr_warn() call around section alignment and added compared values into warning messages. diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c index 5ced96d99f8c..52a2901e8bd0 100644 --- a/tools/lib/bpf/linker.c +++ b/tools/lib/bpf/linker.c @@ -719,13 +719,25 @@ static int linker_sanity_check_elf(struct src_obj *obj) return -EINVAL; } - if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign)) + if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign)) { + pr_warn("ELF section #%zu alignment %llu is non pow-of-2 alignment in %s\n", + sec->sec_idx, (long long unsigned)sec->shdr->sh_addralign, + obj->filename); return -EINVAL; - if (sec->shdr->sh_addralign != sec->data->d_align) + } + if (sec->shdr->sh_addralign != sec->data->d_align) { + pr_warn("ELF section #%zu has inconsistent alignment addr=%llu != d=%llu in %s\n", + sec->sec_idx, (long long unsigned)sec->shdr->sh_addralign, + (long long unsigned)sec->data->d_align, obj->filename); return -EINVAL; + } - if (sec->shdr->sh_size != sec->data->d_size) + if (sec->shdr->sh_size != sec->data->d_size) { + pr_warn("ELF section #%zu has inconsistent section size sh=%llu != d=%llu in %s\n", + sec->sec_idx, (long long unsigned)sec->shdr->sh_size, + (long long unsigned)sec->data->d_size, obj->filename); return -EINVAL; + } switch (sec->shdr->sh_type) { case SHT_SYMTAB: @@ -737,8 +749,12 @@ static int linker_sanity_check_elf(struct src_obj *obj) break; case SHT_PROGBITS: if (sec->shdr->sh_flags & SHF_EXECINSTR) { - if (sec->shdr->sh_size % sizeof(struct bpf_insn) != 0) + if (sec->shdr->sh_size % sizeof(struct bpf_insn) != 0) { + pr_warn("ELF section #%zu has unexpected size alignment %llu in %s\n", + sec->sec_idx, (long long unsigned)sec->shdr->sh_size, + obj->filename); return -EINVAL; + } } break; case SHT_NOBITS: -- 2.42.0