/usr/local/bin/aarch64-unknown-freebsd14.0-ld vs. system the system toolchain on aarch64 [example]: when found, lld is not used by default

From: Mark Millard <marklmi_at_yahoo.com>
Date: Wed, 31 Aug 2022 03:29:36 UTC
(This happens to be an aarch64 context and example. But is not
actually limited to aarch64 or to arm* --and so on. Similarly, I
expect, it is not limited to main [so: 14].)

I just ran into the following trying to locally build some standard-C++20
code via the system-toolchain on main. Note the aarch64-binutils
( devel/freebsd-binutils@aarch64 ) related path that automatically shows
up, despite not being requested:

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

Its use, mixed with a deliberate attempt to use LTO, in turn attempts to
use /usr/bin/../lib/LLVMgold.so --which is not found, breaking the build.
Plus, I did not request anything but the system ld to be used, so the
attempt to use /usr/local/bin/aarch64-unknown-freebsd14.0-ld looks to be
just wrong, independent of LTO being involved in my build.

The failing command:

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

A workaround is to explicitly use -fuse-ld=lld .

===
Mark Millard
marklmi at yahoo.com