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-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: cd5b43e74bbe59849bbbc71c969a2b9d


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