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

Cache object: 7e4b36b23c9b85ea850d346ad34d8dcb


[ 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.