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 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  * $FreeBSD: src/sys/kern/kern_linker.c,v 1.41.2.3 2001/11/21 17:50:35 luigi Exp $
   27  */
   28 
   29 #include "opt_ddb.h"
   30 
   31 #include <sys/param.h>
   32 #include <sys/kernel.h>
   33 #include <sys/systm.h>
   34 #include <sys/malloc.h>
   35 #include <sys/sysproto.h>
   36 #include <sys/sysent.h>
   37 #include <sys/proc.h>
   38 #include <sys/priv.h>
   39 #include <sys/lock.h>
   40 #include <sys/module.h>
   41 #include <sys/queue.h>
   42 #include <sys/linker.h>
   43 #include <sys/fcntl.h>
   44 #include <sys/libkern.h>
   45 #include <sys/nlookup.h>
   46 #include <sys/vnode.h>
   47 #include <sys/sysctl.h>
   48 
   49 #include <vm/vm_zone.h>
   50 
   51 #include <sys/mplock2.h>
   52 
   53 #ifdef _KERNEL_VIRTUAL
   54 #include <dlfcn.h>
   55 #endif
   56 
   57 #ifdef KLD_DEBUG
   58 int kld_debug = 1;
   59 #endif
   60 
   61 /* Metadata from the static kernel */
   62 SET_DECLARE(modmetadata_set, struct mod_metadata);
   63 MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
   64 
   65 linker_file_t linker_current_file;
   66 linker_file_t linker_kernel_file;
   67 
   68 static struct lock lock;        /* lock for the file list */
   69 static linker_class_list_t classes;
   70 static linker_file_list_t linker_files;
   71 static int next_file_id = 1;
   72 
   73 /* XXX wrong name; we're looking at version provision tags here, not modules */
   74 typedef TAILQ_HEAD(, modlist) modlisthead_t;
   75 struct modlist {
   76         TAILQ_ENTRY(modlist) link;      /* chain together all modules */
   77         linker_file_t   container;
   78         const char      *name;
   79         int             version;
   80 };
   81 typedef struct modlist *modlist_t;
   82 static modlisthead_t found_modules;
   83 
   84 
   85 static int linker_load_module(const char *kldname, const char *modname,
   86                               struct linker_file *parent, struct mod_depend *verinfo,
   87                               struct linker_file **lfpp);
   88 
   89 static char *
   90 linker_strdup(const char *str)
   91 {
   92     char        *result;
   93 
   94     result = kmalloc(strlen(str) + 1, M_LINKER, M_WAITOK);
   95     strcpy(result, str);
   96     return(result);
   97 }
   98 
   99 static void
  100 linker_init(void* arg)
  101 {
  102     lockinit(&lock, "klink", 0, 0);
  103     TAILQ_INIT(&classes);
  104     TAILQ_INIT(&linker_files);
  105 }
  106 
  107 SYSINIT(linker, SI_BOOT2_KLD, SI_ORDER_FIRST, linker_init, 0);
  108 
  109 int
  110 linker_add_class(const char* desc, void* priv,
  111                  struct linker_class_ops* ops)
  112 {
  113     linker_class_t lc;
  114 
  115     lc = kmalloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT | M_ZERO);
  116     if (!lc)
  117         return ENOMEM;
  118 
  119     lc->desc = desc;
  120     lc->priv = priv;
  121     lc->ops = ops;
  122     TAILQ_INSERT_HEAD(&classes, lc, link);
  123 
  124     return 0;
  125 }
  126 
  127 static void
  128 linker_file_sysinit(linker_file_t lf)
  129 {
  130     struct sysinit** start, ** stop;
  131     struct sysinit** sipp;
  132     struct sysinit** xipp;
  133     struct sysinit* save;
  134 
  135     KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
  136                    lf->filename));
  137 
  138     if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
  139         return;
  140 
  141     /*
  142      * Perform a bubble sort of the system initialization objects by
  143      * their subsystem (primary key) and order (secondary key).
  144      *
  145      * Since some things care about execution order, this is the
  146      * operation which ensures continued function.
  147      */
  148     for (sipp = start; sipp < stop; sipp++) {
  149         for (xipp = sipp + 1; xipp < stop; xipp++) {
  150             if ((*sipp)->subsystem < (*xipp)->subsystem ||
  151                  ((*sipp)->subsystem == (*xipp)->subsystem &&
  152                   (*sipp)->order <= (*xipp)->order))
  153                 continue;       /* skip*/
  154             save = *sipp;
  155             *sipp = *xipp;
  156             *xipp = save;
  157         }
  158     }
  159 
  160 
  161     /*
  162      * Traverse the (now) ordered list of system initialization tasks.
  163      * Perform each task, and continue on to the next task.
  164      */
  165     for (sipp = start; sipp < stop; sipp++) {
  166         if ((*sipp)->subsystem == SI_SPECIAL_DUMMY)
  167             continue;   /* skip dummy task(s)*/
  168 
  169         /* Call function */
  170         (*((*sipp)->func))((*sipp)->udata);
  171     }
  172 }
  173 
  174 static void
  175 linker_file_sysuninit(linker_file_t lf)
  176 {
  177     struct sysinit** start, ** stop;
  178     struct sysinit** sipp;
  179     struct sysinit** xipp;
  180     struct sysinit* save;
  181 
  182     KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
  183                    lf->filename));
  184 
  185     if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0)
  186         return;
  187 
  188     /*
  189      * Perform a reverse bubble sort of the system initialization objects
  190      * by their subsystem (primary key) and order (secondary key).
  191      *
  192      * Since some things care about execution order, this is the
  193      * operation which ensures continued function.
  194      */
  195     for (sipp = start; sipp < stop; sipp++) {
  196         for (xipp = sipp + 1; xipp < stop; xipp++) {
  197             if ((*sipp)->subsystem > (*xipp)->subsystem ||
  198                  ((*sipp)->subsystem == (*xipp)->subsystem &&
  199                   (*sipp)->order >= (*xipp)->order))
  200                 continue;       /* skip*/
  201             save = *sipp;
  202             *sipp = *xipp;
  203             *xipp = save;
  204         }
  205     }
  206 
  207 
  208     /*
  209      * Traverse the (now) ordered list of system initialization tasks.
  210      * Perform each task, and continue on to the next task.
  211      */
  212     for (sipp = start; sipp < stop; sipp++) {
  213         if ((*sipp)->subsystem == SI_SPECIAL_DUMMY)
  214             continue;   /* skip dummy task(s)*/
  215 
  216         /* Call function */
  217         (*((*sipp)->func))((*sipp)->udata);
  218     }
  219 }
  220 
  221 static void
  222 linker_file_register_sysctls(linker_file_t lf)
  223 {
  224     struct sysctl_oid **start, **stop, **oidp;
  225 
  226     KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
  227                    lf->filename));
  228 
  229     if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
  230         return;
  231     for (oidp = start; oidp < stop; oidp++)
  232         sysctl_register_oid(*oidp);
  233 }
  234 
  235 static void
  236 linker_file_unregister_sysctls(linker_file_t lf)
  237 {
  238     struct sysctl_oid **start, **stop, **oidp;
  239 
  240     KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
  241                    lf->filename));
  242 
  243     if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
  244         return;
  245     for (oidp = start; oidp < stop; oidp++)
  246         sysctl_unregister_oid(*oidp);
  247 }
  248 
  249 static int
  250 linker_file_register_modules(linker_file_t lf)
  251 {
  252     struct mod_metadata **start, **stop, **mdp;
  253     const moduledata_t *moddata;
  254     int             first_error, error;
  255 
  256     KLD_DPF(FILE, ("linker_file_register_modules: registering modules in %s\n",
  257                    lf->filename));
  258 
  259     if (linker_file_lookup_set(lf, "modmetadata_set", &start, &stop, NULL) != 0) {
  260         /*
  261          * This fallback should be unnecessary, but if we get booted
  262          * from boot2 instead of loader and we are missing our
  263          * metadata then we have to try the best we can.
  264          */
  265         if (lf == linker_kernel_file) {
  266             start = SET_BEGIN(modmetadata_set);
  267             stop = SET_LIMIT(modmetadata_set);
  268         } else
  269             return (0);
  270     }
  271     first_error = 0;
  272     for (mdp = start; mdp < stop; mdp++) {
  273         if ((*mdp)->md_type != MDT_MODULE)
  274             continue;
  275         moddata = (*mdp)->md_data;
  276         KLD_DPF(FILE, ("Registering module %s in %s\n", moddata->name, lf->filename));
  277         error = module_register(moddata, lf);
  278         if (error) {
  279             kprintf("Module %s failed to register: %d\n", moddata->name, error);
  280             if (first_error == 0)
  281                 first_error = error;
  282         }
  283     }
  284     return (first_error);
  285 }
  286 
  287 static void
  288 linker_init_kernel_modules(void)
  289 {
  290 
  291     linker_file_register_modules(linker_kernel_file);
  292 }
  293 
  294 SYSINIT(linker_kernel, SI_BOOT2_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0);
  295 
  296 int
  297 linker_load_file(const char *filename, linker_file_t *result)
  298 {
  299     linker_class_t lc;
  300     linker_file_t lf;
  301     int foundfile, error = 0;
  302 
  303     /* Refuse to load modules if securelevel raised */
  304     if (securelevel > 0 || kernel_mem_readonly)
  305         return EPERM; 
  306 
  307     lf = linker_find_file_by_name(filename);
  308     if (lf) {
  309         KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
  310         *result = lf;
  311         lf->refs++;
  312         goto out;
  313     }
  314 
  315     lf = NULL;
  316     foundfile = 0;
  317     TAILQ_FOREACH(lc, &classes, link) {
  318         KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
  319                        filename, lc->desc));
  320 
  321         error = lc->ops->load_file(filename, &lf);
  322         /*
  323          * If we got something other than ENOENT, then it exists but we cannot
  324          * load it for some other reason.
  325          */
  326         if (error != ENOENT)
  327             foundfile = 1;
  328         if (lf) {
  329             error = linker_file_register_modules(lf);
  330             if (error == EEXIST) {
  331                     linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */);
  332                     return (error);
  333             }
  334             linker_file_register_sysctls(lf);
  335             linker_file_sysinit(lf);
  336             lf->flags |= LINKER_FILE_LINKED;
  337             *result = lf;
  338             return (0);
  339         }
  340     }
  341     /*
  342      * Less than ideal, but tells the user whether it failed to load or
  343      * the module was not found.
  344      */
  345     if (foundfile) {
  346             /*
  347              * If the file type has not been recognized by the last try
  348              * printout a message before to fail.
  349              */
  350             if (error == ENOSYS)
  351                     kprintf("linker_load_file: Unsupported file type\n");
  352 
  353             /*
  354              * Format not recognized or otherwise unloadable.
  355              * When loading a module that is statically built into
  356              * the kernel EEXIST percolates back up as the return
  357              * value.  Preserve this so that apps can recognize this
  358              * special case.
  359              */
  360             if (error != EEXIST)
  361                     error = ENOEXEC;
  362     } else {
  363         error = ENOENT;         /* Nothing found */
  364     }
  365 
  366 out:
  367     return error;
  368 }
  369 
  370 
  371 linker_file_t
  372 linker_find_file_by_name(const char* filename)
  373 {
  374     linker_file_t lf = NULL;
  375     char *koname;
  376     int i;
  377 
  378     for (i = strlen(filename); i > 0 && filename[i-1] != '/'; --i)
  379         ;
  380     filename += i;
  381 
  382     koname = kmalloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
  383     ksprintf(koname, "%s.ko", filename);
  384 
  385     lockmgr(&lock, LK_SHARED);
  386     TAILQ_FOREACH(lf, &linker_files, link) {
  387         if (!strcmp(lf->filename, koname))
  388             break;
  389         if (!strcmp(lf->filename, filename))
  390             break;
  391     }
  392     lockmgr(&lock, LK_RELEASE);
  393 
  394     if (koname)
  395         kfree(koname, M_LINKER);
  396     return lf;
  397 }
  398 
  399 linker_file_t
  400 linker_find_file_by_id(int fileid)
  401 {
  402     linker_file_t lf = NULL;
  403 
  404     lockmgr(&lock, LK_SHARED);
  405     TAILQ_FOREACH(lf, &linker_files, link)
  406         if (lf->id == fileid)
  407             break;
  408     lockmgr(&lock, LK_RELEASE);
  409 
  410     return lf;
  411 }
  412 
  413 int
  414 linker_file_foreach(linker_predicate_t *predicate, void *context)
  415 {
  416     linker_file_t lf;
  417     int retval = 0;
  418 
  419     lockmgr(&lock, LK_SHARED);
  420     TAILQ_FOREACH(lf, &linker_files, link) {
  421         retval = predicate(lf, context);
  422         if (retval != 0)
  423             break;
  424     }
  425     lockmgr(&lock, LK_RELEASE);
  426     return (retval);
  427 }
  428 
  429 linker_file_t
  430 linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
  431 {
  432     linker_file_t lf = NULL;
  433     const char *filename;
  434 
  435     filename = rindex(pathname, '/');
  436     if (filename && filename[1])
  437         filename++;
  438     else
  439         filename = pathname;
  440 
  441     KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
  442     lockmgr(&lock, LK_EXCLUSIVE);
  443     lf = kmalloc(sizeof(struct linker_file), M_LINKER, M_WAITOK | M_ZERO);
  444     lf->refs = 1;
  445     lf->userrefs = 0;
  446     lf->flags = 0;
  447     lf->filename = linker_strdup(filename);
  448     lf->id = next_file_id++;
  449     lf->ndeps = 0;
  450     lf->deps = NULL;
  451     STAILQ_INIT(&lf->common);
  452     TAILQ_INIT(&lf->modules);
  453 
  454     lf->priv = priv;
  455     lf->ops = ops;
  456     TAILQ_INSERT_TAIL(&linker_files, lf, link);
  457 
  458     lockmgr(&lock, LK_RELEASE);
  459     return lf;
  460 }
  461 
  462 int
  463 linker_file_unload(linker_file_t file)
  464 {
  465     module_t mod, next;
  466     modlist_t ml, nextml;
  467     struct common_symbol* cp;
  468     int error = 0;
  469     int i;
  470 
  471     /* Refuse to unload modules if securelevel raised */
  472     if (securelevel > 0 || kernel_mem_readonly)
  473         return EPERM; 
  474 
  475     KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
  476 
  477     lockmgr(&lock, LK_EXCLUSIVE);
  478 
  479     /* Easy case of just dropping a reference. */
  480     if (file->refs > 1) {
  481             file->refs--;
  482             lockmgr(&lock, LK_RELEASE);
  483             return (0);
  484     }
  485 
  486     KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
  487 
  488     /*
  489      * Inform any modules associated with this file.
  490      */
  491     mod = TAILQ_FIRST(&file->modules);
  492     for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
  493         next = module_getfnext(mod);
  494 
  495         /*
  496          * Give the module a chance to veto the unload.  Note that the
  497          * act of unloading the module may cause other modules in the
  498          * same file list to be unloaded recursively.
  499          */
  500         if ((error = module_unload(mod)) != 0) {
  501             KLD_DPF(FILE, ("linker_file_unload: module %p vetoes unload\n",
  502                            mod));
  503             lockmgr(&lock, LK_RELEASE);
  504             file->refs--;
  505             goto out;
  506         }
  507         module_release(mod);
  508     }
  509 
  510     TAILQ_FOREACH_MUTABLE(ml, &found_modules, link, nextml) {
  511         if (ml->container == file) {
  512             TAILQ_REMOVE(&found_modules, ml, link);
  513             kfree(ml, M_LINKER);
  514         }
  515     }
  516 
  517     /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
  518     if (file->flags & LINKER_FILE_LINKED) {
  519         file->flags &= ~LINKER_FILE_LINKED;
  520         lockmgr(&lock, LK_RELEASE);
  521         linker_file_sysuninit(file);
  522         linker_file_unregister_sysctls(file);
  523         lockmgr(&lock, LK_EXCLUSIVE);
  524     }
  525 
  526     TAILQ_REMOVE(&linker_files, file, link);
  527 
  528     if (file->deps) {
  529         lockmgr(&lock, LK_RELEASE);
  530         for (i = 0; i < file->ndeps; i++)
  531             linker_file_unload(file->deps[i]);
  532         lockmgr(&lock, LK_EXCLUSIVE);
  533         kfree(file->deps, M_LINKER);
  534         file->deps = NULL;
  535     }
  536 
  537     while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
  538         STAILQ_REMOVE_HEAD(&file->common, link);
  539         kfree(cp, M_LINKER);
  540     }
  541 
  542     file->ops->unload(file);
  543 
  544     if (file->filename) {
  545         kfree(file->filename, M_LINKER);
  546         file->filename = NULL;
  547     }
  548 
  549     kfree(file, M_LINKER);
  550 
  551     lockmgr(&lock, LK_RELEASE);
  552 
  553 out:
  554     return error;
  555 }
  556 
  557 void
  558 linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
  559 {
  560     linker_file_t* newdeps;
  561 
  562     newdeps = kmalloc((file->ndeps + 1) * sizeof(linker_file_t*),
  563                      M_LINKER, M_WAITOK | M_ZERO);
  564 
  565     if (file->deps) {
  566         bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
  567         kfree(file->deps, M_LINKER);
  568     }
  569     file->deps = newdeps;
  570     file->deps[file->ndeps] = dep;
  571     file->ndeps++;
  572 }
  573 
  574 /*
  575  * Locate a linker set and its contents.
  576  * This is a helper function to avoid linker_if.h exposure elsewhere.
  577  * Note: firstp and lastp are really void ***
  578  */
  579 int
  580 linker_file_lookup_set(linker_file_t file, const char *name,
  581                       void *firstp, void *lastp, int *countp)
  582 {
  583     return file->ops->lookup_set(file, name, firstp, lastp, countp);
  584 }
  585 
  586 int
  587 linker_file_lookup_symbol(linker_file_t file, const char* name, int deps, caddr_t *raddr)
  588 {
  589     c_linker_sym_t sym;
  590     linker_symval_t symval;
  591     linker_file_t lf;
  592     size_t common_size = 0;
  593     int i;
  594 
  595     KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
  596                   file, name, deps));
  597 
  598     if (file->ops->lookup_symbol(file, name, &sym) == 0) {
  599         file->ops->symbol_values(file, sym, &symval);
  600 
  601         /*
  602          * XXX Assume a common symbol if its value is 0 and it has a non-zero
  603          * size, otherwise it could be an absolute symbol with a value of 0.
  604          */
  605         if (symval.value == NULL && symval.size != 0) {
  606             /*
  607              * For commons, first look them up in the dependancies and
  608              * only allocate space if not found there.
  609              */
  610             common_size = symval.size;
  611         } else {
  612             KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%p\n", symval.value));
  613             *raddr = symval.value;
  614             return 0;
  615         }
  616     }
  617     if (deps) {
  618         for (i = 0; i < file->ndeps; i++) {
  619             if (linker_file_lookup_symbol(file->deps[i], name, 0, raddr) == 0) {
  620                 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%p\n", *raddr));
  621                 return 0;
  622             }
  623         }
  624 
  625         /* If we have not found it in the dependencies, search globally */
  626         TAILQ_FOREACH(lf, &linker_files, link) {
  627             /* But skip the current file if it's on the list */
  628             if (lf == file)
  629                 continue;
  630             /* And skip the files we searched above */
  631             for (i = 0; i < file->ndeps; i++)
  632                 if (lf == file->deps[i])
  633                     break;
  634             if (i < file->ndeps)
  635                 continue;
  636             if (linker_file_lookup_symbol(lf, name, 0, raddr) == 0) {
  637                 KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%p\n", *raddr));
  638                 return 0;
  639             }
  640         }
  641     }
  642 
  643     if (common_size > 0) {
  644         /*
  645          * This is a common symbol which was not found in the
  646          * dependancies.  We maintain a simple common symbol table in
  647          * the file object.
  648          */
  649         struct common_symbol* cp;
  650 
  651         STAILQ_FOREACH(cp, &file->common, link)
  652             if (!strcmp(cp->name, name)) {
  653                 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%p\n", cp->address));
  654                 *raddr = cp->address;
  655                 return 0;
  656             }
  657 
  658         /*
  659          * Round the symbol size up to align.
  660          */
  661         common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
  662         cp = kmalloc(sizeof(struct common_symbol)
  663                     + common_size
  664                     + strlen(name) + 1,
  665                     M_LINKER, M_WAITOK | M_ZERO);
  666 
  667         cp->address = (caddr_t) (cp + 1);
  668         cp->name = cp->address + common_size;
  669         strcpy(cp->name, name);
  670         bzero(cp->address, common_size);
  671         STAILQ_INSERT_TAIL(&file->common, cp, link);
  672 
  673         KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%p\n", cp->address));
  674         *raddr = cp->address;
  675         return 0;
  676     }
  677 
  678 #ifdef _KERNEL_VIRTUAL
  679     *raddr = dlsym(RTLD_NEXT, name);
  680     if (*raddr != NULL) {
  681         KLD_DPF(SYM, ("linker_file_lookup_symbol: found dlsym=%p\n", *raddr));
  682         return 0;
  683     }
  684 #endif
  685 
  686     KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
  687     return ENOENT;
  688 }
  689 
  690 #ifdef DDB
  691 /*
  692  * DDB Helpers.  DDB has to look across multiple files with their own
  693  * symbol tables and string tables.
  694  *
  695  * Note that we do not obey list locking protocols here.  We really don't
  696  * need DDB to hang because somebody's got the lock held.  We'll take the
  697  * chance that the files list is inconsistant instead.
  698  */
  699 
  700 int
  701 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
  702 {
  703     linker_file_t lf;
  704 
  705     TAILQ_FOREACH(lf, &linker_files, link) {
  706         if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
  707             return 0;
  708     }
  709     return ENOENT;
  710 }
  711 
  712 int
  713 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
  714 {
  715     linker_file_t lf;
  716     u_long off = (uintptr_t)value;
  717     u_long diff, bestdiff;
  718     c_linker_sym_t best;
  719     c_linker_sym_t es;
  720 
  721     best = NULL;
  722     bestdiff = off;
  723     TAILQ_FOREACH(lf, &linker_files, link) {
  724         if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
  725             continue;
  726         if (es != NULL && diff < bestdiff) {
  727             best = es;
  728             bestdiff = diff;
  729         }
  730         if (bestdiff == 0)
  731             break;
  732     }
  733     if (best) {
  734         *sym = best;
  735         *diffp = bestdiff;
  736         return 0;
  737     } else {
  738         *sym = NULL;
  739         *diffp = off;
  740         return ENOENT;
  741     }
  742 }
  743 
  744 int
  745 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
  746 {
  747     linker_file_t lf;
  748 
  749     TAILQ_FOREACH(lf, &linker_files, link) {
  750         if (lf->ops->symbol_values(lf, sym, symval) == 0)
  751             return 0;
  752     }
  753     return ENOENT;
  754 }
  755 
  756 #endif
  757 
  758 /*
  759  * Syscalls.
  760  *
  761  * MPALMOSTSAFE
  762  */
  763 int
  764 sys_kldload(struct kldload_args *uap)
  765 {
  766     struct thread *td = curthread;
  767     char *file;
  768     char *kldname, *modname;
  769     linker_file_t lf;
  770     int error = 0;
  771 
  772     uap->sysmsg_result = -1;
  773 
  774     if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */
  775         return EPERM;
  776 
  777     if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
  778         return error;
  779 
  780     file = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
  781     if ((error = copyinstr(uap->file, file, MAXPATHLEN, NULL)) != 0)
  782         goto out;
  783 
  784     /*
  785      * If file does not contain a qualified name or any dot in it
  786      * (kldname.ko, or kldname.ver.ko) treat it as an interface
  787      * name.
  788      */
  789     if (index(file, '/') || index(file, '.')) {
  790         kldname = file;
  791         modname = NULL;
  792     } else {
  793         kldname = NULL;
  794         modname = file;
  795     }
  796 
  797     get_mplock();
  798     error = linker_load_module(kldname, modname, NULL, NULL, &lf);
  799     rel_mplock();
  800     if (error)
  801         goto out;
  802 
  803     lf->userrefs++;
  804     uap->sysmsg_result = lf->id;
  805 
  806 out:
  807     if (file)
  808         kfree(file, M_TEMP);
  809     return error;
  810 }
  811 
  812 /*
  813  * MPALMOSTSAFE
  814  */
  815 int
  816 sys_kldunload(struct kldunload_args *uap)
  817 {
  818     struct thread *td = curthread;
  819     linker_file_t lf;
  820     int error = 0;
  821 
  822     if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */
  823         return EPERM;
  824 
  825     if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
  826         return error;
  827 
  828     get_mplock();
  829     lf = linker_find_file_by_id(uap->fileid);
  830     if (lf) {
  831         KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
  832         if (lf->userrefs == 0) {
  833             kprintf("linkerunload: attempt to unload file that was loaded by the kernel\n");
  834             error = EBUSY;
  835             goto out;
  836         }
  837         lf->userrefs--;
  838         error = linker_file_unload(lf);
  839         if (error)
  840             lf->userrefs++;
  841     } else {
  842         error = ENOENT;
  843     }
  844 out:
  845     rel_mplock();
  846     return error;
  847 }
  848 
  849 /*
  850  * MPALMOSTSAFE
  851  */
  852 int
  853 sys_kldfind(struct kldfind_args *uap)
  854 {
  855     char *filename = NULL, *modulename;
  856     linker_file_t lf;
  857     int error;
  858 
  859     uap->sysmsg_result = -1;
  860 
  861     filename = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
  862     if ((error = copyinstr(uap->file, filename, MAXPATHLEN, NULL)) != 0)
  863         goto out;
  864 
  865     modulename = rindex(filename, '/');
  866     if (modulename == NULL)
  867         modulename = filename;
  868 
  869     get_mplock();
  870     lf = linker_find_file_by_name(modulename);
  871     if (lf)
  872         uap->sysmsg_result = lf->id;
  873     else
  874         error = ENOENT;
  875     rel_mplock();
  876 
  877 out:
  878     if (filename)
  879         kfree(filename, M_TEMP);
  880     return error;
  881 }
  882 
  883 /*
  884  * MPALMOSTSAFE
  885  */
  886 int
  887 sys_kldnext(struct kldnext_args *uap)
  888 {
  889     linker_file_t lf;
  890     int error = 0;
  891 
  892     get_mplock();
  893     if (uap->fileid == 0) {
  894             lf = TAILQ_FIRST(&linker_files);
  895     } else {
  896             lf = linker_find_file_by_id(uap->fileid);
  897             if (lf == NULL) {
  898                     error = ENOENT;
  899                     goto out;
  900             }
  901             lf = TAILQ_NEXT(lf, link);
  902     }
  903 
  904     /* Skip partially loaded files. */
  905     while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED)) {
  906             lf = TAILQ_NEXT(lf, link);
  907     }
  908 
  909     if (lf)
  910         uap->sysmsg_result = lf->id;
  911     else
  912         uap->sysmsg_result = 0;
  913 
  914 out:
  915     rel_mplock();
  916     return error;
  917 }
  918 
  919 /*
  920  * MPALMOSTSAFE
  921  */
  922 int
  923 sys_kldstat(struct kldstat_args *uap)
  924 {
  925     linker_file_t lf;
  926     int error = 0;
  927     int version;
  928     struct kld_file_stat* stat;
  929     int namelen;
  930 
  931     get_mplock();
  932     lf = linker_find_file_by_id(uap->fileid);
  933     if (!lf) {
  934         error = ENOENT;
  935         goto out;
  936     }
  937 
  938     stat = uap->stat;
  939 
  940     /*
  941      * Check the version of the user's structure.
  942      */
  943     if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
  944         goto out;
  945     if (version != sizeof(struct kld_file_stat)) {
  946         error = EINVAL;
  947         goto out;
  948     }
  949 
  950     namelen = strlen(lf->filename) + 1;
  951     if (namelen > MAXPATHLEN)
  952         namelen = MAXPATHLEN;
  953     if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
  954         goto out;
  955     if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
  956         goto out;
  957     if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
  958         goto out;
  959     if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
  960         goto out;
  961     if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
  962         goto out;
  963 
  964     uap->sysmsg_result = 0;
  965 
  966 out:
  967     rel_mplock();
  968     return error;
  969 }
  970 
  971 /*
  972  * MPALMOSTSAFE
  973  */
  974 int
  975 sys_kldfirstmod(struct kldfirstmod_args *uap)
  976 {
  977     linker_file_t lf;
  978     int error = 0;
  979 
  980     get_mplock();
  981     lf = linker_find_file_by_id(uap->fileid);
  982     if (lf) {
  983         if (TAILQ_FIRST(&lf->modules))
  984             uap->sysmsg_result = module_getid(TAILQ_FIRST(&lf->modules));
  985         else
  986             uap->sysmsg_result = 0;
  987     } else {
  988         error = ENOENT;
  989     }
  990     rel_mplock();
  991 
  992     return error;
  993 }
  994 
  995 /*
  996  * MPALMOSTSAFE
  997  */
  998 int
  999 sys_kldsym(struct kldsym_args *uap)
 1000 {
 1001     char *symstr = NULL;
 1002     c_linker_sym_t sym;
 1003     linker_symval_t symval;
 1004     linker_file_t lf;
 1005     struct kld_sym_lookup lookup;
 1006     int error = 0;
 1007 
 1008     get_mplock();
 1009     if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
 1010         goto out;
 1011     if (lookup.version != sizeof(lookup) || uap->cmd != KLDSYM_LOOKUP) {
 1012         error = EINVAL;
 1013         goto out;
 1014     }
 1015 
 1016     symstr = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
 1017     if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
 1018         goto out;
 1019 
 1020     if (uap->fileid != 0) {
 1021         lf = linker_find_file_by_id(uap->fileid);
 1022         if (lf == NULL) {
 1023             error = ENOENT;
 1024             goto out;
 1025         }
 1026         if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
 1027             lf->ops->symbol_values(lf, sym, &symval) == 0) {
 1028             lookup.symvalue = (uintptr_t)symval.value;
 1029             lookup.symsize = symval.size;
 1030             error = copyout(&lookup, uap->data, sizeof(lookup));
 1031         } else
 1032             error = ENOENT;
 1033     } else {
 1034         TAILQ_FOREACH(lf, &linker_files, link) {
 1035             if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
 1036                 lf->ops->symbol_values(lf, sym, &symval) == 0) {
 1037                 lookup.symvalue = (uintptr_t)symval.value;
 1038                 lookup.symsize = symval.size;
 1039                 error = copyout(&lookup, uap->data, sizeof(lookup));
 1040                 break;
 1041             }
 1042         }
 1043         if (!lf)
 1044             error = ENOENT;
 1045     }
 1046 out:
 1047     rel_mplock();
 1048     if (symstr)
 1049         kfree(symstr, M_TEMP);
 1050     return error;
 1051 }
 1052 
 1053 /*
 1054  * Preloaded module support
 1055  */
 1056 
 1057 static modlist_t
 1058 modlist_lookup(const char *name, int ver)
 1059 {
 1060     modlist_t       mod;
 1061 
 1062     TAILQ_FOREACH(mod, &found_modules, link) {
 1063         if (strcmp(mod->name, name) == 0 && (ver == 0 || mod->version == ver))
 1064             return (mod);
 1065     }
 1066     return (NULL);
 1067 }
 1068 
 1069 static modlist_t
 1070 modlist_lookup2(const char *name, struct mod_depend *verinfo)
 1071 {
 1072     modlist_t       mod, bestmod;
 1073     int             ver;
 1074 
 1075     if (verinfo == NULL)
 1076         return (modlist_lookup(name, 0));
 1077     bestmod = NULL;
 1078     TAILQ_FOREACH(mod, &found_modules, link) {
 1079         if (strcmp(mod->name, name) != 0)
 1080             continue;
 1081         ver = mod->version;
 1082         if (ver == verinfo->md_ver_preferred)
 1083             return (mod);
 1084         if (ver >= verinfo->md_ver_minimum &&
 1085                 ver <= verinfo->md_ver_maximum &&
 1086                 (bestmod == NULL || ver > bestmod->version))
 1087             bestmod = mod;
 1088     }
 1089     return (bestmod);
 1090 }
 1091 
 1092 int
 1093 linker_reference_module(const char *modname, struct mod_depend *verinfo,
 1094     linker_file_t *result)
 1095 {
 1096     modlist_t mod;
 1097     int error;
 1098 
 1099     lockmgr(&lock, LK_SHARED);
 1100     if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
 1101         *result = mod->container;
 1102         (*result)->refs++;
 1103         lockmgr(&lock, LK_RELEASE);
 1104         return (0);
 1105     }
 1106 
 1107     lockmgr(&lock, LK_RELEASE);
 1108     get_mplock();
 1109     error = linker_load_module(NULL, modname, NULL, verinfo, result);
 1110     rel_mplock();
 1111     return (error);
 1112 }
 1113 
 1114 int
 1115 linker_release_module(const char *modname, struct mod_depend *verinfo,
 1116     linker_file_t lf)
 1117 {
 1118     modlist_t mod;
 1119     int error;
 1120 
 1121     lockmgr(&lock, LK_SHARED);
 1122     if (lf == NULL) {
 1123         KASSERT(modname != NULL,
 1124             ("linker_release_module: no file or name"));
 1125         mod = modlist_lookup2(modname, verinfo);
 1126         if (mod == NULL) {
 1127             lockmgr(&lock, LK_RELEASE);
 1128             return (ESRCH);
 1129         }
 1130         lf = mod->container;
 1131     } else
 1132         KASSERT(modname == NULL && verinfo == NULL,
 1133             ("linker_release_module: both file and name"));
 1134     lockmgr(&lock, LK_RELEASE);
 1135     get_mplock();
 1136     error = linker_file_unload(lf);
 1137     rel_mplock();
 1138     return (error);
 1139 }
 1140 
 1141 static modlist_t
 1142 modlist_newmodule(const char *modname, int version, linker_file_t container)
 1143 {
 1144     modlist_t       mod;
 1145 
 1146     mod = kmalloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
 1147     if (mod == NULL)
 1148         panic("no memory for module list");
 1149     mod->container = container;
 1150     mod->name = modname;
 1151     mod->version = version;
 1152     TAILQ_INSERT_TAIL(&found_modules, mod, link);
 1153     return (mod);
 1154 }
 1155 
 1156 static void
 1157 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
 1158                   struct mod_metadata **stop, int preload)
 1159 {
 1160     struct mod_metadata *mp, **mdp;
 1161     const char     *modname;
 1162     int             ver;
 1163 
 1164     for (mdp = start; mdp < stop; mdp++) {
 1165         mp = *mdp;
 1166         if (mp->md_type != MDT_VERSION)
 1167             continue;
 1168         modname = mp->md_cval;
 1169         ver = ((struct mod_version *)mp->md_data)->mv_version;
 1170         if (modlist_lookup(modname, ver) != NULL) {
 1171             kprintf("module %s already present!\n", modname);
 1172             /* XXX what can we do? this is a build error. :-( */
 1173             continue;
 1174         }
 1175         modlist_newmodule(modname, ver, lf);
 1176     }
 1177 }
 1178 
 1179 static void
 1180 linker_preload(void* arg)
 1181 {
 1182     caddr_t             modptr;
 1183     const char          *modname, *nmodname;
 1184     char                *modtype;
 1185     linker_file_t       lf, nlf;
 1186     linker_class_t      lc;
 1187     int                 error;
 1188     linker_file_list_t loaded_files;
 1189     linker_file_list_t depended_files;
 1190     struct mod_metadata *mp, *nmp;
 1191     struct mod_metadata **start, **stop, **mdp, **nmdp;
 1192     struct mod_depend *verinfo;
 1193     int nver;
 1194     int resolves;
 1195     modlist_t mod;
 1196     struct sysinit      **si_start, **si_stop;
 1197 
 1198     TAILQ_INIT(&loaded_files);
 1199     TAILQ_INIT(&depended_files);
 1200     TAILQ_INIT(&found_modules);
 1201 
 1202     modptr = NULL;
 1203     while ((modptr = preload_search_next_name(modptr)) != NULL) {
 1204         modname = (char *)preload_search_info(modptr, MODINFO_NAME);
 1205         modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
 1206         if (modname == NULL) {
 1207             kprintf("Preloaded module at %p does not have a name!\n", modptr);
 1208             continue;
 1209         }
 1210         if (modtype == NULL) {
 1211             kprintf("Preloaded module at %p does not have a type!\n", modptr);
 1212             continue;
 1213         }
 1214 
 1215         if (bootverbose)
 1216                 kprintf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
 1217         lf = NULL;
 1218         TAILQ_FOREACH(lc, &classes, link) {
 1219             error = lc->ops->preload_file(modname, &lf);
 1220             if (!error)
 1221                 break;
 1222             lf = NULL;
 1223         }
 1224         if (lf)
 1225                 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
 1226     }
 1227 
 1228     /*
 1229      * First get a list of stuff in the kernel.
 1230      */
 1231     if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
 1232                                &stop, NULL) == 0)
 1233         linker_addmodules(linker_kernel_file, start, stop, 1);
 1234 
 1235     /*
 1236      * This is a once-off kinky bubble sort to resolve relocation
 1237      * dependency requirements.
 1238      */
 1239 restart:
 1240     TAILQ_FOREACH(lf, &loaded_files, loaded) {
 1241         error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL);
 1242         /*
 1243          * First, look to see if we would successfully link with this
 1244          * stuff.
 1245          */
 1246         resolves = 1;           /* unless we know otherwise */
 1247         if (!error) {
 1248             for (mdp = start; mdp < stop; mdp++) {
 1249                 mp = *mdp;
 1250                 if (mp->md_type != MDT_DEPEND)
 1251                     continue;
 1252                 modname = mp->md_cval;
 1253                 verinfo = mp->md_data;
 1254                 for (nmdp = start; nmdp < stop; nmdp++) {
 1255                     nmp = *nmdp;
 1256                     if (nmp->md_type != MDT_VERSION)
 1257                         continue;
 1258                     nmodname = nmp->md_cval;
 1259                     if (strcmp(modname, nmodname) == 0)
 1260                         break;
 1261                 }
 1262                 if (nmdp < stop)/* it's a self reference */
 1263                     continue;
 1264 
 1265                 /*
 1266                  * ok, the module isn't here yet, we
 1267                  * are not finished
 1268                  */
 1269                 if (modlist_lookup2(modname, verinfo) == NULL)
 1270                     resolves = 0;
 1271             }
 1272         }
 1273         /*
 1274          * OK, if we found our modules, we can link.  So, "provide"
 1275          * the modules inside and add it to the end of the link order
 1276          * list.
 1277          */
 1278         if (resolves) {
 1279             if (!error) {
 1280                 for (mdp = start; mdp < stop; mdp++) {
 1281                     mp = *mdp;
 1282                     if (mp->md_type != MDT_VERSION)
 1283                         continue;
 1284                     modname = mp->md_cval;
 1285                     nver = ((struct mod_version *)mp->md_data)->mv_version;
 1286                     if (modlist_lookup(modname, nver) != NULL) {
 1287                         kprintf("module %s already present!\n", modname);
 1288                         TAILQ_REMOVE(&loaded_files, lf, loaded);
 1289                         linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */ );
 1290                         /* we changed tailq next ptr */
 1291                         goto restart;
 1292                     }
 1293                     modlist_newmodule(modname, nver, lf);
 1294                 }
 1295             }
 1296             TAILQ_REMOVE(&loaded_files, lf, loaded);
 1297             TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
 1298             /*
 1299              * Since we provided modules, we need to restart the
 1300              * sort so that the previous files that depend on us
 1301              * have a chance. Also, we've busted the tailq next
 1302              * pointer with the REMOVE.
 1303              */
 1304             goto restart;
 1305         }
 1306     }
 1307 
 1308     /*
 1309      * At this point, we check to see what could not be resolved..
 1310      */
 1311     while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
 1312         TAILQ_REMOVE(&loaded_files, lf, loaded);
 1313         kprintf("KLD file %s is missing dependencies\n", lf->filename);
 1314         linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */ );
 1315     }
 1316 
 1317     /*
 1318      * We made it. Finish off the linking in the order we determined.
 1319      */
 1320     TAILQ_FOREACH_MUTABLE(lf, &depended_files, loaded, nlf) {
 1321         if (linker_kernel_file) {
 1322             linker_kernel_file->refs++;
 1323             linker_file_add_dependancy(lf, linker_kernel_file);
 1324         }
 1325         lf->userrefs++;
 1326 
 1327         error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL);
 1328         if (!error) {
 1329             for (mdp = start; mdp < stop; mdp++) {
 1330                 mp = *mdp;
 1331                 if (mp->md_type != MDT_DEPEND)
 1332                     continue;
 1333                 modname = mp->md_cval;
 1334                 verinfo = mp->md_data;
 1335                 mod = modlist_lookup2(modname, verinfo);
 1336                 /* Don't count self-dependencies */
 1337                 if (lf == mod->container)
 1338                     continue;
 1339                 mod->container->refs++;
 1340                 linker_file_add_dependancy(lf, mod->container);
 1341             }
 1342         }
 1343         /*
 1344          * Now do relocation etc using the symbol search paths
 1345          * established by the dependencies
 1346          */
 1347         error = lf->ops->preload_finish(lf);
 1348         if (error) {
 1349             TAILQ_REMOVE(&depended_files, lf, loaded);
 1350             kprintf("KLD file %s - could not finalize loading\n",
 1351                     lf->filename);
 1352             linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */);
 1353             continue;
 1354         }
 1355         linker_file_register_modules(lf);
 1356         if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0)
 1357             sysinit_add(si_start, si_stop);
 1358         linker_file_register_sysctls(lf);
 1359         lf->flags |= LINKER_FILE_LINKED;
 1360     }
 1361     /* woohoo! we made it! */
 1362 }
 1363 
 1364 SYSINIT(preload, SI_BOOT2_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
 1365 
 1366 /*
 1367  * Search for a not-loaded module by name.
 1368  *
 1369  * Modules may be found in the following locations:
 1370  *
 1371  * - preloaded (result is just the module name)
 1372  * - on disk (result is full path to module)
 1373  *
 1374  * If the module name is qualified in any way (contains path, etc.)
 1375  * the we simply return a copy of it.
 1376  *
 1377  * The search path can be manipulated via sysctl.  Note that we use the ';'
 1378  * character as a separator to be consistent with the bootloader.
 1379  */
 1380 
 1381 static char linker_path[MAXPATHLEN] = "/boot;/boot/modules;/;/modules";
 1382 
 1383 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
 1384               sizeof(linker_path), "module load search path");
 1385 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
 1386 
 1387 char *
 1388 linker_search_path(const char *name)
 1389 {
 1390     struct nlookupdata  nd;
 1391     char                *cp, *ep, *result;
 1392     size_t              name_len, prefix_len;
 1393     size_t              result_len;
 1394     int                 sep;
 1395     int                 error;
 1396     enum vtype          type;
 1397     const char *exts[] = { "", ".ko", NULL };
 1398     const char **ext;
 1399 
 1400     /* qualified at all? */
 1401     if (index(name, '/'))
 1402         return(linker_strdup(name));
 1403 
 1404     /* traverse the linker path */
 1405     cp = linker_path;
 1406     name_len = strlen(name);
 1407     for (;;) {
 1408 
 1409         /* find the end of this component */
 1410         for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
 1411             ;
 1412         prefix_len = ep - cp;
 1413         /* if this component doesn't end with a slash, add one */
 1414         if (ep == cp || *(ep - 1) != '/')
 1415             sep = 1;
 1416         else
 1417             sep = 0;
 1418 
 1419         /*
 1420          * +2+3 : possible separator, plus terminator + possible extension.
 1421          */
 1422         result = kmalloc(prefix_len + name_len + 2+3, M_LINKER, M_WAITOK);
 1423 
 1424         strncpy(result, cp, prefix_len);
 1425         if (sep)
 1426             result[prefix_len++] = '/';
 1427         strcpy(result + prefix_len, name);
 1428 
 1429         result_len = strlen(result);
 1430         for (ext = exts; *ext != NULL; ext++) {
 1431             strcpy(result + result_len, *ext);
 1432 
 1433             /*
 1434              * Attempt to open the file, and return the path if we succeed and it's
 1435              * a regular file.
 1436              */
 1437             error = nlookup_init(&nd, result, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
 1438             if (error == 0)
 1439                 error = vn_open(&nd, NULL, FREAD, 0);
 1440             if (error == 0) {
 1441                 type = nd.nl_open_vp->v_type;
 1442                 if (type == VREG) {
 1443                     nlookup_done(&nd);
 1444                     return (result);
 1445                 }
 1446             }
 1447             nlookup_done(&nd);
 1448         }
 1449 
 1450         kfree(result, M_LINKER);
 1451 
 1452         if (*ep == 0)
 1453             break;
 1454         cp = ep + 1;
 1455     }
 1456     return(NULL);
 1457 }
 1458 
 1459 /*
 1460  * Find a file which contains given module and load it, if "parent" is not
 1461  * NULL, register a reference to it.
 1462  */
 1463 static int
 1464 linker_load_module(const char *kldname, const char *modname,
 1465                    struct linker_file *parent, struct mod_depend *verinfo,
 1466                    struct linker_file **lfpp)
 1467 {
 1468     linker_file_t   lfdep;
 1469     const char     *filename;
 1470     char           *pathname;
 1471     int             error;
 1472 
 1473     if (modname == NULL) {
 1474         /*
 1475          * We have to load KLD
 1476          */
 1477         KASSERT(verinfo == NULL, ("linker_load_module: verinfo is not NULL"));
 1478         pathname = linker_search_path(kldname);
 1479     } else {
 1480         if (modlist_lookup2(modname, verinfo) != NULL)
 1481             return (EEXIST);
 1482         if (kldname != NULL)
 1483         {
 1484             pathname = linker_strdup(kldname);
 1485         }
 1486         else if (rootvnode == NULL)
 1487             pathname = NULL;
 1488         else
 1489         {
 1490             pathname = linker_search_path(modname);
 1491         }
 1492 #if 0
 1493         /*
 1494          * Need to find a KLD with required module
 1495          */
 1496         pathname = linker_search_module(modname,
 1497                                         strlen(modname), verinfo);
 1498 #endif
 1499     }
 1500     if (pathname == NULL)
 1501         return (ENOENT);
 1502 
 1503     /*
 1504      * Can't load more than one file with the same basename XXX:
 1505      * Actually it should be possible to have multiple KLDs with
 1506      * the same basename but different path because they can
 1507      * provide different versions of the same modules.
 1508      */
 1509     filename = rindex(pathname, '/');
 1510     if (filename == NULL)
 1511         filename = filename;
 1512     else
 1513         filename++;
 1514     if (linker_find_file_by_name(filename))
 1515         error = EEXIST;
 1516     else
 1517         do {
 1518             error = linker_load_file(pathname, &lfdep);
 1519             if (error)
 1520                 break;
 1521             if (modname && verinfo && modlist_lookup2(modname, verinfo) == NULL) {
 1522                 linker_file_unload(lfdep /* , LINKER_UNLOAD_FORCE */ );
 1523                 error = ENOENT;
 1524                 break;
 1525             }
 1526             if (parent) {
 1527                 linker_file_add_dependancy(parent, lfdep);
 1528             }
 1529             if (lfpp)
 1530                 *lfpp = lfdep;
 1531         } while (0);
 1532     kfree(pathname, M_LINKER);
 1533     return (error);
 1534 }
 1535 
 1536 /*
 1537  * This routine is responsible for finding dependencies of userland initiated
 1538  * kldload(2)'s of files.
 1539  */
 1540 int
 1541 linker_load_dependencies(linker_file_t lf)
 1542 {
 1543     linker_file_t   lfdep;
 1544     struct mod_metadata **start, **stop, **mdp, **nmdp;
 1545     struct mod_metadata *mp, *nmp;
 1546     struct mod_depend *verinfo;
 1547     modlist_t       mod;
 1548     const char     *modname, *nmodname;
 1549     int             ver, error = 0, count;
 1550 
 1551     /*
 1552      * All files are dependant on /kernel.
 1553      */
 1554     if (linker_kernel_file) {
 1555         linker_kernel_file->refs++;
 1556         linker_file_add_dependancy(lf, linker_kernel_file);
 1557     }
 1558     if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, &count) != 0)
 1559         return (0);
 1560     for (mdp = start; mdp < stop; mdp++) {
 1561         mp = *mdp;
 1562         if (mp->md_type != MDT_VERSION)
 1563             continue;
 1564         modname = mp->md_cval;
 1565         ver = ((struct mod_version *)mp->md_data)->mv_version;
 1566         mod = modlist_lookup(modname, ver);
 1567         if (mod != NULL) {
 1568             kprintf("interface %s.%d already present in the KLD '%s'!\n",
 1569                     modname, ver, mod->container->filename);
 1570             return (EEXIST);
 1571         }
 1572     }
 1573 
 1574     for (mdp = start; mdp < stop; mdp++) {
 1575         mp = *mdp;
 1576         if (mp->md_type != MDT_DEPEND)
 1577             continue;
 1578         modname = mp->md_cval;
 1579         verinfo = mp->md_data;
 1580         nmodname = NULL;
 1581         for (nmdp = start; nmdp < stop; nmdp++) {
 1582             nmp = *nmdp;
 1583             if (nmp->md_type != MDT_VERSION)
 1584                 continue;
 1585             nmodname = nmp->md_cval;
 1586             if (strcmp(modname, nmodname) == 0)
 1587                 break;
 1588         }
 1589         if (nmdp < stop)        /* early exit, it's a self reference */
 1590             continue;
 1591         mod = modlist_lookup2(modname, verinfo);
 1592         if (mod) {              /* woohoo, it's loaded already */
 1593             lfdep = mod->container;
 1594             lfdep->refs++;
 1595             linker_file_add_dependancy(lf, lfdep);
 1596             continue;
 1597         }
 1598         error = linker_load_module(NULL, modname, lf, verinfo, NULL);
 1599         if (error) {
 1600             kprintf("KLD %s: depends on %s - not available or version mismatch\n",
 1601                     lf->filename, modname);
 1602             break;
 1603         }
 1604     }
 1605 
 1606     if (error)
 1607         return (error);
 1608     linker_addmodules(lf, start, stop, 0);
 1609     return (error);
 1610 }

Cache object: a3375f390d06d16cf004629794ef8e89


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