Bug 26740 - Extra filename generated by --gdwarf-4
Summary: Extra filename generated by --gdwarf-4
Status: RESOLVED DUPLICATE of bug 25878
Alias: None
Product: binutils
Classification: Unclassified
Component: gas (show other bugs)
Version: 2.36
: P2 normal
Target Milestone: 2.36
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-10-15 20:46 UTC by H.J. Lu
Modified: 2020-11-16 19:53 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2020-10-15 20:46:57 UTC
From

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97451

[hjl@gnu-skx-1 stage1-gcc]$ cat /tmp/x.s
	.file	"lbasename.c"
	.text
	.p2align 4
	.globl	unix_lbasename
	.type	unix_lbasename, @function
unix_lbasename:
	ret
	.p2align 4
	.globl	dos_lbasename
	.type	dos_lbasename, @function
dos_lbasename:
	.file 1 "/export/gnu/import/git/sources/gcc/libiberty/lbasename.c"
	ret
	.size	dos_lbasename, .-dos_lbasename
	.file 2 "/export/gnu/import/git/sources/gcc/libiberty/../include/safe-ctype.h"
[hjl@gnu-skx-1 stage1-gcc]$ as --gdwarf-4 --64 -o x.o /tmp/x.s
[hjl@gnu-skx-1 stage1-gcc]$ readelf -w x.o | grep -A4 File
 The File Name Table (offset 0x87):
  Entry	Dir	Time	Size	Name
  1	2	0	0	lbasename.c
  2	3	0	0	safe-ctype.h
  3	1	0	0	x.s <<< This shouldn't be there.
