Bug 27246 - ld.gold does not support DWARF5
Summary: ld.gold does not support DWARF5
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: gold (show other bugs)
Version: 2.35
: P2 normal
Target Milestone: ---
Assignee: Cary Coutant
URL:
Keywords:
: 27971 (view as bug list)
Depends on:
Blocks:
 
Reported: 2021-01-26 09:42 UTC by Martin Liska
Modified: 2022-06-22 06:29 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2021-01-27 00:00:00


Attachments
asm output (1.29 KB, text/plain)
2021-03-19 18:53 UTC, Khem Raj
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Liska 2021-01-26 09:42:41 UTC
Using GCC tip and binutils 2.36 I see the following issue:

$ cat gets-chk.i
void gets();
int __gets_chk() { gets(); }

$ gcc gets-chk.i  -fPIC -g
../sysdeps/x86_64/start.S:104: error: undefined reference to 'main'
/usr/bin/ld.gold: internal error in read_header_prolog, at ../../gold/dwarf_reader.cc:1678
collect2: error: ld returned 1 exit status
Comment 1 Martin Liska 2021-01-26 09:50:36 UTC
Or there's an assembly snippet:

$ cat a-gets-chk.s
	.file 1 "gets-chk.i"
	.cfi_startproc
	.cfi_endproc
	.section	.debug_info,"",@progbits
	.quad	.Ltext0
	.file 0 "/home/marxin/Programming/testcases" "gets-chk.i"

$ ./gold/ld-new /home/marxin/Programming/testcases/a-gets-chk.o 
header_.max_ops_per_insn:0
./gold/ld-new: internal error in read_header_prolog, at ../../gold/dwarf_reader.cc:1679
Comment 2 H.J. Lu 2021-01-27 18:00:17 UTC
A patch is posted

https://sourceware.org/pipermail/binutils/2021-January/115122.html
Comment 3 Sourceware Commits 2021-01-28 12:26:52 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=0318cca4934fc5d85e83351842985ad2a3475146

commit 0318cca4934fc5d85e83351842985ad2a3475146
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Jan 28 04:21:15 2021 -0800

    gold: Skip address size and segment selector for DWARF5
    
    The .debug_line secton in DWARF5 has a byte for address size and a byte
    for segment selector after DWARF version.  Skip them for DWARF5.
    
            PR gold/27246
            * dwarf_reader.cc (Sized_dwarf_line_info::read_header_prolog):
            Skip address size and segment selector for DWARF5.
Comment 4 Sourceware Commits 2021-01-28 12:40:35 UTC
The binutils-2_36-branch branch has been updated by H.J. Lu <hjl@sourceware.org>:

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

commit 69caaa81ae6296b1f01481874520174abcceac11
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Jan 28 04:21:15 2021 -0800

    gold: Skip address size and segment selector for DWARF5
    
    The .debug_line secton in DWARF5 has a byte for address size and a byte
    for segment selector after DWARF version.  Skip them for DWARF5.
    
            PR gold/27246
            * dwarf_reader.cc (Sized_dwarf_line_info::read_header_prolog):
            Skip address size and segment selector for DWARF5.
    
    (cherry picked from commit 0318cca4934fc5d85e83351842985ad2a3475146)
Comment 5 Sourceware Commits 2021-01-28 12:41:04 UTC
The binutils-2_35-branch branch has been updated by H.J. Lu <hjl@sourceware.org>:

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

commit 756beae66817dcf3794028cb49c8371f4ba54bfa
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Jan 28 04:21:15 2021 -0800

    gold: Skip address size and segment selector for DWARF5
    
    The .debug_line secton in DWARF5 has a byte for address size and a byte
    for segment selector after DWARF version.  Skip them for DWARF5.
    
            PR gold/27246
            * dwarf_reader.cc (Sized_dwarf_line_info::read_header_prolog):
            Skip address size and segment selector for DWARF5.
    
    (cherry picked from commit 0318cca4934fc5d85e83351842985ad2a3475146)
