From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id ErjXLZOh0WrRtRYAWB0awg (envelope-from ) for ; Fri, 16 Oct 2026 00:01:23 -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=XUsIRs3R; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 662021E050; Fri, 16 Oct 2026 00:01:23 -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 1ED4D1E047 for ; Fri, 16 Oct 2026 00:01:21 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AAA744BA2E33 for ; Fri, 16 Oct 2026 04:01:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AAA744BA2E33 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=XUsIRs3R Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by sourceware.org (Postfix) with ESMTPS id 635374BA2E0C for ; Fri, 16 Oct 2026 04:00:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 635374BA2E0C 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 635374BA2E0C Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::102a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792123252; cv=none; b=arNoXu+lwbqzK1VfX7v1UPTum7XwyUjh4txvHoQQrFAOZh5cuBDHk9nHCVGN05AWngnFg/b828DKhOB1qz4VTmotgERu8cbQASuOydbr29TeG2iosU6KrsvXBDfL5Wdn23zA6O/3hZzqURG7C/iLUYqNBoIFNzz68Wq+QyNwqR0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792123252; c=relaxed/simple; bh=IbGuMCl1mHRDPRy2zAW9uZabluovh7pn51geaOjn5Ws=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=NUz4fiyu09xoW9KrXv5Xkj0K5tPMiImJlU5mdKlk609ftuZwGudy8Y9XvKGoIZVV25+NsGsh5CIQi/m2VX0Dzai9sY5OwXv8KdeJk8DGc9PpGpM4ph011HwAQym1bp0q4gTPPjNFDDNdGal+kF3NfwGG4DV/RE53I+CM5GKFK3g= 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=XUsIRs3R DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 635374BA2E0C Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-3a4b9732d5aso4789167a91.3 for ; Thu, 15 Oct 2026 21:00:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1792123251; x=1792728051; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=f7KnM17a37P2navSi5JS62T9WtvKPQzDh+5a1Tx6Ka0=; b=XUsIRs3RrBljjd2WyxkW4oIz6kpZ7eA9jqfMmaPZIwMt6z1C9KTgtNFD2EUwuIpefK xfmMtlqbTicBlN+QNsUaqZs8YbRsRgFFuSk9NqCxyyJrFbMLQVaUJtLXjBTPdMW7+KyQ jOM+k1I1PKbZhs9ASAWGV3hLvGW9xdoIn+oBeAVD3xYKYZIVRF6uGMP3aqIc1PD9sjCl 79xDV6MYGgbtCsONpuKOA8XSgnXSx1QL7rSmEVmw0AgPRAyGaeFjavUnGoZ7mxS7kHP8 MXwoo4cX9HLoJKk1QMDVJ2zUK77saP2G+A73fj757WjRvNrTwVQDEUrScnhyM+91499Y mRxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792123251; x=1792728051; h=content-transfer-encoding:mime-version:references:in-reply-to :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=f7KnM17a37P2navSi5JS62T9WtvKPQzDh+5a1Tx6Ka0=; b=DynXGeDXKljttLZfNSDN5Vt8CpKXm9HCjHCFdz2yvunO07NAUYRwtmmBdOdf7G+xra uQv0zJX77IwoUUMK5GyQxa9oUlroQK8WQi+cF3oNslD2C40OETAdNgVDvYBN08ghOaM4 4dFqLj3wNmw7cgzt5yZh9DMsBzFru8LjEG5bGI2oDW0GuTuC+/7A4Fg1SeUYWk2XAqpr uftvB5knB39eGJlWgFkSdpCohIdYKDetULaRw9HkCqRHWj3IgZe+BHjVjm4XDA5NGf7Z 0vcIMwFOXrIHOmV5v3uaSMomL+JuzEvvtSYyaCNWuwJh7rjoLNaskB55wEmAPnB3ppwX cOCQ== X-Gm-Message-State: AFq9FYLQJZTeoFqT6S/Q4jKJtz00WoHVhhdF4wmLGMJnvMTmMk1sE3Qk sXYhpHusTjDkOtpvM5GfHDKPSDJVUOe7mWJxYIhC/tQVP6fsL8JmLF3Oyz0xEiBu84Q= X-Gm-Gg: AYBFou0zI1/w/PtqcJ3OZa5DiX96llYnRtm9XriKh+wXSd1YrVmy4Na2UUL/dx3IzK3 VC21c/oOGLi1sS8Q+BN7rTUImTdmjk5QTVfaQArrQyArH1iG9M5C2CXOjVIJD4rGxyQ1hDF+sdY fMSSwWvO0vxHJqp/3opzSBKP+FGnXpVCVoMxCDcH0Qq9ilBRf1lQfgjUGEoe0DhIekmx4yu7UAm POwGc0csyxfBaYuTmnSlGMNIal3Ha0A4KPXXOCZkiaz3LdJTqztVYT4kFAKCCfUpwDa20mXwlS5 JH70ViynKvrD+KJINvKaVRDsjbdjksFO19I5W1Kx2B0Qchd64nWJBU8mz+a6iwkDIIG7zgUEeCQ 2kwvO+ZdYRnzTz5MMPr4u8B3Xl37HI5bDXSOXuww8gfmYbIoHi3yoTUS7+4C+T87UUy9bVlmt0m CzCskRSs2SBqz3zcytbo/gg/vWgtzr6ncnc4KZmXeFGaMMq8DcvdMETJ9DHE1Y51d/jnjU78vTu TfvzaHQYqTDQdvFazg= X-Received: by 2002:a17:90b:5830:b0:39e:6c68:c787 with SMTP id 98e67ed59e1d1-3ab3acea00dmr19360078a91.61.1792123251057; Thu, 15 Oct 2026 21:00:51 -0700 (PDT) Received: from anatasluo-Precision-5560 ([139.226.166.21]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3acc48f19d9sm2076871a91.11.2026.10.15.21.00.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Oct 2026 21:00:50 -0700 (PDT) From: Longjun Luo To: gdb-patches@sourceware.org Cc: Longjun Luo Subject: [PATCH v2] gdb/dwarf: filter discarded zero-address variables after relocation Date: Fri, 16 Oct 2026 12:00:17 +0800 Message-ID: <20261016040017.1155012-1-luolongjuna@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261009174623.2894733-1-luolongjuna@gmail.com> References: <20261009174623.2894733-1-luolongjuna@gmail.com> 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. Use a distinctive variable name to avoid clashes with shared-library debug symbols after the program starts. 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 --- v1: https://inbox.sourceware.org/gdb-patches/20261009174623.2894733-1-luolongjuna@gmail.com/ Changes in v2: - Use a test-specific variable name to avoid clashes with symbols from shared-library debug information. No reader changes. Linaro CI on ARM and AArch64 reported failures in the three new cases: (gdb) print &var Can't take address of "var" which isn't an lvalue. After the discarded test variable is filtered out, the lookup can find an unrelated symbol with the same name. I reproduced this on x86_64 with a same-named enumerator in shared-library debug information. The renamed test passes with and without those symbols. Re-tested on x86_64 GNU/Linux with GCC, Clang and cc-with-gdb-index (7 PASS each). The three added cases still fail without the fix. All 195 selftests pass. A full native testsuite comparison, with differing cases rechecked, found no new regressions; existing failures and test-driver errors remained in both variants. As in v1, I do not have an FSF copyright assignment for GDB and have signed off this patch under the DCO. gdb/dwarf2/read.c | 27 +++++---- gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.S | 2 +- .../gdb.dwarf2/dw2-var-zero-addr.exp | 60 ++++++++++++++++++- 3 files changed, 76 insertions(+), 13 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.S b/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.S index f7394f33946..233bd6ce79e 100644 --- a/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.S +++ b/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.S @@ -39,7 +39,7 @@ .byte 5 /* DW_AT_encoding */ .uleb128 3 /* Abbrev: DW_TAG_variable (location) */ - .ascii "var\0" /* DW_AT_name */ + .ascii "dw2_discarded_zero_addr_var\0" /* DW_AT_name */ .byte 2f - 1f /* DW_AT_location */ 1: .byte 3 /* DW_OP_addr */ .4byte 0 /* */ diff --git a/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.exp b/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.exp index 04a2aa06ea9..360894661e5 100644 --- a/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.exp +++ b/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.exp @@ -25,9 +25,12 @@ 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\.} + # Use a distinctive name to avoid clashes with shared-library debug + # symbols once the program runs. + gdb_test "print &dw2_discarded_zero_addr_var" \ + {No symbol "dw2_discarded_zero_addr_var" in current context\.} } test @@ -40,3 +43,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.55.0