Skip to content

[Native Image] static-linked building fails with G1Gc #12197

@buuuuuuug

Description

@buuuuuuug

Describe the Issue

static linked building with g1gc always fails with error:

[chaney@centos demo]$ native-image --static --libc=musl --gc=G1 EnvMap
========================================================================================================================
GraalVM Native Image: Generating 'envmap' (static executable)...
========================================================================================================================
Warning: The host machine does not support all features of 'x86-64-v3'. Falling back to '-march=compatibility' for best compatibility.
[1/8] Initializing...                                                                                   (11.4s @ 0.18GB)
 Java version: 25+37-LTS, vendor version: Oracle GraalVM 25+37.1
 Graal compiler: optimization level: 2, target machine: compatibility, PGO: ML-inferred
 C compiler: x86_64-linux-musl-gcc (redhat, x86_64, 11.5.0)
 Garbage collector: G1 GC (max heap size: 25.0% of RAM)
 1 user-specific feature(s):
 - com.oracle.svm.thirdparty.gson.GsonFeature
------------------------------------------------------------------------------------------------------------------------
Build resources:
 - 14.75GB of memory (70.2% of system memory, using available memory)
 - 14 thread(s) (100.0% of 14 available processor(s), determined at start)
[2/8] Performing analysis...  [******]                                                                  (34.8s @ 0.42GB)
    3,260 types,   3,684 fields, and  15,615 methods found reachable
    1,078 types,      35 fields, and     418 methods registered for reflection
       58 types,      59 fields, and      52 methods registered for JNI access
        0 downcalls and 0 upcalls registered for foreign access
        9 native libraries: dl, g1gc-musl-cr, m, pthread, rt, stdc++, z
[3/8] Building universe...                                                                               (3.3s @ 0.45GB)
[4/8] Parsing methods...      [***]                                                                      (7.4s @ 0.49GB)
[5/8] Inlining methods...     [***]                                                                      (1.6s @ 0.50GB)
[6/8] Compiling methods...    [*********]                                                               (87.6s @ 1.00GB)
[7/8] Laying out methods...   [*]                                                                        (1.8s @ 1.08GB)
[8/8] Creating image...       [**
]                                                                       (0.0s @ 1.15GB)
------------------------------------------------------------------------------------------------------------------------
                       6.8s (4.4% of total time) in 1010 GCs | Peak RSS: 3.76GB | CPU load: 2.54
------------------------------------------------------------------------------------------------------------------------
Build artifacts:
 /Users/chaney/IdeaProjects/demo/svm_err_b_20250919T175800.986_pid3909.md (build_info)
========================================================================================================================
Failed generating 'envmap' after 2m 32s.

The build process encountered an unexpected error:

> java.lang.RuntimeException: There was an error linking the native image: Linker command exited with 1

Linker command executed:
/home/chaney/musl/bin/x86_64-linux-musl-gcc -z noexecstack -z text -Wl,--gc-sections -Wl,--version-script,/tmp/SVM-1339411709554429916/exported_symbols.list -o /Users/chaney/IdeaProjects/demo/envmap envmap.o /home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries/linux-amd64/musl/liblibchelper.a /home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries/linux-amd64/musl/libsvm_container.a /home/chaney/.sdkman/candidates/java/25-graal/lib/static/linux-amd64/musl/libnio.a /home/chaney/.sdkman/candidates/java/25-graal/lib/static/linux-amd64/musl/libnet.a /home/chaney/.sdkman/candidates/java/25-graal/lib/static/linux-amd64/musl/libjava.a /home/chaney/.sdkman/candidates/java/25-graal/lib/static/linux-amd64/musl/libzip.a /home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries/linux-amd64/musl/libjvm.a -static -v -L/tmp/SVM-1339411709554429916 -L/home/chaney/.sdkman/candidates/java/25-graal/lib/static/linux-amd64/musl -L/home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries/linux-amd64/musl -L/home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries/linux-amd64 -L/home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries -lg1gc-musl-cr -ldl -lm -lpthread -lstdc++ -lz -ldl -lpthread -lrt

