1 /*
   2  * Copyright (c) 1998, 2012, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * Maintains a list of currently loaded DLLs (Dynamic Link Libraries)
  28  * and their associated handles. Library names are case-insensitive.
  29  */
  30 
  31 #include <windows.h>
  32 #include <stdio.h>
  33 #include <string.h>
  34 #include <errno.h>
  35 #include <io.h>
  36 
  37 #include "sys.h"
  38 
  39 #include "path_md.h"
  40 
  41 static void dll_build_name(char* buffer, size_t buflen,
  42                            const char* pname, const char* fname) {
  43     // Based on os_windows.cpp
  44 
  45     char *path_sep = PATH_SEPARATOR;
  46     char *pathname = (char *)pname;
  47     while (strlen(pathname) > 0) {
  48         char *p = strchr(pathname, *path_sep);
  49         if (p == NULL) {
  50             p = pathname + strlen(pathname);
  51         }
  52         /* check for NULL path */
  53         if (p == pathname) {
  54             continue;
  55         }
  56         if (*(p-1) == ':' || *(p-1) == '\\') {
  57             (void)_snprintf(buffer, buflen, "%.*s%s.dll", (p - pathname),
  58                             pathname, fname);
  59         } else {
  60             (void)_snprintf(buffer, buflen, "%.*s\\%s.dll", (p - pathname),
  61                             pathname, fname);
  62         }
  63         if (_access(buffer, 0) == 0) {
  64             break;
  65         }
  66         pathname = p + 1;
  67         *buffer = '\0';
  68     }
  69 }
  70 
  71 /*
  72  * From system_md.c v1.54
  73  */
  74 int
  75 dbgsysGetLastErrorString(char *buf, int len)
  76 {
  77     long errval;
  78 
  79     if ((errval = GetLastError()) != 0) {
  80         /* DOS error */
  81         int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
  82                               NULL, errval,
  83                               0, buf, len, NULL);
  84         if (n > 3) {
  85             /* Drop final '.', CR, LF */
  86             if (buf[n - 1] == '\n') n--;
  87             if (buf[n - 1] == '\r') n--;
  88             if (buf[n - 1] == '.') n--;
  89             buf[n] = '\0';
  90         }
  91         return n;
  92     }
  93 
  94     if (errno != 0) {
  95         /* C runtime error that has no corresponding DOS error code */
  96         const char *s = strerror(errno);
  97         int n = (int)strlen(s);
  98         if (n >= len) n = len - 1;
  99         strncpy(buf, s, n);
 100         buf[n] = '\0';
 101         return n;
 102     }
 103 
 104     return 0;
 105 }
 106 
 107 /*
 108  * Build a machine dependent library name out of a path and file name.
 109  */
 110 void
 111 dbgsysBuildLibName(char *holder, int holderlen, char *pname, char *fname)
 112 {
 113     const int pnamelen = pname ? (int)strlen(pname) : 0;
 114 
 115     *holder = '\0';
 116     /* Quietly truncates on buffer overflow. Should be an error. */
 117     if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
 118         return;
 119     }
 120 
 121     if (pnamelen == 0) {
 122         sprintf(holder, "%s.dll", fname);
 123     } else {
 124       dll_build_name(holder, holderlen, pname, fname);
 125     }
 126 }
 127 
 128 void *
 129 dbgsysLoadLibrary(const char * name, char *err_buf, int err_buflen)
 130 {
 131     void *result = LoadLibrary(name);
 132     if (result == NULL) {
 133         /* Error message is pretty lame, try to make a better guess. */
 134         long errcode = GetLastError();
 135         if (errcode == ERROR_MOD_NOT_FOUND) {
 136             strncpy(err_buf, "Can't find dependent libraries", err_buflen-2);
 137             err_buf[err_buflen-1] = '\0';
 138         } else {
 139             dbgsysGetLastErrorString(err_buf, err_buflen);
 140         }
 141     }
 142     return result;
 143 }
 144 
 145 void dbgsysUnloadLibrary(void *handle)
 146 {
 147     FreeLibrary(handle);
 148 }
 149 
 150 void * dbgsysFindLibraryEntry(void *handle, const char *name)
 151 {
 152     return GetProcAddress(handle, name);
 153 }