Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp3114671ioo; Tue, 24 May 2022 13:28:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwkaJq45vu8rPeG3ioEKUcajmL4XslaARgz+dd56x5CN2YLA3YDK3ZA72vYN6hNE4dpxxzg X-Received: by 2002:a17:906:c14d:b0:6ff:338:3fe6 with SMTP id dp13-20020a170906c14d00b006ff03383fe6mr4825399ejc.258.1653424104921; Tue, 24 May 2022 13:28:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653424104; cv=none; d=google.com; s=arc-20160816; b=nJJA2+qhGlO3KL2ZVG4sz76dXqGR1TJQY5vc0BPKFqEN+v034EeuKemOb5yhM7b+dY ossTbRfxUkY9om1Vn1hGInNFnQApJEzqLIFjpoL3a6Wb1AsO4AnmaRUwT07IGlhypuNd YSyvLsPLlwHT/NPjCGJ2tP1eIuOJguySj/bLhraXptuzPMmgMOe3NiDGNmQso2jBufmk Sh6tB7lglCB4yi59IQgQm/UnFIxxPQG2C7fHytuJG0l27NqSvZ0hH7ZO6GBfjmI2uf0Z J+YmkNZHau7CeweRxQn/GNOvmWva3NcTUGFQVEx8J/YRWrGesHty+lYDSsSHMF8ldyJ+ lAuw== 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 :message-id:date:subject:cc:to:from; bh=1XraFZMKcmOsjArWO/2FmJRB7eASLW46cgauDTeqBkc=; b=t1ln8O1US85ZhX+gaMUksfCPzobTAKiAq4KIKhf0Rr8jpe3PXyTj3xCIEN5gFJnjsz W5L5xuvXpMeLfFNAEItN65eCd4f/U1b4Yi4lz0gYGdi1wOmpZaOX90IE9/2y8hTEN0yA ulpslhw/nlqAEBkqd2yJO2YkM9vdqc3cZIfWPAwF4uI6Dxichvp4mR/wkYYjJtsFFEaq nR3GxafktV7rMIVkECOKcrBKwda6ZRiOC4ThI78Rt7hNChTAae4WdzbAzE94ZplF1Qc4 5COevSntRyPMR6sLBwh7KMiTvxLa3lEM5m2x9ziWDE6FtQ9aMb4JcymB1vHgk8vnEQyW nWPw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hg4-20020a1709072cc400b006e89c06638bsi19651768ejc.492.2022.05.24.13.27.59; Tue, 24 May 2022 13:28:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235839AbiEXJoy (ORCPT + 99 others); Tue, 24 May 2022 05:44:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231863AbiEXJow (ORCPT ); Tue, 24 May 2022 05:44:52 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6239752E5F; Tue, 24 May 2022 02:44:51 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id EDF9D1FB; Tue, 24 May 2022 02:44:50 -0700 (PDT) Received: from e126130.arm.com (unknown [10.57.82.248]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 4C7313F73D; Tue, 24 May 2022 02:44:44 -0700 (PDT) From: Douglas RAILLARD To: bpf@vger.kernel.org Cc: beata.michalska@arm.com, Douglas Raillard , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Nathan Chancellor , Nick Desaulniers , Tom Rix , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v3] libbpf: Fix determine_ptr_size() guessing Date: Tue, 24 May 2022 10:44:47 +0100 Message-Id: <20220524094447.332186-1-douglas.raillard@arm.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Douglas Raillard One strategy employed by libbpf to guess the pointer size is by finding the size of "unsigned long" type. This is achieved by looking for a type of with the expected name and checking its size. Unfortunately, the C syntax is friendlier to humans than to computers as there is some variety in how such a type can be named. Specifically, gcc and clang do not use the same names for integer types in debug info: - clang uses "unsigned long" - gcc uses "long unsigned int" Lookup all the names for such a type so that libbpf can hope to find the information it wants. Acked-by: Yonghong Song Signed-off-by: Douglas Raillard --- tools/lib/bpf/btf.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) CHANGELOG v2: * Added missing case for "long" v3: * Refactor a bit to use a table * Provide the type names used by gcc and clang in commit msg diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 1383e26c5d1f..65c492a6807f 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -470,12 +470,25 @@ const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id) return btf_type_by_id((struct btf *)btf, type_id); } +static const char * const long_aliases[] = { + "long", + "long int", + "int long", + "unsigned long", + "long unsigned", + "unsigned long int", + "unsigned int long", + "long unsigned int", + "long int unsigned", + "int unsigned long", + "int long unsigned", +}; + static int determine_ptr_size(const struct btf *btf) { const struct btf_type *t; const char *name; - int i, n; - + int i, j, n; if (btf->base_btf && btf->base_btf->ptr_sz > 0) return btf->base_btf->ptr_sz; @@ -489,12 +502,12 @@ static int determine_ptr_size(const struct btf *btf) if (!name) continue; - if (strcmp(name, "long int") == 0 || - strcmp(name, "long unsigned int") == 0) { - if (t->size != 4 && t->size != 8) - continue; - return t->size; - } + if (t->size != 4 && t->size != 8) + continue; + + for (j = 0; j < ARRAY_SIZE(long_aliases); j++) + if (!strcmp(name, long_aliases[j])) + return t->size; } return -1; -- 2.25.1