Fix issue 1774
authorGaurav Khanna <gkhanna@microsoft.com>
Mon, 19 Oct 2015 05:18:43 +0000 (22:18 -0700)
committerGaurav Khanna <gkhanna@microsoft.com>
Fri, 23 Oct 2015 17:59:11 +0000 (10:59 -0700)
Commit migrated from https://github.com/dotnet/coreclr/commit/46a198ef387f482d76d0711944f575450e3d9675

src/coreclr/CMakeLists.txt
src/coreclr/src/coreclr/hosts/unixcoreconsole/coreconsole.cpp
src/coreclr/src/coreclr/hosts/unixcoreruncommon/coreruncommon.cpp
src/coreclr/src/coreclr/hosts/unixcoreruncommon/coreruncommon.h

index 2803728..5588489 100644 (file)
@@ -225,11 +225,37 @@ if (CLR_CMAKE_PLATFORM_LINUX)
     add_definitions(-DFEATURE_EVENTSOURCE_XPLAT=1)
 endif (CLR_CMAKE_PLATFORM_LINUX)
 
+if (CLR_CMAKE_PLATFORM_UNIX)
+  add_definitions(-DPLATFORM_UNIX=1)
+  
+  if(CLR_CMAKE_PLATFORM_LINUX)
+    add_definitions(-D__LINUX__=1)
+    if(CLR_CMAKE_PLATFORM_UNIX_TARGET_AMD64)
+      message("Detected Linux x86_64")
+      add_definitions(-DLINUX64)
+    elseif(CLR_CMAKE_PLATFORM_ARCH_ARM)
+      message("Detected Linux ARM")
+      add_definitions(-DLINUX32)
+    elseif(CLR_CMAKE_PLATFORM_ARCH_ARM64)
+      message("Detected Linux ARM64")
+      add_definitions(-DLINUX64)
+    else()
+      clr_unknown_arch()
+    endif()
+      
+  endif(CLR_CMAKE_PLATFORM_LINUX)
+
+  if(CLR_CMAKE_PLATFORM_FREEBSD)
+    message("Detected FreeBSD amd64")
+  endif(CLR_CMAKE_PLATFORM_FREEBSD)
+
+endif(CLR_CMAKE_PLATFORM_UNIX)
+
 if(CLR_CMAKE_PLATFORM_UNIX)
     add_subdirectory(src/ToolBox/SOS/lldbplugin)
     add_subdirectory(src/pal)
-    add_subdirectory(src/coreclr/hosts/unixcorerun)
     add_subdirectory(src/corefx)
+    add_subdirectory(src/coreclr/hosts/unixcorerun)
     add_subdirectory(src/coreclr/hosts/unixcoreconsole)
 endif(CLR_CMAKE_PLATFORM_UNIX)
 
@@ -238,7 +264,6 @@ if(CLR_CMAKE_PLATFORM_DARWIN)
     add_subdirectory(src/coreclr/hosts/osxbundlerun)
 endif(CLR_CMAKE_PLATFORM_DARWIN)
 
-
 # Add this subdir. We install the headers for the jit.
 
 add_subdirectory(src/pal/prebuilt/inc)
@@ -438,39 +463,19 @@ endif(WIN32)
 
 
 if (CLR_CMAKE_PLATFORM_UNIX)
-  add_definitions(-DPLATFORM_UNIX=1)
   add_definitions(-DFEATURE_PAL_SXS)
   add_definitions(-DFEATURE_COREFX_GLOBALIZATION)
+  add_definitions(-DFEATURE_PAL)
 
   if(IS_64BIT_BUILD)
     add_definitions(-DBIT64=1)
   endif(IS_64BIT_BUILD)
-
-  add_definitions(-DFEATURE_PAL)
-    
-  if(CLR_CMAKE_PLATFORM_LINUX)
-    add_definitions(-D__LINUX__=1)
-    if(CLR_CMAKE_PLATFORM_UNIX_TARGET_AMD64)
-      message("Detected Linux x86_64")
-      add_definitions(-DLINUX64)
-    elseif(CLR_CMAKE_PLATFORM_ARCH_ARM)
-      message("Detected Linux ARM")
-      add_definitions(-DLINUX32)
-    elseif(CLR_CMAKE_PLATFORM_ARCH_ARM64)
-      message("Detected Linux ARM64")
-      add_definitions(-DLINUX64)
-    else()
-      clr_unknown_arch()
-    endif()
-      
-  endif(CLR_CMAKE_PLATFORM_LINUX)
+  
   if(CLR_CMAKE_PLATFORM_DARWIN)
     message("Detected OSX x86_64")
     add_definitions(-D_XOPEN_SOURCE)
   endif(CLR_CMAKE_PLATFORM_DARWIN)
-  if(CLR_CMAKE_PLATFORM_FREEBSD)
-    message("Detected FreeBSD amd64")
-  endif(CLR_CMAKE_PLATFORM_FREEBSD)
+
 endif(CLR_CMAKE_PLATFORM_UNIX)
 
 
index db7301b..5c594a8 100644 (file)
@@ -95,12 +95,20 @@ int main(const int argc, const char* argv[])
 {
     // Make sure we have a full path for argv[0].
     std::string argv0AbsolutePath;
-    if (!GetAbsolutePath(argv[0], argv0AbsolutePath))
+    std::string entryPointExecutablePath;
+
+    if (!GetEntrypointExecutableAbsolutePath(entryPointExecutablePath))
     {
         perror("Could not get full path to current executable");
         return -1;
     }
 
+    if (!GetAbsolutePath(entryPointExecutablePath.c_str(), argv0AbsolutePath))
+    {
+        perror("Could not normalize full path to current executable");
+        return -1;
+    }
+
     // We will try to load the managed assembly with the same name as this executable
     // but with the .dll extension.
     std::string programPath(argv0AbsolutePath);
index ecbbf39..1290da3 100644 (file)
@@ -17,8 +17,8 @@
 #include <string.h>
 #include <sys/stat.h>
 
-// The name of the CoreCLR native runtime DLL
 #if defined(__APPLE__)
+#include <mach-o/dyld.h>
 static const char * const coreClrDll = "libcoreclr.dylib";
 #else
 static const char * const coreClrDll = "libcoreclr.so";
@@ -50,6 +50,47 @@ typedef int (*ExecuteAssemblyFunction)(
             const char* managedAssemblyPath,
             unsigned int* exitCode);
 
+#if defined(__LINUX__)
+#define symlinkEntrypointExecutable "/proc/self/exe"
+#elif !defined(__APPLE__)
+#define symlinkEntrypointExecutable "/proc/curproc/exe"
+#endif
+
+bool GetEntrypointExecutableAbsolutePath(std::string& entrypointExecutable)
+{
+    bool result = false;
+    
+    entrypointExecutable.clear();
+
+    // Get path to the executable for the current process using
+    // platform specific means.
+#if defined(__LINUX__) || !defined(__APPLE__)
+    
+    // On non-Mac OS, return the symlink that will be resolved by GetAbsolutePath
+    // to fetch the entrypoint EXE absolute path, inclusive of filename.
+    entrypointExecutable.assign(symlinkEntrypointExecutable);
+    result = true;
+#elif defined(__APPLE__)
+    
+    // On Mac, we ask the OS for the absolute path to the entrypoint executable
+    uint32_t lenActualPath = 0;
+    if (_NSGetExecutablePath(nullptr, &lenActualPath) == -1)
+    {
+        // OSX has placed the actual path length in lenActualPath,
+        // so re-attempt the operation
+        std::string resizedPath(lenActualPath, '\0');
+        char *pResizedPath = const_cast<char *>(resizedPath.c_str());
+        if (_NSGetExecutablePath(pResizedPath, &lenActualPath) == 0)
+        {
+            entrypointExecutable.assign(pResizedPath);
+            result = true;
+        }
+    }
+ #endif 
+
+    return result;
+}
+
 bool GetAbsolutePath(const char* path, std::string& absolutePath)
 {
     bool result = false;
index b587e40..0ce64de 100644 (file)
@@ -5,6 +5,9 @@
 
 #include <string>
 
+// Get the path to entrypoint executable
+bool GetEntrypointExecutableAbsolutePath(std::string& entrypointExecutable);
+
 // Get absolute path from the specified path.
 // Return true in case of a success, false otherwise.
 bool GetAbsolutePath(const char* path, std::string& absolutePath);