From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id bijWFJRg4GqyvC4AWB0awg (envelope-from ) for ; Tue, 27 Oct 2026 04:28:04 -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=rqaeJLeJ; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 43822378003; Tue, 27 Oct 2026 04:28:04 -0400 (EDT) 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 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 3EB52378002 for ; Tue, 27 Oct 2026 04:28:03 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8C4A74BA23E2 for ; Tue, 27 Oct 2026 08:28:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8C4A74BA23E2 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=rqaeJLeJ Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by sourceware.org (Postfix) with ESMTPS id 5AFA04BA2E06 for ; Tue, 27 Oct 2026 08:27:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5AFA04BA2E06 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 5AFA04BA2E06 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1036 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793089655; cv=none; b=aQIpS6VLwfB5UgAfyaQkn7xTtqqB6cDiQL4YSTUl8G71aF53A6qWrKzndvCI9LYBti6OOXw3EZ8uNVZW5rj94jE4OiGLCkn8leMOfmEUDduuB6EdlS9UdqYDXWSxcHdtWKPzriTQHudtP63QvpiIF+uUVWxTPi5oGv2+4+XV0ag= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793089655; c=relaxed/simple; bh=Dt1T47qjkb65D0/UF2DHanRFDJqB8C5oy5y0gsAM9Hk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=upMrhg/ohKYDtmudzvbhnw04Gj31eaugHZj0cPZwvK3h+bAK12A7VDu6z9h4Vekm6AKaN9PDh3knll+DG6jEm1ahUSYlGwNWMgwqIdHyHZJ/VCk/XiBGgqOY9+6piFQImASzvrrg/8AXOb6rJjdLtXltdWyiQSyjxVVxQhx1n0M= 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=rqaeJLeJ DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5AFA04BA2E06 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-3adf5f6833cso1694459a91.0 for ; Tue, 27 Oct 2026 01:27:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1793089654; x=1793694454; 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=/vlqskO3sPs3DcDcmeqU4Us38ImkZcOstfDy3FGAWIs=; b=rqaeJLeJbCIP+IMizrT1Nvw9ie93XM/rJPhaSZW5OBeNHeoNvsPPUiaDeP9zg48gSg ihLK9sO3QAfVJ6JaY+Fc+ZWVZPl9RJgwUrJBMg182AvT1fXKtFIrvI1kmUdCxGNaff61 zuPDTCivuu1R4hR2qPWs1xFFpCpYPX5yS3K3xguCuS0H8OcEt9umBWrGnMSYJ71eNWCY mP8zFMQgnjdSNAvSMpDenzJcQIEYzbkb61CABA6tARnQWWUu0jVc1m/0U82DYmP48I9f sseAPeG56Ob4O3tOquYi16SLW8k44RpexKhxz9Plv1QllAMpg5/sCg9yTcTIMg+N51kP 7PBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793089654; x=1793694454; 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=/vlqskO3sPs3DcDcmeqU4Us38ImkZcOstfDy3FGAWIs=; b=1RVJKBol+a/yB4roncQa2/mxVQFoCCs/HCqdDejnC5NtuJiRG06MqfgDgrkSoU8uYb qqRwN13jQz97jtnjYMLe1zOF+3PmPg4Eokmr2aai+PAbP8z+6NpB+8b17aMXOiVe4wIq WNzoGvJNtnM88ZOPbA55WUjgGd6jIaC+mfqdjapeMc94u7Mj4Hngrr5OF6PDY+ylcGUo oXPY19Lget4/OtK80ty9rKtzppvLaZcmxjBsiTf9/5ZzyU4wivbi7MmnIVDMZrhIQuU3 DbAVLZj87EakU6vwqAfZiQ8iwcBbeG8FYDhg6FH3Mm5AHnwFqGhBokyufObkUCpzPHRE zHcA== X-Gm-Message-State: AFq9FYKUWLJV0ysY8qKAZZv54hdkTlc/BDwqz7GJU409TPCdQ6A136mQ QARjK//HL8XybahCks09t/j4PTaufODf6zTu9np1VTMIIAAs4vSVP4VXrKstLcCN X-Gm-Gg: AYBFou1KztM4gajHPbhrFOgol2SKPa2jNg3eIGSFe7DywJXgaznpOri1g1AGrn3yaaC HXGduH6xtJiO/fMAi89ZPOSyHQts/0bXodD/1d4mY2Y8MpXK61Hq6bfD5jFlLrnBTVKXpul1m7U 0Ak5m6dlS+0Q0HqCd5j5BQifU2kA8h0LjIaHu3fYbJszm4zo7iwW41sELZaCWOWA+vQDuf51Vum 6y2Tqt/hMSlWPl7XvCmpAoFXygnfr/q37bzi59+xBLhiMmT/H/aiCJSq5WR1NfGawk8hCyKOneI fYCliFsXTCssk1MpKiPCcvQktP8wsAOHtBK7jkZwyZ7cvl9q9KbXlgEP6K4iJpeiZCWKM+r/q2H R7gFqGdUGvQdPxtvwAkXYIJJwWgNwtB76OJ7bA/6m4zEJDA3D/7FI3hMTwD5mB1Y/gVdcHmLdKp tCcs064L9/lhjoreZPKznstw5Glawjd0aNoHjztdoj0/WZ0DEHlKesRlIkljsISTob7z1Mg9QW2 a712y+/nW29nVBmDi+ZCWXMutv4lc2XJfIpJ4JMPx+9rC0Mz0h/HEIV91ZyOZInmAly9fJgYNNb bw== X-Received: by 2002:a17:90b:3c06:b0:3a0:516d:9f88 with SMTP id 98e67ed59e1d1-3adeabde87bmr6870361a91.26.1793089654296; Tue, 27 Oct 2026 01:27:34 -0700 (PDT) Received: from anatasluo-Precision-5560 ([2408:820c:d29:db90:4b71:f390:c83e:d4dd]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3ae40651560sm2733330a91.13.2026.10.27.01.27.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Oct 2026 01:27:33 -0700 (PDT) From: Longjun Luo To: gdb-patches@sourceware.org Cc: Kevin Buettner , Longjun Luo Subject: [PATCH v3] gdb/dwarf: filter discarded zero-address variables after relocation Date: Tue, 27 Oct 2026 16:27:17 +0800 Message-ID: <20261027082717.3945887-1-luolongjuna@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261016040017.1155012-1-luolongjuna@gmail.com> References: <20261016040017.1155012-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 --- Changes in v3: - Document the std::nullopt return value in the comment above var_decode_location. Leave the two callers that ignore the return value as-is, as they only populate the symbol's location. Regarding the copyright assignment: I looked into the process, but I am currently unable to complete the paperwork. I understand the full patch (especially with the new test cases) exceeds the ~15-line threshold for legally significant changes. Would it be acceptable to take only the minimal fix in gdb/dwarf2/read.c under the small-change rule, or should we hold off on this patch until GDB's DCO policy is finalized? I will hold off on sending further patches in the meantime. v2: https://sourceware.org/pipermail/gdb-patches/2026-October/231127.html 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..77a74c49b35 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. Otherwise, return std::nullopt. */ + +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.53.0