mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Douglas RAILLARD <douglas.raillard@arm.com>
To: bpf@vger.kernel.org
Cc: beata.michalska@arm.com,
	Douglas Raillard <douglas.raillard@arm.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Tom Rix <trix@redhat.com>,
	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	[thread overview]
Message-ID: <20220524094447.332186-1-douglas.raillard@arm.com> (raw)

From: Douglas Raillard <douglas.raillard@arm.com>

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 <yhs@fb.com> 
Signed-off-by: Douglas Raillard <douglas.raillard@arm.com>
---
 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


             reply	other threads:[~2022-05-24  9:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-24  9:44 Douglas RAILLARD [this message]
2022-05-31 22:50 ` patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220524094447.332186-1-douglas.raillard@arm.com \
    --to=douglas.raillard@arm.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=beata.michalska@arm.com \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=trix@redhat.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®