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

            Bug ID: 128023
           Summary: [DebugInfo] optimize_string_length emits DW_OP_call4
                    in strict DWARF 2
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: debug
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wujielun402 at gmail dot com
  Target Milestone: ---

With -gdwarf-2 -gstrict-dwarf, GCC emits an invalid DWARF 2 expression for
DW_AT_string_length: it contains DW_OP_call4 (0x99), which was introduced
in DWARF 3 and is not defined in DWARF 2.

Reproducer source (repro.f90):

        subroutine deferred_length_pointer (text, length)
          character(len=:), pointer, intent(in) :: text
          integer, intent(out) :: length
          length = len(text)
        end subroutine deferred_length_pointer

Compile to annotated assembly:

        gfortran -gdwarf-2 -gstrict-dwarf -O0 -S -dA repro.f90 -o repro.s

The DW_AT_string_length expression of DW_TAG_string_type contains the
following operations (the DIE reference is shown symbolically):

        DW_OP_call4 <artificial parameter DIE>
        DW_OP_deref

The referenced artificial parameter has a simple DW_AT_location. However,
DW_OP_call4 is unavailable in DWARF 2, and -gstrict-dwarf explicitly
disallows extensions from later DWARF versions. The string-length
expression should use DWARF-2-compatible operations in this mode.

Compiler Explorer reproduction using the GCC snapshot:
https://godbolt.org/z/143eGv71e

In gcc/dwarf2out.cc, optimize_string_length rewrites DW_OP_GNU_variable_value
into DW_OP_call4 followed by DW_OP_deref when the referenced DIE has a
suitable location description. This rewrite lacks a DWARF-version check
and therefore also runs in strict DWARF 2 mode.

Reply via email to