Comment 6 H.J. Lu 2021-01-28 12:58:52 UTC
Th gold crash has been fixed on master and 2.36/2.35 branches.  Since gold
still can't properly parse DWARF5 debug info, I am leaving this bug open.
Comment 7 Martin Liska 2021-01-28 12:59:45 UTC
Thanks for the patch!
Comment 8 Khem Raj 2021-03-18 22:17:33 UTC
I am seeing another failure with latest binutils trunk today, I have collected the objs and libs and a script a.sh with linker cmdline here 

https://uclibc.org/~kraj/gold-bug.tar.xz

% /tmp/obj/gold/ld-new -v
GNU gold (GNU Binutils 2.36.50.20210318) 1.16


% sh a.sh
/tmp/obj/gold/ld-new: internal error in format_file_lineno, at /home/kraj/work/binutils-gdb/gold/dwarf_reader.cc:2278
Comment 9 Khem Raj 2021-03-18 22:29:36 UTC
(In reply to Khem Raj from comment #8)
> I am seeing another failure with latest binutils trunk today, I have
> collected the objs and libs and a script a.sh with linker cmdline here 
> 
> https://uclibc.org/~kraj/gold-bug.tar.xz
> 
> % /tmp/obj/gold/ld-new -v
> GNU gold (GNU Binutils 2.36.50.20210318) 1.16
> 
> 
> % sh a.sh
> /tmp/obj/gold/ld-new: internal error in format_file_lineno, at
> /home/kraj/work/binutils-gdb/gold/dwarf_reader.cc:2278

actually same crash can be triggered with one object

/tmp/obj/gold/ld-new auth.o
Comment 10 Khem Raj 2021-03-18 23:07:13 UTC
further reduced test is

#define overalloc_at(f, l)                                              \
do {                                                                    \
        fatal_exit("ERROR: attempting to allocate too large block at %s:%d", f, l);\
} while (0)

#define overalloc()     overalloc_at(__FILE__, __LINE__)

void foo()
{
  overalloc();
}

% gcc-11.0.1  /tmp/a.c -c -gdwarf-5
% /tmp/obj/gold/ld-new a.o
/tmp/obj/gold/ld-new: internal error in format_file_lineno, at /home/kraj/work/binutils-gdb/gold/dwarf_reader.cc:2278

ICE not seen when -gdwarf-4 is used
Comment 11 Khem Raj 2021-03-18 23:46:47 UTC
(In reply to Khem Raj from comment #10)
> further reduced test is
> 
> #define overalloc_at(f, l)                                              \
> do {                                                                    \
>         fatal_exit("ERROR: attempting to allocate too large block at %s:%d",
> f, l);\
> } while (0)
> 
> #define overalloc()     overalloc_at(__FILE__, __LINE__)
> 
> void foo()
> {
>   overalloc();
> }
> 
> % gcc-11.0.1  /tmp/a.c -c -gdwarf-5
> % /tmp/obj/gold/ld-new a.o
> /tmp/obj/gold/ld-new: internal error in format_file_lineno, at
> /home/kraj/work/binutils-gdb/gold/dwarf_reader.cc:2278
> 
> ICE not seen when -gdwarf-4 is used


I compared the assembly output and difference ( dwarf4 vs dwarf5 ) that matters is 
that when dwarf5 is enabled then it emits this .file directive into .debug_line section which gold does not like

@@ -289,38 +290,40 @@
        .quad   0
        .section        .debug_line,"",@progbits
 .Ldebug_line0:
+       .file 0 "/mnt/b/yoe/master/build/tmp/work/core2-64-yoe-linux/links/2.21-r0/links-2.21" "/tmp/a.c"
        .section        .debug_str,"MS",@progbits,1
Comment 12 Martin Liska 2021-03-19 07:33:22 UTC
What version of gcc-11.0.1 do you use (--version)?
I cannot reproduce your failure with the latest GCC and ld.gold built from source files.
Comment 13 Khem Raj 2021-03-19 08:27:14 UTC
(In reply to Martin Liska from comment #12)
> What version of gcc-11.0.1 do you use (--version)?
> I cannot reproduce your failure with the latest GCC and ld.gold built from
> source files.


% ../recipe-sysroot-native/usr/bin/x86_64-yoe-linux/x86_64-yoe-linux-gcc-11.0.1  -v
Using built-in specs.
COLLECT_GCC=../recipe-sysroot-native/usr/bin/x86_64-yoe-linux/x86_64-yoe-linux-gcc-11.0.1
COLLECT_LTO_WRAPPER=/mnt/b/yoe/master/build/tmp/work/core2-64-yoe-linux/links/2.21-r0/recipe-sysroot-native/usr/bin/x86_64-yoe-linux/../../libexec/x86_64-yoe-linux/gcc/x86_64-yoe-linux/11.0.1/lto-wrapper
Target: x86_64-yoe-linux
Configured with: ../../../../../../work-shared/gcc-11.0.1-r0/gcc-5074c6fa38cef1abb9a355d717b41441a44c4e6a/configure --build=x86_64-linux --host=x86_64-linux --target=x86_64-yoe-linux --prefix=/host-native/usr --exec_prefix=/host-native/usr --bindir=/host-native/usr/bin/x86_64-yoe-linux --sbindir=/host-native/usr/bin/x86_64-yoe-linux --libexecdir=/host-native/usr/libexec/x86_64-yoe-linux --datadir=/host-native/usr/share --sysconfdir=/host-native/etc --sharedstatedir=/host-native/com --localstatedir=/host-native/var --libdir=/host-native/usr/lib/x86_64-yoe-linux --includedir=/host-native/usr/include --oldincludedir=/host-native/usr/include --infodir=/host-native/usr/share/info --mandir=/host-native/usr/share/man --disable-silent-rules --disable-dependency-tracking --with-libtool-sysroot=/host-native --enable-clocale=generic --with-gnu-ld --enable-shared --enable-languages=c,c++ --enable-threads=posix --disable-multilib --enable-default-pie --enable-c99 --enable-long-long --enable-symvers=gnu --enable-libstdcxx-pch --program-prefix=x86_64-yoe-linux- --without-local-prefix --disable-install-libiberty --disable-libssp --enable-libitm --enable-lto --disable-bootstrap --with-system-zlib --with-linker-hash-style=sysv --enable-linker-build-id --with-ppl=no --with-cloog=no --enable-checking=release --enable-cheaders=c_global --without-isl --with-gxx-include-dir=/not/exist/usr/include/c++/11.0.1 --with-sysroot=/not/exist --with-build-sysroot=/host --enable-poison-system-directories=error --with-system-zlib --disable-static --disable-nls --with-glibc-version=2.28 --enable-initfini-array --enable-__cxa_atexit
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.0.1 20210317 (experimental) (GCC)
Comment 14 Martin Liska 2021-03-19 08:38:01 UTC
(In reply to Khem Raj from comment #13)
> (In reply to Martin Liska from comment #12)
> > What version of gcc-11.0.1 do you use (--version)?
> > I cannot reproduce your failure with the latest GCC and ld.gold built from
> > source files.
> 
> 
> %
> ../recipe-sysroot-native/usr/bin/x86_64-yoe-linux/x86_64-yoe-linux-gcc-11.0.
> 1  -v
> Using built-in specs.
> COLLECT_GCC=../recipe-sysroot-native/usr/bin/x86_64-yoe-linux/x86_64-yoe-
> linux-gcc-11.0.1
> COLLECT_LTO_WRAPPER=/mnt/b/yoe/master/build/tmp/work/core2-64-yoe-linux/
> links/2.21-r0/recipe-sysroot-native/usr/bin/x86_64-yoe-linux/../../libexec/
> x86_64-yoe-linux/gcc/x86_64-yoe-linux/11.0.1/lto-wrapper
> Target: x86_64-yoe-linux
> Configured with:
> ../../../../../../work-shared/gcc-11.0.1-r0/gcc-
> 5074c6fa38cef1abb9a355d717b41441a44c4e6a/configure --build=x86_64-linux
> --host=x86_64-linux --target=x86_64-yoe-linux --prefix=/host-native/usr
> --exec_prefix=/host-native/usr
> --bindir=/host-native/usr/bin/x86_64-yoe-linux
> --sbindir=/host-native/usr/bin/x86_64-yoe-linux
> --libexecdir=/host-native/usr/libexec/x86_64-yoe-linux
> --datadir=/host-native/usr/share --sysconfdir=/host-native/etc
> --sharedstatedir=/host-native/com --localstatedir=/host-native/var
> --libdir=/host-native/usr/lib/x86_64-yoe-linux
> --includedir=/host-native/usr/include
> --oldincludedir=/host-native/usr/include
> --infodir=/host-native/usr/share/info --mandir=/host-native/usr/share/man
> --disable-silent-rules --disable-dependency-tracking
> --with-libtool-sysroot=/host-native --enable-clocale=generic --with-gnu-ld
> --enable-shared --enable-languages=c,c++ --enable-threads=posix
> --disable-multilib --enable-default-pie --enable-c99 --enable-long-long
> --enable-symvers=gnu --enable-libstdcxx-pch
> --program-prefix=x86_64-yoe-linux- --without-local-prefix
> --disable-install-libiberty --disable-libssp --enable-libitm --enable-lto
> --disable-bootstrap --with-system-zlib --with-linker-hash-style=sysv
> --enable-linker-build-id --with-ppl=no --with-cloog=no
> --enable-checking=release --enable-cheaders=c_global --without-isl
> --with-gxx-include-dir=/not/exist/usr/include/c++/11.0.1
> --with-sysroot=/not/exist --with-build-sysroot=/host
> --enable-poison-system-directories=error --with-system-zlib --disable-static
> --disable-nls --with-glibc-version=2.28 --enable-initfini-array
> --enable-__cxa_atexit
> Thread model: posix
> Supported LTO compression algorithms: zlib zstd
> gcc version 11.0.1 20210317 (experimental) (GCC)

That's fine. Can you please attach the full .s file you feed to ld.gold?
Comment 15 Khem Raj 2021-03-19 18:52:57 UTC
here is revised testcase

#include <stdio.h>
#include <stdlib.h>
#define overalloc_at(f, l)                                              \
do {                                                                    \
        printf("ERROR: attempting to allocate too large block at %s:%d", f, l);\
        exit(1);\
} while (0)

#define overalloc()     overalloc_at(__FILE__, __LINE__)

void main()
{
  overalloc();
}



I am attaching the .s file as well.
Comment 16 Khem Raj 2021-03-19 18:53:20 UTC
Created attachment 13321 [details]
asm output
Comment 17 Sourceware Commits 2021-03-19 20:56:18 UTC
The master branch has been updated by Cary Coutant <ccoutant@sourceware.org>:

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

commit 5cde809b7b9da3ad3aa0d65f0e5e92ab199d64f0
Author: Cary Coutant <ccoutant@gmail.com>
Date:   Wed Mar 17 21:31:15 2021 -0700

    Add DWARF 5 support in gold.
    
    elfcpp/
            PR gold/27246
            * dwarf.h (enum DW_LNCT): Add line number table content type codes.
            (enum DW_LINE_OPS): Reformat.
            (enum DW_LINE_EXTENDED_OPS): Reformat.
            (enum DW_CHILDREN): Reformat.
            (enum DW_RLE): Add range list entry types.
            (enum DW_SECT): Update values for DWARF 5.
    
    gold/
            PR gold/27246
            * dwarf_reader.cc (Dwarf_abbrev_table::do_get_abbrev): Handle
            DW_FORM_implicit_const.
            (Dwarf_ranges_table::read_ranges_table): Add version parameter;
            Adjust all callers.  Look for .debug_rnglists section if DWARF 5.
            (Dwarf_ranges_table::read_range_list_v5): New method.
            (Dwarf_die::read_attributes): Handle new DWARF 5 DW_FORM codes.
            (Dwarf_die::skip_attributes): Likewise.
            (Dwarf_info_reader::do_parse): Support DWARF 5 unit header format.
            (Dwarf_info_reader::read_3bytes_from_pointer): New method.
            (Sized_dwarf_line_info::Sized_dwarf_line_info): Initialize
            str_buffer_, str_buffer_start, reloc_map_, line_number_map_.
            Look for .debug_line_str section.
            (Sized_dwarf_line_info::read_header_prolog): Support DWARF 5 prolog.
            (Sized_dwarf_line_info::read_header_tables): Rename to...
            (Sized_dwarf_line_info::read_header_tables_v2): ... this.
            (Sized_dwarf_line_info::read_header_tables_v5): New method.
            (Sized_dwarf_line_info::process_one_opcode): Insert missing "this->".
            Change advance_line to signed int64_t.
            (Sized_dwarf_line_info::read_lines): Add endptr parameter; adjust
            callers.  Insert missing "this->".
            (Sized_dwarf_line_info::read_line_mappings): Support DWARF 5.
            (Sized_dwarf_line_info::do_addr2line): Add debug code.
            * dwarf_reader.h (Dwarf_abbrev_table::Attribute): Add implicit_const
            field. Adjust constructor.
            (Dwarf_abbrev_table::add_sttribute): Add implicit_const parameter.
            (Dwarf_ranges_table::read_ranges_table): Add version parameter.
            (Dwarf_ranges_table::read_range_list_v5): New method.
            (Dwarf_die): Remove unused attr_off field.
            (Dwarf_info_reader::Dwarf_info_reader): Initialize unit_type_ field.
            (Dwarf_info_reader::is_type_unit): New method.
            (Dwarf_info_reader::read_3bytes_from_pointer): New method.
            (Dwarf_info_reader::read_range_list): Call read_range_list_v5 for
            DWARF 5 range lists.
            (Dwarf_info_reader::is_type_unit_): Remove.
            (Dwarf_info_reader::unit_type_): New field.
            (Sized_dwarf_line_info::~Sized_dwarf_line_info): Delete
            str_buffer_start_.
            (Sized_dwarf_line_info::read_header_tables): Rename to...
            (Sized_dwarf_line_info::read_header_tables_v2): ... this.
            (Sized_dwarf_line_info::read_header_tables_v5): New method.
            (Sized_dwarf_line_info::read_lines): Add endptr parameter.
            (Sized_dwarf_line_info::Dwarf_line_infoHeader): Add address_size field.
            (Sized_dwarf_line_info::str_buffer_): New field.
            (Sized_dwarf_line_info::str_buffer_end_): New field.
            (Sized_dwarf_line_info::str_buffer_start_): New field.
            (Sized_dwarf_line_info::end_of_header_length_): New field.
            (Sized_dwarf_line_info::end_of_unit_): New field.
Comment 18 Cary Coutant 2021-03-19 21:05:26 UTC
Fixed on trunk.
Comment 19 Khem Raj 2021-03-19 22:20:30 UTC
it fixed my problem as well.
Comment 20 H.J. Lu 2021-06-10 12:43:56 UTC
*** Bug 27971 has been marked as a duplicate of this bug. ***
Comment 21 Allen 2022-04-11 03:30:55 UTC
If the ld.gold, which install on my system default, don't merge such patch, is there any option can avoid such bug? 

eg: -gdwarf-2
Comment 22 Martin Liska 2022-04-11 06:46:13 UTC
(In reply to Allen from comment #21)
> If the ld.gold, which install on my system default, don't merge such patch,
> is there any option can avoid such bug? 
> 
> eg: -gdwarf-2

Yes, you can compile all objects that are linked e.g. with -gdwarf-4.