Bug 2186531 - Can not link a shared library with libc++
Summary: Can not link a shared library with libc++
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Fedora
Classification: Fedora
Component: clang15
Version: 37
Hardware: x86_64
OS: Linux
unspecified
high
Target Milestone: ---
Assignee: Tulio Magno Quites Machado Filho
QA Contact:
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2023-04-13 16:08 UTC by Oleg Samarin
Modified: 2023-07-20 05:18 UTC (History)
2 users (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2023-07-20 05:18:11 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)

Description Oleg Samarin 2023-04-13 16:08:14 UTC
Description of problem:
Attempts of linking a shared library with libc++ fails with lots of errors like this:

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol '__cxa_unexpected_handler'; recompile with -fPIC
>>> defined in /usr/bin/../lib/gcc/x86_64-redhat-linux/12/../../../../lib64/libc++.a(cxa_default_handlers.cpp.o)
>>> referenced by cxa_default_handlers.cpp.o:(std::set_unexpected(void (*)())) in archive /usr/bin/../lib/gcc/x86_64-redhat-linux/12/../../../../lib64/libc++.a


Version-Release number of selected component (if applicable):
libcxxabi-15.0.7-1.fc37.x86_64
libcxx-15.0.7-1.fc37.x86_64
libcxxabi-devel-15.0.7-1.fc37.x86_64
libcxx-devel-15.0.7-1.fc37.x86_64
libcxxabi-static-15.0.7-1.fc37.x86_64
libcxx-static-15.0.7-1.fc37.x86_64


How reproducible:
Always

Additional info:

Earlier these library were built with -fPIC unconditionally, but now they require passing -DCMAKE_POSITION_INDEPENDENT_CODE=ON on build.

See more details at https://reviews.llvm.org/rGa6e5563dfaff0cb7147058b9c49e38b611a28fb1

I managed to fix this issue by adding -DCMAKE_POSITION_INDEPENDENT_CODE=ON to libcxxabi.spec:

%cmake	-GNinja \
	-DCMAKE_C_COMPILER=/usr/bin/clang \
	-DCMAKE_CXX_COMPILER=/usr/bin/clang++ \
	-DCMAKE_MODULE_PATH=%{_libdir}/cmake/llvm \
	-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
	-DLIBCXXABI_LIBCXX_INCLUDES=%{_includedir}/c++/v1/ \
%if 0%{?__isa_bits} == 64
	-DLIBCXXABI_LIBDIR_SUFFIX:STRING=64 \
%endif
	-DCMAKE_BUILD_TYPE=RelWithDebInfo


and to libcxx.spec:

# Build the static libc++.a.
# We include the libc++abi symbols.
%cmake  -GNinja \
	$common_cmake_flags \
	-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
	-DLIBCXX_ENABLE_ABI_LINKER_SCRIPT=OFF \
	-DLIBCXX_ENABLE_STATIC=ON \
	-DLIBCXX_ENABLE_SHARED=OFF \
	-DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON \
	-DLIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY=ON \
	-DLIBCXX_CXX_ABI_LIBRARY_PATH=%{_libdir}

and rebuilding libcxxabi and libcxx.

Please include this fix upstream.

Comment 1 Tulio Magno Quites Machado Filho 2023-04-19 17:19:03 UTC
@osamarin68 , I tried to reproduce this issue without success.
I took a look at the build logs and I confirmed that -fPIC is being used.
Could you provide a reproducer for this issue, please?

Comment 2 Oleg Samarin 2023-04-20 16:18:38 UTC
Running this script

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
cat >TestShared.cpp <<EOF
#include <exception>

extern void test_shared() {
  std::get_terminate();
}
EOF

clang++ -fPIC -o TestShared.cpp.o -c TestShared.cpp
clang++ -fuse-ld=lld -nostdlib++ -Wl,-Bstatic -lc++ -lc++abi -Wl,-Bdynamic -stdlib=libc++ -shared -o libtest-shared.so TestShared.cpp.o
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

raises lots of linking errors.

Comment 3 Oleg Samarin 2023-04-20 16:22:43 UTC
After installing libcxxabi-static and libcxx-static built with -DCMAKE_POSITION_INDEPENDENT_CODE=ON, the shared library libtest-shared.so can be linked without errors.

