Bug 44724 - LTO segfault with -fuse-linker-plugin
Summary: LTO segfault with -fuse-linker-plugin
Status: RESOLVED WORKSFORME
Alias: None
Product: gcc
Classification: Unclassified
Component: lto (show other bugs)
Version: 4.5.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: lto
Depends on:
Blocks:
 
Reported: 2010-06-30 12:22 UTC by Juhani Viheräkoski
Modified: 2010-07-06 12:57 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-07-01 11:35:19


Attachments
Preprocessed source files to reproduce this problem (12.00 KB, application/x-xz)
2010-07-01 10:47 UTC, Juhani Viheräkoski
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Juhani Viheräkoski 2010-06-30 12:22:34 UTC
VICE Commodore emulator lto-build fails with a segfault in lto1. The code has both C and C++ source files so it is linked with g++. As compilation fails during lto, it is difficult to produce a self-contained testcase, but you should be able to reproduce the fault by downloading and building VICE http://www.zimmers.net/anonftp/pub/cbm/crossplatform/emulators/VICE/vice-2.2.tar.gz

If there is a simple way to produce a smaller testcase for this bug, please let me know.

I am using newest binutils from cvs, configured with 
--enable-gold --enable-plugins

$ ld -v
GNU gold (GNU Binutils 2.20.51.20100629) 1.9

and gcc from svn 4.5 branch revision 161539. 
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/home/misty/gcc/libexec/gcc/i686-pc-linux-gnu/4.5.1/lto-wrapper
Target: i686-pc-linux-gnu
Configured with: ../gcc-45branch/configure --prefix=/home/misty/gcc --enable-languages=c,c++ --enable-gold --enable-lto --enable-plugins
Thread model: posix
gcc version 4.5.1 20100629 (prerelease) (GCC)

Link command:

g++ -flto -fuse-linker-plugin -O3 -fomit-frame-pointer -march=pentium4  -o x64  alarm.o attach.o autostart.o autostart-prg.o charset.o clkguard.o clipboard.o cmdline.o cbmdos.o cbmimage.o color.o crc32.o datasette.o debug.o dma.o embedded.o emuid.o event.o findpath.o fliplist.o gcr.o info.o init.o initcmdline.o interrupt.o ioutil.o joystick.o kbdbuf.o keyboard.o lib.o libm_math.o lightpen.o log.o machine-bus.o machine.o main.o network.o opencbmlib.o palette.o ram.o rawfile.o resources.o romset.o screenshot.o snapshot.o socket.o sound.o sysfile.o translate.o traps.o util.o vsync.o zfile.o zipcode.o maincpu.o mouse.o midi.o ../src/c64/libc64.a ../src/c64/cart/libc64cart.a ../src/drive/iec/libdriveiec.a ../src/drive/iecieee/libdriveiecieee.a ../src/drive/iec/c64exp/libdriveiecc64exp.a ../src/drive/ieee/libdriveieee.a ../src/drive/libdrive.a ../src/iecbus/libiecbus.a ../src/parallel/libparallel.a ../src/vdrive/libvdrive.a ../src/sid/libsid.a ../src/monitor/libmonitor.a ../src/sounddrv/libsounddrv.a ../src/gfxoutputdrv/libgfxoutputdrv.a ../src/printerdrv/libprinterdrv.a ../src/rs232drv/librs232drv.a ../src/diskimage/libdiskimage.a ../src/fsdevice/libfsdevice.a ../src/tape/libtape.a ../src/imagecontents/libimagecontents.a ../src/fileio/libfileio.a ../src/serial/libserial.a ../src/core/libcore.a ../src/vicii/libvicii.a ../src/raster/libraster.a ../src/video/libvideo.a ../src/arch/unix/libarch.a ../src/arch/unix/gui/libarchgui.a ../src/arch/unix/x11/gnome/libgnomeui.a ../src/arch/unix/x11/libx11ui.a     ../src/resid/libresid.a ../src/resid-fp/libresidfp.a -lXrandr -lXxf86vm -pthread -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lgdk_pixbuf-2.0 -lpangocairo-1.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lrt -lglib-2.0         -lasound    -lreadline -ljpeg -lgif -lpng  -lz -ldl  -lbsd -lnsl  -lbsd -lm 
resid-fp.h:32:21: warning: type of ‘residfp_hooks’ does not match original declaration
resid-fp.cc:282:14: note: previously declared here
../../src/interrupt.h:289:14: warning: type of ‘drive_clk’ does not match original declaration
drive.c:131:7: note: previously declared here
fastsid.h:33:21: warning: type of ‘fastsid_hooks’ does not match original declaration
fastsid.c:1028:14: note: previously declared here
lto1: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
lto-wrapper: g++ returned 1 exit status
/home/misty/gcc/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../i686-pc-linux-gnu/bin/ld: fatal error: lto-wrapper failed
collect2: ld returned 1 exit status
Comment 1 Juhani Viheräkoski 2010-06-30 12:27:52 UTC
(In reply to comment #0)

I forgot to add that I configured VICE with a following command:

CFLAGS='-flto -fuse-linker-plugin -O3 -fomit-frame-pointer -march=pentium4' CXXFLAGS='-O3 -fomit-frame-pointer -funroll-loops -march=pentium4 -flto -fuse-linker-plugin' ./configure --prefix=/home/misty/gcc --enable-gnomeui --enable-fullscreen; make

If you forget --enable-gnomeui, build will fail in my experience, so this is important.
Comment 2 Richard Biener 2010-06-30 12:59:35 UTC
On the link command-line add -r -nostdlib and start removing as many objects
and libraries from the command-line as possible (preserving the crash of course).
If static libraries remain, open them up and add/remove individual object
files.

For the smallest set of object files we need preprocessed source and the
command-lines to produce them.
Comment 3 Juhani Viheräkoski 2010-07-01 10:47:01 UTC
Created attachment 21055 [details]
Preprocessed source files to reproduce this problem

I was able to reduce this to two (2) source files. Quite an improvement, original was more than hundred files :) The exact command I used to reproduce this bug is:

