Bug 19526 - Using "gcc -o /dev/null" gives "ld: final link failed: File truncated" error
Summary: Using "gcc -o /dev/null" gives "ld: final link failed: File truncated" error
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL: https://trac.macports.org/ticket/45994
Keywords:
Depends on:
Blocks:
 
Reported: 2016-01-27 20:51 IST by Helge Deller
Modified: 2016-01-31 17:06 IST (History)
3 users (show)

See Also:
Host: hppa-unknown-linux-gnu
Target: hppa-unknown-linux-gnu
Build:
Last reconfirmed:


Attachments
hppa-sort-unwind.d.txt (857 bytes, text/plain)
2016-01-30 02:52 IST, dave.anglin
Details
ldlang.c.d.txt (392 bytes, text/plain)
2016-01-30 21:10 IST, dave.anglin
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Helge Deller 2016-01-27 20:51:27 IST
When running this trivial check if a library (here: libm.so) exists on the hppa platform I get an error:

echo 'int main(){}' | gcc -v -o /dev/null -x c - -lm ; echo $?

/usr/bin/ld: final link failed: File truncated
collect2: error: ld returned 1 exit status

This error does not happen on e.g, the x86_64 platform.

This kind of test is used in various programs, e.g. in "pymol" (Log: https://buildd.debian.org/status/fetch.php?pkg=pymol&arch=hppa&ver=1.7.2.1-2.2&stamp=1453925483).
 
The full log is:

root@sibaris:~# echo 'int main(){}' | gcc -v -o /dev/null -x c - -lm ; echo $?
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/hppa-linux-gnu/5/lto-wrapper
Target: hppa-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 5.3.1-7' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libssp --disable-libitm --disable-libsanitizer --disable-libquadmath --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-hppa/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-hppa --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-hppa --with-arch-directory=parisc --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-libstdcxx-pch --enable-checking=release --build=hppa-linux-gnu --host=hppa-linux-gnu --target=hppa-linux-gnu
Thread model: posix
gcc version 5.3.1 20160121 (Debian 5.3.1-7) 
COLLECT_GCC_OPTIONS='-v' '-o' '/dev/null'
 /usr/lib/gcc/hppa-linux-gnu/5/cc1 -quiet -v -imultilib . -imultiarch hppa-linux-gnu - -quiet -dumpbase - -auxbase - -version -o /tmp/ccnsiCwf.s
GNU C11 (Debian 5.3.1-7) version 5.3.1 20160121 (hppa-linux-gnu)
        compiled by GNU C version 5.3.1 20160121, GMP version 6.1.0, MPFR version 3.1.3-p5, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/hppa-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/hppa-linux-gnu/5/../../../../hppa-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/hppa-linux-gnu/5/include
 /usr/local/include
 /usr/lib/gcc/hppa-linux-gnu/5/include-fixed
 /usr/include/hppa-linux-gnu
 /usr/include
End of search list.
GNU C11 (Debian 5.3.1-7) version 5.3.1 20160121 (hppa-linux-gnu)
        compiled by GNU C version 5.3.1 20160121, GMP version 6.1.0, MPFR version 3.1.3-p5, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 26054780b51578997fdc10bfd741a0f3
COLLECT_GCC_OPTIONS='-v' '-o' '/dev/null'
 as -v -o /tmp/ccuV3ojv.o /tmp/ccnsiCwf.s
GNU assembler version 2.25.90 (hppa-linux-gnu) using BFD version (GNU Binutils for Debian) 2.25.90.20160101
COMPILER_PATH=/usr/lib/gcc/hppa-linux-gnu/5/:/usr/lib/gcc/hppa-linux-gnu/5/:/usr/lib/gcc/hppa-linux-gnu/:/usr/lib/gcc/hppa-linux-gnu/5/:/usr/lib/gcc/hppa-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/hppa-linux-gnu/5/:/usr/lib/gcc/hppa-linux-gnu/5/../../../hppa-linux-gnu/:/usr/lib/gcc/hppa-linux-gnu/5/../../../:/lib/hppa-linux-gnu/:/lib/:/usr/lib/hppa-linux-gnu/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' '/dev/null'
 /usr/lib/gcc/hppa-linux-gnu/5/collect2 -plugin /usr/lib/gcc/hppa-linux-gnu/5/liblto_plugin.so -plugin-opt=/usr/lib/gcc/hppa-linux-gnu/5/lto-wrapper -plugin-opt=-fresolution=/tmp/ccrTnsON.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --sysroot=/ --build-id --eh-frame-hdr -dynamic-linker /lib/ld.so.1 -o /dev/null /usr/lib/gcc/hppa-linux-gnu/5/../../../hppa-linux-gnu/crt1.o /usr/lib/gcc/hppa-linux-gnu/5/../../../hppa-linux-gnu/crti.o /usr/lib/gcc/hppa-linux-gnu/5/crtbegin.o -L/usr/lib/gcc/hppa-linux-gnu/5 -L/usr/lib/gcc/hppa-linux-gnu/5/../../../hppa-linux-gnu -L/usr/lib/gcc/hppa-linux-gnu/5/../../.. -L/lib/hppa-linux-gnu -L/usr/lib/hppa-linux-gnu /tmp/ccuV3ojv.o -lm -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/hppa-linux-gnu/5/crtend.o /usr/lib/gcc/hppa-linux-gnu/5/../../../hppa-linux-gnu/crtn.o
/usr/bin/ld: final link failed: File truncated
collect2: error: ld returned 1 exit status
1
Comment 1 Helge Deller 2016-01-29 21:17:21 IST
The debian "libbsd" package fails because of the same issue:

gcc -isystem ../include/bsd -I.. -include ../config.h -DLIBBSD_DISABLE_DEPRECATED -D__REENTRANT -DLIBBSD_OVERLAY headers-overlay-gen.c -o /dev/null
/usr/bin/ld: final link failed: File truncated
collect2: error: ld returned 1 exit status
FAIL headers-overlay.sh (exit status: 1)

Full log:
https://buildd.debian.org/status/fetch.php?pkg=libbsd&arch=hppa&ver=0.8.2-1&stamp=1453935236
Comment 2 dave.anglin 2016-01-29 22:44:17 IST
On 2016-01-29 4:17 PM, deller at gmx dot de wrote:
> https://sourceware.org/bugzilla/show_bug.cgi?id=19526
>
> --- Comment #1 from Helge Deller <deller at gmx dot de> ---
> The debian "libbsd" package fails because of the same issue:
>
> gcc -isystem ../include/bsd -I.. -include ../config.h
> -DLIBBSD_DISABLE_DEPRECATED -D__REENTRANT -DLIBBSD_OVERLAY
> headers-overlay-gen.c -o /dev/null
> /usr/bin/ld: final link failed: File truncated
> collect2: error: ld returned 1 exit status
> FAIL headers-overlay.sh (exit status: 1)
>
> Full log:
> https://buildd.debian.org/status/fetch.php?pkg=libbsd&arch=hppa&ver=0.8.2-1&stamp=1453935236
>
I have built a debug version of binutils.  I'll see if I can find 
problem this weekend.

Dave
Comment 3 dave.anglin 2016-01-30 02:06:13 IST
It's not necessary to use a pipe to duplicate.  I created a file xxx.c with 'int main(){}' and
compiled with -save-temps and -Wl,-debug to get the actual link command.  Here is backtrace
for bfd_error_file_truncated:

Breakpoint 4, bfd_set_error (error_tag=724328) at ../../src/bfd/bfd.c:511
511	      va_start (ap, error_tag);
(gdb) step
503	{
(gdb) 
504	  bfd_error = error_tag;
(gdb) 
505	  if (error_tag == bfd_error_on_input)
(gdb) p bfd_error
$3 = bfd_error_file_truncated
(gdb) bt
#0  bfd_set_error (error_tag=bfd_error_file_truncated)
    at ../../src/bfd/bfd.c:505
#1  0xfd1ade30 in cache_bread_1 (abfd=0xaecb0, buf=0x257978, nbytes=256)
    at ../../src/bfd/cache.c:347
#2  0xfd1adf60 in cache_bread (abfd=0xaecb0, buf=0x257978, nbytes=256)
    at ../../src/bfd/cache.c:368
#3  0xfd1ac17c in bfd_bread (ptr=0x257978, size=256, abfd=0xaecb0)
    at ../../src/bfd/bfdio.c:196
#4  0xfd1b6000 in _bfd_generic_get_section_contents (abfd=0xaecb0, 
    section=0xb0850, location=0x257978, offset=0, count=256)
    at ../../src/bfd/libbfd.c:812
#5  0xfd1ca458 in bfd_get_section_contents (abfd=0xaecb0, section=0xb0850, 
    location=0x257978, offset=0, count=256) at ../../src/bfd/section.c:1619
