JDK-8262501 : jdk17 libjvm link failure with --as-needed and clock_gettime in librt
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 17
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: generic
  • Submitted: 2021-02-28
  • Updated: 2021-04-22
  • Resolved: 2021-04-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 17
17 b19Fixed
Related Reports
Relates :  
Description
The fix from JDK-8246112 breaks the build with old (but according to comments still supported) glibc versions, when ld defaults to the --as-needed behavior.

The reason for that is the passing of a library (-lrt) in a LDFLAGS macro, not in a LIBS macro.

A quick workaround is

--- a/make/autoconf/flags-ldflags.m4
+++ b/make/autoconf/flags-ldflags.m4
@@ -113,7 +113,7 @@
       # But once our supported minimum build and runtime platform
       # has glibc 2.17, this can be removed as the functions are
       # in libc.
-      OS_LDFLAGS_JVM_ONLY="-lrt"
+      OS_LDFLAGS_JVM_ONLY="-Wl,--no-as-needed -lrt"
     fi
   fi

So -lrt should be passed in the macros which are used to pass -ldl -lpthread ...

Comments
Changeset: 81877f7d Author: David Holmes <dholmes@openjdk.org> Date: 2021-04-15 12:59:21 +0000 URL: https://git.openjdk.java.net/jdk/commit/81877f7d
15-04-2021

New linking command-line: g++ -Wl,-z,defs -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,-O1 -Wl,--hash-style=gnu -m64 -static-libstdc++ -static-libgcc -shared -m64 -Wl,-version-script=mapfile -Wl,-soname=libjvm.so --sysroot=/var/tmp/devkit-linux_x64/gcc10.2.0-OL6.4+1.0/devkit-linux_x64-gcc10.2.0-OL6.4+1.0.tar.gz/x86_64-linux-gnu-to-x86_64-linux-gnu/x86_64-linux-gnu/sysroot -o build/linux-x64-debug-8262501/support/modules_libs/java.base/server/libjvm.so @build/linux-x64-debug-8262501/hotspot/variant-server/libjvm/objs/_BUILD_LIBJVM_objectfilenames.txt -lm -ldl -lpthread -lrt
14-04-2021

Thanks [~doko]. I clearly see the problem. I had expected the -lrt to be with the other libraries. I was not aware of BASIC_JVM_LIBS and the build reviewers did not spot this either. I will fix using your patch - thanks.
14-04-2021

I don't have the build log anymore. If needed, I can re-run it. The link step for libjvm.so fails, because the link line looks like ld --as-needed -lrt <object files> $BASIC_JVM_LIBS -lpthread with the error that clock_gettime is not found in any of the libraries, because librt isn't need at this position of the command line, This is Ubuntu 12.04 LTS, using (e)glibc-2.15 The current local patch is https://salsa.debian.org/openjdk-team/openjdk/-/blob/master/debian/patches/8262501.diff
14-04-2021

[~doko] Can you please elaborate what the actual error is. And what version of glibc. Thanks.
14-04-2021

ILW = MLM = P4
13-04-2021

this seems to be the correct approach. maybe you also want to guard this with a version check on glibc --- a/make/autoconf/flags-ldflags.m4 +++ b/make/autoconf/flags-ldflags.m4 @@ -108,14 +108,6 @@ AC_DEFUN([FLAGS_SETUP_LDFLAGS_HELPER], OS_LDFLAGS_JVM_ONLY="-Wl,-rpath,@loader_path/. -Wl,-rpath,@loader_path/.." OS_LDFLAGS="-mmacosx-version-min=$MACOSX_VERSION_MIN" fi - if test "x$OPENJDK_TARGET_OS" = xlinux; then - # Hotspot needs to link librt to get the clock_* functions. - # But once our supported minimum build and runtime platform - # has glibc 2.17, this can be removed as the functions are - # in libc. - OS_LDFLAGS_JVM_ONLY="-lrt" - fi - fi # Setup debug level-dependent LDFLAGS if test "x$TOOLCHAIN_TYPE" = xgcc; then --- a/make/autoconf/libraries.m4 +++ b/make/autoconf/libraries.m4 @@ -124,6 +124,14 @@ AC_DEFUN_ONCE([LIB_SETUP_LIBRARIES], BASIC_JVM_LIBS="$BASIC_JVM_LIBS -lpthread" fi + if test "x$OPENJDK_TARGET_OS" = xlinux; then + # Hotspot needs to link librt to get the clock_* functions. + # But once our supported minimum build and runtime platform + # has glibc 2.17, this can be removed as the functions are + # in libc. + BASIC_JVM_LIBS=="$BASIC_JVM_LIBS -lrt" + fi + # Atomic library # 32-bit platforms needs fallback library for 8-byte atomic ops on Zero if HOTSPOT_CHECK_JVM_VARIANT(zero); then
22-03-2021