1 /*
   2  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "oops/oop.inline.hpp"
  27 #include "runtime/hpi.hpp"
  28 #include "runtime/os.hpp"
  29 
  30 typedef jint (JNICALL *init_t)(GetInterfaceFunc *, void *);
  31 
  32 void hpi::initialize_get_interface(vm_calls_t *callbacks)
  33 {
  34   // Build name of HPI.
  35   char lib_name[JVM_MAXPATHLEN];
  36 
  37   if (HPILibPath && HPILibPath[0]) {
  38     strncpy(lib_name, HPILibPath, JVM_MAXPATHLEN - 1);
  39     lib_name[JVM_MAXPATHLEN - 1] = '\0';
  40   } else {
  41     os::jvm_path(lib_name, sizeof lib_name);
  42 
  43 #ifdef PRODUCT
  44     const char *hpi_lib = "\\hpi.dll";
  45 #else
  46     char *ptr = strrchr(lib_name, '\\');
  47     //  On Win98 GetModuleFileName() returns the path in the upper case.
  48     assert(_strnicmp(ptr, "\\jvm",4) == 0, "invalid library name");
  49     const char *hpi_lib = (_strnicmp(ptr, "\\jvm_g",6) == 0) ? "\\hpi_g.dll" : "\\hpi.dll";
  50 #endif
  51 
  52     *(::strrchr(lib_name, '\\')) = '\0';  /* get rid of "\\jvm.dll" */
  53     char *p = ::strrchr(lib_name, '\\');
  54     if (p != NULL) *p = '\0';             /* get rid of "\\hotspot" */
  55     strcat(lib_name, hpi_lib);
  56   }
  57 
  58   // Load it.
  59   if (TraceHPI) tty->print_cr("Loading HPI %s ", lib_name);
  60   HINSTANCE lib_handle = LoadLibrary(lib_name);
  61   if (lib_handle == NULL) {
  62     if (TraceHPI) tty->print_cr("LoadLibrary failed, code = %d", GetLastError());
  63     return;
  64   }
  65 
  66   // Find hpi initializer.
  67   init_t initer = (init_t)GetProcAddress(lib_handle, "DLL_Initialize");
  68   if (initer == NULL) {
  69     if (TraceHPI) tty->print_cr("GetProcAddress failed, errcode = %d", GetLastError());
  70     return;
  71   }
  72 
  73   // Call initializer.
  74   jint init_result = (*initer)(&_get_interface, callbacks);
  75   if (init_result < 0) {
  76     if (TraceHPI) tty->print_cr("DLL_Initialize failed, returned %ld", init_result);
  77     return;
  78   }
  79 
  80   if (TraceHPI) tty->print_cr("success");
  81   return;
  82 }