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


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

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

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

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

Cache object: 6ef80022b244634102a0f5142d4cf34d


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