From patchwork Mon Oct 17 18:00:00 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ximin Luo X-Patchwork-Id: 77791 Delivered-To: patch@linaro.org Received: by 10.140.97.247 with SMTP id m110csp511179qge; Mon, 17 Oct 2016 11:01:00 -0700 (PDT) X-Received: by 10.98.93.25 with SMTP id r25mr39325523pfb.136.1476727260078; Mon, 17 Oct 2016 11:01:00 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id ym6si23353218pac.278.2016.10.17.11.00.59 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 Oct 2016 11:01:00 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-438819-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-438819-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-438819-patch=linaro.org@gcc.gnu.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=E06qebWpCrkwQym2PhvXyt4lIS80MFrOhPChsjPKbrQ8cd/W7M Z1i+yHioucH8FvaP5ZG/U1eJV/0MDe0Y5mjEMWCA5jV9pMFqyptRgtVGF9Euz/PJ 0Liplg8VxHPoOAF4Pb/VDf18ap6HlseO3K5EWk3fovDCuI/AQgHwu9wCU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=Jv0b7f/zX7TYY8lY1VIDRA4zNyg=; b=AECzl2+YbEFWeydK3h35 WlA5U9jV54PtEqikADgeIOx3xryLR4+ODE+gOcrbMXRTJCO9IcNkPrgrloyZrRF6 nM6NKzn1TuWp66X1BJrW31QYBkyDt3ZLZKgt+X/SDTRTLx7kVWnM1wrGbY6TTV1P 8xzgxev9WF2ApZNaGsbL8mM= Received: (qmail 120107 invoked by alias); 17 Oct 2016 18:00:42 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 120079 invoked by uid 89); 17 Oct 2016 18:00:41 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.1 required=5.0 tests=BAYES_00, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=no version=3.3.2 spammy=H*r:sk:mail.he, Hx-spam-relays-external:sk:mail.he, H*RU:sk:mail.he, HX-HELO:sk:mail.he X-HELO: mail.headstrong.de Received: from mail.headstrong.de (HELO mail.headstrong.de) (81.7.4.112) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 17 Oct 2016 18:00:31 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by mail.headstrong.de (Postfix) with ESMTP id B04AD1C003CA for ; Mon, 17 Oct 2016 20:00:28 +0200 (CEST) Received: from mail.headstrong.de ([127.0.0.1]) by localhost (mail.headstrong.de [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 0lFY_QqZwr1e for ; Mon, 17 Oct 2016 20:00:27 +0200 (CEST) To: gcc-patches@gcc.gnu.org From: Ximin Luo Subject: [PATCH] PR77895: DWARF: Emit DW_AT_comp_dir in all cases, even if source is an absolute path Message-ID: <85c55bf6-98ed-0277-f035-beaf469bb5bc@pwned.gg> Date: Mon, 17 Oct 2016 18:00:00 +0000 MIME-Version: 1.0 (Please keep me on CC, I am not subscribed) In working on another patch for DW_AT_comp_dir behaviour, I tried to write a test for that other patch. This test did not work as expected - DW_AT_comp_dir was not even generated in the resulting output! But this had nothing to do with my patch so I dug a bit deeper. It turns out that GCC does not emit DW_AT_comp_dir if the source path given is an absolute path, and the file being compiled contains a typedef to a compiler builtin (sorry, I don't know the terminology here very well). This typedef exists in the vast majority of cases in the real world via standard library includes, so from the outside, the behaviour of GCC "looks like" it emits DW_AT_comp_dir in all/most cases. >From looking at the source code dwarf2out.c, Richard Biener determined that the current code is written to "intend to" not emit DW_AT_comp_dir if to path is absolute, regardless of the typedef. It is possible to "fix" this bug by a 1-line change, to conform to the way currently "intended" by GCC code. However, I think a much better fix is a 22-line deletion of code from dwarf2out.c, to instead emit DW_AT_comp_dir in all cases. The original aim of not emitting DW_AT_comp_dir seems to be to avoid emitting redundant information. However, this information is *not* redundant! The DWARF2 spec says: "A DW_AT_comp_dir attribute whose value is a null-terminated string containing the current working directory of the compilation command that produced this compilation unit in whatever form makes sense for the host system." Conceptually, the working directory is independent from an input file, so it would *not* be redundant to emit it in the general case. In future versions of DWARF, other information might be added (such as relative -I flags, etc) that are dependent on knowing the working directory. The logic of "don't emit DW_AT_comp_dir if DW_AT_name is absolute" would then break. In other words, the choice to avoid emitting DW_AT_comp_dir is a brittle and premature optimisation that loses information in the general case. Therefore, it is better to emit it in all circumstances, in case the reader needs to know what the working directory was at compile-time. *Sometimes*, *parts* of it might be redundant yes - and rewriting DW_AT_name to be relative to this, would remove the redundancy. Doing this is compatible with the above points, and I could amend the patch to do this - although I suggest it's not worth the effort, unless there is a function in GCC code that already does this. Some more minor advantages are: - We delete 21 lines, this reduces complexity in the already-complex code. We can also get rid of the Darwin-specific workaround described here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53453 (Not currently part of my patch, but I can add this in.) - The GCC test suite compiles tests via their absolute paths, and many many other buildsystems do this too. This was in fact my original problem - I was trying to test something involving DW_AT_comp_dir, but GCC does not emit this in the testsuite! Rather than saying "this is a bug, everyone else should spend effort fixing this", it is better to fix it in one place, i.e. the source of where it is (not) generated. Thanks for your time, Ximin -- GPG: ed25519/56034877E1F87C35 GPG: rsa4096/1318EFAC5FBBDBCE https://github.com/infinity0/pubkeys.git Index: gcc-7-20161009/gcc/dwarf2out.c =================================================================== --- gcc-7-20161009.orig/gcc/dwarf2out.c +++ gcc-7-20161009/gcc/dwarf2out.c @@ -21994,7 +21994,7 @@ gen_compile_unit_die (const char *filena { add_name_attribute (die, filename); /* Don't add cwd for . */ - if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<') + if (filename[0] != '<') add_comp_dir_attribute (die); } @@ -26332,20 +26332,6 @@ prune_unused_types (void) prune_unmark_dies (ctnode->root_die); } -/* Set the parameter to true if there are any relative pathnames in - the file table. */ -int -file_table_relative_p (dwarf_file_data **slot, bool *p) -{ - struct dwarf_file_data *d = *slot; - if (!IS_ABSOLUTE_PATH (d->filename)) - { - *p = true; - return 0; - } - return 1; -} - /* Helpers to manipulate hash table of comdat type units. */ struct comdat_type_hasher : nofree_ptr_hash @@ -28152,15 +28138,7 @@ dwarf2out_early_finish (const char *file /* Add the name for the main input file now. We delayed this from dwarf2out_init to avoid complications with PCH. */ add_name_attribute (comp_unit_die (), remap_debug_filename (filename)); - if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir) - add_comp_dir_attribute (comp_unit_die ()); - else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL) - { - bool p = false; - file_table->traverse (&p); - if (p) - add_comp_dir_attribute (comp_unit_die ()); - } + add_comp_dir_attribute (comp_unit_die ()); /* With LTO early dwarf was really finished at compile-time, so make sure to adjust the phase after annotating the LTRANS CU DIE. */ Index: gcc-7-20161009/gcc/testsuite/gcc.dg/debug/dwarf2/pr77985.c =================================================================== --- /dev/null +++ gcc-7-20161009/gcc/testsuite/gcc.dg/debug/dwarf2/pr77985.c @@ -0,0 +1,9 @@ +/* DW_AT_comp_dir is emitted even if the file is compiled via an absolute path, + as is the case in the gcc testsuite. */ +/* { dg-do compile } */ +/* { dg-options "-g -dA" } */ +/* { dg-final { scan-assembler-dem "DW_AT_comp_dir:" } } */ + +void func (void) +{ +}