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