Linker command output:
Using built-in specs.
Reading specs from /home/chaney/musl/lib/musl-gcc.specs
rename spec cpp_options to old_cpp_options
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-host-pie --enable-host-bind-now --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-plugin --enable-initfini-array --without-isl --enable-multilib --with-linker-hash-style=gnu --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_64=x86-64-v2 --with-arch_32=x86-64 --build=x86_64-redhat-linux --with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.5.0 20240719 (Red Hat 11.5.0-11) (GCC)
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/11/:/usr/libexec/gcc/x86_64-redhat-linux/11/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/11/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/11/:/usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/11/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-z' 'noexecstack' '-z' 'text' '-o' '/Users/chaney/IdeaProjects/demo/envmap' '-static' '-v' '-L/tmp/SVM-1339411709554429916' '-L/home/chaney/.sdkman/candidates/java/25-graal/lib/static/linux-amd64/musl' '-L/home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries/linux-amd64/musl' '-L/home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries/linux-amd64' '-L/home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries' '-specs=/home/chaney/musl/lib/musl-gcc.specs' '-mtune=generic' '-march=x86-64-v2' '-dumpdir' '/Users/chaney/IdeaProjects/demo/envmap.'
 /usr/libexec/gcc/x86_64-redhat-linux/11/collect2 -plugin /usr/libexec/gcc/x86_64-redhat-linux/11/liblto_plugin.so -plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper -plugin-opt=-fresolution=/tmp/ccTnw1Gm.res -plugin-opt=-pass-through=/usr/lib/gcc/x86_64-redhat-linux/11/libgcc.a -plugin-opt=-pass-through=/usr/lib/gcc/x86_64-redhat-linux/11/libgcc_eh.a -plugin-opt=-pass-through=-lc -dynamic-linker /lib/ld-musl-x86_64.so.1 -nostdlib -static -o /Users/chaney/IdeaProjects/demo/envmap -z noexecstack -z text /home/chaney/musl-toolchain/lib/Scrt1.o /home/chaney/musl-toolchain/lib/crti.o /usr/lib/gcc/x86_64-redhat-linux/11/crtbeginS.o -L/tmp/SVM-1339411709554429916 -L/home/chaney/.sdkman/candidates/java/25-graal/lib/static/linux-amd64/musl -L/home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries/linux-amd64/musl -L/home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries/linux-amd64 -L/home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries -L/home/chaney/musl-toolchain/lib -L /usr/lib/gcc/x86_64-redhat-linux/11/. --gc-sections --version-script /tmp/SVM-1339411709554429916/exported_symbols.list envmap.o /home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries/linux-amd64/musl/liblibchelper.a /home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries/linux-amd64/musl/libsvm_container.a /home/chaney/.sdkman/candidates/java/25-graal/lib/static/linux-amd64/musl/libnio.a /home/chaney/.sdkman/candidates/java/25-graal/lib/static/linux-amd64/musl/libnet.a /home/chaney/.sdkman/candidates/java/25-graal/lib/static/linux-amd64/musl/libjava.a /home/chaney/.sdkman/candidates/java/25-graal/lib/static/linux-amd64/musl/libzip.a /home/chaney/.sdkman/candidates/java/25-graal/lib/svm/clibraries/linux-amd64/musl/libjvm.a -lg1gc-musl-cr -ldl -lm -lpthread -lstdc++ -lz -ldl -lpthread -lrt --start-group /usr/lib/gcc/x86_64-redhat-linux/11/libgcc.a /usr/lib/gcc/x86_64-redhat-linux/11/libgcc_eh.a -lc --end-group /usr/lib/gcc/x86_64-redhat-linux/11/crtendS.o /home/chaney/musl-toolchain/lib/crtn.o
/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/11/./libstdc++.a(guard.o): in function `__cxa_guard_acquire':
(.text.__cxa_guard_acquire+0x17): undefined reference to `__libc_single_threaded'
/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/11/./libstdc++.a(guard.o): in function `__cxa_guard_release':
(.text.__cxa_guard_release+0x7): undefined reference to `__libc_single_threaded'
/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/11/libgcc_eh.a(unwind-dw2-fde-dip.o): in function `_Unwind_Find_FDE':
(.text+0x25fc): undefined reference to `_dl_find_object'
collect2: error: ld returned 1 exit status

Please inspect the generated error report at:
/Users/chaney/IdeaProjects/demo/svm_err_b_20250919T175800.986_pid3909.md

If you are unable to resolve this problem, please file an issue with the error report at:
https://graalvm.org/support
[chaney@centos demo]$ native-image --static --libc=musl --gc=G1 EnvMap

Using the latest version of GraalVM can resolve many issues.

GraalVM Version

java 25 2025-09-16 LTS
Java(TM) SE Runtime Environment Oracle GraalVM 25+37.1 (build 25+37-LTS-jvmci-b01)
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 25+37.1 (build 25+37-LTS-jvmci-b01, mixed mode, sharing)

Operating System and Version

centos 9

Build Command

native-image --static --libc=musl --gc=G1 EnvMap

 import java.util.Map;

 public class EnvMap {
     public static void main (String[] args) {
         var filter = args.length > 0 ? args[0] : "";
         Map<String, String> env = System.getenv();
         for (String envName : env.keySet()) {
             if(envName.contains(filter)) {
                 System.out.format("%s=%s%n",
                                 envName,
                                 env.get(envName));
             }
         }
     }
 }

Expected Behavior

successful building with static linked executable file

Actual Behavior

building fails

Steps to Reproduce

  1. create a java file with content above
  2. native-image --static --libc=musl --gc=G1 EnvMap

Additional Context

No response

Build Log Output and Error Messages

No response

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions