@@ -3977,6 +3977,7 @@ JVM_LoadLibrary(const char *libName, jboolean throwOnFailure)
3977
3977
#endif /* defined(WIN32) */
3978
3978
Trc_SC_LoadLibrary_Entry (libName );
3979
3979
{
3980
+ UDATA slOpenResult = 0 ;
3980
3981
UDATA handle = 0 ;
3981
3982
UDATA flags = J9_ARE_ANY_BITS_SET (javaVM -> extendedRuntimeFlags , J9_EXTENDED_RUNTIME_LAZY_SYMBOL_RESOLUTION ) ? J9PORT_SLOPEN_LAZY : 0 ;
3982
3983
@@ -3986,18 +3987,82 @@ JVM_LoadLibrary(const char *libName, jboolean throwOnFailure)
3986
3987
}
3987
3988
#endif /* defined(J9VM_ZOS_3164_INTEROPERABILITY) */
3988
3989
3989
- UDATA slOpenResult = j9sl_open_shared_library ((char * )libName , & handle , flags );
3990
+ slOpenResult = j9sl_open_shared_library ((char * )libName , & handle , flags );
3990
3991
Trc_SC_LoadLibrary_OpenShared (libName );
3991
3992
3992
- /* jdk17+ calls JVM_LoadLibrary with decorated library names. If the following is done
3993
- * then it overwrites the real error with a failure to load "liblib<name>.so.so".
3994
- */
3995
- #if JAVA_SPEC_VERSION < 17
3996
3993
if (0 != slOpenResult ) {
3997
- slOpenResult = j9sl_open_shared_library ((char * )libName , & handle , flags | J9PORT_SLOPEN_DECORATE );
3998
- Trc_SC_LoadLibrary_OpenShared_Decorate (libName );
3994
+ char * libNameNotDecorated = (char * )libName ;
3995
+ #if JAVA_SPEC_VERSION >= 17
3996
+ /* JDK17+ jdk.internal.loader.NativeLibraries.load() calls JVM_LoadLibrary()
3997
+ * with a decorated library name, i.e., a path to the library name returned
3998
+ * from Java_java_lang_System_mapLibraryName(nameNoPrefixNoExtension).
3999
+ * The library name passed to j9sl_open_shared_library() with the flag
4000
+ * J9PORT_SLOPEN_DECORATE must be platform independent, i.e., it must not
4001
+ * contain any prefix or file extension.
4002
+ */
4003
+ const char * fileExt = strrchr (libName , '.' );
4004
+ BOOLEAN doOpenLibrary = TRUE;
4005
+ char libPath [EsMaxPath ];
4006
+ libPath [0 ] = '\0' ;
4007
+ if (NULL == fileExt ) {
4008
+ /* A decorated library name is expected with a file extension,
4009
+ * pass to j9sl_open_shared_library w/o modification.
4010
+ */
4011
+ Trc_SC_libName_no_extension (libNameNotDecorated );
4012
+ } else {
4013
+ const char * fileNameTmp = strrchr (libName , DIR_SEPARATOR );
4014
+ const char * fileName = (NULL == fileNameTmp ) ? libName : (fileNameTmp + 1 );
4015
+ #if defined(J9OS_I5 ) || defined(WIN32 )
4016
+ /* no library prefix for J9OS_I5 and WIN32 */
4017
+ const size_t libStrLength = 0 ;
4018
+ #else /* defined(J9OS_I5) || defined(WIN32) */
4019
+ /* strlen("lib") = 3 */
4020
+ const size_t libStrLength = 3 ;
4021
+ if (0 != strncmp ("lib" , fileName , libStrLength )) {
4022
+ /* A decorated library name is expected to start with lib prefix for
4023
+ * platforms other than WIN32 & J9OS_I5.
4024
+ * Pass to j9sl_open_shared_library w/o modification.
4025
+ */
4026
+ Trc_SC_libName_no_prefix (fileName );
4027
+ } else
4028
+ #endif /* defined(J9OS_I5) || defined(WIN32) */
4029
+ {
4030
+ const char * fileNameNoPrefix = fileName + libStrLength ;
4031
+ uintptr_t libDirLength = (uintptr_t )fileName - (uintptr_t )libName ;
4032
+ uintptr_t fileNameNotDecoratedLength = (uintptr_t )fileExt - (uintptr_t )fileNameNoPrefix ;
4033
+ size_t libPathLength = libDirLength + fileNameNotDecoratedLength + 1 ;
4034
+ if (libPathLength <= EsMaxPath ) {
4035
+ libNameNotDecorated = libPath ;
4036
+ } else {
4037
+ libNameNotDecorated = (char * )j9mem_allocate_memory (libPathLength , OMRMEM_CATEGORY_VM );
4038
+ }
4039
+ if (NULL == libNameNotDecorated ) {
4040
+ doOpenLibrary = FALSE;
4041
+ Trc_SC_allocate_memory_failed (libPathLength );
4042
+ } else {
4043
+ j9str_printf (PORTLIB ,
4044
+ libNameNotDecorated ,
4045
+ libPathLength ,
4046
+ "%.*s%.*s" ,
4047
+ libDirLength ,
4048
+ libName ,
4049
+ fileNameNotDecoratedLength ,
4050
+ fileNameNoPrefix );
4051
+ }
4052
+ }
4053
+ }
4054
+ if (doOpenLibrary )
4055
+ #endif /* JAVA_SPEC_VERSION >= 17 */
4056
+ {
4057
+ slOpenResult = j9sl_open_shared_library (libNameNotDecorated , & handle , flags | J9PORT_SLOPEN_DECORATE );
4058
+ Trc_SC_LoadLibrary_OpenShared_Decorate (libNameNotDecorated );
4059
+ #if JAVA_SPEC_VERSION >= 17
4060
+ if ((libName != libNameNotDecorated ) && (libPath != libNameNotDecorated )) {
4061
+ j9mem_free_memory (libNameNotDecorated );
4062
+ }
4063
+ #endif /* JAVA_SPEC_VERSION >= 17 */
4064
+ }
3999
4065
}
4000
- #endif /* JAVA_SPEC_VERSION < 17 */
4001
4066
if (0 == slOpenResult ) {
4002
4067
result = (void * )handle ;
4003
4068
}
0 commit comments