From patchwork Fri Jan 12 14:13:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 83999 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 445773858C60 for ; Fri, 12 Jan 2024 14:14:44 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x129.google.com (mail-il1-x129.google.com [IPv6:2607:f8b0:4864:20::129]) by sourceware.org (Postfix) with ESMTPS id D13493858413 for ; Fri, 12 Jan 2024 14:13:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D13493858413 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 D13493858413 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::129 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705068833; cv=none; b=qOlkqI1U1JP1yWpbUwUpcPFo/yP9RMhQMgRr8RGUmWmOE8EEBHUjx1kAXzaex5gcAP8Yj/rUze2d0gdT6RKSo6jMLMQN8cA8UqESA5Pi70f0eU/zLMBh2FCmDeoGY4gxJxhfXuSQ0iwJTFoDT5tAB3eg3dtQx4R0Y2Osizcom7k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705068833; c=relaxed/simple; bh=IU9IMXoWELH+k86OQ3Ow+0TsRkJi8G3p8NkMapy8epI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=HI3K2BMBmp8guXMF7fFNN1hvPy1yjir/RLdBeAJgRA53wLEYWcbnuSBGWQUKBSb7Aag0eX39gVEP/z/RpiB2O7bxjZq1/fLb7sPZW8YF6mgrInmsJj+bSv61sKnZorTgqZO39Ft9NOdH66/bSIAydHuYFbWj4JmWcV4f1JTHg+k= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x129.google.com with SMTP id e9e14a558f8ab-3606f507efeso30934715ab.3 for ; Fri, 12 Jan 2024 06:13:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1705068829; x=1705673629; 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=2TW/w6kqieq5Hmr1WGUK1qp/Azc8zfQgEXVwNuHyz7c=; b=F3vXkzf8nhyua1CgzW1kKGd0rpUfnpBxPA/xjkT1DXahxL8w7p46VlCu7tB18wCTGv D6EXIyJ790FujlkEULxONrJaLJS06lQhByga8e3I/J4JZLJscCz8KJpmhUNInYybFz3y qxfl+nCf5q0M/uFzKb6zOYOCySs8Ymukdy2T2yEgp7ALHR/QFaR6Ns3xkt3i8V8KlsjX 6b66sZijdpZsjlPALVsT7dD3tIZnhcs07pGgU6Lf1qeEiiqNT9v2CpdE4fmLo9i88jXe Y5G+Xs7UfYIVdrkwobSpAOAr+pMhIZRJ7206p2ZmgfcuVHl6876BwsxJLqmaeHUoL9SJ E1fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705068829; x=1705673629; 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=2TW/w6kqieq5Hmr1WGUK1qp/Azc8zfQgEXVwNuHyz7c=; b=sx7YA52pOj6iIy63/ohMeNvK19oQ77IDoClq+v8BLiahdrLqhWnd3w0fEjdGIL9/DG Wq9frFnInLZlav2oo9HrgwKbqkR9aqeKoc4akRgEPTbGxo3MfzkfXLIzA6q3/1Z9od1L /KaZtQP0R0D0bBbyFccz1WcgOOdsmTaf4Sd9+14C+FaXL2mPjIO7FdRdP44TmSi0EatV wRRAOfSJ+l932KeodlHY4ltoIn2Na04qRfhPVFojX16/YhW6fXWhmD2ZCjyyVgZmo8BH vOIK81ain2XYqhRdTXgg47kC0xI0uKl/KtEtZQFs2c+UHLA3UwzPKSvAC/rsMjCs56wm Yqtg== X-Gm-Message-State: AOJu0YwF6+lE7HanBVcF9Ts5ds/jhZPypt6PCvN/IfIkBpZdT+agsdCP FBCEY3VWhpzzHRQthmzWjWpv3w62ZCbyF5yLOC1FRucB3g== X-Google-Smtp-Source: AGHT+IHGQBc5nRUKk1BoWvUrChcsrXJGV7ZY0jaAv4VDiIdO5M0xGP9EGYOqXXRKV0MlFaCtvf/f7Q== X-Received: by 2002:a05:6e02:118a:b0:35f:b61f:a8a8 with SMTP id y10-20020a056e02118a00b0035fb61fa8a8mr1033843ili.26.1705068828967; Fri, 12 Jan 2024 06:13:48 -0800 (PST) Received: from localhost.localdomain (97-122-68-157.hlrn.qwest.net. [97.122.68.157]) by smtp.gmail.com with ESMTPSA id y7-20020a92d0c7000000b0035fffb69deasm956739ila.81.2024.01.12.06.13.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jan 2024 06:13:48 -0800 (PST) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Handle DW_AT_endianity on enumeration types Date: Fri, 12 Jan 2024 07:13:41 -0700 Message-ID: <20240112141341.2575981-1-tromey@adacore.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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+patchwork=sourceware.org@sourceware.org A user found that gdb would not correctly print a field from an Ada record using the scalar storage order feature. We tracked this down to a combination of problems. First, GCC did not emit DW_AT_endianity on the enumeration type. DWARF does not specify this, but it is an obvious and harmless extension. This was fixed in GCC recently: https://gcc.gnu.org/pipermail/gcc-patches/2024-January/642347.html https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=5d8b60effc7268448a94fbbbad923ab6871252cd Second, GDB did not handle this attribute on enumeration types. This patch makes this change and adds a test case that will pass with the patched GCC. So far, the GCC patch isn't on the gcc-13 branch; but if it ever goes in, the test case in this patch can be updated to reflect that. --- gdb/dwarf2/read.c | 66 ++++++++++++------- gdb/testsuite/gdb.ada/scalar_storage.exp | 9 ++- .../gdb.ada/scalar_storage/storage.adb | 9 ++- 3 files changed, 57 insertions(+), 27 deletions(-) diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 76dbeb8d536..4bcd95925fb 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -13170,6 +13170,44 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu) } } +/* Read DW_AT_endianity from DIE and compute the byte order that + should be used. The CU's arch is used as the default. The result + is true if the returned arch differs from the default, and false if + they are the same. If provided, the out parameter BYTE_ORDER is + also set. */ + +static bool +die_byte_order (die_info *die, dwarf2_cu *cu, enum bfd_endian *byte_order) +{ + gdbarch *arch = cu->per_objfile->objfile->arch (); + enum bfd_endian arch_order = gdbarch_byte_order (arch); + enum bfd_endian new_order = arch_order; + + attribute *attr = dwarf2_attr (die, DW_AT_endianity, cu); + if (attr != nullptr && attr->form_is_constant ()) + { + int endianity = attr->constant_value (0); + + switch (endianity) + { + case DW_END_big: + new_order = BFD_ENDIAN_BIG; + break; + case DW_END_little: + new_order = BFD_ENDIAN_LITTLE; + break; + default: + complaint (_("DW_AT_endianity has unrecognized value %d"), endianity); + break; + } + } + + if (byte_order != nullptr) + *byte_order = new_order; + + return new_order != arch_order; +} + /* Assuming DIE is an enumeration type, and TYPE is its associated type, update TYPE using some information only available in DIE's children. In particular, the fields are computed. */ @@ -13317,6 +13355,8 @@ read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu) type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu)); + type->set_endianity_is_not_default (die_byte_order (die, cu, nullptr)); + set_die_type (die, type, cu); /* Finish the creation of this type by using the enum's children. @@ -15220,7 +15260,6 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu) struct attribute *attr; int encoding = 0, bits = 0; const char *name; - gdbarch *arch; attr = dwarf2_attr (die, DW_AT_encoding, cu); if (attr != nullptr && attr->form_is_constant ()) @@ -15232,27 +15271,8 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu) if (!name) complaint (_("DW_AT_name missing from DW_TAG_base_type")); - arch = objfile->arch (); - enum bfd_endian byte_order = gdbarch_byte_order (arch); - - attr = dwarf2_attr (die, DW_AT_endianity, cu); - if (attr != nullptr && attr->form_is_constant ()) - { - int endianity = attr->constant_value (0); - - switch (endianity) - { - case DW_END_big: - byte_order = BFD_ENDIAN_BIG; - break; - case DW_END_little: - byte_order = BFD_ENDIAN_LITTLE; - break; - default: - complaint (_("DW_AT_endianity has unrecognized value %d"), endianity); - break; - } - } + enum bfd_endian byte_order; + bool not_default = die_byte_order (die, cu, &byte_order); if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed) && cu->lang () == language_ada @@ -15390,7 +15410,7 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu) maybe_set_alignment (cu, die, type); - type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order); + type->set_endianity_is_not_default (not_default); if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT) { diff --git a/gdb/testsuite/gdb.ada/scalar_storage.exp b/gdb/testsuite/gdb.ada/scalar_storage.exp index 1c60774a13f..6b83fab709e 100644 --- a/gdb/testsuite/gdb.ada/scalar_storage.exp +++ b/gdb/testsuite/gdb.ada/scalar_storage.exp @@ -33,5 +33,10 @@ if {![runto "storage.adb:$bp_location"]} { return } -gdb_test "print V_LE" "= \\(value => 126, another_value => 12\\)" -gdb_test "print V_BE" "= \\(value => 126, another_value => 12\\)" +gdb_test "print V_LE" "= \\(value => 126, another_value => 12, color => green\\)" + +# This requires a compiler fix that is in GCC 14. +if {[gcc_major_version] < 14} { + setup_kfail "DW_AT_endianity on enum types" *-*-* +} +gdb_test "print V_BE" "= \\(value => 126, another_value => 12, color => green\\)" diff --git a/gdb/testsuite/gdb.ada/scalar_storage/storage.adb b/gdb/testsuite/gdb.ada/scalar_storage/storage.adb index 7c93e2eba4b..009d0b070fd 100644 --- a/gdb/testsuite/gdb.ada/scalar_storage/storage.adb +++ b/gdb/testsuite/gdb.ada/scalar_storage/storage.adb @@ -20,14 +20,19 @@ procedure Storage is subtype Some_Range is Natural range 0..127; subtype Another_Range is Natural range 0..15; + type Colors is (Red, Green, Blue); + for Colors use (Red => 0, Green => 1, Blue => 2); + type Rec is record Value : Some_Range; Another_Value : Another_Range; + Color : Colors; end record; for Rec use record Value at 0 range 0..6; Another_Value at 0 range 7..10; + Color at 0 range 11..13; end record; type Rec_LE is new Rec; @@ -42,8 +47,8 @@ procedure Storage is V_BE : Rec_BE; begin - V_LE := (126, 12); - V_BE := (126, 12); + V_LE := (126, 12, Green); + V_BE := (126, 12, Green); Do_Nothing (V_LE'Address); -- START Do_Nothing (V_BE'Address);