Use of lang/gcc*'s g++* via the likes of USE_GCC=11:build+ in ports when a -Wl,-rpath=/usr/local/lib/gcc* is required: How?

From: Mark Millard <marklmi_at_yahoo.com>
Date: Mon, 18 Apr 2022 02:21:11 UTC
I'll use g++11 as an example here. By no means is the issue
limited to g++11 . Also, the general issue is not limited
to the specifics of the other aspects of the example I report
below.


EXAMPLE PROBLEM:

FreeBSD's /lib/libgcc_s.so.1 is insufficient for what aarch64
g++11 code generation expects to be provided. Programs can be
built but end up with notices that start with:

ld-elf.so.1: /lib/libgcc_s.so.1: version GCC_4.5.0 required by
. . . not found

Sometimes the ". . ." is /usr/local/lib/gcc11/libstdc++.so.6 .
Other times it does not get that far because the program
itself has the issue before shared libraries are bound. A
simple 6 line program on aarch64 can show the issue:

# more fp-binding-failure-aarch64.cpp
#include <cmath>
volatile long double v=NAN;
int main()
{
    return std::isnan(v) ? 1 : 0;
}

# g++11 fp-binding-failure-aarch64.cpp
# ./a.out
ld-elf.so.1: /lib/libgcc_s.so.1: version GCC_4.5.0 required by /usr/home/root/c_tests/a.out not found

The a.out has its own reference to 
__unordtf2@GCC_4.5.0 :

# nm -a a.out | grep '@GCC_[4-9]\.' | more
                 U 
__unordtf2@GCC_4.5.0

For reference . . .

# ldd a.out
a.out:
        libstdc++.so.6 => /usr/local/lib/gcc11/libstdc++.so.6 (0x83000000)
        libm.so.5 => /lib/libm.so.5 (0x81834000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x8275a000)
        libc.so.7 => /lib/libc.so.7 (0x845f3000)

Use of -Wl,-rpath=/usr/local/lib/gcc11 avoids the example's
failure by using gcc11's libgcc_s.so.1 instead of FreeBSD's.
gcc11's libgcc_s.so.1 is more likely to accurately cover
the g++11 compiler's potential range of code generation.

I've no clue what all g++11 targeting aarch64 generates that
also would not be found in /lib/libgcc_s.so.1 on aarch64.

Similarly, I do not know what examples might exist for other
FreeBSD platforms. But aarch64 is tier 1 these days, if that
matters for this subject area.


THE QUESTION:

How does one use the likes of USE_GCC=11:build+ and also
supply the appropriate -Wl,-rpath=/usr/local/lib/gcc??
to match which g++ is actually used?

There is an analogous question for when using a linker
command line directly instead (no -Wl, prefix).

===
Mark Millard
marklmi at yahoo.com