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  * 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.0/sys/kern/kern_linker.c 146733 2005-05-28 23:20:05Z pjd $");
   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 
  401         if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
  402                 *result = mod->container;
  403                 (*result)->refs++;
  404                 return (0);
  405         }
  406 
  407         return (linker_load_module(NULL, modname, NULL, verinfo, result));
  408 }
  409 
  410 linker_file_t
  411 linker_find_file_by_name(const char *filename)
  412 {
  413         linker_file_t lf = 0;
  414         char *koname;
  415 
  416         koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
  417         if (koname == NULL)
  418                 goto out;
  419         sprintf(koname, "%s.ko", filename);
  420 
  421         mtx_lock(&kld_mtx);
  422         TAILQ_FOREACH(lf, &linker_files, link) {
  423                 if (strcmp(lf->filename, koname) == 0)
  424                         break;
  425                 if (strcmp(lf->filename, filename) == 0)
  426                         break;
  427         }
  428         mtx_unlock(&kld_mtx);
  429 out:
  430         if (koname)
  431                 free(koname, M_LINKER);
  432         return (lf);
  433 }
  434 
  435 linker_file_t
  436 linker_find_file_by_id(int fileid)
  437 {
  438         linker_file_t lf = 0;
  439         
  440         mtx_lock(&kld_mtx);
  441         TAILQ_FOREACH(lf, &linker_files, link)
  442                 if (lf->id == fileid)
  443                         break;
  444         mtx_unlock(&kld_mtx);
  445         return (lf);
  446 }
  447 
  448 linker_file_t
  449 linker_make_file(const char *pathname, linker_class_t lc)
  450 {
  451         linker_file_t lf;
  452         const char *filename;
  453 
  454         lf = NULL;
  455         filename = linker_basename(pathname);
  456 
  457         KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
  458         lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
  459         if (lf == NULL)
  460                 goto out;
  461         lf->refs = 1;
  462         lf->userrefs = 0;
  463         lf->flags = 0;
  464         lf->filename = linker_strdup(filename);
  465         LINKER_GET_NEXT_FILE_ID(lf->id);
  466         lf->ndeps = 0;
  467         lf->deps = NULL;
  468         STAILQ_INIT(&lf->common);
  469         TAILQ_INIT(&lf->modules);
  470         mtx_lock(&kld_mtx);
  471         TAILQ_INSERT_TAIL(&linker_files, lf, link);
  472         mtx_unlock(&kld_mtx);
  473 out:
  474         return (lf);
  475 }
  476 
  477 int
  478 linker_file_unload(linker_file_t file, int flags)
  479 {
  480         module_t mod, next;
  481         modlist_t ml, nextml;
  482         struct common_symbol *cp;
  483         int error, i;
  484 
  485         error = 0;
  486 
  487         /* Refuse to unload modules if securelevel raised. */
  488         if (securelevel > 0)
  489                 return (EPERM);
  490 #ifdef MAC
  491         error = mac_check_kld_unload(curthread->td_ucred);
  492         if (error)
  493                 return (error);
  494 #endif
  495 
  496         KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
  497         if (file->refs == 1) {
  498                 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
  499                     " informing modules\n"));
  500 
  501                 /*
  502                  * Inform any modules associated with this file.
  503                  */
  504                 MOD_XLOCK;
  505                 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
  506                         next = module_getfnext(mod);
  507                         MOD_XUNLOCK;
  508 
  509                         /*
  510                          * Give the module a chance to veto the unload.
  511                          */
  512                         if ((error = module_unload(mod, flags)) != 0) {
  513                                 KLD_DPF(FILE, ("linker_file_unload: module %p"
  514                                     " vetoes unload\n", mod));
  515                                 goto out;
  516                         } else
  517                                 MOD_XLOCK;
  518                         module_release(mod);
  519                 }
  520                 MOD_XUNLOCK;
  521         }
  522         file->refs--;
  523         if (file->refs > 0) {
  524                 goto out;
  525         }
  526         for (ml = TAILQ_FIRST(&found_modules); ml; ml = nextml) {
  527                 nextml = TAILQ_NEXT(ml, link);
  528                 if (ml->container == file) {
  529                         TAILQ_REMOVE(&found_modules, ml, link);
  530                         free(ml, M_LINKER);
  531                 }
  532         }
  533 
  534         /* 
  535          * Don't try to run SYSUNINITs if we are unloaded due to a 
  536          * link error.
  537          */
  538         if (file->flags & LINKER_FILE_LINKED) {
  539                 linker_file_sysuninit(file);
  540                 linker_file_unregister_sysctls(file);
  541         }
  542         mtx_lock(&kld_mtx);
  543         TAILQ_REMOVE(&linker_files, file, link);
  544         mtx_unlock(&kld_mtx);
  545 
  546         if (file->deps) {
  547                 for (i = 0; i < file->ndeps; i++)
  548                         linker_file_unload(file->deps[i], flags);
  549                 free(file->deps, M_LINKER);
  550                 file->deps = NULL;
  551         }
  552         for (cp = STAILQ_FIRST(&file->common); cp;
  553             cp = STAILQ_FIRST(&file->common)) {
  554                 STAILQ_REMOVE(&file->common, cp, common_symbol, link);
  555                 free(cp, M_LINKER);
  556         }
  557 
  558         LINKER_UNLOAD(file);
  559         if (file->filename) {
  560                 free(file->filename, M_LINKER);
  561                 file->filename = NULL;
  562         }
  563         kobj_delete((kobj_t) file, M_LINKER);
  564 out:
  565         return (error);
  566 }
  567 
  568 int
  569 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
  570 {
  571         linker_file_t *newdeps;
  572 
  573         newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
  574             M_LINKER, M_WAITOK | M_ZERO);
  575         if (newdeps == NULL)
  576                 return (ENOMEM);
  577 
  578         if (file->deps) {
  579                 bcopy(file->deps, newdeps,
  580                     file->ndeps * sizeof(linker_file_t *));
  581                 free(file->deps, M_LINKER);
  582         }
  583         file->deps = newdeps;
  584         file->deps[file->ndeps] = dep;
  585         file->ndeps++;
  586         return (0);
  587 }
  588 
  589 /*
  590  * Locate a linker set and its contents.  This is a helper function to avoid
  591  * linker_if.h exposure elsewhere.  Note: firstp and lastp are really void ***
  592  */
  593 int
  594 linker_file_lookup_set(linker_file_t file, const char *name,
  595     void *firstp, void *lastp, int *countp)
  596 {
  597 
  598         return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
  599 }
  600 
  601 caddr_t
  602 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
  603 {
  604         c_linker_sym_t sym;
  605         linker_symval_t symval;
  606         caddr_t address;
  607         size_t common_size = 0;
  608         int i;
  609 
  610         KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
  611             file, name, deps));
  612 
  613         if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
  614                 LINKER_SYMBOL_VALUES(file, sym, &symval);
  615                 if (symval.value == 0)
  616                         /*
  617                          * For commons, first look them up in the
  618                          * dependencies and only allocate space if not found
  619                          * there.
  620                          */
  621                         common_size = symval.size;
  622                 else {
  623                         KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
  624                             ".value=%p\n", symval.value));
  625                         return (symval.value);
  626                 }
  627         }
  628         if (deps) {
  629                 for (i = 0; i < file->ndeps; i++) {
  630                         address = linker_file_lookup_symbol(file->deps[i],
  631                             name, 0);
  632                         if (address) {
  633                                 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
  634                                     " deps value=%p\n", address));
  635                                 return (address);
  636                         }
  637                 }
  638         }
  639         if (common_size > 0) {
  640                 /*
  641                  * This is a common symbol which was not found in the
  642                  * dependencies.  We maintain a simple common symbol table in
  643                  * the file object.
  644                  */
  645                 struct common_symbol *cp;
  646 
  647                 STAILQ_FOREACH(cp, &file->common, link) {
  648                         if (strcmp(cp->name, name) == 0) {
  649                                 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
  650                                     " old common value=%p\n", cp->address));
  651                                 return (cp->address);
  652                         }
  653                 }
  654                 /*
  655                  * Round the symbol size up to align.
  656                  */
  657                 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
  658                 cp = malloc(sizeof(struct common_symbol)
  659                     + common_size + strlen(name) + 1, M_LINKER,
  660                     M_WAITOK | M_ZERO);
  661                 if (cp == NULL) {
  662                         KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
  663                         return (0);
  664                 }
  665                 cp->address = (caddr_t)(cp + 1);
  666                 cp->name = cp->address + common_size;
  667                 strcpy(cp->name, name);
  668                 bzero(cp->address, common_size);
  669                 STAILQ_INSERT_TAIL(&file->common, cp, link);
  670 
  671                 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
  672                     " value=%p\n", cp->address));
  673                 return (cp->address);
  674         }
  675         KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
  676         return (0);
  677 }
  678 
  679 #ifdef DDB
  680 /*
  681  * DDB Helpers.  DDB has to look across multiple files with their own symbol
  682  * tables and string tables.
  683  * 
  684  * Note that we do not obey list locking protocols here.  We really don't need
  685  * DDB to hang because somebody's got the lock held.  We'll take the chance
  686  * that the files list is inconsistant instead.
  687  */
  688 
  689 int
  690 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
  691 {
  692         linker_file_t lf;
  693 
  694         TAILQ_FOREACH(lf, &linker_files, link) {
  695                 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
  696                         return (0);
  697         }
  698         return (ENOENT);
  699 }
  700 
  701 int
  702 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
  703 {
  704         linker_file_t lf;
  705         c_linker_sym_t best, es;
  706         u_long diff, bestdiff, off;
  707 
  708         best = 0;
  709         off = (uintptr_t)value;
  710         bestdiff = off;
  711         TAILQ_FOREACH(lf, &linker_files, link) {
  712                 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
  713                         continue;
  714                 if (es != 0 && diff < bestdiff) {
  715                         best = es;
  716                         bestdiff = diff;
  717                 }
  718                 if (bestdiff == 0)
  719                         break;
  720         }
  721         if (best) {
  722                 *sym = best;
  723                 *diffp = bestdiff;
  724                 return (0);
  725         } else {
  726                 *sym = 0;
  727                 *diffp = off;
  728                 return (ENOENT);
  729         }
  730 }
  731 
  732 int
  733 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
  734 {
  735         linker_file_t lf;
  736 
  737         TAILQ_FOREACH(lf, &linker_files, link) {
  738                 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
  739                         return (0);
  740         }
  741         return (ENOENT);
  742 }
  743 #endif
  744 
  745 /*
  746  * Syscalls.
  747  */
  748 /*
  749  * MPSAFE
  750  */
  751 int
  752 kldload(struct thread *td, struct kldload_args *uap)
  753 {
  754         char *kldname, *modname;
  755         char *pathname = NULL;
  756         linker_file_t lf;
  757         int error = 0;
  758 
  759         td->td_retval[0] = -1;
  760 
  761         mtx_lock(&Giant);
  762 
  763         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
  764                 goto out;
  765 
  766         if ((error = suser(td)) != 0)
  767                 goto out;
  768 
  769         pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
  770         if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
  771                 goto out;
  772 
  773         /*
  774          * If path do not contain qualified name or any dot in it
  775          * (kldname.ko, or kldname.ver.ko) treat it as interface
  776          * name.
  777          */
  778         if (index(pathname, '/') || index(pathname, '.')) {
  779                 kldname = pathname;
  780                 modname = NULL;
  781         } else {
  782                 kldname = NULL;
  783                 modname = pathname;
  784         }
  785         error = linker_load_module(kldname, modname, NULL, NULL, &lf);
  786         if (error)
  787                 goto out;
  788 
  789         lf->userrefs++;
  790         td->td_retval[0] = lf->id;
  791 out:
  792         if (pathname)
  793                 free(pathname, M_TEMP);
  794         mtx_unlock(&Giant);
  795         return (error);
  796 }
  797 
  798 /*
  799  * MPSAFE
  800  */
  801 static int
  802 kern_kldunload(struct thread *td, int fileid, int flags)
  803 {
  804         linker_file_t lf;
  805         int error = 0;
  806 
  807         mtx_lock(&Giant);
  808 
  809         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
  810                 goto out;
  811 
  812         if ((error = suser(td)) != 0)
  813                 goto out;
  814 
  815         lf = linker_find_file_by_id(fileid);
  816         if (lf) {
  817                 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
  818                 if (lf->userrefs == 0) {
  819                         /*
  820                          * XXX: maybe LINKER_UNLOAD_FORCE should override ?
  821                          */
  822                         printf("kldunload: attempt to unload file that was"
  823                             " loaded by the kernel\n");
  824                         error = EBUSY;
  825                         goto out;
  826                 }
  827                 lf->userrefs--;
  828                 error = linker_file_unload(lf, flags);
  829                 if (error)
  830                         lf->userrefs++;
  831         } else
  832                 error = ENOENT;
  833 out:
  834         mtx_unlock(&Giant);
  835         return (error);
  836 }
  837 
  838 /*
  839  * MPSAFE
  840  */
  841 int
  842 kldunload(struct thread *td, struct kldunload_args *uap)
  843 {
  844 
  845         return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
  846 }
  847 
  848 /*
  849  * MPSAFE
  850  */
  851 int
  852 kldunloadf(struct thread *td, struct kldunloadf_args *uap)
  853 {
  854 
  855         if (uap->flags != LINKER_UNLOAD_NORMAL &&
  856             uap->flags != LINKER_UNLOAD_FORCE)
  857                 return (EINVAL);
  858         return (kern_kldunload(td, uap->fileid, uap->flags));
  859 }
  860 
  861 /*
  862  * MPSAFE
  863  */
  864 int
  865 kldfind(struct thread *td, struct kldfind_args *uap)
  866 {
  867         char *pathname;
  868         const char *filename;
  869         linker_file_t lf;
  870         int error = 0;
  871 
  872 #ifdef MAC
  873         error = mac_check_kld_stat(td->td_ucred);
  874         if (error)
  875                 return (error);
  876 #endif
  877 
  878         mtx_lock(&Giant);
  879         td->td_retval[0] = -1;
  880 
  881         pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
  882         if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
  883                 goto out;
  884 
  885         filename = linker_basename(pathname);
  886         lf = linker_find_file_by_name(filename);
  887         if (lf)
  888                 td->td_retval[0] = lf->id;
  889         else
  890                 error = ENOENT;
  891 out:
  892         if (pathname)
  893                 free(pathname, M_TEMP);
  894         mtx_unlock(&Giant);
  895         return (error);
  896 }
  897 
  898 /*
  899  * MPSAFE
  900  */
  901 int
  902 kldnext(struct thread *td, struct kldnext_args *uap)
  903 {
  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 
  915         if (uap->fileid == 0) {
  916                 mtx_lock(&kld_mtx);
  917                 if (TAILQ_FIRST(&linker_files))
  918                         td->td_retval[0] = TAILQ_FIRST(&linker_files)->id;
  919                 else
  920                         td->td_retval[0] = 0;
  921                 mtx_unlock(&kld_mtx);
  922                 goto out;
  923         }
  924         lf = linker_find_file_by_id(uap->fileid);
  925         if (lf) {
  926                 if (TAILQ_NEXT(lf, link))
  927                         td->td_retval[0] = TAILQ_NEXT(lf, link)->id;
  928                 else
  929                         td->td_retval[0] = 0;
  930         } else
  931                 error = ENOENT;
  932 out:
  933         mtx_unlock(&Giant);
  934         return (error);
  935 }
  936 
  937 /*
  938  * MPSAFE
  939  */
  940 int
  941 kldstat(struct thread *td, struct kldstat_args *uap)
  942 {
  943         linker_file_t lf;
  944         int error = 0;
  945         int namelen, version;
  946         struct kld_file_stat *stat;
  947 
  948 #ifdef MAC
  949         error = mac_check_kld_stat(td->td_ucred);
  950         if (error)
  951                 return (error);
  952 #endif
  953 
  954         mtx_lock(&Giant);
  955 
  956         lf = linker_find_file_by_id(uap->fileid);
  957         if (lf == NULL) {
  958                 error = ENOENT;
  959                 goto out;
  960         }
  961         stat = uap->stat;
  962 
  963         /*
  964          * Check the version of the user's structure.
  965          */
  966         if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
  967                 goto out;
  968         if (version != sizeof(struct kld_file_stat)) {
  969                 error = EINVAL;
  970                 goto out;
  971         }
  972         namelen = strlen(lf->filename) + 1;
  973         if (namelen > MAXPATHLEN)
  974                 namelen = MAXPATHLEN;
  975         if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
  976                 goto out;
  977         if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
  978                 goto out;
  979         if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
  980                 goto out;
  981         if ((error = copyout(&lf->address, &stat->address,
  982             sizeof(caddr_t))) != 0)
  983                 goto out;
  984         if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
  985                 goto out;
  986 
  987         td->td_retval[0] = 0;
  988 out:
  989         mtx_unlock(&Giant);
  990         return (error);
  991 }
  992 
  993 /*
  994  * MPSAFE
  995  */
  996 int
  997 kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
  998 {
  999         linker_file_t lf;
 1000         module_t mp;
 1001         int error = 0;
 1002 
 1003 #ifdef MAC
 1004         error = mac_check_kld_stat(td->td_ucred);
 1005         if (error)
 1006                 return (error);
 1007 #endif
 1008 
 1009         mtx_lock(&Giant);
 1010         lf = linker_find_file_by_id(uap->fileid);
 1011         if (lf) {
 1012                 MOD_SLOCK;
 1013                 mp = TAILQ_FIRST(&lf->modules);
 1014                 if (mp != NULL)
 1015                         td->td_retval[0] = module_getid(mp);
 1016                 else
 1017                         td->td_retval[0] = 0;
 1018                 MOD_SUNLOCK;
 1019         } else
 1020                 error = ENOENT;
 1021         mtx_unlock(&Giant);
 1022         return (error);
 1023 }
 1024 
 1025 /*
 1026  * MPSAFE
 1027  */
 1028 int
 1029 kldsym(struct thread *td, struct kldsym_args *uap)
 1030 {
 1031         char *symstr = NULL;
 1032         c_linker_sym_t sym;
 1033         linker_symval_t symval;
 1034         linker_file_t lf;
 1035         struct kld_sym_lookup lookup;
 1036         int error = 0;
 1037 
 1038 #ifdef MAC
 1039         error = mac_check_kld_stat(td->td_ucred);
 1040         if (error)
 1041                 return (error);
 1042 #endif
 1043 
 1044         mtx_lock(&Giant);
 1045 
 1046         if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
 1047                 goto out;
 1048         if (lookup.version != sizeof(lookup) ||
 1049             uap->cmd != KLDSYM_LOOKUP) {
 1050                 error = EINVAL;
 1051                 goto out;
 1052         }
 1053         symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
 1054         if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
 1055                 goto out;
 1056         if (uap->fileid != 0) {
 1057                 lf = linker_find_file_by_id(uap->fileid);
 1058                 if (lf == NULL) {
 1059                         error = ENOENT;
 1060                         goto out;
 1061                 }
 1062                 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
 1063                     LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
 1064                         lookup.symvalue = (uintptr_t) symval.value;
 1065                         lookup.symsize = symval.size;
 1066                         error = copyout(&lookup, uap->data, sizeof(lookup));
 1067                 } else
 1068                         error = ENOENT;
 1069         } else {
 1070                 mtx_lock(&kld_mtx);
 1071                 TAILQ_FOREACH(lf, &linker_files, link) {
 1072                         if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
 1073                             LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
 1074                                 lookup.symvalue = (uintptr_t)symval.value;
 1075                                 lookup.symsize = symval.size;
 1076                                 error = copyout(&lookup, uap->data,
 1077                                     sizeof(lookup));
 1078                                 break;
 1079                         }
 1080                 }
 1081                 mtx_unlock(&kld_mtx);
 1082                 if (lf == NULL)
 1083                         error = ENOENT;
 1084         }
 1085 out:
 1086         if (symstr)
 1087                 free(symstr, M_TEMP);
 1088         mtx_unlock(&Giant);
 1089         return (error);
 1090 }
 1091 
 1092 /*
 1093  * Preloaded module support
 1094  */
 1095 
 1096 static modlist_t
 1097 modlist_lookup(const char *name, int ver)
 1098 {
 1099         modlist_t mod;
 1100 
 1101         TAILQ_FOREACH(mod, &found_modules, link) {
 1102                 if (strcmp(mod->name, name) == 0 &&
 1103                     (ver == 0 || mod->version == ver))
 1104                         return (mod);
 1105         }
 1106         return (NULL);
 1107 }
 1108 
 1109 static modlist_t
 1110 modlist_lookup2(const char *name, struct mod_depend *verinfo)
 1111 {
 1112         modlist_t mod, bestmod;
 1113         int ver;
 1114 
 1115         if (verinfo == NULL)
 1116                 return (modlist_lookup(name, 0));
 1117         bestmod = NULL;
 1118         for (mod = TAILQ_FIRST(&found_modules); mod;
 1119             mod = TAILQ_NEXT(mod, link)) {
 1120                 if (strcmp(mod->name, name) != 0)
 1121                         continue;
 1122                 ver = mod->version;
 1123                 if (ver == verinfo->md_ver_preferred)
 1124                         return (mod);
 1125                 if (ver >= verinfo->md_ver_minimum &&
 1126                     ver <= verinfo->md_ver_maximum &&
 1127                     (bestmod == NULL || ver > bestmod->version))
 1128                         bestmod = mod;
 1129         }
 1130         return (bestmod);
 1131 }
 1132 
 1133 static modlist_t
 1134 modlist_newmodule(const char *modname, int version, linker_file_t container)
 1135 {
 1136         modlist_t mod;
 1137 
 1138         mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
 1139         if (mod == NULL)
 1140                 panic("no memory for module list");
 1141         mod->container = container;
 1142         mod->name = modname;
 1143         mod->version = version;
 1144         TAILQ_INSERT_TAIL(&found_modules, mod, link);
 1145         return (mod);
 1146 }
 1147 
 1148 static void
 1149 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
 1150     struct mod_metadata **stop, int preload)
 1151 {
 1152         struct mod_metadata *mp, **mdp;
 1153         const char *modname;
 1154         int ver;
 1155 
 1156         for (mdp = start; mdp < stop; mdp++) {
 1157                 mp = *mdp;
 1158                 if (mp->md_type != MDT_VERSION)
 1159                         continue;
 1160                 modname = mp->md_cval;
 1161                 ver = ((struct mod_version *)mp->md_data)->mv_version;
 1162                 if (modlist_lookup(modname, ver) != NULL) {
 1163                         printf("module %s already present!\n", modname);
 1164                         /* XXX what can we do? this is a build error. :-( */
 1165                         continue;
 1166                 }
 1167                 modlist_newmodule(modname, ver, lf);
 1168         }
 1169 }
 1170 
 1171 static void
 1172 linker_preload(void *arg)
 1173 {
 1174         caddr_t modptr;
 1175         const char *modname, *nmodname;
 1176         char *modtype;
 1177         linker_file_t lf;
 1178         linker_class_t lc;
 1179         int error;
 1180         linker_file_list_t loaded_files;
 1181         linker_file_list_t depended_files;
 1182         struct mod_metadata *mp, *nmp;
 1183         struct mod_metadata **start, **stop, **mdp, **nmdp;
 1184         struct mod_depend *verinfo;
 1185         int nver;
 1186         int resolves;
 1187         modlist_t mod;
 1188         struct sysinit **si_start, **si_stop;
 1189 
 1190         TAILQ_INIT(&loaded_files);
 1191         TAILQ_INIT(&depended_files);
 1192         TAILQ_INIT(&found_modules);
 1193         error = 0;
 1194 
 1195         modptr = NULL;
 1196         while ((modptr = preload_search_next_name(modptr)) != NULL) {
 1197                 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
 1198                 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
 1199                 if (modname == NULL) {
 1200                         printf("Preloaded module at %p does not have a"
 1201                             " name!\n", modptr);
 1202                         continue;
 1203                 }
 1204                 if (modtype == NULL) {
 1205                         printf("Preloaded module at %p does not have a type!\n",
 1206                             modptr);
 1207                         continue;
 1208                 }
 1209                 if (bootverbose)
 1210                         printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
 1211                             modptr);
 1212                 lf = NULL;
 1213                 TAILQ_FOREACH(lc, &classes, link) {
 1214                         error = LINKER_LINK_PRELOAD(lc, modname, &lf);
 1215                         if (!error)
 1216                                 break;
 1217                         lf = NULL;
 1218                 }
 1219                 if (lf)
 1220                         TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
 1221         }
 1222 
 1223         /*
 1224          * First get a list of stuff in the kernel.
 1225          */
 1226         if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
 1227             &stop, NULL) == 0)
 1228                 linker_addmodules(linker_kernel_file, start, stop, 1);
 1229 
 1230         /*
 1231          * this is a once-off kinky bubble sort resolve relocation dependency
 1232          * requirements
 1233          */
 1234 restart:
 1235         TAILQ_FOREACH(lf, &loaded_files, loaded) {
 1236                 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
 1237                     &stop, NULL);
 1238                 /*
 1239                  * First, look to see if we would successfully link with this
 1240                  * stuff.
 1241                  */
 1242                 resolves = 1;   /* unless we know otherwise */
 1243                 if (!error) {
 1244                         for (mdp = start; mdp < stop; mdp++) {
 1245                                 mp = *mdp;
 1246                                 if (mp->md_type != MDT_DEPEND)
 1247                                         continue;
 1248                                 modname = mp->md_cval;
 1249                                 verinfo = mp->md_data;
 1250                                 for (nmdp = start; nmdp < stop; nmdp++) {
 1251                                         nmp = *nmdp;
 1252                                         if (nmp->md_type != MDT_VERSION)
 1253                                                 continue;
 1254                                         nmodname = nmp->md_cval;
 1255                                         if (strcmp(modname, nmodname) == 0)
 1256                                                 break;
 1257                                 }
 1258                                 if (nmdp < stop)   /* it's a self reference */
 1259                                         continue;
 1260         
 1261                                 /*
 1262                                  * ok, the module isn't here yet, we
 1263                                  * are not finished
 1264                                  */
 1265                                 if (modlist_lookup2(modname, verinfo) == NULL)
 1266                                         resolves = 0;
 1267                         }
 1268                 }
 1269                 /*
 1270                  * OK, if we found our modules, we can link.  So, "provide"
 1271                  * the modules inside and add it to the end of the link order
 1272                  * list.
 1273                  */
 1274                 if (resolves) {
 1275                         if (!error) {
 1276                                 for (mdp = start; mdp < stop; mdp++) {
 1277                                         mp = *mdp;
 1278                                         if (mp->md_type != MDT_VERSION)
 1279                                                 continue;
 1280                                         modname = mp->md_cval;
 1281                                         nver = ((struct mod_version *)
 1282                                             mp->md_data)->mv_version;
 1283                                         if (modlist_lookup(modname,
 1284                                             nver) != NULL) {
 1285                                                 printf("module %s already"
 1286                                                     " present!\n", modname);
 1287                                                 linker_file_unload(lf,
 1288                                                     LINKER_UNLOAD_FORCE);
 1289                                                 TAILQ_REMOVE(&loaded_files,
 1290                                                     lf, loaded);
 1291                                                 /* we changed tailq next ptr */
 1292                                                 goto restart;
 1293                                         }
 1294                                         modlist_newmodule(modname, nver, lf);
 1295                                 }
 1296                         }
 1297                         TAILQ_REMOVE(&loaded_files, lf, loaded);
 1298                         TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
 1299                         /*
 1300                          * Since we provided modules, we need to restart the
 1301                          * sort so that the previous files that depend on us
 1302                          * have a chance. Also, we've busted the tailq next
 1303                          * pointer with the REMOVE.
 1304                          */
 1305                         goto restart;
 1306                 }
 1307         }
 1308 
 1309         /*
 1310          * At this point, we check to see what could not be resolved..
 1311          */
 1312         TAILQ_FOREACH(lf, &loaded_files, loaded) {
 1313                 printf("KLD file %s is missing dependencies\n", lf->filename);
 1314                 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
 1315                 TAILQ_REMOVE(&loaded_files, lf, loaded);
 1316         }
 1317 
 1318         /*
 1319          * We made it. Finish off the linking in the order we determined.
 1320          */
 1321         TAILQ_FOREACH(lf, &depended_files, loaded) {
 1322                 if (linker_kernel_file) {
 1323                         linker_kernel_file->refs++;
 1324                         error = linker_file_add_dependency(lf,
 1325                             linker_kernel_file);
 1326                         if (error)
 1327                                 panic("cannot add dependency");
 1328                 }
 1329                 lf->userrefs++; /* so we can (try to) kldunload it */
 1330                 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
 1331                     &stop, NULL);
 1332                 if (!error) {
 1333                         for (mdp = start; mdp < stop; mdp++) {
 1334                                 mp = *mdp;
 1335                                 if (mp->md_type != MDT_DEPEND)
 1336                                         continue;
 1337                                 modname = mp->md_cval;
 1338                                 verinfo = mp->md_data;
 1339                                 mod = modlist_lookup2(modname, verinfo);
 1340                                 mod->container->refs++;
 1341                                 error = linker_file_add_dependency(lf,
 1342                                     mod->container);
 1343                                 if (error)
 1344                                         panic("cannot add dependency");
 1345                         }
 1346                 }
 1347                 /*
 1348                  * Now do relocation etc using the symbol search paths
 1349                  * established by the dependencies
 1350                  */
 1351                 error = LINKER_LINK_PRELOAD_FINISH(lf);
 1352                 if (error) {
 1353                         printf("KLD file %s - could not finalize loading\n",
 1354                             lf->filename);
 1355                         linker_file_unload(lf, LINKER_UNLOAD_FORCE);
 1356                         continue;
 1357                 }
 1358                 linker_file_register_modules(lf);
 1359                 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
 1360                     &si_stop, NULL) == 0)
 1361                         sysinit_add(si_start, si_stop);
 1362                 linker_file_register_sysctls(lf);
 1363                 lf->flags |= LINKER_FILE_LINKED;
 1364         }
 1365         /* woohoo! we made it! */
 1366 }
 1367 
 1368 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0)
 1369 
 1370 /*
 1371  * Search for a not-loaded module by name.
 1372  * 
 1373  * Modules may be found in the following locations:
 1374  * 
 1375  * - preloaded (result is just the module name) - on disk (result is full path
 1376  * to module)
 1377  * 
 1378  * If the module name is qualified in any way (contains path, etc.) the we
 1379  * simply return a copy of it.
 1380  * 
 1381  * The search path can be manipulated via sysctl.  Note that we use the ';'
 1382  * character as a separator to be consistent with the bootloader.
 1383  */
 1384 
 1385 static char linker_hintfile[] = "linker.hints";
 1386 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
 1387 
 1388 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
 1389     sizeof(linker_path), "module load search path");
 1390 
 1391 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
 1392 
 1393 static char *linker_ext_list[] = {
 1394         "",
 1395         ".ko",
 1396         NULL
 1397 };
 1398 
 1399 /*
 1400  * Check if file actually exists either with or without extension listed in
 1401  * the linker_ext_list. (probably should be generic for the rest of the
 1402  * kernel)
 1403  */
 1404 static char *
 1405 linker_lookup_file(const char *path, int pathlen, const char *name,
 1406     int namelen, struct vattr *vap)
 1407 {
 1408         struct nameidata nd;
 1409         struct thread *td = curthread;  /* XXX */
 1410         char *result, **cpp, *sep;
 1411         int error, len, extlen, reclen, flags;
 1412         enum vtype type;
 1413 
 1414         extlen = 0;
 1415         for (cpp = linker_ext_list; *cpp; cpp++) {
 1416                 len = strlen(*cpp);
 1417                 if (len > extlen)
 1418                         extlen = len;
 1419         }
 1420         extlen++;               /* trailing '\0' */
 1421         sep = (path[pathlen - 1] != '/') ? "/" : "";
 1422 
 1423         reclen = pathlen + strlen(sep) + namelen + extlen + 1;
 1424         result = malloc(reclen, M_LINKER, M_WAITOK);
 1425         for (cpp = linker_ext_list; *cpp; cpp++) {
 1426                 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
 1427                     namelen, name, *cpp);
 1428                 /*
 1429                  * Attempt to open the file, and return the path if
 1430                  * we succeed and it's a regular file.
 1431                  */
 1432                 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td);
 1433                 flags = FREAD;
 1434                 error = vn_open(&nd, &flags, 0, -1);
 1435                 if (error == 0) {
 1436                         NDFREE(&nd, NDF_ONLY_PNBUF);
 1437                         type = nd.ni_vp->v_type;
 1438                         if (vap)
 1439                                 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred, td);
 1440                         VOP_UNLOCK(nd.ni_vp, 0, td);
 1441                         vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
 1442                         if (type == VREG)
 1443                                 return (result);
 1444                 }
 1445         }
 1446         free(result, M_LINKER);
 1447         return (NULL);
 1448 }
 1449 
 1450 #define INT_ALIGN(base, ptr)    ptr =                                   \
 1451         (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
 1452 
 1453 /*
 1454  * Lookup KLD which contains requested module in the "linker.hints" file. If
 1455  * version specification is available, then try to find the best KLD.
 1456  * Otherwise just find the latest one.
 1457  */
 1458 static char *
 1459 linker_hints_lookup(const char *path, int pathlen, const char *modname,
 1460     int modnamelen, struct mod_depend *verinfo)
 1461 {
 1462         struct thread *td = curthread;  /* XXX */
 1463         struct ucred *cred = td ? td->td_ucred : NULL;
 1464         struct nameidata nd;
 1465         struct vattr vattr, mattr;
 1466         u_char *hints = NULL;
 1467         u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
 1468         int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
 1469 
 1470         result = NULL;
 1471         bestver = found = 0;
 1472 
 1473         sep = (path[pathlen - 1] != '/') ? "/" : "";
 1474         reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
 1475             strlen(sep) + 1;
 1476         pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
 1477         snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
 1478             linker_hintfile);
 1479 
 1480         NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td);
 1481         flags = FREAD;
 1482         error = vn_open(&nd, &flags, 0, -1);
 1483         if (error)
 1484                 goto bad;
 1485         NDFREE(&nd, NDF_ONLY_PNBUF);
 1486         if (nd.ni_vp->v_type != VREG)
 1487                 goto bad;
 1488         best = cp = NULL;
 1489         error = VOP_GETATTR(nd.ni_vp, &vattr, cred, td);
 1490         if (error)
 1491                 goto bad;
 1492         /*
 1493          * XXX: we need to limit this number to some reasonable value
 1494          */
 1495         if (vattr.va_size > 100 * 1024) {
 1496                 printf("hints file too large %ld\n", (long)vattr.va_size);
 1497                 goto bad;
 1498         }
 1499         hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
 1500         if (hints == NULL)
 1501                 goto bad;
 1502         error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
 1503             UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
 1504         if (error)
 1505                 goto bad;
 1506         VOP_UNLOCK(nd.ni_vp, 0, td);
 1507         vn_close(nd.ni_vp, FREAD, cred, td);
 1508         nd.ni_vp = NULL;
 1509         if (reclen != 0) {
 1510                 printf("can't read %d\n", reclen);
 1511                 goto bad;
 1512         }
 1513         intp = (int *)hints;
 1514         ival = *intp++;
 1515         if (ival != LINKER_HINTS_VERSION) {
 1516                 printf("hints file version mismatch %d\n", ival);
 1517                 goto bad;
 1518         }
 1519         bufend = hints + vattr.va_size;
 1520         recptr = (u_char *)intp;
 1521         clen = blen = 0;
 1522         while (recptr < bufend && !found) {
 1523                 intp = (int *)recptr;
 1524                 reclen = *intp++;
 1525                 ival = *intp++;
 1526                 cp = (char *)intp;
 1527                 switch (ival) {
 1528                 case MDT_VERSION:
 1529                         clen = *cp++;
 1530                         if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
 1531                                 break;
 1532                         cp += clen;
 1533                         INT_ALIGN(hints, cp);
 1534                         ival = *(int *)cp;
 1535                         cp += sizeof(int);
 1536                         clen = *cp++;
 1537                         if (verinfo == NULL ||
 1538                             ival == verinfo->md_ver_preferred) {
 1539                                 found = 1;
 1540                                 break;
 1541                         }
 1542                         if (ival >= verinfo->md_ver_minimum &&
 1543                             ival <= verinfo->md_ver_maximum &&
 1544                             ival > bestver) {
 1545                                 bestver = ival;
 1546                                 best = cp;
 1547                                 blen = clen;
 1548                         }
 1549                         break;
 1550                 default:
 1551                         break;
 1552                 }
 1553                 recptr += reclen + sizeof(int);
 1554         }
 1555         /*
 1556          * Finally check if KLD is in the place
 1557          */
 1558         if (found)
 1559                 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
 1560         else if (best)
 1561                 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
 1562 
 1563         /*
 1564          * KLD is newer than hints file. What we should do now?
 1565          */
 1566         if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
 1567                 printf("warning: KLD '%s' is newer than the linker.hints"
 1568                     " file\n", result);
 1569 bad:
 1570         free(pathbuf, M_LINKER);
 1571         if (hints)
 1572                 free(hints, M_TEMP);
 1573         if (nd.ni_vp != NULL) {
 1574                 VOP_UNLOCK(nd.ni_vp, 0, td);
 1575                 vn_close(nd.ni_vp, FREAD, cred, td);
 1576         }
 1577         /*
 1578          * If nothing found or hints is absent - fallback to the old
 1579          * way by using "kldname[.ko]" as module name.
 1580          */
 1581         if (!found && !bestver && result == NULL)
 1582                 result = linker_lookup_file(path, pathlen, modname,
 1583                     modnamelen, NULL);
 1584         return (result);
 1585 }
 1586 
 1587 /*
 1588  * Lookup KLD which contains requested module in the all directories.
 1589  */
 1590 static char *
 1591 linker_search_module(const char *modname, int modnamelen,
 1592     struct mod_depend *verinfo)
 1593 {
 1594         char *cp, *ep, *result;
 1595 
 1596         /*
 1597          * traverse the linker path
 1598          */
 1599         for (cp = linker_path; *cp; cp = ep + 1) {
 1600                 /* find the end of this component */
 1601                 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
 1602                 result = linker_hints_lookup(cp, ep - cp, modname,
 1603                     modnamelen, verinfo);
 1604                 if (result != NULL)
 1605                         return (result);
 1606                 if (*ep == 0)
 1607                         break;
 1608         }
 1609         return (NULL);
 1610 }
 1611 
 1612 /*
 1613  * Search for module in all directories listed in the linker_path.
 1614  */
 1615 static char *
 1616 linker_search_kld(const char *name)
 1617 {
 1618         char *cp, *ep, *result, **cpp;
 1619         int extlen, len;
 1620 
 1621         /* qualified at all? */
 1622         if (index(name, '/'))
 1623                 return (linker_strdup(name));
 1624 
 1625         extlen = 0;
 1626         for (cpp = linker_ext_list; *cpp; cpp++) {
 1627                 len = strlen(*cpp);
 1628                 if (len > extlen)
 1629                         extlen = len;
 1630         }
 1631         extlen++;               /* trailing '\0' */
 1632 
 1633         /* traverse the linker path */
 1634         len = strlen(name);
 1635         for (ep = linker_path; *ep; ep++) {
 1636                 cp = ep;
 1637                 /* find the end of this component */
 1638                 for (; *ep != 0 && *ep != ';'; ep++);
 1639                 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
 1640                 if (result != NULL)
 1641                         return (result);
 1642         }
 1643         return (NULL);
 1644 }
 1645 
 1646 static const char *
 1647 linker_basename(const char *path)
 1648 {
 1649         const char *filename;
 1650 
 1651         filename = rindex(path, '/');
 1652         if (filename == NULL)
 1653                 return path;
 1654         if (filename[1])
 1655                 filename++;
 1656         return (filename);
 1657 }
 1658 
 1659 /*
 1660  * Find a file which contains given module and load it, if "parent" is not
 1661  * NULL, register a reference to it.
 1662  */
 1663 int
 1664 linker_load_module(const char *kldname, const char *modname,
 1665     struct linker_file *parent, struct mod_depend *verinfo,
 1666     struct linker_file **lfpp)
 1667 {
 1668         linker_file_t lfdep;
 1669         const char *filename;
 1670         char *pathname;
 1671         int error;
 1672 
 1673         if (modname == NULL) {
 1674                 /*
 1675                  * We have to load KLD
 1676                  */
 1677                 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
 1678                     " is not NULL"));
 1679                 pathname = linker_search_kld(kldname);
 1680         } else {
 1681                 if (modlist_lookup2(modname, verinfo) != NULL)
 1682                         return (EEXIST);
 1683                 if (kldname != NULL)
 1684                         pathname = linker_strdup(kldname);
 1685                 else if (rootvnode == NULL)
 1686                         pathname = NULL;
 1687                 else
 1688                         /*
 1689                          * Need to find a KLD with required module
 1690                          */
 1691                         pathname = linker_search_module(modname,
 1692                             strlen(modname), verinfo);
 1693         }
 1694         if (pathname == NULL)
 1695                 return (ENOENT);
 1696 
 1697         /*
 1698          * Can't load more than one file with the same basename XXX:
 1699          * Actually it should be possible to have multiple KLDs with
 1700          * the same basename but different path because they can
 1701          * provide different versions of the same modules.
 1702          */
 1703         filename = linker_basename(pathname);
 1704         if (linker_find_file_by_name(filename)) {
 1705                 error = EEXIST;
 1706                 goto out;
 1707         }
 1708         do {
 1709                 error = linker_load_file(pathname, &lfdep);
 1710                 if (error)
 1711                         break;
 1712                 if (modname && verinfo &&
 1713                     modlist_lookup2(modname, verinfo) == NULL) {
 1714                         linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
 1715                         error = ENOENT;
 1716                         break;
 1717                 }
 1718                 if (parent) {
 1719                         error = linker_file_add_dependency(parent, lfdep);
 1720                         if (error)
 1721                                 break;
 1722                 }
 1723                 if (lfpp)
 1724                         *lfpp = lfdep;
 1725         } while (0);
 1726 out:
 1727         if (pathname)
 1728                 free(pathname, M_LINKER);
 1729         return (error);
 1730 }
 1731 
 1732 /*
 1733  * This routine is responsible for finding dependencies of userland initiated
 1734  * kldload(2)'s of files.
 1735  */
 1736 int
 1737 linker_load_dependencies(linker_file_t lf)
 1738 {
 1739         linker_file_t lfdep;
 1740         struct mod_metadata **start, **stop, **mdp, **nmdp;
 1741         struct mod_metadata *mp, *nmp;
 1742         struct mod_depend *verinfo;
 1743         modlist_t mod;
 1744         const char *modname, *nmodname;
 1745         int ver, error = 0, count;
 1746 
 1747         /*
 1748          * All files are dependant on /kernel.
 1749          */
 1750         if (linker_kernel_file) {
 1751                 linker_kernel_file->refs++;
 1752                 error = linker_file_add_dependency(lf, linker_kernel_file);
 1753                 if (error)
 1754                         return (error);
 1755         }
 1756         if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
 1757             &count) != 0)
 1758                 return (0);
 1759         for (mdp = start; mdp < stop; mdp++) {
 1760                 mp = *mdp;
 1761                 if (mp->md_type != MDT_VERSION)
 1762                         continue;
 1763                 modname = mp->md_cval;
 1764                 ver = ((struct mod_version *)mp->md_data)->mv_version;
 1765                 mod = modlist_lookup(modname, ver);
 1766                 if (mod != NULL) {
 1767                         printf("interface %s.%d already present in the KLD"
 1768                             " '%s'!\n", modname, ver,
 1769                             mod->container->filename);
 1770                         return (EEXIST);
 1771                 }
 1772         }
 1773 
 1774         for (mdp = start; mdp < stop; mdp++) {
 1775                 mp = *mdp;
 1776                 if (mp->md_type != MDT_DEPEND)
 1777                         continue;
 1778                 modname = mp->md_cval;
 1779                 verinfo = mp->md_data;
 1780                 nmodname = NULL;
 1781                 for (nmdp = start; nmdp < stop; nmdp++) {
 1782                         nmp = *nmdp;
 1783                         if (nmp->md_type != MDT_VERSION)
 1784                                 continue;
 1785                         nmodname = nmp->md_cval;
 1786                         if (strcmp(modname, nmodname) == 0)
 1787                                 break;
 1788                 }
 1789                 if (nmdp < stop)/* early exit, it's a self reference */
 1790                         continue;
 1791                 mod = modlist_lookup2(modname, verinfo);
 1792                 if (mod) {      /* woohoo, it's loaded already */
 1793                         lfdep = mod->container;
 1794                         lfdep->refs++;
 1795                         error = linker_file_add_dependency(lf, lfdep);
 1796                         if (error)
 1797                                 break;
 1798                         continue;
 1799                 }
 1800                 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
 1801                 if (error) {
 1802                         printf("KLD %s: depends on %s - not available\n",
 1803                             lf->filename, modname);
 1804                         break;
 1805                 }
 1806         }
 1807 
 1808         if (error)
 1809                 return (error);
 1810         linker_addmodules(lf, start, stop, 0);
 1811         return (error);
 1812 }
 1813 
 1814 static int
 1815 sysctl_kern_function_list_iterate(const char *name, void *opaque)
 1816 {
 1817         struct sysctl_req *req;
 1818 
 1819         req = opaque;
 1820         return (SYSCTL_OUT(req, name, strlen(name) + 1));
 1821 }
 1822 
 1823 /*
 1824  * Export a nul-separated, double-nul-terminated list of all function names
 1825  * in the kernel.
 1826  */
 1827 static int
 1828 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
 1829 {
 1830         linker_file_t lf;
 1831         int error;
 1832 
 1833 #ifdef MAC
 1834         error = mac_check_kld_stat(req->td->td_ucred);
 1835         if (error)
 1836                 return (error);
 1837 #endif
 1838         error = sysctl_wire_old_buffer(req, 0);
 1839         if (error != 0)
 1840                 return (error);
 1841         mtx_lock(&kld_mtx);
 1842         TAILQ_FOREACH(lf, &linker_files, link) {
 1843                 error = LINKER_EACH_FUNCTION_NAME(lf,
 1844                     sysctl_kern_function_list_iterate, req);
 1845                 if (error) {
 1846                         mtx_unlock(&kld_mtx);
 1847                         return (error);
 1848                 }
 1849         }
 1850         mtx_unlock(&kld_mtx);
 1851         return (SYSCTL_OUT(req, "", 1));
 1852 }
 1853 
 1854 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD,
 1855     NULL, 0, sysctl_kern_function_list, "", "kernel function list");

Cache object: daf2dd99fa9405299a0ff840d34b8861


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