Comment 4 Tulio Magno Quites Machado Filho 2023-04-20 16:45:33 UTC
Thank you, Oleg!
I was able to reproduce the issue now.

libcxx rebuilds some files provided by libcxxabi. Part of these files are specifically built under cxxabi_static_objects.dir. This group of files is not being built with -fPIC.

Comment 5 Fedora Update System 2023-04-28 00:36:17 UTC
FEDORA-2023-67a2776b08 has been submitted as an update to Fedora 39. https://bodhi.fedoraproject.org/updates/FEDORA-2023-67a2776b08

Comment 6 Fedora Update System 2023-04-28 13:10:13 UTC
FEDORA-2023-67a2776b08 has been pushed to the Fedora 39 stable repository.
If problem still persists, please make note of it in this bug report.

Comment 7 Fedora Update System 2023-05-02 17:56:58 UTC
FEDORA-2023-fac2a3fb5f has been submitted as an update to Fedora 38. https://bodhi.fedoraproject.org/updates/FEDORA-2023-fac2a3fb5f

Comment 8 Fedora Update System 2023-05-03 01:23:53 UTC
FEDORA-2023-fac2a3fb5f has been pushed to the Fedora 38 testing repository.
Soon you'll be able to install the update with the following command:
`sudo dnf upgrade --enablerepo=updates-testing --refresh --advisory=FEDORA-2023-fac2a3fb5f`
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2023-fac2a3fb5f

See also https://fedoraproject.org/wiki/QA:Updates_Testing for more information on how to test updates.

Comment 9 Fedora Update System 2023-05-05 01:28:44 UTC
FEDORA-2023-fac2a3fb5f has been pushed to the Fedora 38 stable repository.
If problem still persists, please make note of it in this bug report.

Comment 10 Fedora Update System 2023-05-05 16:42:37 UTC
FEDORA-2023-8640c2d9be has been submitted as an update to Fedora 37. https://bodhi.fedoraproject.org/updates/FEDORA-2023-8640c2d9be

Comment 11 Fedora Update System 2023-05-06 02:02:52 UTC
FEDORA-2023-8640c2d9be has been pushed to the Fedora 37 testing repository.
Soon you'll be able to install the update with the following command:
`sudo dnf upgrade --enablerepo=updates-testing --refresh --advisory=FEDORA-2023-8640c2d9be`
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2023-8640c2d9be

See also https://fedoraproject.org/wiki/QA:Updates_Testing for more information on how to test updates.

Comment 12 Fedora Update System 2023-05-14 01:58:53 UTC
FEDORA-2023-8640c2d9be has been pushed to the Fedora 37 stable repository.
If problem still persists, please make note of it in this bug report.

Comment 13 Jesus Checa 2023-07-11 06:03:28 UTC
@tuliom I found out that the issue is still present and reproducible. This makes the libc++_shared_static.cpp test from the integration testsuite to fail. It can be reproduced by following the instuctions in comment 2.

Could you please take a look into this? Thanks!

Comment 14 Tulio Magno Quites Machado Filho 2023-07-11 13:12:38 UTC
Confirmed.
This is only affecting Fedora 37.
Both Fedora 38 and Rawhide are working well.
libc++ will require a rebuild. I'm working on it.

Comment 15 Fedora Update System 2023-07-11 13:34:41 UTC
FEDORA-2023-e4957281af has been submitted as an update to Fedora 37. https://bodhi.fedoraproject.org/updates/FEDORA-2023-e4957281af

Comment 16 Fedora Update System 2023-07-12 02:35:41 UTC
FEDORA-2023-e4957281af has been pushed to the Fedora 37 testing repository.
Soon you'll be able to install the update with the following command:
`sudo dnf upgrade --enablerepo=updates-testing --refresh --advisory=FEDORA-2023-e4957281af`
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2023-e4957281af

See also https://fedoraproject.org/wiki/QA:Updates_Testing for more information on how to test updates.

Comment 17 Fedora Update System 2023-07-20 05:18:11 UTC
FEDORA-2023-e4957281af has been pushed to the Fedora 37 stable repository.
If problem still persists, please make note of it in this bug report.


Note You need to log in before you can comment on or make changes to this bug.