1 /*
2 * Copyright (c) 1998, 2013, 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 * Adapted from JDK 1.2 linker_md.c v1.37. Note that we #define
28 * NATIVE here, whether or not we're running solaris native threads.
29 * Outside the VM, it's unclear how we can do the locking that is
30 * done in the green threads version of the code below.
31 */
32 #define NATIVE
33
34 /*
35 * Machine Dependent implementation of the dynamic linking support
36 * for java. This routine is Solaris specific.
37 */
38
39 #include <stdio.h>
40 #include <dlfcn.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "util.h"
46 #include "path_md.h"
47 #ifndef NATIVE
48 #include "iomgr.h"
49 #include "threads_md.h"
50 #endif
51
52 #ifdef __APPLE__
53 #define LIB_SUFFIX "dylib"
54 #else
55 #define LIB_SUFFIX "so"
56 #endif
57
dll_build_name(char * buffer,size_t buflen,const char * paths,const char * fname)58 static void dll_build_name(char* buffer, size_t buflen,
59 const char* paths, const char* fname) {
60 char *path, *paths_copy, *next_token;
61 *buffer = '\0';
62
63 paths_copy = jvmtiAllocate((int)strlen(paths) + 1);
64 strcpy(paths_copy, paths);
65 if (paths_copy == NULL) {
66 return;
67 }
68
69 next_token = NULL;
70 path = strtok_r(paths_copy, PATH_SEPARATOR, &next_token);
71
72 while (path != NULL) {
73 size_t result_len = (size_t)snprintf(buffer, buflen, "%s/lib%s." LIB_SUFFIX, path, fname);
74 if (result_len >= buflen) {
75 EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, "
76 "likely by sun.boot.library.path, is too long.");
77 } else if (access(buffer, F_OK) == 0) {
78 break;
79 }
80 *buffer = '\0';
81 path = strtok_r(NULL, PATH_SEPARATOR, &next_token);
82 }
83
84 jvmtiDeallocate(paths_copy);
85 }
86
87 /*
88 * create a string for the JNI native function name by adding the
89 * appropriate decorations.
90 */
91 int
dbgsysBuildFunName(char * name,int nameLen,int args_size,int encodingIndex)92 dbgsysBuildFunName(char *name, int nameLen, int args_size, int encodingIndex)
93 {
94 /* On Solaris, there is only one encoding method. */
95 if (encodingIndex == 0)
96 return 1;
97 return 0;
98 }
99
100 /*
101 * create a string for the dynamic lib open call by adding the
102 * appropriate pre and extensions to a filename and the path
103 */
104 void
dbgsysBuildLibName(char * holder,int holderlen,const char * pname,const char * fname)105 dbgsysBuildLibName(char *holder, int holderlen, const char *pname, const char *fname)
106 {
107 const int pnamelen = pname ? strlen(pname) : 0;
108
109 if (pnamelen == 0) {
110 if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
111 EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, "
112 "likely by sun.boot.library.path, is too long.");
113 }
114 (void)snprintf(holder, holderlen, "lib%s." LIB_SUFFIX, fname);
115 } else {
116 dll_build_name(holder, holderlen, pname, fname);
117 }
118 }
119
120 #ifndef NATIVE
121 extern int thr_main(void);
122 #endif
123
124 void *
dbgsysLoadLibrary(const char * name,char * err_buf,int err_buflen)125 dbgsysLoadLibrary(const char *name, char *err_buf, int err_buflen)
126 {
127 void * result;
128 #ifdef NATIVE
129 result = dlopen(name, RTLD_LAZY);
130 #else
131 sysMonitorEnter(greenThreadSelf(), &_dl_lock);
132 result = dlopen(name, RTLD_NOW);
133 sysMonitorExit(greenThreadSelf(), &_dl_lock);
134 /*
135 * This is a bit of bulletproofing to catch the commonly occurring
136 * problem of people loading a library which depends on libthread into
137 * the VM. thr_main() should always return -1 which means that libthread
138 * isn't loaded.
139 */
140 if (thr_main() != -1) {
141 VM_CALL(panic)("libthread loaded into green threads");
142 }
143 #endif
144 if (result == NULL) {
145 (void)strncpy(err_buf, dlerror(), err_buflen-2);
146 err_buf[err_buflen-1] = '\0';
147 }
148 return result;
149 }
150
dbgsysUnloadLibrary(void * handle)151 void dbgsysUnloadLibrary(void *handle)
152 {
153 #ifndef NATIVE
154 sysMonitorEnter(greenThreadSelf(), &_dl_lock);
155 #endif
156 (void)dlclose(handle);
157 #ifndef NATIVE
158 sysMonitorExit(greenThreadSelf(), &_dl_lock);
159 #endif
160 }
161
dbgsysFindLibraryEntry(void * handle,const char * name)162 void * dbgsysFindLibraryEntry(void *handle, const char *name)
163 {
164 void * sym;
165 #ifndef NATIVE
166 sysMonitorEnter(greenThreadSelf(), &_dl_lock);
167 #endif
168 sym = dlsym(handle, name);
169 #ifndef NATIVE
170 sysMonitorExit(greenThreadSelf(), &_dl_lock);
171 #endif
172 return sym;
173 }
174