xref: /titanic_50/usr/src/cmd/mdb/common/kmdb/kmdb_dl.c (revision 20c794b39650d115e17a15983b6b82e46238cf45)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <dlfcn.h>
31 #include <sys/modctl.h>
32 #include <sys/kobj.h>
33 
34 #include <kmdb/kmdb_module.h>
35 #include <mdb/mdb_debug.h>
36 #include <mdb/mdb_gelf.h>
37 #include <mdb/mdb_string.h>
38 #include <mdb/mdb.h>
39 
40 /*
41  * kmdb libdl-style interface for the manipulation of dmods.
42  */
43 
44 static char *dl_errstr;
45 
46 static kmdb_modctl_t *
47 dl_name2ctl(const char *pathname)
48 {
49 	mdb_var_t *v;
50 
51 	if ((v = mdb_nv_lookup(&mdb.m_dmodctl, strbasename(pathname))) ==
52 	    NULL)
53 		return (NULL);
54 
55 	return ((kmdb_modctl_t *)MDB_NV_COOKIE(v));
56 }
57 
58 /*ARGSUSED*/
59 void *
60 dlmopen(Lmid_t lmid, const char *pathname, int mode)
61 {
62 	kmdb_modctl_t *kmc;
63 
64 	if ((kmc = dl_name2ctl(pathname)) == NULL) {
65 		dl_errstr = "unregistered module";
66 		return (NULL);
67 	}
68 
69 	kmc->kmc_dlrefcnt++;
70 
71 	dl_errstr = NULL;
72 
73 	return (kmc);
74 }
75 
76 int
77 dlclose(void *dlp)
78 {
79 	kmdb_modctl_t *kmc = dlp;
80 
81 	dl_errstr = NULL;
82 
83 	ASSERT(kmc->kmc_dlrefcnt > 0);
84 	if (--kmc->kmc_dlrefcnt > 0)
85 		return (0);
86 
87 	return (0);
88 }
89 
90 char *
91 dlerror(void)
92 {
93 	char *str = dl_errstr;
94 
95 	dl_errstr = NULL;
96 
97 	return (str);
98 }
99 
100 static void *
101 dl_findsym(kmdb_modctl_t *kmc, const char *name)
102 {
103 	GElf_Sym sym;
104 	uint_t symid;
105 
106 	if (mdb_gelf_symtab_lookup_by_name(kmc->kmc_symtab, name, &sym,
107 	    &symid) < 0)
108 		return (NULL);
109 
110 	return ((void *)(uintptr_t)sym.st_value);
111 }
112 
113 /*ARGSUSED*/
114 void *
115 dlsym(void *dlp, const char *name)
116 {
117 	kmdb_modctl_t *kmc = dlp;
118 	mdb_var_t *v;
119 	void *addr;
120 
121 	switch ((uintptr_t)dlp) {
122 	case (uintptr_t)RTLD_NEXT:
123 		mdb_nv_rewind(&mdb.m_dmodctl);
124 		while ((v = mdb_nv_advance(&mdb.m_dmodctl)) != NULL) {
125 			if ((addr = dl_findsym(MDB_NV_COOKIE(v), name)) != NULL)
126 				break;
127 		}
128 		break;
129 
130 	case (uintptr_t)RTLD_DEFAULT:
131 	case (uintptr_t)RTLD_SELF:
132 		dl_errstr = "invalid handle";
133 		return (NULL);
134 
135 	default:
136 		addr = dl_findsym(kmc, name);
137 	}
138 
139 	dl_errstr = (addr == NULL) ? "symbol not found" : NULL;
140 
141 	return (addr);
142 }
143 
144 #pragma weak dladdr1 = _dladdr1
145 /*ARGSUSED*/
146 int
147 _dladdr1(void *address, Dl_info *dlip, void **info, int flags)
148 {
149 	/*
150 	 * umem uses this for debugging information.  We'll pretend to fail.
151 	 */
152 
153 	return (0);
154 }
155