From patchwork Tue Oct 31 00:28:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Harmstone X-Patchwork-Id: 159899 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp2593841vqb; Mon, 30 Oct 2023 17:30:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFPioLF5p/+PcaQHqo1htFCoAaubsrp++hSrK6+uCw+XLzWgyfvr52ZFlWGLGjyUirHpctB X-Received: by 2002:ad4:576c:0:b0:671:e8d3:5a08 with SMTP id r12-20020ad4576c000000b00671e8d35a08mr5189477qvx.10.1698712204005; Mon, 30 Oct 2023 17:30:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698712203; cv=pass; d=google.com; s=arc-20160816; b=u0LvhaWibgzvvcuyGa9kbQHEgGR+LZHtL141K9BVKd4odhgg7LBeJM5JcIVLP+exOr c76ebRc2plyjuXZ0TMWeUh2HIGuor8UfIi29WqvXRU795p0hihgi4+iCEYupLwzNSdbl bQSL9wBRcBqLkXYhDuanVJQRqXFnk5KpDgkMun0usmWjoOcaENDz6Us62fAQknrV4NHD mx4df/xT/fjkKyFa0vX74IeFZarbEYQyebPPbbroXmUqjS83FE9kgJU45nHzh7UlQZEs 7HgDrIMtlGLPqLcZeiVKkqa2sPHL1lJffZbC0vOL+8bOZ76W3hS/IYAspJSUSVusJxqV Kibg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=mO2BwVzapblGVPPgfbKr6Tj0Sk0nwNSd9Zy7i+Vtj5k=; fh=ULVDVi0LXJXve4bv6avxt5t1j9fbG672tLMzvs8AGaA=; b=S0Ll6ezribeFO0ZQGL5gg52z3jJ/cCKBt+dIWC/7tRgEVIjTO0BJKOjhtnOcwcjIC+ yj7I2sj4rkKJDnYqchmsr8O4Tv8cFiPoHPM20Vl4yxDvIcVqwmLN7e6ChBfMklSwCPzZ X3gFkUsKbYlg6w3ifQrmvifv80TjJ0VvcZgz04mTC3fFPhmdX/K8eFoLrGpxGn5CmUbO HdZDyZp+/1b7+IQQ2vfATmhlNnBnGJpMc+EbGpO7l+j0wnQLFNPL5B2JJu/iAFoeJ8uj 6E0/rfqsLaTvyGkFkKlrbX8odSlYa1wsvgQtKA6JSCFH0sHSDb262229WEKDkNifnyX7 9ubw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@harmstone.com header.s=mail header.b=4oS5Gi33; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id o5-20020a056214180500b0065af252fe4esi152647qvw.35.2023.10.30.17.30.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 17:30:03 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@harmstone.com header.s=mail header.b=4oS5Gi33; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BEFBE3858439 for ; Tue, 31 Oct 2023 00:30:03 +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 DDA253858D33 for ; Tue, 31 Oct 2023 00:29:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DDA253858D33 Authentication-Results: sourceware.org; dmarc=none (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 DDA253858D33 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=1698712150; cv=none; b=EHu3W7rEYL7hVPG705gHAqcms4tMEBSrq2kz94x/mbGrAOG0omfQYJv6uXcn/FQ1556wIpWXTIxF5r4QqiJ+Y+KU05L9ljjpYgXcVzzF/tXfrmkEoff9XNG2rK7Hn+eJ3efAP7k+3A0BbJ2qNpn5jwWF/FDSX1SuPPfr/3h561w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698712150; c=relaxed/simple; bh=jh++aZn912IUMGmAtQGUMuTGRMKFQ35VAE+umdRilNk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:Mime-Version; b=JV9ts+MWIlKE+R7FKD63ThSp1124J/wm82aZzE4y9kTKz4fvw1OlskGSEQAS/gXV4oR8tujR3ODEQu+pIs3o+oZwF5o5g39kRbptA+93jLDZf9sjadlsXsar320nLPgSjrg8BMWxsIc+R07jxZET4SvAPWWZVliRay72JzSUVtI= 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 98BF316B3C0D6; Tue, 31 Oct 2023 00:29:06 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=harmstone.com; s=mail; t=1698712146; bh=mO2BwVzapblGVPPgfbKr6Tj0Sk0nwNSd9Zy7i+Vtj5k=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=4oS5Gi33iYvHyEg6nCm1QWzwzQt5bhKbHvRAs7kFSLj1lyp6P/2fcNQlprDaSWSO7 dLFRoI/PNK77wTcPdt1TSR8252ZD77lCKkcEumVM1p8nmbfrr19GHMokcSeUd4VSRB mFdGCnDJlDpthvUORoNSRPIa0umfMn9F4IAY5I34= From: Mark Harmstone To: gcc-patches@gcc.gnu.org Cc: Mark Harmstone Subject: [PATCH v4 4/4] Output S_COMPILE3 symbol in CodeView debug section Date: Tue, 31 Oct 2023 00:28:19 +0000 Message-ID: <20231031002859.18892-5-mark@harmstone.com> In-Reply-To: <20231031002859.18892-1-mark@harmstone.com> References: <20231031002859.18892-1-mark@harmstone.com> Mime-Version: 1.0 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_STOCKGEN, SPF_HELO_NONE, SPF_PASS, TXREP 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781228848245719272 X-GMAIL-MSGID: 1781228848245719272 Outputs the S_COMPILE3 symbol in the CodeView .debug$S debug section. The DEBUG_S_SYMBOLS block added here makes up pretty much everything that isn't data structures or line numbers; we add the S_COMPILE3 symbol here to start it off. This is a descriptive bit, the most interesting part of which is the version of the compiler used. --- gcc/dwarf2codeview.cc | 126 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/gcc/dwarf2codeview.cc b/gcc/dwarf2codeview.cc index 9c69ebf8998..db776d79be4 100644 --- a/gcc/dwarf2codeview.cc +++ b/gcc/dwarf2codeview.cc @@ -39,14 +39,25 @@ along with GCC; see the file COPYING3. If not see #define CV_SIGNATURE_C13 4 +#define DEBUG_S_SYMBOLS 0xf1 #define DEBUG_S_LINES 0xf2 #define DEBUG_S_STRINGTABLE 0xf3 #define DEBUG_S_FILECHKSMS 0xf4 #define CHKSUM_TYPE_MD5 1 +#define S_COMPILE3 0x113c + +#define CV_CFL_80386 0x03 +#define CV_CFL_X64 0xD0 + +#define CV_CFL_C 0x00 +#define CV_CFL_CXX 0x01 + #define LINE_LABEL "Lcvline" #define END_FUNC_LABEL "Lcvendfunc" +#define SYMBOL_START_LABEL "Lcvsymstart" +#define SYMBOL_END_LABEL "Lcvsymend" #define HASH_SIZE 16 @@ -120,6 +131,7 @@ struct codeview_function static unsigned int line_label_num; static unsigned int func_label_num; +static unsigned int sym_label_num; static codeview_source_file *files, *last_file; static unsigned int num_files; static uint32_t string_offset = 1; @@ -592,6 +604,119 @@ codeview_end_epilogue (void) } } +/* Return the CodeView constant for the selected architecture. */ + +static uint16_t +target_processor (void) +{ + if (TARGET_64BIT) + return CV_CFL_X64; + else + return CV_CFL_80386; +} + +/* Return the CodeView constant for the language being used. */ + +static uint32_t +language_constant (void) +{ + const char *language_string = lang_hooks.name; + + if (startswith (language_string, "GNU C++")) + return CV_CFL_CXX; + else if (startswith (language_string, "GNU C")) + return CV_CFL_C; + + return 0; +} + +/* Write a S_COMPILE3 symbol, which records the details of the compiler + being used. */ + +static void +write_compile3_symbol (void) +{ + unsigned int label_num = ++sym_label_num; + + static const char compiler_name[] = "GCC "; + + /* This is struct COMPILESYM3 in binutils and Microsoft's cvinfo.h: + + struct COMPILESYM3 + { + uint16_t length; + uint16_t type; + uint32_t flags; + uint16_t machine; + uint16_t frontend_major; + uint16_t frontend_minor; + uint16_t frontend_build; + uint16_t frontend_qfe; + uint16_t backend_major; + uint16_t backend_minor; + uint16_t backend_build; + uint16_t backend_qfe; + } ATTRIBUTE_PACKED; + */ + + fputs (integer_asm_op (2, false), asm_out_file); + asm_fprintf (asm_out_file, + "%L" SYMBOL_END_LABEL "%u - %L" SYMBOL_START_LABEL "%u\n", + label_num, label_num); + + targetm.asm_out.internal_label (asm_out_file, SYMBOL_START_LABEL, label_num); + + fputs (integer_asm_op (2, false), asm_out_file); + fprint_whex (asm_out_file, S_COMPILE3); + putc ('\n', asm_out_file); + + /* Microsoft has the flags as a bitfield, with the bottom 8 bits being the + language constant, and the reset being MSVC-specific stuff. */ + fputs (integer_asm_op (4, false), asm_out_file); + fprint_whex (asm_out_file, language_constant ()); + putc ('\n', asm_out_file); + + fputs (integer_asm_op (2, false), asm_out_file); + fprint_whex (asm_out_file, target_processor ()); + putc ('\n', asm_out_file); + + /* Write 8 uint16_ts for the frontend and backend versions. As with GAS, we + zero these, as it's easier to record the version in the compiler + string. */ + for (unsigned int i = 0; i < 8; i++) + { + fputs (integer_asm_op (2, false), asm_out_file); + fprint_whex (asm_out_file, 0); + putc ('\n', asm_out_file); + } + + ASM_OUTPUT_ASCII (asm_out_file, compiler_name, sizeof (compiler_name) - 1); + ASM_OUTPUT_ASCII (asm_out_file, version_string, strlen (version_string) + 1); + + ASM_OUTPUT_ALIGN (asm_out_file, 2); + + targetm.asm_out.internal_label (asm_out_file, SYMBOL_END_LABEL, label_num); +} + +/* Write the CodeView symbols into the .debug$S section. */ + +static void +write_codeview_symbols (void) +{ + fputs (integer_asm_op (4, false), asm_out_file); + fprint_whex (asm_out_file, DEBUG_S_SYMBOLS); + putc ('\n', asm_out_file); + + fputs (integer_asm_op (4, false), asm_out_file); + asm_fprintf (asm_out_file, "%LLcv_syms_end - %LLcv_syms_start\n"); + + asm_fprintf (asm_out_file, "%LLcv_syms_start:\n"); + + write_compile3_symbol (); + + asm_fprintf (asm_out_file, "%LLcv_syms_end:\n"); +} + /* Finish CodeView debug info emission. */ void @@ -606,6 +731,7 @@ codeview_debug_finish (void) write_strings_table (); write_source_files (); write_line_numbers (); + write_codeview_symbols (); } #endif