[hjl@gnu-skx-1 stage1-gcc]$
Comment 1 Mark Wielaard 2020-10-15 22:35:16 UTC
The issue is that some instructions are emitted (dwarf2_emit_insn is called) before the first .file <NUMBER> directive has been seen. This allocates x.s (the input file) as first file entry. Later when the actual .file 1 lbasename.c and .file 2 safe-ctype are seen the x.s entry is moved to entry 3. We don't record that we never actually use that entry and so we still emit it.
Comment 2 H.J. Lu 2020-10-15 22:49:01 UTC
(In reply to Mark Wielaard from comment #1)
> The issue is that some instructions are emitted (dwarf2_emit_insn is called)
> before the first .file <NUMBER> directive has been seen. This allocates x.s
> (the input file) as first file entry. Later when the actual .file 1
> lbasename.c and .file 2 safe-ctype are seen the x.s entry is moved to entry
> 3. We don't record that we never actually use that entry and so we still
> emit it.

Is this a GCC bug? Even if it is, assembler shouldn't generate bad debug
info.
Comment 3 Mark Wielaard 2020-10-15 23:06:17 UTC
(In reply to H.J. Lu from comment #2)
> (In reply to Mark Wielaard from comment #1)
> > The issue is that some instructions are emitted (dwarf2_emit_insn is called)
> > before the first .file <NUMBER> directive has been seen. This allocates x.s
> > (the input file) as first file entry. Later when the actual .file 1
> > lbasename.c and .file 2 safe-ctype are seen the x.s entry is moved to entry
> > 3. We don't record that we never actually use that entry and so we still
> > emit it.
> 
> Is this a GCC bug? Even if it is, assembler shouldn't generate bad debug
> info.

I don't think so. Although it might help if gcc emitted the .file 1 directive earlier. The debug info isn't really bad, but it does contain an unused file name which it really shouldn't. I am not yet sure how to fix that. 

In gas/dwarf2dbg.c you see that the file entry is moved when a .file NUM is seen and the file entry is auto_assigned. I wonder if we should just replace it instead of moving if dwarf2_any_loc_directive_seen is false (because then we know it has never been used).
Comment 4 Mark Wielaard 2020-10-15 23:13:00 UTC
Note that the auto_assigned moving of file entries was done because of PR25878. Maybe Nick knows why the entries needed to be moved instead of simply being reassigned.
Comment 5 H.J. Lu 2020-10-16 01:19:40 UTC
Dup.

*** This bug has been marked as a duplicate of bug 25878 ***
Comment 6 Sourceware Commits 2020-10-16 11:08:24 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=6915020bb134ae29fd772295c66fd67b5944962d

commit 6915020bb134ae29fd772295c66fd67b5944962d
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Oct 16 04:03:20 2020 -0700

    gas: Reuse the input file entry in the file table
    
    Some instructions can be emitted (dwarf2_emit_insn is called) before the
    first .file <NUMBER> directive has been seen, which allocates the input
    file as the first file entry.  Reuse the input file entry in the file
    table.
    
            PR gas/25878
            PR gas/26740
            * dwarf2dbg.c (file_entry): Remove auto_assigned.
            (assign_file_to_slot): Remove the auto_assign argument.
            (allocate_filenum): Updated.
            (allocate_filename_to_slot): Reuse the input file entry in the
            file table.
            (dwarf2_where): Replace as_where with as_where_physical.
            * testsuite/gas/i386/dwarf5-line-1.d: New file.
            * testsuite/gas/i386/dwarf5-line-1.s: Likewise.
            * testsuite/gas/i386/i386.exp: Run dwarf5-line-1.
Comment 7 Sourceware Commits 2020-10-17 11:27:10 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=bd0c565edbf4ba8121fded38e389530d7fa6f963

commit bd0c565edbf4ba8121fded38e389530d7fa6f963
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sat Oct 17 04:24:22 2020 -0700

    gas: Always use as_where for preprocessed assembly codes
    
    Always clear the slot 1 if it was assigned to the input file before the
    first .file <NUMBER> directive has been seen.  Always use as_where to
    generate the correct debug infor for preprocessed assembly codes.
    
            PR gas/25878
            PR gas/26740
            * dwarf2dbg.c (allocate_filename_to_slot): Don't reuse the slot 1
            here.
            (dwarf2_where): Restore as_where.
            (dwarf2_directive_filename): Clear the slot 1 if it was assigned
            to the input file.
            * testsuite/gas/i386/dwarf5-line-2.d: New file.
            * testsuite/gas/i386/dwarf5-line-2.s: Likewise.
            * testsuite/gas/i386/dwarf5-line-3.d: Likewise.
            * testsuite/gas/i386/dwarf5-line-3.s: Likewise.
            * testsuite/gas/i386/i386.exp: Run dwarf5-line-2 and
            dwarf5-line-3.
Comment 8 Sourceware Commits 2020-10-17 12:36:20 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=7ffdc9ae6ed88601e7f422ab117fe3abeef110dc

commit 7ffdc9ae6ed88601e7f422ab117fe3abeef110dc
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sat Oct 17 05:35:23 2020 -0700

    gas: Replace dwarf5-line-2.S with dwarf5-line-3.S
    
            PR gas/25878
            PR gas/26740
            * testsuite/gas/i386/dwarf5-line-3.s: Replace dwarf5-line-2.S
            with dwarf5-line-3.S.
            * testsuite/gas/i386/dwarf5-line-3.d: Updated.
Comment 9 Sourceware Commits 2020-10-17 12:53:51 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=9717970a4e374218fe74c99cf5bc7f1d45adbf0e

commit 9717970a4e374218fe74c99cf5bc7f1d45adbf0e
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sat Oct 17 05:52:13 2020 -0700

    gas: Add a -gdwarf-5 debug_line test with .s file
    
            PR gas/25878
            PR gas/26740
            * testsuite/gas/i386/dwarf5-line-4.d: New file.
            * testsuite/gas/i386/dwarf5-line-4.s: Likewise.
            * testsuite/gas/i386/i386.exp: Run dwarf5-line-4.
Comment 10 Sourceware Commits 2020-11-16 19:53:54 UTC
The binutils-2_35-branch branch has been updated by Mark Wielaard <mark@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=ea873d892ebfae612aed5d308061b083b3f06636

commit ea873d892ebfae612aed5d308061b083b3f06636
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Oct 16 04:03:20 2020 -0700

    gas: Reuse the input file entry in the file table
    
    Some instructions can be emitted (dwarf2_emit_insn is called) before the
    first .file <NUMBER> directive has been seen, which allocates the input
    file as the first file entry.  Reuse the input file entry in the file
    table.
    
            PR gas/25878
            PR gas/26740
            * dwarf2dbg.c (file_entry): Remove auto_assigned.
            (assign_file_to_slot): Remove the auto_assign argument.
            (allocate_filenum): Updated.
            (allocate_filename_to_slot): Reuse the input file entry in the
            file table.
            (dwarf2_where): Replace as_where with as_where_physical.
            * testsuite/gas/i386/dwarf5-line-1.d: New file.
            * testsuite/gas/i386/dwarf5-line-1.s: Likewise.
            * testsuite/gas/i386/i386.exp: Run dwarf5-line-1.
    
    (cherry picked from commit 6915020bb134ae29fd772295c66fd67b5944962d)
    
    gas: Always use as_where for preprocessed assembly codes
    
    Always clear the slot 1 if it was assigned to the input file before the
    first .file <NUMBER> directive has been seen.  Always use as_where to
    generate the correct debug infor for preprocessed assembly codes.
    
            PR gas/25878
            PR gas/26740
            * dwarf2dbg.c (allocate_filename_to_slot): Don't reuse the slot 1
            here.
            (dwarf2_where): Restore as_where.
            (dwarf2_directive_filename): Clear the slot 1 if it was assigned
            to the input file.
            * testsuite/gas/i386/dwarf5-line-2.d: New file.
            * testsuite/gas/i386/dwarf5-line-2.s: Likewise.
            * testsuite/gas/i386/dwarf5-line-3.d: Likewise.
            * testsuite/gas/i386/dwarf5-line-3.s: Likewise.
            * testsuite/gas/i386/i386.exp: Run dwarf5-line-2 and
            dwarf5-line-3.
    
    (cherry picked from commit bd0c565edbf4ba8121fded38e389530d7fa6f963)
    
    gas: Clear all auto-assigned file slots
    
    Since a file slot is auto-assigned for the #APP marker appeared before
    the first .file <NUMBER> directive has been seen, clear all auto-assigned
    file slots when seeing the first .file <NUMBER> directive.
    
            PR gas/26778
            * * dwarf2dbg.c (num_of_auto_assigned): New.
            (allocate_filenum): Increment num_of_auto_assigned.
            (dwarf2_directive_filename): Clear the slots auto-assigned
            before the first .file <NUMBER> directive was seen.
            * testsuite/gas/i386/dwarf4-line-1.d: New file.
            * testsuite/gas/i386/dwarf4-line-1.s: Likewise.
            * testsuite/gas/i386/i386.exp: Run dwarf4-line-1.
    
    (cherry picked from commit ae9d2233e61a98ff8dba56be10219aa5306ffc9a)