Odd g++ (maybe a bug?)

In the last few days I’ve been dedicated to produce a static build of x265 (which is not the default, because the exec linked to dynamic lib works great and is what most users want, but you never know), facing some assembly issues hard to understand - only a few hours ago I noticed the relevant clue and achieved my static exec, but nevertheless I wish someone more skilled than me can watch at this, and give some feedback - when gcc/g++ itself is involved I’m always keen to think I’m a complete dumb.

I started my quest running:

cd source
cmake -DCMAKE_BUILD_TYPE=Release -DENABLE_SHARED=OFF .
make

and stopped at:

[...]
[ 63%] Built target common
[ 84%] Built target encoder
[ 85%] Linking CXX static library libx265.a
[ 85%] Built target x265-static
[ 87%] Linking CXX executable x265
/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/../../../../x86_64-unknown-haiku/bin/ld: libx265.a(cpu-a.asm.o): relocation R_X86_64_PC32 against symbol `x265_intel_cpu_indicator_init' can not be used when making a shared object; recompile with -fPIC
/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/../../../../x86_64-unknown-haiku/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
CMakeFiles/cli.dir/build.make:257: recipe for target 'x265' failed
make[2]: *** [x265] Error 1
CMakeFiles/Makefile2:200: recipe for target 'CMakeFiles/cli.dir/all' failed
make[1]: *** [CMakeFiles/cli.dir/all] Error 2
Makefile:135: recipe for target 'all' failed
make: *** [all] Error 2

Then I wondered WHY ld complained of an error when “making a shared object” : I don’t want to make a shared object!
Let’s see what happens in details:

~/Desktop/x265_git/source> make VERBOSE=1
[...]
[ 87%] Linking CXX executable x265
/boot/system/bin/cmake -E cmake_link_script CMakeFiles/cli.dir/link.txt --verbose=1
/bin/c++ -O3 -DNDEBUG CMakeFiles/cli.dir/input/input.cpp.o CMakeFiles/cli.dir/input/y4m.cpp.o CMakeFiles/cli.dir/input/yuv.cpp.o CMakeFiles/cli.dir/output/output.cpp.o CMakeFiles/cli.dir/output/raw.cpp.o CMakeFiles/cli.dir/output/reconplay.cpp.o CMakeFiles/cli.dir/output/y4m.cpp.o CMakeFiles/cli.dir/output/yuv.cpp.o CMakeFiles/cli.dir/x265.cpp.o CMakeFiles/cli.dir/x265cli.cpp.o CMakeFiles/cli.dir/abrEncApp.cpp.o -o x265  libx265.a 
/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/../../../../x86_64-unknown-haiku/bin/ld: libx265.a(cpu-a.asm.o): relocation R_X86_64_PC32 against symbol `x265_intel_cpu_indicator_init' can not be used when making a shared object; recompile with -fPIC
/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/../../../../x86_64-unknown-haiku/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
CMakeFiles/cli.dir/build.make:257: recipe for target 'x265' failed
make[2]: *** [x265] Error 1
make[2]: Leaving directory '/boot/home/Desktop/x265_git/source'
CMakeFiles/Makefile2:200: recipe for target 'CMakeFiles/cli.dir/all' failed
make[1]: *** [CMakeFiles/cli.dir/all] Error 2
make[1]: Leaving directory '/boot/home/Desktop/x265_git/source'
Makefile:135: recipe for target 'all' failed
make: *** [all] Error 2

It seems weird to me that the c++ command line doesn’t include any “-static***” options (I can’t see any stripping options either, but I won’t care atm), so I want to see what the internal handling of this huge command is:

