17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7257d1b4Sraf * Common Development and Distribution License (the "License"). 6*7257d1b4Sraf * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21e8031f0aSraf 227c478bd9Sstevel@tonic-gate /* 23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <dlfcn.h> 307c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 317c478bd9Sstevel@tonic-gate #include <sys/kobj.h> 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_module.h> 347c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h> 357c478bd9Sstevel@tonic-gate #include <mdb/mdb_gelf.h> 367c478bd9Sstevel@tonic-gate #include <mdb/mdb_string.h> 377c478bd9Sstevel@tonic-gate #include <mdb/mdb.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate /* 407c478bd9Sstevel@tonic-gate * kmdb libdl-style interface for the manipulation of dmods. 417c478bd9Sstevel@tonic-gate */ 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate static char *dl_errstr; 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate static kmdb_modctl_t * 467c478bd9Sstevel@tonic-gate dl_name2ctl(const char *pathname) 477c478bd9Sstevel@tonic-gate { 487c478bd9Sstevel@tonic-gate mdb_var_t *v; 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate if ((v = mdb_nv_lookup(&mdb.m_dmodctl, strbasename(pathname))) == 517c478bd9Sstevel@tonic-gate NULL) 527c478bd9Sstevel@tonic-gate return (NULL); 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate return ((kmdb_modctl_t *)MDB_NV_COOKIE(v)); 557c478bd9Sstevel@tonic-gate } 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 587c478bd9Sstevel@tonic-gate void * 597c478bd9Sstevel@tonic-gate dlmopen(Lmid_t lmid, const char *pathname, int mode) 607c478bd9Sstevel@tonic-gate { 617c478bd9Sstevel@tonic-gate kmdb_modctl_t *kmc; 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate if ((kmc = dl_name2ctl(pathname)) == NULL) { 647c478bd9Sstevel@tonic-gate dl_errstr = "unregistered module"; 657c478bd9Sstevel@tonic-gate return (NULL); 667c478bd9Sstevel@tonic-gate } 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate kmc->kmc_dlrefcnt++; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate dl_errstr = NULL; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate return (kmc); 737c478bd9Sstevel@tonic-gate } 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate int 767c478bd9Sstevel@tonic-gate dlclose(void *dlp) 777c478bd9Sstevel@tonic-gate { 787c478bd9Sstevel@tonic-gate kmdb_modctl_t *kmc = dlp; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate dl_errstr = NULL; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate ASSERT(kmc->kmc_dlrefcnt > 0); 837c478bd9Sstevel@tonic-gate if (--kmc->kmc_dlrefcnt > 0) 847c478bd9Sstevel@tonic-gate return (0); 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate return (0); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate char * 907c478bd9Sstevel@tonic-gate dlerror(void) 917c478bd9Sstevel@tonic-gate { 927c478bd9Sstevel@tonic-gate char *str = dl_errstr; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate dl_errstr = NULL; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate return (str); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate static void * 1007c478bd9Sstevel@tonic-gate dl_findsym(kmdb_modctl_t *kmc, const char *name) 1017c478bd9Sstevel@tonic-gate { 1027c478bd9Sstevel@tonic-gate GElf_Sym sym; 1037c478bd9Sstevel@tonic-gate uint_t symid; 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate if (mdb_gelf_symtab_lookup_by_name(kmc->kmc_symtab, name, &sym, 1067c478bd9Sstevel@tonic-gate &symid) < 0) 1077c478bd9Sstevel@tonic-gate return (NULL); 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate return ((void *)(uintptr_t)sym.st_value); 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1137c478bd9Sstevel@tonic-gate void * 1147c478bd9Sstevel@tonic-gate dlsym(void *dlp, const char *name) 1157c478bd9Sstevel@tonic-gate { 1167c478bd9Sstevel@tonic-gate kmdb_modctl_t *kmc = dlp; 1177c478bd9Sstevel@tonic-gate mdb_var_t *v; 1187c478bd9Sstevel@tonic-gate void *addr; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate switch ((uintptr_t)dlp) { 1217c478bd9Sstevel@tonic-gate case (uintptr_t)RTLD_NEXT: 1227c478bd9Sstevel@tonic-gate mdb_nv_rewind(&mdb.m_dmodctl); 1237c478bd9Sstevel@tonic-gate while ((v = mdb_nv_advance(&mdb.m_dmodctl)) != NULL) { 1247c478bd9Sstevel@tonic-gate if ((addr = dl_findsym(MDB_NV_COOKIE(v), name)) != NULL) 1257c478bd9Sstevel@tonic-gate break; 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate break; 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate case (uintptr_t)RTLD_DEFAULT: 1307c478bd9Sstevel@tonic-gate case (uintptr_t)RTLD_SELF: 1317c478bd9Sstevel@tonic-gate dl_errstr = "invalid handle"; 1327c478bd9Sstevel@tonic-gate return (NULL); 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate default: 1357c478bd9Sstevel@tonic-gate addr = dl_findsym(kmc, name); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate dl_errstr = (addr == NULL) ? "symbol not found" : NULL; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate return (addr); 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate 143*7257d1b4Sraf #pragma weak _dladdr1 = dladdr1 1447c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1457c478bd9Sstevel@tonic-gate int 146*7257d1b4Sraf dladdr1(void *address, Dl_info *dlip, void **info, int flags) 1477c478bd9Sstevel@tonic-gate { 1487c478bd9Sstevel@tonic-gate /* 1497c478bd9Sstevel@tonic-gate * umem uses this for debugging information. We'll pretend to fail. 1507c478bd9Sstevel@tonic-gate */ 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate return (0); 1537c478bd9Sstevel@tonic-gate } 154