g++ -r -nostdlib -O3 -fomit-frame-pointer -funroll-loops -march=pentium4 -flto -fuse-linker-plugin sid.ii wave.ii

The .ii files were created with

g++ -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"resid\" -DVERSION=\"0.16vice\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DSIZEOF_INT=4 -I. -I.     -O3 -fomit-frame-pointer -funroll-loops -march=pentium4 -flto -fuse-linker-plugin -MT sid.o -MD -MP -MF ".deps/sid.Tpo" -E sid.cc > sid.ii

g++ -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"resid\" -DVERSION=\"0.16vice\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DSIZEOF_INT=4 -I. -I.     -O3 -fomit-frame-pointer -funroll-loops -march=pentium4 -flto -fuse-linker-plugin -MT wave.o -MD -MP -MF ".deps/wave.Tpo" -E wave.cc > wave.ii
Comment 4 Juhani Viheräkoski 2010-07-01 11:18:21 UTC
In the meantime I also built gcc trunk revision 161601 and this also fails with:

lto1: internal compiler error: vector VEC(cgraph_node_ptr,base) index domain error, in lto_cgraph_encoder_deref at lto-cgraph.c:141
Comment 5 Richard Biener 2010-07-01 11:35:19 UTC
Confirmed.  Works without -fuse-linker-plugin.

Reduced testcases:

wave.ii
-------
typedef unsigned int reg8;
typedef unsigned int reg12;
class WaveformGenerator {
public:
    reg8 readOSC();
    inline reg12 output();
    reg8 waveform;
    reg12 output____();
};
inline reg12 WaveformGenerator::output() {
    switch (waveform) {
        default:
            return output____();
    }
}
reg8 WaveformGenerator::readOSC() {
    return output() >> 4;
}

sid.ii
------
typedef unsigned int reg8;
typedef unsigned int reg12;
typedef int sound_sample;
class WaveformGenerator {
public:
    reg8 readOSC();
    inline reg12 output();
    reg8 waveform;
    reg12 output____();
};
inline reg12 WaveformGenerator::output() {
    switch (waveform) {
        case 0x0:
            return output____();
    }
}
class Voice {
    inline sound_sample output();
    WaveformGenerator wave;
    friend class SID;
};
inline sound_sample Voice::output() {
    return wave.output();
};
class Filter {
public:
    void clock(sound_sample voice1, sound_sample voice2, sound_sample voice3);
};
class SID {
    void clock();
    Voice voice[3];
    Filter filter;
};
void SID::clock() {
  filter.clock(voice[0].output(), voice[1].output(), voice[2].output());
}

rguenther@murzim:/tmp> g++-4.5 -r -nostdlib -O2 -march=pentium4 -flto -fuse-linker-plugin -m32 sid.3.ii wave.3.ii
/usr/bin/gold: error: /tmp/ccbAKTWf.lto.o: incompatible target
collect2: ld returned 1 exit status
rguenther@murzim:/tmp> g++-4.5 -r -nostdlib -O2 -march=pentium4 -flto -fuse-linker-plugin -m32 wave.3.ii sid.3.ii
lto1: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://bugs.opensuse.org/> for instructions.
lto-wrapper: g++-4.5 returned 1 exit status
/usr/bin/gold: fatal error: lto-wrapper failed
collect2: ld returned 1 exit status
Comment 6 Richard Biener 2010-07-01 11:40:53 UTC
On trunk with the original testcase we fail with

/abuild/rguenther/trunk-g/gcc> ./xgcc -B. -r -nostdlib -O -march=pentium4 -flto -fuse-linker-plugin sid.ii wave.ii -m32 -B ../lto-plugin/.libs

lto1: out of memory allocating 8589934596 bytes after a total of 520192 bytes
lto-wrapper: ./xgcc returned 1 exit status
/usr/bin/gold: fatal error: lto-wrapper failed
collect2: ld returned 1 exit status

#4  0x00000000007bd6e1 in input_cgraph_1 (file_data=0x7ffff7ee2300, 
    ib=0x166a340) at /space/rguenther/src/svn/trunk/gcc/lto-cgraph.c:1257
1257          char *str = (char *)xmalloc (len + 1);
(gdb) l
1252
1253      /* Input toplevel asms.  */
1254      len = lto_input_uleb128 (ib);
1255      while (len)
1256        {
1257          char *str = (char *)xmalloc (len + 1);
1258          for (i = 0; i < len; ++i)
1259            str[i] = lto_input_1_unsigned (ib);
1260          cgraph_add_asm_node (build_string (len, str));
1261          free (str);
(gdb) p len
$1 = 8589934595

so somehow the output cgraph is corrupt.  Works fine without using the
linker-plugin on trunk, too.
Comment 7 Juhani Viheräkoski 2010-07-06 12:57:25 UTC
This issue is now fixed in trunk. I have no obligation to push it for 4.5 series as I am now able to build whole VICE successfully with 4.6 so I am closing the ticket.