BPF List
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: Lorenz Bauer <oss@lmb.io>, andrii@kernel.org, bpf@vger.kernel.org
Subject: Re: Signedness of char in BTF
Date: Thu, 21 Jul 2022 11:35:22 -0700	[thread overview]
Message-ID: <a73586ad-f2dc-0401-1eba-2004357b7edf@fb.com> (raw)
In-Reply-To: <3fcf2cb7-8d27-4649-b943-7c58e838664a@www.fastmail.com>



On 7/21/22 7:31 AM, Lorenz Bauer wrote:
> Hi Yonghong and Andrii,
> 
> I have some questions re: signedness of chars in BTF. According to [1] BTF_INT_ENCODING() may be one of SIGNED, CHAR or BOOL. If I read [2] correctly the signedness of char is implementation defined. Does this mean that I need to know which implementation generated the BTF to interpret CHAR correctly?
> 
> Somewhat related, how to I make clang emit BTF_INT_CHAR in the first place? I've tried with clang-14, but only ever get
> 
>      [6] INT 'unsigned char' size=1 bits_offset=0 nr_bits=8 encoding=(none)
>      [6] INT 'char' size=1 bits_offset=0 nr_bits=8 encoding=SIGNED
> 
> The kernel seems to agree that CHAR isn't a thing [3].

clang does not generate BTF_INT_CHAR.

BTFTypeInt::BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits,
                        uint32_t OffsetInBits, StringRef TypeName)
     : Name(TypeName) {
   // Translate IR int encoding to BTF int encoding.
   uint8_t BTFEncoding;
   switch (Encoding) {
   case dwarf::DW_ATE_boolean:
     BTFEncoding = BTF::INT_BOOL;
     break;
   case dwarf::DW_ATE_signed:
   case dwarf::DW_ATE_signed_char:
     BTFEncoding = BTF::INT_SIGNED;
     break;
   case dwarf::DW_ATE_unsigned:
   case dwarf::DW_ATE_unsigned_char:
     BTFEncoding = 0;  /* INT_UNSIGNED */
     break;
   default:
     llvm_unreachable("Unknown BTFTypeInt Encoding");
   }

pahole does not generate INT_CHAR type either.
in pahole:

static int32_t btf_encoder__add_base_type(struct btf_encoder *encoder, 
const struct base_type *bt, co
nst char *name)
{
         const struct btf_type *t;
         uint8_t encoding = 0;  /* unsigned */
         uint16_t byte_sz;
         int32_t id;

         if (bt->is_signed) {
                 encoding = BTF_INT_SIGNED;
         } else if (bt->is_bool) {
                 encoding = BTF_INT_BOOL;
         } else if (bt->float_type && encoder->gen_floats) {
                 /* for floats */
         }
         ...
}

So for both clang and pahole, CHAR goes to INT_SIGNED or INT_UNSIGNED.

The reason is originally BTF tries to mimic CTF but a
simplified version, and CTF has CTF_TYPE_INT_CHAR, but later on
found BTF_INT_CHAR is not that useful so llvm and pahole
doesn't generate it any more.

The libbpf and kernel still supports BTF_INT_CHAR and when it is used
to print out values it is interpreted as type 'char'.

> 
> Thanks!
> Lorenz
> 
> 1: https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-int
> 2: https://stackoverflow.com/a/2054941/19544965
> 3: https://sourcegraph.com/github.com/torvalds/linux@353f7988dd8413c47718f7ca79c030b6fb62cfe5/-/blob/kernel/bpf/btf.c?L2928-2934

      parent reply	other threads:[~2022-07-21 18:35 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-21 14:31 Signedness of char in BTF Lorenz Bauer
2022-07-21 14:54 ` Jose E. Marchesi
2022-07-21 18:44   ` Yonghong Song
2022-07-21 22:21     ` Jose E. Marchesi
2022-07-21 22:52       ` Yonghong Song
2022-07-22 11:25         ` Jose E. Marchesi
2022-07-22 15:59           ` Yonghong Song
2022-08-02 17:28           ` Jose E. Marchesi
2022-07-21 18:35 ` Yonghong Song [this message]

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=a73586ad-f2dc-0401-1eba-2004357b7edf@fb.com \
    --to=yhs@fb.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=oss@lmb.io \
    /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