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

            Bug ID: 105089
           Summary: CTF for a defined extern variable is ambiguous
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: debug
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ibhagat at gcc dot gnu.org
  Target Milestone: ---

$ cat extern.c
extern const char a[];
const char a[] = "testme";

$ gcc -gctf -dA -S extern.c

excerpt from extern.s:

        .long   0x5     # objtinfo_var_type
        .long   0x8     # objtinfo_var_type
        .long   0xb     # objtinfo_name
        .long   0x1f    # objtinfo_name
        .long   0xb     # ctv_name
        .long   0x5     # ctv_typeidx
        .long   0x1f    # ctv_name
        .long   0x8     # ctv_typeidx

There are TWO CTF variable records (two ctv_name, each with a ctv_typeidx). 
Further, two CTF objects records as well.

$ gcc -gctf -c extern.c
$ objdump --ctf=.ctf extern.o
shows:

  Variables:
    a ->  8: const const char [7] (size 0x7) -> 7: const char [7] (size 0x7)
    a ->  5: const const char [0] (size 0x0) -> 4: const char [0] (size 0x0)

  Types:
     1: char (size 0x1)
        [0x0] (ID 0x1) (kind 1) char  (aligned at 0x1, format 0x3, offset:bits
0x0:0x8)
     2: const char (size 0x1) -> 1: char (size 0x1)
        [0x0] (ID 0x2) (kind 12) const char  (aligned at 0x1)
     3: void (size 0x0)
        [0x0] (ID 0x3) (kind 1) void  (aligned at 0x0, format 0x1, offset:bits
0x0:0x0)
     4: const char [0] (size 0x0)
        [0x0] (ID 0x4) (kind 4) const char [0]  (aligned at 0x1)
     5: const const char [0] (size 0x0) -> 4: const char [0] (size 0x0)
        [0x0] (ID 0x5) (kind 12) const const char [0]  (aligned at 0x1)
     6: long unsigned int (size 0x8)
        [0x0] (ID 0x6) (kind 1) long unsigned int  (aligned at 0x8, format 0x0,
offset:bits 0x0:0x40)
     7: const char [7] (size 0x7)
        [0x0] (ID 0x7) (kind 4) const char [7]  (aligned at 0x1)
     8: const const char [7] (size 0x7) -> 7: const char [7] (size 0x7)
        [0x0] (ID 0x8) (kind 12) const const char [7]  (aligned at 0x1)

  Strings:
    0: 
    1: char
    6: void
    b: a
    d: long unsigned int
    1f: a
    ...

Note, two entries in the "Variables:" sub-section, and two strings "a" for the
variable name in the "Strings:" sub-section.

In the above case, the compiler must emit only one entry for the variable a
with type specified as const char [7].

Note that, CTF format cannot differentiate between a non-defining extern
variable declaration vs. a defining variable declaration (unlike DWARF). So,
emitting two CTF variable records, one for the non-defining decl and another
for the defining decl will create ambiguity for the linker/CTF reader.

Reply via email to