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

            Bug ID: 122326
           Summary: incorrect variable location in debug information with
                    -fstack-protector-strong enabled
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: debug
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wujielun402 at gmail dot com
  Target Milestone: ---

Consider the following test case:
---
$ cat array.c
int main() {
    int arr1[10];
    int arr2[10];

    for (int i = 0; i < 10; i++) {
        arr1[i] = i;
    }

    return arr1[0];
}
---

$ gcc array.c -g -fstack-protector-strong -o a.out
$ readelf -wi a.out
...
 <2><50>:Abbrev Number: 1 (DW_TAG_variable)
    <51>   DW_AT_name        : (indirect string, offset: 0xa7): arr1
    <55>   DW_AT_decl_file   : 1
    <55>   DW_AT_decl_line   : 2
    <56>   DW_AT_decl_column : 9
    <56>   DW_AT_type        : <0x92>
    <5a>   DW_AT_location    : 2 byte block: 91 40      (DW_OP_fbreg: -64)
 <2><5d>:Abbrev Number: 1 (DW_TAG_variable)
    <5e>   DW_AT_name        : (indirect string, offset: 0xac): arr2
    <62>   DW_AT_decl_file   : 1
    <62>   DW_AT_decl_line   : 3
    <63>   DW_AT_decl_column : 9
    <63>   DW_AT_type        : <0x92>
    <67>   DW_AT_location    : 2 byte block: 91 40      (DW_OP_fbreg: -64)
...

###########################################
We observed that the memory addresses (Corresponding to DW_AT_location)
recorded for variables arr1 and arr2 in the debug information are identical,
which is clearly incorrect. Therefore, when observing variable arr2, the value
of arr1 is actually displayed (e.g., during debugging in GDB).

Notably, this error disappears when the -fstack-protector-strong option is
disabled, suggesting that the option may be triggering this bug.

Reply via email to