2 * Copyright (c) 1997-2000 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include "opt_hwpmc_hooks.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/sysproto.h>
39 #include <sys/sysent.h>
43 #include <sys/mutex.h>
45 #include <sys/module.h>
46 #include <sys/mount.h>
47 #include <sys/linker.h>
48 #include <sys/eventhandler.h>
49 #include <sys/fcntl.h>
51 #include <sys/libkern.h>
52 #include <sys/namei.h>
53 #include <sys/vnode.h>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysctl.h>
59 #include <security/mac/mac_framework.h>
61 #include "linker_if.h"
64 #include <sys/pmckern.h>
69 SYSCTL_INT(_debug, OID_AUTO, kld_debug, CTLFLAG_RW | CTLFLAG_TUN,
70 &kld_debug, 0, "Set various levels of KLD debug");
71 TUNABLE_INT("debug.kld_debug", &kld_debug);
74 /* These variables are used by kernel debuggers to enumerate loaded files. */
75 const int kld_off_address = offsetof(struct linker_file, address);
76 const int kld_off_filename = offsetof(struct linker_file, filename);
77 const int kld_off_pathname = offsetof(struct linker_file, pathname);
78 const int kld_off_next = offsetof(struct linker_file, link.tqe_next);
81 * static char *linker_search_path(const char *name, struct mod_depend
84 static const char *linker_basename(const char *path);
87 * Find a currently loaded file given its filename.
89 static linker_file_t linker_find_file_by_name(const char* _filename);
92 * Find a currently loaded file given its file id.
94 static linker_file_t linker_find_file_by_id(int _fileid);
96 /* Metadata from the static kernel */
97 SET_DECLARE(modmetadata_set, struct mod_metadata);
99 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
101 linker_file_t linker_kernel_file;
103 static struct sx kld_sx; /* kernel linker lock */
106 * Load counter used by clients to determine if a linker file has been
107 * re-loaded. This counter is incremented for each file load.
111 static linker_class_list_t classes;
112 static linker_file_list_t linker_files;
113 static int next_file_id = 1;
114 static int linker_no_more_classes = 0;
116 #define LINKER_GET_NEXT_FILE_ID(a) do { \
117 linker_file_t lftmp; \
120 sx_assert(&kld_sx, SA_XLOCKED); \
122 TAILQ_FOREACH(lftmp, &linker_files, link) { \
123 if (next_file_id == lftmp->id) { \
128 (a) = next_file_id; \
132 /* XXX wrong name; we're looking at version provision tags here, not modules */
133 typedef TAILQ_HEAD(, modlist) modlisthead_t;
135 TAILQ_ENTRY(modlist) link; /* chain together all modules */
136 linker_file_t container;
140 typedef struct modlist *modlist_t;
141 static modlisthead_t found_modules;
143 static int linker_file_add_dependency(linker_file_t file,
145 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
146 const char* name, int deps);
147 static int linker_load_module(const char *kldname,
148 const char *modname, struct linker_file *parent,
149 struct mod_depend *verinfo, struct linker_file **lfpp);
150 static modlist_t modlist_lookup2(const char *name, struct mod_depend *verinfo);
153 linker_init(void *arg)
156 sx_init(&kld_sx, "kernel linker");
157 TAILQ_INIT(&classes);
158 TAILQ_INIT(&linker_files);
161 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
164 linker_stop_class_add(void *arg)
167 linker_no_more_classes = 1;
170 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
173 linker_add_class(linker_class_t lc)
177 * We disallow any class registration past SI_ORDER_ANY
178 * of SI_SUB_KLD. We bump the reference count to keep the
179 * ops from being freed.
181 if (linker_no_more_classes == 1)
183 kobj_class_compile((kobj_class_t) lc);
184 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
185 TAILQ_INSERT_TAIL(&classes, lc, link);
190 linker_file_sysinit(linker_file_t lf)
192 struct sysinit **start, **stop, **sipp, **xipp, *save;
194 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
197 sx_assert(&kld_sx, SA_XLOCKED);
199 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
202 * Perform a bubble sort of the system initialization objects by
203 * their subsystem (primary key) and order (secondary key).
205 * Since some things care about execution order, this is the operation
206 * which ensures continued function.
208 for (sipp = start; sipp < stop; sipp++) {
209 for (xipp = sipp + 1; xipp < stop; xipp++) {
210 if ((*sipp)->subsystem < (*xipp)->subsystem ||
211 ((*sipp)->subsystem == (*xipp)->subsystem &&
212 (*sipp)->order <= (*xipp)->order))
221 * Traverse the (now) ordered list of system initialization tasks.
222 * Perform each task, and continue on to the next task.
226 for (sipp = start; sipp < stop; sipp++) {
227 if ((*sipp)->subsystem == SI_SUB_DUMMY)
228 continue; /* skip dummy task(s) */
231 (*((*sipp)->func)) ((*sipp)->udata);
238 linker_file_sysuninit(linker_file_t lf)
240 struct sysinit **start, **stop, **sipp, **xipp, *save;
242 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
245 sx_assert(&kld_sx, SA_XLOCKED);
247 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
252 * Perform a reverse bubble sort of the system initialization objects
253 * by their subsystem (primary key) and order (secondary key).
255 * Since some things care about execution order, this is the operation
256 * which ensures continued function.
258 for (sipp = start; sipp < stop; sipp++) {
259 for (xipp = sipp + 1; xipp < stop; xipp++) {
260 if ((*sipp)->subsystem > (*xipp)->subsystem ||
261 ((*sipp)->subsystem == (*xipp)->subsystem &&
262 (*sipp)->order >= (*xipp)->order))
271 * Traverse the (now) ordered list of system initialization tasks.
272 * Perform each task, and continue on to the next task.
276 for (sipp = start; sipp < stop; sipp++) {
277 if ((*sipp)->subsystem == SI_SUB_DUMMY)
278 continue; /* skip dummy task(s) */
281 (*((*sipp)->func)) ((*sipp)->udata);
288 linker_file_register_sysctls(linker_file_t lf)
290 struct sysctl_oid **start, **stop, **oidp;
293 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
296 sx_assert(&kld_sx, SA_XLOCKED);
298 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
303 for (oidp = start; oidp < stop; oidp++)
304 sysctl_register_oid(*oidp);
310 linker_file_unregister_sysctls(linker_file_t lf)
312 struct sysctl_oid **start, **stop, **oidp;
314 KLD_DPF(FILE, ("linker_file_unregister_sysctls: unregistering SYSCTLs"
315 " for %s\n", lf->filename));
317 sx_assert(&kld_sx, SA_XLOCKED);
319 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
324 for (oidp = start; oidp < stop; oidp++)
325 sysctl_unregister_oid(*oidp);
331 linker_file_register_modules(linker_file_t lf)
333 struct mod_metadata **start, **stop, **mdp;
334 const moduledata_t *moddata;
335 int first_error, error;
337 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
338 " in %s\n", lf->filename));
340 sx_assert(&kld_sx, SA_XLOCKED);
342 if (linker_file_lookup_set(lf, "modmetadata_set", &start,
345 * This fallback should be unnecessary, but if we get booted
346 * from boot2 instead of loader and we are missing our
347 * metadata then we have to try the best we can.
349 if (lf == linker_kernel_file) {
350 start = SET_BEGIN(modmetadata_set);
351 stop = SET_LIMIT(modmetadata_set);
356 for (mdp = start; mdp < stop; mdp++) {
357 if ((*mdp)->md_type != MDT_MODULE)
359 moddata = (*mdp)->md_data;
360 KLD_DPF(FILE, ("Registering module %s in %s\n",
361 moddata->name, lf->filename));
362 error = module_register(moddata, lf);
364 printf("Module %s failed to register: %d\n",
365 moddata->name, error);
366 if (first_error == 0)
370 return (first_error);
374 linker_init_kernel_modules(void)
378 linker_file_register_modules(linker_kernel_file);
382 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
386 linker_load_file(const char *filename, linker_file_t *result)
390 int foundfile, error, modules;
392 /* Refuse to load modules if securelevel raised */
393 if (prison0.pr_securelevel > 0)
396 sx_assert(&kld_sx, SA_XLOCKED);
397 lf = linker_find_file_by_name(filename);
399 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
400 " incrementing refs\n", filename));
409 * We do not need to protect (lock) classes here because there is
410 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
411 * and there is no class deregistration mechanism at this time.
413 TAILQ_FOREACH(lc, &classes, link) {
414 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
416 error = LINKER_LOAD_FILE(lc, filename, &lf);
418 * If we got something other than ENOENT, then it exists but
419 * we cannot load it for some other reason.
424 error = linker_file_register_modules(lf);
425 if (error == EEXIST) {
426 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
429 modules = !TAILQ_EMPTY(&lf->modules);
430 linker_file_register_sysctls(lf);
431 linker_file_sysinit(lf);
432 lf->flags |= LINKER_FILE_LINKED;
435 * If all of the modules in this file failed
436 * to load, unload the file and return an
439 if (modules && TAILQ_EMPTY(&lf->modules)) {
440 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
443 EVENTHANDLER_INVOKE(kld_load, lf);
449 * Less than ideal, but tells the user whether it failed to load or
450 * the module was not found.
455 * If the file type has not been recognized by the last try
456 * printout a message before to fail.
459 printf("linker_load_file: Unsupported file type\n");
462 * Format not recognized or otherwise unloadable.
463 * When loading a module that is statically built into
464 * the kernel EEXIST percolates back up as the return
465 * value. Preserve this so that apps like sysinstall
466 * can recognize this special case and not post bogus
472 error = ENOENT; /* Nothing found */
477 linker_reference_module(const char *modname, struct mod_depend *verinfo,
478 linker_file_t *result)
484 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
485 *result = mod->container;
491 error = linker_load_module(NULL, modname, NULL, verinfo, result);
497 linker_release_module(const char *modname, struct mod_depend *verinfo,
505 KASSERT(modname != NULL,
506 ("linker_release_module: no file or name"));
507 mod = modlist_lookup2(modname, verinfo);
514 KASSERT(modname == NULL && verinfo == NULL,
515 ("linker_release_module: both file and name"));
516 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
522 linker_find_file_by_name(const char *filename)
527 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
528 sprintf(koname, "%s.ko", filename);
530 sx_assert(&kld_sx, SA_XLOCKED);
531 TAILQ_FOREACH(lf, &linker_files, link) {
532 if (strcmp(lf->filename, koname) == 0)
534 if (strcmp(lf->filename, filename) == 0)
537 free(koname, M_LINKER);
542 linker_find_file_by_id(int fileid)
546 sx_assert(&kld_sx, SA_XLOCKED);
547 TAILQ_FOREACH(lf, &linker_files, link)
548 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
554 linker_file_foreach(linker_predicate_t *predicate, void *context)
560 TAILQ_FOREACH(lf, &linker_files, link) {
561 retval = predicate(lf, context);
570 linker_make_file(const char *pathname, linker_class_t lc)
573 const char *filename;
576 sx_assert(&kld_sx, SA_XLOCKED);
577 filename = linker_basename(pathname);
579 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
580 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
586 lf->filename = strdup(filename, M_LINKER);
587 lf->pathname = strdup(pathname, M_LINKER);
588 LINKER_GET_NEXT_FILE_ID(lf->id);
591 lf->loadcnt = ++loadcnt;
592 STAILQ_INIT(&lf->common);
593 TAILQ_INIT(&lf->modules);
594 TAILQ_INSERT_TAIL(&linker_files, lf, link);
599 linker_file_unload(linker_file_t file, int flags)
602 modlist_t ml, nextml;
603 struct common_symbol *cp;
606 /* Refuse to unload modules if securelevel raised. */
607 if (prison0.pr_securelevel > 0)
610 sx_assert(&kld_sx, SA_XLOCKED);
611 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
613 /* Easy case of just dropping a reference. */
614 if (file->refs > 1) {
619 /* Give eventhandlers a chance to prevent the unload. */
621 EVENTHANDLER_INVOKE(kld_unload_try, file, &error);
625 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
626 " informing modules\n"));
629 * Quiesce all the modules to give them a chance to veto the unload.
632 for (mod = TAILQ_FIRST(&file->modules); mod;
633 mod = module_getfnext(mod)) {
635 error = module_quiesce(mod);
636 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
637 KLD_DPF(FILE, ("linker_file_unload: module %s"
638 " vetoed unload\n", module_getname(mod)));
640 * XXX: Do we need to tell all the quiesced modules
641 * that they can resume work now via a new module
651 * Inform any modules associated with this file that they are
655 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
656 next = module_getfnext(mod);
660 * Give the module a chance to veto the unload.
662 if ((error = module_unload(mod)) != 0) {
665 KLD_DPF(FILE, ("linker_file_unload: module %s"
666 " failed unload\n", module_getname(mod)));
676 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
677 if (ml->container == file) {
678 TAILQ_REMOVE(&found_modules, ml, link);
684 * Don't try to run SYSUNINITs if we are unloaded due to a
687 if (file->flags & LINKER_FILE_LINKED) {
688 file->flags &= ~LINKER_FILE_LINKED;
689 linker_file_sysuninit(file);
690 linker_file_unregister_sysctls(file);
692 TAILQ_REMOVE(&linker_files, file, link);
695 for (i = 0; i < file->ndeps; i++)
696 linker_file_unload(file->deps[i], flags);
697 free(file->deps, M_LINKER);
700 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
701 STAILQ_REMOVE_HEAD(&file->common, link);
707 EVENTHANDLER_INVOKE(kld_unload, file->filename, file->address,
710 if (file->filename) {
711 free(file->filename, M_LINKER);
712 file->filename = NULL;
714 if (file->pathname) {
715 free(file->pathname, M_LINKER);
716 file->pathname = NULL;
718 kobj_delete((kobj_t) file, M_LINKER);
723 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
725 return (LINKER_CTF_GET(file, lc));
729 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
731 linker_file_t *newdeps;
733 sx_assert(&kld_sx, SA_XLOCKED);
734 file->deps = realloc(file->deps, (file->ndeps + 1) * sizeof(*newdeps),
735 M_LINKER, M_WAITOK | M_ZERO);
736 file->deps[file->ndeps] = dep;
738 KLD_DPF(FILE, ("linker_file_add_dependency:"
739 " adding %s as dependency for %s\n",
740 dep->filename, file->filename));
745 * Locate a linker set and its contents. This is a helper function to avoid
746 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
747 * This function is used in this file so we can avoid having lots of (void **)
751 linker_file_lookup_set(linker_file_t file, const char *name,
752 void *firstp, void *lastp, int *countp)
755 sx_assert(&kld_sx, SA_LOCKED);
756 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
760 * List all functions in a file.
763 linker_file_function_listall(linker_file_t lf,
764 linker_function_nameval_callback_t callback_func, void *arg)
766 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
770 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
775 locked = sx_xlocked(&kld_sx);
778 sym = linker_file_lookup_symbol_internal(file, name, deps);
785 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
789 linker_symval_t symval;
791 size_t common_size = 0;
794 sx_assert(&kld_sx, SA_XLOCKED);
795 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
798 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
799 LINKER_SYMBOL_VALUES(file, sym, &symval);
800 if (symval.value == 0)
802 * For commons, first look them up in the
803 * dependencies and only allocate space if not found
806 common_size = symval.size;
808 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
809 ".value=%p\n", symval.value));
810 return (symval.value);
814 for (i = 0; i < file->ndeps; i++) {
815 address = linker_file_lookup_symbol_internal(
816 file->deps[i], name, 0);
818 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
819 " deps value=%p\n", address));
824 if (common_size > 0) {
826 * This is a common symbol which was not found in the
827 * dependencies. We maintain a simple common symbol table in
830 struct common_symbol *cp;
832 STAILQ_FOREACH(cp, &file->common, link) {
833 if (strcmp(cp->name, name) == 0) {
834 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
835 " old common value=%p\n", cp->address));
836 return (cp->address);
840 * Round the symbol size up to align.
842 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
843 cp = malloc(sizeof(struct common_symbol)
844 + common_size + strlen(name) + 1, M_LINKER,
846 cp->address = (caddr_t)(cp + 1);
847 cp->name = cp->address + common_size;
848 strcpy(cp->name, name);
849 bzero(cp->address, common_size);
850 STAILQ_INSERT_TAIL(&file->common, cp, link);
852 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
853 " value=%p\n", cp->address));
854 return (cp->address);
856 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
861 * Both DDB and stack(9) rely on the kernel linker to provide forward and
862 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
863 * do this in a lockfree manner. We provide a set of internal helper
864 * routines to perform these operations without locks, and then wrappers that
867 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
871 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
875 TAILQ_FOREACH(lf, &linker_files, link) {
876 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
884 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
887 c_linker_sym_t best, es;
888 u_long diff, bestdiff, off;
891 off = (uintptr_t)value;
893 TAILQ_FOREACH(lf, &linker_files, link) {
894 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
896 if (es != 0 && diff < bestdiff) {
915 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
919 TAILQ_FOREACH(lf, &linker_files, link) {
920 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
927 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
930 linker_symval_t symval;
935 error = linker_debug_search_symbol(value, &sym, offset);
938 error = linker_debug_symbol_values(sym, &symval);
941 strlcpy(buf, symval.name, buflen);
946 * DDB Helpers. DDB has to look across multiple files with their own symbol
947 * tables and string tables.
949 * Note that we do not obey list locking protocols here. We really don't need
950 * DDB to hang because somebody's got the lock held. We'll take the chance
951 * that the files list is inconsistant instead.
955 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
958 return (linker_debug_lookup(symstr, sym));
963 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
966 return (linker_debug_search_symbol(value, sym, diffp));
970 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
973 return (linker_debug_symbol_values(sym, symval));
977 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
981 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
985 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
986 * obey locking protocols, and offer a significantly less complex interface.
989 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
995 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
1004 kern_kldload(struct thread *td, const char *file, int *fileid)
1006 const char *kldname, *modname;
1010 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1013 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1017 * It is possible that kldloaded module will attach a new ifnet,
1018 * so vnet context must be set when this ocurs.
1020 CURVNET_SET(TD_TO_VNET(td));
1023 * If file does not contain a qualified name or any dot in it
1024 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1027 if (strchr(file, '/') || strchr(file, '.')) {
1036 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1038 sx_xunlock(&kld_sx);
1044 sx_xunlock(&kld_sx);
1052 sys_kldload(struct thread *td, struct kldload_args *uap)
1054 char *pathname = NULL;
1057 td->td_retval[0] = -1;
1059 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1060 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1062 error = kern_kldload(td, pathname, &fileid);
1064 td->td_retval[0] = fileid;
1066 free(pathname, M_TEMP);
1071 kern_kldunload(struct thread *td, int fileid, int flags)
1076 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1079 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1082 CURVNET_SET(TD_TO_VNET(td));
1084 lf = linker_find_file_by_id(fileid);
1086 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1088 if (lf->userrefs == 0) {
1090 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1092 printf("kldunload: attempt to unload file that was"
1093 " loaded by the kernel\n");
1097 error = linker_file_unload(lf, flags);
1103 sx_xunlock(&kld_sx);
1110 sys_kldunload(struct thread *td, struct kldunload_args *uap)
1113 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1117 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1120 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1121 uap->flags != LINKER_UNLOAD_FORCE)
1123 return (kern_kldunload(td, uap->fileid, uap->flags));
1127 sys_kldfind(struct thread *td, struct kldfind_args *uap)
1130 const char *filename;
1135 error = mac_kld_check_stat(td->td_ucred);
1140 td->td_retval[0] = -1;
1142 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1143 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1146 filename = linker_basename(pathname);
1148 lf = linker_find_file_by_name(filename);
1150 td->td_retval[0] = lf->id;
1153 sx_xunlock(&kld_sx);
1155 free(pathname, M_TEMP);
1160 sys_kldnext(struct thread *td, struct kldnext_args *uap)
1166 error = mac_kld_check_stat(td->td_ucred);
1172 if (uap->fileid == 0)
1173 lf = TAILQ_FIRST(&linker_files);
1175 lf = linker_find_file_by_id(uap->fileid);
1180 lf = TAILQ_NEXT(lf, link);
1183 /* Skip partially loaded files. */
1184 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1185 lf = TAILQ_NEXT(lf, link);
1188 td->td_retval[0] = lf->id;
1190 td->td_retval[0] = 0;
1192 sx_xunlock(&kld_sx);
1197 sys_kldstat(struct thread *td, struct kldstat_args *uap)
1199 struct kld_file_stat stat;
1203 * Check the version of the user's structure.
1205 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
1208 if (version != sizeof(struct kld_file_stat_1) &&
1209 version != sizeof(struct kld_file_stat))
1212 error = kern_kldstat(td, uap->fileid, &stat);
1215 return (copyout(&stat, uap->stat, version));
1219 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
1226 error = mac_kld_check_stat(td->td_ucred);
1232 lf = linker_find_file_by_id(fileid);
1234 sx_xunlock(&kld_sx);
1238 /* Version 1 fields: */
1239 namelen = strlen(lf->filename) + 1;
1240 if (namelen > MAXPATHLEN)
1241 namelen = MAXPATHLEN;
1242 bcopy(lf->filename, &stat->name[0], namelen);
1243 stat->refs = lf->refs;
1245 stat->address = lf->address;
1246 stat->size = lf->size;
1247 /* Version 2 fields: */
1248 namelen = strlen(lf->pathname) + 1;
1249 if (namelen > MAXPATHLEN)
1250 namelen = MAXPATHLEN;
1251 bcopy(lf->pathname, &stat->pathname[0], namelen);
1252 sx_xunlock(&kld_sx);
1254 td->td_retval[0] = 0;
1259 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1266 error = mac_kld_check_stat(td->td_ucred);
1272 lf = linker_find_file_by_id(uap->fileid);
1275 mp = TAILQ_FIRST(&lf->modules);
1277 td->td_retval[0] = module_getid(mp);
1279 td->td_retval[0] = 0;
1283 sx_xunlock(&kld_sx);
1288 sys_kldsym(struct thread *td, struct kldsym_args *uap)
1290 char *symstr = NULL;
1292 linker_symval_t symval;
1294 struct kld_sym_lookup lookup;
1298 error = mac_kld_check_stat(td->td_ucred);
1303 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1305 if (lookup.version != sizeof(lookup) ||
1306 uap->cmd != KLDSYM_LOOKUP)
1308 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1309 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1312 if (uap->fileid != 0) {
1313 lf = linker_find_file_by_id(uap->fileid);
1316 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1317 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1318 lookup.symvalue = (uintptr_t) symval.value;
1319 lookup.symsize = symval.size;
1320 error = copyout(&lookup, uap->data, sizeof(lookup));
1324 TAILQ_FOREACH(lf, &linker_files, link) {
1325 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1326 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1327 lookup.symvalue = (uintptr_t)symval.value;
1328 lookup.symsize = symval.size;
1329 error = copyout(&lookup, uap->data,
1337 sx_xunlock(&kld_sx);
1339 free(symstr, M_TEMP);
1344 * Preloaded module support
1348 modlist_lookup(const char *name, int ver)
1352 TAILQ_FOREACH(mod, &found_modules, link) {
1353 if (strcmp(mod->name, name) == 0 &&
1354 (ver == 0 || mod->version == ver))
1361 modlist_lookup2(const char *name, struct mod_depend *verinfo)
1363 modlist_t mod, bestmod;
1366 if (verinfo == NULL)
1367 return (modlist_lookup(name, 0));
1369 TAILQ_FOREACH(mod, &found_modules, link) {
1370 if (strcmp(mod->name, name) != 0)
1373 if (ver == verinfo->md_ver_preferred)
1375 if (ver >= verinfo->md_ver_minimum &&
1376 ver <= verinfo->md_ver_maximum &&
1377 (bestmod == NULL || ver > bestmod->version))
1384 modlist_newmodule(const char *modname, int version, linker_file_t container)
1388 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1390 panic("no memory for module list");
1391 mod->container = container;
1392 mod->name = modname;
1393 mod->version = version;
1394 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1399 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1400 struct mod_metadata **stop, int preload)
1402 struct mod_metadata *mp, **mdp;
1403 const char *modname;
1406 for (mdp = start; mdp < stop; mdp++) {
1408 if (mp->md_type != MDT_VERSION)
1410 modname = mp->md_cval;
1411 ver = ((struct mod_version *)mp->md_data)->mv_version;
1412 if (modlist_lookup(modname, ver) != NULL) {
1413 printf("module %s already present!\n", modname);
1414 /* XXX what can we do? this is a build error. :-( */
1417 modlist_newmodule(modname, ver, lf);
1422 linker_preload(void *arg)
1425 const char *modname, *nmodname;
1427 linker_file_t lf, nlf;
1430 linker_file_list_t loaded_files;
1431 linker_file_list_t depended_files;
1432 struct mod_metadata *mp, *nmp;
1433 struct mod_metadata **start, **stop, **mdp, **nmdp;
1434 struct mod_depend *verinfo;
1438 struct sysinit **si_start, **si_stop;
1440 TAILQ_INIT(&loaded_files);
1441 TAILQ_INIT(&depended_files);
1442 TAILQ_INIT(&found_modules);
1447 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1448 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1449 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1450 if (modname == NULL) {
1451 printf("Preloaded module at %p does not have a"
1452 " name!\n", modptr);
1455 if (modtype == NULL) {
1456 printf("Preloaded module at %p does not have a type!\n",
1461 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1464 TAILQ_FOREACH(lc, &classes, link) {
1465 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1471 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1475 * First get a list of stuff in the kernel.
1477 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1479 linker_addmodules(linker_kernel_file, start, stop, 1);
1482 * This is a once-off kinky bubble sort to resolve relocation
1483 * dependency requirements.
1486 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1487 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1490 * First, look to see if we would successfully link with this
1493 resolves = 1; /* unless we know otherwise */
1495 for (mdp = start; mdp < stop; mdp++) {
1497 if (mp->md_type != MDT_DEPEND)
1499 modname = mp->md_cval;
1500 verinfo = mp->md_data;
1501 for (nmdp = start; nmdp < stop; nmdp++) {
1503 if (nmp->md_type != MDT_VERSION)
1505 nmodname = nmp->md_cval;
1506 if (strcmp(modname, nmodname) == 0)
1509 if (nmdp < stop) /* it's a self reference */
1513 * ok, the module isn't here yet, we
1516 if (modlist_lookup2(modname, verinfo) == NULL)
1521 * OK, if we found our modules, we can link. So, "provide"
1522 * the modules inside and add it to the end of the link order
1527 for (mdp = start; mdp < stop; mdp++) {
1529 if (mp->md_type != MDT_VERSION)
1531 modname = mp->md_cval;
1532 nver = ((struct mod_version *)
1533 mp->md_data)->mv_version;
1534 if (modlist_lookup(modname,
1536 printf("module %s already"
1537 " present!\n", modname);
1538 TAILQ_REMOVE(&loaded_files,
1540 linker_file_unload(lf,
1541 LINKER_UNLOAD_FORCE);
1542 /* we changed tailq next ptr */
1545 modlist_newmodule(modname, nver, lf);
1548 TAILQ_REMOVE(&loaded_files, lf, loaded);
1549 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1551 * Since we provided modules, we need to restart the
1552 * sort so that the previous files that depend on us
1553 * have a chance. Also, we've busted the tailq next
1554 * pointer with the REMOVE.
1561 * At this point, we check to see what could not be resolved..
1563 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1564 TAILQ_REMOVE(&loaded_files, lf, loaded);
1565 printf("KLD file %s is missing dependencies\n", lf->filename);
1566 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1570 * We made it. Finish off the linking in the order we determined.
1572 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1573 if (linker_kernel_file) {
1574 linker_kernel_file->refs++;
1575 error = linker_file_add_dependency(lf,
1576 linker_kernel_file);
1578 panic("cannot add dependency");
1580 lf->userrefs++; /* so we can (try to) kldunload it */
1581 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1584 for (mdp = start; mdp < stop; mdp++) {
1586 if (mp->md_type != MDT_DEPEND)
1588 modname = mp->md_cval;
1589 verinfo = mp->md_data;
1590 mod = modlist_lookup2(modname, verinfo);
1592 printf("KLD file %s - cannot find "
1593 "dependency \"%s\"\n",
1594 lf->filename, modname);
1597 /* Don't count self-dependencies */
1598 if (lf == mod->container)
1600 mod->container->refs++;
1601 error = linker_file_add_dependency(lf,
1604 panic("cannot add dependency");
1608 * Now do relocation etc using the symbol search paths
1609 * established by the dependencies
1611 error = LINKER_LINK_PRELOAD_FINISH(lf);
1613 printf("KLD file %s - could not finalize loading\n",
1617 linker_file_register_modules(lf);
1618 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1619 &si_stop, NULL) == 0)
1620 sysinit_add(si_start, si_stop);
1621 linker_file_register_sysctls(lf);
1622 lf->flags |= LINKER_FILE_LINKED;
1625 TAILQ_REMOVE(&depended_files, lf, loaded);
1626 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1628 sx_xunlock(&kld_sx);
1629 /* woohoo! we made it! */
1632 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1635 * Search for a not-loaded module by name.
1637 * Modules may be found in the following locations:
1639 * - preloaded (result is just the module name) - on disk (result is full path
1642 * If the module name is qualified in any way (contains path, etc.) the we
1643 * simply return a copy of it.
1645 * The search path can be manipulated via sysctl. Note that we use the ';'
1646 * character as a separator to be consistent with the bootloader.
1649 static char linker_hintfile[] = "linker.hints";
1650 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1652 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1653 sizeof(linker_path), "module load search path");
1655 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1657 static char *linker_ext_list[] = {
1664 * Check if file actually exists either with or without extension listed in
1665 * the linker_ext_list. (probably should be generic for the rest of the
1669 linker_lookup_file(const char *path, int pathlen, const char *name,
1670 int namelen, struct vattr *vap)
1672 struct nameidata nd;
1673 struct thread *td = curthread; /* XXX */
1674 char *result, **cpp, *sep;
1675 int error, len, extlen, reclen, flags;
1679 for (cpp = linker_ext_list; *cpp; cpp++) {
1684 extlen++; /* trailing '\0' */
1685 sep = (path[pathlen - 1] != '/') ? "/" : "";
1687 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1688 result = malloc(reclen, M_LINKER, M_WAITOK);
1689 for (cpp = linker_ext_list; *cpp; cpp++) {
1690 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1691 namelen, name, *cpp);
1693 * Attempt to open the file, and return the path if
1694 * we succeed and it's a regular file.
1696 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td);
1698 error = vn_open(&nd, &flags, 0, NULL);
1700 NDFREE(&nd, NDF_ONLY_PNBUF);
1701 type = nd.ni_vp->v_type;
1703 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1704 VOP_UNLOCK(nd.ni_vp, 0);
1705 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1710 free(result, M_LINKER);
1714 #define INT_ALIGN(base, ptr) ptr = \
1715 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1718 * Lookup KLD which contains requested module in the "linker.hints" file. If
1719 * version specification is available, then try to find the best KLD.
1720 * Otherwise just find the latest one.
1723 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1724 int modnamelen, struct mod_depend *verinfo)
1726 struct thread *td = curthread; /* XXX */
1727 struct ucred *cred = td ? td->td_ucred : NULL;
1728 struct nameidata nd;
1729 struct vattr vattr, mattr;
1730 u_char *hints = NULL;
1731 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1732 int error, ival, bestver, *intp, found, flags, clen, blen;
1736 bestver = found = 0;
1738 sep = (path[pathlen - 1] != '/') ? "/" : "";
1739 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1741 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1742 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1745 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td);
1747 error = vn_open(&nd, &flags, 0, NULL);
1750 NDFREE(&nd, NDF_ONLY_PNBUF);
1751 if (nd.ni_vp->v_type != VREG)
1754 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1758 * XXX: we need to limit this number to some reasonable value
1760 if (vattr.va_size > 100 * 1024) {
1761 printf("hints file too large %ld\n", (long)vattr.va_size);
1764 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1767 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1768 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1771 VOP_UNLOCK(nd.ni_vp, 0);
1772 vn_close(nd.ni_vp, FREAD, cred, td);
1775 printf("can't read %zd\n", reclen);
1778 intp = (int *)hints;
1780 if (ival != LINKER_HINTS_VERSION) {
1781 printf("hints file version mismatch %d\n", ival);
1784 bufend = hints + vattr.va_size;
1785 recptr = (u_char *)intp;
1787 while (recptr < bufend && !found) {
1788 intp = (int *)recptr;
1795 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1798 INT_ALIGN(hints, cp);
1802 if (verinfo == NULL ||
1803 ival == verinfo->md_ver_preferred) {
1807 if (ival >= verinfo->md_ver_minimum &&
1808 ival <= verinfo->md_ver_maximum &&
1818 recptr += reclen + sizeof(int);
1821 * Finally check if KLD is in the place
1824 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1826 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1829 * KLD is newer than hints file. What we should do now?
1831 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1832 printf("warning: KLD '%s' is newer than the linker.hints"
1835 free(pathbuf, M_LINKER);
1837 free(hints, M_TEMP);
1838 if (nd.ni_vp != NULL) {
1839 VOP_UNLOCK(nd.ni_vp, 0);
1840 vn_close(nd.ni_vp, FREAD, cred, td);
1843 * If nothing found or hints is absent - fallback to the old
1844 * way by using "kldname[.ko]" as module name.
1846 if (!found && !bestver && result == NULL)
1847 result = linker_lookup_file(path, pathlen, modname,
1853 * Lookup KLD which contains requested module in the all directories.
1856 linker_search_module(const char *modname, int modnamelen,
1857 struct mod_depend *verinfo)
1859 char *cp, *ep, *result;
1862 * traverse the linker path
1864 for (cp = linker_path; *cp; cp = ep + 1) {
1865 /* find the end of this component */
1866 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1867 result = linker_hints_lookup(cp, ep - cp, modname,
1868 modnamelen, verinfo);
1878 * Search for module in all directories listed in the linker_path.
1881 linker_search_kld(const char *name)
1883 char *cp, *ep, *result;
1886 /* qualified at all? */
1887 if (strchr(name, '/'))
1888 return (strdup(name, M_LINKER));
1890 /* traverse the linker path */
1892 for (ep = linker_path; *ep; ep++) {
1894 /* find the end of this component */
1895 for (; *ep != 0 && *ep != ';'; ep++);
1896 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1904 linker_basename(const char *path)
1906 const char *filename;
1908 filename = strrchr(path, '/');
1909 if (filename == NULL)
1918 * Inform hwpmc about the set of kernel modules currently loaded.
1921 linker_hwpmc_list_objects(void)
1924 struct pmckern_map_in *kobase;
1929 TAILQ_FOREACH(lf, &linker_files, link)
1932 /* Allocate nmappings + 1 entries. */
1933 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
1934 M_LINKER, M_WAITOK | M_ZERO);
1936 TAILQ_FOREACH(lf, &linker_files, link) {
1938 /* Save the info for this linker file. */
1939 kobase[i].pm_file = lf->filename;
1940 kobase[i].pm_address = (uintptr_t)lf->address;
1943 sx_sunlock(&kld_sx);
1945 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
1947 /* The last entry of the malloced area comprises of all zeros. */
1948 KASSERT(kobase[i].pm_file == NULL,
1949 ("linker_hwpmc_list_objects: last object not NULL"));
1951 return ((void *)kobase);
1956 * Find a file which contains given module and load it, if "parent" is not
1957 * NULL, register a reference to it.
1960 linker_load_module(const char *kldname, const char *modname,
1961 struct linker_file *parent, struct mod_depend *verinfo,
1962 struct linker_file **lfpp)
1964 linker_file_t lfdep;
1965 const char *filename;
1969 sx_assert(&kld_sx, SA_XLOCKED);
1970 if (modname == NULL) {
1972 * We have to load KLD
1974 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
1976 pathname = linker_search_kld(kldname);
1978 if (modlist_lookup2(modname, verinfo) != NULL)
1980 if (kldname != NULL)
1981 pathname = strdup(kldname, M_LINKER);
1982 else if (rootvnode == NULL)
1986 * Need to find a KLD with required module
1988 pathname = linker_search_module(modname,
1989 strlen(modname), verinfo);
1991 if (pathname == NULL)
1995 * Can't load more than one file with the same basename XXX:
1996 * Actually it should be possible to have multiple KLDs with
1997 * the same basename but different path because they can
1998 * provide different versions of the same modules.
2000 filename = linker_basename(pathname);
2001 if (linker_find_file_by_name(filename))
2004 error = linker_load_file(pathname, &lfdep);
2007 if (modname && verinfo &&
2008 modlist_lookup2(modname, verinfo) == NULL) {
2009 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2014 error = linker_file_add_dependency(parent, lfdep);
2021 free(pathname, M_LINKER);
2026 * This routine is responsible for finding dependencies of userland initiated
2027 * kldload(2)'s of files.
2030 linker_load_dependencies(linker_file_t lf)
2032 linker_file_t lfdep;
2033 struct mod_metadata **start, **stop, **mdp, **nmdp;
2034 struct mod_metadata *mp, *nmp;
2035 struct mod_depend *verinfo;
2037 const char *modname, *nmodname;
2038 int ver, error = 0, count;
2041 * All files are dependant on /kernel.
2043 sx_assert(&kld_sx, SA_XLOCKED);
2044 if (linker_kernel_file) {
2045 linker_kernel_file->refs++;
2046 error = linker_file_add_dependency(lf, linker_kernel_file);
2050 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2053 for (mdp = start; mdp < stop; mdp++) {
2055 if (mp->md_type != MDT_VERSION)
2057 modname = mp->md_cval;
2058 ver = ((struct mod_version *)mp->md_data)->mv_version;
2059 mod = modlist_lookup(modname, ver);
2061 printf("interface %s.%d already present in the KLD"
2062 " '%s'!\n", modname, ver,
2063 mod->container->filename);
2068 for (mdp = start; mdp < stop; mdp++) {
2070 if (mp->md_type != MDT_DEPEND)
2072 modname = mp->md_cval;
2073 verinfo = mp->md_data;
2075 for (nmdp = start; nmdp < stop; nmdp++) {
2077 if (nmp->md_type != MDT_VERSION)
2079 nmodname = nmp->md_cval;
2080 if (strcmp(modname, nmodname) == 0)
2083 if (nmdp < stop)/* early exit, it's a self reference */
2085 mod = modlist_lookup2(modname, verinfo);
2086 if (mod) { /* woohoo, it's loaded already */
2087 lfdep = mod->container;
2089 error = linker_file_add_dependency(lf, lfdep);
2094 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2096 printf("KLD %s: depends on %s - not available or"
2097 " version mismatch\n", lf->filename, modname);
2104 linker_addmodules(lf, start, stop, 0);
2109 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2111 struct sysctl_req *req;
2114 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2118 * Export a nul-separated, double-nul-terminated list of all function names
2122 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2128 error = mac_kld_check_stat(req->td->td_ucred);
2132 error = sysctl_wire_old_buffer(req, 0);
2136 TAILQ_FOREACH(lf, &linker_files, link) {
2137 error = LINKER_EACH_FUNCTION_NAME(lf,
2138 sysctl_kern_function_list_iterate, req);
2140 sx_xunlock(&kld_sx);
2144 sx_xunlock(&kld_sx);
2145 return (SYSCTL_OUT(req, "", 1));
2148 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLTYPE_OPAQUE | CTLFLAG_RD,
2149 NULL, 0, sysctl_kern_function_list, "", "kernel function list");