From patchwork Tue Jun 18 00:17:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Harmstone X-Patchwork-Id: 1948915 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=j2sHlEZS; 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 4W36y5724Lz20XQ for ; Tue, 18 Jun 2024 10:26:53 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4A430388207A for ; Tue, 18 Jun 2024 00:26:52 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail.burntcomma.com (mail2.burntcomma.com [217.169.27.34]) by sourceware.org (Postfix) with ESMTPS id 804D3388207A for ; Tue, 18 Jun 2024 00:17:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 804D3388207A 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 804D3388207A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=217.169.27.34 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718669924; cv=none; b=YEkb6DIDmo92YESXdg2Ft/cmIgj0D/oXQSkJE7YV1uQURHnrj9lc1qn9nmiMuRiDjOQfT0yYdElHhf0/FSu3XcWwLp8GcEMtKMrwOSKInci8hq37edaf/qwbzZTkT53SXJzoQlwvDPXkLySMZ3jS3mH4RFyyauU1oxgz5/tSWbk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718669924; c=relaxed/simple; bh=UQ/1va7I427jVCUt8HqIEiTjNg+wBNndHTxpegC4TBs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:Mime-Version; b=CIE1b5eKjmzs376Tt+fZ/13L8twH59InWpUWd3+lN+rUCyyc278EgfqUSSiiEoI8Gnzrf2+A8Khh3V5gC6wBKMTz+rD88Ck1evEkBnPOp8PfjCmJv9+YYlrLAZHXqOtAvizrZpF0tgarooN7d0scanQ3DBO+KpoLULDgi2I7980= 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 5FCF33CF002D; 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=3zMol9Q5gmT5vH6Vg6A5ZelHUUh2H0wXQtZrygFfYBg=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=j2sHlEZSyaR8aBSO0QUoIRCPcHCluX8tKa5P5ZvRfF2LmC/sQCraaraHUiXgN4JgZ hCIZOVPAKwg7XPP4Wnn7OYONb8E/6rVbvw7KJBI42Q83wJWsSAeWEOef7yj4mBweC2 Qg4gH1Pen7Qzd6FffxTrI4hTCxKOSNBH6dJEWudI= From: Mark Harmstone To: gcc-patches@gcc.gnu.org Cc: Mark Harmstone Subject: [PATCH 11/11] Handle subroutine types in CodeView Date: Tue, 18 Jun 2024 01:17:13 +0100 Message-ID: <20240618001713.24034-12-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_subroutine_type DIEs into LF_PROCEDURE symbols. gcc/ * dwarf2codeview.cc (struct codeview_custom_type): Add lf_procedure and lf_arglist to union. (write_lf_procedure, write_lf_arglist): New functions. (write_custom_types): Call write_lf_procedure and write_lf_arglist. (get_type_num_subroutine_type): New function. (get_type_num): Handle DW_TAG_subroutine_type DIEs. * dwarf2codeview.h (LF_PROCEDURE, LF_ARGLIST): Define. --- gcc/dwarf2codeview.cc | 238 ++++++++++++++++++++++++++++++++++++++++++ gcc/dwarf2codeview.h | 2 + 2 files changed, 240 insertions(+) diff --git a/gcc/dwarf2codeview.cc b/gcc/dwarf2codeview.cc index 06267639169..e8ed3713480 100644 --- a/gcc/dwarf2codeview.cc +++ b/gcc/dwarf2codeview.cc @@ -262,6 +262,19 @@ struct codeview_custom_type uint8_t length; uint8_t position; } lf_bitfield; + struct + { + uint32_t return_type; + uint8_t calling_convention; + uint8_t attributes; + uint16_t num_parameters; + uint32_t arglist; + } lf_procedure; + struct + { + uint32_t num_entries; + uint32_t *args; + } lf_arglist; }; }; @@ -1623,6 +1636,102 @@ write_lf_bitfield (codeview_custom_type *t) asm_fprintf (asm_out_file, "%LLcv_type%x_end:\n", t->num); } +/* Write an LF_PROCEDURE type. Function pointers are implemented as pointers + to one of these. */ + +static void +write_lf_procedure (codeview_custom_type *t) +{ + /* This is lf_procedure in binutils and lfProc in Microsoft's cvinfo.h: + + struct lf_procedure + { + uint16_t size; + uint16_t kind; + uint32_t return_type; + uint8_t calling_convention; + uint8_t attributes; + uint16_t num_parameters; + uint32_t arglist; + } 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_procedure.return_type); + putc ('\n', asm_out_file); + + fputs (integer_asm_op (1, false), asm_out_file); + fprint_whex (asm_out_file, t->lf_procedure.calling_convention); + putc ('\n', asm_out_file); + + fputs (integer_asm_op (1, false), asm_out_file); + fprint_whex (asm_out_file, t->lf_procedure.attributes); + putc ('\n', asm_out_file); + + fputs (integer_asm_op (2, false), asm_out_file); + fprint_whex (asm_out_file, t->lf_procedure.num_parameters); + putc ('\n', asm_out_file); + + fputs (integer_asm_op (4, false), asm_out_file); + fprint_whex (asm_out_file, t->lf_procedure.arglist); + putc ('\n', asm_out_file); + + asm_fprintf (asm_out_file, "%LLcv_type%x_end:\n", t->num); +} + +/* Write an LF_ARGLIST type. This is just a list of other types. LF_PROCEDURE + entries point to one of these. */ + +static void +write_lf_arglist (codeview_custom_type *t) +{ + /* This is lf_arglist in binutils and lfArgList in Microsoft's cvinfo.h: + + struct lf_arglist + { + uint16_t size; + uint16_t kind; + uint32_t num_entries; + uint32_t args[]; + } 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_arglist.num_entries); + putc ('\n', asm_out_file); + + for (uint32_t i = 0; i < t->lf_arglist.num_entries; i++) + { + fputs (integer_asm_op (4, false), asm_out_file); + fprint_whex (asm_out_file, t->lf_arglist.args[i]); + putc ('\n', asm_out_file); + } + + free (t->lf_arglist.args); + + 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. */ @@ -1673,6 +1782,14 @@ write_custom_types (void) case LF_BITFIELD: write_lf_bitfield (custom_types); break; + + case LF_PROCEDURE: + write_lf_procedure (custom_types); + break; + + case LF_ARGLIST: + write_lf_arglist (custom_types); + break; } free (custom_types); @@ -2488,6 +2605,123 @@ get_type_num_struct (dw_die_ref type, bool in_struct, bool *is_fwd_ref) return ct->num; } +/* Process a DW_TAG_subroutine_type DIE, adding an LF_ARGLIST and an + LF_PROCEDURE type, and returning the number of the latter. */ + +static uint32_t +get_type_num_subroutine_type (dw_die_ref type, bool in_struct) +{ + codeview_custom_type *ct; + uint32_t return_type, arglist_type; + uint16_t num_args; + dw_die_ref first_child; + + /* Find the return type. */ + + if (get_AT_ref (type, DW_AT_type)) + { + return_type = get_type_num (get_AT_ref (type, DW_AT_type), in_struct, + false); + if (return_type == 0) + return 0; + } + else + { + return_type = T_VOID; + } + + /* Count the arguments. */ + + first_child = dw_get_die_child (type); + num_args = 0; + + if (first_child) + { + dw_die_ref c; + + c = first_child; + do + { + c = dw_get_die_sib (c); + + if (dw_get_die_tag (c) != DW_TAG_formal_parameter + && dw_get_die_tag (c) != DW_TAG_unspecified_parameters) + continue; + + num_args++; + } + while (c != first_child); + } + + /* Create an LF_ARGLIST for the arguments. If this is a duplicate, ld + will take care of this for us. */ + + first_child = dw_get_die_child (type); + + ct = (codeview_custom_type *) xmalloc (sizeof (codeview_custom_type)); + + ct->next = NULL; + ct->kind = LF_ARGLIST; + ct->lf_arglist.num_entries = num_args; + + if (num_args > 0) + { + dw_die_ref c; + uint32_t *argptr; + + ct->lf_arglist.args = (uint32_t *) xmalloc (sizeof (uint32_t) * num_args); + argptr = ct->lf_arglist.args; + + c = first_child; + do + { + c = dw_get_die_sib (c); + + switch (dw_get_die_tag (c)) + { + case DW_TAG_formal_parameter: + *argptr = get_type_num (get_AT_ref (c, DW_AT_type), in_struct, + false); + argptr++; + break; + + case DW_TAG_unspecified_parameters: + *argptr = 0; + argptr++; + break; + + default: + break; + } + } + while (c != first_child); + } + else + { + ct->lf_arglist.args = NULL; + } + + add_custom_type (ct); + + arglist_type = ct->num; + + /* Finally, create an LF_PROCEDURE. */ + + ct = (codeview_custom_type *) xmalloc (sizeof (codeview_custom_type)); + + ct->next = NULL; + ct->kind = LF_PROCEDURE; + ct->lf_procedure.return_type = return_type; + ct->lf_procedure.calling_convention = 0; + ct->lf_procedure.attributes = 0; + ct->lf_procedure.num_parameters = num_args; + ct->lf_procedure.arglist = arglist_type; + + add_custom_type (ct); + + return ct->num; +} + /* Process a DW_TAG_array_type DIE, adding an LF_ARRAY type and returning its number. */ @@ -2671,6 +2905,10 @@ get_type_num (dw_die_ref type, bool in_struct, bool no_fwd_ref) num = get_type_num_array_type (type, in_struct); break; + case DW_TAG_subroutine_type: + num = get_type_num_subroutine_type (type, in_struct); + break; + default: num = 0; break; diff --git a/gcc/dwarf2codeview.h b/gcc/dwarf2codeview.h index 70eae554b80..e6ad517bf28 100644 --- a/gcc/dwarf2codeview.h +++ b/gcc/dwarf2codeview.h @@ -63,6 +63,8 @@ along with GCC; see the file COPYING3. If not see /* Constants for type definitions. */ #define LF_MODIFIER 0x1001 #define LF_POINTER 0x1002 +#define LF_PROCEDURE 0x1008 +#define LF_ARGLIST 0x1201 #define LF_FIELDLIST 0x1203 #define LF_BITFIELD 0x1205 #define LF_INDEX 0x1404