#6  0xfd1af8c4 in bfd_get_full_section_contents (abfd=0xaecb0, sec=0xb0850, 
    ptr=0xfd7034d0) at ../../src/bfd/compress.c:253
#7  0xfd1ca4d8 in bfd_malloc_and_get_section (abfd=0xaecb0, sec=0xb0850, 
    buf=0xfd7034d0) at ../../src/bfd/section.c:1640
#8  0xfd1ddb4c in elf_hppa_sort_unwind (abfd=0xaecb0)
    at ../../src/bfd/elf-hppa.h:1196
#9  0xfd1e6030 in elf32_hppa_final_link (abfd=0xaecb0, 
    info=0x9f5b0 <link_info>) at ../../src/bfd/elf32-hppa.c:3257
#10 0x00049010 in ldwrite () at ../../src/ld/ldwrite.c:581
#11 0x00043bd8 in main (argc=38, argv=0xfd70302c) at ../../src/ld/ldmain.c:430
(gdb) frame 8
#8  0xfd1ddb4c in elf_hppa_sort_unwind (abfd=0xaecb0)
    at ../../src/bfd/elf-hppa.h:1196
1196	      if (!bfd_malloc_and_get_section (abfd, s, &contents))
(gdb) p *abfd
$4 = {filename = 0xb0d58 "/dev/null", 
  xvec = 0xfd2d07b8 <hppa_elf32_linux_vec>, iostream = 0xb0d68, 
  iovec = 0xfd2c90a0 <cache_iovec>, lru_prev = 0xc5738, lru_next = 0x118568, 
  where = 2228, mtime = 0, id = 0, format = bfd_object, 
  direction = write_direction, flags = 386, cacheable = 1, 
  target_defaulted = 0, opened_once = 1, mtime_set = 0, no_export = 0, 
  output_has_begun = 1, has_armap = 0, is_thin_archive = 0, 
  selective_search = 0, is_linker_output = 1, is_linker_input = 0, 
  plugin_format = bfd_plugin_uknown, lto_output = 0, plugin_dummy_bfd = 0x0, 
  origin = 0, proxy_origin = 0, section_htab = {table = 0x232490, 
    newfunc = @0xfd42031e: 0xfd1c8b08 <bfd_section_hash_newfunc>, 
    memory = 0xafd60, size = 61, count = 31, entsize = 184, frozen = 0}, 
  sections = 0xafdc0, section_last = 0x232878, section_count = 27, 
  archive_pass = 0, start_address = 66496, outsymbols = 0x0, symcount = 93, 
  dynsymcount = 0, arch_info = 0xfd2d1140 <bfd_hppa_arch>, arelt_data = 0x0, 
  my_archive = 0x0, archive_next = 0x0, archive_head = 0x0, 
  nested_archives = 0x0, link = {next = 0xb0ed0, hash = 0xb0ed0}, tdata = {
    aout_data = 0xaed80, aout_ar_data = 0xaed80, oasys_obj_data = 0xaed80, 
    oasys_ar_data = 0xaed80, coff_obj_data = 0xaed80, pe_obj_data = 0xaed80, 
    xcoff_obj_data = 0xaed80, ecoff_obj_data = 0xaed80, ieee_data = 0xaed80, 
    ieee_ar_data = 0xaed80, srec_data = 0xaed80, verilog_data = 0xaed80, 
    ihex_data = 0xaed80, tekhex_data = 0xaed80, elf_obj_data = 0xaed80, 
    nlm_obj_data = 0xaed80, bout_data = 0xaed80, mmo_data = 0xaed80, 
    sun_core_data = 0xaed80, sco5_core_data = 0xaed80, 
    trad_core_data = 0xaed80, som_data = 0xaed80, hpux_core_data = 0xaed80, 
    hppabsd_core_data = 0xaed80, sgi_core_data = 0xaed80, 
    lynx_core_data = 0xaed80, osf_core_data = 0xaed80, 
    cisco_core_data = 0xaed80, versados_data = 0xaed80, 
    netbsd_core_data = 0xaed80, mach_o_data = 0xaed80, 
    mach_o_fat_data = 0xaed80, plugin_data = 0xaed80, pef_data = 0xaed80, 
    pef_xlib_data = 0xaed80, sym_data = 0xaed80, any = 0xaed80}, 
  usrdata = 0x0, memory = 0xaed68, build_id = 0x0}

