Re: [PAHOLE v4 2/3] dwarf_loader: Add support for DW_TAG_GNU_annotation

Vineet Gupta <[email protected]> Wed, 17 Jun 2026 11:18:13 -0700
Newsgroups org.kernel.vger.dwarves,org.kernel.vger.bpf
Message-ID <[email protected]>
On 6/3/26 1:08 PM, Yonghong Song wrote:
> I download and build gcc-16.1 which will be used for gcc compilation.
> I did some experiments with this patch set for both type tag and decl tag.

Thx for this doing this exercise.

> For type tag
> ============

[snip]

> So type tag matches between clang and gcc.

Nice !

> For decl tag
> ============
>
> $ cat decl_tag.c
> /* btf_decl_tag test cases.
>    *
>    * btf_decl_tag can be attached to:
>    *   - global (incl. static) variables
>    *   - functions
>    *   - function parameters
>    *   - struct/union types and their members
>    *   - typedefs
>    *
>    * Build:  clang -O2 -target bpf -g -c decl_tag.c -o decl_tag.o
>    *         /home/yhs/work/gcc-build/opt/gcc-16.1/bin/gcc -O2 -gbtf -g -c decl_tag.c -o decl_tag.o
>    * Dump:   bpftool btf dump file decl_tag.o
>    */
>
> #define __tag(x) __attribute__((btf_decl_tag(x)))
>
> /* tag on a global variable */
> int global_var __tag("global_var_tag");
>
> /* tag on a static variable */
> static int static_var __tag("static_var_tag");
>
> /* multiple tags on one declaration */
> int multi_tag_var __tag("tag_a") __tag("tag_b");
>
> /* tag on struct type and its members */
> struct foo {
>           int a __tag("member_a_tag");
>           int b __tag("member_b_tag");
> } __tag("struct_foo_tag");
>
> /* tag on a typedef */
> typedef struct foo foo_t1 __tag("typedef_foo_tag");
> typedef struct {int foo2;} foo_t2 __tag("typedef_foo2_tag");
>
> /* tag on a function and its parameters */
> __tag("func_add_tag")
> int add(int x __tag("param_x_tag"), int y __tag("param_y_tag"))
> {
>           return x + y;
> }
>
> /* keep the globals/types alive so they land in BTF */
> int use(foo_t1 *f, foo_t2 *g)
> {
>           return add(global_var + static_var + multi_tag_var, f->a + g->foo2);
> }
>
> $ /home/yhs/work/gcc-build/opt/gcc-16.1/bin/gcc -O2 -gbtf -g -c decl_tag.c -o decl_tag.o
> decl_tag.c:30:1: warning: ‘btf_decl_tag’ attribute does not apply to types [-Wattributes]
>      30 | } __tag("struct_foo_tag");
>         | ^
>
[snip]

> Three decl tags (struct_foo_tag, typedef_foo_tag and typedef_foo2_tag)
> are missing here:
>
> struct foo {
>           int a __tag("member_a_tag");
>           int b __tag("member_b_tag");
> } __tag("struct_foo_tag");
>
> /* tag on a typedef */
> typedef struct foo foo_t1 __tag("typedef_foo_tag");
> typedef struct {int foo2;} foo_t2 __tag("typedef_foo2_tag");

Semantically what does this mean ? Will the decl tag will be "applied" 
where ever the type is instantiated ?

> /* tag on a static variable */
> static int static_var __tag("static_var_tag");
>
> ...
>
> [24] DECL_TAG 'tag_b' type_id=22 component_idx=-1
>
> DECL_TAG 'static_var_tag' type_id=0 component_idx=-1 is missing in llvm.
> This is execpted for llvm since 'static_var' is 'inlined' since it is
> value is 0 and the compiler optimization removed it in function use().
> In llvm Dwarf->BTF conversion is very late and we only emit survived
> globals.
>
> gcc emits 'static_var_tag' probably in frontend. Emitting
> 'static_var_tag' is okay, just not used.

Yeah I recall something like this when talking to David.
We can live with this it seems.

> I think gcc should support
>    - declaration tag for typedef, and
>    - declaration tag for the whole struct (like above 'struct_foo_tag')
> to be compatible with llvm.

OK, opened PR/125862 [1]  for future improvement.

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125862

Thx,
-Vineet