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     *buffer = '\0';
  48     while (strlen(pathname) > 0) {
  49         char *p = strchr(pathname, *path_sep);
  50         if (p == NULL) {
  51             p = pathname + strlen(pathname);
  52         }
  53         /* check for NULL path */
  54         if (p == pathname) {
  55             continue;
  56         }
  57         if (*(p-1) == ':' || *(p-1) == '\\') {
  58             (void)_snprintf(buffer, buflen, "%.*s%s.dll", (int)(p - pathname),
  59                             pathname, fname);
  60         } else {
  61             (void)_snprintf(buffer, buflen, "%.*s\\%s.dll", (int)(p - pathname),
  62                             pathname, fname);
  63         }
  64         if (_access(buffer, 0) == 0) {
  65             break;
  66         }
  67         if (*p == '\0') {
  68             pathname = p;
  69         } else {
  70             pathname = p + 1;
  71         }
  72         *buffer = '\0';
  73     }
  74 }
  75 
  76 /*
  77  * From system_md.c v1.54
  78  */
  79 int
  80 dbgsysGetLastErrorString(char *buf, int len)
  81 {
  82     long errval;
  83 
  84     if ((errval = GetLastError()) != 0) {
  85         /* DOS error */
  86         int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
  87                               NULL, errval,
  88                               0, buf, len, NULL);
  89         if (n > 3) {
  90             /* Drop final '.', CR, LF */
  91             if (buf[n - 1] == '\n') n--;
  92             if (buf[n - 1] == '\r') n--;
  93             if (buf[n - 1] == '.') n--;
  94             buf[n] = '\0';
  95         }
  96         return n;
  97     }
  98 
  99     if (errno != 0) {
 100         /* C runtime error that has no corresponding DOS error code */
 101         const char *s = strerror(errno);
 102         int n = (int)strlen(s);
 103         if (n >= len) n = len - 1;
 104         strncpy(buf, s, n);
 105         buf[n] = '\0';
 106         return n;
 107     }
 108 
 109     return 0;
 110 }
 111 
 112 /*
 113  * Build a machine dependent library name out of a path and file name.
 114  */
 115 void
 116 dbgsysBuildLibName(char *holder, int holderlen, char *pname, char *fname)
 117 {
 118     const int pnamelen = pname ? (int)strlen(pname) : 0;
 119 
 120     *holder = '\0';
 121     /* Quietly truncates on buffer overflow. Should be an error. */
 122     if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
 123         return;
 124     }
 125 
 126     if (pnamelen == 0) {
 127         sprintf(holder, "%s.dll", fname);
 128     } else {
 129       dll_build_name(holder, holderlen, pname, fname);
 130     }
 131 }
 132 
 133 void *
 134 dbgsysLoadLibrary(const char * name, char *err_buf, int err_buflen)
 135 {
 136     void *result = LoadLibrary(name);
 137     if (result == NULL) {
 138         /* Error message is pretty lame, try to make a better guess. */
 139         long errcode = GetLastError();
 140         if (errcode == ERROR_MOD_NOT_FOUND) {
 141             strncpy(err_buf, "Can't find dependent libraries", err_buflen-2);
 142             err_buf[err_buflen-1] = '\0';
 143         } else {
 144             dbgsysGetLastErrorString(err_buf, err_buflen);
 145         }
 146     }
 147     return result;
 148 }
 149 
 150 void dbgsysUnloadLibrary(void *handle)
 151 {
 152     FreeLibrary(handle);
 153 }
 154 
 155 void * dbgsysFindLibraryEntry(void *handle, const char *name)
 156 {
 157     return GetProcAddress(handle, name);
 158 }