~/Desktop/x265_git/source> c++ -O3 -DNDEBUG -static -v CMakeFiles/cli.dir/input/
input.cpp.o CMakeFiles/cli.dir/input/y4m.cpp.o CMakeFiles/cli.dir/input/yuv.cpp.
o CMakeFiles/cli.dir/output/output.cpp.o CMakeFiles/cli.dir/output/raw.cpp.o CMa
keFiles/cli.dir/output/reconplay.cpp.o CMakeFiles/cli.dir/output/y4m.cpp.o CMake
Files/cli.dir/output/yuv.cpp.o CMakeFiles/cli.dir/x265.cpp.o CMakeFiles/cli.dir/
x265cli.cpp.o CMakeFiles/cli.dir/abrEncApp.cpp.o -o x265  libx265.a
Using built-in specs.
COLLECT_GCC=c++
COLLECT_LTO_WRAPPER=/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/lto-wrapper
Target: x86_64-unknown-haiku
Configured with: /sources/gcc-11.2.0/configure --build=x86_64-unknown-haiku --prefix=/packages/gcc-11.2.0_2021_07_28-5/.self/develop/tools --libexecdir=/packages/gcc-11.2.0_2021_07_28-5/.self/develop/tools/lib --mandir=/packages/gcc-11.2.0_2021_07_28-5/.self/documentation/man --docdir=/packages/gcc-11.2.0_2021_07_28-5/.self/documentation/packages/gcc --enable-threads=posix --disable-nls --enable-shared --with-gnu-ld --with-gnu-as --enable-version-specific-runtime-libs --enable-languages=c,c++,fortran,objc --enable-lto --enable-frame-pointer --with-pkgversion=2021_07_28 --enable-__cxa-atexit --with-system-zlib --enable-checking=release --with-bug-url=http://dev.haiku-os.org/ --with-default-libstdcxx-abi=gcc4-compatible --enable-libssp --disable-multilib
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.2.0 (2021_07_28) 
COMPILER_PATH=/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/:/boot/system/develop/tools/bin/../lib/gcc/:/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/../../../../x86_64-unknown-haiku/bin/
BELIBRARIES=/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/:/boot/system/develop/tools/bin/../lib/gcc/:/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/../../../../x86_64-unknown-haiku/lib/:/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/../../../:/boot/system/develop/lib/
COLLECT_GCC_OPTIONS='-O3' '-D' 'NDEBUG' '-static' '-v' '-o' 'x265' '-mtune=generic' '-march=x86-64' '-dumpdir' 'x265.'
 /boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/collect2 -plugin /boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/liblto_plugin.so -plugin-opt=/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/lto-wrapper -plugin-opt=-fresolution=/tmp//ccdZUJoe.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lroot -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -m elf_x86_64_haiku -shared -no-undefined -o x265 /boot/system/develop/lib/crti.o /boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/crtbeginS.o /boot/system/develop/lib/start_dyn.o /boot/system/develop/lib/init_term_dyn.o -L/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0 -L/boot/system/develop/tools/bin/../lib/gcc -L/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/../../../../x86_64-unknown-haiku/lib -L/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/../../.. -L/boot/system/develop/lib CMakeFiles/cli.dir/input/input.cpp.o CMakeFiles/cli.dir/input/y4m.cpp.o CMakeFiles/cli.dir/input/yuv.cpp.o CMakeFiles/cli.dir/output/output.cpp.o CMakeFiles/cli.dir/output/raw.cpp.o CMakeFiles/cli.dir/output/reconplay.cpp.o CMakeFiles/cli.dir/output/y4m.cpp.o CMakeFiles/cli.dir/output/yuv.cpp.o CMakeFiles/cli.dir/x265.cpp.o CMakeFiles/cli.dir/x265cli.cpp.o CMakeFiles/cli.dir/abrEncApp.cpp.o libx265.a -lstdc++ -lgcc -lgcc_eh -lroot -lgcc -lgcc_eh /boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/crtendS.o /boot/system/develop/lib/crtn.o
/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/../../../../x86_64-unknown-haiku/bin/ld: libx265.a(cpu-a.asm.o): relocation R_X86_64_PC32 against symbol `x265_intel_cpu_indicator_init' can not be used when making a shared object; recompile with -fPIC
/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/11.2.0/../../../../x86_64-unknown-haiku/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status

Now, the issue!!!
The command for “collect2” has a “-shared” option which doesn’t appear into the very same build process under linux.
If I copy the same command line without that “-shared” option, then HEY! my static executable gets created in a snap! - No complaints.

Finally: is this behavior correct in haiku? I mean, is g++ supposed to always generate shared objects, passing that “-shared” ? or is it ignoring the “-static” to always pass a default set of options to collect2 ?

Yes, we always build shared (relocateable) objects, even for main application executables. That is indeed intentional.

1 Like

Ok, thanks :slight_smile: