From patchwork Tue Jun 18 00:17:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Harmstone X-Patchwork-Id: 1948914 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=harmstone.com header.i=@harmstone.com header.a=rsa-sha256 header.s=mail header.b=u/LCCaYz; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W36wt5tZ6z20XQ for ; Tue, 18 Jun 2024 10:25:50 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0F7F23882AC0 for ; Tue, 18 Jun 2024 00:25:49 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail.burntcomma.com (mail.burntcomma.com [IPv6:2a02:8012:8cf0:250::6d61:696c]) by sourceware.org (Postfix) with ESMTPS id 4AB1C3882106 for ; Tue, 18 Jun 2024 00:17:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4AB1C3882106 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=harmstone.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=harmstone.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4AB1C3882106 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a02:8012:8cf0:250::6d61:696c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718669923; cv=none; b=mOSqhCCBb3jjF5giHlDCbfjG6KbPJb5o+N5wFhLnGDF0agZfMKEn/IVbVxac2M+IvwuWu4F8pUrP+1g7+4kdZVVTSHCzPPaG6y1zBvY3NjEsoJF00BZ7g5SVAHuUFZ8qkw+EY7ytb6N8MIxZ+0/fjJKCs13+d3Rh0oc0p1R7YOA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718669923; c=relaxed/simple; bh=fQZT5yKWpxkRzxBP1tbs0iXDtN5oEr58NDvWgCicVrI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:Mime-Version; b=tFxMm189T/qmNeZTaJePlw4in91UEgH/p8tpndGw3rA8ztRPk/nBGFEs6NqmpWApoT9DCQo+tUBFiyxYYR+KK5cvr4UId+NxABCqiRS4V4ycL5oRIAMnQ8ej4VwU2qeJxxDqaOjWCNIXrAGAdiCzWLXj+Q8maGKqhnpVwaspO+E= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from localhost.localdomain (beren.burntcomma.com [IPv6:2a02:8012:8cf0:0:b62e:99ff:fee9:ad9f]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) by mail.burntcomma.com (Postfix) with ESMTPSA id 2B6123CF0027; Tue, 18 Jun 2024 01:17:22 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=harmstone.com; s=mail; t=1718669842; bh=7vHZBJ2srp3PvLXXemhqsHxpRi5XVtp+/707OkpQDGQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=u/LCCaYzdPVzwQrUzVt7xoNrX5PgZ1HMUNk7wMOzoguYADK2dbFzxAfSZXgWbgLJU dQVAEOIj26pJ+tPv0apV3Yerj2TflSp6Sfe8XPBeNzpDR33BTJNPLkWHyz/SFa533I gYqqVrAMqninOFp65pAoUC9U8uPAhQa6VNMGDBk4= From: Mark Harmstone To: gcc-patches@gcc.gnu.org Cc: Mark Harmstone Subject: [PATCH 09/11] Handle arrays for CodeView Date: Tue, 18 Jun 2024 01:17:11 +0100 Message-ID: <20240618001713.24034-10-mark@harmstone.com> In-Reply-To: <20240618001713.24034-1-mark@harmstone.com> References: <20240618001713.24034-1-mark@harmstone.com> Mime-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Translates DW_TAG_array_type DIEs into LF_ARRAY symbols. gcc/ * dwarf2codeview.cc (struct codeview_custom_type): Add lf_array to union. (write_lf_array): New function. (write_custom_types): Call write_lf_array. (get_type_num_array_type): New function. (get_type_num): Handle DW_TAG_array_type DIEs. * dwarf2codeview.h (LF_ARRAY): Define. --- gcc/dwarf2codeview.cc | 179 ++++++++++++++++++++++++++++++++++++++++++ gcc/dwarf2codeview.h | 1 + 2 files changed, 180 insertions(+) diff --git a/gcc/dwarf2codeview.cc b/gcc/dwarf2codeview.cc index 9e3b64522b2..3f1ce5577fc 100644 --- a/gcc/dwarf2codeview.cc +++ b/gcc/dwarf2codeview.cc @@ -250,6 +250,12 @@ struct codeview_custom_type codeview_integer length; char *name; } lf_structure; + struct + { + uint32_t element_type; + uint32_t index_type; + codeview_integer length_in_bytes; + } lf_array; }; }; @@ -1520,6 +1526,53 @@ write_lf_union (codeview_custom_type *t) asm_fprintf (asm_out_file, "%LLcv_type%x_end:\n", t->num); } +/* Write an LF_ARRAY type. */ + +static void +write_lf_array (codeview_custom_type *t) +{ + size_t leaf_len; + + /* This is lf_array in binutils and lfArray in Microsoft's cvinfo.h: + + struct lf_array + { + uint16_t size; + uint16_t kind; + uint32_t element_type; + uint32_t index_type; + uint16_t length_in_bytes; + char name[]; + } ATTRIBUTE_PACKED; + */ + + fputs (integer_asm_op (2, false), asm_out_file); + asm_fprintf (asm_out_file, "%LLcv_type%x_end - %LLcv_type%x_start\n", + t->num, t->num); + + asm_fprintf (asm_out_file, "%LLcv_type%x_start:\n", t->num); + + fputs (integer_asm_op (2, false), asm_out_file); + fprint_whex (asm_out_file, t->kind); + putc ('\n', asm_out_file); + + fputs (integer_asm_op (4, false), asm_out_file); + fprint_whex (asm_out_file, t->lf_array.element_type); + putc ('\n', asm_out_file); + + fputs (integer_asm_op (4, false), asm_out_file); + fprint_whex (asm_out_file, t->lf_array.index_type); + putc ('\n', asm_out_file); + + leaf_len = 13 + write_cv_integer (&t->lf_array.length_in_bytes); + + ASM_OUTPUT_ASCII (asm_out_file, "", 1); + + write_cv_padding (4 - (leaf_len % 4)); + + asm_fprintf (asm_out_file, "%LLcv_type%x_end:\n", t->num); +} + /* Write the .debug$T section, which contains all of our custom type definitions. */ @@ -1562,6 +1615,10 @@ write_custom_types (void) case LF_UNION: write_lf_union (custom_types); break; + + case LF_ARRAY: + write_lf_array (custom_types); + break; } free (custom_types); @@ -2346,6 +2403,124 @@ get_type_num_struct (dw_die_ref type, bool in_struct, bool *is_fwd_ref) return ct->num; } +/* Process a DW_TAG_array_type DIE, adding an LF_ARRAY type and returning its + number. */ + +static uint32_t +get_type_num_array_type (dw_die_ref type, bool in_struct) +{ + dw_die_ref base_type, t, first_child, c, *dimension_arr; + uint64_t size = 0; + unsigned int dimensions, i; + uint32_t element_type; + + base_type = get_AT_ref (type, DW_AT_type); + if (!base_type) + return 0; + + /* We need to know the size of our base type. Loop through until we find + it. */ + t = base_type; + while (t && size == 0) + { + switch (dw_get_die_tag (t)) + { + case DW_TAG_const_type: + case DW_TAG_volatile_type: + case DW_TAG_typedef: + case DW_TAG_enumeration_type: + t = get_AT_ref (t, DW_AT_type); + break; + + case DW_TAG_base_type: + case DW_TAG_structure_type: + case DW_TAG_class_type: + case DW_TAG_union_type: + case DW_TAG_pointer_type: + size = get_AT_unsigned (t, DW_AT_byte_size); + break; + + default: + return 0; + } + } + + if (size == 0) + return 0; + + first_child = dw_get_die_child (type); + if (!first_child) + return 0; + + element_type = get_type_num (base_type, in_struct, false); + if (element_type == 0) + return 0; + + /* Create an array of our DW_TAG_subrange_type children, in reverse order. + We have to do this because unlike DWARF CodeView doesn't have + multidimensional arrays, so instead we do arrays of arrays. */ + + dimensions = 0; + c = first_child; + do + { + c = dw_get_die_sib (c); + if (dw_get_die_tag (c) != DW_TAG_subrange_type) + continue; + + dimensions++; + } + while (c != first_child); + + if (dimensions == 0) + return 0; + + dimension_arr = (dw_die_ref *) xmalloc (sizeof (dw_die_ref) * dimensions); + + c = first_child; + i = 0; + do + { + c = dw_get_die_sib (c); + if (dw_get_die_tag (c) != DW_TAG_subrange_type) + continue; + + dimension_arr[dimensions - i - 1] = c; + i++; + } + while (c != first_child); + + /* Record an LF_ARRAY entry for each array dimension. If this leads to + duplicate types, ld will take care of it for us. */ + + for (i = 0; i < dimensions; i++) + { + codeview_custom_type *ct; + dw_die_ref index; + + ct = (codeview_custom_type *) xmalloc (sizeof (codeview_custom_type)); + + size *= get_AT_unsigned (dimension_arr[i], DW_AT_upper_bound) + 1; + + index = get_AT_ref (dimension_arr[i], DW_AT_type); + + ct->next = NULL; + ct->kind = LF_ARRAY; + ct->lf_array.element_type = element_type; + ct->lf_array.index_type = get_type_num (index, in_struct, false); + ct->lf_array.length_in_bytes.neg = false; + ct->lf_array.length_in_bytes.num = size; + + add_custom_type (ct); + + element_type = ct->num; + } + + free (dimension_arr); + + return element_type; +} + /* Process a DIE representing a type definition, add a CodeView type if necessary, and return its number. If it's something we can't handle, return 0. We keep a hash table so that we're not adding the same type multiple @@ -2407,6 +2582,10 @@ get_type_num (dw_die_ref type, bool in_struct, bool no_fwd_ref) num = get_type_num_struct (type, in_struct, &is_fwd_ref); break; + case DW_TAG_array_type: + num = get_type_num_array_type (type, in_struct); + break; + default: num = 0; break; diff --git a/gcc/dwarf2codeview.h b/gcc/dwarf2codeview.h index e60fe86e673..70eed6bf2aa 100644 --- a/gcc/dwarf2codeview.h +++ b/gcc/dwarf2codeview.h @@ -66,6 +66,7 @@ along with GCC; see the file COPYING3. If not see #define LF_FIELDLIST 0x1203 #define LF_INDEX 0x1404 #define LF_ENUMERATE 0x1502 +#define LF_ARRAY 0x1503 #define LF_CLASS 0x1504 #define LF_STRUCTURE 0x1505 #define LF_UNION 0x1506