dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / mdb / common / kmdb / kmdb_dl.c
blob781f60fc909ec6a98d4fe0e673c6d48c9d777964
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <dlfcn.h>
30 #include <sys/modctl.h>
31 #include <sys/kobj.h>
33 #include <kmdb/kmdb_module.h>
34 #include <mdb/mdb_debug.h>
35 #include <mdb/mdb_gelf.h>
36 #include <mdb/mdb_string.h>
37 #include <mdb/mdb.h>
40 * kmdb libdl-style interface for the manipulation of dmods.
43 static char *dl_errstr;
45 static kmdb_modctl_t *
46 dl_name2ctl(const char *pathname)
48 mdb_var_t *v;
50 if ((v = mdb_nv_lookup(&mdb.m_dmodctl, strbasename(pathname))) ==
51 NULL)
52 return (NULL);
54 return ((kmdb_modctl_t *)MDB_NV_COOKIE(v));
57 /*ARGSUSED*/
58 void *
59 dlmopen(Lmid_t lmid, const char *pathname, int mode)
61 kmdb_modctl_t *kmc;
63 if ((kmc = dl_name2ctl(pathname)) == NULL) {
64 dl_errstr = "unregistered module";
65 return (NULL);
68 kmc->kmc_dlrefcnt++;
70 dl_errstr = NULL;
72 return (kmc);
75 int
76 dlclose(void *dlp)
78 kmdb_modctl_t *kmc = dlp;
80 dl_errstr = NULL;
82 ASSERT(kmc->kmc_dlrefcnt > 0);
83 if (--kmc->kmc_dlrefcnt > 0)
84 return (0);
86 return (0);
89 char *
90 dlerror(void)
92 char *str = dl_errstr;
94 dl_errstr = NULL;
96 return (str);
99 static void *
100 dl_findsym(kmdb_modctl_t *kmc, const char *name)
102 GElf_Sym sym;
103 uint_t symid;
105 if (mdb_gelf_symtab_lookup_by_name(kmc->kmc_symtab, name, &sym,
106 &symid) < 0)
107 return (NULL);
109 return ((void *)(uintptr_t)sym.st_value);
112 /*ARGSUSED*/
113 void *
114 dlsym(void *dlp, const char *name)
116 kmdb_modctl_t *kmc = dlp;
117 mdb_var_t *v;
118 void *addr;
120 switch ((uintptr_t)dlp) {
121 case (uintptr_t)RTLD_NEXT:
122 mdb_nv_rewind(&mdb.m_dmodctl);
123 while ((v = mdb_nv_advance(&mdb.m_dmodctl)) != NULL) {
124 if ((addr = dl_findsym(MDB_NV_COOKIE(v), name)) != NULL)
125 break;
127 break;
129 case (uintptr_t)RTLD_DEFAULT:
130 case (uintptr_t)RTLD_SELF:
131 dl_errstr = "invalid handle";
132 return (NULL);
134 default:
135 addr = dl_findsym(kmc, name);
138 dl_errstr = (addr == NULL) ? "symbol not found" : NULL;
140 return (addr);
143 #pragma weak _dladdr1 = dladdr1
144 /*ARGSUSED*/
146 dladdr1(void *address, Dl_info *dlip, void **info, int flags)
149 * umem uses this for debugging information. We'll pretend to fail.
152 return (0);