Maybe skipping sort when filename is "/dev/null" will fix.

Is there a better test?

Dave
--
John David Anglin	dave.anglin@bell.net
Comment 4 dave.anglin 2016-01-30 02:51:53 IST
Created attachment 8938 [details]
hppa-sort-unwind.d.txt

On 2016-01-29, at 9:05 PM, John David Anglin wrote:

> Maybe skipping sort when filename is "/dev/null" will fix.

Untested fix.

--
John David Anglin	dave.anglin@bell.net
Comment 5 Andreas Schwab 2016-01-30 08:41:02 IST
I think ld should error out if the output isn't a regular file.  There are probably other cases where the linker needs to reread what it has written out.
Comment 6 Helge Deller 2016-01-30 13:08:47 IST
Andreas wrote:
> I think ld should error out if the output isn't a regular file.

It currently does error out with a "final link failed: File truncated" error in that case. But that's actually the problem. It shouldn't error out as I described in the bug report.
Comment 7 Andreas Schwab 2016-01-30 13:51:08 IST
The linker should not allow writing the output to a non-regular file in the first place.  Changing the behaviour depending on the type of output file would be wrong.
Comment 8 dave.anglin 2016-01-30 18:15:00 IST
On 2016-01-30, at 8:51 AM, schwab@linux-m68k.org wrote:

