All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] libbpf: Fix determine_ptr_size() guessing
@ 2022-05-20 15:38 Douglas RAILLARD
  2022-05-20 19:19 ` Yonghong Song
  0 siblings, 1 reply; 3+ messages in thread
From: Douglas RAILLARD @ 2022-05-20 15:38 UTC (permalink / raw)
  To: bpf; +Cc: beata.michalska

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 name in debug info.

Lookup all the names for such a type so that libbpf can hope to find the
information it wants.

Signed-off-by: Douglas Raillard <douglas.raillard@arm.com>
---
 tools/lib/bpf/btf.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 1383e26c5d1f..ce05e4b1febd 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -489,8 +489,18 @@ 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 (
+			strcmp(name, "long int") == 0 ||
+			strcmp(name, "int long") == 0 ||
+			strcmp(name, "unsigned long") == 0 ||
+			strcmp(name, "long unsigned") == 0 ||
+			strcmp(name, "unsigned long int") == 0 ||
+			strcmp(name, "unsigned int long") == 0 ||
+			strcmp(name, "long unsigned int") == 0 ||
+			strcmp(name, "long int unsigned") == 0 ||
+			strcmp(name, "int unsigned long") == 0 ||
+			strcmp(name, "int long unsigned") == 0
+		) {
 			if (t->size != 4 && t->size != 8)
 				continue;
 			return t->size;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] libbpf: Fix determine_ptr_size() guessing
  2022-05-20 15:38 [PATCH] libbpf: Fix determine_ptr_size() guessing Douglas RAILLARD
@ 2022-05-20 19:19 ` Yonghong Song
  2022-05-23 10:22   ` Douglas Raillard
  0 siblings, 1 reply; 3+ messages in thread
From: Yonghong Song @ 2022-05-20 19:19 UTC (permalink / raw)
  To: Douglas RAILLARD, bpf; +Cc: beata.michalska



On 5/20/22 8:38 AM, Douglas RAILLARD wrote:
> 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 name in debug info.

Yes, this is indeed the case.

> 
> Lookup all the names for such a type so that libbpf can hope to find the
> information it wants.
> 
> Signed-off-by: Douglas Raillard <douglas.raillard@arm.com>

LGTM with some comments and needed change below.

Acked-by: Yonghong Song <yhs@fb.com>

> ---
>   tools/lib/bpf/btf.c | 14 ++++++++++++--
>   1 file changed, 12 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> index 1383e26c5d1f..ce05e4b1febd 100644
> --- a/tools/lib/bpf/btf.c
> +++ b/tools/lib/bpf/btf.c
> @@ -489,8 +489,18 @@ 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 (
> +			strcmp(name, "long int") == 0 ||
> +			strcmp(name, "int long") == 0 ||
> +			strcmp(name, "unsigned long") == 0 ||
> +			strcmp(name, "long unsigned") == 0 ||
> +			strcmp(name, "unsigned long int") == 0 ||
> +			strcmp(name, "unsigned int long") == 0 ||
> +			strcmp(name, "long unsigned int") == 0 ||
> +			strcmp(name, "long int unsigned") == 0 ||
> +			strcmp(name, "int unsigned long") == 0 ||
> +			strcmp(name, "int long unsigned") == 0

Please add "long" as well. For "long t" declaration, clang generates
the following dwarf:

0x00000029:   DW_TAG_base_type
                 DW_AT_name      ("long")
                 DW_AT_encoding  (DW_ATE_signed)
                 DW_AT_byte_size (0x08)

If the type name can be sorted with words, we only need
to compare the following 4 instead of 11
   "long"
   "int long"
   "long unsigned"
   "int long unsigned"

But I don't know whether we have an existing function
to do word sorting or not.


> +		) {
>   			if (t->size != 4 && t->size != 8)
>   				continue;
>   			return t->size;

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] libbpf: Fix determine_ptr_size() guessing
  2022-05-20 19:19 ` Yonghong Song
@ 2022-05-23 10:22   ` Douglas Raillard
  0 siblings, 0 replies; 3+ messages in thread
From: Douglas Raillard @ 2022-05-23 10:22 UTC (permalink / raw)
  To: Yonghong Song, bpf; +Cc: beata.michalska

Hi Yonghong,

On 5/20/22 20:19, Yonghong Song wrote:
>
>
> On 5/20/22 8:38 AM, Douglas RAILLARD wrote:
>> 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 name in debug info.
>
> Yes, this is indeed the case.
>
>>
>> Lookup all the names for such a type so that libbpf can hope to find the
>> information it wants.
>>
>> Signed-off-by: Douglas Raillard <douglas.raillard@arm.com>
>
> LGTM with some comments and needed change below.
>
> Acked-by: Yonghong Song <yhs@fb.com>
>
>> ---
>>   tools/lib/bpf/btf.c | 14 ++++++++++++--
>>   1 file changed, 12 insertions(+), 2 deletions(-)
>>
>> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
>> index 1383e26c5d1f..ce05e4b1febd 100644
>> --- a/tools/lib/bpf/btf.c
>> +++ b/tools/lib/bpf/btf.c
>> @@ -489,8 +489,18 @@ 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 (
>> +            strcmp(name, "long int") == 0 ||
>> +            strcmp(name, "int long") == 0 ||
>> +            strcmp(name, "unsigned long") == 0 ||
>> +            strcmp(name, "long unsigned") == 0 ||
>> +            strcmp(name, "unsigned long int") == 0 ||
>> +            strcmp(name, "unsigned int long") == 0 ||
>> +            strcmp(name, "long unsigned int") == 0 ||
>> +            strcmp(name, "long int unsigned") == 0 ||
>> +            strcmp(name, "int unsigned long") == 0 ||
>> +            strcmp(name, "int long unsigned") == 0
>
> Please add "long" as well. For "long t" declaration, clang generates
> the following dwarf:

Good catch, I missed this one thanks.

>
> 0x00000029:   DW_TAG_base_type
>                  DW_AT_name      ("long")
>                  DW_AT_encoding  (DW_ATE_signed)
>                  DW_AT_byte_size (0x08)
>
> If the type name can be sorted with words, we only need
> to compare the following 4 instead of 11
>    "long"
>    "int long"
>    "long unsigned"
>    "int long unsigned"
>
> But I don't know whether we have an existing function
> to do word sorting or not.

I could not find anything and was thinking it's probably more hassle than it's worth but if
we really want that I could add it somewhere.

>
>> +        ) {
>>               if (t->size != 4 && t->size != 8)
>>                   continue;
>>               return t->size;


Thanks,

Douglas
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-05-23 10:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-20 15:38 [PATCH] libbpf: Fix determine_ptr_size() guessing Douglas RAILLARD
2022-05-20 19:19 ` Yonghong Song
2022-05-23 10:22   ` Douglas Raillard

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.