devel/freebsd-binutils@* conflicts with system toolchain for * matching the system: needs at least a devel/freebsd-binutils@* installation warning if the conflict is not removed

From: Mark Millard <marklmi_at_yahoo.com>
Date: Wed, 14 Sep 2022 17:54:56 UTC
One of the oddities of the FreeBSD system llvm/clang/clang++ related
toolchain is the default search sequence for the linker. Using aarch64
and main [so: 14] as an example context . . .

If the system toolchain is used via cc, c++, clang, clang++ as a front
end to the linker, it searches for the linker in a way that will search
for and, if found, will use:

/usr/local/bin/aarch64-unknown-freebsd14.0-ld

instead of:

/usr/bin/ld
or:
/usr/bin/ld.lld

It does not do this if -fuse-ld=lld is explicitly indicated on
the command line, instead using the /usr/bin/ material.

Later, below, I given clang++ -v output showing the linker
used in an example, providing detailed evdience of the above.

So how does one end up with the likes of:

/usr/local/bin/aarch64-unknown-freebsd14.0-ld

? One installs devel/freebsd-binutils@aarch64 (in the example),
a.k.a. aarch64-binutils , possibly by installing one or more
devel/freebsd-gcc*@aarch64 , a.k.a. aarch64-gcc* end
getting the linker via the dependency structure.

If nothing else, installing a devel/freebsd-binutils@* should
have a warning about the conflict, explicitly indicating that
the system linker will not be used by default unless the likes
of -fuse-ld=lld is on the command line as well.

In my view the FreeBSD system toolchain should not give priority
to the likes of:

/usr/local/bin/aarch64-unknown-freebsd14.0-ld

Such mixing of llvm clang/clang++ and binutils is not documented
to work --nor is it systematically tested. It is also
implicit/hidden unless is happens to produce an error or somthing
like -v happens to be put to use. Challenging the right assumption
for a problem that happens to be related can be rather difficult.


Detailed evidence from an example failure (wrong LTO infrasture
attempted because of the wrong linker being used). Note the:

"/usr/local/bin/aarch64-unknown-freebsd14.0-ld"

(it was an aarch64 main [so: 14] context).

clang++ -v -std=c++20 -Wpedantic -Wall -Wextra   -I../other_src_used  -pedantic  -g3 -O3 -mcpu=cortex-a72 -flto=thin  -pthread -flto=thin ../objs/cpp_thousandslocale-clang++_14_O3lto-libc++.o  ../objs/cpp_clockinfo-clang++_14_O3lto-libc++.o -o ../cpp_clockinfo_main-HoneyComb-65536MiB-threads_16-LP64-FreeBSD_main_n256584_5bc926af9fd1_64bit-clang++_14_O3lto-libc++  -DCPPCLOCKINFO_VERS='"acpphint_0.1.24"'  ../other_src_used/cpp_clockinfo_main.cpp
FreeBSD clang version 14.0.5 (https://github.com/llvm/llvm-project.git llvmorg-14.0.5-0-gc12386ae247c)
Target: aarch64-unknown-freebsd14.0
Thread model: posix
InstalledDir: /usr/bin
"/usr/bin/clang++" -cc1 -triple aarch64-unknown-freebsd14.0 -emit-llvm-bc -flto=thin -flto-unit -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name cpp_clockinfo_main.cpp -mrelocation-model static -mframe-pointer=non-leaf -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu cortex-a72 -target-feature +v8a -target-feature +fp-armv8 -target-feature +neon -target-feature +crc -target-feature +crypto -target-feature +sha2 -target-feature +aes -target-abi aapcs -fallow-half-arguments-and-returns -mllvm -treat-scalable-fixed-error-as-warning -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=gdb -v -fcoverage-compilation-dir=/root/acpphint/acpphint_src -resource-dir /usr/lib/clang/14.0.5 -I ../other_src_used -D "CPPCLOCKINFO_VERS=\"acpphint_0.1.24\"" -internal-isystem /usr/include/c++/v1 -O3 -Wpedantic -Wall -Wextra -pedantic -std=c++20 -fdeprecated-macro -fdebug-compilation-dir=/root/acpphint/acpphint_src -ferror-limit 19 -pthread -fno-signed-char -fgnuc-version=4.2.1 -fno-implicit-modules -fcxx-exceptions -fexceptions -fcolor-diagnostics -vectorize-loops -vectorize-slp -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/cpp_clockinfo_main-56273f.o -x c++ ../other_src_used/cpp_clockinfo_main.cpp
clang -cc1 version 14.0.5 based upon LLVM 14.0.5 default target aarch64-unknown-freebsd14.0
#include "..." search starts here:
#include <...> search starts here:
../other_src_used
/usr/include/c++/v1
/usr/lib/clang/14.0.5/include
/usr/include
End of search list.
"/usr/local/bin/aarch64-unknown-freebsd14.0-ld" --eh-frame-hdr -dynamic-linker /libexec/ld-elf.so.1 --enable-new-dtags -o ../cpp_clockinfo_main-HoneyComb-65536MiB-threads_16-LP64-FreeBSD_main_n256584_5bc926af9fd1_64bit-clang++_14_O3lto-libc++ /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtbegin.o -L/usr/lib -plugin /usr/bin/../lib/LLVMgold.so -plugin-opt=mcpu=cortex-a72 -plugin-opt=O3 -plugin-opt=thinlto ../objs/cpp_thousandslocale-clang++_14_O3lto-libc++.o ../objs/cpp_clockinfo-clang++_14_O3lto-libc++.o /tmp/cpp_clockinfo_main-56273f.o -lc++ -lm -lgcc --as-needed -lgcc_s --no-as-needed -lpthread -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/crtend.o /usr/lib/crtn.o
/usr/local/bin/aarch64-unknown-freebsd14.0-ld: /usr/bin/../lib/LLVMgold.so: error loading plugin: Cannot open "/usr/bin/../lib/LLVMgold.so"
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
*** Error code 1

===
Mark Millard
marklmi at yahoo.com