public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug debug/112849] New: btf: wrong BTF_KIND_DATSEC entries for extern variables without known section
@ 2023-12-04 21:12 david.faust at oracle dot com
  2023-12-04 21:14 ` [Bug debug/112849] " david.faust at oracle dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: david.faust at oracle dot com @ 2023-12-04 21:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112849

            Bug ID: 112849
           Summary: btf: wrong BTF_KIND_DATSEC entries for extern
                    variables without known section
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: btf-debug, wrong-debug
          Severity: normal
          Priority: P3
         Component: debug
          Assignee: unassigned at gcc dot gnu.org
          Reporter: david.faust at oracle dot com
                CC: cupertino.miranda at oracle dot com, ibhagat at gcc dot gnu.org,
                    jemarch at gcc dot gnu.org
  Target Milestone: ---

When building bpf-next selftests with gcc trunk, we get the following
error from libbpf:

libbpf: linker: adding object file 'linked_vars1.bpf.o'...
libbpf: linker: adding object file 'linked_vars2.bpf.o'...
libbpf: global 'input_data1': section mismatch 4 vs 5
Error: failed to link 'linked_vars2.bpf.o': Invalid argument (22)

Here libbpf is doing some form of linking of the two objects.
'input_data1' is declared and initialized in linked_vars1, and
placed in .data. In linked_vars2 it is declared extern, however
an entry for it is added in the .bss BTF_KIND_DATASEC record in
linked_vars2.bpf.o.

clang does not emit entries in BTF_KIND_DATASEC for extern variable
decls without an explicit section e.g. via __attribute__((section(...))).

gcc
[7237] DATASEC '.bss' size=0 vlen=8
        type_id=7059 offset=0 size=4 (VAR 'input_data1')      <-----
        type_id=7167 offset=0 size=4 (VAR 'input_bss1')       <-----
        type_id=7100 offset=0 size=4 (VAR 'output_sink2')
        type_id=7111 offset=0 size=4 (VAR 'output_rodata2')
        type_id=7065 offset=0 size=4 (VAR 'output_data2')
        type_id=7090 offset=0 size=4 (VAR 'output_bss2')
        type_id=7079 offset=0 size=4 (VAR 'input_bss_weak')
        type_id=7192 offset=0 size=4 (VAR 'input_bss2')

clang
[34] DATASEC '.bss' size=0 vlen=6
        type_id=18 offset=0 size=4 (VAR 'input_bss2')
        type_id=19 offset=0 size=4 (VAR 'input_bss_weak')
        type_id=20 offset=0 size=4 (VAR 'output_bss2')
        type_id=21 offset=0 size=4 (VAR 'output_data2')
        type_id=22 offset=0 size=4 (VAR 'output_rodata2')
        type_id=23 offset=0 size=4 (VAR 'output_sink2')


The problem is that when creating the BTF_KIND_DATASEC entires,
gcc is incorrectly falling back to a default section name (like .bss)
for variables, even if they are 'extern' decls. 

gcc should be changed to not emit entries in any BTF_KIND_DATASEC
for extern variable decls without a known section.

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

* [Bug debug/112849] btf: wrong BTF_KIND_DATSEC entries for extern variables without known section
  2023-12-04 21:12 [Bug debug/112849] New: btf: wrong BTF_KIND_DATSEC entries for extern variables without known section david.faust at oracle dot com
@ 2023-12-04 21:14 ` david.faust at oracle dot com
  2023-12-05 22:07 ` ibhagat at gcc dot gnu.org
  2023-12-05 22:08 ` cvs-commit at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: david.faust at oracle dot com @ 2023-12-04 21:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112849

--- Comment #1 from David Faust <david.faust at oracle dot com> ---
Simple reproducer. Note how clang does not emit an entry
in BTF_KIND_DATASEC for 'extern int a'.

$ cat a.c
extern int a;
int b;

int foo (void)
{
  return a + b;
}

