From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id P7hXCNOZC2ikLQUAWB0awg (envelope-from ) for ; Fri, 25 Apr 2025 10:18:59 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=PPr7QQti; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 178311E10E; Fri, 25 Apr 2025 10:18:59 -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,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (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 5935B1E0C0 for ; Fri, 25 Apr 2025 10:18:58 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0535C3857836 for ; Fri, 25 Apr 2025 14:18:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0535C3857836 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=PPr7QQti Received: from mail-il1-x134.google.com (mail-il1-x134.google.com [IPv6:2607:f8b0:4864:20::134]) by sourceware.org (Postfix) with ESMTPS id 994113858D38 for ; Fri, 25 Apr 2025 14:18:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 994113858D38 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 994113858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::134 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745590704; cv=none; b=K77NOw0KQ6bzl0q76WyWvWfSqBzf5DnYJDERIEdpDRfP3G4ChVgIA14HVmAMg78eenrkTYLsQHHkpe7wZspifPEKbwsOjdE8ZxYAs2gYJ1A/rA4cPERURSBxuSStrbZp95KByO9LvbNTmqrZ7IJcrHFqI6YH7Y5SrotN/6xFi/o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745590704; c=relaxed/simple; bh=wvT1xFR+Z1sTMii/rBHg0dtegVvwiidU2QGx7/7mbcw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=DNOjsG9r72aKmko4/vAZ0rOlvK8a1IfdySqMbdAhN9Bk9A4j0xPsrG8+qZo6MHqZ4tXgCMN/XoDoBQ8uDB7Qa+g/SYE8rWx0X+YySFGkkP/KfKUEcsbAlyrw5ADnTRWsw+lWOWlkDe54CkjJ3XAtT97GDFsyZsIXVqXIHNJmSXA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 994113858D38 Received: by mail-il1-x134.google.com with SMTP id e9e14a558f8ab-3d81ea55725so7140685ab.1 for ; Fri, 25 Apr 2025 07:18:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1745590703; x=1746195503; 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; bh=Tq8CRuENrSPRf48ipKrV+m4X9LiB7YkNAhteGgcjTLo=; b=PPr7QQtijKdQs4iFtB0/RAinW/GGxLZBnc9LZy2iuaCr4Svm7+Vw6dmBInK9pNP9F7 MhGCp2KTuUEqaV0Zx+mSG4G14Uvxhnap8DZTg3OsWI9idIBG7oMDQsctJhB2d+jEfRRA RNjFWe19OTD1OkcCy+S0MCb9mmSANVbJDq+GgItVSpeZrrK7I6JGw78r6j+HYhQup5Kp RcCPMU3zOzf/dkYsbIK1dfjlrwOHZtF5Kg1FezJj9njlZgnqQ05dPT7gezitbESf1TI1 0SUWo7SYDmT0/canvbk2rD/jVfLaT2p8XdTWCqsy8xoAvTLjPpH49/nTVV0msINOGTaX SK5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745590703; x=1746195503; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Tq8CRuENrSPRf48ipKrV+m4X9LiB7YkNAhteGgcjTLo=; b=P5G44t3tTF4vyDgGlq/D/hZ/9fFqyruqU7faS/YC/u8oHRC2L5X9VTFQBAtHHM4/Kj d7KdxY5owGYa9apbTzuRsJmBTDDG8ky05lNFky2sd43uk4BoqpweNQFR4UqxU+XbI8ow brNWZGpBd0R/6za1aSTmfw6Y1M5ogNRrp1XeuqAfK4vMhU+bh9HSL/tRsHLx4T4OzY60 f9dX0RseEtrWLn7qgAHyPeMcilRdrYDJZmR9r1RgnkX4XM2i2Gwz3gRuzYarqhsJbD56 8bSDRVTrv30pePsjysL5YIwxqLdDXxjwjhtACnFZqxiqxuMaKJw3S5FDhNZtAkPgSoXG 1rbA== X-Gm-Message-State: AOJu0YysfQlpIrssXR0dKQ5ke8ILluwKSUdXRR1YttxPEUIyOOsN3CBf ZVD6VmYLA53BNokiic1qgW/wQOvxGAYi5G56tL0jlsQ5MBqb3wlvxvsFDHhXwxyBB9ST4WS587M = X-Gm-Gg: ASbGncvhUfPLl7kX8X7PAYAuVWl7WzhrE4pNBFJfr9lteDRz2dsLJ7e7cI1xNyLygv3 5/6RFCtOQ8d6mqOrjktsqErkD+zBQqmr+mcY/N57S6cje/C0+czGQtIpkozUqE/dDdUIIsfS02C sPQ7rd6ABYKZnBUBJiv4/XDW+N2og344Kk/bR8sHLDCxdoeAGb1wgC2GgG+Z3gvuXJKU5uGy8ua HAL+jyczYWqD+S2si4Bc8vDX1mIGLIi8h5vBupeQLXCQ8OVy/J5o3gvaYmIwqWyTpQDUtDnztCi Z60PWazgvWVWQBvVmNNt3yLrlOsytm+avabCyzbIyyztIQAtkm1m8XaKf3sma8mgsoImp/zGGu1 +zmjK X-Google-Smtp-Source: AGHT+IFhoyWkgGjtBjjwPVuT3V/tDlxOj27HUL+qBAFfG6jmcyP4A2wkGpiNohxfPQj6iZbKep9eJg== X-Received: by 2002:a05:6e02:228b:b0:3d1:97dc:2f93 with SMTP id e9e14a558f8ab-3d93b5b6b74mr22309875ab.20.1745590702775; Fri, 25 Apr 2025 07:18:22 -0700 (PDT) Received: from localhost.localdomain (97-118-46-156.hlrn.qwest.net. [97.118.46.156]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4f8249faa70sm804262173.13.2025.04.25.07.18.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Apr 2025 07:18:22 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Handle base type without DW_AT_byte_size Date: Fri, 25 Apr 2025 08:18:13 -0600 Message-ID: <20250425141813.826220-1-tromey@adacore.com> X-Mailer: git-send-email 2.49.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 DWARF says that a base type can have DW_AT_bit_size, without DW_AT_byte_size. However, gdb does not correctly handle this; in fact, it crashes, as pointed out in this LLVM merge request: https://github.com/llvm/llvm-project/pull/137123 This patch reworks the base type size logic a bit to handle this situation. --- gdb/dwarf2/read.c | 67 ++++++++++++++++------------ gdb/testsuite/gdb.dwarf2/intbits.c | 3 ++ gdb/testsuite/gdb.dwarf2/intbits.exp | 19 +++++++- 3 files changed, 59 insertions(+), 30 deletions(-) diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 2523ca86959..a010113b3d3 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -13634,7 +13634,6 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu) struct type *type; struct attribute *attr; ULONGEST encoding = 0; - int bits = 0; const char *name; attr = dwarf2_attr (die, DW_AT_encoding, cu); @@ -13644,9 +13643,33 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu) if (value.has_value ()) encoding = *value; } + attr = dwarf2_attr (die, DW_AT_byte_size, cu); + std::optional byte_size; + if (attr != nullptr) + byte_size = attr->unsigned_constant (); + attr = dwarf2_attr (die, DW_AT_bit_size, cu); + std::optional bit_size; if (attr != nullptr) - bits = attr->unsigned_constant ().value_or (0) * TARGET_CHAR_BIT; + bit_size = attr->unsigned_constant (); + + attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu); + std::optional bit_offset; + if (attr != nullptr) + bit_offset = attr->unsigned_constant (); + + int bits = 0; + if (byte_size.has_value ()) + bits = TARGET_CHAR_BIT * *byte_size; + else if (bit_size.has_value ()) + bits = align_up (*bit_size, 8); + else + { + /* No size, so arrange for an error type. */ + complaint (_("DW_TAG_base_type has neither bit- nor byte-size")); + encoding = (ULONGEST) -1; + } + name = dwarf2_full_name (nullptr, die, cu); if (!name) complaint (_("DW_AT_name missing from DW_TAG_base_type")); @@ -13792,35 +13815,21 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu) type->set_endianity_is_not_default (not_default); - if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT) + /* If both a byte size and bit size were provided, then that means + that not every bit in the object contributes to the value. */ + if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT + && byte_size.has_value () + && bit_size.has_value ()) { - attr = dwarf2_attr (die, DW_AT_bit_size, cu); - if (attr != nullptr && attr->form_is_constant ()) + /* DWARF says: If this attribute is omitted a default data bit + offset of zero is assumed. */ + ULONGEST offset = bit_offset.value_or (0); + + /* Only use the attributes if they make sense together. */ + if (*bit_size + offset <= 8 * type->length ()) { - unsigned real_bit_size = attr->unsigned_constant ().value_or (0); - if (real_bit_size >= 0 && real_bit_size <= 8 * type->length ()) - { - attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu); - /* Only use the attributes if they make sense together. */ - std::optional bit_offset; - if (attr == nullptr) - bit_offset = 0; - else if (attr->form_is_constant ()) - { - bit_offset = attr->unsigned_constant (); - if (bit_offset.has_value () - && *bit_offset + real_bit_size > 8 * type->length ()) - bit_offset.reset (); - } - if (bit_offset.has_value ()) - { - TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size - = real_bit_size; - if (attr != nullptr) - TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset - = *bit_offset; - } - } + TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size = *bit_size; + TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset = offset; } } diff --git a/gdb/testsuite/gdb.dwarf2/intbits.c b/gdb/testsuite/gdb.dwarf2/intbits.c index 82e6ae8a052..909d2837b9f 100644 --- a/gdb/testsuite/gdb.dwarf2/intbits.c +++ b/gdb/testsuite/gdb.dwarf2/intbits.c @@ -41,6 +41,9 @@ unsigned char be30_1_off[4] = { 0x80, 0, 0, 2 }; here, to catch any situation where gdb tries to use the memory. */ unsigned char u32_0[4] = { 0xff, 0xff, 0xff, 0xff }; +/* An 8 bit slot holding a 3 bit value. */ +unsigned char just_bit_0 = 5; + int main (void) { diff --git a/gdb/testsuite/gdb.dwarf2/intbits.exp b/gdb/testsuite/gdb.dwarf2/intbits.exp index 7b50e15a0cd..ff1d69ae226 100644 --- a/gdb/testsuite/gdb.dwarf2/intbits.exp +++ b/gdb/testsuite/gdb.dwarf2/intbits.exp @@ -36,7 +36,7 @@ Dwarf::assemble ${asm_file} { {DW_AT_language @DW_LANG_C_plus_plus} } { declare_labels i7_type u1_type u17_type u31_type \ - u31_1_type u32_0_type u0_0_type be30_1_type + u31_1_type u32_0_type u0_0_type be30_1_type just_bit_type i7_type: DW_TAG_base_type { {DW_AT_encoding @DW_ATE_signed} @@ -167,6 +167,20 @@ Dwarf::assemble ${asm_file} { {DW_AT_location {DW_OP_addr [gdb_target_symbol "u32_0"]} SPECIAL_expr} } + + just_bit_type: DW_TAG_base_type { + {DW_AT_encoding @DW_ATE_unsigned} + {DW_AT_name "just_bit_type"} + {DW_AT_bit_size 3 DW_FORM_udata} + } + + DW_TAG_variable { + {DW_AT_name "v_just_bit"} + {DW_AT_type :${just_bit_type}} + {DW_AT_external 1 DW_FORM_flag} + {DW_AT_location {DW_OP_addr [gdb_target_symbol "just_bit_0"]} + SPECIAL_expr} + } } } } @@ -197,3 +211,6 @@ gdb_test "x/4xb &v_u32_1_off" ":\t0x0e\t0x00\t0x00\t0x00" gdb_test "print v_be30_1_off" "= 1" gdb_test "print v_be30_1_off = 7" " = 7" gdb_test "x/4xb &v_be30_1_off" ":\t0x00\t0x00\t0x00\t0x0e" + +gdb_test "print/x v_just_bit" " = 0x5" +gdb_test "print/x (just_bit_type) 5" " = 0x5" base-commit: 5363deffcfb1c1825db3a8446d377a0ad56eff70 -- 2.49.0