The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_linker.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1997-2000 Doug Rabson
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/8.3/sys/kern/kern_linker.c 227440 2011-11-11 02:15:44Z rstone $");
   29 
   30 #include "opt_ddb.h"
   31 #include "opt_hwpmc_hooks.h"
   32 
   33 #include <sys/param.h>
   34 #include <sys/kernel.h>
   35 #include <sys/systm.h>
   36 #include <sys/malloc.h>
   37 #include <sys/sysproto.h>
   38 #include <sys/sysent.h>
   39 #include <sys/priv.h>
   40 #include <sys/proc.h>
   41 #include <sys/lock.h>
   42 #include <sys/mutex.h>
   43 #include <sys/sx.h>
   44 #include <sys/module.h>
   45 #include <sys/mount.h>
   46 #include <sys/linker.h>
   47 #include <sys/fcntl.h>
   48 #include <sys/jail.h>
   49 #include <sys/libkern.h>
   50 #include <sys/namei.h>
   51 #include <sys/vnode.h>
   52 #include <sys/syscallsubr.h>
   53 #include <sys/sysctl.h>
   54 
   55 #include <net/vnet.h>
   56 
   57 #include <security/mac/mac_framework.h>
   58 
   59 #include "linker_if.h"
   60 
   61 #ifdef HWPMC_HOOKS
   62 #include <sys/pmckern.h>
   63 #endif
   64 
   65 #ifdef KLD_DEBUG
   66 int kld_debug = 0;
   67 #endif
   68 
   69 #define KLD_LOCK()              sx_xlock(&kld_sx)
   70 #define KLD_UNLOCK()            sx_xunlock(&kld_sx)
   71 #define KLD_DOWNGRADE()         sx_downgrade(&kld_sx)
   72 #define KLD_LOCK_READ()         sx_slock(&kld_sx)
   73 #define KLD_UNLOCK_READ()       sx_sunlock(&kld_sx)
   74 #define KLD_LOCKED()            sx_xlocked(&kld_sx)
   75 #define KLD_LOCK_ASSERT() do {                                          \
   76         if (!cold)                                                      \
   77                 sx_assert(&kld_sx, SX_XLOCKED);                         \
   78 } while (0)
   79 
   80 /*
   81  * static char *linker_search_path(const char *name, struct mod_depend
   82  * *verinfo);
   83  */
   84 static const char       *linker_basename(const char *path);
   85 
   86 /*
   87  * Find a currently loaded file given its filename.
   88  */
   89 static linker_file_t linker_find_file_by_name(const char* _filename);
   90 
   91 /*
   92  * Find a currently loaded file given its file id.
   93  */
   94 static linker_file_t linker_find_file_by_id(int _fileid);
   95 
   96 /* Metadata from the static kernel */
   97 SET_DECLARE(modmetadata_set, struct mod_metadata);
   98 
   99 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
  100 
  101 linker_file_t linker_kernel_file;
  102 
  103 static struct sx kld_sx;        /* kernel linker lock */
  104 
  105 /*
  106  * Load counter used by clients to determine if a linker file has been
  107  * re-loaded. This counter is incremented for each file load.
  108  */
  109 static int loadcnt;
  110 
  111 static linker_class_list_t classes;
  112 static linker_file_list_t linker_files;
  113 static int next_file_id = 1;
  114 static int linker_no_more_classes = 0;
  115 
  116 #define LINKER_GET_NEXT_FILE_ID(a) do {                                 \
  117         linker_file_t lftmp;                                            \
  118                                                                         \
  119         KLD_LOCK_ASSERT();                                              \
  120 retry:                                                                  \
  121         TAILQ_FOREACH(lftmp, &linker_files, link) {                     \
  122                 if (next_file_id == lftmp->id) {                        \
  123                         next_file_id++;                                 \
  124                         goto retry;                                     \
  125                 }                                                       \
  126         }                                                               \
  127         (a) = next_file_id;                                             \
  128 } while(0)
  129 
  130 
  131 /* XXX wrong name; we're looking at version provision tags here, not modules */
  132 typedef TAILQ_HEAD(, modlist) modlisthead_t;
  133 struct modlist {
  134         TAILQ_ENTRY(modlist) link;      /* chain together all modules */
  135         linker_file_t   container;
  136         const char      *name;
  137         int             version;
  138 };
  139 typedef struct modlist *modlist_t;
  140 static modlisthead_t found_modules;
  141 
  142 static int      linker_file_add_dependency(linker_file_t file,
  143                     linker_file_t dep);
  144 static caddr_t  linker_file_lookup_symbol_internal(linker_file_t file,
  145                     const char* name, int deps);
  146 static int      linker_load_module(const char *kldname,
  147                     const char *modname, struct linker_file *parent,
  148                     struct mod_depend *verinfo, struct linker_file **lfpp);
  149 static modlist_t modlist_lookup2(const char *name, struct mod_depend *verinfo);
  150 
  151 static char *
  152 linker_strdup(const char *str)
  153 {
  154         char *result;
  155 
  156         if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
  157                 strcpy(result, str);
  158         return (result);
  159 }
  160 
  161 static void
  162 linker_init(void *arg)
  163 {
  164 
  165         sx_init(&kld_sx, "kernel linker");
  166         TAILQ_INIT(&classes);
  167         TAILQ_INIT(&linker_files);
  168 }
  169 
  170 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
  171 
  172 static void
  173 linker_stop_class_add(void *arg)
  174 {
  175 
  176         linker_no_more_classes = 1;
  177 }
  178 
  179 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
  180 
  181 int
  182 linker_add_class(linker_class_t lc)
  183 {
  184 
  185         /*
  186          * We disallow any class registration past SI_ORDER_ANY
  187          * of SI_SUB_KLD.  We bump the reference count to keep the
  188          * ops from being freed.
  189          */
  190         if (linker_no_more_classes == 1)
  191                 return (EPERM);
  192         kobj_class_compile((kobj_class_t) lc);
  193         ((kobj_class_t)lc)->refs++;     /* XXX: kobj_mtx */
  194         TAILQ_INSERT_TAIL(&classes, lc, link);
  195         return (0);
  196 }
  197 
  198 static void
  199 linker_file_sysinit(linker_file_t lf)
  200 {
  201         struct sysinit **start, **stop, **sipp, **xipp, *save;
  202 
  203         KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
  204             lf->filename));
  205 
  206         if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
  207                 return;
  208         /*
  209          * Perform a bubble sort of the system initialization objects by
  210          * their subsystem (primary key) and order (secondary key).
  211          *
  212          * Since some things care about execution order, this is the operation
  213          * which ensures continued function.
  214          */
  215         for (sipp = start; sipp < stop; sipp++) {
  216                 for (xipp = sipp + 1; xipp < stop; xipp++) {
  217                         if ((*sipp)->subsystem < (*xipp)->subsystem ||
  218                             ((*sipp)->subsystem == (*xipp)->subsystem &&
  219                             (*sipp)->order <= (*xipp)->order))
  220                                 continue;       /* skip */
  221                         save = *sipp;
  222                         *sipp = *xipp;
  223                         *xipp = save;
  224                 }
  225         }
  226 
  227         /*
  228          * Traverse the (now) ordered list of system initialization tasks.
  229          * Perform each task, and continue on to the next task.
  230          */
  231         mtx_lock(&Giant);
  232         for (sipp = start; sipp < stop; sipp++) {
  233                 if ((*sipp)->subsystem == SI_SUB_DUMMY)
  234                         continue;       /* skip dummy task(s) */
  235 
  236                 /* Call function */
  237                 (*((*sipp)->func)) ((*sipp)->udata);
  238         }
  239         mtx_unlock(&Giant);
  240 }
  241 
  242 static void
  243 linker_file_sysuninit(linker_file_t lf)
  244 {
  245         struct sysinit **start, **stop, **sipp, **xipp, *save;
  246 
  247         KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
  248             lf->filename));
  249 
  250         if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
  251             NULL) != 0)
  252                 return;
  253 
  254         /*
  255          * Perform a reverse bubble sort of the system initialization objects
  256          * by their subsystem (primary key) and order (secondary key).
  257          *
  258          * Since some things care about execution order, this is the operation
  259          * which ensures continued function.
  260          */
  261         for (sipp = start; sipp < stop; sipp++) {
  262                 for (xipp = sipp + 1; xipp < stop; xipp++) {
  263                         if ((*sipp)->subsystem > (*xipp)->subsystem ||
  264                             ((*sipp)->subsystem == (*xipp)->subsystem &&
  265                             (*sipp)->order >= (*xipp)->order))
  266                                 continue;       /* skip */
  267                         save = *sipp;
  268                         *sipp = *xipp;
  269                         *xipp = save;
  270                 }
  271         }
  272 
  273         /*
  274          * Traverse the (now) ordered list of system initialization tasks.
  275          * Perform each task, and continue on to the next task.
  276          */
  277         mtx_lock(&Giant);
  278         for (sipp = start; sipp < stop; sipp++) {
  279                 if ((*sipp)->subsystem == SI_SUB_DUMMY)
  280                         continue;       /* skip dummy task(s) */
  281 
  282                 /* Call function */
  283                 (*((*sipp)->func)) ((*sipp)->udata);
  284         }
  285         mtx_unlock(&Giant);
  286 }
  287 
  288 static void
  289 linker_file_register_sysctls(linker_file_t lf)
  290 {
  291         struct sysctl_oid **start, **stop, **oidp;
  292 
  293         KLD_DPF(FILE,
  294             ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
  295             lf->filename));
  296 
  297         if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
  298                 return;
  299 
  300         sysctl_lock();
  301         for (oidp = start; oidp < stop; oidp++)
  302                 sysctl_register_oid(*oidp);
  303         sysctl_unlock();
  304 }
  305 
  306 static void
  307 linker_file_unregister_sysctls(linker_file_t lf)
  308 {
  309         struct sysctl_oid **start, **stop, **oidp;
  310 
  311         KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs"
  312             " for %s\n", lf->filename));
  313 
  314         if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
  315                 return;
  316 
  317         sysctl_lock();
  318         for (oidp = start; oidp < stop; oidp++)
  319                 sysctl_unregister_oid(*oidp);
  320         sysctl_unlock();
  321 }
  322 
  323 static int
  324 linker_file_register_modules(linker_file_t lf)
  325 {
  326         struct mod_metadata **start, **stop, **mdp;
  327         const moduledata_t *moddata;
  328         int first_error, error;
  329 
  330         KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
  331             " in %s\n", lf->filename));
  332 
  333         if (linker_file_lookup_set(lf, "modmetadata_set", &start,
  334             &stop, NULL) != 0) {
  335                 /*
  336                  * This fallback should be unnecessary, but if we get booted
  337                  * from boot2 instead of loader and we are missing our
  338                  * metadata then we have to try the best we can.
  339                  */
  340                 if (lf == linker_kernel_file) {
  341                         start = SET_BEGIN(modmetadata_set);
  342                         stop = SET_LIMIT(modmetadata_set);
  343                 } else
  344                         return (0);
  345         }
  346         first_error = 0;
  347         for (mdp = start; mdp < stop; mdp++) {
  348                 if ((*mdp)->md_type != MDT_MODULE)
  349                         continue;
  350                 moddata = (*mdp)->md_data;
  351                 KLD_DPF(FILE, ("Registering module %s in %s\n",
  352                     moddata->name, lf->filename));
  353                 error = module_register(moddata, lf);
  354                 if (error) {
  355                         printf("Module %s failed to register: %d\n",
  356                             moddata->name, error);
  357                         if (first_error == 0)
  358                                 first_error = error;
  359                 }
  360         }
  361         return (first_error);
  362 }
  363 
  364 static void
  365 linker_init_kernel_modules(void)
  366 {
  367 
  368         linker_file_register_modules(linker_kernel_file);
  369 }
  370 
  371 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
  372     0);
  373 
  374 static int
  375 linker_load_file(const char *filename, linker_file_t *result)
  376 {
  377         linker_class_t lc;
  378         linker_file_t lf;
  379         int foundfile, error;
  380 
  381         /* Refuse to load modules if securelevel raised */
  382         if (prison0.pr_securelevel > 0)
  383                 return (EPERM);
  384 
  385         KLD_LOCK_ASSERT();
  386         lf = linker_find_file_by_name(filename);
  387         if (lf) {
  388                 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
  389                     " incrementing refs\n", filename));
  390                 *result = lf;
  391                 lf->refs++;
  392                 return (0);
  393         }
  394         foundfile = 0;
  395         error = 0;
  396 
  397         /*
  398          * We do not need to protect (lock) classes here because there is
  399          * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
  400          * and there is no class deregistration mechanism at this time.
  401          */
  402         TAILQ_FOREACH(lc, &classes, link) {
  403                 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
  404                     filename));
  405                 error = LINKER_LOAD_FILE(lc, filename, &lf);
  406                 /*
  407                  * If we got something other than ENOENT, then it exists but
  408                  * we cannot load it for some other reason.
  409                  */
  410                 if (error != ENOENT)
  411                         foundfile = 1;
  412                 if (lf) {
  413                         error = linker_file_register_modules(lf);
  414                         if (error == EEXIST) {
  415                                 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
  416                                 return (error);
  417                         }
  418                         KLD_UNLOCK();
  419                         linker_file_register_sysctls(lf);
  420                         linker_file_sysinit(lf);
  421                         KLD_LOCK();
  422                         lf->flags |= LINKER_FILE_LINKED;
  423                         *result = lf;
  424                         return (0);
  425                 }
  426         }
  427         /*
  428          * Less than ideal, but tells the user whether it failed to load or
  429          * the module was not found.
  430          */
  431         if (foundfile) {
  432 
  433                 /*
  434                  * If the file type has not been recognized by the last try
  435                  * printout a message before to fail.
  436                  */
  437                 if (error == ENOSYS)
  438                         printf("linker_load_file: Unsupported file type\n");
  439 
  440                 /*
  441                  * Format not recognized or otherwise unloadable.
  442                  * When loading a module that is statically built into
  443                  * the kernel EEXIST percolates back up as the return
  444                  * value.  Preserve this so that apps like sysinstall
  445                  * can recognize this special case and not post bogus
  446                  * dialog boxes.
  447                  */
  448                 if (error != EEXIST)
  449                         error = ENOEXEC;
  450         } else
  451                 error = ENOENT;         /* Nothing found */
  452         return (error);
  453 }
  454 
  455 int
  456 linker_reference_module(const char *modname, struct mod_depend *verinfo,
  457     linker_file_t *result)
  458 {
  459         modlist_t mod;
  460         int error;
  461 
  462         KLD_LOCK();
  463         if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
  464                 *result = mod->container;
  465                 (*result)->refs++;
  466                 KLD_UNLOCK();
  467                 return (0);
  468         }
  469 
  470         error = linker_load_module(NULL, modname, NULL, verinfo, result);
  471         KLD_UNLOCK();
  472         return (error);
  473 }
  474 
  475 int
  476 linker_release_module(const char *modname, struct mod_depend *verinfo,
  477     linker_file_t lf)
  478 {
  479         modlist_t mod;
  480         int error;
  481 
  482         KLD_LOCK();
  483         if (lf == NULL) {
  484                 KASSERT(modname != NULL,
  485                     ("linker_release_module: no file or name"));
  486                 mod = modlist_lookup2(modname, verinfo);
  487                 if (mod == NULL) {
  488                         KLD_UNLOCK();
  489                         return (ESRCH);
  490                 }
  491                 lf = mod->container;
  492         } else
  493                 KASSERT(modname == NULL && verinfo == NULL,
  494                     ("linker_release_module: both file and name"));
  495         error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
  496         KLD_UNLOCK();
  497         return (error);
  498 }
  499 
  500 static linker_file_t
  501 linker_find_file_by_name(const char *filename)
  502 {
  503         linker_file_t lf;
  504         char *koname;
  505 
  506         koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
  507         sprintf(koname, "%s.ko", filename);
  508 
  509         KLD_LOCK_ASSERT();
  510         TAILQ_FOREACH(lf, &linker_files, link) {
  511                 if (strcmp(lf->filename, koname) == 0)
  512                         break;
  513                 if (strcmp(lf->filename, filename) == 0)
  514                         break;
  515         }
  516         free(koname, M_LINKER);
  517         return (lf);
  518 }
  519 
  520 static linker_file_t
  521 linker_find_file_by_id(int fileid)
  522 {
  523         linker_file_t lf;
  524 
  525         KLD_LOCK_ASSERT();
  526         TAILQ_FOREACH(lf, &linker_files, link)
  527                 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
  528                         break;
  529         return (lf);
  530 }
  531 
  532 int
  533 linker_file_foreach(linker_predicate_t *predicate, void *context)
  534 {
  535         linker_file_t lf;
  536         int retval = 0;
  537 
  538         KLD_LOCK();
  539         TAILQ_FOREACH(lf, &linker_files, link) {
  540                 retval = predicate(lf, context);
  541                 if (retval != 0)
  542                         break;
  543         }
  544         KLD_UNLOCK();
  545         return (retval);
  546 }
  547 
  548 linker_file_t
  549 linker_make_file(const char *pathname, linker_class_t lc)
  550 {
  551         linker_file_t lf;
  552         const char *filename;
  553 
  554         KLD_LOCK_ASSERT();
  555         filename = linker_basename(pathname);
  556 
  557         KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
  558         lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
  559         if (lf == NULL)
  560                 return (NULL);
  561         lf->refs = 1;
  562         lf->userrefs = 0;
  563         lf->flags = 0;
  564         lf->filename = linker_strdup(filename);
  565         lf->pathname = linker_strdup(pathname);
  566         LINKER_GET_NEXT_FILE_ID(lf->id);
  567         lf->ndeps = 0;
  568         lf->deps = NULL;
  569         lf->loadcnt = ++loadcnt;
  570         lf->sdt_probes = NULL;
  571         lf->sdt_nprobes = 0;
  572         STAILQ_INIT(&lf->common);
  573         TAILQ_INIT(&lf->modules);
  574         TAILQ_INSERT_TAIL(&linker_files, lf, link);
  575         return (lf);
  576 }
  577 
  578 int
  579 linker_file_unload(linker_file_t file, int flags)
  580 {
  581         module_t mod, next;
  582         modlist_t ml, nextml;
  583         struct common_symbol *cp;
  584         int error, i;
  585 
  586         /* Refuse to unload modules if securelevel raised. */
  587         if (prison0.pr_securelevel > 0)
  588                 return (EPERM);
  589 
  590         KLD_LOCK_ASSERT();
  591         KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
  592 
  593         /* Easy case of just dropping a reference. */
  594         if (file->refs > 1) {
  595                 file->refs--;
  596                 return (0);
  597         }
  598 
  599         KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
  600             " informing modules\n"));
  601 
  602         /*
  603          * Quiesce all the modules to give them a chance to veto the unload.
  604          */
  605         MOD_SLOCK;
  606         for (mod = TAILQ_FIRST(&file->modules); mod;
  607              mod = module_getfnext(mod)) {
  608 
  609                 error = module_quiesce(mod);
  610                 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
  611                         KLD_DPF(FILE, ("linker_file_unload: module %s"
  612                             " vetoed unload\n", module_getname(mod)));
  613                         /*
  614                          * XXX: Do we need to tell all the quiesced modules
  615                          * that they can resume work now via a new module
  616                          * event?
  617                          */
  618                         MOD_SUNLOCK;
  619                         return (error);
  620                 }
  621         }
  622         MOD_SUNLOCK;
  623 
  624         /*
  625          * Inform any modules associated with this file that they are
  626          * being be unloaded.
  627          */
  628         MOD_XLOCK;
  629         for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
  630                 next = module_getfnext(mod);
  631                 MOD_XUNLOCK;
  632 
  633                 /*
  634                  * Give the module a chance to veto the unload.
  635                  */
  636                 if ((error = module_unload(mod)) != 0) {
  637                         KLD_DPF(FILE, ("linker_file_unload: module %s"
  638                             " failed unload\n", module_getname(mod)));
  639                         return (error);
  640                 }
  641                 MOD_XLOCK;
  642                 module_release(mod);
  643         }
  644         MOD_XUNLOCK;
  645 
  646         TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
  647                 if (ml->container == file) {
  648                         TAILQ_REMOVE(&found_modules, ml, link);
  649                         free(ml, M_LINKER);
  650                 }
  651         }
  652 
  653         /*
  654          * Don't try to run SYSUNINITs if we are unloaded due to a
  655          * link error.
  656          */
  657         if (file->flags & LINKER_FILE_LINKED) {
  658                 file->flags &= ~LINKER_FILE_LINKED;
  659                 KLD_UNLOCK();
  660                 linker_file_sysuninit(file);
  661                 linker_file_unregister_sysctls(file);
  662                 KLD_LOCK();
  663         }
  664         TAILQ_REMOVE(&linker_files, file, link);
  665 
  666         if (file->deps) {
  667                 for (i = 0; i < file->ndeps; i++)
  668                         linker_file_unload(file->deps[i], flags);
  669                 free(file->deps, M_LINKER);
  670                 file->deps = NULL;
  671         }
  672         while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
  673                 STAILQ_REMOVE_HEAD(&file->common, link);
  674                 free(cp, M_LINKER);
  675         }
  676 
  677         LINKER_UNLOAD(file);
  678         if (file->filename) {
  679                 free(file->filename, M_LINKER);
  680                 file->filename = NULL;
  681         }
  682         if (file->pathname) {
  683                 free(file->pathname, M_LINKER);
  684                 file->pathname = NULL;
  685         }
  686         kobj_delete((kobj_t) file, M_LINKER);
  687         return (0);
  688 }
  689 
  690 int
  691 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
  692 {
  693         return (LINKER_CTF_GET(file, lc));
  694 }
  695 
  696 static int
  697 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
  698 {
  699         linker_file_t *newdeps;
  700 
  701         KLD_LOCK_ASSERT();
  702         newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
  703             M_LINKER, M_WAITOK | M_ZERO);
  704         if (newdeps == NULL)
  705                 return (ENOMEM);
  706 
  707         if (file->deps) {
  708                 bcopy(file->deps, newdeps,
  709                     file->ndeps * sizeof(linker_file_t *));
  710                 free(file->deps, M_LINKER);
  711         }
  712         file->deps = newdeps;
  713         file->deps[file->ndeps] = dep;
  714         file->ndeps++;
  715         return (0);
  716 }
  717 
  718 /*
  719  * Locate a linker set and its contents.  This is a helper function to avoid
  720  * linker_if.h exposure elsewhere.  Note: firstp and lastp are really void **.
  721  * This function is used in this file so we can avoid having lots of (void **)
  722  * casts.
  723  */
  724 int
  725 linker_file_lookup_set(linker_file_t file, const char *name,
  726     void *firstp, void *lastp, int *countp)
  727 {
  728         int error, locked;
  729 
  730         locked = KLD_LOCKED();
  731         if (!locked)
  732                 KLD_LOCK();
  733         error = LINKER_LOOKUP_SET(file, name, firstp, lastp, countp);
  734         if (!locked)
  735                 KLD_UNLOCK();
  736         return (error);
  737 }
  738 
  739 /*
  740  * List all functions in a file.
  741  */
  742 int
  743 linker_file_function_listall(linker_file_t lf,
  744     linker_function_nameval_callback_t callback_func, void *arg)
  745 {
  746         return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
  747 }
  748 
  749 caddr_t
  750 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
  751 {
  752         caddr_t sym;
  753         int locked;
  754 
  755         locked = KLD_LOCKED();
  756         if (!locked)
  757                 KLD_LOCK();
  758         sym = linker_file_lookup_symbol_internal(file, name, deps);
  759         if (!locked)
  760                 KLD_UNLOCK();
  761         return (sym);
  762 }
  763 
  764 static caddr_t
  765 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
  766     int deps)
  767 {
  768         c_linker_sym_t sym;
  769         linker_symval_t symval;
  770         caddr_t address;
  771         size_t common_size = 0;
  772         int i;
  773 
  774         KLD_LOCK_ASSERT();
  775         KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
  776             file, name, deps));
  777 
  778         if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
  779                 LINKER_SYMBOL_VALUES(file, sym, &symval);
  780                 if (symval.value == 0)
  781                         /*
  782                          * For commons, first look them up in the
  783                          * dependencies and only allocate space if not found
  784                          * there.
  785                          */
  786                         common_size = symval.size;
  787                 else {
  788                         KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
  789                             ".value=%p\n", symval.value));
  790                         return (symval.value);
  791                 }
  792         }
  793         if (deps) {
  794                 for (i = 0; i < file->ndeps; i++) {
  795                         address = linker_file_lookup_symbol_internal(
  796                             file->deps[i], name, 0);
  797                         if (address) {
  798                                 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
  799                                     " deps value=%p\n", address));
  800                                 return (address);
  801                         }
  802                 }
  803         }
  804         if (common_size > 0) {
  805                 /*
  806                  * This is a common symbol which was not found in the
  807                  * dependencies.  We maintain a simple common symbol table in
  808                  * the file object.
  809                  */
  810                 struct common_symbol *cp;
  811 
  812                 STAILQ_FOREACH(cp, &file->common, link) {
  813                         if (strcmp(cp->name, name) == 0) {
  814                                 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
  815                                     " old common value=%p\n", cp->address));
  816                                 return (cp->address);
  817                         }
  818                 }
  819                 /*
  820                  * Round the symbol size up to align.
  821                  */
  822                 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
  823                 cp = malloc(sizeof(struct common_symbol)
  824                     + common_size + strlen(name) + 1, M_LINKER,
  825                     M_WAITOK | M_ZERO);
  826                 cp->address = (caddr_t)(cp + 1);
  827                 cp->name = cp->address + common_size;
  828                 strcpy(cp->name, name);
  829                 bzero(cp->address, common_size);
  830                 STAILQ_INSERT_TAIL(&file->common, cp, link);
  831 
  832                 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
  833                     " value=%p\n", cp->address));
  834                 return (cp->address);
  835         }
  836         KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
  837         return (0);
  838 }
  839 
  840 /*
  841  * Both DDB and stack(9) rely on the kernel linker to provide forward and
  842  * backward lookup of symbols.  However, DDB and sometimes stack(9) need to
  843  * do this in a lockfree manner.  We provide a set of internal helper
  844  * routines to perform these operations without locks, and then wrappers that
  845  * optionally lock.
  846  *
  847  * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
  848  */
  849 #ifdef DDB
  850 static int
  851 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
  852 {
  853         linker_file_t lf;
  854 
  855         TAILQ_FOREACH(lf, &linker_files, link) {
  856                 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
  857                         return (0);
  858         }
  859         return (ENOENT);
  860 }
  861 #endif
  862 
  863 static int
  864 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
  865 {
  866         linker_file_t lf;
  867         c_linker_sym_t best, es;
  868         u_long diff, bestdiff, off;
  869 
  870         best = 0;
  871         off = (uintptr_t)value;
  872         bestdiff = off;
  873         TAILQ_FOREACH(lf, &linker_files, link) {
  874                 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
  875                         continue;
  876                 if (es != 0 && diff < bestdiff) {
  877                         best = es;
  878                         bestdiff = diff;
  879                 }
  880                 if (bestdiff == 0)
  881                         break;
  882         }
  883         if (best) {
  884                 *sym = best;
  885                 *diffp = bestdiff;
  886                 return (0);
  887         } else {
  888                 *sym = 0;
  889                 *diffp = off;
  890                 return (ENOENT);
  891         }
  892 }
  893 
  894 static int
  895 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
  896 {
  897         linker_file_t lf;
  898 
  899         TAILQ_FOREACH(lf, &linker_files, link) {
  900                 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
  901                         return (0);
  902         }
  903         return (ENOENT);
  904 }
  905 
  906 static int
  907 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
  908     long *offset)
  909 {
  910         linker_symval_t symval;
  911         c_linker_sym_t sym;
  912         int error;
  913 
  914         *offset = 0;
  915         error = linker_debug_search_symbol(value, &sym, offset);
  916         if (error)
  917                 return (error);
  918         error = linker_debug_symbol_values(sym, &symval);
  919         if (error)
  920                 return (error);
  921         strlcpy(buf, symval.name, buflen);
  922         return (0);
  923 }
  924 
  925 /*
  926  * DDB Helpers.  DDB has to look across multiple files with their own symbol
  927  * tables and string tables.
  928  *
  929  * Note that we do not obey list locking protocols here.  We really don't need
  930  * DDB to hang because somebody's got the lock held.  We'll take the chance
  931  * that the files list is inconsistant instead.
  932  */
  933 #ifdef DDB
  934 int
  935 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
  936 {
  937 
  938         return (linker_debug_lookup(symstr, sym));
  939 }
  940 #endif
  941 
  942 int
  943 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
  944 {
  945 
  946         return (linker_debug_search_symbol(value, sym, diffp));
  947 }
  948 
  949 int
  950 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
  951 {
  952 
  953         return (linker_debug_symbol_values(sym, symval));
  954 }
  955 
  956 int
  957 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
  958     long *offset)
  959 {
  960 
  961         return (linker_debug_search_symbol_name(value, buf, buflen, offset));
  962 }
  963 
  964 /*
  965  * stack(9) helper for non-debugging environemnts.  Unlike DDB helpers, we do
  966  * obey locking protocols, and offer a significantly less complex interface.
  967  */
  968 int
  969 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
  970     long *offset)
  971 {
  972         int error;
  973 
  974         KLD_LOCK();
  975         error = linker_debug_search_symbol_name(value, buf, buflen, offset);
  976         KLD_UNLOCK();
  977         return (error);
  978 }
  979 
  980 /*
  981  * Syscalls.
  982  */
  983 int
  984 kern_kldload(struct thread *td, const char *file, int *fileid)
  985 {
  986 #ifdef HWPMC_HOOKS
  987         struct pmckern_map_in pkm;
  988 #endif
  989         const char *kldname, *modname;
  990         linker_file_t lf;
  991         int error;
  992 
  993         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
  994                 return (error);
  995 
  996         if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
  997                 return (error);
  998 
  999         /*
 1000          * It is possible that kldloaded module will attach a new ifnet,
 1001          * so vnet context must be set when this ocurs.
 1002          */
 1003         CURVNET_SET(TD_TO_VNET(td));
 1004 
 1005         /*
 1006          * If file does not contain a qualified name or any dot in it
 1007          * (kldname.ko, or kldname.ver.ko) treat it as an interface
 1008          * name.
 1009          */
 1010         if (index(file, '/') || index(file, '.')) {
 1011                 kldname = file;
 1012                 modname = NULL;
 1013         } else {
 1014                 kldname = NULL;
 1015                 modname = file;
 1016         }
 1017 
 1018         KLD_LOCK();
 1019         error = linker_load_module(kldname, modname, NULL, NULL, &lf);
 1020         if (error) {
 1021                 KLD_UNLOCK();
 1022                 goto done;
 1023         }
 1024         lf->userrefs++;
 1025         if (fileid != NULL)
 1026                 *fileid = lf->id;
 1027 #ifdef HWPMC_HOOKS
 1028         KLD_DOWNGRADE();
 1029         pkm.pm_file = lf->filename;
 1030         pkm.pm_address = (uintptr_t) lf->address;
 1031         PMC_CALL_HOOK(td, PMC_FN_KLD_LOAD, (void *) &pkm);
 1032         KLD_UNLOCK_READ();
 1033 #else
 1034         KLD_UNLOCK();
 1035 #endif
 1036 
 1037 done:
 1038         CURVNET_RESTORE();
 1039         return (error);
 1040 }
 1041 
 1042 int
 1043 kldload(struct thread *td, struct kldload_args *uap)
 1044 {
 1045         char *pathname = NULL;
 1046         int error, fileid;
 1047 
 1048         td->td_retval[0] = -1;
 1049 
 1050         pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
 1051         error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
 1052         if (error == 0) {
 1053                 error = kern_kldload(td, pathname, &fileid);
 1054                 if (error == 0)
 1055                         td->td_retval[0] = fileid;
 1056         }
 1057         free(pathname, M_TEMP);
 1058         return (error);
 1059 }
 1060 
 1061 int
 1062 kern_kldunload(struct thread *td, int fileid, int flags)
 1063 {
 1064 #ifdef HWPMC_HOOKS
 1065         struct pmckern_map_out pkm;
 1066 #endif
 1067         linker_file_t lf;
 1068         int error = 0;
 1069 
 1070         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
 1071                 return (error);
 1072 
 1073         if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
 1074                 return (error);
 1075 
 1076         CURVNET_SET(TD_TO_VNET(td));
 1077         KLD_LOCK();
 1078         lf = linker_find_file_by_id(fileid);
 1079         if (lf) {
 1080                 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
 1081 
 1082                 /* Check if there are DTrace probes enabled on this file. */
 1083                 if (lf->nenabled > 0) {
 1084                         printf("kldunload: attempt to unload file that has"
 1085                             " DTrace probes enabled\n");
 1086                         error = EBUSY;
 1087                 } else if (lf->userrefs == 0) {
 1088                         /*
 1089                          * XXX: maybe LINKER_UNLOAD_FORCE should override ?
 1090                          */
 1091                         printf("kldunload: attempt to unload file that was"
 1092                             " loaded by the kernel\n");
 1093                         error = EBUSY;
 1094                 } else {
 1095 #ifdef HWPMC_HOOKS
 1096                         /* Save data needed by hwpmc(4) before unloading. */
 1097                         pkm.pm_address = (uintptr_t) lf->address;
 1098                         pkm.pm_size = lf->size;
 1099 #endif
 1100                         lf->userrefs--;
 1101                         error = linker_file_unload(lf, flags);
 1102                         if (error)
 1103                                 lf->userrefs++;
 1104                 }
 1105         } else
 1106                 error = ENOENT;
 1107 
 1108 #ifdef HWPMC_HOOKS
 1109         if (error == 0) {
 1110                 KLD_DOWNGRADE();
 1111                 PMC_CALL_HOOK(td, PMC_FN_KLD_UNLOAD, (void *) &pkm);
 1112                 KLD_UNLOCK_READ();
 1113         } else
 1114 #else
 1115                 KLD_UNLOCK();
 1116 #endif
 1117         CURVNET_RESTORE();
 1118         return (error);
 1119 }
 1120 
 1121 int
 1122 kldunload(struct thread *td, struct kldunload_args *uap)
 1123 {
 1124 
 1125         return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
 1126 }
 1127 
 1128 int
 1129 kldunloadf(struct thread *td, struct kldunloadf_args *uap)
 1130 {
 1131 
 1132         if (uap->flags != LINKER_UNLOAD_NORMAL &&
 1133             uap->flags != LINKER_UNLOAD_FORCE)
 1134                 return (EINVAL);
 1135         return (kern_kldunload(td, uap->fileid, uap->flags));
 1136 }
 1137 
 1138 int
 1139 kldfind(struct thread *td, struct kldfind_args *uap)
 1140 {
 1141         char *pathname;
 1142         const char *filename;
 1143         linker_file_t lf;
 1144         int error;
 1145 
 1146 #ifdef MAC
 1147         error = mac_kld_check_stat(td->td_ucred);
 1148         if (error)
 1149                 return (error);
 1150 #endif
 1151 
 1152         td->td_retval[0] = -1;
 1153 
 1154         pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
 1155         if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
 1156                 goto out;
 1157 
 1158         filename = linker_basename(pathname);
 1159         KLD_LOCK();
 1160         lf = linker_find_file_by_name(filename);
 1161         if (lf)
 1162                 td->td_retval[0] = lf->id;
 1163         else
 1164                 error = ENOENT;
 1165         KLD_UNLOCK();
 1166 out:
 1167         free(pathname, M_TEMP);
 1168         return (error);
 1169 }
 1170 
 1171 int
 1172 kldnext(struct thread *td, struct kldnext_args *uap)
 1173 {
 1174         linker_file_t lf;
 1175         int error = 0;
 1176 
 1177 #ifdef MAC
 1178         error = mac_kld_check_stat(td->td_ucred);
 1179         if (error)
 1180                 return (error);
 1181 #endif
 1182 
 1183         KLD_LOCK();
 1184         if (uap->fileid == 0)
 1185                 lf = TAILQ_FIRST(&linker_files);
 1186         else {
 1187                 lf = linker_find_file_by_id(uap->fileid);
 1188                 if (lf == NULL) {
 1189                         error = ENOENT;
 1190                         goto out;
 1191                 }
 1192                 lf = TAILQ_NEXT(lf, link);
 1193         }
 1194 
 1195         /* Skip partially loaded files. */
 1196         while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
 1197                 lf = TAILQ_NEXT(lf, link);
 1198 
 1199         if (lf)
 1200                 td->td_retval[0] = lf->id;
 1201         else
 1202                 td->td_retval[0] = 0;
 1203 out:
 1204         KLD_UNLOCK();
 1205         return (error);
 1206 }
 1207 
 1208 int
 1209 kldstat(struct thread *td, struct kldstat_args *uap)
 1210 {
 1211         struct kld_file_stat stat;
 1212         int error, version;
 1213 
 1214         /*
 1215          * Check the version of the user's structure.
 1216          */
 1217         if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
 1218             != 0)
 1219                 return (error);
 1220         if (version != sizeof(struct kld_file_stat_1) &&
 1221             version != sizeof(struct kld_file_stat))
 1222                 return (EINVAL);
 1223 
 1224         error = kern_kldstat(td, uap->fileid, &stat);
 1225         if (error != 0)
 1226                 return (error);
 1227         return (copyout(&stat, uap->stat, version));
 1228 }
 1229 
 1230 int
 1231 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
 1232 {
 1233         linker_file_t lf;
 1234         int namelen;
 1235 #ifdef MAC
 1236         int error;
 1237 
 1238         error = mac_kld_check_stat(td->td_ucred);
 1239         if (error)
 1240                 return (error);
 1241 #endif
 1242 
 1243         KLD_LOCK();
 1244         lf = linker_find_file_by_id(fileid);
 1245         if (lf == NULL) {
 1246                 KLD_UNLOCK();
 1247                 return (ENOENT);
 1248         }
 1249 
 1250         /* Version 1 fields: */
 1251         namelen = strlen(lf->filename) + 1;
 1252         if (namelen > MAXPATHLEN)
 1253                 namelen = MAXPATHLEN;
 1254         bcopy(lf->filename, &stat->name[0], namelen);
 1255         stat->refs = lf->refs;
 1256         stat->id = lf->id;
 1257         stat->address = lf->address;
 1258         stat->size = lf->size;
 1259         /* Version 2 fields: */
 1260         namelen = strlen(lf->pathname) + 1;
 1261         if (namelen > MAXPATHLEN)
 1262                 namelen = MAXPATHLEN;
 1263         bcopy(lf->pathname, &stat->pathname[0], namelen);
 1264         KLD_UNLOCK();
 1265 
 1266         td->td_retval[0] = 0;
 1267         return (0);
 1268 }
 1269 
 1270 int
 1271 kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
 1272 {
 1273         linker_file_t lf;
 1274         module_t mp;
 1275         int error = 0;
 1276 
 1277 #ifdef MAC
 1278         error = mac_kld_check_stat(td->td_ucred);
 1279         if (error)
 1280                 return (error);
 1281 #endif
 1282 
 1283         KLD_LOCK();
 1284         lf = linker_find_file_by_id(uap->fileid);
 1285         if (lf) {
 1286                 MOD_SLOCK;
 1287                 mp = TAILQ_FIRST(&lf->modules);
 1288                 if (mp != NULL)
 1289                         td->td_retval[0] = module_getid(mp);
 1290                 else
 1291                         td->td_retval[0] = 0;
 1292                 MOD_SUNLOCK;
 1293         } else
 1294                 error = ENOENT;
 1295         KLD_UNLOCK();
 1296         return (error);
 1297 }
 1298 
 1299 int
 1300 kldsym(struct thread *td, struct kldsym_args *uap)
 1301 {
 1302         char *symstr = NULL;
 1303         c_linker_sym_t sym;
 1304         linker_symval_t symval;
 1305         linker_file_t lf;
 1306         struct kld_sym_lookup lookup;
 1307         int error = 0;
 1308 
 1309 #ifdef MAC
 1310         error = mac_kld_check_stat(td->td_ucred);
 1311         if (error)
 1312                 return (error);
 1313 #endif
 1314 
 1315         if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
 1316                 return (error);
 1317         if (lookup.version != sizeof(lookup) ||
 1318             uap->cmd != KLDSYM_LOOKUP)
 1319                 return (EINVAL);
 1320         symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
 1321         if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
 1322                 goto out;
 1323         KLD_LOCK();
 1324         if (uap->fileid != 0) {
 1325                 lf = linker_find_file_by_id(uap->fileid);
 1326                 if (lf == NULL)
 1327                         error = ENOENT;
 1328                 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
 1329                     LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
 1330                         lookup.symvalue = (uintptr_t) symval.value;
 1331                         lookup.symsize = symval.size;
 1332                         error = copyout(&lookup, uap->data, sizeof(lookup));
 1333                 } else
 1334                         error = ENOENT;
 1335         } else {
 1336                 TAILQ_FOREACH(lf, &linker_files, link) {
 1337                         if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
 1338                             LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
 1339                                 lookup.symvalue = (uintptr_t)symval.value;
 1340                                 lookup.symsize = symval.size;
 1341                                 error = copyout(&lookup, uap->data,
 1342                                     sizeof(lookup));
 1343                                 break;
 1344                         }
 1345                 }
 1346                 if (lf == NULL)
 1347                         error = ENOENT;
 1348         }
 1349         KLD_UNLOCK();
 1350 out:
 1351         free(symstr, M_TEMP);
 1352         return (error);
 1353 }
 1354 
 1355 /*
 1356  * Preloaded module support
 1357  */
 1358 
 1359 static modlist_t
 1360 modlist_lookup(const char *name, int ver)
 1361 {
 1362         modlist_t mod;
 1363 
 1364         TAILQ_FOREACH(mod, &found_modules, link) {
 1365                 if (strcmp(mod->name, name) == 0 &&
 1366                     (ver == 0 || mod->version == ver))
 1367                         return (mod);
 1368         }
 1369         return (NULL);
 1370 }
 1371 
 1372 static modlist_t
 1373 modlist_lookup2(const char *name, struct mod_depend *verinfo)
 1374 {
 1375         modlist_t mod, bestmod;
 1376         int ver;
 1377 
 1378         if (verinfo == NULL)
 1379                 return (modlist_lookup(name, 0));
 1380         bestmod = NULL;
 1381         TAILQ_FOREACH(mod, &found_modules, link) {
 1382                 if (strcmp(mod->name, name) != 0)
 1383                         continue;
 1384                 ver = mod->version;
 1385                 if (ver == verinfo->md_ver_preferred)
 1386                         return (mod);
 1387                 if (ver >= verinfo->md_ver_minimum &&
 1388                     ver <= verinfo->md_ver_maximum &&
 1389                     (bestmod == NULL || ver > bestmod->version))
 1390                         bestmod = mod;
 1391         }
 1392         return (bestmod);
 1393 }
 1394 
 1395 static modlist_t
 1396 modlist_newmodule(const char *modname, int version, linker_file_t container)
 1397 {
 1398         modlist_t mod;
 1399 
 1400         mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
 1401         if (mod == NULL)
 1402                 panic("no memory for module list");
 1403         mod->container = container;
 1404         mod->name = modname;
 1405         mod->version = version;
 1406         TAILQ_INSERT_TAIL(&found_modules, mod, link);
 1407         return (mod);
 1408 }
 1409 
 1410 static void
 1411 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
 1412     struct mod_metadata **stop, int preload)
 1413 {
 1414         struct mod_metadata *mp, **mdp;
 1415         const char *modname;
 1416         int ver;
 1417 
 1418         for (mdp = start; mdp < stop; mdp++) {
 1419                 mp = *mdp;
 1420                 if (mp->md_type != MDT_VERSION)
 1421                         continue;
 1422                 modname = mp->md_cval;
 1423                 ver = ((struct mod_version *)mp->md_data)->mv_version;
 1424                 if (modlist_lookup(modname, ver) != NULL) {
 1425                         printf("module %s already present!\n", modname);
 1426                         /* XXX what can we do? this is a build error. :-( */
 1427                         continue;
 1428                 }
 1429                 modlist_newmodule(modname, ver, lf);
 1430         }
 1431 }
 1432 
 1433 static void
 1434 linker_preload(void *arg)
 1435 {
 1436         caddr_t modptr;
 1437         const char *modname, *nmodname;
 1438         char *modtype;
 1439         linker_file_t lf, nlf;
 1440         linker_class_t lc;
 1441         int error;
 1442         linker_file_list_t loaded_files;
 1443         linker_file_list_t depended_files;
 1444         struct mod_metadata *mp, *nmp;
 1445         struct mod_metadata **start, **stop, **mdp, **nmdp;
 1446         struct mod_depend *verinfo;
 1447         int nver;
 1448         int resolves;
 1449         modlist_t mod;
 1450         struct sysinit **si_start, **si_stop;
 1451 
 1452         TAILQ_INIT(&loaded_files);
 1453         TAILQ_INIT(&depended_files);
 1454         TAILQ_INIT(&found_modules);
 1455         error = 0;
 1456 
 1457         modptr = NULL;
 1458         while ((modptr = preload_search_next_name(modptr)) != NULL) {
 1459                 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
 1460                 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
 1461                 if (modname == NULL) {
 1462                         printf("Preloaded module at %p does not have a"
 1463                             " name!\n", modptr);
 1464                         continue;
 1465                 }
 1466                 if (modtype == NULL) {
 1467                         printf("Preloaded module at %p does not have a type!\n",
 1468                             modptr);
 1469                         continue;
 1470                 }
 1471                 if (bootverbose)
 1472                         printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
 1473                             modptr);
 1474                 lf = NULL;
 1475                 TAILQ_FOREACH(lc, &classes, link) {
 1476                         error = LINKER_LINK_PRELOAD(lc, modname, &lf);
 1477                         if (!error)
 1478                                 break;
 1479                         lf = NULL;
 1480                 }
 1481                 if (lf)
 1482                         TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
 1483         }
 1484 
 1485         /*
 1486          * First get a list of stuff in the kernel.
 1487          */
 1488         if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
 1489             &stop, NULL) == 0)
 1490                 linker_addmodules(linker_kernel_file, start, stop, 1);
 1491 
 1492         /*
 1493          * This is a once-off kinky bubble sort to resolve relocation
 1494          * dependency requirements.
 1495          */
 1496 restart:
 1497         TAILQ_FOREACH(lf, &loaded_files, loaded) {
 1498                 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
 1499                     &stop, NULL);
 1500                 /*
 1501                  * First, look to see if we would successfully link with this
 1502                  * stuff.
 1503                  */
 1504                 resolves = 1;   /* unless we know otherwise */
 1505                 if (!error) {
 1506                         for (mdp = start; mdp < stop; mdp++) {
 1507                                 mp = *mdp;
 1508                                 if (mp->md_type != MDT_DEPEND)
 1509                                         continue;
 1510                                 modname = mp->md_cval;
 1511                                 verinfo = mp->md_data;
 1512                                 for (nmdp = start; nmdp < stop; nmdp++) {
 1513                                         nmp = *nmdp;
 1514                                         if (nmp->md_type != MDT_VERSION)
 1515                                                 continue;
 1516                                         nmodname = nmp->md_cval;
 1517                                         if (strcmp(modname, nmodname) == 0)
 1518                                                 break;
 1519                                 }
 1520                                 if (nmdp < stop)   /* it's a self reference */
 1521                                         continue;
 1522 
 1523                                 /*
 1524                                  * ok, the module isn't here yet, we
 1525                                  * are not finished
 1526                                  */
 1527                                 if (modlist_lookup2(modname, verinfo) == NULL)
 1528                                         resolves = 0;
 1529                         }
 1530                 }
 1531                 /*
 1532                  * OK, if we found our modules, we can link.  So, "provide"
 1533                  * the modules inside and add it to the end of the link order
 1534                  * list.
 1535                  */
 1536                 if (resolves) {
 1537                         if (!error) {
 1538                                 for (mdp = start; mdp < stop; mdp++) {
 1539                                         mp = *mdp;
 1540                                         if (mp->md_type != MDT_VERSION)
 1541                                                 continue;
 1542                                         modname = mp->md_cval;
 1543                                         nver = ((struct mod_version *)
 1544                                             mp->md_data)->mv_version;
 1545                                         if (modlist_lookup(modname,
 1546                                             nver) != NULL) {
 1547                                                 printf("module %s already"
 1548                                                     " present!\n", modname);
 1549                                                 TAILQ_REMOVE(&loaded_files,
 1550                                                     lf, loaded);
 1551                                                 linker_file_unload(lf,
 1552                                                     LINKER_UNLOAD_FORCE);
 1553                                                 /* we changed tailq next ptr */
 1554                                                 goto restart;
 1555                                         }
 1556                                         modlist_newmodule(modname, nver, lf);
 1557                                 }
 1558                         }
 1559                         TAILQ_REMOVE(&loaded_files, lf, loaded);
 1560                         TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
 1561                         /*
 1562                          * Since we provided modules, we need to restart the
 1563                          * sort so that the previous files that depend on us
 1564                          * have a chance. Also, we've busted the tailq next
 1565                          * pointer with the REMOVE.
 1566                          */
 1567                         goto restart;
 1568                 }
 1569         }
 1570 
 1571         /*
 1572          * At this point, we check to see what could not be resolved..
 1573          */
 1574         while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
 1575                 TAILQ_REMOVE(&loaded_files, lf, loaded);
 1576                 printf("KLD file %s is missing dependencies\n", lf->filename);
 1577                 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
 1578         }
 1579 
 1580         /*
 1581          * We made it. Finish off the linking in the order we determined.
 1582          */
 1583         TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
 1584                 if (linker_kernel_file) {
 1585                         linker_kernel_file->refs++;
 1586                         error = linker_file_add_dependency(lf,
 1587                             linker_kernel_file);
 1588                         if (error)
 1589                                 panic("cannot add dependency");
 1590                 }
 1591                 lf->userrefs++; /* so we can (try to) kldunload it */
 1592                 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
 1593                     &stop, NULL);
 1594                 if (!error) {
 1595                         for (mdp = start; mdp < stop; mdp++) {
 1596                                 mp = *mdp;
 1597                                 if (mp->md_type != MDT_DEPEND)
 1598                                         continue;
 1599                                 modname = mp->md_cval;
 1600                                 verinfo = mp->md_data;
 1601                                 mod = modlist_lookup2(modname, verinfo);
 1602                                 /* Don't count self-dependencies */
 1603                                 if (lf == mod->container)
 1604                                         continue;
 1605                                 mod->container->refs++;
 1606                                 error = linker_file_add_dependency(lf,
 1607                                     mod->container);
 1608                                 if (error)
 1609                                         panic("cannot add dependency");
 1610                         }
 1611                 }
 1612                 /*
 1613                  * Now do relocation etc using the symbol search paths
 1614                  * established by the dependencies
 1615                  */
 1616                 error = LINKER_LINK_PRELOAD_FINISH(lf);
 1617                 if (error) {
 1618                         TAILQ_REMOVE(&depended_files, lf, loaded);
 1619                         printf("KLD file %s - could not finalize loading\n",
 1620                             lf->filename);
 1621                         linker_file_unload(lf, LINKER_UNLOAD_FORCE);
 1622                         continue;
 1623                 }
 1624                 linker_file_register_modules(lf);
 1625                 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
 1626                     &si_stop, NULL) == 0)
 1627                         sysinit_add(si_start, si_stop);
 1628                 linker_file_register_sysctls(lf);
 1629                 lf->flags |= LINKER_FILE_LINKED;
 1630         }
 1631         /* woohoo! we made it! */
 1632 }
 1633 
 1634 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
 1635 
 1636 /*
 1637  * Search for a not-loaded module by name.
 1638  *
 1639  * Modules may be found in the following locations:
 1640  *
 1641  * - preloaded (result is just the module name) - on disk (result is full path
 1642  * to module)
 1643  *
 1644  * If the module name is qualified in any way (contains path, etc.) the we
 1645  * simply return a copy of it.
 1646  *
 1647  * The search path can be manipulated via sysctl.  Note that we use the ';'
 1648  * character as a separator to be consistent with the bootloader.
 1649  */
 1650 
 1651 static char linker_hintfile[] = "linker.hints";
 1652 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
 1653 
 1654 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
 1655     sizeof(linker_path), "module load search path");
 1656 
 1657 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
 1658 
 1659 static char *linker_ext_list[] = {
 1660         "",
 1661         ".ko",
 1662         NULL
 1663 };
 1664 
 1665 /*
 1666  * Check if file actually exists either with or without extension listed in
 1667  * the linker_ext_list. (probably should be generic for the rest of the
 1668  * kernel)
 1669  */
 1670 static char *
 1671 linker_lookup_file(const char *path, int pathlen, const char *name,
 1672     int namelen, struct vattr *vap)
 1673 {
 1674         struct nameidata nd;
 1675         struct thread *td = curthread;  /* XXX */
 1676         char *result, **cpp, *sep;
 1677         int error, len, extlen, reclen, flags, vfslocked;
 1678         enum vtype type;
 1679 
 1680         extlen = 0;
 1681         for (cpp = linker_ext_list; *cpp; cpp++) {
 1682                 len = strlen(*cpp);
 1683                 if (len > extlen)
 1684                         extlen = len;
 1685         }
 1686         extlen++;               /* trailing '\0' */
 1687         sep = (path[pathlen - 1] != '/') ? "/" : "";
 1688 
 1689         reclen = pathlen + strlen(sep) + namelen + extlen + 1;
 1690         result = malloc(reclen, M_LINKER, M_WAITOK);
 1691         for (cpp = linker_ext_list; *cpp; cpp++) {
 1692                 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
 1693                     namelen, name, *cpp);
 1694                 /*
 1695                  * Attempt to open the file, and return the path if
 1696                  * we succeed and it's a regular file.
 1697                  */
 1698                 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td);
 1699                 flags = FREAD;
 1700                 error = vn_open(&nd, &flags, 0, NULL);
 1701                 if (error == 0) {
 1702                         vfslocked = NDHASGIANT(&nd);
 1703                         NDFREE(&nd, NDF_ONLY_PNBUF);
 1704                         type = nd.ni_vp->v_type;
 1705                         if (vap)
 1706                                 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
 1707                         VOP_UNLOCK(nd.ni_vp, 0);
 1708                         vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
 1709                         VFS_UNLOCK_GIANT(vfslocked);
 1710                         if (type == VREG)
 1711                                 return (result);
 1712                 }
 1713         }
 1714         free(result, M_LINKER);
 1715         return (NULL);
 1716 }
 1717 
 1718 #define INT_ALIGN(base, ptr)    ptr =                                   \
 1719         (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
 1720 
 1721 /*
 1722  * Lookup KLD which contains requested module in the "linker.hints" file. If
 1723  * version specification is available, then try to find the best KLD.
 1724  * Otherwise just find the latest one.
 1725  */
 1726 static char *
 1727 linker_hints_lookup(const char *path, int pathlen, const char *modname,
 1728     int modnamelen, struct mod_depend *verinfo)
 1729 {
 1730         struct thread *td = curthread;  /* XXX */
 1731         struct ucred *cred = td ? td->td_ucred : NULL;
 1732         struct nameidata nd;
 1733         struct vattr vattr, mattr;
 1734         u_char *hints = NULL;
 1735         u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
 1736         int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
 1737         int vfslocked = 0;
 1738 
 1739         result = NULL;
 1740         bestver = found = 0;
 1741 
 1742         sep = (path[pathlen - 1] != '/') ? "/" : "";
 1743         reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
 1744             strlen(sep) + 1;
 1745         pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
 1746         snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
 1747             linker_hintfile);
 1748 
 1749         NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td);
 1750         flags = FREAD;
 1751         error = vn_open(&nd, &flags, 0, NULL);
 1752         if (error)
 1753                 goto bad;
 1754         vfslocked = NDHASGIANT(&nd);
 1755         NDFREE(&nd, NDF_ONLY_PNBUF);
 1756         if (nd.ni_vp->v_type != VREG)
 1757                 goto bad;
 1758         best = cp = NULL;
 1759         error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
 1760         if (error)
 1761                 goto bad;
 1762         /*
 1763          * XXX: we need to limit this number to some reasonable value
 1764          */
 1765         if (vattr.va_size > 100 * 1024) {
 1766                 printf("hints file too large %ld\n", (long)vattr.va_size);
 1767                 goto bad;
 1768         }
 1769         hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
 1770         if (hints == NULL)
 1771                 goto bad;
 1772         error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
 1773             UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
 1774         if (error)
 1775                 goto bad;
 1776         VOP_UNLOCK(nd.ni_vp, 0);
 1777         vn_close(nd.ni_vp, FREAD, cred, td);
 1778         VFS_UNLOCK_GIANT(vfslocked);
 1779         nd.ni_vp = NULL;
 1780         if (reclen != 0) {
 1781                 printf("can't read %d\n", reclen);
 1782                 goto bad;
 1783         }
 1784         intp = (int *)hints;
 1785         ival = *intp++;
 1786         if (ival != LINKER_HINTS_VERSION) {
 1787                 printf("hints file version mismatch %d\n", ival);
 1788                 goto bad;
 1789         }
 1790         bufend = hints + vattr.va_size;
 1791         recptr = (u_char *)intp;
 1792         clen = blen = 0;
 1793         while (recptr < bufend && !found) {
 1794                 intp = (int *)recptr;
 1795                 reclen = *intp++;
 1796                 ival = *intp++;
 1797                 cp = (char *)intp;
 1798                 switch (ival) {
 1799                 case MDT_VERSION:
 1800                         clen = *cp++;
 1801                         if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
 1802                                 break;
 1803                         cp += clen;
 1804                         INT_ALIGN(hints, cp);
 1805                         ival = *(int *)cp;
 1806                         cp += sizeof(int);
 1807                         clen = *cp++;
 1808                         if (verinfo == NULL ||
 1809                             ival == verinfo->md_ver_preferred) {
 1810                                 found = 1;
 1811                                 break;
 1812                         }
 1813                         if (ival >= verinfo->md_ver_minimum &&
 1814                             ival <= verinfo->md_ver_maximum &&
 1815                             ival > bestver) {
 1816                                 bestver = ival;
 1817                                 best = cp;
 1818                                 blen = clen;
 1819                         }
 1820                         break;
 1821                 default:
 1822                         break;
 1823                 }
 1824                 recptr += reclen + sizeof(int);
 1825         }
 1826         /*
 1827          * Finally check if KLD is in the place
 1828          */
 1829         if (found)
 1830                 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
 1831         else if (best)
 1832                 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
 1833 
 1834         /*
 1835          * KLD is newer than hints file. What we should do now?
 1836          */
 1837         if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
 1838                 printf("warning: KLD '%s' is newer than the linker.hints"
 1839                     " file\n", result);
 1840 bad:
 1841         free(pathbuf, M_LINKER);
 1842         if (hints)
 1843                 free(hints, M_TEMP);
 1844         if (nd.ni_vp != NULL) {
 1845                 VOP_UNLOCK(nd.ni_vp, 0);
 1846                 vn_close(nd.ni_vp, FREAD, cred, td);
 1847                 VFS_UNLOCK_GIANT(vfslocked);
 1848         }
 1849         /*
 1850          * If nothing found or hints is absent - fallback to the old
 1851          * way by using "kldname[.ko]" as module name.
 1852          */
 1853         if (!found && !bestver && result == NULL)
 1854                 result = linker_lookup_file(path, pathlen, modname,
 1855                     modnamelen, NULL);
 1856         return (result);
 1857 }
 1858 
 1859 /*
 1860  * Lookup KLD which contains requested module in the all directories.
 1861  */
 1862 static char *
 1863 linker_search_module(const char *modname, int modnamelen,
 1864     struct mod_depend *verinfo)
 1865 {
 1866         char *cp, *ep, *result;
 1867 
 1868         /*
 1869          * traverse the linker path
 1870          */
 1871         for (cp = linker_path; *cp; cp = ep + 1) {
 1872                 /* find the end of this component */
 1873                 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
 1874                 result = linker_hints_lookup(cp, ep - cp, modname,
 1875                     modnamelen, verinfo);
 1876                 if (result != NULL)
 1877                         return (result);
 1878                 if (*ep == 0)
 1879                         break;
 1880         }
 1881         return (NULL);
 1882 }
 1883 
 1884 /*
 1885  * Search for module in all directories listed in the linker_path.
 1886  */
 1887 static char *
 1888 linker_search_kld(const char *name)
 1889 {
 1890         char *cp, *ep, *result;
 1891         int len;
 1892 
 1893         /* qualified at all? */
 1894         if (index(name, '/'))
 1895                 return (linker_strdup(name));
 1896 
 1897         /* traverse the linker path */
 1898         len = strlen(name);
 1899         for (ep = linker_path; *ep; ep++) {
 1900                 cp = ep;
 1901                 /* find the end of this component */
 1902                 for (; *ep != 0 && *ep != ';'; ep++);
 1903                 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
 1904                 if (result != NULL)
 1905                         return (result);
 1906         }
 1907         return (NULL);
 1908 }
 1909 
 1910 static const char *
 1911 linker_basename(const char *path)
 1912 {
 1913         const char *filename;
 1914 
 1915         filename = rindex(path, '/');
 1916         if (filename == NULL)
 1917                 return path;
 1918         if (filename[1])
 1919                 filename++;
 1920         return (filename);
 1921 }
 1922 
 1923 #ifdef HWPMC_HOOKS
 1924 /*
 1925  * Inform hwpmc about the set of kernel modules currently loaded.
 1926  */
 1927 void *
 1928 linker_hwpmc_list_objects(void)
 1929 {
 1930         linker_file_t lf;
 1931         struct pmckern_map_in *kobase;
 1932         int i, nmappings;
 1933 
 1934         nmappings = 0;
 1935         KLD_LOCK_READ();
 1936         TAILQ_FOREACH(lf, &linker_files, link)
 1937                 nmappings++;
 1938 
 1939         /* Allocate nmappings + 1 entries. */
 1940         kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
 1941             M_LINKER, M_WAITOK | M_ZERO);
 1942         i = 0;
 1943         TAILQ_FOREACH(lf, &linker_files, link) {
 1944 
 1945                 /* Save the info for this linker file. */
 1946                 kobase[i].pm_file = lf->filename;
 1947                 kobase[i].pm_address = (uintptr_t)lf->address;
 1948                 i++;
 1949         }
 1950         KLD_UNLOCK_READ();
 1951 
 1952         KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
 1953 
 1954         /* The last entry of the malloced area comprises of all zeros. */
 1955         KASSERT(kobase[i].pm_file == NULL,
 1956             ("linker_hwpmc_list_objects: last object not NULL"));
 1957 
 1958         return ((void *)kobase);
 1959 }
 1960 #endif
 1961 
 1962 /*
 1963  * Find a file which contains given module and load it, if "parent" is not
 1964  * NULL, register a reference to it.
 1965  */
 1966 static int
 1967 linker_load_module(const char *kldname, const char *modname,
 1968     struct linker_file *parent, struct mod_depend *verinfo,
 1969     struct linker_file **lfpp)
 1970 {
 1971         linker_file_t lfdep;
 1972         const char *filename;
 1973         char *pathname;
 1974         int error;
 1975 
 1976         KLD_LOCK_ASSERT();
 1977         if (modname == NULL) {
 1978                 /*
 1979                  * We have to load KLD
 1980                  */
 1981                 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
 1982                     " is not NULL"));
 1983                 pathname = linker_search_kld(kldname);
 1984         } else {
 1985                 if (modlist_lookup2(modname, verinfo) != NULL)
 1986                         return (EEXIST);
 1987                 if (kldname != NULL)
 1988                         pathname = linker_strdup(kldname);
 1989                 else if (rootvnode == NULL)
 1990                         pathname = NULL;
 1991                 else
 1992                         /*
 1993                          * Need to find a KLD with required module
 1994                          */
 1995                         pathname = linker_search_module(modname,
 1996                             strlen(modname), verinfo);
 1997         }
 1998         if (pathname == NULL)
 1999                 return (ENOENT);
 2000 
 2001         /*
 2002          * Can't load more than one file with the same basename XXX:
 2003          * Actually it should be possible to have multiple KLDs with
 2004          * the same basename but different path because they can
 2005          * provide different versions of the same modules.
 2006          */
 2007         filename = linker_basename(pathname);
 2008         if (linker_find_file_by_name(filename))
 2009                 error = EEXIST;
 2010         else do {
 2011                 error = linker_load_file(pathname, &lfdep);
 2012                 if (error)
 2013                         break;
 2014                 if (modname && verinfo &&
 2015                     modlist_lookup2(modname, verinfo) == NULL) {
 2016                         linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
 2017                         error = ENOENT;
 2018                         break;
 2019                 }
 2020                 if (parent) {
 2021                         error = linker_file_add_dependency(parent, lfdep);
 2022                         if (error)
 2023                                 break;
 2024                 }
 2025                 if (lfpp)
 2026                         *lfpp = lfdep;
 2027         } while (0);
 2028         free(pathname, M_LINKER);
 2029         return (error);
 2030 }
 2031 
 2032 /*
 2033  * This routine is responsible for finding dependencies of userland initiated
 2034  * kldload(2)'s of files.
 2035  */
 2036 int
 2037 linker_load_dependencies(linker_file_t lf)
 2038 {
 2039         linker_file_t lfdep;
 2040         struct mod_metadata **start, **stop, **mdp, **nmdp;
 2041         struct mod_metadata *mp, *nmp;
 2042         struct mod_depend *verinfo;
 2043         modlist_t mod;
 2044         const char *modname, *nmodname;
 2045         int ver, error = 0, count;
 2046 
 2047         /*
 2048          * All files are dependant on /kernel.
 2049          */
 2050         KLD_LOCK_ASSERT();
 2051         if (linker_kernel_file) {
 2052                 linker_kernel_file->refs++;
 2053                 error = linker_file_add_dependency(lf, linker_kernel_file);
 2054                 if (error)
 2055                         return (error);
 2056         }
 2057         if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
 2058             &count) != 0)
 2059                 return (0);
 2060         for (mdp = start; mdp < stop; mdp++) {
 2061                 mp = *mdp;
 2062                 if (mp->md_type != MDT_VERSION)
 2063                         continue;
 2064                 modname = mp->md_cval;
 2065                 ver = ((struct mod_version *)mp->md_data)->mv_version;
 2066                 mod = modlist_lookup(modname, ver);
 2067                 if (mod != NULL) {
 2068                         printf("interface %s.%d already present in the KLD"
 2069                             " '%s'!\n", modname, ver,
 2070                             mod->container->filename);
 2071                         return (EEXIST);
 2072                 }
 2073         }
 2074 
 2075         for (mdp = start; mdp < stop; mdp++) {
 2076                 mp = *mdp;
 2077                 if (mp->md_type != MDT_DEPEND)
 2078                         continue;
 2079                 modname = mp->md_cval;
 2080                 verinfo = mp->md_data;
 2081                 nmodname = NULL;
 2082                 for (nmdp = start; nmdp < stop; nmdp++) {
 2083                         nmp = *nmdp;
 2084                         if (nmp->md_type != MDT_VERSION)
 2085                                 continue;
 2086                         nmodname = nmp->md_cval;
 2087                         if (strcmp(modname, nmodname) == 0)
 2088                                 break;
 2089                 }
 2090                 if (nmdp < stop)/* early exit, it's a self reference */
 2091                         continue;
 2092                 mod = modlist_lookup2(modname, verinfo);
 2093                 if (mod) {      /* woohoo, it's loaded already */
 2094                         lfdep = mod->container;
 2095                         lfdep->refs++;
 2096                         error = linker_file_add_dependency(lf, lfdep);
 2097                         if (error)
 2098                                 break;
 2099                         continue;
 2100                 }
 2101                 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
 2102                 if (error) {
 2103                         printf("KLD %s: depends on %s - not available or"
 2104                             " version mismatch\n", lf->filename, modname);
 2105                         break;
 2106                 }
 2107         }
 2108 
 2109         if (error)
 2110                 return (error);
 2111         linker_addmodules(lf, start, stop, 0);
 2112         return (error);
 2113 }
 2114 
 2115 static int
 2116 sysctl_kern_function_list_iterate(const char *name, void *opaque)
 2117 {
 2118         struct sysctl_req *req;
 2119 
 2120         req = opaque;
 2121         return (SYSCTL_OUT(req, name, strlen(name) + 1));
 2122 }
 2123 
 2124 /*
 2125  * Export a nul-separated, double-nul-terminated list of all function names
 2126  * in the kernel.
 2127  */
 2128 static int
 2129 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
 2130 {
 2131         linker_file_t lf;
 2132         int error;
 2133 
 2134 #ifdef MAC
 2135         error = mac_kld_check_stat(req->td->td_ucred);
 2136         if (error)
 2137                 return (error);
 2138 #endif
 2139         error = sysctl_wire_old_buffer(req, 0);
 2140         if (error != 0)
 2141                 return (error);
 2142         KLD_LOCK();
 2143         TAILQ_FOREACH(lf, &linker_files, link) {
 2144                 error = LINKER_EACH_FUNCTION_NAME(lf,
 2145                     sysctl_kern_function_list_iterate, req);
 2146                 if (error) {
 2147                         KLD_UNLOCK();
 2148                         return (error);
 2149                 }
 2150         }
 2151         KLD_UNLOCK();
 2152         return (SYSCTL_OUT(req, "", 1));
 2153 }
 2154 
 2155 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD,
 2156     NULL, 0, sysctl_kern_function_list, "", "kernel function list");

Cache object: 294840139189c306c25a87b074ba4bb9


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.