From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 8b4TLpcoyWqcMiEAWB0awg (envelope-from ) for ; Fri, 09 Oct 2026 13:47:03 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=ZGKi761/; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id B2EC21E04E; Fri, 09 Oct 2026 13:47:03 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.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_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED 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 C4D161E04E for ; Fri, 09 Oct 2026 13:47:02 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 592124BA2E0D for ; Fri, 9 Oct 2026 17:47:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 592124BA2E0D Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=ZGKi761/ Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by sourceware.org (Postfix) with ESMTPS id 2C5404BA5435 for ; Fri, 9 Oct 2026 17:46:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2C5404BA5435 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2C5404BA5435 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1791567996; cv=none; b=Y2Rjb5zBtedbT80j3xGxD711eFHH5upx6DBuDEhnjvUcLsaBmr/3jKtL+Q8OMKAHXbTeuzpddI8gMYkDJbqTRFZFkVFHajfvVm9JUU1ZTgeGBb7ggIByvxwqOFP89aB4i08obTgHxx031Tk+a6KfBQKHIPLFfINxhXyfeQB8KTQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1791567996; c=relaxed/simple; bh=F4r6aBmwSEAqIFPWaLuE3IBT5zjTfnAUJ52ohuE0IRo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=vVFlGehKDl/EFc/tJx6ZMsrsZ+7xldSSITO0fiPDN0MZIhTEsW6e4pBosliS9I3dRJy6OTeV0OlVIEVjwssHo7FFQ/P6xZrH2TIkY+aJmoH5zhwymFpjEO4IGYOTpIWg54m9EPNanLwMGv/7rm/lDK+D/rINecssjFAOs9fTVZY= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=ZGKi761/ DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2C5404BA5435 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-8690079f2c1so2646417b3a.2 for ; Fri, 09 Oct 2026 10:46:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791567995; x=1792172795; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=uxeDoVVEscCF8pln0D3lA5gB7M9KMpPZZfxZlBjuyOk=; b=ZGKi761/0izb+i46B6jLek3KmkmQUznHwYyB0h573LXehlZrAtaexp8e8+9Z2v4S7v K+bUu3VWqbLPop9ud+Ss1UU1YlFdu8KQEMNh5H6EKpxdYrITNwZPztPcPGlQfYo0kWWj tXl2pZxvbbnxVQRZOKx6XxqM1ualhY4wvjRm/d9Azy3Ogz5vN8qS2GZx5W1BO/anh5eP WxiFssDrt+Hn9ms4hnSO1C81mX+sPLUKKWjJaWXZFvcV8gj72ojTvUdgSrFDL6jLmud0 4FVcBEKFBiB4GtSP7dnhEOsUi/3tE70Qpzhlgse6wYdvxQsZ6+bDqUld2JAtcP40/zL6 AoAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791567995; x=1792172795; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=uxeDoVVEscCF8pln0D3lA5gB7M9KMpPZZfxZlBjuyOk=; b=cSfKMXwXz9N1kL11yaofUO1B6zAhMXzZmqeM6Ldl6YsZjs1hL5Im9wdkSqRSEVMEv0 e9IOFL3PVCyuhhMGAqkDXF3VR6EnI8IFx29eGUPG/3ZJTJRqNYAiFq5s1xTW7mAd2J9u Q4iWqmXK4ToolkJ/h6w0cbNTI2qhCJFv2yXE+F5rQXwtbIANrXMzj/gZGCtqfDnTsRJS xf2ioceZss58tFBZDtoiIJSMkWEbm/hMVR+lsfz3UhubsyiUxTng36jXmAMZI3inuoVf zIlRjdPH3emiZOB5gr2nTKuwblrKzNIvdxusFEP/bAs95okyKP2efq4ozN4CTpKkUtaW Zptw== X-Gm-Message-State: AFq9FYLL9IAn4LxO/t1ekY5LgcjR0YdgVzmxAVsd9Kv1RKTZFTuzKMYj 6kYRKBgBPTBiALDDPuCaKZLzwAXMplvzdVbzhGIGm08eMNlS8JsTj+OUWl2uRuU7Gnx5xA== X-Gm-Gg: AYBFou2Suc2cY+uVVBee368Smo2dmcsqzuVt0YCr+OhIDPYgtZPowjMre0fZHr9x9XE c4jPwuWByh10DU4E28IKrC/rUu/NFiDC5krUsMw3x4SHqr7cXXzgd0mpwM8y+svNJLrAjSZk0cE CYRKqcAk2PSBgy2y1oBztBDN6VM+wdVXbiys0YBdJXjXynScqunHvmZ9t8MZSpiUZm9Hle0c5OZ gFeT4ZvOpoR0Q5sprIjsyo2mInYcruZq1YlHFdocNkgeOq1fmsMlI0qIcd835+BmGRu9AG+4Sqf oTN2b5rHpbnjaQ6laAvvtS2gk36M+hlfmsNUmLkpgAOjU0YYUuQE4kmQoqwW0yj04K6vIHwNfEy Nq1UsyJpAS/2vAV+CsLsujYTdTpb3fAndXUcGbOFjpl3T0ztPWOJQdPsBV9pEqHQPNN8i85oAIO thhNHFxszSPKVAghEOPdph1l6IQHHQSm7PznaxH7EbuGtAcF5SW4SEoS+Go6XJQfxO/RtLAyTQI IuM/zeoySIq7h1LzuSY X-Received: by 2002:a05:6a00:bb8d:b0:897:4c1b:3c77 with SMTP id d2e1a72fcca58-897c76e3278mr2276738b3a.34.1791567994755; Fri, 09 Oct 2026 10:46:34 -0700 (PDT) Received: from anatasluo-Precision-5560 ([2408:820c:d29:db90:5c2e:7b84:ca62:bb89]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-896c469d2a8sm1520682b3a.52.2026.10.09.10.46.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Oct 2026 10:46:34 -0700 (PDT) From: Longjun Luo To: gdb-patches@sourceware.org Cc: Tom Tromey , Simon Marchi Subject: [PATCH] gdb/dwarf: filter discarded zero-address variables after relocation Date: Sat, 10 Oct 2026 01:46:23 +0800 Message-ID: <20261009174623.2894733-1-luolongjuna@gmail.com> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 A variable discarded by section GC can retain a DIE with a zero DWARF address. var_decode_location adds the section offset before new_symbol checks that address. If the objfile has already been relocated, the symbol survives the zero-address check and can read the ELF header as data. This affects PIE executables when the CU is first expanded after starting the program. It also affects normally loaded shared libraries, including with --readnow, because their objfiles already have a load offset. Have var_decode_location return the address it decoded before relocation, and check that address in new_symbol, as the DWARF reader already does for discarded functions and address ranges. Preserve the existing exception for objects with allocated sections at zero. Extend gdb.dwarf2/dw2-var-zero-addr.exp with a PIE whose CU is first expanded after starting the program, and with a loaded shared library, with and without --readnow. Tested on x86_64 GNU/Linux with GCC. Without the fix, the three new cases fail; with it, the test passes. GDB selftests pass. Signed-off-by: Longjun Luo --- I do not have an FSF copyright assignment for GDB. I contribute to GCC under the DCO and have signed off this patch. Please let me know if any additional paperwork is needed for GDB. This came up while working on a DWARF proposal about post-link addresses [1]. The fix does not depend on the proposal. Tested on x86_64 GNU/Linux with Python enabled and --enable-werror. The revised regression passes with GCC, Clang and cc-with-gdb-index; all three new cases fail on the GCC baseline. Earlier full gdb.dwarf2 testing of the same implementation found no new unexpected failures; the existing debug-frame Tcl errors were unchanged. All 195 selftests passed. [1] https://lists.dwarfstd.org/pipermail/dwarf-discuss/2026-September/002859.html gdb/dwarf2/read.c | 27 +++++---- .../gdb.dwarf2/dw2-var-zero-addr.exp | 55 ++++++++++++++++++- 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 1fc6d7ffb4f..48c1100b645 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -514,9 +514,8 @@ show_dwarf_synchronous (struct ui_file *file, int from_tty, /* local function prototypes */ -static void var_decode_location (struct attribute *attr, - struct symbol *sym, - struct dwarf2_cu *cu); +static std::optional var_decode_location + (struct attribute *attr, struct symbol *sym, struct dwarf2_cu *cu); static unsigned int peek_abbrev_code (bfd *, const gdb_byte *); @@ -15160,7 +15159,11 @@ dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu, &cu->header, comp_dir); } -static void +/* Decode the DW_AT_location attribute ATTR of SYM. If ATTR is a single + DW_OP_addr, DW_OP_addrx or DW_OP_GNU_addr_index, return its address + before relocation. */ + +static std::optional var_decode_location (struct attribute *attr, struct symbol *sym, struct dwarf2_cu *cu) { @@ -15182,7 +15185,7 @@ var_decode_location (struct attribute *attr, struct symbol *sym, if (attr->form_is_block () && attr->as_block ()->size == 0) { sym->set_loc_class_index (LOC_OPTIMIZED_OUT); - return; + return {}; } /* Handle one degenerate form of location expression specially, to @@ -15216,7 +15219,7 @@ var_decode_location (struct attribute *attr, struct symbol *sym, sym->set_value_address (sym->value_address () + objfile->section_offsets[sym->section_index ()]); - return; + return tem; } } @@ -15231,6 +15234,8 @@ var_decode_location (struct attribute *attr, struct symbol *sym, if (sym->computed_ops ()->location_has_loclist) cu->has_loclist = true; + + return {}; } /* A helper function to add an "export" symbol. The new symbol starts @@ -15482,7 +15487,8 @@ new_symbol (struct die_info *die, struct dwarf2_cu *cu, struct symbol *sym, attr = dwarf2_attr (die, DW_AT_location, cu); if (attr != nullptr) { - var_decode_location (attr, sym, cu); + std::optional unrelocated + = var_decode_location (attr, sym, cu); attr2 = dwarf2_attr (die, DW_AT_external, cu); /* Fortran explicitly imports any global symbols to the local @@ -15491,14 +15497,15 @@ new_symbol (struct die_info *die, struct dwarf2_cu *cu, struct symbol *sym, && die->parent->tag == DW_TAG_common_block) attr2 = nullptr; - if (sym->loc_class () == LOC_STATIC - && sym->value_address () == 0 + if (unrelocated == (unrelocated_addr) 0 && !per_objfile->per_bfd->has_section_at_zero) { /* When a static variable is eliminated by the linker, the corresponding debug information is not stripped out, but the variable address is set to null; - do not add such variables into symbol table. */ + do not add such variables into symbol table. Check + the address before relocation, which adds the load + offset of a PIE or shared library. */ } else if (attr2 != nullptr && attr2->as_boolean ()) { diff --git a/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.exp b/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.exp index 04a2aa06ea9..027a073d7e0 100644 --- a/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.exp +++ b/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.exp @@ -25,7 +25,7 @@ if {[prepare_for_testing "failed to prepare" ${testfile} \ } proc test { } { - # FAIL was: = (int *) 0x0 + # FAIL was: = (int *) 0x0, or the load address in a relocated objfile. # Such DIE record can be produced using: gcc -fdata-sections -Wl,-gc-sections gdb_test "print &var" {No symbol "var" in current context\.} } @@ -40,3 +40,56 @@ save_vars { GDBFLAGS } { with_test_prefix "readnow" { test } + +# Expand the CU for the first time after the PIE has been relocated. +proc_with_prefix test_pie_after_run {} { + set executable ${::testfile}-pie + if {[build_executable "failed to prepare" $executable \ + [list $::srcfile $::srcfile2] {nodebug pie}]} { + return + } + + clean_restart $executable + if {[runto_main]} { + test + } +} + +# A shared library is relocated before its CUs are expanded, even with +# --readnow. +proc_with_prefix test_shlib {} { + set lib_path [standard_output_file ${::testfile}-shlib.so] + if {[gdb_compile_shlib $::srcdir/$::subdir/$::srcfile $lib_path \ + {nodebug}] != ""} { + untested "failed to compile shared library" + return + } + + set executable ${::testfile}-shlib + if {[build_executable "failed to prepare" $executable $::srcfile2 \ + [list debug shlib=$lib_path]]} { + return + } + + foreach_with_prefix readnow {false true} { + save_vars { ::GDBFLAGS } { + if {$readnow} { + append ::GDBFLAGS " --readnow" + } + clean_restart $executable + } + gdb_load_shlib $lib_path + if {[runto_main]} { + set lib_name [file tail $lib_path] + gdb_test "info sharedlibrary $lib_name" \ + ".*Yes.*[string_to_regexp $lib_name]" \ + "shared library is loaded" + test + } + } +} + +test_pie_after_run + +require allow_shlib_tests +test_shlib -- 2.53.0