1 /* 2 * Copyright (c) 1998, 2019, 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 * Machine Dependent implementation of the dynamic linking support 28 * for java. This routine is Unix specific. 29 */ 30 31 #include <stdio.h> 32 #include <dlfcn.h> 33 #include <unistd.h> 34 #include <stdlib.h> 35 #include <string.h> 36 37 #include "path_md.h" 38 39 #ifdef __APPLE__ 40 #define LIB_SUFFIX "dylib" 41 #else 42 #define LIB_SUFFIX "so" 43 #endif 44 45 static void dll_build_name(char* buffer, size_t buflen, 46 const char* paths, const char* fname) { 47 char *path, *paths_copy, *next_token; 48 49 paths_copy = strdup(paths); 50 if (paths_copy == NULL) { 51 return; 52 } 53 54 next_token = NULL; 55 path = strtok_r(paths_copy, PATH_SEPARATOR, &next_token); 56 57 while (path != NULL) { 58 snprintf(buffer, buflen, "%s/lib%s." LIB_SUFFIX, path, fname); 59 if (access(buffer, F_OK) == 0) { 60 break; 61 } 62 *buffer = '\0'; 63 path = strtok_r(NULL, PATH_SEPARATOR, &next_token); 64 } 65 66 free(paths_copy); 67 } 68 69 /* 70 * create a string for the JNI native function name by adding the 71 * appropriate decorations. 72 */ 73 int 74 dbgsysBuildFunName(char *name, int nameLen, int args_size, int encodingIndex) 75 { 76 // On Solaris, there is only one encoding method. 77 if (encodingIndex == 0) 78 return 1; 79 return 0; 80 } 81 82 /* 83 * create a string for the dynamic lib open call by adding the 84 * appropriate pre and extensions to a filename and the path 85 */ 86 void 87 dbgsysBuildLibName(char *holder, int holderlen, const char *pname, const char *fname) 88 { 89 const int pnamelen = pname ? strlen(pname) : 0; 90 91 *holder = '\0'; 92 // Quietly truncate on buffer overflow. Should be an error. 93 if (pnamelen + (int)strlen(fname) + 10 > holderlen) { 94 return; 95 } 96 97 if (pnamelen == 0) { 98 (void)snprintf(holder, holderlen, "lib%s." LIB_SUFFIX, fname); 99 } else { 100 dll_build_name(holder, holderlen, pname, fname); 101 } 102 } 103 104 void * 105 dbgsysLoadLibrary(const char *name, char *err_buf, int err_buflen) 106 { 107 void * result; 108 result = dlopen(name, RTLD_LAZY); 109 if (result == NULL) { 110 (void)strncpy(err_buf, dlerror(), err_buflen-2); 111 err_buf[err_buflen-1] = '\0'; 112 } 113 return result; 114 } 115 116 void dbgsysUnloadLibrary(void *handle) 117 { 118 (void)dlclose(handle); 119 } 120 121 void * dbgsysFindLibraryEntry(void *handle, const char *name) 122 { 123 return dlsym(handle, name); 124 }