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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 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. 12 * 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] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <dlfcn.h> 28 #include <sys/modctl.h> 29 #include <sys/kobj.h> 30 31 #include <kmdb/kmdb_module.h> 32 #include <mdb/mdb_debug.h> 33 #include <mdb/mdb_gelf.h> 34 #include <mdb/mdb_string.h> 35 #include <mdb/mdb.h> 36 37 /* 38 * kmdb libdl-style interface for the manipulation of dmods. 39 */ 40 41 static char *dl_errstr; 42 43 static kmdb_modctl_t * 44 dl_name2ctl(const char *pathname) 45 { 46 mdb_var_t *v; 47 48 if ((v = mdb_nv_lookup(&mdb.m_dmodctl, strbasename(pathname))) == 49 NULL) 50 return (NULL); 51 52 return ((kmdb_modctl_t *)MDB_NV_COOKIE(v)); 53 } 54 55 /*ARGSUSED*/ 56 void * 57 dlmopen(Lmid_t lmid, const char *pathname, int mode) 58 { 59 kmdb_modctl_t *kmc; 60 61 if ((kmc = dl_name2ctl(pathname)) == NULL) { 62 dl_errstr = "unregistered module"; 63 return (NULL); 64 } 65 66 kmc->kmc_dlrefcnt++; 67 68 dl_errstr = NULL; 69 70 return (kmc); 71 } 72 73 int 74 dlclose(void *dlp) 75 { 76 kmdb_modctl_t *kmc = dlp; 77 78 dl_errstr = NULL; 79 80 ASSERT(kmc->kmc_dlrefcnt > 0); 81 if (--kmc->kmc_dlrefcnt > 0) 82 return (0); 83 84 return (0); 85 } 86 87 char * 88 dlerror(void) 89 { 90 char *str = dl_errstr; 91 92 dl_errstr = NULL; 93 94 return (str); 95 } 96 97 static void * 98 dl_findsym(kmdb_modctl_t *kmc, const char *name) 99 { 100 GElf_Sym sym; 101 uint_t symid; 102 103 if (mdb_gelf_symtab_lookup_by_name(kmc->kmc_symtab, name, &sym, 104 &symid) < 0) 105 return (NULL); 106 107 return ((void *)(uintptr_t)sym.st_value); 108 } 109 110 /*ARGSUSED*/ 111 void * 112 dlsym(void *dlp, const char *name) 113 { 114 kmdb_modctl_t *kmc = dlp; 115 mdb_var_t *v; 116 void *addr; 117 118 switch ((uintptr_t)dlp) { 119 case (uintptr_t)RTLD_NEXT: 120 mdb_nv_rewind(&mdb.m_dmodctl); 121 while ((v = mdb_nv_advance(&mdb.m_dmodctl)) != NULL) { 122 if ((addr = dl_findsym(MDB_NV_COOKIE(v), name)) != NULL) 123 break; 124 } 125 break; 126 127 case (uintptr_t)RTLD_DEFAULT: 128 case (uintptr_t)RTLD_SELF: 129 dl_errstr = "invalid handle"; 130 return (NULL); 131 132 default: 133 addr = dl_findsym(kmc, name); 134 } 135 136 dl_errstr = (addr == NULL) ? "symbol not found" : NULL; 137 138 return (addr); 139 } 140 141 #pragma weak _dladdr1 = dladdr1 142 /*ARGSUSED*/ 143 int 144 dladdr1(void *address, Dl_info *dlip, void **info, int flags) 145 { 146 /* 147 * umem uses this for debugging information. We'll pretend to fail. 148 */ 149 150 return (0); 151 } 152