btf: recognize BTF_KIND_FUNC in btf_loader

BTF_KIND_FUNC is generated by llvm (latest trunk, 8.0 or later).
Without BTF_KIND_FUNC support, we will see the following errors,

  -bash-4.4$ cat t.c
  struct t {
    int a;
    char b1:1;
    char b2:3;
    int c;
  } g;
  int main() { return 0; }
  -bash-4.4$ clang -O2 -target bpf -g -c t.c -Xclang -target-feature -Xclang +dwarfris

  -bash-4.4$ pahole -F btf t.o
  BTF: idx: 3, off: 28, Unknown
  struct t {
        int                        a;                    /*     0     4 */

        /* Bitfield combined with previous fields */

        <ERROR(__class__fprintf:1342): 5 not found!>

        /* Bitfield combined with previous fields */

        <ERROR(__class__fprintf:1342): 5 not found!>
        int                        c;                    /*     8     4 */

        /* size: 12, cachelines: 1, members: 4 */
        /* last cacheline: 12 bytes */

        /* BRAIN FART ALERT! 12 != 8 + 0(holes), diff = 4 */

  };
  -bash-4.4$

The reason is that llvm generates BTF_KIND_FUNC which btf_loader does not
recognize.

This patch added support for BTF_KIND_FUNC. Since BTF_KIND_FUNC represents
a defined subprogram and not a real type. A null type is used to
represent BTF_KIND_FUNC to avoid skipping type index.

With this fix:

  -bash-4.4$ pahole -F btf t.o
  struct t {
        int                        a;                    /*     0     4 */
        char                       b1:1;                 /*     4: 0  1 */
        char                       b2:3;                 /*     4: 1  1 */
        int                        c;                    /*     8     4 */

        /* size: 12, cachelines: 1, members: 4 */
        /* last cacheline: 12 bytes */

        /* BRAIN FART ALERT! 12 != 9 + 0(holes), diff = 3 */

  };

Signed-off-by: Yonghong Song <yhs@fb.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@fb.com>
Cc: Martin KaFai Lau <kafai@fb.com>
Cc: dwarves@vger.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Yonghong Song 2018-12-22 23:16:49 -08:00 committed by Arnaldo Carvalho de Melo
parent 1176661409
commit f2092f5658
1 changed files with 10 additions and 0 deletions

View File

@ -419,6 +419,16 @@ static int btf__load_types(struct btf *btf)
vlen = 0;
} else if (type == BTF_KIND_FUNC_PROTO) {
vlen = create_new_subroutine_type(btf, ptr, vlen, type_ptr, type_index);
} else if (type == BTF_KIND_FUNC) {
/* BTF_KIND_FUNC corresponding to a defined subprogram.
* This is not really a type and it won't be referred by any other types
* either. Since types cannot be skipped, let us replace it with
* a nullify_type_entry.
*
* No warning here since BTF_KIND_FUNC is a legal entry in BTF.
*/
cu__table_nullify_type_entry(btf->priv, type_index);
vlen = 0;
} else {
fprintf(stderr,
"BTF: idx: %d, off: %zd, Unknown\n",