$ ~/toolchains/bpf/bin/bpf-unknown-none-gcc -gbtf -c a.c -o a.o
$ /usr/sbin/bpftool btf dump file a.o
[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
[2] FUNC_PROTO '(anon)' ret_type_id=1 vlen=0
[3] VAR 'a' type_id=1, linkage=extern
[4] VAR 'b' type_id=1, linkage=global
[5] FUNC 'foo' type_id=2 linkage=global
[6] DATASEC '.bss' size=0 vlen=2
        type_id=3 offset=0 size=4 (VAR 'a')
        type_id=4 offset=0 size=4 (VAR 'b')

$ ~/toolchains/llvm/bin/clang -target bpf -c -g a.c -o a.o
$ /usr/sbin/bpftool btf dump file a.o
[1] FUNC_PROTO '(anon)' ret_type_id=2 vlen=0
[2] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
[3] FUNC 'foo' type_id=1 linkage=global
[4] VAR 'a' type_id=2, linkage=extern
[5] VAR 'b' type_id=2, linkage=global
[6] DATASEC '.bss' size=0 vlen=1
        type_id=5 offset=0 size=4 (VAR 'b')

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

* [Bug debug/112849] btf: wrong BTF_KIND_DATSEC entries for extern variables without known section
  2023-12-04 21:12 [Bug debug/112849] New: btf: wrong BTF_KIND_DATSEC entries for extern variables without known section david.faust at oracle dot com
  2023-12-04 21:14 ` [Bug debug/112849] " david.faust at oracle dot com
@ 2023-12-05 22:07 ` ibhagat at gcc dot gnu.org
  2023-12-05 22:08 ` cvs-commit at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: ibhagat at gcc dot gnu.org @ 2023-12-05 22:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112849

--- Comment #2 from Indu Bhagat <ibhagat at gcc dot gnu.org> ---
I think its also a good idea to propose a patch to btf.rst upstream to clearly
state something to the like of:

  - What variables have an associated BTF_KIND_DATASEC entry 
  - In "2.2.15 BTF_KIND_DATASEC"
    "struct btf_type" encoding requirement:
    * "name_off": offset to a valid name associated with a variable or
                  one of .data/.bss/.rodata
    --> change to -->
    * "name_off": offset to a valid section name associated with a variable 
                  or one of .data/.bss/.rodata

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

* [Bug debug/112849] btf: wrong BTF_KIND_DATSEC entries for extern variables without known section
  2023-12-04 21:12 [Bug debug/112849] New: btf: wrong BTF_KIND_DATSEC entries for extern variables without known section david.faust at oracle dot com
  2023-12-04 21:14 ` [Bug debug/112849] " david.faust at oracle dot com
  2023-12-05 22:07 ` ibhagat at gcc dot gnu.org
@ 2023-12-05 22:08 ` cvs-commit at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-12-05 22:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112849

--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by David Faust <dfaust@gcc.gnu.org>:

https://gcc.gnu.org/g:b8cf266f4ca4ff89704b190c827abf9ea7e7b5bf

commit r14-6195-gb8cf266f4ca4ff89704b190c827abf9ea7e7b5bf
Author: David Faust <david.faust@oracle.com>
Date:   Mon Dec 4 14:08:03 2023 -0800

    btf: avoid wrong DATASEC entries for extern vars [PR112849]

    The process of creating BTF_KIND_DATASEC records involves iterating
    through variable declarations, determining which section they will be
    placed in, and creating an entry in the appropriate DATASEC record
    accordingly.

    For variables without e.g. an explicit __attribute__((section)), we use
    categorize_decl_for_section () to identify the appropriate named section
    and corresponding BTF_KIND_DATASEC record.

    This was incorrectly being done for 'extern' variable declarations as
    well as non-extern ones, which meant that extern variable declarations
    could result in BTF_KIND_DATASEC entries claiming the variable is
    allocated in some section such as '.bss' without any knowledge whether
    that is actually true. That resulted in errors building the Linux kernel
    BPF selftests.

    This patch corrects btf_collect_datasec () to avoid assuming a section
    for extern variables, and only emit BTF_KIND_DATASEC entries for them if
    they have a known section.

    gcc/
            PR debug/112849
            * btfout.cc (btf_collect_datasec): Avoid incorrectly creating an
            entry in a BTF_KIND_DATASEC record for extern variable decls
without
            a known section.

    gcc/testsuite/
            PR debug/112849
            * gcc.dg/debug/btf/btf-datasec-3.c: New test.

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

end of thread, other threads:[~2023-12-05 22:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-04 21:12 [Bug debug/112849] New: btf: wrong BTF_KIND_DATSEC entries for extern variables without known section david.faust at oracle dot com
2023-12-04 21:14 ` [Bug debug/112849] " david.faust at oracle dot com
2023-12-05 22:07 ` ibhagat at gcc dot gnu.org
2023-12-05 22:08 ` cvs-commit at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).