diff --git a/src/dlls/mscorrc/mscorrc.rc b/src/dlls/mscorrc/mscorrc.rc index 1f2423b69c4d..fb6dcd60a57d 100644 --- a/src/dlls/mscorrc/mscorrc.rc +++ b/src/dlls/mscorrc/mscorrc.rc @@ -987,8 +987,11 @@ BEGIN IDS_EE_NDIRECT_BADNATL_CALLCONV "Invalid PInvoke or UnmanagedFunctionPointer calling convention." IDS_EE_NDIRECT_BADNATL_VARARGS_CALLCONV "Invalid PInvoke calling convention. Vararg functions must use the cdecl calling convention." IDS_EE_NDIRECT_BADNATL_THISCALL "Invalid PInvoke calling convention. Thiscall requires that the first parameter is present and can be enregistered." - IDS_EE_NDIRECT_LOADLIB "Unable to load DLL '%1': %2" - IDS_EE_NDIRECT_GETPROCADDRESS "Unable to find an entry point named '%2' in DLL '%1'." + IDS_EE_NDIRECT_LOADLIB_WIN "Unable to load DLL '%1' or one of its dependencies: %2" + IDS_EE_NDIRECT_LOADLIB_LINUX "Unable to load shared library '%1' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: %2" + IDS_EE_NDIRECT_LOADLIB_MAC "Unable to load shared library '%1' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: %2" + IDS_EE_NDIRECT_GETPROCADDRESS_WIN "Unable to find an entry point named '%2' in DLL '%1'." + IDS_EE_NDIRECT_GETPROCADDRESS_UNIX "Unable to find an entry point named '%2' in shared library '%1'." IDS_EE_NDIRECT_GETPROCADDRESS_NONAME "A library name must be specified in a DllImport attribute applied to non-IJW methods." IDS_EE_CLASS_CONSTRAINTS_VIOLATION "GenericArguments[%1], '%2', on '%3' violates the constraint of type parameter '%4'." IDS_EE_METHOD_CONSTRAINTS_VIOLATION "Method %1.%2: type argument '%3' violates the constraint of type parameter '%4'." @@ -1223,9 +1226,8 @@ BEGIN IDS_CLASSLOAD_NOTINTERFACE "Could not load type '%1' from assembly '%2' because it attempts to implement a class as an interface." IDS_CLASSLOAD_VALUEINSTANCEFIELD "Could not load the value type '%1' from assembly '%2' because it has an instance field of itself." - IDS_CLASSLOAD_BYREFLIKE_STATICFIELD "A value type containing a ByRef-like instance field cannot be used as the type for a static field." - IDS_CLASSLOAD_BYREFLIKE_NOTVALUECLASSFIELD "A value type containing a ByRef-like instance field cannot be used as the type for a class instance field." - IDS_CLASSLOAD_NOTBYREFLIKE "A value type containing a ByRef-like instance field must be ByRef-like type." + IDS_CLASSLOAD_BYREFLIKE_STATICFIELD "A value type containing a by-ref instance field, such as Span, cannot be used as the type for a static field." + IDS_CLASSLOAD_BYREFLIKE_NOTVALUECLASSFIELD "A value type containing a by-ref instance field, such as Span, cannot be used as the type for a class instance field." IDS_CLASSLOAD_BAD_NAME "Type name '%1' from assembly '%2' is invalid." IDS_CLASSLOAD_RANK_TOOLARGE "'%1' from assembly '%2' has too many dimensions." diff --git a/src/dlls/mscorrc/resource.h b/src/dlls/mscorrc/resource.h index 45ddd2ec82d0..0fbbf1ce1a6f 100644 --- a/src/dlls/mscorrc/resource.h +++ b/src/dlls/mscorrc/resource.h @@ -95,8 +95,8 @@ #define IDS_EE_NDIRECT_UNSUPPORTED_SIG 0x1708 #define IDS_EE_EXCEPTION_FROM_HRESULT 0x1709 #define IDS_EE_NDIRECT_BADNATL 0x170a -#define IDS_EE_NDIRECT_LOADLIB 0x170b -#define IDS_EE_NDIRECT_GETPROCADDRESS 0x170c +#define IDS_EE_NDIRECT_LOADLIB_WIN 0x170b +#define IDS_EE_NDIRECT_GETPROCADDRESS_WIN 0x170c #define IDS_EE_COM_UNSUPPORTED_SIG 0x170d #define IDS_EE_NOSYNCHRONIZED 0x170f #define IDS_EE_NDIRECT_BADNATL_THISCALL 0x1710 @@ -894,3 +894,6 @@ #define IDS_CLASSLOAD_BYREFLIKE_STATICFIELD 0x263b #define IDS_CLASSLOAD_BYREFLIKE_NOTVALUECLASSFIELD 0x263c #define IDS_CLASSLOAD_NOTBYREFLIKE 0x263d +#define IDS_EE_NDIRECT_LOADLIB_LINUX 0x263e +#define IDS_EE_NDIRECT_LOADLIB_MAC 0x263f +#define IDS_EE_NDIRECT_GETPROCADDRESS_UNIX 0x2640 diff --git a/src/pal/inc/pal.h b/src/pal/inc/pal.h index aff205d7dd82..75a68956a131 100644 --- a/src/pal/inc/pal.h +++ b/src/pal/inc/pal.h @@ -2718,6 +2718,11 @@ PALAPI LPCVOID PAL_GetSymbolModuleBase(void *symbol); +PALIMPORT +LPCSTR +PALAPI +PAL_GetLoadLibraryError(); + PALIMPORT LPVOID PALAPI diff --git a/src/pal/src/loader/module.cpp b/src/pal/src/loader/module.cpp index 9e8f2ac302ef..f73adfa56ac5 100644 --- a/src/pal/src/loader/module.cpp +++ b/src/pal/src/loader/module.cpp @@ -367,8 +367,8 @@ GetProcAddress( } else { - TRACE("Symbol %s not found in module %p (named %S), dlerror message is \"%s\"\n", - lpProcName, module, MODNAME(module), dlerror()); + TRACE("Symbol %s not found in module %p (named %S)\n", + lpProcName, module, MODNAME(module)); SetLastError(ERROR_PROC_NOT_FOUND); } done: @@ -840,6 +840,33 @@ PAL_GetSymbolModuleBase(void *symbol) return retval; } +/*++ + PAL_GetLoadLibraryError + + Wrapper for dlerror() to be used by PAL functions + +Return value: + +A LPCSTR containing the output of dlerror() + +--*/ +PALIMPORT +LPCSTR +PALAPI +PAL_GetLoadLibraryError() +{ + + PERF_ENTRY(PAL_GetLoadLibraryError); + ENTRY("PAL_GetLoadLibraryError"); + + LPCSTR last_error = dlerror(); + + LOGEXIT("PAL_GetLoadLibraryError returns %p\n", last_error); + PERF_EXIT(PAL_GetLoadLibraryError); + return last_error; +} + + /* Internal PAL functions *****************************************************/ /*++ @@ -870,7 +897,7 @@ BOOL LOADInitializeModules() exe_module.dl_handle = dlopen(nullptr, RTLD_LAZY); if (exe_module.dl_handle == nullptr) { - ERROR("Executable module will be broken : dlopen(nullptr) failed dlerror message is \"%s\" \n", dlerror()); + ERROR("Executable module will be broken : dlopen(nullptr) failed\n"); return FALSE; } exe_module.lib_name = nullptr; @@ -1107,7 +1134,7 @@ static BOOL LOADFreeLibrary(MODSTRUCT *module, BOOL fCallDllMain) if (module->dl_handle && 0 != dlclose(module->dl_handle)) { /* report dlclose() failure, but proceed anyway. */ - WARN("dlclose() call failed! error message is \"%s\"\n", dlerror()); + WARN("dlclose() call failed!\n"); } /* release all memory */ @@ -1376,7 +1403,6 @@ static void *LOADLoadLibraryDirect(LPCSTR libraryNameOrPath) void *dl_handle = dlopen(libraryNameOrPath, RTLD_LAZY); if (dl_handle == nullptr) { - WARN("dlopen() failed; dlerror says '%s'\n", dlerror()); SetLastError(ERROR_MOD_NOT_FOUND); } else @@ -1696,7 +1722,7 @@ MODSTRUCT *LOADGetPalLibrary() Dl_info info; if (dladdr((PVOID)&LOADGetPalLibrary, &info) == 0) { - ERROR("LOADGetPalLibrary: dladdr() failed. dlerror message is \"%s\"\n", dlerror()); + ERROR("LOADGetPalLibrary: dladdr() failed.\n"); goto exit; } // Stash a copy of the CoreCLR installation path in a global variable. diff --git a/src/pal/src/loader/modulename.cpp b/src/pal/src/loader/modulename.cpp index 87c1b026c281..e6a371f58250 100644 --- a/src/pal/src/loader/modulename.cpp +++ b/src/pal/src/loader/modulename.cpp @@ -60,7 +60,7 @@ const char *PAL_dladdr(LPVOID ProcAddress) Dl_info dl_info; if (!dladdr(ProcAddress, &dl_info)) { - WARN("dladdr() call failed! dlerror says '%s'\n", dlerror()); + WARN("dladdr() call failed!\n"); /* If we get an error, return NULL */ return (NULL); } diff --git a/src/vm/dllimport.cpp b/src/vm/dllimport.cpp index e7857e412d46..b06de143db09 100644 --- a/src/vm/dllimport.cpp +++ b/src/vm/dllimport.cpp @@ -5721,12 +5721,19 @@ class LoadLibErrorTracker m_priorityOfLastError = 0; } - VOID TrackErrorCode(DWORD dwLastError) + VOID TrackErrorCode() { LIMITED_METHOD_CONTRACT; DWORD priority; +#ifdef FEATURE_PAL + + SetMessage(PAL_GetLoadLibraryError()); +#else + + DWORD dwLastError = GetLastError(); + switch (dwLastError) { case ERROR_FILE_NOT_FOUND: @@ -5747,8 +5754,8 @@ class LoadLibErrorTracker priority = const_priorityCouldNotLoad; break; } - UpdateHR(priority, HRESULT_FROM_WIN32(dwLastError)); +#endif } // Sets the error code to HRESULT as could not load DLL @@ -5762,10 +5769,20 @@ class LoadLibErrorTracker return m_hr; } + SString& GetMessage() + { + return m_message; + } + void DECLSPEC_NORETURN Throw(SString &libraryNameOrPath) { STANDARD_VM_CONTRACT; +#if defined(__APPLE__) + COMPlusThrow(kDllNotFoundException, IDS_EE_NDIRECT_LOADLIB_MAC, libraryNameOrPath.GetUnicode(), GetMessage()); +#elif defined(FEATURE_PAL) + COMPlusThrow(kDllNotFoundException, IDS_EE_NDIRECT_LOADLIB_LINUX, libraryNameOrPath.GetUnicode(), GetMessage()); +#else // __APPLE__ HRESULT theHRESULT = GetHR(); if (theHRESULT == HRESULT_FROM_WIN32(ERROR_BAD_EXE_FORMAT)) { @@ -5775,8 +5792,9 @@ class LoadLibErrorTracker { SString hrString; GetHRMsg(theHRESULT, hrString); - COMPlusThrow(kDllNotFoundException, IDS_EE_NDIRECT_LOADLIB, libraryNameOrPath.GetUnicode(), hrString); + COMPlusThrow(kDllNotFoundException, IDS_EE_NDIRECT_LOADLIB_WIN, libraryNameOrPath.GetUnicode(), hrString); } +#endif // FEATURE_PAL __UNREACHABLE(); } @@ -5791,8 +5809,14 @@ class LoadLibErrorTracker } } + void SetMessage(LPCSTR message) + { + m_message = SString(SString::Utf8, message); + } + HRESULT m_hr; DWORD m_priorityOfLastError; + SString m_message; }; // class LoadLibErrorTracker // Local helper function for the LoadLibraryModule function below @@ -5819,7 +5843,7 @@ static HMODULE LocalLoadLibraryHelper( LPCWSTR name, DWORD flags, LoadLibErrorTr DWORD dwLastError = GetLastError(); if (dwLastError != ERROR_INVALID_PARAMETER) { - pErrorTracker->TrackErrorCode(dwLastError); + pErrorTracker->TrackErrorCode(); return hmod; } } @@ -5832,7 +5856,7 @@ static HMODULE LocalLoadLibraryHelper( LPCWSTR name, DWORD flags, LoadLibErrorTr if (hmod == NULL) { - pErrorTracker->TrackErrorCode(GetLastError()); + pErrorTracker->TrackErrorCode(); } return hmod; @@ -5852,7 +5876,7 @@ static HMODULE LocalLoadLibraryDirectHelper(LPCWSTR name, DWORD flags, LoadLibEr if (hmod == NULL) { - pErrorTracker->TrackErrorCode(GetLastError()); + pErrorTracker->TrackErrorCode(); } return hmod; @@ -6298,7 +6322,7 @@ VOID NDirect::NDirectLink(NDirectMethodDesc *pMD) { if (pMD->GetLibName() == NULL) COMPlusThrow(kEntryPointNotFoundException, IDS_EE_NDIRECT_GETPROCADDRESS_NONAME); - + StackSString ssLibName(SString::Utf8, pMD->GetLibName()); if (!hmod) @@ -6312,8 +6336,11 @@ VOID NDirect::NDirectLink(NDirectMethodDesc *pMD) wszEPName[0] = W('?'); wszEPName[1] = W('\0'); } - - COMPlusThrow(kEntryPointNotFoundException, IDS_EE_NDIRECT_GETPROCADDRESS, ssLibName.GetUnicode(), wszEPName); +#ifdef FEATURE_PAL + COMPlusThrow(kEntryPointNotFoundException, IDS_EE_NDIRECT_GETPROCADDRESS_UNIX, ssLibName.GetUnicode(), wszEPName); +#else + COMPlusThrow(kEntryPointNotFoundException, IDS_EE_NDIRECT_GETPROCADDRESS_WIN, ssLibName.GetUnicode(), wszEPName); +#endif } }