From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id l6EHGXUQAGsOXSMAWB0awg (envelope-from ) for ; Fri, 20 Nov 2026 04:18:45 -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=NK+TXrRq; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 4CEA51E087; Fri, 20 Nov 2026 04:18:45 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.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,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.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 0811D1E087 for ; Fri, 20 Nov 2026 04:18:43 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2B6A748FEDA0 for ; Fri, 20 Nov 2026 09:18:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2B6A748FEDA0 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=NK+TXrRq Received: from m16.mail.126.com (m16.mail.126.com [220.197.31.9]) by sourceware.org (Postfix) with ESMTPS id 65E3B48FE0BA for ; Fri, 20 Nov 2026 09:18:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 65E3B48FE0BA 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 65E3B48FE0BA Authentication-Results: sourceware.org; arc=none smtp.remote-ip=220.197.31.9 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795166293; cv=none; b=fYxxM3aPcWhQARAQZ8C17mD4oPudnlMfDAStMG402oCJhgjKl6R61C/xK2XDUMkDgRUKAiFO9eIfPtSGgK3XesbNtmktcglyGCuRGe8fJTNrbnKD5lRgTuGrnTp24yr9TYN0dz1IAW7FyOGjSwFAterCXZKZ37jtjfcENLFoKvA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795166293; c=relaxed/simple; bh=5Yn3QU/b5Yx3EqThl7SPHr9ntIe5bH0+fTPUwkxWZwY=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=xYEzzVkIpaWreTs2Llq9+RKAio0fc2/YXoqjQgOZgZPwPEKkxVYHmvjEWBp2eYV8WndQIwALyYPRiTJk7OIjNZJhIbUfSG55TXmON1LSyVNyPD/2+7K0b4ChpIAaRzxcAJyvIFgpi0Uh8wbieYTE4hwy2qU3dLNzJRsJI5CnPGg= 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=NK+TXrRq DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 65E3B48FE0BA 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=x8 mx1nuUO4KtB2JTBtPqzET6I+BVtVyJze8VcRQePPg=; b=NK+TXrRqcSuawNyRAA iVCvyQ/xBoRqRj1qAT0+0FkiyJy0Sklqh4VpZbABPCzaQuGkJ+aOyktySE8qJItU lP1Ep6NL1qiYZnPeVgnes9+SlruZYA3n1ViBOk+i61ZWhPcPiziN3kb/q8hIRrMo tZwkwEmQXN6enwnVhDdYr8KmA= Received: from ubuntu-2204.. (unknown []) by gzsmtp4 (Coremail) with SMTP id PykvCgD3H25MEABrTSUCDw--.12072S2; Fri, 20 Nov 2026 17:18:05 +0800 (CST) From: Sendao Yan To: gdb-patches@sourceware.org Cc: tom@tromey.com, yansendao@126.com Subject: [PATCH] [gdb/symtab] Fall back to symtabs when the quick lookup misses Date: Fri, 20 Nov 2026 17:18:03 +0800 Message-Id: <20261120091803.89033-1-yansendao@126.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: PykvCgD3H25MEABrTSUCDw--.12072S2 X-Coremail-Antispam: 1Uf129KBjvJXoW3Jr4kCFyDGw1xCr15Kr4kZwb_yoWxWr1Upa sxGw1rKF4kJF1xAa1xJF17ZF13Cw1fGFyYkr93W3yIywn8tF97JFZ3K34Ykr45JrWFy3ya qF47ZFWDuFn8XFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zM9a9ZUUUUU= X-CM-SenderInfo: 51dq2vpqgd0qqrswhudrp/xtbBnw-xBGsAEE8VzgAA3K 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 lookup_symbol_in_objfile has only searched the cooked index since commit 86ac8c546235 ("Convert lookup_symbol_in_objfile"). The index stores the DWARF spelling of a name, so a DWARF5 static constexpr member of a template instantiation can be missed when the C++ expression evaluator looks it up using canonicalised template arguments: GCC records std::ratio<1, 1> but the lookup uses std::ratio<1l, 1l>. This makes libstdc++'s chrono pretty-printer fail with "gdb.error: value has been optimized out" when printing std::chrono::hh_mm_ss (PR symtab/33533, GCC PR 128817). Walk the expanded symtabs as a fallback when the quick lookup returns nothing, restoring the behaviour that existed before 86ac8c546235 while keeping the quick lookup as the fast path. 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/symtab.c | 20 +++++++++- gdb/testsuite/gdb.cp/static-const-template.cc | 33 +++++++++++++++ .../gdb.cp/static-const-template.exp | 40 +++++++++++++++++++ 3 files changed, 91 insertions(+), 2 deletions(-) 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/symtab.c b/gdb/symtab.c index dc40fbfadf6..13a275c3fcc 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -2501,8 +2501,9 @@ lookup_symbol_in_static_block (const char *name, } /* Perform the standard symbol lookup of NAME in OBJFILE: - 1) First search expanded symtabs, and if not found - 2) Search the "quick" symtabs (partial or .gdb_index). + 1) First search the "quick" symtabs (partial or .gdb_index), and if not + found + 2) Search the expanded symtabs. BLOCK_INDEX is one of GLOBAL_BLOCK or STATIC_BLOCK. */ static struct block_symbol @@ -2521,6 +2522,21 @@ lookup_symbol_in_objfile (struct objfile *objfile, enum block_enum block_index, result = lookup_symbol_via_quick_fns (objfile, block_index, name, domain); + if (result.symbol == NULL) + { + /* The cooked index does not necessarily contain every symbol that + the expanded symtabs do. In particular, GCC emits a DWARF5 + DW_TAG_variable for a static constexpr member using the DWARF + spelling of the enclosing template arguments (e.g. + std::ratio<1, 1>), while the C++ expression evaluator looks the + member up using the canonical spelling (std::ratio<1l, 1l>), so + the quick lookup misses it. Fall back to walking the symtabs, + which was the behaviour before commit 86ac8c546235. + + PR symtab/33533. */ + result = lookup_symbol_in_objfile_symtabs (objfile, block_index, + name, domain); + } symbol_lookup_debug_printf ("lookup_symbol_in_objfile (...) = %s", result.symbol != NULL ? host_address_to_string (result.symbol) 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..af8477dba4e --- /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 -1 +} + +if {![runto_main]} { + return -1 +} + +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