From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id UuqwLgWyAWvepSgAWB0awg (envelope-from ) for ; Sat, 21 Nov 2026 10:00:21 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=LsYgJ4NG; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id AAC971E09B; Sat, 21 Nov 2026 10:00:21 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 2781F1E09B for ; Sat, 21 Nov 2026 10:00:19 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 06CA7487AAA2 for ; Sat, 21 Nov 2026 15:00:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 06CA7487AAA2 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=LsYgJ4NG Received: from m16.mail.126.com (m16.mail.126.com [117.135.210.9]) by sourceware.org (Postfix) with ESMTPS id 80DAC4315066 for ; Sat, 21 Nov 2026 14:59:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 80DAC4315066 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=126.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=126.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 80DAC4315066 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=117.135.210.9 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795273189; cv=none; b=b0/ba/1mFRmRK8IMBED0RVGeA0LV0UlEssVXoFovG6P6N3qed0CHloQe6SZHYXX/H8F5UBrMdAOfke+FWfiNYd1hLnk8/h/Z4QJDdHru6vxCPO4gWjQJWD2mqSRgCHA6iNkAUNv0C2zlUherQPbQxjGeooqLKefYTGYz26VQ/Pc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795273189; c=relaxed/simple; bh=IdSrDBQlo6sJWuC2r0IFr9MSemlwqI8mVjNb8U/6dcA=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=XdjTyaR+xsE/ocTdGHSHEJsfe46si4AoazEswJwERvErOoL28ATr1hRY6+wUI0yCdcP6wLQ7V9AHOx7UKyR1i+RGLeM9dFSljlqNHlCrp3Ixq39/d64cyP9bXEQcPZhHuyjvidVUxXwkobpMEdYZCo2xv4fbjeKp+lFroaG2Xjg= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=LsYgJ4NG DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 80DAC4315066 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=bf c1BhkEiekmCtracVXxY+mhePeQiw13B3I94zhCk3w=; b=LsYgJ4NG4i51cI3Pyq ZuPJQN7q11vrr+ZvvWp5KiPFISla4E0ysNXS6TEmhRR03z44Qrb923vjq1aPN5WE AISvHS2QfkCnH51J7kWdFuHYMZtbYkQ/PD+aAeqnXE3gZTFKZbMMmX5mMQ7UdS1H lUtLUkI79AvON7fRDaO1a4GCQ= Received: from ubuntu-2204.. (unknown []) by gzsmtp3 (Coremail) with SMTP id PikvCgA31erTsQFravZxDw--.27377S2; Sat, 21 Nov 2026 22:59:32 +0800 (CST) From: Sendao Yan To: gdb-patches@sourceware.org Cc: tom@tromey.com, aburgess@redhat.com, keiths@redhat.com, yansendao@126.com Subject: [PATCH v2] [gdb/symtab] Add canonical-name aliases for C++ template static members Date: Sat, 21 Nov 2026 22:59:30 +0800 Message-Id: <20261121145930.1048662-1-yansendao@126.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261120091803.89033-1-yansendao@126.com> References: <20261120091803.89033-1-yansendao@126.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: PikvCgA31erTsQFravZxDw--.27377S2 X-Coremail-Antispam: 1Uf129KBjvJXoW3XFyfAF15Jw13tFyDZF18Xwb_yoW3Wr1Upa 4fWryrKFn7GF1Iya4xJ3W7ZF13u3yfX34Yyas7Wr42y39xKFyxXFs5t3yakayrX3yFv3ya ga12vryDuFn8XFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zE89NQUUUUU= X-CM-SenderInfo: 51dq2vpqgd0qqrswhudrp/xtbBnhe8zmsBsdedKAAA3C X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org The cooked index stores names as they appear in the DWARF. For a static data member of a class template, GCC's DW_AT_name uses the DWARF spelling of the template arguments (e.g. S<1>::v), while the C++ expression evaluator looks the member up using the canonical spelling from the demangled linkage name (S<1l>::v). Since commit 86ac8c546235 lookup_symbol_in_objfile only searches the cooked index, the lookup misses and GDB reports the member as optimized out. This makes libstdc++'s chrono pretty-printer fail (PR symtab/33533, GCC PR 128817). Fix this in the index: for a C++ static data member (DW_TAG_variable or DW_TAG_member with a linkage name) whose canonical name contains template arguments, add a synthesized cooked-index entry using the demangled linkage name. The normal quick lookup then finds the symbol, so no symtab-expansion fallback is needed. Add gdb.cp/static-const-template.exp, which prints a static constexpr member of a class template with a long non-type template parameter. With the unpatched gdb it fails with ; with the fix it passes. Verified with the GCC testcase libstdc++-prettyprinters/chrono.cc (print hms/hms_nano, also with -flto), the new test, and the existing gdb.dwarf2/static-const-member.exp, gdb.cp/m-static.exp, gdb.cp/namespace.exp and gdb.base/condbreak-multi-context.exp. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33533 --- gdb/dwarf2/cooked-index-shard.c | 38 +++++++++++++++++- gdb/testsuite/gdb.cp/static-const-template.cc | 33 +++++++++++++++ .../gdb.cp/static-const-template.exp | 40 +++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.cp/static-const-template.cc create mode 100644 gdb/testsuite/gdb.cp/static-const-template.exp diff --git a/gdb/dwarf2/cooked-index-shard.c b/gdb/dwarf2/cooked-index-shard.c index 206fe4483a4..ad4a0cb6c87 100644 --- a/gdb/dwarf2/cooked-index-shard.c +++ b/gdb/dwarf2/cooked-index-shard.c @@ -23,6 +23,7 @@ #include "dwarf2/read.h" #include "dwarf2/error.h" #include "cp-support.h" +#include "demangle.h" #include "c-lang.h" #include "ada-lang.h" @@ -304,6 +305,7 @@ cooked_index_shard::canonicalize_names () htab_up gnat_entries (htab_create_alloc (10, hash_entry, eq_entry, nullptr, xcalloc, xfree)); std::vector new_gnat_entries; + std::vector new_cxx_entries; for (cooked_index_entry *entry : m_entries) { @@ -319,7 +321,39 @@ cooked_index_shard::canonicalize_names () here, then a check might be required there. */ gdb_assert (entry->canonical == nullptr); if ((entry->flags & IS_LINKAGE) != 0) - entry->canonical = entry->name (); + { + entry->canonical = entry->name (); + + /* For a C++ static data member, the DWARF name of the + enclosing template can use a non-canonical spelling of the + template arguments, while lookups use the canonical + spelling from the demangled linkage name. For example, + GCC emits S<1>::v while the demangled linkage name is + S<1l>::v. Add an alias entry using the canonical + demangled name so that the cooked-index lookup succeeds. + PR symtab/33533. */ + if (entry->lang == language_cplus + && (entry->tag == DW_TAG_variable + || entry->tag == DW_TAG_member)) + { + gdb::unique_xmalloc_ptr demangled + = gdb_demangle (entry->name (), + DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES); + if (demangled != nullptr + && strchr (demangled.get (), ':') != nullptr + && strchr (demangled.get (), '<') != nullptr + && strcmp (demangled.get (), entry->name ()) != 0) + { + const char *canon = m_names.insert (std::move (demangled)); + cooked_index_entry *alias + = create (entry->die_offset, entry->tag, + entry->flags | IS_SYNTHESIZED, entry->lang, + canon, nullptr, entry->per_cu); + alias->canonical = canon; + new_cxx_entries.push_back (alias); + } + } + } else if (entry->lang == language_ada) { /* Newer versions of GNAT emit DW_TAG_module and use a @@ -398,6 +432,8 @@ cooked_index_shard::canonicalize_names () m_entries iterator used in the foreach above. */ m_entries.insert (m_entries.end (), new_gnat_entries.begin (), new_gnat_entries.end ()); + m_entries.insert (m_entries.end (), new_cxx_entries.begin (), + new_cxx_entries.end ()); m_entries.shrink_to_fit (); std::sort (m_entries.begin (), m_entries.end (), diff --git a/gdb/testsuite/gdb.cp/static-const-template.cc b/gdb/testsuite/gdb.cp/static-const-template.cc new file mode 100644 index 00000000000..036e6b3b593 --- /dev/null +++ b/gdb/testsuite/gdb.cp/static-const-template.cc @@ -0,0 +1,33 @@ +/* Copyright 2026 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* A static constexpr member of a class template with a non-type template + parameter. The reference from the member typedef forces GCC to emit the + declaration DIE (DW_TAG_variable with DW_AT_const_value) in DWARF5. */ + +template +struct S +{ + static constexpr unsigned v = N; + using P = int[v]; +}; + +S<1> s; + +int +main () +{ + return 0; +} diff --git a/gdb/testsuite/gdb.cp/static-const-template.exp b/gdb/testsuite/gdb.cp/static-const-template.exp new file mode 100644 index 00000000000..b68d646cd09 --- /dev/null +++ b/gdb/testsuite/gdb.cp/static-const-template.exp @@ -0,0 +1,40 @@ +# Copyright 2026 Free Software Foundation, Inc. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test printing a static constexpr member of a class template with a +# non-type template parameter. +# +# In DWARF5, GCC spells the enclosing type S<1> while the C++ expression +# evaluator looks the member up using the canonical spelling S<1l>. The +# cooked index stores the DWARF spelling, so before the fix for PR +# symtab/33533 the lookup missed and gdb printed . + +standard_testfile .cc + +set opts {debug c++ dwarf5} +lappend opts additional_flags=-std=c++11 + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile $opts]} { + return +} + +if {![runto_main]} { + return +} + +gdb_test "print s.v" "= 1" \ + "print static constexpr member of a template" + +gdb_test "print 'S<1l>::v'" "= 1" \ + "print static constexpr member with canonical template arguments" -- 2.34.1