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 (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2012 by Delphix. All rights reserved. 25 * Copyright 2019 Joyent, Inc. 26 * Copyright 2021 Oxide Computer Company 27 */ 28 29 #include <sys/param.h> 30 #include <unistd.h> 31 #include <strings.h> 32 #include <dlfcn.h> 33 #include <ctype.h> 34 #include <link.h> 35 36 #include <mdb/mdb_module.h> 37 #include <mdb/mdb_modapi.h> 38 #include <mdb/mdb_debug.h> 39 #include <mdb/mdb_string.h> 40 #include <mdb/mdb_err.h> 41 #include <mdb/mdb_io.h> 42 #include <mdb/mdb_frame.h> 43 #include <mdb/mdb.h> 44 45 int 46 mdb_module_load(const char *name, int mode) 47 { 48 const char *wformat = "no module '%s' could be found\n"; 49 const char *fullname = NULL; 50 char buf[MAXPATHLEN], *p, *q; 51 int i; 52 53 ASSERT(!(mode & MDB_MOD_DEFER)); 54 55 if (strchr(name, '/') != NULL) { 56 ASSERT(!(mode & MDB_MOD_BUILTIN)); 57 58 (void) mdb_iob_snprintf(buf, sizeof (buf), "%s", 59 strbasename(name)); 60 61 /* 62 * Remove any .so(.[0-9]+)? suffix 63 */ 64 if ((p = strrchr(buf, '.')) != NULL) { 65 for (q = p + 1; isdigit(*q); q++) 66 ; 67 68 if (*q == '\0') { 69 if (q > p + 1) { 70 71 /* found digits to remove */ 72 *p = '\0'; 73 } 74 } 75 if ((p = strrchr(buf, '.')) != NULL) { 76 if (strcmp(p, ".so") == 0) { 77 *p = '\0'; 78 } 79 } 80 } 81 fullname = name; 82 name = buf; 83 } 84 85 if (!mdb_module_validate_name(name, &wformat)) 86 goto err; 87 88 if (fullname != NULL) { 89 if (access(fullname, F_OK) != 0) { 90 name = fullname; /* for warn() below */ 91 goto err; 92 } 93 return (mdb_module_create(name, fullname, mode, NULL)); 94 } 95 96 /* 97 * If a simple name is specified, search for it in the module path. 98 * The module path is searched in order, and for each element we 99 * look for the following files: 100 * 101 * 1. If the module name ends in ".so(.[0-9]+)?", search for the literal 102 * name and then search for the name without the [0-9]+ suffix. 103 * 2. If the module name ends in ".so", search for the literal name. 104 * 3. Search for the module name with ".so" appended. 105 * 106 * Once a matching file is detected, we attempt to load that module 107 * and do not resume our search in the case of an error. 108 */ 109 for (i = 0; mdb.m_lpath[i] != NULL; i++) { 110 if ((p = strrchr(name, '.')) != NULL && *++p != '\0') { 111 if (strisnum(p) || strcmp(p, "so") == 0) { 112 (void) mdb_iob_snprintf(buf, sizeof (buf), 113 "%s/%s", mdb.m_lpath[i], name); 114 mdb_dprintf(MDB_DBG_MODULE, 115 "checking for %s\n", buf); 116 if (access(buf, F_OK) == 0) { 117 return (mdb_module_create(name, buf, 118 mode, NULL)); 119 } 120 } 121 122 while (strisnum(p) && (p = strrchr(buf, '.')) != NULL) { 123 *p = '\0'; /* strip trailing digits */ 124 mdb_dprintf(MDB_DBG_MODULE, 125 "checking for %s\n", buf); 126 if (access(buf, F_OK) == 0) { 127 return (mdb_module_create(name, buf, 128 mode, NULL)); 129 } 130 } 131 } 132 133 (void) mdb_iob_snprintf(buf, sizeof (buf), "%s/%s.so", 134 mdb.m_lpath[i], name); 135 136 mdb_dprintf(MDB_DBG_MODULE, "checking for %s\n", buf); 137 138 if (access(buf, F_OK) == 0) 139 return (mdb_module_create(name, buf, mode, NULL)); 140 } 141 err: 142 if (!(mode & MDB_MOD_SILENT)) 143 warn(wformat, name); 144 145 return (-1); 146 } 147 148 typedef struct mdb_modload_data { 149 int mld_first; 150 int mld_mode; 151 } mdb_modload_data_t; 152 153 /*ARGSUSED*/ 154 static int 155 module_load(void *fp, const mdb_map_t *map, const char *fullname) 156 { 157 mdb_modload_data_t *mld = fp; 158 const char *name = strbasename(fullname); 159 160 if (mdb_module_load(name, mld->mld_mode) == 0 && mdb.m_term != NULL) { 161 if (mld->mld_first == TRUE) { 162 mdb_iob_puts(mdb.m_out, "Loading modules: ["); 163 mld->mld_first = FALSE; 164 } 165 mdb_iob_printf(mdb.m_out, " %s", name); 166 mdb_iob_flush(mdb.m_out); 167 } 168 169 if (strstr(fullname, "/libc/") != NULL) { 170 /* 171 * A bit of a kludge: because we manage alternately capable 172 * libc instances by mounting the appropriately capable 173 * instance over /lib/libc.so.1, we may find that our object 174 * list does not contain libc.so.1, but rather one of its 175 * hwcap variants. Unfortunately, there is not a way of 176 * getting from this shared object to the object that it is 177 * effectively interposing on -- which means that without 178 * special processing, we will not load any libc.so dmod. So 179 * if we see that we have a shared object coming out of the 180 * "libc" directory, we assume that we have a "libc-like" 181 * object, and explicitly load the "libc.so" dmod. 182 */ 183 return (module_load(fp, map, "libc.so.1")); 184 } 185 186 if (strstr(fullname, "ld.so") != NULL) { 187 /* 188 * This is even more of a kludge. But if we find something has 189 * basically tried to load ld, we will always implicitly load 190 * the list dmod because several binaries and libraries just 191 * build it as a .o and include it in their ELF object. 192 */ 193 (void) mdb_module_load("list", mld->mld_mode); 194 } 195 196 return (0); 197 } 198 199 void 200 mdb_module_load_all(int mode) 201 { 202 uint_t oflag = mdb_iob_getflags(mdb.m_out) & MDB_IOB_PGENABLE; 203 mdb_modload_data_t mld; 204 205 mld.mld_first = TRUE; 206 mld.mld_mode = mode | MDB_MOD_LOCAL | MDB_MOD_SILENT; 207 208 mdb_iob_clrflags(mdb.m_out, oflag); 209 210 (void) mdb_tgt_object_iter(mdb.m_target, module_load, &mld); 211 212 if (mdb.m_term != NULL && mld.mld_first == FALSE) 213 mdb_iob_puts(mdb.m_out, " ]\n"); 214 215 mdb_iob_setflags(mdb.m_out, oflag); 216 } 217 218 /*ARGSUSED*/ 219 int 220 mdb_module_unload(const char *name, int mode) 221 { 222 ASSERT((mode & ~MDB_MOD_SILENT) == 0); 223 224 return (mdb_module_unload_common(name)); 225 } 226