> https://sourceware.org/bugzilla/show_bug.cgi?id=19526
> 
> --- Comment #7 from Andreas Schwab <schwab@linux-m68k.org> ---
> The linker should not allow writing the output to a non-regular file in the
> first place.  Changing the behaviour depending on the type of output file would
> be wrong.


Currently in bfd/opncls.c, there is the following code

  if (abfd->direction == write_direction
      && (abfd->flags & (EXEC_P | DYNAMIC)) != 0)
    {
      struct stat buf;

      if (stat (abfd->filename, &buf) == 0
          /* Do not attempt to change non-regular files.  This is
             here especially for configure scripts and kernel builds
             which run tests with "ld [...] -o /dev/null".  */
          && S_ISREG(buf.st_mode))
        {
          unsigned int mask = umask (0);

          umask (mask);
          chmod (abfd->filename,
                 (0777
                  & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
        }
    }

to support "ld [...] -o /dev/null".

It seems to me we either have to match x86 behaviour, or the linker should not allow writing
to a non-regular file.

Dave
--
John David Anglin	dave.anglin@bell.net
Comment 9 dave.anglin 2016-01-30 21:10:01 IST
Created attachment 8940 [details]
ldlang.c.d.txt

On 2016-01-30, at 1:14 PM, John David Anglin wrote:

> It seems to me we either have to match x86 behaviour, or the linker should not allow writing
> to a non-regular file.

Something like to the following can check if the output file is a regular file.  Various stat errors
could also be reported.

I'm not sure how portable this is.

Dave
--
John David Anglin	dave.anglin@bell.net
Comment 10 Alan Modra 2016-01-31 02:01:56 IST
Dave, I like your first patch, but you should probably allow S_ISLNK too.  The second will only cause grief for distros.
Comment 11 Alan Modra 2016-01-31 02:23:33 IST
Ah, never mind about S_ISLNK.  You're using stat, not lstat.
Comment 12 John David Anglin 2016-01-31 17:06:35 IST
Fixed on trunk by commit 6d4b286.