diff mbox series

[meta-webserver,13/29] hiawatha: Link with libatomic when 64-bit atomics need it

Message ID 20261023065350.3228996-13-khem.raj@oss.qualcomm.com
State New
Headers show
Series [meta-oe,01/29] castxml: upgrade 0.7.0 -> 0.8.0 | expand

Commit Message

Khem Raj Oct. 23, 2026, 6:53 a.m. UTC
hiawatha uses __atomic_store_n()/__atomic_load_n() on time_t values. On
riscv32, where time_t is 64-bit but there are no 64-bit atomic
instructions, these become __atomic_*_8 libcalls, and linking fails:

  ld.lld: error: undefined symbol: __atomic_store_8

Add a patch that checks in CMake whether atomic operations on a time_t
need libatomic and links hiawatha with it only in that case.

AI-Generated: Uses Claude Code
Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
---
 ...libatomic-when-64-bit-atomics-need-i.patch | 70 +++++++++++++++++++
 .../recipes-httpd/hiawatha/hiawatha_12.4.bb   |  1 +
 2 files changed, 71 insertions(+)
 create mode 100644 meta-webserver/recipes-httpd/hiawatha/files/0001-cmake-Link-with-libatomic-when-64-bit-atomics-need-i.patch
diff mbox series

Patch

diff --git a/meta-webserver/recipes-httpd/hiawatha/files/0001-cmake-Link-with-libatomic-when-64-bit-atomics-need-i.patch b/meta-webserver/recipes-httpd/hiawatha/files/0001-cmake-Link-with-libatomic-when-64-bit-atomics-need-i.patch
new file mode 100644
index 0000000000..11f5780ce0
--- /dev/null
+++ b/meta-webserver/recipes-httpd/hiawatha/files/0001-cmake-Link-with-libatomic-when-64-bit-atomics-need-i.patch
@@ -0,0 +1,70 @@ 
+From: Khem Raj <khem.raj@oss.qualcomm.com>
+Date: Wed, 21 Oct 2026 14:00:00 -0700
+Subject: [PATCH] cmake: Link with libatomic when 64-bit atomics need it
+
+hiawatha uses __atomic_store_n()/__atomic_load_n() on time_t values,
+host->access_time in workers.c and, with ENABLE_HTTP2, drain_deadline in
+http2.c. On 32-bit targets with a 64-bit time_t but no 64-bit atomic
+instructions, such as riscv32, the compiler lowers these to
+__atomic_*_8 libcalls, and linking hiawatha fails:
+
+  ld.lld: error: undefined symbol: __atomic_store_8
+  >>> referenced by workers.c (src/workers.c)
+
+Check whether atomic operations on a time_t link on their own, and if
+not, whether they link with libatomic, and link hiawatha with it only in
+that case. The test variable is global so the compiler cannot optimize
+the atomic operations away.
+
+Upstream-Status: Pending
+Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
+---
+ CMakeLists.txt | 23 ++++++++++++++++++++++-
+ 1 file changed, 22 insertions(+), 1 deletion(-)
+
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -25,6 +25,7 @@
+ include(CheckFunctionExists)
+ include(CheckLibraryExists)
+ include(CheckSymbolExists)
++include(CheckCSourceCompiles)
+ include(GNUInstallDirs)
+ if(ENABLE_XSLT)
+ 	include(FindLibXml2)
+@@ -85,6 +86,26 @@
+ check_symbol_exists("kqueue" "sys/types.h;sys/event.h;sys/time.h" HAVE_KQUEUE)
+ check_symbol_exists("kqueue1" "sys/types.h;sys/event.h;sys/time.h" HAVE_KQUEUE1)
+
++# Atomic operations on time_t need libatomic on 32-bit targets with a 64-bit
++# time_t but without 64-bit atomic instructions, e.g. riscv32. The variable is
++# global so the compiler cannot optimize the atomic operations away.
++set(ATOMIC64_TEST_SOURCE "
++#include <time.h>
++time_t t;
++int main(void) {
++	__atomic_store_n(&t, time(NULL), __ATOMIC_RELEASE);
++	return __atomic_load_n(&t, __ATOMIC_SEQ_CST) == 0;
++}")
++check_c_source_compiles("${ATOMIC64_TEST_SOURCE}" HAVE_ATOMIC64)
++if(NOT HAVE_ATOMIC64)
++	set(CMAKE_REQUIRED_LIBRARIES atomic)
++	check_c_source_compiles("${ATOMIC64_TEST_SOURCE}" HAVE_ATOMIC64_WITH_LIBATOMIC)
++	unset(CMAKE_REQUIRED_LIBRARIES)
++	if(HAVE_ATOMIC64_WITH_LIBATOMIC)
++		set(ATOMIC_LIBRARY "atomic")
++	endif()
++endif()
++
+ if(HAVE_CRYPT_LIBRARY)
+ 	set(CRYPT_LIBRARY "crypt")
+ endif()
+@@ -168,7 +189,7 @@
+ add_executable(ssi-cgi ${ssi_cgi_src})
+ add_executable(wigwam ${wigwam_src})
+ target_link_libraries(cgi-wrapper ${Z_LIBRARY})
+-target_link_libraries(hiawatha ${CRYPT_LIBRARY} pthread ${Z_LIBRARY} ${MBEDTLS_LIBRARIES})
++target_link_libraries(hiawatha ${CRYPT_LIBRARY} pthread ${Z_LIBRARY} ${MBEDTLS_LIBRARIES} ${ATOMIC_LIBRARY})
+ target_link_libraries(ssi-cgi ${Z_LIBRARY})
+ target_link_libraries(wigwam ${CRYPT_LIBRARY} ${Z_LIBRARY} ${MBEDTLS_LIBRARIES})
+ if(ENABLE_HTTP2)
diff --git a/meta-webserver/recipes-httpd/hiawatha/hiawatha_12.4.bb b/meta-webserver/recipes-httpd/hiawatha/hiawatha_12.4.bb
index 8164b3076f..87bf6e5409 100644
--- a/meta-webserver/recipes-httpd/hiawatha/hiawatha_12.4.bb
+++ b/meta-webserver/recipes-httpd/hiawatha/hiawatha_12.4.bb
@@ -7,6 +7,7 @@  DEPENDS = "libxml2 libxslt virtual/crypt"
 SECTION = "net"

 SRC_URI = "https://hiawatha.leisink.net/files/download/hiawatha-${PV}.tar.gz \
+           file://0001-cmake-Link-with-libatomic-when-64-bit-atomics-need-i.patch \
            file://hiawatha-init \
            file://hiawatha.service \
           "